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 /* 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);
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;
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,
178 count = smb_GetSMBParm(inp, 1);
179 userp = smb_GetUser(vcp, inp);
182 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
184 /* turn the connection around, if required */
185 code = smb_IoctlPrepareRead(fidp, iop, userp);
188 cm_ReleaseUser(userp);
192 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
193 if (count > leftToCopy) count = leftToCopy;
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);
202 smb_SetSMBDataLength(outp, count+3);
204 op = smb_GetSMBData(outp, NULL);
206 *op++ = count & 0xff;
207 *op++ = (count >> 8) & 0xff;
209 /* now copy the data into the response packet */
210 memcpy(op, iop->outCopied + iop->outAllocp, count);
212 /* and adjust the counters */
213 iop->outCopied += count;
215 cm_ReleaseUser(userp);
216 smb_ReleaseFID(fidp);
221 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
224 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
230 int inDataBlockCount;
233 count = smb_GetSMBParm(inp, 1);
236 smb_IoctlPrepareWrite(fidp, iop);
238 op = smb_GetSMBData(inp, NULL);
239 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
241 if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
242 code = CM_ERROR_TOOBIG;
247 memcpy(iop->inDatap + iop->inCopied, op, count);
250 iop->inCopied += count;
253 /* return # of bytes written */
255 smb_SetSMBParm(outp, 0, count);
256 smb_SetSMBDataLength(outp, 0);
259 smb_ReleaseFID(fidp);
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)
274 count = smb_GetSMBParm(inp, 5);
276 userp = smb_GetUser(vcp, inp);
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",
285 osi_LogSaveString(afsd_logp, uidp->unp->name));
287 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
288 uidp->userID, userp);
289 smb_ReleaseUID(uidp);
292 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
294 code = smb_IoctlPrepareRead(fidp, iop, userp);
296 cm_ReleaseUser(userp);
297 smb_ReleaseFID(fidp);
301 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
302 if (count > leftToCopy) count = leftToCopy;
304 /* 0 and 1 are reserved for request chaining, were setup by our caller,
305 * and will be further filled in after we return.
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 */
318 /* get op ptr after putting in the last parm, since otherwise we don't
319 * know where the data really is.
321 op = smb_GetSMBData(outp, NULL);
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)));
326 /* set the packet data length the count of the # of bytes */
327 smb_SetSMBDataLength(outp, count);
329 /* now copy the data into the response packet */
330 memcpy(op, iop->outCopied + iop->outAllocp, count);
332 /* and adjust the counters */
333 iop->outCopied += count;
335 /* and cleanup things */
336 cm_ReleaseUser(userp);
337 smb_ReleaseFID(fidp);
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,
360 osi_Log0(afsd_logp, "Failed to get raw buf for smb_IoctlReadRaw");
367 userp = smb_GetUser(vcp, inp);
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",
376 osi_LogSaveString(afsd_logp, uidp->unp->name));
378 osi_Log2(afsd_logp, "Ioctl uid %d user %x no name",
379 uidp->userID, userp);
380 smb_ReleaseUID(uidp);
383 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
385 code = smb_IoctlPrepareRead(fidp, iop, userp);
387 cm_ReleaseUser(userp);
388 smb_ReleaseFID(fidp);
392 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
395 memset((char *)ncbp, 0, sizeof(NCB));
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;
404 ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
405 code = Netbios(ncbp);
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);
414 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
416 cm_ReleaseUser(userp);
417 smb_ReleaseFID(fidp);