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)
354 time_t now = time(0);
356 if (adir->timeCheck == now) {
357 return 1; /* stat no more than once a second */
359 adir->timeCheck = now;
361 _afsconf_CellServDBPath(adir, &cellservDB);
362 if (cellservDB == NULL)
365 code = stat(cellservDB, &tstat);
368 return 0; /* Can't throw the error, so just say we're not up to date */
370 /* did file change? */
371 if (tstat.st_mtime == adir->timeRead)
374 /* otherwise file has changed */
379 afsconf_UpToDate(void *rock)
384 code = _afsconf_UpToDate(rock);
391 _afsconf_Check(struct afsconf_dir *adir)
393 /* did configuration change? */
394 if (_afsconf_UpToDate(adir))
397 /* otherwise file has changed, so reopen it */
398 return afsconf_Reopen(adir);
401 /* set modtime on file */
403 _afsconf_Touch(struct afsconf_dir *adir)
408 struct timeval tvp[2];
411 adir->timeRead = 0; /* just in case */
414 _afsconf_CellServDBPath(adir, &cellservDB);
415 if (cellservDB == NULL)
419 code = _utime(cellservDB, NULL);
421 gettimeofday(&tvp[0], NULL);
423 code = utimes(cellservDB, tvp);
424 #endif /* AFS_NT40_ENV */
431 afsconf_Open(const char *adir)
433 struct afsconf_dir *tdir;
437 /* zero structure and fill in name; rest is done by internal routine */
438 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
439 memset(tdir, 0, 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... */
454 if (!(home_dir = getenv("HOME"))) {
455 /* Our last chance is the "/.AFSCONF" file */
456 fp = fopen("/.AFSCONF", "r");
460 return (struct afsconf_dir *)0;
462 fgets(afs_confdir, 128, fp);
467 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
468 fp = fopen(pathname, "r");
470 /* Our last chance is the "/.AFSCONF" file */
471 fp = fopen("/.AFSCONF", "r");
475 return (struct afsconf_dir *)0;
478 fgets(afs_confdir, 128, fp);
481 len = strlen(afs_confdir);
485 return (struct afsconf_dir *)0;
487 if (afs_confdir[len - 1] == '\n') {
488 afs_confdir[len - 1] = 0;
490 afsconf_path = afs_confdir;
492 tdir->name = strdup(afsconf_path);
493 code = afsconf_OpenInternal(tdir, 0, 0);
498 return (struct afsconf_dir *)0;
506 GetCellUnix(struct afsconf_dir *adir)
513 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
514 fp = fopen(tbuffer, "r");
518 rc = fgets(tbuffer, 256, fp);
524 while (*start != '\0' && isspace(*start))
527 while (*p != '\0' && !isspace(*p))
533 adir->cellName = strdup(start);
540 GetCellNT(struct afsconf_dir *adir)
542 if (_afsconf_IsClientConfigDirectory(adir->name)) {
543 /* NT client config dir; ThisCell is in registry (no file). */
544 return afssw_GetClientCellName(&adir->cellName);
546 /* NT server config dir; works just like Unix */
547 return GetCellUnix(adir);
551 /* The following procedures and structs are used on Windows only
552 * to enumerate the Cell information distributed within the
553 * Windows registry. (See src/WINNT/afsd/cm_config.c)
555 typedef struct _cm_enumCellRegistry {
556 afs_uint32 client; /* non-zero if client query */
557 struct afsconf_dir *adir;
558 } cm_enumCellRegistry_t;
561 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
562 char *hostNamep, unsigned short rank)
564 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
566 if (cellInfop->numServers == MAXHOSTSPERCELL)
569 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
570 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
571 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
572 cellInfop->numServers++;
578 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
581 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
582 char linkedName[256] = "";
584 struct afsconf_entry *newEntry;
587 newEntry = malloc(sizeof(struct afsconf_entry));
588 if (newEntry == NULL)
590 newEntry->cellInfo.numServers = 0;
592 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
593 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
594 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
597 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
598 newEntry->cellInfo.name[MAXCELLCHARS-1];
600 newEntry->cellInfo.linkedCell = strdup(linkedName);
602 newEntry->cellInfo.linkedCell = NULL;
603 newEntry->cellInfo.timeout = timeout;
604 newEntry->cellInfo.flags = 0;
606 newEntry->next = enump->adir->entries;
607 enump->adir->entries = newEntry;
613 #endif /* AFS_NT40_ENV */
617 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
622 struct afsconf_entry *curEntry;
623 struct afsconf_aliasentry *curAlias;
631 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
632 #endif /* AFS_NT40_ENV */
634 /* figure out the local cell name */
637 enumCellRegistry.adir = adir;
639 i = GetCellUnix(adir);
642 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
648 /* now parse the individual lines */
651 _afsconf_CellServDBPath(adir, &cellservDB);
654 if (_afsconf_IsClientConfigDirectory(adir->name))
655 enumCellRegistry.client = 1;
656 #endif /* AFS_NT40_ENV */
658 if (!stat(cellservDB, &tstat)) {
659 adir->timeRead = tstat.st_mtime;
664 tf = fopen(cellservDB, "r");
669 /* init the keys queue before any call to afsconf_CloseInternal() */
670 _afsconf_InitKeys(adir);
672 /* The CellServDB file is now open.
673 * The following code parses the contents of the
674 * file and creates a list with the first cell entry
675 * in the CellServDB file at the end of the list.
677 * No checking is performed for duplicates.
678 * The side effects of this process are that duplicate
679 * entries appended to the end of the CellServDB file
680 * take precedence and are found in a shorter period
685 tp = fgets(tbuffer, sizeof(tbuffer), tf);
688 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
689 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
690 continue; /* empty line */
691 if (tbuffer[0] == '>') {
692 char linkedcell[MAXCELLCHARS];
693 /* start new cell item */
695 /* thread this guy on the list */
696 curEntry->next = adir->entries;
697 adir->entries = curEntry;
701 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
702 memset(curEntry, 0, sizeof(struct afsconf_entry));
704 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
706 afsconf_CloseInternal(adir);
711 if (linkedcell[0] != '\0')
712 curEntry->cellInfo.linkedCell = strdup(linkedcell);
714 /* new host in the current cell */
716 afsconf_CloseInternal(adir);
720 i = curEntry->cellInfo.numServers;
721 if (i < MAXHOSTSPERCELL) {
722 if (cell && !strcmp(cell, curEntry->cellInfo.name))
724 ParseHostLine(tbuffer,
725 &curEntry->cellInfo.hostAddr[i],
726 curEntry->cellInfo.hostName[i],
730 ParseHostLine(tbuffer,
731 &curEntry->cellInfo.hostAddr[i],
732 curEntry->cellInfo.hostName[i], 0);
735 if (code == AFSCONF_SYNTAX) {
736 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
742 "Can't properly parse host line \"%s\" in configuration file %s\n",
743 tbuffer, cellservDB);
747 afsconf_CloseInternal(adir);
750 curEntry->cellInfo.numServers = ++i;
753 "Too many hosts for cell %s in configuration file %s\n",
754 curEntry->cellInfo.name, cellservDB);
758 fclose(tf); /* close the file now */
761 /* end the last partially-completed cell */
763 curEntry->next = adir->entries;
764 adir->entries = curEntry;
769 * Windows maintains a CellServDB list in the Registry
770 * that supercedes the contents of the CellServDB file.
771 * Prepending these entries to the head of the list
772 * is sufficient to enforce the precedence.
774 cm_EnumerateCellRegistry( enumCellRegistry.client,
775 cm_enumCellRegistryProc,
777 #endif /* AFS_NT40_ENV */
779 /* Read in the alias list */
780 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
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 = malloc(sizeof(*curAlias));
812 memset(curAlias, 0, sizeof(*curAlias));
814 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
815 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
817 curAlias->next = adir->alias_entries;
818 adir->alias_entries = curAlias;
824 /* now read the fs keys, if possible */
825 code = _afsconf_LoadKeys(adir);
829 code = _afsconf_LoadRealms(adir);
834 /* parse a line of the form
835 *"128.2.1.3 #hostname" or
836 *"[128.2.1.3] #hostname" for clones
837 * into the appropriate pieces.
840 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", &c1, &c2, &c3, &c4, aname);
855 /* FIXME: length of aname unknown here */
856 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
859 return AFSCONF_SYNTAX;
860 addr->sin_family = AF_INET;
862 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
863 addr->sin_len = sizeof(struct sockaddr_in);
865 tp = (char *)&addr->sin_addr;
873 /* parse a line of the form
874 * ">cellname [linkedcellname] [#comments]"
875 * into the appropriate pieces.
878 ParseCellLine(char *aline, char *aname,
882 /* FIXME: length of aname, alname unknown here */
883 code = sscanf(aline, ">%s %s", aname, alname);
887 if (*alname == '#') {
891 return (code > 0 ? 0 : AFSCONF_SYNTAX);
894 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
896 afsconf_CellApply(struct afsconf_dir *adir,
897 int (*aproc) (struct afsconf_cell * cell, void *arock,
898 struct afsconf_dir * dir), void *arock)
900 struct afsconf_entry *tde;
903 for (tde = adir->entries; tde; tde = tde->next) {
904 code = (*aproc) (&tde->cellInfo, arock, adir);
914 /* call aproc(entry, arock, adir) for all cell aliases.
915 * Proc must return 0, or we'll stop early and return the code it returns
918 afsconf_CellAliasApply(struct afsconf_dir *adir,
919 int (*aproc) (struct afsconf_cellalias * alias,
920 void *arock, struct afsconf_dir * dir),
923 struct afsconf_aliasentry *tde;
926 for (tde = adir->alias_entries; tde; tde = tde->next) {
927 code = (*aproc) (&tde->aliasInfo, arock, adir);
937 afs_int32 afsconf_SawCell = 0;
940 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
941 char *aservice, struct afsconf_cell *acellInfo,
947 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
954 cell = (char *)&acellInfo->name;
956 code = afsconf_OpenInternal(adir, cell, clones);
960 #if !defined(AFS_NT40_ENV)
962 afsconf_LookupServer(const char *service, const char *protocol,
963 const char *cellName, unsigned short afsdbPort,
964 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
965 unsigned short ports[], unsigned short ipRanks[],
966 int *numServers, int *ttl, char **arealCellName)
970 unsigned char answer[1024];
974 int cellnamelength, fullnamelength;
981 char *IANAname = (char *) afsconf_FindIANAName(service);
982 int tservice = afsconf_FindService(service);
988 if (tservice <= 0 || !IANAname)
989 return AFSCONF_NOTFOUND; /* service not found */
991 if (strchr(cellName,'.'))
994 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
995 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
996 dotcellname=malloc(fullnamelength);
998 return AFSCONF_NOTFOUND; /* service not found */
1000 #ifdef HAVE_RES_RETRANSRETRY
1001 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1005 * Rx timeout is typically 56 seconds; limit user experience to
1016 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1017 IANAname, protocol, cellName);
1021 code = snprintf(dotcellname, fullnamelength, "%s.",
1026 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1027 IANAname, protocol, cellName);
1031 code = snprintf(dotcellname, fullnamelength, "%s",
1035 if ((code < 0) || (code >= fullnamelength))
1036 goto findservererror;
1038 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1039 UNLOCK_GLOBAL_MUTEX;
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)
1189 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1190 struct afsconf_cell *acellInfo)
1192 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1193 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1194 unsigned short ipRanks[AFSMAXCELLHOSTS];
1195 unsigned short ports[AFSMAXCELLHOSTS];
1196 char *realCellName = NULL;
1197 int ttl, numServers, i;
1198 char *service = aservice;
1200 unsigned short afsdbport;
1202 service = "afs3-vlserver";
1203 afsdbport = htons(7003);
1206 afsdbport = afsconf_FindService(service);
1208 code = afsconf_LookupServer((const char *)service, "udp",
1209 (const char *)acellName, afsdbport,
1210 cellHostAddrs, cellHostNames,
1211 ports, ipRanks, &numServers, &ttl,
1214 /* If we couldn't find an entry for the requested service
1215 * and that service happens to be the prservice or kaservice
1216 * then fallback to searching for afs3-vlserver and assigning
1217 * the port number here. */
1218 if (code < 0 && (afsdbport == htons(7002) || afsdbport == htons(7004))) {
1219 code = afsconf_LookupServer("afs3-vlserver", "udp",
1220 (const char *)acellName, afsdbport,
1221 cellHostAddrs, cellHostNames,
1222 ports, ipRanks, &numServers, &ttl,
1225 for (i = 0; i < numServers; i++)
1226 ports[i] = afsdbport;
1230 acellInfo->timeout = ttl;
1231 acellInfo->numServers = numServers;
1232 for (i = 0; i < numServers; i++) {
1233 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1235 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1236 acellInfo->hostAddr[i].sin_family = AF_INET;
1237 acellInfo->hostAddr[i].sin_port = ports[i];
1240 strlcpy(acellInfo->name, realCellName,
1241 sizeof(acellInfo->name));
1243 realCellName = NULL;
1246 acellInfo->linkedCell = NULL; /* no linked cell */
1247 acellInfo->flags = 0;
1253 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1254 struct afsconf_cell *acellInfo)
1257 int tservice = afsconf_FindService(aservice); /* network byte order */
1258 const char *ianaName = afsconf_FindIANAName(aservice);
1259 struct afsconf_entry DNSce;
1260 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1261 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1262 unsigned short ipRanks[AFSMAXCELLHOSTS];
1263 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1270 return AFSCONF_NOTFOUND;
1272 tservice = 0; /* port will be assigned by caller */
1275 if (ianaName == NULL)
1276 ianaName = "afs3-vlserver";
1278 DNSce.cellInfo.numServers = 0;
1281 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1282 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1284 /* ignore the ttl here since this code is only called by transitory programs
1285 * like klog, etc. */
1287 /* If we couldn't find an entry for the requested service
1288 * and that service happens to be the prservice or kaservice
1289 * then fallback to searching for afs3-vlserver and assigning
1290 * the port number here. */
1291 if (rc < 0 && (tservice == htons(7002) || tservice == htons(7004))) {
1292 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1293 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1296 for (i = 0; i < numServers; i++)
1297 ports[i] = tservice;
1301 if (rc < 0 || numServers == 0)
1304 for (i = 0; i < numServers; i++) {
1305 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1307 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1308 acellInfo->hostAddr[i].sin_family = AF_INET;
1310 acellInfo->hostAddr[i].sin_port = ports[i];
1312 acellInfo->hostAddr[i].sin_port = 0;
1315 acellInfo->numServers = numServers;
1316 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1317 acellInfo->linkedCell = NULL; /* no linked cell */
1318 acellInfo->flags = 0;
1321 #endif /* windows */
1324 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1325 struct afsconf_cell *acellInfo)
1327 struct afsconf_entry *tce;
1328 struct afsconf_aliasentry *tcae;
1329 struct afsconf_entry *bestce;
1339 _afsconf_Check(adir);
1342 cnLen = (int)(strlen(tcell) + 1);
1343 lcstring(tcell, tcell, cnLen);
1344 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1345 /* call to afsconf_GetLocalCell: like klog */
1347 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1349 UNLOCK_GLOBAL_MUTEX;
1354 cnLen = strlen(tcell);
1355 bestce = (struct afsconf_entry *)0;
1358 UNLOCK_GLOBAL_MUTEX;
1362 /* Look through the list of aliases */
1363 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1364 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1365 tcell = tcae->aliasInfo.realName;
1370 for (tce = adir->entries; tce; tce = tce->next) {
1371 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1372 /* found our cell */
1377 if (strlen(tce->cellInfo.name) < cnLen)
1378 continue; /* clearly wrong */
1379 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1381 ambig = 1; /* ambiguous unless we get exact match */
1385 if (!ambig && bestce && bestce->cellInfo.numServers) {
1386 *acellInfo = bestce->cellInfo; /* structure assignment */
1388 tservice = afsconf_FindService(aservice);
1390 UNLOCK_GLOBAL_MUTEX;
1391 return AFSCONF_NOTFOUND; /* service not found */
1393 for (i = 0; i < acellInfo->numServers; i++) {
1394 acellInfo->hostAddr[i].sin_port = tservice;
1397 acellInfo->timeout = 0;
1400 * Until we figure out how to separate out ubik server
1401 * queries from other server queries, only perform gethostbyname()
1402 * lookup on the specified hostnames for the client CellServDB files.
1404 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1405 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1407 short numServers=0; /*Num active servers for the cell */
1408 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1409 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1411 memset(&hostAddr, 0, sizeof(hostAddr));
1412 memset(&hostName, 0, sizeof(hostName));
1414 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1415 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1418 if (he && he->h_addrtype == AF_INET) {
1420 /* obtain all the valid address from the list */
1421 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1422 /* check to see if this is a new address; if so insert it into the list */
1424 for (k=0, dup=0; !dup && k < numServers; k++) {
1425 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1431 hostAddr[numServers].sin_family = AF_INET;
1432 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1433 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1434 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1436 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1437 strcpy(hostName[numServers], acellInfo->hostName[j]);
1443 hostAddr[numServers] = acellInfo->hostAddr[j];
1444 strcpy(hostName[numServers], acellInfo->hostName[j]);
1449 for (i=0; i<numServers; i++) {
1450 acellInfo->hostAddr[i] = hostAddr[i];
1451 strcpy(acellInfo->hostName[i], hostName[i]);
1453 acellInfo->numServers = numServers;
1454 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1456 UNLOCK_GLOBAL_MUTEX;
1459 UNLOCK_GLOBAL_MUTEX;
1460 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1465 * Get the current localcell name.
1467 * Internal function to get a pointer to the local cell name.
1468 * This function must be called with the global afsconf lock held.
1470 * @param[in] adir afsconf object
1471 * @param[out] aname address to a char pointer
1472 * @param[in] check always perform a config check, even if the
1473 * the AFSCELL name is set.
1477 * @retval AFSCONF_UNKNOWN failed to get cellname
1482 _afsconf_GetLocalCell(struct afsconf_dir *adir, char **pname, int check)
1484 static int afsconf_showcell = 0;
1489 * If a cell switch was specified in a command, then it should override the
1490 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1491 * is set and the cell name in the adir structure is used.
1492 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1493 * Optionally, check the configuration, even if using the environment variable.
1495 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1497 _afsconf_Check(adir);
1499 if (!afsconf_showcell) {
1500 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1502 afsconf_showcell = 1;
1504 *pname = afscell_path;
1506 _afsconf_Check(adir);
1507 if (adir->cellName) {
1508 *pname = adir->cellName;
1510 code = AFSCONF_UNKNOWN;
1516 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname, afs_int32 alen)
1519 char *cellname = NULL;
1522 code = _afsconf_GetLocalCell(adir, &cellname, 0);
1523 if (!code && cellname) {
1524 strlcpy(aname, cellname, alen);
1526 UNLOCK_GLOBAL_MUTEX;
1531 afsconf_Close(struct afsconf_dir *adir)
1534 afsconf_CloseInternal(adir);
1538 UNLOCK_GLOBAL_MUTEX;
1543 afsconf_CloseInternal(struct afsconf_dir *adir)
1545 struct afsconf_entry *td, *nd;
1546 struct afsconf_aliasentry *ta, *na;
1549 tname = adir->name; /* remember name, since that's all we preserve */
1551 /* free everything we can find */
1553 free(adir->cellName);
1554 for (td = adir->entries; td; td = nd) {
1556 if (td->cellInfo.linkedCell)
1557 free(td->cellInfo.linkedCell);
1560 for (ta = adir->alias_entries; ta; ta = na) {
1565 _afsconf_FreeAllKeys(adir);
1566 _afsconf_FreeRealms(adir);
1569 memset(adir, 0, sizeof(struct afsconf_dir));
1570 adir->name = tname; /* restore it */
1575 afsconf_Reopen(struct afsconf_dir *adir)
1578 code = afsconf_CloseInternal(adir);
1581 code = afsconf_OpenInternal(adir, 0, 0);