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
11 #include <afs/param.h>
14 #include <afs/fs_utils.h>
18 #include "..\afsreg\afsreg.h" // So we can see if the server's installed
26 * DEFINITIONS ________________________________________________________________
30 const TCHAR cszCLASSNAME[] = TEXT("AfsCreds");
34 * VARIABLES __________________________________________________________________
42 * PROTOTYPES _________________________________________________________________
46 BOOL InitApp (LPSTR pszCmdLineA);
49 void PumpMessage (MSG *pmsg);
50 BOOL IsServerInstalled (void);
54 * ROUTINES ___________________________________________________________________
58 extern "C" int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
61 TaLocale_LoadCorrespondingModule (hInst);
63 osi_InitTraceOption();
64 osi_LogEvent0("AFSCreds Main command line",pCmdLine);
65 fs_utils_InitMountRoot();
67 if (InitApp (pCmdLine))
70 while (GetMessage (&msg, NULL, 0, 0) > 0)
82 #define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
83 #define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
85 BOOL InitApp (LPSTR pszCmdLineA)
90 BOOL fInstall = FALSE;
91 BOOL fUninstall = FALSE;
92 BOOL fAutoInit = FALSE;
93 BOOL fNetDetect = FALSE;
94 BOOL fRenewMaps = FALSE;
96 // Parse the command-line
98 while (pszCmdLineA && *pszCmdLineA)
100 if ((*pszCmdLineA != '-') && (*pszCmdLineA != '/'))
103 switch (*(++pszCmdLineA))
146 MapShareName(pszCmdLineA);
156 TestAndDoMapShare(SERVICE_START_PENDING);
157 TestAndDoMapShare(SERVICE_RUNNING);
161 while (*pszCmdLineA && (*pszCmdLineA != ' '))
163 if (*pszCmdLineA==' ') ++pszCmdLineA;
167 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
169 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
174 if (RegCreateKey (HKEY_CURRENT_USER, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
176 DWORD dwSize = sizeof(g.fStartup);
177 DWORD dwType = REG_DWORD;
178 RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
183 // Only show up if there's not another version of this app around already.
185 for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
186 hSearch && IsWindow(hSearch);
187 hSearch = GetWindow (hSearch, GW_HWNDNEXT))
189 TCHAR szClassName[ cchRESOURCE ];
190 if (GetClassName (hSearch, szClassName, cchRESOURCE))
192 if (!lstrcmpi (szClassName, cszCLASSNAME))
195 PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
197 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
199 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
205 if (fExit || fUninstall || fInstall)
208 // Initialize our global variables and window classes
210 memset (&g, 0x00, sizeof(g));
214 if (RegOpenKey (HKEY_CURRENT_USER, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
216 DWORD dwSize = sizeof(g.fStartup);
217 DWORD dwType = REG_DWORD;
218 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
221 else if (RegOpenKey (HKEY_LOCAL_MACHINE, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
223 DWORD dwSize = sizeof(g.fStartup);
224 DWORD dwType = REG_DWORD;
225 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
229 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
231 // Is this Windows NT?
233 OSVERSIONINFO Version;
234 memset (&Version, 0x00, sizeof(Version));
235 Version.dwOSVersionInfoSize = sizeof(Version);
236 if (GetVersionEx (&Version))
237 g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
240 lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
242 lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
244 // Initialize winsock etc
247 WSAStartup (0x0101, &Data);
249 InitCommonControls();
250 RegisterCheckListClass();
252 lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
253 lock_InitializeMutex(&g.credsLock, "global creds lock");
255 KFW_AFS_wait_for_service_start();
257 if ( IsDebuggerPresent() ) {
259 OutputDebugString("No Service Present on non-NT systems\n");
261 if ( IsServiceRunning() )
262 OutputDebugString("AFSD Service started\n");
264 OutputDebugString("AFSD Service stopped\n");
265 if ( !IsServiceConfigured() )
266 OutputDebugString("AFSD Service not configured\n");
267 else if ( fAutoInit )
268 OutputDebugString("AFSD Service will be started\n");
273 // If the service isn't started yet, and autoInit start the service
274 if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
277 if ((hManager = OpenSCManager( NULL, NULL,
279 SC_MANAGER_ENUMERATE_SERVICE |
280 SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
283 if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"),
284 SERVICE_QUERY_STATUS | SERVICE_START) ) != NULL)
286 if (StartService(hService, 0, 0)) {
287 if ( IsDebuggerPresent() )
288 OutputDebugString("AFSD Service start successful\n");
290 } else if ( IsDebuggerPresent() )
291 OutputDebugString("AFSD Service start failed\n");
293 CloseServiceHandle (hService);
296 CloseServiceHandle (hManager);
298 KFW_AFS_wait_for_service_start();
303 // Create a main window. All further initialization will be done during
304 // processing of WM_INITDIALOG.
307 if (!GetClassInfo (NULL, WC_DIALOG, &wc)) // Get dialog class info
309 wc.hInstance = THIS_HINST;
310 wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
311 wc.lpszClassName = cszCLASSNAME;
312 wc.style |= CS_GLOBALCLASS;
315 g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
319 // If the service isn't started yet, show our startup wizard.
321 if (!IsServiceRunning() && !fQuiet)
324 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
325 else if (IsServiceConfigured())
327 else if (!IsServerInstalled())
328 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
330 if (IsServiceRunning()) {
333 if ( IsDebuggerPresent() )
334 OutputDebugString("Renewing Drive Maps\n");
335 TestAndDoMapShare(SERVICE_START_PENDING);
336 TestAndDoMapShare(SERVICE_RUNNING);
340 if ( IsDebuggerPresent() )
341 OutputDebugString("Displaying Main window\n");
344 // If the root cell is reachable and we have no tokens
345 // display the Obtain Tokens dialog to the user
347 if ( IsDebuggerPresent() )
348 OutputDebugString("Obtaining Tokens (if needed)\n");
349 ObtainTokensFromUserIfNeeded(g.hMain);
351 } else if ( IsDebuggerPresent() )
352 OutputDebugString("AFSD Service Stopped\n");
355 // Start IP Address Change Monitor
356 if ( IsDebuggerPresent() )
357 OutputDebugString("Activating Network Change Monitor\n");
358 IpAddrChangeMonitorInit(g.hMain);
360 Main_EnableRemindTimer(TRUE);
372 void PumpMessage (MSG *pmsg)
374 if (!IsMemoryManagerMessage (pmsg))
376 if (!IsDialogMessage (g.hMain, pmsg))
378 TranslateMessage (pmsg);
379 DispatchMessage (pmsg);
387 if (IsWindow (g.hMain))
389 ChangeTrayIcon (NIM_DELETE);
390 DestroyWindow (g.hMain);
396 BOOL IsServerInstalled (void)
398 BOOL fInstalled = FALSE;
400 TCHAR szKey[] = AFSREG_SVR_SVC_KEY;
401 LPCTSTR pch = lstrchr (szKey, TEXT('\\'));
404 if (RegOpenKey (HKEY_LOCAL_MACHINE, &pch[1], &hk) == 0)