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>
34 #include <cm_scache.h>
40 #include <pioctl_nt.h>
42 #include <lanahelper.h>
44 static char AFSConfigKeyName[] =
45 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
47 #define FS_IOCTLREQUEST_MAXSIZE 8192
48 /* big structure for representing and storing an IOCTL request */
49 typedef struct fs_ioctlRequest {
50 char *mp; /* marshalling/unmarshalling ptr */
51 long nbytes; /* bytes received (when unmarshalling) */
52 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
56 CMtoUNIXerror(int cm_code)
59 case CM_ERROR_TIMEDOUT:
61 case CM_ERROR_NOACCESS:
63 case CM_ERROR_NOSUCHFILE:
71 case CM_ERROR_CROSSDEVLINK:
77 case CM_ERROR_READONLY:
79 case CM_ERROR_WOULDBLOCK:
81 case CM_ERROR_NOSUCHCELL:
82 return ESRCH; /* hack */
83 case CM_ERROR_NOSUCHVOLUME:
84 return EPIPE; /* hack */
85 case CM_ERROR_NOMORETOKENS:
86 return EDOM; /* hack */
87 case CM_ERROR_TOOMANYBUFS:
88 return EFBIG; /* hack */
95 InitFSRequest(fs_ioctlRequest_t * rp)
102 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
105 char netbiosName[MAX_NB_NAME_LENGTH];
106 char tbuffer[256]="";
110 drivep = strchr(fileNamep, ':');
111 if (drivep && (drivep - fileNamep) >= 1) {
112 tbuffer[0] = *(drivep - 1);
114 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
115 } else if (fileNamep[0] == fileNamep[1] &&
116 fileNamep[0] == '\\')
118 int count = 0, i = 0;
120 while (count < 4 && fileNamep[i]) {
121 tbuffer[i] = fileNamep[i];
122 if ( tbuffer[i++] == '\\' )
125 if (fileNamep[i] == 0)
128 strcat(tbuffer, SMB_IOCTL_FILENAME);
132 GetCurrentDirectory(sizeof(curdir), curdir);
133 if ( curdir[1] == ':' ) {
134 tbuffer[0] = curdir[0];
136 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
137 } else if (curdir[0] == curdir[1] &&
140 int count = 0, i = 0;
142 while (count < 4 && curdir[i]) {
143 tbuffer[i] = curdir[i];
144 if ( tbuffer[i++] == '\\' )
150 strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
155 /* No file name starting with drive colon specified, use UNC name */
156 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
157 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
161 /* now open the file */
162 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
163 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
164 FILE_FLAG_WRITE_THROUGH, NULL);
166 if (fh == INVALID_HANDLE_VALUE)
169 /* return fh and success code */
175 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
180 rcount = reqp->mp - reqp->data;
182 return EINVAL; /* not supposed to happen */
184 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
185 /* failed to write */
186 return GetLastError();
189 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
191 return GetLastError();
194 reqp->nbytes = ioCount; /* set # of bytes available */
195 reqp->mp = reqp->data; /* restart marshalling */
202 MarshallLong(fs_ioctlRequest_t * reqp, long val)
204 memcpy(reqp->mp, &val, 4);
210 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
212 /* not enough data left */
213 if (reqp->nbytes < 4) {
217 memcpy(valp, reqp->mp, 4);
223 /* includes marshalling NULL pointer as a null (0 length) string */
225 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
230 count = strlen(stringp) + 1; /* space required including null */
234 /* watch for buffer overflow */
235 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
239 memcpy(reqp->mp, stringp, count);
246 /* take a path with a drive letter, possibly relative, and return a full path
247 * without the drive letter. This is the full path relative to the working
248 * dir for that drive letter. The input and output paths can be the same.
251 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
261 if (pathp[0] != 0 && pathp[1] == ':') {
262 /* there's a drive letter there */
270 if ( firstp[0] == '\\' && firstp[1] == '\\') {
271 /* UNC path - strip off the server and sharename */
273 for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
274 if ( firstp[i] == '\\' || firstp[i] == '/' ) {
278 if ( firstp[i] == 0 ) {
279 strcpy(outPathp,"\\");
281 strcpy(outPathp,&firstp[--i]);
284 } else if (firstp[0] == '\\' || firstp[0] == '/') {
285 /* already an absolute pathname, just copy it back */
286 strcpy(outPathp, firstp);
290 GetCurrentDirectory(sizeof(origPath), origPath);
293 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
294 /* a drive has been specified and it isn't our current drive.
295 * to get path, switch to it first. Must case-fold drive letters
296 * for user convenience.
302 if (!SetCurrentDirectory(newPath)) {
303 code = GetLastError();
308 /* now get the absolute path to the current wdir in this drive */
309 GetCurrentDirectory(sizeof(tpath), tpath);
311 strcpy(outPathp, tpath + 2); /* skip drive letter */
313 strcpy(outPathp, tpath); /* copy entire UNC path */
314 /* if there is a non-null name after the drive, append it */
316 int len = strlen(outPathp);
317 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
318 strcat(outPathp, "\\");
319 strcat(outPathp, firstp);
322 /* finally, if necessary, switch back to our home drive letter */
324 SetCurrentDirectory(origPath);
331 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
333 fs_ioctlRequest_t preq;
339 code = GetIoctlHandle(pathp, &reqHandle);
348 /* init the request structure */
349 InitFSRequest(&preq);
351 /* marshall the opcode, the path name and the input parameters */
352 MarshallLong(&preq, opcode);
353 /* when marshalling the path, remove the drive letter, since we already
354 * used the drive letter to find the AFS daemon; we don't need it any more.
355 * Eventually we'll expand relative path names here, too, since again, only
356 * we understand those.
359 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
361 CloseHandle(reqHandle);
366 strcpy(fullPath, "");
369 MarshallString(&preq, fullPath);
370 if (blobp->in_size) {
371 memcpy(preq.mp, blobp->in, blobp->in_size);
372 preq.mp += blobp->in_size;
375 /* now make the call */
376 code = Transceive(reqHandle, &preq);
378 CloseHandle(reqHandle);
382 /* now unmarshall the return value */
383 UnmarshallLong(&preq, &temp);
385 CloseHandle(reqHandle);
386 errno = CMtoUNIXerror(temp);
390 /* otherwise, unmarshall the output parameters */
391 if (blobp->out_size) {
392 temp = blobp->out_size;
393 if (preq.nbytes < temp)
395 memcpy(blobp->out, preq.mp, temp);
396 blobp->out_size = temp;
399 /* and return success */
400 CloseHandle(reqHandle);