ptserver-restricted-mode-20050415
[openafs.git] / src / ptserver / pt_util.c
index 3b709b0..49a3b19 100644 (file)
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
+#include <errno.h>
 #include <sys/file.h>
 
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID("$Header$");
+RCSID
+    ("$Header$");
 
+#include <afs/cmd.h>           /*Command line parsing */
+#include <errno.h>
 #include <lock.h>
 #include <netinet/in.h>
 #define UBIK_INTERNALS
@@ -39,8 +43,8 @@ RCSID("$Header$");
 
 extern char *optarg;
 extern int optind;
-extern int errno;
 
+int restricted = 0;
 int display_entry();
 void add_group();
 void display_groups();
@@ -49,6 +53,7 @@ void fix_pre();
 char *checkin();
 char *check_core();
 char *id_to_name();
+int CommandProc(struct cmd_syndesc *);
 
 struct hash_entry {
     char h_name[PR_MAXNAMELEN];
@@ -63,18 +68,18 @@ static struct prheader prh;
 static struct ubik_version uv;
 
 struct grp_list {
-    struct grp_list    *next;
-    long               groups[1024];
+    struct grp_list *next;
+    long groups[1024];
 };
-static struct grp_list *grp_head=0;
-static long grp_count=0;
+static struct grp_list *grp_head = 0;
+static long grp_count = 0;
 
 struct usr_list {
     struct usr_list *next;
     char name[PR_MAXNAMELEN];
     long uid;
 };
-static struct usr_list *usr_head=0;
+static struct usr_list *usr_head = 0;
 
 char buffer[1024];
 int dbase_fd;
@@ -93,9 +98,38 @@ int nflag = 0;
 int wflag = 0;
 int flags = 0;
 
-main(argc, argv)
-int argc;
-char **argv;
+int
+main(int argc, char **argv)
+{
+
+    register struct cmd_syndesc *cs;   /*Command line syntax descriptor */
+    register afs_int32 code;   /*Return code */
+
+    cs = cmd_CreateSyntax((char *)0, CommandProc, 0,
+                         "access protection database");
+    cmd_AddParm(cs, "-w", CMD_FLAG, CMD_OPTIONAL,
+               "update prdb with contents of data file");
+    cmd_AddParm(cs, "-user", CMD_FLAG, CMD_OPTIONAL, "display users");
+    cmd_AddParm(cs, "-group", CMD_FLAG, CMD_OPTIONAL, "display groups");
+    cmd_AddParm(cs, "-members", CMD_FLAG, CMD_OPTIONAL,
+               "display group members");
+    cmd_AddParm(cs, "-name", CMD_FLAG, CMD_OPTIONAL,
+               "follow name hash chains (not id hashes)");
+    cmd_AddParm(cs, "-system", CMD_FLAG, CMD_OPTIONAL,
+               "display only system data");
+    cmd_AddParm(cs, "-xtra", CMD_FLAG, CMD_OPTIONAL,
+               "display extra users/groups");
+    cmd_Seek(cs, 10);
+    cmd_AddParm(cs, "-prdb", CMD_SINGLE, CMD_OPTIONAL, "prdb file");
+    cmd_AddParm(cs, "-datafile", CMD_SINGLE, CMD_OPTIONAL, "data file");
+    code = cmd_Dispatch(argc, argv);
+
+    exit(code);
+
+}
+
+int
+CommandProc(register struct cmd_syndesc *a_as)
 {
     register int i;
     register long code;
@@ -104,66 +138,54 @@ char **argv;
     struct ubik_hdr *uh;
     char *dfile = 0;
     char *pfile = "/usr/afs/db/prdb.DB0";
-    
-    while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
-       switch (cc) {
-       case 'p':
-           pfile = optarg;
-           break;
-       case 'd':
-           dfile = optarg;
-           break;
-       case 'n':
-           nflag++;
-           break;
-       case 'w':
-           wflag++;
-           break;
-       case 'u':
-           flags |= DO_USR;
-           break;
-       case 'm':
-           flags |= (DO_GRP|DO_MEM);
-           break;
-       case 'g':
-           flags |= DO_GRP;
-           break;
-       case 's':
-           flags |= DO_SYS;
-           break;
-       case 'x':
-           flags |= DO_OTR;
-           break;
-       default:
-           fprintf(stderr,
-                   "Usage: pt_util [options] [-d data] [-p prdb]\n");
-           fputs("  Options:\n", stderr);
-           fputs("    -w  Update prdb with contents of data file\n", stderr);
-           fputs("    -u  Display users\n", stderr);
-           fputs("    -g  Display groups\n", stderr);
-           fputs("    -m  Display group members\n", stderr);
-           fputs("    -n  Follow name hash chains (not id hashes)\n", stderr);
-           fputs("    -s  Display only system data\n", stderr);
-           fputs("    -x  Display extra users/groups\n", stderr);
-           exit(1);
-       }
+    struct cmd_parmdesc *tparm;
+
+    tparm = a_as->parms;
+
+    if (tparm[0].items) {
+       wflag++;
+    }
+    if (tparm[1].items) {
+       flags |= DO_USR;
+    }
+    if (tparm[2].items) {
+       flags |= DO_GRP;
     }
-    if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY)|O_CREAT, 0600)) 
+    if (tparm[3].items) {
+       flags |= (DO_GRP | DO_MEM);
+    }
+    if (tparm[4].items) {
+       nflag++;
+    }
+    if (tparm[5].items) {
+       flags |= DO_SYS;
+    }
+    if (tparm[6].items) {
+       flags |= DO_OTR;
+    }
+    if (tparm[7].items) {
+       pfile = tparm[7].items->data;
+    }
+    if (tparm[8].items) {
+       dfile = tparm[8].items->data;
+    }
+
+    if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
        < 0) {
-       fprintf(stderr, "pt_util: cannot open %s: %s\n",
-               pfile, strerror(errno));
-       exit (1);
+       fprintf(stderr, "pt_util: cannot open %s: %s\n", pfile,
+               strerror(errno));
+       exit(1);
     }
     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
