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>
23 #define SECURITY_WIN32
31 #include <cm_server.h>
35 #include <cm_scache.h>
42 #include <pioctl_nt.h>
43 #include <WINNT/afsreg.h>
44 #include <lanahelper.h>
46 #include <loadfuncs-krb5.h>
49 static char AFSConfigKeyName[] = AFSREG_CLT_SVC_PARAM_SUBKEY;
51 static const char utf8_prefix[] = UTF8_PREFIX;
52 static const int utf8_prefix_size = sizeof(utf8_prefix) - sizeof(char);
54 #define FS_IOCTLREQUEST_MAXSIZE 8192
55 /* big structure for representing and storing an IOCTL request */
56 typedef struct fs_ioctlRequest {
57 char *mp; /* marshalling/unmarshalling ptr */
58 long nbytes; /* bytes received (when unmarshalling) */
59 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
63 CMtoUNIXerror(int cm_code)
66 case CM_ERROR_TIMEDOUT:
68 case CM_ERROR_NOACCESS:
70 case CM_ERROR_NOSUCHFILE:
71 case CM_ERROR_NOSUCHPATH:
72 case CM_ERROR_BPLUS_NOMATCH:
79 case CM_ERROR_INEXACT_MATCH:
81 case CM_ERROR_CROSSDEVLINK:
87 case CM_ERROR_READONLY:
89 case CM_ERROR_WOULDBLOCK:
91 case CM_ERROR_NOSUCHCELL:
92 return ESRCH; /* hack */
93 case CM_ERROR_NOSUCHVOLUME:
94 return EPIPE; /* hack */
95 case CM_ERROR_NOMORETOKENS:
96 return EDOM; /* hack */
97 case CM_ERROR_TOOMANYBUFS:
98 return EFBIG; /* hack */
99 case CM_ERROR_ALLBUSY:
101 case CM_ERROR_ALLDOWN:
102 return ENOSYS; /* hack */
103 case CM_ERROR_ALLOFFLINE:
104 return ENXIO; /* hack */
106 if (cm_code > 0 && cm_code < EILSEQ)
114 InitFSRequest(fs_ioctlRequest_t * rp)
124 static BOOL debug = 0;
129 if (RegOpenKey (HKEY_LOCAL_MACHINE,
130 TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
132 DWORD dwSize = sizeof(BOOL);
133 DWORD dwType = REG_DWORD;
134 RegQueryValueEx (hk, TEXT("IoctlDebug"), NULL, &dwType, (PBYTE)&debug, &dwSize);
145 DisableServiceManagerCheck(void)
148 static BOOL smcheck = 0;
153 if (RegOpenKey (HKEY_LOCAL_MACHINE,
154 TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
156 DWORD dwSize = sizeof(BOOL);
157 DWORD dwType = REG_DWORD;
158 RegQueryValueEx (hk, TEXT("DisableIoctlSMCheck"), NULL, &dwType, (PBYTE)&smcheck, &dwSize);
170 LPSTR lpszMachineName,
171 LPSTR lpszServiceName,
172 DWORD *lpdwCurrentState)
175 SC_HANDLE schSCManager = NULL;
176 SC_HANDLE schService = NULL;
177 DWORD fdwDesiredAccess = 0;
178 SERVICE_STATUS ssServiceStatus = {0};
181 *lpdwCurrentState = 0;
183 fdwDesiredAccess = GENERIC_READ;
185 schSCManager = OpenSCManager(lpszMachineName,
189 if(schSCManager == NULL)
195 schService = OpenService(schSCManager,
199 if(schService == NULL)
205 fRet = QueryServiceStatus(schService,
214 *lpdwCurrentState = ssServiceStatus.dwCurrentState;
218 CloseServiceHandle(schService);
219 CloseServiceHandle(schSCManager);
225 DECL_FUNC_PTR(krb5_cc_default_name);
226 DECL_FUNC_PTR(krb5_cc_set_default_name);
227 DECL_FUNC_PTR(krb5_get_default_config_files);
228 DECL_FUNC_PTR(krb5_free_config_files);
229 DECL_FUNC_PTR(krb5_free_context);
230 DECL_FUNC_PTR(krb5_get_default_realm);
231 DECL_FUNC_PTR(krb5_free_default_realm);
232 DECL_FUNC_PTR(krb5_init_context);
233 DECL_FUNC_PTR(krb5_cc_default);
234 DECL_FUNC_PTR(krb5_parse_name);
235 DECL_FUNC_PTR(krb5_free_principal);
236 DECL_FUNC_PTR(krb5_cc_close);
237 DECL_FUNC_PTR(krb5_cc_get_principal);
238 DECL_FUNC_PTR(krb5_build_principal);
239 DECL_FUNC_PTR(krb5_c_random_make_octets);
240 DECL_FUNC_PTR(krb5_get_init_creds_password);
241 DECL_FUNC_PTR(krb5_free_cred_contents);
242 DECL_FUNC_PTR(krb5_cc_resolve);
243 DECL_FUNC_PTR(krb5_unparse_name);
244 DECL_FUNC_PTR(krb5_free_unparsed_name);
246 FUNC_INFO krb5_fi[] = {
247 MAKE_FUNC_INFO(krb5_cc_default_name),
248 MAKE_FUNC_INFO(krb5_cc_set_default_name),
249 MAKE_FUNC_INFO(krb5_get_default_config_files),
250 MAKE_FUNC_INFO(krb5_free_config_files),
251 MAKE_FUNC_INFO(krb5_free_context),
252 MAKE_FUNC_INFO(krb5_get_default_realm),
253 MAKE_FUNC_INFO(krb5_free_default_realm),
254 MAKE_FUNC_INFO(krb5_init_context),
255 MAKE_FUNC_INFO(krb5_cc_default),
256 MAKE_FUNC_INFO(krb5_parse_name),
257 MAKE_FUNC_INFO(krb5_free_principal),
258 MAKE_FUNC_INFO(krb5_cc_close),
259 MAKE_FUNC_INFO(krb5_cc_get_principal),
260 MAKE_FUNC_INFO(krb5_build_principal),
261 MAKE_FUNC_INFO(krb5_c_random_make_octets),
262 MAKE_FUNC_INFO(krb5_get_init_creds_password),
263 MAKE_FUNC_INFO(krb5_free_cred_contents),
264 MAKE_FUNC_INFO(krb5_cc_resolve),
265 MAKE_FUNC_INFO(krb5_unparse_name),
266 MAKE_FUNC_INFO(krb5_free_unparsed_name),
272 const char* dll_name,
274 HINSTANCE* ph, // [out, optional] - DLL handle
275 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
276 int cleanup, // cleanup function pointers and unload on error
277 int go_on, // continue loading even if some functions cannot be loaded
278 int silent // do not pop-up a system dialog if DLL cannot be loaded
287 if (pindex) *pindex = -1;
289 for (n = 0; fi[n].func_ptr_var; n++)
290 *(fi[n].func_ptr_var) = 0;
293 em = SetErrorMode(SEM_FAILCRITICALERRORS);
294 h = LoadLibrary(dll_name);
302 for (i = 0; (go_on || !error) && (i < n); i++)
304 void* p = (void*)GetProcAddress(h, fi[i].func_name);
310 *(fi[i].func_ptr_var) = p;
313 if (pindex) *pindex = last_i;
314 if (error && cleanup && !go_on) {
315 for (i = 0; i < n; i++) {
316 *(fi[i].func_ptr_var) = 0;
325 #if defined(_IA64_) || defined(_AMD64_)
326 #define KERB5DLL "krb5_64.dll"
328 #define KERB5DLL "krb5_32.dll"
333 static HINSTANCE hKrb5DLL = 0;
338 hKrb5DLL = LoadLibrary(KERB5DLL);
340 if (!LoadFuncs(KERB5DLL, krb5_fi, 0, 0, 1, 0, 0))
342 FreeLibrary(hKrb5DLL);
352 GetLSAPrincipalName(char * szUser, DWORD *dwSize)
354 krb5_context ctx = 0;
355 krb5_error_code code;
356 krb5_ccache mslsa_ccache=0;
357 krb5_principal princ = 0;
361 if (!IsKrb5Available())
364 if (code = pkrb5_init_context(&ctx))
367 if (code = pkrb5_cc_resolve(ctx, "MSLSA:", &mslsa_ccache))
370 if (code = pkrb5_cc_get_principal(ctx, mslsa_ccache, &princ))
373 if (code = pkrb5_unparse_name(ctx, princ, &pname))
376 if ( strlen(pname) < *dwSize ) {
377 strncpy(szUser, pname, *dwSize);
378 szUser[*dwSize-1] = '\0';
381 *dwSize = (DWORD)strlen(pname);
385 pkrb5_free_unparsed_name(ctx, pname);
388 pkrb5_free_principal(ctx, princ);
391 pkrb5_cc_close(ctx, mslsa_ccache);
394 pkrb5_free_context(ctx);
399 // Recursively evaluate drivestr to find the final
400 // dos drive letter to which the source is mapped.
403 DriveSubstitution(char *drivestr, char *subststr, size_t substlen)
405 char device[MAX_PATH];
407 if ( QueryDosDevice(drivestr, device, MAX_PATH) )
409 if ( device[0] == '\\' &&
413 isalpha(device[4]) &&
416 device[0] = device[4];
419 if ( DriveSubstitution(device, subststr, substlen) )
423 subststr[0] = device[0];
429 if ( device[0] == '\\' &&
439 strncpy(&subststr[1], &device[7], substlen-1);
440 subststr[substlen-1] = '\0';
449 // drivestr - is "<drive-letter>:"
452 DriveIsMappedToAFS(char *drivestr, char *NetbiosName)
454 DWORD dwResult, dwResultEnum;
456 DWORD cbBuffer = 16384; // 16K is a good size
457 DWORD cEntries = -1; // enumerate all possible entries
458 LPNETRESOURCE lpnrLocal; // pointer to enumerated structures
461 char subststr[MAX_PATH];
464 // Handle drive letter substitution created with "SUBST <drive> <path>".
465 // If a substitution has occurred, use the target drive letter instead
468 if ( DriveSubstitution(drivestr, subststr, MAX_PATH) )
470 if (subststr[0] == '\\' &&
473 if (_strnicmp( &subststr[2], NetbiosName, strlen(NetbiosName)) == 0)
482 // Call the WNetOpenEnum function to begin the enumeration.
484 dwResult = WNetOpenEnum(RESOURCE_CONNECTED,
487 NULL, // NULL first time the function is called
488 &hEnum); // handle to the resource
490 if (dwResult != NO_ERROR)
494 // Call the GlobalAlloc function to allocate resources.
496 lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
497 if (lpnrLocal == NULL)
502 // Initialize the buffer.
504 ZeroMemory(lpnrLocal, cbBuffer);
506 // Call the WNetEnumResource function to continue
510 dwResultEnum = WNetEnumResource(hEnum, // resource handle
511 &cEntries, // defined locally as -1
512 lpnrLocal, // LPNETRESOURCE
513 &cbBuffer); // buffer size
515 // If the call succeeds, loop through the structures.
517 if (dwResultEnum == NO_ERROR) {
518 for (i = 0; i < cEntries; i++) {
519 if (lpnrLocal[i].lpLocalName &&
520 toupper(lpnrLocal[i].lpLocalName[0]) == toupper(drivestr[0])) {
522 // Skip the two backslashes at the start of the UNC device name
524 if ( _strnicmp( &(lpnrLocal[i].lpRemoteName[2]), NetbiosName, strlen(NetbiosName)) == 0 )
534 else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
537 while (dwResultEnum != ERROR_NO_MORE_ITEMS);
540 // Call the GlobalFree function to free the memory.
542 GlobalFree((HGLOBAL) lpnrLocal);
544 // Call WNetCloseEnum to end the enumeration.
546 dwResult = WNetCloseEnum(hEnum);
552 DriveIsGlobalAutoMapped(char *drivestr)
556 DWORD dwSubMountSize;
557 char szSubMount[260];
560 dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
561 AFSREG_CLT_SVC_PARAM_SUBKEY "\\GlobalAutoMapper",
562 0, KEY_QUERY_VALUE, &hKey);
563 if (dwResult != ERROR_SUCCESS)
566 dwSubMountSize = sizeof(szSubMount);
568 dwResult = RegQueryValueEx(hKey, drivestr, 0, &dwType, szSubMount, &dwSubMountSize);
571 if (dwResult == ERROR_SUCCESS && dwType == REG_SZ)
578 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
581 char netbiosName[MAX_NB_NAME_LENGTH];
582 DWORD CurrentState = 0;
583 char HostName[64] = "";
584 char tbuffer[MAX_PATH]="";
587 char szUser[128] = "";
588 char szClient[MAX_PATH] = "";
589 char szPath[MAX_PATH] = "";
592 DWORD ioctlDebug = IoctlDebug();
594 DWORD dwSize = sizeof(szUser);
598 memset(HostName, '\0', sizeof(HostName));
599 gethostname(HostName, sizeof(HostName));
600 if (!DisableServiceManagerCheck() &&
601 GetServiceStatus(HostName, TEXT("TransarcAFSDaemon"), &CurrentState) == NOERROR &&
602 CurrentState != SERVICE_RUNNING)
605 // Populate the Netbios Name
606 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
609 drivep = strchr(fileNamep, ':');
610 if (drivep && (drivep - fileNamep) >= 1) {
611 tbuffer[0] = *(drivep - 1);
615 driveType = GetDriveType(tbuffer);
619 if (DriveIsMappedToAFS(tbuffer, netbiosName) ||
620 DriveIsGlobalAutoMapped(tbuffer))
621 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
626 if (DriveIsGlobalAutoMapped(tbuffer))
627 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
631 } else if (fileNamep[0] == fileNamep[1] &&
632 (fileNamep[0] == '\\' || fileNamep[0] == '/'))
634 int count = 0, i = 0;
636 while (count < 4 && fileNamep[i]) {
637 tbuffer[i] = fileNamep[i];
638 if ( tbuffer[i] == '\\' ||
643 if (fileNamep[i] == 0)
646 strcat(tbuffer, SMB_IOCTL_FILENAME);
648 char curdir[MAX_PATH]="";
650 GetCurrentDirectory(sizeof(curdir), curdir);
651 if ( curdir[1] == ':' ) {
652 tbuffer[0] = curdir[0];
656 driveType = GetDriveType(tbuffer);
660 if (DriveIsMappedToAFS(tbuffer, netbiosName) ||
661 DriveIsGlobalAutoMapped(tbuffer))
662 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
667 if (DriveIsGlobalAutoMapped(tbuffer))
668 strcpy(&tbuffer[2], SMB_IOCTL_FILENAME);
672 } else if (curdir[0] == curdir[1] &&
673 (curdir[0] == '\\' || curdir[0] == '/'))
675 int count = 0, i = 0;
677 while (count < 4 && curdir[i]) {
678 tbuffer[i] = curdir[i];
679 if ( tbuffer[i] == '\\' ||
687 strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
692 /* No file name starting with drive colon specified, use UNC name */
693 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
697 /* now open the file */
698 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
699 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
700 FILE_FLAG_WRITE_THROUGH, NULL);
704 if (fh == INVALID_HANDLE_VALUE) {
707 gle = GetLastError();
708 if (gle && ioctlDebug ) {
712 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
715 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
721 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
727 lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL);
729 if (RegOpenKey (HKEY_CURRENT_USER,
730 TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
732 DWORD dwType = REG_SZ;
733 RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize);
740 fprintf(stderr, "pioctl Explorer logon user: [%s]\r\n",szUser);
743 sprintf(szPath, "\\\\%s", szClient);
744 memset (&nr, 0x00, sizeof(NETRESOURCE));
745 nr.dwType=RESOURCETYPE_DISK;
747 nr.lpRemoteName=szPath;
748 res = WNetAddConnection2(&nr,NULL,szUser,0);
752 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
759 sprintf(szPath, "\\\\%s\\all", szClient);
760 res = WNetAddConnection2(&nr,NULL,szUser,0);
764 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
772 goto try_lsa_principal;
774 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
775 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
776 FILE_FLAG_WRITE_THROUGH, NULL);
778 if (fh == INVALID_HANDLE_VALUE) {
779 gle = GetLastError();
780 if (gle && ioctlDebug ) {
784 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
787 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
793 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
803 if (fh == INVALID_HANDLE_VALUE) {
806 dwSize = sizeof(szUser);
807 if (GetLSAPrincipalName(szUser, &dwSize)) {
810 fprintf(stderr, "pioctl LSA Principal logon user: [%s]\r\n",szUser);
813 sprintf(szPath, "\\\\%s", szClient);
814 memset (&nr, 0x00, sizeof(NETRESOURCE));
815 nr.dwType=RESOURCETYPE_DISK;
817 nr.lpRemoteName=szPath;
818 res = WNetAddConnection2(&nr,NULL,szUser,0);
822 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
829 sprintf(szPath, "\\\\%s\\all", szClient);
830 res = WNetAddConnection2(&nr,NULL,szUser,0);
834 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
844 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
845 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
846 FILE_FLAG_WRITE_THROUGH, NULL);
848 if (fh == INVALID_HANDLE_VALUE) {
849 gle = GetLastError();
850 if (gle && ioctlDebug ) {
854 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
857 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
863 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
874 if ( fh == INVALID_HANDLE_VALUE ) {
875 dwSize = sizeof(szUser);
876 if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
879 fprintf(stderr, "pioctl SamCompatible logon user: [%s]\r\n",szUser);
882 sprintf(szPath, "\\\\%s", szClient);
883 memset (&nr, 0x00, sizeof(NETRESOURCE));
884 nr.dwType=RESOURCETYPE_DISK;
886 nr.lpRemoteName=szPath;
887 res = WNetAddConnection2(&nr,NULL,szUser,0);
891 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
897 sprintf(szPath, "\\\\%s\\all", szClient);
898 res = WNetAddConnection2(&nr,NULL,szUser,0);
902 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
909 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
910 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
911 FILE_FLAG_WRITE_THROUGH, NULL);
913 if (fh == INVALID_HANDLE_VALUE) {
914 gle = GetLastError();
915 if (gle && ioctlDebug ) {
919 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
922 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
928 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
936 fprintf(stderr, "GetUserNameEx(NameSamCompatible) failed: 0x%X\r\n", GetLastError());
941 /* return fh and success code */
947 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
952 DWORD ioctlDebug = IoctlDebug();
955 rcount = (long)(reqp->mp - reqp->data);
959 fprintf(stderr, "pioctl Transceive rcount <= 0: %d\r\n",rcount);
962 return EINVAL; /* not supposed to happen */
965 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
966 /* failed to write */
967 gle = GetLastError();
971 fprintf(stderr, "pioctl Transceive WriteFile failed: 0x%X\r\n",gle);
977 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
979 gle = GetLastError();
983 fprintf(stderr, "pioctl Transceive ReadFile failed: 0x%X\r\n",gle);
989 reqp->nbytes = ioCount; /* set # of bytes available */
990 reqp->mp = reqp->data; /* restart marshalling */
997 MarshallLong(fs_ioctlRequest_t * reqp, long val)
999 memcpy(reqp->mp, &val, 4);
1005 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
1009 /* not enough data left */
1010 if (reqp->nbytes < 4) {
1011 if ( IoctlDebug() ) {
1013 fprintf(stderr, "pioctl UnmarshallLong reqp->nbytes < 4: %d\r\n",
1020 memcpy(valp, reqp->mp, 4);
1026 /* includes marshalling NULL pointer as a null (0 length) string */
1028 MarshallString(fs_ioctlRequest_t * reqp, char *stringp, int is_utf8)
1034 count = (int)strlen(stringp) + 1;/* space required including null */
1039 count += utf8_prefix_size;
1042 /* watch for buffer overflow */
1043 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) {
1044 if ( IoctlDebug() ) {
1046 fprintf(stderr, "pioctl MarshallString buffer overflow\r\n");
1053 memcpy(reqp->mp, utf8_prefix, utf8_prefix_size);
1054 reqp->mp += utf8_prefix_size;
1055 count -= utf8_prefix_size;
1059 memcpy(reqp->mp, stringp, count);
1066 /* take a path with a drive letter, possibly relative, and return a full path
1067 * without the drive letter. This is the full path relative to the working
1068 * dir for that drive letter. The input and output paths can be the same.
1071 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
1074 char origPath[1000];
1083 if (pathp[0] != 0 && pathp[1] == ':') {
1084 /* there's a drive letter there */
1092 if ( firstp[0] == '\\' && firstp[1] == '\\' ||
1093 firstp[0] == '/' && firstp[1] == '/') {
1094 /* UNC path - strip off the server and sharename */
1096 for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
1097 if ( firstp[i] == '\\' || firstp[i] == '/' ) {
1101 if ( firstp[i] == 0 ) {
1102 strcpy(outPathp,"\\");
1104 strcpy(outPathp,&firstp[--i]);
1106 for (p=outPathp ;*p; p++) {
1111 } else if (firstp[0] == '\\' || firstp[0] == '/') {
1112 /* already an absolute pathname, just copy it back */
1113 strcpy(outPathp, firstp);
1114 for (p=outPathp ;*p; p++) {
1121 GetCurrentDirectory(sizeof(origPath), origPath);
1124 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
1125 /* a drive has been specified and it isn't our current drive.
1126 * to get path, switch to it first. Must case-fold drive letters
1127 * for user convenience.
1130 newPath[0] = *pathp;
1133 if (!SetCurrentDirectory(newPath)) {
1134 code = GetLastError();
1136 if ( IoctlDebug() ) {
1138 fprintf(stderr, "pioctl fs_GetFullPath SetCurrentDirectory(%s) failed: 0x%X\r\n",
1146 /* now get the absolute path to the current wdir in this drive */
1147 GetCurrentDirectory(sizeof(tpath), tpath);
1148 if (tpath[1] == ':')
1149 strcpy(outPathp, tpath + 2); /* skip drive letter */
1150 else if ( tpath[0] == '\\' && tpath[1] == '\\'||
1151 tpath[0] == '/' && tpath[1] == '/') {
1152 /* UNC path - strip off the server and sharename */
1154 for ( i=2,count=2; count < 4 && tpath[i]; i++ ) {
1155 if ( tpath[i] == '\\' || tpath[i] == '/' ) {
1159 if ( tpath[i] == 0 ) {
1160 strcpy(outPathp,"\\");
1162 strcpy(outPathp,&tpath[--i]);
1165 /* this should never happen */
1166 strcpy(outPathp, tpath);
1169 /* if there is a non-null name after the drive, append it */
1171 int len = (int)strlen(outPathp);
1172 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
1173 strcat(outPathp, "\\");
1174 strcat(outPathp, firstp);
1177 /* finally, if necessary, switch back to our home drive letter */
1179 SetCurrentDirectory(origPath);
1182 for (p=outPathp ;*p; p++) {
1190 pioctl_int(char *pathp, long opcode, struct ViceIoctl *blobp, int follow, int is_utf8)
1192 fs_ioctlRequest_t preq;
1195 char fullPath[1000];
1199 code = GetIoctlHandle(pathp, &reqHandle);
1208 /* init the request structure */
1209 InitFSRequest(&preq);
1211 /* marshall the opcode, the path name and the input parameters */
1212 MarshallLong(&preq, opcode);
1213 /* when marshalling the path, remove the drive letter, since we already
1214 * used the drive letter to find the AFS daemon; we don't need it any more.
1215 * Eventually we'll expand relative path names here, too, since again, only
1216 * we understand those.
1219 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
1221 CloseHandle(reqHandle);
1226 strcpy(fullPath, "");
1229 MarshallString(&preq, fullPath, is_utf8);
1230 if (blobp->in_size) {
1231 if (blobp->in_size > sizeof(preq.data) - (preq.mp - preq.data)*sizeof(char)) {
1235 memcpy(preq.mp, blobp->in, blobp->in_size);
1236 preq.mp += blobp->in_size;
1239 /* now make the call */
1240 code = Transceive(reqHandle, &preq);
1242 CloseHandle(reqHandle);
1246 /* now unmarshall the return value */
1247 if (UnmarshallLong(&preq, &temp) != 0) {
1248 CloseHandle(reqHandle);
1253 CloseHandle(reqHandle);
1254 errno = CMtoUNIXerror(temp);
1255 if ( IoctlDebug() ) {
1257 fprintf(stderr, "pioctl temp != 0: 0x%X\r\n",temp);
1263 /* otherwise, unmarshall the output parameters */
1264 if (blobp->out_size) {
1265 temp = blobp->out_size;
1266 if (preq.nbytes < temp)
1268 memcpy(blobp->out, preq.mp, temp);
1269 blobp->out_size = temp;
1272 /* and return success */
1273 CloseHandle(reqHandle);
1278 pioctl_utf8(char * pathp, long opcode, struct ViceIoctl * blobp, int follow)
1280 return pioctl_int(pathp, opcode, blobp, follow, TRUE);
1284 pioctl(char * pathp, long opcode, struct ViceIoctl * blobp, int follow)
1286 return pioctl_int(pathp, opcode, blobp, follow, FALSE);