Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / afsapplib / al_creds.cpp
blobaf4761bcd7f2b624bea2ab2184d0181b55f3047e
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
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 */
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
18 #include <WINNT/afsapplib.h>
19 #include "al_dynlink.h"
20 #include <WINNT/TaAfsAdmSvrClient.h>
24 * DEFINITIONS ________________________________________________________________
30 * VARIABLES __________________________________________________________________
36 * PROTOTYPES _________________________________________________________________
40 void OnExpiredCredentials (WPARAM wp, LPARAM lp);
42 HRESULT 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);
49 HRESULT 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);
56 HRESULT CALLBACK BadCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
60 * ROUTINES ___________________________________________________________________
64 BOOL AfsAppLib_CrackCredentials (UINT_PTR hCreds, LPTSTR pszCell, LPTSTR pszUser, LPSYSTEMTIME pst, ULONG *pStatus)
66 BOOL rc = FALSE;
67 ULONG status = 0;
69 UINT_PTR idClient;
70 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
72 rc = asc_CredentialsCrack (idClient, (PVOID) hCreds, pszCell, pszUser, pst, &status);
74 else
75 if (OpenClientLibrary())
77 char szUserA[ cchRESOURCE ], szUser2A[ cchRESOURCE ];
78 char szCellA[ cchRESOURCE ];
79 unsigned long dateExpire;
80 int fHasKasToken;
82 if (afsclient_TokenQuery ((PVOID)hCreds, &dateExpire, szUserA, szUser2A, szCellA, &fHasKasToken, (afs_status_p)&status))
84 rc = TRUE;
85 CopyAnsiToString (pszUser, szUserA);
86 CopyAnsiToString (pszCell, szCellA);
87 AfsAppLib_UnixTimeToSystemTime (pst, dateExpire);
90 CloseClientLibrary();
93 if (!hCreds && pStatus)
94 *pStatus = status;
95 return rc;
99 UINT_PTR AfsAppLib_GetCredentials (LPCTSTR pszCell, ULONG *pStatus)
101 UINT_PTR hCreds = 0;
102 ULONG status = 0;
104 UINT_PTR idClient;
105 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
107 hCreds = asc_CredentialsGet (idClient, pszCell, &status);
109 else
110 if (OpenClientLibrary())
112 LPSTR pszCellA = StringToAnsi (pszCell);
114 afsclient_TokenGetExisting (pszCellA, (PVOID *)&hCreds, (afs_status_p)&status);
116 FreeString (pszCellA, pszCell);
117 CloseClientLibrary();
120 if (!hCreds && pStatus)
121 *pStatus = status;
122 return hCreds;
126 UINT_PTR AfsAppLib_SetCredentials (LPCTSTR pszCell, LPCTSTR pszUser, LPCTSTR pszPassword, ULONG *pStatus)
128 UINT_PTR hCreds = 0;
129 ULONG status = 0;
131 UINT_PTR idClient;
132 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
134 hCreds = asc_CredentialsSet (idClient, pszCell, pszUser, pszPassword, &status);
136 else
137 if (OpenClientLibrary())
139 char szCellA[ cchRESOURCE ];
140 char szUserA[ cchRESOURCE ];
141 char szPasswordA[ cchRESOURCE ];
142 CopyStringToAnsi (szCellA, pszCell);
143 CopyStringToAnsi (szUserA, pszUser);
144 CopyStringToAnsi (szPasswordA, pszPassword);
146 afsclient_TokenGetNew (szCellA, szUserA, szPasswordA, (PVOID *)&hCreds, (afs_status_p)&status);
148 CloseClientLibrary();
151 if (hCreds)
153 PostMessage (AfsAppLib_GetMainWindow(), WM_REFRESHED_CREDENTIALS, 0, (LPARAM)hCreds);
156 if (!hCreds && pStatus)
157 *pStatus = status;
158 return hCreds;
163 * OPEN CELL DIALOG ___________________________________________________________
167 BOOL AfsAppLib_ShowOpenCellDialog (LPOPENCELLDLG_PARAMS lpp)
169 HINSTANCE hInst = APP_HINST;
170 if (lpp->idd == 0)
172 hInst = APPLIB_HINST;
173 lpp->idd = IDD_APPLIB_OPENCELL;
175 if (lpp->hCreds == 0)
177 if (lpp->szCell[0])
178 lpp->hCreds = AfsAppLib_GetCredentials (lpp->szCell);
179 else
180 lpp->hCreds = AfsAppLib_GetCredentials (NULL);
183 INT_PTR rc = ModalDialogParam (lpp->idd, lpp->hParent, (DLGPROC)OpenCell_DlgProc, (LPARAM)lpp);
185 return (rc == IDOK) ? TRUE : FALSE;
189 HRESULT CALLBACK OpenCell_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
191 LPOPENCELLDLG_PARAMS lpp;
192 if (msg == WM_INITDIALOG)
193 SetWindowLongPtr (hDlg, DWLP_USER, lp);
195 if ((lpp = (LPOPENCELLDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
197 if (lpp->hookproc)
199 if (CallWindowProc ((WNDPROC)lpp->hookproc, hDlg, msg, wp, lp))
200 return TRUE;
204 if (lpp != NULL)
206 if (AfsAppLib_HandleHelp (lpp->idd, hDlg, msg, wp, lp))
207 return TRUE;
210 switch (msg)
212 case WM_INITDIALOG:
213 OpenCell_OnInitDialog (hDlg, lpp);
214 break;
216 case WM_COMMAND:
217 switch (LOWORD(wp))
219 case IDCANCEL:
220 EndDialog (hDlg, IDCANCEL);
221 return TRUE;
223 case IDOK:
224 if (OpenCell_OnOK (hDlg, lpp))
225 EndDialog (hDlg, IDOK);
226 return TRUE;
228 case IDC_OPENCELL_CELL:
229 switch (HIWORD(wp))
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;
238 case CBN_EDITCHANGE:
239 OpenCell_OnCell (hDlg);
240 break;
242 break;
244 break;
246 case WM_REFRESHED_CREDENTIALS:
247 OpenCell_OnGotCreds (hDlg, lp);
248 break;
251 return FALSE;
255 void OpenCell_OnInitDialog (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
257 // Fix the title of the dialog (unless the caller has supplied a
258 // custom dialog template)
260 if (lpp && (lpp->idd == IDD_APPLIB_OPENCELL))
262 TCHAR szApplication[ cchNAME ];
263 AfsAppLib_GetAppName (szApplication);
264 if (szApplication[0] != TEXT('\0'))
266 TCHAR szTitle[ cchRESOURCE ];
267 GetWindowText (hDlg, szTitle, cchRESOURCE);
268 lstrcat (szTitle, TEXT(" - "));
269 lstrcat (szTitle, szApplication);
270 SetWindowText (hDlg, szTitle);
274 // Fill in the 'Cell:' combobox; we'll list the default cell, and any
275 // cell which the user has specified before.
277 CB_StartChange (GetDlgItem (hDlg, IDC_OPENCELL_CELL), TRUE);
279 if (!lpp->lpcl)
281 TCHAR szDefCell[ cchNAME ];
282 if (AfsAppLib_GetLocalCell (szDefCell) && *szDefCell)
284 CB_AddItem (GetDlgItem (hDlg, IDC_OPENCELL_CELL), szDefCell, 1);
287 else for (size_t ii = 0; ii < lpp->lpcl->nCells; ++ii)
289 CB_AddItem (GetDlgItem (hDlg, IDC_OPENCELL_CELL), lpp->lpcl->aCells[ii], 1+ii);
292 CB_EndChange (GetDlgItem (hDlg, IDC_OPENCELL_CELL), 1);
294 // Set up the "Credentials" box; if the user needs credentials to edit
295 // this default cell, jump the cursor to the appropriate field
297 SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
299 OpenCell_OnCell (hDlg);
303 BOOL OpenCell_OnOK (HWND hDlg, LPOPENCELLDLG_PARAMS lpp)
305 BOOL rc = FALSE;
306 OpenCell_Enable (hDlg, FALSE);
307 StartHourGlass ();
309 // Remember what cell the user chose to edit
311 GetDlgItemText (hDlg, IDC_OPENCELL_CELL, lpp->szCell, cchNAME);
313 // Try to obtain the credentials specified by the user.
315 TCHAR szCell[ cchNAME ];
316 GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
318 TCHAR szUser[ cchRESOURCE ];
319 GetDlgItemText (hDlg, IDC_OPENCELL_ID, szUser, cchNAME);
321 TCHAR szPassword[ cchRESOURCE ];
322 GetDlgItemText (hDlg, IDC_OPENCELL_PASSWORD, szPassword, cchNAME);
324 ULONG status;
325 if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
327 ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
329 else
331 // See if those credentials are sufficient
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;
340 if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
342 SetDlgItemText (hDlg, IDC_OPENCELL_ID, TEXT("admin"));
343 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
347 if (!rc)
348 OpenCell_Enable (hDlg, TRUE);
349 StopHourGlass ();
350 return rc;
354 typedef struct
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;
364 DWORD WINAPI OpenCell_OnCell_ThreadProc (PVOID lp)
366 LPOPENCELL_ONCELL_PARAMS lpp;
367 if ((lpp = (LPOPENCELL_ONCELL_PARAMS)lp) != NULL)
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;
373 if (lpp->fGotCreds && AfsAppLib_IsTimeInFuture (&lpp->stExpire))
375 CHECKCREDS_PARAMS pp;
376 memset (&pp, 0x00, sizeof(pp));
377 pp.hCreds = hCreds;
378 lpp->fValidCreds = AfsAppLib_CheckCredentials(&pp);
381 // Post our results; we'll return the same packet we got.
383 if (IsWindow (lpp->hDlg))
384 PostMessage (lpp->hDlg, WM_REFRESHED_CREDENTIALS, 0, (LPARAM)lpp);
385 else
386 Delete (lpp);
389 return 0;
393 void OpenCell_OnCell (HWND hDlg)
395 // Fire up a background thread to query our current credentials in the
396 // selected cell.
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;
403 DWORD dwThreadID;
404 CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)OpenCell_OnCell_ThreadProc, lpp, 0, &dwThreadID);
408 void OpenCell_OnGotCreds (HWND hDlg, LPARAM lp)
410 LPOPENCELL_ONCELL_PARAMS lpp;
411 if ((lpp = (LPOPENCELL_ONCELL_PARAMS)lp) != NULL)
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.
416 TCHAR szCell[ cchNAME ];
417 GetDlgItemText (hDlg, IDC_OPENCELL_CELL, szCell, cchNAME);
419 if (!lstrcmpi (szCell, lpp->szCell))
421 TCHAR szPrevCreds[ cchRESOURCE ];
422 GetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, szPrevCreds, cchRESOURCE);
424 if (!lpp->fGotCreds)
426 LPTSTR psz = FormatString (IDS_CREDS_NONE, TEXT("%s"), lpp->szCell);
427 SetDlgItemText (hDlg, IDC_OPENCELL_OLDCREDS, psz);
428 FreeString (psz);
430 else if (!AfsAppLib_IsTimeInFuture (&lpp->stExpire))
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);
436 else
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);
443 SetDlgItemText (hDlg, IDC_OPENCELL_ID, (lpp->fGotCreds) ? lpp->szUser : TEXT("admin"));
445 if (!lpp->fValidCreds && !szPrevCreds[0])
446 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_OPENCELL_PASSWORD), TRUE);
449 Delete (lpp);
454 void OpenCell_Enable (HWND hDlg, BOOL fEnable)
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);
466 * NEW CREDENTIALS DIALOG _____________________________________________________
470 BOOL AfsAppLib_ShowCredentialsDialog (LPCREDENTIALSDLG_PARAMS lpp)
472 HINSTANCE hInst = APP_HINST;
473 if (lpp->idd == 0)
475 hInst = APPLIB_HINST;
476 lpp->idd = IDD_APPLIB_CREDENTIALS;
478 if (lpp->hCreds == 0)
480 if (lpp->szCell[0])
481 lpp->hCreds = AfsAppLib_GetCredentials (lpp->szCell);
482 else
483 lpp->hCreds = AfsAppLib_GetCredentials (NULL);
486 INT_PTR rc = ModalDialogParam (lpp->idd, lpp->hParent, (DLGPROC)NewCreds_DlgProc, (LPARAM)lpp);
488 return (rc == IDOK) ? TRUE : FALSE;
492 HRESULT CALLBACK NewCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
494 LPCREDENTIALSDLG_PARAMS lpp;
495 if (msg == WM_INITDIALOG)
496 SetWindowLongPtr (hDlg, DWLP_USER, lp);
498 if ((lpp = (LPCREDENTIALSDLG_PARAMS)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
500 if (lpp->hookproc)
502 if (CallWindowProc ((WNDPROC)lpp->hookproc, hDlg, msg, wp, lp))
503 return TRUE;
507 if (lpp != NULL)
509 if (AfsAppLib_HandleHelp (lpp->idd, hDlg, msg, wp, lp))
510 return TRUE;
513 switch (msg)
515 case WM_INITDIALOG:
516 NewCreds_OnInitDialog (hDlg, lpp);
517 break;
519 case WM_COMMAND:
520 switch (LOWORD(wp))
522 case IDCANCEL:
523 EndDialog (hDlg, IDCANCEL);
524 return TRUE;
526 case IDOK:
527 if (NewCreds_OnOK (hDlg, lpp))
528 EndDialog (hDlg, IDOK);
529 return TRUE;
531 case IDC_CREDS_LOGIN:
532 NewCreds_OnLogin (hDlg);
533 break;
535 break;
538 return FALSE;
542 void NewCreds_OnInitDialog (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
544 // Fix the title of the dialog (unless the caller has supplied a
545 // custom dialog template)
547 if (lpp && (lpp->idd == IDD_APPLIB_CREDENTIALS))
549 TCHAR szApplication[ cchNAME ];
550 AfsAppLib_GetAppName (szApplication);
551 if (szApplication[0] != TEXT('\0'))
553 TCHAR szTitle[ cchRESOURCE ];
554 GetWindowText (hDlg, szTitle, cchRESOURCE);
555 lstrcat (szTitle, TEXT(" - "));
556 lstrcat (szTitle, szApplication);
557 SetWindowText (hDlg, szTitle);
561 // Set up the "Credentials" boxes
563 TCHAR szUser[ cchRESOURCE ];
564 SYSTEMTIME st;
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;
575 if (!AfsAppLib_CrackCredentials (lpp->hCreds, lpp->szCell, szUser, &st))
577 fValidCreds = FALSE;
578 fShowCreds = FALSE;
580 if (!AfsAppLib_IsTimeInFuture (&st))
582 fValidCreds = FALSE;
583 fShowCreds = FALSE;
585 if (!AfsAppLib_CheckCredentials (&pp))
587 fValidCreds = FALSE;
590 CheckDlgButton (hDlg, IDC_CREDS_LOGIN, !fValidCreds);
591 if (!fValidCreds)
592 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_CREDS_PASSWORD), TRUE);
594 SetDlgItemText (hDlg, IDC_CREDS_CELL, lpp->szCell);
596 if (fShowCreds)
598 SetDlgItemText (hDlg, IDC_CREDS_CURRENTID, szUser);
600 LPTSTR pszDate = FormatString (TEXT("%1"), TEXT("%t"), &st);
601 SetDlgItemText (hDlg, IDC_CREDS_EXPDATE, pszDate);
602 FreeString (pszDate);
605 SetDlgItemText (hDlg, IDC_CREDS_ID, TEXT("admin"));
607 NewCreds_OnLogin (hDlg);
611 BOOL NewCreds_OnOK (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
613 NewCreds_GetOutParams (hDlg, lpp);
614 if (!IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN))
615 return TRUE;
617 BOOL rc = FALSE;
618 StartHourGlass ();
619 NewCreds_Enable (hDlg, FALSE);
621 // Try to obtain the credentials specified by the user.
623 TCHAR szUser[ cchRESOURCE ];
624 GetDlgItemText (hDlg, IDC_CREDS_ID, szUser, cchNAME);
626 TCHAR szPassword[ cchRESOURCE ];
627 GetDlgItemText (hDlg, IDC_CREDS_PASSWORD, szPassword, cchNAME);
629 ULONG status;
630 if ((lpp->hCreds = AfsAppLib_SetCredentials (lpp->szCell, szUser, szPassword, &status)) == NULL)
632 ErrorDialog (status, IDS_ERROR_BAD_CREDENTIALS);
634 else
636 // See if those credentials are sufficient
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;
645 if ((rc = AfsAppLib_CheckCredentials (&pp)) == FALSE)
647 SetDlgItemText (hDlg, IDC_CREDS_ID, TEXT("admin"));
648 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg,IDC_CREDS_PASSWORD), TRUE);
652 if (!rc)
653 NewCreds_Enable (hDlg, TRUE);
654 StopHourGlass ();
655 return rc;
659 void NewCreds_OnLogin (HWND hDlg)
661 BOOL fEnable = IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN);
663 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_ID), fEnable);
664 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_PASSWORD), fEnable);
668 void NewCreds_Enable (HWND hDlg, BOOL fEnable)
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);
675 if (fEnable)
676 NewCreds_OnLogin (hDlg);
677 else
679 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_ID), fEnable);
680 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_PASSWORD), fEnable);
685 void NewCreds_GetOutParams (HWND hDlg, LPCREDENTIALSDLG_PARAMS lpp)
687 if (!IsDlgButtonChecked (hDlg, IDC_CREDS_LOGIN))
689 lpp->szIdentity[0] = TEXT('\0');
690 lpp->szPassword[0] = TEXT('\0');
692 else
694 GetDlgItemText (hDlg, IDC_CREDS_ID, lpp->szIdentity, cchNAME);
695 GetDlgItemText (hDlg, IDC_CREDS_PASSWORD, lpp->szPassword, cchNAME);
701 * TEST CREDENTIALS ___________________________________________________________
706 BOOL AfsAppLib_IsUserAdmin (UINT_PTR hCreds, LPTSTR pszUser)
708 #ifndef USE_KASERVER
709 return TRUE;
710 #else
711 BOOL rc = FALSE;
712 afs_status_t status;
714 UINT_PTR idClient;
715 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
717 TCHAR szCell[ cchRESOURCE ];
718 TCHAR szUser[ cchRESOURCE ];
719 SYSTEMTIME stExpire;
720 if (asc_CredentialsCrack (idClient, hCreds, szCell, szUser, &stExpire, (ULONG*)&status))
722 ASID idCell;
723 if (asc_CellOpen (idClient, hCreds, szCell, AFSADMSVR_SCOPE_USERS, &idCell, (ULONG*)&status))
725 ASID idUser;
726 if (asc_ObjectFind (idClient, idCell, TYPE_USER, pszUser, &idUser, (ULONG*)&status))
728 ASOBJPROP Info;
729 if (asc_ObjectPropertiesGet (idClient, GET_ALL_DATA, idCell, idUser, &Info, (ULONG*)&status))
731 if (Info.u.UserProperties.fHaveKasInfo)
733 rc = Info.u.UserProperties.KASINFO.fIsAdmin;
737 asc_CellClose (idClient, idCell, (ULONG*)&status);
741 else if (OpenClientLibrary())
743 if (OpenKasLibrary())
745 char szUserA[ cchRESOURCE ], szUser2A[ cchRESOURCE ];
746 char szCellA[ cchRESOURCE ];
747 unsigned long dateExpire;
748 int fHasKasToken;
750 if (afsclient_TokenQuery (hCreds, &dateExpire, szUserA, szUser2A, szCellA, &fHasKasToken, (afs_status_p)&status))
752 PVOID hCell;
753 if (afsclient_CellOpen (szCellA, hCreds, &hCell, &status))
755 kas_identity_t Identity;
756 memset (&Identity, 0x00, sizeof(Identity));
757 CopyStringToAnsi (Identity.principal, pszUser);
759 kas_principalEntry_t Entry;
760 if (kas_PrincipalGet (hCell, NULL, &Identity, &Entry, &status))
762 if (Entry.adminSetting == KAS_ADMIN)
763 rc = TRUE;
766 afsclient_CellClose (hCell, (afs_status_p)&status);
770 CloseKasLibrary();
773 CloseClientLibrary();
776 return rc;
777 #endif /* USE_KASERVER */
781 typedef struct
783 BADCREDSDLG_PARAMS bcdp;
784 LPTSTR pszFullText;
785 PVOID hCreds;
786 } REALBADCREDSDLG_PARAMS, *LPREALBADCREDSDLG_PARAMS;
788 BOOL AfsAppLib_CheckCredentials (LPCHECKCREDS_PARAMS lpp)
790 BOOL fCredsOK = TRUE;
791 int idsWarning = IDS_BADCREDS_DESC_GENERAL;
793 TCHAR szCell[ cchNAME ];
794 TCHAR szUser[ cchNAME ];
795 SYSTEMTIME stExpire;
797 if (!AfsAppLib_CrackCredentials (lpp->hCreds, szCell, szUser, &stExpire))
799 fCredsOK = FALSE;
801 else if (!AfsAppLib_IsUserAdmin (lpp->hCreds, szUser))
803 fCredsOK = FALSE;
804 idsWarning = IDS_BADCREDS_DESC_BADCHOICE;
806 else if (!AfsAppLib_IsTimeInFuture (&stExpire))
808 fCredsOK = FALSE;
809 idsWarning = IDS_BADCREDS_DESC_EXPIRED;
812 if (!fCredsOK && lpp->fShowWarning)
814 if (lpp->bcdp.pfShowWarningEver && !(*lpp->bcdp.pfShowWarningEver))
816 fCredsOK = TRUE; // user says always ignore bad credentials.
818 else
820 if (!szCell[0])
821 AfsAppLib_GetLocalCell (szCell);
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);
828 REALBADCREDSDLG_PARAMS pp;
829 memset (&pp, 0x00, sizeof(pp));
830 pp.pszFullText = pszFullText;
831 memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
833 HINSTANCE hInst = APP_HINST;
834 if (pp.bcdp.idd == 0)
836 hInst = APPLIB_HINST;
837 pp.bcdp.idd = IDD_APPLIB_BADCREDS;
840 INT_PTR rc = ModalDialogParam (pp.bcdp.idd, pp.bcdp.hParent, (DLGPROC)BadCreds_DlgProc, (LPARAM)&pp);
841 if (rc == IDCANCEL)
843 fCredsOK = TRUE; // user says ignore bad credentials this time.
846 FreeString (pszFullText);
850 return fCredsOK;
854 HRESULT CALLBACK BadCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
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)
861 if (lpp->bcdp.hookproc)
863 if (CallWindowProc ((WNDPROC)lpp->bcdp.hookproc, hDlg, msg, wp, lp))
864 return TRUE;
866 if (AfsAppLib_HandleHelp (lpp->bcdp.idd, hDlg, msg, wp, lp))
867 return TRUE;
870 switch (msg)
872 case WM_INITDIALOG:
873 TCHAR szApplication[ cchNAME ];
874 AfsAppLib_GetAppName (szApplication);
875 if (szApplication[0] != TEXT('\0'))
877 TCHAR szTitle[ cchRESOURCE ];
878 GetWindowText (hDlg, szTitle, cchRESOURCE);
879 lstrcat (szTitle, TEXT(" - "));
880 lstrcat (szTitle, szApplication);
881 SetWindowText (hDlg, szTitle);
883 if (!lpp || !lpp->bcdp.pfShowWarningEver)
884 DestroyWindow (GetWindow (hDlg, IDC_BADCREDS_SHUTUP));
886 SetDlgItemText (hDlg, IDC_BADCREDS_DESC, lpp->pszFullText);
887 break;
889 case WM_COMMAND:
890 switch (LOWORD(wp))
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;
899 break;
902 return FALSE;
907 * EXPIRED CREDENTIALS ________________________________________________________
911 void AfsAppLib_CheckForExpiredCredentials (LPCREDENTIALSDLG_PARAMS lpp)
913 static UINT_PTR hCredsPrevious = 0;
914 static BOOL fHadGoodCredentials;
916 TCHAR szCell[ cchNAME ];
917 TCHAR szUser[ cchNAME ];
918 SYSTEMTIME stExpire;
920 BOOL fHaveCredentials;
921 fHaveCredentials = AfsAppLib_CrackCredentials (lpp->hCreds, szCell, szUser, &stExpire)?TRUE:FALSE;
923 if (hCredsPrevious && (hCredsPrevious != lpp->hCreds))
925 if (!fHaveCredentials)
926 fHadGoodCredentials = FALSE;
927 else if (!AfsAppLib_IsTimeInFuture (&stExpire))
928 fHadGoodCredentials = FALSE;
929 else
930 fHadGoodCredentials = TRUE;
931 hCredsPrevious = lpp->hCreds;
933 else if (fHaveCredentials && AfsAppLib_IsTimeInFuture (&stExpire))
935 fHadGoodCredentials = TRUE;
937 else if (fHadGoodCredentials) // used to have good credentials, but now don't
939 fHadGoodCredentials = FALSE;
941 LPCREDENTIALSDLG_PARAMS lppCopy = New (CREDENTIALSDLG_PARAMS);
942 memcpy (lppCopy, lpp, sizeof(CREDENTIALSDLG_PARAMS));
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);
952 void OnExpiredCredentials (WPARAM wp, LPARAM lp)
954 BOOL fExpired = (BOOL)wp;
955 LPCREDENTIALSDLG_PARAMS lpp = (LPCREDENTIALSDLG_PARAMS)lp;
957 if (lpp && lpp->bcdp.pfShowWarningEver && *(lpp->bcdp.pfShowWarningEver))
959 int idsWarning = (wp) ? IDS_BADCREDS_DESC_EXPIRED : IDS_BADCREDS_DESC_DESTROYED;
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);
966 REALBADCREDSDLG_PARAMS pp;
967 memset (&pp, 0x00, sizeof(pp));
968 pp.pszFullText = pszFullText;
969 memcpy (&pp.bcdp, &lpp->bcdp, sizeof(BADCREDSDLG_PARAMS));
971 if (ModalDialogParam (IDD_APPLIB_BADCREDS, NULL, (DLGPROC)BadCreds_DlgProc, (LPARAM)&pp) != IDCANCEL)
973 AfsAppLib_ShowCredentialsDialog (lpp);
976 FreeString (pszFullText);
979 if (lpp)
981 Delete (lpp);