smb-auth-20040711
[openafs.git] / src / WINNT / afsd / afsd_init.c
index 95e04e8..f88f590 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <winsock2.h>
+#include <strsafe.h>
 
 #include <osi.h>
 #include "afsd.h"
 
 #include "smb.h"
 #include "cm_rpc.h"
+#include "lanahelper.h"
 
-extern int RXAFSCB_ExecuteRequest();
-extern int RXSTATS_ExecuteRequest();
+extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
+extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
 
 extern afs_int32 cryptall;
 
@@ -66,6 +68,8 @@ int logReady = 0;
 char cm_HostName[200];
 long cm_HostAddr;
 
+char cm_NetbiosName[MAX_NB_NAME_LENGTH];
+
 char cm_CachePath[200];
 DWORD cm_CachePathLen;
 
@@ -75,6 +79,10 @@ BOOL reportSessionStartups = FALSE;
 
 cm_initparams_v1 cm_initParams;
 
+char *cm_sysName = 0;
+int   cm_sysNameCount = 0;
+char *cm_sysNameList[MAXNUMSYSNAMES];
+
 /*
  * AFSD Initialization Log
  *
@@ -91,29 +99,45 @@ int cm_dnsEnabled = 1;
 
 char cm_NetBiosName[32];
 
-void cm_InitFakeRootDir();
+extern initUpperCaseTable();
+void afsd_initUpperCaseTable() 
+{
+       initUpperCaseTable();
+}
 
 void
 afsi_start()
 {
        char wd[100];
-       char t[100], u[100];
+       char t[100], u[100], *p, *path;
        int zilch;
        int code;
 
        afsi_file = INVALID_HANDLE_VALUE;
-       code = GetWindowsDirectory(wd, sizeof(wd));
-       if (code == 0) return;
-       strcat(wd, "\\afsd_init.log");
+    if (getenv("TEMP"))
+    {
+        StringCbCopyA(wd, sizeof(wd), getenv("TEMP"));
+    }
+    else
+    {
+        code = GetWindowsDirectory(wd, sizeof(wd));
+        if (code == 0) return;
+    }
+       StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
        GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
        afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                            OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
     SetFilePointer(afsi_file, 0, NULL, FILE_END);
        GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
-       strcat(t, ": Create log file\n");
-       strcat(u, ": Created log file\n");
+       StringCbCatA(t, sizeof(t), ": Create log file\n");
+       StringCbCatA(u, sizeof(u), ": Created log file\n");
        WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
        WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
+    p = "PATH=";
+    path = getenv("PATH");
+       WriteFile(afsi_file, p, strlen(p), &zilch, NULL);
+       WriteFile(afsi_file, path, strlen(path), &zilch, NULL);
+       WriteFile(afsi_file, "\n", 1, &zilch, NULL);
 }
 
 static int afsi_log_useTimestamp = 1;
@@ -121,17 +145,21 @@ static int afsi_log_useTimestamp = 1;
 void
 afsi_log(char *pattern, ...)
 {
-       char s[100], t[100], u[100];
+       char s[256], t[100], d[100], u[512];
        int zilch;
        va_list ap;
        va_start(ap, pattern);
 
-       vsprintf(s, pattern, ap);
+       StringCbVPrintfA(s, sizeof(s), pattern, ap);
     if ( afsi_log_useTimestamp ) {
         GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
-        sprintf(u, "%s: %s\n", t, s);
+               GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
+               StringCbPrintfA(u, sizeof(u), "%s %s: %s\n", d, t, s);
         if (afsi_file != INVALID_HANDLE_VALUE)
             WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
+#ifdef NOTSERVICE
+        printf("%s", u);
+#endif 
     } else {
         if (afsi_file != INVALID_HANDLE_VALUE)
             WriteFile(afsi_file, s, strlen(s), &zilch, NULL);
@@ -146,12 +174,13 @@ void afsd_ForceTrace(BOOL flush)
 {
        HANDLE handle;
        int len;
-       char buf[100];
+       char buf[256];
 
-       if (!logReady) return;
+       if (!logReady) 
+        return;
 
-       len = GetTempPath(99, buf);
-       strcpy(&buf[len], "/afsd.log");
+       len = GetTempPath(sizeof(buf)-10, buf);
+       StringCbCopyA(&buf[len], sizeof(buf)-len, "/afsd.log");
        handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
                            NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (handle == INVALID_HANDLE_VALUE) {
@@ -176,7 +205,9 @@ int afsd_InitCM(char **reasonP)
        long logChunkSize;
        long stats;
        long traceBufSize;
+    long maxcpus;
        long ltt, ltto;
+    long rx_mtu, rx_nojumbo;
        char rootCellName[256];
        struct rx_service *serverp;
        static struct rx_securityClass *nullServerSecurityClassp;
@@ -188,14 +219,19 @@ int afsd_InitCM(char **reasonP)
        long code;
        /*int freelanceEnabled;*/
        WSADATA WSAjunk;
