fix-service-start-permissions-20040302
[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\\Winlogon"), &hk) == 0)
274       {
275       DWORD dwSize = sizeof(szUser);
276       DWORD dwType = REG_SZ;
277       if (RegQueryValueEx (hk, TEXT("DefaultUserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
278          fFoundUserName = TRUE;
279       RegCloseKey (hk);
280       }
281    if (!fFoundUserName)
282       {
283       if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Network\\Logon"), &hk) == 0)
284          {
285          DWORD dwSize = sizeof(szUser);
286          DWORD dwType = REG_SZ;
287          if (RegQueryValueEx (hk, TEXT("UserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
288             fFoundUserName = TRUE;
289          RegCloseKey (hk);
290          }
291       }
292
293    TCHAR szSource[ cchRESOURCE ];
294    TCHAR szTarget[ cchRESOURCE ];
295
296    GetString (szSource, (dwPatch) ? IDS_TITLE_VERSION : IDS_TITLE_VERSION_NOPATCH);
297    wsprintf (szTarget, szSource, szVersion, dwPatch);
298    SetDlgItemText (hDlg, IDC_TITLE_VERSION, szTarget);
299
300    GetDlgItemText (hDlg, IDC_TITLE_NT, szSource, cchRESOURCE);
301    wsprintf (szTarget, szSource, szUser);
302    SetDlgItemText (hDlg, IDC_TITLE_NT, szTarget);
303 }
304
305
306 void Main_OnCheckMenuRemind (void)
307 {
308    BOOL fRemind = FALSE;
309    lock_ObtainMutex(&g.credsLock);
310    for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
311       {
312       if (g.aCreds[ iCreds ].fRemind)
313          fRemind = TRUE;
314       }
315
316    fRemind = !fRemind;
317    for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
318       {
319       if (g.aCreds[ iCreds ].fRemind != fRemind)
320          {
321          g.aCreds[ iCreds ].fRemind = fRemind;
322          SaveRemind (iCreds);
323          }
324       }
325    lock_ReleaseMutex(&g.credsLock);
326
327    // Check the active tab, and fix its checkbox if necessary
328    //
329    HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
330    LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, TabCtrl_GetCurSel(hTab));
331    if (ISCELLTAB(pszTab) && (*pszTab))
332       {
333       HWND hDlg = GetTabChild (hTab);
334       if (hDlg)
335          CheckDlgButton (hDlg, IDC_CREDS_REMIND, fRemind);
336       }
337
338    // Make sure the reminder timer is going
339    //
340    Main_EnableRemindTimer (fRemind);
341 }
342
343
344 void Main_OnRemindTimer (void)
345 {
346    Main_RepopulateTabs (TRUE);
347
348    // See if anything is close to expiring; if so, display a warning
349    // dialog. Make sure we never display a warning more than once.
350    //
351    size_t iExpired;
352    if ((iExpired = Main_FindExpiredCreds()) != -1)
353       {
354       if (InterlockedIncrement (&g.fShowingMessage) != 1)
355          InterlockedDecrement (&g.fShowingMessage);
356       else
357          ShowObtainCreds (TRUE, g.aCreds[ iExpired ].szCell);
358       }
359 }
360
361
362 void Main_OnMouseOver (void)
363 {
364    if ((GetTickCount() - g.tickLastRetest) > cmsecMOUSEOVER)
365       {
366       Main_RepopulateTabs (TRUE);
367       }
368 }
369
370
371 void Main_OnSelectTab (void)
372 {
373    HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
374    size_t iTab = TabCtrl_GetCurSel (hTab);
375
376    HWND hDlgOld = GetTabChild (hTab);
377
378    HWND hDlgNew;
379    if ((hDlgNew = Main_CreateTabDialog (hTab, iTab)) != NULL)
380       ShowWindow (hDlgNew, SW_SHOW);
381
382    if (hDlgOld)
383       DestroyWindow (hDlgOld);
384 }
385
386
387 void Main_OnCheckTerminate (void)
388 {
389    HKEY hk;
390    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
391       {
392       DWORD dwSize = sizeof(g.fStartup);
393       DWORD dwType = REG_DWORD;
394       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
395       RegCloseKey (hk);
396       }
397
398    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
399
400    if (!g.fStartup)
401       Quit();
402 }
403
404
405 HWND Main_CreateTabDialog (HWND hTab, size_t iTab)
406 {
407    HWND hDlg = NULL;
408    LPTSTR psz = NULL;
409
410    TC_ITEM Item;
411    memset (&Item, 0x00, sizeof(Item));
412    Item.mask = TCIF_PARAM;
413    if (TabCtrl_GetItem (hTab, iTab, &Item))
414       {
415       psz = (LPTSTR)(Item.lParam);
416       }
417
418    if (psz == dwTABPARAM_ADVANCED)    // Advanced tab
419       {
420       hDlg = ModelessDialog (IDD_TAB_ADVANCED, hTab, (DLGPROC)Advanced_DlgProc);
421       }
422    else if (psz == dwTABPARAM_MOUNT)  // Mount Points tab
423       {
424       hDlg = ModelessDialog (IDD_TAB_MOUNT, hTab, (DLGPROC)Mount_DlgProc);
425       }
426    else if (ISCELLTAB(psz) && !*psz)  // No Creds tab
427       {
428       hDlg = ModelessDialogParam (IDD_TAB_NOCREDS, hTab, (DLGPROC)Creds_DlgProc, (LPARAM)psz);
429       }
430    else if (ISCELLTAB(psz) && *psz)   // Creds tab for a particular cell
431       {
432       hDlg = ModelessDialogParam (IDD_TAB_CREDS, hTab, (DLGPROC)Creds_DlgProc, (LPARAM)psz);
433       }
434
435    return hDlg;
436 }
437
438
439 void Main_RepopulateTabs (BOOL fDestroyInvalid)
440 {
441    static BOOL fInHere = FALSE;
442    if (!fInHere)
443       {
444       fInHere = TRUE;
445
446       if (IsWindowVisible (g.hMain))
447          fDestroyInvalid = FALSE;
448
449       // First we'll have to look around and see what credentials we currently
450       // have. This call just updates g.aCreds[]; it doesn't do anything else.
451       //
452       (void)GetCurrentCredentials();
453
454       // We want one tab on the main dialog for each g.aCredentials entry,
455       // and one tab for Advanced.
456       //
457       HWND hTab = GetDlgItem (g.hMain, IDC_TABS);
458
459       // Generate a list of the lParams we'll be giving tabs...
460       //
461       LPTSTR *aTabs = NULL;
462       size_t cTabs = 0;
463       size_t iTabOut = 0;
464
465       size_t nCreds = 0;
466       lock_ObtainMutex(&g.credsLock);
467       for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
468          {
469          if (g.aCreds[ iCreds ].szCell[0])
470             ++nCreds;
471          }
472       if (!nCreds)
473          {
474          fDestroyInvalid = TRUE;
475          }
476
477       if (!fDestroyInvalid)
478          {
479          int nTabs = TabCtrl_GetItemCount(hTab);
480          for (int iTab = 0; iTab < nTabs; ++iTab)
481             {
482             LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, iTab);
483             if (ISCELLTAB(pszTab) && (*pszTab))
484                {
485                if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
486                   aTabs[ iTabOut++ ] = CloneString(pszTab);
487                }
488             }
489          }
490
491       if (nCreds == 0)
492          {
493          if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
494             aTabs[ iTabOut++ ] = CloneString (TEXT(""));
495          }
496       else for (iCreds = 0; iCreds < g.cCreds; ++iCreds)
497          {
498          if (g.aCreds[ iCreds ].szCell[0])
499             {
500             for (size_t ii = 0; ii < iTabOut; ++ii)
501                {
502                if (!ISCELLTAB (aTabs[ii]))
503                   continue;
504                if (!lstrcmpi (g.aCreds[ iCreds ].szCell, aTabs[ ii ]))
505                   break;
506                }
507             if (ii == iTabOut)
508                {
509                if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
510                   aTabs[ iTabOut++ ] = CloneString (g.aCreds[ iCreds ].szCell);
511                }
512             }
513          }
514       lock_ReleaseMutex(&g.credsLock);
515
516       if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
517          aTabs[ iTabOut++ ] = dwTABPARAM_MOUNT;
518
519       if (g.fIsWinNT)
520          {
521          if (REALLOC (aTabs, cTabs, 1+iTabOut, cREALLOC_TABS))
522             aTabs[ iTabOut++ ] = dwTABPARAM_ADVANCED;
523          }
524
525       // Now erase the current tabs, and re-add new ones. Remember which tab is
526       // currently selected, so we can try to go back to it later.
527       //
528       int iTabSel = 0;
529       if (TabCtrl_GetItemCount(hTab))
530          {
531          LPTSTR pszTabSel = (LPTSTR)GetTabParam (hTab, TabCtrl_GetCurSel(hTab));
532          for (size_t iSel = 0; iSel < iTabOut; ++iSel)
533             {
534             if ((!ISCELLTAB(pszTabSel))  && (!ISCELLTAB(aTabs[iSel])) && (pszTabSel == aTabs[iSel]))
535                iTabSel = iSel;
536             else if (ISCELLTAB(pszTabSel) && ISCELLTAB(aTabs[iSel]) && !lstrcmpi (pszTabSel, aTabs[iSel]))
537                iTabSel = iSel;
538             }
539          }
540
541       int nTabs = TabCtrl_GetItemCount(hTab);
542       for (int iTab = 0; iTab < nTabs; ++iTab)
543          {
544          LPTSTR pszTab = (LPTSTR)GetTabParam (hTab, iTab);
545          if (ISCELLTAB(pszTab))
546             FreeString (pszTab);
547          }
548       TabCtrl_DeleteAllItems (hTab);
549
550       for (size_t ii = 0; ii < iTabOut; ++ii)
551          {
552          TCHAR szTitle[cchRESOURCE];
553          if (aTabs[ii] == dwTABPARAM_ADVANCED)
554             GetString (szTitle, IDS_ADVANCED);
555          else if (aTabs[ii] == dwTABPARAM_MOUNT)
556             GetString (szTitle, IDS_MOUNT);
557          else if ((nCreds <= 1) || (aTabs[ii][0] == TEXT('\0')))
558             GetString (szTitle, IDS_CREDENTIALS);
559          else
560             lstrcpy (szTitle, aTabs[ii]);
561
562          TC_ITEM Item;
563          memset (&Item, 0x00, sizeof(Item));
564          Item.mask = TCIF_PARAM | TCIF_TEXT;
565          Item.pszText = szTitle;
566          Item.cchTextMax = cchRESOURCE;
567          Item.lParam = (LPARAM)(aTabs[ii]);
568
569          TabCtrl_InsertItem (hTab, ii, &Item);
570          }
571
572       if (aTabs != NULL)
573          Free (aTabs);
574
575       TabCtrl_SetCurSel (hTab, iTabSel);
576       Main_OnSelectTab ();
577
578       fInHere = FALSE;
579       }
580 }
581
582
583 void Main_EnableRemindTimer (BOOL fEnable)
584 {
585    static BOOL bEnabled = FALSE;
586
587    if ( fEnable == FALSE && bEnabled == TRUE ) {
588        KillTimer (g.hMain, ID_REMIND_TIMER);
589        bEnabled = FALSE;
590    } else if ( fEnable == TRUE && bEnabled == FALSE ) {
591       SetTimer (g.hMain, ID_REMIND_TIMER, (ULONG)cmsec1MINUTE * cminREMIND_TEST, NULL);
592       bEnabled = TRUE;
593    }
594 }
595
596
597 size_t Main_FindExpiredCreds (void)
598 {
599    size_t retval = (size_t) -1;
600    lock_ObtainMutex(&g.expirationCheckLock);
601    if ( KFW_is_available() )
602        KFW_AFS_renew_expiring_credentials();
603    lock_ObtainMutex(&g.credsLock);
604    for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
605       {
606       if (!g.aCreds[ iCreds ].szCell[0])
607          continue;
608       if (!g.aCreds[ iCreds ].fRemind)
609          continue;
610
611       SYSTEMTIME stNow;
612       GetLocalTime (&stNow);
613
614       FILETIME ftNow;
615       SystemTimeToFileTime (&stNow, &ftNow);
616
617       FILETIME ftExpires;
618       SystemTimeToFileTime (&g.aCreds[ iCreds ].stExpires, &ftExpires);
619
620       LONGLONG llNow = (((LONGLONG)ftNow.dwHighDateTime) << 32) + (LONGLONG)(ftNow.dwLowDateTime);
621       LONGLONG llExpires = (((LONGLONG)ftExpires.dwHighDateTime) << 32) + (LONGLONG)(ftExpires.dwLowDateTime);
622
623       llNow /= c100ns1SECOND;
624       llExpires /= c100ns1SECOND;
625
626       if (llExpires <= (llNow + (LONGLONG)cminREMIND_WARN * csec1MINUTE))
627          {
628          if ( KFW_is_available() &&
629               KFW_AFS_renew_token_for_cell(g.aCreds[ iCreds ].szCell) )
630              continue;
631          retval = (size_t) iCreds;
632          break;
633          }
634       }
635    
636    lock_ReleaseMutex(&g.credsLock);
637    lock_ReleaseMutex(&g.expirationCheckLock);
638
639    return retval;
640 }
641
642
643 BOOL CALLBACK Terminate_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
644 {
645    switch (msg)
646       {
647       case WM_INITDIALOG:
648          Terminate_OnInitDialog (hDlg);
649          break;
650
651       case WM_COMMAND:
652          switch (LOWORD(wp))
653             {
654             case IDOK:
655                Terminate_OnOK (hDlg);
656                break;
657
658             case IDCANCEL:
659                EndDialog (hDlg, IDCANCEL);
660                break;
661             }
662          break;
663       }
664
665    return FALSE;
666 }
667
668
669 void Terminate_OnInitDialog (HWND hDlg)
670 {
671    BOOL fPersistent = IsServicePersistent();
672
673    CheckDlgButton (hDlg, IDC_STARTUP, g.fStartup);
674    CheckDlgButton (hDlg, IDC_LEAVE, fPersistent);
675    CheckDlgButton (hDlg, IDC_STOP, !fPersistent);
676 }
677
678
679 void Terminate_OnOK (HWND hDlg)
680 {
681    if (IsServiceRunning())
682       {
683       if (IsDlgButtonChecked (hDlg, IDC_STOP))
684          {
685          SC_HANDLE hManager;
686              if ((hManager = OpenSCManager (NULL, NULL, 
687                                             SC_MANAGER_CONNECT |
688                                             SC_MANAGER_ENUMERATE_SERVICE |
689                                             SC_MANAGER_QUERY_LOCK_STATUS)) != NULL)
690             {
691             SC_HANDLE hService;
692             if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
693                                          SERVICE_QUERY_STATUS | SERVICE_START)) != NULL)
694                {
695                SERVICE_STATUS Status;
696                ControlService (hService, SERVICE_CONTROL_STOP, &Status);
697
698                CloseServiceHandle (hService);
699                }
700
701             CloseServiceHandle (hManager);
702             }
703          }
704       }
705
706    g.fStartup = IsDlgButtonChecked (hDlg, IDC_STARTUP);
707
708    HKEY hk;
709    if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
710       {
711       DWORD dwSize = sizeof(g.fStartup);
712       DWORD dwType = REG_DWORD;
713       RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
714       RegCloseKey (hk);
715       }
716
717    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
718
719    Quit();
720    EndDialog (hDlg, IDOK);
721 }
722