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>
16 #include <sys/socket.h>
28 #include "afsd_init.h"
31 #include "cm_server.h"
47 /* Copied from afs_tokens.h */
48 #define PIOCTL_LOGON 0x1
51 osi_mutex_t cm_Afsdsbmt_Lock;
53 extern afs_int32 cryptall;
54 extern char cm_NetbiosName[];
56 void cm_InitIoctl(void)
58 lock_InitializeMutex(&cm_Afsdsbmt_Lock, "AFSDSBMT.INI Access Lock");
61 long cm_FlushFile(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
65 lock_ObtainWrite(&scp->bufCreateLock);
66 code = buf_FlushCleanPages(scp, userp, reqp);
68 lock_ObtainMutex(&scp->mx);
69 scp->cbServerp = NULL;
71 lock_ReleaseMutex(&scp->mx);
73 lock_ReleaseWrite(&scp->bufCreateLock);
80 * cm_ResetACLCache -- invalidate ACL info for a user that has just
81 * obtained or lost tokens
83 void cm_ResetACLCache(cm_user_t *userp)
88 lock_ObtainWrite(&cm_scacheLock);
89 for (hash=0; hash < cm_hashTableSize; hash++) {
90 for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
92 lock_ReleaseWrite(&cm_scacheLock);
93 lock_ObtainMutex(&scp->mx);
94 cm_InvalidateACLUser(scp, userp);
95 lock_ReleaseMutex(&scp->mx);
96 lock_ObtainWrite(&cm_scacheLock);
100 lock_ReleaseWrite(&cm_scacheLock);
104 * TranslateExtendedChars - This is a fix for TR 54482.
106 * If an extended character (80 - FF) is entered into a file
107 * or directory name in Windows, the character is translated
108 * into the OEM character map before being passed to us. Why
109 * this occurs is unknown. Our pioctl functions must match
110 * this translation for paths given via our own commands (like
111 * fs). If we do not do this, then we will try to perform an
112 * operation on a non-translated path, which we will fail to
113 * find, since the path was created with the translated chars.
114 * This function performs the required translation.
116 void TranslateExtendedChars(char *str)
129 while (*p) *p++ &= 0x7f; /* turn off high bit; probably not right */
133 /* parse the passed-in file name and do a namei on it. If we fail,
134 * return an error code, otherwise return the vnode located in *scpp.
136 long cm_ParseIoctlPath(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
140 cm_scache_t *substRootp;
141 char * relativePath = ioctlp->inDatap;
143 /* This is usually the file name, but for StatMountPoint it is the path. */
144 /* ioctlp->inDatap can be either of the form:
147 * \\netbios-name\submount\path\.
148 * \\netbios-name\submount\path\file
150 TranslateExtendedChars(relativePath);
152 if (relativePath[0] == relativePath[1] &&
153 relativePath[1] == '\\' &&
154 !_strnicmp(cm_NetbiosName,relativePath+2,strlen(cm_NetbiosName)))
160 /* We may have found a UNC path.
161 * If the first component is the NetbiosName,
162 * then throw out the second component (the submount)
163 * since it had better expand into the value of ioctl->tidPathp
166 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
167 if ( !_strnicmp("all", p, 3) )
170 for (i = 0; *p && *p != '\\'; i++,p++ ) {
173 p++; /* skip past trailing slash */
174 shareName[i] = 0; /* terminate string */
176 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
178 /* we found a sharename, therefore use the resulting path */
179 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
180 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
181 userp, sharePath, reqp, &substRootp);
183 if (code) return code;
185 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
186 userp, NULL, reqp, scpp);
187 if (code) return code;
189 /* otherwise, treat the name as a cellname mounted off the afs root.
190 * This requires that we reconstruct the shareName string with
191 * leading and trailing slashes.
193 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
194 if ( !_strnicmp("all", p, 3) )
198 for (i = 1; *p && *p != '\\'; i++,p++ ) {
201 p++; /* skip past trailing slash */
202 shareName[i++] = '/'; /* add trailing slash */
203 shareName[i] = 0; /* terminate string */
206 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
207 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
208 userp, shareName, reqp, &substRootp);
209 if (code) return code;
211 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
212 userp, NULL, reqp, scpp);
213 if (code) return code;
216 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
217 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
218 userp, ioctlp->tidPathp, reqp, &substRootp);
219 if (code) return code;
221 code = cm_NameI(substRootp, relativePath, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
222 userp, NULL, reqp, scpp);
223 if (code) return code;
226 /* # of bytes of path */
227 code = strlen(ioctlp->inDatap) + 1;
228 ioctlp->inDatap += code;
230 /* This is usually nothing, but for StatMountPoint it is the file name. */
231 TranslateExtendedChars(ioctlp->inDatap);
233 /* and return success */
237 void cm_SkipIoctlPath(smb_ioctl_t *ioctlp)
241 temp = strlen(ioctlp->inDatap) + 1;
242 ioctlp->inDatap += temp;
246 /* format the specified path to look like "/afs/<cellname>/usr", by
247 * adding "/afs" (if necessary) in front, changing any \'s to /'s, and
248 * removing any trailing "/"'s. One weirdo caveat: "/afs" will be
249 * intentionally returned as "/afs/"--this makes submount manipulation
250 * easier (because we can always jump past the initial "/afs" to find
251 * the AFS path that should be written into afsdsbmt.ini).
253 void cm_NormalizeAfsPath (char *outpathp, char *inpathp)
256 char bslash_mountRoot[256];
258 strncpy(bslash_mountRoot, cm_mountRoot, sizeof(bslash_mountRoot) - 1);
259 bslash_mountRoot[0] = '\\';
261 if (!strnicmp (inpathp, cm_mountRoot, strlen(cm_mountRoot)))
262 lstrcpy (outpathp, inpathp);
263 else if (!strnicmp (inpathp, bslash_mountRoot, strlen(bslash_mountRoot)))
264 lstrcpy (outpathp, inpathp);
265 else if ((inpathp[0] == '/') || (inpathp[0] == '\\'))
266 sprintf (outpathp, "%s%s", cm_mountRoot, inpathp);
267 else // inpathp looks like "<cell>/usr"
268 sprintf (outpathp, "%s/%s", cm_mountRoot, inpathp);
270 for (cp = outpathp; *cp != 0; ++cp) {
275 if (strlen(outpathp) && (outpathp[strlen(outpathp)-1] == '/')) {
276 outpathp[strlen(outpathp)-1] = 0;
279 if (!strcmpi (outpathp, cm_mountRoot)) {
280 strcpy (outpathp, cm_mountRoot);
284 /* parse the passed-in file name and do a namei on its parent. If we fail,
285 * return an error code, otherwise return the vnode located in *scpp.
287 long cm_ParseIoctlParent(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
288 cm_scache_t **scpp, char *leafp)
293 cm_scache_t *substRootp;
295 strcpy(tbuffer, ioctlp->inDatap);
296 tp = strrchr(tbuffer, '\\');
297 jp = strrchr(tbuffer, '/');
300 else if (jp && (tp - tbuffer) < (jp - tbuffer))
303 strcpy(tbuffer, "\\");
305 strcpy(leafp, ioctlp->inDatap);
313 if (tbuffer[0] == tbuffer[1] &&
314 tbuffer[1] == '\\' &&
315 !_strnicmp(cm_NetbiosName,tbuffer+2,strlen(cm_NetbiosName)))
321 /* We may have found a UNC path.
322 * If the first component is the NetbiosName,
323 * then throw out the second component (the submount)
324 * since it had better expand into the value of ioctl->tidPathp
327 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
328 if ( !_strnicmp("all", p, 3) )
331 for (i = 0; *p && *p != '\\'; i++,p++ ) {
334 p++; /* skip past trailing slash */
335 shareName[i] = 0; /* terminate string */
337 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
339 /* we found a sharename, therefore use the resulting path */
340 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
341 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
342 userp, sharePath, reqp, &substRootp);
344 if (code) return code;
346 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
347 userp, NULL, reqp, scpp);
348 if (code) return code;
350 /* otherwise, treat the name as a cellname mounted off the afs root.
351 * This requires that we reconstruct the shareName string with
352 * leading and trailing slashes.
354 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
355 if ( !_strnicmp("all", p, 3) )
359 for (i = 1; *p && *p != '\\'; i++,p++ ) {
362 p++; /* skip past trailing slash */
363 shareName[i++] = '/'; /* add trailing slash */
364 shareName[i] = 0; /* terminate string */
366 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
367 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
368 userp, shareName, reqp, &substRootp);
369 if (code) return code;
371 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
372 userp, NULL, reqp, scpp);
373 if (code) return code;
376 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
377 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
378 userp, ioctlp->tidPathp, reqp, &substRootp);
379 if (code) return code;
381 code = cm_NameI(substRootp, tbuffer, CM_FLAG_FOLLOW,
382 userp, NULL, reqp, scpp);
383 if (code) return code;
386 /* # of bytes of path */
387 code = strlen(ioctlp->inDatap) + 1;
388 ioctlp->inDatap += code;
390 /* and return success */
394 long cm_IoctlGetACL(smb_ioctl_t *ioctlp, cm_user_t *userp)
399 AFSFetchStatus fileStatus;
408 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
409 if (code) return code;
411 /* now make the get acl call */
412 fid.Volume = scp->fid.volume;
413 fid.Vnode = scp->fid.vnode;
414 fid.Unique = scp->fid.unique;
416 acl.AFSOpaque_val = ioctlp->outDatap;
417 acl.AFSOpaque_len = 0;
418 code = cm_Conn(&scp->fid, userp, &req, &connp);
421 code = RXAFS_FetchACL(connp->callp, &fid, &acl, &fileStatus, &volSync);
422 } while (cm_Analyze(connp, userp, &req, &scp->fid,
423 &volSync, NULL, code));
424 code = cm_MapRPCError(code, &req);
425 cm_ReleaseSCache(scp);
427 if (code) return code;
429 /* skip over return data */
430 tlen = strlen(ioctlp->outDatap) + 1;
431 ioctlp->outDatap += tlen;
433 /* and return success */
437 long cm_IoctlGetFileCellName(struct smb_ioctl *ioctlp, struct cm_user *userp)
446 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
447 if (code) return code;
449 cellp = cm_FindCellByID(scp->fid.cell);
451 strcpy(ioctlp->outDatap, cellp->namep);
452 ioctlp->outDatap += strlen(ioctlp->outDatap) + 1;
455 else code = CM_ERROR_NOSUCHCELL;
457 cm_ReleaseSCache(scp);
461 long cm_IoctlSetACL(struct smb_ioctl *ioctlp, struct cm_user *userp)
466 AFSFetchStatus fileStatus;
474 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
475 if (code) return code;
477 /* now make the get acl call */
478 fid.Volume = scp->fid.volume;
479 fid.Vnode = scp->fid.vnode;
480 fid.Unique = scp->fid.unique;
482 acl.AFSOpaque_val = ioctlp->inDatap;
483 acl.AFSOpaque_len = strlen(ioctlp->inDatap)+1;
484 code = cm_Conn(&scp->fid, userp, &req, &connp);
487 code = RXAFS_StoreACL(connp->callp, &fid, &acl, &fileStatus, &volSync);
488 } while (cm_Analyze(connp, userp, &req, &scp->fid,
489 &volSync, NULL, code));
490 code = cm_MapRPCError(code, &req);
492 /* invalidate cache info, since we just trashed the ACL cache */
493 lock_ObtainMutex(&scp->mx);
494 cm_DiscardSCache(scp);
495 lock_ReleaseMutex(&scp->mx);
497 cm_ReleaseSCache(scp);
502 long cm_IoctlFlushVolume(struct smb_ioctl *ioctlp, struct cm_user *userp)
506 unsigned long volume;
512 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
513 if (code) return code;
515 volume = scp->fid.volume;
516 cm_ReleaseSCache(scp);
518 lock_ObtainWrite(&cm_scacheLock);
519 for(i=0; i<cm_hashTableSize; i++) {
520 for(scp = cm_hashTablep[i]; scp; scp = scp->nextp) {
521 if (scp->fid.volume == volume) {
523 lock_ReleaseWrite(&cm_scacheLock);
525 /* now flush the file */
526 cm_FlushFile(scp, userp, &req);
528 lock_ObtainWrite(&cm_scacheLock);
533 lock_ReleaseWrite(&cm_scacheLock);
538 long cm_IoctlFlushFile(struct smb_ioctl *ioctlp, struct cm_user *userp)
546 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
547 if (code) return code;
549 cm_FlushFile(scp, userp, &req);
550 cm_ReleaseSCache(scp);
555 long cm_IoctlSetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
559 char offLineMsg[256];
563 AFSFetchVolumeStatus volStat;
564 AFSStoreVolumeStatus storeStat;
572 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
573 if (code) return code;
575 cellp = cm_FindCellByID(scp->fid.cell);
578 if (scp->flags & CM_SCACHEFLAG_RO) {
579 cm_ReleaseSCache(scp);
580 return CM_ERROR_READONLY;
583 code = cm_GetVolumeByID(cellp, scp->fid.volume, userp, &req, &tvp);
585 cm_ReleaseSCache(scp);
589 /* Copy the junk out, using cp as a roving pointer. */
590 cp = ioctlp->inDatap;
591 memcpy((char *)&volStat, cp, sizeof(AFSFetchVolumeStatus));
592 cp += sizeof(AFSFetchVolumeStatus);
594 cp += strlen(volName)+1;
595 strcpy(offLineMsg, cp);
596 cp += strlen(offLineMsg)+1;
599 if (volStat.MinQuota != -1) {
600 storeStat.MinQuota = volStat.MinQuota;
601 storeStat.Mask |= AFS_SETMINQUOTA;
603 if (volStat.MaxQuota != -1) {
604 storeStat.MaxQuota = volStat.MaxQuota;
605 storeStat.Mask |= AFS_SETMAXQUOTA;
609 code = cm_Conn(&scp->fid, userp, &req, &tcp);
612 code = RXAFS_SetVolumeStatus(tcp->callp, scp->fid.volume,
613 &storeStat, volName, offLineMsg, motd);
614 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, code));
615 code = cm_MapRPCError(code, &req);
617 /* return on failure */
618 cm_ReleaseSCache(scp);
623 /* we are sending parms back to make compat. with prev system. should
624 * change interface later to not ask for current status, just set
627 cp = ioctlp->outDatap;
628 memcpy(cp, (char *)&volStat, sizeof(VolumeStatus));
629 cp += sizeof(VolumeStatus);
631 cp += strlen(volName)+1;
632 strcpy(cp, offLineMsg);
633 cp += strlen(offLineMsg)+1;
635 cp += strlen(motd)+1;
637 /* now return updated return data pointer */
638 ioctlp->outDatap = cp;
643 long cm_IoctlGetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
647 char offLineMsg[256];
651 AFSFetchVolumeStatus volStat;
660 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
661 if (code) return code;
664 OfflineMsg = offLineMsg;
667 code = cm_Conn(&scp->fid, userp, &req, &tcp);
670 code = RXAFS_GetVolumeStatus(tcp->callp, scp->fid.volume,
671 &volStat, &Name, &OfflineMsg, &MOTD);
672 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, code));
673 code = cm_MapRPCError(code, &req);
675 cm_ReleaseSCache(scp);
676 if (code) return code;
678 /* Copy all this junk into msg->im_data, keeping track of the lengths. */
679 cp = ioctlp->outDatap;
680 memcpy(cp, (char *)&volStat, sizeof(AFSFetchVolumeStatus));
681 cp += sizeof(AFSFetchVolumeStatus);
683 cp += strlen(volName)+1;
684 strcpy(cp, offLineMsg);
685 cp += strlen(offLineMsg)+1;
687 cp += strlen(motd)+1;
689 /* return new size */
690 ioctlp->outDatap = cp;
695 long cm_IoctlWhereIs(struct smb_ioctl *ioctlp, struct cm_user *userp)
701 cm_serverRef_t *tsrp;
703 unsigned long volume;
709 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
710 if (code) return code;
712 volume = scp->fid.volume;
714 cellp = cm_FindCellByID(scp->fid.cell);
717 cm_ReleaseSCache(scp);
719 code = cm_GetVolumeByID(cellp, volume, userp, &req, &tvp);
720 if (code) return code;
722 cp = ioctlp->outDatap;
724 lock_ObtainMutex(&tvp->mx);
725 tsrp = cm_GetVolServers(tvp, volume);
726 lock_ObtainRead(&cm_serverLock);
729 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
733 lock_ReleaseRead(&cm_serverLock);
734 lock_ReleaseMutex(&tvp->mx);
736 /* still room for terminating NULL, add it on */
737 volume = 0; /* reuse vbl */
738 memcpy(cp, (char *)&volume, sizeof(long));
741 ioctlp->outDatap = cp;
746 long cm_IoctlStatMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
756 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
757 if (code) return code;
759 cp = ioctlp->inDatap;
761 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
762 cm_ReleaseSCache(dscp);
763 if (code) return code;
765 lock_ObtainMutex(&scp->mx);
767 /* now check that this is a real mount point */
768 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
769 lock_ReleaseMutex(&scp->mx);
770 cm_ReleaseSCache(scp);
771 return CM_ERROR_INVAL;
774 code = cm_ReadMountPoint(scp, userp, &req);
776 cp = ioctlp->outDatap;
777 strcpy(cp, scp->mountPointStringp);
778 cp += strlen(cp) + 1;
779 ioctlp->outDatap = cp;
781 lock_ReleaseMutex(&scp->mx);
782 cm_ReleaseSCache(scp);
787 long cm_IoctlDeleteMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
797 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
798 if (code) return code;
800 cp = ioctlp->inDatap;
802 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
804 /* if something went wrong, bail out now */
809 lock_ObtainMutex(&scp->mx);
810 code = cm_SyncOp(scp, NULL, userp, &req, 0,
811 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
813 lock_ReleaseMutex(&scp->mx);
814 cm_ReleaseSCache(scp);
818 /* now check that this is a real mount point */
819 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
820 lock_ReleaseMutex(&scp->mx);
821 cm_ReleaseSCache(scp);
822 code = CM_ERROR_INVAL;
826 /* time to make the RPC, so drop the lock */
827 lock_ReleaseMutex(&scp->mx);
828 cm_ReleaseSCache(scp);
830 /* easier to do it this way */
831 code = cm_Unlink(dscp, cp, userp, &req);
832 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
833 smb_NotifyChange(FILE_ACTION_REMOVED,
834 FILE_NOTIFY_CHANGE_DIR_NAME,
835 dscp, cp, NULL, TRUE);
838 cm_ReleaseSCache(dscp);
842 long cm_IoctlCheckServers(struct smb_ioctl *ioctlp, struct cm_user *userp)
852 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
853 tp = ioctlp->inDatap;
856 memcpy(&temp, tp, sizeof(temp));
857 if (temp == 0x12345678) { /* For afs3.3 version */
858 memcpy(&csi, tp, sizeof(csi));
859 if (csi.tinterval >= 0) {
860 cp = ioctlp->outDatap;
861 memcpy(cp, (char *)&cm_daemonCheckInterval, sizeof(long));
862 ioctlp->outDatap += sizeof(long);
863 if (csi.tinterval > 0) {
864 if (!smb_SUser(userp))
865 return CM_ERROR_NOACCESS;
866 cm_daemonCheckInterval = csi.tinterval;
874 } else { /* For pre afs3.3 versions */
875 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
876 ioctlp->inDatap = cp = ioctlp->inDatap + sizeof(long);
877 if (cp - ioctlp->inAllocp < ioctlp->inCopied) /* still more data available */
882 * 1: fast check, don't contact servers.
883 * 2: local cell only.
886 /* have cell name, too */
887 cellp = cm_GetCell(cp, 0);
888 if (!cellp) return CM_ERROR_NOSUCHCELL;
890 else cellp = (cm_cell_t *) 0;
891 if (!cellp && (temp & 2)) {
893 cellp = cm_FindCellByID(1);
895 if (!(temp & 1)) { /* if not fast, call server checker routine */
896 /* check down servers */
897 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS | CM_FLAG_CHECKUPSERVERS,
901 /* now return the current down server list */
902 cp = ioctlp->outDatap;
903 lock_ObtainRead(&cm_serverLock);
904 for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
905 if (cellp && tsp->cellp != cellp) continue; /* cell spec'd and wrong */
906 if ((tsp->flags & CM_SERVERFLAG_DOWN)
907 && tsp->type == CM_SERVER_FILE) {
908 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
912 lock_ReleaseRead(&cm_serverLock);
914 ioctlp->outDatap = cp;
918 long cm_IoctlGag(struct smb_ioctl *ioctlp, struct cm_user *userp)
920 /* we don't print anything superfluous, so we don't support the gag call */
921 return CM_ERROR_INVAL;
924 long cm_IoctlCheckVolumes(struct smb_ioctl *ioctlp, struct cm_user *userp)
930 long cm_IoctlSetCacheSize(struct smb_ioctl *ioctlp, struct cm_user *userp)
935 cm_SkipIoctlPath(ioctlp);
937 memcpy(&temp, ioctlp->inDatap, sizeof(temp));
938 if (temp == 0) temp = buf_nOrigBuffers;
940 /* temp is in 1K units, convert to # of buffers */
941 temp = temp / (buf_bufferSize / 1024);
944 /* now adjust the cache size */
945 code = buf_SetNBuffers(temp);
950 long cm_IoctlTraceControl(struct smb_ioctl *ioctlp, struct cm_user *userp)
954 cm_SkipIoctlPath(ioctlp);
956 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
960 afsd_ForceTrace(FALSE);
964 /* set tracing value to low order bit */
965 if ((inValue & 1) == 0) {
966 /* disable tracing */
967 osi_LogDisable(afsd_logp);
971 osi_LogEnable(afsd_logp);
975 /* see if we're supposed to do a reset, too */
977 osi_LogReset(afsd_logp);
980 /* and copy out tracing flag */
981 inValue = afsd_logp->enabled; /* use as a temp vbl */
982 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
983 ioctlp->outDatap += sizeof(long);
987 long cm_IoctlGetCacheParms(struct smb_ioctl *ioctlp, struct cm_user *userp)
989 cm_cacheParms_t parms;
991 memset(&parms, 0, sizeof(parms));
993 /* first we get, in 1K units, the cache size */
994 parms.parms[0] = buf_nbuffers * (buf_bufferSize / 1024);
996 /* and then the actual # of buffers in use (not in the free list, I guess,
997 * will be what we do).
999 parms.parms[1] = (buf_nbuffers - buf_CountFreeList()) * (buf_bufferSize / 1024);
1001 memcpy(ioctlp->outDatap, &parms, sizeof(parms));
1002 ioctlp->outDatap += sizeof(parms);
1007 long cm_IoctlGetCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1012 cm_serverRef_t *serverRefp;
1013 cm_server_t *serverp;
1019 cm_SkipIoctlPath(ioctlp);
1021 tp = ioctlp->inDatap;
1023 memcpy((char *)&whichCell, tp, sizeof(long));
1026 /* see if more than one long passed in, ignoring the null pathname (the -1) */
1027 if (ioctlp->inCopied-1 > sizeof(long)) {
1028 memcpy((char *)&magic, tp, sizeof(long));
1031 lock_ObtainRead(&cm_cellLock);
1032 for(tcellp = cm_allCellsp; tcellp; tcellp = tcellp->nextp) {
1033 if (whichCell == 0) break;
1036 lock_ReleaseRead(&cm_cellLock);
1040 cp = ioctlp->outDatap;
1042 if (magic == 0x12345678) {
1043 memcpy(cp, (char *)&magic, sizeof(long));
1046 memset(cp, 0, max * sizeof(long));
1048 lock_ObtainRead(&cm_serverLock); /* for going down server list */
1049 serverRefp = tcellp->vlServersp;
1050 for(i=0; i<max; i++) {
1051 if (!serverRefp) break;
1052 serverp = serverRefp->server;
1053 memcpy(cp, &serverp->addr.sin_addr.s_addr, sizeof(long));
1055 serverRefp = serverRefp->next;
1057 lock_ReleaseRead(&cm_serverLock);
1058 cp = basep + max * sizeof(afs_int32);
1059 strcpy(cp, tcellp->namep);
1060 cp += strlen(tcellp->namep)+1;
1061 ioctlp->outDatap = cp;
1064 if (tcellp) return 0;
1065 else return CM_ERROR_NOMORETOKENS; /* mapped to EDOM */
1068 extern long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep);
1070 long cm_IoctlNewCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1072 /* NT cache manager will read cell information from afsdcell.ini each time
1073 * cell is accessed. So, this call is necessary only if list of server for a cell
1074 * changes (or IP addresses of cell servers changes).
1075 * All that needs to be done is to refresh server information for all cells that
1076 * are already loaded.
1078 * cell list will be cm_CellLock and cm_ServerLock will be held for write.
1083 cm_SkipIoctlPath(ioctlp);
1084 lock_ObtainWrite(&cm_cellLock);
1086 for(cp = cm_allCellsp; cp; cp=cp->nextp)
1089 /* delete all previous server lists - cm_FreeServerList will ask for write on cm_ServerLock*/
1090 cm_FreeServerList(&cp->vlServersp);
1091 cp->vlServersp = NULL;
1092 code = cm_SearchCellFile(cp->namep, cp->namep, cm_AddCellProc, cp);
1093 #ifdef AFS_AFSDB_ENV
1095 if (cm_dnsEnabled) {
1097 code = cm_SearchCellByDNS(cp->namep, cp->namep, &ttl, cm_AddCellProc, cp);
1098 if ( code == 0 ) { /* got cell from DNS */
1099 cp->flags |= CM_CELLFLAG_DNS;
1100 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1101 cp->timeout = time(0) + ttl;
1106 cp->flags &= ~CM_CELLFLAG_DNS;
1108 #endif /* AFS_AFSDB_ENV */
1110 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
1113 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1114 cm_RandomizeServer(&cp->vlServersp);
1118 lock_ReleaseWrite(&cm_cellLock);
1122 long cm_IoctlGetWsCell(smb_ioctl_t *ioctlp, cm_user_t *userp)
1124 /* if we don't know our default cell, return failure */
1125 if (cm_rootCellp == NULL) {
1126 return CM_ERROR_NOSUCHCELL;
1129 /* return the default cellname to the caller */
1130 strcpy(ioctlp->outDatap, cm_rootCellp->namep);
1131 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
1137 long cm_IoctlSysName(struct smb_ioctl *ioctlp, struct cm_user *userp)
1139 long setSysName, foundname = 0;
1140 char *cp, *cp2, inname[MAXSYSNAME], outname[MAXSYSNAME];
1141 int t, count, num = 0;
1142 char **sysnamelist[MAXSYSNAME];
1144 cm_SkipIoctlPath(ioctlp);
1146 memcpy(&setSysName, ioctlp->inDatap, sizeof(long));
1147 ioctlp->inDatap += sizeof(long);
1151 if ( setSysName < 0 || setSysName > MAXNUMSYSNAMES )
1153 cp2 = ioctlp->inDatap;
1154 for ( cp=ioctlp->inDatap, count = 0; count < setSysName; count++ ) {
1155 /* won't go past end of ioctlp->inDatap since maxsysname*num < ioctlp->inDatap length */
1157 if (t >= MAXSYSNAME || t <= 0)
1159 /* check for names that can shoot us in the foot */
1160 if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
1166 /* inname gets first entry in case we're being a translator */
1167 /* (we are never a translator) */
1168 t = strlen(ioctlp->inDatap);
1169 memcpy(inname, ioctlp->inDatap, t + 1);
1170 ioctlp->inDatap += t + 1;
1174 /* Not xlating, so local case */
1176 osi_panic("cm_IoctlSysName: !cm_sysName\n", __FILE__, __LINE__);
1178 if (!setSysName) { /* user just wants the info */
1179 strcpy(outname, cm_sysName);
1180 foundname = cm_sysNameCount;
1181 *sysnamelist = cm_sysNameList;
1182 } else { /* Local guy; only root can change sysname */
1183 /* clear @sys entries from the dnlc, once afs_lookup can
1184 * do lookups of @sys entries and thinks it can trust them */
1185 /* privs ok, store the entry, ... */
1186 strcpy(cm_sysName, inname);
1187 if (setSysName > 1) { /* ... or list */
1188 cp = ioctlp->inDatap;
1189 for (count = 1; count < setSysName; ++count) {
1190 if (!cm_sysNameList[count])
1192 ("cm_IoctlSysName: no cm_sysNameList entry to write\n"
1193 , __FILE__, __LINE__);
1195 memcpy(cm_sysNameList[count], cp, t + 1); /* include null */
1199 cm_sysNameCount = setSysName;
1203 /* return the sysname to the caller */
1204 cp = ioctlp->outDatap;
1205 memcpy(cp, (char *)&foundname, sizeof(afs_int32));
1206 cp += sizeof(afs_int32); /* skip found flag */
1208 strcpy(cp, outname);
1209 cp += strlen(outname) + 1; /* skip name and terminating null char */
1210 for ( count=1; count < foundname ; ++count) { /* ... or list */
1211 if ( !(*sysnamelist)[count] )
1212 osi_panic("cm_IoctlSysName: no cm_sysNameList entry to read\n"
1213 , __FILE__, __LINE__);
1214 t = strlen((*sysnamelist)[count]);
1215 if (t >= MAXSYSNAME)
1216 osi_panic("cm_IoctlSysName: sysname entry garbled\n"
1217 , __FILE__, __LINE__);
1218 strcpy(cp, (*sysnamelist)[count]);
1222 ioctlp->outDatap = cp;
1229 long cm_IoctlGetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1234 cm_SkipIoctlPath(ioctlp);
1236 cellp = cm_GetCell(ioctlp->inDatap, 0);
1237 if (!cellp) return CM_ERROR_NOSUCHCELL;
1240 lock_ObtainMutex(&cellp->mx);
1241 if (cellp->flags & CM_CELLFLAG_SUID)
1242 temp |= CM_SETCELLFLAG_SUID;
1243 lock_ReleaseMutex(&cellp->mx);
1245 /* now copy out parm */
1246 memcpy(ioctlp->outDatap, &temp, sizeof(long));
1247 ioctlp->outDatap += sizeof(long);
1252 long cm_IoctlSetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1257 cm_SkipIoctlPath(ioctlp);
1259 cellp = cm_GetCell(ioctlp->inDatap + 2*sizeof(long), 0);
1260 if (!cellp) return CM_ERROR_NOSUCHCELL;
1262 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
1264 lock_ObtainMutex(&cellp->mx);
1265 if (temp & CM_SETCELLFLAG_SUID)
1266 cellp->flags |= CM_CELLFLAG_SUID;
1268 cellp->flags &= ~CM_CELLFLAG_SUID;
1269 lock_ReleaseMutex(&cellp->mx);
1274 long cm_IoctlSetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1276 cm_SSetPref_t *spin; /* input */
1277 cm_SPref_t *srvin; /* one input component */
1279 int i, vlonly, noServers, type;
1280 struct sockaddr_in tmp;
1281 unsigned short rank;
1283 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1285 spin = (cm_SSetPref_t *)ioctlp->inDatap;
1286 noServers = spin->num_servers;
1287 vlonly = spin->flags;
1289 type = CM_SERVER_VLDB;
1291 type = CM_SERVER_FILE;
1293 for ( i=0; i < noServers; i++)
1295 srvin = &(spin->servers[i]);
1296 rank = srvin->rank + (rand() & 0x000f);
1297 tmp.sin_addr = srvin->host;
1298 tmp.sin_family = AF_INET;
1300 tsp = cm_FindServer(&tmp, type);
1301 if ( tsp ) /* an existing server - ref count increased */
1303 tsp->ipRank = rank; /* no need to protect by mutex*/
1305 if ( type == CM_SERVER_FILE) /* fileserver */
1307 /* find volumes which might have RO copy
1308 /* on server and change the ordering of
1310 cm_ChangeRankVolume(tsp);
1314 /* set preferences for an existing vlserver */
1315 cm_ChangeRankCellVLServer(tsp);
1317 cm_PutServer(tsp); /* decrease refcount */
1319 else /* add a new server without a cell */
1321 tsp = cm_NewServer(&tmp, type, NULL); /* refcount = 1 */
1328 long cm_IoctlGetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1330 cm_SPrefRequest_t *spin; /* input */
1331 cm_SPrefInfo_t *spout; /* output */
1332 cm_SPref_t *srvout; /* one output component */
1334 int i, vlonly, noServers;
1336 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1338 spin = (cm_SPrefRequest_t *)ioctlp->inDatap;
1339 spout = (cm_SPrefInfo_t *) ioctlp->outDatap;
1340 srvout = spout->servers;
1341 noServers = spin->num_servers;
1342 vlonly = spin->flags & CM_SPREF_VLONLY;
1343 spout->num_servers = 0;
1345 lock_ObtainRead(&cm_serverLock); /* get server lock */
1347 for(tsp=cm_allServersp, i=0; tsp && noServers; tsp=tsp->allNextp,i++){
1348 if (spin->offset > i) {
1349 continue; /* catch up to where we left off */
1352 if ( vlonly && (tsp->type == CM_SERVER_FILE) )
1353 continue; /* ignore fileserver for -vlserver option*/
1354 if ( !vlonly && (tsp->type == CM_SERVER_VLDB) )
1355 continue; /* ignore vlservers */
1357 srvout->host = tsp->addr.sin_addr;
1358 srvout->rank = tsp->ipRank;
1360 spout->num_servers++;
1363 lock_ReleaseRead(&cm_serverLock); /* release server lock */
1365 if ( tsp ) /* we ran out of space in the output buffer */
1366 spout->next_offset = i;
1368 spout->next_offset = 0;
1369 ioctlp->outDatap += sizeof(cm_SPrefInfo_t) +
1370 (spout->num_servers -1 ) * sizeof(cm_SPref_t) ;
1374 long cm_IoctlStoreBehind(struct smb_ioctl *ioctlp, struct cm_user *userp)
1376 /* we ignore default asynchrony since we only have one way
1377 * of doing this today.
1382 long cm_IoctlCreateMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
1398 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1399 if (code) return code;
1401 /* Translate chars for the mount point name */
1402 TranslateExtendedChars(leaf);
1405 * The fs command allows the user to specify partial cell names on NT. These must
1406 * be expanded to the full cell name for mount points so that the mount points will
1407 * work on UNIX clients.
1410 /* Extract the possibly partial cell name */
1411 strcpy(cell, ioctlp->inDatap + 1); /* Skip the mp type character */
1413 if (cp = strchr(cell, ':')) {
1414 /* Extract the volume name */
1416 strcpy(volume, cp + 1);
1418 /* Get the full name for this cell */
1419 code = cm_SearchCellFile(cell, fullCell, 0, 0);
1420 #ifdef AFS_AFSDB_ENV
1421 if (code && cm_dnsEnabled)
1422 code = cm_SearchCellByDNS(cell, fullCell, &ttl, 0, 0);
1425 return CM_ERROR_NOSUCHCELL;
1427 sprintf(mpInfo, "%c%s:%s", *ioctlp->inDatap, fullCell, volume);
1429 /* No cell name specified */
1430 strcpy(mpInfo, ioctlp->inDatap);
1433 #ifdef AFS_FREELANCE_CLIENT
1434 if (cm_freelanceEnabled && dscp == cm_rootSCachep) {
1435 /* we are adding the mount point to the root dir., so call
1436 the freelance code to do the add. */
1437 osi_Log0(afsd_logp,"IoctlCreateMountPoint within Freelance root dir");
1438 code = cm_FreelanceAddMount(leaf, fullCell, volume,
1439 *ioctlp->inDatap == '%', NULL);
1443 /* create the symlink with mode 644. The lack of X bits tells
1444 * us that it is a mount point.
1446 tattr.mask = CM_ATTRMASK_UNIXMODEBITS | CM_ATTRMASK_CLIENTMODTIME;
1447 tattr.unixModeBits = 0644;
1448 tattr.clientModTime = time(NULL);
1450 code = cm_SymLink(dscp, leaf, mpInfo, 0, &tattr, userp, &req);
1451 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1452 smb_NotifyChange(FILE_ACTION_ADDED,
1453 FILE_NOTIFY_CHANGE_DIR_NAME,
1454 dscp, leaf, NULL, TRUE);
1456 cm_ReleaseSCache(dscp);
1460 long cm_IoctlSymlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1471 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1472 if (code) return code;
1474 /* Translate chars for the link name */
1475 TranslateExtendedChars(leaf);
1477 /* Translate chars for the linked to name */
1478 TranslateExtendedChars(ioctlp->inDatap);
1480 cp = ioctlp->inDatap; /* contents of link */
1482 /* Create symlink with mode 0755. */
1483 tattr.mask = CM_ATTRMASK_UNIXMODEBITS;
1484 tattr.unixModeBits = 0755;
1486 code = cm_SymLink(dscp, leaf, cp, 0, &tattr, userp, &req);
1487 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1488 smb_NotifyChange(FILE_ACTION_ADDED,
1489 FILE_NOTIFY_CHANGE_FILE_NAME
1490 | FILE_NOTIFY_CHANGE_DIR_NAME,
1491 dscp, leaf, NULL, TRUE);
1493 cm_ReleaseSCache(dscp);
1498 extern long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1499 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1500 cm_user_t *userp, cm_req_t *reqp);
1502 long cm_IoctlListlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1509 cm_scache_t *newRootScp;
1514 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1515 if (code) return code;
1517 cp = ioctlp->inDatap;
1519 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1520 cm_ReleaseSCache(dscp);
1521 if (code) return code;
1523 /* Check that it's a real symlink */
1524 if (scp->fileType != CM_SCACHETYPE_SYMLINK){
1525 cm_ReleaseSCache(scp);
1526 return CM_ERROR_INVAL;
1529 code = cm_AssembleLink(scp, "", &newRootScp, &spacep, userp, &req);
1530 cm_ReleaseSCache(scp);
1532 cp = ioctlp->outDatap;
1533 if (newRootScp != NULL) {
1534 strcpy(cp, cm_mountRoot);
1538 strcpy(cp, spacep->data);
1539 cp += strlen(cp) + 1;
1540 ioctlp->outDatap = cp;
1541 cm_FreeSpace(spacep);
1542 if (newRootScp != NULL)
1543 cm_ReleaseSCache(newRootScp);
1549 long cm_IoctlIslink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1550 {/*CHECK FOR VALID SYMLINK*/
1559 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1560 if (code) return code;
1562 cp = ioctlp->inDatap;
1563 osi_LogEvent("cm_IoctlListlink",NULL," name[%s]",cp);
1565 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1566 cm_ReleaseSCache(dscp);
1567 if (code) return code;
1569 /* Check that it's a real symlink */
1570 if (scp->fileType != CM_SCACHETYPE_SYMLINK)
1571 code = CM_ERROR_INVAL;
1572 cm_ReleaseSCache(scp);
1576 long cm_IoctlDeletelink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1586 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1587 if (code) return code;
1589 cp = ioctlp->inDatap;
1591 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1593 /* if something went wrong, bail out now */
1598 lock_ObtainMutex(&scp->mx);
1599 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1600 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1602 lock_ReleaseMutex(&scp->mx);
1603 cm_ReleaseSCache(scp);
1607 /* now check that this is a real symlink */
1608 if (scp->fileType != CM_SCACHETYPE_SYMLINK) {
1609 lock_ReleaseMutex(&scp->mx);
1610 cm_ReleaseSCache(scp);
1611 code = CM_ERROR_INVAL;
1615 /* time to make the RPC, so drop the lock */
1616 lock_ReleaseMutex(&scp->mx);
1617 cm_ReleaseSCache(scp);
1619 /* easier to do it this way */
1620 code = cm_Unlink(dscp, cp, userp, &req);
1621 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1622 smb_NotifyChange(FILE_ACTION_REMOVED,
1623 FILE_NOTIFY_CHANGE_FILE_NAME
1624 | FILE_NOTIFY_CHANGE_DIR_NAME,
1625 dscp, cp, NULL, TRUE);
1628 cm_ReleaseSCache(dscp);
1632 long cm_IoctlSetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1639 struct ClearToken ct;
1648 saveDataPtr = ioctlp->inDatap;
1650 cm_SkipIoctlPath(ioctlp);
1652 tp = ioctlp->inDatap;
1655 memcpy(&ticketLen, tp, sizeof(ticketLen));
1656 tp += sizeof(ticketLen);
1657 if (ticketLen < MINKTCTICKETLEN || ticketLen > MAXKTCTICKETLEN)
1658 return CM_ERROR_INVAL;
1660 /* remember ticket and skip over it for now */
1664 /* clear token size */
1665 memcpy(&ctSize, tp, sizeof(ctSize));
1666 tp += sizeof(ctSize);
1667 if (ctSize != sizeof(struct ClearToken))
1668 return CM_ERROR_INVAL;
1671 memcpy(&ct, tp, ctSize);
1673 if (ct.AuthHandle == -1)
1674 ct.AuthHandle = 999; /* more rxvab compat stuff */
1676 /* more stuff, if any */
1677 if (ioctlp->inCopied > tp - saveDataPtr) {
1678 /* flags: logon flag */
1679 memcpy(&flags, tp, sizeof(int));
1683 cellp = cm_GetCell(tp, CM_FLAG_CREATE);
1684 if (!cellp) return CM_ERROR_NOSUCHCELL;
1685 tp += strlen(tp) + 1;
1689 tp += strlen(tp) + 1;
1691 if (flags & PIOCTL_LOGON) {
1692 /* SMB user name with which to associate tokens */
1694 fprintf(stderr, "SMB name = %s\n", smbname);
1695 tp += strlen(tp) + 1;
1698 #ifndef DJGPP /* for win95, session key is back in pioctl */
1700 memcpy(&uuid, tp, sizeof(uuid));
1701 if (!cm_FindTokenEvent(uuid, sessionKey))
1702 return CM_ERROR_INVAL;
1705 cellp = cm_rootCellp;
1707 if (flags & PIOCTL_LOGON) {
1708 userp = smb_FindCMUserByName(smbname, ioctlp->fidp->vcp->rname);
1711 /* store the token */
1712 lock_ObtainMutex(&userp->mx);
1713 ucellp = cm_GetUCell(userp, cellp);
1714 ucellp->ticketLen = ticketLen;
1715 if (ucellp->ticketp)
1716 free(ucellp->ticketp); /* Discard old token if any */
1717 ucellp->ticketp = malloc(ticketLen);
1718 memcpy(ucellp->ticketp, ticket, ticketLen);
1721 * Get the session key from the RPC, rather than from the pioctl.
1724 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1726 memcpy(ucellp->sessionKey.data, sessionKey, sizeof(sessionKey));
1728 /* for win95, we are getting the session key from the pioctl */
1729 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1731 ucellp->kvno = ct.AuthHandle;
1732 ucellp->expirationTime = ct.EndTimestamp;
1734 if (uname) strcpy(ucellp->userName, uname);
1735 ucellp->flags |= CM_UCELLFLAG_RXKAD;
1736 lock_ReleaseMutex(&userp->mx);
1738 if (flags & PIOCTL_LOGON) {
1739 ioctlp->flags |= SMB_IOCTLFLAG_LOGON;
1742 cm_ResetACLCache(userp);
1747 long cm_IoctlGetTokenIter(struct smb_ioctl *ioctlp, struct cm_user *userp)
1753 struct ClearToken ct;
1755 cm_SkipIoctlPath(ioctlp);
1757 tp = ioctlp->inDatap;
1758 cp = ioctlp->outDatap;
1761 memcpy(&iterator, tp, sizeof(iterator));
1762 tp += sizeof(iterator);
1764 lock_ObtainMutex(&userp->mx);
1766 /* look for token */
1767 for (;;iterator++) {
1768 ucellp = cm_FindUCell(userp, iterator);
1770 lock_ReleaseMutex(&userp->mx);
1771 return CM_ERROR_NOMORETOKENS;
1773 if (ucellp->flags & CM_UCELLFLAG_RXKAD)
1778 temp = ucellp->iterator + 1;
1779 memcpy(cp, &temp, sizeof(temp));
1783 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1784 cp += sizeof(ucellp->ticketLen);
1787 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1788 cp += ucellp->ticketLen;
1790 /* clear token size */
1792 memcpy(cp, &temp, sizeof(temp));
1796 ct.AuthHandle = ucellp->kvno;
1799 * Don't give out a real session key here
1802 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1804 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1806 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1808 ct.ViceId = 37; /* XXX */
1809 ct.BeginTimestamp = 0; /* XXX */
1810 ct.EndTimestamp = ucellp->expirationTime;
1811 memcpy(cp, &ct, sizeof(ct));
1814 /* Primary flag (unused) */
1816 memcpy(cp, &temp, sizeof(temp));
1820 strcpy(cp, ucellp->cellp->namep);
1821 cp += strlen(cp) + 1;
1824 strcpy(cp, ucellp->userName);
1825 cp += strlen(cp) + 1;
1827 ioctlp->outDatap = cp;
1829 lock_ReleaseMutex(&userp->mx);
1834 long cm_IoctlGetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1840 struct ClearToken ct;
1846 cm_SkipIoctlPath(ioctlp);
1848 tp = ioctlp->inDatap;
1850 cp = ioctlp->outDatap;
1852 /* cell name is right here */
1853 cellp = cm_GetCell(tp, 0);
1854 if (!cellp) return CM_ERROR_NOSUCHCELL;
1855 tp += strlen(tp) + 1;
1859 memcpy(&uuid, tp, sizeof(uuid));
1862 lock_ObtainMutex(&userp->mx);
1864 ucellp = cm_GetUCell(userp, cellp);
1865 if (!ucellp || !(ucellp->flags & CM_UCELLFLAG_RXKAD)) {
1866 lock_ReleaseMutex(&userp->mx);
1867 return CM_ERROR_NOMORETOKENS;
1871 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1872 cp += sizeof(ucellp->ticketLen);
1875 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1876 cp += ucellp->ticketLen;
1878 /* clear token size */
1880 memcpy(cp, &temp, sizeof(temp));
1884 ct.AuthHandle = ucellp->kvno;
1887 * Don't give out a real session key here
1890 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1892 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1894 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1896 ct.ViceId = 37; /* XXX */
1897 ct.BeginTimestamp = 0; /* XXX */
1898 ct.EndTimestamp = ucellp->expirationTime;
1899 memcpy(cp, &ct, sizeof(ct));
1902 /* Primary flag (unused) */
1904 memcpy(cp, &temp, sizeof(temp));
1908 strcpy(cp, ucellp->cellp->namep);
1909 cp += strlen(cp) + 1;
1912 strcpy(cp, ucellp->userName);
1913 cp += strlen(cp) + 1;
1915 ioctlp->outDatap = cp;
1917 lock_ReleaseMutex(&userp->mx);
1920 cm_RegisterNewTokenEvent(uuid, ucellp->sessionKey.data);
1926 long cm_IoctlDelToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1932 cm_SkipIoctlPath(ioctlp);
1934 cp = ioctlp->outDatap;
1936 /* cell name is right here */
1937 cellp = cm_GetCell(ioctlp->inDatap, 0);
1938 if (!cellp) return CM_ERROR_NOSUCHCELL;
1940 lock_ObtainMutex(&userp->mx);
1942 ucellp = cm_GetUCell(userp, cellp);
1944 lock_ReleaseMutex(&userp->mx);
1945 return CM_ERROR_NOMORETOKENS;
1948 if (ucellp->ticketp) {
1949 free(ucellp->ticketp);
1950 ucellp->ticketp = NULL;
1952 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1955 lock_ReleaseMutex(&userp->mx);
1957 cm_ResetACLCache(userp);
1962 long cm_IoctlDelAllToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1966 lock_ObtainMutex(&userp->mx);
1968 for (ucellp = userp->cellInfop; ucellp; ucellp = ucellp->nextp) {
1969 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1973 lock_ReleaseMutex(&userp->mx);
1975 cm_ResetACLCache(userp);
1980 long cm_IoctlMakeSubmount(smb_ioctl_t *ioctlp, cm_user_t *userp)
1982 char afspath[MAX_PATH];
1985 int nextAutoSubmount;
1987 DWORD dwType, dwSize;
1992 cm_SkipIoctlPath(ioctlp);
1994 /* Serialize this one, to prevent simultaneous mods
1997 lock_ObtainMutex(&cm_Afsdsbmt_Lock);
1999 /* Parse the input parameters--first the required afs path,
2000 * then the requested submount name (which may be "").
2002 cm_NormalizeAfsPath (afspath, ioctlp->inDatap);
2003 submountreqp = ioctlp->inDatap + (strlen(ioctlp->inDatap)+1);
2005 /* If the caller supplied a suggested submount name, see if
2006 * that submount name is in use... if so, the submount's path
2007 * has to match our path.
2010 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
2011 "SOFTWARE\\OpenAFS\\Client\\Submounts",
2014 REG_OPTION_NON_VOLATILE,
2015 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
2020 if (submountreqp && *submountreqp) {
2021 char submountPathNormalized[MAX_PATH];
2022 char submountPath[MAX_PATH];
2023 int submountPathLen;
2025 dwSize = sizeof(submountPath);
2026 status = RegQueryValueEx( hkSubmounts, submountreqp, 0,
2027 &dwType, submountPath, &dwSize);
2029 if (status != ERROR_SUCCESS) {
2031 /* The suggested submount name isn't in use now--
2032 * so we can safely map the requested submount name
2033 * to the supplied path. Remember not to write the
2034 * leading "/afs" when writing out the submount.
2036 RegSetValueEx( hkSubmounts, submountreqp, 0,
2038 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2039 &afspath[strlen(cm_mountRoot)]:"/",
2040 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2041 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2043 RegCloseKey( hkSubmounts );
2044 strcpy(ioctlp->outDatap, submountreqp);
2045 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2046 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2050 /* The suggested submount name is already in use--if the
2051 * supplied path matches the submount's path, we can still
2052 * use the suggested submount name.
2054 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2055 if (!strcmp (submountPathNormalized, afspath)) {
2056 strcpy(ioctlp->outDatap, submountreqp);
2057 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2058 RegCloseKey( hkSubmounts );
2059 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2064 RegQueryInfoKey( hkSubmounts,
2066 NULL, /* lpcClass */
2067 NULL, /* lpReserved */
2068 NULL, /* lpcSubKeys */
2069 NULL, /* lpcMaxSubKeyLen */
2070 NULL, /* lpcMaxClassLen */
2071 &dwSubmounts, /* lpcValues */
2072 NULL, /* lpcMaxValueNameLen */
2073 NULL, /* lpcMaxValueLen */
2074 NULL, /* lpcbSecurityDescriptor */
2075 NULL /* lpftLastWriteTime */
2079 /* Having obtained a list of all available submounts, start
2080 * searching that list for a path which matches the requested
2081 * AFS path. We'll also keep track of the highest "auto15"/"auto47"
2082 * submount, in case we need to add a new one later.
2085 nextAutoSubmount = 1;
2087 for ( dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
2088 char submountPathNormalized[MAX_PATH];
2089 char submountPath[MAX_PATH] = "";
2090 DWORD submountPathLen = sizeof(submountPath);
2091 char submountName[256];
2092 DWORD submountNameLen = sizeof(submountName);
2094 RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
2095 &dwType, submountPath, &submountPathLen);
2097 /* If this is an Auto### submount, remember its ### value */
2099 if ((!strnicmp (submountName, "auto", 4)) &&
2100 (isdigit (submountName[strlen("auto")]))) {
2101 int thisAutoSubmount;
2102 thisAutoSubmount = atoi (&submountName[strlen("auto")]);
2103 nextAutoSubmount = max (nextAutoSubmount,
2104 thisAutoSubmount+1);
2107 if ((submountPathLen == 0) ||
2108 (submountPathLen == sizeof(submountPath) - 1)) {
2112 /* See if the path for this submount matches the path
2113 * that our caller specified. If so, we can return
2116 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2117 if (!strcmp (submountPathNormalized, afspath)) {
2118 strcpy(ioctlp->outDatap, submountName);
2119 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2120 RegCloseKey(hkSubmounts);
2121 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2127 /* We've been through the entire list of existing submounts, and
2128 * didn't find any which matched the specified path. So, we'll
2129 * just have to add one. Remember not to write the leading "/afs"
2130 * when writing out the submount.
2133 sprintf(ioctlp->outDatap, "auto%ld", nextAutoSubmount);
2135 RegSetValueEx( hkSubmounts,
2139 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2140 &afspath[strlen(cm_mountRoot)]:"/",
2141 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2142 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2144 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2145 RegCloseKey(hkSubmounts);
2146 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2150 long cm_IoctlGetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2152 memcpy(ioctlp->outDatap, &cryptall, sizeof(cryptall));
2153 ioctlp->outDatap += sizeof(cryptall);
2158 long cm_IoctlSetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2160 cm_SkipIoctlPath(ioctlp);
2162 memcpy(&cryptall, ioctlp->inDatap, sizeof(cryptall));
2168 extern int afsd_shutdown(int);
2169 extern int afs_shutdown;
2171 long cm_IoctlShutdown(smb_ioctl_t *ioctlp, cm_user_t *userp) {
2172 afs_shutdown = 1; /* flag to shut down */
2177 long cm_IoctlGetSMBName(smb_ioctl_t *ioctlp, cm_user_t *userp)
2179 smb_user_t *uidp = ioctlp->uidp;
2181 if (uidp && uidp->unp) {
2182 memcpy(ioctlp->outDatap, uidp->unp->name, strlen(uidp->unp->name));
2183 ioctlp->outDatap += strlen(uidp->unp->name);
2190 * functions to dump contents of various structures.
2191 * In debug build (linked with crt debug library) will dump allocated but not freed memory
2193 extern int cm_DumpSCache(FILE *outputFile, char *cookie);
2194 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie);
2195 extern int smb_DumpVCP(FILE *outputFile, char *cookie);
2197 long cm_IoctlMemoryDump(struct smb_ioctl *ioctlp, struct cm_user *userp)
2201 char logfileName[MAX_PATH+1];
2205 static _CrtMemState memstate;
2208 cm_SkipIoctlPath(ioctlp);
2209 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
2213 strncpy(logfileName, getenv("TEMP"), MAX_PATH);
2214 logfileName[MAX_PATH] = '\0';
2218 GetWindowsDirectory(logfileName, sizeof(logfileName));
2220 strncat(logfileName, "\\afsd_alloc.log", sizeof(logfileName));
2222 hLogFile = CreateFile(logfileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2228 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2229 ioctlp->outDatap += sizeof(long);
2234 SetFilePointer(hLogFile, 0, NULL, FILE_END);
2236 cookie = inValue ? "b" : "e";
2242 _CrtMemCheckpoint(&memstate);
2246 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
2247 _CrtSetReportFile(_CRT_WARN, hLogFile);
2248 _CrtMemDumpAllObjectsSince(&memstate);
2252 /* dump all interesting data */
2253 cm_DumpSCache(hLogFile, cookie);
2254 cm_DumpBufHashTable(hLogFile, cookie);
2255 smb_DumpVCP(hLogFile, cookie);
2257 CloseHandle(hLogFile);
2259 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2260 ioctlp->outDatap += sizeof(long);