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>
25 #define SECURITY_WIN32
36 #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 #define FS_IOCTLREQUEST_MAXSIZE 8192
52 /* big structure for representing and storing an IOCTL request */
53 typedef struct fs_ioctlRequest {
54 char *mp; /* marshalling/unmarshalling ptr */
55 long nbytes; /* bytes received (when unmarshalling) */
56 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
60 CMtoUNIXerror(int cm_code)
63 case CM_ERROR_TIMEDOUT:
65 case CM_ERROR_NOACCESS:
67 case CM_ERROR_NOSUCHFILE:
75 case CM_ERROR_CROSSDEVLINK:
81 case CM_ERROR_READONLY:
83 case CM_ERROR_WOULDBLOCK:
85 case CM_ERROR_NOSUCHCELL:
86 return ESRCH; /* hack */
87 case CM_ERROR_NOSUCHVOLUME:
88 return EPIPE; /* hack */
89 case CM_ERROR_NOMORETOKENS:
90 return EDOM; /* hack */
91 case CM_ERROR_TOOMANYBUFS:
92 return EFBIG; /* hack */
99 InitFSRequest(fs_ioctlRequest_t * rp)
109 static BOOL debug = 0;
114 if (RegOpenKey (HKEY_LOCAL_MACHINE,
115 TEXT("Software\\OpenAFS\\Client"), &hk) == 0)
117 DWORD dwSize = sizeof(BOOL);
118 DWORD dwType = REG_DWORD;
119 RegQueryValueEx (hk, TEXT("IoctlDebug"), NULL, &dwType, (PBYTE)&debug, &dwSize);
131 DECL_FUNC_PTR(krb5_cc_default_name);
132 DECL_FUNC_PTR(krb5_cc_set_default_name);
133 DECL_FUNC_PTR(krb5_get_default_config_files);
134 DECL_FUNC_PTR(krb5_free_config_files);
135 DECL_FUNC_PTR(krb5_free_context);
136 DECL_FUNC_PTR(krb5_get_default_realm);
137 DECL_FUNC_PTR(krb5_free_default_realm);
138 DECL_FUNC_PTR(krb5_init_context);
139 DECL_FUNC_PTR(krb5_cc_default);
140 DECL_FUNC_PTR(krb5_parse_name);
141 DECL_FUNC_PTR(krb5_free_principal);
142 DECL_FUNC_PTR(krb5_cc_close);
143 DECL_FUNC_PTR(krb5_cc_get_principal);
144 DECL_FUNC_PTR(krb5_build_principal);
145 DECL_FUNC_PTR(krb5_c_random_make_octets);
146 DECL_FUNC_PTR(krb5_get_init_creds_password);
147 DECL_FUNC_PTR(krb5_free_cred_contents);
148 DECL_FUNC_PTR(krb5_cc_resolve);
149 DECL_FUNC_PTR(krb5_unparse_name);
150 DECL_FUNC_PTR(krb5_free_unparsed_name);
152 FUNC_INFO krb5_fi[] = {
153 MAKE_FUNC_INFO(krb5_cc_default_name),
154 MAKE_FUNC_INFO(krb5_cc_set_default_name),
155 MAKE_FUNC_INFO(krb5_get_default_config_files),
156 MAKE_FUNC_INFO(krb5_free_config_files),
157 MAKE_FUNC_INFO(krb5_free_context),
158 MAKE_FUNC_INFO(krb5_get_default_realm),
159 MAKE_FUNC_INFO(krb5_free_default_realm),
160 MAKE_FUNC_INFO(krb5_init_context),
161 MAKE_FUNC_INFO(krb5_cc_default),
162 MAKE_FUNC_INFO(krb5_parse_name),
163 MAKE_FUNC_INFO(krb5_free_principal),
164 MAKE_FUNC_INFO(krb5_cc_close),
165 MAKE_FUNC_INFO(krb5_cc_get_principal),
166 MAKE_FUNC_INFO(krb5_build_principal),
167 MAKE_FUNC_INFO(krb5_c_random_make_octets),
168 MAKE_FUNC_INFO(krb5_get_init_creds_password),
169 MAKE_FUNC_INFO(krb5_free_cred_contents),
170 MAKE_FUNC_INFO(krb5_cc_resolve),
171 MAKE_FUNC_INFO(krb5_unparse_name),
172 MAKE_FUNC_INFO(krb5_free_unparsed_name),
178 const char* dll_name,
180 HINSTANCE* ph, // [out, optional] - DLL handle
181 int* pindex, // [out, optional] - index of last func loaded (-1 if none)
182 int cleanup, // cleanup function pointers and unload on error
183 int go_on, // continue loading even if some functions cannot be loaded
184 int silent // do not pop-up a system dialog if DLL cannot be loaded
193 if (pindex) *pindex = -1;
195 for (n = 0; fi[n].func_ptr_var; n++)
196 *(fi[n].func_ptr_var) = 0;
199 em = SetErrorMode(SEM_FAILCRITICALERRORS);
200 h = LoadLibrary(dll_name);
208 for (i = 0; (go_on || !error) && (i < n); i++)
210 void* p = (void*)GetProcAddress(h, fi[i].func_name);
216 *(fi[i].func_ptr_var) = p;
219 if (pindex) *pindex = last_i;
220 if (error && cleanup && !go_on) {
221 for (i = 0; i < n; i++) {
222 *(fi[i].func_ptr_var) = 0;
232 #define KERB5DLL "krb5_32.dll"
236 static HINSTANCE hKrb5DLL = 0;
241 hKrb5DLL = LoadLibrary(KERB5DLL);
243 if (!LoadFuncs(KERB5DLL, krb5_fi, 0, 0, 1, 0, 0))
245 FreeLibrary(hKrb5DLL);
255 GetLSAPrincipalName(char * szUser, DWORD *dwSize)
257 krb5_context ctx = 0;
258 krb5_error_code code;
259 krb5_ccache mslsa_ccache=0;
260 krb5_principal princ = 0;
264 if (!IsKrb5Available())
267 if (code = pkrb5_init_context(&ctx))
270 if (code = pkrb5_cc_resolve(ctx, "MSLSA:", &mslsa_ccache))
273 if (code = pkrb5_cc_get_principal(ctx, mslsa_ccache, &princ))
276 if (code = pkrb5_unparse_name(ctx, princ, &pname))
279 if ( strlen(pname) < *dwSize ) {
280 strncpy(szUser, pname, *dwSize);
281 szUser[*dwSize-1] = '\0';
284 *dwSize = strlen(pname);
288 pkrb5_free_unparsed_name(ctx, pname);
291 pkrb5_free_principal(ctx, princ);
294 pkrb5_cc_close(ctx, mslsa_ccache);
297 pkrb5_free_context(ctx);
302 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
305 char netbiosName[MAX_NB_NAME_LENGTH];
306 char tbuffer[256]="";
309 char szUser[128] = "";
310 char szClient[MAX_PATH] = "";
311 char szPath[MAX_PATH] = "";
314 DWORD ioctlDebug = IoctlDebug();
316 DWORD dwSize = sizeof(szUser);
319 drivep = strchr(fileNamep, ':');
320 if (drivep && (drivep - fileNamep) >= 1) {
321 tbuffer[0] = *(drivep - 1);
323 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
324 } else if (fileNamep[0] == fileNamep[1] &&
325 fileNamep[0] == '\\')
327 int count = 0, i = 0;
329 while (count < 4 && fileNamep[i]) {
330 tbuffer[i] = fileNamep[i];
331 if ( tbuffer[i++] == '\\' )
334 if (fileNamep[i] == 0)
337 strcat(tbuffer, SMB_IOCTL_FILENAME);
341 GetCurrentDirectory(sizeof(curdir), curdir);
342 if ( curdir[1] == ':' ) {
343 tbuffer[0] = curdir[0];
345 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
346 } else if (curdir[0] == curdir[1] &&
349 int count = 0, i = 0;
351 while (count < 4 && curdir[i]) {
352 tbuffer[i] = curdir[i];
353 if ( tbuffer[i++] == '\\' )
359 strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
364 /* No file name starting with drive colon specified, use UNC name */
365 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
366 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
370 /* now open the file */
371 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
372 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
373 FILE_FLAG_WRITE_THROUGH, NULL);
375 if (fh == INVALID_HANDLE_VALUE) {
378 gle = GetLastError();
379 if (gle && ioctlDebug ) {
382 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
385 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
391 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
396 lana_GetNetbiosName(szClient, LANA_NETBIOS_NAME_FULL);
398 if (RegOpenKey (HKEY_CURRENT_USER,
399 TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0)
401 DWORD dwType = REG_SZ;
402 RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize);
408 fprintf(stderr, "pioctl Explorer logon user: [%s]\r\n",szUser);
410 sprintf(szPath, "\\\\%s", szClient);
411 memset (&nr, 0x00, sizeof(NETRESOURCE));
412 nr.dwType=RESOURCETYPE_DISK;
414 nr.lpRemoteName=szPath;
415 res = WNetAddConnection2(&nr,NULL,szUser,0);
418 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
424 sprintf(szPath, "\\\\%s\\all", szClient);
425 res = WNetAddConnection2(&nr,NULL,szUser,0);
428 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
435 goto try_lsa_principal;
437 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
438 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
439 FILE_FLAG_WRITE_THROUGH, NULL);
441 if (fh == INVALID_HANDLE_VALUE) {
442 gle = GetLastError();
443 if (gle && ioctlDebug ) {
446 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
449 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
455 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
464 if (fh == INVALID_HANDLE_VALUE) {
467 dwSize = sizeof(szUser);
468 if (GetLSAPrincipalName(szUser, &dwSize)) {
470 fprintf(stderr, "pioctl LSA Principal logon user: [%s]\r\n",szUser);
472 sprintf(szPath, "\\\\%s", szClient);
473 memset (&nr, 0x00, sizeof(NETRESOURCE));
474 nr.dwType=RESOURCETYPE_DISK;
476 nr.lpRemoteName=szPath;
477 res = WNetAddConnection2(&nr,NULL,szUser,0);
480 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
486 sprintf(szPath, "\\\\%s\\all", szClient);
487 res = WNetAddConnection2(&nr,NULL,szUser,0);
490 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
499 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
500 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
501 FILE_FLAG_WRITE_THROUGH, NULL);
503 if (fh == INVALID_HANDLE_VALUE) {
504 gle = GetLastError();
505 if (gle && ioctlDebug ) {
508 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
511 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
517 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
526 if ( fh == INVALID_HANDLE_VALUE ) {
527 dwSize = sizeof(szUser);
528 if (GetUserNameEx(NameSamCompatible, szUser, &dwSize)) {
530 fprintf(stderr, "pioctl SamCompatible logon user: [%s]\r\n",szUser);
532 sprintf(szPath, "\\\\%s", szClient);
533 memset (&nr, 0x00, sizeof(NETRESOURCE));
534 nr.dwType=RESOURCETYPE_DISK;
536 nr.lpRemoteName=szPath;
537 res = WNetAddConnection2(&nr,NULL,szUser,0);
540 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
545 sprintf(szPath, "\\\\%s\\all", szClient);
546 res = WNetAddConnection2(&nr,NULL,szUser,0);
549 fprintf(stderr, "pioctl WNetAddConnection2(%s,%s) failed: 0x%X\r\n",
555 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
556 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
557 FILE_FLAG_WRITE_THROUGH, NULL);
559 if (fh == INVALID_HANDLE_VALUE) {
560 gle = GetLastError();
561 if (gle && ioctlDebug ) {
564 if ( FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
567 MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
573 fprintf(stderr,"pioctl CreateFile(%s) failed: 0x%X\r\n\t[%s]\r\n",
580 fprintf(stderr, "GetUserNameEx(NameSamCompatible) failed: 0x%X\r\n", GetLastError());
585 /* return fh and success code */
591 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
597 rcount = reqp->mp - reqp->data;
600 fprintf(stderr, "pioctl Transceive rcount <= 0: %d\r\n",rcount);
601 return EINVAL; /* not supposed to happen */
604 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
605 /* failed to write */
606 gle = GetLastError();
609 fprintf(stderr, "pioctl Transceive WriteFile failed: 0x%X\r\n",gle);
613 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
615 gle = GetLastError();
618 fprintf(stderr, "pioctl Transceive ReadFile failed: 0x%X\r\n",gle);
622 reqp->nbytes = ioCount; /* set # of bytes available */
623 reqp->mp = reqp->data; /* restart marshalling */
630 MarshallLong(fs_ioctlRequest_t * reqp, long val)
632 memcpy(reqp->mp, &val, 4);
638 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
640 /* not enough data left */
641 if (reqp->nbytes < 4) {
643 fprintf(stderr, "pioctl UnmarshallLong reqp->nbytes < 4: %d\r\n",
648 memcpy(valp, reqp->mp, 4);
654 /* includes marshalling NULL pointer as a null (0 length) string */
656 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
661 count = strlen(stringp) + 1; /* space required including null */
665 /* watch for buffer overflow */
666 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) {
668 fprintf(stderr, "pioctl MarshallString buffer overflow\r\n");
673 memcpy(reqp->mp, stringp, count);
680 /* take a path with a drive letter, possibly relative, and return a full path
681 * without the drive letter. This is the full path relative to the working
682 * dir for that drive letter. The input and output paths can be the same.
685 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
695 if (pathp[0] != 0 && pathp[1] == ':') {
696 /* there's a drive letter there */
704 if ( firstp[0] == '\\' && firstp[1] == '\\') {
705 /* UNC path - strip off the server and sharename */
707 for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
708 if ( firstp[i] == '\\' || firstp[i] == '/' ) {
712 if ( firstp[i] == 0 ) {
713 strcpy(outPathp,"\\");
715 strcpy(outPathp,&firstp[--i]);
718 } else if (firstp[0] == '\\' || firstp[0] == '/') {
719 /* already an absolute pathname, just copy it back */
720 strcpy(outPathp, firstp);
724 GetCurrentDirectory(sizeof(origPath), origPath);
727 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
728 /* a drive has been specified and it isn't our current drive.
729 * to get path, switch to it first. Must case-fold drive letters
730 * for user convenience.
736 if (!SetCurrentDirectory(newPath)) {
737 code = GetLastError();
740 fprintf(stderr, "pioctl fs_GetFullPath SetCurrentDirectory(%s) failed: 0x%X\r\n",
746 /* now get the absolute path to the current wdir in this drive */
747 GetCurrentDirectory(sizeof(tpath), tpath);
749 strcpy(outPathp, tpath + 2); /* skip drive letter */
750 else if ( tpath[0] == '\\' && tpath[1] == '\\') {
751 /* UNC path - strip off the server and sharename */
753 for ( i=2,count=2; count < 4 && tpath[i]; i++ ) {
754 if ( tpath[i] == '\\' || tpath[i] == '/' ) {
758 if ( tpath[i] == 0 ) {
759 strcpy(outPathp,"\\");
761 strcpy(outPathp,&tpath[--i]);
764 /* this should never happen */
765 strcpy(outPathp, tpath);
768 /* if there is a non-null name after the drive, append it */
770 int len = strlen(outPathp);
771 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
772 strcat(outPathp, "\\");
773 strcat(outPathp, firstp);
776 /* finally, if necessary, switch back to our home drive letter */
778 SetCurrentDirectory(origPath);
785 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
787 fs_ioctlRequest_t preq;
793 code = GetIoctlHandle(pathp, &reqHandle);
802 /* init the request structure */
803 InitFSRequest(&preq);
805 /* marshall the opcode, the path name and the input parameters */
806 MarshallLong(&preq, opcode);
807 /* when marshalling the path, remove the drive letter, since we already
808 * used the drive letter to find the AFS daemon; we don't need it any more.
809 * Eventually we'll expand relative path names here, too, since again, only
810 * we understand those.
813 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
815 CloseHandle(reqHandle);
820 strcpy(fullPath, "");
823 MarshallString(&preq, fullPath);
824 if (blobp->in_size) {
825 memcpy(preq.mp, blobp->in, blobp->in_size);
826 preq.mp += blobp->in_size;
829 /* now make the call */
830 code = Transceive(reqHandle, &preq);
832 CloseHandle(reqHandle);
836 /* now unmarshall the return value */
837 if (UnmarshallLong(&preq, &temp) != 0) {
838 CloseHandle(reqHandle);
843 CloseHandle(reqHandle);
844 errno = CMtoUNIXerror(temp);
846 fprintf(stderr, "pioctl temp != 0: 0x%X\r\n",temp);
850 /* otherwise, unmarshall the output parameters */
851 if (blobp->out_size) {
852 temp = blobp->out_size;
853 if (preq.nbytes < temp)
855 memcpy(blobp->out, preq.mp, temp);
856 blobp->out_size = temp;
859 /* and return success */
860 CloseHandle(reqHandle);