windows-misc-20040803
[openafs.git] / src / WINNT / client_creds / window.cpp
index f5e3577..d788d9f 100644 (file)
 extern "C" {
 #include <afs/param.h>
 #include <afs/stds.h>
+#include <afs/afskfw.h>
+#include "ipaddrchg.h"
 }
 
 #include "afscreds.h"
 
-
 /*
  * DEFINITIONS ________________________________________________________________
  *
@@ -184,6 +185,44 @@ BOOL CALLBACK Main_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
                break;
             }
          break;
+      case WM_OBTAIN_TOKENS:
+          if ( InterlockedIncrement (&g.fShowingMessage) != 1 )
+              InterlockedDecrement (&g.fShowingMessage);
+          else
+              ShowObtainCreds (wp, (char *)lp);
+          GlobalFree((void *)lp);
+          break;
+
+      case WM_START_SERVICE:
+          {
+              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))
+                          TestAndDoMapShare(SERVICE_START_PENDING);
+                                 if ( KFW_is_available() && KFW_AFS_wait_for_service_start() ) {
+#ifdef USE_MS2MIT
+                              KFW_import_windows_lsa();
+#endif /* USE_MS2MIT */
+                                         KFW_AFS_renew_tokens_for_all_cells();
+                                                 }
+
+                      CloseServiceHandle (hService);
+                  }
+
+                  CloseServiceHandle (hManager);
+              }
+              KFW_AFS_wait_for_service_start();
+              ObtainTokensFromUserIfNeeded(g.hMain);
+          }
+          break;
       }
 
    return FALSE;
