thread-and-timer-corrections-to-afscreds-20031121
[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 }
16
17 #include "afscreds.h"
18 #include "..\afsreg\afsreg.h" // So we can see if the server's installed
19 #include "drivemap.h"
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "rxkad.h"
23
24 /*
25  * DEFINITIONS ________________________________________________________________
26  *
27  */
28
29 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
30
31
32 /*
33  * VARIABLES __________________________________________________________________
34  *
35  */
36
37 GLOBALS g;
38
39
40 /*
41  * PROTOTYPES _________________________________________________________________
42  *
43  */
44
45 BOOL InitApp (LPSTR pszCmdLineA);
46 void ExitApp (void);
47 void Quit (void);
48 void PumpMessage (MSG *pmsg);
49 BOOL IsServerInstalled (void);
50
51
52 /*
53  * ROUTINES ___________________________________________________________________
54  *
55  */
56
57 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
58 {
59    Shortcut_Init();
60    TaLocale_LoadCorrespondingModule (hInst);
61
62    osi_InitTraceOption();
63    osi_LogEvent0("AFSCreds Main command line",pCmdLine);
64    fs_utils_InitMountRoot();
65
66    if (InitApp (pCmdLine))
67       {
68       MSG msg;
69       while (GetMessage (&msg, NULL, 0, 0) > 0)
70          {
71          PumpMessage (&msg);
72          }
73
74       ExitApp();
75       }
76
77    Shortcut_Exit();
78    return 0;
79 }
80
81 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
82 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
83
84 BOOL InitApp (LPSTR pszCmdLineA)
85 {
86    BOOL fShow = FALSE;
87    BOOL fQuiet = FALSE;
88    BOOL fExit = FALSE;
89    BOOL fInstall = FALSE;
90    BOOL fUninstall = FALSE;
91
92    // Parse the command-line
93    //
94    while (pszCmdLineA && *pszCmdLineA)
95       {
96       if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
97          break;
98
99       switch (*(++pszCmdLineA))
100          {
101          case 's':
102          case 'S':
103             fShow = TRUE;
104             break;
105
106          case 'q':
107          case 'Q':
108             fQuiet = TRUE;
109             break;
110
111          case 'e':
112          case 'E':
113             fExit = TRUE;
114             break;
115
116          case 'i':
117          case 'I':
118             fInstall = TRUE;
119             break;
120
121          case 'u':
122          case 'U':
123             fUninstall = TRUE;
124             break;
125                  case ':':
126                          MapShareName(pszCmdLineA);
127                          break;
128          case 'x':
129          case 'X':
130              TestAndDoMapShare(SERVICE_START_PENDING);
131              TestAndDoMapShare(SERVICE_RUNNING);
132              return 0;
133          }
134
135       while (*pszCmdLineA && (*pszCmdLineA != ' '))
136          ++pszCmdLineA;
137           if (*pszCmdLineA==' ') ++pszCmdLineA;
138       }
139
140    if (fInstall)
141       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
142    else if (fUninstall)
143       Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
144
145    if (fInstall)
146       {
147       HKEY hk;
148       if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
149          {
150          DWORD dwSize = sizeof(g.fStartup);
151          DWORD dwType = REG_DWORD;
152          RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
153          RegCloseKey (hk);
154          }
155       }
156
157    // Only show up if there's not another version of this app around already.
158    //
159    for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
160         hSearch && IsWindow(hSearch);
161         hSearch = GetWindow (hSearch, GW_HWNDNEXT))
162       {
163       TCHAR szClassName[ cchRESOURCE ];
164       if (GetClassName (hSearch, szClassName, cchRESOURCE))
165          {
166          if (!lstrcmpi (szClassName, cszCLASSNAME))
167             {
168             if (fShow)
169                PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
170             else if (fExit)
171                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
172             else if (fUninstall)
173                PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
174             return FALSE;
175             }
176          }
177       }
178
179    if (fExit || fUninstall || fInstall)
180       return FALSE;
181
182    // Initialize our global variables and window classes
183    //
184    memset (&g, 0x00, sizeof(g));
185    g.fStartup = TRUE;
186
187    HKEY hk;
188    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
189       {
190       DWORD dwSize = sizeof(g.fStartup);
191       DWORD dwType = REG_DWORD;
192       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
193       RegCloseKey (hk);
194       }
195
196    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
197
198    // Is this Windows NT?
199    //
200    OSVERSIONINFO Version;
201    memset (&Version, 0x00, sizeof(Version));
202    Version.dwOSVersionInfoSize = sizeof(Version);
203    if (GetVersionEx (&Version))
204       g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
205
206    if (!g.fIsWinNT)
207       lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
208    else
209       lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
210
211    // Initialize winsock etc
212    //
213    WSADATA Data;
214    WSAStartup (0x0101, &Data);
215
216    InitCommonControls();
217    RegisterCheckListClass();
218    osi_Init();
219    lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
220    lock_InitializeMutex(&g.credsLock, "global creds lock");
221
222    if ( IsDebuggerPresent() ) {
223        if ( !g.fIsWinNT )
224            OutputDebugString("No Service Present on non-NT systems\n");
225        else {
226            if ( IsServiceRunning() )
227                OutputDebugString("AFSD Service started\n");
228            else {
229                OutputDebugString("AFSD Service stopped\n");
230                if ( !IsServiceConfigured() )
231                    OutputDebugString("AFSD Service not configured\n");
232            }   
233        }
234    }
235
236    // Create a main window. All further initialization will be done during
237    // processing of WM_INITDIALOG.
238    //
239    WNDCLASS wc;
240    if (!GetClassInfo (NULL, WC_DIALOG, &wc))   // Get dialog class info
241       return FALSE;
242    wc.hInstance = THIS_HINST;
243    wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
244    wc.lpszClassName = cszCLASSNAME;
245    wc.style |= CS_GLOBALCLASS;
246    RegisterClass(&wc);
247
248    g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
249    if (g.hMain == NULL)
250       return FALSE;
251
252    // If the service isn't started yet, show our startup wizard.
253    //
254    if (!IsServiceRunning() && !fQuiet)
255       {
256       if (!g.fIsWinNT)
257          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
258       else if (IsServiceConfigured())
259          ShowStartupWizard();
260       else if (!IsServerInstalled())
261          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
262       }
263    if (IsServiceRunning()) { 
264       if (fShow)
265       {
266       if ( IsDebuggerPresent() )
267           OutputDebugString("Displaying Main window\n");
268       Main_Show (TRUE);
269       }
270    } else if ( IsDebuggerPresent() )
271       OutputDebugString("Displaying Main window\n");
272     return TRUE;
273 }
274
275
276 void ExitApp (void)
277 {
278    g.hMain = NULL;
279 }
280
281
282 void PumpMessage (MSG *pmsg)
283 {
284    if (!IsMemoryManagerMessage (pmsg))
285       {
286       if (!IsDialogMessage (g.hMain, pmsg))
287          {
288          TranslateMessage (pmsg);
289          DispatchMessage (pmsg);
290          }
291       }
292 }
293
294
295 void Quit (void)
296 {
297    if (IsWindow (g.hMain))
298       {
299       ChangeTrayIcon (NIM_DELETE);
300       DestroyWindow (g.hMain);
301       }
302    PostQuitMessage (0);
303 }
304
305
306 BOOL IsServerInstalled (void)
307 {
308    BOOL fInstalled = FALSE;
309
310    TCHAR szKey[] = AFSREG_SVR_SVC_KEY;
311    LPCTSTR pch = lstrchr (szKey, TEXT('\\'));
312
313    HKEY hk;
314    if (RegOpenKey (HKEY_LOCAL_MACHINE, &pch[1], &hk) == 0)
315       {
316       fInstalled = TRUE;
317       RegCloseKey (hk);
318       }
319
320    return fInstalled;
321 }
322