more-anonymous-pointer-cleanup-20071031
[openafs.git] / src / auth / cellconfig.c
index a88dfd4..bfef672 100644 (file)
@@ -47,13 +47,7 @@ RCSID
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -85,7 +79,7 @@ static struct afsconf_servPair serviceTable[] = {
 
 /* Prototypes */
 static afs_int32 afsconf_FindService(register const char *aname);
-static int TrimLine(char *abuffer);
+static int TrimLine(char *abuffer, int abufsize);
 #ifdef AFS_NT40_ENV
 static int IsClientConfigDirectory(const char *path);
 static int GetCellNT(struct afsconf_dir *adir);
@@ -249,7 +243,7 @@ afsconf_FindService(register const char *aname)
 }
 
 static int
-TrimLine(char *abuffer)
+TrimLine(char *abuffer, int abufsize)
 {
     char tbuffer[256];
     register char *tp;
@@ -261,8 +255,8 @@ TrimLine(char *abuffer)
            break;
        tp++;
     }
-    strcpy(tbuffer, tp);
-    strcpy(abuffer, tbuffer);
+    strlcpy(tbuffer, tp, sizeof tbuffer);
+    strlcpy(abuffer, tbuffer, abufsize);
     return 0;
 }
 
@@ -406,8 +400,7 @@ afsconf_Open(register const char *adir)
     /* zero structure and fill in name; rest is done by internal routine */
     tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
     memset(tdir, 0, sizeof(struct afsconf_dir));
-    tdir->name = (char *)malloc(strlen(adir) + 1);
-    strcpy(tdir->name, adir);
+    tdir->name = strdup(adir);
 
     code = afsconf_OpenInternal(tdir, 0, 0);
     if (code) {
@@ -459,8 +452,7 @@ afsconf_Open(register const char *adir)
            }
            afsconf_path = afs_confdir;
        }