-       fprintf(stderr, "pt_util: error reading %s: %s\n",
-               pfile, strerror(errno));
-       exit (1);
+       fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
+               strerror(errno));
+       exit(1);
     }
 
     if (dfile) {
        if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
-           fprintf(stderr, "pt_util: error opening %s: %s\n",
-                   dfile, strerror(errno));
+           fprintf(stderr, "pt_util: error opening %s: %s\n", dfile,
+                   strerror(errno));
            exit(1);
        }
     } else
@@ -174,55 +196,59 @@ char **argv;
        fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
                pfile, ntohl(uh->magic), UBIK_MAGIC);
     memcpy(&uv, &uh->version, sizeof(struct ubik_version));
-    if (wflag && uv.epoch==0 && uv.counter==0) {
-       uv.epoch=2; /* a ubik version of 0 or 1 has special meaning */
+    if (wflag && uv.epoch == 0 && uv.counter == 0) {
+       uv.epoch = 2;           /* a ubik version of 0 or 1 has special meaning */
        memcpy(&uh->version, &uv, sizeof(struct ubik_version));
        lseek(dbase_fd, 0, SEEK_SET);
        if (write(dbase_fd, buffer, HDRSIZE) < 0) {
            fprintf(stderr, "pt_util: error writing ubik version to %s: %s\n",
                    pfile, strerror(errno));
-           exit (1);
+           exit(1);
        }
     }
-    fprintf(stderr, "Ubik Version is: %d.%d\n",
-           uv.epoch, uv.counter);
+    fprintf(stderr, "Ubik Version is: %d.%d\n", uv.epoch, uv.counter);
     if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
-       fprintf(stderr, "pt_util: error reading %s: %s\n",
-               pfile, strerror(errno));
-       exit (1);
+       fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
+               strerror(errno));
+       exit(1);
     }
 
     Initdb();