+    lana_number_t lanaNum;
+    int i;
 
        WSAStartup(0x0101, &WSAjunk);
 
+    afsd_initUpperCaseTable();
+
        /* setup osidebug server at RPC slot 1000 */
        osi_LongToUID(1000, &debugID);
        code = osi_InitDebug(&debugID);
        afsi_log("osi_InitDebug code %d", code);
-//     osi_LockTypeSetDefault("stat"); /* comment this out for speed *
+
+    // osi_LockTypeSetDefault("stat"); /* comment this out for speed *
        if (code != 0) {
                *reasonP = "unknown error";
                return -1;
@@ -211,29 +247,72 @@ int afsd_InitCM(char **reasonP)
        srand(ntohl(cm_HostAddr));
 
        /* Look up configuration parameters in Registry */
-
        code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
                                0, KEY_QUERY_VALUE, &parmKey);
        if (code != ERROR_SUCCESS) {
                FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
                                | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                              NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
-               sprintf(buf,
+               StringCbPrintfA(buf, sizeof(buf),
                        "Failure in configuration while opening Registry: %s",
                        msgBuf);
                osi_panic(buf, __FILE__, __LINE__);
        }
 
-       dummyLen = sizeof(LANadapter);
-       code = RegQueryValueEx(parmKey, "LANadapter", NULL, NULL,
-                               (BYTE *) &LANadapter, &dummyLen);
+    dummyLen = sizeof(maxcpus);
+       code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
+                               (BYTE *) &maxcpus, &dummyLen);
+       if (code == ERROR_SUCCESS) {
+        HANDLE hProcess;
+        DWORD_PTR processAffinityMask, systemAffinityMask;
+
+        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION,
+                               FALSE, GetCurrentProcessId());
+        if ( hProcess != NULL &&
+             GetProcessAffinityMask(hProcess, &processAffinityMask, &systemAffinityMask) )
+        {
+            int i, n, bits;
+            DWORD_PTR mask, newAffinityMask;
+
+#if defined(_WIN64)
+            bits = 64;
+#else
+            bits = 32;
+#endif
+            for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits && n<maxcpus; i++ ) {
+                if ( processAffinityMask & mask ) {
+                    newAffinityMask |= mask;
+                    n++;
+                }
+                mask *= 2;
+            }
+
+            SetProcessAffinityMask(hProcess, newAffinityMask);
+            CloseHandle(hProcess);
+            afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, n);
+        } else {
+            afsi_log("CPU Restrictions set to %d cpu(s); unable to access process information", maxcpus);
+        }
+    }
+
+       dummyLen = sizeof(traceBufSize);
+       code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
+                               (BYTE *) &traceBufSize, &dummyLen);
        if (code == ERROR_SUCCESS)
-               afsi_log("LAN adapter number %d", LANadapter);
+               afsi_log("Trace Buffer size %d", traceBufSize);
        else {
-               LANadapter = -1;
-               afsi_log("Default LAN adapter number");
+               traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
+               afsi_log("Default trace buffer size %d", traceBufSize);
        }
 
+       /* setup and enable debug log */
+       afsd_logp = osi_LogCreate("afsd", traceBufSize);
+       afsi_log("osi_LogCreate log addr %x", (int)afsd_logp);
+    osi_LogEnable(afsd_logp);
+       logReady = 1;
+
+    osi_Log0(afsd_logp, "Log init");
+
        dummyLen = sizeof(cacheSize);
        code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
                                (BYTE *) &cacheSize, &dummyLen);
@@ -300,7 +379,7 @@ int afsd_InitCM(char **reasonP)
                ltt = 1;
                afsi_log("Logoff token transfer on by default");
        }
