ptserver: move IDToName, NameToID to ptprocs.c and make static
[openafs.git] / src / ptserver / utils.c
index 4999c11..d0f6ac2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
-#include <sys/types.h>
+#include <afs/opr.h>
 #include <lock.h>
 #include <ubik.h>
-#include <stdio.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <netinet/in.h>
-#include <netdb.h>
-#endif
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
+
 #include "ptserver.h"
 #include "pterror.h"
 
@@ -39,35 +25,28 @@ afs_int32 IsAMemberOfSG(struct ubik_trans *at, afs_int32 aid, afs_int32 gid,
                        afs_int32 depth);
 #endif
 
-afs_int32
-IDHash(x)
-     afs_int32 x;
+static afs_int32
+IDHash(afs_int32 x)
 {
     /* returns hash bucket for x */
     return ((abs(x)) % HASHSIZE);
 }
 
 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;
+    size_t 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);
 }
 
 
 afs_int32
-pr_Write(tt, afd, pos, buff, len)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     char *buff;
-     afs_int32 len;
+pr_Write(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, void *buff, afs_int32 len)
 {
     /* package up seek and write into one procedure for ease of use */
     afs_int32 code;
@@ -84,12 +63,7 @@ pr_Write(tt, afd, pos, buff, len)
 }
 
 afs_int32
-pr_Read(tt, afd, pos, buff, len)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     char *buff;
-     afs_int32 len;
+pr_Read(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, void *buff, afs_int32 len)
 {
     /* same thing for read */
     afs_int32 code;
@@ -100,14 +74,11 @@ pr_Read(tt, afd, pos, buff, len)
     return code;
 }
 
