2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #include <sys/utime.h>
19 #include <WINNT/afssw.h>
24 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
25 #include <arpa/nameser_compat.h>
28 #include <afs/pthread_glock.h>
29 #include <afs/afsint.h>
33 #include <afs/afsutil.h>
35 #include "cellconfig.h"
40 #include <cm_config.h>
41 /* cm_dns.h depends on cellconfig.h */
48 struct afsconf_servPair {
54 static struct afsconf_servPair serviceTable[] = {
55 {"afs", "afs3-fileserver", 7000,},
56 {"afscb", "afs3-callback", 7001,},
57 {"afsprot", "afs3-prserver", 7002,},
58 {"afsvldb", "afs3-vlserver", 7003,},
59 {"afskauth", "afs3-kaserver", 7004,},
60 {"afsvol", "afs3-volserver", 7005,},
61 {"afserror", "afs3-errors", 7006,},
62 {"afsnanny", "afs3-bos", 7007,},
63 {"afsupdate", "afs3-update", 7008,},
64 {"afsrmtsys", "afs3-rmtsys", 7009,},
65 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
66 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
67 {0, 0, 0} /* insert new services before this spot */
71 static int TrimLine(char *abuffer, int abufsize);
73 static int GetCellNT(struct afsconf_dir *adir);
75 static int GetCellUnix(struct afsconf_dir *adir);
76 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
78 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
79 char *aname, char *aclone);
80 static int ParseCellLine(char *aline, char *aname,
82 static int afsconf_CloseInternal(struct afsconf_dir *adir);
83 static int afsconf_Reopen(struct afsconf_dir *adir);
86 #define T_AFSDB 18 /* per RFC1183 section 1 */
89 #define T_SRV 33 /* RFC2782 */
93 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
94 * our code can tell if there is new info in the key files, the cell server db
95 * files or any of the other files (and reopen the thing) if the date on
99 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
100 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
101 get an fd <= 255. We allow the fileserver to claim more fds than that.
102 This has always been a problem since pr_Initialize would have the same
103 issue, but hpr_Initialize makes it more likely that we would see this.
104 Work around it. This is not generic. It's coded with the needs of
105 afsconf_* in mind only.
107 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
112 struct afsconf_iobuffer {
119 typedef struct afsconf_iobuffer afsconf_FILE;
121 static afsconf_FILE *
122 afsconf_fopen(const char *fname, const char *fmode)
127 if ((fd = open(fname, O_RDONLY)) == -1) {
131 iop = malloc(sizeof(struct afsconf_iobuffer));
138 iop->buffer = malloc(BUFFER);
139 if (iop->buffer == NULL) {
145 iop->ptr = iop->buffer;
146 iop->endptr = iop->buffer;
151 afsconf_fclose(afsconf_FILE *iop)
163 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
171 if (iop->ptr == iop->endptr) {
174 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
184 iop->ptr = iop->buffer;
185 iop->endptr = iop->buffer + len;
189 if ((p - s) == (n - 1)) {
199 #define fopen afsconf_fopen
200 #define fclose afsconf_fclose
201 #define fgets afsconf_fgets
203 #define afsconf_FILE FILE
204 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
206 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
208 afsconf_FindService(const char *aname)
210 /* lookup a service name */
212 struct afsconf_servPair *tsp;
214 if (aname == NULL || aname[0] == '\0')
217 ts = (struct servent *) getservbyname(aname, NULL);
219 /* we found it in /etc/services, so we use this value */
220 return ts->s_port; /* already in network byte order */
223 /* not found in /etc/services, see if it is one of ours */
224 for (tsp = serviceTable; tsp->port; tsp++) {
225 if ((tsp->name && (!strcmp(tsp->name, aname)))
226 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
227 return htons(tsp->port);
233 afsconf_FindIANAName(const char *aname)
235 /* lookup a service name */
236 struct afsconf_servPair *tsp;
238 if (aname == NULL || aname[0] == '\0')
241 /* see if it is one of ours */
242 for (tsp = serviceTable; tsp->port; tsp++) {
243 if ((tsp->name && (!strcmp(tsp->name, aname)))
244 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
245 return tsp->ianaName;
251 TrimLine(char *abuffer, int abufsize)
263 strlcpy(tbuffer, tp, sizeof tbuffer);
264 strlcpy(abuffer, tbuffer, abufsize);
269 * IsClientConfigDirectory() -- determine if path matches well-known
270 * client configuration directory.
273 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
274 #else /* AFS_NT40_ENV */
275 #define IS_SEP(x) ((x) == '/')
276 #endif /* AFS_NT40_ENV */
278 _afsconf_IsClientConfigDirectory(const char *path)
280 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
283 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
285 cc = tolower(cdir[i]);
286 pc = tolower(path[i]);
294 #else /* AFS_NT40_ENV */
297 #endif /* AFS_NT40_ENV */
303 /* hit end of one or both; allow mismatch in existence of trailing slash */
304 if (cdir[i] != '\0') {
305 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
309 if (path[i] != '\0') {
310 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
319 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
323 /* NT client CellServDB has different file name than NT server or Unix */
324 if (_afsconf_IsClientConfigDirectory(adir->name)) {
325 if (!afssw_GetClientCellServDBDir(&p)) {
326 if (asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
330 if (asprintf(path, "%s/%s", adir->name,
331 AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
335 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
342 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
344 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
347 #endif /* AFS_NT40_ENV */
350 _afsconf_UpToDate(struct afsconf_dir *adir)
354 time_t now = time(0);
356 if (adir->timeRead && (adir->timeCheck == now)) {
357 return 1; /* stat no more than once a second */
359 adir->timeCheck = now;
361 if (adir->cellservDB == NULL)
364 code = stat(adir->cellservDB, &tstat);
366 return 0; /* Can't throw the error, so just say we're not up to date */
368 /* did file change? */
369 if (tstat.st_mtime == adir->timeRead)
372 /* otherwise file has changed */
377 afsconf_UpToDate(void *rock)
382 code = _afsconf_UpToDate(rock);
389 _afsconf_Check(struct afsconf_dir *adir)
391 /* did configuration change? */
392 if (_afsconf_UpToDate(adir))
395 /* otherwise file has changed, so reopen it */
396 return afsconf_Reopen(adir);
399 /* set modtime on file */
401 _afsconf_Touch(struct afsconf_dir *adir)
405 struct timeval tvp[2];
408 adir->timeRead = 0; /* just in case */
411 if (adir->cellservDB == NULL)
415 code = _utime(adir->cellservDB, NULL);
417 gettimeofday(&tvp[0], NULL);
419 code = utimes(adir->cellservDB, tvp);
420 #endif /* AFS_NT40_ENV */
426 afsconf_Open(const char *adir)
428 struct afsconf_dir *tdir;
432 /* zero structure and fill in name; rest is done by internal routine */
433 tdir = calloc(1, sizeof(struct afsconf_dir));
434 tdir->name = strdup(adir);
436 code = afsconf_OpenInternal(tdir, 0, 0);
438 char *afsconf_path, afs_confdir[128];
441 /* Check global place only when local Open failed for whatever reason */
442 if (!(afsconf_path = getenv("AFSCONF"))) {
443 /* The "AFSCONF" environment (or contents of "/.AFSCONF") will be typically set to something like "/afs/<cell>/common/etc" where, by convention, the default files for "ThisCell" and "CellServDB" will reside; note that a major drawback is that a given afs client on that cell may NOT contain the same contents... */
449 if (!(home_dir = getenv("HOME"))) {
450 /* Our last chance is the "/.AFSCONF" file */
451 fp = fopen("/.AFSCONF", "r");
456 char *pathname = NULL;
458 r = asprintf(&pathname, "%s/%s", home_dir, ".AFSCONF");
459 if (r < 0 || pathname == NULL)
462 fp = fopen(pathname, "r");
466 /* Our last chance is the "/.AFSCONF" file */
467 fp = fopen("/.AFSCONF", "r");
472 if (fgets(afs_confdir, 128, fp) != NULL)
473 len = strlen(afs_confdir);
478 if (afs_confdir[len - 1] == '\n') {
479 afs_confdir[len - 1] = 0;
481 afsconf_path = afs_confdir;
483 tdir->name = strdup(afsconf_path);
484 code = afsconf_OpenInternal(tdir, 0, 0);
500 GetCellUnix(struct afsconf_dir *adir)
507 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE,
509 fp = fopen(tbuffer, "r");
513 rc = fgets(tbuffer, 256, fp);
519 while (*start != '\0' && isspace(*start))
522 while (*p != '\0' && !isspace(*p))
528 adir->cellName = strdup(start);
535 GetCellNT(struct afsconf_dir *adir)
537 if (_afsconf_IsClientConfigDirectory(adir->name)) {
538 /* NT client config dir; ThisCell is in registry (no file). */
539 return afssw_GetClientCellName(&adir->cellName);
541 /* NT server config dir; works just like Unix */
542 return GetCellUnix(adir);
546 /* The following procedures and structs are used on Windows only
547 * to enumerate the Cell information distributed within the
548 * Windows registry. (See src/WINNT/afsd/cm_config.c)
550 typedef struct _cm_enumCellRegistry {
551 afs_uint32 client; /* non-zero if client query */
552 struct afsconf_dir *adir;
553 } cm_enumCellRegistry_t;
556 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
557 char *hostNamep, unsigned short rank)
559 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
561 if (cellInfop->numServers == MAXHOSTSPERCELL)
564 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
565 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
566 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
567 cellInfop->numServers++;
573 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
576 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
577 char linkedName[256] = "";
579 struct afsconf_entry *newEntry;
582 newEntry = malloc(sizeof(struct afsconf_entry));
583 if (newEntry == NULL)
585 newEntry->cellInfo.numServers = 0;
587 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
588 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
589 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
592 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
593 newEntry->cellInfo.name[MAXCELLCHARS-1];
595 newEntry->cellInfo.linkedCell = strdup(linkedName);
597 newEntry->cellInfo.linkedCell = NULL;
598 newEntry->cellInfo.timeout = timeout;
599 newEntry->cellInfo.flags = 0;
601 newEntry->next = enump->adir->entries;
602 enump->adir->entries = newEntry;
608 #endif /* AFS_NT40_ENV */
612 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
617 struct afsconf_entry *curEntry;
618 struct afsconf_aliasentry *curAlias;
625 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
626 #endif /* AFS_NT40_ENV */
628 /* init the keys queue before any call to afsconf_CloseInternal() */
629 _afsconf_InitKeys(adir);
631 /* figure out the local cell name */
634 enumCellRegistry.adir = adir;
636 i = GetCellUnix(adir);
639 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
645 /* now parse the individual lines */
648 _afsconf_CellServDBPath(adir, &adir->cellservDB);
651 if (_afsconf_IsClientConfigDirectory(adir->name))
652 enumCellRegistry.client = 1;
653 #endif /* AFS_NT40_ENV */
655 if (!stat(adir->cellservDB, &tstat)) {
656 adir->timeRead = tstat.st_mtime;
661 tf = fopen(adir->cellservDB, "r");
666 /* The CellServDB file is now open.
667 * The following code parses the contents of the
668 * file and creates a list with the first cell entry
669 * in the CellServDB file at the end of the list.
671 * No checking is performed for duplicates.
672 * The side effects of this process are that duplicate
673 * entries appended to the end of the CellServDB file
674 * take precedence and are found in a shorter period
679 tp = fgets(tbuffer, sizeof(tbuffer), tf);
682 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
683 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
684 continue; /* empty line */
685 if (tbuffer[0] == '>') {
686 char linkedcell[MAXCELLCHARS];
687 /* start new cell item */
689 /* thread this guy on the list */
690 curEntry->next = adir->entries;
691 adir->entries = curEntry;
694 curEntry = calloc(1, sizeof(struct afsconf_entry));
696 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
698 afsconf_CloseInternal(adir);
703 if (linkedcell[0] != '\0')
704 curEntry->cellInfo.linkedCell = strdup(linkedcell);
706 /* new host in the current cell */
708 afsconf_CloseInternal(adir);
712 i = curEntry->cellInfo.numServers;
713 if (i < MAXHOSTSPERCELL) {
714 if (cell && !strcmp(cell, curEntry->cellInfo.name))
716 ParseHostLine(tbuffer,
717 &curEntry->cellInfo.hostAddr[i],
718 curEntry->cellInfo.hostName[i],
722 ParseHostLine(tbuffer,
723 &curEntry->cellInfo.hostAddr[i],
724 curEntry->cellInfo.hostName[i], 0);
727 if (code == AFSCONF_SYNTAX) {
728 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
734 "Can't properly parse host line \"%s\" in configuration file %s\n",
735 tbuffer, adir->cellservDB);
739 afsconf_CloseInternal(adir);
742 curEntry->cellInfo.numServers = ++i;
745 "Too many hosts for cell %s in configuration file %s\n",
746 curEntry->cellInfo.name, adir->cellservDB);
750 fclose(tf); /* close the file now */
752 /* end the last partially-completed cell */
754 curEntry->next = adir->entries;
755 adir->entries = curEntry;
760 * Windows maintains a CellServDB list in the Registry
761 * that supercedes the contents of the CellServDB file.
762 * Prepending these entries to the head of the list
763 * is sufficient to enforce the precedence.
765 cm_EnumerateCellRegistry( enumCellRegistry.client,
766 cm_enumCellRegistryProc,
768 #endif /* AFS_NT40_ENV */
770 /* Read in the alias list */
771 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE,
774 tf = fopen(tbuffer, "r");
778 tp = fgets(tbuffer, sizeof(tbuffer), tf);
781 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
783 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
784 continue; /* empty line */
787 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
790 continue; /* invalid line */
792 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
795 continue; /* invalid line */
798 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
803 curAlias = calloc(1, sizeof(*curAlias));
805 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
806 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
808 curAlias->next = adir->alias_entries;
809 adir->alias_entries = curAlias;
815 /* now read the fs keys, if possible */
816 code = _afsconf_LoadKeys(adir);
820 code = _afsconf_LoadRealms(adir);
825 /* parse a line of the form
826 *"128.2.1.3 #hostname" or
827 *"[128.2.1.3] #hostname" for clones
828 * into the appropriate pieces.
831 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
842 /* FIXME: length of aname unknown here */
843 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c[0], &c[1], &c[2], &c[3],
848 /* FIXME: length of aname unknown here */
849 code = sscanf(aline, "%d.%d.%d.%d #%s", &c[0], &c[1], &c[2], &c[3],
853 return AFSCONF_SYNTAX;
854 for(i = 0; i < 4; ++i) {
855 if (c[i] < 0 || c[i] > 255) {
856 fprintf(stderr, "Illegal IP address %d.%d.%d.%d\n", c[0], c[1],
858 return AFSCONF_SYNTAX;
861 addr->sin_family = AF_INET;
863 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
864 addr->sin_len = sizeof(struct sockaddr_in);
866 tp = (char *)&addr->sin_addr;
874 /* parse a line of the form
875 * ">cellname [linkedcellname] [#comments]"
876 * into the appropriate pieces.
879 ParseCellLine(char *aline, char *aname,
883 /* FIXME: length of aname, alname unknown here */
884 code = sscanf(aline, ">%s %s", aname, alname);
888 if (*alname == '#') {
892 return (code > 0 ? 0 : AFSCONF_SYNTAX);
895 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
897 afsconf_CellApply(struct afsconf_dir *adir,
898 int (*aproc) (struct afsconf_cell * cell, void *arock,
899 struct afsconf_dir * dir), void *arock)
901 struct afsconf_entry *tde;
904 for (tde = adir->entries; tde; tde = tde->next) {
905 code = (*aproc) (&tde->cellInfo, arock, adir);
915 /* call aproc(entry, arock, adir) for all cell aliases.
916 * Proc must return 0, or we'll stop early and return the code it returns
919 afsconf_CellAliasApply(struct afsconf_dir *adir,
920 int (*aproc) (struct afsconf_cellalias * alias,
921 void *arock, struct afsconf_dir * dir),
924 struct afsconf_aliasentry *tde;
927 for (tde = adir->alias_entries; tde; tde = tde->next) {
928 code = (*aproc) (&tde->aliasInfo, arock, adir);
938 afs_int32 afsconf_SawCell = 0;
941 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
942 char *aservice, struct afsconf_cell *acellInfo,
948 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
955 cell = (char *)&acellInfo->name;
957 code = afsconf_OpenInternal(adir, cell, clones);
961 #if !defined(AFS_NT40_ENV)
963 afsconf_LookupServer(const char *service, const char *protocol,
964 const char *cellName, unsigned short afsdbPort,
965 afs_uint32 *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
966 unsigned short ports[], unsigned short ipRanks[],
967 int *numServers, int *ttl, char **arealCellName)
972 unsigned char answer[4096];
974 char *dotcellname = NULL;
982 char *IANAname = (char *) afsconf_FindIANAName(service);
983 int tservice = afsconf_FindService(service);
989 if (tservice <= 0 || !IANAname)
990 return AFSCONF_NOTFOUND; /* service not found */
992 if (strchr(cellName,'.'))
995 #ifdef HAVE_RES_RETRANSRETRY
996 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1000 * Rx timeout is typically 56 seconds; limit user experience to
1012 r = asprintf(&dotcellname, "_%s._%s.%s.", IANAname, protocol, cellName);
1016 r = asprintf(&dotcellname, "%s.", cellName);
1020 r = asprintf(&dotcellname, "_%s._%s.%s", IANAname, protocol, cellName);
1024 r = asprintf(&dotcellname, "%s", cellName);
1027 if (r < 0 || dotcellname == NULL)
1028 goto findservererror;
1031 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1032 UNLOCK_GLOBAL_MUTEX;
1034 if (dotcellname != NULL) {
1049 code = AFSCONF_NOTFOUND;
1050 goto findservererror;
1054 p = answer + sizeof(HEADER); /* Skip header */
1055 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1057 code = AFSCONF_NOTFOUND;
1058 goto findservererror;
1061 p += code + QFIXEDSZ; /* Skip name */
1063 while (p < answer + len) {
1064 int type, ttl, size;
1066 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1068 code = AFSCONF_NOTFOUND;
1069 goto findservererror;
1072 p += code; /* Skip the name */
1073 type = (p[0] << 8) | p[1];
1074 p += 4; /* Skip type and class */
1075 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1076 p += 4; /* Skip the TTL */
1077 size = (p[0] << 8) | p[1];
1078 p += 2; /* Skip the size */
1080 if (type == T_AFSDB) {
1084 afsdb_type = (p[0] << 8) | p[1];
1085 if (afsdb_type == 1) {
1087 * We know this is an AFSDB record for our cell, of the
1088 * right AFSDB type. Write down the true cell name that
1089 * the resolver gave us above.
1092 realCellName = strdup(host);
1095 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1097 code = AFSCONF_NOTFOUND;
1098 goto findservererror;
1101 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1102 /* Do we want to get TTL data for the A record as well? */
1103 (he = gethostbyname(host))) {
1104 if (he->h_addrtype == AF_INET) {
1106 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1107 cellHostAddrs[server_num] = ipaddr;
1108 ports[server_num] = afsdbPort;
1109 ipRanks[server_num] = 0;
1110 strncpy(cellHostNames[server_num], host,
1111 sizeof(cellHostNames[server_num]));
1113 if (!minttl || ttl < minttl)
1118 if (type == T_SRV) {
1120 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1121 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1122 (strncmp(host + strlen(IANAname) + 3, protocol,
1123 strlen(protocol)) == 0)) {
1125 realCellName = strdup(host + strlen(IANAname) +
1126 strlen(protocol) + 4);
1129 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1131 code = AFSCONF_NOTFOUND;
1132 goto findservererror;
1135 if ((server_num < MAXHOSTSPERCELL) &&
1136 /* Do we want to get TTL data for the A record as well? */
1137 (he = gethostbyname(host))) {
1138 if (he->h_addrtype == AF_INET) {
1141 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1142 cellHostAddrs[server_num] = ipaddr;
1143 ipRanks[server_num] = (p[0] << 8) | p[1];
1144 ports[server_num] = htons((p[4] << 8) | p[5]);
1145 /* weight = (p[2] << 8) | p[3]; */
1146 strncpy(cellHostNames[server_num], host,
1147 sizeof(cellHostNames[server_num]));
1150 if (!minttl || ttl < minttl)
1159 if (server_num == 0) { /* No AFSDB or SRV records */
1160 code = AFSCONF_NOTFOUND;
1161 goto findservererror;
1165 /* Convert the real cell name to lowercase */
1166 for (p = (unsigned char *)realCellName; *p; p++)
1170 *numServers = server_num;
1171 *ttl = minttl ? (time(0) + minttl) : 0;
1173 if ( *numServers > 0 ) {
1175 *arealCellName = realCellName;
1177 code = AFSCONF_NOTFOUND;
1180 if (code && realCellName)
1186 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1187 struct afsconf_cell *acellInfo)
1189 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1190 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1191 unsigned short ipRanks[AFSMAXCELLHOSTS];
1192 unsigned short ports[AFSMAXCELLHOSTS];
1193 char *realCellName = NULL;
1194 int ttl, numServers, i;
1195 char *service = aservice;
1197 unsigned short afsdbport;
1199 service = "afs3-vlserver";
1200 afsdbport = htons(7003);
1203 afsdbport = afsconf_FindService(service);
1205 code = afsconf_LookupServer((const char *)service, "udp",
1206 (const char *)acellName, afsdbport,
1207 cellHostAddrs, cellHostNames,
1208 ports, ipRanks, &numServers, &ttl,
1211 /* If we couldn't find an entry for the requested service
1212 * and that service happens to be the prservice or kaservice
1213 * then fallback to searching for afs3-vlserver and assigning
1214 * the port number here. */
1215 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1216 code = afsconf_LookupServer("afs3-vlserver", "udp",
1217 (const char *)acellName, afsdbport,
1218 cellHostAddrs, cellHostNames,
1219 ports, ipRanks, &numServers, &ttl,
1222 for (i = 0; i < numServers; i++)
1223 ports[i] = afsdbport;
1227 acellInfo->timeout = ttl;
1228 acellInfo->numServers = numServers;
1229 for (i = 0; i < numServers; i++) {
1230 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1232 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1233 acellInfo->hostAddr[i].sin_family = AF_INET;
1234 acellInfo->hostAddr[i].sin_port = ports[i];
1237 strlcpy(acellInfo->name, realCellName,
1238 sizeof(acellInfo->name));
1240 realCellName = NULL;
1243 acellInfo->linkedCell = NULL; /* no linked cell */
1244 acellInfo->flags = 0;
1250 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1251 struct afsconf_cell *acellInfo)
1254 int tservice = afsconf_FindService(aservice); /* network byte order */
1255 const char *ianaName = afsconf_FindIANAName(aservice);
1256 struct afsconf_entry DNSce;
1257 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1258 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1259 unsigned short ipRanks[AFSMAXCELLHOSTS];
1260 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1267 return AFSCONF_NOTFOUND;
1269 tservice = 0; /* port will be assigned by caller */
1272 if (ianaName == NULL)
1273 ianaName = "afs3-vlserver";
1275 DNSce.cellInfo.numServers = 0;
1278 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1279 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1281 /* ignore the ttl here since this code is only called by transitory programs
1282 * like klog, etc. */
1284 /* If we couldn't find an entry for the requested service
1285 * and that service happens to be the prservice or kaservice
1286 * then fallback to searching for afs3-vlserver and assigning
1287 * the port number here. */
1288 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1289 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1290 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1293 for (i = 0; i < numServers; i++)
1294 ports[i] = tservice;
1298 if (rc < 0 || numServers == 0)
1301 for (i = 0; i < numServers; i++) {
1302 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1303 sizeof(afs_uint32));
1304 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1305 acellInfo->hostAddr[i].sin_family = AF_INET;
1307 acellInfo->hostAddr[i].sin_port = ports[i];
1309 acellInfo->hostAddr[i].sin_port = 0;
1312 acellInfo->numServers = numServers;
1313 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1314 acellInfo->linkedCell = NULL; /* no linked cell */
1315 acellInfo->flags = 0;
1318 #endif /* windows */
1321 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1322 struct afsconf_cell *acellInfo)
1324 struct afsconf_entry *tce;
1325 struct afsconf_aliasentry *tcae;
1326 struct afsconf_entry *bestce;
1336 _afsconf_Check(adir);
1339 cnLen = (int)(strlen(tcell) + 1);
1340 lcstring(tcell, tcell, cnLen);
1341 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1342 /* call to afsconf_GetLocalCell: like klog */
1344 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1346 UNLOCK_GLOBAL_MUTEX;
1351 cnLen = strlen(tcell);
1352 bestce = (struct afsconf_entry *)0;
1355 UNLOCK_GLOBAL_MUTEX;
1359 /* Look through the list of aliases */
1360 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1361 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1362 tcell = tcae->aliasInfo.realName;
1367 for (tce = adir->entries; tce; tce = tce->next) {
1368 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1369 /* found our cell */
1374 if (strlen(tce->cellInfo.name) < cnLen)
1375 continue; /* clearly wrong */
1376 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1378 ambig = 1; /* ambiguous unless we get exact match */
1382 if (!ambig && bestce && bestce->cellInfo.numServers) {
1383 *acellInfo = bestce->cellInfo; /* structure assignment */
1385 tservice = afsconf_FindService(aservice);
1387 UNLOCK_GLOBAL_MUTEX;
1388 return AFSCONF_NOTFOUND; /* service not found */
1390 for (i = 0; i < acellInfo->numServers; i++) {
1391 acellInfo->hostAddr[i].sin_port = tservice;
1394 acellInfo->timeout = 0;
1397 * Until we figure out how to separate out ubik server
1398 * queries from other server queries, only perform gethostbyname()
1399 * lookup on the specified hostnames for the client CellServDB files.
1401 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1402 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1404 short numServers=0; /*Num active servers for the cell */
1405 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1406 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1408 memset(&hostAddr, 0, sizeof(hostAddr));
1409 memset(&hostName, 0, sizeof(hostName));
1411 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1412 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1415 if (he && he->h_addrtype == AF_INET) {
1417 /* obtain all the valid address from the list */
1418 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1419 /* check to see if this is a new address; if so insert it into the list */
1422 memcpy(&addr, he->h_addr_list[i], sizeof(addr));
1423 for (k=0, dup=0; !dup && k < numServers; k++) {
1424 if (hostAddr[k].sin_addr.s_addr == addr) {
1431 hostAddr[numServers].sin_family = AF_INET;
1432 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1433 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1434 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1436 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(afs_uint32));
1437 strcpy(hostName[numServers], acellInfo->hostName[j]);
1443 hostAddr[numServers] = acellInfo->hostAddr[j];
1444 strcpy(hostName[numServers], acellInfo->hostName[j]);
1449 for (i=0; i<numServers; i++) {
1450 acellInfo->hostAddr[i] = hostAddr[i];
1451 strcpy(acellInfo->hostName[i], hostName[i]);
1453 acellInfo->numServers = numServers;
1454 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1456 UNLOCK_GLOBAL_MUTEX;
1459 UNLOCK_GLOBAL_MUTEX;
1460 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1465 * Get the current localcell name.
1467 * Internal function to get a pointer to the local cell name.
1468 * This function must be called with the global afsconf lock held.
1470 * @param[in] adir afsconf object
1471 * @param[out] aname address to a char pointer
1472 * @param[in] check always perform a config check, even if the
1473 * the AFSCELL name is set.
1477 * @retval AFSCONF_NOCELLNAME cannot determine local cell name
1482 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1484 static int afsconf_showcell = 0;
1489 * If a cell switch was specified in a command, then it should override the
1490 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1491 * is set and the cell name in the adir structure is used.
1492 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1493 * Optionally, check the configuration, even if using the environment variable.
1495 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1497 _afsconf_Check(adir);
1499 if (!afsconf_showcell) {
1500 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1502 afsconf_showcell = 1;
1504 *pname = afscell_path;
1506 _afsconf_Check(adir);
1507 if (adir->cellName) {
1508 *pname = adir->cellName;
1510 code = AFSCONF_NOCELLNAME;
1516 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1519 char *cellname = NULL;
1522 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1523 if (!code && cellname) {
1524 strlcpy(aname, cellname, alen);
1526 UNLOCK_GLOBAL_MUTEX;
1531 afsconf_Close(struct afsconf_dir *adir)
1538 afsconf_CloseInternal(adir);
1542 UNLOCK_GLOBAL_MUTEX;
1547 afsconf_CloseInternal(struct afsconf_dir *adir)
1549 struct afsconf_entry *td, *nd;
1550 struct afsconf_aliasentry *ta, *na;
1557 tname = adir->name; /* remember name, since that's all we preserve */
1559 /* free everything we can find */
1561 free(adir->cellName);
1562 if (adir->cellservDB)
1563 free(adir->cellservDB);
1564 for (td = adir->entries; td; td = nd) {
1566 if (td->cellInfo.linkedCell)
1567 free(td->cellInfo.linkedCell);
1570 for (ta = adir->alias_entries; ta; ta = na) {
1575 _afsconf_FreeAllKeys(adir);
1576 _afsconf_FreeRealms(adir);
1579 memset(adir, 0, sizeof(struct afsconf_dir));
1580 adir->name = tname; /* restore it */
1585 afsconf_Reopen(struct afsconf_dir *adir)
1588 code = afsconf_CloseInternal(adir);
1591 code = afsconf_OpenInternal(adir, 0, 0);