Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / client_creds / credstab.cpp
blob7fec00aa873c22a129b731ffbef0e01878fd79c9
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 "afscreds.h"
22 * PROTOTYPES _________________________________________________________________
26 void Creds_OnUpdate (HWND hDlg);
27 void Creds_OnCheckRemind (HWND hDlg);
28 void Creds_OnClickObtain (HWND hDlg);
29 void Creds_OnClickDestroy (HWND hDlg);
31 BOOL CALLBACK NewCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
32 void NewCreds_OnInitDialog (HWND hDlg);
33 void NewCreds_OnEnable (HWND hDlg);
34 BOOL NewCreds_OnOK (HWND hDlg);
35 void NewCreds_OnCancel (HWND hDlg);
39 * ROUTINES ___________________________________________________________________
43 BOOL CALLBACK Creds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
45 switch (msg)
47 case WM_INITDIALOG:
48 RECT rTab;
49 GetClientRect (GetParent(hDlg), &rTab);
50 TabCtrl_AdjustRect (GetParent (hDlg), FALSE, &rTab);
51 SetWindowPos (hDlg, NULL, rTab.left, rTab.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
53 SetWindowLongPtr (hDlg, DWLP_USER, lp);
54 Creds_OnUpdate (hDlg);
55 break;
57 case WM_COMMAND:
58 switch (LOWORD(wp))
60 case IDC_CREDS_REMIND:
61 Creds_OnCheckRemind (hDlg);
62 break;
64 case IDC_CREDS_OBTAIN:
65 Creds_OnClickObtain (hDlg);
66 break;
68 case IDC_CREDS_DESTROY:
69 Creds_OnClickDestroy (hDlg);
70 break;
72 case IDHELP:
73 Creds_DlgProc (hDlg, WM_HELP, 0, 0);
74 break;
76 break;
78 case WM_HELP:
79 if (IsWindow (GetDlgItem (hDlg, IDC_CREDS_REMIND)))
80 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_TOKENS);
81 else if (IsServiceRunning())
82 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_NOTOKENS_RUNNING);
83 else // (!IsServiceRunning())
84 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_NOTOKENS_STOPPED);
85 break;
88 return FALSE;
92 void Creds_OnCheckRemind (HWND hDlg)
94 LPTSTR pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
95 lock_ObtainMutex(&g.credsLock);
96 size_t iCreds;
97 for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
99 if (!lstrcmpi (g.aCreds[ iCreds ].szCell, pszCell))
100 break;
103 if (iCreds != g.cCreds)
105 g.aCreds[ iCreds ].fRemind = IsDlgButtonChecked (hDlg, IDC_CREDS_REMIND);
106 SaveRemind (iCreds);
108 lock_ReleaseMutex(&g.credsLock);
112 void Creds_OnUpdate (HWND hDlg)
114 LPTSTR pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
115 if (!pszCell || !*pszCell)
117 BOOL fRunning = IsServiceRunning();
118 ShowWindow (GetDlgItem (hDlg, IDC_RUNNING), fRunning);
119 ShowWindow (GetDlgItem (hDlg, IDC_STOPPED), !fRunning);
120 ShowWindow (GetDlgItem (hDlg, IDC_CREDS_OBTAIN), fRunning);
121 return;
124 lock_ObtainMutex(&g.credsLock);
125 size_t iCreds;
126 for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
128 if (!lstrcmpi (g.aCreds[ iCreds ].szCell, pszCell))
129 break;
132 TCHAR szGateway[cchRESOURCE] = TEXT("");
133 if (!g.fIsWinNT)
134 GetGatewayName (szGateway);
136 if (!szGateway[0])
138 SetDlgItemText (hDlg, IDC_CREDS_CELL, pszCell);
140 else
142 TCHAR szCell[ cchRESOURCE ];
143 TCHAR szFormat[ cchRESOURCE ];
144 GetString (szFormat, IDS_CELL_GATEWAY);
145 wsprintf (szCell, szFormat, pszCell, szGateway);
146 SetDlgItemText (hDlg, IDC_CREDS_CELL, szCell);
149 if (iCreds == g.cCreds)
151 TCHAR szText[cchRESOURCE];
152 GetString (szText, IDS_NO_CREDS);
153 SetDlgItemText (hDlg, IDC_CREDS_INFO, szText);
155 else
157 // FormatString(%t) expects a date in GMT, not the local time zone...
159 FILETIME ftLocal;
160 SystemTimeToFileTime (&g.aCreds[ iCreds ].stExpires, &ftLocal);
162 FILETIME ftGMT;
163 LocalFileTimeToFileTime (&ftLocal, &ftGMT);
165 SYSTEMTIME stGMT;
166 FileTimeToSystemTime (&ftGMT, &stGMT);
168 SYSTEMTIME stNow;
169 GetLocalTime (&stNow);
171 FILETIME ftNow;
172 SystemTimeToFileTime (&stNow, &ftNow);
174 LONGLONG llNow = (((LONGLONG)ftNow.dwHighDateTime) << 32) + (LONGLONG)(ftNow.dwLowDateTime);
175 LONGLONG llExpires = (((LONGLONG)ftLocal.dwHighDateTime) << 32) + (LONGLONG)(ftLocal.dwLowDateTime);
177 llNow /= c100ns1SECOND;
178 llExpires /= c100ns1SECOND;
180 LPTSTR pszCreds = NULL;
181 if (llExpires <= (llNow + (LONGLONG)cminREMIND_WARN * csec1MINUTE))
182 pszCreds = FormatString (IDS_CREDS_EXPIRED, TEXT("%s"), g.aCreds[ iCreds ].szUser);
184 if (!pszCreds || !pszCreds[0])
185 pszCreds = FormatString (IDS_CREDS, TEXT("%s%t"), g.aCreds[ iCreds ].szUser, &stGMT);
186 SetDlgItemText (hDlg, IDC_CREDS_INFO, pszCreds);
187 FreeString (pszCreds);
190 lock_ReleaseMutex(&g.credsLock);
191 CheckDlgButton (hDlg, IDC_CREDS_REMIND, (iCreds == g.cCreds) ? FALSE : g.aCreds[iCreds].fRemind);
193 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_OBTAIN), IsServiceRunning());
194 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_REMIND), (iCreds != g.cCreds));
195 EnableWindow (GetDlgItem (hDlg, IDC_CREDS_DESTROY), (iCreds != g.cCreds));
199 void Creds_OnClickObtain (HWND hDlg)
201 LPTSTR pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
203 InterlockedIncrement (&g.fShowingMessage);
204 ShowObtainCreds (FALSE, pszCell);
208 void Creds_OnClickDestroy (HWND hDlg)
210 LPTSTR pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
211 if (pszCell && *pszCell)
213 DestroyCurrentCredentials (pszCell);
214 Main_RepopulateTabs (FALSE);
215 Creds_OnUpdate (hDlg);
220 struct _obtaincreds {
221 DWORD type;
222 HWND parent;
223 char * cell;
226 void ObtainCredsThread(void * data)
228 struct _obtaincreds * oc = (struct _obtaincreds *)data;
230 ModalDialogParam (oc->type, oc->parent, (DLGPROC)NewCreds_DlgProc, (LPARAM)oc->cell);
231 free(oc->cell);
232 free(oc);
235 void ShowObtainCreds (BOOL fExpiring, LPTSTR pszCell)
237 struct _obtaincreds * oc = (struct _obtaincreds *)malloc(sizeof(struct _obtaincreds));
238 if ( !oc )
239 return;
240 oc->parent = (IsWindowVisible (g.hMain)) ? g.hMain : NULL;
241 oc->type = fExpiring ? IDD_NEWCREDS_EXPIRE : IDD_NEWCREDS;
242 oc->cell = _strdup(pszCell);
244 HANDLE thread = 0;
245 ULONG threadID = 123;
247 thread = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)ObtainCredsThread,
248 oc, 0, &threadID);
249 if (thread != NULL)
250 CloseHandle(thread);
251 else {
252 free(oc->cell);
253 free(oc);
258 BOOL CALLBACK NewCreds_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
260 switch (msg)
262 case WM_INITDIALOG:
263 SetWindowLongPtr (hDlg, DWLP_USER, lp);
264 NewCreds_OnInitDialog (hDlg);
265 break;
267 case WM_DESTROY:
268 InterlockedDecrement (&g.fShowingMessage);
269 break;
271 case WM_COMMAND:
272 switch (LOWORD(wp))
274 case IDOK:
275 if (NewCreds_OnOK (hDlg))
276 EndDialog (hDlg, IDOK);
277 break;
279 case IDCANCEL:
280 NewCreds_OnCancel (hDlg);
281 EndDialog (hDlg, IDCANCEL);
282 break;
284 case IDC_NEWCREDS_USER:
285 case IDC_NEWCREDS_PASSWORD:
286 NewCreds_OnEnable (hDlg);
287 break;
289 case IDHELP:
290 NewCreds_DlgProc (hDlg, WM_HELP, 0, 0);
291 break;
293 break;
295 case WM_HELP:
296 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_NEWTOKENS);
297 break;
299 return FALSE;
303 void NewCreds_OnInitDialog (HWND hDlg)
305 LPTSTR pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
306 if (!pszCell)
307 pszCell = TEXT("");
309 if (GetDlgItem (hDlg, IDC_NEWCREDS_TITLE))
311 TCHAR szText[ cchRESOURCE ];
312 GetDlgItemText (hDlg, IDC_NEWCREDS_TITLE, szText, cchRESOURCE);
313 LPTSTR pszText = FormatString (szText, TEXT("%s"), pszCell);
314 SetDlgItemText (hDlg, IDC_NEWCREDS_TITLE, pszText);
315 FreeString (pszText);
318 if (pszCell && *pszCell)
320 SetDlgItemText (hDlg, IDC_NEWCREDS_CELL, pszCell);
322 else
324 TCHAR szCell[ cchRESOURCE ] = TEXT("");
325 (void)GetDefaultCell (szCell);
326 SetDlgItemText (hDlg, IDC_NEWCREDS_CELL, szCell);
329 lock_ObtainMutex(&g.credsLock);
330 size_t iCreds;
331 for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
333 if (*pszCell && !lstrcmpi (g.aCreds[ iCreds ].szCell, pszCell))
334 break;
336 if ((iCreds == g.cCreds) || (!g.aCreds[ iCreds ].szUser[0]))
338 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (hDlg, IDC_NEWCREDS_USER), TRUE);
340 else // (we have a valid username already)
342 SetDlgItemText (hDlg, IDC_NEWCREDS_USER, g.aCreds[ iCreds ].szUser);
343 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (hDlg, IDC_NEWCREDS_PASSWORD), TRUE);
345 lock_ReleaseMutex(&g.credsLock);
347 NewCreds_OnEnable (hDlg);
348 SetForegroundWindow(hDlg);
349 KillTimer (g.hMain, ID_SERVICE_TIMER);
353 void NewCreds_OnEnable (HWND hDlg)
355 BOOL fEnable = TRUE;
357 TCHAR szUser[ cchRESOURCE ];
358 GetDlgItemText (hDlg, IDC_NEWCREDS_USER, szUser, cchRESOURCE);
359 if (!szUser[0])
360 fEnable = FALSE;
362 if ( !strchr(szUser, '@') ) {
363 TCHAR szPassword[ cchRESOURCE ];
364 GetDlgItemText (hDlg, IDC_NEWCREDS_PASSWORD, szPassword, cchRESOURCE);
365 if (!szPassword[0])
366 fEnable = FALSE;
368 EnableWindow (GetDlgItem (hDlg, IDOK), fEnable);
372 BOOL NewCreds_OnOK (HWND hDlg)
374 TCHAR szCell[ cchRESOURCE ];
375 GetDlgItemText (hDlg, IDC_NEWCREDS_CELL, szCell, cchRESOURCE);
377 TCHAR szUser[ cchRESOURCE ];
378 GetDlgItemText (hDlg, IDC_NEWCREDS_USER, szUser, cchRESOURCE);
380 TCHAR szPassword[ cchRESOURCE ];
381 GetDlgItemText (hDlg, IDC_NEWCREDS_PASSWORD, szPassword, cchRESOURCE);
383 int rc;
384 if ((rc = ObtainNewCredentials (szCell, szUser, szPassword, FALSE)) != 0)
386 EnableWindow (GetDlgItem (hDlg, IDOK), TRUE);
387 EnableWindow (GetDlgItem (hDlg, IDCANCEL), TRUE);
388 EnableWindow (GetDlgItem (hDlg, IDC_NEWCREDS_CELL), TRUE);
389 EnableWindow (GetDlgItem (hDlg, IDC_NEWCREDS_USER), TRUE);
390 EnableWindow (GetDlgItem (hDlg, IDC_NEWCREDS_PASSWORD), TRUE);
391 return FALSE;
394 Main_RepopulateTabs (FALSE);
395 return TRUE;
399 void NewCreds_OnCancel (HWND hDlg)
401 TCHAR szText[ cchRESOURCE ] = "";
402 LPTSTR pszCell = NULL;
404 if (GetDlgItem (hDlg, IDC_NEWCREDS_CELL))
406 GetDlgItemText (hDlg, IDC_NEWCREDS_CELL, szText, cchRESOURCE);
407 if ( szText[0] )
408 pszCell = szText;
411 if ( !pszCell )
412 pszCell = (LPTSTR)GetWindowLongPtr (hDlg, DWLP_USER);
413 if (pszCell)
415 HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
416 LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, TabCtrl_GetCurSel(hTab));
417 HWND hChildDlg = NULL;
419 lock_ObtainMutex(&g.credsLock);
420 for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
422 if (!lstrcmpi (g.aCreds[ iCreds ].szCell, pszCell))
424 g.aCreds[ iCreds ].fRemind = FALSE;
425 SaveRemind (iCreds);
427 // Check the active tab, and fix its checkbox if necessary
429 if (pszTab && HIWORD(pszTab) && (!lstrcmpi (pszTab, pszCell)))
431 hChildDlg = GetTabChild (hTab);
435 lock_ReleaseMutex(&g.credsLock);
437 if (hChildDlg)
438 CheckDlgButton (hChildDlg, IDC_CREDS_REMIND, FALSE);