afscreds-unmap-command-line-option-20040308
[openafs.git] / src / WINNT / client_creds / main.cpp
index 51de6b7..f7a66b6 100644 (file)
 extern "C" {
 #include <afs/param.h>
 #include <afs/stds.h>
+#include <osilog.h>
+#include <afs/fs_utils.h>
 }
 
 #include "afscreds.h"
 #include "..\afsreg\afsreg.h" // So we can see if the server's installed
 #include "drivemap.h"
-
+#include <stdlib.h>
+#include <stdio.h>
+#include "rxkad.h"
+#include "afskfw.h"
 
 /*
  * DEFINITIONS ________________________________________________________________
@@ -50,11 +55,15 @@ BOOL IsServerInstalled (void);
  *
  */
 
-int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
+extern "C" int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdShow)
 {
    Shortcut_Init();
    TaLocale_LoadCorrespondingModule (hInst);
 
+   osi_InitTraceOption();
+   osi_LogEvent0("AFSCreds Main command line",pCmdLine);
+   fs_utils_InitMountRoot();
+
    if (InitApp (pCmdLine))
       {
       MSG msg;
@@ -70,6 +79,8 @@ int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pCmdLine, int nCmdSh
    return 0;
 }
 
+#define ISHIGHSECURITY(v) ( ((v) & LOGON_OPTION_HIGHSECURITY)==LOGON_OPTION_HIGHSECURITY)
+#define REG_CLIENT_PROVIDER_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\NetworkProvider"
 
 BOOL InitApp (LPSTR pszCmdLineA)
 {
@@ -78,6 +89,9 @@ BOOL InitApp (LPSTR pszCmdLineA)
    BOOL fExit = FALSE;
    BOOL fInstall = FALSE;
    BOOL fUninstall = FALSE;
+   BOOL fAutoInit = FALSE;
+   BOOL fNetDetect = FALSE;
+   BOOL fRenewMaps = FALSE;
 
    // Parse the command-line
    //
@@ -88,6 +102,21 @@ BOOL InitApp (LPSTR pszCmdLineA)
 
       switch (*(++pszCmdLineA))
          {
+         case 'a':
+         case 'A':
+            fAutoInit = TRUE;
+            break;
+
+         case 'm':
+         case 'M':
+            fRenewMaps = TRUE;
+            break;
+
+         case 'n':
+         case 'N':
+            fNetDetect = TRUE;
+            break;
+
          case 's':
          case 'S':
             fShow = TRUE;
@@ -112,13 +141,21 @@ BOOL InitApp (LPSTR pszCmdLineA)
          case 'U':
             fUninstall = TRUE;
             break;
+
                 case ':':
                         MapShareName(pszCmdLineA);
                         break;
+
+         case 'z':
+         case 'Z':
+             DoUnMapShare(TRUE);
+             return(0);
+
          case 'x':
          case 'X':
-                        DoMapShare();
-                        return 0;
+             TestAndDoMapShare(SERVICE_START_PENDING);
+             TestAndDoMapShare(SERVICE_RUNNING);
+             return 0;
          }
 
       while (*pszCmdLineA && (*pszCmdLineA != ' '))
@@ -204,6 +241,57 @@ BOOL InitApp (LPSTR pszCmdLineA)
 
    InitCommonControls();
    RegisterCheckListClass();
+   osi_Init();
+   lock_InitializeMutex(&g.expirationCheckLock, "expiration check lock");
+   lock_InitializeMutex(&g.credsLock, "global creds lock");
+
+   KFW_AFS_wait_for_service_start();
+
+   if ( IsDebuggerPresent() ) {
+       if ( !g.fIsWinNT )
+           OutputDebugString("No Service Present on non-NT systems\n");
+       else {
+           if ( IsServiceRunning() )
+               OutputDebugString("AFSD Service started\n");
+           else {
+               OutputDebugString("AFSD Service stopped\n");
+               if ( !IsServiceConfigured() )
+                   OutputDebugString("AFSD Service not configured\n");
+               else if ( fAutoInit )
+                   OutputDebugString("AFSD Service will be started\n");
+           }   
+       }
+   }
+
+    // If the service isn't started yet, and autoInit start the service
+    if ( g.fIsWinNT && !IsServiceRunning() && IsServiceConfigured() && fAutoInit ) {
+        SC_HANDLE hManager;
+
+        if ((hManager = OpenSCManager( NULL, NULL, 
+                                       SC_MANAGER_CONNECT |
+                                       SC_MANAGER_ENUMERATE_SERVICE |
+                                       SC_MANAGER_QUERY_LOCK_STATUS)) != NULL )
+        {
+            SC_HANDLE hService;
+            if ((hService = OpenService( hManager, TEXT("TransarcAFSDaemon"), 
+                                         SERVICE_QUERY_STATUS | SERVICE_START) ) != NULL)
+            {
+                if (StartService(hService, 0, 0)) {
+                    if ( IsDebuggerPresent() )
+                        OutputDebugString("AFSD Service start successful\n");
+                    fRenewMaps = TRUE;
+                } else if ( IsDebuggerPresent() )
+                    OutputDebugString("AFSD Service start failed\n");
+
+                CloseServiceHandle (hService);
+            }
+
+            CloseServiceHandle (hManager);
+        }
+        KFW_AFS_wait_for_service_start();
+    }
+
+    KFW_initialize();
 
    // Create a main window. All further initialization will be done during
    // processing of WM_INITDIALOG.
@@ -232,17 +320,44 @@ BOOL InitApp (LPSTR pszCmdLineA)
       else if (!IsServerInstalled())
          Message (MB_ICONHAND, IDS_UNCONFIG_TITLE, IDS_UNCONFIG_DESC);
       }
-   if (IsServiceRunning() && fShow)
+   if (IsServiceRunning()) { 
+      if ( fRenewMaps )
       {
+          if ( IsDebuggerPresent() )
+              OutputDebugString("Renewing Drive Maps\n");
+          TestAndDoMapShare(SERVICE_START_PENDING);
+          TestAndDoMapShare(SERVICE_RUNNING);
+      }
+      if (fShow)
+      {
+      if ( IsDebuggerPresent() )
+          OutputDebugString("Displaying Main window\n");
       Main_Show (TRUE);
       }
-
-   return TRUE;
+      // If the root cell is reachable and we have no tokens
+      // display the Obtain Tokens dialog to the user
+      if ( fAutoInit ) {
+          if ( IsDebuggerPresent() )
+              OutputDebugString("Obtaining Tokens (if needed)\n");
+          ObtainTokensFromUserIfNeeded(g.hMain);
+      }
+   } else if ( IsDebuggerPresent() )
+       OutputDebugString("AFSD Service Stopped\n");
+
+    if ( fNetDetect ) {
+        // Start IP Address Change Monitor
+        if ( IsDebuggerPresent() )
+            OutputDebugString("Activating Network Change Monitor\n");
+        IpAddrChangeMonitorInit(g.hMain);
+    }
+    Main_EnableRemindTimer(TRUE);
+    return TRUE;
 }
 
 
 void ExitApp (void)
 {
+   KFW_cleanup();
    g.hMain = NULL;
 }