ktc-cleanup-20070208
[openafs.git] / src / auth / ktc.c
index d1c22b5..b1af7ea 100644 (file)
@@ -59,6 +59,9 @@ RCSID
 #include <afs/vice.h>
 #ifdef AFS_AIX_ENV
 #include <sys/lockf.h>
+#ifdef AFS_AIX51_ENV
+#include <sys/cred.h>
+#endif
 #endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -71,7 +74,7 @@ RCSID
 
 /* For malloc() */
 #include <stdlib.h>
-
+#include "ktc.h"
 
 #ifdef notdef
 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
@@ -132,7 +135,6 @@ int afs_tf_init(), afs_tf_get_pname(), afs_tf_get_pinst(), afs_tf_get_cred();
 int afs_tf_save_cred(), afs_tf_close(), afs_tf_create();
 int afs_tf_dest_tkt();
 static void ktc_LocalCell();
-char *ktc_tkt_string();
 #endif /* AFS_KERBEROS_ENV */
 
 #ifdef AFS_DUX40_ENV
@@ -415,18 +417,17 @@ OldSetToken(aserver, atoken, aclient, flags)
     return 0;
 }
 
-
-ktc_SetToken(aserver, atoken, aclient, flags)
-     struct ktc_principal *aserver;
-     struct ktc_principal *aclient;
-     struct ktc_token *atoken;
-     afs_int32 flags;
+int
+ktc_SetToken(struct ktc_principal *aserver,
+    struct ktc_token *atoken,
+    struct ktc_principal *aclient,
+    afs_int32 flags)
 {
     int ncode, ocode;
 
-    LOCK_GLOBAL_MUTEX
+    LOCK_GLOBAL_MUTEX;
 #ifdef AFS_KERBEROS_ENV
-       if (!lcell[0])
+    if (!lcell[0])
        ktc_LocalCell();
 
     if (                       /*!strcmp(aclient->cell, lcell) && this would only store local creds */
@@ -453,7 +454,8 @@ ktc_SetToken(aserver, atoken, aclient, flags)
        }
        afs_tf_close();
 #ifdef NO_AFS_CLIENT
-       UNLOCK_GLOBAL_MUTEX return ncode;
+       UNLOCK_GLOBAL_MUTEX;
+       return ncode;
 #endif /* NO_AFS_CLIENT */
     }
 #endif
@@ -466,7 +468,8 @@ ktc_SetToken(aserver, atoken, aclient, flags)
     } else
        ocode = 0;
     if (ncode && ocode) {
-       UNLOCK_GLOBAL_MUTEX if (ocode == -1)
+       UNLOCK_GLOBAL_MUTEX;
+       if (ocode == -1)
            ocode = errno;
        else if (ocode == KTC_PIOCTLFAIL)
            ocode = errno;
@@ -479,7 +482,8 @@ ktc_SetToken(aserver, atoken, aclient, flags)
        return KTC_PIOCTLFAIL;
     }
 #endif /* NO_AFS_CLIENT */
-    UNLOCK_GLOBAL_MUTEX return 0;
+    UNLOCK_GLOBAL_MUTEX;
+    return 0;
 }
 
 /* get token, given server we need and token buffer.  aclient will eventually
@@ -502,9 +506,9 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
     int tktLen;                        /* server ticket length */
     char found = 0;
 
-    LOCK_GLOBAL_MUTEX
+    LOCK_GLOBAL_MUTEX;
 #ifndef NO_AFS_CLIENT
-       TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
+    TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
 #endif /* NO_AFS_CLIENT */
 
 #ifdef AFS_KERBEROS_ENV
@@ -527,7 +531,8 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
                       min(atokenLen, sizeof(struct ktc_token)));
                if (aclient)
                    *aclient = local_tokens[i].client;
-               UNLOCK_GLOBAL_MUTEX return 0;
+               UNLOCK_GLOBAL_MUTEX;
+               return 0;
            }
 #ifdef AFS_KERBEROS_ENV
        if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
@@ -557,13 +562,15 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
                           min(atokenLen, sizeof(struct ktc_token)));
 
                    afs_tf_close();
