]> git.michaelhowe.org Git - packages/o/openafs.git/commitdiff
migrate-to-registry-20040713
authorJeffrey Altman <jaltman@mit.edu>
Tue, 13 Jul 2004 14:09:16 +0000 (14:09 +0000)
committerJeffrey Altman <jaltman@secure-endpoints.com>
Tue, 13 Jul 2004 14:09:16 +0000 (14:09 +0000)
* NTMakefile: missing commit from SMB AUTH patches

* cm_config.c: obtain location of CellServDB from registry
[HKLM\SOFTWARE\OpenAFS\Client] "CellServDB"=reg_sz
  This will allow us to move from %WINDIR%\afsdcell.ini to
  C:\Program Files\OpenAFS\Client\CellServDB.  This is necessary
  for compatibility with Terminal Server in which applications
  are not given access to %WINDIR%.

* cm_freelance.c: migrate freelance mount point data from
  %WINDIR%\afs_freelance.ini to the registry
[HKLM\SOFTWARE\OpenAFS\Client\Freelance]
  Each value, whose name is unimportant, contains one mount point
  entry.  After the first execution of this code, the current data
  in afs_freelance.ini will be moved to the registry and then all
  subsequent data access will be performed via the registry.
  The afs_freelance.ini file will be deleted after the migration
  has occurred.

src/WINNT/afsd/NTMakefile
src/WINNT/afsd/cm_config.c
src/WINNT/afsd/cm_freelance.c

index 76a9690a5289c18fba55b25419d74603915f0e9f..22d5f03a7e0c440c6b28a1583fb30ae9e8464202 100644 (file)
@@ -322,12 +322,13 @@ AFSD_EXEFILE = $(EXEDIR)\afsd.exe
 AFSD_SDKLIBS =\
        largeint.lib \
        netapi32.lib \
-    mpr.lib \
     dnsapi.lib \
        rpcrt4.lib \
        user32.lib \
     Dbghelp.lib \
-    strsafe.lib
+    strsafe.lib \
+    mpr.lib \
+    secur32.lib
 
 AFSD_EXELIBS =\
        $(DESTDIR)\lib\libosi.lib \
