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