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