837499dd962b616dacfe513f030650c44b7d8260
[openafs.git] / src / uss / uss_vol.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 /*
11  *      Implementation of the volume operations used by the AFS user
12  *      account facility.
13  */
14
15 /*
16  * --------------------- Required definitions ---------------------
17  */
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21
22 #include "uss_vol.h"            /*Interface to this module */
23 #include "uss_common.h"         /*Common definitions */
24 #include "uss_procs.h"          /*Defs from procs module */
25 #include "uss_fs.h"             /*CacheManager ops */
26 #include "uss_acl.h"
27 #include <sys/stat.h>
28 #include <pwd.h>
29 #include <netdb.h>
30 #include <errno.h>
31
32 #include <string.h>
33
34 #include <afs/com_err.h>
35 #include <afs/vlserver.h>
36 #include <afs/vldbint.h>
37 #include <afs/auth.h>
38 #include <afs/cellconfig.h>
39 #include <rx/rx_globals.h>
40 #include <afs/volser.h>
41 #include <afs/volint.h>
42 #include <afs/keys.h>
43 #include <afs/afsutil.h>
44 #include <ubik.h>
45
46 extern int line;
47
48 /*
49  * ---------------------- Private definitions ---------------------
50  */
51 #undef USS_VOL_DB
52 #undef USS_VOL_DB_SHOW_OVERRIDES
53
54
55 /*
56  * --------------------- Exported definitions ---------------------
57  */
58 /*
59  * The Volume Server interface imports the Ubik connection
60  * structure to use, expecting it to be named "cstruct".  This
61  * is why we have two names here.  Thus, the UV_CreateVolume()
62  * will work and we can avoid nasty little core dumps.
63  */
64 struct ubik_client *uconn_vldbP;        /*Ubik connection struct */
65 struct ubik_client *cstruct;    /*Required name for above */
66
67 /*
68  * ------------------------ Private globals -----------------------
69  */
70 static int initDone = 0;        /*Module initialized? */
71 static int NoAuthFlag = 0;      /*Use -noauth? */
72 static struct rx_connection
73  *serverconns[VLDB_MAXSERVERS]; /*Connection(s) to VLDB
74                                  * server(s) */
75
76
77 /*-----------------------------------------------------------------------
78  * static InitThisModule
79  *
80  * Description:
81  *      Set up this module, namely set up all the client state for
82  *      dealing with the Volume Location Server(s), including
83  *      network connections.
84  *
85  * Arguments:
86  *      a_noAuthFlag : Do we need authentication?
87  *      a_confDir    : Configuration directory to use.
88  *      a_cellName   : Cell we want to talk to.
89  *
90  * Returns:
91  *      0 if everything went fine, or
92  *      lower-level error code otherwise.
93  *
94  * Environment:
95  *      This routine will only be called once.
96  *
97  * Side Effects:
98  *      As advertised.
99  *------------------------------------------------------------------------*/
100
101 static afs_int32
102 InitThisModule(int a_noAuthFlag, char *a_confDir, char *a_cellName)
103 {                               /*InitThisModule */
104 #ifdef USS_VOL_DB
105     static char rn[] = "uss_vol:InitThisModule";
106 #endif
107     register afs_int32 code;    /*Return code */
108     struct afsconf_dir *tdir;   /*Ptr to conf dir info */
109     struct afsconf_cell info;   /*Info about chosen cell */
110     struct ktc_principal sname; /*Service name */
111     struct ktc_token ttoken;    /*Service ticket */
112     afs_int32 scIndex;          /*Chosen security index */
113     struct rx_securityClass *sc;        /*Generated security object */
114     afs_int32 i;                /*Loop index */
115
116     /*
117      * Only once, guys, will 'ya?
118      */
119     if (initDone) {
120 #ifdef USS_VOL_DB
121         printf("[%s] Called multiple times!\n", rn);
122 #endif /* USS_VOL_DB */
123         return (0);
124     }
125
126     /*
127      * Set up our Rx environment.
128      */
129 #ifdef USS_VOL_DB
130     printf("[%s] Initializing Rx environment\n", rn);
131 #endif /* USS_VOL_DB */
132     code = rx_Init(0);
133     if (code) {
134         fprintf(stderr, "%s:  Couldn't initialize Rx.\n", uss_whoami);
135         return (code);
136     }
137     rx_SetRxDeadTime(50);
138
139     /*
140      * Find out all about our configuration.
141      */
142 #ifdef USS_VOL_DB
143     printf("[%s] Handling configuration info\n", rn);
144 #endif /* USS_VOL_DB */
145     tdir = afsconf_Open(a_confDir);
146     if (!tdir) {
147         fprintf(stderr, "%s: Couldn't open configuration directory (%s).\n",
148                 uss_whoami, a_confDir);
149         return (-1);
150     }
151     code = afsconf_GetCellInfo(tdir, a_cellName, AFSCONF_VLDBSERVICE, &info);
152     if (code) {
153         printf("%s: Can't find VLDB server(s) for cell %s\n", uss_whoami,
154                a_cellName);
155         exit(1);
156     }
157 #ifdef USS_VOL_DB
158     printf("[%s] Getting tickets if needed\n", rn);
159 #endif /* USS_VOL_DB */
160     if (!a_noAuthFlag) {
161         /*
162          * We don't need tickets for unauthenticated connections.
163          */
164         strcpy(sname.cell, info.name);
165         sname.instance[0] = 0;
166         strcpy(sname.name, "afs");
167         code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
168         if (code) {
169             fprintf(stderr,
170                     "%s: Couldn't get AFS tokens, running unauthenticated.\n",
171                     uss_whoami);
172             scIndex = 0;
173         } else {
174             /*
175              * We got a ticket, go for an authenticated connection.
176              */
177             if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
178                 scIndex = 2;    /*Kerberos */
179             else {
180                 fprintf(stderr, "%s: Funny kvno (%d) in ticket, proceeding\n",
181                         uss_whoami, ttoken.kvno);
182                 scIndex = 2;
183             }
184         }                       /*Got a ticket */
185     } /*Authentication desired */
186     else
187         scIndex = 0;
188
189     /*
190      * Generate the appropriate security object for the connection.
191      */
192 #ifdef USS_VOL_DB
193     printf("[%s] Generating Rx security object\n", rn);
194 #endif /* USS_VOL_DB */
195     switch (scIndex) {
196     case 0:
197         sc = (struct rx_securityClass *)
198             rxnull_NewClientSecurityObject();
199         break;
200
201     case 1:
202         break;
203
204     case 2:
205         sc = (struct rx_securityClass *)
206             rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
207                                           ttoken.kvno, ttoken.ticketLen,
208                                           ttoken.ticket);
209         break;
210     }
211
212     /*
213      * Tell UV module about default authentication.
214      */
215 #ifdef USS_VOL_DB
216     printf("[%s] Setting UV security: obj 0x%x, index %d\n", rn, sc, scIndex);
217 #endif /* USS_VOL_DB */
218     UV_SetSecurity(sc, scIndex);
219     if (info.numServers > VLDB_MAXSERVERS) {
220         fprintf(stderr, "%s: info.numServers=%d (> VLDB_MAXSERVERS=%d)\n",
221                 uss_whoami, info.numServers, VLDB_MAXSERVERS);
222         exit(1);
223     }
224
225     /*
226      * Connect to each VLDB server for the chosen cell.
227      */
228     for (i = 0; i < info.numServers; i++) {
229 #ifdef USS_VOL_DB
230         printf
231             ("[%s] Connecting to VLDB server 0x%x, port %d, service id %d\n",
232              rn, info.hostAddr[i].sin_addr.s_addr, info.hostAddr[i].sin_port,
233              USER_SERVICE_ID);
234 #endif /* USS_VOL_DB */
235         serverconns[i] =
236             rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
237                              info.hostAddr[i].sin_port, USER_SERVICE_ID, sc,
238                              scIndex);
239     }
240
241     /*
242      * Set up to execute Ubik transactions on the VLDB.
243      */
244 #ifdef USS_VOL_DB
245     printf("[%s] Initializing Ubik interface\n", rn);
246 #endif /* USS_VOL_DB */
247     code = ubik_ClientInit(serverconns, &uconn_vldbP);
248     if (code) {
249         fprintf(stderr, "%s: Ubik client init failed.\n", uss_whoami);
250         return (code);
251     }
252 #ifdef USS_VOL_DB
253     printf("[%s] VLDB ubik connection structure at 0x%x\n", rn, uconn_vldbP);
254 #endif /* USS_VOL_DB */
255
256     /*
257      * Place the ubik VLDB connection structure in its advertised
258      * location.
259      */
260     cstruct = uconn_vldbP;
261
262     /*
263      * Success!
264      */
265     initDone = 1;
266     return (0);
267
268 }                               /*InitThisModule */
269
270
271 /*-----------------------------------------------------------------------
272  * static HostIDToHostName
273  *
274  * Description:
275  *      Given a host ID (in network byte order), figure out the
276  *      corresponding host name.
277  *
278  * Arguments:
279  *      a_hostID   : Host ID in network byte order.
280  *      a_hostName : Ptr to host name buffer.
281  *
282  * Returns:
283  *      Nothing.
284  *
285  * Environment:
286  *      This routine simply calls the hostutil_GetNameByINet()
287  *      function exported by the utility library (util.a).
288  *
289  * Side Effects:
290  *      As advertised.
291  *------------------------------------------------------------------------*/
292
293 static void
294 HostIDToHostName(afs_int32 a_hostID, char *a_hostName)
295 {                               /*HostIDToHostName */
296
297     strcpy(a_hostName, hostutil_GetNameByINet(a_hostID));
298
299 }                               /*HostIDToHostName */
300
301
302 /*-----------------------------------------------------------------------
303  * static PartIDToPartName
304  *
305  * Description:
306  *      Given a partition ID (in network byte order), figure out the
307  *      corresponding partition name.
308  *
309  * Arguments:
310  *      a_partID   : Partition ID in network byte order.
311  *      a_partName : Ptr to partition name buffer.
312  *
313  * Returns:
314  *      0 if everything went well, or
315  *      -1 if the given partition ID couldn't be translated.
316  *
317  * Environment:
318  *      Nothing interesting.
319  *
320  * Side Effects:
321  *      As advertised.
322  *------------------------------------------------------------------------*/
323
324 static afs_int32
325 PartIDToPartName(afs_int32 a_partID, char *a_partName)
326 {                               /*PartIDToPartName */
327 #ifdef USS_VOL_DB
328     static char rn[] = "PartIDToPartName";
329 #endif
330 #ifdef USS_VOL_DB
331     printf("[%s] Translating partition id %d to its name\n", rn, a_partID);
332 #endif /* USS_VOL_DB */
333
334     if ((a_partID < 0) || (a_partID > VOLMAXPARTS))
335         return (-1);
336
337     if (a_partID < 26) {
338         strcpy(a_partName, "/vicep");
339         a_partName[6] = a_partID + 'a';
340         a_partName[7] = '\0';
341     } else {
342         strcpy(a_partName, "/vicep");
343         a_partID -= 26;
344         a_partName[6] = 'a' + (a_partID / 26);
345         a_partName[7] = 'a' + (a_partID % 26);
346         a_partName[8] = '\0';
347     }
348
349 #ifdef USS_VOL_DB
350     printf("[%s] Translation for part ID %d is '%s'\n", rn, a_partID,
351            a_partName);
352 #endif /* USS_VOL_DB */
353     return (0);
354
355 }                               /*PartIDToPartName */
356
357
358 /*------------------------------------------------------------------------
359  * EXPORTED uss_Vol_GetServer
360  *
361  * Environment:
362  *      Nothing interesting.
363  *
364  * Side Effects:
365  *      As advertised.
366  *------------------------------------------------------------------------*/
367
368 afs_int32
369 uss_vol_GetServer(char *a_name)
370 {                               /*uss_vol_GetServer */
371
372     register struct hostent *th;
373     afs_int32 addr;
374     char b1, b2, b3, b4;
375     register afs_int32 code;
376
377     code = sscanf(a_name, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);
378     if (code == 4) {
379         /*
380          * Parsed as 128.2.9.4, or similar; return it in network
381          * byte order (128 in byte 0).
382          */
383         addr =
384             (((afs_int32) b1) << 24) | (((afs_int32) b2) << 16) |
385             (((afs_int32) b3) << 8) | (afs_int32) b4;
386         return htonl(addr);
387     }
388
389     th = gethostbyname(a_name);
390     if (!th)
391         return (0);
392     memcpy(&addr, th->h_addr, sizeof(addr));
393     return (addr);
394
395 }                               /*uss_vol_GetServer */
396
397 /* XXX - This function is unused, and could be deleted */
398 #if 0
399 /*------------------------------------------------------------------------
400  * static GetVolumeType
401  *
402  * Description:
403  *      Translate the given char string representing a volume type to the
404  *      numeric representation.
405  *
406  * Arguments:
407  *      a_type : Char string volume type.
408  *
409  * Returns:
410  *      One of ROVOL, RWVOL, BACKVOL, or -1 on failure.
411  *
412  * Environment:
413  *      Nothing interesting.
414  *
415  * Side Effects:
416  *      As advertised.
417  *------------------------------------------------------------------------*/
418
419 static afs_int32
420 GetVolumeType(char *a_type)
421 {                               /*GetVolumeType */
422
423     if (!strcmp(a_type, "ro"))
424         return (ROVOL);
425     else if (!strcmp(a_type, "rw"))
426         return (RWVOL);
427     else if (!strcmp(a_type, "bk"))
428         return (BACKVOL);
429     else
430         return (-1);
431
432 }                               /*GetVolumeType */
433 #endif
434
435 /*------------------------------------------------------------------------
436  * EXPORTED uss_Vol_GetPartitionID
437  *
438  * Environment:
439  *      It is assumed that partition names may begin with ``/vicep''.
440  *
441  * Side Effects:
442  *      As advertised.
443  *------------------------------------------------------------------------*/
444
445 afs_int32
446 uss_vol_GetPartitionID(char *a_name)
447 {                               /*uss_vol_GetPartitionID */
448
449     register char tc;
450     char ascii[3];
451
452     tc = *a_name;
453     if (tc == 0)
454         return (-1);
455
456     /*
457      * Numbers go straight through.
458      */
459     if (tc >= '0' && tc <= '9') {
460         return (atoi(a_name));
461     }
462
463     /*
464      * Otherwise, check for vicepa or /vicepa, or just plain "a"
465      */
466     ascii[2] = 0;
467     if (strlen(a_name) <= 2) {
468         strcpy(ascii, a_name);
469     } else if (!strncmp(a_name, "/vicep", 6)) {
470         strncpy(ascii, a_name + 6, 2);
471     } else if (!strncmp(a_name, "vicep", 5)) {
472         strncpy(ascii, a_name + 5, 2);
473     } else
474         return (-1);
475
476     /*
477      * Now, partitions are named /vicepa ... /vicepz, /vicepaa, /vicepab,
478      * .../vicepzz, and are numbered from 0.  Do the appropriate conversion.
479      */
480     if (ascii[1] == 0) {
481         /*
482          * Single-char name, 0..25
483          */
484         if (ascii[0] < 'a' || ascii[0] > 'z')
485             return (-1);        /* wrongo */
486         return (ascii[0] - 'a');
487     } else {
488         /*
489          * Two-char name, 26 .. <whatever>
490          */
491         if (ascii[0] < 'a' || ascii[0] > 'z')
492             return (-1);
493         if (ascii[1] < 'a' || ascii[1] > 'z')
494             return (-1);
495         return ((ascii[0] - 'a') * 26 + (ascii[1] - 'a') + 26);
496     }
497 }                               /*uss_vol_GetPartitionID */
498
499
500 /*-----------------------------------------------------------------------
501  * static CheckDoubleMount
502  *
503  * Description:
504  *      Make sure we're not trying to mount a volume in the same place
505  *      twice.
506  *
507  * Arguments:
508  *      a_mp    : Mountpoint pathname to check.
509  *      a_oldmp : Ptr to buffer into which the old value of the
510  *                mountpoint is placed (if any).
511  *
512  * Returns:
513  *      0                 if the volume was not previously mounted.
514  *      uss_procs_ANCIENT if there was already a mountpoint AND the
515  *                        user was already recorded in the password
516  *                        file.
517  *      uss_procs_YOUNG   if there was a mountpoint for the user taken
518  *                        from the directory pool, yet the user was not
519  *                        yet in the password file.
520  *
521  * Environment:
522  *      Nothing interesting.
523  *
524  * Side Effects:
525  *      May fill in the a_oldmp buffer with the value of the old
526  *      mountpoint.
527  *------------------------------------------------------------------------*/
528
529 static int
530 CheckDoubleMount(char *a_mp, char *a_oldmp)
531 {                               /*CheckDoubleMount */
532
533 #ifdef USS_VOL_DB
534     static char rn[] = "uss_vol:CheckDoubleMount";
535 #endif
536     int start, len, mlen, tlen;
537     int i = 0;
538     struct passwd *pws;
539     struct stat stbuf;
540
541     pws = getpwuid(atoi(uss_Uid));
542     if (pws != NULL) {
543         /*
544          * User exists in the password file, so they've been fully
545          * created and integrated.  Return the ``ancient'' mountpoint.
546          */
547         strcpy(a_oldmp, pws->pw_dir);
548         return (uss_procs_ANCIENT);
549     }
550
551     if (uss_NumGroups) {
552         /*
553          * $AUTO used. Search among the possible directories.
554          */
555         len = strlen(uss_Auto);
556         mlen = strlen(a_mp);
557         while (strncmp(&a_mp[i], uss_Auto, len)) {
558             a_oldmp[i] = a_mp[i];
559             if (++i > (mlen - len)) {
560                 i = -1;
561                 break;
562             }
563         }
564         if ((start = i) != -1) {
565             /*
566              * $AUTO used in mountpoint.
567              */
568             for (i = 0; i < uss_NumGroups; i++) {
569                 /*
570                  * Copy in the base and tail components.
571                  */
572                 tlen = strlen(uss_DirPool[i]);
573                 strncpy(&a_oldmp[start], uss_DirPool[i], tlen);
574                 strcpy(&a_oldmp[start + tlen], &a_mp[start + len]);
575 #ifdef USS_VOL_DB
576                 printf("%s: Checking '%s' for mount point\n", rn, a_oldmp);
577 #endif /* USS_VOL_DB */
578                 if (lstat(a_oldmp, &stbuf) == 0)        /*mp exists */
579                     if (strcmp(a_oldmp, a_mp))
580                         /* and is different */
581                         /*
582                          * The old mount point exists and is different
583                          * from the current one, so return the fact
584                          * that we have a ``young'' mountpoint.
585                          */
586                         return (uss_procs_YOUNG);
587             }                   /*Check each $AUTO directory */
588         }
589     }
590
591     /*$AUTO has been used */
592     /*
593      * No luck finding the old mount point, so we just return that
594      * this is the first time we've seen this volume.
595      */
596     return (0);
597
598 }                               /*CheckDoubleMount */
599
600
601 /*------------------------------------------------------------------------
602  * EXPORTED uss_vol_CreateVol
603  *
604  * Environment:
605  *      Called from the code generated by the uss grammar.
606  *
607  * Side Effects:
608  *      As advertised.
609  *------------------------------------------------------------------------*/
610
611 afs_int32
612 uss_vol_CreateVol(char *a_volname, char *a_server, char *a_partition,
613                   char *a_quota, char *a_mpoint, char *a_owner, 
614                   char *a_acl)
615 {                               /*uss_vol_CreateVol */
616 #ifdef USS_VOL_DB
617     static char rn[] = "uss_vol_CreateVol";     /*Routine name */
618 #endif
619     afs_int32 pname;            /*Partition name */
620     afs_int32 volid, code;      /*Volume ID, return code */
621     afs_int32 saddr;            /*Socket info for server */
622     int VolExistFlag = 0;       /*Does the volume exist? */
623     int mpExistFlag = 0;        /*Does the mountpoint exist? */
624     char *Oldmpoint = NULL;     /*Old mountpoint name, if any */
625     char tmp_str[uss_MAX_SIZE]; /*Useful string buffer */
626     int o;                      /*Owner's user id */
627     char userinput[64];         /*User's input */
628     struct uss_subdir *new_dir; /*Used to remember original ACL */
629
630     /*
631      * Don't do anything if there's already a problem.
632      */
633     if (uss_syntax_err)
634         return (1);
635
636 #ifdef USS_VOL_DB
637     fprintf(stderr, "%s:uss_vol_CreateVol params:\n", rn);
638     fprintf(stderr,
639             "%s: volname '%s', server '%s', partition '%s', quota '%s', mpoint '%s', owner '%s', acl '%s'\n",
640             rn, a_volname, a_server, a_partition, a_quota, a_mpoint, a_owner,
641             a_acl);
642 #endif /* USS_VOL_DB */
643
644     /*
645      * All of the parameters passed in are taken from the template
646      * file.  Override these values if the user has explicitly set
647      * them, namely if the uss commons have non-null strings.
648      */
649     if (uss_Server[0] != '\0') {
650 #ifdef USS_VOL_DB_SHOW_OVERRIDES
651         if (uss_verbose)
652             fprintf(stderr,
653                     "%s: Overriding server field: template value is '%s', overridden to '%s'\n",
654                     rn, a_server, uss_Server);
655 #endif /* USS_VOL_DB_SHOW_OVERRIDES */
656         a_server = uss_Server;
657     }
658
659     if (uss_Partition[0] != '\0') {
660 #ifdef USS_VOL_DB_SHOW_OVERRIDES
661         if (uss_verbose)
662             fprintf(stderr,
663                     "%s: Overriding partition field: template value is '%s', overridden to '%s'\n",
664                     rn, a_partition, uss_Partition);
665 #endif /* USS_VOL_DB_SHOW_OVERRIDES */
666         a_partition = uss_Partition;
667     }
668
669     if (uss_MountPoint[0] != '\0') {
670 #ifdef USS_VOL_DB_SHOW_OVERRIDES
671         if (uss_verbose)
672             fprintf(stderr,
673                     "%s: overriding mountpoint field: template value is '%s', overridden to '%s'\n",
674                     rn, a_mpoint, uss_MountPoint);
675 #endif /* USS_VOL_DB_SHOW_OVERRIDES */
676         a_mpoint = uss_MountPoint;
677     }
678 #ifdef USS_VOL_DB_SHOW_OVERRIDES
679     printf("%s: Params after overrides:\n", uss_whoami);
680     printf
681         ("%s: volname '%s', server '%s', partition '%s', quota '%s', mpoint '%s', owner '%s', acl '%s'\n",
682          uss_whoami, a_volname, a_server, a_partition, a_quota, a_mpoint,
683          a_owner, a_acl);
684 #endif /* USS_VOL_DB_SHOW_OVERRIDES */
685
686     if (uss_verbose)
687         fprintf(stderr,
688                 "Creating volume '%s' on server '%s', partition '%s'\n",
689                 a_volname, a_server, a_partition);
690
691     saddr = uss_vol_GetServer(a_server);
692     if (!saddr) {
693         uss_procs_PrintErr(line,
694                            "File server '%s' not found in config info\n",
695                            a_server);
696         return (1);
697     }
698     pname = uss_vol_GetPartitionID(a_partition);
699     if (pname < 0) {
700         uss_procs_PrintErr(line, "Couldn't interpret partition name '%s'\n",
701                            a_partition);
702         return (1);
703     }
704
705     /*
706      * Make sure our VLDB connection(s) is/are set up before actually
707      * trying to perform a volume creation creation.
708      */
709     if (!initDone) {
710         code = InitThisModule(NoAuthFlag, uss_ConfDir, uss_Cell);
711         if (code) {
712             afs_com_err(uss_whoami, code,
713                     "while inititializing VLDB connection(s)\n");
714             return (code);
715         }
716     }
717     /*Initialize VLDB connection(s) */
718     if (!uss_DryRun) {
719 #ifdef USS_VOL_DB
720         printf("%s: Creating volume on srv 0x%x, part %d, vol name '%s'\n",
721                rn, saddr, pname, a_volname);
722 #endif /* USS_VOL_DB */
723         code = UV_CreateVolume(saddr, pname, a_volname, &volid);
724         if (code) {
725             if (code == VL_NAMEEXIST) {
726                 VolExistFlag = 1;
727                 fprintf(stderr,
728                         "%s: Warning; Volume '%s' exists, using existing one.\n",
729                         uss_whoami, a_volname);
730
731                 /*
732                  * We should get the volid here if we want to use it, but
733                  * we don't need it right now.  What we DO need, though, is
734                  * to ask our caller if it's OK to overwrite the user's files
735                  * if they're pre-existing.
736                  */
737                 if (!uss_OverwriteThisOne) {
738                     printf
739                         ("Overwrite files in pre-existing '%s' volume? [y, n]: ",
740                          a_volname);
741                     scanf("%s", userinput);
742                     if ((userinput[0] == 'y') || (userinput[0] == 'Y')) {
743                         printf("\t[Overwriting allowed]\n");
744                         uss_OverwriteThisOne = 1;
745                     } else
746                         printf("\t[Overwriting not permitted]\n");
747                 }               /*Overwriting not previously allowed */
748             } /*Volume already existed */
749             else {
750                 uss_procs_PrintErr(line,
751                                    "Couldn't create volume '%s' [error %d]: %s\n",
752                                    a_volname, code, strerror(errno));
753                 return (1);
754             }                   /*Failure was NOT because it already existed */
755         }                       /*UV_CreateVolume failed */
756     } /*Not a dry run */
757     else {
758         fprintf(stderr,
759                 "\t[Dry run: Creating volume '%s' on '%s', partition '%s']\n",
760                 a_volname, a_server, a_partition);
761     }                           /*Not a dry run */
762
763     /* OK, we want to make sure we don't double-mount the volume.
764      * If the volume existed, it can be the case that it is
765      * already mounted somewhere else (because of $AUTO or others).
766      * Let's check for that.  Note: we never enter this portion of
767      * the code if we're doing a dry run.
768      */
769     if (VolExistFlag) {
770         if ((Oldmpoint = (char *)malloc(strlen(a_mpoint) + 50)) == NULL) {
771             fprintf(stderr, "%s: No more memory!\n", uss_whoami);
772             return (1);
773         }
774
775         mpExistFlag = CheckDoubleMount(a_mpoint, Oldmpoint);
776         if (mpExistFlag == uss_procs_ANCIENT) {
777             fprintf(stderr,
778                     "%s:\t*** WARNING ***; This user (%s) is already in passwd file (or equivalent). IGNORED.\n",
779                     uss_whoami, uss_User);
780             free(Oldmpoint);
781             uss_syntax_err = 1; /*I know, I know, it's not a SYNTAX error */
782             uss_ignoreFlag = 1;
783             return (0);
784         }
785         if (mpExistFlag == uss_procs_YOUNG) {
786             fprintf(stderr,
787                     "%s: Warning; Volume '%s' is already mounted on %s.  Using the existing one.\n",
788                     uss_whoami, a_volname, Oldmpoint);
789             a_mpoint = Oldmpoint;
790         }
791     }
792
793     if (mpExistFlag == 0) {
794         extern int local_Cell;
795         /*
796          * Let's mount it.
797          */
798         if (local_Cell)
799             sprintf(tmp_str, "#%s.", a_volname);
800         else
801             sprintf(tmp_str, "#%s:%s.", uss_Cell, a_volname);
802         if (!uss_DryRun) {
803             if (symlink(tmp_str, a_mpoint)) {
804                 if (errno == EEXIST) {
805                     fprintf(stderr,
806                             "%s: Warning: Mount point '%s' already exists.\n",
807                             uss_whoami, a_mpoint);
808                 } else {
809                     fprintf(stderr,
810                             "%s: Can't mount volume '%s' on '%s': %s\n",
811                             uss_whoami, a_volname, a_mpoint, strerror(errno));
812                     if (Oldmpoint)
813                         free(Oldmpoint);
814                     return (1);
815                 }               /*There was already something mounted there */
816             }                   /*Mount failed */
817         } /*Dry run */
818         else {
819             fprintf(stderr, "\t[Dry run: Mounting '%s' at '%s']\n", tmp_str,
820                     a_mpoint);
821         }                       /*Not a dry run */
822     }
823
824     /*Mount point didn't already exist */
825     /*
826      * Set the volume disk quota.
827      */
828     if (!uss_DryRun) {
829         if ((code = uss_acl_SetDiskQuota(a_mpoint, atoi(a_quota))))
830             return (code);
831     } /*Dry run */
832     else {
833         fprintf(stderr,
834                 "\t[Dry run: Setting disk quota for '%s' to %s blocks]\n",
835                 a_mpoint, a_quota);
836     }                           /*Not a dry run */
837
838     /*Copy mpoint into $MTPT for others to use */
839     strcpy(uss_MountPoint, a_mpoint);
840
841     o = uss_procs_GetOwner(a_owner);
842     if (!uss_DryRun) {
843         if (chown(a_mpoint, o, -1)) {
844             fprintf(stderr,
845                     "%s: Can't chown() mountpoint '%s' to owner '%s' (uid %d): %s\n",
846                     uss_whoami, a_mpoint, a_owner, o, strerror(errno));
847             if (Oldmpoint)
848                 free(Oldmpoint);
849             return (1);
850         }                       /*chown failed */
851     } /*Dry run */
852     else {
853         fprintf(stderr,
854                 "\t[Dry run: chown() mountpoint '%s' to be owned by user]\n",
855                 a_mpoint);
856     }                           /*Not a dry run */
857
858     /*
859      * Set the ACL on the user's home directory so that, for the duration of
860      * the account creation, only the uss_AccountCreator has any rights on the
861      * files therein.  Make sure to clear this ACL to remove anyone that might
862      * already be there due to volume creation defaults.  We will set this ACL
863      * properly, as well as all ACLs of future subdirectories,as the very last
864      * thing we do to the new account.
865      */
866     new_dir = (struct uss_subdir *)malloc(sizeof(struct uss_subdir));
867     new_dir->previous = uss_currentDir;
868     new_dir->path = (char *)malloc(strlen(a_mpoint) + 1);
869     strcpy(new_dir->path, a_mpoint);
870     new_dir->finalACL = (char *)malloc(strlen(a_acl) + 1);
871     strcpy(new_dir->finalACL, a_acl);
872     uss_currentDir = new_dir;
873     sprintf(tmp_str, "%s %s all", a_mpoint, uss_AccountCreator);
874
875     if (Oldmpoint)
876         free(Oldmpoint);
877
878     if (!uss_DryRun) {
879         if (uss_verbose)
880             fprintf(stderr, "Setting ACL: '%s'\n", tmp_str);
881         if (uss_acl_SetAccess(tmp_str, 1, 0))
882             return (1);
883     } /*For real */
884     else {
885         fprintf(stderr, "\t[Dry run: uss_acl_SetAccess(%s)]\n", tmp_str);
886     }
887     return (0);
888 }                               /*uss_vol_CreateVol */
889
890
891 /*------------------------------------------------------------------------
892  * EXPORTED uss_vol_DeleteVol
893  *
894  * Environment:
895  *      Nothing interesting.
896  *
897  * Side Effects:
898  *      As advertised.
899  *------------------------------------------------------------------------*/
900
901 afs_int32
902 uss_vol_DeleteVol(char *a_volName, afs_int32 a_volID, char *a_servName, 
903                   afs_int32 a_servID, char *a_partName, afs_int32  a_partID)
904 {                               /*uss_vol_DeleteVol */
905
906     static char rn[] = "uss_vol_DeleteVol";     /*Routine name */
907     register afs_int32 code = 0;        /*Return code */
908
909     /*
910      * Make sure we've initialized our VLDB connection(s) before
911      * proceeding apace.
912      */
913     if (!initDone) {
914         code = InitThisModule(NoAuthFlag, uss_ConfDir, uss_Cell);
915         if (code)
916             return (code);
917     }
918
919     if (!uss_DryRun) {
920         /*
921          * Go for the deletion.
922          */
923 #ifdef USS_VOL_DB
924         printf
925             ("%s: Deleting volume '%s' (ID %d) on FileServer '%s' (0x%x), partition '%s' (%d)\n",
926              rn, a_volName, a_volID, a_servName, a_servID, a_partName,
927              a_partID);
928 #endif /* USS_VOL_DB */
929
930         code = UV_DeleteVolume(a_servID, a_partID, a_volID);
931         if (code)
932             printf("%s: Can't delete volume '%s'\n", uss_whoami, a_volName);
933     } else
934         printf("\t[Dry run - volume '%s' NOT removed]\n", a_volName);
935
936     return (code);
937
938 }                               /*uss_vol_DeleteVol */
939
940
941 /*------------------------------------------------------------------------
942  * static GetServerAndPart
943  *
944  * Description:
945  *      Given a VLDB entry, return the server and partition IDs for
946  *      the read/write volume.
947  *
948  * Arguments:
949  *      a_vldbEntryP : Ptr to VLDB entry.
950  *      a_servIDP    : Ptr to server ID to set.
951  *      a_partIDP    : Ptr to partition ID to set.
952  *
953  * Returns:
954  *      0 if everything went well, or
955  *      -1 otherwise.
956  *
957  * Environment:
958  *      Nothing interesting.
959  *
960  * Side Effects:
961  *      As advertised.
962  *------------------------------------------------------------------------*/
963
964 static afs_int32
965 GetServerAndPart(struct vldbentry *a_vldbEntryP, afs_int32 *a_servIDP,
966                  afs_int32 *a_partIDP)
967 {                               /*GetServerAndPart */
968
969     /*
970      * It really looks like all we need to do is pull off the
971      * first entry's info.
972      */
973     *a_servIDP = a_vldbEntryP->serverNumber[0];
974     *a_partIDP = a_vldbEntryP->serverPartition[0];
975     return (0);
976
977 }                               /*GetServerAndPart */
978
979
980 /*------------------------------------------------------------------------
981  * EXPORTED uss_vol_GetVolInfoFromMountPoint
982  *
983  * Environment:
984  *      If the mountpoint path provided is not 
985  *
986  * Side Effects:
987  *      As advertised.
988  *------------------------------------------------------------------------*/
989
990 afs_int32
991 uss_vol_GetVolInfoFromMountPoint(char *a_mountpoint)
992 {                               /*uss_vol_GetVolInfoFromMountPoint */
993 #ifdef USS_VOL_DB
994     static char rn[] = "uss_vol_GetVolInfoFromMountPoint";
995 #endif
996     register afs_int32 code;    /*Return code */
997     uss_VolumeStatus_t *statusP;        /*Ptr to returned status */
998     afs_int32 volID;            /*Volume ID */
999     struct vldbentry vldbEntry; /*VLDB entry for volume */
1000     afs_int32 serverID;         /*Addr of host FileServer */
1001     afs_int32 partID;           /*Volume's partition ID */
1002
1003     /*
1004      * Set up to ask the CacheManager to give us all the info
1005      * it has about the given mountpoint.
1006      */
1007     code = uss_fs_GetVolStat(a_mountpoint, uss_fs_OutBuff, USS_FS_MAX_SIZE);
1008 #ifdef USS_VOL_DB
1009     printf("[%s] Result of uss_fs_GetVolStat: code = %d, errno = %d\n", rn,
1010            code, errno);
1011 #endif /* USS_VOL_DB */
1012     if (code) {
1013         if (errno == EINVAL || errno == ENOENT || errno == ENODEV) {
1014             /*
1015              * We were given a mountpoint pathname that doesn't
1016              * point to a volume, or a mountpoint that has already
1017              * been deleted.  This means that there is no info
1018              * to get from this pathname.  Zero out the volume,
1019              * server & partition info and return successfully.
1020              */
1021             uss_Volume[0] = '\0';
1022             uss_Server[0] = '\0';
1023             uss_Partition[0] = '\0';
1024             uss_VolumeID = 0;
1025             uss_ServerID = 0;
1026             uss_PartitionID = 0;
1027             if (uss_verbose) {
1028                 printf("%s: Warning: Mountpoint pathname '%s': ", uss_whoami,
1029                        a_mountpoint);
1030                 if (errno == EINVAL)
1031                     printf("Volume not reachable\n");
1032                 else if (errno == ENODEV)
1033                     printf("No such device\n");
1034                 else
1035                     printf("Not found\n");
1036             }
1037             return (0);
1038         } else {
1039             printf("%s: Can't get volume information from mountpoint '%s'\n",
1040                    uss_whoami, a_mountpoint);
1041             return (code);
1042         }
1043     }
1044
1045     /*Can't get volume status */
1046     /*
1047      * Pull out the volume name from the returned information and store
1048      * it in the common area.  It resides right after the basic volume
1049      * status structure.
1050      */
1051     statusP = (uss_VolumeStatus_t *) uss_fs_OutBuff;
1052     strcpy(uss_Volume, (((char *)statusP) + sizeof(*statusP)));
1053     volID = statusP->Vid;
1054     uss_VolumeID = volID;
1055     if (volID == 0) {
1056         printf("%s: Illegal volume ID: %d\n", uss_whoami, volID);
1057         return (-1);
1058     }
1059
1060     /*
1061      * With the volume name in hand, find out where that volume
1062      * lives.  Make sure our VLDB stuff has been initialized first.
1063      */
1064     if (!initDone) {
1065         code = InitThisModule(NoAuthFlag, uss_ConfDir, uss_Cell);
1066         if (code)
1067             return (code);
1068     }
1069     code = ubik_VL_GetEntryByID( uconn_vldbP, 0, volID, -1, &vldbEntry);
1070     if (code) {
1071         printf("%s: Can't fetch VLDB entry for volume ID %d\n", uss_whoami,
1072                volID);
1073         return (code);
1074     }
1075
1076     /*
1077      * Translate the given VLDB entry from network to host format, then
1078      * checking on the volume's validity.  Specifically, it must be a
1079      * read/write volume and must only exist on one server.
1080      */
1081     MapHostToNetwork(&vldbEntry);
1082     if (vldbEntry.volumeId[RWVOL] != volID) {
1083         printf("%s: Volume '%s' (ID %d) is not a read/write volume!!\n",
1084                uss_whoami, uss_Volume, volID);
1085         return (-1);
1086     }
1087     if (vldbEntry.nServers != 1) {
1088         printf("s: Volume '%s' (ID %d) exists on multiple servers!!\n",
1089                uss_whoami, uss_Volume, volID);
1090         return (-1);
1091     }
1092
1093     /*
1094      * Pull out the int32words containing the server and partition info
1095      * for the read/write volume.
1096      */
1097     code = GetServerAndPart(&vldbEntry, &serverID, &partID);
1098     if (code) {
1099         printf
1100             ("%s: Can't get server/partition info from VLDB entry for volume '%s' (ID %d)\n",
1101              uss_whoami, uss_Volume, volID);
1102         return (-1);
1103     }
1104
1105     /*
1106      * Store the raw data, then translate the FileServer address to a
1107      * host name, and the partition ID to a partition name.
1108      */
1109     uss_ServerID = serverID;
1110     uss_PartitionID = partID;
1111     HostIDToHostName(serverID, uss_Server);
1112 #ifdef USS_VOL_DB
1113     printf("[%s] Server ID 0x%x translated to '%s'\n", rn, serverID,
1114            uss_Server);
1115 #endif /* USS_VOL_DB */
1116     code = PartIDToPartName(partID, uss_Partition);
1117     if (code) {
1118         printf("%s: Error translating partition ID %d to partition name\n",
1119                uss_whoami, partID);
1120         return (code);
1121     }
1122
1123     /*
1124      * We got it, home boy.
1125      */
1126     return (0);
1127
1128 }                               /*uss_vol_GetVolInfoFromMountPoint */