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>
26 smb_ioctlProc_t *smb_ioctlProcsp[SMB_IOCTL_MAXPROCS];
28 void smb_InitIoctl(void)
30 smb_ioctlProcsp[VIOCGETAL] = cm_IoctlGetACL;
31 smb_ioctlProcsp[VIOC_FILE_CELL_NAME] = cm_IoctlGetFileCellName;
32 smb_ioctlProcsp[VIOCSETAL] = cm_IoctlSetACL;
33 smb_ioctlProcsp[VIOC_FLUSHVOLUME] = cm_IoctlFlushVolume;
34 smb_ioctlProcsp[VIOCFLUSH] = cm_IoctlFlushFile;
35 smb_ioctlProcsp[VIOCSETVOLSTAT] = cm_IoctlSetVolumeStatus;
36 smb_ioctlProcsp[VIOCGETVOLSTAT] = cm_IoctlGetVolumeStatus;
37 smb_ioctlProcsp[VIOCWHEREIS] = cm_IoctlWhereIs;
38 smb_ioctlProcsp[VIOC_AFS_STAT_MT_PT] = cm_IoctlStatMountPoint;
39 smb_ioctlProcsp[VIOC_AFS_DELETE_MT_PT] = cm_IoctlDeleteMountPoint;
40 smb_ioctlProcsp[VIOCCKSERV] = cm_IoctlCheckServers;
41 smb_ioctlProcsp[VIOC_GAG] = cm_IoctlGag;
42 smb_ioctlProcsp[VIOCCKBACK] = cm_IoctlCheckVolumes;
43 smb_ioctlProcsp[VIOCSETCACHESIZE] = cm_IoctlSetCacheSize;
44 smb_ioctlProcsp[VIOCGETCACHEPARMS] = cm_IoctlGetCacheParms;
45 smb_ioctlProcsp[VIOCGETCELL] = cm_IoctlGetCell;
46 smb_ioctlProcsp[VIOCNEWCELL] = cm_IoctlNewCell;
47 smb_ioctlProcsp[VIOC_GET_WS_CELL] = cm_IoctlGetWsCell;
48 smb_ioctlProcsp[VIOC_AFS_SYSNAME] = cm_IoctlSysName;
49 smb_ioctlProcsp[VIOC_GETCELLSTATUS] = cm_IoctlGetCellStatus;
50 smb_ioctlProcsp[VIOC_SETCELLSTATUS] = cm_IoctlSetCellStatus;
51 smb_ioctlProcsp[VIOC_SETSPREFS] = cm_IoctlSetSPrefs;
52 smb_ioctlProcsp[VIOC_GETSPREFS] = cm_IoctlGetSPrefs;
53 smb_ioctlProcsp[VIOC_STOREBEHIND] = cm_IoctlStoreBehind;
54 smb_ioctlProcsp[VIOC_AFS_CREATE_MT_PT] = cm_IoctlCreateMountPoint;
55 smb_ioctlProcsp[VIOC_TRACECTL] = cm_IoctlTraceControl;
56 smb_ioctlProcsp[VIOCSETTOK] = cm_IoctlSetToken;
57 smb_ioctlProcsp[VIOCGETTOK] = cm_IoctlGetTokenIter;
58 smb_ioctlProcsp[VIOCNEWGETTOK] = cm_IoctlGetToken;
59 smb_ioctlProcsp[VIOCDELTOK] = cm_IoctlDelToken;
60 smb_ioctlProcsp[VIOCDELALLTOK] = cm_IoctlDelAllToken;
61 smb_ioctlProcsp[VIOC_SYMLINK] = cm_IoctlSymlink;
62 smb_ioctlProcsp[VIOC_LISTSYMLINK] = cm_IoctlListlink;
63 smb_ioctlProcsp[VIOC_DELSYMLINK] = cm_IoctlDeletelink;
64 smb_ioctlProcsp[VIOC_MAKESUBMOUNT] = cm_IoctlMakeSubmount;
67 /* called to make a fid structure into an IOCTL fid structure */
68 void smb_SetupIoctlFid(smb_fid_t *fidp, cm_space_t *prefix)
71 cm_space_t *copyPrefix;
73 lock_ObtainMutex(&fidp->mx);
74 fidp->flags |= SMB_FID_IOCTL;
75 fidp->scp = &cm_fakeSCache;
76 if (fidp->ioctlp == NULL) {
77 iop = malloc(sizeof(*iop));
78 memset(iop, 0, sizeof(*iop));
82 copyPrefix = cm_GetSpace();
83 strcpy(copyPrefix->data, prefix->data);
84 fidp->ioctlp->prefix = copyPrefix;
86 lock_ReleaseMutex(&fidp->mx);
89 /* called when we receive a read call, does the send of the received data if
90 * this is the first read call. This is the function that actually makes the
91 * call to the ioctl code.
93 smb_IoctlPrepareRead(smb_fid_t *fidp, smb_ioctl_t *ioctlp, cm_user_t *userp)
96 smb_ioctlProc_t *procp;
99 if (ioctlp->flags & SMB_IOCTLFLAG_DATAIN) {
100 ioctlp->flags &= ~SMB_IOCTLFLAG_DATAIN;
102 /* do the call now, or fail if we didn't get an opcode, or
103 * enough of an opcode.
105 if (ioctlp->inCopied < sizeof(long)) return CM_ERROR_INVAL;
106 memcpy(&opcode, ioctlp->inDatap, sizeof(long));
107 ioctlp->inDatap += sizeof(long);
109 osi_Log1(afsd_logp, "Ioctl opcode %d", opcode);
111 /* check for opcode out of bounds */
112 if (opcode < 0 || opcode >= SMB_IOCTL_MAXPROCS)
113 return CM_ERROR_TOOBIG;
115 /* check for no such proc */
116 procp = smb_ioctlProcsp[opcode];
117 if (procp == NULL) return CM_ERROR_BADOP;
119 /* otherwise, make the call */
120 ioctlp->outDatap += sizeof(long); /* reserve room for return code */
121 code = (*procp)(ioctlp, userp);
123 osi_Log1(afsd_logp, "Ioctl return code %d", code);
125 /* copy in return code */
126 memcpy(ioctlp->outAllocp, &code, sizeof(long));
131 /* called when we receive a write call. If this is the first write call after
132 * a series of reads (or the very first call), then we start a new call.
133 * We also ensure that things are properly initialized for the start of a call.
135 void smb_IoctlPrepareWrite(smb_fid_t *fidp, smb_ioctl_t *ioctlp)
137 /* make sure the buffer(s) are allocated */
138 if (!ioctlp->inAllocp) ioctlp->inAllocp = malloc(SMB_IOCTL_MAXDATA);
139 if (!ioctlp->outAllocp) ioctlp->outAllocp = malloc(SMB_IOCTL_MAXDATA);
141 /* and make sure that we've reset our state for the new incoming request */
142 if (!(ioctlp->flags & SMB_IOCTLFLAG_DATAIN)) {
143 ioctlp->inCopied = 0;
144 ioctlp->outCopied = 0;
145 ioctlp->inDatap = ioctlp->inAllocp;
146 ioctlp->outDatap = ioctlp->outAllocp;
147 ioctlp->flags |= SMB_IOCTLFLAG_DATAIN;
151 /* called from smb_ReceiveCoreRead when we receive a read on the ioctl fid */
152 long smb_IoctlRead(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
163 count = smb_GetSMBParm(inp, 1);
164 userp = smb_GetUser(vcp, inp);
167 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
169 /* turn the connection around, if required */
170 code = smb_IoctlPrepareRead(fidp, iop, userp);
173 cm_ReleaseUser(userp);
177 if (iop->flags & SMB_IOCTLFLAG_LOGON) {
178 vcp->logonDLLUser = userp;
179 userp->flags |= CM_USERFLAG_WASLOGON;
182 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
183 if (count > leftToCopy) count = leftToCopy;
185 /* now set the parms for a read of count bytes */
186 smb_SetSMBParm(outp, 0, count);
187 smb_SetSMBParm(outp, 1, 0);
188 smb_SetSMBParm(outp, 2, 0);
189 smb_SetSMBParm(outp, 3, 0);
190 smb_SetSMBParm(outp, 4, 0);
192 smb_SetSMBDataLength(outp, count+3);
194 op = smb_GetSMBData(outp, NULL);
196 *op++ = count & 0xff;
197 *op++ = (count >> 8) & 0xff;
199 /* now copy the data into the response packet */
200 memcpy(op, iop->outCopied + iop->outAllocp, count);
202 /* and adjust the counters */
203 iop->outCopied += count;
205 cm_ReleaseUser(userp);
206 smb_ReleaseFID(fidp);
211 /* called from smb_ReceiveCoreWrite when we receive a write call on the IOCTL
214 long smb_IoctlWrite(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
220 long inDataBlockCount;
223 count = smb_GetSMBParm(inp, 1);
226 smb_IoctlPrepareWrite(fidp, iop);
228 op = smb_GetSMBData(inp, NULL);
229 op = smb_ParseDataBlock(op, NULL, &inDataBlockCount);
231 if (count + iop->inCopied > SMB_IOCTL_MAXDATA) {
232 code = CM_ERROR_TOOBIG;
237 memcpy(iop->inDatap + iop->inCopied, op, count);
240 iop->inCopied += count;
243 /* return # of bytes written */
245 smb_SetSMBParm(outp, 0, count);
246 smb_SetSMBDataLength(outp, 0);
249 smb_ReleaseFID(fidp);
253 /* called from V3 read to handle IOCTL descriptor reads */
254 long smb_IoctlV3Read(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp, smb_packet_t *outp)
264 count = smb_GetSMBParm(inp, 5);
266 userp = smb_GetUser(vcp, inp);
271 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
272 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
274 osi_LogSaveString(afsd_logp, uidp->name));
275 smb_ReleaseUID(uidp);
278 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
280 code = smb_IoctlPrepareRead(fidp, iop, userp);
282 cm_ReleaseUser(userp);
283 smb_ReleaseFID(fidp);
287 if (iop->flags & SMB_IOCTLFLAG_LOGON) {
288 vcp->logonDLLUser = userp;
289 userp->flags |= CM_USERFLAG_WASLOGON;
292 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
293 if (count > leftToCopy) count = leftToCopy;
295 /* 0 and 1 are reserved for request chaining, were setup by our caller,
296 * and will be further filled in after we return.
298 smb_SetSMBParm(outp, 2, 0); /* remaining bytes, for pipes */
299 smb_SetSMBParm(outp, 3, 0); /* resvd */
300 smb_SetSMBParm(outp, 4, 0); /* resvd */
301 smb_SetSMBParm(outp, 5, count); /* # of bytes we're going to read */
302 /* fill in #6 when we have all the parameters' space reserved */
303 smb_SetSMBParm(outp, 7, 0); /* resv'd */
304 smb_SetSMBParm(outp, 8, 0); /* resv'd */
305 smb_SetSMBParm(outp, 9, 0); /* resv'd */
306 smb_SetSMBParm(outp, 10, 0); /* resv'd */
307 smb_SetSMBParm(outp, 11, 0); /* reserved */
309 /* get op ptr after putting in the last parm, since otherwise we don't
310 * know where the data really is.
312 op = smb_GetSMBData(outp, NULL);
314 /* now fill in offset from start of SMB header to first data byte (to op) */
315 smb_SetSMBParm(outp, 6, ((int) (op - outp->data)));
317 /* set the packet data length the count of the # of bytes */
318 smb_SetSMBDataLength(outp, count);
320 /* now copy the data into the response packet */
321 memcpy(op, iop->outCopied + iop->outAllocp, count);
323 /* and adjust the counters */
324 iop->outCopied += count;
326 /* and cleanup things */
327 cm_ReleaseUser(userp);
328 smb_ReleaseFID(fidp);
333 /* called from Read Raw to handle IOCTL descriptor reads */
334 long smb_IoctlReadRaw(smb_fid_t *fidp, smb_vc_t *vcp, smb_packet_t *inp,
345 userp = smb_GetUser(vcp, inp);
350 uidp = smb_FindUID(vcp, ((smb_t *)inp)->uid, 0);
351 osi_Log3(afsd_logp, "Ioctl uid %d user %x name %s",
353 osi_LogSaveString(afsd_logp, uidp->name));
354 smb_ReleaseUID(uidp);
357 iop->tidPathp = smb_GetTIDPath(vcp, ((smb_t *)inp)->tid);
359 code = smb_IoctlPrepareRead(fidp, iop, userp);
361 cm_ReleaseUser(userp);
362 smb_ReleaseFID(fidp);
366 if (iop->flags & SMB_IOCTLFLAG_LOGON) {
367 vcp->logonDLLUser = userp;
368 userp->flags |= CM_USERFLAG_WASLOGON;
371 leftToCopy = (iop->outDatap - iop->outAllocp) - iop->outCopied;
374 memset((char *)ncbp, 0, sizeof(NCB));
376 ncbp->ncb_length = (unsigned short) leftToCopy;
377 ncbp->ncb_lsn = (unsigned char) vcp->lsn;
378 ncbp->ncb_command = NCBSEND;
379 ncbp->ncb_buffer = iop->outCopied + iop->outAllocp;
381 code = Netbios(ncbp);
383 osi_Log1(afsd_logp, "ReadRaw send failure code %d", code);
385 cm_ReleaseUser(userp);
386 smb_ReleaseFID(fidp);