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))
142 MapShareName(pszCmdLineA);
146 TestAndDoMapShare(SERVICE_START_PENDING);
147 TestAndDoMapShare(SERVICE_RUNNING);
151 while (*pszCmdLineA && (*pszCmdLineA != ' '))
153 if (*pszCmdLineA==' ') ++pszCmdLineA;
157 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = TRUE);
159 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup = FALSE);
164 if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
166 DWORD dwSize = sizeof(g.fStartup);
167 DWORD dwType = REG_DWORD;
168 RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
173 // Only show up if there's not another version of this app around already.
175 for (HWND hSearch = GetWindow (GetDesktopWindow(), GW_CHILD);
176 hSearch && IsWindow(hSearch);
177 hSearch = GetWindow (hSearch, GW_HWNDNEXT))
179 TCHAR szClassName[ cchRESOURCE ];
180 if (GetClassName (hSearch, szClassName, cchRESOURCE))
182 if (!lstrcmpi (szClassName, cszCLASSNAME))
185 PostMessage (hSearch, WM_COMMAND, M_ACTIVATE, 0);
187 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
189 PostMessage (hSearch, WM_COMMAND, M_TERMINATE_NOW, 0);
195 if (fExit || fUninstall || fInstall)
198 // Initialize our global variables and window classes
200 memset (&g, 0x00, sizeof(g));
204 if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
206 DWORD dwSize = sizeof(g.fStartup);
207 DWORD dwType = REG_DWORD;
208 RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
212 Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
214 // Is this Windows NT?
216 OSVERSIONINFO Version;
217 memset (&Version, 0x00, sizeof(Version));
218 Version.dwOSVersionInfoSize = sizeof(Version);
219 if (GetVersionEx (&Version))
220 g.fIsWinNT = (Version.dwPlatformId == VER_PLATFORM_WIN32_NT) ? TRUE : FALSE;
223 lstrcpy (g.szHelpFile, TEXT("afs-light.hlp"));
225 lstrcpy (g.szHelpFile, TEXT("afs-nt.hlp"));
227 // Initialize winsock etc
230 WSAStartup (0x0101, &Data);
232 InitCommonControls();
233 RegisterCheckListClass();
235 lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
236 lock_InitializeMutex(&g.credsLock, "global creds lock");
238 KFW_AFS_wait_for_service_start();
240 if ( IsDebuggerPresent() ) {
242 OutputDebugString("No Service Present on non-NT systems\n");
244 if ( IsServiceRunning() )
245 OutputDebugString("AFSD Service started\n");
247 OutputDebugString("AFSD Service stopped\n");
248 if ( !IsServiceConfigured() )
249 OutputDebugString("AFSD Service not configured\n");
250 else if ( fAutoInit )
251 OutputDebugString("AFSD Service will be started\n");
256 // If the service isn't started yet, and autoInit start the service
257 if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
260 if ((hManager = OpenSCManager( NULL, NULL,
262 SC_MANAGER_ENUMERATE_SERVICE |
263 SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
266 if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"),
267 SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG |
268 SERVICE_QUERY_STATUS) ) != NULL)
270 if (StartService(hService, 0, 0)) {
271 if ( IsDebuggerPresent() )
272 OutputDebugString("AFSD Service start successful\n");
274 } else if ( IsDebuggerPresent() )
275 OutputDebugString("AFSD Service start failed\n");
277 CloseServiceHandle (hService);
280 CloseServiceHandle (hManager);
282 KFW_AFS_wait_for_service_start();
287 // Create a main window. All further initialization will be done during
288 // processing of WM_INITDIALOG.
291 if (!GetClassInfo (NULL, WC_DIALOG, &wc)) // Get dialog class info
293 wc.hInstance = THIS_HINST;
294 wc.hIcon = TaLocale_LoadIcon (IDI_MAIN);
295 wc.lpszClassName = cszCLASSNAME;
296 wc.style |= CS_GLOBALCLASS;
299 g.hMain = ModelessDialog (IDD_MAIN, NULL, (DLGPROC)Main_DlgProc);
303 // If the service isn't started yet, show our startup wizard.
305 if (!IsServiceRunning() && !fQuiet)
308 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE_95, IDS_UNCONFIG_DESC_95);
309 else if (IsServiceConfigured())
311 else if (!IsServerInstalled())
312 Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
314 if (IsServiceRunning()) {
317 if ( IsDebuggerPresent() )
318 OutputDebugString("Renewing Drive Maps\n");
319 TestAndDoMapShare(SERVICE_START_PENDING);
320 TestAndDoMapShare(SERVICE_RUNNING);
324 if ( IsDebuggerPresent() )
325 OutputDebugString("Displaying Main window\n");
328 // If the root cell is reachable and we have no tokens
329 // display the Obtain Tokens dialog to the user
331 if ( IsDebuggerPresent() )
332 OutputDebugString("Obtaining Tokens (if needed)\n");
333 ObtainTokensFromUserIfNeeded(g.hMain);
335 } else if ( IsDebuggerPresent() )
336 OutputDebugString("AFSD Service Stopped\n");
339 // Start IP Address Change Monitor
340 if ( IsDebuggerPresent() )
341 OutputDebugString("Activating Network Change Monitor\n");
342 IpAddrChangeMonitorInit(g.hMain);
344 Main_EnableRemindTimer(TRUE);
356 void PumpMessage (MSG *pmsg)
358 if (!IsMemoryManagerMessage (pmsg))
360 if (!IsDialogMessage (g.hMain, pmsg))
362 TranslateMessage (pmsg);
363 DispatchMessage (pmsg);
371 if (IsWindow (g.hMain))
373 ChangeTrayIcon (NIM_DELETE);
374 DestroyWindow (g.hMain);
380 BOOL IsServerInstalled (void)
382 BOOL fInstalled = FALSE;
384 TCHAR szKey[] = AFSREG_SVR_SVC_KEY;
385 LPCTSTR pch = lstrchr (szKey, TEXT('\\'));
388 if (RegOpenKey (HKEY_LOCAL_MACHINE, &pch[1], &hk) == 0)