Use asprintf for string construction
[openafs.git] / src / ptserver / db_verify.c
index e2ee4a8..6a04ab1 100644 (file)
@@ -1,18 +1,17 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
-
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 /*
  *                      (3) Define a structure, idused, instead of an
@@ -32,45 +31,33 @@ RCSID
  *                          conditions.
  */
 
-#include <afs/stds.h>
-#include <sys/types.h>
+
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
-#include <io.h>
 #else
-#include <netdb.h>
-#include <netinet/in.h>
 #include <sys/file.h>
 #endif
-#include <stdio.h>
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-#include <errno.h>
-#include <fcntl.h>
+
 #include <afs/cellconfig.h>
 #include <afs/afsutil.h>
 #include <ubik.h>
 #include <afs/cmd.h>
+#include <afs/com_err.h>
 
 #include "ptint.h"
 #include "pterror.h"
 #include "ptserver.h"
+#include "ptuser.h"
+#include "display.h"
 
 struct prheader cheader;
 int fd;
-char *pr_dbaseName;
+const char *pr_dbaseName;
 char *whoami = "db_verify";
 #define UBIK_HEADERSIZE 64
 
 afs_int32
-printheader(h)
-     struct prheader *h;
+printheader(struct prheader *h)
 {
     printf("Version           = %d\n", ntohl(h->version));
     printf("Header Size       = %d\n", ntohl(h->headerSize));
@@ -87,13 +74,11 @@ printheader(h)
 /* printf("Sub/super Count   = %d\n", ntohl(h->instcount));    NYI */
     printf("Name Hash         = %d buckets\n", HASHSIZE);
     printf("ID   Hash         = %d buckets\n", HASHSIZE);
+    return 0;
 }
 
 static afs_int32
-pr_Read(pos, buff, len)
-     afs_int32 pos;
-     char *buff;
-     afs_int32 len;
+pr_Read(afs_int32 pos, void *buff, afs_int32 len)
 {
     afs_int32 code;
 
@@ -118,13 +103,13 @@ pr_Read(pos, buff, len)
  */
 
 afs_int32
-ReadHeader()
+ReadHeader(void)
 {
     afs_int32 code;
 
     code = pr_Read(0, (char *)&cheader, sizeof(cheader));
     if (code) {
-       com_err(whoami, code, "couldn't read header");
+       afs_com_err(whoami, code, "couldn't read header");
        return code;
     }
     /* Check and see if database exists and is approximately OK. */
@@ -132,30 +117,28 @@ ReadHeader()
        || ntohl(cheader.eofPtr) == 0) {
        if (code)
            return code;
-       com_err(whoami, PRDBBAD, "header is bad");
+       afs_com_err(whoami, PRDBBAD, "header is bad");
        return PRDBBAD;
     }
     return 0;
 }
 
 static afs_int32
-IDHash(x)
-     afs_int32 x;
+IDHash(afs_int32 x)
 {
     /* returns hash bucket for x */
     return ((abs(x)) % HASHSIZE);
 }
 
 static afs_int32
-NameHash(aname)
-     register unsigned char *aname;
+NameHash(char *aname)
 {
     /* returns hash bucket for aname */
-    register unsigned int hash = 0;
-    register int i;
+    unsigned int hash = 0;
+    int i;
 /* stolen directly from the HashString function in the vol package */
     for (i = strlen(aname), aname += i - 1; i--; aname--)
-       hash = (hash * 31) + (*aname - 31);
+       hash = (hash * 31) + (*(unsigned char *)aname - 31);
     return (hash % HASHSIZE);
 }
 
@@ -207,8 +190,7 @@ int idcount(struct idused **idmapp, int id);
 #endif
 
 int