-pr_WriteEntry(tt, afd, pos, tentry)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     struct prentry *tentry;
+int
+pr_WriteEntry(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, struct prentry *tentry)
 {
     afs_int32 code;
-    register afs_int32 i;
+    afs_int32 i;
     struct prentry nentry;
 
     if (ntohl(1) != 1) {       /* Need to swap bytes. */
@@ -130,12 +101,10 @@ pr_WriteEntry(tt, afd, pos, tentry)
        nentry.sibling = htonl(tentry->sibling);
        nentry.child = htonl(tentry->child);
        strncpy(nentry.name, tentry->name, PR_MAXNAMELEN);
-#ifdef PR_REMEMBER_TIMES
        nentry.createTime = htonl(tentry->createTime);
        nentry.addTime = htonl(tentry->addTime);
        nentry.removeTime = htonl(tentry->removeTime);
        nentry.changeTime = htonl(tentry->changeTime);
-#endif
        for (i = 0; i < PRSIZE; i++)
            nentry.entries[i] = htonl(tentry->entries[i]);
        tentry = &nentry;
@@ -144,14 +113,11 @@ pr_WriteEntry(tt, afd, pos, tentry)
     return (code);
 }
 
-pr_ReadEntry(tt, afd, pos, tentry)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     struct prentry *tentry;
+int
+pr_ReadEntry(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, struct prentry *tentry)
 {
     afs_int32 code;
-    register afs_int32 i;
+    afs_int32 i;
     struct prentry nentry;
     code = ubik_Seek(tt, afd, pos);
     if (code)
@@ -182,25 +148,20 @@ pr_ReadEntry(tt, afd, pos, tentry)
     tentry->sibling = ntohl(nentry.sibling);
     tentry->child = ntohl(nentry.child);
     strncpy(tentry->name, nentry.name, PR_MAXNAMELEN);
-#ifdef PR_REMEMBER_TIMES
     tentry->createTime = ntohl(nentry.createTime);
     tentry->addTime = ntohl(nentry.addTime);
     tentry->removeTime = ntohl(nentry.removeTime);
     tentry->changeTime = ntohl(nentry.changeTime);
-#endif
     for (i = 0; i < PRSIZE; i++)
        tentry->entries[i] = ntohl(nentry.entries[i]);
     return (code);
 }
 
-pr_WriteCoEntry(tt, afd, pos, tentry)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     struct contentry *tentry;
+int
+pr_WriteCoEntry(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, struct contentry *tentry)
 {
     afs_int32 code;
-    register afs_int32 i;
+    afs_int32 i;
     struct contentry nentry;
 
     if (ntohl(1) != 1) {       /* No need to swap */
@@ -217,14 +178,11 @@ pr_WriteCoEntry(tt, afd, pos, tentry)
     return (code);
 }
 
-pr_ReadCoEntry(tt, afd, pos, tentry)
-     struct ubik_trans *tt;
-     afs_int32 afd;
-     afs_int32 pos;
-     struct contentry *tentry;
+int
+pr_ReadCoEntry(struct ubik_trans *tt, afs_int32 afd, afs_int32 pos, struct contentry *tentry)
 {
     afs_int32 code;
-    register afs_int32 i;
+    afs_int32 i;
     struct contentry nentry;
     code = ubik_Seek(tt, afd, pos);
     if (code)
@@ -250,10 +208,9 @@ pr_ReadCoEntry(tt, afd, pos, tentry)
  * new entry */
 
 afs_int32
-AllocBlock(at)
-     register struct ubik_trans *at;
+AllocBlock(struct ubik_trans *at)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 temp;
     struct prentry tentry;
 
@@ -284,12 +241,10 @@ AllocBlock(at)
 }
 
 afs_int32
-FreeBlock(at, pos)
-     register struct ubik_trans *at;
-     afs_int32 pos;
+FreeBlock(struct ubik_trans *at, afs_int32 pos)
 {
     /* add a block of storage to the free list */
-    register afs_int32 code;
+    afs_int32 code;
     struct prentry tentry;
 
     memset(&tentry, 0, sizeof(tentry));
@@ -307,12 +262,10 @@ FreeBlock(at, pos)
 }
 
 afs_int32
-FindByID(at, aid)
-     register struct ubik_trans *at;
-     afs_int32 aid;
+FindByID(struct ubik_trans *at, afs_int32 aid)
 {
     /* returns address of entry if found, 0 otherwise */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     struct prentry tentry;
     afs_int32 entry;
@@ -329,6 +282,7 @@ FindByID(at, aid)
        return 0;
     if (aid == tentry.id)
        return entry;
+    opr_Assert(entry != tentry.nextID);
     entry = tentry.nextID;
     while (entry != 0) {
        memset(&tentry, 0, sizeof(tentry));
@@ -337,21 +291,17 @@ FindByID(at, aid)
            return 0;
        if (aid == tentry.id)
            return entry;
+       opr_Assert(entry != tentry.nextID);
        entry = tentry.nextID;
     }
     return 0;
 }
 
-
-
 afs_int32
-FindByName(at, aname, tentryp)
-     register struct ubik_trans *at;
-     char aname[PR_MAXNAMELEN];
-     struct prentry *tentryp;
+FindByName(struct ubik_trans *at, char aname[PR_MAXNAMELEN], struct prentry *tentryp)
 {
     /* ditto */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     afs_int32 entry;
 
@@ -365,6 +315,7 @@ FindByName(at, aname, tentryp)
        return 0;
     if ((strncmp(aname, tentryp->name, PR_MAXNAMELEN)) == 0)
        return entry;
+    opr_Assert(entry != tentryp->nextName);
     entry = tentryp->nextName;
     while (entry != 0) {
        memset(tentryp, 0, sizeof(struct prentry));
@@ -373,25 +324,24 @@ FindByName(at, aname, tentryp)
            return 0;
        if ((strncmp(aname, tentryp->name, PR_MAXNAMELEN)) == 0)
            return entry;
+       opr_Assert(entry != tentryp->nextName);
        entry = tentryp->nextName;
     }
     return 0;
 }
 
 afs_int32
-AllocID(at, flag, aid)
-     register struct ubik_trans *at;
-     afs_int32 flag;
-     afs_int32 *aid;
+AllocID(struct ubik_trans *at, afs_int32 flag, afs_int32 *aid)
 {
     /* allocs an id from the proper area of address space, based on flag */
-    register afs_int32 code = 1;
-    register afs_int32 i = 0;
-    register maxcount = 50;    /* to prevent infinite loops */
+    afs_int32 code = 1;
+    afs_int32 i = 0;
+    int maxcount = 50; /* to prevent infinite loops */
 
     if (flag & PRGRP) {
        *aid = ntohl(cheader.maxGroup);
-       while (code && i < maxcount) {
+       /* Check for PRBADID to avoid wrap-around. */
+       while (code && i < maxcount && *aid != PRBADID) {
            --(*aid);
            code = FindByID(at, *aid);
            i++;
@@ -423,7 +373,7 @@ AllocID(at, flag, aid)
        return PRSUCCESS;
     } else {
        *aid = ntohl(cheader.maxID);
-       while (code && i < maxcount) {
+       while (code && i < maxcount && *aid != 0x7fffffff) {
            ++(*aid);
            code = FindByID(at, *aid);
            i++;
@@ -440,64 +390,24 @@ AllocID(at, flag, aid)
     }
 }
 
-afs_int32
-IDToName(at, aid, aname)
-     register struct ubik_trans *at;
-     afs_int32 aid;
-     char aname[PR_MAXNAMELEN];
-{
-    afs_int32 temp;
-    struct prentry tentry;
-    register afs_int32 code;
-
-    temp = FindByID(at, aid);
-    if (temp == 0)
-       return PRNOENT;
-    code = pr_Read(at, 0, temp, (char *)&tentry, sizeof(tentry));
-    if (code)
-       return code;
-    strncpy(aname, tentry.name, PR_MAXNAMELEN);
-    return PRSUCCESS;
-}
-
-afs_int32
-NameToID(at, aname, aid)
-     register struct ubik_trans *at;
-     char aname[PR_MAXNAMELEN];
-     afs_int32 *aid;
-{
-    afs_int32 temp;
-    struct prentry tentry;
-
-    temp = FindByName(at, aname, &tentry);
-    if (!temp)
-       return PRNOENT;
-    *aid = tentry.id;
-    return PRSUCCESS;
-}
-
 int
-IDCmp(a, b)
-     afs_int32 *a;
-     afs_int32 *b;
+IDCmp(const void *a, const void *b)
 {
     /* used to sort CPS's so that comparison with acl's is easier */
-    if (*a > *b)
+    if (*(afs_int32 *)a > *(afs_int32 *)b) {
        return 1;
-    if (*a == *b)
+    } else if (*(afs_int32 *)a == *(afs_int32 *)b) {
        return 0;
-    if (*a < *b)
+    } else /* (*a < *b) */ {
        return -1;
+    }
 }
 
 afs_int32
-RemoveFromIDHash(tt, aid, loc)
-     struct ubik_trans *tt;
-     afs_int32 aid;
-     afs_int32 *loc;           /* ??? in case ID hashed twice ??? */
+RemoveFromIDHash(struct ubik_trans *tt, afs_int32 aid, afs_int32 *loc)         /* ??? in case ID hashed twice ??? */
 {
     /* remove entry designated by aid from id hash table */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 current, trail, i;
     struct prentry tentry;
     struct prentry bentry;
@@ -515,6 +425,7 @@ RemoveFromIDHash(tt, aid, loc)
     if (code)
        return PRDBFAIL;
     while (aid != tentry.id) {
+       opr_Assert(trail != current);
        trail = current;
        current = tentry.nextID;
        if (current == 0)
@@ -539,19 +450,18 @@ RemoveFromIDHash(tt, aid, loc)
            return PRDBFAIL;
        bentry.nextID = tentry.nextID;
        code = pr_WriteEntry(tt, 0, trail, &bentry);
+       if (code)
+           return PRDBFAIL;
     }
     *loc = current;
     return PRSUCCESS;
 }
 
 afs_int32
-AddToIDHash(tt, aid, loc)
-     struct ubik_trans *tt;
-     afs_int32 aid;
-     afs_int32 loc;            /* ??? */
+AddToIDHash(struct ubik_trans *tt, afs_int32 aid, afs_int32 loc)
 {
     /* add entry at loc designated by aid to id hash table */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     struct prentry tentry;
 
@@ -576,13 +486,10 @@ AddToIDHash(tt, aid, loc)
 }
 
 afs_int32
-RemoveFromNameHash(tt, aname, loc)
-     struct ubik_trans *tt;
-     char *aname;
-     afs_int32 *loc;
+RemoveFromNameHash(struct ubik_trans *tt, char *aname, afs_int32 *loc)
 {
     /* remove from name hash */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 current, trail, i;
     struct prentry tentry;
     struct prentry bentry;
@@ -598,6 +505,7 @@ RemoveFromNameHash(tt, aname, loc)
     if (code)
        return PRDBFAIL;
     while (strcmp(aname, tentry.name)) {
+       opr_Assert(trail != current);
        trail = current;
        current = tentry.nextName;
        if (current == 0)
@@ -622,19 +530,18 @@ RemoveFromNameHash(tt, aname, loc)
            return PRDBFAIL;
        bentry.nextName = tentry.nextName;
        code = pr_WriteEntry(tt, 0, trail, &bentry);
+       if (code)
+           return PRDBFAIL;
     }
     *loc = current;
     return PRSUCCESS;
 }
 
 afs_int32
-AddToNameHash(tt, aname, loc)
-     struct ubik_trans *tt;
-     char *aname;
-     afs_int32 loc;
+AddToNameHash(struct ubik_trans *tt, char *aname, afs_int32 loc)
 {
     /* add to name hash */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     struct prentry tentry;
 
@@ -657,13 +564,10 @@ AddToNameHash(tt, aname, loc)
 }
 
 afs_int32
-AddToOwnerChain(at, gid, oid)
-     struct ubik_trans *at;
-     afs_int32 gid;
-     afs_int32 oid;
+AddToOwnerChain(struct ubik_trans *at, afs_int32 gid, afs_int32 oid)
 {
     /* add entry designated by gid to owner chain of entry designated by oid */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 loc;
     struct prentry tentry;
     struct prentry gentry;
@@ -698,12 +602,9 @@ AddToOwnerChain(at, gid, oid)
 /* RemoveFromOwnerChain - remove gid from owner chain for oid */
 
 afs_int32
-RemoveFromOwnerChain(at, gid, oid)
-     struct ubik_trans *at;
-     afs_int32 gid;
-     afs_int32 oid;
+RemoveFromOwnerChain(struct ubik_trans *at, afs_int32 gid, afs_int32 oid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 nptr;
     struct prentry thisEntry;
     struct prentry thatEntry;
@@ -760,11 +661,9 @@ RemoveFromOwnerChain(at, gid, oid)
 /* AddToOrphan - add gid to orphan list, as it's owner has died */
 
 afs_int32
-AddToOrphan(at, gid)
-     struct ubik_trans *at;
-     afs_int32 gid;
+AddToOrphan(struct ubik_trans *at, afs_int32 gid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 loc;
     struct prentry tentry;
 
@@ -786,12 +685,10 @@ AddToOrphan(at, gid)
 }
 
 afs_int32
-RemoveFromOrphan(at, gid)
-     struct ubik_trans *at;
-     afs_int32 gid;
+RemoveFromOrphan(struct ubik_trans *at, afs_int32 gid)
 {
     /* remove gid from the orphan list */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 loc;
     afs_int32 nptr;
     struct prentry tentry;
@@ -843,13 +740,10 @@ RemoveFromOrphan(at, gid)
 }
 
 afs_int32
-IsOwnerOf(at, aid, gid)
-     struct ubik_trans *at;
-     afs_int32 aid;
-     afs_int32 gid;
+IsOwnerOf(struct ubik_trans *at, afs_int32 aid, afs_int32 gid)
 {
     /* returns 1 if aid is the owner of gid, 0 otherwise */
-    register afs_int32 code;
+    afs_int32 code;
     struct prentry tentry;
     afs_int32 loc;
 
@@ -865,12 +759,10 @@ IsOwnerOf(at, aid, gid)
 }
 
 afs_int32
-OwnerOf(at, gid)
-     struct ubik_trans *at;
-     afs_int32 gid;
+OwnerOf(struct ubik_trans *at, afs_int32 gid)
 {
     /* returns the owner of gid */
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 loc;
     struct prentry tentry;
 
@@ -885,16 +777,13 @@ OwnerOf(at, gid)
 
 
 afs_int32
-IsAMemberOf(at, aid, gid)
-     struct ubik_trans *at;
-     afs_int32 aid;
-     afs_int32 gid;
+IsAMemberOf(struct ubik_trans *at, afs_int32 aid, afs_int32 gid)
 {
     /* returns true if aid is a member of gid */
 #if !defined(SUPERGROUPS)
     struct prentry tentry;
     struct contentry centry;
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     afs_int32 loc;
 #endif
@@ -904,6 +793,9 @@ IsAMemberOf(at, aid, gid)
        return 1;
     if (gid == AUTHUSERID && aid != ANONYMOUSID)
        return 1;
+    /* check -localauth case */
+    if (gid == SYSADMINID && aid == SYSADMINID)
+        return 1;
     if ((gid == 0) || (aid == 0))
        return 0;
 #if defined(SUPERGROUPS)
@@ -946,18 +838,13 @@ IsAMemberOf(at, aid, gid)
 
 
 #if defined(SUPERGROUPS)
-
 afs_int32
-IsAMemberOfSG(at, aid, gid, depth)
-     struct ubik_trans *at;
-     afs_int32 aid;
-     afs_int32 gid;
-     afs_int32 depth;
+IsAMemberOfSG(struct ubik_trans *at, afs_int32 aid, afs_int32 gid, afs_int32 depth)
 {
     /* returns true if aid is a member of gid */
     struct prentry tentry;
     struct contentry centry;
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 i;
     afs_int32 loc;
 
@@ -983,7 +870,9 @@ IsAMemberOfSG(at, aid, gid, depth)
        if (gid == AUTHUSERID && aid != ANONYMOUSID)
            return 1;
        if (gid < 0) {
+#ifndef AFS_PTHREAD_ENV
            IOMGR_Poll();
+#endif
            if (IsAMemberOfSG(at, aid, gid, depth - 1))
                return 1;
        }
@@ -1006,7 +895,9 @@ IsAMemberOfSG(at, aid, gid, depth)
                if (gid == AUTHUSERID && aid != ANONYMOUSID)
                    return 1;
                if (gid < 0) {
+#ifndef AFS_PTHREAD_ENV
                    IOMGR_Poll();
+#endif
                    if (IsAMemberOfSG(at, aid, gid, depth - 1))
                        return 1;
                }
@@ -1016,5 +907,4 @@ IsAMemberOfSG(at, aid, gid, depth)
     }
     return 0;                  /* actually, should never get here */
 }
-
 #endif /* SUPERGROUPS */