Windows: AFSTearDownExtents may experience active extents
[openafs.git] / src / WINNT / afsapplib / al_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 <WINNT/afsapplib.h>
21 #include "al_dynlink.h"
22 #include <WINNT/TaAfsAdmSvrClient.h>
23
24
25 /*
26  * DEFINITIONS ________________________________________________________________
27  *
28  */
29
30
31 /*
32  * VARIABLES __________________________________________________________________
33  *
34  */
35
36
37 /*
38  * PROTOTYPES _________________________________________________________________
39  *
40  */
41
42 void OnExpiredCredentials (WPARAM wp, LPARAM lp);
43
44 HRESULT CALLBACK OpenCell_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
45 void OpenCell_OnInitDialog (HWND hDlg, LPOPENCELLDLG_PARAMS lpp);
46 BOOL OpenCell_OnOK (HWND hDlg, LPOPENCELLDLG_PARAMS lpp);
47 void OpenCell_OnCell (HWND hDlg);
48 void OpenCell_Enable (HWND hDlg, BOOL fEnable);
49 void OpenCell_OnGotCreds (HWND hDlg, LPARAM lp);
50
51 HRESULT CALLBACK NewCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
52 void NewCreds_OnInitDialog (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp);
53 BOOL NewCreds_OnOK (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp);
54 void NewCreds_OnLogin (HWND hDlg);
55 void NewCreds_Enable (HWND hDlg, BOOL fEnable);
56 void NewCreds_GetOutParams (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp);
57
58 HRESULT CALLBACK BadCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
59
60
61 /*
62  * ROUTINES ___________________________________________________________________
63  *
64  */
65
66 BOOL AfsAppLib_CrackCredentials (UINT_PTR hCreds, LPTSTR pszCell, LPTSTR pszUser, LPSYSTEMTIME pst, ULONG *pStatus)
67 {
68    BOOL rc = FALSE;
69    ULONG status = 0;
70
71    UINT_PTR idClient;
72    if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
73       {
74       rc = asc_CredentialsCrack (idClient, (PVOID) hCreds, pszCell, pszUser, pst, &status);
75       }
76    else
77        if (OpenClientLibrary())
78       {
79       char szUserA[ cchRESOURCE ], szUser2A[ cchRESOURCE ];
80       char szCellA[ cchRESOURCE ];
81       unsigned long dateExpire;
82       int fHasKasToken;
83
84       if (afsclient_TokenQuery ((PVOID)hCreds, &dateExpire, szUserA, szUser2A, szCellA, &fHasKasToken, (afs_status_p)&status))
85          {
86          rc = TRUE;
87          CopyAnsiToString (pszUser, szUserA);
88          CopyAnsiToString (pszCell, szCellA);
89          AfsAppLib_UnixTimeToSystemTime (pst, dateExpire);
90          }
91
92       CloseClientLibrary();
93       }
94
95    if (!hCreds && pStatus)
96       *pStatus = status;
97    return rc;
98 }
99
100
101 UINT_PTR AfsAppLib_GetCredentials (LPCTSTR pszCell, ULONG *pStatus)
102 {
103    UINT_PTR hCreds = 0;
104    ULONG status = 0;
105
106    UINT_PTR idClient;
107    if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
108       {
109       hCreds = asc_CredentialsGet (idClient, pszCell, &status);
110       }
111    else
112        if (OpenClientLibrary())
113       {
114       LPSTR pszCellA = StringToAnsi (pszCell);
115
116       afsclient_TokenGetExisting (pszCellA, (PVOID *)&hCreds, (afs_status_p)&status);
117
118       FreeString (pszCellA, pszCell);
119       CloseClientLibrary();
120       }
121
122    if (!hCreds && pStatus)
123       *pStatus = status;
124    return hCreds;
125 }
126
127
128 UINT_PTR AfsAppLib_SetCredentials (LPCTSTR pszCell, LPCTSTR pszUser, LPCTSTR pszPassword, ULONG *pStatus)
129 {
130    UINT_PTR hCreds = 0;
131    ULONG status = 0;
132
133    UINT_PTR idClient;
134    if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
135       {
136       hCreds = asc_CredentialsSet (idClient, pszCell, pszUser, pszPassword, &status);
137       }
138    else
139        if (OpenClientLibrary())
140       {
141       char szCellA[ cchRESOURCE ];
142       char szUserA[ cchRESOURCE ];
143       char szPasswordA[ cchRESOURCE ];
144       CopyStringToAnsi (szCellA, pszCell);
145       CopyStringToAnsi (szUserA, pszUser);
146       CopyStringToAnsi (szPasswordA, pszPassword);
147
148       afsclient_TokenGetNew (szCellA, szUserA, szPasswordA, (PVOID *)&hCreds, (afs_status_p)&status);
149
150       CloseClientLibrary();
151       }
152
153    if (hCreds)
154       {
155       PostMessage (AfsAppLib_GetMainWindow(), WM_REFRESHED_CREDENTIALS, 0, (LPARAM)hCreds);
156       }
157
158    if (!hCreds && pStatus)
159       *pStatus = status;
160    return hCreds;
161 }
162
163
164 /*
165  * OPEN CELL DIALOG ___________________________________________________________
166  *
167  */
168
169 BOOL AfsAppLib_ShowOpenCellDialog (LPOPENCELLDLG_PARAMS lpp)
170 {
171    HINSTANCE hInst = APP_HINST;
172    if (lpp->idd == 0)
173       {
174       hInst = APPLIB_HINST;
175       lpp->idd = IDD_APPLIB_OPENCELL;
176       }
177    if (lpp->hCreds == 0)
178       {
179       if (lpp->szCell[0])
180          lpp->hCreds = AfsAppLib_GetCredentials (lpp->szCell);
181       else
182          lpp->hCreds = AfsAppLib_GetCredentials (NULL);
183       }
184
185    INT_PTR rc = ModalDialogParam (lpp->idd, lpp->hParent, (DLGPROC)OpenCell_DlgProc, (LPARAM)lpp);
186
187    return (rc == IDOK) ? TRUE : FALSE;
188 }
189
190
191 HRESULT CALLBACK OpenCell_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
192 {
193    LPOPENCELLDLG_PARAMS lpp;
194    if (msg == WM_INITDIALOG)
195       SetWindowLongPtr (hDlg, DWLP_USER, lp);
196
197    if ((lpp = (LPOPENCELLDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
198       {
199       if (lpp->hookproc)
200          {
201          if (CallWindowProc ((WNDPROC)lpp->hookproc, hDlg, msg, wp, lp))
202             return TRUE;
203          }
204       }
205
206    if (lpp != NULL)
207       {
208       if (AfsAppLib_HandleHelp (lpp->idd, hDlg, msg, wp, lp))
209          return TRUE;
210       }
211
212    switch (msg)
213       {
214       case WM_INITDIALOG:
215          OpenCell_OnInitDialog (hDlg, lpp);
216          break;
217
218       case WM_COMMAND:
219          switch (LOWORD(wp))
220             {
221             case IDCANCEL:
222                EndDialog (hDlg, IDCANCEL);
223                return TRUE;
224
225             case IDOK:
226                if (OpenCell_OnOK (hDlg, lpp))
227                   EndDialog (hDlg, IDOK);
228                return TRUE;
229
230             case IDC_OPENCELL_CELL:
231                switch (HIWORD(wp))
232                   {
233                   case CBN_SELCHANGE:
234                      TCHAR szCell[ cchNAME ];
235                      SendDlgItemMessage (hDlg, IDC_OPENCELL_CELL, CB_GETLBTEXT, CB_GetSelected(GetDlgItem (hDlg, IDC_OPENCELL_CELL)), (LPARAM)szCell);
236                      SetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell);
237                      OpenCell_OnCell (hDlg);
238                      break;
239
240                   case CBN_EDITCHANGE:
241                      OpenCell_OnCell (hDlg);
242                      break;
243                   }
244                break;
245             }
246          break;
247
248       case WM_REFRESHED_CREDENTIALS:
249          OpenCell_OnGotCreds (hDlg, lp);
250          break;
251       }
252
253    return FALSE;
254 }
255
256
257 void OpenCell_OnInitDialog (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
258 {
259    // Fix the title of the dialog (unless the caller has supplied a
260    // custom dialog template)
261    //
262    if (lpp && (lpp->idd == IDD_APPLIB_OPENCELL))
263       {
264       TCHAR szApplication[ cchNAME ];
265       AfsAppLib_GetAppName (szApplication);
266       if (szApplication[0] != TEXT('\0'))
267          {
268          TCHAR szTitle[ cchRESOURCE ];
269          GetWindowText (hDlg, szTitle, cchRESOURCE);
270          lstrcat (szTitle, TEXT(" - "));
271          lstrcat (szTitle, szApplication);
272          SetWindowText (hDlg, szTitle);
273          }
274       }
275
276    // Fill in the 'Cell:' combobox; we'll list the default cell, and any
277    // cell which the user has specified before.
278    //
279    CB_StartChange (GetDlgItem (hDlg, IDC_OPENCELL_CELL), TRUE);
280
281    if (!lpp->lpcl)
282       {
283       TCHAR szDefCell[ cchNAME ];
284       if (AfsAppLib_GetLocalCell (szDefCell) && *szDefCell)
285          {
286          CB_AddItem (GetDlgItem (hDlg, IDC_OPENCELL_CELL), szDefCell, 1);
287          }
288       }
289    else for (size_t ii = 0; ii < lpp->lpcl->nCells; ++ii)
290       {
291       CB_AddItem (GetDlgItem (hDlg, IDC_OPENCELL_CELL), lpp->lpcl->aCells[ii], 1+ii);
292       }
293
294    CB_EndChange (GetDlgItem (hDlg, IDC_OPENCELL_CELL), 1);
295
296    // Set up the "Credentials" box; if the user needs credentials to edit
297    // this default cell, jump the cursor to the appropriate field
298    //
299    SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
300
301    OpenCell_OnCell (hDlg);
302 }
303
304
305 BOOL OpenCell_OnOK (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
306 {
307    BOOL rc = FALSE;
308    OpenCell_Enable (hDlg, FALSE);
309    StartHourGlass ();
310
311    // Remember what cell the user chose to edit
312    //
313    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, lpp->szCell, cchNAME);
314
315    // Try to obtain the credentials specified by the user.
316    //
317    TCHAR szCell[ cchNAME ];
318    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
319
320    TCHAR szUser[ cchRESOURCE ];
321    GetDlgItemText (hDlg, IDC_OPENCELL_ID, szUser, cchNAME);
322
323    TCHAR szPassword[ cchRESOURCE ];
324    GetDlgItemText (hDlg, IDC_OPENCELL_PASSWORD, szPassword, cchNAME);
325
326    ULONG status;
327    if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
328       {
329       ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
330       }
331    else
332       {
333       // See if those credentials are sufficient
334       //
335       CHECKCREDS_PARAMS pp;
336       memset (&pp, 0x00, sizeof(pp));
337       memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
338       pp.bcdp.hParent = hDlg;
339       pp.hCreds = lpp->hCreds;
340       pp.fShowWarning = TRUE;
341
342       if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
343          {
344          SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
345          PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
346          }
347       }
348
349    if (!rc)
350       OpenCell_Enable (hDlg, TRUE);
351    StopHourGlass ();
352    return rc;
353 }
354
355
356 typedef struct
357    {
358    HWND hDlg;
359    TCHAR szCell[ cchNAME ];
360    TCHAR szUser[ cchNAME ];
361    SYSTEMTIME stExpire;
362    BOOL fGotCreds;
363    BOOL fValidCreds;
364    } OPENCELL_ONCELL_PARAMS, *LPOPENCELL_ONCELL_PARAMS;
365
366 DWORD WINAPI OpenCell_OnCell_ThreadProc (PVOID lp)
367 {
368    LPOPENCELL_ONCELL_PARAMS lpp;
369    if ((lpp = (LPOPENCELL_ONCELL_PARAMS)lp) != NULL)
370       {
371       UINT_PTR hCreds = AfsAppLib_GetCredentials (lpp->szCell);
372       lpp->fGotCreds = AfsAppLib_CrackCredentials (hCreds, lpp->szCell, lpp->szUser, &lpp->stExpire)?TRUE:FALSE;
373       lpp->fValidCreds = FALSE;
374
375       if (lpp->fGotCreds && AfsAppLib_IsTimeInFuture (&lpp->stExpire))
376          {
377          CHECKCREDS_PARAMS pp;
378          memset (&pp, 0x00, sizeof(pp));
379          pp.hCreds = hCreds;
380          lpp->fValidCreds = AfsAppLib_CheckCredentials(&pp);
381          }
382
383       // Post our results; we'll return the same packet we got.
384       //
385       if (IsWindow (lpp->hDlg))
386          PostMessage (lpp->hDlg, WM_REFRESHED_CREDENTIALS, 0, (LPARAM)lpp);
387       else
388          Delete (lpp);
389       }
390
391    return 0;
392 }
393
394
395 void OpenCell_OnCell (HWND hDlg)
396 {
397    // Fire up a background thread to query our current credentials in the
398    // selected cell.
399    //
400    LPOPENCELL_ONCELL_PARAMS lpp = New (OPENCELL_ONCELL_PARAMS);
401    memset (lpp, 0x00, sizeof(lpp));
402    GetDlgItemText (hDlg, IDC_OPENCELL_CELL, lpp->szCell, cchNAME);
403    lpp->hDlg = hDlg;
404
405    DWORD dwThreadID;
406    CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)OpenCell_OnCell_ThreadProc, lpp, 0, &dwThreadID);
407 }
408
409
410 void OpenCell_OnGotCreds (HWND hDlg, LPARAM lp)
411 {
412    LPOPENCELL_ONCELL_PARAMS lpp;
413    if ((lpp = (LPOPENCELL_ONCELL_PARAMS)lp) != NULL)
414       {
415       // Don't do anything to the dialog if the user has chosen a different
416       // cell than that for which we just queried credentials.
417       //
418       TCHAR szCell[ cchNAME ];
419       GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
420
421       if (!lstrcmpi (szCell, lpp->szCell))
422          {
423          TCHAR szPrevCreds[ cchRESOURCE ];
424          GetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, szPrevCreds, cchRESOURCE);
425
426          if (!lpp->fGotCreds)
427             {
428             LPTSTR psz = FormatString (IDS_CREDS_NONE, TEXT("%s"), lpp->szCell);
429             SetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, psz);
430             FreeString (psz);
431             }
432          else if (!AfsAppLib_IsTimeInFuture (&lpp->stExpire))
433             {
434             LPTSTR pszCreds = FormatString (IDS_CREDS_EXPIRED, TEXT("%s%s%t"), lpp->szCell, lpp->szUser, &lpp->stExpire);
435             SetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, pszCreds);
436             FreeString (pszCreds);
437             }
438          else
439             {
440             LPTSTR pszCreds = FormatString (IDS_CREDS_VALID, TEXT("%s%s%t"), lpp->szCell, lpp->szUser, &lpp->stExpire);
441             SetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, pszCreds);
442             FreeString (pszCreds);
443             }
444
445          SetDlgItemText (hDlg, IDC_OPENCELL_ID, (lpp->fGotCreds) ? lpp->szUser : TEXT("admin"));
446
447          if (!lpp->fValidCreds && !szPrevCreds[0])
448             PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
449          }
450
451       Delete (lpp);
452       }
453 }
454
455
456 void OpenCell_Enable (HWND hDlg, BOOL fEnable)
457 {
458    EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
459    EnableWindow (GetDlgItem (hDlg, IDCANCEL), fEnable);
460    EnableWindow (GetDlgItem (hDlg, IDHELP), fEnable);
461    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_CELL), fEnable);
462    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_ID), fEnable);
463    EnableWindow (GetDlgItem (hDlg, IDC_OPENCELL_PASSWORD), fEnable);
464 }
465
466
467 /*
468  * NEW CREDENTIALS DIALOG _____________________________________________________
469  *
470  */
471
472 BOOL AfsAppLib_ShowCredentialsDialog (LPCREDENTIALSDLG_PARAMS lpp)
473 {
474    HINSTANCE hInst = APP_HINST;
475    if (lpp->idd == 0)
476       {
477       hInst = APPLIB_HINST;
478       lpp->idd = IDD_APPLIB_CREDENTIALS;
479       }
480    if (lpp->hCreds == 0)
481       {
482       if (lpp->szCell[0])
483          lpp->hCreds = AfsAppLib_GetCredentials (lpp->szCell);
484       else
485          lpp->hCreds = AfsAppLib_GetCredentials (NULL);
486       }
487
488    INT_PTR rc = ModalDialogParam (lpp->idd, lpp->hParent, (DLGPROC)NewCreds_DlgProc, (LPARAM)lpp);
489
490    return (rc == IDOK) ? TRUE : FALSE;
491 }
492
493
494 HRESULT CALLBACK NewCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
495 {
496    LPCREDENTIALSDLG_PARAMS lpp;
497    if (msg == WM_INITDIALOG)
498       SetWindowLongPtr (hDlg, DWLP_USER, lp);
499
500    if ((lpp = (LPCREDENTIALSDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
501       {
502       if (lpp->hookproc)
503          {
504          if (CallWindowProc ((WNDPROC)lpp->hookproc, hDlg, msg, wp, lp))
505             return TRUE;
506          }
507       }
508
509    if (lpp != NULL)
510       {
511       if (AfsAppLib_HandleHelp (lpp->idd, hDlg, msg, wp, lp))
512          return TRUE;
513       }
514
515    switch (msg)
516       {
517       case WM_INITDIALOG:
518          NewCreds_OnInitDialog (hDlg, lpp);
519          break;
520
521       case WM_COMMAND:
522          switch (LOWORD(wp))
523             {
524             case IDCANCEL:
525                EndDialog (hDlg, IDCANCEL);
526                return TRUE;
527
528             case IDOK:
529                               if (NewCreds_OnOK (hDlg, lpp))
530                   EndDialog (hDlg, IDOK);
531                return TRUE;
532
533             case IDC_CREDS_LOGIN:
534                NewCreds_OnLogin (hDlg);
535                break;
536             }
537          break;
538       }
539
540    return FALSE;
541 }
542
543
544 void NewCreds_OnInitDialog (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
545 {
546    // Fix the title of the dialog (unless the caller has supplied a
547    // custom dialog template)
548    //
549    if (lpp && (lpp->idd == IDD_APPLIB_CREDENTIALS))
550       {
551       TCHAR szApplication[ cchNAME ];
552       AfsAppLib_GetAppName (szApplication);
553       if (szApplication[0] != TEXT('\0'))
554          {
555          TCHAR szTitle[ cchRESOURCE ];
556          GetWindowText (hDlg, szTitle, cchRESOURCE);
557          lstrcat (szTitle, TEXT(" - "));
558          lstrcat (szTitle, szApplication);
559          SetWindowText (hDlg, szTitle);
560          }
561       }
562
563    // Set up the "Credentials" boxes
564    //
565    TCHAR szUser[ cchRESOURCE ];
566    SYSTEMTIME st;
567
568    BOOL fValidCreds = TRUE;
569    BOOL fShowCreds = TRUE;
570    CHECKCREDS_PARAMS pp;
571    memset (&pp, 0x00, sizeof(pp));
572    pp.hCreds = lpp->hCreds;
573    pp.fShowWarning = FALSE;
574    memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
575    pp.bcdp.hParent = hDlg;
576
577    if (!AfsAppLib_CrackCredentials (lpp->hCreds, lpp->szCell, szUser, &st))
578       {
579       fValidCreds = FALSE;
580       fShowCreds = FALSE;
581       }
582    if (!AfsAppLib_IsTimeInFuture (&st))
583       {
584       fValidCreds = FALSE;
585       fShowCreds = FALSE;
586       }
587    if (!AfsAppLib_CheckCredentials (&pp))
588       {
589       fValidCreds = FALSE;
590       }
591
592    CheckDlgButton (hDlg, IDC_CREDS_LOGIN, !fValidCreds);
593    if (!fValidCreds)
594       PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_CREDS_PASSWORD), TRUE);
595
596    SetDlgItemText (hDlg, IDC_CREDS_CELL, lpp->szCell);
597
598    if (fShowCreds)
599       {
600       SetDlgItemText (hDlg, IDC_CREDS_CURRENTID, szUser);
601
602       LPTSTR pszDate = FormatString (TEXT("%1"), TEXT("%t"), &st);
603       SetDlgItemText (hDlg, IDC_CREDS_EXPDATE, pszDate);
604       FreeString (pszDate);
605       }
606
607    SetDlgItemText (hDlg, IDC_CREDS_ID, TEXT("admin"));
608
609    NewCreds_OnLogin (hDlg);
610 }
611
612
613 BOOL NewCreds_OnOK (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
614 {
615    NewCreds_GetOutParams (hDlg, lpp);
616    if (!IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN))
617       return TRUE;
618
619    BOOL rc = FALSE;
620    StartHourGlass ();
621    NewCreds_Enable (hDlg, FALSE);
622
623    // Try to obtain the credentials specified by the user.
624    //
625    TCHAR szUser[ cchRESOURCE ];
626    GetDlgItemText (hDlg, IDC_CREDS_ID, szUser, cchNAME);
627
628    TCHAR szPassword[ cchRESOURCE ];
629    GetDlgItemText (hDlg, IDC_CREDS_PASSWORD, szPassword, cchNAME);
630
631    ULONG status;
632    if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
633       {
634       ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
635       }
636    else
637       {
638       // See if those credentials are sufficient
639       //
640       CHECKCREDS_PARAMS pp;
641       memset (&pp, 0x00, sizeof(pp));
642       memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
643       pp.bcdp.hParent = hDlg;
644       pp.hCreds = lpp->hCreds;
645       pp.fShowWarning = TRUE;
646
647       if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
648          {
649          SetDlgItemText (hDlg, IDC_CREDS_ID, TEXT("admin"));
650          PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_CREDS_PASSWORD), TRUE);
651          }
652       }
653
654    if (!rc)
655       NewCreds_Enable (hDlg, TRUE);
656    StopHourGlass ();
657    return rc;
658 }
659
660
661 void NewCreds_OnLogin (HWND hDlg)
662 {
663    BOOL fEnable = IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN);
664
665    EnableWindow (GetDlgItem (hDlg, IDC_CREDS_ID),       fEnable);
666    EnableWindow (GetDlgItem (hDlg, IDC_CREDS_PASSWORD), fEnable);
667 }
668
669
670 void NewCreds_Enable (HWND hDlg, BOOL fEnable)
671 {
672    EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
673    EnableWindow (GetDlgItem (hDlg, IDCANCEL), fEnable);
674    EnableWindow (GetDlgItem (hDlg, IDHELP), fEnable);
675    EnableWindow (GetDlgItem (hDlg, IDC_CREDS_LOGIN), fEnable);
676
677    if (fEnable)
678       NewCreds_OnLogin (hDlg);
679    else
680       {
681       EnableWindow (GetDlgItem (hDlg, IDC_CREDS_ID),       fEnable);
682       EnableWindow (GetDlgItem (hDlg, IDC_CREDS_PASSWORD), fEnable);
683       }
684 }
685
686
687 void NewCreds_GetOutParams (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
688 {
689    if (!IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN))
690       {
691       lpp->szIdentity[0] = TEXT('\0');
692       lpp->szPassword[0] = TEXT('\0');
693       }
694    else
695       {
696       GetDlgItemText (hDlg, IDC_CREDS_ID, lpp->szIdentity, cchNAME);
697       GetDlgItemText (hDlg, IDC_CREDS_PASSWORD, lpp->szPassword, cchNAME);
698       }
699 }
700
701
702 /*
703  * TEST CREDENTIALS ___________________________________________________________
704  *
705  */
706
707
708 BOOL AfsAppLib_IsUserAdmin (UINT_PTR hCreds, LPTSTR pszUser)
709 {
710 #ifndef USE_KASERVER
711     return TRUE;
712 #else
713    BOOL rc = FALSE;
714    afs_status_t status;
715
716    UINT_PTR idClient;
717    if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
718       {
719       TCHAR szCell[ cchRESOURCE ];
720       TCHAR szUser[ cchRESOURCE ];
721       SYSTEMTIME stExpire;
722       if (asc_CredentialsCrack (idClient, hCreds, szCell, szUser, &stExpire, (ULONG*)&status))
723          {
724          ASID idCell;
725          if (asc_CellOpen (idClient, hCreds, szCell, AFSADMSVR_SCOPE_USERS, &idCell, (ULONG*)&status))
726             {
727             ASID idUser;
728             if (asc_ObjectFind (idClient, idCell, TYPE_USER, pszUser, &idUser, (ULONG*)&status))
729                {
730                ASOBJPROP Info;
731                if (asc_ObjectPropertiesGet (idClient, GET_ALL_DATA, idCell, idUser, &Info, (ULONG*)&status))
732                   {
733                   if (Info.u.UserProperties.fHaveKasInfo)
734                      {
735                      rc = Info.u.UserProperties.KASINFO.fIsAdmin;
736                      }
737                   }
738                }
739             asc_CellClose (idClient, idCell, (ULONG*)&status);
740             }
741          }
742       }
743    else if (OpenClientLibrary())
744       {
745       if (OpenKasLibrary())
746          {
747          char szUserA[ cchRESOURCE ], szUser2A[ cchRESOURCE ];
748          char szCellA[ cchRESOURCE ];
749          unsigned long dateExpire;
750          int fHasKasToken;
751
752          if (afsclient_TokenQuery (hCreds, &dateExpire, szUserA, szUser2A, szCellA, &fHasKasToken, (afs_status_p)&status))
753             {
754             PVOID hCell;
755             if (afsclient_CellOpen (szCellA, hCreds, &hCell, &status))
756                {
757                kas_identity_t Identity;
758                memset (&Identity, 0x00, sizeof(Identity));
759                CopyStringToAnsi (Identity.principal, pszUser);
760
761                kas_principalEntry_t Entry;
762                if (kas_PrincipalGet (hCell, NULL, &Identity, &Entry, &status))
763                   {
764                   if (Entry.adminSetting == KAS_ADMIN)
765                      rc = TRUE;
766                   }
767
768                afsclient_CellClose (hCell, (afs_status_p)&status);
769                }
770             }
771
772          CloseKasLibrary();
773          }
774
775       CloseClientLibrary();
776       }
777
778    return rc;
779 #endif /* USE_KASERVER */
780 }
781
782
783 typedef struct
784    {
785    BADCREDSDLG_PARAMS bcdp;
786    LPTSTR pszFullText;
787    PVOID hCreds;
788    } REALBADCREDSDLG_PARAMS, *LPREALBADCREDSDLG_PARAMS;
789
790 BOOL AfsAppLib_CheckCredentials (LPCHECKCREDS_PARAMS lpp)
791 {
792    BOOL fCredsOK = TRUE;
793    int idsWarning = IDS_BADCREDS_DESC_GENERAL;
794
795    TCHAR szCell[ cchNAME ];
796    TCHAR szUser[ cchNAME ];
797    SYSTEMTIME stExpire;
798
799    if (!AfsAppLib_CrackCredentials (lpp->hCreds, szCell, szUser, &stExpire))
800       {
801       fCredsOK = FALSE;
802       }
803    else if (!AfsAppLib_IsUserAdmin (lpp->hCreds, szUser))
804       {
805       fCredsOK = FALSE;
806       idsWarning = IDS_BADCREDS_DESC_BADCHOICE;
807       }
808    else if (!AfsAppLib_IsTimeInFuture (&stExpire))
809       {
810       fCredsOK = FALSE;
811       idsWarning = IDS_BADCREDS_DESC_EXPIRED;
812       }
813
814    if (!fCredsOK && lpp->fShowWarning)
815       {
816       if (lpp->bcdp.pfShowWarningEver && !(*lpp->bcdp.pfShowWarningEver))
817          {
818          fCredsOK = TRUE; // user says always ignore bad credentials.
819          }
820       else
821          {
822          if (!szCell[0])
823             AfsAppLib_GetLocalCell (szCell);
824
825          int idsDesc = (lpp->bcdp.idsDesc) ? lpp->bcdp.idsDesc : IDS_BADCREDS_DESC2;
826          LPTSTR pszDesc = FormatString (idsDesc, TEXT("%s"), szCell);
827          LPTSTR pszFullText = FormatString (idsWarning, TEXT("%s%s%m"), szCell, pszDesc, IDS_BADCREDS_DESC3);
828          FreeString (pszDesc);
829
830          REALBADCREDSDLG_PARAMS pp;
831          memset (&pp, 0x00, sizeof(pp));
832          pp.pszFullText = pszFullText;
833          memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
834
835          HINSTANCE hInst = APP_HINST;
836          if (pp.bcdp.idd == 0)
837             {
838             hInst = APPLIB_HINST;
839             pp.bcdp.idd = IDD_APPLIB_BADCREDS;
840             }
841
842          INT_PTR rc = ModalDialogParam (pp.bcdp.idd, pp.bcdp.hParent, (DLGPROC)BadCreds_DlgProc, (LPARAM)&pp);
843          if (rc == IDCANCEL)
844             {
845             fCredsOK = TRUE; // user says ignore bad credentials this time.
846             }
847
848          FreeString (pszFullText);
849          }
850       }
851
852    return fCredsOK;
853 }
854
855
856 HRESULT CALLBACK BadCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
857 {
858    LPREALBADCREDSDLG_PARAMS lpp;
859    if (msg == WM_INITDIALOG)
860       SetWindowLongPtr (hDlg, DWLP_USER, lp);
861    if ((lpp = (LPREALBADCREDSDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
862       {
863       if (lpp->bcdp.hookproc)
864          {
865          if (CallWindowProc ((WNDPROC)lpp->bcdp.hookproc, hDlg, msg, wp, lp))
866             return TRUE;
867          }
868       if (AfsAppLib_HandleHelp (lpp->bcdp.idd, hDlg, msg, wp, lp))
869          return TRUE;
870       }
871
872    switch (msg)
873       {
874       case WM_INITDIALOG:
875          TCHAR szApplication[ cchNAME ];
876          AfsAppLib_GetAppName (szApplication);
877          if (szApplication[0] != TEXT('\0'))
878             {
879             TCHAR szTitle[ cchRESOURCE ];
880             GetWindowText (hDlg, szTitle, cchRESOURCE);
881             lstrcat (szTitle, TEXT(" - "));
882             lstrcat (szTitle, szApplication);
883             SetWindowText (hDlg, szTitle);
884             }
885          if (!lpp || !lpp->bcdp.pfShowWarningEver)
886             DestroyWindow (GetWindow (hDlg, IDC_BADCREDS_SHUTUP));
887
888          SetDlgItemText (hDlg, IDC_BADCREDS_DESC, lpp->pszFullText);
889          break;
890
891       case WM_COMMAND:
892          switch (LOWORD(wp))
893             {
894             case IDOK:
895             case IDCANCEL:
896                if (lpp && lpp->bcdp.pfShowWarningEver)
897                   *lpp->bcdp.pfShowWarningEver = !IsDlgButtonChecked (hDlg, IDC_BADCREDS_SHUTUP);
898                EndDialog (hDlg, LOWORD(wp));
899                break;
900             }
901          break;
902       }
903
904    return FALSE;
905 }
906
907
908 /*
909  * EXPIRED CREDENTIALS ________________________________________________________
910  *
911  */
912
913 void AfsAppLib_CheckForExpiredCredentials (LPCREDENTIALSDLG_PARAMS lpp)
914 {
915    static UINT_PTR hCredsPrevious = 0;
916    static BOOL fHadGoodCredentials;
917
918    TCHAR szCell[ cchNAME ];
919    TCHAR szUser[ cchNAME ];
920    SYSTEMTIME stExpire;
921
922    BOOL fHaveCredentials;
923    fHaveCredentials = AfsAppLib_CrackCredentials (lpp->hCreds, szCell, szUser, &stExpire)?TRUE:FALSE;
924
925    if (hCredsPrevious && (hCredsPrevious != lpp->hCreds))
926       {
927       if (!fHaveCredentials)
928          fHadGoodCredentials = FALSE;
929       else if (!AfsAppLib_IsTimeInFuture (&stExpire))
930          fHadGoodCredentials = FALSE;
931       else
932          fHadGoodCredentials = TRUE;
933       hCredsPrevious = lpp->hCreds;
934       }
935    else if (fHaveCredentials && AfsAppLib_IsTimeInFuture (&stExpire))
936       {
937       fHadGoodCredentials = TRUE;
938       }
939    else if (fHadGoodCredentials) // used to have good credentials, but now don't
940       {
941       fHadGoodCredentials = FALSE;
942
943       LPCREDENTIALSDLG_PARAMS lppCopy = New (CREDENTIALSDLG_PARAMS);
944       memcpy (lppCopy, lpp, sizeof(CREDENTIALSDLG_PARAMS));
945
946       if (!AfsAppLib_GetMainWindow())
947          OnExpiredCredentials ((WPARAM)(!AfsAppLib_IsTimeInFuture(&stExpire)), (LPARAM)lppCopy);
948       else
949          PostMessage (AfsAppLib_GetMainWindow(), WM_EXPIRED_CREDENTIALS, (WPARAM)(!AfsAppLib_IsTimeInFuture(&stExpire)), (LPARAM)lppCopy);
950       }
951 }
952
953
954 void OnExpiredCredentials (WPARAM wp, LPARAM lp)
955 {
956    BOOL fExpired = (BOOL)wp;
957    LPCREDENTIALSDLG_PARAMS lpp = (LPCREDENTIALSDLG_PARAMS)lp;
958
959    if (lpp && lpp->bcdp.pfShowWarningEver && *(lpp->bcdp.pfShowWarningEver))
960       {
961       int idsWarning = (wp) ? IDS_BADCREDS_DESC_EXPIRED : IDS_BADCREDS_DESC_DESTROYED;
962
963       int idsDesc = (lpp->bcdp.idsDesc) ? lpp->bcdp.idsDesc : IDS_BADCREDS_DESC2;
964       LPTSTR pszDesc = FormatString (idsDesc, TEXT("%s"), lpp->szCell);
965       LPTSTR pszFullText = FormatString (idsWarning, TEXT("%s%s%m"), lpp->szCell, pszDesc, IDS_BADCREDS_DESC3);
966       FreeString (pszDesc);
967
968       REALBADCREDSDLG_PARAMS pp;
969       memset (&pp, 0x00, sizeof(pp));
970       pp.pszFullText = pszFullText;
971       memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
972
973       if (ModalDialogParam (IDD_APPLIB_BADCREDS, NULL, (DLGPROC)BadCreds_DlgProc, (LPARAM)&pp) != IDCANCEL)
974          {
975          AfsAppLib_ShowCredentialsDialog (lpp);
976          }
977
978       FreeString (pszFullText);
979       }
980
981    if (lpp)
982       {
983       Delete (lpp);
984       }
985 }
986
987