windows-pioctl-disable-service-mgr-check-20080320
[openafs.git] / src / sys / pioctl_nt.c
index f924b4f..009d874 100644 (file)
@@ -22,24 +22,32 @@ RCSID
 #include <string.h>
 #include <winioctl.h>
 #include <winsock2.h>
+#define SECURITY_WIN32
+#include <security.h>
 #include <nb30.h>
 
 #include <osi.h>
 
 #include <cm.h>
-#include <cm_dir.h>
+#include <cm_server.h>
 #include <cm_cell.h>
 #include <cm_user.h>
 #include <cm_conn.h>
 #include <cm_scache.h>
 #include <cm_buf.h>
+#include <cm_dir.h>
 #include <cm_utils.h>
 #include <cm_ioctl.h>
 
 #include <smb.h>
 #include <pioctl_nt.h>
-static char AFSConfigKeyName[] =
-    "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
+#include <WINNT/afsreg.h>
+#include <lanahelper.h>
+
+#include <loadfuncs-krb5.h>
+#include <krb5.h>
+
+static char AFSConfigKeyName[] = AFSREG_CLT_SVC_PARAM_SUBKEY;
 
 #define FS_IOCTLREQUEST_MAXSIZE        8192
 /* big structure for representing and storing an IOCTL request */
@@ -58,12 +66,15 @@ CMtoUNIXerror(int cm_code)
     case CM_ERROR_NOACCESS:
        return EACCES;
     case CM_ERROR_NOSUCHFILE:
+    case CM_ERROR_NOSUCHPATH:
+    case CM_ERROR_BPLUS_NOMATCH:
        return ENOENT;
     case CM_ERROR_INVAL:
        return EINVAL;
     case CM_ERROR_BADFD:
        return EBADF;
     case CM_ERROR_EXISTS:
+    case CM_ERROR_INEXACT_MATCH:
        return EEXIST;
     case CM_ERROR_CROSSDEVLINK:
        return EXDEV;
@@ -83,8 +94,17 @@ CMtoUNIXerror(int cm_code)
        return EDOM;            /* hack */
     case CM_ERROR_TOOMANYBUFS:
        return EFBIG;           /* hack */
+    case CM_ERROR_ALLBUSY:
+        return EBUSY;
+    case CM_ERROR_ALLDOWN:
+        return ENOSYS;          /* hack */
+    case CM_ERROR_ALLOFFLINE:
+        return ENXIO;           /* hack */
     default:
-       return ENOTTY;
+       if (cm_code > 0 && cm_code < EILSEQ)
+           return cm_code;
+       else
+           return ENOTTY;
     }
 }
 
@@ -95,92 +115,582 @@ InitFSRequest(fs_ioctlRequest_t * rp)
     rp->nbytes = 0;
 }
 
