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