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>
16 #include <afs/pthread_glock.h>
17 #include <sys/types.h>
20 #include <sys/utime.h>
22 #include <WINNT/afssw.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
35 #endif /* AFS_NT40_ENV */
36 #include <afs/afsint.h>
51 #include <afs/afsutil.h>
53 #include "cellconfig.h"
58 #include <cm_config.h>
59 /* cm_dns.h depends on cellconfig.h */
66 struct afsconf_servPair {
72 static struct afsconf_servPair serviceTable[] = {
73 {"afs", "afs3-fileserver", 7000,},
74 {"afscb", "afs3-callback", 7001,},
75 {"afsprot", "afs3-prserver", 7002,},
76 {"afsvldb", "afs3-vlserver", 7003,},
77 {"afskauth", "afs3-kaserver", 7004,},
78 {"afsvol", "afs3-volserver", 7005,},
79 {"afserror", "afs3-errors", 7006,},
80 {"afsnanny", "afs3-bos", 7007,},
81 {"afsupdate", "afs3-update", 7008,},
82 {"afsrmtsys", "afs3-rmtsys", 7009,},
83 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
84 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
85 {0, 0, 0} /* insert new services before this spot */
89 static int TrimLine(char *abuffer, int abufsize);
91 static int GetCellNT(struct afsconf_dir *adir);
93 static int GetCellUnix(struct afsconf_dir *adir);
94 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
96 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
97 char *aname, char *aclone);
98 static int ParseCellLine(char *aline, char *aname,
100 static int afsconf_CloseInternal(struct afsconf_dir *adir);
101 static int afsconf_Reopen(struct afsconf_dir *adir);
104 #define T_AFSDB 18 /* per RFC1183 section 1 */
107 #define T_SRV 33 /* RFC2782 */
111 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
112 * our code can tell if there is new info in the key files, the cell server db
113 * files or any of the other files (and reopen the thing) if the date on
114 * CellServDB changes.
117 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
118 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
119 get an fd <= 255. We allow the fileserver to claim more fds than that.
120 This has always been a problem since pr_Initialize would have the same
121 issue, but hpr_Initialize makes it more likely that we would see this.
122 Work around it. This is not generic. It's coded with the needs of
123 afsconf_* in mind only.
125 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
130 struct afsconf_iobuffer {
137 typedef struct afsconf_iobuffer afsconf_FILE;
139 static afsconf_FILE *
140 afsconf_fopen(const char *fname, const char *fmode)
145 if ((fd = open(fname, O_RDONLY)) == -1) {
149 iop = malloc(sizeof(struct afsconf_iobuffer));
156 iop->buffer = malloc(BUFFER);
157 if (iop->buffer == NULL) {
163 iop->ptr = iop->buffer;
164 iop->endptr = iop->buffer;
169 afsconf_fclose(afsconf_FILE *iop)
175 free((void *)iop->buffer);
181 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
189 if (iop->ptr == iop->endptr) {
192 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
202 iop->ptr = iop->buffer;
203 iop->endptr = iop->buffer + len;
207 if ((p - s) == (n - 1)) {
217 #define fopen afsconf_fopen
218 #define fclose afsconf_fclose
219 #define fgets afsconf_fgets
221 #define afsconf_FILE FILE
222 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
224 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
226 afsconf_FindService(const char *aname)
228 /* lookup a service name */
230 struct afsconf_servPair *tsp;
232 if (aname == NULL || aname[0] == '\0')
235 #if defined(AFS_OSF_ENV)
236 ts = getservbyname(aname, "");
238 ts = (struct servent *) getservbyname(aname, NULL);
241 /* we found it in /etc/services, so we use this value */
242 return ts->s_port; /* already in network byte order */
245 /* not found in /etc/services, 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 htons(tsp->port);
255 afsconf_FindIANAName(const char *aname)
257 /* lookup a service name */
258 struct afsconf_servPair *tsp;
260 if (aname == NULL || aname[0] == '\0')
263 /* see if it is one of ours */
264 for (tsp = serviceTable; tsp->port; tsp++) {
265 if ((tsp->name && (!strcmp(tsp->name, aname)))
266 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
267 return tsp->ianaName;
273 TrimLine(char *abuffer, int abufsize)
285 strlcpy(tbuffer, tp, sizeof tbuffer);
286 strlcpy(abuffer, tbuffer, abufsize);
291 * IsClientConfigDirectory() -- determine if path matches well-known
292 * client configuration directory.
295 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
296 #else /* AFS_NT40_ENV */
297 #define IS_SEP(x) ((x) == '/')
298 #endif /* AFS_NT40_ENV */
300 _afsconf_IsClientConfigDirectory(const char *path)
302 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
305 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
307 cc = tolower(cdir[i]);
308 pc = tolower(path[i]);
316 #else /* AFS_NT40_ENV */
319 #endif /* AFS_NT40_ENV */
325 /* hit end of one or both; allow mismatch in existence of trailing slash */
326 if (cdir[i] != '\0') {
327 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
331 if (path[i] != '\0') {
332 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
341 _afsconf_Check(struct afsconf_dir *adir)
351 /* NT client CellServDB has different file name than NT server or Unix */
352 if (_afsconf_IsClientConfigDirectory(adir->name)) {
353 if (!afssw_GetClientCellServDBDir(&p)) {
354 strcompose(tbuffer, sizeof(tbuffer), p, "/",
355 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
359 strncpy(tbuffer, adir->name, sizeof(tbuffer));
360 len = (int)strlen(tbuffer);
361 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
362 strncat(tbuffer, "\\", sizeof(tbuffer));
364 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
366 tbuffer[sizeof(tbuffer) - 1] = '\0';
369 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
373 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
374 #endif /* AFS_NT40_ENV */
376 code = stat(tbuffer, &tstat);
380 /* did file change? */
381 if (tstat.st_mtime == adir->timeRead) {
384 /* otherwise file has changed, so reopen it */
385 return afsconf_Reopen(adir);
388 /* set modtime on file */
390 _afsconf_Touch(struct afsconf_dir *adir)
394 struct timeval tvp[2];
399 adir->timeRead = 0; /* just in case */
402 /* NT client CellServDB has different file name than NT server or Unix */
404 if (_afsconf_IsClientConfigDirectory(adir->name)) {
405 if (!afssw_GetClientCellServDBDir(&p)) {
406 strcompose(tbuffer, sizeof(tbuffer), p, "/",
407 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
410 int len = (int)strlen(tbuffer);
411 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
412 strncat(tbuffer, "\\", sizeof(tbuffer));
414 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
416 tbuffer[sizeof(tbuffer) - 1] = '\0';
419 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
423 return _utime(tbuffer, NULL);
426 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
427 gettimeofday(&tvp[0], NULL);
429 return utimes(tbuffer, tvp);
430 #endif /* AFS_NT40_ENV */
434 afsconf_Open(const char *adir)
436 struct afsconf_dir *tdir;
440 /* zero structure and fill in name; rest is done by internal routine */
441 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
442 memset(tdir, 0, sizeof(struct afsconf_dir));
443 tdir->name = strdup(adir);
445 code = afsconf_OpenInternal(tdir, 0, 0);
447 char *afsconf_path, afs_confdir[128];
450 /* Check global place only when local Open failed for whatever reason */
451 if (!(afsconf_path = getenv("AFSCONF"))) {
452 /* 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... */
457 if (!(home_dir = getenv("HOME"))) {
458 /* Our last chance is the "/.AFSCONF" file */
459 fp = fopen("/.AFSCONF", "r");
463 return (struct afsconf_dir *)0;
465 fgets(afs_confdir, 128, fp);
470 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
471 fp = fopen(pathname, "r");
473 /* Our last chance is the "/.AFSCONF" file */
474 fp = fopen("/.AFSCONF", "r");
478 return (struct afsconf_dir *)0;
481 fgets(afs_confdir, 128, fp);
484 len = strlen(afs_confdir);
488 return (struct afsconf_dir *)0;
490 if (afs_confdir[len - 1] == '\n') {
491 afs_confdir[len - 1] = 0;
493 afsconf_path = afs_confdir;
495 tdir->name = strdup(afsconf_path);
496 code = afsconf_OpenInternal(tdir, 0, 0);
501 return (struct afsconf_dir *)0;
509 GetCellUnix(struct afsconf_dir *adir)
516 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
517 fp = fopen(tbuffer, "r");
521 rc = fgets(tbuffer, 256, fp);
527 while (*start != '\0' && isspace(*start))
530 while (*p != '\0' && !isspace(*p))
536 adir->cellName = strdup(start);
543 GetCellNT(struct afsconf_dir *adir)
545 if (_afsconf_IsClientConfigDirectory(adir->name)) {
546 /* NT client config dir; ThisCell is in registry (no file). */
547 return afssw_GetClientCellName(&adir->cellName);
549 /* NT server config dir; works just like Unix */
550 return GetCellUnix(adir);
554 /* The following procedures and structs are used on Windows only
555 * to enumerate the Cell information distributed within the
556 * Windows registry. (See src/WINNT/afsd/cm_config.c)
558 typedef struct _cm_enumCellRegistry {
559 afs_uint32 client; /* non-zero if client query */
560 struct afsconf_dir *adir;
561 } cm_enumCellRegistry_t;
564 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
565 char *hostNamep, unsigned short rank)
567 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
569 if (cellInfop->numServers == MAXHOSTSPERCELL)
572 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
573 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
574 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
575 cellInfop->numServers++;
581 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
584 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
585 char linkedName[256] = "";
587 struct afsconf_entry *newEntry;
590 newEntry = malloc(sizeof(struct afsconf_entry));
591 if (newEntry == NULL)
593 newEntry->cellInfo.numServers = 0;
595 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
596 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
597 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
600 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
601 newEntry->cellInfo.name[MAXCELLCHARS-1];
603 newEntry->cellInfo.linkedCell = strdup(linkedName);
605 newEntry->cellInfo.linkedCell = NULL;
606 newEntry->cellInfo.timeout = timeout;
607 newEntry->cellInfo.flags = 0;
609 newEntry->next = enump->adir->entries;
610 enump->adir->entries = newEntry;
616 #endif /* AFS_NT40_ENV */
620 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
625 struct afsconf_entry *curEntry;
626 struct afsconf_aliasentry *curAlias;
629 char tbuffer[256], tbuf1[256];
632 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
633 #endif /* AFS_NT40_ENV */
635 /* figure out the local cell name */
638 enumCellRegistry.adir = adir;
640 i = GetCellUnix(adir);
643 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
649 /* now parse the individual lines */
653 /* NT client/server have a CellServDB that is the same format as Unix.
654 * However, the NT client uses a different file name
656 if (_afsconf_IsClientConfigDirectory(adir->name)) {
657 /* NT client config dir */
660 enumCellRegistry.client = 1;
662 if (!afssw_GetClientCellServDBDir(&p)) {
663 strcompose(tbuffer, sizeof(tbuffer), p, "/",
664 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
668 strncpy(tbuffer, adir->name, sizeof(tbuffer));
669 len = (int)strlen(tbuffer);
670 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
671 strncat(tbuffer, "\\", sizeof(tbuffer));
673 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
675 tbuffer[sizeof(tbuffer) - 1] = '\0';
678 /* NT server config dir */
679 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
683 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
684 #endif /* AFS_NT40_ENV */
686 if (!stat(tbuffer, &tstat)) {
687 adir->timeRead = tstat.st_mtime;
692 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
693 tf = fopen(tbuffer, "r");
698 /* The CellServDB file is now open.
699 * The following code parses the contents of the
700 * file and creates a list with the first cell entry
701 * in the CellServDB file at the end of the list.
703 * No checking is performed for duplicates.
704 * The side effects of this process are that duplicate
705 * entries appended to the end of the CellServDB file
706 * take precedence and are found in a shorter period
711 tp = fgets(tbuffer, sizeof(tbuffer), tf);
714 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
715 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
716 continue; /* empty line */
717 if (tbuffer[0] == '>') {
718 char linkedcell[MAXCELLCHARS];
719 /* start new cell item */
721 /* thread this guy on the list */
722 curEntry->next = adir->entries;
723 adir->entries = curEntry;
727 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
728 memset(curEntry, 0, sizeof(struct afsconf_entry));
730 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
732 afsconf_CloseInternal(adir);
737 if (linkedcell[0] != '\0')
738 curEntry->cellInfo.linkedCell = strdup(linkedcell);
740 /* new host in the current cell */
742 afsconf_CloseInternal(adir);
746 i = curEntry->cellInfo.numServers;
747 if (i < MAXHOSTSPERCELL) {
748 if (cell && !strcmp(cell, curEntry->cellInfo.name))
750 ParseHostLine(tbuffer,
751 &curEntry->cellInfo.hostAddr[i],
752 curEntry->cellInfo.hostName[i],
756 ParseHostLine(tbuffer,
757 &curEntry->cellInfo.hostAddr[i],
758 curEntry->cellInfo.hostName[i], 0);
761 if (code == AFSCONF_SYNTAX) {
762 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
768 "Can't properly parse host line \"%s\" in configuration file %s\n",
773 afsconf_CloseInternal(adir);
776 curEntry->cellInfo.numServers = ++i;
779 "Too many hosts for cell %s in configuration file %s\n",
780 curEntry->cellInfo.name, tbuf1);
784 fclose(tf); /* close the file now */
786 /* end the last partially-completed cell */
788 curEntry->next = adir->entries;
789 adir->entries = curEntry;
794 * Windows maintains a CellServDB list in the Registry
795 * that supercedes the contents of the CellServDB file.
796 * Prepending these entries to the head of the list
797 * is sufficient to enforce the precedence.
799 cm_EnumerateCellRegistry( enumCellRegistry.client,
800 cm_enumCellRegistryProc,
802 #endif /* AFS_NT40_ENV */
804 /* Read in the alias list */
805 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
807 tf = fopen(tbuffer, "r");
811 tp = fgets(tbuffer, sizeof(tbuffer), tf);
814 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
816 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
817 continue; /* empty line */
820 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
823 continue; /* invalid line */
825 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
828 continue; /* invalid line */
831 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
836 curAlias = malloc(sizeof(*curAlias));
837 memset(curAlias, 0, sizeof(*curAlias));
839 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
840 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
842 curAlias->next = adir->alias_entries;
843 adir->alias_entries = curAlias;
848 /* now read the fs keys, if possible */
849 adir->keystr = (struct afsconf_keys *)0;
850 _afsconf_IntGetKeys(adir);
855 /* parse a line of the form
856 *"128.2.1.3 #hostname" or
857 *"[128.2.1.3] #hostname" for clones
858 * into the appropriate pieces.
861 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
871 /* FIXME: length of aname unknown here */
872 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
876 /* FIXME: length of aname unknown here */
877 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
880 return AFSCONF_SYNTAX;
881 addr->sin_family = AF_INET;
883 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
884 addr->sin_len = sizeof(struct sockaddr_in);
886 tp = (char *)&addr->sin_addr;
894 /* parse a line of the form
895 * ">cellname [linkedcellname] [#comments]"
896 * into the appropriate pieces.
899 ParseCellLine(char *aline, char *aname,
903 /* FIXME: length of aname, alname unknown here */
904 code = sscanf(aline, ">%s %s", aname, alname);
908 if (*alname == '#') {
912 return (code > 0 ? 0 : AFSCONF_SYNTAX);
915 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
917 afsconf_CellApply(struct afsconf_dir *adir,
918 int (*aproc) (struct afsconf_cell * cell, void *arock,
919 struct afsconf_dir * dir), void *arock)
921 struct afsconf_entry *tde;
924 for (tde = adir->entries; tde; tde = tde->next) {
925 code = (*aproc) (&tde->cellInfo, arock, adir);
935 /* call aproc(entry, arock, adir) for all cell aliases.
936 * Proc must return 0, or we'll stop early and return the code it returns
939 afsconf_CellAliasApply(struct afsconf_dir *adir,
940 int (*aproc) (struct afsconf_cellalias * alias,
941 void *arock, struct afsconf_dir * dir),
944 struct afsconf_aliasentry *tde;
947 for (tde = adir->alias_entries; tde; tde = tde->next) {
948 code = (*aproc) (&tde->aliasInfo, arock, adir);
958 afs_int32 afsconf_SawCell = 0;
961 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
962 char *aservice, struct afsconf_cell *acellInfo,
968 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
975 cell = (char *)&acellInfo->name;
977 code = afsconf_OpenInternal(adir, cell, clones);
981 #if !defined(AFS_NT40_ENV)
983 afsconf_LookupServer(const char *service, const char *protocol,
984 const char *cellName, unsigned short afsdbPort,
985 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
986 unsigned short ports[], unsigned short ipRanks[],
987 int *numServers, int *ttl, char **arealCellName)
991 unsigned char answer[1024];
995 int cellnamelength, fullnamelength;
1002 char *IANAname = (char *) afsconf_FindIANAName(service);
1003 int tservice = afsconf_FindService(service);
1005 realCellName = NULL;
1009 if (tservice <= 0 || !IANAname)
1010 return AFSCONF_NOTFOUND; /* service not found */
1012 if (strchr(cellName,'.'))
1015 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1016 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1017 dotcellname=malloc(fullnamelength);
1019 return AFSCONF_NOTFOUND; /* service not found */
1021 #ifdef HAVE_RES_RETRANSRETRY
1022 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1026 * Rx timeout is typically 56 seconds; limit user experience to
1037 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1038 IANAname, protocol, cellName);
1042 code = snprintf(dotcellname, fullnamelength, "%s.",
1047 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1048 IANAname, protocol, cellName);
1052 code = snprintf(dotcellname, fullnamelength, "%s",
1056 if ((code < 0) || (code >= fullnamelength))
1057 goto findservererror;
1059 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1060 UNLOCK_GLOBAL_MUTEX;
1072 code = AFSCONF_NOTFOUND;
1073 goto findservererror;
1077 p = answer + sizeof(HEADER); /* Skip header */
1078 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1080 code = AFSCONF_NOTFOUND;
1081 goto findservererror;
1084 p += code + QFIXEDSZ; /* Skip name */
1086 while (p < answer + len) {
1087 int type, ttl, size;
1089 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1091 code = AFSCONF_NOTFOUND;
1092 goto findservererror;
1095 p += code; /* Skip the name */
1096 type = (p[0] << 8) | p[1];
1097 p += 4; /* Skip type and class */
1098 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1099 p += 4; /* Skip the TTL */
1100 size = (p[0] << 8) | p[1];
1101 p += 2; /* Skip the size */
1103 if (type == T_AFSDB) {
1107 afsdb_type = (p[0] << 8) | p[1];
1108 if (afsdb_type == 1) {
1110 * We know this is an AFSDB record for our cell, of the
1111 * right AFSDB type. Write down the true cell name that
1112 * the resolver gave us above.
1115 realCellName = strdup(host);
1118 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1120 code = AFSCONF_NOTFOUND;
1121 goto findservererror;
1124 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1125 /* Do we want to get TTL data for the A record as well? */
1126 (he = gethostbyname(host))) {
1128 memcpy(&ipaddr, he->h_addr, he->h_length);
1129 cellHostAddrs[server_num] = ipaddr;
1130 ports[server_num] = afsdbPort;
1131 ipRanks[server_num] = 0;
1132 strncpy(cellHostNames[server_num], host,
1133 sizeof(cellHostNames[server_num]));
1136 if (!minttl || ttl < minttl)
1140 if (type == T_SRV) {
1142 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1143 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1144 (strncmp(host + strlen(IANAname) + 3, protocol,
1145 strlen(protocol)) == 0)) {
1147 realCellName = strdup(host + strlen(IANAname) +
1148 strlen(protocol) + 4);
1151 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1153 code = AFSCONF_NOTFOUND;
1154 goto findservererror;
1157 if ((server_num < MAXHOSTSPERCELL) &&
1158 /* Do we want to get TTL data for the A record as well? */
1159 (he = gethostbyname(host))) {
1161 memcpy(&ipaddr, he->h_addr, he->h_length);
1162 cellHostAddrs[server_num] = ipaddr;
1163 ipRanks[server_num] = (p[0] << 8) | p[1];
1164 ports[server_num] = htons((p[4] << 8) | p[5]);
1165 /* weight = (p[2] << 8) | p[3]; */
1166 strncpy(cellHostNames[server_num], host,
1167 sizeof(cellHostNames[server_num]));
1170 if (!minttl || ttl < minttl)
1178 if (server_num == 0) { /* No AFSDB or SRV records */
1179 code = AFSCONF_NOTFOUND;
1180 goto findservererror;
1184 /* Convert the real cell name to lowercase */
1185 for (p = (unsigned char *)realCellName; *p; p++)
1189 *numServers = server_num;
1190 *ttl = minttl ? (time(0) + minttl) : 0;
1192 if ( *numServers > 0 ) {
1194 *arealCellName = realCellName;
1196 code = AFSCONF_NOTFOUND;
1199 if (code && realCellName)
1206 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1207 struct afsconf_cell *acellInfo)
1209 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1210 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1211 unsigned short ipRanks[AFSMAXCELLHOSTS];
1212 unsigned short ports[AFSMAXCELLHOSTS];
1213 char *realCellName = NULL;
1214 int ttl, numServers, i;
1215 char *service = aservice;
1217 unsigned short afsdbport;
1219 service = "afs3-vlserver";
1220 afsdbport = htons(7003);
1223 afsdbport = afsconf_FindService(service);
1225 code = afsconf_LookupServer((const char *)service, "udp",
1226 (const char *)acellName, afsdbport,
1227 cellHostAddrs, cellHostNames,
1228 ports, ipRanks, &numServers, &ttl,
1232 acellInfo->timeout = ttl;
1233 acellInfo->numServers = numServers;
1234 for (i = 0; i < numServers; i++) {
1235 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1237 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1238 acellInfo->hostAddr[i].sin_family = AF_INET;
1239 acellInfo->hostAddr[i].sin_port = ports[i];
1242 strlcpy(acellInfo->name, realCellName,
1243 sizeof(acellInfo->name));
1245 realCellName = NULL;
1248 acellInfo->linkedCell = NULL; /* no linked cell */
1249 acellInfo->flags = 0;
1255 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1256 struct afsconf_cell *acellInfo)
1259 int tservice = afsconf_FindService(aservice); /* network byte order */
1260 const char *ianaName = afsconf_FindIANAName(aservice);
1261 struct afsconf_entry DNSce;
1262 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1263 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1264 unsigned short ipRanks[AFSMAXCELLHOSTS];
1265 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1272 return AFSCONF_NOTFOUND;
1274 tservice = 0; /* port will be assigned by caller */
1277 if (ianaName == NULL)
1278 ianaName = "afs3-vlserver";
1280 DNSce.cellInfo.numServers = 0;
1283 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1284 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1286 /* ignore the ttl here since this code is only called by transitory programs
1287 * like klog, etc. */
1289 /* If we couldn't find an entry for the requested service
1290 * and that service happens to be the prservice or kaservice
1291 * then fallback to searching for afs3-vlserver and assigning
1292 * the port number here. */
1293 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1294 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1295 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1298 for (i = 0; i < numServers; i++)
1299 ports[i] = tservice;
1303 if (rc < 0 || numServers == 0)
1306 for (i = 0; i < numServers; i++) {
1307 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1309 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1310 acellInfo->hostAddr[i].sin_family = AF_INET;
1312 acellInfo->hostAddr[i].sin_port = ports[i];
1314 acellInfo->hostAddr[i].sin_port = 0;
1317 acellInfo->numServers = numServers;
1318 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1319 acellInfo->linkedCell = NULL; /* no linked cell */
1320 acellInfo->flags = 0;
1323 #endif /* windows */
1326 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1327 struct afsconf_cell *acellInfo)
1329 struct afsconf_entry *tce;
1330 struct afsconf_aliasentry *tcae;
1331 struct afsconf_entry *bestce;
1341 _afsconf_Check(adir);
1344 cnLen = (int)(strlen(tcell) + 1);
1345 lcstring(tcell, tcell, cnLen);
1346 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1347 /* call to afsconf_GetLocalCell: like klog */
1349 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1351 UNLOCK_GLOBAL_MUTEX;
1356 cnLen = strlen(tcell);
1357 bestce = (struct afsconf_entry *)0;
1360 UNLOCK_GLOBAL_MUTEX;
1364 /* Look through the list of aliases */
1365 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1366 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1367 tcell = tcae->aliasInfo.realName;
1372 for (tce = adir->entries; tce; tce = tce->next) {
1373 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1374 /* found our cell */
1379 if (strlen(tce->cellInfo.name) < cnLen)
1380 continue; /* clearly wrong */
1381 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1383 ambig = 1; /* ambiguous unless we get exact match */
1387 if (!ambig && bestce && bestce->cellInfo.numServers) {
1388 *acellInfo = bestce->cellInfo; /* structure assignment */
1390 tservice = afsconf_FindService(aservice);
1392 UNLOCK_GLOBAL_MUTEX;
1393 return AFSCONF_NOTFOUND; /* service not found */
1395 for (i = 0; i < acellInfo->numServers; i++) {
1396 acellInfo->hostAddr[i].sin_port = tservice;
1399 acellInfo->timeout = 0;
1402 * Until we figure out how to separate out ubik server
1403 * queries from other server queries, only perform gethostbyname()
1404 * lookup on the specified hostnames for the client CellServDB files.
1406 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1407 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1409 short numServers=0; /*Num active servers for the cell */
1410 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1411 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1413 memset(&hostAddr, 0, sizeof(hostAddr));
1414 memset(&hostName, 0, sizeof(hostName));
1416 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1417 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1420 if (he && he->h_addrtype == AF_INET) {
1422 /* obtain all the valid address from the list */
1423 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1424 /* check to see if this is a new address; if so insert it into the list */
1426 for (k=0, dup=0; !dup && k < numServers; k++) {
1427 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
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(long));
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 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1470 static int afsconf_showcell = 0;
1476 * If a cell switch was specified in a command, then it should override the
1477 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1478 * is set and the cell name in the adir structure is used.
1479 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1481 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1482 if (!afsconf_showcell) {
1483 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1485 afsconf_showcell = 1;
1487 strncpy(aname, afscell_path, alen);
1489 _afsconf_Check(adir);
1490 if (adir->cellName) {
1491 strncpy(aname, adir->cellName, alen);
1493 code = AFSCONF_UNKNOWN;
1496 UNLOCK_GLOBAL_MUTEX;
1501 afsconf_UpToDate(struct afsconf_dir *adir)
1508 afs_int32 code = 0; /* default to not up to date */
1511 /* NT client config dir has no KeyFile; don't risk attempting open
1512 * because there might be a random file of this name if dir is shared.
1514 if (_afsconf_IsClientConfigDirectory(adir->name)) {
1515 /* Not a server, nothing to reread */
1519 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1521 /* did file change? */
1522 code = stat(tbuffer, &tstat);
1523 if ((code == 0) && (tstat.st_mtime <= adir->timeRead)) {
1529 UNLOCK_GLOBAL_MUTEX;
1534 afsconf_Close(struct afsconf_dir *adir)
1537 afsconf_CloseInternal(adir);
1541 UNLOCK_GLOBAL_MUTEX;
1546 afsconf_CloseInternal(struct afsconf_dir *adir)
1548 struct afsconf_entry *td, *nd;
1549 struct afsconf_aliasentry *ta, *na;
1552 tname = adir->name; /* remember name, since that's all we preserve */
1554 /* free everything we can find */
1556 free(adir->cellName);
1557 for (td = adir->entries; td; td = nd) {
1559 if (td->cellInfo.linkedCell)
1560 free(td->cellInfo.linkedCell);
1563 for (ta = adir->alias_entries; ta; ta = na) {
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);