Use xdr_alloc and xdr_free within ptuser
[openafs.git] / src / ptserver / ptuser.c
index ebf6c47..c1384c2 100644 (file)
@@ -14,8 +14,6 @@
 #include <afs/param.h>
 #endif
 
-RCSID
-    ("$Header$");
 
 #if defined(UKERNEL)
 #include "afs/sysincludes.h"
@@ -24,13 +22,13 @@ RCSID
 #include "afs/stds.h"
 #include "rx/rx.h"
 #include "rx/xdr.h"
-#include "rx/rxkad.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>
@@ -41,19 +39,13 @@ RCSID
 #include <netinet/in.h>
 #endif
 #include <stdio.h>
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
 #include <rx/rx.h>
 #include <rx/xdr.h>
-#include <rx/rxkad.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"
@@ -78,7 +70,9 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
     afs_int32 scIndex;
     static struct afsconf_cell info;
     afs_int32 i;
+#if !defined(UKERNEL)
     char cellstr[64];
+#endif
     afs_int32 gottdir = 0;
     afs_int32 refresh = 0;
 
@@ -98,18 +92,18 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
        if (!tdir) {
            if (confDir && strcmp(confDir, ""))
                fprintf(stderr,
-                       "libprot: Could not open configuration directory: %s.\n",
-                       confDir);
+                       "%s: Could not open configuration directory: %s.\n",
+                       whoami, confDir);
             else
                fprintf(stderr,
-                       "libprot: No configuration directory specified.\n");
+                       "%s: No configuration directory specified.\n",
+                       whoami);
            return -1;
        }
         gottdir = 1;
 
         code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
         if (code) {
-           afsconf_Close(tdir);
             fprintf(stderr,
                      "libprot: Could not get local cell. [%d]\n", code);
             return code;
@@ -154,7 +148,6 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
 
        code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
        if (code) {
-           afsconf_Close(tdir);
            fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
                    cell, confDir, AFSDIR_CELLSERVDB_FILE);
            return code;
@@ -166,13 +159,11 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
      * which case we will get one (and re-read the key file).
      */
     if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
-       afsconf_Close(tdir);
        return 0;
     }
 
     code = rx_Init(0);
     if (code) {
-       afsconf_Close(tdir);
        fprintf(stderr, "libprot:  Could not initialize rx.\n");
        return code;
     }
@@ -184,16 +175,22 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
     /* 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) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
-       /* 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)
-           fprintf(stderr,
-                   "libprot: clientauthsecure returns %d %s"
-                   " (so trying noauth)\n", code, error_message(code));
-       if (code)
-           scIndex = 0;        /* use noauth */
+    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];
@@ -203,34 +200,37 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
        sname.instance[0] = 0;
        strcpy(sname.name, "afs");
        code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
-       if (code)
+       if (code) {
+           afs_com_err(whoami, code, "(getting token)");
+           if (secLevel > 1)
+               return code;
            scIndex = 0;
-       else {
+       } else {
            if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
                /* this is a kerberos ticket, set scIndex accordingly */
                scIndex = 2;
            else {
                fprintf(stderr,
-                       "libprot: funny kvno (%d) in ticket, proceeding\n",
-                       ttoken.kvno);
+                       "%s: funny kvno (%d) in ticket, proceeding\n",
+                       whoami, ttoken.kvno);
                scIndex = 2;
            }
            sc[2] =
-               rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
+               rxkad_NewClientSecurityObject((secLevel > 1) ? rxkad_crypt :
+                                             rxkad_clear, &ttoken.sessionKey,
                                              ttoken.kvno, ttoken.ticketLen,
                                              ttoken.ticket);
        }
     }
-    afsconf_Close(tdir);
-    tdir = NULL;
 
     if (scIndex == 1)
        return PRBADARG;
     if ((scIndex == 0) && (sc[0] == 0))
        sc[0] = rxnull_NewClientSecurityObject();
     if ((scIndex == 0) && (secLevel != 0))
-       com_err(whoami, code,
-               "Could not get afs tokens, running unauthenticated.");
+       fprintf(stderr,
+               "%s: Could not get afs tokens, running unauthenticated\n",
+               whoami);
 
     memset(serverconns, 0, sizeof(serverconns));       /* terminate list!!! */
     for (i = 0; i < info.numServers; i++)
@@ -241,7 +241,7 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
 
     code = ubik_ClientInit(serverconns, &pruclient);
     if (code) {
-       com_err(whoami, code, "ubik client init failed.");
+       afs_com_err(whoami, code, "ubik client init failed.");
        return code;
     }
     lastLevel = scIndex;
@@ -251,7 +251,7 @@ pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
 }
 
 int
