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>
17 #include <afs/pthread_glock.h>
19 #include "afs/sysincludes.h"
20 #include "afsincludes.h"
25 #include <sys/types.h>
28 #include <sys/utime.h>
30 #include <WINNT/afssw.h>
32 #include <sys/socket.h>
33 #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 static struct afsconf_servPair serviceTable[] = {
86 {"afsres", 7010,}, /* residency database for MR-AFS */
87 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
88 {0, 0} /* insert new services before this spot */
92 static afs_int32 afsconf_FindService(register const char *aname);
93 static int TrimLine(char *abuffer);
94 static int IsClientConfigDirectory(const char *path);
96 static int GetCellNT(struct afsconf_dir *adir);
98 static int afsconf_Check(register struct afsconf_dir *adir);
99 static int afsconf_Touch(register struct afsconf_dir *adir);
100 static int GetCellUnix(struct afsconf_dir *adir);
101 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
103 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
104 char *aname, char *aclone);
105 static int ParseCellLine(register char *aline, register char *aname,
106 register char *alname);
107 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
108 static int afsconf_Reopen(register struct afsconf_dir *adir);
109 static int SaveKeys(struct afsconf_dir *adir);
112 #define T_AFSDB 18 /* per RFC1183 section 1 */
116 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
117 * our code can tell if there is new info in the key files, the cell server db
118 * files or any of the other files (and reopen the thing) if the date on
119 * CellServDB changes.
122 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
123 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
124 get an fd <= 255. We allow the fileserver to claim more fds than that.
125 This has always been a problem since pr_Initialize would have the same
126 issue, but hpr_Initialize makes it more likely that we would see this.
127 Work around it. This is not generic. It's coded with the needs of
128 afsconf_* in mind only.
130 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
135 struct afsconf_iobuffer {
142 typedef struct afsconf_iobuffer afsconf_FILE;
144 static afsconf_FILE *
145 afsconf_fopen(const char *fname, const char *fmode)
150 if ((fd = open(fname, O_RDONLY)) == -1) {
154 iop = malloc(sizeof(struct afsconf_iobuffer));
161 iop->buffer = malloc(BUFFER);
162 if (iop->buffer == NULL) {
168 iop->ptr = iop->buffer;
169 iop->endptr = iop->buffer;
174 afsconf_fclose(afsconf_FILE *iop)
180 free((void *)iop->buffer);
186 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
194 if (iop->ptr == iop->endptr) {
197 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
207 iop->ptr = iop->buffer;
208 iop->endptr = iop->buffer + len;
212 if ((p - s) == (n - 1)) {
222 #define fopen afsconf_fopen
223 #define fclose afsconf_fclose
224 #define fgets afsconf_fgets
226 #define afsconf_FILE FILE
227 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
229 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
231 afsconf_FindService(register const char *aname)
233 /* lookup a service name */
235 register struct afsconf_servPair *tsp;
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++) {
249 if (tsp->name == NULL)
251 if (!strcmp(tsp->name, aname))
252 return htons(tsp->port);
257 TrimLine(char *abuffer)
270 strcpy(abuffer, tbuffer);
275 * IsClientConfigDirectory() -- determine if path matches well-known
276 * client configuration directory.
279 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
280 #else /* AFS_NT40_ENV */
281 #define IS_SEP(x) ((x) == '/')
282 #endif /* AFS_NT40_ENV */
284 IsClientConfigDirectory(const char *path)
286 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
289 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
291 cc = tolower(cdir[i]);
292 pc = tolower(path[i]);
300 #else /* AFS_NT40_ENV */
303 #endif /* AFS_NT40_ENV */
309 /* hit end of one or both; allow mismatch in existence of trailing slash */
310 if (cdir[i] != '\0') {
311 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
315 if (path[i] != '\0') {
316 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
325 afsconf_Check(register struct afsconf_dir *adir)
332 register afs_int32 code;
335 /* NT client CellServDB has different file name than NT server or Unix */
336 if (IsClientConfigDirectory(adir->name)) {
337 if (!afssw_GetClientCellServDBDir(&p)) {
338 strcompose(tbuffer, sizeof(tbuffer), p, "/",
339 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
343 strncpy(tbuffer, adir->name, sizeof(tbuffer));
344 len = (int)strlen(tbuffer);
345 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
346 strncat(tbuffer, "\\", sizeof(tbuffer));
348 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
350 tbuffer[sizeof(tbuffer) - 1] = '\0';
353 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
357 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
358 #endif /* AFS_NT40_ENV */
360 code = stat(tbuffer, &tstat);
364 /* did file change? */
365 if (tstat.st_mtime == adir->timeRead) {
368 /* otherwise file has changed, so reopen it */
369 return afsconf_Reopen(adir);
372 /* set modtime on file */
374 afsconf_Touch(register struct afsconf_dir *adir)
378 struct timeval tvp[2];
383 adir->timeRead = 0; /* just in case */
386 /* NT client CellServDB has different file name than NT server or Unix */
388 if (IsClientConfigDirectory(adir->name)) {
389 if (!afssw_GetClientCellServDBDir(&p)) {
390 strcompose(tbuffer, sizeof(tbuffer), p, "/",
391 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
394 int len = (int)strlen(tbuffer);
395 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
396 strncat(tbuffer, "\\", sizeof(tbuffer));
398 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
400 tbuffer[sizeof(tbuffer) - 1] = '\0';
403 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
407 return _utime(tbuffer, NULL);
410 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
411 gettimeofday(&tvp[0], NULL);
413 return utimes(tbuffer, tvp);
414 #endif /* AFS_NT40_ENV */
418 afsconf_Open(register const char *adir)
420 register struct afsconf_dir *tdir;
421 register afs_int32 code;
424 /* zero structure and fill in name; rest is done by internal routine */
425 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
426 memset(tdir, 0, sizeof(struct afsconf_dir));
427 tdir->name = (char *)malloc(strlen(adir) + 1);
428 strcpy(tdir->name, adir);
430 code = afsconf_OpenInternal(tdir, 0, 0);
432 char *afsconf_path, afs_confdir[128];
435 /* Check global place only when local Open failed for whatever reason */
436 if (!(afsconf_path = getenv("AFSCONF"))) {
437 /* 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... */
442 if (!(home_dir = getenv("HOME"))) {
443 /* Our last chance is the "/.AFSCONF" file */
444 fp = fopen("/.AFSCONF", "r");
448 return (struct afsconf_dir *)0;
450 fgets(afs_confdir, 128, fp);
455 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
456 fp = fopen(pathname, "r");
458 /* Our last chance is the "/.AFSCONF" file */
459 fp = fopen("/.AFSCONF", "r");
463 return (struct afsconf_dir *)0;
466 fgets(afs_confdir, 128, fp);
469 len = strlen(afs_confdir);
473 return (struct afsconf_dir *)0;
475 if (afs_confdir[len - 1] == '\n') {
476 afs_confdir[len - 1] = 0;
478 afsconf_path = afs_confdir;
480 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
481 strcpy(tdir->name, afsconf_path);
482 code = afsconf_OpenInternal(tdir, 0, 0);
487 return (struct afsconf_dir *)0;
495 GetCellUnix(struct afsconf_dir *adir)
502 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
503 fp = fopen(tbuffer, "r");
507 rc = fgets(tbuffer, 256, fp);
513 while (*start != '\0' && isspace(*start))
516 while (*p != '\0' && !isspace(*p))
522 adir->cellName = strdup(start);
529 GetCellNT(struct afsconf_dir *adir)
531 if (IsClientConfigDirectory(adir->name)) {
532 /* NT client config dir; ThisCell is in registry (no file). */
533 return afssw_GetClientCellName(&adir->cellName);
535 /* NT server config dir; works just like Unix */
536 return GetCellUnix(adir);
540 /* The following procedures and structs are used on Windows only
541 * to enumerate the Cell information distributed within the
542 * Windows registry. (See src/WINNT/afsd/cm_config.c)
544 typedef struct _cm_enumCellRegistry {
545 afs_uint32 client; /* non-zero if client query */
546 struct afsconf_dir *adir;
547 } cm_enumCellRegistry_t;
550 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
551 char *hostNamep, unsigned short rank)
553 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
555 if (cellInfop->numServers == MAXHOSTSPERCELL)
558 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
559 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
560 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
561 cellInfop->numServers++;
567 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
570 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
571 char linkedName[256] = "";
573 struct afsconf_entry *newEntry;
576 newEntry = malloc(sizeof(struct afsconf_entry));
577 if (newEntry == NULL)
579 newEntry->cellInfo.numServers = 0;
581 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
582 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
583 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
586 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
587 newEntry->cellInfo.name[MAXCELLCHARS-1];
589 newEntry->cellInfo.linkedCell = strdup(linkedName);
591 newEntry->cellInfo.linkedCell = NULL;
592 newEntry->cellInfo.timeout = timeout;
593 newEntry->cellInfo.flags = 0;
595 newEntry->next = enump->adir->entries;
596 enump->adir->entries = newEntry;
602 #endif /* AFS_NT40_ENV */
606 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
610 register char *tp, *bp;
611 register struct afsconf_entry *curEntry;
612 struct afsconf_aliasentry *curAlias;
613 register afs_int32 code;
615 char tbuffer[256], tbuf1[256];
618 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
619 #endif /* AFS_NT40_ENV */
621 /* figure out the local cell name */
624 enumCellRegistry.adir = adir;
626 i = GetCellUnix(adir);
629 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
635 /* now parse the individual lines */
639 /* NT client/server have a CellServDB that is the same format as Unix.
640 * However, the NT client uses a different file name
642 if (IsClientConfigDirectory(adir->name)) {
643 /* NT client config dir */
646 enumCellRegistry.client = 1;
648 if (!afssw_GetClientCellServDBDir(&p)) {
649 strcompose(tbuffer, sizeof(tbuffer), p, "/",
650 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
654 strncpy(tbuffer, adir->name, sizeof(tbuffer));
655 len = (int)strlen(tbuffer);
656 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
657 strncat(tbuffer, "\\", sizeof(tbuffer));
659 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
661 tbuffer[sizeof(tbuffer) - 1] = '\0';
664 /* NT server config dir */
665 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
669 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
670 #endif /* AFS_NT40_ENV */
672 if (!stat(tbuffer, &tstat)) {
673 adir->timeRead = tstat.st_mtime;
678 strcpy(tbuf1, tbuffer);
679 tf = fopen(tbuffer, "r");
684 /* The CellServDB file is now open.
685 * The following code parses the contents of the
686 * file and creates a list with the first cell entry
687 * in the CellServDB file at the end of the list.
689 * No checking is performed for duplicates.
690 * The side effects of this process are that duplicate
691 * entries appended to the end of the CellServDB file
692 * take precedence and are found in a shorter period
697 tp = fgets(tbuffer, sizeof(tbuffer), tf);
700 TrimLine(tbuffer); /* remove white space */
701 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
702 continue; /* empty line */
703 if (tbuffer[0] == '>') {
704 char linkedcell[MAXCELLCHARS];
705 /* start new cell item */
707 /* thread this guy on the list */
708 curEntry->next = adir->entries;
709 adir->entries = curEntry;
713 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
714 memset(curEntry, 0, sizeof(struct afsconf_entry));
716 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
718 afsconf_CloseInternal(adir);
723 if (linkedcell[0] != '\0') {
724 curEntry->cellInfo.linkedCell =
725 (char *)malloc(strlen(linkedcell) + 1);
726 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
729 /* new host in the current cell */
731 afsconf_CloseInternal(adir);
735 i = curEntry->cellInfo.numServers;
736 if (i < MAXHOSTSPERCELL) {
737 if (cell && !strcmp(cell, curEntry->cellInfo.name))
739 ParseHostLine(tbuffer,
740 &curEntry->cellInfo.hostAddr[i],
741 curEntry->cellInfo.hostName[i],
745 ParseHostLine(tbuffer,
746 &curEntry->cellInfo.hostAddr[i],
747 curEntry->cellInfo.hostName[i], 0);
750 if (code == AFSCONF_SYNTAX) {
751 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
757 "Can't properly parse host line \"%s\" in configuration file %s\n",
762 afsconf_CloseInternal(adir);
765 curEntry->cellInfo.numServers = ++i;
768 "Too many hosts for cell %s in configuration file %s\n",
769 curEntry->cellInfo.name, tbuf1);
773 fclose(tf); /* close the file now */
775 /* end the last partially-completed cell */
777 curEntry->next = adir->entries;
778 adir->entries = curEntry;
783 * Windows maintains a CellServDB list in the Registry
784 * that supercedes the contents of the CellServDB file.
785 * Prepending these entries to the head of the list
786 * is sufficient to enforce the precedence.
788 cm_EnumerateCellRegistry( enumCellRegistry.client,
789 cm_enumCellRegistryProc,
791 #endif /* AFS_NT40_ENV */
793 /* Read in the alias list */
794 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
796 tf = fopen(tbuffer, "r");
800 tp = fgets(tbuffer, sizeof(tbuffer), tf);
803 TrimLine(tbuffer); /* remove white space */
805 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
806 continue; /* empty line */
809 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
812 continue; /* invalid line */
814 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
817 continue; /* invalid line */
820 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
825 curAlias = malloc(sizeof(*curAlias));
826 memset(curAlias, 0, sizeof(*curAlias));
828 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
829 strcpy(curAlias->aliasInfo.realName, tbuffer);
831 curAlias->next = adir->alias_entries;
832 adir->alias_entries = curAlias;
837 /* now read the fs keys, if possible */
838 adir->keystr = (struct afsconf_keys *)0;
839 afsconf_IntGetKeys(adir);
844 /* parse a line of the form
845 *"128.2.1.3 #hostname" or
846 *"[128.2.1.3] #hostname" for clones
847 * into the appropriate pieces.
850 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
854 register afs_int32 code;
860 /* FIXME: length of aname unknown here */
861 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
865 /* FIXME: length of aname unknown here */
866 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
869 return AFSCONF_SYNTAX;
870 addr->sin_family = AF_INET;
872 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
873 addr->sin_len = sizeof(struct sockaddr_in);
875 tp = (char *)&addr->sin_addr;
883 /* parse a line of the form
884 * ">cellname [linkedcellname] [#comments]"
885 * into the appropriate pieces.
888 ParseCellLine(register char *aline, register char *aname,
889 register char *alname)
892 /* FIXME: length of aname, alname unknown here */
893 code = sscanf(aline, ">%s %s", aname, alname);
897 if (*alname == '#') {
901 return (code > 0 ? 0 : AFSCONF_SYNTAX);
904 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
906 afsconf_CellApply(struct afsconf_dir *adir,
907 int (*aproc) (struct afsconf_cell * cell, void *arock,
908 struct afsconf_dir * dir), void *arock)
910 register struct afsconf_entry *tde;
911 register afs_int32 code;
913 for (tde = adir->entries; tde; tde = tde->next) {
914 code = (*aproc) (&tde->cellInfo, arock, adir);
924 /* call aproc(entry, arock, adir) for all cell aliases.
925 * Proc must return 0, or we'll stop early and return the code it returns
928 afsconf_CellAliasApply(struct afsconf_dir *adir,
929 int (*aproc) (struct afsconf_cellalias * alias,
930 void *arock, struct afsconf_dir * dir),
933 register struct afsconf_aliasentry *tde;
934 register afs_int32 code;
936 for (tde = adir->alias_entries; tde; tde = tde->next) {
937 code = (*aproc) (&tde->aliasInfo, arock, adir);
947 afs_int32 afsconf_SawCell = 0;
950 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
951 char *aservice, struct afsconf_cell *acellInfo,
957 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
964 cell = (char *)&acellInfo->name;
966 code = afsconf_OpenInternal(adir, cell, clones);
971 #if !defined(AFS_NT40_ENV)
973 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
974 struct afsconf_cell *acellInfo)
977 int tservice, i, len;
978 unsigned char answer[1024];
982 char realCellName[256];
988 /* The resolver isn't always MT-safe.. Perhaps this ought to be
989 * replaced with a more fine-grained lock just for the resolver
994 if ( ! strchr(acellName,'.') ) {
995 cellnamelength=strlen(acellName);
996 dotcellname=malloc(cellnamelength+2);
997 memcpy(dotcellname,acellName,cellnamelength);
998 dotcellname[cellnamelength]='.';
999 dotcellname[cellnamelength+1]=0;
1001 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
1003 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1005 UNLOCK_GLOBAL_MUTEX;
1009 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1010 UNLOCK_GLOBAL_MUTEX;
1018 return AFSCONF_NOTFOUND;
1021 p = answer + sizeof(HEADER); /* Skip header */
1022 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1024 return AFSCONF_NOTFOUND;
1026 p += code + QFIXEDSZ; /* Skip name */
1028 while (p < answer + len) {
1029 int type, ttl, size;
1031 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1033 return AFSCONF_NOTFOUND;
1035 p += code; /* Skip the name */
1036 type = (p[0] << 8) | p[1];
1037 p += 4; /* Skip type and class */
1038 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1039 p += 4; /* Skip the TTL */
1040 size = (p[0] << 8) | p[1];
1041 p += 2; /* Skip the size */
1043 if (type == T_AFSDB) {
1047 afsdb_type = (p[0] << 8) | p[1];
1048 if (afsdb_type == 1) {
1050 * We know this is an AFSDB record for our cell, of the
1051 * right AFSDB type. Write down the true cell name that
1052 * the resolver gave us above.
1054 strcpy(realCellName, host);
1057 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1059 return AFSCONF_NOTFOUND;
1061 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1062 /* Do we want to get TTL data for the A record as well? */
1063 (he = gethostbyname(host))) {
1065 memcpy(&ipaddr, he->h_addr, he->h_length);
1066 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1067 strncpy(acellInfo->hostName[server_num], host,
1068 sizeof(acellInfo->hostName[server_num]));
1071 if (!minttl || ttl < minttl)
1079 if (server_num == 0) /* No AFSDB records */
1080 return AFSCONF_NOTFOUND;
1082 /* Convert the real cell name to lowercase */
1083 for (p = (unsigned char *)realCellName; *p; p++)
1086 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1087 acellInfo->numServers = server_num;
1090 tservice = afsconf_FindService(aservice);
1092 return AFSCONF_NOTFOUND; /* service not found */
1093 for (i = 0; i < acellInfo->numServers; i++) {
1094 acellInfo->hostAddr[i].sin_port = tservice;
1098 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1104 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1105 struct afsconf_cell *acellInfo)
1107 register afs_int32 i;
1109 struct afsconf_entry DNSce;
1110 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1111 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1112 unsigned short ipRanks[AFSMAXCELLHOSTS];
1117 DNSce.cellInfo.numServers = 0;
1119 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1121 /* ignore the ttl here since this code is only called by transitory programs
1122 * like klog, etc. */
1125 if (numServers == 0)
1128 for (i = 0; i < numServers; i++) {
1129 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1131 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1132 acellInfo->hostAddr[i].sin_family = AF_INET;
1134 /* sin_port supplied by connection code */
1137 acellInfo->numServers = numServers;
1138 strcpy(acellInfo->name, acellName);
1141 tservice = afsconf_FindService(aservice);
1142 UNLOCK_GLOBAL_MUTEX;
1144 return AFSCONF_NOTFOUND; /* service not found */
1146 for (i = 0; i < acellInfo->numServers; i++) {
1147 acellInfo->hostAddr[i].sin_port = tservice;
1150 acellInfo->linkedCell = NULL; /* no linked cell */
1151 acellInfo->flags = 0;
1154 #endif /* windows */
1155 #endif /* AFS_AFSDB_ENV */
1158 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1159 struct afsconf_cell *acellInfo)
1161 register struct afsconf_entry *tce;
1162 struct afsconf_aliasentry *tcae;
1163 struct afsconf_entry *bestce;
1164 register afs_int32 i;
1173 afsconf_Check(adir);
1176 cnLen = (int)(strlen(tcell) + 1);
1177 lcstring(tcell, tcell, cnLen);
1178 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1179 /* call to afsconf_GetLocalCell: like klog */
1181 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1183 UNLOCK_GLOBAL_MUTEX;
1188 cnLen = strlen(tcell);
1189 bestce = (struct afsconf_entry *)0;
1192 UNLOCK_GLOBAL_MUTEX;
1196 /* Look through the list of aliases */
1197 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1198 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1199 tcell = tcae->aliasInfo.realName;
1204 for (tce = adir->entries; tce; tce = tce->next) {
1205 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1206 /* found our cell */
1211 if (strlen(tce->cellInfo.name) < cnLen)
1212 continue; /* clearly wrong */
1213 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1215 ambig = 1; /* ambiguous unless we get exact match */
1219 if (!ambig && bestce && bestce->cellInfo.numServers) {
1220 *acellInfo = bestce->cellInfo; /* structure assignment */
1222 tservice = afsconf_FindService(aservice);
1224 UNLOCK_GLOBAL_MUTEX;
1225 return AFSCONF_NOTFOUND; /* service not found */
1227 for (i = 0; i < acellInfo->numServers; i++) {
1228 acellInfo->hostAddr[i].sin_port = tservice;
1231 acellInfo->timeout = 0;
1234 * Until we figure out how to separate out ubik server
1235 * queries from other server queries, only perform gethostbyname()
1236 * lookup on the specified hostnames for the client CellServDB files.
1238 if (IsClientConfigDirectory(adir->name) &&
1239 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1241 short numServers=0; /*Num active servers for the cell */
1242 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1243 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1245 memset(&hostAddr, 0, sizeof(hostAddr));
1246 memset(&hostName, 0, sizeof(hostName));
1248 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1249 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1252 if (he && he->h_addrtype == AF_INET) {
1254 /* obtain all the valid address from the list */
1255 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1256 /* check to see if this is a new address; if so insert it into the list */
1258 for (k=0, dup=0; !dup && k < numServers; k++) {
1259 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1265 hostAddr[numServers].sin_family = AF_INET;
1266 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1267 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1268 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1270 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1271 strcpy(hostName[numServers], acellInfo->hostName[j]);
1277 hostAddr[numServers] = acellInfo->hostAddr[j];
1278 strcpy(hostName[numServers], acellInfo->hostName[j]);
1283 for (i=0; i<numServers; i++) {
1284 acellInfo->hostAddr[i] = hostAddr[i];
1285 strcpy(acellInfo->hostName[i], hostName[i]);
1287 acellInfo->numServers = numServers;
1288 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1290 UNLOCK_GLOBAL_MUTEX;
1293 UNLOCK_GLOBAL_MUTEX;
1294 #ifdef AFS_AFSDB_ENV
1295 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1297 return AFSCONF_NOTFOUND;
1298 #endif /* AFS_AFSDB_ENV */
1303 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1306 static int afsconf_showcell = 0;
1312 * If a cell switch was specified in a command, then it should override the
1313 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1314 * is set and the cell name in the adir structure is used.
1315 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1317 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1318 if (!afsconf_showcell) {
1319 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1321 afsconf_showcell = 1;
1323 strncpy(aname, afscell_path, alen);
1325 afsconf_Check(adir);
1326 if (adir->cellName) {
1327 strncpy(aname, adir->cellName, alen);
1329 code = AFSCONF_UNKNOWN;
1332 UNLOCK_GLOBAL_MUTEX;
1337 afsconf_Close(struct afsconf_dir *adir)
1340 afsconf_CloseInternal(adir);
1344 UNLOCK_GLOBAL_MUTEX;
1349 afsconf_CloseInternal(register struct afsconf_dir *adir)
1351 register struct afsconf_entry *td, *nd;
1352 struct afsconf_aliasentry *ta, *na;
1353 register char *tname;
1355 tname = adir->name; /* remember name, since that's all we preserve */
1357 /* free everything we can find */
1359 free(adir->cellName);
1360 for (td = adir->entries; td; td = nd) {
1362 if (td->cellInfo.linkedCell)
1363 free(td->cellInfo.linkedCell);
1366 for (ta = adir->alias_entries; ta; ta = na) {
1374 memset(adir, 0, sizeof(struct afsconf_dir));
1375 adir->name = tname; /* restore it */
1380 afsconf_Reopen(register struct afsconf_dir *adir)
1382 register afs_int32 code;
1383 code = afsconf_CloseInternal(adir);
1386 code = afsconf_OpenInternal(adir, 0, 0);
1390 /* called during opening of config file */
1392 afsconf_IntGetKeys(struct afsconf_dir *adir)
1396 struct afsconf_keys *tstr;
1397 register afs_int32 code;
1400 /* NT client config dir has no KeyFile; don't risk attempting open
1401 * because there might be a random file of this name if dir is shared.
1403 if (IsClientConfigDirectory(adir->name)) {
1404 adir->keystr = ((struct afsconf_keys *)
1405 malloc(sizeof(struct afsconf_keys)));
1406 adir->keystr->nkeys = 0;
1409 #endif /* AFS_NT40_ENV */
1412 /* compute the key name and other setup */
1413 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1414 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1415 adir->keystr = tstr;
1418 fd = open(tbuffer, O_RDONLY);
1421 UNLOCK_GLOBAL_MUTEX;
1424 code = read(fd, tstr, sizeof(struct afsconf_keys));
1426 if (code < sizeof(afs_int32)) {
1428 UNLOCK_GLOBAL_MUTEX;
1432 /* convert key structure to host order */
1433 tstr->nkeys = ntohl(tstr->nkeys);
1435 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1437 UNLOCK_GLOBAL_MUTEX;
1441 for (fd = 0; fd < tstr->nkeys; fd++)
1442 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1444 UNLOCK_GLOBAL_MUTEX;
1448 /* get keys structure */
1450 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1452 register afs_int32 code;
1455 code = afsconf_Check(adir);
1457 UNLOCK_GLOBAL_MUTEX;
1458 return AFSCONF_FAILURE;
1460 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1461 UNLOCK_GLOBAL_MUTEX;
1465 /* get latest key */
1467 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1468 struct ktc_encryptionKey *akey)
1472 register struct afsconf_key *tk;
1473 register afs_int32 best;
1474 struct afsconf_key *bestk;
1475 register afs_int32 code;
1478 code = afsconf_Check(adir);
1480 UNLOCK_GLOBAL_MUTEX;
1481 return AFSCONF_FAILURE;
1483 maxa = adir->keystr->nkeys;
1485 best = -1; /* highest kvno we've seen yet */
1486 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1487 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1488 if (tk->kvno == 999)
1489 continue; /* skip bcrypt keys */
1490 if (tk->kvno > best) {
1495 if (bestk) { /* found any */
1497 memcpy(akey, bestk->key, 8); /* copy out latest key */
1499 *avno = bestk->kvno; /* and kvno to caller */
1500 UNLOCK_GLOBAL_MUTEX;
1503 UNLOCK_GLOBAL_MUTEX;
1504 return AFSCONF_NOTFOUND; /* didn't find any keys */
1507 /* get a particular key */
1509 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1511 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1512 register int i, maxa;
1513 register struct afsconf_key *tk;
1514 register afs_int32 code;
1517 code = afsconf_Check(adir);
1519 UNLOCK_GLOBAL_MUTEX;
1520 return AFSCONF_FAILURE;
1522 maxa = adir->keystr->nkeys;
1524 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1525 if (tk->kvno == avno) {
1526 memcpy(akey, tk->key, 8);
1527 UNLOCK_GLOBAL_MUTEX;
1532 UNLOCK_GLOBAL_MUTEX;
1533 return AFSCONF_NOTFOUND;
1536 /* save the key structure in the appropriate file */
1538 SaveKeys(struct afsconf_dir *adir)
1540 struct afsconf_keys tkeys;
1542 register afs_int32 i;
1545 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1547 /* convert it to net byte order */
1548 for (i = 0; i < tkeys.nkeys; i++)
1549 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1550 tkeys.nkeys = htonl(tkeys.nkeys);
1552 /* rewrite keys file */
1553 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1554 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1556 return AFSCONF_FAILURE;
1557 i = write(fd, &tkeys, sizeof(tkeys));
1558 if (i != sizeof(tkeys)) {
1560 return AFSCONF_FAILURE;
1563 return AFSCONF_FAILURE;
1568 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1569 afs_int32 overwrite)
1571 register struct afsconf_keys *tk;
1572 register struct afsconf_key *tkey;
1573 register afs_int32 i;
1580 if (akvno < 0 || akvno > 255) {
1581 UNLOCK_GLOBAL_MUTEX;
1586 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1587 if (tkey->kvno == akvno) {
1589 UNLOCK_GLOBAL_MUTEX;
1590 return AFSCONF_KEYINUSE;
1597 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1598 UNLOCK_GLOBAL_MUTEX;
1599 return AFSCONF_FULL;
1601 tkey = &tk->key[tk->nkeys++];
1604 memcpy(tkey->key, akey, 8);
1606 afsconf_Touch(adir);
1607 UNLOCK_GLOBAL_MUTEX;
1611 /* this proc works by sliding the other guys down, rather than using a funny
1612 kvno value, so that callers can count on getting a good key in key[0].
1615 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1617 register struct afsconf_keys *tk;
1618 register struct afsconf_key *tkey;
1625 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1626 if (tkey->kvno == akvno) {
1632 UNLOCK_GLOBAL_MUTEX;
1633 return AFSCONF_NOTFOUND;
1636 /* otherwise slide the others down. i and tkey point at the guy to delete */
1637 for (; i < tk->nkeys - 1; i++, tkey++) {
1638 tkey->kvno = (tkey + 1)->kvno;
1639 memcpy(tkey->key, (tkey + 1)->key, 8);
1643 afsconf_Touch(adir);
1644 UNLOCK_GLOBAL_MUTEX;