@@ -207,63 +246,86 @@ void Main_Show (BOOL fShow)
 
 void Main_OnInitDialog (HWND hDlg)
 {
-   if (!g.fIsWinNT)
-      {
-      TCHAR szTitle[256];
-      GetString (szTitle, IDS_TITLE_95);
-      SetWindowText (hDlg, szTitle);
-      }
-
-   TCHAR szVersion[256];
-   DWORD dwPatch = 0;
-   TCHAR szUser[256];
-   GetString (szVersion, IDS_UNKNOWN);
-   GetString (szUser, IDS_UNKNOWN);
-
-   HKEY hk;
-   if (RegOpenKey (HKEY_LOCAL_MACHINE, REGSTR_PATH_AFS, &hk) == 0)
-      {
-      DWORD dwSize = sizeof(szVersion);
-      DWORD dwType = REG_SZ;
-      RegQueryValueEx (hk, REGVAL_AFS_VERSION, NULL, &dwType, (PBYTE)szVersion, &dwSize);
-
-      dwSize = sizeof(dwPatch);
-      dwType = REG_DWORD;
-      RegQueryValueEx (hk, REGVAL_AFS_PATCH, NULL, &dwType, (PBYTE)&dwPatch, &dwSize);
-      RegCloseKey (hk);
-      }
-
-   BOOL fFoundUserName = FALSE;
-   if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hk) == 0)
-      {
-      DWORD dwSize = sizeof(szUser);
-      DWORD dwType = REG_SZ;
-      if (RegQueryValueEx (hk, TEXT("DefaultUserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
-         fFoundUserName = TRUE;
-      RegCloseKey (hk);
-      }
-   if (!fFoundUserName)
-      {
-      if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Network\\Logon"), &hk) == 0)
-         {
-         DWORD dwSize = sizeof(szUser);
-         DWORD dwType = REG_SZ;
-         if (RegQueryValueEx (hk, TEXT("UserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
+    if (!g.fIsWinNT)
+    {
+        TCHAR szTitle[256];
+        GetString (szTitle, IDS_TITLE_95);
+        SetWindowText (hDlg, szTitle);
+    }
+
+    TCHAR szVersion[256];
+    DWORD dwPatch = 0;
+    TCHAR szUser[256];
+    GetString (szVersion, IDS_UNKNOWN);
+    GetString (szUser, IDS_UNKNOWN);
+
+    HKEY hk;
+    if (RegOpenKey (HKEY_LOCAL_MACHINE, REGSTR_PATH_AFS, &hk) == 0)
+    {
+        DWORD dwSize = sizeof(szVersion);
+        DWORD dwType = REG_SZ;
+        RegQueryValueEx (hk, REGVAL_AFS_VERSION, NULL, &dwType, (PBYTE)szVersion, &dwSize);
+
+        dwSize = sizeof(dwPatch);
+        dwType = REG_DWORD;
+        RegQueryValueEx (hk, REGVAL_AFS_PATCH, NULL, &dwType, (PBYTE)&dwPatch, &dwSize);
+        RegCloseKey (hk);
+    }
+
+    /* We should probably be using GetUserNameEx() for this */
+    BOOL fFoundUserName = FALSE;
+    if (RegOpenKey (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
+    {
+        DWORD dwSize = sizeof(szUser);
+        DWORD dwType = REG_SZ;
+        if (RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
             fFoundUserName = TRUE;
-         RegCloseKey (hk);
-         }
-      }
-
-   TCHAR szSource[ cchRESOURCE ];
-   TCHAR szTarget[ cchRESOURCE ];
-
-   GetString (szSource, (dwPatch) ? IDS_TITLE_VERSION : IDS_TITLE_VERSION_NOPATCH);
-   wsprintf (szTarget, szSource, szVersion, dwPatch);
-   SetDlgItemText (hDlg, IDC_TITLE_VERSION, szTarget);
-
-   GetDlgItemText (hDlg, IDC_TITLE_NT, szSource, cchRESOURCE);
-   wsprintf (szTarget, szSource, szUser);
-   SetDlgItemText (hDlg, IDC_TITLE_NT, szTarget);
+        RegCloseKey (hk);
+    }
+    if (!fFoundUserName ) 
+    {
+        if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Explorer"), &hk) == 0)
+        {
+            DWORD dwSize = sizeof(szUser);
+            DWORD dwType = REG_SZ;
+            if (RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
+                fFoundUserName = TRUE;
+            RegCloseKey (hk);
+        }
+    }
+    if (!fFoundUserName ) 
+    {
+        if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hk) == 0)
+        {
+            DWORD dwSize = sizeof(szUser);
+            DWORD dwType = REG_SZ;
+            if (RegQueryValueEx (hk, TEXT("DefaultUserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
+                fFoundUserName = TRUE;
+            RegCloseKey (hk);
+        }
+    }   
+    if (!fFoundUserName)
+    {
+        if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Network\\Logon"), &hk) == 0)
+        {
+            DWORD dwSize = sizeof(szUser);
+            DWORD dwType = REG_SZ;
+            if (RegQueryValueEx (hk, TEXT("UserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0)
+                fFoundUserName = TRUE;
+            RegCloseKey (hk);
+        }
+    }
+
+    TCHAR szSource[ cchRESOURCE ];
+    TCHAR szTarget[ cchRESOURCE ];
+
+    GetString (szSource, (dwPatch) ? IDS_TITLE_VERSION : IDS_TITLE_VERSION_NOPATCH);
+    wsprintf (szTarget, szSource, szVersion, dwPatch);
+    SetDlgItemText (hDlg, IDC_TITLE_VERSION, szTarget);
+
+    GetDlgItemText (hDlg, IDC_TITLE_NT, szSource, cchRESOURCE);
+    wsprintf (szTarget, szSource, szUser);
+    SetDlgItemText (hDlg, IDC_TITLE_NT, szTarget);
 }
 
 
@@ -350,19 +412,27 @@ void Main_OnSelectTab (void)
 
 void Main_OnCheckTerminate (void)
 {
-   HKEY hk;
-   if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
-      {
+    HKEY hk;
+
+    if (RegOpenKey (HKEY_CURRENT_USER, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
+    {
+        DWORD dwSize = sizeof(g.fStartup);
+        DWORD dwType = REG_DWORD;
+        RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
+        RegCloseKey (hk);
+    }
+    else if (RegOpenKey (HKEY_LOCAL_MACHINE, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
+    {
       DWORD dwSize = sizeof(g.fStartup);
       DWORD dwType = REG_DWORD;
       RegQueryValueEx (hk, TEXT("ShowTrayIcon"), NULL, &dwType, (PBYTE)&g.fStartup, &dwSize);
       RegCloseKey (hk);
-      }
+    }
 
-   Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
+    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
 
-   if (!g.fStartup)
-      Quit();
+    if (!g.fStartup)
+        Quit();
 }
 
 
@@ -562,6 +632,8 @@ size_t Main_FindExpiredCreds (void)
 {
    size_t retval = (size_t) -1;
    lock_ObtainMutex(&g.expirationCheckLock);
+   if ( KFW_is_available() )
+       KFW_AFS_renew_expiring_tokens();
    lock_ObtainMutex(&g.credsLock);
    for (size_t iCreds = 0; iCreds < g.cCreds; ++iCreds)
       {
@@ -586,8 +658,13 @@ size_t Main_FindExpiredCreds (void)
       llExpires /= c100ns1SECOND;
 
       if (llExpires <= (llNow + (LONGLONG)cminREMIND_WARN * csec1MINUTE))
+         {
+         if ( KFW_is_available() &&
+              KFW_AFS_renew_token_for_cell(g.aCreds[ iCreds ].szCell) )
+             continue;
          retval = (size_t) iCreds;
          break;
+         }
       }
    
    lock_ReleaseMutex(&g.credsLock);
@@ -640,10 +717,14 @@ void Terminate_OnOK (HWND hDlg)
       if (IsDlgButtonChecked (hDlg, IDC_STOP))
          {
          SC_HANDLE hManager;
-         if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
+             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_ALL_ACCESS)) != NULL)
+            if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), 
+                                         SERVICE_QUERY_STATUS | SERVICE_START)) != NULL)
                {
                SERVICE_STATUS Status;
                ControlService (hService, SERVICE_CONTROL_STOP, &Status);
@@ -658,14 +739,14 @@ void Terminate_OnOK (HWND hDlg)
 
    g.fStartup = IsDlgButtonChecked (hDlg, IDC_STARTUP);
 
-   HKEY hk;
-   if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
-      {
-      DWORD dwSize = sizeof(g.fStartup);
-      DWORD dwType = REG_DWORD;
-      RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
-      RegCloseKey (hk);
-      }
+    HKEY hk;
+    if (RegCreateKey (HKEY_CURRENT_USER, REGSTR_PATH_OPENAFS_CLIENT, &hk) == 0)
+    {
+        DWORD dwSize = sizeof(g.fStartup);
+        DWORD dwType = REG_DWORD;
+        RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
+        RegCloseKey (hk);
+    }
 
    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);