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)
157 free((void *)iop->buffer);
163 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
171 if (iop->ptr == iop->endptr) {
174 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
184 iop->ptr = iop->buffer;
185 iop->endptr = iop->buffer + len;
189 if ((p - s) == (n - 1)) {
199 #define fopen afsconf_fopen
200 #define fclose afsconf_fclose
201 #define fgets afsconf_fgets
203 #define afsconf_FILE FILE
204 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
206 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
208 afsconf_FindService(const char *aname)
210 /* lookup a service name */
212 struct afsconf_servPair *tsp;
214 if (aname == NULL || aname[0] == '\0')
217 #if defined(AFS_OSF_ENV)
218 ts = getservbyname(aname, "");
220 ts = (struct servent *) getservbyname(aname, NULL);
223 /* we found it in /etc/services, so we use this value */
224 return ts->s_port; /* already in network byte order */
227 /* not found in /etc/services, see if it is one of ours */
228 for (tsp = serviceTable; tsp->port; tsp++) {
229 if ((tsp->name && (!strcmp(tsp->name, aname)))
230 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
231 return htons(tsp->port);
237 afsconf_FindIANAName(const char *aname)
239 /* lookup a service name */
240 struct afsconf_servPair *tsp;
242 if (aname == NULL || aname[0] == '\0')
245 /* see if it is one of ours */
246 for (tsp = serviceTable; tsp->port; tsp++) {
247 if ((tsp->name && (!strcmp(tsp->name, aname)))
248 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
249 return tsp->ianaName;
255 TrimLine(char *abuffer, int abufsize)
267 strlcpy(tbuffer, tp, sizeof tbuffer);
268 strlcpy(abuffer, tbuffer, abufsize);
273 * IsClientConfigDirectory() -- determine if path matches well-known
274 * client configuration directory.
277 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
278 #else /* AFS_NT40_ENV */
279 #define IS_SEP(x) ((x) == '/')
280 #endif /* AFS_NT40_ENV */
282 _afsconf_IsClientConfigDirectory(const char *path)
284 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
287 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
289 cc = tolower(cdir[i]);
290 pc = tolower(path[i]);
298 #else /* AFS_NT40_ENV */
301 #endif /* AFS_NT40_ENV */
307 /* hit end of one or both; allow mismatch in existence of trailing slash */
308 if (cdir[i] != '\0') {
309 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
313 if (path[i] != '\0') {
314 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
323 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
327 /* NT client CellServDB has different file name than NT server or Unix */
328 if (_afsconf_IsClientConfigDirectory(adir->name)) {
329 if (!afssw_GetClientCellServDBDir(&p)) {
330 asprintf(path, "%s/%s", p, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
333 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
336 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
342 _afsconf_CellServDBPath(struct afsconf_dir *adir, char **path)
344 asprintf(path, "%s/%s", adir->name, AFSDIR_CELLSERVDB_FILE);
346 #endif /* AFS_NT40_ENV */
349 _afsconf_UpToDate(struct afsconf_dir *adir)
355 _afsconf_CellServDBPath(adir, &cellservDB);
356 if (cellservDB == NULL)
359 code = stat(cellservDB, &tstat);
362 return 0; /* Can't throw the error, so just say we're not up to date */
364 /* did file change? */
365 if (tstat.st_mtime == adir->timeRead)
368 /* otherwise file has changed */
373 afsconf_UpToDate(void *rock)
378 code = _afsconf_UpToDate(rock);
385 _afsconf_Check(struct afsconf_dir *adir)
387 /* did configuration change? */
388 if (_afsconf_UpToDate(adir))
391 /* otherwise file has changed, so reopen it */
392 return afsconf_Reopen(adir);
395 /* set modtime on file */
397 _afsconf_Touch(struct afsconf_dir *adir)
402 struct timeval tvp[2];
405 adir->timeRead = 0; /* just in case */
407 _afsconf_CellServDBPath(adir, &cellservDB);
408 if (cellservDB == NULL)
412 code = _utime(cellservDB, NULL);
414 gettimeofday(&tvp[0], NULL);
416 code = utimes(cellservDB, tvp);
417 #endif /* AFS_NT40_ENV */
424 afsconf_Open(const char *adir)
426 struct afsconf_dir *tdir;
430 /* zero structure and fill in name; rest is done by internal routine */
431 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
432 memset(tdir, 0, sizeof(struct afsconf_dir));
433 tdir->name = strdup(adir);
435 code = afsconf_OpenInternal(tdir, 0, 0);
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... */
447 if (!(home_dir = getenv("HOME"))) {
448 /* Our last chance is the "/.AFSCONF" file */
449 fp = fopen("/.AFSCONF", "r");
453 return (struct afsconf_dir *)0;
455 fgets(afs_confdir, 128, fp);
460 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
461 fp = fopen(pathname, "r");
463 /* Our last chance is the "/.AFSCONF" file */
464 fp = fopen("/.AFSCONF", "r");
468 return (struct afsconf_dir *)0;
471 fgets(afs_confdir, 128, fp);
474 len = strlen(afs_confdir);
478 return (struct afsconf_dir *)0;
480 if (afs_confdir[len - 1] == '\n') {
481 afs_confdir[len - 1] = 0;
483 afsconf_path = afs_confdir;
485 tdir->name = strdup(afsconf_path);
486 code = afsconf_OpenInternal(tdir, 0, 0);
491 return (struct afsconf_dir *)0;
499 GetCellUnix(struct afsconf_dir *adir)
506 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
507 fp = fopen(tbuffer, "r");
511 rc = fgets(tbuffer, 256, fp);
517 while (*start != '\0' && isspace(*start))
520 while (*p != '\0' && !isspace(*p))
526 adir->cellName = strdup(start);
533 GetCellNT(struct afsconf_dir *adir)
535 if (_afsconf_IsClientConfigDirectory(adir->name)) {
536 /* NT client config dir; ThisCell is in registry (no file). */
537 return afssw_GetClientCellName(&adir->cellName);
539 /* NT server config dir; works just like Unix */
540 return GetCellUnix(adir);
544 /* The following procedures and structs are used on Windows only
545 * to enumerate the Cell information distributed within the
546 * Windows registry. (See src/WINNT/afsd/cm_config.c)
548 typedef struct _cm_enumCellRegistry {
549 afs_uint32 client; /* non-zero if client query */
550 struct afsconf_dir *adir;
551 } cm_enumCellRegistry_t;
554 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
555 char *hostNamep, unsigned short rank)
557 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
559 if (cellInfop->numServers == MAXHOSTSPERCELL)
562 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
563 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
564 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
565 cellInfop->numServers++;
571 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
574 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
575 char linkedName[256] = "";
577 struct afsconf_entry *newEntry;
580 newEntry = malloc(sizeof(struct afsconf_entry));
581 if (newEntry == NULL)
583 newEntry->cellInfo.numServers = 0;
585 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
586 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
587 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
590 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
591 newEntry->cellInfo.name[MAXCELLCHARS-1];
593 newEntry->cellInfo.linkedCell = strdup(linkedName);
595 newEntry->cellInfo.linkedCell = NULL;
596 newEntry->cellInfo.timeout = timeout;
597 newEntry->cellInfo.flags = 0;
599 newEntry->next = enump->adir->entries;
600 enump->adir->entries = newEntry;
606 #endif /* AFS_NT40_ENV */
610 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
615 struct afsconf_entry *curEntry;
616 struct afsconf_aliasentry *curAlias;
624 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
625 #endif /* AFS_NT40_ENV */
627 /* figure out the local cell name */
630 enumCellRegistry.adir = adir;
632 i = GetCellUnix(adir);
635 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
641 /* now parse the individual lines */
644 _afsconf_CellServDBPath(adir, &cellservDB);
647 if (_afsconf_IsClientConfigDirectory(adir->name))
648 enumCellRegistry.client = 1;
649 #endif /* AFS_NT40_ENV */
651 if (!stat(cellservDB, &tstat)) {
652 adir->timeRead = tstat.st_mtime;
657 tf = fopen(cellservDB, "r");
662 /* The CellServDB file is now open.
663 * The following code parses the contents of the
664 * file and creates a list with the first cell entry
665 * in the CellServDB file at the end of the list.
667 * No checking is performed for duplicates.
668 * The side effects of this process are that duplicate
669 * entries appended to the end of the CellServDB file
670 * take precedence and are found in a shorter period
675 tp = fgets(tbuffer, sizeof(tbuffer), tf);
678 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
679 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
680 continue; /* empty line */
681 if (tbuffer[0] == '>') {
682 char linkedcell[MAXCELLCHARS];
683 /* start new cell item */
685 /* thread this guy on the list */
686 curEntry->next = adir->entries;
687 adir->entries = curEntry;
691 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
692 memset(curEntry, 0, 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 if (cell && !strcmp(cell, curEntry->cellInfo.name))
714 ParseHostLine(tbuffer,
715 &curEntry->cellInfo.hostAddr[i],
716 curEntry->cellInfo.hostName[i],
720 ParseHostLine(tbuffer,
721 &curEntry->cellInfo.hostAddr[i],
722 curEntry->cellInfo.hostName[i], 0);
725 if (code == AFSCONF_SYNTAX) {
726 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
732 "Can't properly parse host line \"%s\" in configuration file %s\n",
733 tbuffer, cellservDB);
737 afsconf_CloseInternal(adir);
740 curEntry->cellInfo.numServers = ++i;
743 "Too many hosts for cell %s in configuration file %s\n",
744 curEntry->cellInfo.name, cellservDB);
748 fclose(tf); /* close the file now */
751 /* end the last partially-completed cell */
753 curEntry->next = adir->entries;
754 adir->entries = curEntry;
759 * Windows maintains a CellServDB list in the Registry
760 * that supercedes the contents of the CellServDB file.
761 * Prepending these entries to the head of the list
762 * is sufficient to enforce the precedence.
764 cm_EnumerateCellRegistry( enumCellRegistry.client,
765 cm_enumCellRegistryProc,
767 #endif /* AFS_NT40_ENV */
769 /* Read in the alias list */
770 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
772 tf = fopen(tbuffer, "r");
776 tp = fgets(tbuffer, sizeof(tbuffer), tf);
779 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
781 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
782 continue; /* empty line */
785 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
788 continue; /* invalid line */
790 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
793 continue; /* invalid line */
796 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
801 curAlias = malloc(sizeof(*curAlias));
802 memset(curAlias, 0, sizeof(*curAlias));
804 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
805 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
807 curAlias->next = adir->alias_entries;
808 adir->alias_entries = curAlias;
813 /* now read the fs keys, if possible */
815 _afsconf_InitKeys(adir);
816 code = _afsconf_LoadKeys(adir);
821 /* parse a line of the form
822 *"128.2.1.3 #hostname" or
823 *"[128.2.1.3] #hostname" for clones
824 * into the appropriate pieces.
827 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
837 /* FIXME: length of aname unknown here */
838 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
842 /* FIXME: length of aname unknown here */
843 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
846 return AFSCONF_SYNTAX;
847 addr->sin_family = AF_INET;
849 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
850 addr->sin_len = sizeof(struct sockaddr_in);
852 tp = (char *)&addr->sin_addr;
860 /* parse a line of the form
861 * ">cellname [linkedcellname] [#comments]"
862 * into the appropriate pieces.
865 ParseCellLine(char *aline, char *aname,
869 /* FIXME: length of aname, alname unknown here */
870 code = sscanf(aline, ">%s %s", aname, alname);
874 if (*alname == '#') {
878 return (code > 0 ? 0 : AFSCONF_SYNTAX);
881 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
883 afsconf_CellApply(struct afsconf_dir *adir,
884 int (*aproc) (struct afsconf_cell * cell, void *arock,
885 struct afsconf_dir * dir), void *arock)
887 struct afsconf_entry *tde;
890 for (tde = adir->entries; tde; tde = tde->next) {
891 code = (*aproc) (&tde->cellInfo, arock, adir);
901 /* call aproc(entry, arock, adir) for all cell aliases.
902 * Proc must return 0, or we'll stop early and return the code it returns
905 afsconf_CellAliasApply(struct afsconf_dir *adir,
906 int (*aproc) (struct afsconf_cellalias * alias,
907 void *arock, struct afsconf_dir * dir),
910 struct afsconf_aliasentry *tde;
913 for (tde = adir->alias_entries; tde; tde = tde->next) {
914 code = (*aproc) (&tde->aliasInfo, arock, adir);
924 afs_int32 afsconf_SawCell = 0;
927 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
928 char *aservice, struct afsconf_cell *acellInfo,
934 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
941 cell = (char *)&acellInfo->name;
943 code = afsconf_OpenInternal(adir, cell, clones);
947 #if !defined(AFS_NT40_ENV)
949 afsconf_LookupServer(const char *service, const char *protocol,
950 const char *cellName, unsigned short afsdbPort,
951 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
952 unsigned short ports[], unsigned short ipRanks[],
953 int *numServers, int *ttl, char **arealCellName)
957 unsigned char answer[1024];
961 int cellnamelength, fullnamelength;
968 char *IANAname = (char *) afsconf_FindIANAName(service);
969 int tservice = afsconf_FindService(service);
975 if (tservice <= 0 || !IANAname)
976 return AFSCONF_NOTFOUND; /* service not found */
978 if (strchr(cellName,'.'))
981 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
982 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
983 dotcellname=malloc(fullnamelength);
985 return AFSCONF_NOTFOUND; /* service not found */
987 #ifdef HAVE_RES_RETRANSRETRY
988 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
992 * Rx timeout is typically 56 seconds; limit user experience to
1003 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1004 IANAname, protocol, cellName);
1008 code = snprintf(dotcellname, fullnamelength, "%s.",
1013 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1014 IANAname, protocol, cellName);
1018 code = snprintf(dotcellname, fullnamelength, "%s",
1022 if ((code < 0) || (code >= fullnamelength))
1023 goto findservererror;
1025 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1026 UNLOCK_GLOBAL_MUTEX;
1038 code = AFSCONF_NOTFOUND;
1039 goto findservererror;
1043 p = answer + sizeof(HEADER); /* Skip header */
1044 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1046 code = AFSCONF_NOTFOUND;
1047 goto findservererror;
1050 p += code + QFIXEDSZ; /* Skip name */
1052 while (p < answer + len) {
1053 int type, ttl, size;
1055 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1057 code = AFSCONF_NOTFOUND;
1058 goto findservererror;
1061 p += code; /* Skip the name */
1062 type = (p[0] << 8) | p[1];
1063 p += 4; /* Skip type and class */
1064 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1065 p += 4; /* Skip the TTL */
1066 size = (p[0] << 8) | p[1];
1067 p += 2; /* Skip the size */
1069 if (type == T_AFSDB) {
1073 afsdb_type = (p[0] << 8) | p[1];
1074 if (afsdb_type == 1) {
1076 * We know this is an AFSDB record for our cell, of the
1077 * right AFSDB type. Write down the true cell name that
1078 * the resolver gave us above.
1081 realCellName = strdup(host);
1084 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1086 code = AFSCONF_NOTFOUND;
1087 goto findservererror;
1090 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1091 /* Do we want to get TTL data for the A record as well? */
1092 (he = gethostbyname(host))) {
1094 memcpy(&ipaddr, he->h_addr, he->h_length);
1095 cellHostAddrs[server_num] = ipaddr;
1096 ports[server_num] = afsdbPort;
1097 ipRanks[server_num] = 0;
1098 strncpy(cellHostNames[server_num], host,
1099 sizeof(cellHostNames[server_num]));
1102 if (!minttl || ttl < minttl)
1106 if (type == T_SRV) {
1108 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1109 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1110 (strncmp(host + strlen(IANAname) + 3, protocol,
1111 strlen(protocol)) == 0)) {
1113 realCellName = strdup(host + strlen(IANAname) +
1114 strlen(protocol) + 4);
1117 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1119 code = AFSCONF_NOTFOUND;
1120 goto findservererror;
1123 if ((server_num < MAXHOSTSPERCELL) &&
1124 /* Do we want to get TTL data for the A record as well? */
1125 (he = gethostbyname(host))) {
1127 memcpy(&ipaddr, he->h_addr, he->h_length);
1128 cellHostAddrs[server_num] = ipaddr;
1129 ipRanks[server_num] = (p[0] << 8) | p[1];
1130 ports[server_num] = htons((p[4] << 8) | p[5]);
1131 /* weight = (p[2] << 8) | p[3]; */
1132 strncpy(cellHostNames[server_num], host,
1133 sizeof(cellHostNames[server_num]));
1136 if (!minttl || ttl < minttl)
1144 if (server_num == 0) { /* No AFSDB or SRV records */
1145 code = AFSCONF_NOTFOUND;
1146 goto findservererror;
1150 /* Convert the real cell name to lowercase */
1151 for (p = (unsigned char *)realCellName; *p; p++)
1155 *numServers = server_num;
1156 *ttl = minttl ? (time(0) + minttl) : 0;
1158 if ( *numServers > 0 ) {
1160 *arealCellName = realCellName;
1162 code = AFSCONF_NOTFOUND;
1165 if (code && realCellName)
1172 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1173 struct afsconf_cell *acellInfo)
1175 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1176 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1177 unsigned short ipRanks[AFSMAXCELLHOSTS];
1178 unsigned short ports[AFSMAXCELLHOSTS];
1179 char *realCellName = NULL;
1180 int ttl, numServers, i;
1181 char *service = aservice;
1183 unsigned short afsdbport;
1185 service = "afs3-vlserver";
1186 afsdbport = htons(7003);
1189 afsdbport = afsconf_FindService(service);
1191 code = afsconf_LookupServer((const char *)service, "udp",
1192 (const char *)acellName, afsdbport,
1193 cellHostAddrs, cellHostNames,
1194 ports, ipRanks, &numServers, &ttl,
1197 /* If we couldn't find an entry for the requested service
1198 * and that service happens to be the prservice or kaservice
1199 * then fallback to searching for afs3-vlserver and assigning
1200 * the port number here. */
1201 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1202 code = afsconf_LookupServer("afs3-vlserver", "udp",
1203 (const char *)acellName, afsdbport,
1204 cellHostAddrs, cellHostNames,
1205 ports, ipRanks, &numServers, &ttl,
1208 for (i = 0; i < numServers; i++)
1209 ports[i] = afsdbport;
1213 acellInfo->timeout = ttl;
1214 acellInfo->numServers = numServers;
1215 for (i = 0; i < numServers; i++) {
1216 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1218 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1219 acellInfo->hostAddr[i].sin_family = AF_INET;
1220 acellInfo->hostAddr[i].sin_port = ports[i];
1223 strlcpy(acellInfo->name, realCellName,
1224 sizeof(acellInfo->name));
1226 realCellName = NULL;
1229 acellInfo->linkedCell = NULL; /* no linked cell */
1230 acellInfo->flags = 0;
1236 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1237 struct afsconf_cell *acellInfo)
1240 int tservice = afsconf_FindService(aservice); /* network byte order */
1241 const char *ianaName = afsconf_FindIANAName(aservice);
1242 struct afsconf_entry DNSce;
1243 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1244 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1245 unsigned short ipRanks[AFSMAXCELLHOSTS];
1246 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1253 return AFSCONF_NOTFOUND;
1255 tservice = 0; /* port will be assigned by caller */
1258 if (ianaName == NULL)
1259 ianaName = "afs3-vlserver";
1261 DNSce.cellInfo.numServers = 0;
1264 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1265 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1267 /* ignore the ttl here since this code is only called by transitory programs
1268 * like klog, etc. */
1270 /* If we couldn't find an entry for the requested service
1271 * and that service happens to be the prservice or kaservice
1272 * then fallback to searching for afs3-vlserver and assigning
1273 * the port number here. */
1274 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1275 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1276 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1279 for (i = 0; i < numServers; i++)
1280 ports[i] = tservice;
1284 if (rc < 0 || numServers == 0)
1287 for (i = 0; i < numServers; i++) {
1288 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1290 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1291 acellInfo->hostAddr[i].sin_family = AF_INET;
1293 acellInfo->hostAddr[i].sin_port = ports[i];
1295 acellInfo->hostAddr[i].sin_port = 0;
1298 acellInfo->numServers = numServers;
1299 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1300 acellInfo->linkedCell = NULL; /* no linked cell */
1301 acellInfo->flags = 0;
1304 #endif /* windows */
1307 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1308 struct afsconf_cell *acellInfo)
1310 struct afsconf_entry *tce;
1311 struct afsconf_aliasentry *tcae;
1312 struct afsconf_entry *bestce;
1322 _afsconf_Check(adir);
1325 cnLen = (int)(strlen(tcell) + 1);
1326 lcstring(tcell, tcell, cnLen);
1327 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1328 /* call to afsconf_GetLocalCell: like klog */
1330 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1332 UNLOCK_GLOBAL_MUTEX;
1337 cnLen = strlen(tcell);
1338 bestce = (struct afsconf_entry *)0;
1341 UNLOCK_GLOBAL_MUTEX;
1345 /* Look through the list of aliases */
1346 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1347 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1348 tcell = tcae->aliasInfo.realName;
1353 for (tce = adir->entries; tce; tce = tce->next) {
1354 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1355 /* found our cell */
1360 if (strlen(tce->cellInfo.name) < cnLen)
1361 continue; /* clearly wrong */
1362 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1364 ambig = 1; /* ambiguous unless we get exact match */
1368 if (!ambig && bestce && bestce->cellInfo.numServers) {
1369 *acellInfo = bestce->cellInfo; /* structure assignment */
1371 tservice = afsconf_FindService(aservice);
1373 UNLOCK_GLOBAL_MUTEX;
1374 return AFSCONF_NOTFOUND; /* service not found */
1376 for (i = 0; i < acellInfo->numServers; i++) {
1377 acellInfo->hostAddr[i].sin_port = tservice;
1380 acellInfo->timeout = 0;
1383 * Until we figure out how to separate out ubik server
1384 * queries from other server queries, only perform gethostbyname()
1385 * lookup on the specified hostnames for the client CellServDB files.
1387 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1388 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1390 short numServers=0; /*Num active servers for the cell */
1391 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1392 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1394 memset(&hostAddr, 0, sizeof(hostAddr));
1395 memset(&hostName, 0, sizeof(hostName));
1397 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1398 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1401 if (he && he->h_addrtype == AF_INET) {
1403 /* obtain all the valid address from the list */
1404 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1405 /* check to see if this is a new address; if so insert it into the list */
1407 for (k=0, dup=0; !dup && k < numServers; k++) {
1408 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1414 hostAddr[numServers].sin_family = AF_INET;
1415 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1416 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1417 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1419 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1420 strcpy(hostName[numServers], acellInfo->hostName[j]);
1426 hostAddr[numServers] = acellInfo->hostAddr[j];
1427 strcpy(hostName[numServers], acellInfo->hostName[j]);
1432 for (i=0; i<numServers; i++) {
1433 acellInfo->hostAddr[i] = hostAddr[i];
1434 strcpy(acellInfo->hostName[i], hostName[i]);
1436 acellInfo->numServers = numServers;
1437 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1439 UNLOCK_GLOBAL_MUTEX;
1442 UNLOCK_GLOBAL_MUTEX;
1443 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1448 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1451 static int afsconf_showcell = 0;
1457 * If a cell switch was specified in a command, then it should override the
1458 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1459 * is set and the cell name in the adir structure is used.
1460 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1462 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1463 if (!afsconf_showcell) {
1464 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1466 afsconf_showcell = 1;
1468 strncpy(aname, afscell_path, alen);
1470 _afsconf_Check(adir);
1471 if (adir->cellName) {
1472 strncpy(aname, adir->cellName, alen);
1474 code = AFSCONF_UNKNOWN;
1477 UNLOCK_GLOBAL_MUTEX;
1482 afsconf_Close(struct afsconf_dir *adir)
1485 afsconf_CloseInternal(adir);
1489 UNLOCK_GLOBAL_MUTEX;
1494 afsconf_CloseInternal(struct afsconf_dir *adir)
1496 struct afsconf_entry *td, *nd;
1497 struct afsconf_aliasentry *ta, *na;
1500 tname = adir->name; /* remember name, since that's all we preserve */
1502 /* free everything we can find */
1504 free(adir->cellName);
1505 for (td = adir->entries; td; td = nd) {
1507 if (td->cellInfo.linkedCell)
1508 free(td->cellInfo.linkedCell);
1511 for (ta = adir->alias_entries; ta; ta = na) {
1516 _afsconf_FreeAllKeys(adir);
1519 memset(adir, 0, sizeof(struct afsconf_dir));
1520 adir->name = tname; /* restore it */
1525 afsconf_Reopen(struct afsconf_dir *adir)
1528 code = afsconf_CloseInternal(adir);
1531 code = afsconf_OpenInternal(adir, 0, 0);