avoid writing loopback addresses into CellServDB
[openafs.git] / src / auth / writeconfig.c
index 3372d27..715d8a8 100644 (file)
@@ -11,6 +11,7 @@
 #include <afs/param.h>
 
 #include <roken.h>
+#include <afs/opr.h>
 
 #include <afs/pthread_glock.h>
 #include <afs/afsutil.h>
@@ -33,14 +34,30 @@ VerifyEntries(struct afsconf_cell *aci)
        if (aci->hostAddr[i].sin_addr.s_addr == 0) {
            /* no address spec'd */
            if (*(aci->hostName[i]) != 0) {
-               th = gethostbyname(aci->hostName[i]);
-               if (!th) {
-                   printf("Host %s not found in host database...\n",
-                          aci->hostName[i]);
+               int code;
+               struct addrinfo hints;
+               struct addrinfo *result;
+               struct addrinfo *rp;
+
+               memset(&hints, 0, sizeof(struct addrinfo));
+               hints.ai_family = AF_INET;
+               hints.ai_socktype = SOCK_DGRAM;
+
+               code = getaddrinfo(aci->hostName[i], NULL, &hints, &result);
+               if (code) {
+                   return AFSCONF_FAILURE;
+               }
+               for (rp = result; rp != NULL; rp = rp->ai_next) {
+                   struct sockaddr_in *sa = (struct sockaddr_in *)rp->ai_addr;
+                   if (!rx_IsLoopbackAddr(ntohl(sa->sin_addr.s_addr))) {
+                       aci->hostAddr[i].sin_addr.s_addr = sa->sin_addr.s_addr;
+                       break;
+                   }
+               }
+               freeaddrinfo(result);
+               if (aci->hostAddr[i].sin_addr.s_addr == 0) {
                    return AFSCONF_FAILURE;
                }
-               memcpy(&aci->hostAddr[i].sin_addr, th->h_addr,
-                      sizeof(afs_int32));
            }
            /* otherwise we're deleting this entry */
        } else {
@@ -53,7 +70,12 @@ VerifyEntries(struct afsconf_cell *aci)
            if (!th) {
                strcpy(aci->hostName[i], "UNKNOWNHOST");
            } else {
-               strcpy(aci->hostName[i], th->h_name);
+               if (strlcpy(aci->hostName[i],
+                           th->h_name,
+                           sizeof(aci->hostName[i]))
+                       >= sizeof(aci->hostName[i])) {
+                  strcpy(aci->hostName[i], "UNKNOWNHOST");
+               }
            }
        }
     }
@@ -87,7 +109,7 @@ afsconf_SetExtendedCellInfo(struct afsconf_dir *adir,
 
     LOCK_GLOBAL_MUTEX;
     /* write ThisCell file */
-    strcompose(tbuffer, 1024, apath, "/", AFSDIR_THISCELL_FILE, NULL);
+    strcompose(tbuffer, 1024, apath, "/", AFSDIR_THISCELL_FILE, (char *)NULL);
 
     fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
     if (fd < 0) {
@@ -114,7 +136,7 @@ afsconf_SetExtendedCellInfo(struct afsconf_dir *adir,
     }
 
     /* write CellServDB */
-    strcompose(tbuffer, 1024, apath, "/", AFSDIR_CELLSERVDB_FILE, NULL);
+    strcompose(tbuffer, 1024, apath, "/", AFSDIR_CELLSERVDB_FILE, (char *)NULL);
     tf = fopen(tbuffer, "w");
     if (!tf) {
        UNLOCK_GLOBAL_MUTEX;