windows-file-versioning-20030619
[openafs.git] / src / WINNT / afssvrmgr / creds.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "svrmgr.h"
16 #include "creds.h"
17 #include "display.h"
18 #include "time.h"
19 #include "subset.h"
20
21
22 /*
23  * OPENCELL DIALOG ____________________________________________________________
24  *
25  */
26
27 void OpenCell_OnSubset (HWND hDlg)
28 {
29    BOOL fEnable;
30
31    fEnable = TRUE;
32    if (!IsWindowEnabled (GetDlgItem (hDlg, IDC_MON_ONE)))
33       fEnable = FALSE;
34    if (!IsDlgButtonChecked (hDlg, IDC_MON_ONE))
35       fEnable = FALSE;
36    EnableWindow (GetDlgItem (hDlg, IDC_MON_SERVER), fEnable);
37
38    fEnable = TRUE;
39    if (!IsWindowEnabled (GetDlgItem (hDlg, IDC_MON_SOME)))
40       fEnable = FALSE;
41    if (!IsDlgButtonChecked (hDlg, IDC_MON_SOME))
42       fEnable = FALSE;
43    EnableWindow (GetDlgItem (hDlg, IDC_MON_SUBSET), fEnable);
44 }
45
46
47 void OpenCell_Hook_Enable (HWND hDlg, BOOL fEnable)
48 {
49    EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
50    EnableWindow (GetDlgItem (hDlg, IDHELP), fEnable);
51    EnableWindow (GetDlgItem (hDlg, IDCANCEL), fEnable);
52    EnableWindow (GetDlgItem (hDlg, IDC_ADVANCED), fEnable);
53    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_CELL), fEnable);
54    EnableWindow (GetDlgItem (hDlg, IDC_MON_ALL), fEnable);
55    EnableWindow (GetDlgItem (hDlg, IDC_MON_ONE), fEnable);
56    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_ID), fEnable);
57    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_PASSWORD), fEnable);
58
59    if (fEnable)
60       {
61       BOOL fAnySubsets = !!SendDlgItemMessage (hDlg, IDC_MON_SUBSET, CB_GETCOUNT, 0, 0);
62       EnableWindow (GetDlgItem (hDlg, IDC_MON_SOME), fAnySubsets);
63       }
64    else
65       {
66       EnableWindow (GetDlgItem (hDlg, IDC_MON_SOME), FALSE);
67       }
68
69    OpenCell_OnSubset (hDlg);
70 }
71
72
73 void OpenCellDlg_Hook_OnOK (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
74 {
75    BOOL rc = FALSE;
76    OpenCell_Hook_Enable (hDlg, FALSE);
77    StartHourGlass ();
78
79    // Remember what cell the user chose to edit
80    //
81    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, lpp->szCell, cchNAME);
82
83    // Try to obtain the credentials specified by the user.
84    //
85    TCHAR szCell[ cchNAME ];
86    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
87
88    TCHAR szUser[ cchRESOURCE ];
89    GetDlgItemText (hDlg, IDC_OPENCELL_ID, szUser, cchNAME);
90
91    TCHAR szPassword[ cchRESOURCE ];
92    GetDlgItemText (hDlg, IDC_OPENCELL_PASSWORD, szPassword, cchNAME);
93
94    ULONG status;
95    if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
96       {
97       ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
98       }
99    else
100       {
101       // See if those credentials are sufficient
102       //
103       CHECKCREDS_PARAMS pp;
104       memset (&pp, 0x00, sizeof(pp));
105       memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
106       pp.bcdp.hParent = hDlg;
107       pp.hCreds = lpp->hCreds;
108       pp.fShowWarning = TRUE;
109
110       if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
111          {
112          SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
113          PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
114          }
115       }
116
117    if (rc)
118       {
119       g.hCreds = lpp->hCreds;
120
121       // Instead of closing the dialog, start an taskOPENCELL task;
122       // we'll wait for that to complete successfully before we'll
123       // close the dialog.
124       //
125       LPOPENCELL_PACKET lpocp = New (OPENCELL_PACKET);
126       memset (lpocp, 0x00, sizeof(OPENCELL_PACKET));
127       lstrcpy (lpocp->szCell, lpp->szCell);
128       lpocp->fCloseAppOnFail = FALSE;
129       lpocp->hCreds = lpp->hCreds;
130
131       if (IsDlgButtonChecked (hDlg, IDC_MON_ALL))
132          {
133          lpocp->sub = NULL;
134          }
135       else if (IsDlgButtonChecked (hDlg, IDC_MON_ONE))
136          {
137          TCHAR szServer[ cchNAME ];
138          GetDlgItemText (hDlg, IDC_MON_SERVER, szServer, cchNAME);
139          lpocp->sub = New (SUBSET);
140          memset (lpocp->sub, 0x0, sizeof(SUBSET));
141          if (szServer[0])
142             FormatMultiString (&lpocp->sub->pszMonitored, TRUE, TEXT("%1"), TEXT("%s"), szServer);
143          else
144             {
145             lpocp->sub->pszMonitored = AllocateString (2);
146             lpocp->sub->pszMonitored[0] = TEXT('\0');
147             lpocp->sub->pszMonitored[1] = TEXT('\0');
148             }
149          }
150       else // (IsDlgButtonChecked (hDlg, IDC_MON_SOME))
151          {
152          TCHAR szSubset[ cchNAME ];
153          GetDlgItemText (hDlg, IDC_MON_SUBSET, szSubset, cchNAME);
154          lpocp->sub = Subsets_LoadSubset (szCell, szSubset);
155          }
156
157       StartTask (taskOPENCELL, hDlg, lpocp);
158       }
159
160    if (!rc)
161       OpenCell_Hook_Enable (hDlg, TRUE);
162    StopHourGlass ();
163 }
164
165
166 void OpenCell_OnCellChange (HWND hDlg, BOOL fMoveCaret)
167 {
168    HWND hCombo = GetDlgItem (hDlg, IDC_MON_SUBSET);
169    CB_StartChange (hCombo, TRUE);
170    int iSel = 0;
171    BOOL fAddedAny = FALSE;
172
173    TCHAR szCell[ cchNAME ];
174    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
175
176    if (szCell[0] != TEXT('\0'))
177       {
178       BOOL fSelectedCurrentCell = FALSE;
179       if (g.lpiCell)
180          {
181          TCHAR szCurrentCell[ cchNAME ];
182          g.lpiCell->GetCellName (szCurrentCell);
183
184          if (!lstrcmpi (szCell, szCurrentCell))
185             fSelectedCurrentCell = TRUE;
186
187          if (g.sub && g.sub->pszMonitored)
188             SetDlgItemText (hDlg, IDC_MON_SERVER, g.sub->pszMonitored);
189          else
190             SetDlgItemText (hDlg, IDC_MON_SERVER, TEXT(""));
191          }
192
193       TCHAR szSubset[ cchNAME ];
194       for (size_t iIndex = 0; Subsets_EnumSubsets (szCell, iIndex, szSubset); ++iIndex)
195          {
196          CB_AddItem (hCombo, szSubset, (LPARAM)iIndex+1);
197          fAddedAny = TRUE;
198
199          if (fSelectedCurrentCell && !lstrcmpi (szSubset, g.sub->szSubset))
200             iSel = iIndex+1;
201          }
202       }
203
204    CB_EndChange (hCombo, (LPARAM)(iSel == 0) ? 1 : iSel);
205
206    if (fAddedAny && iSel)
207       {
208       CheckDlgButton (hDlg, IDC_MON_ALL, FALSE);
209       CheckDlgButton (hDlg, IDC_MON_ONE, FALSE);
210       CheckDlgButton (hDlg, IDC_MON_SOME, TRUE);
211       }
212    else if (!fAddedAny && IsDlgButtonChecked (hDlg, IDC_MON_SOME))
213       {
214       CheckDlgButton (hDlg, IDC_MON_ALL, FALSE);
215       CheckDlgButton (hDlg, IDC_MON_SOME, FALSE);
216       CheckDlgButton (hDlg, IDC_MON_ONE, TRUE);
217       }
218
219    BOOL fEnable = fAddedAny;
220    if (!IsWindowEnabled (GetDlgItem (hDlg, IDC_MON_ALL)))
221       fEnable = FALSE;
222    EnableWindow (GetDlgItem (hDlg, IDC_MON_SOME), fEnable);
223
224    OpenCell_OnSubset (hDlg);
225 }
226
227
228 void OpenCell_OnAdvanced (HWND hDlg)
229 {
230    HWND hGroup = GetDlgItem (hDlg, IDC_ADVANCED_GROUP);
231
232    RECT rWindow;
233    RECT rClient;
234    RECT rGroup;
235
236    GetWindowRect (hDlg, &rWindow);
237    GetClientRect (hDlg, &rClient);
238    GetRectInParent (hGroup, &rGroup);
239
240    if (cyRECT(rClient) <= rGroup.top) // closed now?
241       {
242       SetWindowPos (hDlg, NULL,
243                     0, 0,
244                     cxRECT(rWindow),
245                     cyRECT(rWindow) + cyRECT(rGroup) + 14,
246                     SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
247
248       TCHAR szText[ cchRESOURCE ];
249       GetString (szText, IDS_ADVANCEDIN_BUTTON);
250       SetDlgItemText (hDlg, IDC_ADVANCED, szText);
251       }
252    else // open now?
253       {
254       SetWindowPos (hDlg, NULL,
255                     0, 0,
256                     cxRECT(rWindow),
257                     cyRECT(rWindow) - cyRECT(rGroup) - 14,
258                     SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
259
260       TCHAR szText[ cchRESOURCE ];
261       GetString (szText, IDS_ADVANCEDOUT_BUTTON);
262       SetDlgItemText (hDlg, IDC_ADVANCED, szText);
263       }
264 }
265
266
267 void OpenCellDlg_Hook_OnEndTask_OpenCell (HWND hDlg, LPTASKPACKET ptp)
268 {
269    if (ptp->rc)
270       {
271       EndDialog (hDlg, IDOK);
272       }
273    else
274       {
275       OpenCell_Hook_Enable (hDlg, TRUE);
276
277       TCHAR szCell[ cchNAME ];
278       GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
279       ErrorDialog (ptp->status, IDS_ERROR_CANT_OPEN_CELL, TEXT("%s"), szCell);
280       }
281 }
282
283
284 BOOL CALLBACK OpenCellDlg_Hook (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
285 {
286    switch (msg)
287       {
288       case WM_SHOWWINDOW:
289          CheckDlgButton (hDlg, IDC_MON_ALL,  TRUE);
290          CheckDlgButton (hDlg, IDC_MON_ONE,  FALSE);
291          CheckDlgButton (hDlg, IDC_MON_SOME, FALSE);
292          OpenCell_OnSubset (hDlg);
293          OpenCell_OnAdvanced (hDlg);
294          OpenCell_OnCellChange (hDlg, TRUE);
295          break;
296
297       case WM_ENDTASK:
298          LPTASKPACKET ptp;
299          if ((ptp = (LPTASKPACKET)lp) != NULL)
300             {
301             if (ptp->idTask == taskOPENCELL)
302                OpenCellDlg_Hook_OnEndTask_OpenCell (hDlg, ptp);
303             FreeTaskPacket (ptp);
304             }
305          break;
306
307       case WM_COMMAND:
308          switch (LOWORD(wp))
309             {
310             case IDOK:
311                OpenCellDlg_Hook_OnOK (hDlg, (LPOPENCELLDLG_PARAMS)GetWindowLong (hDlg, DWL_USER));
312                return TRUE;
313
314             case IDC_OPENCELL_CELL:
315                switch (HIWORD(wp))
316                   {
317                   case CBN_SELCHANGE:
318                      TCHAR szCell[ cchNAME ];
319                      SendDlgItemMessage (hDlg, IDC_OPENCELL_CELL, CB_GETLBTEXT, CB_GetSelected(GetDlgItem (hDlg, IDC_OPENCELL_CELL)), (LPARAM)szCell);
320                      SetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell);
321                      OpenCell_OnCellChange (hDlg, FALSE);
322                      break;
323
324                   case CBN_EDITCHANGE:
325                      OpenCell_OnCellChange (hDlg, FALSE);
326                      break;
327                   }
328                break;
329
330             case IDC_ADVANCED:
331                OpenCell_OnAdvanced (hDlg);
332                break;
333
334             case IDC_MON_ALL:
335             case IDC_MON_ONE:
336             case IDC_MON_SOME:
337                OpenCell_OnSubset (hDlg);
338                break;
339             }
340          break;
341       }
342
343    return FALSE;
344 }
345
346
347 /*
348  * CREDENTIALS ________________________________________________________________
349  *
350  */
351
352 void GetBadCredsDlgParams (LPBADCREDSDLG_PARAMS lpp)
353 {
354    memset (lpp, 0x00, sizeof(BADCREDSDLG_PARAMS));
355    lpp->pfShowWarningEver = &gr.fWarnBadCreds;
356    lpp->idsDesc = IDS_BADCREDS_DESC;
357 }
358
359
360 void GetCredentialsDlgParams (LPCREDENTIALSDLG_PARAMS lpp)
361 {
362    memset (lpp, 0x00, sizeof(CREDENTIALSDLG_PARAMS));
363    lpp->hParent = g.hMain;
364    if (g.lpiCell)
365       g.lpiCell->GetCellName (lpp->szCell);
366    else
367       AfsAppLib_GetLocalCell (lpp->szCell);
368    lpp->hCreds = g.hCreds;
369    GetBadCredsDlgParams (&lpp->bcdp);
370 }
371
372
373 /*
374  * OPERATIONS _________________________________________________________________
375  *
376  */
377
378 BOOL OpenCellDialog (void)
379 {
380    if (!Subsets_SaveIfDirty (g.sub))
381       return FALSE;
382
383    OPENCELLDLG_PARAMS pp;
384    memset (&pp, 0x00, sizeof(pp));
385    pp.idd = IDD_OPENCELL;
386    pp.hookproc = (DLGPROC)OpenCellDlg_Hook;
387    pp.hParent = g.hMain;
388    pp.idsDesc = 0;
389    pp.lpcl = AfsAppLib_GetCellList (HKCU, REGSTR_SETTINGS_CELLS);
390    pp.hCreds = g.hCreds;
391    GetBadCredsDlgParams (&pp.bcdp);
392
393    BOOL rc = AfsAppLib_ShowOpenCellDialog (&pp);
394
395    AfsAppLib_FreeCellList (pp.lpcl);
396    return rc;
397 }
398
399
400 BOOL NewCredsDialog (void)
401 {
402    CREDENTIALSDLG_PARAMS pp;
403    GetCredentialsDlgParams (&pp);
404    return AfsAppLib_ShowCredentialsDialog (&pp);
405 }
406
407
408 void CheckForExpiredCredentials (void)
409 {
410    CREDENTIALSDLG_PARAMS pp;
411    GetCredentialsDlgParams (&pp);
412    AfsAppLib_CheckForExpiredCredentials (&pp);
413 }
414
415
416 BOOL CheckCredentials (BOOL fComplain)
417 {
418    CHECKCREDS_PARAMS pp;
419    memset (&pp, 0x00, sizeof(pp));
420    pp.fShowWarning = fComplain;
421    pp.hCreds = g.hCreds;
422    GetBadCredsDlgParams (&pp.bcdp);
423
424    return AfsAppLib_CheckCredentials (&pp);
425 }
426