windows-drivemap-renewal-20051121
[openafs.git] / src / WINNT / client_creds / main.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 <osilog.h>
14 #include <afs/fs_utils.h>
15 #include <rxkad.h>
16 #include <afs/afskfw.h>
17 #include "ipaddrchg.h"
18 }
19
20 #include "afscreds.h"
21 #include <WINNT\afsreg.h> // So we can see if the server's installed
22 #include "drivemap.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 /*
27  * DEFINITIONS ________________________________________________________________
28  *
29  */
30
31 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
32
33
34 /*
35  * VARIABLES __________________________________________________________________
36  *
37  */
38
39 GLOBALS g;
40
41
42 /*
43  * PROTOTYPES _________________________________________________________________
44  *
45  */
46
47 BOOL InitApp (LPSTR pszCmdLineA);
48 void ExitApp (void);
49 void Quit (void);
50 void PumpMessage (MSG *pmsg);
51 BOOL IsServerInstalled (void);
52
53
54 /*
55  * ROUTINES ___________________________________________________________________
56  *
57  */
58
59 extern "C" int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
60 {
61    Shortcut_Init();
62    TaLocale_LoadCorrespondingModule (hInst);
63
64    osi_InitTraceOption();
65    osi_LogEvent0("AFSCreds Main command line",pCmdLine);
66    fs_utils_InitMountRoot();
67
68    if (InitApp (pCmdLine))
69       {
70       MSG msg;
71       while (GetMessage (&msg, NULL, 0, 0) > 0)
72          {
73          PumpMessage (&msg);
74          }
75
76       ExitApp();
77       }
78
79    Shortcut_Exit();
80    return 0;
81 }
82
83 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
84 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
85
86 BOOL InitApp (LPSTR pszCmdLineA)
87 {
88    BOOL fShow = FALSE;
89    BOOL fQuiet = FALSE;
90    BOOL fExit = FALSE;
91    BOOL fInstall = FALSE;
92    BOOL fUninstall = FALSE;
93    BOOL fAutoInit = FALSE;
94    BOOL fNetDetect = FALSE;
95    BOOL fRenewMaps = FALSE;
96
97    // Initialize our global variables and window classes
98    //
99    memset (&g, 0x00, sizeof(g));
100    g.fStartup = TRUE;
101
102    // Parse the command-line
103    //
104    while (pszCmdLineA && *pszCmdLineA)
105       {
106       if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
107          break;
108
109       switch (*(++pszCmdLineA))
110          {
111          case 'a':
112          case 'A':
113             fAutoInit = TRUE;
114             break;
115
116          case 'm':
117          case 'M':
118             fRenewMaps = TRUE;
119             break;
120
121          case 'n':
122          case 'N':
123             fNetDetect = TRUE;
124             break;
125
126          case 's':
127          case 'S':
128             fShow = TRUE;
129             break;
130
131          case 'q':
132          case 'Q':
133             fQuiet = TRUE;
134             break;
135
136          case 'e':
137          case 'E':
138             fExit = TRUE;
139             break;
140
141          case 'i':
142          case 'I':
143             fInstall = TRUE;
144             break;
145
146          case 'u':
147          case 'U':
148             fUninstall = TRUE;
149             break;
150
151                  case ':':
152              CopyAnsiToString(g.SmbName,pszCmdLineA);
153                          MapShareName(pszCmdLineA);
154                          break;
155
156          case 'z':
157          case 'Z':
158              DoUnMapShare(TRUE);
159              return(0);
160
161          case 'x':
162          case 'X':
163              TestAndDoMapShare(SERVICE_START_PENDING);
164              TestAndDoMapShare(SERVICE_RUNNING);
165              return 0;
166          }
167
168       while (*pszCmdLineA && (*pszCmdLineA != ' '))
169          ++pszCmdLineA;
170           if (*pszCmdLineA==' ') ++pszCmdLineA;
171       }
172
173    if (fInstall)
174       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
175    else if (fUninstall)
176       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
177
178    if (fInstall)
179       {
180       HKEY hk;
181       if (RegCreateKey (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, &hk) == 0)
182          {
183          DWORD dwSize = sizeof(g.fStartup);
184          DWORD dwType = REG_DWORD;
185          RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
186          RegCloseKey (hk);
187          }
188       }
189
190    // Only show up if there's not another version of this app around already.
191    //
192    for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
193         hSearch && IsWindow(hSearch);
194         hSearch = GetWindow (hSearch, GW_HWNDNEXT))
195       {
196       TCHAR szClassName[ cchRESOURCE ];
197       if (GetClassName (hSearch, szClassName, cchRESOURCE))
198          {
199          if (!lstrcmpi (szClassName, cszCLASSNAME))
200             {
201             if (fShow)
202                PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
203             else if (fExit)
204                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
205             else if (fUninstall)
206                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
207             return FALSE;
208             }
209          }
210       }
211
212    if (fExit || fUninstall || fInstall)
213       return FALSE;
214
215    HKEY hk;
216     if (RegOpenKey (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, &hk) == 0)
217     {
218         DWORD dwSize = sizeof(g.fStartup);
219         DWORD dwType = REG_DWORD;
220         RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
221         RegCloseKey (hk);
222     }
223     else if (RegOpenKey (HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY, &hk) == 0)
224       {
225       DWORD dwSize = sizeof(g.fStartup);
226       DWORD dwType = REG_DWORD;
227       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
228       RegCloseKey (hk);
229       }
230
231    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
232
233    // Is this Windows NT?
234    //
235    OSVERSIONINFO Version;
236    memset (&Version, 0x00, sizeof(Version));
237    Version.dwOSVersionInfoSize = sizeof(Version);
238    if (GetVersionEx (&Version))
239       g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
240
241    if (!g.fIsWinNT)
242       lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
243    else
244       lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
245
246    // Initialize winsock etc
247    //
248    WSADATA Data;
249    WSAStartup (0x0101, &Data);
250
251    InitCommonControls();
252    RegisterCheckListClass();
253    osi_Init();
254    lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
255    lock_InitializeMutex(&g.credsLock, "global creds lock");
256
257    KFW_AFS_wait_for_service_start();
258
259    if ( IsDebuggerPresent() ) {
260        if ( !g.fIsWinNT )
261            OutputDebugString("No Service Present on non-NT systems\n");
262        else {
263            if ( IsServiceRunning() )
264                OutputDebugString("AFSD Service started\n");
265            else {
266                OutputDebugString("AFSD Service stopped\n");
267                if ( !IsServiceConfigured() )
268                    OutputDebugString("AFSD Service not configured\n");
269                else if ( fAutoInit )
270                    OutputDebugString("AFSD Service will be started\n");
271            }   
272        }
273    }
274
275     // If the service isn't started yet, and autoInit start the service
276     if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
277         SC_HANDLE hManager;
278
279         if ((hManager = OpenSCManager( NULL, NULL, 
280                                        SC_MANAGER_CONNECT |
281                                        SC_MANAGER_ENUMERATE_SERVICE |
282                                        SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
283         {
284             SC_HANDLE hService;
285             if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"), 
286                                          SERVICE_QUERY_STATUS | SERVICE_START) ) != NULL)
287             {
288                 if (StartService(hService, 0, 0)) {
289                     if ( IsDebuggerPresent() )
290                         OutputDebugString("AFSD Service start successful\n");
291                     fRenewMaps = TRUE;
292                 } else if ( IsDebuggerPresent() )
293                     OutputDebugString("AFSD Service start failed\n");
294
295                 CloseServiceHandle (hService);
296             }
297
298             CloseServiceHandle (hManager);
299         }
300         KFW_AFS_wait_for_service_start();
301     }
302
303     KFW_initialize();
304
305    // Create a main window. All further initialization will be done during
306    // processing of WM_INITDIALOG.
307    //
308    WNDCLASS wc;
309    if (!GetClassInfo (NULL, WC_DIALOG, &wc))   // Get dialog class info
310       return FALSE;
311    wc.hInstance = THIS_HINST;
312    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
313    wc.lpszClassName = cszCLASSNAME;
314    wc.style |= CS_GLOBALCLASS;
315    RegisterClass(&wc);
316
317    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
318    if (g.hMain == NULL)
319       return FALSE;
320
321    // If the service isn't started yet, show our startup wizard.
322    //
323    if (!IsServiceRunning() && !fQuiet)
324       {
325       if (!g.fIsWinNT)
326          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
327       else if (IsServiceConfigured())
328          ShowStartupWizard();
329       else if (!IsServerInstalled())
330          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
331       }
332    if (IsServiceRunning()) { 
333       if ( fRenewMaps )
334       {
335           if ( IsDebuggerPresent() )
336               OutputDebugString("Renewing Drive Maps\n");
337           DoMapShareChange(FALSE);
338       }
339       if (fShow)
340       {
341       if ( IsDebuggerPresent() )
342           OutputDebugString("Displaying Main window\n");
343       Main_Show (TRUE);
344       }
345       // If the root cell is reachable and we have no tokens
346       // display the Obtain Tokens dialog to the user
347       if ( fAutoInit ) {
348           if ( IsDebuggerPresent() )
349               OutputDebugString("Obtaining Tokens (if needed)\n");
350           ObtainTokensFromUserIfNeeded(g.hMain);
351       }
352    } else if ( IsDebuggerPresent() )
353        OutputDebugString("AFSD Service Stopped\n");
354
355     if ( fNetDetect ) {
356         // Start IP Address Change Monitor
357         if ( IsDebuggerPresent() )
358             OutputDebugString("Activating Network Change Monitor\n");
359         IpAddrChangeMonitorInit(g.hMain);
360     }
361     Main_EnableRemindTimer(TRUE);
362     return TRUE;
363 }
364
365
366 void ExitApp (void)
367 {
368    KFW_cleanup();
369    g.hMain = NULL;
370 }
371
372
373 void PumpMessage (MSG *pmsg)
374 {
375    if (!IsMemoryManagerMessage (pmsg))
376       {
377       if (!IsDialogMessage (g.hMain, pmsg))
378          {
379          TranslateMessage (pmsg);
380          DispatchMessage (pmsg);
381          }
382       }
383 }
384
385
386 void Quit (void)
387 {
388    if (IsWindow (g.hMain))
389       {
390       ChangeTrayIcon (NIM_DELETE);
391       DestroyWindow (g.hMain);
392       }
393    PostQuitMessage (0);
394 }
395
396
397 BOOL IsServerInstalled (void)
398 {
399    BOOL fInstalled = FALSE;
400    HKEY hk;
401
402    if (RegOpenKey (HKEY_LOCAL_MACHINE, AFSREG_SVR_SVC_SUBKEY, &hk) == 0)
403       {
404       fInstalled = TRUE;
405       RegCloseKey (hk);
406       }
407
408    return fInstalled;
409 }
410