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>
52 #include "cellconfig.h"
56 #include <cm_config.h>
57 /* cm_dns.h depends on cellconfig.h */
64 struct afsconf_servPair {
70 static struct afsconf_servPair serviceTable[] = {
71 {"afs", "afs3-fileserver", 7000,},
72 {"afscb", "afs3-callback", 7001,},
73 {"afsprot", "afs3-prserver", 7002,},
74 {"afsvldb", "afs3-vlserver", 7003,},
75 {"afskauth", "afs3-kaserver", 7004,},
76 {"afsvol", "afs3-volserver", 7005,},
77 {"afserror", "afs3-errors", 7006,},
78 {"afsnanny", "afs3-bos", 7007,},
79 {"afsupdate", "afs3-update", 7008,},
80 {"afsrmtsys", "afs3-rmtsys", 7009,},
81 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
82 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
83 {0, 0, 0} /* insert new services before this spot */
87 static int TrimLine(char *abuffer, int abufsize);
88 static int IsClientConfigDirectory(const char *path);
90 static int GetCellNT(struct afsconf_dir *adir);
92 static int afsconf_Check(struct afsconf_dir *adir);
93 static int afsconf_Touch(struct afsconf_dir *adir);
94 static int GetCellUnix(struct afsconf_dir *adir);
95 static int afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
97 static int ParseHostLine(char *aline, struct sockaddr_in *addr,
98 char *aname, char *aclone);
99 static int ParseCellLine(char *aline, char *aname,
101 static int afsconf_CloseInternal(struct afsconf_dir *adir);
102 static int afsconf_Reopen(struct afsconf_dir *adir);
103 static int SaveKeys(struct afsconf_dir *adir);
106 #define T_AFSDB 18 /* per RFC1183 section 1 */
109 #define T_SRV 33 /* RFC2782 */
113 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
114 * our code can tell if there is new info in the key files, the cell server db
115 * files or any of the other files (and reopen the thing) if the date on
116 * CellServDB changes.
119 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
120 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
121 get an fd <= 255. We allow the fileserver to claim more fds than that.
122 This has always been a problem since pr_Initialize would have the same
123 issue, but hpr_Initialize makes it more likely that we would see this.
124 Work around it. This is not generic. It's coded with the needs of
125 afsconf_* in mind only.
127 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
132 struct afsconf_iobuffer {
139 typedef struct afsconf_iobuffer afsconf_FILE;
141 static afsconf_FILE *
142 afsconf_fopen(const char *fname, const char *fmode)
147 if ((fd = open(fname, O_RDONLY)) == -1) {
151 iop = malloc(sizeof(struct afsconf_iobuffer));
158 iop->buffer = malloc(BUFFER);
159 if (iop->buffer == NULL) {
165 iop->ptr = iop->buffer;
166 iop->endptr = iop->buffer;
171 afsconf_fclose(afsconf_FILE *iop)
177 free((void *)iop->buffer);
183 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
191 if (iop->ptr == iop->endptr) {
194 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
204 iop->ptr = iop->buffer;
205 iop->endptr = iop->buffer + len;
209 if ((p - s) == (n - 1)) {
219 #define fopen afsconf_fopen
220 #define fclose afsconf_fclose
221 #define fgets afsconf_fgets
223 #define afsconf_FILE FILE
224 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
226 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
228 afsconf_FindService(const char *aname)
230 /* lookup a service name */
232 struct afsconf_servPair *tsp;
234 if (aname == NULL || aname[0] == '\0')
237 #if defined(AFS_OSF_ENV)
238 ts = getservbyname(aname, "");
240 ts = (struct servent *) getservbyname(aname, NULL);
243 /* we found it in /etc/services, so we use this value */
244 return ts->s_port; /* already in network byte order */
247 /* not found in /etc/services, see if it is one of ours */
248 for (tsp = serviceTable; tsp->port; tsp++) {
249 if ((tsp->name && (!strcmp(tsp->name, aname)))
250 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
251 return htons(tsp->port);
257 afsconf_FindIANAName(const char *aname)
259 /* lookup a service name */
260 struct afsconf_servPair *tsp;
262 if (aname == NULL || aname[0] == '\0')
265 /* see if it is one of ours */
266 for (tsp = serviceTable; tsp->port; tsp++) {
267 if ((tsp->name && (!strcmp(tsp->name, aname)))
268 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
269 return tsp->ianaName;
275 TrimLine(char *abuffer, int abufsize)
287 strlcpy(tbuffer, tp, sizeof tbuffer);
288 strlcpy(abuffer, tbuffer, abufsize);
293 * IsClientConfigDirectory() -- determine if path matches well-known
294 * client configuration directory.
297 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
298 #else /* AFS_NT40_ENV */
299 #define IS_SEP(x) ((x) == '/')
300 #endif /* AFS_NT40_ENV */
302 IsClientConfigDirectory(const char *path)
304 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
307 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
309 cc = tolower(cdir[i]);
310 pc = tolower(path[i]);
318 #else /* AFS_NT40_ENV */
321 #endif /* AFS_NT40_ENV */
327 /* hit end of one or both; allow mismatch in existence of trailing slash */
328 if (cdir[i] != '\0') {
329 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
333 if (path[i] != '\0') {
334 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
343 afsconf_Check(struct afsconf_dir *adir)
353 /* NT client CellServDB has different file name than NT server or Unix */
354 if (IsClientConfigDirectory(adir->name)) {
355 if (!afssw_GetClientCellServDBDir(&p)) {
356 strcompose(tbuffer, sizeof(tbuffer), p, "/",
357 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
361 strncpy(tbuffer, adir->name, sizeof(tbuffer));
362 len = (int)strlen(tbuffer);
363 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
364 strncat(tbuffer, "\\", sizeof(tbuffer));
366 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
368 tbuffer[sizeof(tbuffer) - 1] = '\0';
371 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
375 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
376 #endif /* AFS_NT40_ENV */
378 code = stat(tbuffer, &tstat);
382 /* did file change? */
383 if (tstat.st_mtime == adir->timeRead) {
386 /* otherwise file has changed, so reopen it */
387 return afsconf_Reopen(adir);
390 /* set modtime on file */
392 afsconf_Touch(struct afsconf_dir *adir)
396 struct timeval tvp[2];
401 adir->timeRead = 0; /* just in case */
404 /* NT client CellServDB has different file name than NT server or Unix */
406 if (IsClientConfigDirectory(adir->name)) {
407 if (!afssw_GetClientCellServDBDir(&p)) {
408 strcompose(tbuffer, sizeof(tbuffer), p, "/",
409 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
412 int len = (int)strlen(tbuffer);
413 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
414 strncat(tbuffer, "\\", sizeof(tbuffer));
416 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
418 tbuffer[sizeof(tbuffer) - 1] = '\0';
421 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
425 return _utime(tbuffer, NULL);
428 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
429 gettimeofday(&tvp[0], NULL);
431 return utimes(tbuffer, tvp);
432 #endif /* AFS_NT40_ENV */
436 afsconf_Open(const char *adir)
438 struct afsconf_dir *tdir;
442 /* zero structure and fill in name; rest is done by internal routine */
443 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
444 memset(tdir, 0, sizeof(struct afsconf_dir));
445 tdir->name = strdup(adir);
447 code = afsconf_OpenInternal(tdir, 0, 0);
449 char *afsconf_path, afs_confdir[128];
452 /* Check global place only when local Open failed for whatever reason */
453 if (!(afsconf_path = getenv("AFSCONF"))) {
454 /* The "AFSCONF" environment (or contents of "/.AFSCONF") will be typically set to something like "/afs/<cell>/common/etc" where, by convention, the default files for "ThisCell" and "CellServDB" will reside; note that a major drawback is that a given afs client on that cell may NOT contain the same contents... */
459 if (!(home_dir = getenv("HOME"))) {
460 /* Our last chance is the "/.AFSCONF" file */
461 fp = fopen("/.AFSCONF", "r");
465 return (struct afsconf_dir *)0;
467 fgets(afs_confdir, 128, fp);
472 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
473 fp = fopen(pathname, "r");
475 /* Our last chance is the "/.AFSCONF" file */
476 fp = fopen("/.AFSCONF", "r");
480 return (struct afsconf_dir *)0;
483 fgets(afs_confdir, 128, fp);
486 len = strlen(afs_confdir);
490 return (struct afsconf_dir *)0;
492 if (afs_confdir[len - 1] == '\n') {
493 afs_confdir[len - 1] = 0;
495 afsconf_path = afs_confdir;
497 tdir->name = strdup(afsconf_path);
498 code = afsconf_OpenInternal(tdir, 0, 0);
503 return (struct afsconf_dir *)0;
511 GetCellUnix(struct afsconf_dir *adir)
518 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
519 fp = fopen(tbuffer, "r");
523 rc = fgets(tbuffer, 256, fp);
529 while (*start != '\0' && isspace(*start))
532 while (*p != '\0' && !isspace(*p))
538 adir->cellName = strdup(start);
545 GetCellNT(struct afsconf_dir *adir)
547 if (IsClientConfigDirectory(adir->name)) {
548 /* NT client config dir; ThisCell is in registry (no file). */
549 return afssw_GetClientCellName(&adir->cellName);
551 /* NT server config dir; works just like Unix */
552 return GetCellUnix(adir);
556 /* The following procedures and structs are used on Windows only
557 * to enumerate the Cell information distributed within the
558 * Windows registry. (See src/WINNT/afsd/cm_config.c)
560 typedef struct _cm_enumCellRegistry {
561 afs_uint32 client; /* non-zero if client query */
562 struct afsconf_dir *adir;
563 } cm_enumCellRegistry_t;
566 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
567 char *hostNamep, unsigned short rank)
569 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
571 if (cellInfop->numServers == MAXHOSTSPERCELL)
574 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
575 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
576 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
577 cellInfop->numServers++;
583 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
586 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
587 char linkedName[256] = "";
589 struct afsconf_entry *newEntry;
592 newEntry = malloc(sizeof(struct afsconf_entry));
593 if (newEntry == NULL)
595 newEntry->cellInfo.numServers = 0;
597 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
598 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
599 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
602 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
603 newEntry->cellInfo.name[MAXCELLCHARS-1];
605 newEntry->cellInfo.linkedCell = strdup(linkedName);
607 newEntry->cellInfo.linkedCell = NULL;
608 newEntry->cellInfo.timeout = timeout;
609 newEntry->cellInfo.flags = 0;
611 newEntry->next = enump->adir->entries;
612 enump->adir->entries = newEntry;
618 #endif /* AFS_NT40_ENV */
622 afsconf_OpenInternal(struct afsconf_dir *adir, char *cell,
627 struct afsconf_entry *curEntry;
628 struct afsconf_aliasentry *curAlias;
631 char tbuffer[256], tbuf1[256];
634 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
635 #endif /* AFS_NT40_ENV */
637 /* figure out the local cell name */
640 enumCellRegistry.adir = adir;
642 i = GetCellUnix(adir);
645 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
651 /* now parse the individual lines */
655 /* NT client/server have a CellServDB that is the same format as Unix.
656 * However, the NT client uses a different file name
658 if (IsClientConfigDirectory(adir->name)) {
659 /* NT client config dir */
662 enumCellRegistry.client = 1;
664 if (!afssw_GetClientCellServDBDir(&p)) {
665 strcompose(tbuffer, sizeof(tbuffer), p, "/",
666 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
670 strncpy(tbuffer, adir->name, sizeof(tbuffer));
671 len = (int)strlen(tbuffer);
672 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
673 strncat(tbuffer, "\\", sizeof(tbuffer));
675 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
677 tbuffer[sizeof(tbuffer) - 1] = '\0';
680 /* NT server config dir */
681 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
685 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
686 #endif /* AFS_NT40_ENV */
688 if (!stat(tbuffer, &tstat)) {
689 adir->timeRead = tstat.st_mtime;
694 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
695 tf = fopen(tbuffer, "r");
700 /* The CellServDB file is now open.
701 * The following code parses the contents of the
702 * file and creates a list with the first cell entry
703 * in the CellServDB file at the end of the list.
705 * No checking is performed for duplicates.
706 * The side effects of this process are that duplicate
707 * entries appended to the end of the CellServDB file
708 * take precedence and are found in a shorter period
713 tp = fgets(tbuffer, sizeof(tbuffer), tf);
716 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
717 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
718 continue; /* empty line */
719 if (tbuffer[0] == '>') {
720 char linkedcell[MAXCELLCHARS];
721 /* start new cell item */
723 /* thread this guy on the list */
724 curEntry->next = adir->entries;
725 adir->entries = curEntry;
729 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
730 memset(curEntry, 0, sizeof(struct afsconf_entry));
732 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
734 afsconf_CloseInternal(adir);
739 if (linkedcell[0] != '\0')
740 curEntry->cellInfo.linkedCell = strdup(linkedcell);
742 /* new host in the current cell */
744 afsconf_CloseInternal(adir);
748 i = curEntry->cellInfo.numServers;
749 if (i < MAXHOSTSPERCELL) {
750 if (cell && !strcmp(cell, curEntry->cellInfo.name))
752 ParseHostLine(tbuffer,
753 &curEntry->cellInfo.hostAddr[i],
754 curEntry->cellInfo.hostName[i],
758 ParseHostLine(tbuffer,
759 &curEntry->cellInfo.hostAddr[i],
760 curEntry->cellInfo.hostName[i], 0);
763 if (code == AFSCONF_SYNTAX) {
764 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
770 "Can't properly parse host line \"%s\" in configuration file %s\n",
775 afsconf_CloseInternal(adir);
778 curEntry->cellInfo.numServers = ++i;
781 "Too many hosts for cell %s in configuration file %s\n",
782 curEntry->cellInfo.name, tbuf1);
786 fclose(tf); /* close the file now */
788 /* end the last partially-completed cell */
790 curEntry->next = adir->entries;
791 adir->entries = curEntry;
796 * Windows maintains a CellServDB list in the Registry
797 * that supercedes the contents of the CellServDB file.
798 * Prepending these entries to the head of the list
799 * is sufficient to enforce the precedence.
801 cm_EnumerateCellRegistry( enumCellRegistry.client,
802 cm_enumCellRegistryProc,
804 #endif /* AFS_NT40_ENV */
806 /* Read in the alias list */
807 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
809 tf = fopen(tbuffer, "r");
813 tp = fgets(tbuffer, sizeof(tbuffer), tf);
816 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
818 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
819 continue; /* empty line */
822 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
825 continue; /* invalid line */
827 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
830 continue; /* invalid line */
833 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
838 curAlias = malloc(sizeof(*curAlias));
839 memset(curAlias, 0, sizeof(*curAlias));
841 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
842 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
844 curAlias->next = adir->alias_entries;
845 adir->alias_entries = curAlias;
850 /* now read the fs keys, if possible */
851 adir->keystr = (struct afsconf_keys *)0;
852 afsconf_IntGetKeys(adir);
857 /* parse a line of the form
858 *"128.2.1.3 #hostname" or
859 *"[128.2.1.3] #hostname" for clones
860 * into the appropriate pieces.
863 ParseHostLine(char *aline, struct sockaddr_in *addr, char *aname,
873 /* FIXME: length of aname unknown here */
874 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
878 /* FIXME: length of aname unknown here */
879 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
882 return AFSCONF_SYNTAX;
883 addr->sin_family = AF_INET;
885 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
886 addr->sin_len = sizeof(struct sockaddr_in);
888 tp = (char *)&addr->sin_addr;
896 /* parse a line of the form
897 * ">cellname [linkedcellname] [#comments]"
898 * into the appropriate pieces.
901 ParseCellLine(char *aline, char *aname,
905 /* FIXME: length of aname, alname unknown here */
906 code = sscanf(aline, ">%s %s", aname, alname);
910 if (*alname == '#') {
914 return (code > 0 ? 0 : AFSCONF_SYNTAX);
917 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
919 afsconf_CellApply(struct afsconf_dir *adir,
920 int (*aproc) (struct afsconf_cell * cell, void *arock,
921 struct afsconf_dir * dir), void *arock)
923 struct afsconf_entry *tde;
926 for (tde = adir->entries; tde; tde = tde->next) {
927 code = (*aproc) (&tde->cellInfo, arock, adir);
937 /* call aproc(entry, arock, adir) for all cell aliases.
938 * Proc must return 0, or we'll stop early and return the code it returns
941 afsconf_CellAliasApply(struct afsconf_dir *adir,
942 int (*aproc) (struct afsconf_cellalias * alias,
943 void *arock, struct afsconf_dir * dir),
946 struct afsconf_aliasentry *tde;
949 for (tde = adir->alias_entries; tde; tde = tde->next) {
950 code = (*aproc) (&tde->aliasInfo, arock, adir);
960 afs_int32 afsconf_SawCell = 0;
963 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
964 char *aservice, struct afsconf_cell *acellInfo,
970 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
977 cell = (char *)&acellInfo->name;
979 code = afsconf_OpenInternal(adir, cell, clones);
983 #if !defined(AFS_NT40_ENV)
985 afsconf_LookupServer(const char *service, const char *protocol,
986 const char *cellName, unsigned short afsdbPort,
987 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
988 unsigned short ports[], unsigned short ipRanks[],
989 int *numServers, int *ttl, char **arealCellName)
993 unsigned char answer[1024];
997 int cellnamelength, fullnamelength;
1004 char *IANAname = (char *) afsconf_FindIANAName(service);
1005 int tservice = afsconf_FindService(service);
1007 realCellName = NULL;
1011 if (tservice <= 0 || !IANAname)
1012 return AFSCONF_NOTFOUND; /* service not found */
1014 if (strchr(cellName,'.'))
1017 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1018 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1019 dotcellname=malloc(fullnamelength);
1021 return AFSCONF_NOTFOUND; /* service not found */
1023 #ifdef HAVE_RES_RETRANSRETRY
1024 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1028 * Rx timeout is typically 56 seconds; limit user experience to
1039 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1040 IANAname, protocol, cellName);
1044 code = snprintf(dotcellname, fullnamelength, "%s.",
1049 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1050 IANAname, protocol, cellName);
1054 code = snprintf(dotcellname, fullnamelength, "%s",
1058 if ((code < 0) || (code >= fullnamelength))
1059 goto findservererror;
1061 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1062 UNLOCK_GLOBAL_MUTEX;
1074 code = AFSCONF_NOTFOUND;
1075 goto findservererror;
1079 p = answer + sizeof(HEADER); /* Skip header */
1080 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1082 code = AFSCONF_NOTFOUND;
1083 goto findservererror;
1086 p += code + QFIXEDSZ; /* Skip name */
1088 while (p < answer + len) {
1089 int type, ttl, size;
1091 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1093 code = AFSCONF_NOTFOUND;
1094 goto findservererror;
1097 p += code; /* Skip the name */
1098 type = (p[0] << 8) | p[1];
1099 p += 4; /* Skip type and class */
1100 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1101 p += 4; /* Skip the TTL */
1102 size = (p[0] << 8) | p[1];
1103 p += 2; /* Skip the size */
1105 if (type == T_AFSDB) {
1109 afsdb_type = (p[0] << 8) | p[1];
1110 if (afsdb_type == 1) {
1112 * We know this is an AFSDB record for our cell, of the
1113 * right AFSDB type. Write down the true cell name that
1114 * the resolver gave us above.
1117 realCellName = strdup(host);
1120 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1122 code = AFSCONF_NOTFOUND;
1123 goto findservererror;
1126 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1127 /* Do we want to get TTL data for the A record as well? */
1128 (he = gethostbyname(host))) {
1130 memcpy(&ipaddr, he->h_addr, he->h_length);
1131 cellHostAddrs[server_num] = ipaddr;
1132 ports[server_num] = afsdbPort;
1133 ipRanks[server_num] = 0;
1134 strncpy(cellHostNames[server_num], host,
1135 sizeof(cellHostNames[server_num]));
1138 if (!minttl || ttl < minttl)
1142 if (type == T_SRV) {
1144 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1145 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1146 (strncmp(host + strlen(IANAname) + 3, protocol,
1147 strlen(protocol)) == 0)) {
1149 realCellName = strdup(host + strlen(IANAname) +
1150 strlen(protocol) + 4);
1153 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1155 code = AFSCONF_NOTFOUND;
1156 goto findservererror;
1159 if ((server_num < MAXHOSTSPERCELL) &&
1160 /* Do we want to get TTL data for the A record as well? */
1161 (he = gethostbyname(host))) {
1163 memcpy(&ipaddr, he->h_addr, he->h_length);
1164 cellHostAddrs[server_num] = ipaddr;
1165 ipRanks[server_num] = (p[0] << 8) | p[1];
1166 ports[server_num] = htons((p[4] << 8) | p[5]);
1167 /* weight = (p[2] << 8) | p[3]; */
1168 strncpy(cellHostNames[server_num], host,
1169 sizeof(cellHostNames[server_num]));
1172 if (!minttl || ttl < minttl)
1180 if (server_num == 0) { /* No AFSDB or SRV records */
1181 code = AFSCONF_NOTFOUND;
1182 goto findservererror;
1186 /* Convert the real cell name to lowercase */
1187 for (p = (unsigned char *)realCellName; *p; p++)
1191 *numServers = server_num;
1192 *ttl = minttl ? (time(0) + minttl) : 0;
1194 if ( *numServers > 0 ) {
1196 *arealCellName = realCellName;
1198 code = AFSCONF_NOTFOUND;
1201 if (code && realCellName)
1208 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1209 struct afsconf_cell *acellInfo)
1211 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1212 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1213 unsigned short ipRanks[AFSMAXCELLHOSTS];
1214 unsigned short ports[AFSMAXCELLHOSTS];
1215 char *realCellName = NULL;
1216 int ttl, numServers, i;
1217 char *service = aservice;
1219 unsigned short afsdbport;
1221 service = "afs3-vlserver";
1222 afsdbport = htons(7003);
1225 afsdbport = afsconf_FindService(service);
1227 code = afsconf_LookupServer((const char *)service, "udp",
1228 (const char *)acellName, afsdbport,
1229 cellHostAddrs, cellHostNames,
1230 ports, ipRanks, &numServers, &ttl,
1234 acellInfo->timeout = ttl;
1235 acellInfo->numServers = numServers;
1236 for (i = 0; i < numServers; i++) {
1237 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1239 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1240 acellInfo->hostAddr[i].sin_family = AF_INET;
1241 acellInfo->hostAddr[i].sin_port = ports[i];
1244 strlcpy(acellInfo->name, realCellName,
1245 sizeof(acellInfo->name));
1247 realCellName = NULL;
1250 acellInfo->linkedCell = NULL; /* no linked cell */
1251 acellInfo->flags = 0;
1257 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1258 struct afsconf_cell *acellInfo)
1261 int tservice = afsconf_FindService(aservice); /* network byte order */
1262 const char *ianaName = afsconf_FindIANAName(aservice);
1263 struct afsconf_entry DNSce;
1264 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1265 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1266 unsigned short ipRanks[AFSMAXCELLHOSTS];
1267 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1274 return AFSCONF_NOTFOUND;
1276 tservice = 0; /* port will be assigned by caller */
1279 if (ianaName == NULL)
1280 ianaName = "afs3-vlserver";
1282 DNSce.cellInfo.numServers = 0;
1285 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1286 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1288 /* ignore the ttl here since this code is only called by transitory programs
1289 * like klog, etc. */
1291 /* If we couldn't find an entry for the requested service
1292 * and that service happens to be the prservice or kaservice
1293 * then fallback to searching for afs3-vlserver and assigning
1294 * the port number here. */
1295 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1296 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1297 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1300 for (i = 0; i < numServers; i++)
1301 ports[i] = tservice;
1305 if (rc < 0 || numServers == 0)
1308 for (i = 0; i < numServers; i++) {
1309 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1311 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1312 acellInfo->hostAddr[i].sin_family = AF_INET;
1314 acellInfo->hostAddr[i].sin_port = ports[i];
1316 acellInfo->hostAddr[i].sin_port = 0;
1319 acellInfo->numServers = numServers;
1320 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1321 acellInfo->linkedCell = NULL; /* no linked cell */
1322 acellInfo->flags = 0;
1325 #endif /* windows */
1328 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1329 struct afsconf_cell *acellInfo)
1331 struct afsconf_entry *tce;
1332 struct afsconf_aliasentry *tcae;
1333 struct afsconf_entry *bestce;
1343 afsconf_Check(adir);
1346 cnLen = (int)(strlen(tcell) + 1);
1347 lcstring(tcell, tcell, cnLen);
1348 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1349 /* call to afsconf_GetLocalCell: like klog */
1351 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1353 UNLOCK_GLOBAL_MUTEX;
1358 cnLen = strlen(tcell);
1359 bestce = (struct afsconf_entry *)0;
1362 UNLOCK_GLOBAL_MUTEX;
1366 /* Look through the list of aliases */
1367 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1368 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1369 tcell = tcae->aliasInfo.realName;
1374 for (tce = adir->entries; tce; tce = tce->next) {
1375 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1376 /* found our cell */
1381 if (strlen(tce->cellInfo.name) < cnLen)
1382 continue; /* clearly wrong */
1383 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1385 ambig = 1; /* ambiguous unless we get exact match */
1389 if (!ambig && bestce && bestce->cellInfo.numServers) {
1390 *acellInfo = bestce->cellInfo; /* structure assignment */
1392 tservice = afsconf_FindService(aservice);
1394 UNLOCK_GLOBAL_MUTEX;
1395 return AFSCONF_NOTFOUND; /* service not found */
1397 for (i = 0; i < acellInfo->numServers; i++) {
1398 acellInfo->hostAddr[i].sin_port = tservice;
1401 acellInfo->timeout = 0;
1404 * Until we figure out how to separate out ubik server
1405 * queries from other server queries, only perform gethostbyname()
1406 * lookup on the specified hostnames for the client CellServDB files.
1408 if (IsClientConfigDirectory(adir->name) &&
1409 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1411 short numServers=0; /*Num active servers for the cell */
1412 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1413 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1415 memset(&hostAddr, 0, sizeof(hostAddr));
1416 memset(&hostName, 0, sizeof(hostName));
1418 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1419 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1422 if (he && he->h_addrtype == AF_INET) {
1424 /* obtain all the valid address from the list */
1425 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1426 /* check to see if this is a new address; if so insert it into the list */
1428 for (k=0, dup=0; !dup && k < numServers; k++) {
1429 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1435 hostAddr[numServers].sin_family = AF_INET;
1436 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1437 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1438 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1440 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1441 strcpy(hostName[numServers], acellInfo->hostName[j]);
1447 hostAddr[numServers] = acellInfo->hostAddr[j];
1448 strcpy(hostName[numServers], acellInfo->hostName[j]);
1453 for (i=0; i<numServers; i++) {
1454 acellInfo->hostAddr[i] = hostAddr[i];
1455 strcpy(acellInfo->hostName[i], hostName[i]);
1457 acellInfo->numServers = numServers;
1458 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1460 UNLOCK_GLOBAL_MUTEX;
1463 UNLOCK_GLOBAL_MUTEX;
1464 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1469 afsconf_GetLocalCell(struct afsconf_dir *adir, char *aname,
1472 static int afsconf_showcell = 0;
1478 * If a cell switch was specified in a command, then it should override the
1479 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1480 * is set and the cell name in the adir structure is used.
1481 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1483 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1484 if (!afsconf_showcell) {
1485 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1487 afsconf_showcell = 1;
1489 strncpy(aname, afscell_path, alen);
1491 afsconf_Check(adir);
1492 if (adir->cellName) {
1493 strncpy(aname, adir->cellName, alen);
1495 code = AFSCONF_UNKNOWN;
1498 UNLOCK_GLOBAL_MUTEX;
1503 afsconf_UpToDate(struct afsconf_dir *adir)
1510 afs_int32 code = 0; /* default to not up to date */
1513 /* NT client config dir has no KeyFile; don't risk attempting open
1514 * because there might be a random file of this name if dir is shared.
1516 if (IsClientConfigDirectory(adir->name)) {
1517 /* Not a server, nothing to reread */
1521 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1523 /* did file change? */
1524 code = stat(tbuffer, &tstat);
1525 if ((code == 0) && (tstat.st_mtime <= adir->timeRead)) {
1531 UNLOCK_GLOBAL_MUTEX;
1536 afsconf_Close(struct afsconf_dir *adir)
1539 afsconf_CloseInternal(adir);
1543 UNLOCK_GLOBAL_MUTEX;
1548 afsconf_CloseInternal(struct afsconf_dir *adir)
1550 struct afsconf_entry *td, *nd;
1551 struct afsconf_aliasentry *ta, *na;
1554 tname = adir->name; /* remember name, since that's all we preserve */
1556 /* free everything we can find */
1558 free(adir->cellName);
1559 for (td = adir->entries; td; td = nd) {
1561 if (td->cellInfo.linkedCell)
1562 free(td->cellInfo.linkedCell);
1565 for (ta = adir->alias_entries; ta; ta = na) {
1573 memset(adir, 0, sizeof(struct afsconf_dir));
1574 adir->name = tname; /* restore it */
1579 afsconf_Reopen(struct afsconf_dir *adir)
1582 code = afsconf_CloseInternal(adir);
1585 code = afsconf_OpenInternal(adir, 0, 0);
1589 /* called during opening of config file */
1591 afsconf_IntGetKeys(struct afsconf_dir *adir)
1595 struct afsconf_keys *tstr;
1599 /* NT client config dir has no KeyFile; don't risk attempting open
1600 * because there might be a random file of this name if dir is shared.
1602 if (IsClientConfigDirectory(adir->name)) {
1603 adir->keystr = ((struct afsconf_keys *)
1604 malloc(sizeof(struct afsconf_keys)));
1605 adir->keystr->nkeys = 0;
1608 #endif /* AFS_NT40_ENV */
1611 /* compute the key name and other setup */
1612 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1613 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1614 adir->keystr = tstr;
1617 fd = open(tbuffer, O_RDONLY);
1620 UNLOCK_GLOBAL_MUTEX;
1623 code = read(fd, tstr, sizeof(struct afsconf_keys));
1625 if (code < sizeof(afs_int32)) {
1627 UNLOCK_GLOBAL_MUTEX;
1631 /* convert key structure to host order */
1632 tstr->nkeys = ntohl(tstr->nkeys);
1634 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1636 UNLOCK_GLOBAL_MUTEX;
1640 for (fd = 0; fd < tstr->nkeys; fd++)
1641 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1643 UNLOCK_GLOBAL_MUTEX;
1647 /* get keys structure */
1649 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1654 code = afsconf_Check(adir);
1656 UNLOCK_GLOBAL_MUTEX;
1657 return AFSCONF_FAILURE;
1659 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1660 UNLOCK_GLOBAL_MUTEX;
1664 /* get latest key */
1666 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1667 struct ktc_encryptionKey *akey)
1671 struct afsconf_key *tk;
1673 struct afsconf_key *bestk;
1677 code = afsconf_Check(adir);
1679 UNLOCK_GLOBAL_MUTEX;
1680 return AFSCONF_FAILURE;
1682 maxa = adir->keystr->nkeys;
1684 best = -1; /* highest kvno we've seen yet */
1685 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1686 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1687 if (tk->kvno == 999)
1688 continue; /* skip bcrypt keys */
1689 if (tk->kvno > best) {
1694 if (bestk) { /* found any */
1696 memcpy(akey, bestk->key, 8); /* copy out latest key */
1698 *avno = bestk->kvno; /* and kvno to caller */
1699 UNLOCK_GLOBAL_MUTEX;
1702 UNLOCK_GLOBAL_MUTEX;
1703 return AFSCONF_NOTFOUND; /* didn't find any keys */
1706 /* get a particular key */
1708 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1710 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1712 struct afsconf_key *tk;
1716 code = afsconf_Check(adir);
1718 UNLOCK_GLOBAL_MUTEX;
1719 return AFSCONF_FAILURE;
1721 maxa = adir->keystr->nkeys;
1723 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1724 if (tk->kvno == avno) {
1725 memcpy(akey, tk->key, 8);
1726 UNLOCK_GLOBAL_MUTEX;
1731 UNLOCK_GLOBAL_MUTEX;
1732 return AFSCONF_NOTFOUND;
1735 /* save the key structure in the appropriate file */
1737 SaveKeys(struct afsconf_dir *adir)
1739 struct afsconf_keys tkeys;
1744 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1746 /* convert it to net byte order */
1747 for (i = 0; i < tkeys.nkeys; i++)
1748 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1749 tkeys.nkeys = htonl(tkeys.nkeys);
1751 /* rewrite keys file */
1752 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1753 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1755 return AFSCONF_FAILURE;
1756 i = write(fd, &tkeys, sizeof(tkeys));
1757 if (i != sizeof(tkeys)) {
1759 return AFSCONF_FAILURE;
1762 return AFSCONF_FAILURE;
1767 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1768 afs_int32 overwrite)
1770 struct afsconf_keys *tk;
1771 struct afsconf_key *tkey;
1779 if (akvno < 0 || akvno > 255) {
1780 UNLOCK_GLOBAL_MUTEX;
1785 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1786 if (tkey->kvno == akvno) {
1788 UNLOCK_GLOBAL_MUTEX;
1789 return AFSCONF_KEYINUSE;
1796 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1797 UNLOCK_GLOBAL_MUTEX;
1798 return AFSCONF_FULL;
1800 tkey = &tk->key[tk->nkeys++];
1803 memcpy(tkey->key, akey, 8);
1805 afsconf_Touch(adir);
1806 UNLOCK_GLOBAL_MUTEX;
1810 /* this proc works by sliding the other guys down, rather than using a funny
1811 kvno value, so that callers can count on getting a good key in key[0].
1814 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1816 struct afsconf_keys *tk;
1817 struct afsconf_key *tkey;
1824 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1825 if (tkey->kvno == akvno) {
1831 UNLOCK_GLOBAL_MUTEX;
1832 return AFSCONF_NOTFOUND;
1835 /* otherwise slide the others down. i and tkey point at the guy to delete */
1836 for (; i < tk->nkeys - 1; i++, tkey++) {
1837 tkey->kvno = (tkey + 1)->kvno;
1838 memcpy(tkey->key, (tkey + 1)->key, 8);
1842 afsconf_Touch(adir);
1843 UNLOCK_GLOBAL_MUTEX;