2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
28 smb_ioctlProc_t *smb_ioctlProcsp[SMB_IOCTL_MAXPROCS];
30 /*extern unsigned char smb_LANadapter;*/
32 void smb_InitIoctl(void)
35 for (i=0; i<SMB_IOCTL_MAXPROCS; i++)
36 smb_ioctlProcsp[i] = NULL;
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;
77 smb_ioctlProcsp[VIOC_SHUTDOWN] = cm_IoctlShutdown;
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;
86 /* called to make a fid structure into an IOCTL fid structure */
87 void smb_SetupIoctlFid(smb_fid_t *fidp, cm_space_t *prefix)
90 cm_space_t *copyPrefix;
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));
103 copyPrefix = cm_GetSpace();
104 strcpy(copyPrefix->data, prefix->data);
105 fidp->ioctlp->prefix = copyPrefix;
107 lock_ReleaseMutex(&fidp->mx);
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.
114 smb_IoctlPrepareRead(smb_fid_t *fidp, smb_ioctl_t *ioctlp, cm_user_t *userp)
117 smb_ioctlProc_t *procp;
120 if (ioctlp->flags & SMB_IOCTLFLAG_DATAIN) {
121 ioctlp->flags &= ~SMB_IOCTLFLAG_DATAIN;
123 /* do the call now, or fail if we didn't get an opcode, or
124 * enough of an opcode.
126 if (ioctlp->inCopied < sizeof(long))
127 return CM_ERROR_INVAL;
128 memcpy(&opcode, ioctlp->inDatap, sizeof(long));
129 ioctlp->inDatap += sizeof(long);
131 osi_Log1(afsd_logp, "Ioctl opcode 0x%x", opcode);
133 /* check for opcode out of bounds */
134 if (opcode < 0 || opcode >= SMB_IOCTL_MAXPROCS)
135 return CM_ERROR_TOOBIG;
137 /* check for no such proc */
138 procp = smb_ioctlProcsp[opcode];
140 return CM_ERROR_BADOP;
142 /* otherwise, make the call */
143 ioctlp->outDatap += sizeof(long); /* reserve room for return code */
144 code = (*procp)(ioctlp, userp);
146 osi_Log1(afsd_logp, "Ioctl return code 0x%x", code);
148 /* copy in return code */
149 memcpy(ioctlp->outAllocp, &code, sizeof(long));
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.
158 void smb_IoctlPrepareWrite(smb_fid_t *fidp, smb_ioctl_t *ioctlp)
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);
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);
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;
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,
190 count = smb_GetSMBParm(inp, 1);
191 userp = smb_GetUser(vcp, inp);
194 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
196 cm_ReleaseUser(userp);
197 return CM_ERROR_NOSUCHPATH;
200 /* turn the connection around, if required */
201 code = smb_IoctlPrepareRead(fidp, iop, userp);
204 cm_ReleaseUser(userp);
208 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
209 if (count > leftToCopy) count = leftToCopy;
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);
218 smb_SetSMBDataLength(outp, count+3);
220 op = smb_GetSMBData(outp, NULL);
222 *op++ = (char)(count & 0xff);
223 *op++ = (char)((count >> 8) & 0xff);
225 /* now copy the data into the response packet */
226 memcpy(op, iop->outCopied + iop->outAllocp, count);
228 /* and adjust the counters */
229 iop->outCopied += count;
231 cm_ReleaseUser(userp);
232 smb_ReleaseFID(fidp);
237 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
240 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
246 int inDataBlockCount;
249 count = smb_GetSMBParm(inp, 1);
252 smb_IoctlPrepareWrite(fidp, iop);
254 op = smb_GetSMBData(inp, NULL);
255 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
257 if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
258 code = CM_ERROR_TOOBIG;
263 memcpy(iop->inDatap + iop->inCopied, op, count);
266 iop->inCopied += count;
269 /* return # of bytes written */
271 smb_SetSMBParm(outp, 0, count);
272 smb_SetSMBDataLength(outp, 0);
275 smb_ReleaseFID(fidp);
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)
291 count = smb_GetSMBParm(inp, 5);
293 userp = smb_GetUser(vcp, inp);
294 osi_assert(userp != 0);
296 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
298 if (uidp && uidp->unp)
299 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
301 osi_LogSaveString(afsd_logp, uidp->unp->name));
304 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
305 uidp->userID, userp);
307 osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
311 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
313 smb_ReleaseUID(uidp);
314 cm_ReleaseUser(userp);
315 smb_ReleaseFID(fidp);
316 return CM_ERROR_NOSUCHPATH;
319 code = smb_IoctlPrepareRead(fidp, iop, userp);
322 smb_ReleaseUID(uidp);
325 cm_ReleaseUser(userp);
326 smb_ReleaseFID(fidp);
330 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
331 if (count > leftToCopy) count = leftToCopy;
333 /* 0 and 1 are reserved for request chaining, were setup by our caller,
334 * and will be further filled in after we return.
336 smb_SetSMBParm(outp, 2, 0); /* remaining bytes, for pipes */
337 smb_SetSMBParm(outp, 3, 0); /* resvd */
338 smb_SetSMBParm(outp, 4, 0); /* resvd */
339 smb_SetSMBParm(outp, 5, count); /* # of bytes we're going to read */
340 /* fill in #6 when we have all the parameters' space reserved */
341 smb_SetSMBParm(outp, 7, 0); /* resv'd */
342 smb_SetSMBParm(outp, 8, 0); /* resv'd */
343 smb_SetSMBParm(outp, 9, 0); /* resv'd */
344 smb_SetSMBParm(outp, 10, 0); /* resv'd */
345 smb_SetSMBParm(outp, 11, 0); /* reserved */
347 /* get op ptr after putting in the last parm, since otherwise we don't
348 * know where the data really is.
350 op = smb_GetSMBData(outp, NULL);
352 /* now fill in offset from start of SMB header to first data byte (to op) */
353 smb_SetSMBParm(outp, 6, ((int) (op - outp->data)));
355 /* set the packet data length the count of the # of bytes */
356 smb_SetSMBDataLength(outp, count);
358 /* now copy the data into the response packet */
359 memcpy(op, iop->outCopied + iop->outAllocp, count);
361 /* and adjust the counters */
362 iop->outCopied += count;
364 /* and cleanup things */
365 cm_ReleaseUser(userp);
366 smb_ReleaseFID(fidp);
371 /* called from Read Raw to handle IOCTL descriptor reads */
372 long smb_IoctlReadRaw(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
389 osi_Log0(afsd_logp, "Failed to get raw buf for smb_IoctlReadRaw");
396 userp = smb_GetUser(vcp, inp);
401 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
402 if (uidp && uidp->unp)
403 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
405 osi_LogSaveString(afsd_logp, uidp->unp->name));
407 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
408 uidp->userID, userp);
410 osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
412 if (uidp) smb_ReleaseUID(uidp);
415 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
417 cm_ReleaseUser(userp);
418 smb_ReleaseFID(fidp);
419 return CM_ERROR_NOSUCHPATH;
422 code = smb_IoctlPrepareRead(fidp, iop, userp);
424 cm_ReleaseUser(userp);
425 smb_ReleaseFID(fidp);
429 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
432 memset((char *)ncbp, 0, sizeof(NCB));
434 ncbp->ncb_length = (unsigned short) leftToCopy;
435 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
436 ncbp->ncb_command = NCBSEND;
437 /*ncbp->ncb_lana_num = smb_LANadapter;*/
438 ncbp->ncb_lana_num = vcp->lana;
441 ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
442 code = Netbios(ncbp);
444 dosmemput(iop->outCopied + iop->outAllocp, ncbp->ncb_length, rawBuf);
445 ncbp->ncb_buffer = rawBuf;
446 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
447 code = Netbios(ncbp, dos_ncb);
451 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
453 cm_ReleaseUser(userp);
454 smb_ReleaseFID(fidp);