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