index ce6f9257fc29899afd62c5b601e6908e1de113b0..1462d04273da243beb7ecde66f46fa033d962098 100644 (file)
@@ -150,38 +150,63 @@ long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
 {
        char wdir[257];
     int tlen;
-    FILE *tfilep, *bestp, *tempp;
+    FILE *tfilep = NULL, *bestp, *tempp;
     char *tp;
     char lineBuffer[257];
     struct hostent *thp;
     char *valuep;
     struct sockaddr_in vlSockAddr;
     int inRightCell;
-    int foundCell;
+    int foundCell = 0;
     long code;
        int tracking = 1, partial = 0;
 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
     char *afsconf_path;
 #endif
+#if !defined(DJGPP)
+    HKEY hkClient;
+    PBYTE pFilename;
+    DWORD dwType, dwSize;
+#endif
 
-       foundCell = 0;
+#if !defined(DJGPP)
+    /* First attempt to find the CellServDB file via the registry */
+    if (RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
+                        "SOFTWARE\\OpenAFS\\Client",
+                        0,
+                        NULL,
+                        REG_OPTION_NON_VOLATILE,
+                        KEY_READ|KEY_WRITE,
+                        NULL,
+                        &hkClient,
+                        NULL) == ERROR_SUCCESS) {
+        if (RegQueryValueEx( hkClient, "CellServDB", 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS) {
+            pFilename = malloc(dwSize);
+            RegQueryValueEx( hkClient, "CellServDB", 0, &dwType, pFilename, &dwSize);
+            tfilep = fopen(pFilename, "r");
+        }
+        RegCloseKey(hkClient);
+    }
+#endif
 
+    /* If not found, fallback to old behavior */
+    if (!tfilep) {
 #if !defined(DJGPP)
-       code = GetWindowsDirectory(wdir, sizeof(wdir));
-    if (code == 0 || code > sizeof(wdir))
-        return -1;
+        code = GetWindowsDirectory(wdir, sizeof(wdir));
+        if (code == 0 || code > sizeof(wdir))
+            return -1;
 
-       /* add trailing backslash, if required */
-    tlen = strlen(wdir);
-    if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
+        /* add trailing backslash, if required */
+        tlen = strlen(wdir);
+        if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
 #else
-    strcpy(wdir, cm_confDir);
-    strcat(wdir,"/");
+        strcpy(wdir, cm_confDir);
+        strcat(wdir,"/");
 #endif /* !DJGPP */
         
-    strcat(wdir, AFS_CELLSERVDB);
-
-    tfilep = fopen(wdir, "r");
+        strcat(wdir, AFS_CELLSERVDB);
+        tfilep = fopen(wdir, "r");
+    }
 
 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
     if (!tfilep) {
@@ -688,22 +713,22 @@ void cm_GetConfigDir(char *dir)
 
 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
        code = GetWindowsDirectory(wdir, sizeof(wdir));
-      if (code == 0 || code > sizeof(wdir)) wdir[0] = 0;
+    if (code == 0 || code > sizeof(wdir)) wdir[0] = 0;
         
        /* add trailing backslash, if required */
-        tlen = strlen(wdir);
-        if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
+    tlen = strlen(wdir);
+    if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
 #else
 #ifdef DJGPP
-        strcpy(wdir,cm_confDir);
+    strcpy(wdir,cm_confDir);
 #else
-        afsconf_path = getenv("AFSCONF");
-        if (!afsconf_path)
-          strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
-        else
-          strcpy(wdir, afsconf_path);
+    afsconf_path = getenv("AFSCONF");
+    if (!afsconf_path)
+        strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
+    else
+        strcpy(wdir, afsconf_path);
 #endif /* !DJGPP */
-        strcat(wdir,"\\");
+    strcat(wdir,"\\");
 #endif /* DJGPP || WIN95 */
-        strcpy(dir, wdir);
+    strcpy(dir, wdir);
 }
index 0280852f1c27a82cc9c17c94a116a642141a0462..5d2057473bd1536b1d62d9c2329ca8ab3fc9fa62 100644 (file)
@@ -3,6 +3,7 @@
 
 #ifndef DJGPP
 #include <windows.h>
+#include <winreg.h>
 #include <winsock2.h>
 #else
 #include <netdb.h>
@@ -33,13 +34,6 @@ int cm_freelanceEnabled = 0;
 void cm_InitFakeRootDir();
 
 void cm_InitFreelance() {
-  
-#ifdef COMMENT
-    while ( !IsDebuggerPresent() ) {
-        Sleep(1000);
-    }
-#endif
-
        lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
   
        // yj: first we make a call to cm_initLocalMountPoints
@@ -344,13 +338,104 @@ int cm_reInitLocalMountPoints() {
 // process for the freelance client.
 /* to be called while holding freelance lock unless during init. */
 long cm_InitLocalMountPoints() {
-       
        FILE *fp;
        char line[512];
        int i;
        char* t;
        cm_localMountPoint_t* aLocalMountPoint;
        char hdir[120];
+    long code;
+    char rootCellName[256];
+#if !defined(DJGPP)
+    HKEY hkFreelance = 0;
+    DWORD dwType, dwSize;
+    DWORD dwMountPoints;
+    DWORD dwIndex;
+#endif
+
+#if !defined(DJGPP)
+    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+                        "SOFTWARE\\OpenAFS\\Client\\Freelance",
+                                               0,
+                        KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
+                        &hkFreelance) == ERROR_SUCCESS) {
+
+        RegQueryInfoKey( hkFreelance,
+                         NULL,  /* lpClass */
+                         NULL,  /* lpcClass */
+                         NULL,  /* lpReserved */
+                         NULL,  /* lpcSubKeys */
+                         NULL,  /* lpcMaxSubKeyLen */
+                         NULL,  /* lpcMaxClassLen */
+                         &dwMountPoints, /* lpcValues */
+                         NULL,  /* lpcMaxValueNameLen */
+                         NULL,  /* lpcMaxValueLen */
+                         NULL,  /* lpcbSecurityDescriptor */
+                         NULL   /* lpftLastWriteTime */
+                         );
+
+        if ( dwMountPoints == 0 ) {
+            sprintf(line,"%s#%s:root.cell.\n",rootCellName,rootCellName);
+            dwType = REG_SZ;
+            dwSize = strlen(line) + 1;
+            RegSetValueEx( hkFreelance, "0", 0, dwType, line, dwSize);
+            sprintf(line,".%s%%%s:root.cell.\n",rootCellName,rootCellName);
+            dwSize = strlen(line) + 1;
+            RegSetValueEx( hkFreelance, "1", 0, dwType, line, dwSize);
+            dwMountPoints = 2;
+        }
+
+        // get the number of entries there are from the first line
+        // that we read
+        cm_noLocalMountPoints = dwMountPoints;
+
+        // create space to store the local mount points
+        cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
+        aLocalMountPoint = cm_localMountPoints;
+
+        // now we read n lines and parse them into local mount points
+        // where n is the number of local mount points there are, as
+        // determined above.
+        // Each line in the ini file represents 1 local mount point and 
+        // is in the format xxx#yyy:zzz, where xxx is the directory
+        // entry name, yyy is the cell name and zzz is the volume name.
+        // #yyy:zzz together make up the mount point.
+        for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
+            TCHAR szValueName[16];
+            DWORD dwValueSize = 16;
+            dwSize = sizeof(line);
+            RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
+                          &dwType, line, &dwSize);
+
+            // line is not empty, so let's parse it
+            t = strchr(line, '#');
+            if (!t)
+                t = strchr(line, '%');
+            // make sure that there is a '#' or '%' separator in the line
+            if (!t) {
+                afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
+                fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
+                return -1;
+            }
+            aLocalMountPoint->namep=malloc(t-line+1);
+            memcpy(aLocalMountPoint->namep, line, t-line);
+            *(aLocalMountPoint->namep + (t-line)) = 0;
+               
+            aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
+            memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-2);
+            *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-2)) = 0;
+    
+            osi_Log2(afsd_logp,"found mount point: name %s, string %s",
+                      aLocalMountPoint->namep,
+                      aLocalMountPoint->mountPointStringp);
+
+            aLocalMountPoint++;
+        }
+
+        RegCloseKey(hkFreelance);
+        return 0;
+    }
+#endif
 
        cm_GetConfigDir(hdir);
        strcat(hdir, AFS_FREELANCE_INI);
