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>
17 #include <sys/utime.h>
18 #include <WINNT/afssw.h>
23 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
24 #include <arpa/nameser_compat.h>
27 #include <afs/pthread_glock.h>
28 #include <afs/afsint.h>
32 #include <afs/afsutil.h>
34 #include "cellconfig.h"
39 #include <cm_config.h>
40 /* cm_dns.h depends on cellconfig.h */
47 struct afsconf_servPair {
53 static struct afsconf_servPair serviceTable[] = {
54 {"afs", "afs3-fileserver", 7000,},
55 {"afscb", "afs3-callback", 7001,},
56 {"afsprot", "afs3-prserver", 7002,},
57 {"afsvldb", "afs3-vlserver", 7003,},
58 {"afskauth", "afs3-kaserver", 7004,},
59 {"afsvol", "afs3-volserver", 7005,},
60 {"afserror", "afs3-errors", 7006,},
61 {"afsnanny", "afs3-bos", 7007,},
62 {"afsupdate", "afs3-update", 7008,},
63 {"afsrmtsys", "afs3-rmtsys", 7009,},
64 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
65 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
66 {0, 0, 0} /* insert new services before this spot */
70 static int TrimLine(char *abuffer, int abufsize);
72 static int GetCellNT(struct afsconf_dir *adir);
74 static int GetCellUnix(struct afsconf_dir *adir);
75 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
77 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
78 char *aname, char *aclone);
79 static int ParseCellLine(char *aline, char *aname,
81 static int afsconf_CloseInternal(struct afsconf_dir *adir);
82 static int afsconf_Reopen(struct afsconf_dir *adir);
85 #define T_AFSDB 18 /* per RFC1183 section 1 */
88 #define T_SRV 33 /* RFC2782 */
92 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
93 * our code can tell if there is new info in the key files, the cell server db
94 * files or any of the other files (and reopen the thing) if the date on
98 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
99 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
100 get an fd <= 255. We allow the fileserver to claim more fds than that.
101 This has always been a problem since pr_Initialize would have the same
102 issue, but hpr_Initialize makes it more likely that we would see this.
103 Work around it. This is not generic. It's coded with the needs of
104 afsconf_* in mind only.
106 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
111 struct afsconf_iobuffer {
118 typedef struct afsconf_iobuffer afsconf_FILE;
120 static afsconf_FILE *
121 afsconf_fopen(const char *fname, const char *fmode)
126 if ((fd = open(fname, O_RDONLY)) == -1) {
130 iop = malloc(sizeof(struct afsconf_iobuffer));
137 iop->buffer = malloc(BUFFER);
138 if (iop->buffer == NULL) {
144 iop->ptr = iop->buffer;
145 iop->endptr = iop->buffer;
150 afsconf_fclose(afsconf_FILE *iop)
156 free((void *)iop->buffer);
162 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
170 if (iop->ptr == iop->endptr) {
173 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
183 iop->ptr = iop->buffer;
184 iop->endptr = iop->buffer + len;
188 if ((p - s) == (n - 1)) {
198 #define fopen afsconf_fopen
199 #define fclose afsconf_fclose
200 #define fgets afsconf_fgets
202 #define afsconf_FILE FILE
203 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
205 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
207 afsconf_FindService(const char *aname)
209 /* lookup a service name */
211 struct afsconf_servPair *tsp;
213 if (aname == NULL || aname[0] == '\0')
216 #if defined(AFS_OSF_ENV)
217 ts = getservbyname(aname, "");
219 ts = (struct servent *) getservbyname(aname, NULL);
222 /* we found it in /etc/services, so we use this value */
223 return ts->s_port; /* already in network byte order */
226 /* not found in /etc/services, see if it is one of ours */
227 for (tsp = serviceTable; tsp->port; tsp++) {
228 if ((tsp->name && (!strcmp(tsp->name, aname)))
229 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
230 return htons(tsp->port);
236 afsconf_FindIANAName(const char *aname)
238 /* lookup a service name */
239 struct afsconf_servPair *tsp;
241 if (aname == NULL || aname[0] == '\0')
244 /* see if it is one of ours */
245 for (tsp = serviceTable; tsp->port; tsp++) {
246 if ((tsp->name && (!strcmp(tsp->name, aname)))
247 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
248 return tsp->ianaName;
254 TrimLine(char *abuffer, int abufsize)
266 strlcpy(tbuffer, tp, sizeof tbuffer);
267 strlcpy(abuffer, tbuffer, abufsize);
272 * IsClientConfigDirectory() -- determine if path matches well-known
273 * client configuration directory.
276 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
277 #else /* AFS_NT40_ENV */
278 #define IS_SEP(x) ((x) == '/')
279 #endif /* AFS_NT40_ENV */
281 _afsconf_IsClientConfigDirectory(const char *path)
283 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
286 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
288 cc = tolower(cdir[i]);
289 pc = tolower(path[i]);
297 #else /* AFS_NT40_ENV */
300 #endif /* AFS_NT40_ENV */
306 /* hit end of one or both; allow mismatch in existence of trailing slash */
307 if (cdir[i] != '\0') {
308 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
312 if (path[i] != '\0') {
313 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
322 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
326 /* NT client CellServDB has different file name than NT server or Unix */
327 if (_afsconf_IsClientConfigDirectory(adir->name)) {
328 if (!afssw_GetClientCellServDBDir(&p)) {
329 asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
332 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
335 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
341 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
343 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
345 #endif /* AFS_NT40_ENV */
348 _afsconf_UpToDate(struct afsconf_dir *adir)
354 _afsconf_CellServDBPath(adir, &cellservDB);
355 if (cellservDB == NULL)
358 code = stat(cellservDB, &tstat);
361 return 0; /* Can't throw the error, so just say we're not up to date */
363 /* did file change? */
364 if (tstat.st_mtime == adir->timeRead)
367 /* otherwise file has changed */
372 afsconf_UpToDate(void *rock)
377 code = _afsconf_UpToDate(rock);
384 _afsconf_Check(struct afsconf_dir *adir)
386 /* did configuration change? */
387 if (_afsconf_UpToDate(adir))
390 /* otherwise file has changed, so reopen it */
391 return afsconf_Reopen(adir);
394 /* set modtime on file */
396 _afsconf_Touch(struct afsconf_dir *adir)
401 struct timeval tvp[2];
404 adir->timeRead = 0; /* just in case */
406 _afsconf_CellServDBPath(adir, &cellservDB);
407 if (cellservDB == NULL)
411 code = _utime(cellservDB, NULL);
413 gettimeofday(&tvp[0], NULL);
415 code = utimes(cellservDB, tvp);
416 #endif /* AFS_NT40_ENV */
423 afsconf_Open(const char *adir)
425 struct afsconf_dir *tdir;
429 /* zero structure and fill in name; rest is done by internal routine */
430 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
431 memset(tdir, 0, sizeof(struct afsconf_dir));
432 tdir->name = strdup(adir);
434 code = afsconf_OpenInternal(tdir, 0, 0);
436 char *afsconf_path, afs_confdir[128];
439 /* Check global place only when local Open failed for whatever reason */
440 if (!(afsconf_path = getenv("AFSCONF"))) {
441 /* 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... */
446 if (!(home_dir = getenv("HOME"))) {
447 /* Our last chance is the "/.AFSCONF" file */
448 fp = fopen("/.AFSCONF", "r");
452 return (struct afsconf_dir *)0;
454 fgets(afs_confdir, 128, fp);
459 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
460 fp = fopen(pathname, "r");
462 /* Our last chance is the "/.AFSCONF" file */
463 fp = fopen("/.AFSCONF", "r");
467 return (struct afsconf_dir *)0;
470 fgets(afs_confdir, 128, fp);
473 len = strlen(afs_confdir);
477 return (struct afsconf_dir *)0;
479 if (afs_confdir[len - 1] == '\n') {
480 afs_confdir[len - 1] = 0;
482 afsconf_path = afs_confdir;
484 tdir->name = strdup(afsconf_path);
485 code = afsconf_OpenInternal(tdir, 0, 0);
490 return (struct afsconf_dir *)0;
498 GetCellUnix(struct afsconf_dir *adir)
505 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
506 fp = fopen(tbuffer, "r");
510 rc = fgets(tbuffer, 256, fp);
516 while (*start != '\0' && isspace(*start))
519 while (*p != '\0' && !isspace(*p))
525 adir->cellName = strdup(start);
532 GetCellNT(struct afsconf_dir *adir)
534 if (_afsconf_IsClientConfigDirectory(adir->name)) {
535 /* NT client config dir; ThisCell is in registry (no file). */
536 return afssw_GetClientCellName(&adir->cellName);
538 /* NT server config dir; works just like Unix */
539 return GetCellUnix(adir);
543 /* The following procedures and structs are used on Windows only
544 * to enumerate the Cell information distributed within the
545 * Windows registry. (See src/WINNT/afsd/cm_config.c)
547 typedef struct _cm_enumCellRegistry {
548 afs_uint32 client; /* non-zero if client query */
549 struct afsconf_dir *adir;
550 } cm_enumCellRegistry_t;
553 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
554 char *hostNamep, unsigned short rank)
556 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
558 if (cellInfop->numServers == MAXHOSTSPERCELL)
561 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
562 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
563 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
564 cellInfop->numServers++;
570 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
573 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
574 char linkedName[256] = "";
576 struct afsconf_entry *newEntry;
579 newEntry = malloc(sizeof(struct afsconf_entry));
580 if (newEntry == NULL)
582 newEntry->cellInfo.numServers = 0;
584 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
585 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
586 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
589 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
590 newEntry->cellInfo.name[MAXCELLCHARS-1];
592 newEntry->cellInfo.linkedCell = strdup(linkedName);
594 newEntry->cellInfo.linkedCell = NULL;
595 newEntry->cellInfo.timeout = timeout;
596 newEntry->cellInfo.flags = 0;
598 newEntry->next = enump->adir->entries;
599 enump->adir->entries = newEntry;
605 #endif /* AFS_NT40_ENV */
609 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
614 struct afsconf_entry *curEntry;
615 struct afsconf_aliasentry *curAlias;
623 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
624 #endif /* AFS_NT40_ENV */
626 /* figure out the local cell name */
629 enumCellRegistry.adir = adir;
631 i = GetCellUnix(adir);
634 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
640 /* now parse the individual lines */
643 _afsconf_CellServDBPath(adir, &cellservDB);
646 if (_afsconf_IsClientConfigDirectory(adir->name))
647 enumCellRegistry.client = 1;
648 #endif /* AFS_NT40_ENV */
650 if (!stat(cellservDB, &tstat)) {
651 adir->timeRead = tstat.st_mtime;
656 tf = fopen(cellservDB, "r");
661 /* The CellServDB file is now open.
662 * The following code parses the contents of the
663 * file and creates a list with the first cell entry
664 * in the CellServDB file at the end of the list.
666 * No checking is performed for duplicates.
667 * The side effects of this process are that duplicate
668 * entries appended to the end of the CellServDB file
669 * take precedence and are found in a shorter period
674 tp = fgets(tbuffer, sizeof(tbuffer), tf);
677 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
678 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
679 continue; /* empty line */
680 if (tbuffer[0] == '>') {
681 char linkedcell[MAXCELLCHARS];
682 /* start new cell item */
684 /* thread this guy on the list */
685 curEntry->next = adir->entries;
686 adir->entries = curEntry;
690 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
691 memset(curEntry, 0, sizeof(struct afsconf_entry));
693 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
695 afsconf_CloseInternal(adir);
700 if (linkedcell[0] != '\0')
701 curEntry->cellInfo.linkedCell = strdup(linkedcell);
703 /* new host in the current cell */
705 afsconf_CloseInternal(adir);
709 i = curEntry->cellInfo.numServers;
710 if (i < MAXHOSTSPERCELL) {
711 if (cell && !strcmp(cell, curEntry->cellInfo.name))
713 ParseHostLine(tbuffer,
714 &curEntry->cellInfo.hostAddr[i],
715 curEntry->cellInfo.hostName[i],
719 ParseHostLine(tbuffer,
720 &curEntry->cellInfo.hostAddr[i],
721 curEntry->cellInfo.hostName[i], 0);
724 if (code == AFSCONF_SYNTAX) {
725 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
731 "Can't properly parse host line \"%s\" in configuration file %s\n",
732 tbuffer, cellservDB);
736 afsconf_CloseInternal(adir);
739 curEntry->cellInfo.numServers = ++i;
742 "Too many hosts for cell %s in configuration file %s\n",
743 curEntry->cellInfo.name, cellservDB);
747 fclose(tf); /* close the file now */
750 /* end the last partially-completed cell */
752 curEntry->next = adir->entries;
753 adir->entries = curEntry;
758 * Windows maintains a CellServDB list in the Registry
759 * that supercedes the contents of the CellServDB file.
760 * Prepending these entries to the head of the list
761 * is sufficient to enforce the precedence.
763 cm_EnumerateCellRegistry( enumCellRegistry.client,
764 cm_enumCellRegistryProc,
766 #endif /* AFS_NT40_ENV */
768 /* Read in the alias list */
769 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
771 tf = fopen(tbuffer, "r");
775 tp = fgets(tbuffer, sizeof(tbuffer), tf);
778 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
780 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
781 continue; /* empty line */
784 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
787 continue; /* invalid line */
789 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
792 continue; /* invalid line */
795 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
800 curAlias = malloc(sizeof(*curAlias));
801 memset(curAlias, 0, sizeof(*curAlias));
803 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
804 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
806 curAlias->next = adir->alias_entries;
807 adir->alias_entries = curAlias;
812 /* now read the fs keys, if possible */
814 _afsconf_InitKeys(adir);
815 code = _afsconf_LoadKeys(adir);
820 /* parse a line of the form
821 *"128.2.1.3 #hostname" or
822 *"[128.2.1.3] #hostname" for clones
823 * into the appropriate pieces.
826 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
836 /* FIXME: length of aname unknown here */
837 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
841 /* FIXME: length of aname unknown here */
842 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
845 return AFSCONF_SYNTAX;
846 addr->sin_family = AF_INET;
848 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
849 addr->sin_len = sizeof(struct sockaddr_in);
851 tp = (char *)&addr->sin_addr;
859 /* parse a line of the form
860 * ">cellname [linkedcellname] [#comments]"
861 * into the appropriate pieces.
864 ParseCellLine(char *aline, char *aname,
868 /* FIXME: length of aname, alname unknown here */
869 code = sscanf(aline, ">%s %s", aname, alname);
873 if (*alname == '#') {
877 return (code > 0 ? 0 : AFSCONF_SYNTAX);
880 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
882 afsconf_CellApply(struct afsconf_dir *adir,
883 int (*aproc) (struct afsconf_cell * cell, void *arock,
884 struct afsconf_dir * dir), void *arock)
886 struct afsconf_entry *tde;
889 for (tde = adir->entries; tde; tde = tde->next) {
890 code = (*aproc) (&tde->cellInfo, arock, adir);
900 /* call aproc(entry, arock, adir) for all cell aliases.
901 * Proc must return 0, or we'll stop early and return the code it returns
904 afsconf_CellAliasApply(struct afsconf_dir *adir,
905 int (*aproc) (struct afsconf_cellalias * alias,
906 void *arock, struct afsconf_dir * dir),
909 struct afsconf_aliasentry *tde;
912 for (tde = adir->alias_entries; tde; tde = tde->next) {
913 code = (*aproc) (&tde->aliasInfo, arock, adir);
923 afs_int32 afsconf_SawCell = 0;
926 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
927 char *aservice, struct afsconf_cell *acellInfo,
933 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
940 cell = (char *)&acellInfo->name;
942 code = afsconf_OpenInternal(adir, cell, clones);
946 #if !defined(AFS_NT40_ENV)
948 afsconf_LookupServer(const char *service, const char *protocol,
949 const char *cellName, unsigned short afsdbPort,
950 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
951 unsigned short ports[], unsigned short ipRanks[],
952 int *numServers, int *ttl, char **arealCellName)
956 unsigned char answer[1024];
960 int cellnamelength, fullnamelength;
967 char *IANAname = (char *) afsconf_FindIANAName(service);
968 int tservice = afsconf_FindService(service);
974 if (tservice <= 0 || !IANAname)
975 return AFSCONF_NOTFOUND; /* service not found */
977 if (strchr(cellName,'.'))
980 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
981 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
982 dotcellname=malloc(fullnamelength);
984 return AFSCONF_NOTFOUND; /* service not found */
986 #ifdef HAVE_RES_RETRANSRETRY
987 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
991 * Rx timeout is typically 56 seconds; limit user experience to
1002 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1003 IANAname, protocol, cellName);
1007 code = snprintf(dotcellname, fullnamelength, "%s.",
1012 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1013 IANAname, protocol, cellName);
1017 code = snprintf(dotcellname, fullnamelength, "%s",
1021 if ((code < 0) || (code >= fullnamelength))
1022 goto findservererror;
1024 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1025 UNLOCK_GLOBAL_MUTEX;
1037 code = AFSCONF_NOTFOUND;
1038 goto findservererror;
1042 p = answer + sizeof(HEADER); /* Skip header */
1043 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1045 code = AFSCONF_NOTFOUND;
1046 goto findservererror;
1049 p += code + QFIXEDSZ; /* Skip name */
1051 while (p < answer + len) {
1052 int type, ttl, size;
1054 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1056 code = AFSCONF_NOTFOUND;
1057 goto findservererror;
1060 p += code; /* Skip the name */
1061 type = (p[0] << 8) | p[1];
1062 p += 4; /* Skip type and class */
1063 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1064 p += 4; /* Skip the TTL */
1065 size = (p[0] << 8) | p[1];
1066 p += 2; /* Skip the size */
1068 if (type == T_AFSDB) {
1072 afsdb_type = (p[0] << 8) | p[1];
1073 if (afsdb_type == 1) {
1075 * We know this is an AFSDB record for our cell, of the
1076 * right AFSDB type. Write down the true cell name that
1077 * the resolver gave us above.
1080 realCellName = strdup(host);
1083 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1085 code = AFSCONF_NOTFOUND;
1086 goto findservererror;
1089 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1090 /* Do we want to get TTL data for the A record as well? */
1091 (he = gethostbyname(host))) {
1093 memcpy(&ipaddr, he->h_addr, he->h_length);
1094 cellHostAddrs[server_num] = ipaddr;
1095 ports[server_num] = afsdbPort;
1096 ipRanks[server_num] = 0;
1097 strncpy(cellHostNames[server_num], host,
1098 sizeof(cellHostNames[server_num]));
1101 if (!minttl || ttl < minttl)
1105 if (type == T_SRV) {
1107 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1108 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1109 (strncmp(host + strlen(IANAname) + 3, protocol,
1110 strlen(protocol)) == 0)) {
1112 realCellName = strdup(host + strlen(IANAname) +
1113 strlen(protocol) + 4);
1116 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1118 code = AFSCONF_NOTFOUND;
1119 goto findservererror;
1122 if ((server_num < MAXHOSTSPERCELL) &&
1123 /* Do we want to get TTL data for the A record as well? */
1124 (he = gethostbyname(host))) {
1126 memcpy(&ipaddr, he->h_addr, he->h_length);
1127 cellHostAddrs[server_num] = ipaddr;
1128 ipRanks[server_num] = (p[0] << 8) | p[1];
1129 ports[server_num] = htons((p[4] << 8) | p[5]);
1130 /* weight = (p[2] << 8) | p[3]; */
1131 strncpy(cellHostNames[server_num], host,
1132 sizeof(cellHostNames[server_num]));
1135 if (!minttl || ttl < minttl)
1143 if (server_num == 0) { /* No AFSDB or SRV records */
1144 code = AFSCONF_NOTFOUND;
1145 goto findservererror;
1149 /* Convert the real cell name to lowercase */
1150 for (p = (unsigned char *)realCellName; *p; p++)
1154 *numServers = server_num;
1155 *ttl = minttl ? (time(0) + minttl) : 0;
1157 if ( *numServers > 0 ) {
1159 *arealCellName = realCellName;
1161 code = AFSCONF_NOTFOUND;
1164 if (code && realCellName)
1171 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1172 struct afsconf_cell *acellInfo)
1174 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1175 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1176 unsigned short ipRanks[AFSMAXCELLHOSTS];
1177 unsigned short ports[AFSMAXCELLHOSTS];
1178 char *realCellName = NULL;
1179 int ttl, numServers, i;
1180 char *service = aservice;
1182 unsigned short afsdbport;
1184 service = "afs3-vlserver";
1185 afsdbport = htons(7003);
1188 afsdbport = afsconf_FindService(service);
1190 code = afsconf_LookupServer((const char *)service, "udp",
1191 (const char *)acellName, afsdbport,
1192 cellHostAddrs, cellHostNames,
1193 ports, ipRanks, &numServers, &ttl,
1197 acellInfo->timeout = ttl;
1198 acellInfo->numServers = numServers;
1199 for (i = 0; i < numServers; i++) {
1200 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1202 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1203 acellInfo->hostAddr[i].sin_family = AF_INET;
1204 acellInfo->hostAddr[i].sin_port = ports[i];
1207 strlcpy(acellInfo->name, realCellName,
1208 sizeof(acellInfo->name));
1210 realCellName = NULL;
1213 acellInfo->linkedCell = NULL; /* no linked cell */
1214 acellInfo->flags = 0;
1220 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1221 struct afsconf_cell *acellInfo)
1224 int tservice = afsconf_FindService(aservice); /* network byte order */
1225 const char *ianaName = afsconf_FindIANAName(aservice);
1226 struct afsconf_entry DNSce;
1227 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1228 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1229 unsigned short ipRanks[AFSMAXCELLHOSTS];
1230 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1237 return AFSCONF_NOTFOUND;
1239 tservice = 0; /* port will be assigned by caller */
1242 if (ianaName == NULL)
1243 ianaName = "afs3-vlserver";
1245 DNSce.cellInfo.numServers = 0;
1248 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1249 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1251 /* ignore the ttl here since this code is only called by transitory programs
1252 * like klog, etc. */
1254 /* If we couldn't find an entry for the requested service
1255 * and that service happens to be the prservice or kaservice
1256 * then fallback to searching for afs3-vlserver and assigning
1257 * the port number here. */
1258 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1259 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1260 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1263 for (i = 0; i < numServers; i++)
1264 ports[i] = tservice;
1268 if (rc < 0 || numServers == 0)
1271 for (i = 0; i < numServers; i++) {
1272 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1274 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1275 acellInfo->hostAddr[i].sin_family = AF_INET;
1277 acellInfo->hostAddr[i].sin_port = ports[i];
1279 acellInfo->hostAddr[i].sin_port = 0;
1282 acellInfo->numServers = numServers;
1283 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1284 acellInfo->linkedCell = NULL; /* no linked cell */
1285 acellInfo->flags = 0;
1288 #endif /* windows */
1291 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1292 struct afsconf_cell *acellInfo)
1294 struct afsconf_entry *tce;
1295 struct afsconf_aliasentry *tcae;
1296 struct afsconf_entry *bestce;
1306 _afsconf_Check(adir);
1309 cnLen = (int)(strlen(tcell) + 1);
1310 lcstring(tcell, tcell, cnLen);
1311 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1312 /* call to afsconf_GetLocalCell: like klog */
1314 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1316 UNLOCK_GLOBAL_MUTEX;
1321 cnLen = strlen(tcell);
1322 bestce = (struct afsconf_entry *)0;
1325 UNLOCK_GLOBAL_MUTEX;
1329 /* Look through the list of aliases */
1330 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1331 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1332 tcell = tcae->aliasInfo.realName;
1337 for (tce = adir->entries; tce; tce = tce->next) {
1338 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1339 /* found our cell */
1344 if (strlen(tce->cellInfo.name) < cnLen)
1345 continue; /* clearly wrong */
1346 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1348 ambig = 1; /* ambiguous unless we get exact match */
1352 if (!ambig && bestce && bestce->cellInfo.numServers) {
1353 *acellInfo = bestce->cellInfo; /* structure assignment */
1355 tservice = afsconf_FindService(aservice);
1357 UNLOCK_GLOBAL_MUTEX;
1358 return AFSCONF_NOTFOUND; /* service not found */
1360 for (i = 0; i < acellInfo->numServers; i++) {
1361 acellInfo->hostAddr[i].sin_port = tservice;
1364 acellInfo->timeout = 0;
1367 * Until we figure out how to separate out ubik server
1368 * queries from other server queries, only perform gethostbyname()
1369 * lookup on the specified hostnames for the client CellServDB files.
1371 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1372 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1374 short numServers=0; /*Num active servers for the cell */
1375 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1376 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1378 memset(&hostAddr, 0, sizeof(hostAddr));
1379 memset(&hostName, 0, sizeof(hostName));
1381 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1382 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1385 if (he && he->h_addrtype == AF_INET) {
1387 /* obtain all the valid address from the list */
1388 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1389 /* check to see if this is a new address; if so insert it into the list */
1391 for (k=0, dup=0; !dup && k < numServers; k++) {
1392 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1398 hostAddr[numServers].sin_family = AF_INET;
1399 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1400 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1401 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1403 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1404 strcpy(hostName[numServers], acellInfo->hostName[j]);
1410 hostAddr[numServers] = acellInfo->hostAddr[j];
1411 strcpy(hostName[numServers], acellInfo->hostName[j]);
1416 for (i=0; i<numServers; i++) {
1417 acellInfo->hostAddr[i] = hostAddr[i];
1418 strcpy(acellInfo->hostName[i], hostName[i]);
1420 acellInfo->numServers = numServers;
1421 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1423 UNLOCK_GLOBAL_MUTEX;
1426 UNLOCK_GLOBAL_MUTEX;
1427 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1432 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1435 static int afsconf_showcell = 0;
1441 * If a cell switch was specified in a command, then it should override the
1442 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1443 * is set and the cell name in the adir structure is used.
1444 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1446 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1447 if (!afsconf_showcell) {
1448 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1450 afsconf_showcell = 1;
1452 strncpy(aname, afscell_path, alen);
1454 _afsconf_Check(adir);
1455 if (adir->cellName) {
1456 strncpy(aname, adir->cellName, alen);
1458 code = AFSCONF_UNKNOWN;
1461 UNLOCK_GLOBAL_MUTEX;
1466 afsconf_Close(struct afsconf_dir *adir)
1469 afsconf_CloseInternal(adir);
1473 UNLOCK_GLOBAL_MUTEX;
1478 afsconf_CloseInternal(struct afsconf_dir *adir)
1480 struct afsconf_entry *td, *nd;
1481 struct afsconf_aliasentry *ta, *na;
1484 tname = adir->name; /* remember name, since that's all we preserve */
1486 /* free everything we can find */
1488 free(adir->cellName);
1489 for (td = adir->entries; td; td = nd) {
1491 if (td->cellInfo.linkedCell)
1492 free(td->cellInfo.linkedCell);
1495 for (ta = adir->alias_entries; ta; ta = na) {
1500 _afsconf_FreeAllKeys(adir);
1503 memset(adir, 0, sizeof(struct afsconf_dir));
1504 adir->name = tname; /* restore it */
1509 afsconf_Reopen(struct afsconf_dir *adir)
1512 code = afsconf_CloseInternal(adir);
1515 code = afsconf_OpenInternal(adir, 0, 0);