-        smb_LogoffTokenTransfer = ltt;
+    smb_LogoffTokenTransfer = ltt;
 
        if (ltt) {
                dummyLen = sizeof(ltto);
@@ -313,8 +392,10 @@ int afsd_InitCM(char **reasonP)
                        ltto = 10;
                        afsi_log("Default logoff token transfer timeout 10 seconds");
                }
-       }
-        smb_LogoffTransferTimeout = ltto;
+       } else {
+        ltto = 0;
+    }
+    smb_LogoffTransferTimeout = ltto;
 
        dummyLen = sizeof(cm_rootVolumeName);
        code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
@@ -322,7 +403,7 @@ int afsd_InitCM(char **reasonP)
        if (code == ERROR_SUCCESS)
                afsi_log("Root volume %s", cm_rootVolumeName);
        else {
-               strcpy(cm_rootVolumeName, "root.afs");
+               StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
                afsi_log("Default root volume name root.afs");
        }
 
@@ -333,7 +414,7 @@ int afsd_InitCM(char **reasonP)
                afsi_log("Mount root %s", cm_mountRoot);
                cm_mountRootLen = strlen(cm_mountRoot);
        } else {
-               strcpy(cm_mountRoot, "/afs");
+               StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
                cm_mountRootLen = 4;
                /* Don't log */
        }
@@ -346,7 +427,7 @@ int afsd_InitCM(char **reasonP)
        else {
                GetWindowsDirectory(cm_CachePath, sizeof(cm_CachePath));
                cm_CachePath[2] = 0;    /* get drive letter only */
-               strcat(cm_CachePath, "\\AFSCache");
+               StringCbCatA(cm_CachePath, sizeof(cm_CachePath), "\\AFSCache");
                afsi_log("Default cache path %s", cm_CachePath);
        }
 
@@ -361,17 +442,6 @@ int afsd_InitCM(char **reasonP)
                /* Don't log */
        }
 
-       dummyLen = sizeof(isGateway);
-       code = RegQueryValueEx(parmKey, "IsGateway", NULL, NULL,
-                               (BYTE *) &isGateway, &dummyLen);
-       if (code == ERROR_SUCCESS)
-               afsi_log("Set for %s service",
-                        isGateway ? "gateway" : "stand-alone");
-       else {
-               isGateway = 0;
-               /* Don't log */
-       }
-
        dummyLen = sizeof(reportSessionStartups);
        code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
                                (BYTE *) &reportSessionStartups, &dummyLen);
@@ -383,25 +453,22 @@ int afsd_InitCM(char **reasonP)
                /* Don't log */
        }
 
-       dummyLen = sizeof(traceBufSize);
-       code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
-                               (BYTE *) &traceBufSize, &dummyLen);
-       if (code == ERROR_SUCCESS)
-               afsi_log("Trace Buffer size %d", traceBufSize);
-       else {
-               traceBufSize = CM_CONFIGDEFAULT_TRACEBUFSIZE;
-               afsi_log("Default trace buffer size %d", traceBufSize);
-       }
+    for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
+        cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
+        cm_sysNameList[i][0] = '\0';
+    }
+    cm_sysName = cm_sysNameList[0];
 
-       dummyLen = sizeof(cm_sysName);
+       dummyLen = MAXSYSNAME;
        code = RegQueryValueEx(parmKey, "SysName", NULL, NULL,
                                cm_sysName, &dummyLen);
        if (code == ERROR_SUCCESS)
                afsi_log("Sys name %s", cm_sysName);
        else {
-               strcat(cm_sysName, "i386_nt40");
+               StringCbCopyA(cm_sysName, MAXSYSNAME, "i386_nt40");
                afsi_log("Default sys name %s", cm_sysName);
        }
+    cm_sysNameCount = 1;
 
        dummyLen = sizeof(cryptall);
        code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
@@ -442,43 +509,125 @@ int afsd_InitCM(char **reasonP)
        }
 #endif /* AFS_FREELANCE_CLIENT */
 
-    dummyLen = sizeof(cm_NetBiosName);
+    dummyLen = sizeof(buf);
     code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
