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 if (asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
334 if (asprintf(path, "%s/%s", adir->name,
335 AFSDIR_CELLSERVDB_FILE_NTCLIENT) < 0)
339 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
346 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
348 if (asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE) < 0)
351 #endif /* AFS_NT40_ENV */
354 _afsconf_UpToDate(struct afsconf_dir *adir)
359 time_t now = time(0);
361 if (adir->timeRead && (adir->timeCheck == now)) {
362 return 1; /* stat no more than once a second */
364 adir->timeCheck = now;
366 _afsconf_CellServDBPath(adir, &cellservDB);
367 if (cellservDB == NULL)
370 code = stat(cellservDB, &tstat);
373 return 0; /* Can't throw the error, so just say we're not up to date */
375 /* did file change? */
376 if (tstat.st_mtime == adir->timeRead)
379 /* otherwise file has changed */
384 afsconf_UpToDate(void *rock)
389 code = _afsconf_UpToDate(rock);
396 _afsconf_Check(struct afsconf_dir *adir)
398 /* did configuration change? */
399 if (_afsconf_UpToDate(adir))
402 /* otherwise file has changed, so reopen it */
403 return afsconf_Reopen(adir);
406 /* set modtime on file */
408 _afsconf_Touch(struct afsconf_dir *adir)
413 struct timeval tvp[2];
416 adir->timeRead = 0; /* just in case */
419 _afsconf_CellServDBPath(adir, &cellservDB);
420 if (cellservDB == NULL)
424 code = _utime(cellservDB, NULL);
426 gettimeofday(&tvp[0], NULL);
428 code = utimes(cellservDB, tvp);
429 #endif /* AFS_NT40_ENV */
436 afsconf_Open(const char *adir)
438 struct afsconf_dir *tdir;
442 /* zero structure and fill in name; rest is done by internal routine */
443 tdir = calloc(1, sizeof(struct afsconf_dir));
444 tdir->name = strdup(adir);
446 code = afsconf_OpenInternal(tdir, 0, 0);
448 char *afsconf_path, afs_confdir[128];
451 /* Check global place only when local Open failed for whatever reason */
452 if (!(afsconf_path = getenv("AFSCONF"))) {
453 /* 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... */
459 if (!(home_dir = getenv("HOME"))) {
460 /* Our last chance is the "/.AFSCONF" file */
461 fp = fopen("/.AFSCONF", "r");
466 char *pathname = NULL;
468 r = asprintf(&pathname, "%s/%s", home_dir, ".AFSCONF");
469 if (r < 0 || pathname == NULL)
472 fp = fopen(pathname, "r");
476 /* Our last chance is the "/.AFSCONF" file */
477 fp = fopen("/.AFSCONF", "r");
482 if (fgets(afs_confdir, 128, fp) != NULL)
483 len = strlen(afs_confdir);
488 if (afs_confdir[len - 1] == '\n') {
489 afs_confdir[len - 1] = 0;
491 afsconf_path = afs_confdir;
493 tdir->name = strdup(afsconf_path);
494 code = afsconf_OpenInternal(tdir, 0, 0);
510 GetCellUnix(struct afsconf_dir *adir)
517 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE,
519 fp = fopen(tbuffer, "r");
523 rc = fgets(tbuffer, 256, fp);
529 while (*start != '\0' && isspace(*start))
532 while (*p != '\0' && !isspace(*p))
538 adir->cellName = strdup(start);
545 GetCellNT(struct afsconf_dir *adir)
547 if (_afsconf_IsClientConfigDirectory(adir->name)) {
548 /* NT client config dir; ThisCell is in registry (no file). */
549 return afssw_GetClientCellName(&adir->cellName);
551 /* NT server config dir; works just like Unix */
552 return GetCellUnix(adir);
556 /* The following procedures and structs are used on Windows only
557 * to enumerate the Cell information distributed within the
558 * Windows registry. (See src/WINNT/afsd/cm_config.c)
560 typedef struct _cm_enumCellRegistry {
561 afs_uint32 client; /* non-zero if client query */
562 struct afsconf_dir *adir;
563 } cm_enumCellRegistry_t;
566 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
567 char *hostNamep, unsigned short rank)
569 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
571 if (cellInfop->numServers == MAXHOSTSPERCELL)
574 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
575 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
576 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
577 cellInfop->numServers++;
583 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
586 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
587 char linkedName[256] = "";
589 struct afsconf_entry *newEntry;
592 newEntry = malloc(sizeof(struct afsconf_entry));
593 if (newEntry == NULL)
595 newEntry->cellInfo.numServers = 0;
597 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
598 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
599 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
602 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
603 newEntry->cellInfo.name[MAXCELLCHARS-1];
605 newEntry->cellInfo.linkedCell = strdup(linkedName);
607 newEntry->cellInfo.linkedCell = NULL;
608 newEntry->cellInfo.timeout = timeout;
609 newEntry->cellInfo.flags = 0;
611 newEntry->next = enump->adir->entries;
612 enump->adir->entries = newEntry;
618 #endif /* AFS_NT40_ENV */
622 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
627 struct afsconf_entry *curEntry;
628 struct afsconf_aliasentry *curAlias;
636 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
637 #endif /* AFS_NT40_ENV */
639 /* init the keys queue before any call to afsconf_CloseInternal() */
640 _afsconf_InitKeys(adir);
642 /* figure out the local cell name */
645 enumCellRegistry.adir = adir;
647 i = GetCellUnix(adir);
650 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
656 /* now parse the individual lines */
659 _afsconf_CellServDBPath(adir, &cellservDB);
662 if (_afsconf_IsClientConfigDirectory(adir->name))
663 enumCellRegistry.client = 1;
664 #endif /* AFS_NT40_ENV */
666 if (!stat(cellservDB, &tstat)) {
667 adir->timeRead = tstat.st_mtime;
672 tf = fopen(cellservDB, "r");
677 /* The CellServDB file is now open.
678 * The following code parses the contents of the
679 * file and creates a list with the first cell entry
680 * in the CellServDB file at the end of the list.
682 * No checking is performed for duplicates.
683 * The side effects of this process are that duplicate
684 * entries appended to the end of the CellServDB file
685 * take precedence and are found in a shorter period
690 tp = fgets(tbuffer, sizeof(tbuffer), tf);
693 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
694 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
695 continue; /* empty line */
696 if (tbuffer[0] == '>') {
697 char linkedcell[MAXCELLCHARS];
698 /* start new cell item */
700 /* thread this guy on the list */
701 curEntry->next = adir->entries;
702 adir->entries = curEntry;
705 curEntry = calloc(1, sizeof(struct afsconf_entry));
707 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
709 afsconf_CloseInternal(adir);
714 if (linkedcell[0] != '\0')
715 curEntry->cellInfo.linkedCell = strdup(linkedcell);
717 /* new host in the current cell */
719 afsconf_CloseInternal(adir);
723 i = curEntry->cellInfo.numServers;
724 if (i < MAXHOSTSPERCELL) {
725 if (cell && !strcmp(cell, curEntry->cellInfo.name))
727 ParseHostLine(tbuffer,
728 &curEntry->cellInfo.hostAddr[i],
729 curEntry->cellInfo.hostName[i],
733 ParseHostLine(tbuffer,
734 &curEntry->cellInfo.hostAddr[i],
735 curEntry->cellInfo.hostName[i], 0);
738 if (code == AFSCONF_SYNTAX) {
739 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
745 "Can't properly parse host line \"%s\" in configuration file %s\n",
746 tbuffer, cellservDB);
750 afsconf_CloseInternal(adir);
753 curEntry->cellInfo.numServers = ++i;
756 "Too many hosts for cell %s in configuration file %s\n",
757 curEntry->cellInfo.name, cellservDB);
761 fclose(tf); /* close the file now */
764 /* end the last partially-completed cell */
766 curEntry->next = adir->entries;
767 adir->entries = curEntry;
772 * Windows maintains a CellServDB list in the Registry
773 * that supercedes the contents of the CellServDB file.
774 * Prepending these entries to the head of the list
775 * is sufficient to enforce the precedence.
777 cm_EnumerateCellRegistry( enumCellRegistry.client,
778 cm_enumCellRegistryProc,
780 #endif /* AFS_NT40_ENV */
782 /* Read in the alias list */
783 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE,
786 tf = fopen(tbuffer, "r");
790 tp = fgets(tbuffer, sizeof(tbuffer), tf);
793 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
795 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
796 continue; /* empty line */
799 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
802 continue; /* invalid line */
804 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
807 continue; /* invalid line */
810 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
815 curAlias = calloc(1, sizeof(*curAlias));
817 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
818 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
820 curAlias->next = adir->alias_entries;
821 adir->alias_entries = curAlias;
827 /* now read the fs keys, if possible */
828 code = _afsconf_LoadKeys(adir);
832 code = _afsconf_LoadRealms(adir);
837 /* parse a line of the form
838 *"128.2.1.3 #hostname" or
839 *"[128.2.1.3] #hostname" for clones
840 * into the appropriate pieces.
843 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
853 /* FIXME: length of aname unknown here */
854 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
858 /* FIXME: length of aname unknown here */
859 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
862 return AFSCONF_SYNTAX;
863 addr->sin_family = AF_INET;
865 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
866 addr->sin_len = sizeof(struct sockaddr_in);
868 tp = (char *)&addr->sin_addr;
876 /* parse a line of the form
877 * ">cellname [linkedcellname] [#comments]"
878 * into the appropriate pieces.
881 ParseCellLine(char *aline, char *aname,
885 /* FIXME: length of aname, alname unknown here */
886 code = sscanf(aline, ">%s %s", aname, alname);
890 if (*alname == '#') {
894 return (code > 0 ? 0 : AFSCONF_SYNTAX);
897 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
899 afsconf_CellApply(struct afsconf_dir *adir,
900 int (*aproc) (struct afsconf_cell * cell, void *arock,
901 struct afsconf_dir * dir), void *arock)
903 struct afsconf_entry *tde;
906 for (tde = adir->entries; tde; tde = tde->next) {
907 code = (*aproc) (&tde->cellInfo, arock, adir);
917 /* call aproc(entry, arock, adir) for all cell aliases.
918 * Proc must return 0, or we'll stop early and return the code it returns
921 afsconf_CellAliasApply(struct afsconf_dir *adir,
922 int (*aproc) (struct afsconf_cellalias * alias,
923 void *arock, struct afsconf_dir * dir),
926 struct afsconf_aliasentry *tde;
929 for (tde = adir->alias_entries; tde; tde = tde->next) {
930 code = (*aproc) (&tde->aliasInfo, arock, adir);
940 afs_int32 afsconf_SawCell = 0;
943 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
944 char *aservice, struct afsconf_cell *acellInfo,
950 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
957 cell = (char *)&acellInfo->name;
959 code = afsconf_OpenInternal(adir, cell, clones);
963 #if !defined(AFS_NT40_ENV)
965 afsconf_LookupServer(const char *service, const char *protocol,
966 const char *cellName, unsigned short afsdbPort,
967 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
968 unsigned short ports[], unsigned short ipRanks[],
969 int *numServers, int *ttl, char **arealCellName)
974 unsigned char answer[4096];
976 char *dotcellname = NULL;
984 char *IANAname = (char *) afsconf_FindIANAName(service);
985 int tservice = afsconf_FindService(service);
991 if (tservice <= 0 || !IANAname)
992 return AFSCONF_NOTFOUND; /* service not found */
994 if (strchr(cellName,'.'))
997 #ifdef HAVE_RES_RETRANSRETRY
998 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1002 * Rx timeout is typically 56 seconds; limit user experience to
1014 r = asprintf(&dotcellname, "_%s._%s.%s.", IANAname, protocol, cellName);
1018 r = asprintf(&dotcellname, "%s.", cellName);
1022 r = asprintf(&dotcellname, "_%s._%s.%s", IANAname, protocol, cellName);
1026 r = asprintf(&dotcellname, "%s", cellName);
1029 if (r < 0 || dotcellname == NULL)
1030 goto findservererror;
1033 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1034 UNLOCK_GLOBAL_MUTEX;
1036 if (dotcellname != NULL) {
1051 code = AFSCONF_NOTFOUND;
1052 goto findservererror;
1056 p = answer + sizeof(HEADER); /* Skip header */
1057 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1059 code = AFSCONF_NOTFOUND;
1060 goto findservererror;
1063 p += code + QFIXEDSZ; /* Skip name */
1065 while (p < answer + len) {
1066 int type, ttl, size;
1068 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1070 code = AFSCONF_NOTFOUND;
1071 goto findservererror;
1074 p += code; /* Skip the name */
1075 type = (p[0] << 8) | p[1];
1076 p += 4; /* Skip type and class */
1077 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1078 p += 4; /* Skip the TTL */
1079 size = (p[0] << 8) | p[1];
1080 p += 2; /* Skip the size */
1082 if (type == T_AFSDB) {
1086 afsdb_type = (p[0] << 8) | p[1];
1087 if (afsdb_type == 1) {
1089 * We know this is an AFSDB record for our cell, of the
1090 * right AFSDB type. Write down the true cell name that
1091 * the resolver gave us above.
1094 realCellName = strdup(host);
1097 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1099 code = AFSCONF_NOTFOUND;
1100 goto findservererror;
1103 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1104 /* Do we want to get TTL data for the A record as well? */
1105 (he = gethostbyname(host))) {
1106 if (he->h_addrtype == AF_INET) {
1108 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1109 cellHostAddrs[server_num] = ipaddr;
1110 ports[server_num] = afsdbPort;
1111 ipRanks[server_num] = 0;
1112 strncpy(cellHostNames[server_num], host,
1113 sizeof(cellHostNames[server_num]));
1115 if (!minttl || ttl < minttl)
1120 if (type == T_SRV) {
1122 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1123 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1124 (strncmp(host + strlen(IANAname) + 3, protocol,
1125 strlen(protocol)) == 0)) {
1127 realCellName = strdup(host + strlen(IANAname) +
1128 strlen(protocol) + 4);
1131 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1133 code = AFSCONF_NOTFOUND;
1134 goto findservererror;
1137 if ((server_num < MAXHOSTSPERCELL) &&
1138 /* Do we want to get TTL data for the A record as well? */
1139 (he = gethostbyname(host))) {
1140 if (he->h_addrtype == AF_INET) {
1143 memcpy(&ipaddr, he->h_addr, sizeof(ipaddr));
1144 cellHostAddrs[server_num] = ipaddr;
1145 ipRanks[server_num] = (p[0] << 8) | p[1];
1146 ports[server_num] = htons((p[4] << 8) | p[5]);
1147 /* weight = (p[2] << 8) | p[3]; */
1148 strncpy(cellHostNames[server_num], host,
1149 sizeof(cellHostNames[server_num]));
1152 if (!minttl || ttl < minttl)
1161 if (server_num == 0) { /* No AFSDB or SRV records */
1162 code = AFSCONF_NOTFOUND;
1163 goto findservererror;
1167 /* Convert the real cell name to lowercase */
1168 for (p = (unsigned char *)realCellName; *p; p++)
1172 *numServers = server_num;
1173 *ttl = minttl ? (time(0) + minttl) : 0;
1175 if ( *numServers > 0 ) {
1177 *arealCellName = realCellName;
1179 code = AFSCONF_NOTFOUND;
1182 if (code && realCellName)
1188 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1189 struct afsconf_cell *acellInfo)
1191 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1192 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1193 unsigned short ipRanks[AFSMAXCELLHOSTS];
1194 unsigned short ports[AFSMAXCELLHOSTS];
1195 char *realCellName = NULL;
1196 int ttl, numServers, i;
1197 char *service = aservice;
1199 unsigned short afsdbport;
1201 service = "afs3-vlserver";
1202 afsdbport = htons(7003);
1205 afsdbport = afsconf_FindService(service);
1207 code = afsconf_LookupServer((const char *)service, "udp",
1208 (const char *)acellName, afsdbport,
1209 cellHostAddrs, cellHostNames,
1210 ports, ipRanks, &numServers, &ttl,
1213 /* If we couldn't find an entry for the requested service
1214 * and that service happens to be the prservice or kaservice
1215 * then fallback to searching for afs3-vlserver and assigning
1216 * the port number here. */
1217 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1218 code = afsconf_LookupServer("afs3-vlserver", "udp",
1219 (const char *)acellName, afsdbport,
1220 cellHostAddrs, cellHostNames,
1221 ports, ipRanks, &numServers, &ttl,
1224 for (i = 0; i < numServers; i++)
1225 ports[i] = afsdbport;
1229 acellInfo->timeout = ttl;
1230 acellInfo->numServers = numServers;
1231 for (i = 0; i < numServers; i++) {
1232 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1234 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1235 acellInfo->hostAddr[i].sin_family = AF_INET;
1236 acellInfo->hostAddr[i].sin_port = ports[i];
1239 strlcpy(acellInfo->name, realCellName,
1240 sizeof(acellInfo->name));
1242 realCellName = NULL;
1245 acellInfo->linkedCell = NULL; /* no linked cell */
1246 acellInfo->flags = 0;
1252 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1253 struct afsconf_cell *acellInfo)
1256 int tservice = afsconf_FindService(aservice); /* network byte order */
1257 const char *ianaName = afsconf_FindIANAName(aservice);
1258 struct afsconf_entry DNSce;
1259 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1260 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1261 unsigned short ipRanks[AFSMAXCELLHOSTS];
1262 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1269 return AFSCONF_NOTFOUND;
1271 tservice = 0; /* port will be assigned by caller */
1274 if (ianaName == NULL)
1275 ianaName = "afs3-vlserver";
1277 DNSce.cellInfo.numServers = 0;
1280 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1281 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1283 /* ignore the ttl here since this code is only called by transitory programs
1284 * like klog, etc. */
1286 /* If we couldn't find an entry for the requested service
1287 * and that service happens to be the prservice or kaservice
1288 * then fallback to searching for afs3-vlserver and assigning
1289 * the port number here. */
1290 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1291 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1292 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1295 for (i = 0; i < numServers; i++)
1296 ports[i] = tservice;
1300 if (rc < 0 || numServers == 0)
1303 for (i = 0; i < numServers; i++) {
1304 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1305 sizeof(afs_uint32));
1306 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1307 acellInfo->hostAddr[i].sin_family = AF_INET;
1309 acellInfo->hostAddr[i].sin_port = ports[i];
1311 acellInfo->hostAddr[i].sin_port = 0;
1314 acellInfo->numServers = numServers;
1315 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1316 acellInfo->linkedCell = NULL; /* no linked cell */
1317 acellInfo->flags = 0;
1320 #endif /* windows */
1323 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1324 struct afsconf_cell *acellInfo)
1326 struct afsconf_entry *tce;
1327 struct afsconf_aliasentry *tcae;
1328 struct afsconf_entry *bestce;
1338 _afsconf_Check(adir);
1341 cnLen = (int)(strlen(tcell) + 1);
1342 lcstring(tcell, tcell, cnLen);
1343 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1344 /* call to afsconf_GetLocalCell: like klog */
1346 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1348 UNLOCK_GLOBAL_MUTEX;
1353 cnLen = strlen(tcell);
1354 bestce = (struct afsconf_entry *)0;
1357 UNLOCK_GLOBAL_MUTEX;
1361 /* Look through the list of aliases */
1362 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1363 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1364 tcell = tcae->aliasInfo.realName;
1369 for (tce = adir->entries; tce; tce = tce->next) {
1370 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1371 /* found our cell */
1376 if (strlen(tce->cellInfo.name) < cnLen)
1377 continue; /* clearly wrong */
1378 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1380 ambig = 1; /* ambiguous unless we get exact match */
1384 if (!ambig && bestce && bestce->cellInfo.numServers) {
1385 *acellInfo = bestce->cellInfo; /* structure assignment */
1387 tservice = afsconf_FindService(aservice);
1389 UNLOCK_GLOBAL_MUTEX;
1390 return AFSCONF_NOTFOUND; /* service not found */
1392 for (i = 0; i < acellInfo->numServers; i++) {
1393 acellInfo->hostAddr[i].sin_port = tservice;
1396 acellInfo->timeout = 0;
1399 * Until we figure out how to separate out ubik server
1400 * queries from other server queries, only perform gethostbyname()
1401 * lookup on the specified hostnames for the client CellServDB files.
1403 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1404 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1406 short numServers=0; /*Num active servers for the cell */
1407 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1408 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1410 memset(&hostAddr, 0, sizeof(hostAddr));
1411 memset(&hostName, 0, sizeof(hostName));
1413 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1414 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1417 if (he && he->h_addrtype == AF_INET) {
1419 /* obtain all the valid address from the list */
1420 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1421 /* check to see if this is a new address; if so insert it into the list */
1424 memcpy(&addr, he->h_addr_list[i], sizeof(addr));
1425 for (k=0, dup=0; !dup && k < numServers; k++) {
1426 if (hostAddr[k].sin_addr.s_addr == addr) {
1433 hostAddr[numServers].sin_family = AF_INET;
1434 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1435 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1436 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1438 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(afs_uint32));
1439 strcpy(hostName[numServers], acellInfo->hostName[j]);
1445 hostAddr[numServers] = acellInfo->hostAddr[j];
1446 strcpy(hostName[numServers], acellInfo->hostName[j]);
1451 for (i=0; i<numServers; i++) {
1452 acellInfo->hostAddr[i] = hostAddr[i];
1453 strcpy(acellInfo->hostName[i], hostName[i]);
1455 acellInfo->numServers = numServers;
1456 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1458 UNLOCK_GLOBAL_MUTEX;
1461 UNLOCK_GLOBAL_MUTEX;
1462 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1467 * Get the current localcell name.
1469 * Internal function to get a pointer to the local cell name.
1470 * This function must be called with the global afsconf lock held.
1472 * @param[in] adir afsconf object
1473 * @param[out] aname address to a char pointer
1474 * @param[in] check always perform a config check, even if the
1475 * the AFSCELL name is set.
1479 * @retval AFSCONF_NOCELLNAME cannot determine local cell name
1484 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1486 static int afsconf_showcell = 0;
1491 * If a cell switch was specified in a command, then it should override the
1492 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1493 * is set and the cell name in the adir structure is used.
1494 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1495 * Optionally, check the configuration, even if using the environment variable.
1497 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1499 _afsconf_Check(adir);
1501 if (!afsconf_showcell) {
1502 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1504 afsconf_showcell = 1;
1506 *pname = afscell_path;
1508 _afsconf_Check(adir);
1509 if (adir->cellName) {
1510 *pname = adir->cellName;
1512 code = AFSCONF_NOCELLNAME;
1518 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1521 char *cellname = NULL;
1524 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1525 if (!code && cellname) {
1526 strlcpy(aname, cellname, alen);
1528 UNLOCK_GLOBAL_MUTEX;
1533 afsconf_Close(struct afsconf_dir *adir)
1536 afsconf_CloseInternal(adir);
1540 UNLOCK_GLOBAL_MUTEX;
1545 afsconf_CloseInternal(struct afsconf_dir *adir)
1547 struct afsconf_entry *td, *nd;
1548 struct afsconf_aliasentry *ta, *na;
1551 tname = adir->name; /* remember name, since that's all we preserve */
1553 /* free everything we can find */
1555 free(adir->cellName);
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, 0, 0);