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 extern void afsi_log(char *pattern, ...);
59 void cm_InitIoctl(void)
61 lock_InitializeMutex(&cm_Afsdsbmt_Lock, "AFSDSBMT.INI Access Lock");
64 long cm_FlushFile(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
68 lock_ObtainWrite(&scp->bufCreateLock);
69 code = buf_FlushCleanPages(scp, userp, reqp);
71 lock_ObtainMutex(&scp->mx);
72 scp->cbServerp = NULL;
74 lock_ReleaseMutex(&scp->mx);
76 lock_ReleaseWrite(&scp->bufCreateLock);
83 * cm_ResetACLCache -- invalidate ACL info for a user that has just
84 * obtained or lost tokens
86 void cm_ResetACLCache(cm_user_t *userp)
91 lock_ObtainWrite(&cm_scacheLock);
92 for (hash=0; hash < cm_hashTableSize; hash++) {
93 for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
95 lock_ReleaseWrite(&cm_scacheLock);
96 lock_ObtainMutex(&scp->mx);
97 cm_InvalidateACLUser(scp, userp);
98 lock_ReleaseMutex(&scp->mx);
99 lock_ObtainWrite(&cm_scacheLock);
103 lock_ReleaseWrite(&cm_scacheLock);
107 * TranslateExtendedChars - This is a fix for TR 54482.
109 * If an extended character (80 - FF) is entered into a file
110 * or directory name in Windows, the character is translated
111 * into the OEM character map before being passed to us. Why
112 * this occurs is unknown. Our pioctl functions must match
113 * this translation for paths given via our own commands (like
114 * fs). If we do not do this, then we will try to perform an
115 * operation on a non-translated path, which we will fail to
116 * find, since the path was created with the translated chars.
117 * This function performs the required translation.
119 void TranslateExtendedChars(char *str)
132 while (*p) *p++ &= 0x7f; /* turn off high bit; probably not right */
136 /* parse the passed-in file name and do a namei on it. If we fail,
137 * return an error code, otherwise return the vnode located in *scpp.
139 long cm_ParseIoctlPath(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
143 cm_scache_t *substRootp;
144 char * relativePath = ioctlp->inDatap;
146 /* This is usually the file name, but for StatMountPoint it is the path. */
147 /* ioctlp->inDatap can be either of the form:
150 * \\netbios-name\submount\path\.
151 * \\netbios-name\submount\path\file
153 TranslateExtendedChars(relativePath);
155 if (relativePath[0] == relativePath[1] &&
156 relativePath[1] == '\\' &&
157 !_strnicmp(cm_NetbiosName,relativePath+2,strlen(cm_NetbiosName)))
163 /* We may have found a UNC path.
164 * If the first component is the NetbiosName,
165 * then throw out the second component (the submount)
166 * since it had better expand into the value of ioctl->tidPathp
169 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
170 if ( !_strnicmp("all", p, 3) )
173 for (i = 0; *p && *p != '\\'; i++,p++ ) {
176 p++; /* skip past trailing slash */
177 shareName[i] = 0; /* terminate string */
179 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
181 /* we found a sharename, therefore use the resulting path */
182 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
183 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
184 userp, sharePath, reqp, &substRootp);
189 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
190 userp, NULL, reqp, scpp);
194 /* otherwise, treat the name as a cellname mounted off the afs root.
195 * This requires that we reconstruct the shareName string with
196 * leading and trailing slashes.
198 p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
199 if ( !_strnicmp("all", p, 3) )
203 for (i = 1; *p && *p != '\\'; i++,p++ ) {
206 p++; /* skip past trailing slash */
207 shareName[i++] = '/'; /* add trailing slash */
208 shareName[i] = 0; /* terminate string */
211 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
212 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
213 userp, shareName, reqp, &substRootp);
217 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
218 userp, NULL, reqp, scpp);
223 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
224 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
225 userp, ioctlp->tidPathp, reqp, &substRootp);
229 code = cm_NameI(substRootp, relativePath, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
230 userp, NULL, reqp, scpp);
235 /* # of bytes of path */
236 code = strlen(ioctlp->inDatap) + 1;
237 ioctlp->inDatap += code;
239 /* This is usually nothing, but for StatMountPoint it is the file name. */
240 TranslateExtendedChars(ioctlp->inDatap);
242 /* and return success */
246 void cm_SkipIoctlPath(smb_ioctl_t *ioctlp)
250 temp = strlen(ioctlp->inDatap) + 1;
251 ioctlp->inDatap += temp;
255 /* format the specified path to look like "/afs/<cellname>/usr", by
256 * adding "/afs" (if necessary) in front, changing any \'s to /'s, and
257 * removing any trailing "/"'s. One weirdo caveat: "/afs" will be
258 * intentionally returned as "/afs/"--this makes submount manipulation
259 * easier (because we can always jump past the initial "/afs" to find
260 * the AFS path that should be written into afsdsbmt.ini).
262 void cm_NormalizeAfsPath (char *outpathp, char *inpathp)
265 char bslash_mountRoot[256];
267 strncpy(bslash_mountRoot, cm_mountRoot, sizeof(bslash_mountRoot) - 1);
268 bslash_mountRoot[0] = '\\';
270 if (!strnicmp (inpathp, cm_mountRoot, strlen(cm_mountRoot)))
271 lstrcpy (outpathp, inpathp);
272 else if (!strnicmp (inpathp, bslash_mountRoot, strlen(bslash_mountRoot)))
273 lstrcpy (outpathp, inpathp);
274 else if ((inpathp[0] == '/') || (inpathp[0] == '\\'))
275 sprintf (outpathp, "%s%s", cm_mountRoot, inpathp);
276 else // inpathp looks like "<cell>/usr"
277 sprintf (outpathp, "%s/%s", cm_mountRoot, inpathp);
279 for (cp = outpathp; *cp != 0; ++cp) {
284 if (strlen(outpathp) && (outpathp[strlen(outpathp)-1] == '/')) {
285 outpathp[strlen(outpathp)-1] = 0;
288 if (!strcmpi (outpathp, cm_mountRoot)) {
289 strcpy (outpathp, cm_mountRoot);
293 /* parse the passed-in file name and do a namei on its parent. If we fail,
294 * return an error code, otherwise return the vnode located in *scpp.
296 long cm_ParseIoctlParent(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
297 cm_scache_t **scpp, char *leafp)
302 cm_scache_t *substRootp;
304 strcpy(tbuffer, ioctlp->inDatap);
305 tp = strrchr(tbuffer, '\\');
306 jp = strrchr(tbuffer, '/');
309 else if (jp && (tp - tbuffer) < (jp - tbuffer))
312 strcpy(tbuffer, "\\");
314 strcpy(leafp, ioctlp->inDatap);
322 if (tbuffer[0] == tbuffer[1] &&
323 tbuffer[1] == '\\' &&
324 !_strnicmp(cm_NetbiosName,tbuffer+2,strlen(cm_NetbiosName)))
330 /* We may have found a UNC path.
331 * If the first component is the NetbiosName,
332 * then throw out the second component (the submount)
333 * since it had better expand into the value of ioctl->tidPathp
336 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
337 if ( !_strnicmp("all", p, 3) )
340 for (i = 0; *p && *p != '\\'; i++,p++ ) {
343 p++; /* skip past trailing slash */
344 shareName[i] = 0; /* terminate string */
346 shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
348 /* we found a sharename, therefore use the resulting path */
349 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
350 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
351 userp, sharePath, reqp, &substRootp);
353 if (code) return code;
355 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
356 userp, NULL, reqp, scpp);
357 if (code) return code;
359 /* otherwise, treat the name as a cellname mounted off the afs root.
360 * This requires that we reconstruct the shareName string with
361 * leading and trailing slashes.
363 p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
364 if ( !_strnicmp("all", p, 3) )
368 for (i = 1; *p && *p != '\\'; i++,p++ ) {
371 p++; /* skip past trailing slash */
372 shareName[i++] = '/'; /* add trailing slash */
373 shareName[i] = 0; /* terminate string */
375 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
376 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
377 userp, shareName, reqp, &substRootp);
378 if (code) return code;
380 code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
381 userp, NULL, reqp, scpp);
382 if (code) return code;
385 code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
386 CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
387 userp, ioctlp->tidPathp, reqp, &substRootp);
388 if (code) return code;
390 code = cm_NameI(substRootp, tbuffer, CM_FLAG_FOLLOW,
391 userp, NULL, reqp, scpp);
392 if (code) return code;
395 /* # of bytes of path */
396 code = strlen(ioctlp->inDatap) + 1;
397 ioctlp->inDatap += code;
399 /* and return success */
403 long cm_IoctlGetACL(smb_ioctl_t *ioctlp, cm_user_t *userp)
408 AFSFetchStatus fileStatus;
417 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
418 if (code) return code;
420 /* now make the get acl call */
421 fid.Volume = scp->fid.volume;
422 fid.Vnode = scp->fid.vnode;
423 fid.Unique = scp->fid.unique;
425 acl.AFSOpaque_val = ioctlp->outDatap;
426 acl.AFSOpaque_len = 0;
427 code = cm_Conn(&scp->fid, userp, &req, &connp);
430 lock_ObtainMutex(&connp->mx);
431 code = RXAFS_FetchACL(connp->callp, &fid, &acl, &fileStatus, &volSync);
432 lock_ReleaseMutex(&connp->mx);
433 } while (cm_Analyze(connp, userp, &req, &scp->fid, &volSync, NULL, NULL, code));
434 code = cm_MapRPCError(code, &req);
435 cm_ReleaseSCache(scp);
437 if (code) return code;
439 /* skip over return data */
440 tlen = strlen(ioctlp->outDatap) + 1;
441 ioctlp->outDatap += tlen;
443 /* and return success */
447 long cm_IoctlGetFileCellName(struct smb_ioctl *ioctlp, struct cm_user *userp)
456 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
457 if (code) return code;
459 cellp = cm_FindCellByID(scp->fid.cell);
461 strcpy(ioctlp->outDatap, cellp->namep);
462 ioctlp->outDatap += strlen(ioctlp->outDatap) + 1;
465 else code = CM_ERROR_NOSUCHCELL;
467 cm_ReleaseSCache(scp);
471 long cm_IoctlSetACL(struct smb_ioctl *ioctlp, struct cm_user *userp)
476 AFSFetchStatus fileStatus;
484 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
485 if (code) return code;
487 /* now make the get acl call */
488 fid.Volume = scp->fid.volume;
489 fid.Vnode = scp->fid.vnode;
490 fid.Unique = scp->fid.unique;
492 acl.AFSOpaque_val = ioctlp->inDatap;
493 acl.AFSOpaque_len = strlen(ioctlp->inDatap)+1;
494 code = cm_Conn(&scp->fid, userp, &req, &connp);
497 lock_ObtainMutex(&connp->mx);
498 code = RXAFS_StoreACL(connp->callp, &fid, &acl, &fileStatus, &volSync);
499 lock_ReleaseMutex(&connp->mx);
500 } while (cm_Analyze(connp, userp, &req, &scp->fid, &volSync, NULL, NULL, code));
501 code = cm_MapRPCError(code, &req);
503 /* invalidate cache info, since we just trashed the ACL cache */
504 lock_ObtainMutex(&scp->mx);
505 cm_DiscardSCache(scp);
506 lock_ReleaseMutex(&scp->mx);
508 cm_ReleaseSCache(scp);
513 long cm_IoctlFlushVolume(struct smb_ioctl *ioctlp, struct cm_user *userp)
517 unsigned long volume;
523 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
524 if (code) return code;
526 volume = scp->fid.volume;
527 cm_ReleaseSCache(scp);
529 lock_ObtainWrite(&cm_scacheLock);
530 for (i=0; i<cm_hashTableSize; i++) {
531 for (scp = cm_hashTablep[i]; scp; scp = scp->nextp) {
532 if (scp->fid.volume == volume) {
534 lock_ReleaseWrite(&cm_scacheLock);
536 /* now flush the file */
537 code = cm_FlushFile(scp, userp, &req);
539 afsi_log("cm_FlushFile returns error: [%x]",code);
540 lock_ObtainWrite(&cm_scacheLock);
545 lock_ReleaseWrite(&cm_scacheLock);
550 long cm_IoctlFlushFile(struct smb_ioctl *ioctlp, struct cm_user *userp)
558 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
559 if (code) return code;
561 cm_FlushFile(scp, userp, &req);
562 cm_ReleaseSCache(scp);
567 long cm_IoctlSetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
571 char offLineMsg[256];
575 AFSFetchVolumeStatus volStat;
576 AFSStoreVolumeStatus storeStat;
584 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
585 if (code) return code;
587 cellp = cm_FindCellByID(scp->fid.cell);
590 if (scp->flags & CM_SCACHEFLAG_RO) {
591 cm_ReleaseSCache(scp);
592 return CM_ERROR_READONLY;
595 code = cm_GetVolumeByID(cellp, scp->fid.volume, userp, &req, &tvp);
597 cm_ReleaseSCache(scp);
601 /* Copy the junk out, using cp as a roving pointer. */
602 cp = ioctlp->inDatap;
603 memcpy((char *)&volStat, cp, sizeof(AFSFetchVolumeStatus));
604 cp += sizeof(AFSFetchVolumeStatus);
606 cp += strlen(volName)+1;
607 strcpy(offLineMsg, cp);
608 cp += strlen(offLineMsg)+1;
611 if (volStat.MinQuota != -1) {
612 storeStat.MinQuota = volStat.MinQuota;
613 storeStat.Mask |= AFS_SETMINQUOTA;
615 if (volStat.MaxQuota != -1) {
616 storeStat.MaxQuota = volStat.MaxQuota;
617 storeStat.Mask |= AFS_SETMAXQUOTA;
621 code = cm_Conn(&scp->fid, userp, &req, &tcp);
624 lock_ObtainMutex(&tcp->mx);
625 code = RXAFS_SetVolumeStatus(tcp->callp, scp->fid.volume,
626 &storeStat, volName, offLineMsg, motd);
627 lock_ReleaseMutex(&tcp->mx);
628 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
629 code = cm_MapRPCError(code, &req);
631 /* return on failure */
632 cm_ReleaseSCache(scp);
637 /* we are sending parms back to make compat. with prev system. should
638 * change interface later to not ask for current status, just set
641 cp = ioctlp->outDatap;
642 memcpy(cp, (char *)&volStat, sizeof(VolumeStatus));
643 cp += sizeof(VolumeStatus);
645 cp += strlen(volName)+1;
646 strcpy(cp, offLineMsg);
647 cp += strlen(offLineMsg)+1;
649 cp += strlen(motd)+1;
651 /* now return updated return data pointer */
652 ioctlp->outDatap = cp;
657 long cm_IoctlGetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
661 char offLineMsg[256];
665 AFSFetchVolumeStatus volStat;
674 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
675 if (code) return code;
678 OfflineMsg = offLineMsg;
681 code = cm_Conn(&scp->fid, userp, &req, &tcp);
684 lock_ObtainMutex(&tcp->mx);
685 code = RXAFS_GetVolumeStatus(tcp->callp, scp->fid.volume,
686 &volStat, &Name, &OfflineMsg, &MOTD);
687 lock_ReleaseMutex(&tcp->mx);
688 } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
689 code = cm_MapRPCError(code, &req);
691 cm_ReleaseSCache(scp);
692 if (code) return code;
694 /* Copy all this junk into msg->im_data, keeping track of the lengths. */
695 cp = ioctlp->outDatap;
696 memcpy(cp, (char *)&volStat, sizeof(AFSFetchVolumeStatus));
697 cp += sizeof(AFSFetchVolumeStatus);
699 cp += strlen(volName)+1;
700 strcpy(cp, offLineMsg);
701 cp += strlen(offLineMsg)+1;
703 cp += strlen(motd)+1;
705 /* return new size */
706 ioctlp->outDatap = cp;
711 long cm_IoctlWhereIs(struct smb_ioctl *ioctlp, struct cm_user *userp)
717 cm_serverRef_t **tsrpp, *current;
719 unsigned long volume;
725 code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
726 if (code) return code;
728 volume = scp->fid.volume;
730 cellp = cm_FindCellByID(scp->fid.cell);
733 cm_ReleaseSCache(scp);
735 code = cm_GetVolumeByID(cellp, volume, userp, &req, &tvp);
736 if (code) return code;
738 cp = ioctlp->outDatap;
740 lock_ObtainMutex(&tvp->mx);
741 tsrpp = cm_GetVolServers(tvp, volume);
742 lock_ObtainRead(&cm_serverLock);
743 for (current = *tsrpp; current; current = current->next) {
744 tsp = current->server;
745 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
748 lock_ReleaseRead(&cm_serverLock);
749 cm_FreeServerList(tsrpp);
750 lock_ReleaseMutex(&tvp->mx);
752 /* still room for terminating NULL, add it on */
753 volume = 0; /* reuse vbl */
754 memcpy(cp, (char *)&volume, sizeof(long));
757 ioctlp->outDatap = cp;
762 long cm_IoctlStatMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
772 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
773 if (code) return code;
775 cp = ioctlp->inDatap;
777 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
778 cm_ReleaseSCache(dscp);
779 if (code) return code;
781 lock_ObtainMutex(&scp->mx);
783 /* now check that this is a real mount point */
784 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
785 lock_ReleaseMutex(&scp->mx);
786 cm_ReleaseSCache(scp);
787 return CM_ERROR_INVAL;
790 code = cm_ReadMountPoint(scp, userp, &req);
792 cp = ioctlp->outDatap;
793 strcpy(cp, scp->mountPointStringp);
794 cp += strlen(cp) + 1;
795 ioctlp->outDatap = cp;
797 lock_ReleaseMutex(&scp->mx);
798 cm_ReleaseSCache(scp);
803 long cm_IoctlDeleteMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
813 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
814 if (code) return code;
816 cp = ioctlp->inDatap;
818 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
820 /* if something went wrong, bail out now */
825 lock_ObtainMutex(&scp->mx);
826 code = cm_SyncOp(scp, NULL, userp, &req, 0,
827 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
829 lock_ReleaseMutex(&scp->mx);
830 cm_ReleaseSCache(scp);
834 /* now check that this is a real mount point */
835 if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
836 lock_ReleaseMutex(&scp->mx);
837 cm_ReleaseSCache(scp);
838 code = CM_ERROR_INVAL;
842 /* time to make the RPC, so drop the lock */
843 lock_ReleaseMutex(&scp->mx);
844 cm_ReleaseSCache(scp);
846 /* easier to do it this way */
847 code = cm_Unlink(dscp, cp, userp, &req);
848 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
849 smb_NotifyChange(FILE_ACTION_REMOVED,
850 FILE_NOTIFY_CHANGE_DIR_NAME,
851 dscp, cp, NULL, TRUE);
854 cm_ReleaseSCache(dscp);
858 long cm_IoctlCheckServers(struct smb_ioctl *ioctlp, struct cm_user *userp)
868 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
869 tp = ioctlp->inDatap;
872 memcpy(&temp, tp, sizeof(temp));
873 if (temp == 0x12345678) { /* For afs3.3 version */
874 memcpy(&csi, tp, sizeof(csi));
875 if (csi.tinterval >= 0) {
876 cp = ioctlp->outDatap;
877 memcpy(cp, (char *)&cm_daemonCheckInterval, sizeof(long));
878 ioctlp->outDatap += sizeof(long);
879 if (csi.tinterval > 0) {
880 if (!smb_SUser(userp))
881 return CM_ERROR_NOACCESS;
882 cm_daemonCheckInterval = csi.tinterval;
890 } else { /* For pre afs3.3 versions */
891 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
892 ioctlp->inDatap = cp = ioctlp->inDatap + sizeof(long);
893 if (cp - ioctlp->inAllocp < ioctlp->inCopied) /* still more data available */
898 * 1: fast check, don't contact servers.
899 * 2: local cell only.
902 /* have cell name, too */
903 cellp = cm_GetCell(cp, 0);
904 if (!cellp) return CM_ERROR_NOSUCHCELL;
906 else cellp = (cm_cell_t *) 0;
907 if (!cellp && (temp & 2)) {
909 cellp = cm_FindCellByID(1);
911 if (!(temp & 1)) { /* if not fast, call server checker routine */
912 /* check down servers */
913 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS | CM_FLAG_CHECKUPSERVERS,
917 /* now return the current down server list */
918 cp = ioctlp->outDatap;
919 lock_ObtainRead(&cm_serverLock);
920 for (tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
921 if (cellp && tsp->cellp != cellp) continue; /* cell spec'd and wrong */
922 if ((tsp->flags & CM_SERVERFLAG_DOWN)
923 && tsp->type == CM_SERVER_FILE) {
924 memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
928 lock_ReleaseRead(&cm_serverLock);
930 ioctlp->outDatap = cp;
934 long cm_IoctlGag(struct smb_ioctl *ioctlp, struct cm_user *userp)
936 /* we don't print anything superfluous, so we don't support the gag call */
937 return CM_ERROR_INVAL;
940 long cm_IoctlCheckVolumes(struct smb_ioctl *ioctlp, struct cm_user *userp)
946 long cm_IoctlSetCacheSize(struct smb_ioctl *ioctlp, struct cm_user *userp)
951 cm_SkipIoctlPath(ioctlp);
953 memcpy(&temp, ioctlp->inDatap, sizeof(temp));
955 temp = buf_nOrigBuffers;
957 /* temp is in 1K units, convert to # of buffers */
958 temp = temp / (buf_bufferSize / 1024);
961 /* now adjust the cache size */
962 code = buf_SetNBuffers(temp);
967 long cm_IoctlTraceControl(struct smb_ioctl *ioctlp, struct cm_user *userp)
971 cm_SkipIoctlPath(ioctlp);
973 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
977 afsd_ForceTrace(FALSE);
981 /* set tracing value to low order bit */
982 if ((inValue & 1) == 0) {
983 /* disable tracing */
984 osi_LogDisable(afsd_logp);
988 osi_LogEnable(afsd_logp);
992 /* see if we're supposed to do a reset, too */
994 osi_LogReset(afsd_logp);
997 /* and copy out tracing flag */
998 inValue = afsd_logp->enabled; /* use as a temp vbl */
999 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
1000 ioctlp->outDatap += sizeof(long);
1004 long cm_IoctlGetCacheParms(struct smb_ioctl *ioctlp, struct cm_user *userp)
1006 cm_cacheParms_t parms;
1008 memset(&parms, 0, sizeof(parms));
1010 /* first we get, in 1K units, the cache size */
1011 parms.parms[0] = buf_nbuffers * (buf_bufferSize / 1024);
1013 /* and then the actual # of buffers in use (not in the free list, I guess,
1014 * will be what we do).
1016 parms.parms[1] = (buf_nbuffers - buf_CountFreeList()) * (buf_bufferSize / 1024);
1018 memcpy(ioctlp->outDatap, &parms, sizeof(parms));
1019 ioctlp->outDatap += sizeof(parms);
1024 long cm_IoctlGetCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1029 cm_serverRef_t *serverRefp;
1030 cm_server_t *serverp;
1036 cm_SkipIoctlPath(ioctlp);
1038 tp = ioctlp->inDatap;
1040 memcpy((char *)&whichCell, tp, sizeof(long));
1043 /* see if more than one long passed in, ignoring the null pathname (the -1) */
1044 if (ioctlp->inCopied-1 > sizeof(long)) {
1045 memcpy((char *)&magic, tp, sizeof(long));
1048 lock_ObtainRead(&cm_cellLock);
1049 for (tcellp = cm_allCellsp; tcellp; tcellp = tcellp->nextp) {
1050 if (whichCell == 0) break;
1053 lock_ReleaseRead(&cm_cellLock);
1057 cp = ioctlp->outDatap;
1059 if (magic == 0x12345678) {
1060 memcpy(cp, (char *)&magic, sizeof(long));
1063 memset(cp, 0, max * sizeof(long));
1065 lock_ObtainRead(&cm_serverLock); /* for going down server list */
1066 /* jaltman - do the reference counts to serverRefp contents need to be increased? */
1067 serverRefp = tcellp->vlServersp;
1068 for (i=0; i<max; i++) {
1069 if (!serverRefp) break;
1070 serverp = serverRefp->server;
1071 memcpy(cp, &serverp->addr.sin_addr.s_addr, sizeof(long));
1073 serverRefp = serverRefp->next;
1075 lock_ReleaseRead(&cm_serverLock);
1076 cp = basep + max * sizeof(afs_int32);
1077 strcpy(cp, tcellp->namep);
1078 cp += strlen(tcellp->namep)+1;
1079 ioctlp->outDatap = cp;
1085 return CM_ERROR_NOMORETOKENS; /* mapped to EDOM */
1088 extern long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep);
1090 long cm_IoctlNewCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1092 /* NT cache manager will read cell information from afsdcell.ini each time
1093 * cell is accessed. So, this call is necessary only if list of server for a cell
1094 * changes (or IP addresses of cell servers changes).
1095 * All that needs to be done is to refresh server information for all cells that
1096 * are already loaded.
1098 * cell list will be cm_CellLock and cm_ServerLock will be held for write.
1103 cm_SkipIoctlPath(ioctlp);
1104 lock_ObtainWrite(&cm_cellLock);
1106 for (cp = cm_allCellsp; cp; cp=cp->nextp)
1109 /* delete all previous server lists - cm_FreeServerList will ask for write on cm_ServerLock*/
1110 cm_FreeServerList(&cp->vlServersp);
1111 cp->vlServersp = NULL;
1112 code = cm_SearchCellFile(cp->namep, cp->namep, cm_AddCellProc, cp);
1113 #ifdef AFS_AFSDB_ENV
1115 if (cm_dnsEnabled) {
1117 code = cm_SearchCellByDNS(cp->namep, cp->namep, &ttl, cm_AddCellProc, cp);
1118 if ( code == 0 ) { /* got cell from DNS */
1119 cp->flags |= CM_CELLFLAG_DNS;
1120 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1121 cp->timeout = time(0) + ttl;
1126 cp->flags &= ~CM_CELLFLAG_DNS;
1128 #endif /* AFS_AFSDB_ENV */
1130 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
1133 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1134 cm_RandomizeServer(&cp->vlServersp);
1138 lock_ReleaseWrite(&cm_cellLock);
1142 long cm_IoctlGetWsCell(smb_ioctl_t *ioctlp, cm_user_t *userp)
1144 /* if we don't know our default cell, return failure */
1145 if (cm_rootCellp == NULL) {
1146 return CM_ERROR_NOSUCHCELL;
1149 /* return the default cellname to the caller */
1150 strcpy(ioctlp->outDatap, cm_rootCellp->namep);
1151 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
1157 long cm_IoctlSysName(struct smb_ioctl *ioctlp, struct cm_user *userp)
1159 long setSysName, foundname = 0;
1160 char *cp, *cp2, inname[MAXSYSNAME], outname[MAXSYSNAME];
1161 int t, count, num = 0;
1162 char **sysnamelist[MAXSYSNAME];
1164 cm_SkipIoctlPath(ioctlp);
1166 memcpy(&setSysName, ioctlp->inDatap, sizeof(long));
1167 ioctlp->inDatap += sizeof(long);
1171 if ( setSysName < 0 || setSysName > MAXNUMSYSNAMES )
1173 cp2 = ioctlp->inDatap;
1174 for ( cp=ioctlp->inDatap, count = 0; count < setSysName; count++ ) {
1175 /* won't go past end of ioctlp->inDatap since maxsysname*num < ioctlp->inDatap length */
1177 if (t >= MAXSYSNAME || t <= 0)
1179 /* check for names that can shoot us in the foot */
1180 if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
1186 /* inname gets first entry in case we're being a translator */
1187 /* (we are never a translator) */
1188 t = strlen(ioctlp->inDatap);
1189 memcpy(inname, ioctlp->inDatap, t + 1);
1190 ioctlp->inDatap += t + 1;
1194 /* Not xlating, so local case */
1196 osi_panic("cm_IoctlSysName: !cm_sysName\n", __FILE__, __LINE__);
1198 if (!setSysName) { /* user just wants the info */
1199 strcpy(outname, cm_sysName);
1200 foundname = cm_sysNameCount;
1201 *sysnamelist = cm_sysNameList;
1203 /* Local guy; only root can change sysname */
1204 /* clear @sys entries from the dnlc, once afs_lookup can
1205 * do lookups of @sys entries and thinks it can trust them */
1206 /* privs ok, store the entry, ... */
1207 strcpy(cm_sysName, inname);
1208 if (setSysName > 1) { /* ... or list */
1209 cp = ioctlp->inDatap;
1210 for (count = 1; count < setSysName; ++count) {
1211 if (!cm_sysNameList[count])
1212 osi_panic("cm_IoctlSysName: no cm_sysNameList entry to write\n",
1213 __FILE__, __LINE__);
1215 memcpy(cm_sysNameList[count], cp, t + 1); /* include null */
1219 cm_sysNameCount = setSysName;
1223 /* return the sysname to the caller */
1224 cp = ioctlp->outDatap;
1225 memcpy(cp, (char *)&foundname, sizeof(afs_int32));
1226 cp += sizeof(afs_int32); /* skip found flag */
1228 strcpy(cp, outname);
1229 cp += strlen(outname) + 1; /* skip name and terminating null char */
1230 for ( count=1; count < foundname ; ++count) { /* ... or list */
1231 if ( !(*sysnamelist)[count] )
1232 osi_panic("cm_IoctlSysName: no cm_sysNameList entry to read\n",
1233 __FILE__, __LINE__);
1234 t = strlen((*sysnamelist)[count]);
1235 if (t >= MAXSYSNAME)
1236 osi_panic("cm_IoctlSysName: sysname entry garbled\n",
1237 __FILE__, __LINE__);
1238 strcpy(cp, (*sysnamelist)[count]);
1242 ioctlp->outDatap = cp;
1249 long cm_IoctlGetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1254 cm_SkipIoctlPath(ioctlp);
1256 cellp = cm_GetCell(ioctlp->inDatap, 0);
1258 return CM_ERROR_NOSUCHCELL;
1261 lock_ObtainMutex(&cellp->mx);
1262 if (cellp->flags & CM_CELLFLAG_SUID)
1263 temp |= CM_SETCELLFLAG_SUID;
1264 lock_ReleaseMutex(&cellp->mx);
1266 /* now copy out parm */
1267 memcpy(ioctlp->outDatap, &temp, sizeof(long));
1268 ioctlp->outDatap += sizeof(long);
1273 long cm_IoctlSetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1278 cm_SkipIoctlPath(ioctlp);
1280 cellp = cm_GetCell(ioctlp->inDatap + 2*sizeof(long), 0);
1282 return CM_ERROR_NOSUCHCELL;
1284 memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
1286 lock_ObtainMutex(&cellp->mx);
1287 if (temp & CM_SETCELLFLAG_SUID)
1288 cellp->flags |= CM_CELLFLAG_SUID;
1290 cellp->flags &= ~CM_CELLFLAG_SUID;
1291 lock_ReleaseMutex(&cellp->mx);
1296 long cm_IoctlSetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1298 cm_SSetPref_t *spin; /* input */
1299 cm_SPref_t *srvin; /* one input component */
1301 int i, vlonly, noServers, type;
1302 struct sockaddr_in tmp;
1303 unsigned short rank;
1305 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1307 spin = (cm_SSetPref_t *)ioctlp->inDatap;
1308 noServers = spin->num_servers;
1309 vlonly = spin->flags;
1311 type = CM_SERVER_VLDB;
1313 type = CM_SERVER_FILE;
1315 for ( i=0; i < noServers; i++)
1317 srvin = &(spin->servers[i]);
1318 rank = srvin->rank + (rand() & 0x000f);
1319 tmp.sin_addr = srvin->host;
1320 tmp.sin_family = AF_INET;
1322 tsp = cm_FindServer(&tmp, type);
1323 if ( tsp ) /* an existing server - ref count increased */
1325 tsp->ipRank = rank; /* no need to protect by mutex*/
1327 if ( type == CM_SERVER_FILE) /* fileserver */
1329 /* find volumes which might have RO copy
1330 /* on server and change the ordering of
1332 cm_ChangeRankVolume(tsp);
1336 /* set preferences for an existing vlserver */
1337 cm_ChangeRankCellVLServer(tsp);
1339 cm_PutServer(tsp); /* decrease refcount */
1341 else /* add a new server without a cell */
1343 tsp = cm_NewServer(&tmp, type, NULL); /* refcount = 1 */
1350 long cm_IoctlGetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1352 cm_SPrefRequest_t *spin; /* input */
1353 cm_SPrefInfo_t *spout; /* output */
1354 cm_SPref_t *srvout; /* one output component */
1356 int i, vlonly, noServers;
1358 cm_SkipIoctlPath(ioctlp); /* we don't care about the path */
1360 spin = (cm_SPrefRequest_t *)ioctlp->inDatap;
1361 spout = (cm_SPrefInfo_t *) ioctlp->outDatap;
1362 srvout = spout->servers;
1363 noServers = spin->num_servers;
1364 vlonly = spin->flags & CM_SPREF_VLONLY;
1365 spout->num_servers = 0;
1367 lock_ObtainRead(&cm_serverLock); /* get server lock */
1369 for (tsp=cm_allServersp, i=0; tsp && noServers; tsp=tsp->allNextp,i++){
1370 if (spin->offset > i) {
1371 continue; /* catch up to where we left off */
1374 if ( vlonly && (tsp->type == CM_SERVER_FILE) )
1375 continue; /* ignore fileserver for -vlserver option*/
1376 if ( !vlonly && (tsp->type == CM_SERVER_VLDB) )
1377 continue; /* ignore vlservers */
1379 srvout->host = tsp->addr.sin_addr;
1380 srvout->rank = tsp->ipRank;
1382 spout->num_servers++;
1385 lock_ReleaseRead(&cm_serverLock); /* release server lock */
1387 if ( tsp ) /* we ran out of space in the output buffer */
1388 spout->next_offset = i;
1390 spout->next_offset = 0;
1391 ioctlp->outDatap += sizeof(cm_SPrefInfo_t) +
1392 (spout->num_servers -1 ) * sizeof(cm_SPref_t) ;
1396 long cm_IoctlStoreBehind(struct smb_ioctl *ioctlp, struct cm_user *userp)
1398 /* we ignore default asynchrony since we only have one way
1399 * of doing this today.
1404 long cm_IoctlCreateMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
1420 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1421 if (code) return code;
1423 /* Translate chars for the mount point name */
1424 TranslateExtendedChars(leaf);
1427 * The fs command allows the user to specify partial cell names on NT. These must
1428 * be expanded to the full cell name for mount points so that the mount points will
1429 * work on UNIX clients.
1432 /* Extract the possibly partial cell name */
1433 strcpy(cell, ioctlp->inDatap + 1); /* Skip the mp type character */
1435 if (cp = strchr(cell, ':')) {
1436 /* Extract the volume name */
1438 strcpy(volume, cp + 1);
1440 /* Get the full name for this cell */
1441 code = cm_SearchCellFile(cell, fullCell, 0, 0);
1442 #ifdef AFS_AFSDB_ENV
1443 if (code && cm_dnsEnabled)
1444 code = cm_SearchCellByDNS(cell, fullCell, &ttl, 0, 0);
1447 return CM_ERROR_NOSUCHCELL;
1449 sprintf(mpInfo, "%c%s:%s", *ioctlp->inDatap, fullCell, volume);
1451 /* No cell name specified */
1452 strcpy(mpInfo, ioctlp->inDatap);
1455 #ifdef AFS_FREELANCE_CLIENT
1456 if (cm_freelanceEnabled && dscp == cm_rootSCachep) {
1457 /* we are adding the mount point to the root dir., so call
1458 * the freelance code to do the add. */
1459 osi_Log0(afsd_logp,"IoctlCreateMountPoint within Freelance root dir");
1460 code = cm_FreelanceAddMount(leaf, fullCell, volume,
1461 *ioctlp->inDatap == '%', NULL);
1465 /* create the symlink with mode 644. The lack of X bits tells
1466 * us that it is a mount point.
1468 tattr.mask = CM_ATTRMASK_UNIXMODEBITS | CM_ATTRMASK_CLIENTMODTIME;
1469 tattr.unixModeBits = 0644;
1470 tattr.clientModTime = time(NULL);
1472 code = cm_SymLink(dscp, leaf, mpInfo, 0, &tattr, userp, &req);
1473 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1474 smb_NotifyChange(FILE_ACTION_ADDED,
1475 FILE_NOTIFY_CHANGE_DIR_NAME,
1476 dscp, leaf, NULL, TRUE);
1478 cm_ReleaseSCache(dscp);
1482 long cm_IoctlSymlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1493 code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1494 if (code) return code;
1496 /* Translate chars for the link name */
1497 TranslateExtendedChars(leaf);
1499 /* Translate chars for the linked to name */
1500 TranslateExtendedChars(ioctlp->inDatap);
1502 cp = ioctlp->inDatap; /* contents of link */
1504 /* Create symlink with mode 0755. */
1505 tattr.mask = CM_ATTRMASK_UNIXMODEBITS;
1506 tattr.unixModeBits = 0755;
1508 code = cm_SymLink(dscp, leaf, cp, 0, &tattr, userp, &req);
1509 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1510 smb_NotifyChange(FILE_ACTION_ADDED,
1511 FILE_NOTIFY_CHANGE_FILE_NAME
1512 | FILE_NOTIFY_CHANGE_DIR_NAME,
1513 dscp, leaf, NULL, TRUE);
1515 cm_ReleaseSCache(dscp);
1520 extern long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1521 cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1522 cm_user_t *userp, cm_req_t *reqp);
1524 long cm_IoctlListlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1531 cm_scache_t *newRootScp;
1536 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1537 if (code) return code;
1539 cp = ioctlp->inDatap;
1541 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1542 cm_ReleaseSCache(dscp);
1543 if (code) return code;
1545 /* Check that it's a real symlink */
1546 if (scp->fileType != CM_SCACHETYPE_SYMLINK){
1547 cm_ReleaseSCache(scp);
1548 return CM_ERROR_INVAL;
1551 code = cm_AssembleLink(scp, "", &newRootScp, &spacep, userp, &req);
1552 cm_ReleaseSCache(scp);
1554 cp = ioctlp->outDatap;
1555 if (newRootScp != NULL) {
1556 strcpy(cp, cm_mountRoot);
1560 strcpy(cp, spacep->data);
1561 cp += strlen(cp) + 1;
1562 ioctlp->outDatap = cp;
1563 cm_FreeSpace(spacep);
1564 if (newRootScp != NULL)
1565 cm_ReleaseSCache(newRootScp);
1571 long cm_IoctlIslink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1572 {/*CHECK FOR VALID SYMLINK*/
1581 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1582 if (code) return code;
1584 cp = ioctlp->inDatap;
1585 osi_LogEvent("cm_IoctlListlink",NULL," name[%s]",cp);
1587 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1588 cm_ReleaseSCache(dscp);
1589 if (code) return code;
1591 /* Check that it's a real symlink */
1592 if (scp->fileType != CM_SCACHETYPE_SYMLINK)
1593 code = CM_ERROR_INVAL;
1594 cm_ReleaseSCache(scp);
1598 long cm_IoctlDeletelink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1608 code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1609 if (code) return code;
1611 cp = ioctlp->inDatap;
1613 code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1615 /* if something went wrong, bail out now */
1620 lock_ObtainMutex(&scp->mx);
1621 code = cm_SyncOp(scp, NULL, userp, &req, 0,
1622 CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1624 lock_ReleaseMutex(&scp->mx);
1625 cm_ReleaseSCache(scp);
1629 /* now check that this is a real symlink */
1630 if (scp->fileType != CM_SCACHETYPE_SYMLINK) {
1631 lock_ReleaseMutex(&scp->mx);
1632 cm_ReleaseSCache(scp);
1633 code = CM_ERROR_INVAL;
1637 /* time to make the RPC, so drop the lock */
1638 lock_ReleaseMutex(&scp->mx);
1639 cm_ReleaseSCache(scp);
1641 /* easier to do it this way */
1642 code = cm_Unlink(dscp, cp, userp, &req);
1643 if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1644 smb_NotifyChange(FILE_ACTION_REMOVED,
1645 FILE_NOTIFY_CHANGE_FILE_NAME
1646 | FILE_NOTIFY_CHANGE_DIR_NAME,
1647 dscp, cp, NULL, TRUE);
1650 cm_ReleaseSCache(dscp);
1654 long cm_IoctlSetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1661 struct ClearToken ct;
1670 saveDataPtr = ioctlp->inDatap;
1672 cm_SkipIoctlPath(ioctlp);
1674 tp = ioctlp->inDatap;
1677 memcpy(&ticketLen, tp, sizeof(ticketLen));
1678 tp += sizeof(ticketLen);
1679 if (ticketLen < MINKTCTICKETLEN || ticketLen > MAXKTCTICKETLEN)
1680 return CM_ERROR_INVAL;
1682 /* remember ticket and skip over it for now */
1686 /* clear token size */
1687 memcpy(&ctSize, tp, sizeof(ctSize));
1688 tp += sizeof(ctSize);
1689 if (ctSize != sizeof(struct ClearToken))
1690 return CM_ERROR_INVAL;
1693 memcpy(&ct, tp, ctSize);
1695 if (ct.AuthHandle == -1)
1696 ct.AuthHandle = 999; /* more rxvab compat stuff */
1698 /* more stuff, if any */
1699 if (ioctlp->inCopied > tp - saveDataPtr) {
1700 /* flags: logon flag */
1701 memcpy(&flags, tp, sizeof(int));
1705 cellp = cm_GetCell(tp, CM_FLAG_CREATE);
1706 if (!cellp) return CM_ERROR_NOSUCHCELL;
1707 tp += strlen(tp) + 1;
1711 tp += strlen(tp) + 1;
1713 if (flags & PIOCTL_LOGON) {
1714 /* SMB user name with which to associate tokens */
1716 osi_Log2(smb_logp,"cm_IoctlSetToken for user [%s] smbname [%s]",
1717 osi_LogSaveString(smb_logp,uname), osi_LogSaveString(smb_logp,smbname));
1718 fprintf(stderr, "SMB name = %s\n", smbname);
1719 tp += strlen(tp) + 1;
1721 osi_Log1(smb_logp,"cm_IoctlSetToken for user [%s]",
1722 osi_LogSaveString(smb_logp,uname));
1725 #ifndef DJGPP /* for win95, session key is back in pioctl */
1727 memcpy(&uuid, tp, sizeof(uuid));
1728 if (!cm_FindTokenEvent(uuid, sessionKey))
1729 return CM_ERROR_INVAL;
1732 cellp = cm_rootCellp;
1733 osi_Log0(smb_logp,"cm_IoctlSetToken - no name specified");
1736 if (flags & PIOCTL_LOGON) {
1737 userp = smb_FindCMUserByName(smbname, ioctlp->fidp->vcp->rname);
1740 /* store the token */
1741 lock_ObtainMutex(&userp->mx);
1742 ucellp = cm_GetUCell(userp, cellp);
1743 osi_Log1(smb_logp,"cm_IoctlSetToken ucellp %lx", ucellp);
1744 ucellp->ticketLen = ticketLen;
1745 if (ucellp->ticketp)
1746 free(ucellp->ticketp); /* Discard old token if any */
1747 ucellp->ticketp = malloc(ticketLen);
1748 memcpy(ucellp->ticketp, ticket, ticketLen);
1751 * Get the session key from the RPC, rather than from the pioctl.
1754 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1756 memcpy(ucellp->sessionKey.data, sessionKey, sizeof(sessionKey));
1758 /* for win95, we are getting the session key from the pioctl */
1759 memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1761 ucellp->kvno = ct.AuthHandle;
1762 ucellp->expirationTime = ct.EndTimestamp;
1764 if (uname) strcpy(ucellp->userName, uname);
1765 ucellp->flags |= CM_UCELLFLAG_RXKAD;
1766 lock_ReleaseMutex(&userp->mx);
1768 if (flags & PIOCTL_LOGON) {
1769 ioctlp->flags |= SMB_IOCTLFLAG_LOGON;
1772 cm_ResetACLCache(userp);
1777 long cm_IoctlGetTokenIter(struct smb_ioctl *ioctlp, struct cm_user *userp)
1783 struct ClearToken ct;
1785 cm_SkipIoctlPath(ioctlp);
1787 tp = ioctlp->inDatap;
1788 cp = ioctlp->outDatap;
1791 memcpy(&iterator, tp, sizeof(iterator));
1792 tp += sizeof(iterator);
1794 lock_ObtainMutex(&userp->mx);
1796 /* look for token */
1797 for (;;iterator++) {
1798 ucellp = cm_FindUCell(userp, iterator);
1800 lock_ReleaseMutex(&userp->mx);
1801 return CM_ERROR_NOMORETOKENS;
1803 if (ucellp->flags & CM_UCELLFLAG_RXKAD)
1808 temp = ucellp->iterator + 1;
1809 memcpy(cp, &temp, sizeof(temp));
1813 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1814 cp += sizeof(ucellp->ticketLen);
1817 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1818 cp += ucellp->ticketLen;
1820 /* clear token size */
1822 memcpy(cp, &temp, sizeof(temp));
1826 ct.AuthHandle = ucellp->kvno;
1829 * Don't give out a real session key here
1832 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1834 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1836 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1838 ct.ViceId = 37; /* XXX */
1839 ct.BeginTimestamp = 0; /* XXX */
1840 ct.EndTimestamp = ucellp->expirationTime;
1841 memcpy(cp, &ct, sizeof(ct));
1844 /* Primary flag (unused) */
1846 memcpy(cp, &temp, sizeof(temp));
1850 strcpy(cp, ucellp->cellp->namep);
1851 cp += strlen(cp) + 1;
1854 strcpy(cp, ucellp->userName);
1855 cp += strlen(cp) + 1;
1857 ioctlp->outDatap = cp;
1859 lock_ReleaseMutex(&userp->mx);
1864 long cm_IoctlGetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1870 struct ClearToken ct;
1876 cm_SkipIoctlPath(ioctlp);
1878 tp = ioctlp->inDatap;
1880 cp = ioctlp->outDatap;
1882 /* cell name is right here */
1883 cellp = cm_GetCell(tp, 0);
1884 if (!cellp) return CM_ERROR_NOSUCHCELL;
1885 tp += strlen(tp) + 1;
1889 memcpy(&uuid, tp, sizeof(uuid));
1892 lock_ObtainMutex(&userp->mx);
1894 ucellp = cm_GetUCell(userp, cellp);
1895 if (!ucellp || !(ucellp->flags & CM_UCELLFLAG_RXKAD)) {
1896 lock_ReleaseMutex(&userp->mx);
1897 return CM_ERROR_NOMORETOKENS;
1901 memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1902 cp += sizeof(ucellp->ticketLen);
1905 memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1906 cp += ucellp->ticketLen;
1908 /* clear token size */
1910 memcpy(cp, &temp, sizeof(temp));
1914 ct.AuthHandle = ucellp->kvno;
1917 * Don't give out a real session key here
1920 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1922 memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1924 memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1926 ct.ViceId = 37; /* XXX */
1927 ct.BeginTimestamp = 0; /* XXX */
1928 ct.EndTimestamp = ucellp->expirationTime;
1929 memcpy(cp, &ct, sizeof(ct));
1932 /* Primary flag (unused) */
1934 memcpy(cp, &temp, sizeof(temp));
1938 strcpy(cp, ucellp->cellp->namep);
1939 cp += strlen(cp) + 1;
1942 strcpy(cp, ucellp->userName);
1943 cp += strlen(cp) + 1;
1945 ioctlp->outDatap = cp;
1947 lock_ReleaseMutex(&userp->mx);
1950 cm_RegisterNewTokenEvent(uuid, ucellp->sessionKey.data);
1956 long cm_IoctlDelToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1962 cm_SkipIoctlPath(ioctlp);
1964 cp = ioctlp->outDatap;
1966 /* cell name is right here */
1967 cellp = cm_GetCell(ioctlp->inDatap, 0);
1968 if (!cellp) return CM_ERROR_NOSUCHCELL;
1970 lock_ObtainMutex(&userp->mx);
1972 ucellp = cm_GetUCell(userp, cellp);
1974 lock_ReleaseMutex(&userp->mx);
1975 return CM_ERROR_NOMORETOKENS;
1978 osi_Log1(smb_logp,"cm_IoctlDelToken ucellp %lx", ucellp);
1980 if (ucellp->ticketp) {
1981 free(ucellp->ticketp);
1982 ucellp->ticketp = NULL;
1984 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1987 lock_ReleaseMutex(&userp->mx);
1989 cm_ResetACLCache(userp);
1994 long cm_IoctlDelAllToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1998 lock_ObtainMutex(&userp->mx);
2000 for (ucellp = userp->cellInfop; ucellp; ucellp = ucellp->nextp) {
2001 osi_Log1(smb_logp,"cm_IoctlDelAllToken ucellp %lx", ucellp);
2002 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
2006 lock_ReleaseMutex(&userp->mx);
2008 cm_ResetACLCache(userp);
2013 long cm_IoctlMakeSubmount(smb_ioctl_t *ioctlp, cm_user_t *userp)
2015 char afspath[MAX_PATH];
2017 int nextAutoSubmount;
2019 DWORD dwType, dwSize;
2024 cm_SkipIoctlPath(ioctlp);
2026 /* Serialize this one, to prevent simultaneous mods
2029 lock_ObtainMutex(&cm_Afsdsbmt_Lock);
2031 /* Parse the input parameters--first the required afs path,
2032 * then the requested submount name (which may be "").
2034 cm_NormalizeAfsPath (afspath, ioctlp->inDatap);
2035 submountreqp = ioctlp->inDatap + (strlen(ioctlp->inDatap)+1);
2037 /* If the caller supplied a suggested submount name, see if
2038 * that submount name is in use... if so, the submount's path
2039 * has to match our path.
2042 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
2043 "SOFTWARE\\OpenAFS\\Client\\Submounts",
2046 REG_OPTION_NON_VOLATILE,
2047 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
2052 if (submountreqp && *submountreqp) {
2053 char submountPathNormalized[MAX_PATH];
2054 char submountPath[MAX_PATH];
2056 dwSize = sizeof(submountPath);
2057 status = RegQueryValueEx( hkSubmounts, submountreqp, 0,
2058 &dwType, submountPath, &dwSize);
2060 if (status != ERROR_SUCCESS) {
2062 /* The suggested submount name isn't in use now--
2063 * so we can safely map the requested submount name
2064 * to the supplied path. Remember not to write the
2065 * leading "/afs" when writing out the submount.
2067 RegSetValueEx( hkSubmounts, submountreqp, 0,
2069 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2070 &afspath[strlen(cm_mountRoot)]:"/",
2071 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2072 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2074 RegCloseKey( hkSubmounts );
2075 strcpy(ioctlp->outDatap, submountreqp);
2076 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2077 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2081 /* The suggested submount name is already in use--if the
2082 * supplied path matches the submount's path, we can still
2083 * use the suggested submount name.
2085 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2086 if (!strcmp (submountPathNormalized, afspath)) {
2087 strcpy(ioctlp->outDatap, submountreqp);
2088 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2089 RegCloseKey( hkSubmounts );
2090 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2095 RegQueryInfoKey( hkSubmounts,
2097 NULL, /* lpcClass */
2098 NULL, /* lpReserved */
2099 NULL, /* lpcSubKeys */
2100 NULL, /* lpcMaxSubKeyLen */
2101 NULL, /* lpcMaxClassLen */
2102 &dwSubmounts, /* lpcValues */
2103 NULL, /* lpcMaxValueNameLen */
2104 NULL, /* lpcMaxValueLen */
2105 NULL, /* lpcbSecurityDescriptor */
2106 NULL /* lpftLastWriteTime */
2110 /* Having obtained a list of all available submounts, start
2111 * searching that list for a path which matches the requested
2112 * AFS path. We'll also keep track of the highest "auto15"/"auto47"
2113 * submount, in case we need to add a new one later.
2116 nextAutoSubmount = 1;
2118 for ( dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
2119 char submountPathNormalized[MAX_PATH];
2120 char submountPath[MAX_PATH] = "";
2121 DWORD submountPathLen = sizeof(submountPath);
2122 char submountName[MAX_PATH];
2123 DWORD submountNameLen = sizeof(submountName);
2126 RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
2127 &dwType, submountPath, &submountPathLen);
2128 if (dwType == REG_EXPAND_SZ) {
2130 StringCbCopyA(buf, MAX_PATH, submountPath);
2131 submountPathLen = ExpandEnvironmentStrings(buf, submountPath, MAX_PATH);
2132 if (submountPathLen > MAX_PATH)
2136 /* If this is an Auto### submount, remember its ### value */
2137 if ((!strnicmp (submountName, "auto", 4)) &&
2138 (isdigit (submountName[strlen("auto")]))) {
2139 int thisAutoSubmount;
2140 thisAutoSubmount = atoi (&submountName[strlen("auto")]);
2141 nextAutoSubmount = max (nextAutoSubmount,
2142 thisAutoSubmount+1);
2145 if ((submountPathLen == 0) ||
2146 (submountPathLen == sizeof(submountPath) - 1)) {
2150 /* See if the path for this submount matches the path
2151 * that our caller specified. If so, we can return
2154 cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2155 if (!strcmp (submountPathNormalized, afspath)) {
2156 strcpy(ioctlp->outDatap, submountName);
2157 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2158 RegCloseKey(hkSubmounts);
2159 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2165 /* We've been through the entire list of existing submounts, and
2166 * didn't find any which matched the specified path. So, we'll
2167 * just have to add one. Remember not to write the leading "/afs"
2168 * when writing out the submount.
2171 sprintf(ioctlp->outDatap, "auto%ld", nextAutoSubmount);
2173 RegSetValueEx( hkSubmounts,
2177 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2178 &afspath[strlen(cm_mountRoot)]:"/",
2179 (strlen(&afspath[strlen(cm_mountRoot)])) ?
2180 strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2182 ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2183 RegCloseKey(hkSubmounts);
2184 lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2188 long cm_IoctlGetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2190 memcpy(ioctlp->outDatap, &cryptall, sizeof(cryptall));
2191 ioctlp->outDatap += sizeof(cryptall);
2196 long cm_IoctlSetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2198 cm_SkipIoctlPath(ioctlp);
2200 memcpy(&cryptall, ioctlp->inDatap, sizeof(cryptall));
2206 extern int afsd_shutdown(int);
2207 extern int afs_shutdown;
2209 long cm_IoctlShutdown(smb_ioctl_t *ioctlp, cm_user_t *userp) {
2210 afs_shutdown = 1; /* flag to shut down */
2215 long cm_IoctlGetSMBName(smb_ioctl_t *ioctlp, cm_user_t *userp)
2217 smb_user_t *uidp = ioctlp->uidp;
2219 if (uidp && uidp->unp) {
2220 memcpy(ioctlp->outDatap, uidp->unp->name, strlen(uidp->unp->name));
2221 ioctlp->outDatap += strlen(uidp->unp->name);
2228 * functions to dump contents of various structures.
2229 * In debug build (linked with crt debug library) will dump allocated but not freed memory
2231 extern int cm_DumpSCache(FILE *outputFile, char *cookie);
2232 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie);
2233 extern int smb_DumpVCP(FILE *outputFile, char *cookie);
2235 long cm_IoctlMemoryDump(struct smb_ioctl *ioctlp, struct cm_user *userp)
2239 char logfileName[MAX_PATH+1];
2243 static _CrtMemState memstate;
2246 cm_SkipIoctlPath(ioctlp);
2247 memcpy(&inValue, ioctlp->inDatap, sizeof(long));
2251 strncpy(logfileName, getenv("TEMP"), MAX_PATH);
2252 logfileName[MAX_PATH] = '\0';
2256 GetWindowsDirectory(logfileName, sizeof(logfileName));
2258 strncat(logfileName, "\\afsd_alloc.log", sizeof(logfileName));
2260 hLogFile = CreateFile(logfileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2266 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2267 ioctlp->outDatap += sizeof(long);
2272 SetFilePointer(hLogFile, 0, NULL, FILE_END);
2274 cookie = inValue ? "b" : "e";
2280 _CrtMemCheckpoint(&memstate);
2284 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
2285 _CrtSetReportFile(_CRT_WARN, hLogFile);
2286 _CrtMemDumpAllObjectsSince(&memstate);
2290 /* dump all interesting data */
2291 cm_DumpSCache(hLogFile, cookie);
2292 cm_DumpBufHashTable(hLogFile, cookie);
2293 smb_DumpVCP(hLogFile, cookie);
2295 CloseHandle(hLogFile);
2297 memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2298 ioctlp->outDatap += sizeof(long);