-                   UNLOCK_GLOBAL_MUTEX return 0;
+                   UNLOCK_GLOBAL_MUTEX;
+                   return 0;
                }
            }
        }
        afs_tf_close();
 #endif
-       UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+       UNLOCK_GLOBAL_MUTEX;
+       return KTC_NOENT;
     }
 #ifndef NO_AFS_CLIENT
     for (index = 0; index < 200; index++) {    /* sanity check in case pioctl fails */
@@ -577,7 +584,8 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
        if (code) {
            /* failed to retrieve specified token */
            if (code < 0 && errno == EDOM) {
-               UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+               UNLOCK_GLOBAL_MUTEX;
+               return KTC_NOENT;
            }
        } else {
            /* token retrieved; parse buffer */
@@ -595,7 +603,8 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
            /* get size of clear token and verify */
            memcpy(&temp, tp, sizeof(afs_int32));
            if (temp != sizeof(struct ClearToken)) {
-               UNLOCK_GLOBAL_MUTEX return KTC_ERROR;
+               UNLOCK_GLOBAL_MUTEX;
+               return KTC_ERROR;
            }
            tp += sizeof(afs_int32);
 
@@ -618,7 +627,8 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
                maxLen =
                    atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
                if (maxLen < tktLen) {
-                   UNLOCK_GLOBAL_MUTEX return KTC_TOOBIG;
+                   UNLOCK_GLOBAL_MUTEX;
+                   return KTC_TOOBIG;
                }
 
                /* set return values */
@@ -645,13 +655,15 @@ ktc_GetToken(aserver, atoken, atokenLen, aclient)
                        sprintf(aclient->name, "Unix UID %d", ct.ViceId);
                    }
                }
-               UNLOCK_GLOBAL_MUTEX return 0;
+               UNLOCK_GLOBAL_MUTEX;
+               return 0;
            }
        }
     }
 #endif /* NO_AFS_CLIENT */
 
-    UNLOCK_GLOBAL_MUTEX if ((code < 0) && (errno == EINVAL))
+    UNLOCK_GLOBAL_MUTEX;
+    if ((code < 0) && (errno == EINVAL))
        return KTC_NOPIOCTL;
     return KTC_PIOCTLFAIL;     /* probable cause */
 }
@@ -666,10 +678,12 @@ ktc_ForgetToken(aserver)
 {
     int rc;
 
-    LOCK_GLOBAL_MUTEX TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
+    LOCK_GLOBAL_MUTEX;
+    TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
 
     rc = ktc_ForgetAllTokens();        /* bogus, but better */
-    UNLOCK_GLOBAL_MUTEX return rc;
+    UNLOCK_GLOBAL_MUTEX;
+    return rc;
 }
 #endif /* NO_AFS_CLIENT */
 
@@ -677,9 +691,10 @@ ktc_ForgetToken(aserver)
  * next rock in (*aindex).  (*aserver) is set to the relevant ticket on
  * success.  */
 
-ktc_ListTokens(aprevIndex, aindex, aserver)
-     int aprevIndex, *aindex;
-     struct ktc_principal *aserver;
+int
+ktc_ListTokens(int aprevIndex,
+    int *aindex,
+    struct ktc_principal *aserver)
 {
     struct ViceIoctl iob;
     char tbuffer[MAXPIOCTLTOKENLEN];
@@ -689,9 +704,9 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
 
     memset(tbuffer, 0, sizeof(tbuffer));
 
-    LOCK_GLOBAL_MUTEX
+    LOCK_GLOBAL_MUTEX;
 #ifndef NO_AFS_CLIENT
-       TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
+    TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
 #endif /* NO_AFS_CLIENT */
 
     index = aprevIndex;
@@ -708,20 +723,23 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
        if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
            || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
            afs_tf_close();
-           UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+           UNLOCK_GLOBAL_MUTEX;
+           return KTC_NOENT;
        }
 
        for (i = 214; i < index; i++) {
            if (afs_tf_get_cred(&cprincipal, &ctoken)) {
                afs_tf_close();
-               UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+               UNLOCK_GLOBAL_MUTEX;
+               return KTC_NOENT;
            }
        }
 
       again:
        if (afs_tf_get_cred(&cprincipal, &ctoken)) {
            afs_tf_close();
-           UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+           UNLOCK_GLOBAL_MUTEX;
+           return KTC_NOENT;
        }
        index++;
 
