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>
16 #include <afs/pthread_glock.h>
17 #include <sys/types.h>
20 #include <sys/utime.h>
22 #include <WINNT/afssw.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
35 #endif /* AFS_NT40_ENV */
36 #include <afs/afsint.h>
51 #include <afs/afsutil.h>
53 #include "cellconfig.h"
58 #include <cm_config.h>
59 /* cm_dns.h depends on cellconfig.h */
66 struct afsconf_servPair {
72 static struct afsconf_servPair serviceTable[] = {
73 {"afs", "afs3-fileserver", 7000,},
74 {"afscb", "afs3-callback", 7001,},
75 {"afsprot", "afs3-prserver", 7002,},
76 {"afsvldb", "afs3-vlserver", 7003,},
77 {"afskauth", "afs3-kaserver", 7004,},
78 {"afsvol", "afs3-volserver", 7005,},
79 {"afserror", "afs3-errors", 7006,},
80 {"afsnanny", "afs3-bos", 7007,},
81 {"afsupdate", "afs3-update", 7008,},
82 {"afsrmtsys", "afs3-rmtsys", 7009,},
83 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
84 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
85 {0, 0, 0} /* insert new services before this spot */
89 static int TrimLine(char *abuffer, int abufsize);
91 static int GetCellNT(struct afsconf_dir *adir);
93 static int GetCellUnix(struct afsconf_dir *adir);
94 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
96 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
97 char *aname, char *aclone);
98 static int ParseCellLine(char *aline, char *aname,
100 static int afsconf_CloseInternal(struct afsconf_dir *adir);
101 static int afsconf_Reopen(struct afsconf_dir *adir);
104 #define T_AFSDB 18 /* per RFC1183 section 1 */
107 #define T_SRV 33 /* RFC2782 */
111 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
112 * our code can tell if there is new info in the key files, the cell server db
113 * files or any of the other files (and reopen the thing) if the date on
114 * CellServDB changes.
117 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
118 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
119 get an fd <= 255. We allow the fileserver to claim more fds than that.
120 This has always been a problem since pr_Initialize would have the same
121 issue, but hpr_Initialize makes it more likely that we would see this.
122 Work around it. This is not generic. It's coded with the needs of
123 afsconf_* in mind only.
125 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
130 struct afsconf_iobuffer {
137 typedef struct afsconf_iobuffer afsconf_FILE;
139 static afsconf_FILE *
140 afsconf_fopen(const char *fname, const char *fmode)
145 if ((fd = open(fname, O_RDONLY)) == -1) {
149 iop = malloc(sizeof(struct afsconf_iobuffer));
156 iop->buffer = malloc(BUFFER);
157 if (iop->buffer == NULL) {
163 iop->ptr = iop->buffer;
164 iop->endptr = iop->buffer;
169 afsconf_fclose(afsconf_FILE *iop)
175 free((void *)iop->buffer);
181 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
189 if (iop->ptr == iop->endptr) {
192 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
202 iop->ptr = iop->buffer;
203 iop->endptr = iop->buffer + len;
207 if ((p - s) == (n - 1)) {
217 #define fopen afsconf_fopen
218 #define fclose afsconf_fclose
219 #define fgets afsconf_fgets
221 #define afsconf_FILE FILE
222 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
224 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
226 afsconf_FindService(const char *aname)
228 /* lookup a service name */
230 struct afsconf_servPair *tsp;
232 if (aname == NULL || aname[0] == '\0')
235 #if defined(AFS_OSF_ENV)
236 ts = getservbyname(aname, "");
238 ts = (struct servent *) getservbyname(aname, NULL);
241 /* we found it in /etc/services, so we use this value */
242 return ts->s_port; /* already in network byte order */
245 /* not found in /etc/services, see if it is one of ours */
246 for (tsp = serviceTable; tsp->port; tsp++) {
247 if ((tsp->name && (!strcmp(tsp->name, aname)))
248 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
249 return htons(tsp->port);
255 afsconf_FindIANAName(const char *aname)
257 /* lookup a service name */
258 struct afsconf_servPair *tsp;
260 if (aname == NULL || aname[0] == '\0')
263 /* see if it is one of ours */
264 for (tsp = serviceTable; tsp->port; tsp++) {
265 if ((tsp->name && (!strcmp(tsp->name, aname)))
266 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
267 return tsp->ianaName;
273 TrimLine(char *abuffer, int abufsize)
285 strlcpy(tbuffer, tp, sizeof tbuffer);
286 strlcpy(abuffer, tbuffer, abufsize);
291 * IsClientConfigDirectory() -- determine if path matches well-known
292 * client configuration directory.
295 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
296 #else /* AFS_NT40_ENV */
297 #define IS_SEP(x) ((x) == '/')
298 #endif /* AFS_NT40_ENV */
300 _afsconf_IsClientConfigDirectory(const char *path)
302 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
305 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
307 cc = tolower(cdir[i]);
308 pc = tolower(path[i]);
316 #else /* AFS_NT40_ENV */
319 #endif /* AFS_NT40_ENV */
325 /* hit end of one or both; allow mismatch in existence of trailing slash */
326 if (cdir[i] != '\0') {
327 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
331 if (path[i] != '\0') {
332 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
341 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
345 /* NT client CellServDB has different file name than NT server or Unix */
346 if (_afsconf_IsClientConfigDirectory(adir->name)) {
347 if (!afssw_GetClientCellServDBDir(&p)) {
348 asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
351 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
354 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
360 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
362 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
364 #endif /* AFS_NT40_ENV */
367 _afsconf_UpToDate(struct afsconf_dir *adir)
373 _afsconf_CellServDBPath(adir, &cellservDB);
374 if (cellservDB == NULL)
377 code = stat(cellservDB, &tstat);
380 return 0; /* Can't throw the error, so just say we're not up to date */
382 /* did file change? */
383 if (tstat.st_mtime == adir->timeRead)
386 /* otherwise file has changed */
391 afsconf_UpToDate(void *rock)
396 code = _afsconf_UpToDate(rock);
403 _afsconf_Check(struct afsconf_dir *adir)
405 /* did configuration change? */
406 if (_afsconf_UpToDate(adir))
409 /* otherwise file has changed, so reopen it */
410 return afsconf_Reopen(adir);
413 /* set modtime on file */
415 _afsconf_Touch(struct afsconf_dir *adir)
420 struct timeval tvp[2];
423 adir->timeRead = 0; /* just in case */
425 _afsconf_CellServDBPath(adir, &cellservDB);
426 if (cellservDB == NULL)
430 code = _utime(cellservDB, NULL);
432 gettimeofday(&tvp[0], NULL);
434 code = utimes(cellservDB, tvp);
435 #endif /* AFS_NT40_ENV */
442 afsconf_Open(const char *adir)
444 struct afsconf_dir *tdir;
448 /* zero structure and fill in name; rest is done by internal routine */
449 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
450 memset(tdir, 0, sizeof(struct afsconf_dir));
451 tdir->name = strdup(adir);
453 code = afsconf_OpenInternal(tdir, 0, 0);
455 char *afsconf_path, afs_confdir[128];
458 /* Check global place only when local Open failed for whatever reason */
459 if (!(afsconf_path = getenv("AFSCONF"))) {
460 /* 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... */
465 if (!(home_dir = getenv("HOME"))) {
466 /* Our last chance is the "/.AFSCONF" file */
467 fp = fopen("/.AFSCONF", "r");
471 return (struct afsconf_dir *)0;
473 fgets(afs_confdir, 128, fp);
478 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
479 fp = fopen(pathname, "r");
481 /* Our last chance is the "/.AFSCONF" file */
482 fp = fopen("/.AFSCONF", "r");
486 return (struct afsconf_dir *)0;
489 fgets(afs_confdir, 128, fp);
492 len = strlen(afs_confdir);
496 return (struct afsconf_dir *)0;
498 if (afs_confdir[len - 1] == '\n') {
499 afs_confdir[len - 1] = 0;
501 afsconf_path = afs_confdir;
503 tdir->name = strdup(afsconf_path);
504 code = afsconf_OpenInternal(tdir, 0, 0);
509 return (struct afsconf_dir *)0;
517 GetCellUnix(struct afsconf_dir *adir)
524 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
525 fp = fopen(tbuffer, "r");
529 rc = fgets(tbuffer, 256, fp);
535 while (*start != '\0' && isspace(*start))
538 while (*p != '\0' && !isspace(*p))
544 adir->cellName = strdup(start);
551 GetCellNT(struct afsconf_dir *adir)
553 if (_afsconf_IsClientConfigDirectory(adir->name)) {
554 /* NT client config dir; ThisCell is in registry (no file). */
555 return afssw_GetClientCellName(&adir->cellName);
557 /* NT server config dir; works just like Unix */
558 return GetCellUnix(adir);
562 /* The following procedures and structs are used on Windows only
563 * to enumerate the Cell information distributed within the
564 * Windows registry. (See src/WINNT/afsd/cm_config.c)
566 typedef struct _cm_enumCellRegistry {
567 afs_uint32 client; /* non-zero if client query */
568 struct afsconf_dir *adir;
569 } cm_enumCellRegistry_t;
572 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
573 char *hostNamep, unsigned short rank)
575 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
577 if (cellInfop->numServers == MAXHOSTSPERCELL)
580 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
581 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
582 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
583 cellInfop->numServers++;
589 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
592 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
593 char linkedName[256] = "";
595 struct afsconf_entry *newEntry;
598 newEntry = malloc(sizeof(struct afsconf_entry));
599 if (newEntry == NULL)
601 newEntry->cellInfo.numServers = 0;
603 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
604 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
605 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
608 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
609 newEntry->cellInfo.name[MAXCELLCHARS-1];
611 newEntry->cellInfo.linkedCell = strdup(linkedName);
613 newEntry->cellInfo.linkedCell = NULL;
614 newEntry->cellInfo.timeout = timeout;
615 newEntry->cellInfo.flags = 0;
617 newEntry->next = enump->adir->entries;
618 enump->adir->entries = newEntry;
624 #endif /* AFS_NT40_ENV */
628 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
633 struct afsconf_entry *curEntry;
634 struct afsconf_aliasentry *curAlias;
642 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
643 #endif /* AFS_NT40_ENV */
645 /* figure out the local cell name */
648 enumCellRegistry.adir = adir;
650 i = GetCellUnix(adir);
653 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
659 /* now parse the individual lines */
662 _afsconf_CellServDBPath(adir, &cellservDB);
665 if (_afsconf_IsClientConfigDirectory(adir->name))
666 enumCellRegistry.client = 1;
667 #endif /* AFS_NT40_ENV */
669 if (!stat(cellservDB, &tstat)) {
670 adir->timeRead = tstat.st_mtime;
675 tf = fopen(cellservDB, "r");
680 /* The CellServDB file is now open.
681 * The following code parses the contents of the
682 * file and creates a list with the first cell entry
683 * in the CellServDB file at the end of the list.
685 * No checking is performed for duplicates.
686 * The side effects of this process are that duplicate
687 * entries appended to the end of the CellServDB file
688 * take precedence and are found in a shorter period
693 tp = fgets(tbuffer, sizeof(tbuffer), tf);
696 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
697 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
698 continue; /* empty line */
699 if (tbuffer[0] == '>') {
700 char linkedcell[MAXCELLCHARS];
701 /* start new cell item */
703 /* thread this guy on the list */
704 curEntry->next = adir->entries;
705 adir->entries = curEntry;
709 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
710 memset(curEntry, 0, sizeof(struct afsconf_entry));
712 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
714 afsconf_CloseInternal(adir);
719 if (linkedcell[0] != '\0')
720 curEntry->cellInfo.linkedCell = strdup(linkedcell);
722 /* new host in the current cell */
724 afsconf_CloseInternal(adir);
728 i = curEntry->cellInfo.numServers;
729 if (i < MAXHOSTSPERCELL) {
730 if (cell && !strcmp(cell, curEntry->cellInfo.name))
732 ParseHostLine(tbuffer,
733 &curEntry->cellInfo.hostAddr[i],
734 curEntry->cellInfo.hostName[i],
738 ParseHostLine(tbuffer,
739 &curEntry->cellInfo.hostAddr[i],
740 curEntry->cellInfo.hostName[i], 0);
743 if (code == AFSCONF_SYNTAX) {
744 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
750 "Can't properly parse host line \"%s\" in configuration file %s\n",
751 tbuffer, cellservDB);
755 afsconf_CloseInternal(adir);
758 curEntry->cellInfo.numServers = ++i;
761 "Too many hosts for cell %s in configuration file %s\n",
762 curEntry->cellInfo.name, cellservDB);
766 fclose(tf); /* close the file now */
769 /* end the last partially-completed cell */
771 curEntry->next = adir->entries;
772 adir->entries = curEntry;
777 * Windows maintains a CellServDB list in the Registry
778 * that supercedes the contents of the CellServDB file.
779 * Prepending these entries to the head of the list
780 * is sufficient to enforce the precedence.
782 cm_EnumerateCellRegistry( enumCellRegistry.client,
783 cm_enumCellRegistryProc,
785 #endif /* AFS_NT40_ENV */
787 /* Read in the alias list */
788 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
790 tf = fopen(tbuffer, "r");
794 tp = fgets(tbuffer, sizeof(tbuffer), tf);
797 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
799 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
800 continue; /* empty line */
803 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
806 continue; /* invalid line */
808 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
811 continue; /* invalid line */
814 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
819 curAlias = malloc(sizeof(*curAlias));
820 memset(curAlias, 0, sizeof(*curAlias));
822 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
823 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
825 curAlias->next = adir->alias_entries;
826 adir->alias_entries = curAlias;
831 /* now read the fs keys, if possible */
833 _afsconf_InitKeys(adir);
834 code = _afsconf_LoadKeys(adir);
839 /* parse a line of the form
840 *"128.2.1.3 #hostname" or
841 *"[128.2.1.3] #hostname" for clones
842 * into the appropriate pieces.
845 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
855 /* FIXME: length of aname unknown here */
856 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
860 /* FIXME: length of aname unknown here */
861 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
864 return AFSCONF_SYNTAX;
865 addr->sin_family = AF_INET;
867 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
868 addr->sin_len = sizeof(struct sockaddr_in);
870 tp = (char *)&addr->sin_addr;
878 /* parse a line of the form
879 * ">cellname [linkedcellname] [#comments]"
880 * into the appropriate pieces.
883 ParseCellLine(char *aline, char *aname,
887 /* FIXME: length of aname, alname unknown here */
888 code = sscanf(aline, ">%s %s", aname, alname);
892 if (*alname == '#') {
896 return (code > 0 ? 0 : AFSCONF_SYNTAX);
899 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
901 afsconf_CellApply(struct afsconf_dir *adir,
902 int (*aproc) (struct afsconf_cell * cell, void *arock,
903 struct afsconf_dir * dir), void *arock)
905 struct afsconf_entry *tde;
908 for (tde = adir->entries; tde; tde = tde->next) {
909 code = (*aproc) (&tde->cellInfo, arock, adir);
919 /* call aproc(entry, arock, adir) for all cell aliases.
920 * Proc must return 0, or we'll stop early and return the code it returns
923 afsconf_CellAliasApply(struct afsconf_dir *adir,
924 int (*aproc) (struct afsconf_cellalias * alias,
925 void *arock, struct afsconf_dir * dir),
928 struct afsconf_aliasentry *tde;
931 for (tde = adir->alias_entries; tde; tde = tde->next) {
932 code = (*aproc) (&tde->aliasInfo, arock, adir);
942 afs_int32 afsconf_SawCell = 0;
945 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
946 char *aservice, struct afsconf_cell *acellInfo,
952 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
959 cell = (char *)&acellInfo->name;
961 code = afsconf_OpenInternal(adir, cell, clones);
965 #if !defined(AFS_NT40_ENV)
967 afsconf_LookupServer(const char *service, const char *protocol,
968 const char *cellName, unsigned short afsdbPort,
969 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
970 unsigned short ports[], unsigned short ipRanks[],
971 int *numServers, int *ttl, char **arealCellName)
975 unsigned char answer[1024];
979 int cellnamelength, fullnamelength;
986 char *IANAname = (char *) afsconf_FindIANAName(service);
987 int tservice = afsconf_FindService(service);
993 if (tservice <= 0 || !IANAname)
994 return AFSCONF_NOTFOUND; /* service not found */
996 if (strchr(cellName,'.'))
999 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1000 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1001 dotcellname=malloc(fullnamelength);
1003 return AFSCONF_NOTFOUND; /* service not found */
1005 #ifdef HAVE_RES_RETRANSRETRY
1006 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1010 * Rx timeout is typically 56 seconds; limit user experience to
1021 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1022 IANAname, protocol, cellName);
1026 code = snprintf(dotcellname, fullnamelength, "%s.",
1031 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1032 IANAname, protocol, cellName);
1036 code = snprintf(dotcellname, fullnamelength, "%s",
1040 if ((code < 0) || (code >= fullnamelength))
1041 goto findservererror;
1043 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1044 UNLOCK_GLOBAL_MUTEX;
1056 code = AFSCONF_NOTFOUND;
1057 goto findservererror;
1061 p = answer + sizeof(HEADER); /* Skip header */
1062 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1064 code = AFSCONF_NOTFOUND;
1065 goto findservererror;
1068 p += code + QFIXEDSZ; /* Skip name */
1070 while (p < answer + len) {
1071 int type, ttl, size;
1073 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1075 code = AFSCONF_NOTFOUND;
1076 goto findservererror;
1079 p += code; /* Skip the name */
1080 type = (p[0] << 8) | p[1];
1081 p += 4; /* Skip type and class */
1082 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1083 p += 4; /* Skip the TTL */
1084 size = (p[0] << 8) | p[1];
1085 p += 2; /* Skip the size */
1087 if (type == T_AFSDB) {
1091 afsdb_type = (p[0] << 8) | p[1];
1092 if (afsdb_type == 1) {
1094 * We know this is an AFSDB record for our cell, of the
1095 * right AFSDB type. Write down the true cell name that
1096 * the resolver gave us above.
1099 realCellName = strdup(host);
1102 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1104 code = AFSCONF_NOTFOUND;
1105 goto findservererror;
1108 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1109 /* Do we want to get TTL data for the A record as well? */
1110 (he = gethostbyname(host))) {
1112 memcpy(&ipaddr, he->h_addr, he->h_length);
1113 cellHostAddrs[server_num] = ipaddr;
1114 ports[server_num] = afsdbPort;
1115 ipRanks[server_num] = 0;
1116 strncpy(cellHostNames[server_num], host,
1117 sizeof(cellHostNames[server_num]));
1120 if (!minttl || ttl < minttl)
1124 if (type == T_SRV) {
1126 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1127 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1128 (strncmp(host + strlen(IANAname) + 3, protocol,
1129 strlen(protocol)) == 0)) {
1131 realCellName = strdup(host + strlen(IANAname) +
1132 strlen(protocol) + 4);
1135 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1137 code = AFSCONF_NOTFOUND;
1138 goto findservererror;
1141 if ((server_num < MAXHOSTSPERCELL) &&
1142 /* Do we want to get TTL data for the A record as well? */
1143 (he = gethostbyname(host))) {
1145 memcpy(&ipaddr, he->h_addr, he->h_length);
1146 cellHostAddrs[server_num] = ipaddr;
1147 ipRanks[server_num] = (p[0] << 8) | p[1];
1148 ports[server_num] = htons((p[4] << 8) | p[5]);
1149 /* weight = (p[2] << 8) | p[3]; */
1150 strncpy(cellHostNames[server_num], host,
1151 sizeof(cellHostNames[server_num]));
1154 if (!minttl || ttl < minttl)
1162 if (server_num == 0) { /* No AFSDB or SRV records */
1163 code = AFSCONF_NOTFOUND;
1164 goto findservererror;
1168 /* Convert the real cell name to lowercase */
1169 for (p = (unsigned char *)realCellName; *p; p++)
1173 *numServers = server_num;
1174 *ttl = minttl ? (time(0) + minttl) : 0;
1176 if ( *numServers > 0 ) {
1178 *arealCellName = realCellName;
1180 code = AFSCONF_NOTFOUND;
1183 if (code && realCellName)
1190 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1191 struct afsconf_cell *acellInfo)
1193 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1194 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1195 unsigned short ipRanks[AFSMAXCELLHOSTS];
1196 unsigned short ports[AFSMAXCELLHOSTS];
1197 char *realCellName = NULL;
1198 int ttl, numServers, i;
1199 char *service = aservice;
1201 unsigned short afsdbport;
1203 service = "afs3-vlserver";
1204 afsdbport = htons(7003);
1207 afsdbport = afsconf_FindService(service);
1209 code = afsconf_LookupServer((const char *)service, "udp",
1210 (const char *)acellName, afsdbport,
1211 cellHostAddrs, cellHostNames,
1212 ports, ipRanks, &numServers, &ttl,
1216 acellInfo->timeout = ttl;
1217 acellInfo->numServers = numServers;
1218 for (i = 0; i < numServers; i++) {
1219 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1221 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1222 acellInfo->hostAddr[i].sin_family = AF_INET;
1223 acellInfo->hostAddr[i].sin_port = ports[i];
1226 strlcpy(acellInfo->name, realCellName,
1227 sizeof(acellInfo->name));
1229 realCellName = NULL;
1232 acellInfo->linkedCell = NULL; /* no linked cell */
1233 acellInfo->flags = 0;
1239 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1240 struct afsconf_cell *acellInfo)
1243 int tservice = afsconf_FindService(aservice); /* network byte order */
1244 const char *ianaName = afsconf_FindIANAName(aservice);
1245 struct afsconf_entry DNSce;
1246 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1247 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1248 unsigned short ipRanks[AFSMAXCELLHOSTS];
1249 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1256 return AFSCONF_NOTFOUND;
1258 tservice = 0; /* port will be assigned by caller */
1261 if (ianaName == NULL)
1262 ianaName = "afs3-vlserver";
1264 DNSce.cellInfo.numServers = 0;
1267 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1268 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1270 /* ignore the ttl here since this code is only called by transitory programs
1271 * like klog, etc. */
1273 /* If we couldn't find an entry for the requested service
1274 * and that service happens to be the prservice or kaservice
1275 * then fallback to searching for afs3-vlserver and assigning
1276 * the port number here. */
1277 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1278 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1279 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1282 for (i = 0; i < numServers; i++)
1283 ports[i] = tservice;
1287 if (rc < 0 || numServers == 0)
1290 for (i = 0; i < numServers; i++) {
1291 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1293 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1294 acellInfo->hostAddr[i].sin_family = AF_INET;
1296 acellInfo->hostAddr[i].sin_port = ports[i];
1298 acellInfo->hostAddr[i].sin_port = 0;
1301 acellInfo->numServers = numServers;
1302 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1303 acellInfo->linkedCell = NULL; /* no linked cell */
1304 acellInfo->flags = 0;
1307 #endif /* windows */
1310 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1311 struct afsconf_cell *acellInfo)
1313 struct afsconf_entry *tce;
1314 struct afsconf_aliasentry *tcae;
1315 struct afsconf_entry *bestce;
1325 _afsconf_Check(adir);
1328 cnLen = (int)(strlen(tcell) + 1);
1329 lcstring(tcell, tcell, cnLen);
1330 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1331 /* call to afsconf_GetLocalCell: like klog */
1333 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1335 UNLOCK_GLOBAL_MUTEX;
1340 cnLen = strlen(tcell);
1341 bestce = (struct afsconf_entry *)0;
1344 UNLOCK_GLOBAL_MUTEX;
1348 /* Look through the list of aliases */
1349 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1350 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1351 tcell = tcae->aliasInfo.realName;
1356 for (tce = adir->entries; tce; tce = tce->next) {
1357 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1358 /* found our cell */
1363 if (strlen(tce->cellInfo.name) < cnLen)
1364 continue; /* clearly wrong */
1365 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1367 ambig = 1; /* ambiguous unless we get exact match */
1371 if (!ambig && bestce && bestce->cellInfo.numServers) {
1372 *acellInfo = bestce->cellInfo; /* structure assignment */
1374 tservice = afsconf_FindService(aservice);
1376 UNLOCK_GLOBAL_MUTEX;
1377 return AFSCONF_NOTFOUND; /* service not found */
1379 for (i = 0; i < acellInfo->numServers; i++) {
1380 acellInfo->hostAddr[i].sin_port = tservice;
1383 acellInfo->timeout = 0;
1386 * Until we figure out how to separate out ubik server
1387 * queries from other server queries, only perform gethostbyname()
1388 * lookup on the specified hostnames for the client CellServDB files.
1390 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1391 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1393 short numServers=0; /*Num active servers for the cell */
1394 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1395 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1397 memset(&hostAddr, 0, sizeof(hostAddr));
1398 memset(&hostName, 0, sizeof(hostName));
1400 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1401 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1404 if (he && he->h_addrtype == AF_INET) {
1406 /* obtain all the valid address from the list */
1407 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1408 /* check to see if this is a new address; if so insert it into the list */
1410 for (k=0, dup=0; !dup && k < numServers; k++) {
1411 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1417 hostAddr[numServers].sin_family = AF_INET;
1418 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1419 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1420 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1422 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1423 strcpy(hostName[numServers], acellInfo->hostName[j]);
1429 hostAddr[numServers] = acellInfo->hostAddr[j];
1430 strcpy(hostName[numServers], acellInfo->hostName[j]);
1435 for (i=0; i<numServers; i++) {
1436 acellInfo->hostAddr[i] = hostAddr[i];
1437 strcpy(acellInfo->hostName[i], hostName[i]);
1439 acellInfo->numServers = numServers;
1440 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1442 UNLOCK_GLOBAL_MUTEX;
1445 UNLOCK_GLOBAL_MUTEX;
1446 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1451 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1454 static int afsconf_showcell = 0;
1460 * If a cell switch was specified in a command, then it should override the
1461 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1462 * is set and the cell name in the adir structure is used.
1463 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1465 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1466 if (!afsconf_showcell) {
1467 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1469 afsconf_showcell = 1;
1471 strncpy(aname, afscell_path, alen);
1473 _afsconf_Check(adir);
1474 if (adir->cellName) {
1475 strncpy(aname, adir->cellName, alen);
1477 code = AFSCONF_UNKNOWN;
1480 UNLOCK_GLOBAL_MUTEX;
1485 afsconf_Close(struct afsconf_dir *adir)
1488 afsconf_CloseInternal(adir);
1492 UNLOCK_GLOBAL_MUTEX;
1497 afsconf_CloseInternal(struct afsconf_dir *adir)
1499 struct afsconf_entry *td, *nd;
1500 struct afsconf_aliasentry *ta, *na;
1503 tname = adir->name; /* remember name, since that's all we preserve */
1505 /* free everything we can find */
1507 free(adir->cellName);
1508 for (td = adir->entries; td; td = nd) {
1510 if (td->cellInfo.linkedCell)
1511 free(td->cellInfo.linkedCell);
1514 for (ta = adir->alias_entries; ta; ta = na) {
1519 _afsconf_FreeAllKeys(adir);
1522 memset(adir, 0, sizeof(struct afsconf_dir));
1523 adir->name = tname; /* restore it */
1528 afsconf_Reopen(struct afsconf_dir *adir)
1531 code = afsconf_CloseInternal(adir);
1534 code = afsconf_OpenInternal(adir, 0, 0);