-static long
-GetIoctlHandle(char *fileNamep, HANDLE * handlep)
+static BOOL
+IoctlDebug(void)
 {
-    char *drivep;
-    char hostName[256];
-    char tbuffer[100];
-    char explicitNetbiosName[32];
-    char *ctemp;
-    HANDLE fh;
-    HKEY parmKey;
-    DWORD dummyLen;
-    long code;
+    static int init = 0;
+    static BOOL debug = 0;
 
-    if (fileNamep) {
-       drivep = strchr(fileNamep, ':');
-       if (drivep && (drivep - fileNamep) >= 1) {
-           tbuffer[0] = *(drivep - 1);
-           tbuffer[1] = ':';
-           strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
-       } else
-           strcpy(tbuffer, SMB_IOCTL_FILENAME);
-    } else {
-       /* No file name specified, use UNC name */
-       /* First look for gateway host in Registry */
-       code =
-           RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName, 0,
-                        KEY_QUERY_VALUE, &parmKey);
-       if (code != ERROR_SUCCESS)
-           goto nogateway;
-   dummyLen = sizeof(explicitNetbiosName);
-   code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
-                           (BYTE *) &explicitNetbiosName, &dummyLen);
-   if (!code == ERROR_SUCCESS) 
-   {
-       explicitNetbiosName[0] = 0;
-   }
-   dummyLen = sizeof(hostName);
-       code =
-           RegQueryValueEx(parmKey, "Gateway", NULL, NULL, hostName,
-                           &dummyLen);
-       RegCloseKey(parmKey);
-       if (code == ERROR_SUCCESS)
-           goto havehost;
-      nogateway:
-       /* No gateway name in registry; use ourself */
-#ifndef AFS_WIN95_ENV
-       gethostname(hostName, sizeof(hostName));
-#else
-       {
-           int hostsize;
-           /* DJGPP version of gethostname gets the NetBIOS
-            * name of the machine, so that is what we are using for
-            * the AFS server name instead of the DNS name. */
-           hostsize = sizeof(hostName);
-           GetComputerName(hostName, &hostsize);
-       }
-#endif /* AFS_WIN95_ENV */
+    if ( !init ) {
+        HKEY hk;
+
+        if (RegOpenKey (HKEY_LOCAL_MACHINE, 
+                         TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
+        {
+            DWORD dwSize = sizeof(BOOL);
+            DWORD dwType = REG_DWORD;
+            RegQueryValueEx (hk, TEXT("IoctlDebug"), NULL, &dwType, (PBYTE)&debug, &dwSize);
+            RegCloseKey (hk);
+        }
 
-      havehost:
-        ctemp = strchr(hostName, '.'); /* turn ntafs.* into ntafs */
-        if (ctemp)
-            *ctemp = 0;
-        hostName[11] = 0;
+        init = 1;
+    }
+
+    return debug;
+}
 
-        if (explicitNetbiosName[0])
+static BOOL
+DisableServiceManagerCheck(void)
+{
+    static int init = 0;
+    static BOOL smcheck = 0;
+
+    if ( !init ) {
+        HKEY hk;
+
+        if (RegOpenKey (HKEY_LOCAL_MACHINE, 
+                         TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
         {
-            _strupr(explicitNetbiosName);
-            sprintf(tbuffer, "\\\\%s\\all%s",
-                     explicitNetbiosName, SMB_IOCTL_FILENAME);
+            DWORD dwSize = sizeof(BOOL);
+            DWORD dwType = REG_DWORD;
+            RegQueryValueEx (hk, TEXT("DisableIoctlSMCheck"), NULL, &dwType, (PBYTE)&smcheck, &dwSize);
+            RegCloseKey (hk);
         }
+
+        init = 1;
+    }
+
+    return smcheck;
+}
+
+static DWORD 
+GetServiceStatus(
+    LPSTR lpszMachineName, 
+    LPSTR lpszServiceName,
+    DWORD *lpdwCurrentState) 
+{ 
+    DWORD           hr               = NOERROR; 
+    SC_HANDLE       schSCManager     = NULL; 
+    SC_HANDLE       schService       = NULL; 
+    DWORD           fdwDesiredAccess = 0; 
+    SERVICE_STATUS  ssServiceStatus  = {0}; 
+    BOOL            fRet             = FALSE; 
+
+    *lpdwCurrentState = 0; 
+    fdwDesiredAccess = GENERIC_READ; 
+    schSCManager = OpenSCManager(lpszMachineName,  
+                                 NULL,
+                                 fdwDesiredAccess); 
+    if(schSCManager == NULL) 
+    { 
+        hr = GetLastError();
+        goto cleanup; 
+    } 
+    schService = OpenService(schSCManager,
+                             lpszServiceName,
+                             fdwDesiredAccess); 
+    if(schService == NULL) 
+    { 
+        hr = GetLastError();
+        goto cleanup; 
+    } 
+    fRet = QueryServiceStatus(schService,
+                              &ssServiceStatus); 
+    if(fRet == FALSE) 
+    { 
+        hr = GetLastError(); 
+        goto cleanup; 
+    } 
+    *lpdwCurrentState = ssServiceStatus.dwCurrentState; 
+cleanup: 
+    CloseServiceHandle(schService); 
+    CloseServiceHandle(schSCManager); 
+    return(hr); 
+} 
+
+// krb5 functions
+DECL_FUNC_PTR(krb5_cc_default_name);
+DECL_FUNC_PTR(krb5_cc_set_default_name);
+DECL_FUNC_PTR(krb5_get_default_config_files);
+DECL_FUNC_PTR(krb5_free_config_files);
+DECL_FUNC_PTR(krb5_free_context);
+DECL_FUNC_PTR(krb5_get_default_realm);
+DECL_FUNC_PTR(krb5_free_default_realm);
+DECL_FUNC_PTR(krb5_init_context);
+DECL_FUNC_PTR(krb5_cc_default);
+DECL_FUNC_PTR(krb5_parse_name);
+DECL_FUNC_PTR(krb5_free_principal);
+DECL_FUNC_PTR(krb5_cc_close);
+DECL_FUNC_PTR(krb5_cc_get_principal);
+DECL_FUNC_PTR(krb5_build_principal);
+DECL_FUNC_PTR(krb5_c_random_make_octets);
+DECL_FUNC_PTR(krb5_get_init_creds_password);
+DECL_FUNC_PTR(krb5_free_cred_contents);
+DECL_FUNC_PTR(krb5_cc_resolve);
+DECL_FUNC_PTR(krb5_unparse_name);
+DECL_FUNC_PTR(krb5_free_unparsed_name);
+
+FUNC_INFO krb5_fi[] = {
+    MAKE_FUNC_INFO(krb5_cc_default_name),
+    MAKE_FUNC_INFO(krb5_cc_set_default_name),
+    MAKE_FUNC_INFO(krb5_get_default_config_files),
+    MAKE_FUNC_INFO(krb5_free_config_files),
+    MAKE_FUNC_INFO(krb5_free_context),
+    MAKE_FUNC_INFO(krb5_get_default_realm),
+    MAKE_FUNC_INFO(krb5_free_default_realm),
+    MAKE_FUNC_INFO(krb5_init_context),
+    MAKE_FUNC_INFO(krb5_cc_default),
+    MAKE_FUNC_INFO(krb5_parse_name),
+    MAKE_FUNC_INFO(krb5_free_principal),
+    MAKE_FUNC_INFO(krb5_cc_close),
+    MAKE_FUNC_INFO(krb5_cc_get_principal),
+    MAKE_FUNC_INFO(krb5_build_principal),
+    MAKE_FUNC_INFO(krb5_c_random_make_octets),
+    MAKE_FUNC_INFO(krb5_get_init_creds_password),
+    MAKE_FUNC_INFO(krb5_free_cred_contents),
+    MAKE_FUNC_INFO(krb5_cc_resolve),
+    MAKE_FUNC_INFO(krb5_unparse_name),
+    MAKE_FUNC_INFO(krb5_free_unparsed_name),
+    END_FUNC_INFO
+};
+
+static int
+LoadFuncs(
+    const char* dll_name,
+    FUNC_INFO fi[],
+    HINSTANCE* ph,  // [out, optional] - DLL handle
+    int* pindex,    // [out, optional] - index of last func loaded (-1 if none)
+    int cleanup,    // cleanup function pointers and unload on error
+    int go_on,      // continue loading even if some functions cannot be loaded
+    int silent      // do not pop-up a system dialog if DLL cannot be loaded
+    )
+{
+    HINSTANCE h;
+    int i, n, last_i;
+    int error = 0;
+    UINT em;
+
+    if (ph) *ph = 0;
+    if (pindex) *pindex = -1;
+
+    for (n = 0; fi[n].func_ptr_var; n++)
+        *(fi[n].func_ptr_var) = 0;
+
+    if (silent)
+        em = SetErrorMode(SEM_FAILCRITICALERRORS);
+    h = LoadLibrary(dll_name);
+    if (silent)
+        SetErrorMode(em);
+
+    if (!h)
+        return 0;
+
+    last_i = -1;
+    for (i = 0; (go_on || !error) && (i < n); i++)
+    {
+        void* p = (void*)GetProcAddress(h, fi[i].func_name);
+        if (!p)
+            error = 1;
         else
         {
-            _strupr(hostName);
-            sprintf(tbuffer, "\\\\%s-AFS\\all%s",
-                     hostName, SMB_IOCTL_FILENAME);
+            last_i = i;
+            *(fi[i].func_ptr_var) = p;
+        }
+    }
+    if (pindex) *pindex = last_i;
+    if (error && cleanup && !go_on) {
+        for (i = 0; i < n; i++) {
+            *(fi[i].func_ptr_var) = 0;
+        }
+        FreeLibrary(h);
+        return 0;
+    }
+    if (ph) *ph = h;
+    if (error) return 0;
+    return 1;
+}
+#if defined(_IA64_) || defined(_AMD64_)
+#define KERB5DLL "krb5_64.dll"
+#else
+#define KERB5DLL "krb5_32.dll"
+#endif
+static BOOL
+IsKrb5Available()
+{
+    static HINSTANCE hKrb5DLL = 0;
+
+    if ( hKrb5DLL )
+        return TRUE;
+
+    hKrb5DLL = LoadLibrary(KERB5DLL);
+    if (hKrb5DLL) {
+        if (!LoadFuncs(KERB5DLL, krb5_fi, 0, 0, 1, 0, 0))
+        {
+            FreeLibrary(hKrb5DLL);
+            hKrb5DLL = 0;
+            return FALSE;
+        }
+        return TRUE;
+    }
+    return FALSE;
+}
+
+static BOOL
+GetLSAPrincipalName(char * szUser, DWORD *dwSize)
+{
+    krb5_context   ctx = 0;
+    krb5_error_code code;
+    krb5_ccache mslsa_ccache=0;
+    krb5_principal princ = 0;
+    char * pname = 0;
+    BOOL success = 0;
+
+    if (!IsKrb5Available())
+        return FALSE;
+
+    if (code = pkrb5_init_context(&ctx))
+        goto cleanup;
+
+    if (code = pkrb5_cc_resolve(ctx, "MSLSA:", &mslsa_ccache))
+        goto cleanup;
+
+    if (code = pkrb5_cc_get_principal(ctx, mslsa_ccache, &princ))
+        goto cleanup;
+
+    if (code = pkrb5_unparse_name(ctx, princ, &pname))
+        goto cleanup;
+
+    if ( strlen(pname) < *dwSize ) {
+        strncpy(szUser, pname, *dwSize);
+        szUser[*dwSize-1] = '\0';
+        success = 1;
+    }
+    *dwSize = (DWORD)strlen(pname);
+
+  cleanup:
+    if (pname)
+        pkrb5_free_unparsed_name(ctx, pname);
+
+    if (princ)
+        pkrb5_free_principal(ctx, princ);
+
+    if (mslsa_ccache)
+        pkrb5_cc_close(ctx, mslsa_ccache);
+
+    if (ctx)
+        pkrb5_free_context(ctx);
+    return success;
+}
+
+static long
+GetIoctlHandle(char *fileNamep, HANDLE * handlep)
+{
+    char *drivep;
+    char netbiosName[MAX_NB_NAME_LENGTH];
+    DWORD CurrentState = 0;
+    char  HostName[64] = "";
+    char tbuffer[256]="";
+    HANDLE fh;
+    HKEY hk;
+    char szUser[128] = "";
+    char szClient[MAX_PATH] = "";
+    char szPath[MAX_PATH] = "";
+    NETRESOURCE nr;
+    DWORD res;
+    DWORD ioctlDebug = IoctlDebug();
+    DWORD gle;
+    DWORD dwSize = sizeof(szUser);
+
+    memset(HostName, '\0', sizeof(HostName));
+    gethostname(HostName, sizeof(HostName));
+    if (!DisableServiceManagerCheck() &&
+        GetServiceStatus(HostName, TEXT("TransarcAFSDaemon"), &CurrentState) == NOERROR &&
+       CurrentState != SERVICE_RUNNING)
+       return -1;
+
+    if (fileNamep) {
+        drivep = strchr(fileNamep, ':');
+        if (drivep && (drivep - fileNamep) >= 1) {
+            tbuffer[0] = *(drivep - 1);
+            tbuffer[1] = ':';
+            strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
+        } else if (fileNamep[0] == fileNamep[1] && 
+                  (fileNamep[0] == '\\' || fileNamep[0] == '/'))
+        {
+            int count = 0, i = 0;
+
+            while (count < 4 && fileNamep[i]) {
+                tbuffer[i] = fileNamep[i];
+                if ( tbuffer[i] == '\\' ||
+                    tbuffer[i] == '/')
+                    count++;
+               i++;
+            }
+            if (fileNamep[i] == 0)
+                tbuffer[i++] = '\\';
+            tbuffer[i] = 0;
+            strcat(tbuffer, SMB_IOCTL_FILENAME);
+        } else {
+            char curdir[256]="";
+
+            GetCurrentDirectory(sizeof(curdir), curdir);
+            if ( curdir[1] == ':' ) {
+                tbuffer[0] = curdir[0];
+                tbuffer[1] = ':';
+                strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
+            } else if (curdir[0] == curdir[1] &&
+                       (curdir[0] == '\\' || curdir[0] == '/')) 
+            {
+                int count = 0, i = 0;
+
+                while (count < 4 && curdir[i]) {
+                    tbuffer[i] = curdir[i];
+                   if ( tbuffer[i] == '\\' ||
+                        tbuffer[i] == '/')
+                       count++;
+                   i++;
+                }
+                if (tbuffer[i] == 0)
+                    tbuffer[i++] = '\\';
+                tbuffer[i] = 0;
+                strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
+            }
         }
     }
+    if (!tbuffer[0]) {
+        /* No file name starting with drive colon specified, use UNC name */
+        lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
+        sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
+    }
 
     fflush(stdout);
     /* now open the file */
     fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
                    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
                    FILE_FLAG_WRITE_THROUGH, NULL);
-    fflush(stdout);
-    if (fh == INVALID_HANDLE_VALUE)
-       return -1;
+
+       fflush(stdout);
+
+    if (fh == INVALID_HANDLE_VALUE) {
+        int  gonext = 0;
+
+        gle = GetLastError();
+        if (gle && ioctlDebug ) {
+            char buf[4096];
+
+            if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                               NULL,
+                               gle,
+                               MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                               buf,
+                               4096,
+                               (va_list *) NULL
+                               ) )
+            {
+                fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                        tbuffer,gle,buf);
+            }
+        }
+
+        lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL);
+
+        if (RegOpenKey (HKEY_CURRENT_USER, 
+                         TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
+        {
+            DWORD dwType = REG_SZ;
+            RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize);
+            RegCloseKey (hk);
+        }
+
+        if ( szUser[0] ) {
+            if ( ioctlDebug )
+                fprintf(stderr, "pioctl Explorer logon user: [%s]\r\n",szUser);
+
+            sprintf(szPath, "\\\\%s", szClient);
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=0;
+            nr.lpRemoteName=szPath;
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+                gonext = 1;
+            }
+
+            sprintf(szPath, "\\\\%s\\all", szClient);
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+                gonext = 1;
+            }
+
+            if (gonext)
+                goto try_lsa_principal;
+
+            fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                             FILE_FLAG_WRITE_THROUGH, NULL);
+            fflush(stdout);
+            if (fh == INVALID_HANDLE_VALUE) {
+                gle = GetLastError();
+                if (gle && ioctlDebug ) {
+                    char buf[4096];
+
+                    if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                                        NULL,
+                                        gle,
+                                        MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                                        buf,
+                                        4096,
+                                        (va_list *) NULL
+                                        ) )
+                    {
+                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                                 tbuffer,gle,buf);
+                    }
+                }
+            }
+        }
+    }
+
+  try_lsa_principal:
+    if (fh == INVALID_HANDLE_VALUE) {
+        int  gonext = 0;
+
+        dwSize = sizeof(szUser);
+        if (GetLSAPrincipalName(szUser, &dwSize)) {
+            if ( ioctlDebug )
+                fprintf(stderr, "pioctl LSA Principal logon user: [%s]\r\n",szUser);
+
+            sprintf(szPath, "\\\\%s", szClient);
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=0;
+            nr.lpRemoteName=szPath;
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+                gonext = 1;
+            }
+
+            sprintf(szPath, "\\\\%s\\all", szClient);
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+                gonext = 1;
+            }
+
+            if (gonext)
+                goto try_sam_compat;
+
+            fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                             FILE_FLAG_WRITE_THROUGH, NULL);
+            fflush(stdout);
+            if (fh == INVALID_HANDLE_VALUE) {
+                gle = GetLastError();
+                if (gle && ioctlDebug ) {
+                    char buf[4096];
+
+                    if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                                        NULL,
+                                        gle,
+                                        MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                                        buf,
+                                        4096,
+                                        (va_list *) NULL
+                                        ) )
+                    {
+                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                                 tbuffer,gle,buf);
+                    }
+                }
+            }
+        }
+    }
+
+  try_sam_compat:
+    if ( fh == INVALID_HANDLE_VALUE ) {
+        dwSize = sizeof(szUser);
+        if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
+            if ( ioctlDebug )
+                fprintf(stderr, "pioctl SamCompatible logon user: [%s]\r\n",szUser);
+
+            sprintf(szPath, "\\\\%s", szClient);
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=0;
+            nr.lpRemoteName=szPath;
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+            }
+
+            sprintf(szPath, "\\\\%s\\all", szClient);
+            res = WNetAddConnection2(&nr,NULL,szUser,0);
+            if (res) {
+                if ( ioctlDebug ) {
+                    fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
+                             szPath,szUser,res);
+                }
+                return -1;
+            }
+
+            fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
+                             FILE_FLAG_WRITE_THROUGH, NULL);
+            fflush(stdout);
+            if (fh == INVALID_HANDLE_VALUE) {
+                gle = GetLastError();
+                if (gle && ioctlDebug ) {
+                    char buf[4096];
+
+                    if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                                        NULL,
+                                        gle,
+                                        MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
+                                        buf,
+                                        4096,
+                                        (va_list *) NULL
+                                        ) )
+                    {
+                        fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
+                                 tbuffer,gle,buf);
+                    }
+                }
+                return -1;
+            }
+        } else {
+            fprintf(stderr, "GetUserNameEx(NameSamCompatible) failed: 0x%X\r\n", GetLastError());
+            return -1;
+        }
+    }
 
     /* return fh and success code */
     *handlep = fh;
