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 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, int abufsize);
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, int abufsize)
269 strlcpy(tbuffer, tp, sizeof tbuffer);
270 strlcpy(abuffer, tbuffer, abufsize);
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 = strdup(adir);
429 code = afsconf_OpenInternal(tdir, 0, 0);
431 char *afsconf_path, afs_confdir[128];
434 /* Check global place only when local Open failed for whatever reason */
435 if (!(afsconf_path = getenv("AFSCONF"))) {
436 /* 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... */
441 if (!(home_dir = getenv("HOME"))) {
442 /* Our last chance is the "/.AFSCONF" file */
443 fp = fopen("/.AFSCONF", "r");
447 return (struct afsconf_dir *)0;
449 fgets(afs_confdir, 128, fp);
454 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
455 fp = fopen(pathname, "r");
457 /* Our last chance is the "/.AFSCONF" file */
458 fp = fopen("/.AFSCONF", "r");
462 return (struct afsconf_dir *)0;
465 fgets(afs_confdir, 128, fp);
468 len = strlen(afs_confdir);
472 return (struct afsconf_dir *)0;
474 if (afs_confdir[len - 1] == '\n') {
475 afs_confdir[len - 1] = 0;
477 afsconf_path = afs_confdir;
479 tdir->name = strdup(afsconf_path);
480 code = afsconf_OpenInternal(tdir, 0, 0);
485 return (struct afsconf_dir *)0;
493 GetCellUnix(struct afsconf_dir *adir)
500 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
501 fp = fopen(tbuffer, "r");
505 rc = fgets(tbuffer, 256, fp);
511 while (*start != '\0' && isspace(*start))
514 while (*p != '\0' && !isspace(*p))
520 adir->cellName = strdup(start);
527 GetCellNT(struct afsconf_dir *adir)
529 if (IsClientConfigDirectory(adir->name)) {
530 /* NT client config dir; ThisCell is in registry (no file). */
531 return afssw_GetClientCellName(&adir->cellName);
533 /* NT server config dir; works just like Unix */
534 return GetCellUnix(adir);
538 /* The following procedures and structs are used on Windows only
539 * to enumerate the Cell information distributed within the
540 * Windows registry. (See src/WINNT/afsd/cm_config.c)
542 typedef struct _cm_enumCellRegistry {
543 afs_uint32 client; /* non-zero if client query */
544 struct afsconf_dir *adir;
545 } cm_enumCellRegistry_t;
548 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
549 char *hostNamep, unsigned short rank)
551 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
553 if (cellInfop->numServers == MAXHOSTSPERCELL)
556 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
557 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
558 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
559 cellInfop->numServers++;
565 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
568 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
569 char linkedName[256] = "";
571 struct afsconf_entry *newEntry;
574 newEntry = malloc(sizeof(struct afsconf_entry));
575 if (newEntry == NULL)
577 newEntry->cellInfo.numServers = 0;
579 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
580 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
581 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
584 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
585 newEntry->cellInfo.name[MAXCELLCHARS-1];
587 newEntry->cellInfo.linkedCell = strdup(linkedName);
589 newEntry->cellInfo.linkedCell = NULL;
590 newEntry->cellInfo.timeout = timeout;
591 newEntry->cellInfo.flags = 0;
593 newEntry->next = enump->adir->entries;
594 enump->adir->entries = newEntry;
600 #endif /* AFS_NT40_ENV */
604 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
608 register char *tp, *bp;
609 register struct afsconf_entry *curEntry;
610 struct afsconf_aliasentry *curAlias;
611 register afs_int32 code;
613 char tbuffer[256], tbuf1[256];
616 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
617 #endif /* AFS_NT40_ENV */
619 /* figure out the local cell name */
622 enumCellRegistry.adir = adir;
624 i = GetCellUnix(adir);
627 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
633 /* now parse the individual lines */
637 /* NT client/server have a CellServDB that is the same format as Unix.
638 * However, the NT client uses a different file name
640 if (IsClientConfigDirectory(adir->name)) {
641 /* NT client config dir */
644 enumCellRegistry.client = 1;
646 if (!afssw_GetClientCellServDBDir(&p)) {
647 strcompose(tbuffer, sizeof(tbuffer), p, "/",
648 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
652 strncpy(tbuffer, adir->name, sizeof(tbuffer));
653 len = (int)strlen(tbuffer);
654 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
655 strncat(tbuffer, "\\", sizeof(tbuffer));
657 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
659 tbuffer[sizeof(tbuffer) - 1] = '\0';
662 /* NT server config dir */
663 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
667 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
668 #endif /* AFS_NT40_ENV */
670 if (!stat(tbuffer, &tstat)) {
671 adir->timeRead = tstat.st_mtime;
676 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
677 tf = fopen(tbuffer, "r");
682 /* The CellServDB file is now open.
683 * The following code parses the contents of the
684 * file and creates a list with the first cell entry
685 * in the CellServDB file at the end of the list.
687 * No checking is performed for duplicates.
688 * The side effects of this process are that duplicate
689 * entries appended to the end of the CellServDB file
690 * take precedence and are found in a shorter period
695 tp = fgets(tbuffer, sizeof(tbuffer), tf);
698 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
699 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
700 continue; /* empty line */
701 if (tbuffer[0] == '>') {
702 char linkedcell[MAXCELLCHARS];
703 /* start new cell item */
705 /* thread this guy on the list */
706 curEntry->next = adir->entries;
707 adir->entries = curEntry;
711 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
712 memset(curEntry, 0, sizeof(struct afsconf_entry));
714 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
716 afsconf_CloseInternal(adir);
721 if (linkedcell[0] != '\0')
722 curEntry->cellInfo.linkedCell = strdup(linkedcell);
724 /* new host in the current cell */
726 afsconf_CloseInternal(adir);
730 i = curEntry->cellInfo.numServers;
731 if (i < MAXHOSTSPERCELL) {
732 if (cell && !strcmp(cell, curEntry->cellInfo.name))
734 ParseHostLine(tbuffer,
735 &curEntry->cellInfo.hostAddr[i],
736 curEntry->cellInfo.hostName[i],
740 ParseHostLine(tbuffer,
741 &curEntry->cellInfo.hostAddr[i],
742 curEntry->cellInfo.hostName[i], 0);
745 if (code == AFSCONF_SYNTAX) {
746 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
752 "Can't properly parse host line \"%s\" in configuration file %s\n",
757 afsconf_CloseInternal(adir);
760 curEntry->cellInfo.numServers = ++i;
763 "Too many hosts for cell %s in configuration file %s\n",
764 curEntry->cellInfo.name, tbuf1);
768 fclose(tf); /* close the file now */
770 /* end the last partially-completed cell */
772 curEntry->next = adir->entries;
773 adir->entries = curEntry;
778 * Windows maintains a CellServDB list in the Registry
779 * that supercedes the contents of the CellServDB file.
780 * Prepending these entries to the head of the list
781 * is sufficient to enforce the precedence.
783 cm_EnumerateCellRegistry( enumCellRegistry.client,
784 cm_enumCellRegistryProc,
786 #endif /* AFS_NT40_ENV */
788 /* Read in the alias list */
789 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
791 tf = fopen(tbuffer, "r");
795 tp = fgets(tbuffer, sizeof(tbuffer), tf);
798 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
800 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
801 continue; /* empty line */
804 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
807 continue; /* invalid line */
809 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
812 continue; /* invalid line */
815 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
820 curAlias = malloc(sizeof(*curAlias));
821 memset(curAlias, 0, sizeof(*curAlias));
823 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
824 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
826 curAlias->next = adir->alias_entries;
827 adir->alias_entries = curAlias;
832 /* now read the fs keys, if possible */
833 adir->keystr = (struct afsconf_keys *)0;
834 afsconf_IntGetKeys(adir);
839 /* parse a line of the form
840 *"128.2.1.3 #hostname" or
841 *"[128.2.1.3] #hostname" for clones
842 * into the appropriate pieces.
845 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
849 register afs_int32 code;
855 /* FIXME: length of aname unknown here */
856 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
860 /* FIXME: length of aname unknown here */
861 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
864 return AFSCONF_SYNTAX;
865 addr->sin_family = AF_INET;
867 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
868 addr->sin_len = sizeof(struct sockaddr_in);
870 tp = (char *)&addr->sin_addr;
878 /* parse a line of the form
879 * ">cellname [linkedcellname] [#comments]"
880 * into the appropriate pieces.
883 ParseCellLine(register char *aline, register char *aname,
884 register char *alname)
887 /* FIXME: length of aname, alname unknown here */
888 code = sscanf(aline, ">%s %s", aname, alname);
892 if (*alname == '#') {
896 return (code > 0 ? 0 : AFSCONF_SYNTAX);
899 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
901 afsconf_CellApply(struct afsconf_dir *adir,
902 int (*aproc) (struct afsconf_cell * cell, void *arock,
903 struct afsconf_dir * dir), void *arock)
905 register struct afsconf_entry *tde;
906 register afs_int32 code;
908 for (tde = adir->entries; tde; tde = tde->next) {
909 code = (*aproc) (&tde->cellInfo, arock, adir);
919 /* call aproc(entry, arock, adir) for all cell aliases.
920 * Proc must return 0, or we'll stop early and return the code it returns
923 afsconf_CellAliasApply(struct afsconf_dir *adir,
924 int (*aproc) (struct afsconf_cellalias * alias,
925 void *arock, struct afsconf_dir * dir),
928 register struct afsconf_aliasentry *tde;
929 register afs_int32 code;
931 for (tde = adir->alias_entries; tde; tde = tde->next) {
932 code = (*aproc) (&tde->aliasInfo, arock, adir);
942 afs_int32 afsconf_SawCell = 0;
945 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
946 char *aservice, struct afsconf_cell *acellInfo,
952 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
959 cell = (char *)&acellInfo->name;
961 code = afsconf_OpenInternal(adir, cell, clones);
966 #if !defined(AFS_NT40_ENV)
968 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
969 struct afsconf_cell *acellInfo)
972 int tservice, i, len;
973 unsigned char answer[1024];
977 char realCellName[256];
983 /* The resolver isn't always MT-safe.. Perhaps this ought to be
984 * replaced with a more fine-grained lock just for the resolver
989 if ( ! strchr(acellName,'.') ) {
990 cellnamelength=strlen(acellName);
991 dotcellname=malloc(cellnamelength+2);
992 memcpy(dotcellname,acellName,cellnamelength);
993 dotcellname[cellnamelength]='.';
994 dotcellname[cellnamelength+1]=0;
996 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
998 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1000 UNLOCK_GLOBAL_MUTEX;
1004 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1005 UNLOCK_GLOBAL_MUTEX;
1013 return AFSCONF_NOTFOUND;
1016 p = answer + sizeof(HEADER); /* Skip header */
1017 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1019 return AFSCONF_NOTFOUND;
1021 p += code + QFIXEDSZ; /* Skip name */
1023 while (p < answer + len) {
1024 int type, ttl, size;
1026 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1028 return AFSCONF_NOTFOUND;
1030 p += code; /* Skip the name */
1031 type = (p[0] << 8) | p[1];
1032 p += 4; /* Skip type and class */
1033 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1034 p += 4; /* Skip the TTL */
1035 size = (p[0] << 8) | p[1];
1036 p += 2; /* Skip the size */
1038 if (type == T_AFSDB) {
1042 afsdb_type = (p[0] << 8) | p[1];
1043 if (afsdb_type == 1) {
1045 * We know this is an AFSDB record for our cell, of the
1046 * right AFSDB type. Write down the true cell name that
1047 * the resolver gave us above.
1049 strlcpy(realCellName, host, sizeof realCellName);
1052 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1054 return AFSCONF_NOTFOUND;
1056 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1057 /* Do we want to get TTL data for the A record as well? */
1058 (he = gethostbyname(host))) {
1060 memcpy(&ipaddr, he->h_addr, he->h_length);
1061 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1062 strncpy(acellInfo->hostName[server_num], host,
1063 sizeof(acellInfo->hostName[server_num]));
1066 if (!minttl || ttl < minttl)
1074 if (server_num == 0) /* No AFSDB records */
1075 return AFSCONF_NOTFOUND;
1077 /* Convert the real cell name to lowercase */
1078 for (p = (unsigned char *)realCellName; *p; p++)
1081 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1082 acellInfo->numServers = server_num;
1085 tservice = afsconf_FindService(aservice);
1087 return AFSCONF_NOTFOUND; /* service not found */
1088 for (i = 0; i < acellInfo->numServers; i++) {
1089 acellInfo->hostAddr[i].sin_port = tservice;
1093 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1099 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1100 struct afsconf_cell *acellInfo)
1102 register afs_int32 i;
1104 struct afsconf_entry DNSce;
1105 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1106 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1107 unsigned short ipRanks[AFSMAXCELLHOSTS];
1112 DNSce.cellInfo.numServers = 0;
1114 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1116 /* ignore the ttl here since this code is only called by transitory programs
1117 * like klog, etc. */
1120 if (numServers == 0)
1123 for (i = 0; i < numServers; i++) {
1124 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1126 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1127 acellInfo->hostAddr[i].sin_family = AF_INET;
1129 /* sin_port supplied by connection code */
1132 acellInfo->numServers = numServers;
1133 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1136 tservice = afsconf_FindService(aservice);
1137 UNLOCK_GLOBAL_MUTEX;
1139 return AFSCONF_NOTFOUND; /* service not found */
1141 for (i = 0; i < acellInfo->numServers; i++) {
1142 acellInfo->hostAddr[i].sin_port = tservice;
1145 acellInfo->linkedCell = NULL; /* no linked cell */
1146 acellInfo->flags = 0;
1149 #endif /* windows */
1150 #endif /* AFS_AFSDB_ENV */
1153 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1154 struct afsconf_cell *acellInfo)
1156 register struct afsconf_entry *tce;
1157 struct afsconf_aliasentry *tcae;
1158 struct afsconf_entry *bestce;
1159 register afs_int32 i;
1168 afsconf_Check(adir);
1171 cnLen = (int)(strlen(tcell) + 1);
1172 lcstring(tcell, tcell, cnLen);
1173 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1174 /* call to afsconf_GetLocalCell: like klog */
1176 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1178 UNLOCK_GLOBAL_MUTEX;
1183 cnLen = strlen(tcell);
1184 bestce = (struct afsconf_entry *)0;
1187 UNLOCK_GLOBAL_MUTEX;
1191 /* Look through the list of aliases */
1192 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1193 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1194 tcell = tcae->aliasInfo.realName;
1199 for (tce = adir->entries; tce; tce = tce->next) {
1200 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1201 /* found our cell */
1206 if (strlen(tce->cellInfo.name) < cnLen)
1207 continue; /* clearly wrong */
1208 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1210 ambig = 1; /* ambiguous unless we get exact match */
1214 if (!ambig && bestce && bestce->cellInfo.numServers) {
1215 *acellInfo = bestce->cellInfo; /* structure assignment */
1217 tservice = afsconf_FindService(aservice);
1219 UNLOCK_GLOBAL_MUTEX;
1220 return AFSCONF_NOTFOUND; /* service not found */
1222 for (i = 0; i < acellInfo->numServers; i++) {
1223 acellInfo->hostAddr[i].sin_port = tservice;
1226 acellInfo->timeout = 0;
1229 * Until we figure out how to separate out ubik server
1230 * queries from other server queries, only perform gethostbyname()
1231 * lookup on the specified hostnames for the client CellServDB files.
1233 if (IsClientConfigDirectory(adir->name) &&
1234 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1236 short numServers=0; /*Num active servers for the cell */
1237 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1238 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1240 memset(&hostAddr, 0, sizeof(hostAddr));
1241 memset(&hostName, 0, sizeof(hostName));
1243 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1244 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1247 if (he && he->h_addrtype == AF_INET) {
1249 /* obtain all the valid address from the list */
1250 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1251 /* check to see if this is a new address; if so insert it into the list */
1253 for (k=0, dup=0; !dup && k < numServers; k++) {
1254 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1260 hostAddr[numServers].sin_family = AF_INET;
1261 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1262 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1263 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1265 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1266 strcpy(hostName[numServers], acellInfo->hostName[j]);
1272 hostAddr[numServers] = acellInfo->hostAddr[j];
1273 strcpy(hostName[numServers], acellInfo->hostName[j]);
1278 for (i=0; i<numServers; i++) {
1279 acellInfo->hostAddr[i] = hostAddr[i];
1280 strcpy(acellInfo->hostName[i], hostName[i]);
1282 acellInfo->numServers = numServers;
1283 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1285 UNLOCK_GLOBAL_MUTEX;
1288 UNLOCK_GLOBAL_MUTEX;
1289 #ifdef AFS_AFSDB_ENV
1290 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1292 return AFSCONF_NOTFOUND;
1293 #endif /* AFS_AFSDB_ENV */
1298 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1301 static int afsconf_showcell = 0;
1307 * If a cell switch was specified in a command, then it should override the
1308 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1309 * is set and the cell name in the adir structure is used.
1310 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1312 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1313 if (!afsconf_showcell) {
1314 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1316 afsconf_showcell = 1;
1318 strncpy(aname, afscell_path, alen);
1320 afsconf_Check(adir);
1321 if (adir->cellName) {
1322 strncpy(aname, adir->cellName, alen);
1324 code = AFSCONF_UNKNOWN;
1327 UNLOCK_GLOBAL_MUTEX;
1332 afsconf_Close(struct afsconf_dir *adir)
1335 afsconf_CloseInternal(adir);
1339 UNLOCK_GLOBAL_MUTEX;
1344 afsconf_CloseInternal(register struct afsconf_dir *adir)
1346 register struct afsconf_entry *td, *nd;
1347 struct afsconf_aliasentry *ta, *na;
1348 register char *tname;
1350 tname = adir->name; /* remember name, since that's all we preserve */
1352 /* free everything we can find */
1354 free(adir->cellName);
1355 for (td = adir->entries; td; td = nd) {
1357 if (td->cellInfo.linkedCell)
1358 free(td->cellInfo.linkedCell);
1361 for (ta = adir->alias_entries; ta; ta = na) {
1369 memset(adir, 0, sizeof(struct afsconf_dir));
1370 adir->name = tname; /* restore it */
1375 afsconf_Reopen(register struct afsconf_dir *adir)
1377 register afs_int32 code;
1378 code = afsconf_CloseInternal(adir);
1381 code = afsconf_OpenInternal(adir, 0, 0);
1385 /* called during opening of config file */
1387 afsconf_IntGetKeys(struct afsconf_dir *adir)
1391 struct afsconf_keys *tstr;
1392 register afs_int32 code;
1395 /* NT client config dir has no KeyFile; don't risk attempting open
1396 * because there might be a random file of this name if dir is shared.
1398 if (IsClientConfigDirectory(adir->name)) {
1399 adir->keystr = ((struct afsconf_keys *)
1400 malloc(sizeof(struct afsconf_keys)));
1401 adir->keystr->nkeys = 0;
1404 #endif /* AFS_NT40_ENV */
1407 /* compute the key name and other setup */
1408 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1409 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1410 adir->keystr = tstr;
1413 fd = open(tbuffer, O_RDONLY);
1416 UNLOCK_GLOBAL_MUTEX;
1419 code = read(fd, tstr, sizeof(struct afsconf_keys));
1421 if (code < sizeof(afs_int32)) {
1423 UNLOCK_GLOBAL_MUTEX;
1427 /* convert key structure to host order */
1428 tstr->nkeys = ntohl(tstr->nkeys);
1430 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1432 UNLOCK_GLOBAL_MUTEX;
1436 for (fd = 0; fd < tstr->nkeys; fd++)
1437 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1439 UNLOCK_GLOBAL_MUTEX;
1443 /* get keys structure */
1445 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1447 register afs_int32 code;
1450 code = afsconf_Check(adir);
1452 UNLOCK_GLOBAL_MUTEX;
1453 return AFSCONF_FAILURE;
1455 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1456 UNLOCK_GLOBAL_MUTEX;
1460 /* get latest key */
1462 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1463 struct ktc_encryptionKey *akey)
1467 register struct afsconf_key *tk;
1468 register afs_int32 best;
1469 struct afsconf_key *bestk;
1470 register afs_int32 code;
1473 code = afsconf_Check(adir);
1475 UNLOCK_GLOBAL_MUTEX;
1476 return AFSCONF_FAILURE;
1478 maxa = adir->keystr->nkeys;
1480 best = -1; /* highest kvno we've seen yet */
1481 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1482 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1483 if (tk->kvno == 999)
1484 continue; /* skip bcrypt keys */
1485 if (tk->kvno > best) {
1490 if (bestk) { /* found any */
1492 memcpy(akey, bestk->key, 8); /* copy out latest key */
1494 *avno = bestk->kvno; /* and kvno to caller */
1495 UNLOCK_GLOBAL_MUTEX;
1498 UNLOCK_GLOBAL_MUTEX;
1499 return AFSCONF_NOTFOUND; /* didn't find any keys */
1502 /* get a particular key */
1504 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1506 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1507 register int i, maxa;
1508 register struct afsconf_key *tk;
1509 register afs_int32 code;
1512 code = afsconf_Check(adir);
1514 UNLOCK_GLOBAL_MUTEX;
1515 return AFSCONF_FAILURE;
1517 maxa = adir->keystr->nkeys;
1519 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1520 if (tk->kvno == avno) {
1521 memcpy(akey, tk->key, 8);
1522 UNLOCK_GLOBAL_MUTEX;
1527 UNLOCK_GLOBAL_MUTEX;
1528 return AFSCONF_NOTFOUND;
1531 /* save the key structure in the appropriate file */
1533 SaveKeys(struct afsconf_dir *adir)
1535 struct afsconf_keys tkeys;
1537 register afs_int32 i;
1540 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1542 /* convert it to net byte order */
1543 for (i = 0; i < tkeys.nkeys; i++)
1544 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1545 tkeys.nkeys = htonl(tkeys.nkeys);
1547 /* rewrite keys file */
1548 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1549 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1551 return AFSCONF_FAILURE;
1552 i = write(fd, &tkeys, sizeof(tkeys));
1553 if (i != sizeof(tkeys)) {
1555 return AFSCONF_FAILURE;
1558 return AFSCONF_FAILURE;
1563 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1564 afs_int32 overwrite)
1566 register struct afsconf_keys *tk;
1567 register struct afsconf_key *tkey;
1568 register afs_int32 i;
1575 if (akvno < 0 || akvno > 255) {
1576 UNLOCK_GLOBAL_MUTEX;
1581 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1582 if (tkey->kvno == akvno) {
1584 UNLOCK_GLOBAL_MUTEX;
1585 return AFSCONF_KEYINUSE;
1592 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1593 UNLOCK_GLOBAL_MUTEX;
1594 return AFSCONF_FULL;
1596 tkey = &tk->key[tk->nkeys++];
1599 memcpy(tkey->key, akey, 8);
1601 afsconf_Touch(adir);
1602 UNLOCK_GLOBAL_MUTEX;
1606 /* this proc works by sliding the other guys down, rather than using a funny
1607 kvno value, so that callers can count on getting a good key in key[0].
1610 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1612 register struct afsconf_keys *tk;
1613 register struct afsconf_key *tkey;
1620 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1621 if (tkey->kvno == akvno) {
1627 UNLOCK_GLOBAL_MUTEX;
1628 return AFSCONF_NOTFOUND;
1631 /* otherwise slide the others down. i and tkey point at the guy to delete */
1632 for (; i < tk->nkeys - 1; i++, tkey++) {
1633 tkey->kvno = (tkey + 1)->kvno;
1634 memcpy(tkey->key, (tkey + 1)->key, 8);
1638 afsconf_Touch(adir);
1639 UNLOCK_GLOBAL_MUTEX;