2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #include <sys/utime.h>
19 #include <WINNT/afssw.h>
24 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
25 #include <arpa/nameser_compat.h>
28 #include <afs/pthread_glock.h>
29 #include <afs/afsint.h>
33 #include <afs/afsutil.h>
35 #include "cellconfig.h"
40 #include <cm_config.h>
41 /* cm_dns.h depends on cellconfig.h */
48 struct afsconf_servPair {
54 static struct afsconf_servPair serviceTable[] = {
55 {"afs", "afs3-fileserver", 7000,},
56 {"afscb", "afs3-callback", 7001,},
57 {"afsprot", "afs3-prserver", 7002,},
58 {"afsvldb", "afs3-vlserver", 7003,},
59 {"afskauth", "afs3-kaserver", 7004,},
60 {"afsvol", "afs3-volserver", 7005,},
61 {"afserror", "afs3-errors", 7006,},
62 {"afsnanny", "afs3-bos", 7007,},
63 {"afsupdate", "afs3-update", 7008,},
64 {"afsrmtsys", "afs3-rmtsys", 7009,},
65 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
66 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
67 {0, 0, 0} /* insert new services before this spot */
71 static int TrimLine(char *abuffer, int abufsize);
73 static int GetCellNT(struct afsconf_dir *adir);
75 static int GetCellUnix(struct afsconf_dir *adir);
76 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
78 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
79 char *aname, char *aclone);
80 static int ParseCellLine(char *aline, char *aname,
82 static int afsconf_CloseInternal(struct afsconf_dir *adir);
83 static int afsconf_Reopen(struct afsconf_dir *adir);
86 #define T_AFSDB 18 /* per RFC1183 section 1 */
89 #define T_SRV 33 /* RFC2782 */
93 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
94 * our code can tell if there is new info in the key files, the cell server db
95 * files or any of the other files (and reopen the thing) if the date on
99 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
100 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
101 get an fd <= 255. We allow the fileserver to claim more fds than that.
102 This has always been a problem since pr_Initialize would have the same
103 issue, but hpr_Initialize makes it more likely that we would see this.
104 Work around it. This is not generic. It's coded with the needs of
105 afsconf_* in mind only.
107 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
112 struct afsconf_iobuffer {
119 typedef struct afsconf_iobuffer afsconf_FILE;
121 static afsconf_FILE *
122 afsconf_fopen(const char *fname, const char *fmode)
127 if ((fd = open(fname, O_RDONLY)) == -1) {
131 iop = malloc(sizeof(struct afsconf_iobuffer));
138 iop->buffer = malloc(BUFFER);
139 if (iop->buffer == NULL) {
145 iop->ptr = iop->buffer;
146 iop->endptr = iop->buffer;
151 afsconf_fclose(afsconf_FILE *iop)
163 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
171 if (iop->ptr == iop->endptr) {
174 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
184 iop->ptr = iop->buffer;
185 iop->endptr = iop->buffer + len;
189 if ((p - s) == (n - 1)) {
199 #define fopen afsconf_fopen
200 #define fclose afsconf_fclose
201 #define fgets afsconf_fgets
203 #define afsconf_FILE FILE
204 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
206 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
208 afsconf_FindService(const char *aname)
210 /* lookup a service name */
212 struct afsconf_servPair *tsp;
214 if (aname == NULL || aname[0] == '\0')
217 ts = (struct servent *) getservbyname(aname, NULL);
219 /* we found it in /etc/services, so we use this value */
220 return ts->s_port; /* already in network byte order */
223 /* not found in /etc/services, see if it is one of ours */
224 for (tsp = serviceTable; tsp->port; tsp++) {
225 if ((tsp->name && (!strcmp(tsp->name, aname)))
226 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
227 return htons(tsp->port);
233 afsconf_FindIANAName(const char *aname)
235 /* lookup a service name */
236 struct afsconf_servPair *tsp;
238 if (aname == NULL || aname[0] == '\0')
241 /* see if it is one of ours */
242 for (tsp = serviceTable; tsp->port; tsp++) {
243 if ((tsp->name && (!strcmp(tsp->name, aname)))
244 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
245 return tsp->ianaName;
251 TrimLine(char *abuffer, int abufsize)
263 strlcpy(tbuffer, tp, sizeof tbuffer);
264 strlcpy(abuffer, tbuffer, abufsize);
269 * IsClientConfigDirectory() -- determine if path matches well-known
270 * client configuration directory.
273 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
274 #else /* AFS_NT40_ENV */
275 #define IS_SEP(x) ((x) == '/')
276 #endif /* AFS_NT40_ENV */
278 _afsconf_IsClientConfigDirectory(const char *path)
280 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
283 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
285 cc = tolower(cdir[i]);
286 pc = tolower(path[i]);
294 #else /* AFS_NT40_ENV */
297 #endif /* AFS_NT40_ENV */
303 /* hit end of one or both; allow mismatch in existence of trailing slash */
304 if (cdir[i] != '\0') {
305 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
309 if (path[i] != '\0') {
310 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
319 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
323 /* NT client CellServDB has different file name than NT server or Unix */
324 if (_afsconf_IsClientConfigDirectory(adir->name)) {
325 if (!afssw_GetClientCellServDBDir(&p)) {
326 if (asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
330 if (asprintf(path, "%s/%s", adir->name,
331 AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
335 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
342 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
344 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
347 #endif /* AFS_NT40_ENV */
350 _afsconf_UpToDate(struct afsconf_dir *adir)
355 time_t now = time(0);
357 if (adir->timeRead && (adir->timeCheck == now)) {
358 return 1; /* stat no more than once a second */
360 adir->timeCheck = now;
362 _afsconf_CellServDBPath(adir, &cellservDB);
363 if (cellservDB == NULL)
366 code = stat(cellservDB, &tstat);
369 return 0; /* Can't throw the error, so just say we're not up to date */
371 /* did file change? */
372 if (tstat.st_mtime == adir->timeRead)
375 /* otherwise file has changed */
380 afsconf_UpToDate(void *rock)
385 code = _afsconf_UpToDate(rock);
392 _afsconf_Check(struct afsconf_dir *adir)
394 /* did configuration change? */
395 if (_afsconf_UpToDate(adir))
398 /* otherwise file has changed, so reopen it */
399 return afsconf_Reopen(adir);
402 /* set modtime on file */
404 _afsconf_Touch(struct afsconf_dir *adir)
409 struct timeval tvp[2];
412 adir->timeRead = 0; /* just in case */
415 _afsconf_CellServDBPath(adir, &cellservDB);
416 if (cellservDB == NULL)
420 code = _utime(cellservDB, NULL);
422 gettimeofday(&tvp[0], NULL);
424 code = utimes(cellservDB, tvp);
425 #endif /* AFS_NT40_ENV */
432 afsconf_Open(const char *adir)
434 struct afsconf_dir *tdir;
438 /* zero structure and fill in name; rest is done by internal routine */
439 tdir = calloc(1, sizeof(struct afsconf_dir));
440 tdir->name = strdup(adir);
442 code = afsconf_OpenInternal(tdir, 0, 0);
444 char *afsconf_path, afs_confdir[128];
447 /* Check global place only when local Open failed for whatever reason */
448 if (!(afsconf_path = getenv("AFSCONF"))) {
449 /* 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... */
455 if (!(home_dir = getenv("HOME"))) {
456 /* Our last chance is the "/.AFSCONF" file */
457 fp = fopen("/.AFSCONF", "r");
462 char *pathname = NULL;
464 r = asprintf(&pathname, "%s/%s", home_dir, ".AFSCONF");
465 if (r < 0 || pathname == NULL)
468 fp = fopen(pathname, "r");
472 /* Our last chance is the "/.AFSCONF" file */
473 fp = fopen("/.AFSCONF", "r");
478 if (fgets(afs_confdir, 128, fp) != NULL)
479 len = strlen(afs_confdir);
484 if (afs_confdir[len - 1] == '\n') {
485 afs_confdir[len - 1] = 0;
487 afsconf_path = afs_confdir;
489 tdir->name = strdup(afsconf_path);
490 code = afsconf_OpenInternal(tdir, 0, 0);
506 GetCellUnix(struct afsconf_dir *adir)
513 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE,
515 fp = fopen(tbuffer, "r");
519 rc = fgets(tbuffer, 256, fp);
525 while (*start != '\0' && isspace(*start))
528 while (*p != '\0' && !isspace(*p))
534 adir->cellName = strdup(start);
541 GetCellNT(struct afsconf_dir *adir)
543 if (_afsconf_IsClientConfigDirectory(adir->name)) {
544 /* NT client config dir; ThisCell is in registry (no file). */
545 return afssw_GetClientCellName(&adir->cellName);
547 /* NT server config dir; works just like Unix */
548 return GetCellUnix(adir);
552 /* The following procedures and structs are used on Windows only
553 * to enumerate the Cell information distributed within the
554 * Windows registry. (See src/WINNT/afsd/cm_config.c)
556 typedef struct _cm_enumCellRegistry {
557 afs_uint32 client; /* non-zero if client query */
558 struct afsconf_dir *adir;
559 } cm_enumCellRegistry_t;
562 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
563 char *hostNamep, unsigned short rank)
565 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
567 if (cellInfop->numServers == MAXHOSTSPERCELL)
570 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
571 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
572 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
573 cellInfop->numServers++;
579 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
582 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
583 char linkedName[256] = "";
585 struct afsconf_entry *newEntry;
588 newEntry = malloc(sizeof(struct afsconf_entry));
589 if (newEntry == NULL)
591 newEntry->cellInfo.numServers = 0;
593 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
594 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
595 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
598 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
599 newEntry->cellInfo.name[MAXCELLCHARS-1];
601 newEntry->cellInfo.linkedCell = strdup(linkedName);
603 newEntry->cellInfo.linkedCell = NULL;
604 newEntry->cellInfo.timeout = timeout;
605 newEntry->cellInfo.flags = 0;
607 newEntry->next = enump->adir->entries;
608 enump->adir->entries = newEntry;
614 #endif /* AFS_NT40_ENV */
618 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
623 struct afsconf_entry *curEntry;
624 struct afsconf_aliasentry *curAlias;
632 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
633 #endif /* AFS_NT40_ENV */
635 /* init the keys queue before any call to afsconf_CloseInternal() */
636 _afsconf_InitKeys(adir);
638 /* figure out the local cell name */
641 enumCellRegistry.adir = adir;
643 i = GetCellUnix(adir);
646 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
652 /* now parse the individual lines */
655 _afsconf_CellServDBPath(adir, &cellservDB);
658 if (_afsconf_IsClientConfigDirectory(adir->name))
659 enumCellRegistry.client = 1;
660 #endif /* AFS_NT40_ENV */
662 if (!stat(cellservDB, &tstat)) {
663 adir->timeRead = tstat.st_mtime;
668 tf = fopen(cellservDB, "r");
673 /* The CellServDB file is now open.
674 * The following code parses the contents of the
675 * file and creates a list with the first cell entry
676 * in the CellServDB file at the end of the list.
678 * No checking is performed for duplicates.
679 * The side effects of this process are that duplicate
680 * entries appended to the end of the CellServDB file
681 * take precedence and are found in a shorter period
686 tp = fgets(tbuffer, sizeof(tbuffer), tf);
689 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
690 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
691 continue; /* empty line */
692 if (tbuffer[0] == '>') {
693 char linkedcell[MAXCELLCHARS];
694 /* start new cell item */
696 /* thread this guy on the list */
697 curEntry->next = adir->entries;
698 adir->entries = curEntry;
701 curEntry = calloc(1, sizeof(struct afsconf_entry));
703 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
705 afsconf_CloseInternal(adir);
710 if (linkedcell[0] != '\0')
711 curEntry->cellInfo.linkedCell = strdup(linkedcell);
713 /* new host in the current cell */
715 afsconf_CloseInternal(adir);
719 i = curEntry->cellInfo.numServers;
720 if (i < MAXHOSTSPERCELL) {
721 if (cell && !strcmp(cell, curEntry->cellInfo.name))
723 ParseHostLine(tbuffer,
724 &curEntry->cellInfo.hostAddr[i],
725 curEntry->cellInfo.hostName[i],
729 ParseHostLine(tbuffer,
730 &curEntry->cellInfo.hostAddr[i],
731 curEntry->cellInfo.hostName[i], 0);
734 if (code == AFSCONF_SYNTAX) {
735 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
741 "Can't properly parse host line \"%s\" in configuration file %s\n",
742 tbuffer, cellservDB);
746 afsconf_CloseInternal(adir);
749 curEntry->cellInfo.numServers = ++i;
752 "Too many hosts for cell %s in configuration file %s\n",
753 curEntry->cellInfo.name, cellservDB);
757 fclose(tf); /* close the file now */
760 /* end the last partially-completed cell */
762 curEntry->next = adir->entries;
763 adir->entries = curEntry;
768 * Windows maintains a CellServDB list in the Registry
769 * that supercedes the contents of the CellServDB file.
770 * Prepending these entries to the head of the list
771 * is sufficient to enforce the precedence.
773 cm_EnumerateCellRegistry( enumCellRegistry.client,
774 cm_enumCellRegistryProc,
776 #endif /* AFS_NT40_ENV */
778 /* Read in the alias list */
779 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE,
782 tf = fopen(tbuffer, "r");
786 tp = fgets(tbuffer, sizeof(tbuffer), tf);
789 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
791 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
792 continue; /* empty line */
795 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
798 continue; /* invalid line */
800 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
803 continue; /* invalid line */
806 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
811 curAlias = calloc(1, sizeof(*curAlias));
813 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
814 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
816 curAlias->next = adir->alias_entries;
817 adir->alias_entries = curAlias;
823 /* now read the fs keys, if possible */
824 code = _afsconf_LoadKeys(adir);
828 code = _afsconf_LoadRealms(adir);
833 /* parse a line of the form
834 *"128.2.1.3 #hostname" or
835 *"[128.2.1.3] #hostname" for clones
836 * into the appropriate pieces.
839 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
850 /* FIXME: length of aname unknown here */
851 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c[0], &c[1], &c[2], &c[3],
856 /* FIXME: length of aname unknown here */
857 code = sscanf(aline, "%d.%d.%d.%d #%s", &c[0], &c[1], &c[2], &c[3],
861 return AFSCONF_SYNTAX;
862 for(i = 0; i < 4; ++i) {
863 if (c[i] < 0 || c[i] > 255) {
864 fprintf(stderr, "Illegal IP address %d.%d.%d.%d\n", c[0], c[1],
866 return AFSCONF_SYNTAX;
869 addr->sin_family = AF_INET;
871 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
872 addr->sin_len = sizeof(struct sockaddr_in);
874 tp = (char *)&addr->sin_addr;
882 /* parse a line of the form
883 * ">cellname [linkedcellname] [#comments]"
884 * into the appropriate pieces.
887 ParseCellLine(char *aline, char *aname,
891 /* FIXME: length of aname, alname unknown here */
892 code = sscanf(aline, ">%s %s", aname, alname);
896 if (*alname == '#') {
900 return (code > 0 ? 0 : AFSCONF_SYNTAX);
903 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
905 afsconf_CellApply(struct afsconf_dir *adir,
906 int (*aproc) (struct afsconf_cell * cell, void *arock,
907 struct afsconf_dir * dir), void *arock)
909 struct afsconf_entry *tde;
912 for (tde = adir->entries; tde; tde = tde->next) {
913 code = (*aproc) (&tde->cellInfo, arock, adir);
923 /* call aproc(entry, arock, adir) for all cell aliases.
924 * Proc must return 0, or we'll stop early and return the code it returns
927 afsconf_CellAliasApply(struct afsconf_dir *adir,
928 int (*aproc) (struct afsconf_cellalias * alias,
929 void *arock, struct afsconf_dir * dir),
932 struct afsconf_aliasentry *tde;
935 for (tde = adir->alias_entries; tde; tde = tde->next) {
936 code = (*aproc) (&tde->aliasInfo, arock, adir);
946 afs_int32 afsconf_SawCell = 0;
949 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
950 char *aservice, struct afsconf_cell *acellInfo,
956 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
963 cell = (char *)&acellInfo->name;
965 code = afsconf_OpenInternal(adir, cell, clones);
969 #if !defined(AFS_NT40_ENV)
971 afsconf_LookupServer(const char *service, const char *protocol,
972 const char *cellName, unsigned short afsdbPort,
973 afs_uint32 *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
974 unsigned short ports[], unsigned short ipRanks[],
975 int *numServers, int *ttl, char **arealCellName)
980 unsigned char answer[4096];
982 char *dotcellname = NULL;
990 char *IANAname = (char *) afsconf_FindIANAName(service);
991 int tservice = afsconf_FindService(service);
997 if (tservice <= 0 || !IANAname)
998 return AFSCONF_NOTFOUND; /* service not found */
1000 if (strchr(cellName,'.'))
1003 #ifdef HAVE_RES_RETRANSRETRY
1004 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1008 * Rx timeout is typically 56 seconds; limit user experience to
1020 r = asprintf(&dotcellname, "_%s._%s.%s.", IANAname, protocol, cellName);
1024 r = asprintf(&dotcellname, "%s.", cellName);
1028 r = asprintf(&dotcellname, "_%s._%s.%s", IANAname, protocol, cellName);
1032 r = asprintf(&dotcellname, "%s", cellName);
1035 if (r < 0 || dotcellname == NULL)
1036 goto findservererror;
1039 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1040 UNLOCK_GLOBAL_MUTEX;
1042 if (dotcellname != NULL) {
1057 code = AFSCONF_NOTFOUND;
1058 goto findservererror;
1062 p = answer + sizeof(HEADER); /* Skip header */
1063 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1065 code = AFSCONF_NOTFOUND;
1066 goto findservererror;
1069 p += code + QFIXEDSZ; /* Skip name */
1071 while (p < answer + len) {
1072 int type, ttl, size;
1074 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1076 code = AFSCONF_NOTFOUND;
1077 goto findservererror;
1080 p += code; /* Skip the name */
1081 type = (p[0] << 8) | p[1];
1082 p += 4; /* Skip type and class */
1083 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1084 p += 4; /* Skip the TTL */
1085 size = (p[0] << 8) | p[1];
1086 p += 2; /* Skip the size */
1088 if (type == T_AFSDB) {
1092 afsdb_type = (p[0] << 8) | p[1];
1093 if (afsdb_type == 1) {
1095 * We know this is an AFSDB record for our cell, of the
1096 * right AFSDB type. Write down the true cell name that
1097 * the resolver gave us above.
1100 realCellName = strdup(host);
1103 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1105 code = AFSCONF_NOTFOUND;
1106 goto findservererror;
1109 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1110 /* Do we want to get TTL data for the A record as well? */
1111 (he = gethostbyname(host))) {
1112 if (he->h_addrtype == AF_INET) {
1114 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1115 cellHostAddrs[server_num] = ipaddr;
1116 ports[server_num] = afsdbPort;
1117 ipRanks[server_num] = 0;
1118 strncpy(cellHostNames[server_num], host,
1119 sizeof(cellHostNames[server_num]));
1121 if (!minttl || ttl < minttl)
1126 if (type == T_SRV) {
1128 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1129 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1130 (strncmp(host + strlen(IANAname) + 3, protocol,
1131 strlen(protocol)) == 0)) {
1133 realCellName = strdup(host + strlen(IANAname) +
1134 strlen(protocol) + 4);
1137 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1139 code = AFSCONF_NOTFOUND;
1140 goto findservererror;
1143 if ((server_num < MAXHOSTSPERCELL) &&
1144 /* Do we want to get TTL data for the A record as well? */
1145 (he = gethostbyname(host))) {
1146 if (he->h_addrtype == AF_INET) {
1149 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1150 cellHostAddrs[server_num] = ipaddr;
1151 ipRanks[server_num] = (p[0] << 8) | p[1];
1152 ports[server_num] = htons((p[4] << 8) | p[5]);
1153 /* weight = (p[2] << 8) | p[3]; */
1154 strncpy(cellHostNames[server_num], host,
1155 sizeof(cellHostNames[server_num]));
1158 if (!minttl || ttl < minttl)
1167 if (server_num == 0) { /* No AFSDB or SRV records */
1168 code = AFSCONF_NOTFOUND;
1169 goto findservererror;
1173 /* Convert the real cell name to lowercase */
1174 for (p = (unsigned char *)realCellName; *p; p++)
1178 *numServers = server_num;
1179 *ttl = minttl ? (time(0) + minttl) : 0;
1181 if ( *numServers > 0 ) {
1183 *arealCellName = realCellName;
1185 code = AFSCONF_NOTFOUND;
1188 if (code && realCellName)
1194 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1195 struct afsconf_cell *acellInfo)
1197 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1198 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1199 unsigned short ipRanks[AFSMAXCELLHOSTS];
1200 unsigned short ports[AFSMAXCELLHOSTS];
1201 char *realCellName = NULL;
1202 int ttl, numServers, i;
1203 char *service = aservice;
1205 unsigned short afsdbport;
1207 service = "afs3-vlserver";
1208 afsdbport = htons(7003);
1211 afsdbport = afsconf_FindService(service);
1213 code = afsconf_LookupServer((const char *)service, "udp",
1214 (const char *)acellName, afsdbport,
1215 cellHostAddrs, cellHostNames,
1216 ports, ipRanks, &numServers, &ttl,
1219 /* If we couldn't find an entry for the requested service
1220 * and that service happens to be the prservice or kaservice
1221 * then fallback to searching for afs3-vlserver and assigning
1222 * the port number here. */
1223 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1224 code = afsconf_LookupServer("afs3-vlserver", "udp",
1225 (const char *)acellName, afsdbport,
1226 cellHostAddrs, cellHostNames,
1227 ports, ipRanks, &numServers, &ttl,
1230 for (i = 0; i < numServers; i++)
1231 ports[i] = afsdbport;
1235 acellInfo->timeout = ttl;
1236 acellInfo->numServers = numServers;
1237 for (i = 0; i < numServers; i++) {
1238 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1240 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1241 acellInfo->hostAddr[i].sin_family = AF_INET;
1242 acellInfo->hostAddr[i].sin_port = ports[i];
1245 strlcpy(acellInfo->name, realCellName,
1246 sizeof(acellInfo->name));
1248 realCellName = NULL;
1251 acellInfo->linkedCell = NULL; /* no linked cell */
1252 acellInfo->flags = 0;
1258 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1259 struct afsconf_cell *acellInfo)
1262 int tservice = afsconf_FindService(aservice); /* network byte order */
1263 const char *ianaName = afsconf_FindIANAName(aservice);
1264 struct afsconf_entry DNSce;
1265 afs_uint32 cellHostAddrs[AFSMAXCELLHOSTS];
1266 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1267 unsigned short ipRanks[AFSMAXCELLHOSTS];
1268 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1275 return AFSCONF_NOTFOUND;
1277 tservice = 0; /* port will be assigned by caller */
1280 if (ianaName == NULL)
1281 ianaName = "afs3-vlserver";
1283 DNSce.cellInfo.numServers = 0;
1286 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1287 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1289 /* ignore the ttl here since this code is only called by transitory programs
1290 * like klog, etc. */
1292 /* If we couldn't find an entry for the requested service
1293 * and that service happens to be the prservice or kaservice
1294 * then fallback to searching for afs3-vlserver and assigning
1295 * the port number here. */
1296 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1297 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1298 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1301 for (i = 0; i < numServers; i++)
1302 ports[i] = tservice;
1306 if (rc < 0 || numServers == 0)
1309 for (i = 0; i < numServers; i++) {
1310 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1311 sizeof(afs_uint32));
1312 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1313 acellInfo->hostAddr[i].sin_family = AF_INET;
1315 acellInfo->hostAddr[i].sin_port = ports[i];
1317 acellInfo->hostAddr[i].sin_port = 0;
1320 acellInfo->numServers = numServers;
1321 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1322 acellInfo->linkedCell = NULL; /* no linked cell */
1323 acellInfo->flags = 0;
1326 #endif /* windows */
1329 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1330 struct afsconf_cell *acellInfo)
1332 struct afsconf_entry *tce;
1333 struct afsconf_aliasentry *tcae;
1334 struct afsconf_entry *bestce;
1344 _afsconf_Check(adir);
1347 cnLen = (int)(strlen(tcell) + 1);
1348 lcstring(tcell, tcell, cnLen);
1349 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1350 /* call to afsconf_GetLocalCell: like klog */
1352 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1354 UNLOCK_GLOBAL_MUTEX;
1359 cnLen = strlen(tcell);
1360 bestce = (struct afsconf_entry *)0;
1363 UNLOCK_GLOBAL_MUTEX;
1367 /* Look through the list of aliases */
1368 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1369 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1370 tcell = tcae->aliasInfo.realName;
1375 for (tce = adir->entries; tce; tce = tce->next) {
1376 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1377 /* found our cell */
1382 if (strlen(tce->cellInfo.name) < cnLen)
1383 continue; /* clearly wrong */
1384 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1386 ambig = 1; /* ambiguous unless we get exact match */
1390 if (!ambig && bestce && bestce->cellInfo.numServers) {
1391 *acellInfo = bestce->cellInfo; /* structure assignment */
1393 tservice = afsconf_FindService(aservice);
1395 UNLOCK_GLOBAL_MUTEX;
1396 return AFSCONF_NOTFOUND; /* service not found */
1398 for (i = 0; i < acellInfo->numServers; i++) {
1399 acellInfo->hostAddr[i].sin_port = tservice;
1402 acellInfo->timeout = 0;
1405 * Until we figure out how to separate out ubik server
1406 * queries from other server queries, only perform gethostbyname()
1407 * lookup on the specified hostnames for the client CellServDB files.
1409 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1410 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1412 short numServers=0; /*Num active servers for the cell */
1413 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1414 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1416 memset(&hostAddr, 0, sizeof(hostAddr));
1417 memset(&hostName, 0, sizeof(hostName));
1419 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1420 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1423 if (he && he->h_addrtype == AF_INET) {
1425 /* obtain all the valid address from the list */
1426 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1427 /* check to see if this is a new address; if so insert it into the list */
1430 memcpy(&addr, he->h_addr_list[i], sizeof(addr));
1431 for (k=0, dup=0; !dup && k < numServers; k++) {
1432 if (hostAddr[k].sin_addr.s_addr == addr) {
1439 hostAddr[numServers].sin_family = AF_INET;
1440 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1441 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1442 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1444 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(afs_uint32));
1445 strcpy(hostName[numServers], acellInfo->hostName[j]);
1451 hostAddr[numServers] = acellInfo->hostAddr[j];
1452 strcpy(hostName[numServers], acellInfo->hostName[j]);
1457 for (i=0; i<numServers; i++) {
1458 acellInfo->hostAddr[i] = hostAddr[i];
1459 strcpy(acellInfo->hostName[i], hostName[i]);
1461 acellInfo->numServers = numServers;
1462 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1464 UNLOCK_GLOBAL_MUTEX;
1467 UNLOCK_GLOBAL_MUTEX;
1468 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1473 * Get the current localcell name.
1475 * Internal function to get a pointer to the local cell name.
1476 * This function must be called with the global afsconf lock held.
1478 * @param[in] adir afsconf object
1479 * @param[out] aname address to a char pointer
1480 * @param[in] check always perform a config check, even if the
1481 * the AFSCELL name is set.
1485 * @retval AFSCONF_NOCELLNAME cannot determine local cell name
1490 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1492 static int afsconf_showcell = 0;
1497 * If a cell switch was specified in a command, then it should override the
1498 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1499 * is set and the cell name in the adir structure is used.
1500 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1501 * Optionally, check the configuration, even if using the environment variable.
1503 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1505 _afsconf_Check(adir);
1507 if (!afsconf_showcell) {
1508 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1510 afsconf_showcell = 1;
1512 *pname = afscell_path;
1514 _afsconf_Check(adir);
1515 if (adir->cellName) {
1516 *pname = adir->cellName;
1518 code = AFSCONF_NOCELLNAME;
1524 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1527 char *cellname = NULL;
1530 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1531 if (!code && cellname) {
1532 strlcpy(aname, cellname, alen);
1534 UNLOCK_GLOBAL_MUTEX;
1539 afsconf_Close(struct afsconf_dir *adir)
1542 afsconf_CloseInternal(adir);
1546 UNLOCK_GLOBAL_MUTEX;
1551 afsconf_CloseInternal(struct afsconf_dir *adir)
1553 struct afsconf_entry *td, *nd;
1554 struct afsconf_aliasentry *ta, *na;
1557 tname = adir->name; /* remember name, since that's all we preserve */
1559 /* free everything we can find */
1561 free(adir->cellName);
1562 for (td = adir->entries; td; td = nd) {
1564 if (td->cellInfo.linkedCell)
1565 free(td->cellInfo.linkedCell);
1568 for (ta = adir->alias_entries; ta; ta = na) {
1573 _afsconf_FreeAllKeys(adir);
1574 _afsconf_FreeRealms(adir);
1577 memset(adir, 0, sizeof(struct afsconf_dir));
1578 adir->name = tname; /* restore it */
1583 afsconf_Reopen(struct afsconf_dir *adir)
1586 code = afsconf_CloseInternal(adir);
1589 code = afsconf_OpenInternal(adir, 0, 0);