rx-refcount-20041012
[openafs.git] / src / WINNT / afsd / cm_ioctl.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #ifndef DJGPP
14 #include <windows.h>
15 #else
16 #include <sys/socket.h>
17 #endif /* !DJGPP */
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <malloc.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <time.h>
24
25 #include <osi.h>
26
27 #include "afsd.h"
28 #include "afsd_init.h"
29
30 #include "smb.h"
31 #include "cm_server.h"
32
33 #ifndef DJGPP
34 #include <rx/rxkad.h>
35 #include "afsrpc.h"
36 #else
37 #include <rx/rxkad.h>
38 #include "afsrpc95.h"
39 #endif
40
41 #include "cm_rpc.h"
42 #include <strsafe.h>
43
44 #ifdef _DEBUG
45 #include <crtdbg.h>
46 #endif
47
48 /* Copied from afs_tokens.h */
49 #define PIOCTL_LOGON    0x1
50 #define MAX_PATH 260
51
52 osi_mutex_t cm_Afsdsbmt_Lock;
53
54 extern afs_int32 cryptall;
55 extern char cm_NetbiosName[];
56
57 extern void afsi_log(char *pattern, ...);
58
59 void cm_InitIoctl(void)
60 {
61     lock_InitializeMutex(&cm_Afsdsbmt_Lock, "AFSDSBMT.INI Access Lock");
62 }
63
64 long cm_FlushFile(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp)
65 {
66     long code;
67
68     lock_ObtainWrite(&scp->bufCreateLock);
69     code = buf_FlushCleanPages(scp, userp, reqp);
70         
71     lock_ObtainMutex(&scp->mx);
72     scp->cbServerp = NULL;
73     scp->cbExpires = 0;
74     lock_ReleaseMutex(&scp->mx);
75
76     lock_ReleaseWrite(&scp->bufCreateLock);
77     cm_dnlcPurgedp(scp);
78
79     return code;
80 }
81
82 /*
83  * cm_ResetACLCache -- invalidate ACL info for a user that has just
84  *                      obtained or lost tokens
85  */
86 void cm_ResetACLCache(cm_user_t *userp)
87 {
88     cm_scache_t *scp;
89     int hash;
90
91     lock_ObtainWrite(&cm_scacheLock);
92     for (hash=0; hash < cm_hashTableSize; hash++) {
93         for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
94             scp->refCount++;
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);
100             scp->refCount--;
101         }
102     }
103     lock_ReleaseWrite(&cm_scacheLock);
104 }       
105
106 /*
107  *  TranslateExtendedChars - This is a fix for TR 54482.
108  *
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.
118  */
119 void TranslateExtendedChars(char *str)
120 {
121 #ifdef DJGPP
122     char *p;
123 #endif
124
125     if (!str || !*str)
126         return;
127
128 #ifndef DJGPP
129     CharToOem(str, str);
130 #else
131     p = str;
132     while (*p) *p++ &= 0x7f;  /* turn off high bit; probably not right */
133 #endif
134 }
135         
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.
138  */
139 long cm_ParseIoctlPath(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
140         cm_scache_t **scpp)
141 {
142     long code;
143     cm_scache_t *substRootp;
144     char * relativePath = ioctlp->inDatap;
145
146     /* This is usually the file name, but for StatMountPoint it is the path. */
147     /* ioctlp->inDatap can be either of the form:
148      *    \path\.
149      *    \path\file
150      *    \\netbios-name\submount\path\.
151      *    \\netbios-name\submount\path\file
152      */
153     TranslateExtendedChars(relativePath);
154
155     if (relativePath[0] == relativePath[1] &&
156          relativePath[1] == '\\' && 
157          !_strnicmp(cm_NetbiosName,relativePath+2,strlen(cm_NetbiosName))) 
158     {
159         char shareName[256];
160         char *sharePath;
161         int shareFound, i;
162
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
167          */
168         char * p;
169         p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
170         if ( !_strnicmp("all", p, 3) )
171             p += 4;
172
173         for (i = 0; *p && *p != '\\'; i++,p++ ) {
174             shareName[i] = *p;
175         }
176         p++;                    /* skip past trailing slash */
177         shareName[i] = 0;       /* terminate string */
178
179         shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
180         if ( shareFound ) {
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);
185             free(sharePath);
186             if (code) 
187                 return code;
188
189             code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
190                              userp, NULL, reqp, scpp);
191             if (code) 
192                 return code;
193         } else {
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.
197              */
198             p = relativePath + 2 + strlen(cm_NetbiosName) + 1;
199             if ( !_strnicmp("all", p, 3) )
200                 p += 4;
201
202             shareName[0] = '/';
203             for (i = 1; *p && *p != '\\'; i++,p++ ) {
204                 shareName[i] = *p;
205             }
206             p++;                    /* skip past trailing slash */
207             shareName[i++] = '/';       /* add trailing slash */
208             shareName[i] = 0;       /* terminate string */
209
210
211             code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
212                              CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
213                              userp, shareName, reqp, &substRootp);
214             if (code) 
215                 return code;
216
217             code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
218                             userp, NULL, reqp, scpp);
219             if (code) 
220                 return code;
221         }
222     } else {
223         code = cm_NameI(cm_rootSCachep, ioctlp->prefix->data,
224                          CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
225                          userp, ioctlp->tidPathp, reqp, &substRootp);
226         if (code) 
227             return code;
228         
229         code = cm_NameI(substRootp, relativePath, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
230                          userp, NULL, reqp, scpp);
231         if (code) 
232             return code;
233     }
234
235     /* # of bytes of path */
236     code = strlen(ioctlp->inDatap) + 1;
237     ioctlp->inDatap += code;
238
239     /* This is usually nothing, but for StatMountPoint it is the file name. */
240     TranslateExtendedChars(ioctlp->inDatap);
241
242     /* and return success */
243     return 0;
244 }
245
246 void cm_SkipIoctlPath(smb_ioctl_t *ioctlp)
247 {
248     long temp;
249         
250     temp = strlen(ioctlp->inDatap) + 1;
251     ioctlp->inDatap += temp;
252 }       
253
254
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).
261  */
262 void cm_NormalizeAfsPath (char *outpathp, char *inpathp)
263 {
264     char *cp;
265     char bslash_mountRoot[256];
266        
267     strncpy(bslash_mountRoot, cm_mountRoot, sizeof(bslash_mountRoot) - 1);
268     bslash_mountRoot[0] = '\\';
269        
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);
278
279     for (cp = outpathp; *cp != 0; ++cp) {
280         if (*cp == '\\')
281             *cp = '/';
282     }       
283
284     if (strlen(outpathp) && (outpathp[strlen(outpathp)-1] == '/')) {
285         outpathp[strlen(outpathp)-1] = 0;
286     }
287
288     if (!strcmpi (outpathp, cm_mountRoot)) {
289         strcpy (outpathp, cm_mountRoot);
290     }
291 }
292
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.
295  */
296 long cm_ParseIoctlParent(smb_ioctl_t *ioctlp, cm_user_t *userp, cm_req_t *reqp,
297                          cm_scache_t **scpp, char *leafp)
298 {
299     long code;
300     char tbuffer[1024];
301     char *tp, *jp;
302     cm_scache_t *substRootp;
303
304     strcpy(tbuffer, ioctlp->inDatap);
305     tp = strrchr(tbuffer, '\\');
306     jp = strrchr(tbuffer, '/');
307     if (!tp)
308         tp = jp;
309     else if (jp && (tp - tbuffer) < (jp - tbuffer))
310         tp = jp;
311     if (!tp) {
312         strcpy(tbuffer, "\\");
313         if (leafp) 
314             strcpy(leafp, ioctlp->inDatap);
315     }
316     else {
317         *tp = 0;
318         if (leafp) 
319             strcpy(leafp, tp+1);
320     }   
321
322     if (tbuffer[0] == tbuffer[1] &&
323         tbuffer[1] == '\\' && 
324         !_strnicmp(cm_NetbiosName,tbuffer+2,strlen(cm_NetbiosName))) 
325     {
326         char shareName[256];
327         char *sharePath;
328         int shareFound, i;
329
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
334          */
335         char * p;
336         p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
337         if ( !_strnicmp("all", p, 3) )
338             p += 4;
339
340         for (i = 0; *p && *p != '\\'; i++,p++ ) {
341             shareName[i] = *p;
342         }
343         p++;                    /* skip past trailing slash */
344         shareName[i] = 0;       /* terminate string */
345
346         shareFound = smb_FindShare(ioctlp->fidp->vcp, ioctlp->uidp, shareName, &sharePath);
347         if ( shareFound ) {
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);
352             free(sharePath);
353             if (code) return code;
354
355             code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
356                              userp, NULL, reqp, scpp);
357             if (code) return code;
358         } else {
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.
362              */
363             p = tbuffer + 2 + strlen(cm_NetbiosName) + 1;
364             if ( !_strnicmp("all", p, 3) )
365                 p += 4;
366
367             shareName[0] = '/';
368             for (i = 1; *p && *p != '\\'; i++,p++ ) {
369                 shareName[i] = *p;
370             }
371             p++;                    /* skip past trailing slash */
372             shareName[i++] = '/';       /* add trailing slash */
373             shareName[i] = 0;       /* terminate string */
374
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;
379
380             code = cm_NameI(substRootp, p, CM_FLAG_CASEFOLD | CM_FLAG_FOLLOW,
381                             userp, NULL, reqp, scpp);
382             if (code) return code;
383         }
384     } else {
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;
389
390         code = cm_NameI(substRootp, tbuffer, CM_FLAG_FOLLOW,
391                         userp, NULL, reqp, scpp);
392         if (code) return code;
393     }
394
395     /* # of bytes of path */
396     code = strlen(ioctlp->inDatap) + 1;
397     ioctlp->inDatap += code;
398
399     /* and return success */
400     return 0;
401 }
402
403 long cm_IoctlGetACL(smb_ioctl_t *ioctlp, cm_user_t *userp)
404 {
405     cm_conn_t *connp;
406     cm_scache_t *scp;
407     AFSOpaque acl;
408     AFSFetchStatus fileStatus;
409     AFSVolSync volSync;
410     long code;
411     AFSFid fid;
412     int tlen;
413     cm_req_t req;
414     struct rx_connection * callp;
415
416     cm_InitReq(&req);
417
418     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
419     if (code) return code;
420
421     /* now make the get acl call */
422     fid.Volume = scp->fid.volume;
423     fid.Vnode = scp->fid.vnode;
424     fid.Unique = scp->fid.unique;
425     do {
426         acl.AFSOpaque_val = ioctlp->outDatap;
427         acl.AFSOpaque_len = 0;
428         code = cm_Conn(&scp->fid, userp, &req, &connp);
429         if (code) continue;
430
431         callp = cm_GetRxConn(connp);
432         code = RXAFS_FetchACL(callp, &fid, &acl, &fileStatus, &volSync);
433         rx_PutConnection(callp);
434
435     } while (cm_Analyze(connp, userp, &req, &scp->fid, &volSync, NULL, NULL, code));
436     code = cm_MapRPCError(code, &req);
437     cm_ReleaseSCache(scp);
438
439     if (code) return code;
440
441     /* skip over return data */
442     tlen = strlen(ioctlp->outDatap) + 1;
443     ioctlp->outDatap += tlen;
444
445     /* and return success */
446     return 0;
447 }
448
449 long cm_IoctlGetFileCellName(struct smb_ioctl *ioctlp, struct cm_user *userp)
450 {
451     long code;
452     cm_scache_t *scp;
453     cm_cell_t *cellp;
454     cm_req_t req;
455
456     cm_InitReq(&req);
457
458     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
459     if (code) return code;
460
461     cellp = cm_FindCellByID(scp->fid.cell);
462     if (cellp) {
463         strcpy(ioctlp->outDatap, cellp->namep);
464         ioctlp->outDatap += strlen(ioctlp->outDatap) + 1;
465         code = 0;
466     }
467     else code = CM_ERROR_NOSUCHCELL;
468
469     cm_ReleaseSCache(scp);
470     return code;
471 }
472
473 long cm_IoctlSetACL(struct smb_ioctl *ioctlp, struct cm_user *userp)
474 {
475     cm_conn_t *connp;
476     cm_scache_t *scp;
477     AFSOpaque acl;
478     AFSFetchStatus fileStatus;
479     AFSVolSync volSync;
480     long code;
481     AFSFid fid;
482     cm_req_t req;
483     struct rx_connection * callp;
484
485     cm_InitReq(&req);
486
487     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
488     if (code) return code;
489         
490     /* now make the get acl call */
491     fid.Volume = scp->fid.volume;
492     fid.Vnode = scp->fid.vnode;
493     fid.Unique = scp->fid.unique;
494     do {
495         acl.AFSOpaque_val = ioctlp->inDatap;
496         acl.AFSOpaque_len = strlen(ioctlp->inDatap)+1;
497         code = cm_Conn(&scp->fid, userp, &req, &connp);
498         if (code) continue;
499
500         callp = cm_GetRxConn(connp);
501         code = RXAFS_StoreACL(callp, &fid, &acl, &fileStatus, &volSync);
502         rx_PutConnection(callp);
503
504     } while (cm_Analyze(connp, userp, &req, &scp->fid, &volSync, NULL, NULL, code));
505     code = cm_MapRPCError(code, &req);
506
507     /* invalidate cache info, since we just trashed the ACL cache */
508     lock_ObtainMutex(&scp->mx);
509     cm_DiscardSCache(scp);
510     lock_ReleaseMutex(&scp->mx);
511
512     cm_ReleaseSCache(scp);
513
514     return code;
515 }
516
517 long cm_IoctlFlushVolume(struct smb_ioctl *ioctlp, struct cm_user *userp)
518 {
519     long code;
520     cm_scache_t *scp;
521     unsigned long volume;
522     int i;
523     cm_req_t req;
524
525     cm_InitReq(&req);
526
527     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
528     if (code) return code;
529         
530     volume = scp->fid.volume;
531     cm_ReleaseSCache(scp);
532
533     lock_ObtainWrite(&cm_scacheLock);
534     for (i=0; i<cm_hashTableSize; i++) {
535         for (scp = cm_hashTablep[i]; scp; scp = scp->nextp) {
536             if (scp->fid.volume == volume) {
537                 scp->refCount++;
538                 lock_ReleaseWrite(&cm_scacheLock);
539
540                 /* now flush the file */
541                 code = cm_FlushFile(scp, userp, &req);
542                 if ( code )
543                     afsi_log("cm_FlushFile returns error: [%x]",code);
544                 lock_ObtainWrite(&cm_scacheLock);
545                 scp->refCount--;
546             }
547         }
548     }
549     lock_ReleaseWrite(&cm_scacheLock);
550
551     return code;
552 }
553
554 long cm_IoctlFlushFile(struct smb_ioctl *ioctlp, struct cm_user *userp)
555 {
556     long code;
557     cm_scache_t *scp;
558     cm_req_t req;
559
560     cm_InitReq(&req);
561
562     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
563     if (code) return code;
564         
565     cm_FlushFile(scp, userp, &req);
566     cm_ReleaseSCache(scp);
567
568     return 0;
569 }
570
571 long cm_IoctlSetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
572 {
573     cm_scache_t *scp;
574     char volName[32];
575     char offLineMsg[256];
576     char motd[256];
577     cm_conn_t *tcp;
578     long code;
579     AFSFetchVolumeStatus volStat;
580     AFSStoreVolumeStatus storeStat;
581     cm_volume_t *tvp;
582     char *cp;
583     cm_cell_t *cellp;
584     cm_req_t req;
585     struct rx_connection * callp;
586
587     cm_InitReq(&req);
588
589     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
590     if (code) return code;
591
592     cellp = cm_FindCellByID(scp->fid.cell);
593     osi_assert(cellp);
594
595     if (scp->flags & CM_SCACHEFLAG_RO) {
596         cm_ReleaseSCache(scp);
597         return CM_ERROR_READONLY;
598     }
599
600     code = cm_GetVolumeByID(cellp, scp->fid.volume, userp, &req, &tvp);
601     if (code) {
602         cm_ReleaseSCache(scp);
603         return code;
604     }
605
606     /* Copy the junk out, using cp as a roving pointer. */
607     cp = ioctlp->inDatap;
608     memcpy((char *)&volStat, cp, sizeof(AFSFetchVolumeStatus));
609     cp += sizeof(AFSFetchVolumeStatus);
610     strcpy(volName, cp);
611     cp += strlen(volName)+1;
612     strcpy(offLineMsg, cp);
613     cp +=  strlen(offLineMsg)+1;
614     strcpy(motd, cp);
615     storeStat.Mask = 0;
616     if (volStat.MinQuota != -1) {
617         storeStat.MinQuota = volStat.MinQuota;
618         storeStat.Mask |= AFS_SETMINQUOTA;
619     }
620     if (volStat.MaxQuota != -1) {
621         storeStat.MaxQuota = volStat.MaxQuota;
622         storeStat.Mask |= AFS_SETMAXQUOTA;
623     }
624
625     do {
626         code = cm_Conn(&scp->fid, userp, &req, &tcp);
627         if (code) continue;
628
629         callp = cm_GetRxConn(tcp);
630         code = RXAFS_SetVolumeStatus(callp, scp->fid.volume,
631                                       &storeStat, volName, offLineMsg, motd);
632         rx_PutConnection(callp);
633
634     } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
635     code = cm_MapRPCError(code, &req);
636
637     /* return on failure */
638     cm_ReleaseSCache(scp);
639     if (code) {
640         return code;
641     }
642
643     /* we are sending parms back to make compat. with prev system.  should
644      * change interface later to not ask for current status, just set
645      * new status
646      */
647     cp = ioctlp->outDatap;
648     memcpy(cp, (char *)&volStat, sizeof(VolumeStatus));
649     cp += sizeof(VolumeStatus);
650     strcpy(cp, volName);
651     cp += strlen(volName)+1;
652     strcpy(cp, offLineMsg);
653     cp += strlen(offLineMsg)+1;
654     strcpy(cp, motd);
655     cp += strlen(motd)+1;
656
657     /* now return updated return data pointer */
658     ioctlp->outDatap = cp;
659
660     return 0;
661 }       
662
663 long cm_IoctlGetVolumeStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
664 {
665     char volName[32];
666     cm_scache_t *scp;
667     char offLineMsg[256];
668     char motd[256];
669     cm_conn_t *tcp;
670     register long code;
671     AFSFetchVolumeStatus volStat;
672     register char *cp;
673     char *Name;
674     char *OfflineMsg;
675     char *MOTD;
676     cm_req_t req;
677     struct rx_connection * callp;
678
679     cm_InitReq(&req);
680
681     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
682     if (code) return code;
683
684     Name = volName;
685     OfflineMsg = offLineMsg;
686     MOTD = motd;
687     do {
688         code = cm_Conn(&scp->fid, userp, &req, &tcp);
689         if (code) continue;
690
691         callp = cm_GetRxConn(tcp);
692         code = RXAFS_GetVolumeStatus(callp, scp->fid.volume,
693                                       &volStat, &Name, &OfflineMsg, &MOTD);
694         rx_PutConnection(callp);
695
696     } while (cm_Analyze(tcp, userp, &req, &scp->fid, NULL, NULL, NULL, code));
697     code = cm_MapRPCError(code, &req);
698
699     cm_ReleaseSCache(scp);
700     if (code) return code;
701
702     /* Copy all this junk into msg->im_data, keeping track of the lengths. */
703     cp = ioctlp->outDatap;
704     memcpy(cp, (char *)&volStat, sizeof(AFSFetchVolumeStatus));
705     cp += sizeof(AFSFetchVolumeStatus);
706     strcpy(cp, volName);
707     cp += strlen(volName)+1;
708     strcpy(cp, offLineMsg);
709     cp += strlen(offLineMsg)+1;
710     strcpy(cp, motd);
711     cp += strlen(motd)+1;
712
713     /* return new size */
714     ioctlp->outDatap = cp;
715
716     return 0;
717 }
718
719 long cm_IoctlWhereIs(struct smb_ioctl *ioctlp, struct cm_user *userp)
720 {
721     long code;
722     cm_scache_t *scp;
723     cm_cell_t *cellp;
724     cm_volume_t *tvp;
725     cm_serverRef_t **tsrpp, *current;
726     cm_server_t *tsp;
727     unsigned long volume;
728     char *cp;
729     cm_req_t req;
730
731     cm_InitReq(&req);
732
733     code = cm_ParseIoctlPath(ioctlp, userp, &req, &scp);
734     if (code) return code;
735         
736     volume = scp->fid.volume;
737
738     cellp = cm_FindCellByID(scp->fid.cell);
739     osi_assert(cellp);
740
741     cm_ReleaseSCache(scp);
742
743     code = cm_GetVolumeByID(cellp, volume, userp, &req, &tvp);
744     if (code) return code;
745         
746     cp = ioctlp->outDatap;
747         
748     lock_ObtainMutex(&tvp->mx);
749     tsrpp = cm_GetVolServers(tvp, volume);
750     lock_ObtainRead(&cm_serverLock);
751     for (current = *tsrpp; current; current = current->next) {
752         tsp = current->server;
753         memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
754         cp += sizeof(long);
755     }
756     lock_ReleaseRead(&cm_serverLock);
757     cm_FreeServerList(tsrpp);
758     lock_ReleaseMutex(&tvp->mx);
759
760     /* still room for terminating NULL, add it on */
761     volume = 0; /* reuse vbl */
762     memcpy(cp, (char *)&volume, sizeof(long));
763     cp += sizeof(long);
764
765     ioctlp->outDatap = cp;
766     cm_PutVolume(tvp);
767     return 0;
768 }       
769
770 long cm_IoctlStatMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
771 {
772     long code;
773     cm_scache_t *dscp;
774     cm_scache_t *scp;
775     char *cp;
776     cm_req_t req;
777
778     cm_InitReq(&req);
779
780     code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
781     if (code) return code;
782
783     cp = ioctlp->inDatap;
784
785     code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
786     cm_ReleaseSCache(dscp);
787     if (code) return code;
788         
789     lock_ObtainMutex(&scp->mx);
790
791     /* now check that this is a real mount point */
792     if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
793         lock_ReleaseMutex(&scp->mx);
794         cm_ReleaseSCache(scp);
795         return CM_ERROR_INVAL;
796     }
797
798     code = cm_ReadMountPoint(scp, userp, &req);
799     if (code == 0) {
800         cp = ioctlp->outDatap;
801         strcpy(cp, scp->mountPointStringp);
802         cp += strlen(cp) + 1;
803         ioctlp->outDatap = cp;
804     }
805     lock_ReleaseMutex(&scp->mx);
806     cm_ReleaseSCache(scp);
807
808     return code;
809 }       
810
811 long cm_IoctlDeleteMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
812 {
813     long code;
814     cm_scache_t *dscp;
815     cm_scache_t *scp;
816     char *cp;
817     cm_req_t req;
818
819     cm_InitReq(&req);
820
821     code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
822     if (code) return code;
823
824     cp = ioctlp->inDatap;
825
826     code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
827         
828     /* if something went wrong, bail out now */
829     if (code) {
830         goto done;
831     }
832         
833     lock_ObtainMutex(&scp->mx);
834     code = cm_SyncOp(scp, NULL, userp, &req, 0,
835                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
836     if (code) {     
837         lock_ReleaseMutex(&scp->mx);
838         cm_ReleaseSCache(scp);
839         goto done;
840     }
841
842     /* now check that this is a real mount point */
843     if (scp->fileType != CM_SCACHETYPE_MOUNTPOINT) {
844         lock_ReleaseMutex(&scp->mx);
845         cm_ReleaseSCache(scp);
846         code = CM_ERROR_INVAL;
847         goto done;
848     }
849
850     /* time to make the RPC, so drop the lock */
851     lock_ReleaseMutex(&scp->mx);
852     cm_ReleaseSCache(scp);
853
854     /* easier to do it this way */
855     code = cm_Unlink(dscp, cp, userp, &req);
856     if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
857         smb_NotifyChange(FILE_ACTION_REMOVED,
858                           FILE_NOTIFY_CHANGE_DIR_NAME,
859                           dscp, cp, NULL, TRUE);
860
861   done:
862     cm_ReleaseSCache(dscp);
863     return code;
864 }
865
866 long cm_IoctlCheckServers(struct smb_ioctl *ioctlp, struct cm_user *userp)
867 {
868     cm_cell_t *cellp;
869     chservinfo_t csi;
870     char *tp;
871     char *cp;
872     long temp;
873     cm_server_t *tsp;
874     int haveCell;
875         
876     cm_SkipIoctlPath(ioctlp);   /* we don't care about the path */
877     tp = ioctlp->inDatap;
878     haveCell = 0;
879
880     memcpy(&temp, tp, sizeof(temp));
881     if (temp == 0x12345678) {   /* For afs3.3 version */
882         memcpy(&csi, tp, sizeof(csi));
883         if (csi.tinterval >= 0) {
884             cp = ioctlp->outDatap;
885             memcpy(cp, (char *)&cm_daemonCheckInterval, sizeof(long));
886             ioctlp->outDatap += sizeof(long);
887             if (csi.tinterval > 0) {
888                 if (!smb_SUser(userp))
889                     return CM_ERROR_NOACCESS;
890                 cm_daemonCheckInterval = csi.tinterval;
891             }
892             return 0;
893         }
894         if (csi.tsize)
895             haveCell = 1;
896         temp = csi.tflags;
897         cp = csi.tbuffer;
898     } else {    /* For pre afs3.3 versions */
899         memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
900         ioctlp->inDatap = cp = ioctlp->inDatap + sizeof(long);
901         if (cp - ioctlp->inAllocp < ioctlp->inCopied)   /* still more data available */
902             haveCell = 1;
903     }       
904
905     /* 
906      * 1: fast check, don't contact servers.
907      * 2: local cell only.
908      */
909     if (haveCell) {
910         /* have cell name, too */
911         cellp = cm_GetCell(cp, 0);
912         if (!cellp) return CM_ERROR_NOSUCHCELL;
913     }
914     else cellp = (cm_cell_t *) 0;
915     if (!cellp && (temp & 2)) {
916         /* use local cell */
917         cellp = cm_FindCellByID(1);
918     }
919     if (!(temp & 1)) {  /* if not fast, call server checker routine */
920         /* check down servers */
921         cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS | CM_FLAG_CHECKUPSERVERS,
922                          cellp);
923     }       
924
925     /* now return the current down server list */
926     cp = ioctlp->outDatap;
927     lock_ObtainRead(&cm_serverLock);
928     for (tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
929         if (cellp && tsp->cellp != cellp) continue;     /* cell spec'd and wrong */
930         if ((tsp->flags & CM_SERVERFLAG_DOWN)
931              && tsp->type == CM_SERVER_FILE) {
932             memcpy(cp, (char *)&tsp->addr.sin_addr.s_addr, sizeof(long));
933             cp += sizeof(long);
934         }
935     }
936     lock_ReleaseRead(&cm_serverLock);
937
938     ioctlp->outDatap = cp;
939     return 0;
940 }
941
942 long cm_IoctlGag(struct smb_ioctl *ioctlp, struct cm_user *userp)
943 {
944     /* we don't print anything superfluous, so we don't support the gag call */
945     return CM_ERROR_INVAL;
946 }
947
948 long cm_IoctlCheckVolumes(struct smb_ioctl *ioctlp, struct cm_user *userp)
949 {
950     cm_CheckVolumes();
951     return 0;
952 }       
953
954 long cm_IoctlSetCacheSize(struct smb_ioctl *ioctlp, struct cm_user *userp)
955 {
956     long temp;
957     long code;
958
959     cm_SkipIoctlPath(ioctlp);
960
961     memcpy(&temp, ioctlp->inDatap, sizeof(temp));
962     if (temp == 0) 
963         temp = buf_nOrigBuffers;
964     else {
965         /* temp is in 1K units, convert to # of buffers */
966         temp = temp / (buf_bufferSize / 1024);
967     }       
968
969     /* now adjust the cache size */
970     code = buf_SetNBuffers(temp);
971
972     return code;
973 }
974
975 long cm_IoctlTraceControl(struct smb_ioctl *ioctlp, struct cm_user *userp)
976 {
977     long inValue;
978         
979     cm_SkipIoctlPath(ioctlp);
980         
981     memcpy(&inValue, ioctlp->inDatap, sizeof(long));
982
983     /* print trace */
984     if (inValue & 8) {
985         afsd_ForceTrace(FALSE);
986     }
987         
988     if (inValue & 2) {
989         /* set tracing value to low order bit */
990         if ((inValue & 1) == 0) {
991             /* disable tracing */
992             osi_LogDisable(afsd_logp);
993         }
994         else {
995             /* enable tracing */
996             osi_LogEnable(afsd_logp);
997         }
998     }
999
1000     /* see if we're supposed to do a reset, too */
1001     if (inValue & 4) {
1002         osi_LogReset(afsd_logp);
1003     }
1004
1005     /* and copy out tracing flag */
1006     inValue = afsd_logp->enabled;       /* use as a temp vbl */
1007     memcpy(ioctlp->outDatap, &inValue, sizeof(long));
1008     ioctlp->outDatap += sizeof(long);
1009     return 0;
1010 }       
1011
1012 long cm_IoctlGetCacheParms(struct smb_ioctl *ioctlp, struct cm_user *userp)
1013 {
1014     cm_cacheParms_t parms;
1015
1016     memset(&parms, 0, sizeof(parms));
1017
1018     /* first we get, in 1K units, the cache size */
1019     parms.parms[0] = buf_nbuffers * (buf_bufferSize / 1024);
1020
1021     /* and then the actual # of buffers in use (not in the free list, I guess,
1022      * will be what we do).
1023      */
1024     parms.parms[1] = (buf_nbuffers - buf_CountFreeList()) * (buf_bufferSize / 1024);
1025
1026     memcpy(ioctlp->outDatap, &parms, sizeof(parms));
1027     ioctlp->outDatap += sizeof(parms);
1028
1029     return 0;
1030 }
1031
1032 long cm_IoctlGetCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1033 {
1034     long whichCell;
1035     long magic = 0;
1036     cm_cell_t *tcellp;
1037     cm_serverRef_t *serverRefp;
1038     cm_server_t *serverp;
1039     long i;
1040     char *cp;
1041     char *tp;
1042     char *basep;
1043
1044     cm_SkipIoctlPath(ioctlp);
1045
1046     tp = ioctlp->inDatap;
1047
1048     memcpy((char *)&whichCell, tp, sizeof(long));
1049     tp += sizeof(long);
1050
1051     /* see if more than one long passed in, ignoring the null pathname (the -1) */
1052     if (ioctlp->inCopied-1 > sizeof(long)) {
1053         memcpy((char *)&magic, tp, sizeof(long));
1054     }
1055
1056     lock_ObtainRead(&cm_cellLock);
1057     for (tcellp = cm_allCellsp; tcellp; tcellp = tcellp->nextp) {
1058         if (whichCell == 0) break;
1059         whichCell--;
1060     }
1061     lock_ReleaseRead(&cm_cellLock);
1062     if (tcellp) {
1063         int max = 8;
1064
1065         cp = ioctlp->outDatap;
1066
1067         if (magic == 0x12345678) {
1068             memcpy(cp, (char *)&magic, sizeof(long));
1069             max = 13;
1070         }
1071         memset(cp, 0, max * sizeof(long));
1072         basep = cp;
1073         lock_ObtainRead(&cm_serverLock);        /* for going down server list */
1074         /* jaltman - do the reference counts to serverRefp contents need to be increased? */
1075         serverRefp = tcellp->vlServersp;
1076         for (i=0; i<max; i++) {
1077             if (!serverRefp) break;
1078             serverp = serverRefp->server;
1079             memcpy(cp, &serverp->addr.sin_addr.s_addr, sizeof(long));
1080             cp += sizeof(long);
1081             serverRefp = serverRefp->next;
1082         }
1083         lock_ReleaseRead(&cm_serverLock);
1084         cp = basep + max * sizeof(afs_int32);
1085         strcpy(cp, tcellp->namep);
1086         cp += strlen(tcellp->namep)+1;
1087         ioctlp->outDatap = cp;
1088     }
1089
1090     if (tcellp) 
1091         return 0;
1092     else 
1093         return CM_ERROR_NOMORETOKENS;   /* mapped to EDOM */
1094 }
1095
1096 extern long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep);
1097
1098 long cm_IoctlNewCell(struct smb_ioctl *ioctlp, struct cm_user *userp)
1099 {
1100     /* NT cache manager will read cell information from afsdcell.ini each time
1101      * cell is accessed. So, this call is necessary only if list of server for a cell 
1102      * changes (or IP addresses of cell servers changes).
1103      * All that needs to be done is to refresh server information for all cells that 
1104      * are already loaded.
1105   
1106      * cell list will be cm_CellLock and cm_ServerLock will be held for write.
1107      */  
1108   
1109     cm_cell_t *cp;
1110   
1111     cm_SkipIoctlPath(ioctlp);
1112     lock_ObtainWrite(&cm_cellLock);
1113   
1114     for (cp = cm_allCellsp; cp; cp=cp->nextp) 
1115     {
1116         long code;
1117         /* delete all previous server lists - cm_FreeServerList will ask for write on cm_ServerLock*/
1118         cm_FreeServerList(&cp->vlServersp);
1119         cp->vlServersp = NULL;
1120         code = cm_SearchCellFile(cp->namep, cp->namep, cm_AddCellProc, cp);
1121 #ifdef AFS_AFSDB_ENV
1122         if (code) {
1123             if (cm_dnsEnabled) {
1124                 int ttl;
1125                 code = cm_SearchCellByDNS(cp->namep, cp->namep, &ttl, cm_AddCellProc, cp);
1126                 if ( code == 0 ) { /* got cell from DNS */
1127                     cp->flags |= CM_CELLFLAG_DNS;
1128                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1129                     cp->timeout = time(0) + ttl;
1130                 }
1131             }
1132         } 
1133         else {
1134             cp->flags &= ~CM_CELLFLAG_DNS;
1135         }
1136 #endif /* AFS_AFSDB_ENV */
1137         if (code) {
1138             cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
1139         }
1140         else {
1141             cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
1142             cm_RandomizeServer(&cp->vlServersp);
1143         }
1144     }
1145     
1146     lock_ReleaseWrite(&cm_cellLock);
1147     return 0;       
1148 }
1149
1150 long cm_IoctlGetWsCell(smb_ioctl_t *ioctlp, cm_user_t *userp)
1151 {
1152     /* if we don't know our default cell, return failure */
1153     if (cm_rootCellp == NULL) {
1154         return CM_ERROR_NOSUCHCELL;
1155     }
1156
1157     /* return the default cellname to the caller */
1158     strcpy(ioctlp->outDatap, cm_rootCellp->namep);
1159     ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
1160
1161     /* done: success */
1162     return 0;
1163 }
1164
1165 long cm_IoctlSysName(struct smb_ioctl *ioctlp, struct cm_user *userp)
1166 {
1167     long setSysName, foundname = 0;
1168     char *cp, *cp2, inname[MAXSYSNAME], outname[MAXSYSNAME];
1169     int t, count, num = 0;
1170     char **sysnamelist[MAXSYSNAME];
1171         
1172     cm_SkipIoctlPath(ioctlp);
1173
1174     memcpy(&setSysName, ioctlp->inDatap, sizeof(long));
1175     ioctlp->inDatap += sizeof(long);
1176         
1177     if (setSysName) {
1178         /* check my args */
1179         if ( setSysName < 0 || setSysName > MAXNUMSYSNAMES )
1180             return EINVAL;
1181         cp2 = ioctlp->inDatap;
1182         for ( cp=ioctlp->inDatap, count = 0; count < setSysName; count++ ) {
1183             /* won't go past end of ioctlp->inDatap since maxsysname*num < ioctlp->inDatap length */
1184             t = strlen(cp);
1185             if (t >= MAXSYSNAME || t <= 0)
1186                 return EINVAL;
1187             /* check for names that can shoot us in the foot */
1188             if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
1189                 return EINVAL;
1190             cp += t + 1;
1191         }
1192         /* args ok */
1193
1194         /* inname gets first entry in case we're being a translator */
1195         /* (we are never a translator) */
1196         t = strlen(ioctlp->inDatap);
1197         memcpy(inname, ioctlp->inDatap, t + 1);
1198         ioctlp->inDatap += t + 1;
1199         num = count;
1200     }
1201
1202     /* Not xlating, so local case */
1203     if (!cm_sysName)
1204         osi_panic("cm_IoctlSysName: !cm_sysName\n", __FILE__, __LINE__);
1205
1206     if (!setSysName) {      /* user just wants the info */
1207         strcpy(outname, cm_sysName);
1208         foundname = cm_sysNameCount;
1209         *sysnamelist = cm_sysNameList;
1210     } else {        
1211         /* Local guy; only root can change sysname */
1212         /* clear @sys entries from the dnlc, once afs_lookup can
1213          * do lookups of @sys entries and thinks it can trust them */
1214         /* privs ok, store the entry, ... */
1215         strcpy(cm_sysName, inname);
1216         if (setSysName > 1) {       /* ... or list */
1217             cp = ioctlp->inDatap;
1218             for (count = 1; count < setSysName; ++count) {
1219                 if (!cm_sysNameList[count])
1220                     osi_panic("cm_IoctlSysName: no cm_sysNameList entry to write\n",
1221                                __FILE__, __LINE__);
1222                 t = strlen(cp);
1223                 memcpy(cm_sysNameList[count], cp, t + 1);  /* include null */
1224                 cp += t + 1;
1225             }
1226         }
1227         cm_sysNameCount = setSysName;
1228     }
1229
1230     if (!setSysName) {
1231         /* return the sysname to the caller */
1232         cp = ioctlp->outDatap;
1233         memcpy(cp, (char *)&foundname, sizeof(afs_int32));
1234         cp += sizeof(afs_int32);        /* skip found flag */
1235         if (foundname) {
1236             strcpy(cp, outname);
1237             cp += strlen(outname) + 1;  /* skip name and terminating null char */
1238             for ( count=1; count < foundname ; ++count) {   /* ... or list */
1239                 if ( !(*sysnamelist)[count] )
1240                     osi_panic("cm_IoctlSysName: no cm_sysNameList entry to read\n", 
1241                                __FILE__, __LINE__);
1242                 t = strlen((*sysnamelist)[count]);
1243                 if (t >= MAXSYSNAME)
1244                     osi_panic("cm_IoctlSysName: sysname entry garbled\n", 
1245                                __FILE__, __LINE__);
1246                 strcpy(cp, (*sysnamelist)[count]);
1247                 cp += t + 1;
1248             }
1249         }
1250         ioctlp->outDatap = cp;
1251     }
1252         
1253     /* done: success */
1254     return 0;
1255 }
1256
1257 long cm_IoctlGetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1258 {
1259     long temp;
1260     cm_cell_t *cellp;
1261
1262     cm_SkipIoctlPath(ioctlp);
1263
1264     cellp = cm_GetCell(ioctlp->inDatap, 0);
1265     if (!cellp) 
1266         return CM_ERROR_NOSUCHCELL;
1267
1268     temp = 0;
1269     lock_ObtainMutex(&cellp->mx);
1270     if (cellp->flags & CM_CELLFLAG_SUID)
1271         temp |= CM_SETCELLFLAG_SUID;
1272     lock_ReleaseMutex(&cellp->mx);
1273         
1274     /* now copy out parm */
1275     memcpy(ioctlp->outDatap, &temp, sizeof(long));
1276     ioctlp->outDatap += sizeof(long);
1277
1278     return 0;
1279 }
1280
1281 long cm_IoctlSetCellStatus(struct smb_ioctl *ioctlp, struct cm_user *userp)
1282 {
1283     long temp;
1284     cm_cell_t *cellp;
1285
1286     cm_SkipIoctlPath(ioctlp);
1287
1288     cellp = cm_GetCell(ioctlp->inDatap + 2*sizeof(long), 0);
1289     if (!cellp) 
1290         return CM_ERROR_NOSUCHCELL;
1291
1292     memcpy((char *)&temp, ioctlp->inDatap, sizeof(long));
1293
1294     lock_ObtainMutex(&cellp->mx);
1295     if (temp & CM_SETCELLFLAG_SUID)
1296         cellp->flags |= CM_CELLFLAG_SUID;
1297     else
1298         cellp->flags &= ~CM_CELLFLAG_SUID;
1299     lock_ReleaseMutex(&cellp->mx);
1300
1301     return 0;
1302 }
1303
1304 long cm_IoctlSetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1305 {
1306     cm_SSetPref_t         *spin; /* input */
1307     cm_SPref_t        *srvin;   /* one input component */
1308     cm_server_t       *tsp;
1309     int                   i, vlonly, noServers, type;
1310     struct sockaddr_in  tmp;
1311     unsigned short        rank;
1312
1313     cm_SkipIoctlPath(ioctlp);       /* we don't care about the path */
1314
1315     spin           = (cm_SSetPref_t *)ioctlp->inDatap;
1316     noServers  = spin->num_servers;
1317     vlonly     = spin->flags;
1318     if ( vlonly )
1319         type = CM_SERVER_VLDB;
1320     else    
1321         type = CM_SERVER_FILE;
1322
1323     for ( i=0; i < noServers; i++) 
1324     {
1325         srvin          = &(spin->servers[i]);
1326         rank           = srvin->rank + (rand() & 0x000f);
1327         tmp.sin_addr   = srvin->host;
1328         tmp.sin_family = AF_INET;
1329
1330         tsp = cm_FindServer(&tmp, type);
1331         if ( tsp )              /* an existing server - ref count increased */
1332         {
1333             tsp->ipRank = rank; /* no need to protect by mutex*/
1334
1335             if ( type == CM_SERVER_FILE) /* fileserver */
1336             {
1337                 /* find volumes which might have RO copy 
1338                 /* on server and change the ordering of 
1339                 ** their RO list */
1340                     cm_ChangeRankVolume(tsp);
1341             }
1342             else        
1343             {
1344                 /* set preferences for an existing vlserver */
1345                 cm_ChangeRankCellVLServer(tsp);
1346             }
1347             cm_PutServer(tsp);  /* decrease refcount */
1348         }
1349         else    /* add a new server without a cell */
1350         {
1351             tsp = cm_NewServer(&tmp, type, NULL); /* refcount = 1 */
1352             tsp->ipRank = rank;
1353         }
1354     }
1355     return 0;
1356 }
1357
1358 long cm_IoctlGetSPrefs(struct smb_ioctl *ioctlp, struct cm_user *userp)
1359 {
1360     cm_SPrefRequest_t *spin; /* input */
1361     cm_SPrefInfo_t    *spout;   /* output */
1362     cm_SPref_t        *srvout;   /* one output component */
1363     cm_server_t       *tsp;
1364     int                   i, vlonly, noServers;
1365
1366     cm_SkipIoctlPath(ioctlp);       /* we don't care about the path */
1367
1368     spin      = (cm_SPrefRequest_t *)ioctlp->inDatap;
1369     spout     = (cm_SPrefInfo_t *) ioctlp->outDatap;
1370     srvout    = spout->servers;
1371     noServers = spin->num_servers; 
1372     vlonly    = spin->flags & CM_SPREF_VLONLY;
1373     spout->num_servers = 0;
1374
1375     lock_ObtainRead(&cm_serverLock); /* get server lock */
1376
1377     for (tsp=cm_allServersp, i=0; tsp && noServers; tsp=tsp->allNextp,i++){
1378         if (spin->offset > i) {
1379             continue;    /* catch up to where we left off */
1380         }
1381
1382         if ( vlonly && (tsp->type == CM_SERVER_FILE) )
1383             continue;   /* ignore fileserver for -vlserver option*/
1384         if ( !vlonly && (tsp->type == CM_SERVER_VLDB) )
1385             continue;   /* ignore vlservers */
1386
1387         srvout->host = tsp->addr.sin_addr;
1388         srvout->rank = tsp->ipRank;
1389         srvout++;       
1390         spout->num_servers++;
1391         noServers--;
1392     }
1393     lock_ReleaseRead(&cm_serverLock); /* release server lock */
1394
1395     if ( tsp )  /* we ran out of space in the output buffer */
1396         spout->next_offset = i;
1397     else    
1398         spout->next_offset = 0; 
1399     ioctlp->outDatap += sizeof(cm_SPrefInfo_t) + 
1400         (spout->num_servers -1 ) * sizeof(cm_SPref_t) ;
1401     return 0;
1402 }
1403
1404 long cm_IoctlStoreBehind(struct smb_ioctl *ioctlp, struct cm_user *userp)
1405 {
1406     /* we ignore default asynchrony since we only have one way
1407      * of doing this today.
1408      */
1409     return 0;
1410 }       
1411
1412 long cm_IoctlCreateMountPoint(struct smb_ioctl *ioctlp, struct cm_user *userp)
1413 {
1414     char leaf[256];
1415     long code;
1416     cm_scache_t *dscp;
1417     cm_attr_t tattr;
1418     char *cp;
1419     cm_req_t req;
1420     char mpInfo[256];
1421     char fullCell[256];
1422     char volume[256];
1423     char cell[256];
1424     int ttl;
1425
1426     cm_InitReq(&req);
1427         
1428     code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1429     if (code) return code;
1430
1431     /* Translate chars for the mount point name */
1432     TranslateExtendedChars(leaf);
1433
1434     /* 
1435      * The fs command allows the user to specify partial cell names on NT.  These must
1436      * be expanded to the full cell name for mount points so that the mount points will
1437      * work on UNIX clients.
1438      */
1439
1440     /* Extract the possibly partial cell name */
1441     strcpy(cell, ioctlp->inDatap + 1);      /* Skip the mp type character */
1442         
1443     if (cp = strchr(cell, ':')) {
1444         /* Extract the volume name */
1445         *cp = 0;
1446         strcpy(volume,  cp + 1);
1447         
1448         /* Get the full name for this cell */
1449         code = cm_SearchCellFile(cell, fullCell, 0, 0);
1450 #ifdef AFS_AFSDB_ENV
1451         if (code && cm_dnsEnabled)
1452             code = cm_SearchCellByDNS(cell, fullCell, &ttl, 0, 0);
1453 #endif
1454         if (code)
1455             return CM_ERROR_NOSUCHCELL;
1456         
1457         sprintf(mpInfo, "%c%s:%s", *ioctlp->inDatap, fullCell, volume);
1458     } else {
1459         /* No cell name specified */
1460         strcpy(mpInfo, ioctlp->inDatap);
1461     }
1462
1463 #ifdef AFS_FREELANCE_CLIENT
1464     if (cm_freelanceEnabled && dscp == cm_rootSCachep) {
1465         /* we are adding the mount point to the root dir., so call
1466          * the freelance code to do the add. */
1467         osi_Log0(afsd_logp,"IoctlCreateMountPoint within Freelance root dir");
1468         code = cm_FreelanceAddMount(leaf, fullCell, volume, 
1469                                     *ioctlp->inDatap == '%', NULL);
1470         return code;
1471     }
1472 #endif
1473     /* create the symlink with mode 644.  The lack of X bits tells
1474      * us that it is a mount point.
1475      */
1476     tattr.mask = CM_ATTRMASK_UNIXMODEBITS | CM_ATTRMASK_CLIENTMODTIME;
1477     tattr.unixModeBits = 0644;
1478     tattr.clientModTime = time(NULL);
1479
1480     code = cm_SymLink(dscp, leaf, mpInfo, 0, &tattr, userp, &req);
1481     if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1482         smb_NotifyChange(FILE_ACTION_ADDED,
1483                          FILE_NOTIFY_CHANGE_DIR_NAME,
1484                          dscp, leaf, NULL, TRUE);
1485
1486     cm_ReleaseSCache(dscp);
1487     return code;
1488 }
1489
1490 long cm_IoctlSymlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1491 {
1492     char leaf[256];
1493     long code;
1494     cm_scache_t *dscp;
1495     cm_attr_t tattr;
1496     char *cp;
1497     cm_req_t req;
1498
1499     cm_InitReq(&req);
1500
1501     code = cm_ParseIoctlParent(ioctlp, userp, &req, &dscp, leaf);
1502     if (code) return code;
1503
1504     /* Translate chars for the link name */
1505     TranslateExtendedChars(leaf);
1506
1507     /* Translate chars for the linked to name */
1508     TranslateExtendedChars(ioctlp->inDatap);
1509
1510     cp = ioctlp->inDatap;               /* contents of link */
1511
1512     /* Create symlink with mode 0755. */
1513     tattr.mask = CM_ATTRMASK_UNIXMODEBITS;
1514     tattr.unixModeBits = 0755;
1515
1516     code = cm_SymLink(dscp, leaf, cp, 0, &tattr, userp, &req);
1517     if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1518         smb_NotifyChange(FILE_ACTION_ADDED,
1519                           FILE_NOTIFY_CHANGE_FILE_NAME
1520                           | FILE_NOTIFY_CHANGE_DIR_NAME,
1521                           dscp, leaf, NULL, TRUE);
1522
1523     cm_ReleaseSCache(dscp);
1524
1525     return code;
1526 }
1527
1528 extern long cm_AssembleLink(cm_scache_t *linkScp, char *pathSuffixp,
1529                             cm_scache_t **newRootScpp, cm_space_t **newSpaceBufferp,
1530                             cm_user_t *userp, cm_req_t *reqp);
1531
1532 long cm_IoctlListlink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1533 {
1534     long code;
1535     cm_scache_t *dscp;
1536     cm_scache_t *scp;
1537     char *cp;
1538     cm_space_t *spacep;
1539     cm_scache_t *newRootScp;
1540     cm_req_t req;
1541
1542     cm_InitReq(&req);
1543
1544     code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1545     if (code) return code;
1546
1547     cp = ioctlp->inDatap;
1548
1549     code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1550     cm_ReleaseSCache(dscp);
1551     if (code) return code;
1552
1553     /* Check that it's a real symlink */
1554     if (scp->fileType != CM_SCACHETYPE_SYMLINK){
1555         cm_ReleaseSCache(scp);
1556         return CM_ERROR_INVAL;
1557     }
1558
1559     code = cm_AssembleLink(scp, "", &newRootScp, &spacep, userp, &req);
1560     cm_ReleaseSCache(scp);
1561     if (code == 0) {
1562         cp = ioctlp->outDatap;
1563         if (newRootScp != NULL) {
1564             strcpy(cp, cm_mountRoot);
1565             strcat(cp, "/");
1566             cp += strlen(cp);
1567         }
1568         strcpy(cp, spacep->data);
1569         cp += strlen(cp) + 1;
1570         ioctlp->outDatap = cp;
1571         cm_FreeSpace(spacep);
1572         if (newRootScp != NULL)
1573             cm_ReleaseSCache(newRootScp);
1574     }       
1575
1576     return code;
1577 }
1578
1579 long cm_IoctlIslink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1580 {/*CHECK FOR VALID SYMLINK*/
1581     long code;
1582     cm_scache_t *dscp;
1583     cm_scache_t *scp;
1584     char *cp;
1585     cm_req_t req;
1586
1587     cm_InitReq(&req);
1588
1589     code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1590     if (code) return code;
1591
1592     cp = ioctlp->inDatap;
1593     osi_LogEvent("cm_IoctlListlink",NULL," name[%s]",cp);
1594
1595     code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1596     cm_ReleaseSCache(dscp);
1597     if (code) return code;
1598
1599     /* Check that it's a real symlink */
1600     if (scp->fileType != CM_SCACHETYPE_SYMLINK)
1601         code = CM_ERROR_INVAL;
1602     cm_ReleaseSCache(scp);
1603     return code;
1604 }
1605
1606 long cm_IoctlDeletelink(struct smb_ioctl *ioctlp, struct cm_user *userp)
1607 {
1608     long code;
1609     cm_scache_t *dscp;
1610     cm_scache_t *scp;
1611     char *cp;
1612     cm_req_t req;
1613
1614     cm_InitReq(&req);
1615
1616     code = cm_ParseIoctlPath(ioctlp, userp, &req, &dscp);
1617     if (code) return code;
1618
1619     cp = ioctlp->inDatap;
1620
1621     code = cm_Lookup(dscp, cp, CM_FLAG_NOMOUNTCHASE, userp, &req, &scp);
1622         
1623     /* if something went wrong, bail out now */
1624     if (code) {
1625         goto done;
1626     }
1627         
1628     lock_ObtainMutex(&scp->mx);
1629     code = cm_SyncOp(scp, NULL, userp, &req, 0,
1630                       CM_SCACHESYNC_NEEDCALLBACK | CM_SCACHESYNC_GETSTATUS);
1631     if (code) {     
1632         lock_ReleaseMutex(&scp->mx);
1633         cm_ReleaseSCache(scp);
1634         goto done;
1635     }
1636         
1637     /* now check that this is a real symlink */
1638     if (scp->fileType != CM_SCACHETYPE_SYMLINK) {
1639         lock_ReleaseMutex(&scp->mx);
1640         cm_ReleaseSCache(scp);
1641         code = CM_ERROR_INVAL;
1642         goto done;
1643     }
1644         
1645     /* time to make the RPC, so drop the lock */
1646     lock_ReleaseMutex(&scp->mx);
1647     cm_ReleaseSCache(scp);
1648         
1649     /* easier to do it this way */
1650     code = cm_Unlink(dscp, cp, userp, &req);
1651     if (code == 0 && (dscp->flags & CM_SCACHEFLAG_ANYWATCH))
1652         smb_NotifyChange(FILE_ACTION_REMOVED,
1653                           FILE_NOTIFY_CHANGE_FILE_NAME
1654                           | FILE_NOTIFY_CHANGE_DIR_NAME,
1655                           dscp, cp, NULL, TRUE);
1656
1657   done:
1658     cm_ReleaseSCache(dscp);
1659     return code;
1660 }
1661
1662 long cm_IoctlSetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1663 {
1664     char *saveDataPtr;
1665     char *tp;
1666     int ticketLen;
1667     char *ticket;
1668     int ctSize;
1669     struct ClearToken ct;
1670     cm_cell_t *cellp;
1671     cm_ucell_t *ucellp;
1672     char *uname = NULL;
1673     afs_uuid_t uuid;
1674     int flags;
1675     char sessionKey[8];
1676     char *smbname;
1677
1678     saveDataPtr = ioctlp->inDatap;
1679
1680     cm_SkipIoctlPath(ioctlp);
1681
1682     tp = ioctlp->inDatap;
1683
1684     /* ticket length */
1685     memcpy(&ticketLen, tp, sizeof(ticketLen));
1686     tp += sizeof(ticketLen);
1687     if (ticketLen < MINKTCTICKETLEN || ticketLen > MAXKTCTICKETLEN)
1688         return CM_ERROR_INVAL;
1689
1690     /* remember ticket and skip over it for now */
1691     ticket = tp;
1692     tp += ticketLen;
1693
1694     /* clear token size */
1695     memcpy(&ctSize, tp, sizeof(ctSize));
1696     tp += sizeof(ctSize);
1697     if (ctSize != sizeof(struct ClearToken))
1698         return CM_ERROR_INVAL;
1699
1700     /* clear token */
1701     memcpy(&ct, tp, ctSize);
1702     tp += ctSize;
1703     if (ct.AuthHandle == -1)
1704         ct.AuthHandle = 999;    /* more rxvab compat stuff */
1705
1706     /* more stuff, if any */
1707     if (ioctlp->inCopied > tp - saveDataPtr) {
1708         /* flags:  logon flag */
1709         memcpy(&flags, tp, sizeof(int));
1710         tp += sizeof(int);
1711
1712         /* cell name */
1713         cellp = cm_GetCell(tp, CM_FLAG_CREATE);
1714         if (!cellp) return CM_ERROR_NOSUCHCELL;
1715         tp += strlen(tp) + 1;
1716
1717         /* user name */
1718         uname = tp;
1719         tp += strlen(tp) + 1;
1720
1721         if (flags & PIOCTL_LOGON) {
1722             /* SMB user name with which to associate tokens */
1723             smbname = tp;
1724             osi_Log2(smb_logp,"cm_IoctlSetToken for user [%s] smbname [%s]",
1725                      osi_LogSaveString(smb_logp,uname), osi_LogSaveString(smb_logp,smbname));
1726             fprintf(stderr, "SMB name = %s\n", smbname);
1727             tp += strlen(tp) + 1;
1728         } else {
1729             osi_Log1(smb_logp,"cm_IoctlSetToken for user [%s]",
1730                      osi_LogSaveString(smb_logp,uname));
1731         }
1732
1733 #ifndef DJGPP   /* for win95, session key is back in pioctl */
1734                 /* uuid */
1735         memcpy(&uuid, tp, sizeof(uuid));
1736         if (!cm_FindTokenEvent(uuid, sessionKey))
1737             return CM_ERROR_INVAL;
1738 #endif /* !DJGPP */
1739     } else {
1740         cellp = cm_rootCellp;
1741         osi_Log0(smb_logp,"cm_IoctlSetToken - no name specified");
1742     }
1743
1744     if (flags & PIOCTL_LOGON) {
1745         userp = smb_FindCMUserByName(smbname, ioctlp->fidp->vcp->rname);
1746     }
1747
1748     /* store the token */
1749     lock_ObtainMutex(&userp->mx);
1750     ucellp = cm_GetUCell(userp, cellp);
1751     osi_Log1(smb_logp,"cm_IoctlSetToken ucellp %lx", ucellp);
1752     ucellp->ticketLen = ticketLen;
1753     if (ucellp->ticketp)
1754         free(ucellp->ticketp);  /* Discard old token if any */
1755     ucellp->ticketp = malloc(ticketLen);
1756     memcpy(ucellp->ticketp, ticket, ticketLen);
1757 #ifndef DJGPP
1758     /*
1759      * Get the session key from the RPC, rather than from the pioctl.
1760      */
1761     /*
1762     memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1763     */
1764     memcpy(ucellp->sessionKey.data, sessionKey, sizeof(sessionKey));
1765 #else
1766     /* for win95, we are getting the session key from the pioctl */
1767     memcpy(&ucellp->sessionKey, ct.HandShakeKey, sizeof(ct.HandShakeKey));
1768 #endif /* !DJGPP */
1769     ucellp->kvno = ct.AuthHandle;
1770     ucellp->expirationTime = ct.EndTimestamp;
1771     ucellp->gen++;
1772     if (uname) strcpy(ucellp->userName, uname);
1773     ucellp->flags |= CM_UCELLFLAG_RXKAD;
1774     lock_ReleaseMutex(&userp->mx);
1775
1776     if (flags & PIOCTL_LOGON) {
1777         ioctlp->flags |= SMB_IOCTLFLAG_LOGON;
1778     }
1779
1780     cm_ResetACLCache(userp);
1781
1782     return 0;
1783 }
1784
1785 long cm_IoctlGetTokenIter(struct smb_ioctl *ioctlp, struct cm_user *userp)
1786 {
1787     char *tp, *cp;
1788     int iterator;
1789     int temp;
1790     cm_ucell_t *ucellp;
1791     struct ClearToken ct;
1792
1793     cm_SkipIoctlPath(ioctlp);
1794
1795     tp = ioctlp->inDatap;
1796     cp = ioctlp->outDatap;
1797
1798     /* iterator */
1799     memcpy(&iterator, tp, sizeof(iterator));
1800     tp += sizeof(iterator);
1801
1802     lock_ObtainMutex(&userp->mx);
1803
1804     /* look for token */
1805     for (;;iterator++) {
1806         ucellp = cm_FindUCell(userp, iterator);
1807         if (!ucellp) {
1808             lock_ReleaseMutex(&userp->mx);
1809             return CM_ERROR_NOMORETOKENS;
1810         }
1811         if (ucellp->flags & CM_UCELLFLAG_RXKAD)
1812             break;
1813     }       
1814
1815     /* new iterator */
1816     temp = ucellp->iterator + 1;
1817     memcpy(cp, &temp, sizeof(temp));
1818     cp += sizeof(temp);
1819
1820     /* ticket length */
1821     memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1822     cp += sizeof(ucellp->ticketLen);
1823
1824     /* ticket */
1825     memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1826     cp += ucellp->ticketLen;
1827
1828     /* clear token size */
1829     temp = sizeof(ct);
1830     memcpy(cp, &temp, sizeof(temp));
1831     cp += sizeof(temp);
1832
1833     /* clear token */
1834     ct.AuthHandle = ucellp->kvno;
1835 #ifndef DJGPP
1836     /*
1837      * Don't give out a real session key here
1838      */
1839     /*
1840     memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1841     */
1842     memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1843 #else
1844     memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1845 #endif /* !DJGPP */
1846     ct.ViceId = 37;                     /* XXX */
1847     ct.BeginTimestamp = 0;              /* XXX */
1848     ct.EndTimestamp = ucellp->expirationTime;
1849     memcpy(cp, &ct, sizeof(ct));
1850     cp += sizeof(ct);
1851
1852     /* Primary flag (unused) */
1853     temp = 0;
1854     memcpy(cp, &temp, sizeof(temp));
1855     cp += sizeof(temp);
1856
1857     /* cell name */
1858     strcpy(cp, ucellp->cellp->namep);
1859     cp += strlen(cp) + 1;
1860
1861     /* user name */
1862     strcpy(cp, ucellp->userName);
1863     cp += strlen(cp) + 1;
1864
1865     ioctlp->outDatap = cp;
1866
1867     lock_ReleaseMutex(&userp->mx);
1868
1869     return 0;
1870 }
1871
1872 long cm_IoctlGetToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1873 {
1874     char *cp;
1875     int temp;
1876     cm_cell_t *cellp;
1877     cm_ucell_t *ucellp;
1878     struct ClearToken ct;
1879     char *tp;
1880 #ifndef DJGPP
1881     afs_uuid_t uuid;
1882 #endif /* !DJGPP */
1883
1884     cm_SkipIoctlPath(ioctlp);
1885
1886     tp = ioctlp->inDatap;
1887
1888     cp = ioctlp->outDatap;
1889
1890     /* cell name is right here */
1891     cellp = cm_GetCell(tp, 0);
1892     if (!cellp) return CM_ERROR_NOSUCHCELL;
1893     tp += strlen(tp) + 1;
1894
1895 #ifndef DJGPP
1896     /* uuid */
1897     memcpy(&uuid, tp, sizeof(uuid));
1898 #endif /* !DJGPP */
1899
1900     lock_ObtainMutex(&userp->mx);
1901
1902     ucellp = cm_GetUCell(userp, cellp);
1903     if (!ucellp || !(ucellp->flags & CM_UCELLFLAG_RXKAD)) {
1904         lock_ReleaseMutex(&userp->mx);
1905         return CM_ERROR_NOMORETOKENS;
1906     }
1907
1908     /* ticket length */
1909     memcpy(cp, &ucellp->ticketLen, sizeof(ucellp->ticketLen));
1910     cp += sizeof(ucellp->ticketLen);
1911
1912     /* ticket */
1913     memcpy(cp, ucellp->ticketp, ucellp->ticketLen);
1914     cp += ucellp->ticketLen;
1915
1916     /* clear token size */
1917     temp = sizeof(ct);
1918     memcpy(cp, &temp, sizeof(temp));
1919     cp += sizeof(temp);
1920
1921     /* clear token */
1922     ct.AuthHandle = ucellp->kvno;
1923 #ifndef DJGPP
1924     /*
1925      * Don't give out a real session key here
1926      */
1927     /*
1928     memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1929     */
1930     memset(ct.HandShakeKey, 0, sizeof(ct.HandShakeKey));
1931 #else
1932     memcpy(ct.HandShakeKey, &ucellp->sessionKey, sizeof(ct.HandShakeKey));
1933 #endif /* !DJGPP */
1934     ct.ViceId = 37;                     /* XXX */
1935     ct.BeginTimestamp = 0;              /* XXX */
1936     ct.EndTimestamp = ucellp->expirationTime;
1937     memcpy(cp, &ct, sizeof(ct));
1938     cp += sizeof(ct);
1939
1940     /* Primary flag (unused) */
1941     temp = 0;
1942     memcpy(cp, &temp, sizeof(temp));
1943     cp += sizeof(temp);
1944
1945     /* cell name */
1946     strcpy(cp, ucellp->cellp->namep);
1947     cp += strlen(cp) + 1;
1948
1949     /* user name */
1950     strcpy(cp, ucellp->userName);
1951     cp += strlen(cp) + 1;
1952
1953     ioctlp->outDatap = cp;
1954
1955     lock_ReleaseMutex(&userp->mx);
1956
1957 #ifndef DJGPP
1958     cm_RegisterNewTokenEvent(uuid, ucellp->sessionKey.data);
1959 #endif /* !DJGPP */
1960
1961     return 0;
1962 }
1963
1964 long cm_IoctlDelToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
1965 {
1966     char *cp;
1967     cm_cell_t *cellp;
1968     cm_ucell_t *ucellp;
1969
1970     cm_SkipIoctlPath(ioctlp);
1971
1972     cp = ioctlp->outDatap;
1973
1974     /* cell name is right here */
1975     cellp = cm_GetCell(ioctlp->inDatap, 0);
1976     if (!cellp) return CM_ERROR_NOSUCHCELL;
1977
1978     lock_ObtainMutex(&userp->mx);
1979
1980     ucellp = cm_GetUCell(userp, cellp);
1981     if (!ucellp) {
1982         lock_ReleaseMutex(&userp->mx);
1983         return CM_ERROR_NOMORETOKENS;
1984     }
1985
1986     osi_Log1(smb_logp,"cm_IoctlDelToken ucellp %lx", ucellp);
1987
1988     if (ucellp->ticketp) {
1989         free(ucellp->ticketp);
1990         ucellp->ticketp = NULL;
1991     }
1992     ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
1993     ucellp->gen++;
1994
1995     lock_ReleaseMutex(&userp->mx);
1996
1997     cm_ResetACLCache(userp);
1998
1999     return 0;
2000 }
2001
2002 long cm_IoctlDelAllToken(struct smb_ioctl *ioctlp, struct cm_user *userp)
2003 {
2004     cm_ucell_t *ucellp;
2005
2006     lock_ObtainMutex(&userp->mx);
2007
2008     for (ucellp = userp->cellInfop; ucellp; ucellp = ucellp->nextp) {
2009         osi_Log1(smb_logp,"cm_IoctlDelAllToken ucellp %lx", ucellp);
2010         ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
2011         ucellp->gen++;
2012     }
2013
2014     lock_ReleaseMutex(&userp->mx);
2015
2016     cm_ResetACLCache(userp);
2017
2018     return 0;
2019 }
2020
2021 long cm_IoctlMakeSubmount(smb_ioctl_t *ioctlp, cm_user_t *userp)
2022 {
2023     char afspath[MAX_PATH];
2024     char *submountreqp;
2025     int nextAutoSubmount;
2026     HKEY hkSubmounts;
2027     DWORD dwType, dwSize;
2028     DWORD status;
2029     DWORD dwIndex;
2030     DWORD dwSubmounts;
2031
2032     cm_SkipIoctlPath(ioctlp);
2033
2034     /* Serialize this one, to prevent simultaneous mods
2035      * to afsdsbmt.ini
2036      */
2037     lock_ObtainMutex(&cm_Afsdsbmt_Lock);
2038
2039     /* Parse the input parameters--first the required afs path,
2040      * then the requested submount name (which may be "").
2041      */
2042     cm_NormalizeAfsPath (afspath, ioctlp->inDatap);
2043     submountreqp = ioctlp->inDatap + (strlen(ioctlp->inDatap)+1);
2044
2045     /* If the caller supplied a suggested submount name, see if
2046      * that submount name is in use... if so, the submount's path
2047      * has to match our path.
2048      */
2049
2050     RegCreateKeyEx( HKEY_LOCAL_MACHINE, 
2051                     "SOFTWARE\\OpenAFS\\Client\\Submounts",
2052                     0, 
2053                     "AFS", 
2054                     REG_OPTION_NON_VOLATILE,
2055                     KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
2056                     NULL, 
2057                     &hkSubmounts,
2058                     NULL );
2059
2060     if (submountreqp && *submountreqp) {
2061         char submountPathNormalized[MAX_PATH];
2062         char submountPath[MAX_PATH];
2063
2064         dwSize = sizeof(submountPath);
2065         status = RegQueryValueEx( hkSubmounts, submountreqp, 0,
2066                                   &dwType, submountPath, &dwSize);
2067
2068         if (status != ERROR_SUCCESS) {
2069
2070             /* The suggested submount name isn't in use now--
2071              * so we can safely map the requested submount name
2072              * to the supplied path. Remember not to write the
2073              * leading "/afs" when writing out the submount.
2074              */
2075             RegSetValueEx( hkSubmounts, submountreqp, 0,
2076                            REG_EXPAND_SZ, 
2077                            (strlen(&afspath[strlen(cm_mountRoot)])) ?
2078                            &afspath[strlen(cm_mountRoot)]:"/",
2079                            (strlen(&afspath[strlen(cm_mountRoot)])) ?
2080                            strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2081
2082             RegCloseKey( hkSubmounts );
2083                         strcpy(ioctlp->outDatap, submountreqp);
2084                         ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2085                         lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2086             return 0;
2087         }
2088
2089         /* The suggested submount name is already in use--if the
2090          * supplied path matches the submount's path, we can still
2091          * use the suggested submount name.
2092          */
2093         cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2094         if (!strcmp (submountPathNormalized, afspath)) {
2095             strcpy(ioctlp->outDatap, submountreqp);
2096             ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2097             RegCloseKey( hkSubmounts );
2098             lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2099             return 0;
2100         }
2101     }
2102
2103     RegQueryInfoKey( hkSubmounts,
2104                      NULL,  /* lpClass */
2105                      NULL,  /* lpcClass */
2106                      NULL,  /* lpReserved */
2107                      NULL,  /* lpcSubKeys */
2108                      NULL,  /* lpcMaxSubKeyLen */
2109                      NULL,  /* lpcMaxClassLen */
2110                      &dwSubmounts, /* lpcValues */
2111                      NULL,  /* lpcMaxValueNameLen */
2112                      NULL,  /* lpcMaxValueLen */
2113                      NULL,  /* lpcbSecurityDescriptor */
2114                      NULL   /* lpftLastWriteTime */
2115                      );
2116
2117
2118     /* Having obtained a list of all available submounts, start
2119      * searching that list for a path which matches the requested
2120      * AFS path. We'll also keep track of the highest "auto15"/"auto47"
2121      * submount, in case we need to add a new one later.
2122      */
2123
2124     nextAutoSubmount = 1;
2125
2126     for ( dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
2127         char submountPathNormalized[MAX_PATH];
2128         char submountPath[MAX_PATH] = "";
2129         DWORD submountPathLen = sizeof(submountPath);
2130         char submountName[MAX_PATH];
2131         DWORD submountNameLen = sizeof(submountName);
2132
2133         dwType = 0;
2134         RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
2135                       &dwType, submountPath, &submountPathLen);
2136         if (dwType == REG_EXPAND_SZ) {
2137             char buf[MAX_PATH];
2138             StringCbCopyA(buf, MAX_PATH, submountPath);
2139             submountPathLen = ExpandEnvironmentStrings(buf, submountPath, MAX_PATH);
2140             if (submountPathLen > MAX_PATH)
2141                 continue;
2142         }
2143
2144         /* If this is an Auto### submount, remember its ### value */
2145         if ((!strnicmp (submountName, "auto", 4)) &&
2146              (isdigit (submountName[strlen("auto")]))) {
2147             int thisAutoSubmount;
2148             thisAutoSubmount = atoi (&submountName[strlen("auto")]);
2149             nextAutoSubmount = max (nextAutoSubmount,
2150                                      thisAutoSubmount+1);
2151         }       
2152
2153         if ((submountPathLen == 0) ||
2154              (submountPathLen == sizeof(submountPath) - 1)) {
2155             continue;
2156         }
2157
2158         /* See if the path for this submount matches the path
2159          * that our caller specified. If so, we can return
2160          * this submount.
2161          */
2162         cm_NormalizeAfsPath (submountPathNormalized, submountPath);
2163         if (!strcmp (submountPathNormalized, afspath)) {
2164             strcpy(ioctlp->outDatap, submountName);
2165             ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2166             RegCloseKey(hkSubmounts);
2167             lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2168             return 0;
2169
2170         }
2171     }
2172
2173     /* We've been through the entire list of existing submounts, and
2174      * didn't find any which matched the specified path. So, we'll
2175      * just have to add one. Remember not to write the leading "/afs"
2176      * when writing out the submount.
2177      */
2178
2179     sprintf(ioctlp->outDatap, "auto%ld", nextAutoSubmount);
2180
2181     RegSetValueEx( hkSubmounts, 
2182                    ioctlp->outDatap,
2183                    0,
2184                    REG_EXPAND_SZ, 
2185                    (strlen(&afspath[strlen(cm_mountRoot)])) ?
2186                    &afspath[strlen(cm_mountRoot)]:"/",
2187                    (strlen(&afspath[strlen(cm_mountRoot)])) ?
2188                    strlen(&afspath[strlen(cm_mountRoot)])+1:2);
2189
2190     ioctlp->outDatap += strlen(ioctlp->outDatap) +1;
2191     RegCloseKey(hkSubmounts);
2192     lock_ReleaseMutex(&cm_Afsdsbmt_Lock);
2193     return 0;
2194 }
2195
2196 long cm_IoctlGetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2197 {
2198     memcpy(ioctlp->outDatap, &cryptall, sizeof(cryptall));
2199     ioctlp->outDatap += sizeof(cryptall);
2200
2201     return 0;
2202 }
2203
2204 long cm_IoctlSetRxkcrypt(smb_ioctl_t *ioctlp, cm_user_t *userp)
2205 {
2206     cm_SkipIoctlPath(ioctlp);
2207
2208     memcpy(&cryptall, ioctlp->inDatap, sizeof(cryptall));
2209
2210     return 0;
2211 }
2212
2213 #ifdef DJGPP
2214 extern int afsd_shutdown(int);
2215 extern int afs_shutdown;
2216
2217 long cm_IoctlShutdown(smb_ioctl_t *ioctlp, cm_user_t *userp) {
2218   afs_shutdown = 1;   /* flag to shut down */
2219   return 0;
2220 }
2221 #endif /* DJGPP */
2222
2223 long cm_IoctlGetSMBName(smb_ioctl_t *ioctlp, cm_user_t *userp)
2224 {
2225   smb_user_t *uidp = ioctlp->uidp;
2226
2227   if (uidp && uidp->unp) {
2228     memcpy(ioctlp->outDatap, uidp->unp->name, strlen(uidp->unp->name));
2229     ioctlp->outDatap += strlen(uidp->unp->name);
2230   }
2231
2232   return 0;
2233 }
2234
2235 /* 
2236  * functions to dump contents of various structures. 
2237  * In debug build (linked with crt debug library) will dump allocated but not freed memory
2238  */
2239 extern int cm_DumpSCache(FILE *outputFile, char *cookie);
2240 extern int cm_DumpBufHashTable(FILE *outputFile, char *cookie);
2241 extern int smb_DumpVCP(FILE *outputFile, char *cookie);
2242
2243 long cm_IoctlMemoryDump(struct smb_ioctl *ioctlp, struct cm_user *userp)
2244 {
2245     long inValue = 0;
2246     HANDLE hLogFile;
2247     char logfileName[MAX_PATH+1];
2248     char *cookie;
2249   
2250 #ifdef _DEBUG  
2251     static _CrtMemState memstate;
2252 #endif
2253   
2254     cm_SkipIoctlPath(ioctlp);
2255     memcpy(&inValue, ioctlp->inDatap, sizeof(long));
2256   
2257     if (getenv("TEMP"))
2258     {
2259         strncpy(logfileName, getenv("TEMP"), MAX_PATH);
2260         logfileName[MAX_PATH] = '\0';
2261     }
2262     else
2263     {
2264         GetWindowsDirectory(logfileName, sizeof(logfileName));
2265     }
2266     strncat(logfileName, "\\afsd_alloc.log", sizeof(logfileName));
2267
2268     hLogFile = CreateFile(logfileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2269   
2270     if (!hLogFile)
2271     {
2272       /* error */
2273       inValue = -1;
2274       memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2275       ioctlp->outDatap += sizeof(long);
2276       
2277       return 0;               
2278     }
2279   
2280     SetFilePointer(hLogFile, 0, NULL, FILE_END);
2281   
2282     cookie = inValue ? "b" : "e";
2283   
2284 #ifdef _DEBUG  
2285   
2286     if (inValue)
2287     {
2288       _CrtMemCheckpoint(&memstate);           
2289     }
2290     else
2291     {
2292         _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
2293         _CrtSetReportFile(_CRT_WARN, hLogFile);
2294         _CrtMemDumpAllObjectsSince(&memstate);
2295     }
2296 #endif
2297   
2298     /* dump all interesting data */
2299     cm_DumpSCache(hLogFile, cookie);
2300     cm_DumpBufHashTable(hLogFile, cookie);
2301     smb_DumpVCP(hLogFile, cookie);
2302
2303     CloseHandle(hLogFile);                          
2304   
2305     memcpy(ioctlp->outDatap, &inValue, sizeof(long));
2306     ioctlp->outDatap += sizeof(long);
2307   
2308     return 0;
2309 }