-       tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
-       strcpy(tdir->name, afsconf_path);
+       tdir->name = strdup(afsconf_path);
        code = afsconf_OpenInternal(tdir, 0, 0);
        if (code) {
            free(tdir->name);
@@ -476,29 +468,32 @@ afsconf_Open(register const char *adir)
 static int
 GetCellUnix(struct afsconf_dir *adir)
 {
-    int rc;
+    char *rc;
     char tbuffer[256];
-    int fd;
+    char *start, *p;
+    afsconf_FILE *fp;
     
     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
-    fd = open(tbuffer, O_RDONLY, 0);
-    if (fd < 0) {
-        return -1;
-    } else {
-       int sz;
-
-       memset(tbuffer, 0, 256);
-        sz = read(fd, tbuffer, 255);
-        close(fd);
-        if (sz > 0) {
-           char *p = strchr(tbuffer, '\n');
-           if (p)
-               *p = '\0';
-
-            adir->cellName = (char *)malloc(sz + 1);
-            strncpy(adir->cellName, tbuffer, sz);
-        }
+    fp = fopen(tbuffer, "r");
+    if (fp == 0) {
+       return -1;
     }
+    rc = fgets(tbuffer, 256, fp);
+    fclose(fp);
+    if (rc == NULL)
+        return -1;
+
+    start = tbuffer;
+    while (*start != '\0' && isspace(*start))
+        start++;
+    p = start;
+    while (*p != '\0' && !isspace(*p))
+        p++;
+    *p = '\0';
+    if (*start == '\0')
+        return -1;
+
+    adir->cellName = strdup(start);
     return 0;
 }
 
@@ -531,7 +526,7 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
     char tbuffer[256], tbuf1[256];
     struct stat tstat;
 
-    /* figure out the cell name */
+    /* figure out the local cell name */
 #ifdef AFS_NT40_ENV
     i = GetCellNT(adir);
 #else
@@ -584,7 +579,7 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
        adir->timeRead = 0;
     }
 
-    strcpy(tbuf1, tbuffer);
+    strlcpy(tbuf1, tbuffer, sizeof tbuf1);
     tf = fopen(tbuffer, "r");
     if (!tf) {
        return -1;
@@ -593,7 +588,7 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
        tp = fgets(tbuffer, sizeof(tbuffer), tf);
        if (!tp)
            break;
-       TrimLine(tbuffer);      /* remove white space */
+       TrimLine(tbuffer, sizeof tbuffer);      /* remove white space */
        if (tbuffer[0] == 0 || tbuffer[0] == '\n')
            continue;           /* empty line */
        if (tbuffer[0] == '>') {
@@ -616,11 +611,8 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
                free(curEntry);
                return -1;
            }
-           if (linkedcell[0] != '\0') {
-               curEntry->cellInfo.linkedCell =
-                   (char *)malloc(strlen(linkedcell) + 1);
-               strcpy(curEntry->cellInfo.linkedCell, linkedcell);
-           }
+           if (linkedcell[0] != '\0')
+               curEntry->cellInfo.linkedCell = strdup(linkedcell);
        } else {
            /* new host in the current cell */
            if (!curEntry) {
@@ -674,7 +666,7 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
        tp = fgets(tbuffer, sizeof(tbuffer), tf);
        if (!tp)
            break;
-       TrimLine(tbuffer);      /* remove white space */
+       TrimLine(tbuffer, sizeof tbuffer);      /* remove white space */
 
        if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
            continue;           /* empty line */
@@ -699,8 +691,8 @@ afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
        curAlias = malloc(sizeof(*curAlias));
        memset(curAlias, 0, sizeof(*curAlias));
 
-       strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
-       strcpy(curAlias->aliasInfo.realName, tbuffer);
+       strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
+       strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
 
        curAlias->next = adir->alias_entries;
        adir->alias_entries = curAlias;
@@ -778,8 +770,8 @@ ParseCellLine(register char *aline, register char *aname,
 /* call aproc(entry, arock, adir) for all cells.  Proc must return 0, or we'll stop early and return the code it returns */
 int
 afsconf_CellApply(struct afsconf_dir *adir,
-                 int (*aproc) (struct afsconf_cell * cell, char *arock,
-                               struct afsconf_dir * dir), char *arock)
+                 int (*aproc) (struct afsconf_cell * cell, void *arock,
+                               struct afsconf_dir * dir), void *arock)
 {
     register struct afsconf_entry *tde;
     register afs_int32 code;
@@ -801,8 +793,8 @@ afsconf_CellApply(struct afsconf_dir *adir,
 int
 afsconf_CellAliasApply(struct afsconf_dir *adir,
                       int (*aproc) (struct afsconf_cellalias * alias,
-                                    char *arock, struct afsconf_dir * dir),
-                      char *arock)
+                                    void *arock, struct afsconf_dir * dir),
+                      void *arock)
 {
     register struct afsconf_aliasentry *tde;
     register afs_int32 code;
@@ -857,12 +849,14 @@ afsconf_GetAfsdbInfo(char *acellName, char *aservice,
     char host[256];
     int server_num = 0;
     int minttl = 0;
+    int try_init = 0;
 
     /* The resolver isn't always MT-safe.. Perhaps this ought to be
      * replaced with a more fine-grained lock just for the resolver
      * operations.
      */
 
+ retryafsdb:
     if ( ! strchr(acellName,'.') ) {
        cellnamelength=strlen(acellName);
        dotcellname=malloc(cellnamelength+2);
@@ -870,7 +864,7 @@ afsconf_GetAfsdbInfo(char *acellName, char *aservice,
        dotcellname[cellnamelength]='.';
        dotcellname[cellnamelength+1]=0;
        LOCK_GLOBAL_MUTEX;
-           len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
+       len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
        if ( len < 0 ) {
           len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
        }
@@ -878,11 +872,17 @@ afsconf_GetAfsdbInfo(char *acellName, char *aservice,
        free(dotcellname);
     } else {
        LOCK_GLOBAL_MUTEX;
-           len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
+       len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
        UNLOCK_GLOBAL_MUTEX;
     }
-    if (len < 0)
+    if (len < 0) {
+       if (try_init < 1) {
+           try_init++;
+           res_init();
+           goto retryafsdb;
+       }
        return AFSCONF_NOTFOUND;
+    }
 
     p = answer + sizeof(HEADER);       /* Skip header */
     code = dn_expand(answer, answer + len, p, host, sizeof(host));
@@ -917,7 +917,7 @@ afsconf_GetAfsdbInfo(char *acellName, char *aservice,
                 * right AFSDB type.  Write down the true cell name that
                 * the resolver gave us above.
                 */
-               strcpy(realCellName, host);
+               strlcpy(realCellName, host, sizeof realCellName);
            }
 
            code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
@@ -1000,7 +1000,7 @@ afsconf_GetAfsdbInfo(char *acellName, char *aservice,
     }
 
     acellInfo->numServers = numServers;
-    strcpy(acellInfo->name, acellName);
+    strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
     if (aservice) {
        LOCK_GLOBAL_MUTEX;
        tservice = afsconf_FindService(aservice);