viced: Abstract out peer mapping code
[openafs.git] / src / viced / host.c
index 0102da4..2d80f43 100644 (file)
@@ -110,7 +110,7 @@ GetCEBlock(void)
     struct CEBlock *block;
     int i;
 
-    block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
+    block = malloc(sizeof(struct CEBlock));
     if (!block) {
        ViceLog(0, ("Failed malloc in GetCEBlock\n"));
        ShutDownAndCore(PANIC);
@@ -198,7 +198,7 @@ GetHTBlock(void)
        return;
     }
 
-    block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
+    block = malloc(sizeof(struct HTBlock));
     if (!block) {
        ViceLog(0, ("Failed malloc in GetHTBlock\n"));
        ShutDownAndCore(PANIC);
@@ -330,21 +330,34 @@ hpr_End(struct ubik_client *uclient)
     return code;
 }
 
+static_inline int
+getThreadClient(struct ubik_client **client)
+{
+    int code;
+
+    *client = pthread_getspecific(viced_uclient_key);
+    if (*client != NULL)
+       return 0;
+
+    code = hpr_Initialize(client);
+    if (code)
+       return code;
+
+    osi_Assert(pthread_setspecific(viced_uclient_key, *client) == 0);
+
+    return 0;
+}
+
 int
 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
 {
     afs_int32 code;
     afs_int32 over;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     over = 0;
     code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
@@ -365,16 +378,11 @@ hpr_NameToId(namelist *names, idlist *ids)
 {
     afs_int32 code;
     afs_int32 i;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     for (i = 0; i < names->namelist_len; i++)
         stolower(names->namelist_val[i]);
@@ -386,16 +394,11 @@ int
 hpr_IdToName(idlist *ids, namelist *names)
 {
     afs_int32 code;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     code = ubik_PR_IDToName(uclient, 0, ids, names);
     return code;
@@ -406,16 +409,11 @@ hpr_GetCPS(afs_int32 id, prlist *CPS)
 {
     afs_int32 code;
     afs_int32 over;
-    struct ubik_client *uclient =
-       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    struct ubik_client *uclient;
 
-    if (!uclient) {
-        code = hpr_Initialize(&uclient);
-       if (!code)
-           osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
-       else
-           return code;
-    }
+    code = getThreadClient(&uclient);
+    if (code)
+       return code;
 
     over = 0;
     code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
@@ -954,7 +952,7 @@ h_Enumerate(int (*proc) (struct host*, void *), void *param)
        H_UNLOCK;
        return;
     }
-    list = (struct host **)malloc(hostCount * sizeof(struct host *));
+    list = malloc(hostCount * sizeof(struct host *));
     if (!list) {
        ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
     }
@@ -999,7 +997,7 @@ h_Enumerate(int (*proc) (struct host*, void *), void *param)
        }
        H_UNLOCK;
     }
-    free((void *)list);
+    free(list);
 }      /* h_Enumerate */
 
 
@@ -1130,7 +1128,7 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
     }
 
     /* insert into beginning of list for this bucket */
-    chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
+    chain = malloc(sizeof(struct h_UuidHashChain));
     if (!chain) {
        ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
     }
@@ -1311,7 +1309,7 @@ createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct ho
     char hoststr[16];
 
     /* insert into beginning of list for this bucket */
-    chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
+    chain = malloc(sizeof(struct h_AddrHashChain));
     if (!chain) {
        ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
     }
@@ -1530,8 +1528,8 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
                  ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
                  ntohs(port)));
 
-    interface = (struct Interface *)
-       malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
+    interface = malloc(sizeof(struct Interface)
+                      + (sizeof(struct AddrPort) * number));
     if (!interface) {
        ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
     }