@@ -192,19 +702,31 @@ Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
 {
     long rcount;
     long ioCount;
+    DWORD gle;
 
-    rcount = reqp->mp - reqp->data;
-    if (rcount <= 0)
+    rcount = (long)(reqp->mp - reqp->data);
+    if (rcount <= 0) {
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl Transceive rcount <= 0: %d\r\n",rcount);
        return EINVAL;          /* not supposed to happen */
+    }
 
     if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
        /* failed to write */
-       return GetLastError();
+       gle = GetLastError();
+
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl Transceive WriteFile failed: 0x%X\r\n",gle);
+        return gle;
     }
 
     if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
        /* failed to read */
-       return GetLastError();
+       gle = GetLastError();
+
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl Transceive ReadFile failed: 0x%X\r\n",gle);
+        return gle;
     }
 
     reqp->nbytes = ioCount;    /* set # of bytes available */
@@ -227,6 +749,9 @@ UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
 {
     /* not enough data left */
     if (reqp->nbytes < 4) {
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl UnmarshallLong reqp->nbytes < 4: %d\r\n",
+                     reqp->nbytes);
        return -1;
     }
 
@@ -243,13 +768,16 @@ MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
     int count;
 
     if (stringp)
-       count = strlen(stringp) + 1;    /* space required including null */
+       count = (int)strlen(stringp) + 1;/* space required including null */
     else
        count = 1;
 
     /* watch for buffer overflow */
