2 * Copyright 2000, International Business Machines Corporation and others.
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
14 #include <afs/param.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
)
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
);
60 case IDC_CREDS_REMIND
:
61 Creds_OnCheckRemind (hDlg
);
64 case IDC_CREDS_OBTAIN
:
65 Creds_OnClickObtain (hDlg
);
68 case IDC_CREDS_DESTROY
:
69 Creds_OnClickDestroy (hDlg
);
73 Creds_DlgProc (hDlg
, WM_HELP
, 0, 0);
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
);
92 void Creds_OnCheckRemind (HWND hDlg
)
94 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
95 lock_ObtainMutex(&g
.credsLock
);
97 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
99 if (!lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
103 if (iCreds
!= g
.cCreds
)
105 g
.aCreds
[ iCreds
].fRemind
= IsDlgButtonChecked (hDlg
, IDC_CREDS_REMIND
);
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
);
124 lock_ObtainMutex(&g
.credsLock
);
126 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
128 if (!lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
132 TCHAR szGateway
[cchRESOURCE
] = TEXT("");
134 GetGatewayName (szGateway
);
138 SetDlgItemText (hDlg
, IDC_CREDS_CELL
, pszCell
);
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
);
157 // FormatString(%t) expects a date in GMT, not the local time zone...
160 SystemTimeToFileTime (&g
.aCreds
[ iCreds
].stExpires
, &ftLocal
);
163 LocalFileTimeToFileTime (&ftLocal
, &ftGMT
);
166 FileTimeToSystemTime (&ftGMT
, &stGMT
);
169 GetLocalTime (&stNow
);
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
{
226 void ObtainCredsThread(void * data
)
228 struct _obtaincreds
* oc
= (struct _obtaincreds
*)data
;
230 ModalDialogParam (oc
->type
, oc
->parent
, (DLGPROC
)NewCreds_DlgProc
, (LPARAM
)oc
->cell
);
235 void ShowObtainCreds (BOOL fExpiring
, LPTSTR pszCell
)
237 struct _obtaincreds
* oc
= (struct _obtaincreds
*)malloc(sizeof(struct _obtaincreds
));
240 oc
->parent
= (IsWindowVisible (g
.hMain
)) ? g
.hMain
: NULL
;
241 oc
->type
= fExpiring
? IDD_NEWCREDS_EXPIRE
: IDD_NEWCREDS
;
242 oc
->cell
= _strdup(pszCell
);
245 ULONG threadID
= 123;
247 thread
= CreateThread(NULL
, 0, (PTHREAD_START_ROUTINE
)ObtainCredsThread
,
258 BOOL CALLBACK
NewCreds_DlgProc (HWND hDlg
, UINT msg
, WPARAM wp
, LPARAM lp
)
263 SetWindowLongPtr (hDlg
, DWLP_USER
, lp
);
264 NewCreds_OnInitDialog (hDlg
);
268 InterlockedDecrement (&g
.fShowingMessage
);
275 if (NewCreds_OnOK (hDlg
))
276 EndDialog (hDlg
, IDOK
);
280 NewCreds_OnCancel (hDlg
);
281 EndDialog (hDlg
, IDCANCEL
);
284 case IDC_NEWCREDS_USER
:
285 case IDC_NEWCREDS_PASSWORD
:
286 NewCreds_OnEnable (hDlg
);
290 NewCreds_DlgProc (hDlg
, WM_HELP
, 0, 0);
296 WinHelp (hDlg
, g
.szHelpFile
, HELP_CONTEXT
, IDH_AFSCREDS_NEWTOKENS
);
303 void NewCreds_OnInitDialog (HWND hDlg
)
305 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
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
);
324 TCHAR szCell
[ cchRESOURCE
] = TEXT("");
325 (void)GetDefaultCell (szCell
);
326 SetDlgItemText (hDlg
, IDC_NEWCREDS_CELL
, szCell
);
329 lock_ObtainMutex(&g
.credsLock
);
331 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
333 if (*pszCell
&& !lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
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
)
357 TCHAR szUser
[ cchRESOURCE
];
358 GetDlgItemText (hDlg
, IDC_NEWCREDS_USER
, szUser
, cchRESOURCE
);
362 if ( !strchr(szUser
, '@') ) {
363 TCHAR szPassword
[ cchRESOURCE
];
364 GetDlgItemText (hDlg
, IDC_NEWCREDS_PASSWORD
, szPassword
, cchRESOURCE
);
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
);
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
);
394 Main_RepopulateTabs (FALSE
);
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
);
412 pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
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
;
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
);
438 CheckDlgButton (hChildDlg
, IDC_CREDS_REMIND
, FALSE
);