-readUbikHeader(misc)
-     struct misc_data *misc;
+readUbikHeader(struct misc_data *misc)
 {
     int offset, r;
     struct ubik_hdr uheader;
@@ -222,13 +204,13 @@ readUbikHeader(misc)
     /* now read the info */
     r = read(fd, &uheader, sizeof(uheader));
     if (r != sizeof(uheader)) {
-       printf("error: read of %d bytes failed: %d %d\n", sizeof(uheader), r,
-              errno);
+       printf("error: read of %" AFS_SIZET_FMT " bytes failed: %d %d\n",
+              sizeof(uheader), r, errno);
        return (-1);
     }
 
     uheader.magic = ntohl(uheader.magic);
-    uheader.size = ntohl(uheader.size);
+    uheader.size = ntohs(uheader.size);
     uheader.version.epoch = ntohl(uheader.version.epoch);
     uheader.version.counter = ntohl(uheader.version.counter);
 
@@ -251,9 +233,7 @@ readUbikHeader(misc)
 }
 
 afs_int32
-ConvertDiskAddress(ea, eiP)
-     afs_uint32 ea;
-     int *eiP;
+ConvertDiskAddress(afs_uint32 ea, int *eiP)
 {
     int i;
 
@@ -273,11 +253,7 @@ ConvertDiskAddress(ea, eiP)
 }
 
 int
-PrintEntryError(misc, ea, e, indent)
-     struct misc_data *misc;
-     afs_int32 ea;
-     struct prentry *e;
-     int indent;
+PrintEntryError(struct misc_data *misc, afs_int32 ea, struct prentry *e, int indent)
 {
 
     pr_PrintEntry(stderr, /*net order */ 0, ea, e, indent);
@@ -285,11 +261,10 @@ PrintEntryError(misc, ea, e, indent)
 }
 
 afs_int32
