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