-    if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
+    if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) {
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl MarshallString buffer overflow\r\n");
        return -1;
+    }
 
     if (stringp)
        memcpy(reqp->mp, stringp, count);
@@ -273,6 +801,7 @@ fs_GetFullPath(char *pathp, char *outPathp, long outSize)
     int pathHasDrive;
     int doSwitch;
     char newPath[3];
+    char * p;
 
     if (pathp[0] != 0 && pathp[1] == ':') {
        /* there's a drive letter there */
@@ -283,10 +812,33 @@ fs_GetFullPath(char *pathp, char *outPathp, long outSize)
        pathHasDrive = 0;
     }
 
-    if (*firstp == '\\' || *firstp == '/') {
-       /* already an absolute pathname, just copy it back */
-       strcpy(outPathp, firstp);
-       return 0;
+    if ( firstp[0] == '\\' && firstp[1] == '\\' || 
+        firstp[0] == '/' && firstp[1] == '/') {
+        /* UNC path - strip off the server and sharename */
+        int i, count;
+        for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
+            if ( firstp[i] == '\\' || firstp[i] == '/' ) {
+                count++;
+            }
+        }
+        if ( firstp[i] == 0 ) {
+            strcpy(outPathp,"\\");
+        } else {
+            strcpy(outPathp,&firstp[--i]);
+        }
+       for (p=outPathp ;*p; p++) {
+           if (*p == '/')
+               *p = '\\';
+       }
+        return 0;
+    } else if (firstp[0] == '\\' || firstp[0] == '/') {
+        /* already an absolute pathname, just copy it back */
+        strcpy(outPathp, firstp);
+       for (p=outPathp ;*p; p++) {
+           if (*p == '/')
+               *p = '\\';
+       }
+        return 0;
     }
 
     GetCurrentDirectory(sizeof(origPath), origPath);