@@ -359,8 +444,6 @@ long cm_InitLocalMountPoints() {
 
        // if we fail to open the file, create an empty one
        if (!fp) {
-        long code;
-        char rootCellName[256];
         fp = fopen(hdir, "w");
        code = cm_GetRootCellName(rootCellName);
         if (code == 0) {
@@ -379,6 +462,20 @@ long cm_InitLocalMountPoints() {
        // we successfully opened the file
        osi_Log0(afsd_logp,"opened afs_freelance.ini");
        
+
+#if !defined(DJGPP)
+    RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
+                    "SOFTWARE\\OpenAFS\\Client\\Freelance",
+                    0,
+                    NULL,
+                    REG_OPTION_NON_VOLATILE,
+                    KEY_READ|KEY_WRITE,
+                    NULL,
+                    &hkFreelance,
+                    NULL);
+    dwIndex = 0;
+#endif
+
        // now we read the first line to see how many entries
        // there are
        fgets(line, sizeof(line), fp);
@@ -414,6 +511,18 @@ long cm_InitLocalMountPoints() {
                        fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
                        return -1;
                }
+
+#if !defined(DJGPP)
+        if ( hkFreelance ) {
+            char szIndex[16];
+            /* we are migrating to the registry */
+            sprintf(szIndex,"%d",dwIndex++);
+            dwType = REG_SZ;
+            dwSize = strlen(line) + 1;
+            RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
+        }
+#endif 
+
                // line is not empty, so let's parse it
                t = strchr(line, '#');
         if (!t)
@@ -439,6 +548,12 @@ long cm_InitLocalMountPoints() {
         aLocalMountPoint++;
        }
        fclose(fp);
+#if !defined(DJGPP)
+    if ( hkFreelance ) {
+        RegCloseKey(hkFreelance);
+        DeleteFile(hdir);
+    }
+#endif
        return 0;
 }
 
@@ -458,6 +573,12 @@ long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw,
     char fullname[200];
     int n;
     int alias = 0;
+#if !defined(DJGPP)
+    HKEY hkFreelance = 0;
+    DWORD dwType, dwSize;
+    DWORD dwMountPoints;
+    DWORD dwIndex;
+#endif
 
     /* before adding, verify the cell name; if it is not a valid cell,
        don't add the mount point.
@@ -478,22 +599,67 @@ long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw,
 
     lock_ObtainMutex(&cm_Freelance_Lock);
 
-    cm_GetConfigDir(hfile);
-    strcat(hfile, AFS_FREELANCE_INI);
-    fp = fopen(hfile, "r+");
-    if (!fp)
-        return CM_ERROR_INVAL;
-    fgets(line, sizeof(line), fp);
-    n = atoi(line);
-    n++;
-    fseek(fp, 0, SEEK_SET);
-    fprintf(fp, "%d", n);
-    fseek(fp, 0, SEEK_END);
-    if (rw)
-        fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
-    else
-        fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
-    fclose(fp);
+#if !defined(DJGPP)
+    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+                      "SOFTWARE\\OpenAFS\\Client\\Freelance",
+                      0,
+                      KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
+                      &hkFreelance) == ERROR_SUCCESS) {
+
+        RegQueryInfoKey( hkFreelance,
+                         NULL,  /* lpClass */
+                         NULL,  /* lpcClass */
+                         NULL,  /* lpReserved */
+                         NULL,  /* lpcSubKeys */
+                         NULL,  /* lpcMaxSubKeyLen */
+                         NULL,  /* lpcMaxClassLen */
+                         &dwMountPoints, /* lpcValues */
+                         NULL,  /* lpcMaxValueNameLen */
+                         NULL,  /* lpcMaxValueLen */
+                         NULL,  /* lpcbSecurityDescriptor */
+                         NULL   /* lpftLastWriteTime */
+                         );
+
+        if (rw)
+            sprintf(line, "%s%%%s:%s\n", filename, fullname, volume);
+        else
+            sprintf(line, "%s#%s:%s\n", filename, fullname, volume);
+
+        /* If we are adding a new value, there must be an unused name
+         * within the range 0 to dwMountPoints 
+         */
+        for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
+            char szIndex[16];
+            sprintf(szIndex, "%d", dwIndex);
+            if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, NULL, &dwSize) != ERROR_SUCCESS) {
+                /* found an unused value */
+                dwType = REG_SZ;
+                dwSize = strlen(line) + 1;
+                RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
+                break;
+            }
+        }
+        RegCloseKey(hkFreelance);
+    } else 
+#endif
+    {
+        cm_GetConfigDir(hfile);
+        strcat(hfile, AFS_FREELANCE_INI);
+        fp = fopen(hfile, "r+");
+        if (!fp)
+            return CM_ERROR_INVAL;
+        fgets(line, sizeof(line), fp);
+        n = atoi(line);
+        n++;
+        fseek(fp, 0, SEEK_SET);
+        fprintf(fp, "%d", n);
+        fseek(fp, 0, SEEK_END);
+        if (rw)
+            fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
+        else
+            fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
+        fclose(fp);
+    }
     lock_ReleaseMutex(&cm_Freelance_Lock);
 
     /* cm_reInitLocalMountPoints(); */
