Don't cast the return from realloc()
[openafs.git] / src / ptserver / ptuser.c
index 59a0f02..230ba07 100644 (file)
 /*
  * 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>
-#if defined(UKERNEL)
-#include "afs/param.h"
-#else
 #include <afs/param.h>
-#endif
+#include <afs/stds.h>
 
+#include <roken.h>
+#include <afs/opr.h>
 
-#if defined(UKERNEL)
-#include "afs/sysincludes.h"
-#include "afs_usrops.h"
-#include "afsincludes.h"
-#include "afs/stds.h"
-#include "rx/rx.h"
-#include "rx/xdr.h"
-#include "afs/auth.h"
-#include "afs/cellconfig.h"
-#include "afs/afsutil.h"
-#include "afs/ptclient.h"
-#include "afs/ptuser.h"
-#include "afs/pterror.h"
-#include "afs/com_err.h"
-#else /* defined(UKERNEL) */
-#include <afs/stds.h>
-#include <ctype.h>
-#include <sys/types.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <netinet/in.h>
-#endif
-#include <stdio.h>
-#include <string.h>
 #include <rx/rx.h>
 #include <rx/xdr.h>
 #include <afs/auth.h>
 #include <afs/cellconfig.h>
 #include <afs/afsutil.h>
 #include <afs/com_err.h>
+
 #include "ptclient.h"
 #include "ptuser.h"
 #include "pterror.h"
-#endif /* defined(UKERNEL) */
 
+#ifdef UKERNEL
+# include "afs_usrops.h"
+#endif
 
 struct ubik_client *pruclient = 0;
 static afs_int32 lastLevel;    /* security level pruclient, if any */
 
 static char *whoami = "libprot";
 