@@ -303,17 +855,43 @@ fs_GetFullPath(char *pathp, char *outPathp, long outSize)
        newPath[2] = 0;
        if (!SetCurrentDirectory(newPath)) {
            code = GetLastError();
+
+            if ( IoctlDebug() )
+                fprintf(stderr, "pioctl fs_GetFullPath SetCurrentDirectory(%s) failed: 0x%X\r\n",
+                         newPath, code);
            return code;
        }
     }
 
     /* now get the absolute path to the current wdir in this drive */
     GetCurrentDirectory(sizeof(tpath), tpath);
-    strcpy(outPathp, tpath + 2);       /* skip drive letter */
+    if (tpath[1] == ':')
+        strcpy(outPathp, tpath + 2);   /* skip drive letter */
+    else if ( tpath[0] == '\\' && tpath[1] == '\\'||
+             tpath[0] == '/' && tpath[1] == '/') {
+        /* UNC path - strip off the server and sharename */
+        int i, count;
+        for ( i=2,count=2; count < 4 && tpath[i]; i++ ) {
+            if ( tpath[i] == '\\' || tpath[i] == '/' ) {
+                count++;
+            }
+        }
+        if ( tpath[i] == 0 ) {
+            strcpy(outPathp,"\\");
+        } else {
+            strcpy(outPathp,&tpath[--i]);
+        }
+    } else {
+        /* this should never happen */
+        strcpy(outPathp, tpath);
+    }
+
     /* if there is a non-null name after the drive, append it */
     if (*firstp != 0) {
-       strcat(outPathp, "\\");
-       strcat(outPathp, firstp);
+        int len = (int)strlen(outPathp);
+        if (outPathp[len-1] != '\\' && outPathp[len-1] != '/') 
+            strcat(outPathp, "\\");
+        strcat(outPathp, firstp);
     }
 
     /* finally, if necessary, switch back to our home drive letter */
@@ -321,6 +899,10 @@ fs_GetFullPath(char *pathp, char *outPathp, long outSize)
        SetCurrentDirectory(origPath);
     }
 
+    for (p=outPathp ;*p; p++) {
+       if (*p == '/')
+           *p = '\\';
+    }
     return 0;
 }
 
@@ -365,6 +947,10 @@ pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
 
     MarshallString(&preq, fullPath);
     if (blobp->in_size) {
+        if (blobp->in_size > sizeof(preq.data) - (preq.mp - preq.data)*sizeof(char)) {
+            errno = E2BIG;
+            return -1;
+        }
        memcpy(preq.mp, blobp->in, blobp->in_size);
        preq.mp += blobp->in_size;
     }
@@ -377,10 +963,16 @@ pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
     }
 
     /* now unmarshall the return value */
-    UnmarshallLong(&preq, &temp);
+    if (UnmarshallLong(&preq, &temp) != 0) {
+        CloseHandle(reqHandle);
+        return -1;
+    }
+
     if (temp != 0) {
        CloseHandle(reqHandle);
        errno = CMtoUNIXerror(temp);
+        if ( IoctlDebug() )
+            fprintf(stderr, "pioctl temp != 0: 0x%X\r\n",temp);
        return -1;
     }