ioctl-handle-20040603
[openafs.git] / src / sys / pioctl_nt.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <afs/stds.h>
17 #include <windows.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <malloc.h>
22 #include <string.h>
23 #include <winioctl.h>
24 #include <winsock2.h>
25 #include <nb30.h>
26
27 #include <osi.h>
28
29 #include <cm.h>
30 #include <cm_dir.h>
31 #include <cm_cell.h>
32 #include <cm_user.h>
33 #include <cm_conn.h>
34 #include <cm_scache.h>
35 #include <cm_buf.h>
36 #include <cm_utils.h>
37 #include <cm_ioctl.h>
38
39 #include <smb.h>
40 #include <pioctl_nt.h>
41
42 #include <lanahelper.h>
43
44 static char AFSConfigKeyName[] =
45     "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
46
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 */
53 } fs_ioctlRequest_t;
54
55 static int
56 CMtoUNIXerror(int cm_code)
57 {
58     switch (cm_code) {
59     case CM_ERROR_TIMEDOUT:
60         return ETIMEDOUT;
61     case CM_ERROR_NOACCESS:
62         return EACCES;
63     case CM_ERROR_NOSUCHFILE:
64         return ENOENT;
65     case CM_ERROR_INVAL:
66         return EINVAL;
67     case CM_ERROR_BADFD:
68         return EBADF;
69     case CM_ERROR_EXISTS:
70         return EEXIST;
71     case CM_ERROR_CROSSDEVLINK:
72         return EXDEV;
73     case CM_ERROR_NOTDIR:
74         return ENOTDIR;
75     case CM_ERROR_ISDIR:
76         return EISDIR;
77     case CM_ERROR_READONLY:
78         return EROFS;
79     case CM_ERROR_WOULDBLOCK:
80         return EWOULDBLOCK;
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 */
89     default:
90         return ENOTTY;
91     }
92 }
93
94 static void
95 InitFSRequest(fs_ioctlRequest_t * rp)
96 {
97     rp->mp = rp->data;
98     rp->nbytes = 0;
99 }
100
101 static long
102 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
103 {
104     char *drivep;
105     char netbiosName[MAX_NB_NAME_LENGTH];
106     char tbuffer[256]="";
107     HANDLE fh;
108
109     if (fileNamep) {
110         drivep = strchr(fileNamep, ':');
111         if (drivep && (drivep - fileNamep) >= 1) {
112             tbuffer[0] = *(drivep - 1);
113             tbuffer[1] = ':';
114             strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
115         } else {
116             char curdir[256]="";
117
118             GetCurrentDirectory(sizeof(curdir), curdir);
119             if ( curdir[1] == ':' ) {
120                 tbuffer[0] = curdir[0];
121                 tbuffer[1] = ':';
122                 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
123             }
124         }
125         }
126         if (!tbuffer[0]) {
127         /* No file name starting with drive colon specified, use UNC name */
128         lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
129         sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
130     }
131
132     fflush(stdout);
133     /* now open the file */
134     fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
135                     FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
136                     FILE_FLAG_WRITE_THROUGH, NULL);
137     fflush(stdout);
138     if (fh == INVALID_HANDLE_VALUE)
139         return -1;
140
141     /* return fh and success code */
142     *handlep = fh;
143     return 0;
144 }
145
146 static long
147 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
148 {
149     long rcount;
150     long ioCount;
151
152     rcount = reqp->mp - reqp->data;
153     if (rcount <= 0)
154         return EINVAL;          /* not supposed to happen */
155
156     if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
157         /* failed to write */
158         return GetLastError();
159     }
160
161     if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
162         /* failed to read */
163         return GetLastError();
164     }
165
166     reqp->nbytes = ioCount;     /* set # of bytes available */
167     reqp->mp = reqp->data;      /* restart marshalling */
168
169     /* return success */
170     return 0;
171 }
172
173 static long
174 MarshallLong(fs_ioctlRequest_t * reqp, long val)
175 {
176     memcpy(reqp->mp, &val, 4);
177     reqp->mp += 4;
178     return 0;
179 }
180
181 static long
182 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
183 {
184     /* not enough data left */
185     if (reqp->nbytes < 4) {
186         return -1;
187     }
188
189     memcpy(valp, reqp->mp, 4);
190     reqp->mp += 4;
191     reqp->nbytes -= 4;
192     return 0;
193 }
194
195 /* includes marshalling NULL pointer as a null (0 length) string */
196 static long
197 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
198 {
199     int count;
200
201     if (stringp)
202         count = strlen(stringp) + 1;    /* space required including null */
203     else
204         count = 1;
205
206     /* watch for buffer overflow */
207     if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
208         return -1;
209
210     if (stringp)
211         memcpy(reqp->mp, stringp, count);
212     else
213         *(reqp->mp) = 0;
214     reqp->mp += count;
215     return 0;
216 }
217
218 /* take a path with a drive letter, possibly relative, and return a full path
219  * without the drive letter.  This is the full path relative to the working
220  * dir for that drive letter.  The input and output paths can be the same.
221  */
222 static long
223 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
224 {
225     char tpath[1000];
226     char origPath[1000];
227     char *firstp;
228     long code;
229     int pathHasDrive;
230     int doSwitch;
231     char newPath[3];
232
233     if (pathp[0] != 0 && pathp[1] == ':') {
234         /* there's a drive letter there */
235         firstp = pathp + 2;
236         pathHasDrive = 1;
237     } else {
238         firstp = pathp;
239         pathHasDrive = 0;
240     }
241
242     if (*firstp == '\\' || *firstp == '/') {
243         /* already an absolute pathname, just copy it back */
244         strcpy(outPathp, firstp);
245         return 0;
246     }
247
248     GetCurrentDirectory(sizeof(origPath), origPath);
249
250     doSwitch = 0;
251     if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
252         /* a drive has been specified and it isn't our current drive.
253          * to get path, switch to it first.  Must case-fold drive letters
254          * for user convenience.
255          */
256         doSwitch = 1;
257         newPath[0] = *pathp;
258         newPath[1] = ':';
259         newPath[2] = 0;
260         if (!SetCurrentDirectory(newPath)) {
261             code = GetLastError();
262             return code;
263         }
264     }
265
266     /* now get the absolute path to the current wdir in this drive */
267     GetCurrentDirectory(sizeof(tpath), tpath);
268         if (tpath[1] == ':')
269             strcpy(outPathp, tpath + 2);        /* skip drive letter */
270         else
271                 strcpy(outPathp, tpath);                /* copy entire UNC path */
272     /* if there is a non-null name after the drive, append it */
273     if (*firstp != 0) {
274                 int len = strlen(outPathp);
275                 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/') 
276                         strcat(outPathp, "\\");
277                 strcat(outPathp, firstp);
278     }
279
280     /* finally, if necessary, switch back to our home drive letter */
281     if (doSwitch) {
282         SetCurrentDirectory(origPath);
283     }
284
285     return 0;
286 }
287
288 long
289 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
290 {
291     fs_ioctlRequest_t preq;
292     long code;
293     long temp;
294     char fullPath[1000];
295     HANDLE reqHandle;
296
297     code = GetIoctlHandle(pathp, &reqHandle);
298     if (code) {
299         if (pathp)
300             errno = EINVAL;
301         else
302             errno = ENODEV;
303         return code;
304     }
305
306     /* init the request structure */
307     InitFSRequest(&preq);
308
309     /* marshall the opcode, the path name and the input parameters */
310     MarshallLong(&preq, opcode);
311     /* when marshalling the path, remove the drive letter, since we already
312      * used the drive letter to find the AFS daemon; we don't need it any more.
313      * Eventually we'll expand relative path names here, too, since again, only
314      * we understand those.
315      */
316     if (pathp) {
317         code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
318         if (code) {
319             CloseHandle(reqHandle);
320             errno = EINVAL;
321             return code;
322         }
323     } else {
324         strcpy(fullPath, "");
325     }
326
327     MarshallString(&preq, fullPath);
328     if (blobp->in_size) {
329         memcpy(preq.mp, blobp->in, blobp->in_size);
330         preq.mp += blobp->in_size;
331     }
332
333     /* now make the call */
334     code = Transceive(reqHandle, &preq);
335     if (code) {
336         CloseHandle(reqHandle);
337         return code;
338     }
339
340     /* now unmarshall the return value */
341     UnmarshallLong(&preq, &temp);
342     if (temp != 0) {
343         CloseHandle(reqHandle);
344         errno = CMtoUNIXerror(temp);
345         return -1;
346     }
347
348     /* otherwise, unmarshall the output parameters */
349     if (blobp->out_size) {
350         temp = blobp->out_size;
351         if (preq.nbytes < temp)
352             temp = preq.nbytes;
353         memcpy(blobp->out, preq.mp, temp);
354         blobp->out_size = temp;
355     }
356
357     /* and return success */
358     CloseHandle(reqHandle);
359     return 0;
360 }