-pr_End()
+pr_End(void)
 {
     int code = 0;
 
@@ -338,7 +338,7 @@ pr_AddToGroup(char *user, char *group)
     idlist lids;
 
     lnames.namelist_len = 2;
-    lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
+    lnames.namelist_val = (prname *) xdr_alloc(2 * PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
     lids.idlist_val = 0;
@@ -357,9 +357,9 @@ pr_AddToGroup(char *user, char *group)
                  lids.idlist_val[1]);
   done:
     if (lnames.namelist_val)
-       free(lnames.namelist_val);
+       xdr_free(lnames.namelist_val, 2 * PR_MAXNAMELEN);
     if (lids.idlist_val)
-       free(lids.idlist_val);
+       xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
     return code;
 }
 
@@ -371,7 +371,7 @@ pr_RemoveUserFromGroup(char *user, char *group)
     idlist lids;
 
     lnames.namelist_len = 2;
-    lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
+    lnames.namelist_val = (prname *) xdr_alloc(2 * PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
     lids.idlist_val = 0;
@@ -390,9 +390,9 @@ pr_RemoveUserFromGroup(char *user, char *group)
                  lids.idlist_val[1]);
   done:
     if (lnames.namelist_val)
-       free(lnames.namelist_val);
+       xdr_free(lnames.namelist_val, 2 * PR_MAXNAMELEN);
     if (lids.idlist_val)
-       free(lids.idlist_val);
+       xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
     return code;
 
 }
@@ -419,16 +419,16 @@ pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
     lids.idlist_len = 0;
     lids.idlist_val = 0;
     lnames.namelist_len = 1;
-    lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
+    lnames.namelist_val = (prname *) xdr_alloc(PR_MAXNAMELEN);
     stolower(name);
     strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
     code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
     if (lids.idlist_val) {
        *id = *lids.idlist_val;
-       free(lids.idlist_val);
+       xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
     }
     if (lnames.namelist_val)
-       free(lnames.namelist_val);
+       xdr_free(lnames.namelist_val, PR_MAXNAMELEN);
     return code;
 }
 
@@ -449,17 +449,17 @@ pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
     register afs_int32 code;
 
     lids.idlist_len = 1;
-    lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
+    lids.idlist_val = (afs_int32 *) xdr_alloc(sizeof(afs_int32));
     *lids.idlist_val = id;
     lnames.namelist_len = 0;
     lnames.namelist_val = 0;
     code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
     if (lnames.namelist_val) {
        strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
-       free(lnames.namelist_val);
+       xdr_free(lnames.namelist_val, PR_MAXNAMELEN);
     }
     if (lids.idlist_val)
-       free(lids.idlist_val);
+       xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
     return code;
 }
 
@@ -559,7 +559,7 @@ pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
     if (code)
        return code;
     if (alist.prlist_val)
-       free(alist.prlist_val);
+       xdr_free(alist.prlist_val, alist.prlist_len * sizeof(alist.prlist_val[0]));
     return PRSUCCESS;
 }
 
@@ -585,7 +585,7 @@ pr_IDListMembers(afs_int32 gid, namelist *lnames)
     if (code)
        return code;
     if (alist.prlist_val)
-       free(alist.prlist_val);
+       xdr_free(alist.prlist_val, alist.prlist_len * sizeof(alist.prlist_val[0]));
     return PRSUCCESS;
 }
 
@@ -631,7 +631,6 @@ pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
     if (*id == ANONYMOUSID)
        return PRNOENT;
     code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
-      //code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
     if (code)
        return code;
     /* this should be done in one RPC, but I'm lazy. */
@@ -688,7 +687,10 @@ pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
        if (oid == ANONYMOUSID)
            return PRNOENT;
     }
-    code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, newid);
+    if (newid)
+       code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
+    else
+       code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
     return code;
 }
 
@@ -702,7 +704,7 @@ pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
     stolower(uname);
     stolower(gname);
     lnames.namelist_len = 2;
-    lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
+    lnames.namelist_val = (prname *) xdr_alloc(2 * PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
     lids.idlist_val = 0;
@@ -710,18 +712,18 @@ pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
     code = pr_NameToId(&lnames, &lids);
     if (code) {
        if (lnames.namelist_val)
-           free(lnames.namelist_val);
+           xdr_free(lnames.namelist_val, 2 * PR_MAXNAMELEN);
        if (lids.idlist_val)
-           free(lids.idlist_val);
+           xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
        return code;
     }
     code =
        ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
                  lids.idlist_val[1], flag);
     if (lnames.namelist_val)
-       free(lnames.namelist_val);
+       xdr_free(lnames.namelist_val, 2 * PR_MAXNAMELEN);
     if (lids.idlist_val)
-       free(lids.idlist_val);
+       xdr_free(lids.idlist_val, lids.idlist_len * sizeof(lids.idlist_val[0]));
     return code;
 }