-                           (BYTE *) &cm_NetBiosName, &dummyLen);
+                           (BYTE *) &buf, &dummyLen);
     if (code == ERROR_SUCCESS) {
-        afsi_log("Explicit NetBios name is used %s", cm_NetBiosName);
+        DWORD len = ExpandEnvironmentStrings(buf, cm_NetBiosName, MAX_NB_NAME_LENGTH);
+        if ( len > 0 && len <= MAX_NB_NAME_LENGTH ) {
+            afsi_log("Explicit NetBios name is used %s", cm_NetBiosName);
+        } else {
+            afsi_log("Unable to Expand Explicit NetBios name: %s", buf);
+            cm_NetBiosName[0] = 0;  /* turn it off */
+        }
     }
     else {
         cm_NetBiosName[0] = 0;   /* default off */
     }
 
+    dummyLen = sizeof(smb_hideDotFiles);
+    code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
+                           (BYTE *) &smb_hideDotFiles, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        smb_hideDotFiles = 1; /* default on */
+    }
+    afsi_log("Dot files/dirs will %sbe marked hidden",
+              smb_hideDotFiles ? "" : "not ");
+
+    dummyLen = sizeof(smb_maxMpxRequests);
+    code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
+                           (BYTE *) &smb_maxMpxRequests, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        smb_maxMpxRequests = 50;
+    }
+    afsi_log("Maximum number of multiplexed sessions is %d", smb_maxMpxRequests);
+
+    dummyLen = sizeof(smb_maxVCPerServer);
+    code = RegQueryValueEx(parmKey, "MaxVCPerServer", NULL, NULL,
+                           (BYTE *) &smb_maxVCPerServer, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        smb_maxVCPerServer = 100;
+    }
+    afsi_log("Maximum number of VCs per server is %d", smb_maxVCPerServer);
+
+       dummyLen = sizeof(smb_authType);
+       code = RegQueryValueEx(parmKey, "SMBAuthType", NULL, NULL,
+               (BYTE *) &smb_authType, &dummyLen);
+
+       if (code != ERROR_SUCCESS || 
+        (smb_authType != SMB_AUTH_EXTENDED && smb_authType != SMB_AUTH_NTLM && smb_authType != SMB_AUTH_NONE)) {
+               smb_authType = SMB_AUTH_EXTENDED; /* default is to use extended authentication */
+       }
+       afsi_log("SMB authentication type is %s", ((smb_authType == SMB_AUTH_NONE)?"NONE":((smb_authType == SMB_AUTH_EXTENDED)?"EXTENDED":"NTLM")));
+
+    dummyLen = sizeof(rx_nojumbo);
+    code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
+                           (BYTE *) &rx_nojumbo, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        rx_nojumbo = 0;
+    }
+    if(rx_nojumbo)
+        afsi_log("RX Jumbograms are disabled");
+
+    dummyLen = sizeof(rx_mtu);
+    code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
+                           (BYTE *) &rx_mtu, &dummyLen);
+    if (code != ERROR_SUCCESS || !rx_mtu) {
+        rx_mtu = -1;
+    }
+    if(rx_mtu != -1)
+        afsi_log("RX maximum MTU is %d", rx_mtu);
+
+    dummyLen = sizeof(ConnDeadtimeout);
+    code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
+                           (BYTE *) &ConnDeadtimeout, &dummyLen);
+    afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
+
+    dummyLen = sizeof(HardDeadtimeout);
+    code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
+                           (BYTE *) &HardDeadtimeout, &dummyLen);
+    afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
+
        RegCloseKey (parmKey);
 
+    /* Call lanahelper to get Netbios name, lan adapter number and gateway flag */
+    if(SUCCEEDED(code = lana_GetUncServerNameEx(cm_NetbiosName, &lanaNum, &isGateway, LANA_NETBIOS_NAME_FULL))) {
+        LANadapter = (lanaNum == LANA_INVALID)? -1: lanaNum;
+
+        if(LANadapter != -1)
+            afsi_log("LAN adapter number %d", LANadapter);
+        else
+            afsi_log("LAN adapter number not determined");
+
+        if(isGateway)
+            afsi_log("Set for gateway service");
+
+        afsi_log("Using >%s< as SMB server name", cm_NetbiosName);
+    } else {
+        /* something went horribly wrong.  We can't proceed without a netbios name */
+        StringCbPrintfA(buf,sizeof(buf),"Netbios name could not be determined: %li", code);
+        osi_panic(buf, __FILE__, __LINE__);
+    }
+
        /* setup early variables */
        /* These both used to be configurable. */
        smb_UseV3 = 1;
