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