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