-        buf_bufferSize = CM_CONFIGDEFAULT_BLOCKSIZE;
+    buf_bufferSize = CM_CONFIGDEFAULT_BLOCKSIZE;
 
        /* turn from 1024 byte units into memory blocks */
-        cacheBlocks = (cacheSize * 1024) / buf_bufferSize;
+    cacheBlocks = (cacheSize * 1024) / buf_bufferSize;
         
-       /* setup and enable debug log */
-       afsd_logp = osi_LogCreate("afsd", traceBufSize);
-       afsi_log("osi_LogCreate log addr %x", afsd_logp);
-        osi_LogEnable(afsd_logp);
-       logReady = 1;
-
        /* get network related info */
        cm_noIPAddr = CM_MAXINTERFACE_ADDR;
        code = syscfg_GetIFInfo(&cm_noIPAddr,
-                               cm_IPAddr, cm_SubnetMask,
-                               cm_NetMtu, cm_NetFlags);
+                            cm_IPAddr, cm_SubnetMask,
+                            cm_NetMtu, cm_NetFlags);
 
        if ( (cm_noIPAddr <= 0) || (code <= 0 ) )
            afsi_log("syscfg_GetIFInfo error code %d", code);
        else
            afsi_log("First Network address %x SubnetMask %x",
-                    cm_IPAddr[0], cm_SubnetMask[0]);
+                 cm_IPAddr[0], cm_SubnetMask[0]);
 
        /*
         * Save client configuration for GetCacheConfig requests
@@ -493,9 +642,41 @@ int afsd_InitCM(char **reasonP)
        cm_initParams.setTime = 0;
        cm_initParams.memCache = 0;
 
+    /* Set RX parameters before initializing RX */
+    if ( rx_nojumbo ) {
+        rx_SetNoJumbo();
+        afsi_log("rx_SetNoJumbo successful");
+    }
+
+    if ( rx_mtu != -1 ) {
+        rx_SetMaxMTU(rx_mtu);
+        afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
+    }
+
+    /* Open Microsoft Firewall to allow in port 7001 */
+    {
+        HKEY hk;
+        DWORD dwDisp;
+        TCHAR* value = TEXT("7001:UDP:*:Enabled:AFS Cache Manager Callback");
+        if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, 
+                            "SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\DomainProfile\\GloballyOpenP", 
+                            0, TEXT("container"), 0, KEY_SET_VALUE, NULL, &hk, &dwDisp) == ERROR_SUCCESS)
+        {
+            RegSetValueEx (hk, TEXT("7001:UDP"), 0, REG_SZ, (PBYTE)value, sizeof(TCHAR) * (1+lstrlen(value)));
+            RegCloseKey (hk);
+        }
+        if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, 
+                            "SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\GloballyOpenP", 
+                            0, TEXT("container"), 0, KEY_SET_VALUE, NULL, &hk, &dwDisp) == ERROR_SUCCESS)
+        {
+            RegSetValueEx (hk, TEXT("7001:UDP"), 0, REG_SZ, (PBYTE)value, sizeof(TCHAR) * (1+lstrlen(value)));
+            RegCloseKey (hk);
+        }
+    }
+
        /* initialize RX, and tell it to listen to port 7001, which is used for
-         * callback RPC messages.
-         */
+     * callback RPC messages.
+     */
        code = rx_Init(htons(7001));
        afsi_log("rx_Init code %x", code);
        if (code != 0) {
@@ -508,25 +689,25 @@ int afsd_InitCM(char **reasonP)
 
        /* create an unauthenticated service #1 for callbacks */
        nullServerSecurityClassp = rxnull_NewServerSecurityObject();
-        serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
-               RXAFSCB_ExecuteRequest);
-       afsi_log("rx_NewService addr %x", serverp);
+    serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
+                            RXAFSCB_ExecuteRequest);
+       afsi_log("rx_NewService addr %x", (int)serverp);
        if (serverp == NULL) {
                *reasonP = "unknown error";
                return -1;
        }
 
        nullServerSecurityClassp = rxnull_NewServerSecurityObject();
