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