-WalkHashTable(hashtable, hashType, map, misc)
-     afs_int32 hashtable[];    /* hash table to walk */
-     int hashType;             /* hash function to use */
-     char map[];               /* one byte per db entry */
-     struct misc_data *misc;   /* stuff to keep track of */
+WalkHashTable(afs_int32 hashtable[],   /* hash table to walk */
+             int hashType,             /* hash function to use */
+             char map[],               /* one byte per db entry */
+             struct misc_data *misc)   /* stuff to keep track of */
 {
     afs_int32 code;
     int hi;                    /* index in hash table */
@@ -326,7 +301,7 @@ WalkHashTable(hashtable, hashType, map, misc)
 
            id = ntohl(e.id);
 
-           if (((ntohl(e.flags) & (PRGRP | PRINST)) == 0)
+           if (((e.flags & htonl((PRGRP | PRINST))) == 0)
                && (strchr(e.name, '@'))) {
                /* Foreign user */
                if (id > misc->maxForId)
@@ -403,11 +378,9 @@ WalkHashTable(hashtable, hashType, map, misc)
 }
 
 afs_int32
-WalkNextChain(map, misc, ea, e)
-     char map[];               /* one byte per db entry */
-     struct misc_data *misc;   /* stuff to keep track of */
-     afs_int32 ea;
-     struct prentry *e;
+WalkNextChain(char map[],              /* one byte per db entry */
+             struct misc_data *misc,   /* stuff to keep track of */
+             afs_int32 ea, struct prentry *e)
 {
     afs_int32 head;
     int bit;
@@ -415,13 +388,14 @@ WalkNextChain(map, misc, ea, e)
     struct prentry c;          /* continuation entry */
     afs_int32 na;              /* next thread */
     int ni;
-    afs_int32 eid;
-    int count;                 /* number of members */
+    afs_int32 eid = 0;
+    int count = 0;             /* number of members, set to > 9999 if */
+                               /* list ends early */
     int i;
     int noErrors = 1;
     int length;                        /* length of chain */
 #if defined(SUPERGROUPS)
-    int sgcount;               /* number of sgentrys */
+    int sgcount = 0;           /* number of sgentrys */
     afs_int32 sghead;
 #define g (((struct prentryg *)e))
 #endif
@@ -430,9 +404,7 @@ WalkNextChain(map, misc, ea, e)
        head = ntohl(e->next);
        eid = ntohl(e->id);
        bit = MAP_CONT;
-       count = 0;              /* set to >9999 if list ends early */
 #if defined(SUPERGROUPS)
-       sgcount = 0;
        sghead = ntohl(g->next);
 #endif
        for (i = 0; i < PRSIZE; i++) {
@@ -483,8 +455,8 @@ WalkNextChain(map, misc, ea, e)
                break;
        }
 #if defined(SUPERGROUPS)
-       sghead = g->nextsg;
-       if ((e->flags & PRGRP)) {
+       sghead = ntohl(g->nextsg);
+       if ((e->flags & htonl(PRGRP))) {
            for (i = 0; i < SGSIZE; ++i) {
                afs_int32 id = ntohl(g->supergroup[i]);
                if (id == PRBADID)
@@ -516,7 +488,7 @@ WalkNextChain(map, misc, ea, e)
     for (na = sghead; na; na = ntohl(c.next)) {
        code = ConvertDiskAddress(na, &ni);
        if (code) {
-           fprintf(stderr, "Bad continuation ptr %d", na);
+           fprintf(stderr, "Bad SGcontinuation ptr %d", na);
            if (PrintEntryError(misc, ea, e, 2))
                return PRDBBAD;
            if (na != sghead) {
@@ -703,7 +675,7 @@ WalkNextChain(map, misc, ea, e)
 #if defined(SUPERGROUPS)
        noErrors = 0;
     }
-    if (e && (e->flags & PRGRP) && (sgcount != ntohl(g->countsg))) {
+    if (e && (e->flags & htonl(PRGRP)) && (sgcount != ntohl(g->countsg))) {
        fprintf(stderr, "SGCount was %d should be %d\n", sgcount,
                ntohl(g->countsg));
        if (PrintEntryError(misc, ea, e, 2))
@@ -724,18 +696,16 @@ WalkNextChain(map, misc, ea, e)
 }
 
 afs_int32
-WalkOwnedChain(map, misc, ea, e)
-     char map[];               /* one byte per db entry */
-     struct misc_data *misc;   /* stuff to keep track of */
-     afs_int32 ea;
-     struct prentry *e;
+WalkOwnedChain(char map[],             /* one byte per db entry */
+              struct misc_data *misc,  /* stuff to keep track of */
+              afs_int32 ea, struct prentry *e)
 {
     afs_int32 head;
     afs_int32 code;
     struct prentry c;          /* continuation entry */
     afs_int32 na;              /* next thread */
     int ni;
-    afs_int32 eid;
+    afs_int32 eid = 0;
     int length;                        /* length of chain */
 
     if (e) {
@@ -808,9 +778,8 @@ WalkOwnedChain(map, misc, ea, e)
 }
 
 afs_int32
-WalkChains(map, misc)
-     char map[];               /* one byte per db entry */
-     struct misc_data *misc;   /* stuff to keep track of */
+WalkChains(char map[],         /* one byte per db entry */
+          struct misc_data *misc)      /* stuff to keep track of */
 {
     afs_int32 code;
     int ei;
@@ -908,7 +877,7 @@ WalkChains(map, misc)
            case PRFOREIGN:
                fprintf(stderr,
                        "ENTRY IS unexpected type [PRFOREIGN] (flags=0x%x)\n",
-                       e.flags);
+                       ntohl(e.flags));
                break;
            case PRINST:
                misc->ninsts++;
@@ -923,9 +892,7 @@ WalkChains(map, misc)
 }
 
 afs_int32
-GC(map, misc)
-     char map[];
-     struct misc_data *misc;
+GC(char map[], struct misc_data *misc)
 {
     afs_int32 code;
     int ei;
@@ -957,13 +924,14 @@ GC(map, misc)
            id = ntohl(e.id);
 #if defined(SUPERGROUPS)
            if ((id != ANONYMOUSID)
-               && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count))) {
+               && ((refCount = idcount(&misc->idmap, id)) != ntohl(e.count)))
 #else
            if ((id >= misc->minId) && (id <= misc->maxId)
                && (id != ANONYMOUSID)
                && ((refCount = misc->idmap[id - misc->minId]) !=
-                   ntohl(e.count))) {
+                   ntohl(e.count)))
 #endif /* SUPERGROUPS */
+             {
                afs_int32 na;
                fprintf(stderr,
                        "Entry membership count is inconsistent: %d entries refer to this one\n",
@@ -990,24 +958,18 @@ GC(map, misc)
 }
 
 char *
-QuoteName(s)
-     char *s;
+QuoteName(char *s)
 {
     char *qs;
     if (strpbrk(s, " \t")) {
-       qs = (char *)malloc(strlen(s) + 3);
-       strcpy(qs, "\"");
-       strcat(qs, s);
-       strcat(qs, "\"");
+       asprintf(&qs, "\"%s\"", s);
     } else
        qs = s;
     return qs;
 }
 
 afs_int32
-DumpRecreate(map, misc)
-     char map[];
-     struct misc_data *misc;
+DumpRecreate(char map[], struct misc_data *misc)
 {
     afs_int32 code;
     int ei;
@@ -1068,10 +1030,11 @@ DumpRecreate(map, misc)
                /* check for duplicate id.  This may still lead to duplicate
                 * names. */
 #if defined(SUPERGROUPS)
-               if (idcount(&idmap, id)) {
+               if (idcount(&idmap, id))
 #else
-               if (idmap[id - misc->minId]) {
+               if (idmap[id - misc->minId])
 #endif
+                 {
                    fprintf(stderr, "Skipping entry with duplicate id %di\n",
                            id);
                    goto user_done;
@@ -1178,10 +1141,11 @@ DumpRecreate(map, misc)
 
            owner = ntohl(e.owner);
 #if defined(SUPERGROUPS)
-           if (!idcount(&idmap, owner)) {
+           if (!idcount(&idmap, owner))
 #else
-           if (idmap[owner - misc->minId] == 0) {
+           if (idmap[owner - misc->minId] == 0)
 #endif
+             {
                fprintf(stderr,
                        "Skipping chown of '%s' to non-existant owner %di\n",
                        e.name, owner);
@@ -1206,9 +1170,6 @@ DumpRecreate(map, misc)
            if ((id < 0) && (flags & PRGRP)) {
                int count = 0;
                afs_int32 na;
-#if defined(SUPERGROUPS)
-               afs_int32 ng;
-#endif
                int i;
                for (i = 0; i < PRSIZE; i++) {
                    afs_int32 uid = ntohl(e.entries[i]);
@@ -1227,43 +1188,6 @@ DumpRecreate(map, misc)
                                id);
 #endif
                }
-#if defined(SUPERGROUPS)
-#define g      (*((struct prentryg *)&e))
-               ng = ntohl(g.nextsg);
-               for (i = 0; i < SGSIZE; i++) {
-                   afs_int32 uid = ntohl(g.supergroup[i]);
-                   if (uid == 0)
-                       break;
-                   if (uid == PRBADID)
-                       continue;
-                   fprintf(rc, "au %d %d\n", uid, id);
-                   count++;
-               }
-               while (ng) {
-                   struct prentry c;
-                   code = pr_Read(ng, (char *)&c, sizeof(c));
-                   if (code)
-                       return code;
-
-                   if ((id == ntohl(c.id)) && (ntohl(c.flags) & PRCONT)) {
-                       for (i = 0; i < COSIZE; i++) {
-                           afs_int32 uid = ntohl(c.entries[i]);
-                           if (uid == 0)
-                               break;
-                           if (uid == PRBADID)
-                               continue;
-                           fprintf(rc, "au %d %d\n", uid, id);
-                           count++;
-                       }
-                   } else {
-                       fprintf(stderr, "Skipping continuation block at %d\n",
-                               ng);
-                       break;
-                   }
-                   ng = ntohl(c.next);
-               }
-#undef g
-#endif /* SUPERGROUPS */
                na = ntohl(e.next);
                while (na) {
                    struct prentry c;
@@ -1271,7 +1195,7 @@ DumpRecreate(map, misc)
                    if (code)
                        return code;
 
-                   if ((id == ntohl(c.id)) && (ntohl(c.flags) & PRCONT)) {
+                   if ((id == ntohl(c.id)) && (c.flags & htonl(PRCONT))) {
                        for (i = 0; i < COSIZE; i++) {
                            afs_int32 uid = ntohl(c.entries[i]);
                            if (uid == 0)
@@ -1309,8 +1233,7 @@ DumpRecreate(map, misc)
 }
 
 afs_int32
-CheckPrDatabase(misc)
-     struct misc_data *misc;   /* info & statistics */
+CheckPrDatabase(struct misc_data *misc)        /* info & statistics */
 {
     afs_int32 code;
     afs_int32 eof;
@@ -1322,7 +1245,8 @@ CheckPrDatabase(misc)
     n = eof / sizeof(struct prentry);
     if ((eof < 0) || (n * sizeof(struct prentry) != eof)) {
        code = PRDBBAD;
-       com_err(whoami, code, "eof ptr no good: eof=%d, sizeof(prentry)=%d",
+       afs_com_err(whoami, code,
+                   "eof ptr no good: eof=%d, sizeof(prentry)=%" AFS_SIZET_FMT,
                eof, sizeof(struct prentry));
       abort:
        return code;
@@ -1339,7 +1263,7 @@ CheckPrDatabase(misc)
     }
     code = WalkHashTable(cheader.nameHash, MAP_NAMEHASH, map, misc);
     if (code) {
-       com_err(whoami, code, "walking name hash");
+       afs_com_err(whoami, code, "walking name hash");
        goto abort;
     }
     if (misc->verbose) {
@@ -1348,7 +1272,7 @@ CheckPrDatabase(misc)
     }
     code = WalkHashTable(cheader.idHash, MAP_IDHASH, map, misc);
     if (code) {
-       com_err(whoami, code, "walking id hash");
+       afs_com_err(whoami, code, "walking id hash");
        goto abort;
     }
 
@@ -1360,7 +1284,7 @@ CheckPrDatabase(misc)
     misc->idRange = n - misc->minId + 1;
     misc->idmap = (afs_int32 *) malloc(misc->idRange * sizeof(afs_int32));
     if (!misc->idmap) {
-       com_err(whoami, 0, "Unable to malloc space for max ids of %d",
+       afs_com_err(whoami, 0, "Unable to malloc space for max ids of %d",
                misc->idRange);
        code = -1;
        goto abort;
@@ -1374,7 +1298,7 @@ CheckPrDatabase(misc)
     }
     code = WalkChains(map, misc);
     if (code) {
-       com_err(whoami, code, "walking chains");
+       afs_com_err(whoami, code, "walking chains");
        goto abort;
     }
     if (misc->verbose) {
@@ -1383,7 +1307,7 @@ CheckPrDatabase(misc)
     }
     code = WalkNextChain(map, misc, 0, 0);
     if (code) {
-       com_err(whoami, code, "walking free list");
+       afs_com_err(whoami, code, "walking free list");
        goto abort;
     }
     if (misc->verbose) {
@@ -1392,7 +1316,7 @@ CheckPrDatabase(misc)
     }
     code = WalkOwnedChain(map, misc, 0, 0);
     if (code) {
-       com_err(whoami, code, "walking orphan list");
+       afs_com_err(whoami, code, "walking orphan list");
        goto abort;
     }
 
@@ -1402,7 +1326,7 @@ CheckPrDatabase(misc)
     }
     code = GC(map, misc);
     if (code) {
-       com_err(whoami, code, "looking for unreferenced entries");
+       afs_com_err(whoami, code, "looking for unreferenced entries");
        goto abort;
     }
 
@@ -1446,14 +1370,14 @@ CheckPrDatabase(misc)
               misc->nforeigns, misc->ngroups);
     }
 
+    free(map);
     return code;
 }
 
 #include "AFS_component_version_number.c"
 
-WorkerBee(as, arock)
-     struct cmd_syndesc *as;
-     char *arock;
+int
+WorkerBee(struct cmd_syndesc *as, void *arock)
 {
     afs_int32 code;
     char *recreateFile;
@@ -1474,7 +1398,7 @@ WorkerBee(as, arock)
 
     fd = open(pr_dbaseName, O_RDONLY, 0);
     if (fd == -1) {
-       com_err(whoami, errno, "Open failed on db %s", pr_dbaseName);
+       afs_com_err(whoami, errno, "Open failed on db %s", pr_dbaseName);
        exit(2);
     }
 
@@ -1492,7 +1416,7 @@ WorkerBee(as, arock)
     if (recreateFile) {
        misc.recreate = fopen(recreateFile, "w");
        if (misc.recreate == 0) {
-           com_err(whoami, errno,
+           afs_com_err(whoami, errno,
                    "can't create file for recreation instructions: %s",
                    recreateFile);
            exit(4);
@@ -1500,15 +1424,14 @@ WorkerBee(as, arock)
     }
     code = CheckPrDatabase(&misc);
     if (code) {
-       com_err(whoami, code, "Checking prserver database");
+       afs_com_err(whoami, code, "Checking prserver database");
        exit(3);
     }
     exit(0);
 }
 
-main(argc, argv)
-     int argc;
-     char *argv[];
+int
+main(int argc, char *argv[])
 {
     struct cmd_syndesc *ts;
 
@@ -1534,11 +1457,10 @@ main(argc, argv)
 /* new routines to deal with very large ID numbers */
 
 void
-zeromap(idmap)
-     struct idused *idmap;
+zeromap(struct idused *idmap)
 {
     while (idmap) {
-       bzero((char *)idmap->idcount, sizeof idmap->idcount);
+       memset(idmap->idcount, 0, sizeof idmap->idcount);
        idmap = idmap->idnext;
     }
 }
@@ -1552,7 +1474,7 @@ inccount(struct idused **idmapp, int id)
        fprintf(stderr, "IDCOUNT must be power of 2!\n");
        exit(1);
     }
-    while (idmap = *idmapp) {
+    while ((idmap = *idmapp) != NULL) {
        if (idmap->idstart == (id & ~(IDCOUNT - 1)))
            break;
        idmapp = &idmap->idnext;
@@ -1563,7 +1485,7 @@ inccount(struct idused **idmapp, int id)
            perror("idmap");
            exit(1);
        }
-       bzero((char *)idmap, sizeof idmap);
+       memset(idmap, 0, sizeof idmap);
        idmap->idstart = id & ~(IDCOUNT - 1);
        idmap->idnext = *idmapp;
        *idmapp = idmap;
@@ -1572,8 +1494,7 @@ inccount(struct idused **idmapp, int id)
 }
 
 int
-idcount(idmapp, id)
-     struct idused **idmapp;
+idcount(struct idused **idmapp, int id)
 {
     struct idused *idmap;
 
@@ -1581,7 +1502,7 @@ idcount(idmapp, id)
        fprintf(stderr, "IDCOUNT must be power of 2!\n");
        exit(1);
     }
-    while (idmap = *idmapp) {
+    while ((idmap = *idmapp) != NULL) {
        if (idmap->idstart == (id & ~(IDCOUNT - 1))) {
            return idmap->idcount[id & (IDCOUNT - 1)];
        }
@@ -1589,5 +1510,4 @@ idcount(idmapp, id)
     }
     return 0;
 }
-
 #endif /* SUPERGROUPS */