skyrope-mit-merge-hell-20040226
[openafs.git] / src / WINNT / client_creds / advtab.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 #ifdef USE_KFW
17 #include "afskrb5.h"
18 #endif
19
20 /*
21  * PROTOTYPES _________________________________________________________________
22  *
23  */
24
25 void Advanced_OnInitDialog (HWND hDlg);
26 void Advanced_StartTimer (HWND hDlg);
27 void Advanced_OnServiceTimer (HWND hDlg);
28 void Advanced_OnOpenCPL (HWND hDlg);
29 void Advanced_OnChangeService (HWND hDlg, WORD wCmd);
30 void Advanced_OnStartup (HWND hDlg);
31
32
33 /*
34  * ROUTINES ___________________________________________________________________
35  *
36  */
37
38 BOOL CALLBACK Advanced_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
39 {
40    switch (msg)
41       {
42       case WM_INITDIALOG:
43          RECT rTab;
44          GetClientRect (GetParent(hDlg), &rTab);
45          TabCtrl_AdjustRect (GetParent (hDlg), FALSE, &rTab); 
46          SetWindowPos (hDlg, NULL, rTab.left, rTab.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
47
48          Advanced_OnInitDialog (hDlg);
49          break;
50
51       case WM_TIMER:
52          Advanced_OnServiceTimer (hDlg);
53          break;
54
55       case WM_COMMAND:
56          switch (LOWORD(wp))
57             {
58             case IDC_SERVICE_STOP:
59             case IDC_SERVICE_START:
60             case IDC_SERVICE_AUTO:
61                Advanced_OnChangeService (hDlg, LOWORD(wp));
62                break;
63
64             case IDC_OPEN_CPL:
65                Advanced_OnOpenCPL (hDlg);
66                break;
67
68             case IDC_STARTUP:
69                Advanced_OnStartup (hDlg);
70                break;
71
72             case IDHELP:
73                Advanced_DlgProc (hDlg, WM_HELP, 0, 0);
74                break;
75             }
76          break;
77
78       case WM_HELP:
79          WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_ADVANCED);
80          break;
81       }
82    return FALSE;
83 }
84
85
86 void Advanced_OnInitDialog (HWND hDlg)
87 {
88    CheckDlgButton (hDlg, IDC_STARTUP, g.fStartup);
89    Advanced_OnServiceTimer (hDlg);
90    Advanced_StartTimer (hDlg);
91 }
92
93
94 void Advanced_StartTimer (HWND hDlg)
95 {
96    KillTimer (hDlg, ID_SERVICE_TIMER);
97    SetTimer (hDlg, ID_SERVICE_TIMER, (ULONG)cmsecSERVICE, NULL);
98 }
99
100
101 void Advanced_OnServiceTimer (HWND hDlg)
102 {
103    BOOL fFinal = TRUE;
104    BOOL fFound = FALSE;
105
106    struct {
107       QUERY_SERVICE_CONFIG Config;
108       BYTE buf[1024];
109    } Config;
110    memset (&Config, 0x00, sizeof(Config));
111    Config.Config.dwStartType = SERVICE_DEMAND_START;
112
113    SERVICE_STATUS Status;
114    memset (&Status, 0x00, sizeof(Status));
115    Status.dwCurrentState = SERVICE_STOPPED;
116
117    SC_HANDLE hManager;
118    if ((hManager = OpenSCManager (NULL, NULL, GENERIC_READ)) != NULL)
119       {
120       SC_HANDLE hService;
121       if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), GENERIC_READ)) != NULL)
122          {
123          fFound = TRUE;
124          DWORD dwSize = sizeof(Config);
125          QueryServiceConfig (hService, (QUERY_SERVICE_CONFIG*)&Config, sizeof(Config), &dwSize);
126          QueryServiceStatus (hService, &Status);
127                  TestAndDoMapShare(Status.dwCurrentState);
128
129          CloseServiceHandle (hService);
130          }
131
132       CloseServiceHandle (hManager);
133       }
134
135    if (fFound)
136       {
137       if ((Status.dwCurrentState == SERVICE_STOP_PENDING) ||
138           (Status.dwCurrentState == SERVICE_START_PENDING))
139          {
140          fFinal = FALSE;
141          }
142       }
143
144    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), fFound && (Status.dwCurrentState == SERVICE_STOPPED));
145    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), fFound && (Status.dwCurrentState == SERVICE_RUNNING));
146    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_AUTO), fFound);
147    CheckDlgButton (hDlg, IDC_SERVICE_AUTO, fFound && (Config.Config.dwStartType == SERVICE_AUTO_START));
148
149    TCHAR szStatus[cchRESOURCE];
150    if (!fFound)
151       GetString (szStatus, IDS_SERVICE_BROKEN);
152    else if (Status.dwCurrentState == SERVICE_RUNNING)
153       GetString (szStatus, IDS_SERVICE_RUNNING);
154    else if (Status.dwCurrentState == SERVICE_STOPPED)
155       GetString (szStatus, IDS_SERVICE_STOPPED);
156    else if (Status.dwCurrentState == SERVICE_STOP_PENDING)
157       GetString (szStatus, IDS_SERVICE_STOPPING);
158    else if (Status.dwCurrentState == SERVICE_START_PENDING)
159       GetString (szStatus, IDS_SERVICE_STARTING);
160    else
161       GetString (szStatus, IDS_SERVICE_UNKNOWN);
162    TestAndDoMapShare(Status.dwCurrentState);
163    SetDlgItemText (hDlg, IDC_SERVICE_STATUS, szStatus);
164
165    if (fFinal && GetWindowLong (hDlg, DWL_USER))
166       {
167       SetWindowLong (hDlg, DWL_USER, 0);
168       Main_RepopulateTabs (FALSE);
169       }
170
171    if (fFinal)
172       {
173       KillTimer (hDlg, ID_SERVICE_TIMER);
174       }
175 }
176
177
178 void Advanced_OnChangeService (HWND hDlg, WORD wCmd)
179 {
180    BOOL fSuccess = FALSE;
181    ULONG error = 0;
182    SC_HANDLE hManager, hService;
183    
184     switch (wCmd)
185     {
186     case IDC_SERVICE_AUTO:
187         DWORD StartType;
188         if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT |
189                                         SC_MANAGER_ENUMERATE_SERVICE |
190                                         SC_MANAGER_QUERY_LOCK_STATUS)) != NULL)
191         {
192             if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
193                                          SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | 
194                                          SERVICE_QUERY_STATUS)) != NULL)
195             {
196                 StartType = (IsDlgButtonChecked (hDlg, wCmd)) ? SERVICE_AUTO_START : SERVICE_DEMAND_START;
197                 if (ChangeServiceConfig (hService, SERVICE_NO_CHANGE, StartType, 
198                                          SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0))
199                     fSuccess = TRUE;
200                 CloseServiceHandle (hService);
201             }
202             CloseServiceHandle (hManager);
203         }
204         break;
205
206     case IDC_SERVICE_START:
207         if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT |
208                                         SC_MANAGER_ENUMERATE_SERVICE |
209                                         SC_MANAGER_QUERY_LOCK_STATUS )) != NULL)
210         {
211             if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
212                                          SERVICE_QUERY_STATUS | SERVICE_START)) != NULL)
213             {
214                 if (StartService (hService, 0, 0))
215                 {
216                     TestAndDoMapShare(SERVICE_START_PENDING);
217                     fSuccess = TRUE;
218                 }
219                 CloseServiceHandle (hService);
220             }
221             CloseServiceHandle (hManager);
222         }
223         break;
224
225     case IDC_SERVICE_STOP:
226         if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_CONNECT |
227                                         SC_MANAGER_ENUMERATE_SERVICE |
228                                         SC_MANAGER_QUERY_LOCK_STATUS )) != NULL)
229         {            
230             if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
231                                          SERVICE_QUERY_STATUS | SERVICE_STOP)) != NULL)
232             {
233                 SERVICE_STATUS Status;
234                 TestAndDoUnMapShare();
235                 ControlService (hService, SERVICE_CONTROL_STOP, &Status);
236                 fSuccess = TRUE;
237             }
238             CloseServiceHandle (hService);
239         }
240         CloseServiceHandle (hManager);
241         break;
242     }
243
244    if (fSuccess)
245       {
246       if (wCmd == IDC_SERVICE_STOP)
247          SetWindowLong (hDlg, DWL_USER, TRUE);
248       Advanced_OnServiceTimer (hDlg);
249       Advanced_StartTimer (hDlg);
250 #ifdef USE_KFW
251       if ( wCmd == IDC_SERVICE_START && KRB5_is_available() && KRB5_wait_for_service_start() )
252           KRB5_AFS_renew_tokens_for_all_cells();
253 #endif /* USE_KFW */
254       }
255    else
256       {
257       if (!error)
258          error = GetLastError();
259
260       int ids;
261       switch (wCmd)
262          {
263          case IDC_SERVICE_START:
264             ids = IDS_SERVICE_FAIL_START;
265             break;
266          case IDC_SERVICE_STOP:
267             ids = IDS_SERVICE_FAIL_STOP;
268             break;
269          default:
270             ids = IDS_SERVICE_FAIL_CONFIG;
271             break;
272          }
273
274       Message (MB_OK | MB_ICONHAND, IDS_SERVICE_ERROR, ids, TEXT("%08lX"), error);
275       }
276 }
277
278
279 void Advanced_OnOpenCPL (HWND hDlg)
280 {
281    WinExec ("afs_config.exe", SW_SHOW);
282 }
283
284
285 void Advanced_OnStartup (HWND hDlg)
286 {
287    g.fStartup = IsDlgButtonChecked (hDlg, IDC_STARTUP);
288
289    HKEY hk;
290    if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
291       {
292       DWORD dwSize = sizeof(g.fStartup);
293       DWORD dwType = REG_DWORD;
294       RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
295       RegCloseKey (hk);
296       }
297
298    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
299 }
300