2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
24 #define SECURITY_WIN32
32 #include <cm_server.h>
36 #include <cm_scache.h>
43 #include <pioctl_nt.h>
44 #include <WINNT/afsreg.h>
45 #include <lanahelper.h>
47 #include <loadfuncs-krb5.h>
50 static char AFSConfigKeyName[] = AFSREG_CLT_SVC_PARAM_SUBKEY;
52 static const char utf8_prefix[] = UTF8_PREFIX;
53 static const int utf8_prefix_size = sizeof(utf8_prefix) - sizeof(char);
55 #define FS_IOCTLREQUEST_MAXSIZE 8192
56 /* big structure for representing and storing an IOCTL request */
57 typedef struct fs_ioctlRequest {
58 char *mp; /* marshalling/unmarshalling ptr */
59 long nbytes; /* bytes received (when unmarshalling) */
60 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
64 CMtoUNIXerror(int cm_code)
67 case CM_ERROR_TIMEDOUT:
69 case CM_ERROR_NOACCESS:
71 case CM_ERROR_NOSUCHFILE:
72 case CM_ERROR_NOSUCHPATH:
73 case CM_ERROR_BPLUS_NOMATCH:
80 case CM_ERROR_INEXACT_MATCH:
82 case CM_ERROR_CROSSDEVLINK:
88 case CM_ERROR_READONLY:
90 case CM_ERROR_WOULDBLOCK:
92 case CM_ERROR_NOSUCHCELL:
93 return ESRCH; /* hack */
94 case CM_ERROR_NOSUCHVOLUME:
95 return EPIPE; /* hack */
96 case CM_ERROR_NOMORETOKENS:
97 return EDOM; /* hack */
98 case CM_ERROR_TOOMANYBUFS:
99 return EFBIG; /* hack */
100 case CM_ERROR_ALLBUSY:
102 case CM_ERROR_ALLDOWN:
103 return ENOSYS; /* hack */
104 case CM_ERROR_ALLOFFLINE:
105 return ENXIO; /* hack */
107 if (cm_code > 0 && cm_code < EILSEQ)
115 InitFSRequest(fs_ioctlRequest_t * rp)
125 static BOOL debug = 0;
130 if (RegOpenKey (HKEY_LOCAL_MACHINE,
131 TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
133 DWORD dwSize = sizeof(BOOL);
134 DWORD dwType = REG_DWORD;
135 RegQueryValueEx (hk, TEXT("IoctlDebug"), NULL, &dwType, (PBYTE)&debug, &dwSize);
146 DisableServiceManagerCheck(void)
149 static BOOL smcheck = 0;
154 if (RegOpenKey (HKEY_LOCAL_MACHINE,
155 TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
157 DWORD dwSize = sizeof(BOOL);
158 DWORD dwType = REG_DWORD;
159 RegQueryValueEx (hk, TEXT("DisableIoctlSMCheck"), NULL, &dwType, (PBYTE)&smcheck, &dwSize);
171 LPSTR lpszMachineName,
172 LPSTR lpszServiceName,
173 DWORD *lpdwCurrentState)
176 SC_HANDLE schSCManager = NULL;
177 SC_HANDLE schService = NULL;
178 DWORD fdwDesiredAccess = 0;
179 SERVICE_STATUS ssServiceStatus = {0};
182 *lpdwCurrentState = 0;
184 fdwDesiredAccess = GENERIC_READ;
186 schSCManager = OpenSCManager(lpszMachineName,
190 if(schSCManager == NULL)
196 schService = OpenService(schSCManager,
200 if(schService == NULL)
206 fRet = QueryServiceStatus(schService,
215 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
219 CloseServiceHandle(schService);
220 CloseServiceHandle(schSCManager);
226 DECL_FUNC_PTR(krb5_cc_default_name);
227 DECL_FUNC_PTR(krb5_cc_set_default_name);
228 DECL_FUNC_PTR(krb5_get_default_config_files);
229 DECL_FUNC_PTR(krb5_free_config_files);
230 DECL_FUNC_PTR(krb5_free_context);
231 DECL_FUNC_PTR(krb5_get_default_realm);
232 DECL_FUNC_PTR(krb5_free_default_realm);
233 DECL_FUNC_PTR(krb5_init_context);
234 DECL_FUNC_PTR(krb5_cc_default);
235 DECL_FUNC_PTR(krb5_parse_name);
236 DECL_FUNC_PTR(krb5_free_principal);
237 DECL_FUNC_PTR(krb5_cc_close);
238 DECL_FUNC_PTR(krb5_cc_get_principal);
239 DECL_FUNC_PTR(krb5_build_principal);
240 DECL_FUNC_PTR(krb5_c_random_make_octets);
241 DECL_FUNC_PTR(krb5_get_init_creds_password);
242 DECL_FUNC_PTR(krb5_free_cred_contents);
243 DECL_FUNC_PTR(krb5_cc_resolve);
244 DECL_FUNC_PTR(krb5_unparse_name);
245 DECL_FUNC_PTR(krb5_free_unparsed_name);
247 FUNC_INFO krb5_fi[] = {
248 MAKE_FUNC_INFO(krb5_cc_default_name),
249 MAKE_FUNC_INFO(krb5_cc_set_default_name),
250 MAKE_FUNC_INFO(krb5_get_default_config_files),
251 MAKE_FUNC_INFO(krb5_free_config_files),
252 MAKE_FUNC_INFO(krb5_free_context),
253 MAKE_FUNC_INFO(krb5_get_default_realm),
254 MAKE_FUNC_INFO(krb5_free_default_realm),
255 MAKE_FUNC_INFO(krb5_init_context),
256 MAKE_FUNC_INFO(krb5_cc_default),
257 MAKE_FUNC_INFO(krb5_parse_name),
258 MAKE_FUNC_INFO(krb5_free_principal),
259 MAKE_FUNC_INFO(krb5_cc_close),
260 MAKE_FUNC_INFO(krb5_cc_get_principal),
261 MAKE_FUNC_INFO(krb5_build_principal),
262 MAKE_FUNC_INFO(krb5_c_random_make_octets),
263 MAKE_FUNC_INFO(krb5_get_init_creds_password),
264 MAKE_FUNC_INFO(krb5_free_cred_contents),
265 MAKE_FUNC_INFO(krb5_cc_resolve),
266 MAKE_FUNC_INFO(krb5_unparse_name),
267 MAKE_FUNC_INFO(krb5_free_unparsed_name),
273 const char* dll_name,
275 HINSTANCE* ph, // [out, optional] - DLL handle
276 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
277 int cleanup, // cleanup function pointers and unload on error
278 int go_on, // continue loading even if some functions cannot be loaded
279 int silent // do not pop-up a system dialog if DLL cannot be loaded
288 if (pindex) *pindex = -1;
290 for (n = 0; fi[n].func_ptr_var; n++)
291 *(fi[n].func_ptr_var) = 0;
294 em = SetErrorMode(SEM_FAILCRITICALERRORS);
295 h = LoadLibrary(dll_name);
303 for (i = 0; (go_on || !error) && (i < n); i++)
305 void* p = (void*)GetProcAddress(h, fi[i].func_name);
311 *(fi[i].func_ptr_var) = p;
314 if (pindex) *pindex = last_i;
315 if (error && cleanup && !go_on) {
316 for (i = 0; i < n; i++) {
317 *(fi[i].func_ptr_var) = 0;
326 #if defined(_IA64_) || defined(_AMD64_)
327 #define KERB5DLL "krb5_64.dll"
329 #define KERB5DLL "krb5_32.dll"
334 static HINSTANCE hKrb5DLL = 0;
339 hKrb5DLL = LoadLibrary(KERB5DLL);
341 if (!LoadFuncs(KERB5DLL, krb5_fi, 0, 0, 1, 0, 0))
343 FreeLibrary(hKrb5DLL);
353 GetLSAPrincipalName(char * szUser, DWORD *dwSize)
355 krb5_context ctx = 0;
356 krb5_error_code code;
357 krb5_ccache mslsa_ccache=0;
358 krb5_principal princ = 0;
362 if (!IsKrb5Available())
365 if (code = pkrb5_init_context(&ctx))
368 if (code = pkrb5_cc_resolve(ctx, "MSLSA:", &mslsa_ccache))
371 if (code = pkrb5_cc_get_principal(ctx, mslsa_ccache, &princ))
374 if (code = pkrb5_unparse_name(ctx, princ, &pname))
377 if ( strlen(pname) < *dwSize ) {
378 strncpy(szUser, pname, *dwSize);
379 szUser[*dwSize-1] = '\0';
382 *dwSize = (DWORD)strlen(pname);
386 pkrb5_free_unparsed_name(ctx, pname);
389 pkrb5_free_principal(ctx, princ);
392 pkrb5_cc_close(ctx, mslsa_ccache);
395 pkrb5_free_context(ctx);
400 // Recursively evaluate drivestr to find the final
401 // dos drive letter to which the source is mapped.
404 DriveSubstitution(char *drivestr, char *subststr, size_t substlen)
406 char device[MAX_PATH];
408 if ( QueryDosDevice(drivestr, device, MAX_PATH) )
410 if ( device[0] == '\\' &&
414 isalpha(device[4]) &&
417 device[0] = device[4];
420 if ( DriveSubstitution(device, subststr, substlen) )
424 subststr[0] = device[0];
430 if ( device[0] == '\\' &&
440 strncpy(&subststr[1], &device[7], substlen-1);
441 subststr[substlen-1] = '\0';
450 // drivestr - is "<drive-letter>:"
453 DriveIsMappedToAFS(char *drivestr, char *NetbiosName)
455 DWORD dwResult, dwResultEnum;
457 DWORD cbBuffer = 16384; // 16K is a good size
458 DWORD cEntries = -1; // enumerate all possible entries
459 LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
462 char subststr[MAX_PATH];
465 // Handle drive letter substitution created with "SUBST <drive> <path>".
466 // If a substitution has occurred, use the target drive letter instead
469 if ( DriveSubstitution(drivestr, subststr, MAX_PATH) )
471 if (subststr[0] == '\\' &&
474 if (_strnicmp( &subststr[2], NetbiosName, strlen(NetbiosName)) == 0)
483 // Call the WNetOpenEnum function to begin the enumeration.
485 dwResult = WNetOpenEnum(RESOURCE_CONNECTED,
488 NULL, // NULL first time the function is called
489 &hEnum); // handle to the resource
491 if (dwResult != NO_ERROR)
495 // Call the GlobalAlloc function to allocate resources.
497 lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
498 if (lpnrLocal == NULL)
503 // Initialize the buffer.
505 ZeroMemory(lpnrLocal, cbBuffer);
507 // Call the WNetEnumResource function to continue
511 dwResultEnum = WNetEnumResource(hEnum, // resource handle
512 &cEntries, // defined locally as -1
513 lpnrLocal, // LPNETRESOURCE
514 &cbBuffer); // buffer size
516 // If the call succeeds, loop through the structures.
518 if (dwResultEnum == NO_ERROR) {
519 for (i = 0; i < cEntries; i++) {
520 if (lpnrLocal[i].lpLocalName &&
521 toupper(lpnrLocal[i].lpLocalName[0]) == toupper(drivestr[0])) {
523 // Skip the two backslashes at the start of the UNC device name
525 if ( _strnicmp( &(lpnrLocal[i].lpRemoteName[2]), NetbiosName, strlen(NetbiosName)) == 0 )
535 else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
538 while (dwResultEnum != ERROR_NO_MORE_ITEMS);
541 // Call the GlobalFree function to free the memory.
543 GlobalFree((HGLOBAL) lpnrLocal);
545 // Call WNetCloseEnum to end the enumeration.
547 dwResult = WNetCloseEnum(hEnum);
553 DriveIsGlobalAutoMapped(char *drivestr)
557 DWORD dwSubMountSize;
558 char szSubMount[260];
561 dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
562 AFSREG_CLT_SVC_PARAM_SUBKEY "\\GlobalAutoMapper",
563 0, KEY_QUERY_VALUE, &hKey);
564 if (dwResult != ERROR_SUCCESS)
567 dwSubMountSize = sizeof(szSubMount);
569 dwResult = RegQueryValueEx(hKey, drivestr, 0, &dwType, szSubMount, &dwSubMountSize);
572 if (dwResult == ERROR_SUCCESS && dwType == REG_SZ)
579 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
582 char netbiosName[MAX_NB_NAME_LENGTH];
583 DWORD CurrentState = 0;
584 char HostName[64] = "";
585 char tbuffer[MAX_PATH]="";
588 char szUser[128] = "";
589 char szClient[MAX_PATH] = "";
590 char szPath[MAX_PATH] = "";
593 DWORD ioctlDebug = IoctlDebug();
595 DWORD dwSize = sizeof(szUser);
598 int sharingViolation;
600 memset(HostName, '\0', sizeof(HostName));
601 gethostname(HostName, sizeof(HostName));
602 if (!DisableServiceManagerCheck() &&
603 GetServiceStatus(HostName, TEXT("TransarcAFSDaemon"), &CurrentState) == NOERROR &&
604 CurrentState != SERVICE_RUNNING)
607 // Populate the Netbios Name
608 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
611 drivep = strchr(fileNamep, ':');
612 if (drivep && (drivep - fileNamep) >= 1) {
613 tbuffer[0] = *(drivep - 1);
617 driveType = GetDriveType(tbuffer);
621 if (DriveIsMappedToAFS(tbuffer, netbiosName) ||
622 DriveIsGlobalAutoMapped(tbuffer))
623 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
628 if (DriveIsGlobalAutoMapped(tbuffer))
629 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
633 } else if (fileNamep[0] == fileNamep[1] &&
634 (fileNamep[0] == '\\' || fileNamep[0] == '/'))
636 int count = 0, i = 0;
638 while (count < 4 && fileNamep[i]) {
639 tbuffer[i] = fileNamep[i];
640 if ( tbuffer[i] == '\\' ||
645 if (fileNamep[i] == 0)
648 strcat(tbuffer, SMB_IOCTL_FILENAME);
650 char curdir[MAX_PATH]="";
652 GetCurrentDirectory(sizeof(curdir), curdir);
653 if ( curdir[1] == ':' ) {
654 tbuffer[0] = curdir[0];
658 driveType = GetDriveType(tbuffer);
662 if (DriveIsMappedToAFS(tbuffer, netbiosName) ||
663 DriveIsGlobalAutoMapped(tbuffer))
664 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
669 if (DriveIsGlobalAutoMapped(tbuffer))
670 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
674 } else if (curdir[0] == curdir[1] &&
675 (curdir[0] == '\\' || curdir[0] == '/'))
677 int count = 0, i = 0;
679 while (count < 4 && curdir[i]) {
680 tbuffer[i] = curdir[i];
681 if ( tbuffer[i] == '\\' ||
689 strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
694 /* No file name starting with drive colon specified, use UNC name */
695 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
699 /* now open the file */
700 sharingViolation = 0;
702 if (sharingViolation)
704 fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
705 FILE_SHARE_READ, NULL, OPEN_EXISTING,
706 FILE_FLAG_WRITE_THROUGH, NULL);
708 } while (fh == INVALID_HANDLE_VALUE &&
709 GetLastError() == ERROR_SHARING_VIOLATION &&
710 sharingViolation < 100);
713 if (fh == INVALID_HANDLE_VALUE) {
714 gle = GetLastError();
715 if (gle && ioctlDebug ) {
719 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
722 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
728 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
736 if (fh == INVALID_HANDLE_VALUE &&
737 GetLastError() != ERROR_SHARING_VIOLATION) {
740 lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL);
742 if (RegOpenKey (HKEY_CURRENT_USER,
743 TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
745 DWORD dwType = REG_SZ;
746 RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize);
753 fprintf(stderr, "pioctl Explorer logon user: [%s]\r\n",szUser);
756 sprintf(szPath, "\\\\%s", szClient);
757 memset (&nr, 0x00, sizeof(NETRESOURCE));
758 nr.dwType=RESOURCETYPE_DISK;
760 nr.lpRemoteName=szPath;
761 res = WNetAddConnection2(&nr,NULL,szUser,0);
765 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
772 sprintf(szPath, "\\\\%s\\all", szClient);
773 res = WNetAddConnection2(&nr,NULL,szUser,0);
777 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
785 goto try_lsa_principal;
787 sharingViolation = 0;
789 if (sharingViolation)
791 fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
792 FILE_SHARE_READ, NULL, OPEN_EXISTING,
793 FILE_FLAG_WRITE_THROUGH, NULL);
795 } while (fh == INVALID_HANDLE_VALUE &&
796 GetLastError() == ERROR_SHARING_VIOLATION &&
797 sharingViolation < 100);
799 if (fh == INVALID_HANDLE_VALUE) {
800 gle = GetLastError();
801 if (gle && ioctlDebug ) {
805 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
808 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
814 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
825 if (fh == INVALID_HANDLE_VALUE &&
826 GetLastError() != ERROR_SHARING_VIOLATION) {
829 dwSize = sizeof(szUser);
830 if (GetLSAPrincipalName(szUser, &dwSize)) {
833 fprintf(stderr, "pioctl LSA Principal logon user: [%s]\r\n",szUser);
836 sprintf(szPath, "\\\\%s", szClient);
837 memset (&nr, 0x00, sizeof(NETRESOURCE));
838 nr.dwType=RESOURCETYPE_DISK;
840 nr.lpRemoteName=szPath;
841 res = WNetAddConnection2(&nr,NULL,szUser,0);
845 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
852 sprintf(szPath, "\\\\%s\\all", szClient);
853 res = WNetAddConnection2(&nr,NULL,szUser,0);
857 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
867 sharingViolation = 0;
869 if (sharingViolation)
871 fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
872 FILE_SHARE_READ, NULL, OPEN_EXISTING,
873 FILE_FLAG_WRITE_THROUGH, NULL);
875 } while (fh == INVALID_HANDLE_VALUE &&
876 GetLastError() == ERROR_SHARING_VIOLATION &&
877 sharingViolation < 100);
879 if (fh == INVALID_HANDLE_VALUE) {
880 gle = GetLastError();
881 if (gle && ioctlDebug ) {
885 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
888 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
894 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
905 if (fh == INVALID_HANDLE_VALUE &&
906 GetLastError() != ERROR_SHARING_VIOLATION) {
907 dwSize = sizeof(szUser);
908 if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
911 fprintf(stderr, "pioctl SamCompatible logon user: [%s]\r\n",szUser);
914 sprintf(szPath, "\\\\%s", szClient);
915 memset (&nr, 0x00, sizeof(NETRESOURCE));
916 nr.dwType=RESOURCETYPE_DISK;
918 nr.lpRemoteName=szPath;
919 res = WNetAddConnection2(&nr,NULL,szUser,0);
923 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
929 sprintf(szPath, "\\\\%s\\all", szClient);
930 res = WNetAddConnection2(&nr,NULL,szUser,0);
934 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
941 sharingViolation = 0;
943 if (sharingViolation)
945 fh = CreateFile(tbuffer, FILE_READ_DATA | FILE_WRITE_DATA,
946 FILE_SHARE_READ, NULL, OPEN_EXISTING,
947 FILE_FLAG_WRITE_THROUGH, NULL);
949 } while (fh == INVALID_HANDLE_VALUE &&
950 GetLastError() == ERROR_SHARING_VIOLATION &&
951 sharingViolation < 100);
953 if (fh == INVALID_HANDLE_VALUE) {
954 gle = GetLastError();
955 if (gle && ioctlDebug ) {
959 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
962 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
968 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
976 fprintf(stderr, "GetUserNameEx(NameSamCompatible) failed: 0x%X\r\n", GetLastError());
981 if (fh == INVALID_HANDLE_VALUE)
984 /* return fh and success code */
990 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
995 DWORD ioctlDebug = IoctlDebug();
998 rcount = (long)(reqp->mp - reqp->data);
1002 fprintf(stderr, "pioctl Transceive rcount <= 0: %d\r\n",rcount);
1005 return EINVAL; /* not supposed to happen */
1008 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
1009 /* failed to write */
1010 gle = GetLastError();
1014 fprintf(stderr, "pioctl Transceive WriteFile failed: 0x%X\r\n",gle);
1020 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
1021 /* failed to read */
1022 gle = GetLastError();
1026 fprintf(stderr, "pioctl Transceive ReadFile failed: 0x%X\r\n",gle);
1032 reqp->nbytes = ioCount; /* set # of bytes available */
1033 reqp->mp = reqp->data; /* restart marshalling */
1035 /* return success */
1040 MarshallLong(fs_ioctlRequest_t * reqp, long val)
1042 memcpy(reqp->mp, &val, 4);
1048 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
1052 /* not enough data left */
1053 if (reqp->nbytes < 4) {
1054 if ( IoctlDebug() ) {
1056 fprintf(stderr, "pioctl UnmarshallLong reqp->nbytes < 4: %d\r\n",
1063 memcpy(valp, reqp->mp, 4);
1069 /* includes marshalling NULL pointer as a null (0 length) string */
1071 MarshallString(fs_ioctlRequest_t * reqp, char *stringp, int is_utf8)
1077 count = (int)strlen(stringp) + 1;/* space required including null */
1082 count += utf8_prefix_size;
1085 /* watch for buffer overflow */
1086 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) {
1087 if ( IoctlDebug() ) {
1089 fprintf(stderr, "pioctl MarshallString buffer overflow\r\n");
1096 memcpy(reqp->mp, utf8_prefix, utf8_prefix_size);
1097 reqp->mp += utf8_prefix_size;
1098 count -= utf8_prefix_size;
1102 memcpy(reqp->mp, stringp, count);
1109 /* take a path with a drive letter, possibly relative, and return a full path
1110 * without the drive letter. This is the full path relative to the working
1111 * dir for that drive letter. The input and output paths can be the same.
1114 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
1117 char origPath[1000];
1126 if (pathp[0] != 0 && pathp[1] == ':') {
1127 /* there's a drive letter there */
1135 if ( firstp[0] == '\\' && firstp[1] == '\\' ||
1136 firstp[0] == '/' && firstp[1] == '/') {
1137 /* UNC path - strip off the server and sharename */
1139 for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
1140 if ( firstp[i] == '\\' || firstp[i] == '/' ) {
1144 if ( firstp[i] == 0 ) {
1145 strcpy(outPathp,"\\");
1147 strcpy(outPathp,&firstp[--i]);
1149 for (p=outPathp ;*p; p++) {
1154 } else if (firstp[0] == '\\' || firstp[0] == '/') {
1155 /* already an absolute pathname, just copy it back */
1156 strcpy(outPathp, firstp);
1157 for (p=outPathp ;*p; p++) {
1164 GetCurrentDirectory(sizeof(origPath), origPath);
1167 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
1168 /* a drive has been specified and it isn't our current drive.
1169 * to get path, switch to it first. Must case-fold drive letters
1170 * for user convenience.
1173 newPath[0] = *pathp;
1176 if (!SetCurrentDirectory(newPath)) {
1177 code = GetLastError();
1179 if ( IoctlDebug() ) {
1181 fprintf(stderr, "pioctl fs_GetFullPath SetCurrentDirectory(%s) failed: 0x%X\r\n",
1189 /* now get the absolute path to the current wdir in this drive */
1190 GetCurrentDirectory(sizeof(tpath), tpath);
1191 if (tpath[1] == ':')
1192 strcpy(outPathp, tpath + 2); /* skip drive letter */
1193 else if ( tpath[0] == '\\' && tpath[1] == '\\'||
1194 tpath[0] == '/' && tpath[1] == '/') {
1195 /* UNC path - strip off the server and sharename */
1197 for ( i=2,count=2; count < 4 && tpath[i]; i++ ) {
1198 if ( tpath[i] == '\\' || tpath[i] == '/' ) {
1202 if ( tpath[i] == 0 ) {
1203 strcpy(outPathp,"\\");
1205 strcpy(outPathp,&tpath[--i]);
1208 /* this should never happen */
1209 strcpy(outPathp, tpath);
1212 /* if there is a non-null name after the drive, append it */
1214 int len = (int)strlen(outPathp);
1215 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
1216 strcat(outPathp, "\\");
1217 strcat(outPathp, firstp);
1220 /* finally, if necessary, switch back to our home drive letter */
1222 SetCurrentDirectory(origPath);
1225 for (p=outPathp ;*p; p++) {
1233 pioctl_int(char *pathp, afs_int32 opcode, struct ViceIoctl *blobp, afs_int32 follow, afs_int32 is_utf8)
1235 fs_ioctlRequest_t preq;
1238 char fullPath[1000];
1242 code = GetIoctlHandle(pathp, &reqHandle);
1251 /* init the request structure */
1252 InitFSRequest(&preq);
1254 /* marshall the opcode, the path name and the input parameters */
1255 MarshallLong(&preq, opcode);
1256 /* when marshalling the path, remove the drive letter, since we already
1257 * used the drive letter to find the AFS daemon; we don't need it any more.
1258 * Eventually we'll expand relative path names here, too, since again, only
1259 * we understand those.
1262 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
1264 CloseHandle(reqHandle);
1269 strcpy(fullPath, "");
1272 MarshallString(&preq, fullPath, is_utf8);
1273 if (blobp->in_size) {
1274 if (blobp->in_size > sizeof(preq.data) - (preq.mp - preq.data)*sizeof(char)) {
1278 memcpy(preq.mp, blobp->in, blobp->in_size);
1279 preq.mp += blobp->in_size;
1282 /* now make the call */
1283 code = Transceive(reqHandle, &preq);
1285 CloseHandle(reqHandle);
1289 /* now unmarshall the return value */
1290 if (UnmarshallLong(&preq, &temp) != 0) {
1291 CloseHandle(reqHandle);
1296 CloseHandle(reqHandle);
1297 errno = CMtoUNIXerror(temp);
1298 if ( IoctlDebug() ) {
1300 fprintf(stderr, "pioctl temp != 0: 0x%X\r\n",temp);
1306 /* otherwise, unmarshall the output parameters */
1307 if (blobp->out_size) {
1308 temp = blobp->out_size;
1309 if (preq.nbytes < temp)
1311 memcpy(blobp->out, preq.mp, temp);
1312 blobp->out_size = temp;
1315 /* and return success */
1316 CloseHandle(reqHandle);
1321 pioctl_utf8(char * pathp, afs_int32 opcode, struct ViceIoctl * blobp, afs_int32 follow)
1323 return pioctl_int(pathp, opcode, blobp, follow, TRUE);
1327 pioctl(char * pathp, afs_int32 opcode, struct ViceIoctl * blobp, afs_int32 follow)
1329 return pioctl_int(pathp, opcode, blobp, follow, FALSE);