pt_util: gcc warning fix
[openafs.git] / src / ptserver / pt_util.c
index f74b6a9..2b6d35b 100644 (file)
  *                  output of this program to be valid.
  */
 
-#include <sys/types.h>
-#include <sys/time.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/file.h>
-
 #include <afsconfig.h>
 #include <afs/param.h>
-#include <afs/com_err.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
+
+#ifndef _WIN32
+#include <sys/file.h>
+#else
+#define L_SET SEEK_SET
+#endif
+#include <ctype.h>
 
+#include <afs/com_err.h>
 #include <afs/cmd.h>           /*Command line parsing */
 #include <afs/afsutil.h>
-#include <errno.h>
 #include <lock.h>
-#include <netinet/in.h>
 #define UBIK_INTERNALS
 #include <ubik.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
-#include <afs/com_err.h>
+
 #include "ptint.h"
 #include "ptserver.h"
 #include "pterror.h"
+#include "ptprototypes.h"
 
 #define IDHash(x) (abs(x) % HASHSIZE)
 #define print_id(x) ( ((flags&DO_SYS)==0 && (x<-32767 || x>97536)) || \
@@ -47,16 +44,19 @@ RCSID
 extern char *optarg;
 extern int optind;
 
+extern int pr_noAuth;
+
 int restricted = 0;
