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);
77 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
78 char *aname, char *aclone /* boolean */);
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)
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 ts = (struct servent *) getservbyname(aname, NULL);
218 /* we found it in /etc/services, so we use this value */
219 return ts->s_port; /* already in network byte order */
222 /* not found in /etc/services, see if it is one of ours */
223 for (tsp = serviceTable; tsp->port; tsp++) {
224 if ((tsp->name && (!strcmp(tsp->name, aname)))
225 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
226 return htons(tsp->port);
232 afsconf_FindIANAName(const char *aname)
234 /* lookup a service name */
235 struct afsconf_servPair *tsp;
237 if (aname == NULL || aname[0] == '\0')
240 /* see if it is one of ours */
241 for (tsp = serviceTable; tsp->port; tsp++) {
242 if ((tsp->name && (!strcmp(tsp->name, aname)))
243 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
244 return tsp->ianaName;
250 TrimLine(char *abuffer, int abufsize)
262 strlcpy(tbuffer, tp, sizeof tbuffer);
263 strlcpy(abuffer, tbuffer, abufsize);
268 * IsClientConfigDirectory() -- determine if path matches well-known
269 * client configuration directory.
272 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
273 #else /* AFS_NT40_ENV */
274 #define IS_SEP(x) ((x) == '/')
275 #endif /* AFS_NT40_ENV */
277 _afsconf_IsClientConfigDirectory(const char *path)
279 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
282 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
284 cc = tolower(cdir[i]);
285 pc = tolower(path[i]);
293 #else /* AFS_NT40_ENV */
296 #endif /* AFS_NT40_ENV */
302 /* hit end of one or both; allow mismatch in existence of trailing slash */
303 if (cdir[i] != '\0') {
304 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
308 if (path[i] != '\0') {
309 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
318 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
322 /* NT client CellServDB has different file name than NT server or Unix */
323 if (_afsconf_IsClientConfigDirectory(adir->name)) {
324 if (!afssw_GetClientCellServDBDir(&p)) {
325 if (asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
329 if (asprintf(path, "%s/%s", adir->name,
330 AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
334 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
341 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
343 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
346 #endif /* AFS_NT40_ENV */
349 _afsconf_UpToDate(struct afsconf_dir *adir)
353 time_t now = time(0);
355 if (adir->timeRead && (adir->timeCheck == now)) {
356 return 1; /* stat no more than once a second */
358 adir->timeCheck = now;
360 if (adir->cellservDB == NULL)
363 code = stat(adir->cellservDB, &tstat);
365 return 0; /* Can't throw the error, so just say we're not up to date */
367 /* did file change? */
368 if (tstat.st_mtime == adir->timeRead)
371 /* otherwise file has changed */
376 afsconf_UpToDate(void *rock)
381 code = _afsconf_UpToDate(rock);
388 _afsconf_Check(struct afsconf_dir *adir)
390 /* did configuration change? */
391 if (_afsconf_UpToDate(adir))
394 /* otherwise file has changed, so reopen it */
395 return afsconf_Reopen(adir);
398 /* set modtime on file */
400 _afsconf_Touch(struct afsconf_dir *adir)
404 struct timeval tvp[2];
407 adir->timeRead = 0; /* just in case */
410 if (adir->cellservDB == NULL)
414 code = _utime(adir->cellservDB, NULL);
416 gettimeofday(&tvp[0], NULL);
418 code = utimes(adir->cellservDB, tvp);
419 #endif /* AFS_NT40_ENV */
425 afsconf_Open(const char *adir)
427 struct afsconf_dir *tdir;
431 /* zero structure and fill in name; rest is done by internal routine */
432 tdir = calloc(1, sizeof(struct afsconf_dir));
433 tdir->name = strdup(adir);
435 code = afsconf_OpenInternal(tdir);
437 char *afsconf_path, afs_confdir[128];
440 /* Check global place only when local Open failed for whatever reason */
441 if (!(afsconf_path = getenv("AFSCONF"))) {
442 /* 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... */
448 if (!(home_dir = getenv("HOME"))) {
449 /* Our last chance is the "/.AFSCONF" file */
450 fp = fopen("/.AFSCONF", "r");
455 char *pathname = NULL;
457 r = asprintf(&pathname, "%s/%s", home_dir, ".AFSCONF");
458 if (r < 0 || pathname == NULL)
461 fp = fopen(pathname, "r");
465 /* Our last chance is the "/.AFSCONF" file */
466 fp = fopen("/.AFSCONF", "r");
471 if (fgets(afs_confdir, 128, fp) != NULL)
472 len = strlen(afs_confdir);
477 if (afs_confdir[len - 1] == '\n') {
478 afs_confdir[len - 1] = 0;
480 afsconf_path = afs_confdir;
482 tdir->name = strdup(afsconf_path);
483 code = afsconf_OpenInternal(tdir);
499 GetCellUnix(struct afsconf_dir *adir)
506 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE,
508 fp = fopen(tbuffer, "r");
512 rc = fgets(tbuffer, 256, fp);
518 while (*start != '\0' && isspace(*start))
521 while (*p != '\0' && !isspace(*p))
527 adir->cellName = strdup(start);
534 GetCellNT(struct afsconf_dir *adir)
536 if (_afsconf_IsClientConfigDirectory(adir->name)) {
537 /* NT client config dir; ThisCell is in registry (no file). */
538 return afssw_GetClientCellName(&adir->cellName);
540 /* NT server config dir; works just like Unix */
541 return GetCellUnix(adir);
545 /* The following procedures and structs are used on Windows only
546 * to enumerate the Cell information distributed within the
547 * Windows registry. (See src/WINNT/afsd/cm_config.c)
549 typedef struct _cm_enumCellRegistry {
550 afs_uint32 client; /* non-zero if client query */
551 struct afsconf_dir *adir;
552 } cm_enumCellRegistry_t;
555 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
556 char *hostNamep, unsigned short rank)
558 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
560 if (cellInfop->numServers == MAXHOSTSPERCELL)
563 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
564 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
565 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
566 cellInfop->numServers++;
572 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
575 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
576 char linkedName[256] = "";
578 struct afsconf_entry *newEntry;
581 newEntry = malloc(sizeof(struct afsconf_entry));
582 if (newEntry == NULL)
584 newEntry->cellInfo.numServers = 0;
586 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
587 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
588 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
591 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
592 newEntry->cellInfo.name[MAXCELLCHARS-1];
594 newEntry->cellInfo.linkedCell = strdup(linkedName);
596 newEntry->cellInfo.linkedCell = NULL;
597 newEntry->cellInfo.timeout = timeout;
598 newEntry->cellInfo.flags = 0;
600 newEntry->next = enump->adir->entries;
601 enump->adir->entries = newEntry;
607 #endif /* AFS_NT40_ENV */
611 afsconf_OpenInternal(struct afsconf_dir *adir)
615 struct afsconf_entry *curEntry;
616 struct afsconf_aliasentry *curAlias;
623 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
624 #endif /* AFS_NT40_ENV */
626 /* init the keys queue before any call to afsconf_CloseInternal() */
627 _afsconf_InitKeys(adir);
629 /* figure out the local cell name */
632 enumCellRegistry.adir = adir;
634 i = GetCellUnix(adir);
637 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
643 /* now parse the individual lines */
646 _afsconf_CellServDBPath(adir, &adir->cellservDB);
649 if (_afsconf_IsClientConfigDirectory(adir->name))
650 enumCellRegistry.client = 1;
651 #endif /* AFS_NT40_ENV */
653 if (!stat(adir->cellservDB, &tstat)) {
654 adir->timeRead = tstat.st_mtime;
659 tf = fopen(adir->cellservDB, "r");
664 /* The CellServDB file is now open.
665 * The following code parses the contents of the
666 * file and creates a list with the first cell entry
667 * in the CellServDB file at the end of the list.
669 * No checking is performed for duplicates.
670 * The side effects of this process are that duplicate
671 * entries appended to the end of the CellServDB file
672 * take precedence and are found in a shorter period
677 tp = fgets(tbuffer, sizeof(tbuffer), tf);
680 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
681 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
682 continue; /* empty line */
683 if (tbuffer[0] == '>') {
684 char linkedcell[MAXCELLCHARS];
685 /* start new cell item */
687 /* thread this guy on the list */
688 curEntry->next = adir->entries;
689 adir->entries = curEntry;
692 curEntry = calloc(1, sizeof(struct afsconf_entry));
694 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
696 afsconf_CloseInternal(adir);
701 if (linkedcell[0] != '\0')
702 curEntry->cellInfo.linkedCell = strdup(linkedcell);
704 /* new host in the current cell */
706 afsconf_CloseInternal(adir);
710 i = curEntry->cellInfo.numServers;
711 if (i < MAXHOSTSPERCELL) {
712 code = ParseHostLine(tbuffer,
713 &curEntry->cellInfo.hostAddr[i],
714 curEntry->cellInfo.hostName[i],
715 &curEntry->cellInfo.clone[i]);
717 if (code == AFSCONF_SYNTAX) {
718 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
724 "Can't properly parse host line \"%s\" in configuration file %s\n",
725 tbuffer, adir->cellservDB);
729 afsconf_CloseInternal(adir);
732 curEntry->cellInfo.numServers = ++i;
735 "Too many hosts for cell %s in configuration file %s\n",
736 curEntry->cellInfo.name, adir->cellservDB);
740 fclose(tf); /* close the file now */
742 /* end the last partially-completed cell */
744 curEntry->next = adir->entries;
745 adir->entries = curEntry;
750 * Windows maintains a CellServDB list in the Registry
751 * that supercedes the contents of the CellServDB file.
752 * Prepending these entries to the head of the list
753 * is sufficient to enforce the precedence.
755 cm_EnumerateCellRegistry( enumCellRegistry.client,
756 cm_enumCellRegistryProc,
758 #endif /* AFS_NT40_ENV */
760 /* Read in the alias list */
761 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE,
764 tf = fopen(tbuffer, "r");
768 tp = fgets(tbuffer, sizeof(tbuffer), tf);
771 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
773 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
774 continue; /* empty line */
777 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
780 continue; /* invalid line */
782 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
785 continue; /* invalid line */
788 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
793 curAlias = calloc(1, sizeof(*curAlias));
795 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
796 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
798 curAlias->next = adir->alias_entries;
799 adir->alias_entries = curAlias;
805 /* now read the fs keys, if possible */
806 code = _afsconf_LoadKeys(adir);
810 code = _afsconf_LoadRealms(adir);
815 /* parse a line of the form
816 *"128.2.1.3 #hostname" or
817 *"[128.2.1.3] #hostname" for clones
818 * into the appropriate pieces.
821 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
822 char *aclone /* boolean */)
832 /* FIXME: length of aname unknown here */
833 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c[0], &c[1], &c[2], &c[3],
838 /* FIXME: length of aname unknown here */
839 code = sscanf(aline, "%d.%d.%d.%d #%s", &c[0], &c[1], &c[2], &c[3],
843 return AFSCONF_SYNTAX;
844 for(i = 0; i < 4; ++i) {
845 if (c[i] < 0 || c[i] > 255) {
846 fprintf(stderr, "Illegal IP address %d.%d.%d.%d\n", c[0], c[1],
848 return AFSCONF_SYNTAX;
851 addr->sin_family = AF_INET;
853 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
854 addr->sin_len = sizeof(struct sockaddr_in);
856 tp = (char *)&addr->sin_addr;
864 /* parse a line of the form
865 * ">cellname [linkedcellname] [#comments]"
866 * into the appropriate pieces.
869 ParseCellLine(char *aline, char *aname,
873 /* FIXME: length of aname, alname unknown here */
874 code = sscanf(aline, ">%s %s", aname, alname);
878 if (*alname == '#') {
882 return (code > 0 ? 0 : AFSCONF_SYNTAX);
885 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
887 afsconf_CellApply(struct afsconf_dir *adir,
888 int (*aproc) (struct afsconf_cell * cell, void *arock,
889 struct afsconf_dir * dir), void *arock)
891 struct afsconf_entry *tde;
894 for (tde = adir->entries; tde; tde = tde->next) {
895 code = (*aproc) (&tde->cellInfo, arock, adir);
905 /* call aproc(entry, arock, adir) for all cell aliases.
906 * Proc must return 0, or we'll stop early and return the code it returns
909 afsconf_CellAliasApply(struct afsconf_dir *adir,
910 int (*aproc) (struct afsconf_cellalias * alias,
911 void *arock, struct afsconf_dir * dir),
914 struct afsconf_aliasentry *tde;
917 for (tde = adir->alias_entries; tde; tde = tde->next) {
918 code = (*aproc) (&tde->aliasInfo, arock, adir);
928 afs_int32 afsconf_SawCell = 0;
931 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
932 char *aservice, struct afsconf_cell *acellInfo,
938 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
942 for (i = 0; i < acellInfo->numServers; i++) {
943 clones[i] = acellInfo->clone[i];
948 #if !defined(AFS_NT40_ENV)
950 afsconf_LookupServer(const char *service, const char *protocol,
951 const char *cellName, unsigned short afsdbPort,
952 afs_uint32 *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
953 unsigned short ports[], unsigned short ipRanks[],
954 int *numServers, int *ttl, char **arealCellName)
959 unsigned char answer[4096];
961 char *dotcellname = NULL;
969 char *IANAname = (char *) afsconf_FindIANAName(service);
970 int tservice = afsconf_FindService(service);
976 if (tservice <= 0 || !IANAname)
977 return AFSCONF_NOTFOUND; /* service not found */
979 if (strchr(cellName,'.'))
982 #ifdef HAVE_RES_RETRANSRETRY
983 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
987 * Rx timeout is typically 56 seconds; limit user experience to
999 r = asprintf(&dotcellname, "_%s._%s.%s.", IANAname, protocol, cellName);
1003 r = asprintf(&dotcellname, "%s.", cellName);
1007 r = asprintf(&dotcellname, "_%s._%s.%s", IANAname, protocol, cellName);
1011 r = asprintf(&dotcellname, "%s", cellName);
1014 if (r < 0 || dotcellname == NULL)
1015 goto findservererror;
1018 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1019 UNLOCK_GLOBAL_MUTEX;
1021 if (dotcellname != NULL) {
1036 code = AFSCONF_NOTFOUND;
1037 goto findservererror;
1041 p = answer + sizeof(HEADER); /* Skip header */
1042 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1044 code = AFSCONF_NOTFOUND;
1045 goto findservererror;
1048 p += code + QFIXEDSZ; /* Skip name */
1050 while (p < answer + len) {
1051 int type, ttl, size;
1053 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1055 code = AFSCONF_NOTFOUND;
1056 goto findservererror;
1059 p += code; /* Skip the name */
1060 type = (p[0] << 8) | p[1];
1061 p += 4; /* Skip type and class */
1062 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1063 p += 4; /* Skip the TTL */
1064 size = (p[0] << 8) | p[1];
1065 p += 2; /* Skip the size */
1067 if (type == T_AFSDB) {
1071 afsdb_type = (p[0] << 8) | p[1];
1072 if (afsdb_type == 1) {
1074 * We know this is an AFSDB record for our cell, of the
1075 * right AFSDB type. Write down the true cell name that
1076 * the resolver gave us above.
1079 realCellName = strdup(host);
1082 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1084 code = AFSCONF_NOTFOUND;
1085 goto findservererror;
1088 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1089 /* Do we want to get TTL data for the A record as well? */
1090 (he = gethostbyname(host))) {
1091 if (he->h_addrtype == AF_INET) {
1093 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
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]));
1100 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))) {
1125 if (he->h_addrtype == AF_INET) {
1128 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1129 cellHostAddrs[server_num] = ipaddr;
1130 ipRanks[server_num] = (p[0] << 8) | p[1];
1131 ports[server_num] = htons((p[4] << 8) | p[5]);
1132 /* weight = (p[2] << 8) | p[3]; */
1133 strncpy(cellHostNames[server_num], host,
1134 sizeof(cellHostNames[server_num]));
1137 if (!minttl || ttl < minttl)
1146 if (server_num == 0) { /* No AFSDB or SRV records */
1147 code = AFSCONF_NOTFOUND;
1148 goto findservererror;
1152 /* Convert the real cell name to lowercase */
1153 for (p = (unsigned char *)realCellName; *p; p++)
1157 *numServers = server_num;
1158 *ttl = minttl ? (time(0) + minttl) : 0;
1160 if ( *numServers > 0 ) {
1162 *arealCellName = realCellName;
1164 code = AFSCONF_NOTFOUND;
1167 if (code && realCellName)
1173 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1174 struct afsconf_cell *acellInfo)
1176 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1177 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1178 unsigned short ipRanks[AFSMAXCELLHOSTS];
1179 unsigned short ports[AFSMAXCELLHOSTS];
1180 char *realCellName = NULL;
1181 int ttl, numServers, i;
1182 char *service = aservice;
1184 unsigned short afsdbport;
1186 service = "afs3-vlserver";
1187 afsdbport = htons(7003);
1190 afsdbport = afsconf_FindService(service);
1192 code = afsconf_LookupServer((const char *)service, "udp",
1193 (const char *)acellName, afsdbport,
1194 cellHostAddrs, cellHostNames,
1195 ports, ipRanks, &numServers, &ttl,
1198 /* If we couldn't find an entry for the requested service
1199 * and that service happens to be the prservice or kaservice
1200 * then fallback to searching for afs3-vlserver and assigning
1201 * the port number here. */
1202 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1203 code = afsconf_LookupServer("afs3-vlserver", "udp",
1204 (const char *)acellName, afsdbport,
1205 cellHostAddrs, cellHostNames,
1206 ports, ipRanks, &numServers, &ttl,
1209 for (i = 0; i < numServers; i++)
1210 ports[i] = afsdbport;
1214 acellInfo->timeout = ttl;
1215 acellInfo->numServers = numServers;
1216 for (i = 0; i < numServers; i++) {
1217 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1219 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1220 acellInfo->hostAddr[i].sin_family = AF_INET;
1221 acellInfo->hostAddr[i].sin_port = ports[i];
1224 strlcpy(acellInfo->name, realCellName,
1225 sizeof(acellInfo->name));
1227 realCellName = NULL;
1230 acellInfo->linkedCell = NULL; /* no linked cell */
1231 acellInfo->flags = 0;
1237 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1238 struct afsconf_cell *acellInfo)
1241 int tservice = afsconf_FindService(aservice); /* network byte order */
1242 const char *ianaName = afsconf_FindIANAName(aservice);
1243 struct afsconf_entry DNSce;
1244 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1245 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1246 unsigned short ipRanks[AFSMAXCELLHOSTS];
1247 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1254 return AFSCONF_NOTFOUND;
1256 tservice = 0; /* port will be assigned by caller */
1259 if (ianaName == NULL)
1260 ianaName = "afs3-vlserver";
1262 DNSce.cellInfo.numServers = 0;
1265 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1266 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1268 /* ignore the ttl here since this code is only called by transitory programs
1269 * like klog, etc. */
1271 /* If we couldn't find an entry for the requested service
1272 * and that service happens to be the prservice or kaservice
1273 * then fallback to searching for afs3-vlserver and assigning
1274 * the port number here. */
1275 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1276 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1277 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1280 for (i = 0; i < numServers; i++)
1281 ports[i] = tservice;
1285 if (rc < 0 || numServers == 0)
1288 for (i = 0; i < numServers; i++) {
1289 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1290 sizeof(afs_uint32));
1291 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1292 acellInfo->hostAddr[i].sin_family = AF_INET;
1294 acellInfo->hostAddr[i].sin_port = ports[i];
1296 acellInfo->hostAddr[i].sin_port = 0;
1299 acellInfo->numServers = numServers;
1300 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1301 acellInfo->linkedCell = NULL; /* no linked cell */
1302 acellInfo->flags = 0;
1305 #endif /* windows */
1308 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1309 struct afsconf_cell *acellInfo)
1311 struct afsconf_entry *tce;
1312 struct afsconf_aliasentry *tcae;
1313 struct afsconf_entry *bestce;
1323 _afsconf_Check(adir);
1326 cnLen = (int)(strlen(tcell) + 1);
1327 lcstring(tcell, tcell, cnLen);
1328 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1329 /* call to afsconf_GetLocalCell: like klog */
1331 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1333 UNLOCK_GLOBAL_MUTEX;
1338 cnLen = strlen(tcell);
1339 bestce = (struct afsconf_entry *)0;
1342 UNLOCK_GLOBAL_MUTEX;
1346 /* Look through the list of aliases */
1347 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1348 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1349 tcell = tcae->aliasInfo.realName;
1354 for (tce = adir->entries; tce; tce = tce->next) {
1355 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1356 /* found our cell */
1361 if (strlen(tce->cellInfo.name) < cnLen)
1362 continue; /* clearly wrong */
1363 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1365 ambig = 1; /* ambiguous unless we get exact match */
1369 if (!ambig && bestce && bestce->cellInfo.numServers) {
1370 *acellInfo = bestce->cellInfo; /* structure assignment */
1372 tservice = afsconf_FindService(aservice);
1374 UNLOCK_GLOBAL_MUTEX;
1375 return AFSCONF_NOTFOUND; /* service not found */
1377 for (i = 0; i < acellInfo->numServers; i++) {
1378 acellInfo->hostAddr[i].sin_port = tservice;
1381 acellInfo->timeout = 0;
1384 * Until we figure out how to separate out ubik server
1385 * queries from other server queries, only perform gethostbyname()
1386 * lookup on the specified hostnames for the client CellServDB files.
1388 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1389 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1391 short numServers=0; /*Num active servers for the cell */
1392 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1393 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1394 char clone[MAXHOSTSPERCELL]; /*Indicates which ones are clones. */
1396 memset(&hostAddr, 0, sizeof(hostAddr));
1397 memset(&hostName, 0, sizeof(hostName));
1398 memset(&clone, 0, sizeof(clone));
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 */
1411 memcpy(&addr, he->h_addr_list[i], sizeof(addr));
1412 for (k=0, dup=0; !dup && k < numServers; k++) {
1413 if (hostAddr[k].sin_addr.s_addr == addr) {
1420 hostAddr[numServers].sin_family = AF_INET;
1421 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1422 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1423 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1425 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(afs_uint32));
1426 strcpy(hostName[numServers], acellInfo->hostName[j]);
1427 clone[numServers] = acellInfo->clone[j];
1433 hostAddr[numServers] = acellInfo->hostAddr[j];
1434 strcpy(hostName[numServers], acellInfo->hostName[j]);
1435 clone[numServers] = acellInfo->clone[j];
1440 for (i=0; i<numServers; i++) {
1441 acellInfo->hostAddr[i] = hostAddr[i];
1442 strcpy(acellInfo->hostName[i], hostName[i]);
1443 acellInfo->clone[i] = clone[i];
1445 acellInfo->numServers = numServers;
1446 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1448 UNLOCK_GLOBAL_MUTEX;
1451 UNLOCK_GLOBAL_MUTEX;
1452 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1457 * Get the current localcell name.
1459 * Internal function to get a pointer to the local cell name.
1460 * This function must be called with the global afsconf lock held.
1462 * @param[in] adir afsconf object
1463 * @param[out] aname address to a char pointer
1464 * @param[in] check always perform a config check, even if the
1465 * the AFSCELL name is set.
1469 * @retval AFSCONF_NOCELLNAME cannot determine local cell name
1474 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1476 static int afsconf_showcell = 0;
1481 * If a cell switch was specified in a command, then it should override the
1482 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1483 * is set and the cell name in the adir structure is used.
1484 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1485 * Optionally, check the configuration, even if using the environment variable.
1487 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1489 _afsconf_Check(adir);
1491 if (!afsconf_showcell) {
1492 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1494 afsconf_showcell = 1;
1496 *pname = afscell_path;
1498 _afsconf_Check(adir);
1499 if (adir->cellName) {
1500 *pname = adir->cellName;
1502 code = AFSCONF_NOCELLNAME;
1508 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1511 char *cellname = NULL;
1514 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1515 if (!code && cellname) {
1516 strlcpy(aname, cellname, alen);
1518 UNLOCK_GLOBAL_MUTEX;
1523 afsconf_Close(struct afsconf_dir *adir)
1530 afsconf_CloseInternal(adir);
1534 UNLOCK_GLOBAL_MUTEX;
1539 afsconf_CloseInternal(struct afsconf_dir *adir)
1541 struct afsconf_entry *td, *nd;
1542 struct afsconf_aliasentry *ta, *na;
1549 tname = adir->name; /* remember name, since that's all we preserve */
1551 /* free everything we can find */
1553 free(adir->cellName);
1554 if (adir->cellservDB)
1555 free(adir->cellservDB);
1556 for (td = adir->entries; td; td = nd) {
1558 if (td->cellInfo.linkedCell)
1559 free(td->cellInfo.linkedCell);
1562 for (ta = adir->alias_entries; ta; ta = na) {
1567 _afsconf_FreeAllKeys(adir);
1568 _afsconf_FreeRealms(adir);
1571 memset(adir, 0, sizeof(struct afsconf_dir));
1572 adir->name = tname; /* restore it */
1577 afsconf_Reopen(struct afsconf_dir *adir)
1580 code = afsconf_CloseInternal(adir);
1583 code = afsconf_OpenInternal(adir);