+
+#define ID_HASH_SIZE 1024
+#define ID_STACK_SIZE 1024
+
+/**
+ * Hash table chain of user and group ids.
+ */
+struct idchain {
+    struct idchain *next;
+    afs_int32 id;
+};
+
+/**
+ * Hash table of user and group ids.
+ */
+struct idhash {
+    afs_uint32 userEntries;         /**< number of user id entries hashed */
+    afs_uint32 groupEntries;        /**< number of group id entries hashed */
+    struct idchain *hash[ID_HASH_SIZE];
+};
+
+/**
+ * Allocate a new id hash table.
+ */
+static afs_int32
+AllocateIdHash(struct idhash **aidhash)
+{
+    struct idhash *idhash;
+
+    idhash = calloc(1, sizeof(struct idhash));
+    if (!idhash) {
+       return ENOMEM;
+    }
+    *aidhash = idhash;
+    return 0;
+}
+
+/**
+ * Free the id hash.
+ */
+static void
+FreeIdHash(struct idhash *idhash)
+{
+    int index;
+    struct idchain *chain;
+    struct idchain *next;
+
+    for (index = 0; index < ID_HASH_SIZE; index++) {
+       for (chain = idhash->hash[index]; chain; chain = next) {
+           next = chain->next;
+           free(chain);
+       }
+    }
+    free(idhash);
+}
+
+/**
+ * Indicate if group/user id is already hashed, and
+ * if not insert it.
+ *
+ * @returns whether id is present
+ *   @retval >0 id is already present in the hash
+ *   @retval 0  id was not found and was inserted into the hash
+ *   @retval <0 error encountered
+ */
+static afs_int32
+FindId(struct idhash *idhash, afs_int32 id)
+{
+    afs_int32 index;
+    struct idchain *chain;
+    struct idchain *newChain;
+
+    index = abs(id) % ID_HASH_SIZE;
+    for (chain = idhash->hash[index]; chain; chain = chain->next) {
+       if (chain->id == id) {
+           return 1;
+       }
+    }
+
+    /* Insert this id but return not found. */
+    newChain = (struct idchain *)malloc(sizeof(struct idchain));
+    if (!newChain) {
+       return ENOMEM;
+    } else {
+       newChain->id = id;
+       newChain->next = idhash->hash[index];
+       idhash->hash[index] = newChain;
+       if (id < 0) {
+           idhash->groupEntries++;
+       } else {
+           idhash->userEntries++;
+       }
+    }
+    return 0;
+}
+
+/**
+ * Create an idlist from the ids in the hash.
+ */
+static afs_int32
+CreateIdList(struct idhash *idhash, idlist * alist, afs_int32 select)
+{
+    struct idchain *chain;
+    afs_int32 entries = 0;
+    int index;
+    int i;
+
+    if (select & PRGROUPS) {
+       entries += idhash->groupEntries;
+    }
+    if (select & PRUSERS) {
+       entries += idhash->userEntries;
+    }
+
+    alist->idlist_len = entries;
+    alist->idlist_val = (afs_int32 *) malloc(sizeof(afs_int32) * entries);
+    if (!alist->idlist_val) {
+       return ENOMEM;
+    }
+
+    for (i = 0, index = 0; index < ID_HASH_SIZE; index++) {
+       for (chain = idhash->hash[index]; chain; chain = chain->next) {
+           if (chain->id < 0) {
+               if (select & PRGROUPS) {
+                   alist->idlist_val[i++] = chain->id;
+               }
+           } else {
+               if (select & PRUSERS) {
+                   alist->idlist_val[i++] = chain->id;
+               }
+           }
+       }
+    }
+    return 0;
+}
+
 afs_int32
 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
 {
     afs_int32 code;
     struct rx_connection *serverconns[MAXSERVERS];
-    struct rx_securityClass *sc[3];
+    struct rx_securityClass *sc = NULL;
     static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL;      /* only do this once */
     static char tconfDir[100] = "";
     static char tcell[64] = "";
-    struct ktc_token ttoken;
     afs_int32 scIndex;
+    afs_int32 secFlags;
     static struct afsconf_cell info;
     afs_int32 i;
 #if !defined(UKERNEL)
@@ -87,7 +200,7 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
     }
 #else /* defined(UKERNEL) */
     if (!cell) {
-        if (!tdir) 
+        if (!tdir)
             tdir = afsconf_Open(confDir);
        if (!tdir) {
            if (confDir && strcmp(confDir, ""))
@@ -168,66 +281,34 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
        return code;
     }
 
-    scIndex = secLevel;
-    sc[0] = 0;
-    sc[1] = 0;
-    sc[2] = 0;
     /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
      * to force use of the KeyFile.  secLevel == 0 implies -noauth was
      * specified. */
     if (secLevel == 2) {
-       code = afsconf_GetLatestKey(tdir, 0, 0);
-       if (code) {
-           afs_com_err(whoami, code, 
-                       "(getting key from local KeyFile)\n");
-           scIndex = 0; /* use noauth */
-       } else {
-           /* If secLevel is two assume we're on a file server and use
-            * ClientAuthSecure if possible. */
-           code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
-           if (code) {
-               afs_com_err(whoami, code,
-                           "(calling client secure)\n");
-               scIndex = 0;    /* use noauth */
-           }
-        }
-       if (scIndex != 2)
-           /* if there was a problem, an unauthenticated conn is returned */
-           sc[scIndex] = sc[2];
+       /* If secLevel is two assume we're on a file server and use
+        * ClientAuthSecure if possible. */
+       code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
+       if (code)
+           afs_com_err(whoami, code, "(calling client secure)\n");
     } else if (secLevel > 0) {
-       struct ktc_principal sname;
-       strcpy(sname.cell, info.name);
-       sname.instance[0] = 0;
-       strcpy(sname.name, "afs");
-       code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
+       secFlags = 0;
+       if (secLevel > 1)
+           secFlags |= AFSCONF_SECOPTS_ALWAYSENCRYPT;
+
+       code = afsconf_ClientAuthToken(&info, secFlags, &sc, &scIndex, NULL);
        if (code) {
            afs_com_err(whoami, code, "(getting token)");
            if (secLevel > 1)
                return code;
-           scIndex = 0;
-       } else {
-           if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
-               /* this is a kerberos ticket, set scIndex accordingly */
-               scIndex = 2;
-           else {
-               fprintf(stderr,
-                       "%s: funny kvno (%d) in ticket, proceeding\n",
-                       whoami, ttoken.kvno);
-               scIndex = 2;
-           }
-           sc[2] =
-               rxkad_NewClientSecurityObject((secLevel > 1) ? rxkad_crypt :
-                                             rxkad_clear, &ttoken.sessionKey,
-                                             ttoken.kvno, ttoken.ticketLen,
-                                             ttoken.ticket);
        }
     }
 
-    if (scIndex == 1)
-       return PRBADARG;
-    if ((scIndex == 0) && (sc[0] == 0))
-       sc[0] = rxnull_NewClientSecurityObject();
-    if ((scIndex == 0) && (secLevel != 0))
+    if (sc == NULL) {
+       sc = rxnull_NewClientSecurityObject();
+        scIndex = RX_SECIDX_NULL;
+    }
+
+    if ((scIndex == RX_SECIDX_NULL) && (secLevel != 0))
        fprintf(stderr,
                "%s: Could not get afs tokens, running unauthenticated\n",
                whoami);
@@ -236,7 +317,7 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
     for (i = 0; i < info.numServers; i++)
        serverconns[i] =
            rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
-                            info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
+                            info.hostAddr[i].sin_port, PRSRV, sc,
                             scIndex);
 
     code = ubik_ClientInit(serverconns, &pruclient);
