Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / client_creds / main.cpp
blob8883735c8eab80ef42b97f070d1010b31e5d5693
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
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 */
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
16 #include <osilog.h>
17 #include <afs/fs_utils.h>
18 #include <rx\rxkad.h>
19 #include <afs/afskfw.h>
20 #include "ipaddrchg.h"
23 #include "afscreds.h"
24 #include <WINNT\afsreg.h> // So we can see if the server's installed
25 #include "drivemap.h"
26 #include <stdlib.h>
27 #include <stdio.h>
30 * DEFINITIONS ________________________________________________________________
34 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
38 * VARIABLES __________________________________________________________________
42 GLOBALS g;
46 * PROTOTYPES _________________________________________________________________
50 BOOL InitApp (LPSTR pszCmdLineA);
51 void ExitApp (void);
52 void Quit (void);
53 void PumpMessage (MSG *pmsg);
54 BOOL IsServerInstalled (void);
58 * ROUTINES ___________________________________________________________________
62 extern "C" int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
64 Shortcut_Init();
65 TaLocale_LoadCorrespondingModule (hInst);
67 osi_InitTraceOption();
68 osi_LogEvent0("AFSCreds Main command line",pCmdLine);
69 fs_utils_InitMountRoot();
72 if (InitApp (pCmdLine))
74 MSG msg;
75 while (GetMessage (&msg, NULL, 0, 0) > 0)
77 PumpMessage (&msg);
80 ExitApp();
83 Shortcut_Exit();
84 return 0;
87 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
88 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
90 BOOL InitApp (LPSTR pszCmdLineA)
92 BOOL fShow = FALSE;
93 BOOL fQuiet = FALSE;
94 BOOL fExit = FALSE;
95 BOOL fInstall = FALSE;
96 BOOL fUninstall = FALSE;
97 BOOL fAutoInit = FALSE;
98 BOOL fNetDetect = FALSE;
99 BOOL fRenewMaps = FALSE;
101 // Initialize our global variables and window classes
103 memset (&g, 0x00, sizeof(g));
104 g.fStartup = TRUE;
106 // Parse the command-line
108 while (pszCmdLineA && *pszCmdLineA)
110 if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
111 break;
113 switch (*(++pszCmdLineA))
115 case 'a':
116 case 'A':
117 fAutoInit = TRUE;
118 break;
120 case 'm':
121 case 'M':
122 fRenewMaps = TRUE;
123 break;
125 case 'n':
126 case 'N':
127 fNetDetect = TRUE;
128 break;
130 case 's':
131 case 'S':
132 fShow = TRUE;
133 break;
135 case 'q':
136 case 'Q':
137 fQuiet = TRUE;
138 break;
140 case 'e':
141 case 'E':
142 fExit = TRUE;
143 break;
145 case 'i':
146 case 'I':
147 fInstall = TRUE;
148 break;
150 case 'u':
151 case 'U':
152 fUninstall = TRUE;
153 break;
155 case ':':
156 CopyAnsiToString(g.SmbName,pszCmdLineA);
157 MapShareName(pszCmdLineA);
158 break;
160 case 'z':
161 case 'Z':
162 DoUnMapShare(TRUE);
163 return(0);
165 case 'x':
166 case 'X':
167 TestAndDoMapShare(SERVICE_START_PENDING);
168 TestAndDoMapShare(SERVICE_RUNNING);
169 return 0;
172 while (*pszCmdLineA && (*pszCmdLineA != ' '))
173 ++pszCmdLineA;
174 if (*pszCmdLineA==' ') ++pszCmdLineA;
177 if (fInstall)
178 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
179 else if (fUninstall)
180 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
182 if (fInstall || fUninstall)
184 HKEY hk;
185 if (RegCreateKeyEx (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, 0, NULL, 0,
186 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE, NULL, &hk, NULL) == 0)
188 DWORD dwSize = sizeof(g.fStartup);
189 DWORD dwType = REG_DWORD;
190 RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
191 RegCloseKey (hk);
195 // Only show up if there's not another version of this app around already.
197 for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
198 hSearch && IsWindow(hSearch);
199 hSearch = GetWindow (hSearch, GW_HWNDNEXT))
201 TCHAR szClassName[ cchRESOURCE ];
202 if (GetClassName (hSearch, szClassName, cchRESOURCE))
204 if (!lstrcmpi (szClassName, cszCLASSNAME))
206 if (fShow)
207 PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
208 else if (fExit)
209 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
210 else if (fUninstall)
211 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
212 return FALSE;
217 if (fExit || fUninstall || fInstall)
218 return FALSE;
220 HKEY hk;
221 if (RegOpenKeyEx (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, 0,
222 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)
224 DWORD dwSize = sizeof(g.fStartup);
225 DWORD dwType = REG_DWORD;
226 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
227 RegCloseKey (hk);
229 else if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY, 0,
230 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)
232 DWORD dwSize = sizeof(g.fStartup);
233 DWORD dwType = REG_DWORD;
234 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
235 RegCloseKey (hk);
238 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
240 // Is this Windows NT?
242 OSVERSIONINFO Version;
243 memset (&Version, 0x00, sizeof(Version));
244 Version.dwOSVersionInfoSize = sizeof(Version);
245 if (GetVersionEx (&Version))
246 g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
248 if (!g.fIsWinNT)
249 lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
250 else
251 lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
253 // Initialize winsock etc
255 WSADATA Data;
256 WSAStartup (0x0101, &Data);
258 InitCommonControls();
259 RegisterCheckListClass();
260 osi_Init();
261 lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock", 0);
262 lock_InitializeMutex(&g.credsLock, "global creds lock", 0);
264 KFW_AFS_wait_for_service_start();
266 if ( IsDebuggerPresent() ) {
267 if ( !g.fIsWinNT )
268 OutputDebugString("No Service Present on non-NT systems\n");
269 else {
270 if ( IsServiceRunning() )
271 OutputDebugString("AFSD Service started\n");
272 else {
273 OutputDebugString("AFSD Service stopped\n");
274 if ( !IsServiceConfigured() )
275 OutputDebugString("AFSD Service not configured\n");
276 else if ( fAutoInit )
277 OutputDebugString("AFSD Service will be started\n");
282 // If the service isn't started yet, and autoInit start the service
283 if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
284 SC_HANDLE hManager;
286 if ((hManager = OpenSCManager( NULL, NULL,
287 SC_MANAGER_CONNECT |
288 SC_MANAGER_ENUMERATE_SERVICE |
289 SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
291 SC_HANDLE hService;
292 if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"),
293 SERVICE_QUERY_STATUS | SERVICE_START) ) != NULL)
295 if (StartService(hService, 0, 0)) {
296 if ( IsDebuggerPresent() )
297 OutputDebugString("AFSD Service start successful\n");
298 fRenewMaps = TRUE;
299 } else if ( IsDebuggerPresent() )
300 OutputDebugString("AFSD Service start failed\n");
302 CloseServiceHandle (hService);
305 CloseServiceHandle (hManager);
307 KFW_AFS_wait_for_service_start();
310 KFW_initialize();
312 // Create a main window. All further initialization will be done during
313 // processing of WM_INITDIALOG.
315 WNDCLASS wc;
316 if (!GetClassInfo (NULL, WC_DIALOG, &wc)) // Get dialog class info
317 return FALSE;
318 wc.hInstance = THIS_HINST;
319 wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
320 wc.lpszClassName = cszCLASSNAME;
321 wc.style |= CS_GLOBALCLASS;
322 RegisterClass(&wc);
324 g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
325 if (g.hMain == NULL)
326 return FALSE;
328 // If the service isn't started yet, show our startup wizard.
330 if (!IsServiceRunning() && !fQuiet)
332 if (!g.fIsWinNT)
333 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
334 else if (IsServiceConfigured())
335 ShowStartupWizard();
336 else if (!IsServerInstalled())
337 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
339 if (IsServiceRunning()) {
340 if ( fRenewMaps )
342 if ( IsDebuggerPresent() )
343 OutputDebugString("Renewing Drive Maps\n");
344 DoMapShareChange(FALSE);
346 if (fShow)
348 if ( IsDebuggerPresent() )
349 OutputDebugString("Displaying Main window\n");
350 Main_Show (TRUE);
352 // If the root cell is reachable and we have no tokens
353 // display the Obtain Tokens dialog to the user
354 if ( fAutoInit ) {
355 if ( IsDebuggerPresent() )
356 OutputDebugString("Obtaining Tokens (if needed)\n");
357 ObtainTokensFromUserIfNeeded(g.hMain);
359 } else if ( IsDebuggerPresent() )
360 OutputDebugString("AFSD Service Stopped\n");
362 if ( fNetDetect ) {
363 // Start IP Address Change Monitor
364 if ( IsDebuggerPresent() )
365 OutputDebugString("Activating Network Change Monitor\n");
366 IpAddrChangeMonitorInit(g.hMain);
368 Main_EnableRemindTimer(TRUE);
369 return TRUE;
373 void ExitApp (void)
375 KFW_cleanup();
376 g.hMain = NULL;
380 void PumpMessage (MSG *pmsg)
382 if (!IsMemoryManagerMessage (pmsg))
384 if (!IsDialogMessage (g.hMain, pmsg))
386 TranslateMessage (pmsg);
387 DispatchMessage (pmsg);
393 void Quit (void)
395 if (IsWindow (g.hMain))
397 ChangeTrayIcon (NIM_DELETE);
398 DestroyWindow (g.hMain);
400 PostQuitMessage (0);
404 BOOL IsServerInstalled (void)
406 BOOL fInstalled = FALSE;
407 HKEY hk;
409 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, AFSREG_SVR_SVC_SUBKEY, 0,
410 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)
412 fInstalled = TRUE;
413 RegCloseKey (hk);
416 return fInstalled;