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)
34 smb_ioctlProcsp[VIOCGETAL] = cm_IoctlGetACL;
35 smb_ioctlProcsp[VIOC_FILE_CELL_NAME] = cm_IoctlGetFileCellName;
36 smb_ioctlProcsp[VIOCSETAL] = cm_IoctlSetACL;
37 smb_ioctlProcsp[VIOC_FLUSHVOLUME] = cm_IoctlFlushVolume;
38 smb_ioctlProcsp[VIOCFLUSH] = cm_IoctlFlushFile;
39 smb_ioctlProcsp[VIOCSETVOLSTAT] = cm_IoctlSetVolumeStatus;
40 smb_ioctlProcsp[VIOCGETVOLSTAT] = cm_IoctlGetVolumeStatus;
41 smb_ioctlProcsp[VIOCWHEREIS] = cm_IoctlWhereIs;
42 smb_ioctlProcsp[VIOC_AFS_STAT_MT_PT] = cm_IoctlStatMountPoint;
43 smb_ioctlProcsp[VIOC_AFS_DELETE_MT_PT] = cm_IoctlDeleteMountPoint;
44 smb_ioctlProcsp[VIOCCKSERV] = cm_IoctlCheckServers;
45 smb_ioctlProcsp[VIOC_GAG] = cm_IoctlGag;
46 smb_ioctlProcsp[VIOCCKBACK] = cm_IoctlCheckVolumes;
47 smb_ioctlProcsp[VIOCSETCACHESIZE] = cm_IoctlSetCacheSize;
48 smb_ioctlProcsp[VIOCGETCACHEPARMS] = cm_IoctlGetCacheParms;
49 smb_ioctlProcsp[VIOCGETCELL] = cm_IoctlGetCell;
50 smb_ioctlProcsp[VIOCNEWCELL] = cm_IoctlNewCell;
51 smb_ioctlProcsp[VIOC_GET_WS_CELL] = cm_IoctlGetWsCell;
52 smb_ioctlProcsp[VIOC_AFS_SYSNAME] = cm_IoctlSysName;
53 smb_ioctlProcsp[VIOC_GETCELLSTATUS] = cm_IoctlGetCellStatus;
54 smb_ioctlProcsp[VIOC_SETCELLSTATUS] = cm_IoctlSetCellStatus;
55 smb_ioctlProcsp[VIOC_SETSPREFS] = cm_IoctlSetSPrefs;
56 smb_ioctlProcsp[VIOC_GETSPREFS] = cm_IoctlGetSPrefs;
57 smb_ioctlProcsp[VIOC_STOREBEHIND] = cm_IoctlStoreBehind;
58 smb_ioctlProcsp[VIOC_AFS_CREATE_MT_PT] = cm_IoctlCreateMountPoint;
59 smb_ioctlProcsp[VIOC_TRACECTL] = cm_IoctlTraceControl;
60 smb_ioctlProcsp[VIOCSETTOK] = cm_IoctlSetToken;
61 smb_ioctlProcsp[VIOCGETTOK] = cm_IoctlGetTokenIter;
62 smb_ioctlProcsp[VIOCNEWGETTOK] = cm_IoctlGetToken;
63 smb_ioctlProcsp[VIOCDELTOK] = cm_IoctlDelToken;
64 smb_ioctlProcsp[VIOCDELALLTOK] = cm_IoctlDelAllToken;
65 smb_ioctlProcsp[VIOC_SYMLINK] = cm_IoctlSymlink;
66 smb_ioctlProcsp[VIOC_LISTSYMLINK] = cm_IoctlListlink;
67 smb_ioctlProcsp[VIOC_DELSYMLINK] = cm_IoctlDeletelink;
68 smb_ioctlProcsp[VIOC_MAKESUBMOUNT] = cm_IoctlMakeSubmount;
69 smb_ioctlProcsp[VIOC_GETRXKCRYPT] = cm_IoctlGetRxkcrypt;
70 smb_ioctlProcsp[VIOC_SETRXKCRYPT] = cm_IoctlSetRxkcrypt;
71 smb_ioctlProcsp[VIOC_ISSYMLINK] = cm_IoctlIslink;
73 smb_ioctlProcsp[VIOC_SHUTDOWN] = cm_IoctlShutdown;
75 smb_ioctlProcsp[VIOC_TRACEMEMDUMP] = cm_IoctlMemoryDump;
76 smb_ioctlProcsp[VIOC_ISSYMLINK] = cm_IoctlIslink;
79 /* called to make a fid structure into an IOCTL fid structure */
80 void smb_SetupIoctlFid(smb_fid_t *fidp, cm_space_t *prefix)
83 cm_space_t *copyPrefix;
85 lock_ObtainMutex(&fidp->mx);
86 fidp->flags |= SMB_FID_IOCTL;
87 fidp->scp = &cm_fakeSCache;
88 if (fidp->ioctlp == NULL) {
89 iop = malloc(sizeof(*iop));
90 memset(iop, 0, sizeof(*iop));
95 copyPrefix = cm_GetSpace();
96 strcpy(copyPrefix->data, prefix->data);
97 fidp->ioctlp->prefix = copyPrefix;
99 lock_ReleaseMutex(&fidp->mx);
102 /* called when we receive a read call, does the send of the received data if
103 * this is the first read call. This is the function that actually makes the
104 * call to the ioctl code.
106 smb_IoctlPrepareRead(smb_fid_t *fidp, smb_ioctl_t *ioctlp, cm_user_t *userp)
109 smb_ioctlProc_t *procp;
112 if (ioctlp->flags & SMB_IOCTLFLAG_DATAIN) {
113 ioctlp->flags &= ~SMB_IOCTLFLAG_DATAIN;
115 /* do the call now, or fail if we didn't get an opcode, or
116 * enough of an opcode.
118 if (ioctlp->inCopied < sizeof(long)) return CM_ERROR_INVAL;
119 memcpy(&opcode, ioctlp->inDatap, sizeof(long));
120 ioctlp->inDatap += sizeof(long);
122 osi_Log1(afsd_logp, "Ioctl opcode %d", opcode);
124 /* check for opcode out of bounds */
125 if (opcode < 0 || opcode >= SMB_IOCTL_MAXPROCS)
126 return CM_ERROR_TOOBIG;
128 /* check for no such proc */
129 procp = smb_ioctlProcsp[opcode];
130 if (procp == NULL) return CM_ERROR_BADOP;
132 /* otherwise, make the call */
133 ioctlp->outDatap += sizeof(long); /* reserve room for return code */
134 code = (*procp)(ioctlp, userp);
136 osi_Log1(afsd_logp, "Ioctl return code %d", code);
138 /* copy in return code */
139 memcpy(ioctlp->outAllocp, &code, sizeof(long));
144 /* called when we receive a write call. If this is the first write call after
145 * a series of reads (or the very first call), then we start a new call.
146 * We also ensure that things are properly initialized for the start of a call.
148 void smb_IoctlPrepareWrite(smb_fid_t *fidp, smb_ioctl_t *ioctlp)
150 /* make sure the buffer(s) are allocated */
151 if (!ioctlp->inAllocp) ioctlp->inAllocp = malloc(SMB_IOCTL_MAXDATA);
152 if (!ioctlp->outAllocp) ioctlp->outAllocp = malloc(SMB_IOCTL_MAXDATA);
154 /* Fixes fs la problem. We do a StrToOEM later and if this data isn't initialized we get memory issues. */
155 (void) memset(ioctlp->inAllocp, 0, SMB_IOCTL_MAXDATA);
156 (void) memset(ioctlp->outAllocp, 0, SMB_IOCTL_MAXDATA);
158 /* and make sure that we've reset our state for the new incoming request */
159 if (!(ioctlp->flags & SMB_IOCTLFLAG_DATAIN)) {
160 ioctlp->inCopied = 0;
161 ioctlp->outCopied = 0;
162 ioctlp->inDatap = ioctlp->inAllocp;
163 ioctlp->outDatap = ioctlp->outAllocp;
164 ioctlp->flags |= SMB_IOCTLFLAG_DATAIN;
168 /* called from smb_ReceiveCoreRead when we receive a read on the ioctl fid */
169 long smb_IoctlRead(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
180 count = smb_GetSMBParm(inp, 1);
181 userp = smb_GetUser(vcp, inp);
184 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
186 cm_ReleaseUser(userp);
187 return CM_ERROR_NOSUCHPATH;
190 /* turn the connection around, if required */
191 code = smb_IoctlPrepareRead(fidp, iop, userp);
194 cm_ReleaseUser(userp);
198 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
199 if (count > leftToCopy) count = leftToCopy;
201 /* now set the parms for a read of count bytes */
202 smb_SetSMBParm(outp, 0, count);
203 smb_SetSMBParm(outp, 1, 0);
204 smb_SetSMBParm(outp, 2, 0);
205 smb_SetSMBParm(outp, 3, 0);
206 smb_SetSMBParm(outp, 4, 0);
208 smb_SetSMBDataLength(outp, count+3);
210 op = smb_GetSMBData(outp, NULL);
212 *op++ = (char)(count & 0xff);
213 *op++ = (char)((count >> 8) & 0xff);
215 /* now copy the data into the response packet */
216 memcpy(op, iop->outCopied + iop->outAllocp, count);
218 /* and adjust the counters */
219 iop->outCopied += count;
221 cm_ReleaseUser(userp);
222 smb_ReleaseFID(fidp);
227 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
230 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
236 int inDataBlockCount;
239 count = smb_GetSMBParm(inp, 1);
242 smb_IoctlPrepareWrite(fidp, iop);
244 op = smb_GetSMBData(inp, NULL);
245 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
247 if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
248 code = CM_ERROR_TOOBIG;
253 memcpy(iop->inDatap + iop->inCopied, op, count);
256 iop->inCopied += count;
259 /* return # of bytes written */
261 smb_SetSMBParm(outp, 0, count);
262 smb_SetSMBDataLength(outp, 0);
265 smb_ReleaseFID(fidp);
269 /* called from V3 read to handle IOCTL descriptor reads */
270 long smb_IoctlV3Read(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
281 count = smb_GetSMBParm(inp, 5);
283 userp = smb_GetUser(vcp, inp);
284 osi_assert(userp != 0);
286 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
288 if (uidp && uidp->unp)
289 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
291 osi_LogSaveString(afsd_logp, uidp->unp->name));
294 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
295 uidp->userID, userp);
297 osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
301 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
303 smb_ReleaseUID(uidp);
304 cm_ReleaseUser(userp);
305 smb_ReleaseFID(fidp);
306 return CM_ERROR_NOSUCHPATH;
309 code = smb_IoctlPrepareRead(fidp, iop, userp);
312 smb_ReleaseUID(uidp);
315 cm_ReleaseUser(userp);
316 smb_ReleaseFID(fidp);
320 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
321 if (count > leftToCopy) count = leftToCopy;
323 /* 0 and 1 are reserved for request chaining, were setup by our caller,
324 * and will be further filled in after we return.
326 smb_SetSMBParm(outp, 2, 0); /* remaining bytes, for pipes */
327 smb_SetSMBParm(outp, 3, 0); /* resvd */
328 smb_SetSMBParm(outp, 4, 0); /* resvd */
329 smb_SetSMBParm(outp, 5, count); /* # of bytes we're going to read */
330 /* fill in #6 when we have all the parameters' space reserved */
331 smb_SetSMBParm(outp, 7, 0); /* resv'd */
332 smb_SetSMBParm(outp, 8, 0); /* resv'd */
333 smb_SetSMBParm(outp, 9, 0); /* resv'd */
334 smb_SetSMBParm(outp, 10, 0); /* resv'd */
335 smb_SetSMBParm(outp, 11, 0); /* reserved */
337 /* get op ptr after putting in the last parm, since otherwise we don't
338 * know where the data really is.
340 op = smb_GetSMBData(outp, NULL);
342 /* now fill in offset from start of SMB header to first data byte (to op) */
343 smb_SetSMBParm(outp, 6, ((int) (op - outp->data)));
345 /* set the packet data length the count of the # of bytes */
346 smb_SetSMBDataLength(outp, count);
348 /* now copy the data into the response packet */
349 memcpy(op, iop->outCopied + iop->outAllocp, count);
351 /* and adjust the counters */
352 iop->outCopied += count;
354 /* and cleanup things */
355 cm_ReleaseUser(userp);
356 smb_ReleaseFID(fidp);
361 /* called from Read Raw to handle IOCTL descriptor reads */
362 long smb_IoctlReadRaw(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
379 osi_Log0(afsd_logp, "Failed to get raw buf for smb_IoctlReadRaw");
386 userp = smb_GetUser(vcp, inp);
391 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
392 if (uidp && uidp->unp)
393 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
395 osi_LogSaveString(afsd_logp, uidp->unp->name));
397 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
398 uidp->userID, userp);
400 osi_Log1(afsd_logp, "Ioctl no uid user %x no name",
402 if (uidp) smb_ReleaseUID(uidp);
405 code = smb_LookupTIDPath(vcp, ((smb_t *)inp)->tid, &iop->tidPathp);
407 cm_ReleaseUser(userp);
408 smb_ReleaseFID(fidp);
409 return CM_ERROR_NOSUCHPATH;
412 code = smb_IoctlPrepareRead(fidp, iop, userp);
414 cm_ReleaseUser(userp);
415 smb_ReleaseFID(fidp);
419 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
422 memset((char *)ncbp, 0, sizeof(NCB));
424 ncbp->ncb_length = (unsigned short) leftToCopy;
425 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
426 ncbp->ncb_command = NCBSEND;
427 /*ncbp->ncb_lana_num = smb_LANadapter;*/
428 ncbp->ncb_lana_num = vcp->lana;
431 ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
432 code = Netbios(ncbp);
434 dosmemput(iop->outCopied + iop->outAllocp, ncbp->ncb_length, rawBuf);
435 ncbp->ncb_buffer = rawBuf;
436 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
437 code = Netbios(ncbp, dos_ncb);
441 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
443 cm_ReleaseUser(userp);
444 smb_ReleaseFID(fidp);