0a2cb1482c48ae9960949a70e1a5c4d56f029297
[openafs.git] / src / WINNT / client_creds / window.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 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "afscreds.h"
16 #include "afskfw.h"
17
18
19 /*
20  * DEFINITIONS ________________________________________________________________
21  *
22  */
23
24 #define ID_REMIND_TIMER      1000
25 #define ID_SERVICE_TIMER     1001
26
27 #define cREALLOC_TABS        4
28
29 #define dwTABPARAM_MOUNT     (LPTSTR)0
30 #define dwTABPARAM_ADVANCED  (LPTSTR)1
31 #define ISCELLTAB(_psz)      ((HIWORD((LONG)(_psz))) != 0)
32
33
34 /*
35  * PROTOTYPES _________________________________________________________________
36  *
37  */
38
39 void Main_OnInitDialog (HWND hDlg);
40 void Main_OnCheckMenuRemind (void);
41 void Main_OnRemindTimer (void);
42 void Main_OnMouseOver (void);
43 void Main_OnSelectTab (void);
44 void Main_OnCheckTerminate (void);
45 HWND Main_CreateTabDialog (HWND hTab, size_t iTab);
46
47 BOOL CALLBACK Terminate_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
48 void Terminate_OnInitDialog (HWND hDlg);
49 void Terminate_OnOK (HWND hDlg);
50
51
52 /*
53  * ROUTINES ___________________________________________________________________
54  *
55  */
56
57 BOOL CALLBACK Main_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
58 {
59    static UINT msgCheckTerminate = 0;
60    if (msgCheckTerminate == 0)
61       msgCheckTerminate = RegisterWindowMessage (TEXT("AfsCredsCheckTerminate"));
62
63    if (msg == msgCheckTerminate)
64       {
65       Main_OnCheckTerminate();
66       }
67    else switch (msg)
68       {
69       case WM_INITDIALOG:
70          g.hMain = hDlg;
71          Main_OnInitDialog (hDlg);
72          break;
73
74       case WM_DESTROY:
75          Creds_CloseLibraries();
76          ChangeTrayIcon (NIM_DELETE);
77          break;
78
79       case WM_ACTIVATEAPP:
80          if (wp)
81             {
82             Main_RepopulateTabs (FALSE);
83             }
84          break;
85
86       case WM_COMMAND:
87          switch (LOWORD(wp))
88             {
89             case IDOK:
90             case IDCANCEL:
91                Main_Show (FALSE);
92                break;
93
94             case M_ACTIVATE:
95                if (g.fIsWinNT || IsServiceRunning())
96                   {
97                   if (!lp) // Got here from "/show" parameter? switch tabs.
98                      {
99                      HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
100                      TabCtrl_SetCurSel (hTab, 0);
101                      Main_OnSelectTab();
102                      }
103                   Main_Show (TRUE);
104                   }
105                else
106                   {
107                   Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
108                   }
109                break;
110
111             case M_TERMINATE:
112                if (g.fIsWinNT && IsServiceRunning())
113                   ModalDialog (IDD_TERMINATE, NULL, (DLGPROC)Terminate_DlgProc);
114                else if (g.fIsWinNT)
115                   ModalDialog (IDD_TERMINATE_SMALL, NULL, (DLGPROC)Terminate_DlgProc);
116                else // (!g.fIsWinNT)
117                   ModalDialog (IDD_TERMINATE_SMALL_95, NULL, (DLGPROC)Terminate_DlgProc);
118                break;
119
120             case M_TERMINATE_NOW:
121                Quit();
122                break;
123
124             case M_REMIND:
125                Main_OnCheckMenuRemind();
126                break;
127             }
128          break;
129
130       case WM_TIMER:
131          Main_OnRemindTimer();
132          break;
133
134       case WM_NOTIFY:
135          switch (((NMHDR*)lp)->code)
136             {
137             case TCN_SELCHANGE:
138                Main_OnSelectTab();
139                break;
140             }
141          break;
142
143       case WM_TRAYICON:
144          switch (lp)
145             {
146             case WM_LBUTTONDOWN:
147                if (IsServiceRunning() || !IsServiceConfigured())
148                   Main_Show (TRUE);
149                else if (!g.fIsWinNT)
150                   Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
151                else
152                   ShowStartupWizard();
153                break;
154
155             case WM_RBUTTONDOWN:
156                HMENU hm;
157                if ((hm = TaLocale_LoadMenu (MENU_TRAYICON)) != 0)
158                   {
159                   POINT pt;
160                   GetCursorPos(&pt);
161
162                   HMENU hmDummy = CreateMenu();
163                   InsertMenu (hmDummy, 0, MF_POPUP, (UINT)hm, NULL);
164
165                   BOOL fRemind = FALSE;
166                   lock_ObtainMutex(&g.credsLock);
167                   for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
168                      {
169                      if (g.aCreds[ iCreds ].fRemind)
170                         fRemind = TRUE;
171                      }
172                   lock_ReleaseMutex(&g.credsLock);
173                   CheckMenuItem (hm, M_REMIND, MF_BYCOMMAND | ((fRemind) ? MF_CHECKED : MF_UNCHECKED));
174
175                   TrackPopupMenu (GetSubMenu (hmDummy, 0),
176                                   TPM_RIGHTALIGN | TPM_RIGHTBUTTON,
177                                   pt.x, pt.y, NULL, hDlg, NULL);
178
179                   DestroyMenu (hmDummy);
180                   }
181                break;
182
183             case WM_MOUSEMOVE:
184                Main_OnMouseOver();
185                break;
186             }
187          break;
188       case WM_OBTAIN_TOKENS:
189           if ( InterlockedIncrement (&g.fShowingMessage) != 1 )
190               InterlockedDecrement (&g.fShowingMessage);
191           else
192               ShowObtainCreds (wp, (char *)lp);
193           GlobalFree((void *)lp);
194           break;
195
196       case WM_START_SERVICE:
197           {
198               SC_HANDLE hManager;
199               if ((hManager = OpenSCManager ( NULL, NULL, 
200                                               SC_MANAGER_CONNECT |
201                                               SC_MANAGER_ENUMERATE_SERVICE |
202                                               SC_MANAGER_QUERY_LOCK_STATUS)) != NULL)
203               {
204                   SC_HANDLE hService;
205                   if ((hService = OpenService ( hManager, TEXT("TransarcAFSDaemon"), 
206                                                 SERVICE_QUERY_STATUS | SERVICE_START)) != NULL)
207                   {
208                       if (StartService (hService, 0, 0))
209                           TestAndDoMapShare(SERVICE_START_PENDING);
210                                   if ( KFW_is_available() && KFW_AFS_wait_for_service_start() ) {
211                                           KFW_AFS_renew_tokens_for_all_cells();
212                                                   }
213
214                       CloseServiceHandle (hService);
215                   }
216
217                   CloseServiceHandle (hManager);
218               }
219               KFW_AFS_wait_for_service_start();
220               ObtainTokensFromUserIfNeeded(g.hMain);
221           }
222           break;
223       }
224
225    return FALSE;
226 }
227
228
229 void Main_Show (BOOL fShow)
230 {
231    if (fShow)
232       {
233       ShowWindow (g.hMain, SW_SHOW);
234       SetWindowPos (g.hMain, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
235       SetForegroundWindow (g.hMain);
236       }
237    else
238       {
239       SetWindowPos (g.hMain, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_HIDEWINDOW);
240       }
241 }
242
243
244 void Main_OnInitDialog (HWND hDlg)
245 {
246    if (!g.fIsWinNT)
247       {
248       TCHAR szTitle[256];
249       GetString (szTitle, IDS_TITLE_95);
250       SetWindowText (hDlg, szTitle);
251       }
252
253    TCHAR szVersion[256];
254    DWORD dwPatch = 0;
255    TCHAR szUser[256];
256    GetString (szVersion, IDS_UNKNOWN);
257    GetString (szUser, IDS_UNKNOWN);
258
259    HKEY hk;
260    if (RegOpenKey (HKEY_LOCAL_MACHINE, REGSTR_PATH_AFS, &hk) == 0)
261       {
262       DWORD dwSize = sizeof(szVersion);
263       DWORD dwType = REG_SZ;
264       RegQueryValueEx (hk, REGVAL_AFS_VERSION, NULL, &dwType, (PBYTE)szVersion, &dwSize);
265
266       dwSize = sizeof(dwPatch);
267       dwType = REG_DWORD;
268       RegQueryValueEx (hk, REGVAL_AFS_PATCH, NULL, &dwType, (PBYTE)&dwPatch, &dwSize);
269       RegCloseKey (hk);
270       }
271
272    BOOL fFoundUserName = FALSE;
273     if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Explorer"), &hk) == 0)
274     {
275         DWORD dwSize = sizeof(szUser);
276         DWORD dwType = REG_SZ;
277         if (RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
278             fFoundUserName = TRUE;
279         RegCloseKey (hk);
280     }
281     if (!fFoundUserName ) 
282     {
283         if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hk) == 0)
284         {
285             DWORD dwSize = sizeof(szUser);
286             DWORD dwType = REG_SZ;
287             if (RegQueryValueEx (hk, TEXT("DefaultUserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
288                 fFoundUserName = TRUE;
289             RegCloseKey (hk);
290         }
291     }
292    if (!fFoundUserName)
293    {
294        if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Network\\Logon"), &hk) == 0)
295        {
296            DWORD dwSize = sizeof(szUser);
297            DWORD dwType = REG_SZ;
298            if (RegQueryValueEx (hk, TEXT("UserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
299                fFoundUserName = TRUE;
300            RegCloseKey (hk);
301        }
302    }
303
304    TCHAR szSource[ cchRESOURCE ];
305    TCHAR szTarget[ cchRESOURCE ];
306
307    GetString (szSource, (dwPatch) ? IDS_TITLE_VERSION : IDS_TITLE_VERSION_NOPATCH);
308    wsprintf (szTarget, szSource, szVersion, dwPatch);
309    SetDlgItemText (hDlg, IDC_TITLE_VERSION, szTarget);
310
311    GetDlgItemText (hDlg, IDC_TITLE_NT, szSource, cchRESOURCE);
312    wsprintf (szTarget, szSource, szUser);
313    SetDlgItemText (hDlg, IDC_TITLE_NT, szTarget);
314 }
315
316
317 void Main_OnCheckMenuRemind (void)
318 {
319    BOOL fRemind = FALSE;
320    lock_ObtainMutex(&g.credsLock);
321    for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
322       {
323       if (g.aCreds[ iCreds ].fRemind)
324          fRemind = TRUE;
325       }
326
327    fRemind = !fRemind;
328    for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
329       {
330       if (g.aCreds[ iCreds ].fRemind != fRemind)
331          {
332          g.aCreds[ iCreds ].fRemind = fRemind;
333          SaveRemind (iCreds);
334          }
335       }
336    lock_ReleaseMutex(&g.credsLock);
337
338    // Check the active tab, and fix its checkbox if necessary
339    //
340    HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
341    LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, TabCtrl_GetCurSel(hTab));
342    if (ISCELLTAB(pszTab) && (*pszTab))
343       {
344       HWND hDlg = GetTabChild (hTab);
345       if (hDlg)
346          CheckDlgButton (hDlg, IDC_CREDS_REMIND, fRemind);
347       }
348
349    // Make sure the reminder timer is going
350    //
351    Main_EnableRemindTimer (fRemind);
352 }
353
354
355 void Main_OnRemindTimer (void)
356 {
357    Main_RepopulateTabs (TRUE);
358
359    // See if anything is close to expiring; if so, display a warning
360    // dialog. Make sure we never display a warning more than once.
361    //
362    size_t iExpired;
363    if ((iExpired = Main_FindExpiredCreds()) != -1)
364       {
365       if (InterlockedIncrement (&g.fShowingMessage) != 1)
366          InterlockedDecrement (&g.fShowingMessage);
367       else
368          ShowObtainCreds (TRUE, g.aCreds[ iExpired ].szCell);
369       }
370 }
371
372
373 void Main_OnMouseOver (void)
374 {
375    if ((GetTickCount() - g.tickLastRetest) > cmsecMOUSEOVER)
376       {
377       Main_RepopulateTabs (TRUE);
378       }
379 }
380
381
382 void Main_OnSelectTab (void)
383 {
384    HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
385    size_t iTab = TabCtrl_GetCurSel (hTab);
386
387    HWND hDlgOld = GetTabChild (hTab);
388
389    HWND hDlgNew;
390    if ((hDlgNew = Main_CreateTabDialog (hTab, iTab)) != NULL)
391       ShowWindow (hDlgNew, SW_SHOW);
392
393    if (hDlgOld)
394       DestroyWindow (hDlgOld);
395 }
396
397
398 void Main_OnCheckTerminate (void)
399 {
400    HKEY hk;
401    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
402       {
403       DWORD dwSize = sizeof(g.fStartup);
404       DWORD dwType = REG_DWORD;
405       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
406       RegCloseKey (hk);
407       }
408
409    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
410
411    if (!g.fStartup)
412       Quit();
413 }
414
415
416 HWND Main_CreateTabDialog (HWND hTab, size_t iTab)
417 {
418    HWND hDlg = NULL;
419    LPTSTR psz = NULL;
420
421    TC_ITEM Item;
422    memset (&Item, 0x00, sizeof(Item));
423    Item.mask = TCIF_PARAM;
424    if (TabCtrl_GetItem (hTab, iTab, &Item))
425       {
426       psz = (LPTSTR)(Item.lParam);
427       }
428
429    if (psz == dwTABPARAM_ADVANCED)    // Advanced tab
430       {
431       hDlg = ModelessDialog (IDD_TAB_ADVANCED, hTab, (DLGPROC)Advanced_DlgProc);
432       }
433    else if (psz == dwTABPARAM_MOUNT)  // Mount Points tab
434       {
435       hDlg = ModelessDialog (IDD_TAB_MOUNT, hTab, (DLGPROC)Mount_DlgProc);
436       }
437    else if (ISCELLTAB(psz) && !*psz)  // No Creds tab
438       {
439       hDlg = ModelessDialogParam (IDD_TAB_NOCREDS, hTab, (DLGPROC)Creds_DlgProc, (LPARAM)psz);
440       }
441    else if (ISCELLTAB(psz) && *psz)   // Creds tab for a particular cell
442       {
443       hDlg = ModelessDialogParam (IDD_TAB_CREDS, hTab, (DLGPROC)Creds_DlgProc, (LPARAM)psz);
444       }
445
446    return hDlg;
447 }
448
449
450 void Main_RepopulateTabs (BOOL fDestroyInvalid)
451 {
452    static BOOL fInHere = FALSE;
453    if (!fInHere)
454       {
455       fInHere = TRUE;
456
457       if (IsWindowVisible (g.hMain))
458          fDestroyInvalid = FALSE;
459
460       // First we'll have to look around and see what credentials we currently
461       // have. This call just updates g.aCreds[]; it doesn't do anything else.
462       //
463       (void)GetCurrentCredentials();
464
465       // We want one tab on the main dialog for each g.aCredentials entry,
466       // and one tab for Advanced.
467       //
468       HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
469
470       // Generate a list of the lParams we'll be giving tabs...
471       //
472       LPTSTR *aTabs = NULL;
473       size_t cTabs = 0;
474       size_t iTabOut = 0;
475
476       size_t nCreds = 0;
477       lock_ObtainMutex(&g.credsLock);
478       for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
479          {
480          if (g.aCreds[ iCreds ].szCell[0])
481             ++nCreds;
482          }
483       if (!nCreds)
484          {
485          fDestroyInvalid = TRUE;
486          }
487
488       if (!fDestroyInvalid)
489          {
490          int nTabs = TabCtrl_GetItemCount(hTab);
491          for (int iTab = 0; iTab < nTabs; ++iTab)
492             {
493             LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, iTab);
494             if (ISCELLTAB(pszTab) && (*pszTab))
495                {
496                if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
497                   aTabs[ iTabOut++ ] = CloneString(pszTab);
498                }
499             }
500          }
501
502       if (nCreds == 0)
503          {
504          if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
505             aTabs[ iTabOut++ ] = CloneString (TEXT(""));
506          }
507       else for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
508          {
509          if (g.aCreds[ iCreds ].szCell[0])
510             {
511             for (size_t ii = 0; ii < iTabOut; ++ii)
512                {
513                if (!ISCELLTAB (aTabs[ii]))
514                   continue;
515                if (!lstrcmpi (g.aCreds[ iCreds ].szCell, aTabs[ ii ]))
516                   break;
517                }
518             if (ii == iTabOut)
519                {
520                if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
521                   aTabs[ iTabOut++ ] = CloneString (g.aCreds[ iCreds ].szCell);
522                }
523             }
524          }
525       lock_ReleaseMutex(&g.credsLock);
526
527       if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
528          aTabs[ iTabOut++ ] = dwTABPARAM_MOUNT;
529
530       if (g.fIsWinNT)
531          {
532          if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
533             aTabs[ iTabOut++ ] = dwTABPARAM_ADVANCED;
534          }
535
536       // Now erase the current tabs, and re-add new ones. Remember which tab is
537       // currently selected, so we can try to go back to it later.
538       //
539       int iTabSel = 0;
540       if (TabCtrl_GetItemCount(hTab))
541          {
542          LPTSTR pszTabSel = (LPTSTR)GetTabParam (hTab, TabCtrl_GetCurSel(hTab));
543          for (size_t iSel = 0; iSel < iTabOut; ++iSel)
544             {
545             if ((!ISCELLTAB(pszTabSel))  && (!ISCELLTAB(aTabs[iSel])) && (pszTabSel == aTabs[iSel]))
546                iTabSel = iSel;
547             else if (ISCELLTAB(pszTabSel) && ISCELLTAB(aTabs[iSel]) && !lstrcmpi (pszTabSel, aTabs[iSel]))
548                iTabSel = iSel;
549             }
550          }
551
552       int nTabs = TabCtrl_GetItemCount(hTab);
553       for (int iTab = 0; iTab < nTabs; ++iTab)
554          {
555          LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, iTab);
556          if (ISCELLTAB(pszTab))
557             FreeString (pszTab);
558          }
559       TabCtrl_DeleteAllItems (hTab);
560
561       for (size_t ii = 0; ii < iTabOut; ++ii)
562          {
563          TCHAR szTitle[cchRESOURCE];
564          if (aTabs[ii] == dwTABPARAM_ADVANCED)
565             GetString (szTitle, IDS_ADVANCED);
566          else if (aTabs[ii] == dwTABPARAM_MOUNT)
567             GetString (szTitle, IDS_MOUNT);
568          else if ((nCreds <= 1) || (aTabs[ii][0] == TEXT('\0')))
569             GetString (szTitle, IDS_CREDENTIALS);
570          else
571             lstrcpy (szTitle, aTabs[ii]);
572
573          TC_ITEM Item;
574          memset (&Item, 0x00, sizeof(Item));
575          Item.mask = TCIF_PARAM | TCIF_TEXT;
576          Item.pszText = szTitle;
577          Item.cchTextMax = cchRESOURCE;
578          Item.lParam = (LPARAM)(aTabs[ii]);
579
580          TabCtrl_InsertItem (hTab, ii, &Item);
581          }
582
583       if (aTabs != NULL)
584          Free (aTabs);
585
586       TabCtrl_SetCurSel (hTab, iTabSel);
587       Main_OnSelectTab ();
588
589       fInHere = FALSE;
590       }
591 }
592
593
594 void Main_EnableRemindTimer (BOOL fEnable)
595 {
596    static BOOL bEnabled = FALSE;
597
598    if ( fEnable == FALSE && bEnabled == TRUE ) {
599        KillTimer (g.hMain, ID_REMIND_TIMER);
600        bEnabled = FALSE;
601    } else if ( fEnable == TRUE && bEnabled == FALSE ) {
602       SetTimer (g.hMain, ID_REMIND_TIMER, (ULONG)cmsec1MINUTE * cminREMIND_TEST, NULL);
603       bEnabled = TRUE;
604    }
605 }
606
607
608 size_t Main_FindExpiredCreds (void)
609 {
610    size_t retval = (size_t) -1;
611    lock_ObtainMutex(&g.expirationCheckLock);
612    if ( KFW_is_available() )
613        KFW_AFS_renew_expiring_tokens();
614    lock_ObtainMutex(&g.credsLock);
615    for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
616       {
617       if (!g.aCreds[ iCreds ].szCell[0])
618          continue;
619       if (!g.aCreds[ iCreds ].fRemind)
620          continue;
621
622       SYSTEMTIME stNow;
623       GetLocalTime (&stNow);
624
625       FILETIME ftNow;
626       SystemTimeToFileTime (&stNow, &ftNow);
627
628       FILETIME ftExpires;
629       SystemTimeToFileTime (&g.aCreds[ iCreds ].stExpires, &ftExpires);
630
631       LONGLONG llNow = (((LONGLONG)ftNow.dwHighDateTime) << 32) + (LONGLONG)(ftNow.dwLowDateTime);
632       LONGLONG llExpires = (((LONGLONG)ftExpires.dwHighDateTime) << 32) + (LONGLONG)(ftExpires.dwLowDateTime);
633
634       llNow /= c100ns1SECOND;
635       llExpires /= c100ns1SECOND;
636
637       if (llExpires <= (llNow + (LONGLONG)cminREMIND_WARN * csec1MINUTE))
638          {
639          if ( KFW_is_available() &&
640               KFW_AFS_renew_token_for_cell(g.aCreds[ iCreds ].szCell) )
641              continue;
642          retval = (size_t) iCreds;
643          break;
644          }
645       }
646    
647    lock_ReleaseMutex(&g.credsLock);
648    lock_ReleaseMutex(&g.expirationCheckLock);
649
650    return retval;
651 }
652
653
654 BOOL CALLBACK Terminate_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
655 {
656    switch (msg)
657       {
658       case WM_INITDIALOG:
659          Terminate_OnInitDialog (hDlg);
660          break;
661
662       case WM_COMMAND:
663          switch (LOWORD(wp))
664             {
665             case IDOK:
666                Terminate_OnOK (hDlg);
667                break;
668
669             case IDCANCEL:
670                EndDialog (hDlg, IDCANCEL);
671                break;
672             }
673          break;
674       }
675
676    return FALSE;
677 }
678
679
680 void Terminate_OnInitDialog (HWND hDlg)
681 {
682    BOOL fPersistent = IsServicePersistent();
683
684    CheckDlgButton (hDlg, IDC_STARTUP, g.fStartup);
685    CheckDlgButton (hDlg, IDC_LEAVE, fPersistent);
686    CheckDlgButton (hDlg, IDC_STOP, !fPersistent);
687 }
688
689
690 void Terminate_OnOK (HWND hDlg)
691 {
692    if (IsServiceRunning())
693       {
694       if (IsDlgButtonChecked (hDlg, IDC_STOP))
695          {
696          SC_HANDLE hManager;
697              if ((hManager = OpenSCManager (NULL, NULL, 
698                                             SC_MANAGER_CONNECT |
699                                             SC_MANAGER_ENUMERATE_SERVICE |
700                                             SC_MANAGER_QUERY_LOCK_STATUS)) != NULL)
701             {
702             SC_HANDLE hService;
703             if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
704                                          SERVICE_QUERY_STATUS | SERVICE_START)) != NULL)
705                {
706                SERVICE_STATUS Status;
707                ControlService (hService, SERVICE_CONTROL_STOP, &Status);
708
709                CloseServiceHandle (hService);
710                }
711
712             CloseServiceHandle (hManager);
713             }
714          }
715       }
716
717    g.fStartup = IsDlgButtonChecked (hDlg, IDC_STARTUP);
718
719    HKEY hk;
720    if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
721       {
722       DWORD dwSize = sizeof(g.fStartup);
723       DWORD dwType = REG_DWORD;
724       RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
725       RegCloseKey (hk);
726       }
727
728    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
729
730    Quit();
731    EndDialog (hDlg, IDOK);
732 }
733