@@ -507,59 +673,109 @@ long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw,
 
 long cm_FreelanceRemoveMount(char *toremove)
 {
-     int i, n;
-     char* cp;
-     char line[512];
-     char shortname[200];
-     char hfile[120], hfile2[120];
-     FILE *fp1, *fp2;
-     int found=0;
+    int i, n;
+    char* cp;
+    char line[512];
+    char shortname[200];
+    char hfile[120], hfile2[120];
+    FILE *fp1, *fp2;
+    int found=0;
+#if !defined(DJGPP)
+    HKEY hkFreelance = 0;
+    DWORD dwType, dwSize;
+    DWORD dwMountPoints;
+    DWORD dwIndex;
+#endif
 
     lock_ObtainMutex(&cm_Freelance_Lock);
 
-     cm_GetConfigDir(hfile);
-     strcat(hfile, AFS_FREELANCE_INI);
-     strcpy(hfile2, hfile);
-     strcat(hfile2, "2");
-     fp1=fopen(hfile, "r+");
-     if (!fp1)
-       return CM_ERROR_INVAL;
-     fp2=fopen(hfile2, "w+");
-     if (!fp2) {
-       fclose(fp1);
-       return CM_ERROR_INVAL;
-     }
-
-     fgets(line, sizeof(line), fp1);
-     n=atoi(line);
-     fprintf(fp2, "%d\n", n-1);
-
-     for (i=0; i<n; i++) {
-         fgets(line, sizeof(line), fp1);
-         cp=strchr(line, '#');
-         if (!cp)
-             cp=strchr(line, '%');
-         memcpy(shortname, line, cp-line);
-         shortname[cp-line]=0;
-
-         if (strcmp(shortname, toremove)==0) {
-
-         } else {
-             found = 1;
-             fputs(line, fp2);
-         }
-     }
-
-     fclose(fp1);
-     fclose(fp2);
-     if (!found)
-         return CM_ERROR_NOSUCHFILE;
-
-    unlink(hfile);
-    rename(hfile2, hfile);
 
-    lock_ReleaseMutex(&cm_Freelance_Lock);
+#if !defined(DJGPP)
+    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+                      "SOFTWARE\\OpenAFS\\Client\\Freelance",
+                      0,
+                      KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
+                      &hkFreelance) == ERROR_SUCCESS) {
+
+        RegQueryInfoKey( hkFreelance,
+                         NULL,  /* lpClass */
+                         NULL,  /* lpcClass */
+                         NULL,  /* lpReserved */
+                         NULL,  /* lpcSubKeys */
+                         NULL,  /* lpcMaxSubKeyLen */
+                         NULL,  /* lpcMaxClassLen */
+                         &dwMountPoints, /* lpcValues */
+                         NULL,  /* lpcMaxValueNameLen */
+                         NULL,  /* lpcMaxValueLen */
+                         NULL,  /* lpcbSecurityDescriptor */
+                         NULL   /* lpftLastWriteTime */
+                         );
+
+        for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
+            TCHAR szValueName[16];
+            DWORD dwValueSize = 16;
+            dwSize = sizeof(line);
+            RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
+                          &dwType, line, &dwSize);
+
+            cp=strchr(line, '#');
+            if (!cp)
+                cp=strchr(line, '%');
+            memcpy(shortname, line, cp-line);
+            shortname[cp-line]=0;
+
+            if (!strcmp(shortname, toremove)) {
+                RegDeleteValue( hkFreelance, szValueName );
+                break;
+            }
+        }
+        RegCloseKey(hkFreelance);
+    } else 
+#endif
+    {
+        cm_GetConfigDir(hfile);
+        strcat(hfile, AFS_FREELANCE_INI);
+        strcpy(hfile2, hfile);
+        strcat(hfile2, "2");
+        fp1=fopen(hfile, "r+");
+        if (!fp1)
+            return CM_ERROR_INVAL;
+        fp2=fopen(hfile2, "w+");
+        if (!fp2) {
+            fclose(fp1);
+            return CM_ERROR_INVAL;
+        }
+
+        fgets(line, sizeof(line), fp1);
+        n=atoi(line);
+        fprintf(fp2, "%d\n", n-1);
+
+        for (i=0; i<n; i++) {
+            fgets(line, sizeof(line), fp1);
+            cp=strchr(line, '#');
+            if (!cp)
+                cp=strchr(line, '%');
+            memcpy(shortname, line, cp-line);
+            shortname[cp-line]=0;
+
+            if (strcmp(shortname, toremove)==0) {
+
+            } else {
+                found = 1;
+                fputs(line, fp2);
+            }
+        }
+
+        fclose(fp1);
+        fclose(fp2);
+        if (!found)
+            return CM_ERROR_NOSUCHFILE;
 
+        unlink(hfile);
+        rename(hfile2, hfile);
+    }
+    
+    lock_ReleaseMutex(&cm_Freelance_Lock);
     cm_noteLocalMountPointChange();
     return 0;
 }