@@ -246,7 +327,7 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
     }
     lastLevel = scIndex;
 
-    code = rxs_Release(sc[scIndex]);
+    code = rxs_Release(sc);
     return code;
 }
 
@@ -267,7 +348,7 @@ pr_End(void)
 int
 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     stolower(name);
     if (*id) {
@@ -280,10 +361,10 @@ pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
 
 }
 
-int 
+int
 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 oid = 0;
     afs_int32 flags = 0;
 
@@ -308,7 +389,7 @@ pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *i
 int
 pr_Delete(char *name)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 id;
 
     stolower(name);
@@ -324,7 +405,7 @@ pr_Delete(char *name)
 int
 pr_DeleteByID(afs_int32 id)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     code = ubik_PR_Delete(pruclient, 0, id);
     return code;
@@ -333,7 +414,7 @@ pr_DeleteByID(afs_int32 id)
 int
 pr_AddToGroup(char *user, char *group)
 {
-    register afs_int32 code;
+    afs_int32 code;
     namelist lnames;
     idlist lids;
 
@@ -366,7 +447,7 @@ pr_AddToGroup(char *user, char *group)
 int
 pr_RemoveUserFromGroup(char *user, char *group)
 {
-    register afs_int32 code;
+    afs_int32 code;
     namelist lnames;
     idlist lids;
 
@@ -400,8 +481,8 @@ pr_RemoveUserFromGroup(char *user, char *group)
 int
 pr_NameToId(namelist *names, idlist *ids)
 {
-    register afs_int32 code;
-    register afs_int32 i;
+    afs_int32 code;
+    afs_int32 i;
 
     for (i = 0; i < names->namelist_len; i++)
        stolower(names->namelist_val[i]);
@@ -414,7 +495,7 @@ pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
 {
     namelist lnames;
     idlist lids;
-    register afs_int32 code;
+    afs_int32 code;
 
     lids.idlist_len = 0;
     lids.idlist_val = 0;
@@ -435,7 +516,7 @@ pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
 int
 pr_IdToName(idlist *ids, namelist *names)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     code = ubik_PR_IDToName(pruclient, 0, ids, names);
     return code;
@@ -446,7 +527,7 @@ pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
 {
     namelist lnames;
     idlist lids;
-    register afs_int32 code;
+    afs_int32 code;
 
     lids.idlist_len = 1;
     lids.idlist_val = malloc(sizeof(afs_int32));
@@ -469,7 +550,7 @@ pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
 int
 pr_GetCPS(afs_int32 id, prlist *CPS)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 over;
 
     over = 0;
@@ -486,9 +567,9 @@ pr_GetCPS(afs_int32 id, prlist *CPS)
 }
 
 int
-pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
+pr_GetCPS2(afs_int32 id, afs_uint32 host, prlist *CPS)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 over;
 
     over = 0;
@@ -505,9 +586,9 @@ pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
 }
 
 int
-pr_GetHostCPS(afs_int32 host, prlist *CPS)
+pr_GetHostCPS(afs_uint32 host, prlist *CPS)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 over;
 
     over = 0;
@@ -527,7 +608,7 @@ pr_GetHostCPS(afs_int32 host, prlist *CPS)
 int
 pr_ListMembers(char *group, namelist *lnames)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 gid;
 
     code = pr_SNameToId(group, &gid);
@@ -542,7 +623,7 @@ pr_ListMembers(char *group, namelist *lnames)
 int
 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
 {
-    register afs_int32 code;
+    afs_int32 code;
     prlist alist;
     idlist *lids;
 
@@ -571,7 +652,7 @@ pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
 int
 pr_IDListMembers(afs_int32 gid, namelist *lnames)
 {
-    register afs_int32 code;
+    afs_int32 code;
     prlist alist;
     idlist *lids;
     afs_int32 over;
@@ -596,9 +677,101 @@ pr_IDListMembers(afs_int32 gid, namelist *lnames)
 }
 
 int
+pr_IDListExpandedMembers(afs_int32 aid, namelist * lnames)
+{
+    afs_int32 code;
+    afs_int32 gid;
+    idlist lids;
+    prlist alist;
+    afs_int32 over;
+    struct idhash *members = NULL;
+    afs_int32 *stack = NULL;
+    afs_int32 maxstack = ID_STACK_SIZE;
+    int n = 0;                 /* number of ids stacked */
+    int i;
+    int firstpass = 1;
+
+    code = AllocateIdHash(&members);
+    if (code) {
+       return code;
+    }
+    stack = (afs_int32 *) malloc(sizeof(afs_int32) * maxstack);
+    if (!stack) {
+       code = ENOMEM;
+       goto done;
+    }
+
+    stack[n++] = aid;
+    while (n) {
+       gid = stack[--n];       /* pop next group id */
+       alist.prlist_len = 0;
+       alist.prlist_val = NULL;
+       if (firstpass || aid < 0) {
+           firstpass = 0;
+           code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
+       } else {
+           code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
+           if (code == RXGEN_OPCODE) {
+               alist.prlist_len = 0;
+               alist.prlist_val = NULL;
+               code = 0; /* server does not support supergroups. */
+           }
+       }
+       if (code)
+           goto done;
+       if (over) {
+           fprintf(stderr,
+                   "membership list for id %d exceeds display limit\n", gid);
+       }
+       for (i = 0; i < alist.prlist_len; i++) {
+           afs_int32 found;
+           afs_int32 id;
+
+           id = alist.prlist_val[i];
+           found = FindId(members, id);
+           if (found < 0) {
+               code = found;
+               xdr_free((xdrproc_t) xdr_prlist, &alist);
+               goto done;
+           }
+           if (found == 0 && id < 0) {
+               if (n == maxstack) {    /* need more stack space */
+                   afs_int32 *tmp;
+                   maxstack += n;
+                   tmp = realloc(stack, maxstack * sizeof(afs_int32));
+                   if (!tmp) {
+                       code = ENOMEM;
+                       xdr_free((xdrproc_t) xdr_prlist, &alist);
+                       goto done;
+                   }
+                   stack = tmp;
+               }
+               stack[n++] = id;        /* push group id */
+           }
+       }
+       xdr_free((xdrproc_t) xdr_prlist, &alist);
+    }
+
+    code = CreateIdList(members, &lids, (aid < 0 ? PRUSERS : PRGROUPS));
+    if (code) {
+       goto done;
+    }
+    code = pr_IdToName(&lids, lnames);
+    if (lids.idlist_len)
+       free(lids.idlist_val);
+
+  done:
+    if (stack)
+       free(stack);
+    if (members)
+       FreeIdHash(members);
+    return code;
+}
+
+int
 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
     return code;
@@ -628,7 +801,7 @@ int
 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
 {
     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
-    register afs_int32 code;
+    afs_int32 code;
     struct prcheckentry aentry;
 
     code = pr_SNameToId(name, id);
@@ -653,7 +826,7 @@ int
 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
 {
     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
-    register afs_int32 code;
+    afs_int32 code;
     struct prcheckentry aentry;
 
     code = pr_SIdToName(id, name);
@@ -677,7 +850,7 @@ pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
 int
 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 id;
     afs_int32 oid = 0;
 
@@ -703,7 +876,7 @@ pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
 int
 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
 {
-    register afs_int32 code;
+    afs_int32 code;
     namelist lnames;
     idlist lids;
 
@@ -734,7 +907,7 @@ pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
 int
 pr_ListMaxUserId(afs_int32 *mid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 gid;
     code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
     return code;
@@ -743,7 +916,7 @@ pr_ListMaxUserId(afs_int32 *mid)
 int
 pr_SetMaxUserId(afs_int32 mid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 flag = 0;
     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
     return code;
@@ -752,7 +925,7 @@ pr_SetMaxUserId(afs_int32 mid)
 int
 pr_ListMaxGroupId(afs_int32 *mid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 id;
     code = ubik_PR_ListMax(pruclient, 0, &id, mid);
     return code;
@@ -761,7 +934,7 @@ pr_ListMaxGroupId(afs_int32 *mid)
 int
 pr_SetMaxGroupId(afs_int32 mid)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 flag = 0;
 
     flag |= PRGRP;
@@ -772,10 +945,34 @@ pr_SetMaxGroupId(afs_int32 mid)
 afs_int32
 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     code =
        ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
                  nusers, 0, 0);
     return code;
 }
+
+int
+pr_ListSuperGroups(afs_int32 gid, namelist * lnames)
+{
+    afs_int32 code;
+    prlist alist;
+    idlist *lids;
+    afs_int32 over;
+
+    alist.prlist_len = 0;
+    alist.prlist_val = 0;
+    code = ubik_PR_ListSuperGroups(pruclient, 0, gid, &alist, &over);
+    if (code)
+       return code;
+    if (over) {
+       fprintf(stderr, "supergroup list for id %d exceeds display limit\n",
+               gid);
+    }
+    lids = (idlist *) & alist;
+    code = pr_IdToName(lids, lnames);
+
+    xdr_free((xdrproc_t) xdr_prlist, &alist);
+    return code;
+}