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>
15 #include <afs/pthread_glock.h>
17 #include "afs/sysincludes.h"
18 #include "afsincludes.h"
24 #include <sys/types.h>
27 #include <sys/utime.h>
29 #include <WINNT/afssw.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
40 #include <arpa/nameser_compat.h>
43 #endif /* AFS_AFSDB_ENV */
44 #endif /* AFS_NT40_ENV */
45 #include <afs/afsint.h>
60 #include <afs/afsutil.h>
61 #include "cellconfig.h"
66 #include <cm_config.h>
67 /* cm_dns.h depends on cellconfig.h */
70 #endif /* AFS_AFSDB_ENV */
75 struct afsconf_servPair {
81 static struct afsconf_servPair serviceTable[] = {
82 {"afs", "afs3-fileserver", 7000,},
83 {"afscb", "afs3-callback", 7001,},
84 {"afsprot", "afs3-prserver", 7002,},
85 {"afsvldb", "afs3-vlserver", 7003,},
86 {"afskauth", "afs3-kaserver", 7004,},
87 {"afsvol", "afs3-volserver", 7005,},
88 {"afserror", "afs3-errors", 7006,},
89 {"afsnanny", "afs3-bos", 7007,},
90 {"afsupdate", "afs3-update", 7008,},
91 {"afsrmtsys", "afs3-rmtsys", 7009,},
92 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
93 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
94 {0, 0, 0} /* insert new services before this spot */
98 static int TrimLine(char *abuffer, int abufsize);
99 static int IsClientConfigDirectory(const char *path);
101 static int GetCellNT(struct afsconf_dir *adir);
103 static int afsconf_Check(register struct afsconf_dir *adir);
104 static int afsconf_Touch(register struct afsconf_dir *adir);
105 static int GetCellUnix(struct afsconf_dir *adir);
106 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
108 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
109 char *aname, char *aclone);
110 static int ParseCellLine(register char *aline, register char *aname,
111 register char *alname);
112 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
113 static int afsconf_Reopen(register struct afsconf_dir *adir);
114 static int SaveKeys(struct afsconf_dir *adir);
117 #define T_AFSDB 18 /* per RFC1183 section 1 */
121 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
122 * our code can tell if there is new info in the key files, the cell server db
123 * files or any of the other files (and reopen the thing) if the date on
124 * CellServDB changes.
127 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
128 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
129 get an fd <= 255. We allow the fileserver to claim more fds than that.
130 This has always been a problem since pr_Initialize would have the same
131 issue, but hpr_Initialize makes it more likely that we would see this.
132 Work around it. This is not generic. It's coded with the needs of
133 afsconf_* in mind only.
135 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
140 struct afsconf_iobuffer {
147 typedef struct afsconf_iobuffer afsconf_FILE;
149 static afsconf_FILE *
150 afsconf_fopen(const char *fname, const char *fmode)
155 if ((fd = open(fname, O_RDONLY)) == -1) {
159 iop = malloc(sizeof(struct afsconf_iobuffer));
166 iop->buffer = malloc(BUFFER);
167 if (iop->buffer == NULL) {
173 iop->ptr = iop->buffer;
174 iop->endptr = iop->buffer;
179 afsconf_fclose(afsconf_FILE *iop)
185 free((void *)iop->buffer);
191 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
199 if (iop->ptr == iop->endptr) {
202 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
212 iop->ptr = iop->buffer;
213 iop->endptr = iop->buffer + len;
217 if ((p - s) == (n - 1)) {
227 #define fopen afsconf_fopen
228 #define fclose afsconf_fclose
229 #define fgets afsconf_fgets
231 #define afsconf_FILE FILE
232 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
234 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
236 afsconf_FindService(const char *aname)
238 /* lookup a service name */
240 struct afsconf_servPair *tsp;
242 if (aname == NULL || aname[0] == '\0')
245 #if defined(AFS_OSF_ENV)
246 ts = getservbyname(aname, "");
248 ts = (struct servent *) getservbyname(aname, NULL);
251 /* we found it in /etc/services, so we use this value */
252 return ts->s_port; /* already in network byte order */
255 /* not found in /etc/services, see if it is one of ours */
256 for (tsp = serviceTable; tsp->port; tsp++) {
257 if ((tsp->name && (!strcmp(tsp->name, aname)))
258 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
259 return htons(tsp->port);
265 afsconf_FindIANAName(const char *aname)
267 /* lookup a service name */
268 struct afsconf_servPair *tsp;
270 if (aname == NULL || aname[0] == '\0')
273 /* see if it is one of ours */
274 for (tsp = serviceTable; tsp->port; tsp++) {
275 if ((tsp->name && (!strcmp(tsp->name, aname)))
276 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
277 return tsp->ianaName;
283 TrimLine(char *abuffer, int abufsize)
295 strlcpy(tbuffer, tp, sizeof tbuffer);
296 strlcpy(abuffer, tbuffer, abufsize);
301 * IsClientConfigDirectory() -- determine if path matches well-known
302 * client configuration directory.
305 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
306 #else /* AFS_NT40_ENV */
307 #define IS_SEP(x) ((x) == '/')
308 #endif /* AFS_NT40_ENV */
310 IsClientConfigDirectory(const char *path)
312 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
315 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
317 cc = tolower(cdir[i]);
318 pc = tolower(path[i]);
326 #else /* AFS_NT40_ENV */
329 #endif /* AFS_NT40_ENV */
335 /* hit end of one or both; allow mismatch in existence of trailing slash */
336 if (cdir[i] != '\0') {
337 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
341 if (path[i] != '\0') {
342 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
351 afsconf_Check(register struct afsconf_dir *adir)
358 register afs_int32 code;
361 /* NT client CellServDB has different file name than NT server or Unix */
362 if (IsClientConfigDirectory(adir->name)) {
363 if (!afssw_GetClientCellServDBDir(&p)) {
364 strcompose(tbuffer, sizeof(tbuffer), p, "/",
365 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
369 strncpy(tbuffer, adir->name, sizeof(tbuffer));
370 len = (int)strlen(tbuffer);
371 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
372 strncat(tbuffer, "\\", sizeof(tbuffer));
374 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
376 tbuffer[sizeof(tbuffer) - 1] = '\0';
379 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
383 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
384 #endif /* AFS_NT40_ENV */
386 code = stat(tbuffer, &tstat);
390 /* did file change? */
391 if (tstat.st_mtime == adir->timeRead) {
394 /* otherwise file has changed, so reopen it */
395 return afsconf_Reopen(adir);
398 /* set modtime on file */
400 afsconf_Touch(register struct afsconf_dir *adir)
404 struct timeval tvp[2];
409 adir->timeRead = 0; /* just in case */
412 /* NT client CellServDB has different file name than NT server or Unix */
414 if (IsClientConfigDirectory(adir->name)) {
415 if (!afssw_GetClientCellServDBDir(&p)) {
416 strcompose(tbuffer, sizeof(tbuffer), p, "/",
417 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
420 int len = (int)strlen(tbuffer);
421 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
422 strncat(tbuffer, "\\", sizeof(tbuffer));
424 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
426 tbuffer[sizeof(tbuffer) - 1] = '\0';
429 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
433 return _utime(tbuffer, NULL);
436 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
437 gettimeofday(&tvp[0], NULL);
439 return utimes(tbuffer, tvp);
440 #endif /* AFS_NT40_ENV */
444 afsconf_Open(register const char *adir)
446 register struct afsconf_dir *tdir;
447 register afs_int32 code;
450 /* zero structure and fill in name; rest is done by internal routine */
451 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
452 memset(tdir, 0, sizeof(struct afsconf_dir));
453 tdir->name = strdup(adir);
455 code = afsconf_OpenInternal(tdir, 0, 0);
457 char *afsconf_path, afs_confdir[128];
460 /* Check global place only when local Open failed for whatever reason */
461 if (!(afsconf_path = getenv("AFSCONF"))) {
462 /* 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... */
467 if (!(home_dir = getenv("HOME"))) {
468 /* Our last chance is the "/.AFSCONF" file */
469 fp = fopen("/.AFSCONF", "r");
473 return (struct afsconf_dir *)0;
475 fgets(afs_confdir, 128, fp);
480 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
481 fp = fopen(pathname, "r");
483 /* Our last chance is the "/.AFSCONF" file */
484 fp = fopen("/.AFSCONF", "r");
488 return (struct afsconf_dir *)0;
491 fgets(afs_confdir, 128, fp);
494 len = strlen(afs_confdir);
498 return (struct afsconf_dir *)0;
500 if (afs_confdir[len - 1] == '\n') {
501 afs_confdir[len - 1] = 0;
503 afsconf_path = afs_confdir;
505 tdir->name = strdup(afsconf_path);
506 code = afsconf_OpenInternal(tdir, 0, 0);
511 return (struct afsconf_dir *)0;
519 GetCellUnix(struct afsconf_dir *adir)
526 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
527 fp = fopen(tbuffer, "r");
531 rc = fgets(tbuffer, 256, fp);
537 while (*start != '\0' && isspace(*start))
540 while (*p != '\0' && !isspace(*p))
546 adir->cellName = strdup(start);
553 GetCellNT(struct afsconf_dir *adir)
555 if (IsClientConfigDirectory(adir->name)) {
556 /* NT client config dir; ThisCell is in registry (no file). */
557 return afssw_GetClientCellName(&adir->cellName);
559 /* NT server config dir; works just like Unix */
560 return GetCellUnix(adir);
564 /* The following procedures and structs are used on Windows only
565 * to enumerate the Cell information distributed within the
566 * Windows registry. (See src/WINNT/afsd/cm_config.c)
568 typedef struct _cm_enumCellRegistry {
569 afs_uint32 client; /* non-zero if client query */
570 struct afsconf_dir *adir;
571 } cm_enumCellRegistry_t;
574 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
575 char *hostNamep, unsigned short rank)
577 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
579 if (cellInfop->numServers == MAXHOSTSPERCELL)
582 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
583 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
584 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
585 cellInfop->numServers++;
591 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
594 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
595 char linkedName[256] = "";
597 struct afsconf_entry *newEntry;
600 newEntry = malloc(sizeof(struct afsconf_entry));
601 if (newEntry == NULL)
603 newEntry->cellInfo.numServers = 0;
605 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
606 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
607 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
610 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
611 newEntry->cellInfo.name[MAXCELLCHARS-1];
613 newEntry->cellInfo.linkedCell = strdup(linkedName);
615 newEntry->cellInfo.linkedCell = NULL;
616 newEntry->cellInfo.timeout = timeout;
617 newEntry->cellInfo.flags = 0;
619 newEntry->next = enump->adir->entries;
620 enump->adir->entries = newEntry;
626 #endif /* AFS_NT40_ENV */
630 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
634 register char *tp, *bp;
635 register struct afsconf_entry *curEntry;
636 struct afsconf_aliasentry *curAlias;
637 register afs_int32 code;
639 char tbuffer[256], tbuf1[256];
642 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
643 #endif /* AFS_NT40_ENV */
645 /* figure out the local cell name */
648 enumCellRegistry.adir = adir;
650 i = GetCellUnix(adir);
653 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
659 /* now parse the individual lines */
663 /* NT client/server have a CellServDB that is the same format as Unix.
664 * However, the NT client uses a different file name
666 if (IsClientConfigDirectory(adir->name)) {
667 /* NT client config dir */
670 enumCellRegistry.client = 1;
672 if (!afssw_GetClientCellServDBDir(&p)) {
673 strcompose(tbuffer, sizeof(tbuffer), p, "/",
674 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
678 strncpy(tbuffer, adir->name, sizeof(tbuffer));
679 len = (int)strlen(tbuffer);
680 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
681 strncat(tbuffer, "\\", sizeof(tbuffer));
683 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
685 tbuffer[sizeof(tbuffer) - 1] = '\0';
688 /* NT server config dir */
689 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
693 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
694 #endif /* AFS_NT40_ENV */
696 if (!stat(tbuffer, &tstat)) {
697 adir->timeRead = tstat.st_mtime;
702 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
703 tf = fopen(tbuffer, "r");
708 /* The CellServDB file is now open.
709 * The following code parses the contents of the
710 * file and creates a list with the first cell entry
711 * in the CellServDB file at the end of the list.
713 * No checking is performed for duplicates.
714 * The side effects of this process are that duplicate
715 * entries appended to the end of the CellServDB file
716 * take precedence and are found in a shorter period
721 tp = fgets(tbuffer, sizeof(tbuffer), tf);
724 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
725 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
726 continue; /* empty line */
727 if (tbuffer[0] == '>') {
728 char linkedcell[MAXCELLCHARS];
729 /* start new cell item */
731 /* thread this guy on the list */
732 curEntry->next = adir->entries;
733 adir->entries = curEntry;
737 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
738 memset(curEntry, 0, sizeof(struct afsconf_entry));
740 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
742 afsconf_CloseInternal(adir);
747 if (linkedcell[0] != '\0')
748 curEntry->cellInfo.linkedCell = strdup(linkedcell);
750 /* new host in the current cell */
752 afsconf_CloseInternal(adir);
756 i = curEntry->cellInfo.numServers;
757 if (i < MAXHOSTSPERCELL) {
758 if (cell && !strcmp(cell, curEntry->cellInfo.name))
760 ParseHostLine(tbuffer,
761 &curEntry->cellInfo.hostAddr[i],
762 curEntry->cellInfo.hostName[i],
766 ParseHostLine(tbuffer,
767 &curEntry->cellInfo.hostAddr[i],
768 curEntry->cellInfo.hostName[i], 0);
771 if (code == AFSCONF_SYNTAX) {
772 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
778 "Can't properly parse host line \"%s\" in configuration file %s\n",
783 afsconf_CloseInternal(adir);
786 curEntry->cellInfo.numServers = ++i;
789 "Too many hosts for cell %s in configuration file %s\n",
790 curEntry->cellInfo.name, tbuf1);
794 fclose(tf); /* close the file now */
796 /* end the last partially-completed cell */
798 curEntry->next = adir->entries;
799 adir->entries = curEntry;
804 * Windows maintains a CellServDB list in the Registry
805 * that supercedes the contents of the CellServDB file.
806 * Prepending these entries to the head of the list
807 * is sufficient to enforce the precedence.
809 cm_EnumerateCellRegistry( enumCellRegistry.client,
810 cm_enumCellRegistryProc,
812 #endif /* AFS_NT40_ENV */
814 /* Read in the alias list */
815 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
817 tf = fopen(tbuffer, "r");
821 tp = fgets(tbuffer, sizeof(tbuffer), tf);
824 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
826 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
827 continue; /* empty line */
830 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
833 continue; /* invalid line */
835 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
838 continue; /* invalid line */
841 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
846 curAlias = malloc(sizeof(*curAlias));
847 memset(curAlias, 0, sizeof(*curAlias));
849 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
850 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
852 curAlias->next = adir->alias_entries;
853 adir->alias_entries = curAlias;
858 /* now read the fs keys, if possible */
859 adir->keystr = (struct afsconf_keys *)0;
860 afsconf_IntGetKeys(adir);
865 /* parse a line of the form
866 *"128.2.1.3 #hostname" or
867 *"[128.2.1.3] #hostname" for clones
868 * into the appropriate pieces.
871 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
875 register afs_int32 code;
881 /* FIXME: length of aname unknown here */
882 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
886 /* FIXME: length of aname unknown here */
887 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
890 return AFSCONF_SYNTAX;
891 addr->sin_family = AF_INET;
893 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
894 addr->sin_len = sizeof(struct sockaddr_in);
896 tp = (char *)&addr->sin_addr;
904 /* parse a line of the form
905 * ">cellname [linkedcellname] [#comments]"
906 * into the appropriate pieces.
909 ParseCellLine(register char *aline, register char *aname,
910 register char *alname)
913 /* FIXME: length of aname, alname unknown here */
914 code = sscanf(aline, ">%s %s", aname, alname);
918 if (*alname == '#') {
922 return (code > 0 ? 0 : AFSCONF_SYNTAX);
925 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
927 afsconf_CellApply(struct afsconf_dir *adir,
928 int (*aproc) (struct afsconf_cell * cell, void *arock,
929 struct afsconf_dir * dir), void *arock)
931 register struct afsconf_entry *tde;
932 register afs_int32 code;
934 for (tde = adir->entries; tde; tde = tde->next) {
935 code = (*aproc) (&tde->cellInfo, arock, adir);
945 /* call aproc(entry, arock, adir) for all cell aliases.
946 * Proc must return 0, or we'll stop early and return the code it returns
949 afsconf_CellAliasApply(struct afsconf_dir *adir,
950 int (*aproc) (struct afsconf_cellalias * alias,
951 void *arock, struct afsconf_dir * dir),
954 register struct afsconf_aliasentry *tde;
955 register afs_int32 code;
957 for (tde = adir->alias_entries; tde; tde = tde->next) {
958 code = (*aproc) (&tde->aliasInfo, arock, adir);
968 afs_int32 afsconf_SawCell = 0;
971 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
972 char *aservice, struct afsconf_cell *acellInfo,
978 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
985 cell = (char *)&acellInfo->name;
987 code = afsconf_OpenInternal(adir, cell, clones);
992 #if !defined(AFS_NT40_ENV)
994 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
995 struct afsconf_cell *acellInfo)
998 int tservice, i, len;
999 unsigned char answer[1024];
1003 char realCellName[256];
1009 /* The resolver isn't always MT-safe.. Perhaps this ought to be
1010 * replaced with a more fine-grained lock just for the resolver
1015 if ( ! strchr(acellName,'.') ) {
1016 cellnamelength=strlen(acellName);
1017 dotcellname=malloc(cellnamelength+2);
1018 memcpy(dotcellname,acellName,cellnamelength);
1019 dotcellname[cellnamelength]='.';
1020 dotcellname[cellnamelength+1]=0;
1022 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
1024 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1026 UNLOCK_GLOBAL_MUTEX;
1030 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1031 UNLOCK_GLOBAL_MUTEX;
1039 return AFSCONF_NOTFOUND;
1042 p = answer + sizeof(HEADER); /* Skip header */
1043 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1045 return AFSCONF_NOTFOUND;
1047 p += code + QFIXEDSZ; /* Skip name */
1049 while (p < answer + len) {
1050 int type, ttl, size;
1052 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1054 return AFSCONF_NOTFOUND;
1056 p += code; /* Skip the name */
1057 type = (p[0] << 8) | p[1];
1058 p += 4; /* Skip type and class */
1059 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1060 p += 4; /* Skip the TTL */
1061 size = (p[0] << 8) | p[1];
1062 p += 2; /* Skip the size */
1064 if (type == T_AFSDB) {
1068 afsdb_type = (p[0] << 8) | p[1];
1069 if (afsdb_type == 1) {
1071 * We know this is an AFSDB record for our cell, of the
1072 * right AFSDB type. Write down the true cell name that
1073 * the resolver gave us above.
1075 strlcpy(realCellName, host, sizeof realCellName);
1078 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1080 return AFSCONF_NOTFOUND;
1082 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1083 /* Do we want to get TTL data for the A record as well? */
1084 (he = gethostbyname(host))) {
1086 memcpy(&ipaddr, he->h_addr, he->h_length);
1087 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1088 strncpy(acellInfo->hostName[server_num], host,
1089 sizeof(acellInfo->hostName[server_num]));
1092 if (!minttl || ttl < minttl)
1100 if (server_num == 0) /* No AFSDB records */
1101 return AFSCONF_NOTFOUND;
1103 /* Convert the real cell name to lowercase */
1104 for (p = (unsigned char *)realCellName; *p; p++)
1107 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1108 acellInfo->numServers = server_num;
1111 tservice = afsconf_FindService(aservice);
1113 return AFSCONF_NOTFOUND; /* service not found */
1114 for (i = 0; i < acellInfo->numServers; i++) {
1115 acellInfo->hostAddr[i].sin_port = tservice;
1119 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1125 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1126 struct afsconf_cell *acellInfo)
1128 register afs_int32 i;
1130 struct afsconf_entry DNSce;
1131 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1132 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1133 unsigned short ipRanks[AFSMAXCELLHOSTS];
1138 DNSce.cellInfo.numServers = 0;
1140 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1142 /* ignore the ttl here since this code is only called by transitory programs
1143 * like klog, etc. */
1146 if (numServers == 0)
1149 for (i = 0; i < numServers; i++) {
1150 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1152 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1153 acellInfo->hostAddr[i].sin_family = AF_INET;
1155 /* sin_port supplied by connection code */
1158 acellInfo->numServers = numServers;
1159 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1162 tservice = afsconf_FindService(aservice);
1163 UNLOCK_GLOBAL_MUTEX;
1165 return AFSCONF_NOTFOUND; /* service not found */
1167 for (i = 0; i < acellInfo->numServers; i++) {
1168 acellInfo->hostAddr[i].sin_port = tservice;
1171 acellInfo->linkedCell = NULL; /* no linked cell */
1172 acellInfo->flags = 0;
1175 #endif /* windows */
1176 #endif /* AFS_AFSDB_ENV */
1179 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1180 struct afsconf_cell *acellInfo)
1182 register struct afsconf_entry *tce;
1183 struct afsconf_aliasentry *tcae;
1184 struct afsconf_entry *bestce;
1185 register afs_int32 i;
1194 afsconf_Check(adir);
1197 cnLen = (int)(strlen(tcell) + 1);
1198 lcstring(tcell, tcell, cnLen);
1199 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1200 /* call to afsconf_GetLocalCell: like klog */
1202 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1204 UNLOCK_GLOBAL_MUTEX;
1209 cnLen = strlen(tcell);
1210 bestce = (struct afsconf_entry *)0;
1213 UNLOCK_GLOBAL_MUTEX;
1217 /* Look through the list of aliases */
1218 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1219 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1220 tcell = tcae->aliasInfo.realName;
1225 for (tce = adir->entries; tce; tce = tce->next) {
1226 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1227 /* found our cell */
1232 if (strlen(tce->cellInfo.name) < cnLen)
1233 continue; /* clearly wrong */
1234 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1236 ambig = 1; /* ambiguous unless we get exact match */
1240 if (!ambig && bestce && bestce->cellInfo.numServers) {
1241 *acellInfo = bestce->cellInfo; /* structure assignment */
1243 tservice = afsconf_FindService(aservice);
1245 UNLOCK_GLOBAL_MUTEX;
1246 return AFSCONF_NOTFOUND; /* service not found */
1248 for (i = 0; i < acellInfo->numServers; i++) {
1249 acellInfo->hostAddr[i].sin_port = tservice;
1252 acellInfo->timeout = 0;
1255 * Until we figure out how to separate out ubik server
1256 * queries from other server queries, only perform gethostbyname()
1257 * lookup on the specified hostnames for the client CellServDB files.
1259 if (IsClientConfigDirectory(adir->name) &&
1260 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1262 short numServers=0; /*Num active servers for the cell */
1263 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1264 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1266 memset(&hostAddr, 0, sizeof(hostAddr));
1267 memset(&hostName, 0, sizeof(hostName));
1269 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1270 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1273 if (he && he->h_addrtype == AF_INET) {
1275 /* obtain all the valid address from the list */
1276 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1277 /* check to see if this is a new address; if so insert it into the list */
1279 for (k=0, dup=0; !dup && k < numServers; k++) {
1280 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1286 hostAddr[numServers].sin_family = AF_INET;
1287 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1288 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1289 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1291 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1292 strcpy(hostName[numServers], acellInfo->hostName[j]);
1298 hostAddr[numServers] = acellInfo->hostAddr[j];
1299 strcpy(hostName[numServers], acellInfo->hostName[j]);
1304 for (i=0; i<numServers; i++) {
1305 acellInfo->hostAddr[i] = hostAddr[i];
1306 strcpy(acellInfo->hostName[i], hostName[i]);
1308 acellInfo->numServers = numServers;
1309 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1311 UNLOCK_GLOBAL_MUTEX;
1314 UNLOCK_GLOBAL_MUTEX;
1315 #ifdef AFS_AFSDB_ENV
1316 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1318 return AFSCONF_NOTFOUND;
1319 #endif /* AFS_AFSDB_ENV */
1324 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1327 static int afsconf_showcell = 0;
1333 * If a cell switch was specified in a command, then it should override the
1334 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1335 * is set and the cell name in the adir structure is used.
1336 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1338 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1339 if (!afsconf_showcell) {
1340 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1342 afsconf_showcell = 1;
1344 strncpy(aname, afscell_path, alen);
1346 afsconf_Check(adir);
1347 if (adir->cellName) {
1348 strncpy(aname, adir->cellName, alen);
1350 code = AFSCONF_UNKNOWN;
1353 UNLOCK_GLOBAL_MUTEX;
1358 afsconf_Close(struct afsconf_dir *adir)
1361 afsconf_CloseInternal(adir);
1365 UNLOCK_GLOBAL_MUTEX;
1370 afsconf_CloseInternal(register struct afsconf_dir *adir)
1372 register struct afsconf_entry *td, *nd;
1373 struct afsconf_aliasentry *ta, *na;
1374 register char *tname;
1376 tname = adir->name; /* remember name, since that's all we preserve */
1378 /* free everything we can find */
1380 free(adir->cellName);
1381 for (td = adir->entries; td; td = nd) {
1383 if (td->cellInfo.linkedCell)
1384 free(td->cellInfo.linkedCell);
1387 for (ta = adir->alias_entries; ta; ta = na) {
1395 memset(adir, 0, sizeof(struct afsconf_dir));
1396 adir->name = tname; /* restore it */
1401 afsconf_Reopen(register struct afsconf_dir *adir)
1403 register afs_int32 code;
1404 code = afsconf_CloseInternal(adir);
1407 code = afsconf_OpenInternal(adir, 0, 0);
1411 /* called during opening of config file */
1413 afsconf_IntGetKeys(struct afsconf_dir *adir)
1417 struct afsconf_keys *tstr;
1418 register afs_int32 code;
1421 /* NT client config dir has no KeyFile; don't risk attempting open
1422 * because there might be a random file of this name if dir is shared.
1424 if (IsClientConfigDirectory(adir->name)) {
1425 adir->keystr = ((struct afsconf_keys *)
1426 malloc(sizeof(struct afsconf_keys)));
1427 adir->keystr->nkeys = 0;
1430 #endif /* AFS_NT40_ENV */
1433 /* compute the key name and other setup */
1434 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1435 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1436 adir->keystr = tstr;
1439 fd = open(tbuffer, O_RDONLY);
1442 UNLOCK_GLOBAL_MUTEX;
1445 code = read(fd, tstr, sizeof(struct afsconf_keys));
1447 if (code < sizeof(afs_int32)) {
1449 UNLOCK_GLOBAL_MUTEX;
1453 /* convert key structure to host order */
1454 tstr->nkeys = ntohl(tstr->nkeys);
1456 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1458 UNLOCK_GLOBAL_MUTEX;
1462 for (fd = 0; fd < tstr->nkeys; fd++)
1463 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1465 UNLOCK_GLOBAL_MUTEX;
1469 /* get keys structure */
1471 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1473 register afs_int32 code;
1476 code = afsconf_Check(adir);
1478 UNLOCK_GLOBAL_MUTEX;
1479 return AFSCONF_FAILURE;
1481 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1482 UNLOCK_GLOBAL_MUTEX;
1486 /* get latest key */
1488 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1489 struct ktc_encryptionKey *akey)
1493 register struct afsconf_key *tk;
1494 register afs_int32 best;
1495 struct afsconf_key *bestk;
1496 register afs_int32 code;
1499 code = afsconf_Check(adir);
1501 UNLOCK_GLOBAL_MUTEX;
1502 return AFSCONF_FAILURE;
1504 maxa = adir->keystr->nkeys;
1506 best = -1; /* highest kvno we've seen yet */
1507 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1508 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1509 if (tk->kvno == 999)
1510 continue; /* skip bcrypt keys */
1511 if (tk->kvno > best) {
1516 if (bestk) { /* found any */
1518 memcpy(akey, bestk->key, 8); /* copy out latest key */
1520 *avno = bestk->kvno; /* and kvno to caller */
1521 UNLOCK_GLOBAL_MUTEX;
1524 UNLOCK_GLOBAL_MUTEX;
1525 return AFSCONF_NOTFOUND; /* didn't find any keys */
1528 /* get a particular key */
1530 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1532 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1533 register int i, maxa;
1534 register struct afsconf_key *tk;
1535 register afs_int32 code;
1538 code = afsconf_Check(adir);
1540 UNLOCK_GLOBAL_MUTEX;
1541 return AFSCONF_FAILURE;
1543 maxa = adir->keystr->nkeys;
1545 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1546 if (tk->kvno == avno) {
1547 memcpy(akey, tk->key, 8);
1548 UNLOCK_GLOBAL_MUTEX;
1553 UNLOCK_GLOBAL_MUTEX;
1554 return AFSCONF_NOTFOUND;
1557 /* save the key structure in the appropriate file */
1559 SaveKeys(struct afsconf_dir *adir)
1561 struct afsconf_keys tkeys;
1563 register afs_int32 i;
1566 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1568 /* convert it to net byte order */
1569 for (i = 0; i < tkeys.nkeys; i++)
1570 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1571 tkeys.nkeys = htonl(tkeys.nkeys);
1573 /* rewrite keys file */
1574 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1575 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1577 return AFSCONF_FAILURE;
1578 i = write(fd, &tkeys, sizeof(tkeys));
1579 if (i != sizeof(tkeys)) {
1581 return AFSCONF_FAILURE;
1584 return AFSCONF_FAILURE;
1589 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1590 afs_int32 overwrite)
1592 register struct afsconf_keys *tk;
1593 register struct afsconf_key *tkey;
1594 register afs_int32 i;
1601 if (akvno < 0 || akvno > 255) {
1602 UNLOCK_GLOBAL_MUTEX;
1607 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1608 if (tkey->kvno == akvno) {
1610 UNLOCK_GLOBAL_MUTEX;
1611 return AFSCONF_KEYINUSE;
1618 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1619 UNLOCK_GLOBAL_MUTEX;
1620 return AFSCONF_FULL;
1622 tkey = &tk->key[tk->nkeys++];
1625 memcpy(tkey->key, akey, 8);
1627 afsconf_Touch(adir);
1628 UNLOCK_GLOBAL_MUTEX;
1632 /* this proc works by sliding the other guys down, rather than using a funny
1633 kvno value, so that callers can count on getting a good key in key[0].
1636 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1638 register struct afsconf_keys *tk;
1639 register struct afsconf_key *tkey;
1646 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1647 if (tkey->kvno == akvno) {
1653 UNLOCK_GLOBAL_MUTEX;
1654 return AFSCONF_NOTFOUND;
1657 /* otherwise slide the others down. i and tkey point at the guy to delete */
1658 for (; i < tk->nkeys - 1; i++, tkey++) {
1659 tkey->kvno = (tkey + 1)->kvno;
1660 memcpy(tkey->key, (tkey + 1)->key, 8);
1664 afsconf_Touch(adir);
1665 UNLOCK_GLOBAL_MUTEX;