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 #if defined(AFS_OSF_ENV)
218 ts = getservbyname(aname, "");
220 ts = (struct servent *) getservbyname(aname, NULL);
223 /* we found it in /etc/services, so we use this value */
224 return ts->s_port; /* already in network byte order */
227 /* not found in /etc/services, see if it is one of ours */
228 for (tsp = serviceTable; tsp->port; tsp++) {
229 if ((tsp->name && (!strcmp(tsp->name, aname)))
230 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
231 return htons(tsp->port);
237 afsconf_FindIANAName(const char *aname)
239 /* lookup a service name */
240 struct afsconf_servPair *tsp;
242 if (aname == NULL || aname[0] == '\0')
245 /* 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 tsp->ianaName;
255 TrimLine(char *abuffer, int abufsize)
267 strlcpy(tbuffer, tp, sizeof tbuffer);
268 strlcpy(abuffer, tbuffer, abufsize);
273 * IsClientConfigDirectory() -- determine if path matches well-known
274 * client configuration directory.
277 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
278 #else /* AFS_NT40_ENV */
279 #define IS_SEP(x) ((x) == '/')
280 #endif /* AFS_NT40_ENV */
282 _afsconf_IsClientConfigDirectory(const char *path)
284 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
287 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
289 cc = tolower(cdir[i]);
290 pc = tolower(path[i]);
298 #else /* AFS_NT40_ENV */
301 #endif /* AFS_NT40_ENV */
307 /* hit end of one or both; allow mismatch in existence of trailing slash */
308 if (cdir[i] != '\0') {
309 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
313 if (path[i] != '\0') {
314 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
323 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
327 /* NT client CellServDB has different file name than NT server or Unix */
328 if (_afsconf_IsClientConfigDirectory(adir->name)) {
329 if (!afssw_GetClientCellServDBDir(&p)) {
330 asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
333 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
336 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
342 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
344 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
346 #endif /* AFS_NT40_ENV */
349 _afsconf_UpToDate(struct afsconf_dir *adir)
354 time_t now = time(0);
356 if (adir->timeCheck == now) {
357 return 1; /* stat no more than once a second */
359 adir->timeCheck = now;
361 _afsconf_CellServDBPath(adir, &cellservDB);
362 if (cellservDB == NULL)
365 code = stat(cellservDB, &tstat);
368 return 0; /* Can't throw the error, so just say we're not up to date */
370 /* did file change? */
371 if (tstat.st_mtime == adir->timeRead)
374 /* otherwise file has changed */
379 afsconf_UpToDate(void *rock)
384 code = _afsconf_UpToDate(rock);
391 _afsconf_Check(struct afsconf_dir *adir)
393 /* did configuration change? */
394 if (_afsconf_UpToDate(adir))
397 /* otherwise file has changed, so reopen it */
398 return afsconf_Reopen(adir);
401 /* set modtime on file */
403 _afsconf_Touch(struct afsconf_dir *adir)
408 struct timeval tvp[2];
411 adir->timeRead = 0; /* just in case */
414 _afsconf_CellServDBPath(adir, &cellservDB);
415 if (cellservDB == NULL)
419 code = _utime(cellservDB, NULL);
421 gettimeofday(&tvp[0], NULL);
423 code = utimes(cellservDB, tvp);
424 #endif /* AFS_NT40_ENV */
431 afsconf_Open(const char *adir)
433 struct afsconf_dir *tdir;
437 /* zero structure and fill in name; rest is done by internal routine */
438 tdir = calloc(1, sizeof(struct afsconf_dir));
439 tdir->name = strdup(adir);
441 code = afsconf_OpenInternal(tdir, 0, 0);
443 char *afsconf_path, afs_confdir[128];
446 /* Check global place only when local Open failed for whatever reason */
447 if (!(afsconf_path = getenv("AFSCONF"))) {
448 /* 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... */
453 if (!(home_dir = getenv("HOME"))) {
454 /* Our last chance is the "/.AFSCONF" file */
455 fp = fopen("/.AFSCONF", "r");
459 return (struct afsconf_dir *)0;
461 fgets(afs_confdir, 128, fp);
466 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
467 fp = fopen(pathname, "r");
469 /* Our last chance is the "/.AFSCONF" file */
470 fp = fopen("/.AFSCONF", "r");
474 return (struct afsconf_dir *)0;
477 fgets(afs_confdir, 128, fp);
480 len = strlen(afs_confdir);
484 return (struct afsconf_dir *)0;
486 if (afs_confdir[len - 1] == '\n') {
487 afs_confdir[len - 1] = 0;
489 afsconf_path = afs_confdir;
491 tdir->name = strdup(afsconf_path);
492 code = afsconf_OpenInternal(tdir, 0, 0);
497 return (struct afsconf_dir *)0;
505 GetCellUnix(struct afsconf_dir *adir)
512 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
513 fp = fopen(tbuffer, "r");
517 rc = fgets(tbuffer, 256, fp);
523 while (*start != '\0' && isspace(*start))
526 while (*p != '\0' && !isspace(*p))
532 adir->cellName = strdup(start);
539 GetCellNT(struct afsconf_dir *adir)
541 if (_afsconf_IsClientConfigDirectory(adir->name)) {
542 /* NT client config dir; ThisCell is in registry (no file). */
543 return afssw_GetClientCellName(&adir->cellName);
545 /* NT server config dir; works just like Unix */
546 return GetCellUnix(adir);
550 /* The following procedures and structs are used on Windows only
551 * to enumerate the Cell information distributed within the
552 * Windows registry. (See src/WINNT/afsd/cm_config.c)
554 typedef struct _cm_enumCellRegistry {
555 afs_uint32 client; /* non-zero if client query */
556 struct afsconf_dir *adir;
557 } cm_enumCellRegistry_t;
560 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
561 char *hostNamep, unsigned short rank)
563 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
565 if (cellInfop->numServers == MAXHOSTSPERCELL)
568 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
569 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
570 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
571 cellInfop->numServers++;
577 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
580 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
581 char linkedName[256] = "";
583 struct afsconf_entry *newEntry;
586 newEntry = malloc(sizeof(struct afsconf_entry));
587 if (newEntry == NULL)
589 newEntry->cellInfo.numServers = 0;
591 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
592 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
593 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
596 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
597 newEntry->cellInfo.name[MAXCELLCHARS-1];
599 newEntry->cellInfo.linkedCell = strdup(linkedName);
601 newEntry->cellInfo.linkedCell = NULL;
602 newEntry->cellInfo.timeout = timeout;
603 newEntry->cellInfo.flags = 0;
605 newEntry->next = enump->adir->entries;
606 enump->adir->entries = newEntry;
612 #endif /* AFS_NT40_ENV */
616 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
621 struct afsconf_entry *curEntry;
622 struct afsconf_aliasentry *curAlias;
630 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
631 #endif /* AFS_NT40_ENV */
633 /* figure out the local cell name */
636 enumCellRegistry.adir = adir;
638 i = GetCellUnix(adir);
641 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
647 /* now parse the individual lines */
650 _afsconf_CellServDBPath(adir, &cellservDB);
653 if (_afsconf_IsClientConfigDirectory(adir->name))
654 enumCellRegistry.client = 1;
655 #endif /* AFS_NT40_ENV */
657 if (!stat(cellservDB, &tstat)) {
658 adir->timeRead = tstat.st_mtime;
663 tf = fopen(cellservDB, "r");
668 /* init the keys queue before any call to afsconf_CloseInternal() */
669 _afsconf_InitKeys(adir);
671 /* The CellServDB file is now open.
672 * The following code parses the contents of the
673 * file and creates a list with the first cell entry
674 * in the CellServDB file at the end of the list.
676 * No checking is performed for duplicates.
677 * The side effects of this process are that duplicate
678 * entries appended to the end of the CellServDB file
679 * take precedence and are found in a shorter period
684 tp = fgets(tbuffer, sizeof(tbuffer), tf);
687 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
688 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
689 continue; /* empty line */
690 if (tbuffer[0] == '>') {
691 char linkedcell[MAXCELLCHARS];
692 /* start new cell item */
694 /* thread this guy on the list */
695 curEntry->next = adir->entries;
696 adir->entries = curEntry;
699 curEntry = calloc(1, sizeof(struct afsconf_entry));
701 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
703 afsconf_CloseInternal(adir);
708 if (linkedcell[0] != '\0')
709 curEntry->cellInfo.linkedCell = strdup(linkedcell);
711 /* new host in the current cell */
713 afsconf_CloseInternal(adir);
717 i = curEntry->cellInfo.numServers;
718 if (i < MAXHOSTSPERCELL) {
719 if (cell && !strcmp(cell, curEntry->cellInfo.name))
721 ParseHostLine(tbuffer,
722 &curEntry->cellInfo.hostAddr[i],
723 curEntry->cellInfo.hostName[i],
727 ParseHostLine(tbuffer,
728 &curEntry->cellInfo.hostAddr[i],
729 curEntry->cellInfo.hostName[i], 0);
732 if (code == AFSCONF_SYNTAX) {
733 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
739 "Can't properly parse host line \"%s\" in configuration file %s\n",
740 tbuffer, cellservDB);
744 afsconf_CloseInternal(adir);
747 curEntry->cellInfo.numServers = ++i;
750 "Too many hosts for cell %s in configuration file %s\n",
751 curEntry->cellInfo.name, cellservDB);
755 fclose(tf); /* close the file now */
758 /* end the last partially-completed cell */
760 curEntry->next = adir->entries;
761 adir->entries = curEntry;
766 * Windows maintains a CellServDB list in the Registry
767 * that supercedes the contents of the CellServDB file.
768 * Prepending these entries to the head of the list
769 * is sufficient to enforce the precedence.
771 cm_EnumerateCellRegistry( enumCellRegistry.client,
772 cm_enumCellRegistryProc,
774 #endif /* AFS_NT40_ENV */
776 /* Read in the alias list */
777 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
779 tf = fopen(tbuffer, "r");
783 tp = fgets(tbuffer, sizeof(tbuffer), tf);
786 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
788 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
789 continue; /* empty line */
792 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
795 continue; /* invalid line */
797 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
800 continue; /* invalid line */
803 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
808 curAlias = calloc(1, sizeof(*curAlias));
810 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
811 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
813 curAlias->next = adir->alias_entries;
814 adir->alias_entries = curAlias;
820 /* now read the fs keys, if possible */
821 code = _afsconf_LoadKeys(adir);
825 code = _afsconf_LoadRealms(adir);
830 /* parse a line of the form
831 *"128.2.1.3 #hostname" or
832 *"[128.2.1.3] #hostname" for clones
833 * into the appropriate pieces.
836 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
846 /* FIXME: length of aname unknown here */
847 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
851 /* FIXME: length of aname unknown here */
852 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
855 return AFSCONF_SYNTAX;
856 addr->sin_family = AF_INET;
858 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
859 addr->sin_len = sizeof(struct sockaddr_in);
861 tp = (char *)&addr->sin_addr;
869 /* parse a line of the form
870 * ">cellname [linkedcellname] [#comments]"
871 * into the appropriate pieces.
874 ParseCellLine(char *aline, char *aname,
878 /* FIXME: length of aname, alname unknown here */
879 code = sscanf(aline, ">%s %s", aname, alname);
883 if (*alname == '#') {
887 return (code > 0 ? 0 : AFSCONF_SYNTAX);
890 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
892 afsconf_CellApply(struct afsconf_dir *adir,
893 int (*aproc) (struct afsconf_cell * cell, void *arock,
894 struct afsconf_dir * dir), void *arock)
896 struct afsconf_entry *tde;
899 for (tde = adir->entries; tde; tde = tde->next) {
900 code = (*aproc) (&tde->cellInfo, arock, adir);
910 /* call aproc(entry, arock, adir) for all cell aliases.
911 * Proc must return 0, or we'll stop early and return the code it returns
914 afsconf_CellAliasApply(struct afsconf_dir *adir,
915 int (*aproc) (struct afsconf_cellalias * alias,
916 void *arock, struct afsconf_dir * dir),
919 struct afsconf_aliasentry *tde;
922 for (tde = adir->alias_entries; tde; tde = tde->next) {
923 code = (*aproc) (&tde->aliasInfo, arock, adir);
933 afs_int32 afsconf_SawCell = 0;
936 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
937 char *aservice, struct afsconf_cell *acellInfo,
943 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
950 cell = (char *)&acellInfo->name;
952 code = afsconf_OpenInternal(adir, cell, clones);
956 #if !defined(AFS_NT40_ENV)
958 afsconf_LookupServer(const char *service, const char *protocol,
959 const char *cellName, unsigned short afsdbPort,
960 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
961 unsigned short ports[], unsigned short ipRanks[],
962 int *numServers, int *ttl, char **arealCellName)
966 unsigned char answer[4096];
968 char *dotcellname = NULL;
976 char *IANAname = (char *) afsconf_FindIANAName(service);
977 int tservice = afsconf_FindService(service);
983 if (tservice <= 0 || !IANAname)
984 return AFSCONF_NOTFOUND; /* service not found */
986 if (strchr(cellName,'.'))
989 #ifdef HAVE_RES_RETRANSRETRY
990 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
994 * Rx timeout is typically 56 seconds; limit user experience to
1005 asprintf(&dotcellname, "_%s._%s.%s.", IANAname, protocol, cellName);
1009 asprintf(&dotcellname, "%s.", cellName);
1013 asprintf(&dotcellname, "_%s._%s.%s", IANAname, protocol, cellName);
1017 asprintf(&dotcellname, "%s", cellName);
1020 if (dotcellname == NULL)
1021 goto findservererror;
1024 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1025 UNLOCK_GLOBAL_MUTEX;
1027 if (dotcellname != NULL) {
1042 code = AFSCONF_NOTFOUND;
1043 goto findservererror;
1047 p = answer + sizeof(HEADER); /* Skip header */
1048 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1050 code = AFSCONF_NOTFOUND;
1051 goto findservererror;
1054 p += code + QFIXEDSZ; /* Skip name */
1056 while (p < answer + len) {
1057 int type, ttl, size;
1059 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1061 code = AFSCONF_NOTFOUND;
1062 goto findservererror;
1065 p += code; /* Skip the name */
1066 type = (p[0] << 8) | p[1];
1067 p += 4; /* Skip type and class */
1068 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1069 p += 4; /* Skip the TTL */
1070 size = (p[0] << 8) | p[1];
1071 p += 2; /* Skip the size */
1073 if (type == T_AFSDB) {
1077 afsdb_type = (p[0] << 8) | p[1];
1078 if (afsdb_type == 1) {
1080 * We know this is an AFSDB record for our cell, of the
1081 * right AFSDB type. Write down the true cell name that
1082 * the resolver gave us above.
1085 realCellName = strdup(host);
1088 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1090 code = AFSCONF_NOTFOUND;
1091 goto findservererror;
1094 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1095 /* Do we want to get TTL data for the A record as well? */
1096 (he = gethostbyname(host))) {
1097 if (he->h_addrtype == AF_INET) {
1099 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1100 cellHostAddrs[server_num] = ipaddr;
1101 ports[server_num] = afsdbPort;
1102 ipRanks[server_num] = 0;
1103 strncpy(cellHostNames[server_num], host,
1104 sizeof(cellHostNames[server_num]));
1106 if (!minttl || ttl < minttl)
1111 if (type == T_SRV) {
1113 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1114 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1115 (strncmp(host + strlen(IANAname) + 3, protocol,
1116 strlen(protocol)) == 0)) {
1118 realCellName = strdup(host + strlen(IANAname) +
1119 strlen(protocol) + 4);
1122 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1124 code = AFSCONF_NOTFOUND;
1125 goto findservererror;
1128 if ((server_num < MAXHOSTSPERCELL) &&
1129 /* Do we want to get TTL data for the A record as well? */
1130 (he = gethostbyname(host))) {
1131 if (he->h_addrtype == AF_INET) {
1134 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1135 cellHostAddrs[server_num] = ipaddr;
1136 ipRanks[server_num] = (p[0] << 8) | p[1];
1137 ports[server_num] = htons((p[4] << 8) | p[5]);
1138 /* weight = (p[2] << 8) | p[3]; */
1139 strncpy(cellHostNames[server_num], host,
1140 sizeof(cellHostNames[server_num]));
1143 if (!minttl || ttl < minttl)
1152 if (server_num == 0) { /* No AFSDB or SRV records */
1153 code = AFSCONF_NOTFOUND;
1154 goto findservererror;
1158 /* Convert the real cell name to lowercase */
1159 for (p = (unsigned char *)realCellName; *p; p++)
1163 *numServers = server_num;
1164 *ttl = minttl ? (time(0) + minttl) : 0;
1166 if ( *numServers > 0 ) {
1168 *arealCellName = realCellName;
1170 code = AFSCONF_NOTFOUND;
1173 if (code && realCellName)
1179 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1180 struct afsconf_cell *acellInfo)
1182 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1183 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1184 unsigned short ipRanks[AFSMAXCELLHOSTS];
1185 unsigned short ports[AFSMAXCELLHOSTS];
1186 char *realCellName = NULL;
1187 int ttl, numServers, i;
1188 char *service = aservice;
1190 unsigned short afsdbport;
1192 service = "afs3-vlserver";
1193 afsdbport = htons(7003);
1196 afsdbport = afsconf_FindService(service);
1198 code = afsconf_LookupServer((const char *)service, "udp",
1199 (const char *)acellName, afsdbport,
1200 cellHostAddrs, cellHostNames,
1201 ports, ipRanks, &numServers, &ttl,
1204 /* If we couldn't find an entry for the requested service
1205 * and that service happens to be the prservice or kaservice
1206 * then fallback to searching for afs3-vlserver and assigning
1207 * the port number here. */
1208 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1209 code = afsconf_LookupServer("afs3-vlserver", "udp",
1210 (const char *)acellName, afsdbport,
1211 cellHostAddrs, cellHostNames,
1212 ports, ipRanks, &numServers, &ttl,
1215 for (i = 0; i < numServers; i++)
1216 ports[i] = afsdbport;
1220 acellInfo->timeout = ttl;
1221 acellInfo->numServers = numServers;
1222 for (i = 0; i < numServers; i++) {
1223 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1225 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1226 acellInfo->hostAddr[i].sin_family = AF_INET;
1227 acellInfo->hostAddr[i].sin_port = ports[i];
1230 strlcpy(acellInfo->name, realCellName,
1231 sizeof(acellInfo->name));
1233 realCellName = NULL;
1236 acellInfo->linkedCell = NULL; /* no linked cell */
1237 acellInfo->flags = 0;
1243 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1244 struct afsconf_cell *acellInfo)
1247 int tservice = afsconf_FindService(aservice); /* network byte order */
1248 const char *ianaName = afsconf_FindIANAName(aservice);
1249 struct afsconf_entry DNSce;
1250 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1251 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1252 unsigned short ipRanks[AFSMAXCELLHOSTS];
1253 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1260 return AFSCONF_NOTFOUND;
1262 tservice = 0; /* port will be assigned by caller */
1265 if (ianaName == NULL)
1266 ianaName = "afs3-vlserver";
1268 DNSce.cellInfo.numServers = 0;
1271 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1272 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1274 /* ignore the ttl here since this code is only called by transitory programs
1275 * like klog, etc. */
1277 /* If we couldn't find an entry for the requested service
1278 * and that service happens to be the prservice or kaservice
1279 * then fallback to searching for afs3-vlserver and assigning
1280 * the port number here. */
1281 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1282 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1283 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1286 for (i = 0; i < numServers; i++)
1287 ports[i] = tservice;
1291 if (rc < 0 || numServers == 0)
1294 for (i = 0; i < numServers; i++) {
1295 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1297 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1298 acellInfo->hostAddr[i].sin_family = AF_INET;
1300 acellInfo->hostAddr[i].sin_port = ports[i];
1302 acellInfo->hostAddr[i].sin_port = 0;
1305 acellInfo->numServers = numServers;
1306 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1307 acellInfo->linkedCell = NULL; /* no linked cell */
1308 acellInfo->flags = 0;
1311 #endif /* windows */
1314 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1315 struct afsconf_cell *acellInfo)
1317 struct afsconf_entry *tce;
1318 struct afsconf_aliasentry *tcae;
1319 struct afsconf_entry *bestce;
1329 _afsconf_Check(adir);
1332 cnLen = (int)(strlen(tcell) + 1);
1333 lcstring(tcell, tcell, cnLen);
1334 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1335 /* call to afsconf_GetLocalCell: like klog */
1337 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1339 UNLOCK_GLOBAL_MUTEX;
1344 cnLen = strlen(tcell);
1345 bestce = (struct afsconf_entry *)0;
1348 UNLOCK_GLOBAL_MUTEX;
1352 /* Look through the list of aliases */
1353 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1354 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1355 tcell = tcae->aliasInfo.realName;
1360 for (tce = adir->entries; tce; tce = tce->next) {
1361 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1362 /* found our cell */
1367 if (strlen(tce->cellInfo.name) < cnLen)
1368 continue; /* clearly wrong */
1369 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1371 ambig = 1; /* ambiguous unless we get exact match */
1375 if (!ambig && bestce && bestce->cellInfo.numServers) {
1376 *acellInfo = bestce->cellInfo; /* structure assignment */
1378 tservice = afsconf_FindService(aservice);
1380 UNLOCK_GLOBAL_MUTEX;
1381 return AFSCONF_NOTFOUND; /* service not found */
1383 for (i = 0; i < acellInfo->numServers; i++) {
1384 acellInfo->hostAddr[i].sin_port = tservice;
1387 acellInfo->timeout = 0;
1390 * Until we figure out how to separate out ubik server
1391 * queries from other server queries, only perform gethostbyname()
1392 * lookup on the specified hostnames for the client CellServDB files.
1394 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1395 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1397 short numServers=0; /*Num active servers for the cell */
1398 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1399 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1401 memset(&hostAddr, 0, sizeof(hostAddr));
1402 memset(&hostName, 0, sizeof(hostName));
1404 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1405 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1408 if (he && he->h_addrtype == AF_INET) {
1410 /* obtain all the valid address from the list */
1411 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1412 /* check to see if this is a new address; if so insert it into the list */
1414 for (k=0, dup=0; !dup && k < numServers; k++) {
1415 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1421 hostAddr[numServers].sin_family = AF_INET;
1422 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1423 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1424 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1426 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1427 strcpy(hostName[numServers], acellInfo->hostName[j]);
1433 hostAddr[numServers] = acellInfo->hostAddr[j];
1434 strcpy(hostName[numServers], acellInfo->hostName[j]);
1439 for (i=0; i<numServers; i++) {
1440 acellInfo->hostAddr[i] = hostAddr[i];
1441 strcpy(acellInfo->hostName[i], hostName[i]);
1443 acellInfo->numServers = numServers;
1444 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1446 UNLOCK_GLOBAL_MUTEX;
1449 UNLOCK_GLOBAL_MUTEX;
1450 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1455 * Get the current localcell name.
1457 * Internal function to get a pointer to the local cell name.
1458 * This function must be called with the global afsconf lock held.
1460 * @param[in] adir afsconf object
1461 * @param[out] aname address to a char pointer
1462 * @param[in] check always perform a config check, even if the
1463 * the AFSCELL name is set.
1467 * @retval AFSCONF_UNKNOWN failed to get cellname
1472 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1474 static int afsconf_showcell = 0;
1479 * If a cell switch was specified in a command, then it should override the
1480 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1481 * is set and the cell name in the adir structure is used.
1482 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1483 * Optionally, check the configuration, even if using the environment variable.
1485 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1487 _afsconf_Check(adir);
1489 if (!afsconf_showcell) {
1490 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1492 afsconf_showcell = 1;
1494 *pname = afscell_path;
1496 _afsconf_Check(adir);
1497 if (adir->cellName) {
1498 *pname = adir->cellName;
1500 code = AFSCONF_UNKNOWN;
1506 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1509 char *cellname = NULL;
1512 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1513 if (!code && cellname) {
1514 strlcpy(aname, cellname, alen);
1516 UNLOCK_GLOBAL_MUTEX;
1521 afsconf_Close(struct afsconf_dir *adir)
1524 afsconf_CloseInternal(adir);
1528 UNLOCK_GLOBAL_MUTEX;
1533 afsconf_CloseInternal(struct afsconf_dir *adir)
1535 struct afsconf_entry *td, *nd;
1536 struct afsconf_aliasentry *ta, *na;
1539 tname = adir->name; /* remember name, since that's all we preserve */
1541 /* free everything we can find */
1543 free(adir->cellName);
1544 for (td = adir->entries; td; td = nd) {
1546 if (td->cellInfo.linkedCell)
1547 free(td->cellInfo.linkedCell);
1550 for (ta = adir->alias_entries; ta; ta = na) {
1555 _afsconf_FreeAllKeys(adir);
1556 _afsconf_FreeRealms(adir);
1559 memset(adir, 0, sizeof(struct afsconf_dir));
1560 adir->name = tname; /* restore it */
1565 afsconf_Reopen(struct afsconf_dir *adir)
1568 code = afsconf_CloseInternal(adir);
1571 code = afsconf_OpenInternal(adir, 0, 0);