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;*/
31 extern LANA_ENUM lana_list;
33 void smb_InitIoctl(void)
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;
73 smb_ioctlProcsp[VIOC_SHUTDOWN] = cm_IoctlShutdown;
77 /* called to make a fid structure into an IOCTL fid structure */
78 void smb_SetupIoctlFid(smb_fid_t *fidp, cm_space_t *prefix)
81 cm_space_t *copyPrefix;
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));
93 copyPrefix = cm_GetSpace();
94 strcpy(copyPrefix->data, prefix->data);
95 fidp->ioctlp->prefix = copyPrefix;
97 lock_ReleaseMutex(&fidp->mx);
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.
104 smb_IoctlPrepareRead(smb_fid_t *fidp, smb_ioctl_t *ioctlp, cm_user_t *userp)
107 smb_ioctlProc_t *procp;
110 if (ioctlp->flags & SMB_IOCTLFLAG_DATAIN) {
111 ioctlp->flags &= ~SMB_IOCTLFLAG_DATAIN;
113 /* do the call now, or fail if we didn't get an opcode, or
114 * enough of an opcode.
116 if (ioctlp->inCopied < sizeof(long)) return CM_ERROR_INVAL;
117 memcpy(&opcode, ioctlp->inDatap, sizeof(long));
118 ioctlp->inDatap += sizeof(long);
120 osi_Log1(afsd_logp, "Ioctl opcode %d", opcode);
122 /* check for opcode out of bounds */
123 if (opcode < 0 || opcode >= SMB_IOCTL_MAXPROCS)
124 return CM_ERROR_TOOBIG;
126 /* check for no such proc */
127 procp = smb_ioctlProcsp[opcode];
128 if (procp == NULL) return CM_ERROR_BADOP;
130 /* otherwise, make the call */
131 ioctlp->outDatap += sizeof(long); /* reserve room for return code */
132 code = (*procp)(ioctlp, userp);
134 osi_Log1(afsd_logp, "Ioctl return code %d", code);
136 /* copy in return code */
137 memcpy(ioctlp->outAllocp, &code, sizeof(long));
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.
146 void smb_IoctlPrepareWrite(smb_fid_t *fidp, smb_ioctl_t *ioctlp)
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);
152 /* and make sure that we've reset our state for the new incoming request */
153 if (!(ioctlp->flags & SMB_IOCTLFLAG_DATAIN)) {
154 ioctlp->inCopied = 0;
155 ioctlp->outCopied = 0;
156 ioctlp->inDatap = ioctlp->inAllocp;
157 ioctlp->outDatap = ioctlp->outAllocp;
158 ioctlp->flags |= SMB_IOCTLFLAG_DATAIN;
162 /* called from smb_ReceiveCoreRead when we receive a read on the ioctl fid */
163 long smb_IoctlRead(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
174 count = smb_GetSMBParm(inp, 1);
175 userp = smb_GetUser(vcp, inp);
178 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
180 /* turn the connection around, if required */
181 code = smb_IoctlPrepareRead(fidp, iop, userp);
184 cm_ReleaseUser(userp);
188 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
189 if (count > leftToCopy) count = leftToCopy;
191 /* now set the parms for a read of count bytes */
192 smb_SetSMBParm(outp, 0, count);
193 smb_SetSMBParm(outp, 1, 0);
194 smb_SetSMBParm(outp, 2, 0);
195 smb_SetSMBParm(outp, 3, 0);
196 smb_SetSMBParm(outp, 4, 0);
198 smb_SetSMBDataLength(outp, count+3);
200 op = smb_GetSMBData(outp, NULL);
202 *op++ = count & 0xff;
203 *op++ = (count >> 8) & 0xff;
205 /* now copy the data into the response packet */
206 memcpy(op, iop->outCopied + iop->outAllocp, count);
208 /* and adjust the counters */
209 iop->outCopied += count;
211 cm_ReleaseUser(userp);
212 smb_ReleaseFID(fidp);
217 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
220 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
226 int inDataBlockCount;
229 count = smb_GetSMBParm(inp, 1);
232 smb_IoctlPrepareWrite(fidp, iop);
234 op = smb_GetSMBData(inp, NULL);
235 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
237 if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
238 code = CM_ERROR_TOOBIG;
243 memcpy(iop->inDatap + iop->inCopied, op, count);
246 iop->inCopied += count;
249 /* return # of bytes written */
251 smb_SetSMBParm(outp, 0, count);
252 smb_SetSMBDataLength(outp, 0);
255 smb_ReleaseFID(fidp);
259 /* called from V3 read to handle IOCTL descriptor reads */
260 long smb_IoctlV3Read(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
270 count = smb_GetSMBParm(inp, 5);
272 userp = smb_GetUser(vcp, inp);
277 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
278 if (uidp && uidp->unp)
279 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
281 osi_LogSaveString(afsd_logp, uidp->unp->name));
283 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
284 uidp->userID, userp);
285 smb_ReleaseUID(uidp);
288 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
290 code = smb_IoctlPrepareRead(fidp, iop, userp);
292 cm_ReleaseUser(userp);
293 smb_ReleaseFID(fidp);
297 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
298 if (count > leftToCopy) count = leftToCopy;
300 /* 0 and 1 are reserved for request chaining, were setup by our caller,
301 * and will be further filled in after we return.
303 smb_SetSMBParm(outp, 2, 0); /* remaining bytes, for pipes */
304 smb_SetSMBParm(outp, 3, 0); /* resvd */
305 smb_SetSMBParm(outp, 4, 0); /* resvd */
306 smb_SetSMBParm(outp, 5, count); /* # of bytes we're going to read */
307 /* fill in #6 when we have all the parameters' space reserved */
308 smb_SetSMBParm(outp, 7, 0); /* resv'd */
309 smb_SetSMBParm(outp, 8, 0); /* resv'd */
310 smb_SetSMBParm(outp, 9, 0); /* resv'd */
311 smb_SetSMBParm(outp, 10, 0); /* resv'd */
312 smb_SetSMBParm(outp, 11, 0); /* reserved */
314 /* get op ptr after putting in the last parm, since otherwise we don't
315 * know where the data really is.
317 op = smb_GetSMBData(outp, NULL);
319 /* now fill in offset from start of SMB header to first data byte (to op) */
320 smb_SetSMBParm(outp, 6, ((int) (op - outp->data)));
322 /* set the packet data length the count of the # of bytes */
323 smb_SetSMBDataLength(outp, count);
325 /* now copy the data into the response packet */
326 memcpy(op, iop->outCopied + iop->outAllocp, count);
328 /* and adjust the counters */
329 iop->outCopied += count;
331 /* and cleanup things */
332 cm_ReleaseUser(userp);
333 smb_ReleaseFID(fidp);
338 /* called from Read Raw to handle IOCTL descriptor reads */
339 long smb_IoctlReadRaw(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
356 osi_Log0(afsd_logp, "Failed to get raw buf for smb_IoctlReadRaw");
363 userp = smb_GetUser(vcp, inp);
368 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
369 if (uidp && uidp->unp)
370 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
372 osi_LogSaveString(afsd_logp, uidp->unp->name));
374 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
375 uidp->userID, userp);
376 smb_ReleaseUID(uidp);
379 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
381 code = smb_IoctlPrepareRead(fidp, iop, userp);
383 cm_ReleaseUser(userp);
384 smb_ReleaseFID(fidp);
388 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
391 memset((char *)ncbp, 0, sizeof(NCB));
393 ncbp->ncb_length = (unsigned short) leftToCopy;
394 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
395 ncbp->ncb_command = NCBSEND;
396 /*ncbp->ncb_lana_num = smb_LANadapter;*/
397 ncbp->ncb_lana_num = vcp->lana;
400 ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
401 code = Netbios(ncbp);
403 dosmemput(iop->outCopied + iop->outAllocp, ncbp->ncb_length, rawBuf);
404 ncbp->ncb_buffer = rawBuf;
405 dos_ncb = ((smb_ncb_t *)ncbp)->dos_ncb;
406 code = Netbios(ncbp, dos_ncb);
410 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
412 cm_ReleaseUser(userp);
413 smb_ReleaseFID(fidp);