windows-fs-test_volstat-20080103
[openafs.git] / src / WINNT / afsd / smb_ioctl.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 <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <stdlib.h>
15 #include <malloc.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <time.h>
19
20 #include <osi.h>
21
22 #include "afsd.h"
23
24 #include "smb.h"
25
26 smb_ioctlProc_t *smb_ioctlProcsp[SMB_IOCTL_MAXPROCS];
27
28 /*extern unsigned char smb_LANadapter;*/
29
30 void smb_InitIoctl(void)
31 {
32         int i;
33         for (i=0; i<SMB_IOCTL_MAXPROCS; i++)
34             smb_ioctlProcsp[i] = NULL;
35
36         smb_ioctlProcsp[VIOCGETAL] = cm_IoctlGetACL;
37         smb_ioctlProcsp[VIOC_FILE_CELL_NAME] = cm_IoctlGetFileCellName;
38         smb_ioctlProcsp[VIOCSETAL] = cm_IoctlSetACL;
39         smb_ioctlProcsp[VIOC_FLUSHVOLUME] = cm_IoctlFlushVolume;
40         smb_ioctlProcsp[VIOCFLUSH] = cm_IoctlFlushFile;
41         smb_ioctlProcsp[VIOCSETVOLSTAT] = cm_IoctlSetVolumeStatus;
42         smb_ioctlProcsp[VIOCGETVOLSTAT] = cm_IoctlGetVolumeStatus;
43         smb_ioctlProcsp[VIOCWHEREIS] = cm_IoctlWhereIs;
44         smb_ioctlProcsp[VIOC_AFS_STAT_MT_PT] = cm_IoctlStatMountPoint;
45         smb_ioctlProcsp[VIOC_AFS_DELETE_MT_PT] = cm_IoctlDeleteMountPoint;
46         smb_ioctlProcsp[VIOCCKSERV] = cm_IoctlCheckServers;
47         smb_ioctlProcsp[VIOC_GAG] = cm_IoctlGag;
48         smb_ioctlProcsp[VIOCCKBACK] = cm_IoctlCheckVolumes;
49         smb_ioctlProcsp[VIOCSETCACHESIZE] = cm_IoctlSetCacheSize;
50         smb_ioctlProcsp[VIOCGETCACHEPARMS] = cm_IoctlGetCacheParms;
51         smb_ioctlProcsp[VIOCGETCELL] = cm_IoctlGetCell;
52         smb_ioctlProcsp[VIOCNEWCELL] = cm_IoctlNewCell;
53         smb_ioctlProcsp[VIOC_GET_WS_CELL] = cm_IoctlGetWsCell;
54         smb_ioctlProcsp[VIOC_AFS_SYSNAME] = cm_IoctlSysName;
55         smb_ioctlProcsp[VIOC_GETCELLSTATUS] = cm_IoctlGetCellStatus;
56         smb_ioctlProcsp[VIOC_SETCELLSTATUS] = cm_IoctlSetCellStatus;
57         smb_ioctlProcsp[VIOC_SETSPREFS] = cm_IoctlSetSPrefs;
58         smb_ioctlProcsp[VIOC_GETSPREFS] = cm_IoctlGetSPrefs;
59         smb_ioctlProcsp[VIOC_STOREBEHIND] = cm_IoctlStoreBehind;
60         smb_ioctlProcsp[VIOC_AFS_CREATE_MT_PT] = cm_IoctlCreateMountPoint;
61         smb_ioctlProcsp[VIOC_TRACECTL] = cm_IoctlTraceControl;
62         smb_ioctlProcsp[VIOCSETTOK] = cm_IoctlSetToken;
63         smb_ioctlProcsp[VIOCGETTOK] = cm_IoctlGetTokenIter;
64         smb_ioctlProcsp[VIOCNEWGETTOK] = cm_IoctlGetToken;
65         smb_ioctlProcsp[VIOCDELTOK] = cm_IoctlDelToken;
66         smb_ioctlProcsp[VIOCDELALLTOK] = cm_IoctlDelAllToken;
67         smb_ioctlProcsp[VIOC_SYMLINK] = cm_IoctlSymlink;
68         smb_ioctlProcsp[VIOC_LISTSYMLINK] = cm_IoctlListlink;
69         smb_ioctlProcsp[VIOC_DELSYMLINK] = cm_IoctlDeletelink;
70         smb_ioctlProcsp[VIOC_MAKESUBMOUNT] = cm_IoctlMakeSubmount;
71         smb_ioctlProcsp[VIOC_GETRXKCRYPT] = cm_IoctlGetRxkcrypt;
72         smb_ioctlProcsp[VIOC_SETRXKCRYPT] = cm_IoctlSetRxkcrypt;
73         smb_ioctlProcsp[VIOC_ISSYMLINK] = cm_IoctlIslink;
74         smb_ioctlProcsp[VIOC_TRACEMEMDUMP] = cm_IoctlMemoryDump;
75         smb_ioctlProcsp[VIOC_ISSYMLINK] = cm_IoctlIslink;
76         smb_ioctlProcsp[VIOC_FLUSHALL] = cm_IoctlFlushAllVolumes;
77         smb_ioctlProcsp[VIOCGETFID] = cm_IoctlGetFid;
78         smb_ioctlProcsp[VIOCGETOWNER] = cm_IoctlGetOwner;
79         smb_ioctlProcsp[VIOC_RXSTAT_PROC] = cm_IoctlRxStatProcess;
80         smb_ioctlProcsp[VIOC_RXSTAT_PEER] = cm_IoctlRxStatPeer;
81         smb_ioctlProcsp[VIOC_UUIDCTL] = cm_IoctlUUIDControl;
82         smb_ioctlProcsp[VIOC_PATH_AVAILABILITY] = cm_IoctlPathAvailability;
83         smb_ioctlProcsp[VIOC_VOLSTAT_TEST] = cm_IoctlVolStatTest;
84 }
85
86 /* called to make a fid structure into an IOCTL fid structure */
87 void smb_SetupIoctlFid(smb_fid_t *fidp, cm_space_t *prefix)
88 {
89     smb_ioctl_t *iop;
90     cm_space_t *copyPrefix;
91
92     lock_ObtainMutex(&fidp->mx);
93     fidp->flags |= SMB_FID_IOCTL;
94     fidp->scp = &cm_data.fakeSCache;
95     cm_HoldSCache(fidp->scp);
96     if (fidp->ioctlp == NULL) {
97         iop = malloc(sizeof(*iop));
98         memset(iop, 0, sizeof(*iop));
99         fidp->ioctlp = iop;
100         iop->fidp = fidp;
101     }
102     if (prefix) {
103         copyPrefix = cm_GetSpace();
104         strcpy(copyPrefix->data, prefix->data);
105         fidp->ioctlp->prefix = copyPrefix;
106     }
107     lock_ReleaseMutex(&fidp->mx);
108 }
109
110 /* called when we receive a read call, does the send of the received data if
111  * this is the first read call.  This is the function that actually makes the
112  * call to the ioctl code.
113  */
114 long smb_IoctlPrepareRead(smb_fid_t *fidp, smb_ioctl_t *ioctlp, cm_user_t *userp)
115 {
116     long opcode;
117     smb_ioctlProc_t *procp = NULL;
118     long code;
119
120     if (ioctlp->flags & SMB_IOCTLFLAG_DATAIN) {
121         ioctlp->flags &= ~SMB_IOCTLFLAG_DATAIN;
122
123         /* do the call now, or fail if we didn't get an opcode, or
124          * enough of an opcode.
125          */
126         if (ioctlp->inCopied < sizeof(long)) 
127             return CM_ERROR_INVAL;
128         memcpy(&opcode, ioctlp->inDatap, sizeof(long));
129         ioctlp->inDatap += sizeof(long);
130
131         osi_Log1(afsd_logp, "Ioctl opcode 0x%x", opcode);
132
133         /* check for opcode out of bounds */
134         if (opcode < 0 || opcode >= SMB_IOCTL_MAXPROCS)
135             return CM_ERROR_TOOBIG;
136
137         /* check for no such proc */
138         procp = smb_ioctlProcsp[opcode];
139         if (procp == NULL) 
140             return CM_ERROR_BADOP;
141
142         /* otherwise, make the call */
143         ioctlp->outDatap += sizeof(long);       /* reserve room for return code */
144         code = (*procp)(ioctlp, userp);
145
146         osi_Log1(afsd_logp, "Ioctl return code 0x%x", code);
147
148         /* copy in return code */
149         memcpy(ioctlp->outAllocp, &code, sizeof(long));
150     }
151     return 0;
152 }
153
154 /* called when we receive a write call.  If this is the first write call after
155  * a series of reads (or the very first call), then we start a new call.
156  * We also ensure that things are properly initialized for the start of a call.
157  */
158 void smb_IoctlPrepareWrite(smb_fid_t *fidp, smb_ioctl_t *ioctlp)
159 {
160         /* make sure the buffer(s) are allocated */
161         if (!ioctlp->inAllocp) ioctlp->inAllocp = malloc(SMB_IOCTL_MAXDATA);
162         if (!ioctlp->outAllocp) ioctlp->outAllocp = malloc(SMB_IOCTL_MAXDATA);
163
164         /* Fixes fs la problem.  We do a StrToOEM later and if this data isn't initialized we get memory issues. */
165        (void) memset(ioctlp->inAllocp, 0, SMB_IOCTL_MAXDATA);
166        (void) memset(ioctlp->outAllocp, 0, SMB_IOCTL_MAXDATA);
167
168         /* and make sure that we've reset our state for the new incoming request */
169         if (!(ioctlp->flags & SMB_IOCTLFLAG_DATAIN)) {
170                 ioctlp->inCopied = 0;
171                 ioctlp->outCopied = 0;
172                 ioctlp->inDatap = ioctlp->inAllocp;
173                 ioctlp->outDatap = ioctlp->outAllocp;
174                 ioctlp->flags |= SMB_IOCTLFLAG_DATAIN;
175         }
176 }
177
178 /* called from smb_ReceiveCoreRead when we receive a read on the ioctl fid */
179 long smb_IoctlRead(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
180         smb_packet_t *outp)
181 {
182         smb_ioctl_t *iop;
183         long count;
184         long leftToCopy;
185         char *op;
186         long code;
187         cm_user_t *userp;
188
189         iop = fidp->ioctlp;
190         count = smb_GetSMBParm(inp, 1);
191         userp = smb_GetUserFromVCP(vcp, inp);
192
193         /* Identify tree */
194     code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
195     if(code) {
196         cm_ReleaseUser(userp);
197         return CM_ERROR_NOSUCHPATH;
198     }
199
200         /* turn the connection around, if required */
201         code = smb_IoctlPrepareRead(fidp, iop, userp);
202
203         if (code) {
204                 cm_ReleaseUser(userp);
205                 return code;
206         }
207
208         leftToCopy = (long)((iop->outDatap - iop->outAllocp) - iop->outCopied);
209         if (count > leftToCopy) count = leftToCopy;
210         
211         /* now set the parms for a read of count bytes */
212         smb_SetSMBParm(outp, 0, count);
213         smb_SetSMBParm(outp, 1, 0);
214         smb_SetSMBParm(outp, 2, 0);
215         smb_SetSMBParm(outp, 3, 0);
216         smb_SetSMBParm(outp, 4, 0);
217
218         smb_SetSMBDataLength(outp, count+3);
219
220         op = smb_GetSMBData(outp, NULL);
221         *op++ = 1;
222         *op++ = (char)(count & 0xff);
223         *op++ = (char)((count >> 8) & 0xff);
224         
225         /* now copy the data into the response packet */
226         memcpy(op, iop->outCopied + iop->outAllocp, count);
227
228         /* and adjust the counters */
229         iop->outCopied += count;
230         
231         cm_ReleaseUser(userp);
232
233         return 0;
234 }
235
236 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
237  * file descriptor.
238  */
239 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
240 {
241         smb_ioctl_t *iop;
242         long count;
243         long code;
244         char *op;
245         int inDataBlockCount;
246
247         code = 0;
248         count = smb_GetSMBParm(inp, 1);
249         iop = fidp->ioctlp;
250         
251         smb_IoctlPrepareWrite(fidp, iop);
252
253         op = smb_GetSMBData(inp, NULL);
254         op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
255         
256         if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
257                 code = CM_ERROR_TOOBIG;
258                 goto done;
259         }
260         
261         /* copy data */
262         memcpy(iop->inDatap + iop->inCopied, op, count);
263         
264         /* adjust counts */
265         iop->inCopied += count;
266
267 done:
268         /* return # of bytes written */
269         if (code == 0) {
270                 smb_SetSMBParm(outp, 0, count);
271                 smb_SetSMBDataLength(outp, 0);
272         }
273
274         return code;
275 }
276
277 /* called from smb_ReceiveV3WriteX when we receive a write call on the IOCTL
278  * file descriptor.
279  */
280 long smb_IoctlV3Write(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
281 {
282         smb_ioctl_t *iop;
283         long count;
284         long code;
285         char *op;
286         int inDataBlockCount;
287
288         code = 0;
289         count = smb_GetSMBParm(inp, 10);
290         iop = fidp->ioctlp;
291         
292         smb_IoctlPrepareWrite(fidp, iop);
293
294         op = inp->data + smb_GetSMBParm(inp, 11);
295         inDataBlockCount = count;
296         
297         if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
298                 code = CM_ERROR_TOOBIG;
299                 goto done;
300         }
301         
302         /* copy data */
303         memcpy(iop->inDatap + iop->inCopied, op, count);
304         
305         /* adjust counts */
306         iop->inCopied += count;
307
308 done:
309         /* return # of bytes written */
310         if (code == 0) {
311                 smb_SetSMBParm(outp, 2, count);
312                 smb_SetSMBParm(outp, 3, 0); /* reserved */
313                 smb_SetSMBParm(outp, 4, 0); /* reserved */
314                 smb_SetSMBParm(outp, 5, 0); /* reserved */
315                 smb_SetSMBDataLength(outp, 0);
316         }
317
318         return code;
319 }
320
321
322 /* called from V3 read to handle IOCTL descriptor reads */
323 long smb_IoctlV3Read(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
324 {
325     smb_ioctl_t *iop;
326     long count;
327     long code;
328     long leftToCopy;
329     char *op;
330     cm_user_t *userp;
331     smb_user_t *uidp;
332
333     iop = fidp->ioctlp;
334     count = smb_GetSMBParm(inp, 5);
335         
336     uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
337     userp = smb_GetUserFromUID(uidp);
338     osi_assertx(userp != NULL, "null cm_user_t");
339     iop->uidp = uidp;
340     if (uidp && uidp->unp) {
341         osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
342                   uidp->userID, userp,
343                   osi_LogSaveString(afsd_logp, uidp->unp->name));
344     } else {
345         if (uidp)
346             osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
347                      uidp->userID, userp);
348         else
349             osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
350                      userp);
351     }
352
353     code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
354     if (code) {
355         if (uidp)
356             smb_ReleaseUID(uidp);
357         cm_ReleaseUser(userp);
358         return CM_ERROR_NOSUCHPATH;
359     }
360
361     code = smb_IoctlPrepareRead(fidp, iop, userp);
362     if (uidp) {
363         iop->uidp = 0;
364         smb_ReleaseUID(uidp);
365     }
366     if (code) {
367         cm_ReleaseUser(userp);
368         return code;
369     }
370
371     leftToCopy = (long)((iop->outDatap - iop->outAllocp) - iop->outCopied);
372     if (count > leftToCopy) count = leftToCopy;
373         
374     /* 0 and 1 are reserved for request chaining, were setup by our caller,
375      * and will be further filled in after we return.
376      */
377     smb_SetSMBParm(outp, 2, 0); /* remaining bytes, for pipes */
378     smb_SetSMBParm(outp, 3, 0); /* resvd */
379     smb_SetSMBParm(outp, 4, 0); /* resvd */
380     smb_SetSMBParm(outp, 5, count);     /* # of bytes we're going to read */
381     /* fill in #6 when we have all the parameters' space reserved */
382     smb_SetSMBParm(outp, 7, 0); /* resv'd */
383     smb_SetSMBParm(outp, 8, 0); /* resv'd */
384     smb_SetSMBParm(outp, 9, 0); /* resv'd */
385     smb_SetSMBParm(outp, 10, 0);        /* resv'd */
386     smb_SetSMBParm(outp, 11, 0);        /* reserved */
387
388     /* get op ptr after putting in the last parm, since otherwise we don't
389      * know where the data really is.
390      */
391     op = smb_GetSMBData(outp, NULL);
392         
393     /* now fill in offset from start of SMB header to first data byte (to op) */
394     smb_SetSMBParm(outp, 6, ((int) (op - outp->data)));
395
396     /* set the packet data length the count of the # of bytes */
397     smb_SetSMBDataLength(outp, count);
398         
399     /* now copy the data into the response packet */
400     memcpy(op, iop->outCopied + iop->outAllocp, count);
401
402     /* and adjust the counters */
403     iop->outCopied += count;
404
405     /* and cleanup things */
406     cm_ReleaseUser(userp);
407
408     return 0;
409 }       
410
411 /* called from Read Raw to handle IOCTL descriptor reads */
412 long smb_IoctlReadRaw(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
413                       smb_packet_t *outp
414                       )
415 {
416     smb_ioctl_t *iop;
417     long leftToCopy;
418     NCB *ncbp;
419     long code;
420     cm_user_t *userp;
421
422     iop = fidp->ioctlp;
423
424     userp = smb_GetUserFromVCP(vcp, inp);
425
426     /* Log the user */
427     {
428         smb_user_t *uidp;
429
430         uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
431         if (uidp && uidp->unp) {
432             osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
433                      uidp->userID, userp,
434                      osi_LogSaveString(afsd_logp, uidp->unp->name));
435         } else if (uidp) {
436             osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
437                      uidp->userID, userp);
438         } else {
439             osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
440                      userp);
441         }
442         if (uidp) 
443             smb_ReleaseUID(uidp);
444     }   
445
446     code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
447     if (code) {
448         cm_ReleaseUser(userp);
449         return CM_ERROR_NOSUCHPATH;
450     }
451
452     code = smb_IoctlPrepareRead(fidp, iop, userp);
453     if (code) {
454         cm_ReleaseUser(userp);
455         return code;
456     }
457
458     leftToCopy = (long)((iop->outDatap - iop->outAllocp) - iop->outCopied);
459
460     ncbp = outp->ncbp;
461     memset((char *)ncbp, 0, sizeof(NCB));
462
463     ncbp->ncb_length = (unsigned short) leftToCopy;
464     ncbp->ncb_lsn = (unsigned char) vcp->lsn;
465     ncbp->ncb_command = NCBSEND;
466     /*ncbp->ncb_lana_num = smb_LANadapter;*/
467     ncbp->ncb_lana_num = vcp->lana;
468
469     ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
470     code = Netbios(ncbp);
471
472     if (code != 0)
473         osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
474
475     cm_ReleaseUser(userp);
476
477     return 0;
478 }