unc-paths-current-directory-20040727
[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 if (fileNamep[0] == fileNamep[1] && 
116                                fileNamep[0] == '\\')
117         {
118             int count = 0, i = 0;
119
120             while (count < 4 && fileNamep[i]) {
121                 tbuffer[i] = fileNamep[i];
122                 if ( tbuffer[i++] == '\\' )
123                     count++;
124             }
125             if (fileNamep[i] == 0)
126                 tbuffer[i++] = '\\';
127             tbuffer[i] = 0;
128             strcat(tbuffer, SMB_IOCTL_FILENAME);
129         } else {
130             char curdir[256]="";
131
132             GetCurrentDirectory(sizeof(curdir), curdir);
133             if ( curdir[1] == ':' ) {
134                 tbuffer[0] = curdir[0];
135                 tbuffer[1] = ':';
136                 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
137             } else if (curdir[0] == curdir[1] &&
138                        curdir[0] == '\\') 
139             {
140                 int count = 0, i = 0;
141
142                 while (count < 4 && curdir[i]) {
143                     tbuffer[i] = curdir[i];
144                     if ( tbuffer[i++] == '\\' )
145                         count++;
146                 }
147                 if (tbuffer[i] == 0)
148                     tbuffer[i++] = '\\';
149                 tbuffer[i] = 0;
150                 strcat(tbuffer, SMB_IOCTL_FILENAME_NOSLASH);
151             }
152         }
153         }
154         if (!tbuffer[0]) {
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);
158     }
159
160     fflush(stdout);
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);
165     fflush(stdout);
166     if (fh == INVALID_HANDLE_VALUE)
167         return -1;
168
169     /* return fh and success code */
170     *handlep = fh;
171     return 0;
172 }
173
174 static long
175 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
176 {
177     long rcount;
178     long ioCount;
179
180     rcount = reqp->mp - reqp->data;
181     if (rcount <= 0)
182         return EINVAL;          /* not supposed to happen */
183
184     if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
185         /* failed to write */
186         return GetLastError();
187     }
188
189     if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
190         /* failed to read */
191         return GetLastError();
192     }
193
194     reqp->nbytes = ioCount;     /* set # of bytes available */
195     reqp->mp = reqp->data;      /* restart marshalling */
196
197     /* return success */
198     return 0;
199 }
200
201 static long
202 MarshallLong(fs_ioctlRequest_t * reqp, long val)
203 {
204     memcpy(reqp->mp, &val, 4);
205     reqp->mp += 4;
206     return 0;
207 }
208
209 static long
210 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
211 {
212     /* not enough data left */
213     if (reqp->nbytes < 4) {
214         return -1;
215     }
216
217     memcpy(valp, reqp->mp, 4);
218     reqp->mp += 4;
219     reqp->nbytes -= 4;
220     return 0;
221 }
222
223 /* includes marshalling NULL pointer as a null (0 length) string */
224 static long
225 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
226 {
227     int count;
228
229     if (stringp)
230         count = strlen(stringp) + 1;    /* space required including null */
231     else
232         count = 1;
233
234     /* watch for buffer overflow */
235     if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
236         return -1;
237
238     if (stringp)
239         memcpy(reqp->mp, stringp, count);
240     else
241         *(reqp->mp) = 0;
242     reqp->mp += count;
243     return 0;
244 }
245
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.
249  */
250 static long
251 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
252 {
253     char tpath[1000];
254     char origPath[1000];
255     char *firstp;
256     long code;
257     int pathHasDrive;
258     int doSwitch;
259     char newPath[3];
260
261     if (pathp[0] != 0 && pathp[1] == ':') {
262         /* there's a drive letter there */
263         firstp = pathp + 2;
264         pathHasDrive = 1;
265     } else {
266         firstp = pathp;
267         pathHasDrive = 0;
268     }
269
270     if ( firstp[0] == '\\' && firstp[1] == '\\') {
271         /* UNC path - strip off the server and sharename */
272         int i, count;
273         for ( i=2,count=2; count < 4 && firstp[i]; i++ ) {
274             if ( firstp[i] == '\\' || firstp[i] == '/' ) {
275                 count++;
276             }
277         }
278         if ( firstp[i] == 0 ) {
279             strcpy(outPathp,"\\");
280         } else {
281             strcpy(outPathp,&firstp[--i]);
282         }
283         return 0;
284     } else if (firstp[0] == '\\' || firstp[0] == '/') {
285         /* already an absolute pathname, just copy it back */
286         strcpy(outPathp, firstp);
287         return 0;
288     }
289
290     GetCurrentDirectory(sizeof(origPath), origPath);
291
292     doSwitch = 0;
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.
297          */
298         doSwitch = 1;
299         newPath[0] = *pathp;
300         newPath[1] = ':';
301         newPath[2] = 0;
302         if (!SetCurrentDirectory(newPath)) {
303             code = GetLastError();
304             return code;
305         }
306     }
307
308     /* now get the absolute path to the current wdir in this drive */
309     GetCurrentDirectory(sizeof(tpath), tpath);
310         if (tpath[1] == ':')
311             strcpy(outPathp, tpath + 2);        /* skip drive letter */
312         else if ( tpath[0] == '\\' && tpath[1] == '\\') {
313         /* UNC path - strip off the server and sharename */
314         int i, count;
315         for ( i=2,count=2; count < 4 && tpath[i]; i++ ) {
316             if ( tpath[i] == '\\' || tpath[i] == '/' ) {
317                 count++;
318             }
319         }
320         if ( tpath[i] == 0 ) {
321             strcpy(outPathp,"\\");
322         } else {
323             strcpy(outPathp,&tpath[--i]);
324         }
325     } else {
326         /* this should never happen */
327         strcpy(outPathp, tpath);
328     }
329
330     /* if there is a non-null name after the drive, append it */
331     if (*firstp != 0) {
332                 int len = strlen(outPathp);
333                 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/') 
334                         strcat(outPathp, "\\");
335                 strcat(outPathp, firstp);
336     }
337
338     /* finally, if necessary, switch back to our home drive letter */
339     if (doSwitch) {
340         SetCurrentDirectory(origPath);
341     }
342
343     return 0;
344 }
345
346 long
347 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
348 {
349     fs_ioctlRequest_t preq;
350     long code;
351     long temp;
352     char fullPath[1000];
353     HANDLE reqHandle;
354
355     code = GetIoctlHandle(pathp, &reqHandle);
356     if (code) {
357         if (pathp)
358             errno = EINVAL;
359         else
360             errno = ENODEV;
361         return code;
362     }
363
364     /* init the request structure */
365     InitFSRequest(&preq);
366
367     /* marshall the opcode, the path name and the input parameters */
368     MarshallLong(&preq, opcode);
369     /* when marshalling the path, remove the drive letter, since we already
370      * used the drive letter to find the AFS daemon; we don't need it any more.
371      * Eventually we'll expand relative path names here, too, since again, only
372      * we understand those.
373      */
374     if (pathp) {
375         code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
376         if (code) {
377             CloseHandle(reqHandle);
378             errno = EINVAL;
379             return code;
380         }
381     } else {
382         strcpy(fullPath, "");
383     }
384
385     MarshallString(&preq, fullPath);
386     if (blobp->in_size) {
387         memcpy(preq.mp, blobp->in, blobp->in_size);
388         preq.mp += blobp->in_size;
389     }
390
391     /* now make the call */
392     code = Transceive(reqHandle, &preq);
393     if (code) {
394         CloseHandle(reqHandle);
395         return code;
396     }
397
398     /* now unmarshall the return value */
399     UnmarshallLong(&preq, &temp);
400     if (temp != 0) {
401         CloseHandle(reqHandle);
402         errno = CMtoUNIXerror(temp);
403         return -1;
404     }
405
406     /* otherwise, unmarshall the output parameters */
407     if (blobp->out_size) {
408         temp = blobp->out_size;
409         if (preq.nbytes < temp)
410             temp = preq.nbytes;
411         memcpy(blobp->out, preq.mp, temp);
412         blobp->out_size = temp;
413     }
414
415     /* and return success */
416     CloseHandle(reqHandle);
417     return 0;
418 }