2 * Copyright 2000, International Business Machines Corporation and others.
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
14 #include <afs/param.h>
17 #include <afs/fs_utils.h>
19 #include <afs/afskfw.h>
20 #include "ipaddrchg.h"
24 #include <WINNT\afsreg.h> // So we can see if the server's installed
30 * DEFINITIONS ________________________________________________________________
34 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
38 * VARIABLES __________________________________________________________________
46 * PROTOTYPES _________________________________________________________________
50 BOOL InitApp (LPSTR pszCmdLineA);
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)
65 TaLocale_LoadCorrespondingModule (hInst);
67 osi_InitTraceOption();
68 osi_LogEvent0("AFSCreds Main command line",pCmdLine);
69 fs_utils_InitMountRoot();
71 if (InitApp (pCmdLine))
74 while (GetMessage (&msg, NULL, 0, 0) > 0)
86 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
87 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
89 BOOL InitApp (LPSTR pszCmdLineA)
94 BOOL fInstall = FALSE;
95 BOOL fUninstall = FALSE;
96 BOOL fAutoInit = FALSE;
97 BOOL fNetDetect = FALSE;
98 BOOL fRenewMaps = FALSE;
100 // Initialize our global variables and window classes
102 memset (&g, 0x00, sizeof(g));
105 // Parse the command-line
107 while (pszCmdLineA && *pszCmdLineA)
109 if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
112 switch (*(++pszCmdLineA))
155 CopyAnsiToString(g.SmbName,pszCmdLineA);
156 MapShareName(pszCmdLineA);
166 TestAndDoMapShare(SERVICE_START_PENDING);
167 TestAndDoMapShare(SERVICE_RUNNING);
171 while (*pszCmdLineA && (*pszCmdLineA != ' '))
173 if (*pszCmdLineA==' ') ++pszCmdLineA;
177 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
179 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
181 if (fInstall || fUninstall)
184 if (RegCreateKeyEx (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, 0, NULL, 0,
185 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE, NULL, &hk, NULL) == 0)
187 DWORD dwSize = sizeof(g.fStartup);
188 DWORD dwType = REG_DWORD;
189 RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
194 // Only show up if there's not another version of this app around already.
196 for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
197 hSearch && IsWindow(hSearch);
198 hSearch = GetWindow (hSearch, GW_HWNDNEXT))
200 TCHAR szClassName[ cchRESOURCE ];
201 if (GetClassName (hSearch, szClassName, cchRESOURCE))
203 if (!lstrcmpi (szClassName, cszCLASSNAME))
206 PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
208 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
210 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
216 if (fExit || fUninstall || fInstall)
220 if (RegOpenKeyEx (HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY, 0,
221 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)
223 DWORD dwSize = sizeof(g.fStartup);
224 DWORD dwType = REG_DWORD;
225 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
228 else if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY, 0,
229 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)
231 DWORD dwSize = sizeof(g.fStartup);
232 DWORD dwType = REG_DWORD;
233 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
237 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
239 // Is this Windows NT?
241 OSVERSIONINFO Version;
242 memset (&Version, 0x00, sizeof(Version));
243 Version.dwOSVersionInfoSize = sizeof(Version);
244 if (GetVersionEx (&Version))
245 g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
248 lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
250 lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
252 // Initialize winsock etc
255 WSAStartup (0x0101, &Data);
257 InitCommonControls();
258 RegisterCheckListClass();
260 lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
261 lock_InitializeMutex(&g.credsLock, "global creds lock");
263 KFW_AFS_wait_for_service_start();
265 if ( IsDebuggerPresent() ) {
267 OutputDebugString("No Service Present on non-NT systems\n");
269 if ( IsServiceRunning() )
270 OutputDebugString("AFSD Service started\n");
272 OutputDebugString("AFSD Service stopped\n");
273 if ( !IsServiceConfigured() )
274 OutputDebugString("AFSD Service not configured\n");
275 else if ( fAutoInit )
276 OutputDebugString("AFSD Service will be started\n");
281 // If the service isn't started yet, and autoInit start the service
282 if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
285 if ((hManager = OpenSCManager( NULL, NULL,
287 SC_MANAGER_ENUMERATE_SERVICE |
288 SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
291 if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"),
292 SERVICE_QUERY_STATUS | SERVICE_START) ) != NULL)
294 if (StartService(hService, 0, 0)) {
295 if ( IsDebuggerPresent() )
296 OutputDebugString("AFSD Service start successful\n");
298 } else if ( IsDebuggerPresent() )
299 OutputDebugString("AFSD Service start failed\n");
301 CloseServiceHandle (hService);
304 CloseServiceHandle (hManager);
306 KFW_AFS_wait_for_service_start();
311 // Create a main window. All further initialization will be done during
312 // processing of WM_INITDIALOG.
315 if (!GetClassInfo (NULL, WC_DIALOG, &wc)) // Get dialog class info
317 wc.hInstance = THIS_HINST;
318 wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
319 wc.lpszClassName = cszCLASSNAME;
320 wc.style |= CS_GLOBALCLASS;
323 g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
327 // If the service isn't started yet, show our startup wizard.
329 if (!IsServiceRunning() && !fQuiet)
332 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
333 else if (IsServiceConfigured())
335 else if (!IsServerInstalled())
336 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
338 if (IsServiceRunning()) {
341 if ( IsDebuggerPresent() )
342 OutputDebugString("Renewing Drive Maps\n");
343 DoMapShareChange(FALSE);
347 if ( IsDebuggerPresent() )
348 OutputDebugString("Displaying Main window\n");
351 // If the root cell is reachable and we have no tokens
352 // display the Obtain Tokens dialog to the user
354 if ( IsDebuggerPresent() )
355 OutputDebugString("Obtaining Tokens (if needed)\n");
356 ObtainTokensFromUserIfNeeded(g.hMain);
358 } else if ( IsDebuggerPresent() )
359 OutputDebugString("AFSD Service Stopped\n");
362 // Start IP Address Change Monitor
363 if ( IsDebuggerPresent() )
364 OutputDebugString("Activating Network Change Monitor\n");
365 IpAddrChangeMonitorInit(g.hMain);
367 Main_EnableRemindTimer(TRUE);
379 void PumpMessage (MSG *pmsg)
381 if (!IsMemoryManagerMessage (pmsg))
383 if (!IsDialogMessage (g.hMain, pmsg))
385 TranslateMessage (pmsg);
386 DispatchMessage (pmsg);
394 if (IsWindow (g.hMain))
396 ChangeTrayIcon (NIM_DELETE);
397 DestroyWindow (g.hMain);
403 BOOL IsServerInstalled (void)
405 BOOL fInstalled = FALSE;
408 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, AFSREG_SVR_SVC_SUBKEY, 0,
409 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk) == 0)