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')) {
340 _afsconf_UpToDate(struct afsconf_dir *adir)
350 /* NT client CellServDB has different file name than NT server or Unix */
351 if (_afsconf_IsClientConfigDirectory(adir->name)) {
352 if (!afssw_GetClientCellServDBDir(&p)) {
353 strcompose(tbuffer, sizeof(tbuffer), p, "/",
354 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
358 strncpy(tbuffer, adir->name, sizeof(tbuffer));
359 len = (int)strlen(tbuffer);
360 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
361 strncat(tbuffer, "\\", sizeof(tbuffer));
363 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
365 tbuffer[sizeof(tbuffer) - 1] = '\0';
368 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
372 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
373 #endif /* AFS_NT40_ENV */
375 code = stat(tbuffer, &tstat);
377 return 0; /* Can't throw the error, so just say we're not up to date */
379 /* did file change? */
380 if (tstat.st_mtime == adir->timeRead)
383 /* otherwise file has changed */
388 afsconf_UpToDate(void *rock)
393 code = _afsconf_UpToDate(rock);
400 _afsconf_Check(struct afsconf_dir *adir)
402 /* did configuration change? */
403 if (_afsconf_UpToDate(adir))
406 /* otherwise file has changed, so reopen it */
407 return afsconf_Reopen(adir);
410 /* set modtime on file */
412 _afsconf_Touch(struct afsconf_dir *adir)
416 struct timeval tvp[2];
421 adir->timeRead = 0; /* just in case */
424 /* NT client CellServDB has different file name than NT server or Unix */
426 if (_afsconf_IsClientConfigDirectory(adir->name)) {
427 if (!afssw_GetClientCellServDBDir(&p)) {
428 strcompose(tbuffer, sizeof(tbuffer), p, "/",
429 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
432 int len = (int)strlen(tbuffer);
433 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
434 strncat(tbuffer, "\\", sizeof(tbuffer));
436 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
438 tbuffer[sizeof(tbuffer) - 1] = '\0';
441 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
445 return _utime(tbuffer, NULL);
448 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
449 gettimeofday(&tvp[0], NULL);
451 return utimes(tbuffer, tvp);
452 #endif /* AFS_NT40_ENV */
456 afsconf_Open(const char *adir)
458 struct afsconf_dir *tdir;
462 /* zero structure and fill in name; rest is done by internal routine */
463 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
464 memset(tdir, 0, sizeof(struct afsconf_dir));
465 tdir->name = strdup(adir);
467 code = afsconf_OpenInternal(tdir, 0, 0);
469 char *afsconf_path, afs_confdir[128];
472 /* Check global place only when local Open failed for whatever reason */
473 if (!(afsconf_path = getenv("AFSCONF"))) {
474 /* 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... */
479 if (!(home_dir = getenv("HOME"))) {
480 /* Our last chance is the "/.AFSCONF" file */
481 fp = fopen("/.AFSCONF", "r");
485 return (struct afsconf_dir *)0;
487 fgets(afs_confdir, 128, fp);
492 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
493 fp = fopen(pathname, "r");
495 /* Our last chance is the "/.AFSCONF" file */
496 fp = fopen("/.AFSCONF", "r");
500 return (struct afsconf_dir *)0;
503 fgets(afs_confdir, 128, fp);
506 len = strlen(afs_confdir);
510 return (struct afsconf_dir *)0;
512 if (afs_confdir[len - 1] == '\n') {
513 afs_confdir[len - 1] = 0;
515 afsconf_path = afs_confdir;
517 tdir->name = strdup(afsconf_path);
518 code = afsconf_OpenInternal(tdir, 0, 0);
523 return (struct afsconf_dir *)0;
531 GetCellUnix(struct afsconf_dir *adir)
538 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
539 fp = fopen(tbuffer, "r");
543 rc = fgets(tbuffer, 256, fp);
549 while (*start != '\0' && isspace(*start))
552 while (*p != '\0' && !isspace(*p))
558 adir->cellName = strdup(start);
565 GetCellNT(struct afsconf_dir *adir)
567 if (_afsconf_IsClientConfigDirectory(adir->name)) {
568 /* NT client config dir; ThisCell is in registry (no file). */
569 return afssw_GetClientCellName(&adir->cellName);
571 /* NT server config dir; works just like Unix */
572 return GetCellUnix(adir);
576 /* The following procedures and structs are used on Windows only
577 * to enumerate the Cell information distributed within the
578 * Windows registry. (See src/WINNT/afsd/cm_config.c)
580 typedef struct _cm_enumCellRegistry {
581 afs_uint32 client; /* non-zero if client query */
582 struct afsconf_dir *adir;
583 } cm_enumCellRegistry_t;
586 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
587 char *hostNamep, unsigned short rank)
589 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
591 if (cellInfop->numServers == MAXHOSTSPERCELL)
594 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
595 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
596 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
597 cellInfop->numServers++;
603 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
606 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
607 char linkedName[256] = "";
609 struct afsconf_entry *newEntry;
612 newEntry = malloc(sizeof(struct afsconf_entry));
613 if (newEntry == NULL)
615 newEntry->cellInfo.numServers = 0;
617 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
618 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
619 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
622 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
623 newEntry->cellInfo.name[MAXCELLCHARS-1];
625 newEntry->cellInfo.linkedCell = strdup(linkedName);
627 newEntry->cellInfo.linkedCell = NULL;
628 newEntry->cellInfo.timeout = timeout;
629 newEntry->cellInfo.flags = 0;
631 newEntry->next = enump->adir->entries;
632 enump->adir->entries = newEntry;
638 #endif /* AFS_NT40_ENV */
642 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
647 struct afsconf_entry *curEntry;
648 struct afsconf_aliasentry *curAlias;
651 char tbuffer[256], tbuf1[256];
654 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
655 #endif /* AFS_NT40_ENV */
657 /* figure out the local cell name */
660 enumCellRegistry.adir = adir;
662 i = GetCellUnix(adir);
665 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
671 /* now parse the individual lines */
675 /* NT client/server have a CellServDB that is the same format as Unix.
676 * However, the NT client uses a different file name
678 if (_afsconf_IsClientConfigDirectory(adir->name)) {
679 /* NT client config dir */
682 enumCellRegistry.client = 1;
684 if (!afssw_GetClientCellServDBDir(&p)) {
685 strcompose(tbuffer, sizeof(tbuffer), p, "/",
686 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
690 strncpy(tbuffer, adir->name, sizeof(tbuffer));
691 len = (int)strlen(tbuffer);
692 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
693 strncat(tbuffer, "\\", sizeof(tbuffer));
695 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
697 tbuffer[sizeof(tbuffer) - 1] = '\0';
700 /* NT server config dir */
701 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
705 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
706 #endif /* AFS_NT40_ENV */
708 if (!stat(tbuffer, &tstat)) {
709 adir->timeRead = tstat.st_mtime;
714 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
715 tf = fopen(tbuffer, "r");
720 /* The CellServDB file is now open.
721 * The following code parses the contents of the
722 * file and creates a list with the first cell entry
723 * in the CellServDB file at the end of the list.
725 * No checking is performed for duplicates.
726 * The side effects of this process are that duplicate
727 * entries appended to the end of the CellServDB file
728 * take precedence and are found in a shorter period
733 tp = fgets(tbuffer, sizeof(tbuffer), tf);
736 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
737 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
738 continue; /* empty line */
739 if (tbuffer[0] == '>') {
740 char linkedcell[MAXCELLCHARS];
741 /* start new cell item */
743 /* thread this guy on the list */
744 curEntry->next = adir->entries;
745 adir->entries = curEntry;
749 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
750 memset(curEntry, 0, sizeof(struct afsconf_entry));
752 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
754 afsconf_CloseInternal(adir);
759 if (linkedcell[0] != '\0')
760 curEntry->cellInfo.linkedCell = strdup(linkedcell);
762 /* new host in the current cell */
764 afsconf_CloseInternal(adir);
768 i = curEntry->cellInfo.numServers;
769 if (i < MAXHOSTSPERCELL) {
770 if (cell && !strcmp(cell, curEntry->cellInfo.name))
772 ParseHostLine(tbuffer,
773 &curEntry->cellInfo.hostAddr[i],
774 curEntry->cellInfo.hostName[i],
778 ParseHostLine(tbuffer,
779 &curEntry->cellInfo.hostAddr[i],
780 curEntry->cellInfo.hostName[i], 0);
783 if (code == AFSCONF_SYNTAX) {
784 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
790 "Can't properly parse host line \"%s\" in configuration file %s\n",
795 afsconf_CloseInternal(adir);
798 curEntry->cellInfo.numServers = ++i;
801 "Too many hosts for cell %s in configuration file %s\n",
802 curEntry->cellInfo.name, tbuf1);
806 fclose(tf); /* close the file now */
808 /* end the last partially-completed cell */
810 curEntry->next = adir->entries;
811 adir->entries = curEntry;
816 * Windows maintains a CellServDB list in the Registry
817 * that supercedes the contents of the CellServDB file.
818 * Prepending these entries to the head of the list
819 * is sufficient to enforce the precedence.
821 cm_EnumerateCellRegistry( enumCellRegistry.client,
822 cm_enumCellRegistryProc,
824 #endif /* AFS_NT40_ENV */
826 /* Read in the alias list */
827 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
829 tf = fopen(tbuffer, "r");
833 tp = fgets(tbuffer, sizeof(tbuffer), tf);
836 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
838 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
839 continue; /* empty line */
842 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
845 continue; /* invalid line */
847 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
850 continue; /* invalid line */
853 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
858 curAlias = malloc(sizeof(*curAlias));
859 memset(curAlias, 0, sizeof(*curAlias));
861 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
862 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
864 curAlias->next = adir->alias_entries;
865 adir->alias_entries = curAlias;
870 /* now read the fs keys, if possible */
872 _afsconf_InitKeys(adir);
873 code = _afsconf_LoadKeys(adir);
878 /* parse a line of the form
879 *"128.2.1.3 #hostname" or
880 *"[128.2.1.3] #hostname" for clones
881 * into the appropriate pieces.
884 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
894 /* FIXME: length of aname unknown here */
895 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
899 /* FIXME: length of aname unknown here */
900 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
903 return AFSCONF_SYNTAX;
904 addr->sin_family = AF_INET;
906 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
907 addr->sin_len = sizeof(struct sockaddr_in);
909 tp = (char *)&addr->sin_addr;
917 /* parse a line of the form
918 * ">cellname [linkedcellname] [#comments]"
919 * into the appropriate pieces.
922 ParseCellLine(char *aline, char *aname,
926 /* FIXME: length of aname, alname unknown here */
927 code = sscanf(aline, ">%s %s", aname, alname);
931 if (*alname == '#') {
935 return (code > 0 ? 0 : AFSCONF_SYNTAX);
938 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
940 afsconf_CellApply(struct afsconf_dir *adir,
941 int (*aproc) (struct afsconf_cell * cell, void *arock,
942 struct afsconf_dir * dir), void *arock)
944 struct afsconf_entry *tde;
947 for (tde = adir->entries; tde; tde = tde->next) {
948 code = (*aproc) (&tde->cellInfo, arock, adir);
958 /* call aproc(entry, arock, adir) for all cell aliases.
959 * Proc must return 0, or we'll stop early and return the code it returns
962 afsconf_CellAliasApply(struct afsconf_dir *adir,
963 int (*aproc) (struct afsconf_cellalias * alias,
964 void *arock, struct afsconf_dir * dir),
967 struct afsconf_aliasentry *tde;
970 for (tde = adir->alias_entries; tde; tde = tde->next) {
971 code = (*aproc) (&tde->aliasInfo, arock, adir);
981 afs_int32 afsconf_SawCell = 0;
984 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
985 char *aservice, struct afsconf_cell *acellInfo,
991 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
998 cell = (char *)&acellInfo->name;
1000 code = afsconf_OpenInternal(adir, cell, clones);
1004 #if !defined(AFS_NT40_ENV)
1006 afsconf_LookupServer(const char *service, const char *protocol,
1007 const char *cellName, unsigned short afsdbPort,
1008 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
1009 unsigned short ports[], unsigned short ipRanks[],
1010 int *numServers, int *ttl, char **arealCellName)
1014 unsigned char answer[1024];
1018 int cellnamelength, fullnamelength;
1025 char *IANAname = (char *) afsconf_FindIANAName(service);
1026 int tservice = afsconf_FindService(service);
1028 realCellName = NULL;
1032 if (tservice <= 0 || !IANAname)
1033 return AFSCONF_NOTFOUND; /* service not found */
1035 if (strchr(cellName,'.'))
1038 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1039 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1040 dotcellname=malloc(fullnamelength);
1042 return AFSCONF_NOTFOUND; /* service not found */
1044 #ifdef HAVE_RES_RETRANSRETRY
1045 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1049 * Rx timeout is typically 56 seconds; limit user experience to
1060 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1061 IANAname, protocol, cellName);
1065 code = snprintf(dotcellname, fullnamelength, "%s.",
1070 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1071 IANAname, protocol, cellName);
1075 code = snprintf(dotcellname, fullnamelength, "%s",
1079 if ((code < 0) || (code >= fullnamelength))
1080 goto findservererror;
1082 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1083 UNLOCK_GLOBAL_MUTEX;
1095 code = AFSCONF_NOTFOUND;
1096 goto findservererror;
1100 p = answer + sizeof(HEADER); /* Skip header */
1101 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1103 code = AFSCONF_NOTFOUND;
1104 goto findservererror;
1107 p += code + QFIXEDSZ; /* Skip name */
1109 while (p < answer + len) {
1110 int type, ttl, size;
1112 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1114 code = AFSCONF_NOTFOUND;
1115 goto findservererror;
1118 p += code; /* Skip the name */
1119 type = (p[0] << 8) | p[1];
1120 p += 4; /* Skip type and class */
1121 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1122 p += 4; /* Skip the TTL */
1123 size = (p[0] << 8) | p[1];
1124 p += 2; /* Skip the size */
1126 if (type == T_AFSDB) {
1130 afsdb_type = (p[0] << 8) | p[1];
1131 if (afsdb_type == 1) {
1133 * We know this is an AFSDB record for our cell, of the
1134 * right AFSDB type. Write down the true cell name that
1135 * the resolver gave us above.
1138 realCellName = strdup(host);
1141 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1143 code = AFSCONF_NOTFOUND;
1144 goto findservererror;
1147 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1148 /* Do we want to get TTL data for the A record as well? */
1149 (he = gethostbyname(host))) {
1151 memcpy(&ipaddr, he->h_addr, he->h_length);
1152 cellHostAddrs[server_num] = ipaddr;
1153 ports[server_num] = afsdbPort;
1154 ipRanks[server_num] = 0;
1155 strncpy(cellHostNames[server_num], host,
1156 sizeof(cellHostNames[server_num]));
1159 if (!minttl || ttl < minttl)
1163 if (type == T_SRV) {
1165 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1166 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1167 (strncmp(host + strlen(IANAname) + 3, protocol,
1168 strlen(protocol)) == 0)) {
1170 realCellName = strdup(host + strlen(IANAname) +
1171 strlen(protocol) + 4);
1174 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1176 code = AFSCONF_NOTFOUND;
1177 goto findservererror;
1180 if ((server_num < MAXHOSTSPERCELL) &&
1181 /* Do we want to get TTL data for the A record as well? */
1182 (he = gethostbyname(host))) {
1184 memcpy(&ipaddr, he->h_addr, he->h_length);
1185 cellHostAddrs[server_num] = ipaddr;
1186 ipRanks[server_num] = (p[0] << 8) | p[1];
1187 ports[server_num] = htons((p[4] << 8) | p[5]);
1188 /* weight = (p[2] << 8) | p[3]; */
1189 strncpy(cellHostNames[server_num], host,
1190 sizeof(cellHostNames[server_num]));
1193 if (!minttl || ttl < minttl)
1201 if (server_num == 0) { /* No AFSDB or SRV records */
1202 code = AFSCONF_NOTFOUND;
1203 goto findservererror;
1207 /* Convert the real cell name to lowercase */
1208 for (p = (unsigned char *)realCellName; *p; p++)
1212 *numServers = server_num;
1213 *ttl = minttl ? (time(0) + minttl) : 0;
1215 if ( *numServers > 0 ) {
1217 *arealCellName = realCellName;
1219 code = AFSCONF_NOTFOUND;
1222 if (code && realCellName)
1229 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1230 struct afsconf_cell *acellInfo)
1232 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1233 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1234 unsigned short ipRanks[AFSMAXCELLHOSTS];
1235 unsigned short ports[AFSMAXCELLHOSTS];
1236 char *realCellName = NULL;
1237 int ttl, numServers, i;
1238 char *service = aservice;
1240 unsigned short afsdbport;
1242 service = "afs3-vlserver";
1243 afsdbport = htons(7003);
1246 afsdbport = afsconf_FindService(service);
1248 code = afsconf_LookupServer((const char *)service, "udp",
1249 (const char *)acellName, afsdbport,
1250 cellHostAddrs, cellHostNames,
1251 ports, ipRanks, &numServers, &ttl,
1255 acellInfo->timeout = ttl;
1256 acellInfo->numServers = numServers;
1257 for (i = 0; i < numServers; i++) {
1258 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1260 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1261 acellInfo->hostAddr[i].sin_family = AF_INET;
1262 acellInfo->hostAddr[i].sin_port = ports[i];
1265 strlcpy(acellInfo->name, realCellName,
1266 sizeof(acellInfo->name));
1268 realCellName = NULL;
1271 acellInfo->linkedCell = NULL; /* no linked cell */
1272 acellInfo->flags = 0;
1278 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1279 struct afsconf_cell *acellInfo)
1282 int tservice = afsconf_FindService(aservice); /* network byte order */
1283 const char *ianaName = afsconf_FindIANAName(aservice);
1284 struct afsconf_entry DNSce;
1285 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1286 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1287 unsigned short ipRanks[AFSMAXCELLHOSTS];
1288 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1295 return AFSCONF_NOTFOUND;
1297 tservice = 0; /* port will be assigned by caller */
1300 if (ianaName == NULL)
1301 ianaName = "afs3-vlserver";
1303 DNSce.cellInfo.numServers = 0;
1306 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1307 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1309 /* ignore the ttl here since this code is only called by transitory programs
1310 * like klog, etc. */
1312 /* If we couldn't find an entry for the requested service
1313 * and that service happens to be the prservice or kaservice
1314 * then fallback to searching for afs3-vlserver and assigning
1315 * the port number here. */
1316 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1317 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1318 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1321 for (i = 0; i < numServers; i++)
1322 ports[i] = tservice;
1326 if (rc < 0 || numServers == 0)
1329 for (i = 0; i < numServers; i++) {
1330 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1332 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1333 acellInfo->hostAddr[i].sin_family = AF_INET;
1335 acellInfo->hostAddr[i].sin_port = ports[i];
1337 acellInfo->hostAddr[i].sin_port = 0;
1340 acellInfo->numServers = numServers;
1341 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1342 acellInfo->linkedCell = NULL; /* no linked cell */
1343 acellInfo->flags = 0;
1346 #endif /* windows */
1349 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1350 struct afsconf_cell *acellInfo)
1352 struct afsconf_entry *tce;
1353 struct afsconf_aliasentry *tcae;
1354 struct afsconf_entry *bestce;
1364 _afsconf_Check(adir);
1367 cnLen = (int)(strlen(tcell) + 1);
1368 lcstring(tcell, tcell, cnLen);
1369 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1370 /* call to afsconf_GetLocalCell: like klog */
1372 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1374 UNLOCK_GLOBAL_MUTEX;
1379 cnLen = strlen(tcell);
1380 bestce = (struct afsconf_entry *)0;
1383 UNLOCK_GLOBAL_MUTEX;
1387 /* Look through the list of aliases */
1388 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1389 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1390 tcell = tcae->aliasInfo.realName;
1395 for (tce = adir->entries; tce; tce = tce->next) {
1396 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1397 /* found our cell */
1402 if (strlen(tce->cellInfo.name) < cnLen)
1403 continue; /* clearly wrong */
1404 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1406 ambig = 1; /* ambiguous unless we get exact match */
1410 if (!ambig && bestce && bestce->cellInfo.numServers) {
1411 *acellInfo = bestce->cellInfo; /* structure assignment */
1413 tservice = afsconf_FindService(aservice);
1415 UNLOCK_GLOBAL_MUTEX;
1416 return AFSCONF_NOTFOUND; /* service not found */
1418 for (i = 0; i < acellInfo->numServers; i++) {
1419 acellInfo->hostAddr[i].sin_port = tservice;
1422 acellInfo->timeout = 0;
1425 * Until we figure out how to separate out ubik server
1426 * queries from other server queries, only perform gethostbyname()
1427 * lookup on the specified hostnames for the client CellServDB files.
1429 if (_afsconf_IsClientConfigDirectory(adir->name) &&
1430 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1432 short numServers=0; /*Num active servers for the cell */
1433 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1434 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1436 memset(&hostAddr, 0, sizeof(hostAddr));
1437 memset(&hostName, 0, sizeof(hostName));
1439 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1440 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1443 if (he && he->h_addrtype == AF_INET) {
1445 /* obtain all the valid address from the list */
1446 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1447 /* check to see if this is a new address; if so insert it into the list */
1449 for (k=0, dup=0; !dup && k < numServers; k++) {
1450 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1456 hostAddr[numServers].sin_family = AF_INET;
1457 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1458 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1459 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1461 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1462 strcpy(hostName[numServers], acellInfo->hostName[j]);
1468 hostAddr[numServers] = acellInfo->hostAddr[j];
1469 strcpy(hostName[numServers], acellInfo->hostName[j]);
1474 for (i=0; i<numServers; i++) {
1475 acellInfo->hostAddr[i] = hostAddr[i];
1476 strcpy(acellInfo->hostName[i], hostName[i]);
1478 acellInfo->numServers = numServers;
1479 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1481 UNLOCK_GLOBAL_MUTEX;
1484 UNLOCK_GLOBAL_MUTEX;
1485 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1490 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1493 static int afsconf_showcell = 0;
1499 * If a cell switch was specified in a command, then it should override the
1500 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1501 * is set and the cell name in the adir structure is used.
1502 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1504 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1505 if (!afsconf_showcell) {
1506 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1508 afsconf_showcell = 1;
1510 strncpy(aname, afscell_path, alen);
1512 _afsconf_Check(adir);
1513 if (adir->cellName) {
1514 strncpy(aname, adir->cellName, alen);
1516 code = AFSCONF_UNKNOWN;
1519 UNLOCK_GLOBAL_MUTEX;
1524 afsconf_Close(struct afsconf_dir *adir)
1527 afsconf_CloseInternal(adir);
1531 UNLOCK_GLOBAL_MUTEX;
1536 afsconf_CloseInternal(struct afsconf_dir *adir)
1538 struct afsconf_entry *td, *nd;
1539 struct afsconf_aliasentry *ta, *na;
1542 tname = adir->name; /* remember name, since that's all we preserve */
1544 /* free everything we can find */
1546 free(adir->cellName);
1547 for (td = adir->entries; td; td = nd) {
1549 if (td->cellInfo.linkedCell)
1550 free(td->cellInfo.linkedCell);
1553 for (ta = adir->alias_entries; ta; ta = na) {
1558 _afsconf_FreeAllKeys(adir);
1561 memset(adir, 0, sizeof(struct afsconf_dir));
1562 adir->name = tname; /* restore it */
1567 afsconf_Reopen(struct afsconf_dir *adir)
1570 code = afsconf_CloseInternal(adir);
1573 code = afsconf_OpenInternal(adir, 0, 0);