@@ -1724,7 +1722,7 @@ h_GetHost_r(struct rx_connection *tcon)
        H_LOCK;
        if ((code == RXGEN_OPCODE) ||
            ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           identP = malloc(sizeof(struct Identity));
            if (!identP) {
                ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
            }
@@ -1765,7 +1763,7 @@ h_GetHost_r(struct rx_connection *tcon)
            }
        } else if (code == 0) {
            interfValid = 1;
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           identP = malloc(sizeof(struct Identity));
            if (!identP) {
                ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
            }
@@ -1951,8 +1949,7 @@ h_GetHost_r(struct rx_connection *tcon)
            if ((code == RXGEN_OPCODE) ||
                ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
                if (!identP)
-                   identP =
-                       (struct Identity *)malloc(sizeof(struct Identity));
+                   identP = malloc(sizeof(struct Identity));
                else
                    pident = 1;
 
@@ -1969,8 +1966,7 @@ h_GetHost_r(struct rx_connection *tcon)
                code = 0;
            } else if (code == 0) {
                if (!identP)
-                   identP =
-                       (struct Identity *)malloc(sizeof(struct Identity));
+                   identP = malloc(sizeof(struct Identity));
                else
                    pident = 1;
 
@@ -2290,6 +2286,74 @@ format_vname(char *vname, int usize, const char *tname, const char *tinst,
     return 0;
 }
 
+static int
+getPeerDetails(struct rx_connection *conn,
+              afs_int32 *viceid, afs_int32 *expTime, int authClass)
+{
+    int code;
+#if (64-MAXKTCNAMELEN)
+    ticket name length != 64
+#endif
+    char tname[64];
+    char tinst[64];
+    char tcell[MAXKTCREALMLEN];
+    char uname[PR_MAXNAMELEN];
+
+    *viceid = AnonymousID;
+    *expTime = 0x7fffffff;
+
+    ViceLog(5,
+           ("FindClient: authenticating connection: authClass=%d\n",
+            authClass));
+    if (authClass == 1) {
+       /* A bcrypt tickets, no longer supported */
+       ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
+       return 0;
+    }
+
+    if (authClass == 2) {
+       /* an rxkad ticket */
+       afs_int32 kvno;
+       afs_int32 islocal;
+
+       /* kerberos ticket */
+       code = rxkad_GetServerInfo(conn, /*level */ 0, (afs_uint32 *)expTime,
+                                  tname, tinst, tcell, &kvno);
+       if (code) {
+           ViceLog(1, ("Failed to get rxkad ticket info\n"));
+           return 0;
+       }
+
+       ViceLog(5,
+               ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
+                tname, tinst, tcell, *expTime, kvno));
+       code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
+
+       if (code) {
+           ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
+           return 0;
+       }
+
+       code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
+       if (code) {
+           ViceLog(0, ("FindClient: uname truncated."));
+           return 0;
+       }
+
+       /* translate the name to a vice id */
+       code = MapName_r(uname, viceid);
+       if (code) {
+           ViceLog(1, ("failed to map name=%s -> code=%d\n", uname,
+                       code));
+           return code; /* Actually flag this is a failure */
+       }
+
+       return 0;
+    }
+
+    return 0;
+}
+
 /*
  * Called by the server main loop.  Returns a h_Held client, which must be
  * released later the main loop.  Allocates a client if the matching one
@@ -2310,13 +2374,7 @@ h_FindClient_r(struct rx_connection *tcon)
     afs_int32 expTime;
     afs_int32 code;
     int authClass;
-#if (64-MAXKTCNAMELEN)
-    ticket name length != 64
-#endif
-    char tname[64];
-    char tinst[64];
-    char uname[PR_MAXNAMELEN];
-    char tcell[MAXKTCREALMLEN];
+
     int fail = 0;
     int created = 0;
 
@@ -2344,61 +2402,11 @@ h_FindClient_r(struct rx_connection *tcon)
        client = NULL;
     }
 
-    authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
-    ViceLog(5,
-           ("FindClient: authenticating connection: authClass=%d\n",
-            authClass));
-    if (authClass == 1) {
-       /* A bcrypt tickets, no longer supported */
-       ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
-       viceid = AnonymousID;
-       expTime = 0x7fffffff;
-    } else if (authClass == 2) {
-       afs_int32 kvno;
-       afs_int32 islocal;
+    authClass = rx_SecurityClassOf(tcon);
 
-       /* kerberos ticket */
-       code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
-                                  tname, tinst, tcell, &kvno);
-       if (code) {
-           ViceLog(1, ("Failed to get rxkad ticket info\n"));
-           viceid = AnonymousID;
-           expTime = 0x7fffffff;
-       } else {
-           ViceLog(5,
-                   ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
-                    tname, tinst, tcell, expTime, kvno));
-           code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
-           if (code) {
-               ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
-               viceid = AnonymousID;
-               expTime = 0x7fffffff;
-           }
-           if (!code) {
-               code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
-               if (code) {
-                   ViceLog(0, ("FindClient: uname truncated."));
-                   viceid = AnonymousID;
-                   expTime = 0x7fffffff;
-               }
-           }
-           if (!code) {
-               /* translate the name to a vice id */
-               code = MapName_r(uname, &viceid);
-               if (code) {
-                   ViceLog(1,
-                           ("failed to map name=%s -> code=%d\n", uname,
-                            code));
-                   fail = 1;
-                   viceid = AnonymousID;
-                   expTime = 0x7fffffff;
-               }
-           }
-       }
-    } else {
-       viceid = AnonymousID;   /* unknown security class */
-       expTime = 0x7fffffff;
-    }
+    code = getPeerDetails(tcon, &viceid, &expTime, authClass);
+    if (code)
+       fail = 1;
 
     if (!client) { /* loop */
        host = h_GetHost_r(tcon);       /* Returns with incremented refCount  */
@@ -2678,7 +2686,7 @@ h_UserName(struct client *client)
     idlist lids;
 
     lids.idlist_len = 1;
-    lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
+    lids.idlist_val = malloc(1 * sizeof(afs_int32));
     if (!lids.idlist_val) {
        ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
     }
@@ -3260,7 +3268,7 @@ h_stateSaveHost(struct host * host, void* rock)
     if (host->interface) {
        if_len = sizeof(struct Interface) +
            ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
-       ifp = (struct Interface *) malloc(if_len);
+       ifp = malloc(if_len);
        osi_Assert(ifp != NULL);
        memcpy(ifp, host->interface, if_len);
        hdr.interfaces = host->interface->numberOfInterfaces;
@@ -3271,7 +3279,7 @@ h_stateSaveHost(struct host * host, void* rock)
     if (host->hcps.prlist_val) {
        hdr.hcps = host->hcps.prlist_len;
        hcps_len = hdr.hcps * sizeof(afs_int32);
-       hcps = (afs_int32 *) malloc(hcps_len);
+       hcps = malloc(hcps_len);
        osi_Assert(hcps != NULL);
        memcpy(hcps, host->hcps.prlist_val, hcps_len);
        iov[iovcnt].iov_base = (char *) hcps;
@@ -3343,7 +3351,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
     if (hdr.interfaces) {
        ifp_len = sizeof(struct Interface) +
            ((hdr.interfaces-1) * sizeof(struct AddrPort));
-       ifp = (struct Interface *) malloc(ifp_len);
+       ifp = malloc(ifp_len);
        osi_Assert(ifp != NULL);
        iov[iovcnt].iov_base = (char *) ifp;
        iov[iovcnt].iov_len = ifp_len;
@@ -3351,7 +3359,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
     }
     if (hdr.hcps) {
        hcps_len = hdr.hcps * sizeof(afs_int32);
-       hcps = (afs_int32 *) malloc(hcps_len);
+       hcps = malloc(hcps_len);
        osi_Assert(hcps != NULL);
        iov[iovcnt].iov_base = (char *) hcps;
        iov[iovcnt].iov_len = hcps_len;
@@ -3372,7 +3380,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
 
     if (!hdr.hcps && hdsk.hcps_valid) {
        /* valid, zero-length host cps ; does this ever happen? */
-       hcps = (afs_int32 *) malloc(sizeof(afs_int32));
+       hcps = malloc(sizeof(afs_int32));
        osi_Assert(hcps != NULL);
     }
 
@@ -4066,16 +4074,15 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
      * Allocate and initialize an interface structure for this host.
      */
     if (found) {
-       interface = (struct Interface *)
-           malloc(sizeof(struct Interface) +
-                  (sizeof(struct AddrPort) * (count - 1)));
+       interface = malloc(sizeof(struct Interface) +
+                          (sizeof(struct AddrPort) * (count - 1)));
        if (!interface) {
            ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
        }
        interface->numberOfInterfaces = count;
     } else {
-       interface = (struct Interface *)
-           malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
+       interface = malloc(sizeof(struct Interface) +
+                          (sizeof(struct AddrPort) * count));
        if (!interface) {
            ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
        }