@@ -743,7 +761,8 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
        *aserver = cprincipal;
        *aindex = index;
        afs_tf_close();
-       UNLOCK_GLOBAL_MUTEX return 0;
+       UNLOCK_GLOBAL_MUTEX;
+       return 0;
     }
 #endif
 
@@ -753,15 +772,16 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
            if (local_tokens[index - 123].valid) {
                *aserver = local_tokens[index - 123].server;
                *aindex = index + 1;
-               UNLOCK_GLOBAL_MUTEX return 0;
+               UNLOCK_GLOBAL_MUTEX;
+               return 0;
            }
            index++;
        }
-       UNLOCK_GLOBAL_MUTEX
+       UNLOCK_GLOBAL_MUTEX;
 #ifdef AFS_KERBEROS_ENV
-           return ktc_ListTokens(214, aindex, aserver);
+       return ktc_ListTokens(214, aindex, aserver);
 #else
-           return KTC_NOENT;
+       return KTC_NOENT;
 #endif
     }
 
@@ -776,9 +796,11 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
            if (index < 123) {
                int rc;
                rc = ktc_ListTokens(123, aindex, aserver);
-               UNLOCK_GLOBAL_MUTEX return rc;
+               UNLOCK_GLOBAL_MUTEX;
+               return rc;
            } else {
-               UNLOCK_GLOBAL_MUTEX return KTC_NOENT;
+               UNLOCK_GLOBAL_MUTEX;
+               return KTC_NOENT;
            }
        }
        if (code == 0)
@@ -787,7 +809,8 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
        index++;
     }
     if (code < 0) {
-       UNLOCK_GLOBAL_MUTEX if (errno == EINVAL)
+       UNLOCK_GLOBAL_MUTEX;
+       if (errno == EINVAL)
            return KTC_NOPIOCTL;
        return KTC_PIOCTLFAIL;
     }
@@ -803,7 +826,8 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
     tp += temp;                        /* skip ticket for now */
     memcpy(&temp, tp, sizeof(afs_int32));      /* get size of clear token */
     if (temp != sizeof(struct ClearToken)) {
-       UNLOCK_GLOBAL_MUTEX return KTC_ERROR;
+       UNLOCK_GLOBAL_MUTEX;
+       return KTC_ERROR;
     }
     tp += sizeof(afs_int32);   /* skip length */
     tp += temp;                        /* skip clear token itself */
@@ -813,13 +837,14 @@ ktc_ListTokens(aprevIndex, aindex, aserver)
     aserver->instance[0] = 0;
     strcpy(aserver->name, "afs");
 #endif /* NO_AFS_CLIENT */
-    UNLOCK_GLOBAL_MUTEX return 0;
+    UNLOCK_GLOBAL_MUTEX;
+    return 0;
 }
 
 /* discard all tokens from this user's cache */
 
 static int
-NewForgetAll()
+NewForgetAll(void)
 {
 #ifndef NO_AFS_CLIENT
     TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
@@ -828,7 +853,7 @@ NewForgetAll()
 }
 
 static int
-OldForgetAll()
+OldForgetAll(void)
 {
     struct ViceIoctl iob;
     register afs_int32 code;
@@ -850,13 +875,13 @@ OldForgetAll()
 }
 
 int
-ktc_ForgetAllTokens()
+ktc_ForgetAllTokens(void)
 {
     int ncode, ocode;
 
-    LOCK_GLOBAL_MUTEX
+    LOCK_GLOBAL_MUTEX;
 #ifdef AFS_KERBEROS_ENV
-    (void) afs_tf_dest_tkt();
+    (void)afs_tf_dest_tkt();
 #endif
 
     ncode = NewForgetAll();
@@ -866,11 +891,13 @@ ktc_ForgetAllTokens()
            ocode = errno;
        else if (ocode == KTC_PIOCTLFAIL)
            ocode = errno;
-       UNLOCK_GLOBAL_MUTEX if (ocode == EINVAL)
+       UNLOCK_GLOBAL_MUTEX;
+       if (ocode == EINVAL)
            return KTC_NOPIOCTL;
        return KTC_PIOCTLFAIL;
     }