-        serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
-               &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
-       afsi_log("rx_NewService addr %x", serverp);
+    serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
+                            &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
+       afsi_log("rx_NewService addr %x", (int)serverp);
        if (serverp == NULL) {
                *reasonP = "unknown error";
                return -1;
        }
         
-        /* start server threads, *not* donating this one to the pool */
-        rx_StartServer(0);
+    /* start server threads, *not* donating this one to the pool */
+    rx_StartServer(0);
        afsi_log("rx_StartServer");
 
        /* init user daemon, and other packages */
@@ -536,21 +717,21 @@ int afsd_InitCM(char **reasonP)
 
        cm_InitConn();
 
-        cm_InitCell();
+    cm_InitCell();
         
-        cm_InitServer();
+    cm_InitServer();
         
-        cm_InitVolume();
-        
-        cm_InitIoctl();
+    cm_InitVolume();
+
+    cm_InitIoctl();
         
-        smb_InitIoctl();
+    smb_InitIoctl();
         
-        cm_InitCallback();
+    cm_InitCallback();
         
-        cm_InitSCache(stats);
+    cm_InitSCache(stats);
         
-        code = cm_InitDCache(0, cacheBlocks);
+    code = cm_InitDCache(0, cacheBlocks);
        afsi_log("cm_InitDCache code %x", code);
        if (code != 0) {
                *reasonP = "error initializing cache";
@@ -558,28 +739,33 @@ int afsd_InitCM(char **reasonP)
        }
 
 #ifdef AFS_AFSDB_ENV
+#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
        if (cm_InitDNS(cm_dnsEnabled) == -1)
          cm_dnsEnabled = 0;  /* init failed, so deactivate */
        afsi_log("cm_InitDNS %d", cm_dnsEnabled);
 #endif
+#endif
 
        code = cm_GetRootCellName(rootCellName);
