]> andersk Git - moira.git/blobdiff - afssync/pt_util.c
Don't prototype these ourself; ubik.h does.
[moira.git] / afssync / pt_util.c
index c9033c2a5af3ce33e19877bb416f7a8088c186c8..b5d53d356f53b951445c9633e1beb2bf18b1abdd 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *
- * ptdump: Program to dump the AFS protection server database
+ * pt_util: Program to dump the AFS protection server database
  *         into an ascii file.
  *
  *     Assumptions: We *cheat* here and read the datafile directly, ie.
@@ -13,8 +13,9 @@
 #include <sys/time.h>
 #include <stdio.h>
 #include <ctype.h>
-#include <strings.h>
+#include <string.h>
 #include <sys/file.h>
+#include <stdlib.h>
 
 #include <afs/param.h>
 #include <lock.h>
 #include "ptint.h"
 #include "ptserver.h"
 #include "pterror.h"
+#include "vis.h"
 
 #define IDHash(x) (abs(x) % HASHSIZE)
-#define print_id(x) ( ((flags&DO_SYS)==0 && (abs(x)>32767)) || \
-                     ((flags&DO_OTR)==0 && (abs(x)<32768)))
+#define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
+                     ((flags&DO_OTR)==0 && (x>-32768 && x<97537)))
 
 extern char *optarg;
 extern int optind;
@@ -100,6 +102,7 @@ char **argv;
     struct ubik_hdr *uh;
     char *dfile = 0;
     char *pfile = "/usr/afs/db/prdb.DB0";
+    char *str;
     
     while ((cc = getopt(argc, argv, "wugmxsnp:d:")) != EOF) {
        switch (cc) {
@@ -132,7 +135,7 @@ char **argv;
            break;
        default:
            fprintf(stderr,
-                   "Usage: ptdump [options] [-d data] [-p prdb]\n");
+                   "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);
@@ -145,19 +148,19 @@ char **argv;
        }
     }
     if ((dbase_fd = open(pfile, wflag ? O_RDWR : O_RDONLY, 0600)) < 0) {
-       fprintf(stderr, "ptdump: cannot open %s: %s\n",
+       fprintf(stderr, "pt_util: cannot open %s: %s\n",
                pfile, sys_errlist[errno]);
        exit (1);
     }
     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
-       fprintf(stderr, "ptdump: error reading %s: %s\n",
+       fprintf(stderr, "pt_util: error reading %s: %s\n",
                pfile, sys_errlist[errno]);
        exit (1);
     }
 
     if (dfile) {
        if ((dfp = fopen(dfile, wflag ? "r" : "w")) == 0) {
-           fprintf(stderr, "ptdump: error opening %s: %s\n",
+           fprintf(stderr, "pt_util: error opening %s: %s\n",
                    dfile, sys_errlist[errno]);
            exit(1);
        }
@@ -166,13 +169,23 @@ char **argv;
 
     uh = (struct ubik_hdr *)buffer;
     if (ntohl(uh->magic) != UBIK_MAGIC)
-       fprintf(stderr, "ptdump: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
+       fprintf(stderr, "pt_util: %s: Bad UBIK_MAGIC. Is %x should be %x\n",
                pfile, ntohl(uh->magic), UBIK_MAGIC);
-    bcopy(&uh->version, &uv, sizeof(struct ubik_version));
+    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 */
+       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, sys_errlist[errno]);
+           exit (1);
+       }
+    }
     fprintf(stderr, "Ubik Version is: %d.%d\n",
            uv.epoch, uv.counter);
     if (read(dbase_fd, &prh, sizeof(struct prheader)) < 0) {
-       fprintf(stderr, "ptdump: error reading %s: %s\n",
+       fprintf(stderr, "pt_util: error reading %s: %s\n",
                pfile, sys_errlist[errno]);
        exit (1);
     }
@@ -232,6 +245,15 @@ char **argv;
                sscanf(buffer, "%s %d/%d %d %d %d",
                       name, &flags, &quota, &id, &oid, &cid);
 
+               str = malloc(strlen(name) + 1);
+               if (!str)
+                 {
+                   fprintf(stderr, "malloc failed!");
+                   exit(1);
+                 }
+               strunvis(str, name);
+               strncpy(name, str, PR_MAXNAMELEN);
+               name[PR_MAXNAMELEN] = '\0';
                if (FindByID(0, id))
                    code = PRIDEXIST;
                else
@@ -278,14 +300,14 @@ char **argv;
 
     lseek (dbase_fd, 0, L_SET);                /* rewind to beginning of file */
     if (read(dbase_fd, buffer, HDRSIZE) < 0) {
-       fprintf(stderr, "ptdump: error reading %s: %s\n",
+       fprintf(stderr, "pt_util: error reading %s: %s\n",
                pfile, sys_errlist[errno]);
        exit (1);
     }
     uh = (struct ubik_hdr *)buffer;
     if ((uh->version.epoch != uv.epoch) ||
        (uh->version.counter != uv.counter)) {
-       fprintf(stderr, "ptdump: Ubik Version number changed during execution.\n");
+       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);
@@ -363,7 +385,7 @@ void display_group(id)
     while (offset) {
        lseek(dbase_fd, offset+HDRSIZE, L_SET);
        if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
-           fprintf(stderr, "ptdump: read i/o error: %s\n",
+           fprintf(stderr, "pt_util: read i/o error: %s\n",
                    strerror(errno));
            exit (1);
        }
@@ -429,7 +451,24 @@ void fix_pre(pre)
     struct prentry *pre;
 {
     register int i;
-    
+    char *str = malloc(4 * strlen(pre->name) + 1);
+
+    if (!str)
+      {
+       fprintf(stderr, "malloc failed in fix_pre()!");
+       exit(1);
+      }
+    strvis(str, pre->name, VIS_WHITE);
+    if (strlen(str) > PR_MAXNAMELEN) 
+      {
+       fprintf(stderr, "encoding greater than PR_MAXNAMELEN!\n");
+       fprintf(stderr, "groupname %s will not be encoded!\n", pre->name);
+      }
+    else 
+      {
+       strncpy(pre->name, str, PR_MAXNAMELEN);
+       pre->name[PR_MAXNAMELEN] = '\0';
+      }
     pre->flags = ntohl(pre->flags);
     pre->id = ntohl(pre->id);
     pre->cellid = ntohl(pre->cellid);
@@ -465,7 +504,7 @@ int id;
     while (offset) {
        lseek(dbase_fd, offset+HDRSIZE, L_SET);
        if (read(dbase_fd, &pre, sizeof(struct prentry)) < 0) {
-           fprintf(stderr, "ptdump: read i/o error: %s\n",
+           fprintf(stderr, "pt_util: read i/o error: %s\n",
                    sys_errlist[errno]);
            exit (1);
        }
@@ -495,7 +534,7 @@ struct prentry *pre;
     }
     he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
     if (he == 0) {
-       fprintf(stderr, "ptdump: No Memory for internal hash table.\n");
+       fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
        exit (1);
     }
     he->h_id = id;
This page took 0.040954 seconds and 4 git commands to generate.