pt_util: gcc warning fix
[openafs.git] / src / ptserver / pt_util.c
index e0f53ad..2b6d35b 100644 (file)
  *                  output of this program to be valid.
  */
 
-#include <sys/types.h>
+#include <afsconfig.h>
+#include <afs/param.h>
+
+#include <roken.h>
+
 #ifndef _WIN32
-#include <sys/time.h>
 #include <sys/file.h>
 #else
-#include <fcntl.h>
-#include <io.h>
 #define L_SET SEEK_SET
 #endif
-#include <stdio.h>
 #include <ctype.h>
-#include <string.h>
-#include <errno.h>
 
-#include <afsconfig.h>
-#include <afs/param.h>
 #include <afs/com_err.h>
-
-
 #include <afs/cmd.h>           /*Command line parsing */
 #include <afs/afsutil.h>
-#include <errno.h>
 #include <lock.h>
-#ifndef _WIN32
-#include <netinet/in.h>
-#endif
 #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"
@@ -54,6 +44,8 @@
 extern char *optarg;
 extern int optind;
 
+extern int pr_noAuth;
+
 int restricted = 0;
 
 static int display_entry(int);
@@ -157,6 +149,9 @@ CommandProc(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;
@@ -184,7 +179,7 @@ CommandProc(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))
@@ -241,13 +236,26 @@ CommandProc(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)
@@ -297,8 +305,15 @@ CommandProc(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;
@@ -306,7 +321,7 @@ CommandProc(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);
@@ -342,7 +357,7 @@ CommandProc(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
@@ -405,7 +420,7 @@ add_group(long id)
 
     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;
     }
@@ -572,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);