#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
/* 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);
}
static int
-TrimLine(char *abuffer)
+TrimLine(char *abuffer, int abufsize)
{
char tbuffer[256];
register char *tp;
break;
tp++;
}
- strcpy(tbuffer, tp);
- strcpy(abuffer, tbuffer);
+ strlcpy(tbuffer, tp, sizeof tbuffer);
+ strlcpy(abuffer, tbuffer, abufsize);
return 0;
}
/* 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) {
}
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);
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;
}
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
adir->timeRead = 0;
}
- strcpy(tbuf1, tbuffer);
+ strlcpy(tbuf1, tbuffer, sizeof tbuf1);
tf = fopen(tbuffer, "r");
if (!tf) {
return -1;
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] == '>') {
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) {
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 */
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;
/* 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;
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;
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);
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));
}
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));
* 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));
}
acellInfo->numServers = numServers;
- strcpy(acellInfo->name, acellName);
+ strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
if (aservice) {
LOCK_GLOBAL_MUTEX;
tservice = afsconf_FindService(aservice);