-    initialize_pt_error_table();
+    initialize_PT_error_table();
 
     if (wflag) {
        struct usr_list *u;
 
-       while(fgets(buffer, sizeof(buffer), dfp)) {
+       while (fgets(buffer, sizeof(buffer), dfp)) {
            int id, oid, cid, flags, quota, uid;
            char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
 
            if (isspace(*buffer)) {
                sscanf(buffer, "%s %d", mem, &uid);
 
-               for (u=usr_head; u; u=u->next)
-                   if (u->uid && u->uid==uid) break;
+               for (u = usr_head; u; u = u->next)
+                   if (u->uid && u->uid == uid)
+                       break;
                if (u) {
                    /* Add user - deferred because it is probably foreign */
                    u->uid = 0;
                    if (FindByID(0, uid))
                        code = PRIDEXIST;
                    else {
-                       if (!code && (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)){
+                       if (!code
+                           && (flags & (PRGRP | PRQUOTA)) ==
+                           (PRGRP | PRQUOTA)) {
                            gentry.ngroups++;
-                           code = pr_WriteEntry(0,0,gpos,&gentry);
+                           code = pr_WriteEntry(0, 0, gpos, &gentry);
                            if (code)
-                               fprintf(stderr, "Error setting group count on %s: %s\n",
+                               fprintf(stderr,
+                                       "Error setting group count on %s: %s\n",
                                        name, error_message(code));
                        }
-                       code = CreateEntry
-                           (0, u->name, &uid, 1/*idflag*/, 1/*gflag*/,
-                            SYSADMINID/*oid*/, SYSADMINID/*cid*/);
+                       code = CreateEntry(0, u->name, &uid, 1 /*idflag */ ,
+                                          1 /*gflag */ ,
+                                          SYSADMINID /*oid */ ,
+                                          SYSADMINID /*cid */ );
                    }
                    if (code)
                        fprintf(stderr, "Error while creating %s: %s\n",
@@ -230,57 +256,62 @@ char **argv;
                    continue;
                }
                /* Add user to group */
-               if (id==ANYUSERID || id==AUTHUSERID || uid==ANONYMOUSID) {
+               if (id == ANYUSERID || id == AUTHUSERID || uid == ANONYMOUSID) {
                    code = PRPERM;
-               } else if ((upos=FindByID(0,uid)) && (gpos=FindByID(0,id))) {
-                   code = pr_ReadEntry(0,0,upos,&uentry);
-                   if (!code) code = pr_ReadEntry(0,0,gpos,&gentry);
-                   if (!code) code = AddToEntry (0, &gentry, gpos, uid);
-                   if (!code) code = AddToEntry (0, &uentry, upos, id);
+               } else if ((upos = FindByID(0, uid))
+                          && (gpos = FindByID(0, id))) {
+                   code = pr_ReadEntry(0, 0, upos, &uentry);
+                   if (!code)
+                       code = pr_ReadEntry(0, 0, gpos, &gentry);
+                   if (!code)
+                       code = AddToEntry(0, &gentry, gpos, uid);
+                   if (!code)
+                       code = AddToEntry(0, &uentry, upos, id);
                } else
                    code = PRNOENT;
 
                if (code)
-                   fprintf(stderr, "Error while adding %s to %s: %s\n",
-                           mem, name, error_message(code));
+                   fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
+                           name, error_message(code));
            } else {
-               sscanf(buffer, "%s %d/%d %d %d %d",
-                      name, &flags, &quota, &id, &oid, &cid);
+               sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
+                      &oid, &cid);
 
                if (FindByID(0, id))
                    code = PRIDEXIST;
                else
-                   code = CreateEntry(0, name, &id, 1/*idflag*/,
-                                      flags&PRGRP, oid, cid);
+                   code = CreateEntry(0, name, &id, 1 /*idflag */ ,
+                                      flags & PRGRP, oid, cid);
                if (code == PRBADNAM) {
                    u = (struct usr_list *)malloc(sizeof(struct usr_list));
                    u->next = usr_head;
                    u->uid = id;
                    strcpy(u->name, name);
                    usr_head = u;
-               } else
-               if (code) {
-                   fprintf(stderr, "Error while creating %s: %s\n",
-                           name, error_message(code));
-               } else if ((flags&PRACCESS) ||
-                          (flags&(PRGRP|PRQUOTA))==(PRGRP|PRQUOTA)) {
+               } else if (code) {
+                   fprintf(stderr, "Error while creating %s: %s\n", name,
+                           error_message(code));
+               } else if ((flags & PRACCESS)
+                          || (flags & (PRGRP | PRQUOTA)) ==
+                          (PRGRP | PRQUOTA)) {
                    gpos = FindByID(0, id);
-                   code = pr_ReadEntry(0,0,gpos,&gentry);
+                   code = pr_ReadEntry(0, 0, gpos, &gentry);
                    if (!code) {
                        gentry.flags = flags;
                        gentry.ngroups = quota;
-                       code = pr_WriteEntry(0,0,gpos,&gentry);
+                       code = pr_WriteEntry(0, 0, gpos, &gentry);
                    }
                    if (code)
-                       fprintf(stderr,"Error while setting flags on %s: %s\n",
-                               name, error_message(code));
+                       fprintf(stderr,
+                               "Error while setting flags on %s: %s\n", name,
+                               error_message(code));
                }
            }
        }
-       for (u=usr_head; u; u=u->next)
+       for (u = usr_head; u; u = u->next)
            if (u->uid)
-               fprintf(stderr, "Error while creating %s: %s\n",
-                       u->name, error_message(PRBADNAM));
+               fprintf(stderr, "Error while creating %s: %s\n", u->name,
+                       error_message(PRBADNAM));
     } else {
        for (i = 0; i < HASHSIZE; i++) {
            upos = nflag ? ntohl(prh.nameHash[i]) : ntohl(prh.idHash[i]);
@@ -291,30 +322,30 @@ char **argv;
            display_groups();
     }
 
-    lseek (dbase_fd, 0, L_SET);                /* rewind to beginning of file */
+    lseek(dbase_fd, 0, L_SET); /* rewind to beginning of file */
     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
-       fprintf(stderr, "pt_util: error reading %s: %s\n",
-               pfile, strerror(errno));
-       exit (1);
+       fprintf(stderr, "pt_util: error reading %s: %s\n", pfile,
+               strerror(errno));
+       exit(1);
     }
     uh = (struct ubik_hdr *)buffer;
-    if ((uh->version.epoch != uv.epoch) ||
-       (uh->version.counter != uv.counter)) {
-       fprintf(stderr, "pt_util: Ubik Version number changed during execution.\n");
+    if ((uh->version.epoch != uv.epoch)
+       || (uh->version.counter != uv.counter)) {
+       fprintf(stderr,
+               "pt_util: Ubik Version number changed during execution.\n");
        fprintf(stderr, "Old Version = %d.%d, new version = %d.%d\n",
-               uv.epoch, uv.counter, uh->version.epoch,
-               uh->version.counter);
+               uv.epoch, uv.counter, uh->version.epoch, uh->version.counter);
     }
-    close (dbase_fd);
-    exit (0);
+    close(dbase_fd);
+    exit(0);
 }
 
-int display_entry (offset)
-int offset;
+int
+display_entry(int offset)
 {
     register int i;
 
-    lseek (dbase_fd, offset+HDRSIZE, L_SET);
+    lseek(dbase_fd, offset + HDRSIZE, L_SET);
     read(dbase_fd, &pre, sizeof(struct prentry));
 
     fix_pre(&pre);
@@ -324,18 +355,17 @@ int offset;
            if (flags & DO_GRP)
                add_group(pre.id);
        } else {
-           if (print_id(pre.id) && (flags&DO_USR))
-               fprintf(dfp, FMT_BASE,
-                       pre.name, pre.flags, pre.ngroups,
+           if (print_id(pre.id) && (flags & DO_USR))
+               fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
                        pre.id, pre.owner, pre.creator);
            checkin(&pre);
        }
     }
-    return(nflag ? pre.nextName: pre.nextID);
+    return (nflag ? pre.nextName : pre.nextID);
 }
 
