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