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