-void add_group(id)
-    long id;
+void
+add_group(long id)
 {
     struct grp_list *g;
     register long i;
@@ -350,17 +380,18 @@ void add_group(id)
     g->groups[i] = id;
 }
 
-void display_groups()
+void
+display_groups()
 {
     register int i, id;
     struct grp_list *g;
 
     g = grp_head;
     while (grp_count--) {
-       i = grp_count%1024;
+       i = grp_count % 1024;
        id = g->groups[i];
        display_group(id);
-       if (i==0) {
+       if (i == 0) {
            grp_head = g->next;
            free(g);
            g = grp_head;
@@ -368,19 +399,18 @@ void display_groups()
     }
 }
 
-void display_group(id)
-    int id;
+void
+display_group(int id)
 {
     register int i, offset;
     int print_grp = 0;
 
     offset = ntohl(prh.idHash[IDHash(id)]);
     while (offset) {
-       lseek(dbase_fd, offset+HDRSIZE, L_SET);
+       lseek(dbase_fd, offset + HDRSIZE, L_SET);
        if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
-           fprintf(stderr, "pt_util: read i/o error: %s\n",
-                   strerror(errno));
-           exit (1);
+           fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
+           exit(1);
        }
        fix_pre(&pre);
        if (pre.id == id)
@@ -389,23 +419,22 @@ void display_group(id)
     }
 
     if (print_id(id)) {
-       fprintf(dfp, FMT_BASE,
-               pre.name, pre.flags, pre.ngroups,
-               pre.id, pre.owner, pre.creator);
+       fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups, pre.id,
+               pre.owner, pre.creator);
        print_grp = 1;
     }
 
-    if ((flags&DO_MEM) == 0)
+    if ((flags & DO_MEM) == 0)
        return;
 
-    for (i=0; i<PRSIZE; i++) {
-       if ((id=pre.entries[i]) == 0)
+    for (i = 0; i < PRSIZE; i++) {
+       if ((id = pre.entries[i]) == 0)
            break;
-       if (id==PRBADID) continue;
-       if (print_id(id) || print_grp==1) {
-           if (print_grp==0) {
-               fprintf(dfp, FMT_BASE,
-                       pre.name, pre.flags, pre.ngroups,
+       if (id == PRBADID)
+           continue;
+       if (print_id(id) || print_grp == 1) {
+           if (print_grp == 0) {
+               fprintf(dfp, FMT_BASE, pre.name, pre.flags, pre.ngroups,
                        pre.id, pre.owner, pre.creator);
                print_grp = 2;
            }
@@ -415,19 +444,19 @@ void display_group(id)
     if (i == PRSIZE) {
        offset = pre.next;
        while (offset) {
-           lseek(dbase_fd, offset+HDRSIZE, L_SET);
+           lseek(dbase_fd, offset + HDRSIZE, L_SET);
            read(dbase_fd, &prco, sizeof(struct contentry));
            prco.next = ntohl(prco.next);
            for (i = 0; i < COSIZE; i++) {
                prco.entries[i] = ntohl(prco.entries[i]);
-               if ((id=prco.entries[i]) == 0)
+               if ((id = prco.entries[i]) == 0)
                    break;
-               if (id==PRBADID) continue;
-               if (print_id(id) || print_grp==1) {
-                   if (print_grp==0) {
-                       fprintf(dfp, FMT_BASE,
-                               pre.name, pre.flags, pre.ngroups,
-                               pre.id, pre.owner, pre.creator);
+               if (id == PRBADID)
+                   continue;
+               if (print_id(id) || print_grp == 1) {
+                   if (print_grp == 0) {
+                       fprintf(dfp, FMT_BASE, pre.name, pre.flags,
+                               pre.ngroups, pre.id, pre.owner, pre.creator);
                        print_grp = 2;
                    }
                    fprintf(dfp, FMT_MEM, id_to_name(id), id);
@@ -435,16 +464,17 @@ void display_group(id)
            }
            if ((i == COSIZE) && prco.next)
                offset = prco.next;
-           else offset = 0;
+           else
+               offset = 0;
        }
     }
 }
 
-void fix_pre(pre)
-    struct prentry *pre;
+void
+fix_pre(struct prentry *pre)
 {
     register int i;
-    
+
     pre->flags = ntohl(pre->flags);
     pre->id = ntohl(pre->id);
     pre->cellid = ntohl(pre->cellid);
@@ -467,35 +497,35 @@ void fix_pre(pre)
     }
 }
 
-char *id_to_name(id)
-int id;
+char *
+id_to_name(int id)
 {
     register int offset;
     static struct prentry pre;
     char *name;
 
     name = check_core(id);
-    if (name) return(name);
+    if (name)
+       return (name);
     offset = ntohl(prh.idHash[IDHash(id)]);
     while (offset) {
-       lseek(dbase_fd, offset+HDRSIZE, L_SET);
+       lseek(dbase_fd, offset + HDRSIZE, L_SET);
        if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
-           fprintf(stderr, "pt_util: read i/o error: %s\n",
-                   strerror(errno));
-           exit (1);
+           fprintf(stderr, "pt_util: read i/o error: %s\n", strerror(errno));
+           exit(1);
        }
        pre.id = ntohl(pre.id);
        if (pre.id == id) {
            name = checkin(&pre);
-           return(name);
+           return (name);
        }
        offset = ntohl(pre.nextID);
     }
     return 0;
 }
 
-char *checkin(pre)
-struct prentry *pre;
+char *
+checkin(struct prentry *pre)
 {
     struct hash_entry *he, *last;
     register int id;
@@ -504,30 +534,34 @@ struct prentry *pre;
     last = (struct hash_entry *)0;
     he = hat[IDHash(id)];
     while (he) {
-       if (id == he->h_id) return(he->h_name);
+       if (id == he->h_id)
+           return (he->h_name);
        last = he;
        he = he->next;
     }
     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
     if (he == 0) {
        fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
-       exit (1);
+       exit(1);
     }
     he->h_id = id;
     he->next = (struct hash_entry *)0;
     strncpy(he->h_name, pre->name, PR_MAXNAMELEN);
-    if (last == (struct hash_entry *)0) hat[IDHash(id)] = he;
-    else last->next = he;
-    return(he->h_name);
+    if (last == (struct hash_entry *)0)
+       hat[IDHash(id)] = he;
+    else
+       last->next = he;
+    return (he->h_name);
 }
 
-char *check_core(id)
-register int id;
+char *
+check_core(register int id)
 {
     struct hash_entry *he;
     he = hat[IDHash(id)];
     while (he) {
-       if (id == he->h_id) return(he->h_name);
+       if (id == he->h_id)
+           return (he->h_name);
        he = he->next;
     }
     return 0;