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