-    UNLOCK_GLOBAL_MUTEX return 0;
+    UNLOCK_GLOBAL_MUTEX;
+    return 0;
 }
 
 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
@@ -879,14 +906,15 @@ ktc_ForgetAllTokens()
 ktc_OldPioctl()
 {
     int rc;
-    LOCK_GLOBAL_MUTEX
+    LOCK_GLOBAL_MUTEX;
 #ifdef KERNEL_KTC_COMPAT
-       CHECK_KERNEL;
+    CHECK_KERNEL;
     rc = (kernelKTC != 1);     /* old style interface */
 #else
-       rc = 1;
+    rc = 1;
 #endif
-    UNLOCK_GLOBAL_MUTEX return rc;
+    UNLOCK_GLOBAL_MUTEX;
+    return rc;
 }
 
 
@@ -1411,11 +1439,18 @@ char *getenv();
 static char krb_ticket_string[4096] = "";
 
 char *
-ktc_tkt_string()
+ktc_tkt_string(void)
+{
+    return ktc_tkt_string_uid(getuid());
+}
+
+char *
+ktc_tkt_string_uid(afs_uint32 uid)
 {
     char *env;
 
-    LOCK_GLOBAL_MUTEX if (!*krb_ticket_string) {
+    LOCK_GLOBAL_MUTEX;
+    if (!*krb_ticket_string) {
        if (env = getenv("KRBTKFILE")) {
            (void)strncpy(krb_ticket_string, env,
                          sizeof(krb_ticket_string) - 1);
@@ -1423,10 +1458,11 @@ ktc_tkt_string()
        } else {
            /* 32 bits of signed integer will always fit in 11 characters
             * (including the sign), so no need to worry about overflow */
-           (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, getuid());
+           (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
        }
     }
-    UNLOCK_GLOBAL_MUTEX return krb_ticket_string;
+    UNLOCK_GLOBAL_MUTEX;
+    return krb_ticket_string;
 }
 
 /*
@@ -1441,14 +1477,14 @@ ktc_tkt_string()
  */
 
 void
-ktc_set_tkt_string(val)
-     char *val;
+ktc_set_tkt_string(char * val)
 {
 
-    LOCK_GLOBAL_MUTEX(void) strncpy(krb_ticket_string, val,
-                                   sizeof(krb_ticket_string) - 1);
+    LOCK_GLOBAL_MUTEX;
+    (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
     krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
-    UNLOCK_GLOBAL_MUTEX return;
+    UNLOCK_GLOBAL_MUTEX;
+    return;
 }
 
 /*
@@ -1579,6 +1615,13 @@ afs_tf_dest_tkt()
 static afs_uint32
 curpag()
 {
+#if defined(AFS_AIX51_ENV)
+    afs_int32 pag;
+
+    if (get_pag(PAG_AFS, &pag) < 0 || pag == 0)
+        pag = NOPAG;
+    return pag;
+#else
     gid_t groups[NGROUPS_MAX];
     afs_uint32 g0, g1;
     afs_uint32 h, l, ret;
@@ -1602,6 +1645,7 @@ curpag()
            return -1;
     }
     return -1;
+#endif
 }
 
 
@@ -1615,7 +1659,8 @@ ktc_newpag()
     int numenv;
     char **newenv, **senv, **denv;
 
-    LOCK_GLOBAL_MUTEX if (stat("/ticket", &sbuf) == -1) {
+    LOCK_GLOBAL_MUTEX;
+    if (stat("/ticket", &sbuf) == -1) {
        prefix = "/tmp/tkt";
     }
 
@@ -1641,7 +1686,8 @@ ktc_newpag()
     strcat(*denv, fname);
     *++denv = 0;
     environ = newenv;
-UNLOCK_GLOBAL_MUTEX}
+    UNLOCK_GLOBAL_MUTEX;
+}
 
 /*
  * BLETCH!  We have to invoke the entire afsconf package just to