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"
48 /* Copied from afs_tokens.h */
49 #define PIOCTL_LOGON 0x1
52 osi_mutex_t cm_Afsdsbmt_Lock;
54 extern afs_int32 cryptall;
55 extern char cm_NetbiosName[];
57 void cm_InitIoctl(void)
59 lock_InitializeMutex(&cm_Afsdsbmt_Lock, "AFSDSBMT.INI Access Lock");
62 long cm_FlushFile(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
66 lock_ObtainWrite(&scp->bufCreateLock);
67 code = buf_FlushCleanPages(scp, userp, reqp);
69 lock_ObtainMutex(&scp->mx);
70 scp->cbServerp = NULL;
72 lock_ReleaseMutex(&scp->mx);
74 lock_ReleaseWrite(&scp->bufCreateLock);
81 * cm_ResetACLCache -- invalidate ACL info for a user that has just
82 * obtained or lost tokens
84 void cm_ResetACLCache(cm_user_t *userp)
89 lock_ObtainWrite(&cm_scacheLock);
90 for (hash=0; hash < cm_hashTableSize; hash++) {
91 for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
93 lock_ReleaseWrite(&cm_scacheLock);
94 lock_ObtainMutex(&scp->mx);
95 cm_InvalidateACLUser(scp, userp);
96 lock_ReleaseMutex(&scp->mx);
97 lock_ObtainWrite(&cm_scacheLock);
101 lock_ReleaseWrite(&cm_scacheLock);
105 * TranslateExtendedChars - This is a fix for TR 54482.
107 * If an extended character (80 - FF) is entered into a file
108 * or directory name in Windows, the character is translated
109 * into the OEM character map before being passed to us. Why
110 * this occurs is unknown. Our pioctl functions must match
111 * this translation for paths given via our own commands (like
112 * fs). If we do not do this, then we will try to perform an
113 * operation on a non-translated path, which we will fail to
114 * find, since the path was created with the translated chars.
115 * This function performs the required translation.
117 void TranslateExtendedChars(char *str)
130 while (*p) *p++ &= 0x7f; /* turn off high bit; probably not right */
134 /* parse the passed-in file name and do a namei on it. If we fail,
135 * return an error code, otherwise return the vnode located in *scpp.
137 long cm_ParseIoctlPath(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
141 cm_scache_t *substRootp;
142 char * relativePath = ioctlp->inDatap;
144 /* This is usually the file name, but for StatMountPoint it is the path. */
145 /* ioctlp->inDatap can be either of the form:
148 * \\netbios-name\submount\path\.
149 * \\netbios-name\submount\path\file
151 TranslateExtendedChars(relativePath);
153 if (relativePath[0] == relativePath[1] &&
154 relativePath[1] == '\\' &&
155 !_strnicmp(cm_NetbiosName,relativePath+2,strlen(cm_NetbiosName)))
161 /* We may have found a UNC path.
162 * If the first component is the NetbiosName,
163 * then throw out the second component (the submount)
164 * since it had better expand into the value of ioctl->tidPathp
167 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
168 if ( !_strnicmp("all", p, 3) )
171 for (i = 0; *p && *p != '\\'; i++,p++ ) {
174 p++; /* skip past trailing slash */
175 shareName[i] = 0; /* terminate string */
177 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
179 /* we found a sharename, therefore use the resulting path */
180 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
181 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
182 userp, sharePath, reqp, &substRootp);
184 if (code) return code;
186 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
187 userp, NULL, reqp, scpp);
188 if (code) return code;
190 /* otherwise, treat the name as a cellname mounted off the afs root.
191 * This requires that we reconstruct the shareName string with
192 * leading and trailing slashes.
194 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
195 if ( !_strnicmp("all", p, 3) )
199 for (i = 1; *p && *p != '\\'; i++,p++ ) {
202 p++; /* skip past trailing slash */
203 shareName[i++] = '/'; /* add trailing slash */
204 shareName[i] = 0; /* terminate string */
207 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
208 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
209 userp, shareName, reqp, &substRootp);
210 if (code) return code;
212 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
213 userp, NULL, reqp, scpp);
214 if (code) return code;
217 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
218 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
219 userp, ioctlp->tidPathp, reqp, &substRootp);
220 if (code) return code;
222 code = cm_NameI(substRootp, relativePath, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
223 userp, NULL, reqp, scpp);
224 if (code) return code;
227 /* # of bytes of path */
228 code = strlen(ioctlp->inDatap) + 1;
229 ioctlp->inDatap += code;
231 /* This is usually nothing, but for StatMountPoint it is the file name. */
232 TranslateExtendedChars(ioctlp->inDatap);
234 /* and return success */
238 void cm_SkipIoctlPath(smb_ioctl_t *ioctlp)
242 temp = strlen(ioctlp->inDatap) + 1;
243 ioctlp->inDatap += temp;
247 /* format the specified path to look like "/afs/<cellname>/usr", by
248 * adding "/afs" (if necessary) in front, changing any \'s to /'s, and
249 * removing any trailing "/"'s. One weirdo caveat: "/afs" will be
250 * intentionally returned as "/afs/"--this makes submount manipulation
251 * easier (because we can always jump past the initial "/afs" to find
252 * the AFS path that should be written into afsdsbmt.ini).
254 void cm_NormalizeAfsPath (char *outpathp, char *inpathp)
257 char bslash_mountRoot[256];
259 strncpy(bslash_mountRoot, cm_mountRoot, sizeof(bslash_mountRoot) - 1);
260 bslash_mountRoot[0] = '\\';
262 if (!strnicmp (inpathp, cm_mountRoot, strlen(cm_mountRoot)))
263 lstrcpy (outpathp, inpathp);
264 else if (!strnicmp (inpathp, bslash_mountRoot, strlen(bslash_mountRoot)))
265 lstrcpy (outpathp, inpathp);
266 else if ((inpathp[0] == '/') || (inpathp[0] == '\\'))
267 sprintf (outpathp, "%s%s", cm_mountRoot, inpathp);
268 else // inpathp looks like "<cell>/usr"
269 sprintf (outpathp, "%s/%s", cm_mountRoot, inpathp);
271 for (cp = outpathp; *cp != 0; ++cp) {
276 if (strlen(outpathp) && (outpathp[strlen(outpathp)-1] == '/')) {
277 outpathp[strlen(outpathp)-1] = 0;
280 if (!strcmpi (outpathp, cm_mountRoot)) {
281 strcpy (outpathp, cm_mountRoot);
285 /* parse the passed-in file name and do a namei on its parent. If we fail,
286 * return an error code, otherwise return the vnode located in *scpp.
288 long cm_ParseIoctlParent(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
289 cm_scache_t **scpp, char *leafp)
294 cm_scache_t *substRootp;
296 strcpy(tbuffer, ioctlp->inDatap);
297 tp = strrchr(tbuffer, '\\');
298 jp = strrchr(tbuffer, '/');
301 else if (jp && (tp - tbuffer) < (jp - tbuffer))
304 strcpy(tbuffer, "\\");
306 strcpy(leafp, ioctlp->inDatap);
314 if (tbuffer[0] == tbuffer[1] &&
315 tbuffer[1] == '\\' &&
316 !_strnicmp(cm_NetbiosName,tbuffer+2,strlen(cm_NetbiosName)))
322 /* We may have found a UNC path.
323 * If the first component is the NetbiosName,
324 * then throw out the second component (the submount)
325 * since it had better expand into the value of ioctl->tidPathp
328 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
329 if ( !_strnicmp("all", p, 3) )
332 for (i = 0; *p && *p != '\\'; i++,p++ ) {
335 p++; /* skip past trailing slash */
336 shareName[i] = 0; /* terminate string */
338 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
340 /* we found a sharename, therefore use the resulting path */
341 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
342 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
343 userp, sharePath, reqp, &substRootp);
345 if (code) return code;
347 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
348 userp, NULL, reqp, scpp);
349 if (code) return code;
351 /* otherwise, treat the name as a cellname mounted off the afs root.
352 * This requires that we reconstruct the shareName string with
353 * leading and trailing slashes.
355 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
356 if ( !_strnicmp("all", p, 3) )
360 for (i = 1; *p && *p != '\\'; i++,p++ ) {
363 p++; /* skip past trailing slash */
364 shareName[i++] = '/'; /* add trailing slash */
365 shareName[i] = 0; /* terminate string */
367 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
368 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
369 userp, shareName, reqp, &substRootp);
370 if (code) return code;
372 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
373 userp, NULL, reqp, scpp);
374 if (code) return code;
377 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
378 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
379 userp, ioctlp->tidPathp, reqp, &substRootp);
380 if (code) return code;
382 code = cm_NameI(substRootp, tbuffer, CM_FLAG_FOLLOW,
383 userp, NULL, reqp, scpp);
384 if (code) return code;
387 /* # of bytes of path */
388 code = strlen(ioctlp->inDatap) + 1;
389 ioctlp->inDatap += code;
391 /* and return success */
395 long cm_IoctlGetACL(smb_ioctl_t *ioctlp, cm_user_t *userp)
400 AFSFetchStatus fileStatus;
409 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
410 if (code) return code;
412 /* now make the get acl call */
413 fid.Volume = scp->fid.volume;
414 fid.Vnode = scp->fid.vnode;
415 fid.Unique = scp->fid.unique;
417 acl.AFSOpaque_val = ioctlp->outDatap;
418 acl.AFSOpaque_len = 0;
419 code = cm_Conn(&scp->fid, userp, &req, &connp);
422 code = RXAFS_FetchACL(connp->callp, &fid, &acl, &fileStatus, &volSync);
423 } while (cm_Analyze(connp, userp, &req, &scp->fid, &volSync, NULL, 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, &volSync, NULL, NULL, code));
489 code = cm_MapRPCError(code, &req);
491 /* invalidate cache info, since we just trashed the ACL cache */
492 lock_ObtainMutex(&scp->mx);
493 cm_DiscardSCache(scp);
494 lock_ReleaseMutex(&scp->mx);
496 cm_ReleaseSCache(scp);
501 long cm_IoctlFlushVolume(struct smb_ioctl *ioctlp, struct cm_user *userp)
505 unsigned long volume;
511 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
512 if (code) return code;
514 volume = scp->fid.volume;
515 cm_ReleaseSCache(scp);
517 lock_ObtainWrite(&cm_scacheLock);
518 for(i=0; i<cm_hashTableSize; i++) {
519 for(scp = cm_hashTablep[i]; scp; scp = scp->nextp) {
520 if (scp->fid.volume == volume) {
522 lock_ReleaseWrite(&cm_scacheLock);
524 /* now flush the file */
525 cm_FlushFile(scp, userp, &req);
527 lock_ObtainWrite(&cm_scacheLock);
532 lock_ReleaseWrite(&cm_scacheLock);
537 long cm_IoctlFlushFile(struct smb_ioctl *ioctlp, struct cm_user *userp)
545 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
546 if (code) return code;
548 cm_FlushFile(scp, userp, &req);
549 cm_ReleaseSCache(scp);
554 long cm_IoctlSetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
558 char offLineMsg[256];
562 AFSFetchVolumeStatus volStat;
563 AFSStoreVolumeStatus storeStat;
571 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
572 if (code) return code;
574 cellp = cm_FindCellByID(scp->fid.cell);
577 if (scp->flags & CM_SCACHEFLAG_RO) {
578 cm_ReleaseSCache(scp);
579 return CM_ERROR_READONLY;
582 code = cm_GetVolumeByID(cellp, scp->fid.volume, userp, &req, &tvp);
584 cm_ReleaseSCache(scp);
588 /* Copy the junk out, using cp as a roving pointer. */
589 cp = ioctlp->inDatap;
590 memcpy((char *)&volStat, cp, sizeof(AFSFetchVolumeStatus));
591 cp += sizeof(AFSFetchVolumeStatus);
593 cp += strlen(volName)+1;
594 strcpy(offLineMsg, cp);
595 cp += strlen(offLineMsg)+1;
598 if (volStat.MinQuota != -1) {
599 storeStat.MinQuota = volStat.MinQuota;
600 storeStat.Mask |= AFS_SETMINQUOTA;
602 if (volStat.MaxQuota != -1) {
603 storeStat.MaxQuota = volStat.MaxQuota;
604 storeStat.Mask |= AFS_SETMAXQUOTA;
608 code = cm_Conn(&scp->fid, userp, &req, &tcp);
611 code = RXAFS_SetVolumeStatus(tcp->callp, scp->fid.volume,
612 &storeStat, volName, offLineMsg, motd);
613 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
614 code = cm_MapRPCError(code, &req);
616 /* return on failure */
617 cm_ReleaseSCache(scp);
622 /* we are sending parms back to make compat. with prev system. should
623 * change interface later to not ask for current status, just set
626 cp = ioctlp->outDatap;
627 memcpy(cp, (char *)&volStat, sizeof(VolumeStatus));
628 cp += sizeof(VolumeStatus);
630 cp += strlen(volName)+1;
631 strcpy(cp, offLineMsg);
632 cp += strlen(offLineMsg)+1;
634 cp += strlen(motd)+1;
636 /* now return updated return data pointer */
637 ioctlp->outDatap = cp;
642 long cm_IoctlGetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
646 char offLineMsg[256];
650 AFSFetchVolumeStatus volStat;
659 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
660 if (code) return code;
663 OfflineMsg = offLineMsg;
666 code = cm_Conn(&scp->fid, userp, &req, &tcp);
669 code = RXAFS_GetVolumeStatus(tcp->callp, scp->fid.volume,
670 &volStat, &Name, &OfflineMsg, &MOTD);
671 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
672 code = cm_MapRPCError(code, &req);
674 cm_ReleaseSCache(scp);
675 if (code) return code;
677 /* Copy all this junk into msg->im_data, keeping track of the lengths. */
678 cp = ioctlp->outDatap;
679 memcpy(cp, (char *)&volStat, sizeof(AFSFetchVolumeStatus));
680 cp += sizeof(AFSFetchVolumeStatus);
682 cp += strlen(volName)+1;
683 strcpy(cp, offLineMsg);
684 cp += strlen(offLineMsg)+1;
686 cp += strlen(motd)+1;
688 /* return new size */
689 ioctlp->outDatap = cp;
694 long cm_IoctlWhereIs(struct smb_ioctl *ioctlp, struct cm_user *userp)
700 cm_serverRef_t **tsrpp, *current;
702 unsigned long volume;
708 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
709 if (code) return code;
711 volume = scp->fid.volume;
713 cellp = cm_FindCellByID(scp->fid.cell);
716 cm_ReleaseSCache(scp);
718 code = cm_GetVolumeByID(cellp, volume, userp, &req, &tvp);
719 if (code) return code;
721 cp = ioctlp->outDatap;
723 lock_ObtainMutex(&tvp->mx);
724 tsrpp = cm_GetVolServers(tvp, volume);
725 lock_ObtainRead(&cm_serverLock);
726 for (current = *tsrpp; current; current = current->next) {
727 tsp = current->server;
728 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
731 lock_ReleaseRead(&cm_serverLock);
732 cm_FreeServerList(tsrpp);
733 lock_ReleaseMutex(&tvp->mx);
735 /* still room for terminating NULL, add it on */
736 volume = 0; /* reuse vbl */
737 memcpy(cp, (char *)&volume, sizeof(long));
740 ioctlp->outDatap = cp;
745 long cm_IoctlStatMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
755 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
756 if (code) return code;
758 cp = ioctlp->inDatap;
760 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
761 cm_ReleaseSCache(dscp);
762 if (code) return code;
764 lock_ObtainMutex(&scp->mx);
766 /* now check that this is a real mount point */
767 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
768 lock_ReleaseMutex(&scp->mx);
769 cm_ReleaseSCache(scp);
770 return CM_ERROR_INVAL;
773 code = cm_ReadMountPoint(scp, userp, &req);
775 cp = ioctlp->outDatap;
776 strcpy(cp, scp->mountPointStringp);
777 cp += strlen(cp) + 1;
778 ioctlp->outDatap = cp;
780 lock_ReleaseMutex(&scp->mx);
781 cm_ReleaseSCache(scp);
786 long cm_IoctlDeleteMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
796 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
797 if (code) return code;
799 cp = ioctlp->inDatap;
801 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
803 /* if something went wrong, bail out now */
808 lock_ObtainMutex(&scp->mx);
809 code = cm_SyncOp(scp, NULL, userp, &req, 0,
810 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
812 lock_ReleaseMutex(&scp->mx);
813 cm_ReleaseSCache(scp);
817 /* now check that this is a real mount point */
818 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
819 lock_ReleaseMutex(&scp->mx);
820 cm_ReleaseSCache(scp);
821 code = CM_ERROR_INVAL;
825 /* time to make the RPC, so drop the lock */
826 lock_ReleaseMutex(&scp->mx);
827 cm_ReleaseSCache(scp);
829 /* easier to do it this way */
830 code = cm_Unlink(dscp, cp, userp, &req);
831 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
832 smb_NotifyChange(FILE_ACTION_REMOVED,
833 FILE_NOTIFY_CHANGE_DIR_NAME,
834 dscp, cp, NULL, TRUE);
837 cm_ReleaseSCache(dscp);
841 long cm_IoctlCheckServers(struct smb_ioctl *ioctlp, struct cm_user *userp)
851 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
852 tp = ioctlp->inDatap;
855 memcpy(&temp, tp, sizeof(temp));
856 if (temp == 0x12345678) { /* For afs3.3 version */
857 memcpy(&csi, tp, sizeof(csi));
858 if (csi.tinterval >= 0) {
859 cp = ioctlp->outDatap;
860 memcpy(cp, (char *)&cm_daemonCheckInterval, sizeof(long));
861 ioctlp->outDatap += sizeof(long);
862 if (csi.tinterval > 0) {
863 if (!smb_SUser(userp))
864 return CM_ERROR_NOACCESS;
865 cm_daemonCheckInterval = csi.tinterval;
873 } else { /* For pre afs3.3 versions */
874 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
875 ioctlp->inDatap = cp = ioctlp->inDatap + sizeof(long);
876 if (cp - ioctlp->inAllocp < ioctlp->inCopied) /* still more data available */
881 * 1: fast check, don't contact servers.
882 * 2: local cell only.
885 /* have cell name, too */
886 cellp = cm_GetCell(cp, 0);
887 if (!cellp) return CM_ERROR_NOSUCHCELL;
889 else cellp = (cm_cell_t *) 0;
890 if (!cellp && (temp & 2)) {
892 cellp = cm_FindCellByID(1);
894 if (!(temp & 1)) { /* if not fast, call server checker routine */
895 /* check down servers */
896 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS | CM_FLAG_CHECKUPSERVERS,
900 /* now return the current down server list */
901 cp = ioctlp->outDatap;
902 lock_ObtainRead(&cm_serverLock);
903 for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
904 if (cellp && tsp->cellp != cellp) continue; /* cell spec'd and wrong */
905 if ((tsp->flags & CM_SERVERFLAG_DOWN)
906 && tsp->type == CM_SERVER_FILE) {
907 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
911 lock_ReleaseRead(&cm_serverLock);
913 ioctlp->outDatap = cp;
917 long cm_IoctlGag(struct smb_ioctl *ioctlp, struct cm_user *userp)
919 /* we don't print anything superfluous, so we don't support the gag call */
920 return CM_ERROR_INVAL;
923 long cm_IoctlCheckVolumes(struct smb_ioctl *ioctlp, struct cm_user *userp)
929 long cm_IoctlSetCacheSize(struct smb_ioctl *ioctlp, struct cm_user *userp)
934 cm_SkipIoctlPath(ioctlp);
936 memcpy(&temp, ioctlp->inDatap, sizeof(temp));
937 if (temp == 0) temp = buf_nOrigBuffers;
939 /* temp is in 1K units, convert to # of buffers */
940 temp = temp / (buf_bufferSize / 1024);
943 /* now adjust the cache size */
944 code = buf_SetNBuffers(temp);
949 long cm_IoctlTraceControl(struct smb_ioctl *ioctlp, struct cm_user *userp)
953 cm_SkipIoctlPath(ioctlp);
955 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
959 afsd_ForceTrace(FALSE);
963 /* set tracing value to low order bit */
964 if ((inValue & 1) == 0) {
965 /* disable tracing */
966 osi_LogDisable(afsd_logp);
970 osi_LogEnable(afsd_logp);
974 /* see if we're supposed to do a reset, too */
976 osi_LogReset(afsd_logp);
979 /* and copy out tracing flag */
980 inValue = afsd_logp->enabled; /* use as a temp vbl */
981 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
982 ioctlp->outDatap += sizeof(long);
986 long cm_IoctlGetCacheParms(struct smb_ioctl *ioctlp, struct cm_user *userp)
988 cm_cacheParms_t parms;
990 memset(&parms, 0, sizeof(parms));
992 /* first we get, in 1K units, the cache size */
993 parms.parms[0] = buf_nbuffers * (buf_bufferSize / 1024);
995 /* and then the actual # of buffers in use (not in the free list, I guess,
996 * will be what we do).
998 parms.parms[1] = (buf_nbuffers - buf_CountFreeList()) * (buf_bufferSize / 1024);
1000 memcpy(ioctlp->outDatap, &parms, sizeof(parms));
1001 ioctlp->outDatap += sizeof(parms);
1006 long cm_IoctlGetCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1011 cm_serverRef_t *serverRefp;
1012 cm_server_t *serverp;
1018 cm_SkipIoctlPath(ioctlp);
1020 tp = ioctlp->inDatap;
1022 memcpy((char *)&whichCell, tp, sizeof(long));
1025 /* see if more than one long passed in, ignoring the null pathname (the -1) */
1026 if (ioctlp->inCopied-1 > sizeof(long)) {
1027 memcpy((char *)&magic, tp, sizeof(long));
1030 lock_ObtainRead(&cm_cellLock);
1031 for(tcellp = cm_allCellsp; tcellp; tcellp = tcellp->nextp) {
1032 if (whichCell == 0) break;
1035 lock_ReleaseRead(&cm_cellLock);
1039 cp = ioctlp->outDatap;
1041 if (magic == 0x12345678) {
1042 memcpy(cp, (char *)&magic, sizeof(long));
1045 memset(cp, 0, max * sizeof(long));
1047 lock_ObtainRead(&cm_serverLock); /* for going down server list */
1048 /* jaltman - do the reference counts to serverRefp contents need to be increased? */
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;
1067 return CM_ERROR_NOMORETOKENS; /* mapped to EDOM */
1070 extern long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep);
1072 long cm_IoctlNewCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1074 /* NT cache manager will read cell information from afsdcell.ini each time
1075 * cell is accessed. So, this call is necessary only if list of server for a cell
1076 * changes (or IP addresses of cell servers changes).
1077 * All that needs to be done is to refresh server information for all cells that
1078 * are already loaded.
1080 * cell list will be cm_CellLock and cm_ServerLock will be held for write.
1085 cm_SkipIoctlPath(ioctlp);
1086 lock_ObtainWrite(&cm_cellLock);
1088 for(cp = cm_allCellsp; cp; cp=cp->nextp)
1091 /* delete all previous server lists - cm_FreeServerList will ask for write on cm_ServerLock*/
1092 cm_FreeServerList(&cp->vlServersp);
1093 cp->vlServersp = NULL;
1094 code = cm_SearchCellFile(cp->namep, cp->namep, cm_AddCellProc, cp);
1095 #ifdef AFS_AFSDB_ENV
1097 if (cm_dnsEnabled) {
1099 code = cm_SearchCellByDNS(cp->namep, cp->namep, &ttl, cm_AddCellProc, cp);
1100 if ( code == 0 ) { /* got cell from DNS */
1101 cp->flags |= CM_CELLFLAG_DNS;
1102 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1103 cp->timeout = time(0) + ttl;
1108 cp->flags &= ~CM_CELLFLAG_DNS;
1110 #endif /* AFS_AFSDB_ENV */
1112 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
1115 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1116 cm_RandomizeServer(&cp->vlServersp);
1120 lock_ReleaseWrite(&cm_cellLock);
1124 long cm_IoctlGetWsCell(smb_ioctl_t *ioctlp, cm_user_t *userp)
1126 /* if we don't know our default cell, return failure */
1127 if (cm_rootCellp == NULL) {
1128 return CM_ERROR_NOSUCHCELL;
1131 /* return the default cellname to the caller */
1132 strcpy(ioctlp->outDatap, cm_rootCellp->namep);
1133 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
1139 long cm_IoctlSysName(struct smb_ioctl *ioctlp, struct cm_user *userp)
1141 long setSysName, foundname = 0;
1142 char *cp, *cp2, inname[MAXSYSNAME], outname[MAXSYSNAME];
1143 int t, count, num = 0;
1144 char **sysnamelist[MAXSYSNAME];
1146 cm_SkipIoctlPath(ioctlp);
1148 memcpy(&setSysName, ioctlp->inDatap, sizeof(long));
1149 ioctlp->inDatap += sizeof(long);
1153 if ( setSysName < 0 || setSysName > MAXNUMSYSNAMES )
1155 cp2 = ioctlp->inDatap;
1156 for ( cp=ioctlp->inDatap, count = 0; count < setSysName; count++ ) {
1157 /* won't go past end of ioctlp->inDatap since maxsysname*num < ioctlp->inDatap length */
1159 if (t >= MAXSYSNAME || t <= 0)
1161 /* check for names that can shoot us in the foot */
1162 if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
1168 /* inname gets first entry in case we're being a translator */
1169 /* (we are never a translator) */
1170 t = strlen(ioctlp->inDatap);
1171 memcpy(inname, ioctlp->inDatap, t + 1);
1172 ioctlp->inDatap += t + 1;
1176 /* Not xlating, so local case */
1178 osi_panic("cm_IoctlSysName: !cm_sysName\n", __FILE__, __LINE__);
1180 if (!setSysName) { /* user just wants the info */
1181 strcpy(outname, cm_sysName);
1182 foundname = cm_sysNameCount;
1183 *sysnamelist = cm_sysNameList;
1184 } else { /* Local guy; only root can change sysname */
1185 /* clear @sys entries from the dnlc, once afs_lookup can
1186 * do lookups of @sys entries and thinks it can trust them */
1187 /* privs ok, store the entry, ... */
1188 strcpy(cm_sysName, inname);
1189 if (setSysName > 1) { /* ... or list */
1190 cp = ioctlp->inDatap;
1191 for (count = 1; count < setSysName; ++count) {
1192 if (!cm_sysNameList[count])
1194 ("cm_IoctlSysName: no cm_sysNameList entry to write\n"
1195 , __FILE__, __LINE__);
1197 memcpy(cm_sysNameList[count], cp, t + 1); /* include null */
1201 cm_sysNameCount = setSysName;
1205 /* return the sysname to the caller */
1206 cp = ioctlp->outDatap;
1207 memcpy(cp, (char *)&foundname, sizeof(afs_int32));
1208 cp += sizeof(afs_int32); /* skip found flag */
1210 strcpy(cp, outname);
1211 cp += strlen(outname) + 1; /* skip name and terminating null char */
1212 for ( count=1; count < foundname ; ++count) { /* ... or list */
1213 if ( !(*sysnamelist)[count] )
1214 osi_panic("cm_IoctlSysName: no cm_sysNameList entry to read\n"
1215 , __FILE__, __LINE__);
1216 t = strlen((*sysnamelist)[count]);
1217 if (t >= MAXSYSNAME)
1218 osi_panic("cm_IoctlSysName: sysname entry garbled\n"
1219 , __FILE__, __LINE__);
1220 strcpy(cp, (*sysnamelist)[count]);
1224 ioctlp->outDatap = cp;
1231 long cm_IoctlGetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1236 cm_SkipIoctlPath(ioctlp);
1238 cellp = cm_GetCell(ioctlp->inDatap, 0);
1239 if (!cellp) return CM_ERROR_NOSUCHCELL;
1242 lock_ObtainMutex(&cellp->mx);
1243 if (cellp->flags & CM_CELLFLAG_SUID)
1244 temp |= CM_SETCELLFLAG_SUID;
1245 lock_ReleaseMutex(&cellp->mx);
1247 /* now copy out parm */
1248 memcpy(ioctlp->outDatap, &temp, sizeof(long));
1249 ioctlp->outDatap += sizeof(long);
1254 long cm_IoctlSetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1259 cm_SkipIoctlPath(ioctlp);
1261 cellp = cm_GetCell(ioctlp->inDatap + 2*sizeof(long), 0);
1262 if (!cellp) return CM_ERROR_NOSUCHCELL;
1264 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
1266 lock_ObtainMutex(&cellp->mx);
1267 if (temp & CM_SETCELLFLAG_SUID)
1268 cellp->flags |= CM_CELLFLAG_SUID;
1270 cellp->flags &= ~CM_CELLFLAG_SUID;
1271 lock_ReleaseMutex(&cellp->mx);
1276 long cm_IoctlSetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1278 cm_SSetPref_t *spin; /* input */
1279 cm_SPref_t *srvin; /* one input component */
1281 int i, vlonly, noServers, type;
1282 struct sockaddr_in tmp;
1283 unsigned short rank;
1285 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1287 spin = (cm_SSetPref_t *)ioctlp->inDatap;
1288 noServers = spin->num_servers;
1289 vlonly = spin->flags;
1291 type = CM_SERVER_VLDB;
1293 type = CM_SERVER_FILE;
1295 for ( i=0; i < noServers; i++)
1297 srvin = &(spin->servers[i]);
1298 rank = srvin->rank + (rand() & 0x000f);
1299 tmp.sin_addr = srvin->host;
1300 tmp.sin_family = AF_INET;
1302 tsp = cm_FindServer(&tmp, type);
1303 if ( tsp ) /* an existing server - ref count increased */
1305 tsp->ipRank = rank; /* no need to protect by mutex*/
1307 if ( type == CM_SERVER_FILE) /* fileserver */
1309 /* find volumes which might have RO copy
1310 /* on server and change the ordering of
1312 cm_ChangeRankVolume(tsp);
1316 /* set preferences for an existing vlserver */
1317 cm_ChangeRankCellVLServer(tsp);
1319 cm_PutServer(tsp); /* decrease refcount */
1321 else /* add a new server without a cell */
1323 tsp = cm_NewServer(&tmp, type, NULL); /* refcount = 1 */
1330 long cm_IoctlGetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1332 cm_SPrefRequest_t *spin; /* input */
1333 cm_SPrefInfo_t *spout; /* output */
1334 cm_SPref_t *srvout; /* one output component */
1336 int i, vlonly, noServers;
1338 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1340 spin = (cm_SPrefRequest_t *)ioctlp->inDatap;
1341 spout = (cm_SPrefInfo_t *) ioctlp->outDatap;
1342 srvout = spout->servers;
1343 noServers = spin->num_servers;
1344 vlonly = spin->flags & CM_SPREF_VLONLY;
1345 spout->num_servers = 0;
1347 lock_ObtainRead(&cm_serverLock); /* get server lock */
1349 for(tsp=cm_allServersp, i=0; tsp && noServers; tsp=tsp->allNextp,i++){
1350 if (spin->offset > i) {
1351 continue; /* catch up to where we left off */
1354 if ( vlonly && (tsp->type == CM_SERVER_FILE) )
1355 continue; /* ignore fileserver for -vlserver option*/
1356 if ( !vlonly && (tsp->type == CM_SERVER_VLDB) )
1357 continue; /* ignore vlservers */
1359 srvout->host = tsp->addr.sin_addr;
1360 srvout->rank = tsp->ipRank;
1362 spout->num_servers++;
1365 lock_ReleaseRead(&cm_serverLock); /* release server lock */
1367 if ( tsp ) /* we ran out of space in the output buffer */
1368 spout->next_offset = i;
1370 spout->next_offset = 0;
1371 ioctlp->outDatap += sizeof(cm_SPrefInfo_t) +
1372 (spout->num_servers -1 ) * sizeof(cm_SPref_t) ;
1376 long cm_IoctlStoreBehind(struct smb_ioctl *ioctlp, struct cm_user *userp)
1378 /* we ignore default asynchrony since we only have one way
1379 * of doing this today.
1384 long cm_IoctlCreateMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
1400 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1401 if (code) return code;
1403 /* Translate chars for the mount point name */
1404 TranslateExtendedChars(leaf);
1407 * The fs command allows the user to specify partial cell names on NT. These must
1408 * be expanded to the full cell name for mount points so that the mount points will
1409 * work on UNIX clients.
1412 /* Extract the possibly partial cell name */
1413 strcpy(cell, ioctlp->inDatap + 1); /* Skip the mp type character */
1415 if (cp = strchr(cell, ':')) {
1416 /* Extract the volume name */
1418 strcpy(volume, cp + 1);
1420 /* Get the full name for this cell */
1421 code = cm_SearchCellFile(cell, fullCell, 0, 0);
1422 #ifdef AFS_AFSDB_ENV
1423 if (code && cm_dnsEnabled)
1424 code = cm_SearchCellByDNS(cell, fullCell, &ttl, 0, 0);
1427 return CM_ERROR_NOSUCHCELL;
1429 sprintf(mpInfo, "%c%s:%s", *ioctlp->inDatap, fullCell, volume);
1431 /* No cell name specified */
1432 strcpy(mpInfo, ioctlp->inDatap);
1435 #ifdef AFS_FREELANCE_CLIENT
1436 if (cm_freelanceEnabled && dscp == cm_rootSCachep) {
1437 /* we are adding the mount point to the root dir., so call
1438 the freelance code to do the add. */
1439 osi_Log0(afsd_logp,"IoctlCreateMountPoint within Freelance root dir");
1440 code = cm_FreelanceAddMount(leaf, fullCell, volume,
1441 *ioctlp->inDatap == '%', NULL);
1445 /* create the symlink with mode 644. The lack of X bits tells
1446 * us that it is a mount point.
1448 tattr.mask = CM_ATTRMASK_UNIXMODEBITS | CM_ATTRMASK_CLIENTMODTIME;
1449 tattr.unixModeBits = 0644;
1450 tattr.clientModTime = time(NULL);
1452 code = cm_SymLink(dscp, leaf, mpInfo, 0, &tattr, userp, &req);
1453 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1454 smb_NotifyChange(FILE_ACTION_ADDED,
1455 FILE_NOTIFY_CHANGE_DIR_NAME,
1456 dscp, leaf, NULL, TRUE);
1458 cm_ReleaseSCache(dscp);
1462 long cm_IoctlSymlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1473 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1474 if (code) return code;
1476 /* Translate chars for the link name */
1477 TranslateExtendedChars(leaf);
1479 /* Translate chars for the linked to name */
1480 TranslateExtendedChars(ioctlp->inDatap);
1482 cp = ioctlp->inDatap; /* contents of link */
1484 /* Create symlink with mode 0755. */
1485 tattr.mask = CM_ATTRMASK_UNIXMODEBITS;
1486 tattr.unixModeBits = 0755;
1488 code = cm_SymLink(dscp, leaf, cp, 0, &tattr, userp, &req);
1489 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1490 smb_NotifyChange(FILE_ACTION_ADDED,
1491 FILE_NOTIFY_CHANGE_FILE_NAME
1492 | FILE_NOTIFY_CHANGE_DIR_NAME,
1493 dscp, leaf, NULL, TRUE);
1495 cm_ReleaseSCache(dscp);
1500 extern long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1501 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1502 cm_user_t *userp, cm_req_t *reqp);
1504 long cm_IoctlListlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1511 cm_scache_t *newRootScp;
1516 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1517 if (code) return code;
1519 cp = ioctlp->inDatap;
1521 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1522 cm_ReleaseSCache(dscp);
1523 if (code) return code;
1525 /* Check that it's a real symlink */
1526 if (scp->fileType != CM_SCACHETYPE_SYMLINK){
1527 cm_ReleaseSCache(scp);
1528 return CM_ERROR_INVAL;
1531 code = cm_AssembleLink(scp, "", &newRootScp, &spacep, userp, &req);
1532 cm_ReleaseSCache(scp);
1534 cp = ioctlp->outDatap;
1535 if (newRootScp != NULL) {
1536 strcpy(cp, cm_mountRoot);
1540 strcpy(cp, spacep->data);
1541 cp += strlen(cp) + 1;
1542 ioctlp->outDatap = cp;
1543 cm_FreeSpace(spacep);
1544 if (newRootScp != NULL)
1545 cm_ReleaseSCache(newRootScp);
1551 long cm_IoctlIslink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1552 {/*CHECK FOR VALID SYMLINK*/
1561 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1562 if (code) return code;
1564 cp = ioctlp->inDatap;
1565 osi_LogEvent("cm_IoctlListlink",NULL," name[%s]",cp);
1567 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1568 cm_ReleaseSCache(dscp);
1569 if (code) return code;
1571 /* Check that it's a real symlink */
1572 if (scp->fileType != CM_SCACHETYPE_SYMLINK)
1573 code = CM_ERROR_INVAL;
1574 cm_ReleaseSCache(scp);
1578 long cm_IoctlDeletelink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1588 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1589 if (code) return code;
1591 cp = ioctlp->inDatap;
1593 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1595 /* if something went wrong, bail out now */
1600 lock_ObtainMutex(&scp->mx);
1601 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1602 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1604 lock_ReleaseMutex(&scp->mx);
1605 cm_ReleaseSCache(scp);
1609 /* now check that this is a real symlink */
1610 if (scp->fileType != CM_SCACHETYPE_SYMLINK) {
1611 lock_ReleaseMutex(&scp->mx);
1612 cm_ReleaseSCache(scp);
1613 code = CM_ERROR_INVAL;
1617 /* time to make the RPC, so drop the lock */
1618 lock_ReleaseMutex(&scp->mx);
1619 cm_ReleaseSCache(scp);
1621 /* easier to do it this way */
1622 code = cm_Unlink(dscp, cp, userp, &req);
1623 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1624 smb_NotifyChange(FILE_ACTION_REMOVED,
1625 FILE_NOTIFY_CHANGE_FILE_NAME
1626 | FILE_NOTIFY_CHANGE_DIR_NAME,
1627 dscp, cp, NULL, TRUE);
1630 cm_ReleaseSCache(dscp);
1634 long cm_IoctlSetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1641 struct ClearToken ct;
1650 saveDataPtr = ioctlp->inDatap;
1652 cm_SkipIoctlPath(ioctlp);
1654 tp = ioctlp->inDatap;
1657 memcpy(&ticketLen, tp, sizeof(ticketLen));
1658 tp += sizeof(ticketLen);
1659 if (ticketLen < MINKTCTICKETLEN || ticketLen > MAXKTCTICKETLEN)
1660 return CM_ERROR_INVAL;
1662 /* remember ticket and skip over it for now */
1666 /* clear token size */
1667 memcpy(&ctSize, tp, sizeof(ctSize));
1668 tp += sizeof(ctSize);
1669 if (ctSize != sizeof(struct ClearToken))
1670 return CM_ERROR_INVAL;
1673 memcpy(&ct, tp, ctSize);
1675 if (ct.AuthHandle == -1)
1676 ct.AuthHandle = 999; /* more rxvab compat stuff */
1678 /* more stuff, if any */
1679 if (ioctlp->inCopied > tp - saveDataPtr) {
1680 /* flags: logon flag */
1681 memcpy(&flags, tp, sizeof(int));
1685 cellp = cm_GetCell(tp, CM_FLAG_CREATE);
1686 if (!cellp) return CM_ERROR_NOSUCHCELL;
1687 tp += strlen(tp) + 1;
1691 tp += strlen(tp) + 1;
1693 if (flags & PIOCTL_LOGON) {
1694 /* SMB user name with which to associate tokens */
1696 osi_Log2(smb_logp,"cm_IoctlSetToken for user [%s] smbname [%s]",
1697 osi_LogSaveString(smb_logp,uname), osi_LogSaveString(smb_logp,smbname));
1698 fprintf(stderr, "SMB name = %s\n", smbname);
1699 tp += strlen(tp) + 1;
1701 osi_Log1(smb_logp,"cm_IoctlSetToken for user [%s]",
1702 osi_LogSaveString(smb_logp,uname));
1705 #ifndef DJGPP /* for win95, session key is back in pioctl */
1707 memcpy(&uuid, tp, sizeof(uuid));
1708 if (!cm_FindTokenEvent(uuid, sessionKey))
1709 return CM_ERROR_INVAL;
1712 cellp = cm_rootCellp;
1713 osi_Log0(smb_logp,"cm_IoctlSetToken - no name specified");
1716 if (flags & PIOCTL_LOGON) {
1717 userp = smb_FindCMUserByName(smbname, ioctlp->fidp->vcp->rname);
1720 /* store the token */
1721 lock_ObtainMutex(&userp->mx);
1722 ucellp = cm_GetUCell(userp, cellp);
1723 osi_Log1(smb_logp,"cm_IoctlSetToken ucellp %lx", ucellp);
1724 ucellp->ticketLen = ticketLen;
1725 if (ucellp->ticketp)
1726 free(ucellp->ticketp); /* Discard old token if any */
1727 ucellp->ticketp = malloc(ticketLen);
1728 memcpy(ucellp->ticketp, ticket, ticketLen);
1731 * Get the session key from the RPC, rather than from the pioctl.
1734 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1736 memcpy(ucellp->sessionKey.data, sessionKey, sizeof(sessionKey));
1738 /* for win95, we are getting the session key from the pioctl */
1739 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1741 ucellp->kvno = ct.AuthHandle;
1742 ucellp->expirationTime = ct.EndTimestamp;
1744 if (uname) strcpy(ucellp->userName, uname);
1745 ucellp->flags |= CM_UCELLFLAG_RXKAD;
1746 lock_ReleaseMutex(&userp->mx);
1748 if (flags & PIOCTL_LOGON) {
1749 ioctlp->flags |= SMB_IOCTLFLAG_LOGON;
1752 cm_ResetACLCache(userp);
1757 long cm_IoctlGetTokenIter(struct smb_ioctl *ioctlp, struct cm_user *userp)
1763 struct ClearToken ct;
1765 cm_SkipIoctlPath(ioctlp);
1767 tp = ioctlp->inDatap;
1768 cp = ioctlp->outDatap;
1771 memcpy(&iterator, tp, sizeof(iterator));
1772 tp += sizeof(iterator);
1774 lock_ObtainMutex(&userp->mx);
1776 /* look for token */
1777 for (;;iterator++) {
1778 ucellp = cm_FindUCell(userp, iterator);
1780 lock_ReleaseMutex(&userp->mx);
1781 return CM_ERROR_NOMORETOKENS;
1783 if (ucellp->flags & CM_UCELLFLAG_RXKAD)
1788 temp = ucellp->iterator + 1;
1789 memcpy(cp, &temp, sizeof(temp));
1793 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1794 cp += sizeof(ucellp->ticketLen);
1797 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1798 cp += ucellp->ticketLen;
1800 /* clear token size */
1802 memcpy(cp, &temp, sizeof(temp));
1806 ct.AuthHandle = ucellp->kvno;
1809 * Don't give out a real session key here
1812 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1814 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1816 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1818 ct.ViceId = 37; /* XXX */
1819 ct.BeginTimestamp = 0; /* XXX */
1820 ct.EndTimestamp = ucellp->expirationTime;
1821 memcpy(cp, &ct, sizeof(ct));
1824 /* Primary flag (unused) */
1826 memcpy(cp, &temp, sizeof(temp));
1830 strcpy(cp, ucellp->cellp->namep);
1831 cp += strlen(cp) + 1;
1834 strcpy(cp, ucellp->userName);
1835 cp += strlen(cp) + 1;
1837 ioctlp->outDatap = cp;
1839 lock_ReleaseMutex(&userp->mx);
1844 long cm_IoctlGetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1850 struct ClearToken ct;
1856 cm_SkipIoctlPath(ioctlp);
1858 tp = ioctlp->inDatap;
1860 cp = ioctlp->outDatap;
1862 /* cell name is right here */
1863 cellp = cm_GetCell(tp, 0);
1864 if (!cellp) return CM_ERROR_NOSUCHCELL;
1865 tp += strlen(tp) + 1;
1869 memcpy(&uuid, tp, sizeof(uuid));
1872 lock_ObtainMutex(&userp->mx);
1874 ucellp = cm_GetUCell(userp, cellp);
1875 if (!ucellp || !(ucellp->flags & CM_UCELLFLAG_RXKAD)) {
1876 lock_ReleaseMutex(&userp->mx);
1877 return CM_ERROR_NOMORETOKENS;
1881 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1882 cp += sizeof(ucellp->ticketLen);
1885 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1886 cp += ucellp->ticketLen;
1888 /* clear token size */
1890 memcpy(cp, &temp, sizeof(temp));
1894 ct.AuthHandle = ucellp->kvno;
1897 * Don't give out a real session key here
1900 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1902 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1904 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1906 ct.ViceId = 37; /* XXX */
1907 ct.BeginTimestamp = 0; /* XXX */
1908 ct.EndTimestamp = ucellp->expirationTime;
1909 memcpy(cp, &ct, sizeof(ct));
1912 /* Primary flag (unused) */
1914 memcpy(cp, &temp, sizeof(temp));
1918 strcpy(cp, ucellp->cellp->namep);
1919 cp += strlen(cp) + 1;
1922 strcpy(cp, ucellp->userName);
1923 cp += strlen(cp) + 1;
1925 ioctlp->outDatap = cp;
1927 lock_ReleaseMutex(&userp->mx);
1930 cm_RegisterNewTokenEvent(uuid, ucellp->sessionKey.data);
1936 long cm_IoctlDelToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1942 cm_SkipIoctlPath(ioctlp);
1944 cp = ioctlp->outDatap;
1946 /* cell name is right here */
1947 cellp = cm_GetCell(ioctlp->inDatap, 0);
1948 if (!cellp) return CM_ERROR_NOSUCHCELL;
1950 lock_ObtainMutex(&userp->mx);
1952 ucellp = cm_GetUCell(userp, cellp);
1954 lock_ReleaseMutex(&userp->mx);
1955 return CM_ERROR_NOMORETOKENS;
1958 osi_Log1(smb_logp,"cm_IoctlDelToken ucellp %lx", ucellp);
1960 if (ucellp->ticketp) {
1961 free(ucellp->ticketp);
1962 ucellp->ticketp = NULL;
1964 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1967 lock_ReleaseMutex(&userp->mx);
1969 cm_ResetACLCache(userp);
1974 long cm_IoctlDelAllToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1978 lock_ObtainMutex(&userp->mx);
1980 for (ucellp = userp->cellInfop; ucellp; ucellp = ucellp->nextp) {
1981 osi_Log1(smb_logp,"cm_IoctlDelAllToken ucellp %lx", ucellp);
1982 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1986 lock_ReleaseMutex(&userp->mx);
1988 cm_ResetACLCache(userp);
1993 long cm_IoctlMakeSubmount(smb_ioctl_t *ioctlp, cm_user_t *userp)
1995 char afspath[MAX_PATH];
1997 int nextAutoSubmount;
1999 DWORD dwType, dwSize;
2004 cm_SkipIoctlPath(ioctlp);
2006 /* Serialize this one, to prevent simultaneous mods
2009 lock_ObtainMutex(&cm_Afsdsbmt_Lock);
2011 /* Parse the input parameters--first the required afs path,
2012 * then the requested submount name (which may be "").
2014 cm_NormalizeAfsPath (afspath, ioctlp->inDatap);
2015 submountreqp = ioctlp->inDatap + (strlen(ioctlp->inDatap)+1);
2017 /* If the caller supplied a suggested submount name, see if
2018 * that submount name is in use... if so, the submount's path
2019 * has to match our path.
2022 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
2023 "SOFTWARE\\OpenAFS\\Client\\Submounts",
2026 REG_OPTION_NON_VOLATILE,
2027 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
2032 if (submountreqp && *submountreqp) {
2033 char submountPathNormalized[MAX_PATH];
2034 char submountPath[MAX_PATH];
2036 dwSize = sizeof(submountPath);
2037 status = RegQueryValueEx( hkSubmounts, submountreqp, 0,
2038 &dwType, submountPath, &dwSize);
2040 if (status != ERROR_SUCCESS) {
2042 /* The suggested submount name isn't in use now--
2043 * so we can safely map the requested submount name
2044 * to the supplied path. Remember not to write the
2045 * leading "/afs" when writing out the submount.
2047 RegSetValueEx( hkSubmounts, submountreqp, 0,
2049 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2050 &afspath[strlen(cm_mountRoot)]:"/",
2051 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2052 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2054 RegCloseKey( hkSubmounts );
2055 strcpy(ioctlp->outDatap, submountreqp);
2056 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2057 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2061 /* The suggested submount name is already in use--if the
2062 * supplied path matches the submount's path, we can still
2063 * use the suggested submount name.
2065 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2066 if (!strcmp (submountPathNormalized, afspath)) {
2067 strcpy(ioctlp->outDatap, submountreqp);
2068 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2069 RegCloseKey( hkSubmounts );
2070 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2075 RegQueryInfoKey( hkSubmounts,
2077 NULL, /* lpcClass */
2078 NULL, /* lpReserved */
2079 NULL, /* lpcSubKeys */
2080 NULL, /* lpcMaxSubKeyLen */
2081 NULL, /* lpcMaxClassLen */
2082 &dwSubmounts, /* lpcValues */
2083 NULL, /* lpcMaxValueNameLen */
2084 NULL, /* lpcMaxValueLen */
2085 NULL, /* lpcbSecurityDescriptor */
2086 NULL /* lpftLastWriteTime */
2090 /* Having obtained a list of all available submounts, start
2091 * searching that list for a path which matches the requested
2092 * AFS path. We'll also keep track of the highest "auto15"/"auto47"
2093 * submount, in case we need to add a new one later.
2096 nextAutoSubmount = 1;
2098 for ( dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
2099 char submountPathNormalized[MAX_PATH];
2100 char submountPath[MAX_PATH] = "";
2101 DWORD submountPathLen = sizeof(submountPath);
2102 char submountName[MAX_PATH];
2103 DWORD submountNameLen = sizeof(submountName);
2106 RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
2107 &dwType, submountPath, &submountPathLen);
2108 if (dwType == REG_EXPAND_SZ) {
2110 StringCbCopyA(buf, MAX_PATH, submountPath);
2111 submountPathLen = ExpandEnvironmentStrings(buf, submountPath, MAX_PATH);
2112 if (submountPathLen > MAX_PATH)
2116 /* If this is an Auto### submount, remember its ### value */
2117 if ((!strnicmp (submountName, "auto", 4)) &&
2118 (isdigit (submountName[strlen("auto")]))) {
2119 int thisAutoSubmount;
2120 thisAutoSubmount = atoi (&submountName[strlen("auto")]);
2121 nextAutoSubmount = max (nextAutoSubmount,
2122 thisAutoSubmount+1);
2125 if ((submountPathLen == 0) ||
2126 (submountPathLen == sizeof(submountPath) - 1)) {
2130 /* See if the path for this submount matches the path
2131 * that our caller specified. If so, we can return
2134 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2135 if (!strcmp (submountPathNormalized, afspath)) {
2136 strcpy(ioctlp->outDatap, submountName);
2137 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2138 RegCloseKey(hkSubmounts);
2139 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2145 /* We've been through the entire list of existing submounts, and
2146 * didn't find any which matched the specified path. So, we'll
2147 * just have to add one. Remember not to write the leading "/afs"
2148 * when writing out the submount.
2151 sprintf(ioctlp->outDatap, "auto%ld", nextAutoSubmount);
2153 RegSetValueEx( hkSubmounts,
2157 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2158 &afspath[strlen(cm_mountRoot)]:"/",
2159 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2160 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2162 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2163 RegCloseKey(hkSubmounts);
2164 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2168 long cm_IoctlGetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2170 memcpy(ioctlp->outDatap, &cryptall, sizeof(cryptall));
2171 ioctlp->outDatap += sizeof(cryptall);
2176 long cm_IoctlSetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2178 cm_SkipIoctlPath(ioctlp);
2180 memcpy(&cryptall, ioctlp->inDatap, sizeof(cryptall));
2186 extern int afsd_shutdown(int);
2187 extern int afs_shutdown;
2189 long cm_IoctlShutdown(smb_ioctl_t *ioctlp, cm_user_t *userp) {
2190 afs_shutdown = 1; /* flag to shut down */
2195 long cm_IoctlGetSMBName(smb_ioctl_t *ioctlp, cm_user_t *userp)
2197 smb_user_t *uidp = ioctlp->uidp;
2199 if (uidp && uidp->unp) {
2200 memcpy(ioctlp->outDatap, uidp->unp->name, strlen(uidp->unp->name));
2201 ioctlp->outDatap += strlen(uidp->unp->name);
2208 * functions to dump contents of various structures.
2209 * In debug build (linked with crt debug library) will dump allocated but not freed memory
2211 extern int cm_DumpSCache(FILE *outputFile, char *cookie);
2212 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie);
2213 extern int smb_DumpVCP(FILE *outputFile, char *cookie);
2215 long cm_IoctlMemoryDump(struct smb_ioctl *ioctlp, struct cm_user *userp)
2219 char logfileName[MAX_PATH+1];
2223 static _CrtMemState memstate;
2226 cm_SkipIoctlPath(ioctlp);
2227 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
2231 strncpy(logfileName, getenv("TEMP"), MAX_PATH);
2232 logfileName[MAX_PATH] = '\0';
2236 GetWindowsDirectory(logfileName, sizeof(logfileName));
2238 strncat(logfileName, "\\afsd_alloc.log", sizeof(logfileName));
2240 hLogFile = CreateFile(logfileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2246 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2247 ioctlp->outDatap += sizeof(long);
2252 SetFilePointer(hLogFile, 0, NULL, FILE_END);
2254 cookie = inValue ? "b" : "e";
2260 _CrtMemCheckpoint(&memstate);
2264 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
2265 _CrtSetReportFile(_CRT_WARN, hLogFile);
2266 _CrtMemDumpAllObjectsSince(&memstate);
2270 /* dump all interesting data */
2271 cm_DumpSCache(hLogFile, cookie);
2272 cm_DumpBufHashTable(hLogFile, cookie);
2273 smb_DumpVCP(hLogFile, cookie);
2275 CloseHandle(hLogFile);
2277 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2278 ioctlp->outDatap += sizeof(long);