-int display_entry();
-void add_group();
-void display_groups();
-void display_group();
-void fix_pre();
-char *checkin();
-char *check_core();
-char *id_to_name();
-int CommandProc(struct cmd_syndesc *, void *);
+
+static int display_entry(int);
+static void add_group(long);
+static void display_groups(void);
+static void display_group(int);
+static void fix_pre(struct prentry *);
+static char *id_to_name(int);
+static char *checkin(struct prentry *);
+static char *check_core(int);
+static int CommandProc(struct cmd_syndesc *, void *);
 
 struct hash_entry {
     char h_name[PR_MAXNAMELEN];
@@ -105,8 +105,8 @@ int
 main(int argc, char **argv)
 {
 
-    register struct cmd_syndesc *cs;   /*Command line syntax descriptor */
-    register afs_int32 code;   /*Return code */
+    struct cmd_syndesc *cs;    /*Command line syntax descriptor */
+    afs_int32 code;    /*Return code */
 
     cs = cmd_CreateSyntax(NULL, CommandProc, NULL,
                          "access protection database");
@@ -130,17 +130,17 @@ main(int argc, char **argv)
 
 }
 
-int
-CommandProc(register struct cmd_syndesc *a_as, void *arock)
+static int
+CommandProc(struct cmd_syndesc *a_as, void *arock)
 {
-    register int i;
+    int i;
     long code = 0;
-    long cc, upos;
+    long upos;
     long gpos = 0;
     struct prentry uentry, gentry;
     struct ubik_hdr *uh;
     char *dfile = 0;
-    char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
+    const char *pbase = AFSDIR_SERVER_PRDB_FILEPATH;
     char *pfile = NULL;
     char pbuffer[1028];
     struct cmd_parmdesc *tparm;
@@ -149,6 +149,9 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
 
     if (tparm[0].items) {
        wflag++;
+       /* so we are treated as admin and can create "mis"owned
+          groups */
+       pr_noAuth = 1;
     }
     if (tparm[1].items) {
        flags |= DO_USR;
@@ -176,7 +179,7 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
     }
 
     if (pfile == NULL) {
-        afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
+        snprintf(pbuffer, sizeof(pbuffer), "%s.DB0", pbase);
         pfile = pbuffer;
     }
     if ((dbase_fd = open(pfile, (wflag ? O_RDWR : O_RDONLY) | O_CREAT, 0600))
@@ -233,13 +236,26 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
 
     if (wflag) {
        struct usr_list *u;
+       int seenGroup = 0, id = 0, flags = 0;
 
        while (fgets(buffer, sizeof(buffer), dfp)) {
-           int id, oid, cid, flags, quota, uid;
+           int oid, cid, quota, uid;
            char name[PR_MAXNAMELEN], mem[PR_MAXNAMELEN];
 
            if (isspace(*buffer)) {
-               sscanf(buffer, "%s %d", mem, &uid);
+               code = sscanf(buffer, "%s %d", mem, &uid);
+               if (code != 2) {
+                   fprintf(stderr,
+                           "Insuffient data provided for group membership\n");
+                   exit(1);
+               }
+
+               if (!seenGroup) {
+                   fprintf(stderr,
+                           "Group member %s listed outside of group\n",
+                           mem);
+                   exit(1);
+               }
 
                for (u = usr_head; u; u = u->next)
                    if (u->uid && u->uid == uid)
@@ -289,8 +305,15 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
                    fprintf(stderr, "Error while adding %s to %s: %s\n", mem,
                            name, afs_error_message(code));
            } else {
-               sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
-                      &oid, &cid);
+               code = sscanf(buffer, "%s %d/%d %d %d %d", name, &flags, &quota, &id,
+                             &oid, &cid);
+               if (code != 6) {
+                   fprintf(stderr,
+                           "Insufficient data provided for user/group\n");
+                   exit(1);
+               }
+
+               seenGroup = 1;
 
                if (FindByID(0, id))
                    code = PRIDEXIST;
@@ -298,7 +321,7 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
                    code = CreateEntry(0, name, &id, 1 /*idflag */ ,
                                       flags & PRGRP, oid, cid);
                if (code == PRBADNAM) {
-                   u = (struct usr_list *)malloc(sizeof(struct usr_list));
+                   u = malloc(sizeof(struct usr_list));
                    u->next = usr_head;
                    u->uid = id;
                    strcpy(u->name, name);
@@ -334,7 +357,7 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
                long newpos;
                newpos = display_entry(upos);
                if (newpos == upos) {
-                   fprintf(stderr, "pt_util: hash error in %s chain %d\n", 
+                   fprintf(stderr, "pt_util: hash error in %s chain %d\n",
                            nflag ? "name":"id", i);
                    exit(1);
                } else
@@ -367,11 +390,9 @@ CommandProc(register struct cmd_syndesc *a_as, void *arock)
     exit(0);
 }
 
-int
+static int
 display_entry(int offset)
 {
-    register int i;
-
     lseek(dbase_fd, offset + HDRSIZE, L_SET);
     read(dbase_fd, &pre, sizeof(struct prentry));
 
@@ -391,15 +412,15 @@ display_entry(int offset)
     return (nflag ? pre.nextName : pre.nextID);
 }
 
-void
+static void
 add_group(long id)
 {
     struct grp_list *g;
-    register long i;
+    long i;
 
     i = grp_count++ % 1024;
     if (i == 0) {
-       g = (struct grp_list *)malloc(sizeof(struct grp_list));
+       g = malloc(sizeof(struct grp_list));
        g->next = grp_head;
        grp_head = g;
     }
@@ -407,10 +428,10 @@ add_group(long id)
     g->groups[i] = id;
 }
 
-void
-display_groups()
+static void
+display_groups(void)
 {
-    register int i, id;
+    int i, id;
     struct grp_list *g;
 
     g = grp_head;
@@ -426,10 +447,10 @@ display_groups()
     }
 }
 
-void
+static void
 display_group(int id)
 {
-    register int i, offset;
+    int i, offset;
     int print_grp = 0;
 
     offset = ntohl(prh.idHash[IDHash(id)]);
@@ -497,10 +518,10 @@ display_group(int id)
     }
 }
 
-void
+static void
 fix_pre(struct prentry *pre)
 {
-    register int i;
+    int i;
 
     pre->flags = ntohl(pre->flags);
     pre->id = ntohl(pre->id);
@@ -524,10 +545,10 @@ fix_pre(struct prentry *pre)
     }
 }
 
-char *
+static char *
 id_to_name(int id)
 {
-    register int offset;
+    int offset;
     static struct prentry pre;
     char *name;
 
@@ -551,11 +572,11 @@ id_to_name(int id)
     return 0;
 }
 
-char *
+static char *
 checkin(struct prentry *pre)
 {
     struct hash_entry *he, *last;
-    register int id;
+    int id;
 
     id = pre->id;
     last = (struct hash_entry *)0;
@@ -566,7 +587,7 @@ checkin(struct prentry *pre)
        last = he;
        he = he->next;
     }
-    he = (struct hash_entry *)malloc(sizeof(struct hash_entry));
+    he = malloc(sizeof(struct hash_entry));
     if (he == 0) {
        fprintf(stderr, "pt_util: No Memory for internal hash table.\n");
        exit(1);
@@ -581,8 +602,8 @@ checkin(struct prentry *pre)
     return (he->h_name);
 }
 
-char *
-check_core(register int id)
+static char *
+check_core(int id)
 {
     struct hash_entry *he;
     he = hat[IDHash(id)];