-       afsi_log("cm_GetRootCellName code %d rcn %s", code,
-                (code ? "<none>" : rootCellName));
-       if (code != 0 && !cm_freelanceEnabled) {
+       afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s", 
+             code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
+       if (code != 0 && !cm_freelanceEnabled) 
+    {
            *reasonP = "can't find root cell name in afsd.ini";
            return -1;
+    }
+    else if (cm_freelanceEnabled)
+        cm_rootCellp = NULL;
+
+    if (code == 0 && !cm_freelanceEnabled) 
+    {
+        cm_rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
+        afsi_log("cm_GetCell addr %x", (int)cm_rootCellp);
+        if (cm_rootCellp == NULL) 
+        {
+            *reasonP = "can't find root cell in afsdcell.ini";
+            return -1;
         }
-        else if (cm_freelanceEnabled)
-          cm_rootCellp = NULL;
-
-        if (code == 0 && !cm_freelanceEnabled) {
-         cm_rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
-          afsi_log("cm_GetCell addr %x", cm_rootCellp);
-         if (cm_rootCellp == NULL) {
-           *reasonP = "can't find root cell in afsdcell.ini";
-           return -1;
-         }
        }
 
 #ifdef AFS_FREELANCE_CLIENT
@@ -599,30 +785,31 @@ int afsd_InitDaemons(char **reasonP)
 
        /* this should really be in an init daemon from here on down */
 
-        if (!cm_freelanceEnabled) {
-          code = cm_GetVolumeByName(cm_rootCellp, cm_rootVolumeName, cm_rootUserp,
-                                    &req, CM_FLAG_CREATE, &cm_rootVolumep);
-          afsi_log("cm_GetVolumeByName code %x root vol %x", code,
-                   (code ? 0xffffffff : cm_rootVolumep));
-          if (code != 0) {
+    if (!cm_freelanceEnabled) {
+               osi_Log0(afsd_logp, "Loading Root Volume from cell");
+        code = cm_GetVolumeByName(cm_rootCellp, cm_rootVolumeName, cm_rootUserp,
+                                  &req, CM_FLAG_CREATE, &cm_rootVolumep);
+        afsi_log("cm_GetVolumeByName code %x root vol %x", code,
+                 (code ? (cm_volume_t *)-1 : cm_rootVolumep));
+        if (code != 0) {
             *reasonP = "can't find root volume in root cell";
             return -1;
-          }
         }
+    }
 
        /* compute the root fid */
        if (!cm_freelanceEnabled) {
-         cm_rootFid.cell = cm_rootCellp->cellID;
-         cm_rootFid.volume = cm_GetROVolumeID(cm_rootVolumep);
-         cm_rootFid.vnode = 1;
-         cm_rootFid.unique = 1;
+        cm_rootFid.cell = cm_rootCellp->cellID;
+        cm_rootFid.volume = cm_GetROVolumeID(cm_rootVolumep);
+        cm_rootFid.vnode = 1;
+        cm_rootFid.unique = 1;
        }
        else
-         cm_FakeRootFid(&cm_rootFid);
+        cm_FakeRootFid(&cm_rootFid);
         
-        code = cm_GetSCache(&cm_rootFid, &cm_rootSCachep, cm_rootUserp, &req);
+    code = cm_GetSCache(&cm_rootFid, &cm_rootSCachep, cm_rootUserp, &req);
        afsi_log("cm_GetSCache code %x scache %x", code,
-                (code ? 0xffffffff : cm_rootSCachep));
+             (code ? (cm_scache_t *)-1 : cm_rootSCachep));
        if (code != 0) {
                *reasonP = "unknown error";
                return -1;
@@ -636,30 +823,11 @@ int afsd_InitDaemons(char **reasonP)
 
 int afsd_InitSMB(char **reasonP, void *aMBfunc)
 {
-       char hostName[200];
-       char *ctemp;
-
        /* Do this last so that we don't handle requests before init is done.
      * Here we initialize the SMB listener.
      */
-    if (!cm_NetBiosName[0])
-    {
-        strcpy(hostName, cm_HostName);
-        ctemp = strchr(hostName, '.'); /* turn ntdfs.* into ntdfs */
-        if (ctemp) *ctemp = 0;
-            hostName[11] = 0; /* ensure that even after adding the -A, we
-                               * leave one byte free for the netbios server
-                               * type.
-                               */
-        strcat(hostName, "-AFS");
-    } else {
-        strcpy(hostName, cm_NetBiosName);
-    }
-    _strupr(hostName);
-
-    smb_Init(afsd_logp, hostName, smb_UseV3, LANadapter, numSvThreads, 
-             aMBfunc);
-       afsi_log("smb_Init");
+    smb_Init(afsd_logp, cm_NetbiosName, smb_UseV3, LANadapter, numSvThreads, aMBfunc);
+    afsi_log("smb_Init");
 
        return 0;
 }
@@ -740,7 +908,7 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
   
     offset = 0;
   
-    afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol\n" );
+    afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
   
     for ( frameNum = 0; ; ++ frameNum )
     {
@@ -764,7 +932,7 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
       
         if ( s.AddrPC.Offset == 0 )
         {
-            afsi_log("(-nosymbols- PC == 0)\n" );
+            afsi_log("(-nosymbols- PC == 0)" );
         }
         else
         { 
@@ -773,7 +941,7 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
             {
                 if ( GetLastError() != ERROR_INVALID_ADDRESS )
                 {
-                    afsi_log("SymGetSymFromAddr(): errno = %lu\n", 
+                    afsi_log("SymGetSymFromAddr(): errno = %lu", 
                              GetLastError());
                 }
             }
@@ -785,7 +953,7 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
 
                 if ( offset != 0 )
                 {
-                    afsi_log(" %+ld bytes\n", (long) offset);
+                    afsi_log(" %+ld bytes", (long) offset);
                 }
             }
 
@@ -793,16 +961,15 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
             {
                 if (GetLastError() != ERROR_INVALID_ADDRESS)
                 {
-                    afsi_log("Error: SymGetLineFromAddr(): errno = %lu\n", 
+                    afsi_log("Error: SymGetLineFromAddr(): errno = %lu", 
                              GetLastError());
                 }
             }
             else
             {
-                afsi_log("    Line: %s(%lu) %+ld bytes\n", Line.FileName, 
+                afsi_log("    Line: %s(%lu) %+ld bytes", Line.FileName, 
                          Line.LineNumber, offset);
             }
-         
         }
       
         /* no return address means no deeper stackframe */
@@ -854,7 +1021,7 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
           
     if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
     {
-        afsi_log("EXCEPTION_BREAKPOINT - continue execition ...\n");
+        afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
     
 #ifdef _DEBUG
         if (allocRequestBrk)