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, int abufsize);
95 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);
276 * IsClientConfigDirectory() -- determine if path matches well-known
277 * client configuration directory.
280 IsClientConfigDirectory(const char *path)
282 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
285 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
286 int cc = tolower(cdir[i]);
287 int pc = tolower(path[i]);
300 /* hit end of one or both; allow mismatch in existence of trailing slash */
301 if (cdir[i] != '\0') {
302 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
306 if (path[i] != '\0') {
307 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
313 #endif /* AFS_NT40_ENV */
317 afsconf_Check(register struct afsconf_dir *adir)
324 register afs_int32 code;
327 /* NT client CellServDB has different file name than NT server or Unix */
328 if (IsClientConfigDirectory(adir->name)) {
329 if (!afssw_GetClientCellServDBDir(&p)) {
330 strcompose(tbuffer, sizeof(tbuffer), p, "/",
331 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
335 strncpy(tbuffer, adir->name, sizeof(tbuffer));
336 len = (int)strlen(tbuffer);
337 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
338 strncat(tbuffer, "\\", sizeof(tbuffer));
340 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
342 tbuffer[sizeof(tbuffer) - 1] = '\0';
345 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
349 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
350 #endif /* AFS_NT40_ENV */
352 code = stat(tbuffer, &tstat);
356 /* did file change? */
357 if (tstat.st_mtime == adir->timeRead) {
360 /* otherwise file has changed, so reopen it */
361 return afsconf_Reopen(adir);
364 /* set modtime on file */
366 afsconf_Touch(register struct afsconf_dir *adir)
370 struct timeval tvp[2];
375 adir->timeRead = 0; /* just in case */
378 /* NT client CellServDB has different file name than NT server or Unix */
380 if (IsClientConfigDirectory(adir->name)) {
381 if (!afssw_GetClientCellServDBDir(&p)) {
382 strcompose(tbuffer, sizeof(tbuffer), p, "/",
383 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
386 int len = (int)strlen(tbuffer);
387 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
388 strncat(tbuffer, "\\", sizeof(tbuffer));
390 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
392 tbuffer[sizeof(tbuffer) - 1] = '\0';
395 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
399 return _utime(tbuffer, NULL);
402 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
403 gettimeofday(&tvp[0], NULL);
405 return utimes(tbuffer, tvp);
406 #endif /* AFS_NT40_ENV */
410 afsconf_Open(register const char *adir)
412 register struct afsconf_dir *tdir;
413 register afs_int32 code;
416 /* zero structure and fill in name; rest is done by internal routine */
417 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
418 memset(tdir, 0, sizeof(struct afsconf_dir));
419 tdir->name = strdup(adir);
421 code = afsconf_OpenInternal(tdir, 0, 0);
423 char *afsconf_path, afs_confdir[128];
426 /* Check global place only when local Open failed for whatever reason */
427 if (!(afsconf_path = getenv("AFSCONF"))) {
428 /* 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... */
433 if (!(home_dir = getenv("HOME"))) {
434 /* Our last chance is the "/.AFSCONF" file */
435 fp = fopen("/.AFSCONF", "r");
439 return (struct afsconf_dir *)0;
441 fgets(afs_confdir, 128, fp);
446 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
447 fp = fopen(pathname, "r");
449 /* Our last chance is the "/.AFSCONF" file */
450 fp = fopen("/.AFSCONF", "r");
454 return (struct afsconf_dir *)0;
457 fgets(afs_confdir, 128, fp);
460 len = strlen(afs_confdir);
464 return (struct afsconf_dir *)0;
466 if (afs_confdir[len - 1] == '\n') {
467 afs_confdir[len - 1] = 0;
469 afsconf_path = afs_confdir;
471 tdir->name = strdup(afsconf_path);
472 code = afsconf_OpenInternal(tdir, 0, 0);
477 return (struct afsconf_dir *)0;
485 GetCellUnix(struct afsconf_dir *adir)
492 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
493 fp = fopen(tbuffer, "r");
497 rc = fgets(tbuffer, 256, fp);
503 while (*start != '\0' && isspace(*start))
506 while (*p != '\0' && !isspace(*p))
512 adir->cellName = strdup(start);
519 GetCellNT(struct afsconf_dir *adir)
521 if (IsClientConfigDirectory(adir->name)) {
522 /* NT client config dir; ThisCell is in registry (no file). */
523 return afssw_GetClientCellName(&adir->cellName);
525 /* NT server config dir; works just like Unix */
526 return GetCellUnix(adir);
530 /* The following procedures and structs are used on Windows only
531 * to enumerate the Cell information distributed within the
532 * Windows registry. (See src/WINNT/afsd/cm_config.c)
534 typedef struct _cm_enumCellRegistry {
535 afs_uint32 client; /* non-zero if client query */
536 struct afsconf_dir *adir;
537 } cm_enumCellRegistry_t;
540 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
541 char *hostNamep, unsigned short rank)
543 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
545 if (cellInfop->numServers == MAXHOSTSPERCELL)
548 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
549 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
550 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
551 cellInfop->numServers++;
557 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
560 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
561 char linkedName[256] = "";
563 struct afsconf_entry *newEntry;
566 newEntry = malloc(sizeof(struct afsconf_entry));
567 if (newEntry == NULL)
569 newEntry->cellInfo.numServers = 0;
571 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
572 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
573 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
576 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
577 newEntry->cellInfo.name[MAXCELLCHARS-1];
579 newEntry->cellInfo.linkedCell = strdup(linkedName);
581 newEntry->cellInfo.linkedCell = NULL;
582 newEntry->cellInfo.timeout = timeout;
583 newEntry->cellInfo.flags = 0;
585 newEntry->next = enump->adir->entries;
586 enump->adir->entries = newEntry;
592 #endif /* AFS_NT40_ENV */
596 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
600 register char *tp, *bp;
601 register struct afsconf_entry *curEntry;
602 struct afsconf_aliasentry *curAlias;
603 register afs_int32 code;
605 char tbuffer[256], tbuf1[256];
608 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
609 #endif /* AFS_NT40_ENV */
611 /* figure out the local cell name */
614 enumCellRegistry.adir = adir;
616 i = GetCellUnix(adir);
619 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
625 /* now parse the individual lines */
629 /* NT client/server have a CellServDB that is the same format as Unix.
630 * However, the NT client uses a different file name
632 if (IsClientConfigDirectory(adir->name)) {
633 /* NT client config dir */
636 enumCellRegistry.client = 1;
638 if (!afssw_GetClientCellServDBDir(&p)) {
639 strcompose(tbuffer, sizeof(tbuffer), p, "/",
640 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
644 strncpy(tbuffer, adir->name, sizeof(tbuffer));
645 len = (int)strlen(tbuffer);
646 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
647 strncat(tbuffer, "\\", sizeof(tbuffer));
649 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
651 tbuffer[sizeof(tbuffer) - 1] = '\0';
654 /* NT server config dir */
655 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
659 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
660 #endif /* AFS_NT40_ENV */
662 if (!stat(tbuffer, &tstat)) {
663 adir->timeRead = tstat.st_mtime;
668 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
669 tf = fopen(tbuffer, "r");
674 /* The CellServDB file is now open.
675 * The following code parses the contents of the
676 * file and creates a list with the first cell entry
677 * in the CellServDB file at the end of the list.
679 * No checking is performed for duplicates.
680 * The side effects of this process are that duplicate
681 * entries appended to the end of the CellServDB file
682 * take precedence and are found in a shorter period
687 tp = fgets(tbuffer, sizeof(tbuffer), tf);
690 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
691 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
692 continue; /* empty line */
693 if (tbuffer[0] == '>') {
694 char linkedcell[MAXCELLCHARS];
695 /* start new cell item */
697 /* thread this guy on the list */
698 curEntry->next = adir->entries;
699 adir->entries = curEntry;
703 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
704 memset(curEntry, 0, sizeof(struct afsconf_entry));
706 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
708 afsconf_CloseInternal(adir);
713 if (linkedcell[0] != '\0')
714 curEntry->cellInfo.linkedCell = strdup(linkedcell);
716 /* new host in the current cell */
718 afsconf_CloseInternal(adir);
722 i = curEntry->cellInfo.numServers;
723 if (cell && !strcmp(cell, curEntry->cellInfo.name))
725 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
726 curEntry->cellInfo.hostName[i], &clones[i]);
729 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
730 curEntry->cellInfo.hostName[i], 0);
732 if (code == AFSCONF_SYNTAX) {
733 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
739 "Can't properly parse host line \"%s\" in configuration file %s\n",
744 afsconf_CloseInternal(adir);
747 curEntry->cellInfo.numServers = ++i;
750 fclose(tf); /* close the file now */
752 /* end the last partially-completed cell */
754 curEntry->next = adir->entries;
755 adir->entries = curEntry;
760 * Windows maintains a CellServDB list in the Registry
761 * that supercedes the contents of the CellServDB file.
762 * Prepending these entries to the head of the list
763 * is sufficient to enforce the precedence.
765 cm_EnumerateCellRegistry( enumCellRegistry.client,
766 cm_enumCellRegistryProc,
768 #endif /* AFS_NT40_ENV */
770 /* Read in the alias list */
771 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
773 tf = fopen(tbuffer, "r");
777 tp = fgets(tbuffer, sizeof(tbuffer), tf);
780 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
782 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
783 continue; /* empty line */
786 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
789 continue; /* invalid line */
791 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
794 continue; /* invalid line */
797 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
802 curAlias = malloc(sizeof(*curAlias));
803 memset(curAlias, 0, sizeof(*curAlias));
805 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
806 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
808 curAlias->next = adir->alias_entries;
809 adir->alias_entries = curAlias;
814 /* now read the fs keys, if possible */
815 adir->keystr = (struct afsconf_keys *)0;
816 afsconf_IntGetKeys(adir);
821 /* parse a line of the form
822 *"128.2.1.3 #hostname" or
823 *"[128.2.1.3] #hostname" for clones
824 * into the appropriate pieces.
827 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
831 register afs_int32 code;
837 /* FIXME: length of aname unknown here */
838 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
842 /* FIXME: length of aname unknown here */
843 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
846 return AFSCONF_SYNTAX;
847 addr->sin_family = AF_INET;
849 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
850 addr->sin_len = sizeof(struct sockaddr_in);
852 tp = (char *)&addr->sin_addr;
860 /* parse a line of the form
861 * ">cellname [linkedcellname] [#comments]"
862 * into the appropriate pieces.
865 ParseCellLine(register char *aline, register char *aname,
866 register char *alname)
869 /* FIXME: length of aname, alname unknown here */
870 code = sscanf(aline, ">%s %s", aname, alname);
874 if (*alname == '#') {
878 return (code > 0 ? 0 : AFSCONF_SYNTAX);
881 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
883 afsconf_CellApply(struct afsconf_dir *adir,
884 int (*aproc) (struct afsconf_cell * cell, void *arock,
885 struct afsconf_dir * dir), void *arock)
887 register struct afsconf_entry *tde;
888 register afs_int32 code;
890 for (tde = adir->entries; tde; tde = tde->next) {
891 code = (*aproc) (&tde->cellInfo, arock, adir);
901 /* call aproc(entry, arock, adir) for all cell aliases.
902 * Proc must return 0, or we'll stop early and return the code it returns
905 afsconf_CellAliasApply(struct afsconf_dir *adir,
906 int (*aproc) (struct afsconf_cellalias * alias,
907 void *arock, struct afsconf_dir * dir),
910 register struct afsconf_aliasentry *tde;
911 register afs_int32 code;
913 for (tde = adir->alias_entries; tde; tde = tde->next) {
914 code = (*aproc) (&tde->aliasInfo, arock, adir);
924 afs_int32 afsconf_SawCell = 0;
927 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
928 char *aservice, struct afsconf_cell *acellInfo,
934 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
941 cell = (char *)&acellInfo->name;
943 code = afsconf_OpenInternal(adir, cell, clones);
948 #if !defined(AFS_NT40_ENV)
950 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
951 struct afsconf_cell *acellInfo)
954 int tservice, i, len;
955 unsigned char answer[1024];
959 char realCellName[256];
965 /* The resolver isn't always MT-safe.. Perhaps this ought to be
966 * replaced with a more fine-grained lock just for the resolver
971 if ( ! strchr(acellName,'.') ) {
972 cellnamelength=strlen(acellName);
973 dotcellname=malloc(cellnamelength+2);
974 memcpy(dotcellname,acellName,cellnamelength);
975 dotcellname[cellnamelength]='.';
976 dotcellname[cellnamelength+1]=0;
978 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
980 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
986 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
995 return AFSCONF_NOTFOUND;
998 p = answer + sizeof(HEADER); /* Skip header */
999 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1001 return AFSCONF_NOTFOUND;
1003 p += code + QFIXEDSZ; /* Skip name */
1005 while (p < answer + len) {
1006 int type, ttl, size;
1008 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1010 return AFSCONF_NOTFOUND;
1012 p += code; /* Skip the name */
1013 type = (p[0] << 8) | p[1];
1014 p += 4; /* Skip type and class */
1015 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1016 p += 4; /* Skip the TTL */
1017 size = (p[0] << 8) | p[1];
1018 p += 2; /* Skip the size */
1020 if (type == T_AFSDB) {
1024 afsdb_type = (p[0] << 8) | p[1];
1025 if (afsdb_type == 1) {
1027 * We know this is an AFSDB record for our cell, of the
1028 * right AFSDB type. Write down the true cell name that
1029 * the resolver gave us above.
1031 strlcpy(realCellName, host, sizeof realCellName);
1034 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1036 return AFSCONF_NOTFOUND;
1038 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1039 /* Do we want to get TTL data for the A record as well? */
1040 (he = gethostbyname(host))) {
1042 memcpy(&ipaddr, he->h_addr, he->h_length);
1043 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1044 strncpy(acellInfo->hostName[server_num], host,
1045 sizeof(acellInfo->hostName[server_num]));
1048 if (!minttl || ttl < minttl)
1056 if (server_num == 0) /* No AFSDB records */
1057 return AFSCONF_NOTFOUND;
1059 /* Convert the real cell name to lowercase */
1060 for (p = (unsigned char *)realCellName; *p; p++)
1063 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1064 acellInfo->numServers = server_num;
1067 tservice = afsconf_FindService(aservice);
1069 return AFSCONF_NOTFOUND; /* service not found */
1070 for (i = 0; i < acellInfo->numServers; i++) {
1071 acellInfo->hostAddr[i].sin_port = tservice;
1075 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1081 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1082 struct afsconf_cell *acellInfo)
1084 register afs_int32 i;
1086 struct afsconf_entry DNSce;
1087 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1088 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1089 unsigned short ipRanks[AFSMAXCELLHOSTS];
1094 DNSce.cellInfo.numServers = 0;
1096 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1098 /* ignore the ttl here since this code is only called by transitory programs
1099 * like klog, etc. */
1102 if (numServers == 0)
1105 for (i = 0; i < numServers; i++) {
1106 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1108 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1109 acellInfo->hostAddr[i].sin_family = AF_INET;
1111 /* sin_port supplied by connection code */
1114 acellInfo->numServers = numServers;
1115 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1118 tservice = afsconf_FindService(aservice);
1119 UNLOCK_GLOBAL_MUTEX;
1121 return AFSCONF_NOTFOUND; /* service not found */
1123 for (i = 0; i < acellInfo->numServers; i++) {
1124 acellInfo->hostAddr[i].sin_port = tservice;
1127 acellInfo->linkedCell = NULL; /* no linked cell */
1128 acellInfo->flags = 0;
1131 #endif /* windows */
1132 #endif /* AFS_AFSDB_ENV */
1135 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1136 struct afsconf_cell *acellInfo)
1138 register struct afsconf_entry *tce;
1139 struct afsconf_aliasentry *tcae;
1140 struct afsconf_entry *bestce;
1141 register afs_int32 i;
1150 afsconf_Check(adir);
1153 cnLen = (int)(strlen(tcell) + 1);
1154 lcstring(tcell, tcell, cnLen);
1155 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1156 /* call to afsconf_GetLocalCell: like klog */
1158 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1160 UNLOCK_GLOBAL_MUTEX;
1165 cnLen = strlen(tcell);
1166 bestce = (struct afsconf_entry *)0;
1169 UNLOCK_GLOBAL_MUTEX;
1173 /* Look through the list of aliases */
1174 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1175 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1176 tcell = tcae->aliasInfo.realName;
1181 for (tce = adir->entries; tce; tce = tce->next) {
1182 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1183 /* found our cell */
1188 if (strlen(tce->cellInfo.name) < cnLen)
1189 continue; /* clearly wrong */
1190 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1192 ambig = 1; /* ambiguous unless we get exact match */
1196 if (!ambig && bestce && bestce->cellInfo.numServers) {
1197 *acellInfo = bestce->cellInfo; /* structure assignment */
1199 tservice = afsconf_FindService(aservice);
1201 UNLOCK_GLOBAL_MUTEX;
1202 return AFSCONF_NOTFOUND; /* service not found */
1204 for (i = 0; i < acellInfo->numServers; i++) {
1205 acellInfo->hostAddr[i].sin_port = tservice;
1208 acellInfo->timeout = 0;
1209 UNLOCK_GLOBAL_MUTEX;
1212 UNLOCK_GLOBAL_MUTEX;
1213 #ifdef AFS_AFSDB_ENV
1214 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1216 return AFSCONF_NOTFOUND;
1217 #endif /* AFS_AFSDB_ENV */
1222 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1225 static int afsconf_showcell = 0;
1231 * If a cell switch was specified in a command, then it should override the
1232 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1233 * is set and the cell name in the adir structure is used.
1234 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1236 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1237 if (!afsconf_showcell) {
1238 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1240 afsconf_showcell = 1;
1242 strncpy(aname, afscell_path, alen);
1244 afsconf_Check(adir);
1245 if (adir->cellName) {
1246 strncpy(aname, adir->cellName, alen);
1248 code = AFSCONF_UNKNOWN;
1251 UNLOCK_GLOBAL_MUTEX;
1256 afsconf_Close(struct afsconf_dir *adir)
1259 afsconf_CloseInternal(adir);
1263 UNLOCK_GLOBAL_MUTEX;
1268 afsconf_CloseInternal(register struct afsconf_dir *adir)
1270 register struct afsconf_entry *td, *nd;
1271 struct afsconf_aliasentry *ta, *na;
1272 register char *tname;
1274 tname = adir->name; /* remember name, since that's all we preserve */
1276 /* free everything we can find */
1278 free(adir->cellName);
1279 for (td = adir->entries; td; td = nd) {
1281 if (td->cellInfo.linkedCell)
1282 free(td->cellInfo.linkedCell);
1285 for (ta = adir->alias_entries; ta; ta = na) {
1293 memset(adir, 0, sizeof(struct afsconf_dir));
1294 adir->name = tname; /* restore it */
1299 afsconf_Reopen(register struct afsconf_dir *adir)
1301 register afs_int32 code;
1302 code = afsconf_CloseInternal(adir);
1305 code = afsconf_OpenInternal(adir, 0, 0);
1309 /* called during opening of config file */
1311 afsconf_IntGetKeys(struct afsconf_dir *adir)
1315 struct afsconf_keys *tstr;
1316 register afs_int32 code;
1319 /* NT client config dir has no KeyFile; don't risk attempting open
1320 * because there might be a random file of this name if dir is shared.
1322 if (IsClientConfigDirectory(adir->name)) {
1323 adir->keystr = ((struct afsconf_keys *)
1324 malloc(sizeof(struct afsconf_keys)));
1325 adir->keystr->nkeys = 0;
1328 #endif /* AFS_NT40_ENV */
1331 /* compute the key name and other setup */
1332 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1333 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1334 adir->keystr = tstr;
1337 fd = open(tbuffer, O_RDONLY);
1340 UNLOCK_GLOBAL_MUTEX;
1343 code = read(fd, tstr, sizeof(struct afsconf_keys));
1345 if (code < sizeof(afs_int32)) {
1347 UNLOCK_GLOBAL_MUTEX;
1351 /* convert key structure to host order */
1352 tstr->nkeys = ntohl(tstr->nkeys);
1354 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1356 UNLOCK_GLOBAL_MUTEX;
1360 for (fd = 0; fd < tstr->nkeys; fd++)
1361 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1363 UNLOCK_GLOBAL_MUTEX;
1367 /* get keys structure */
1369 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1371 register afs_int32 code;
1374 code = afsconf_Check(adir);
1376 UNLOCK_GLOBAL_MUTEX;
1377 return AFSCONF_FAILURE;
1379 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1380 UNLOCK_GLOBAL_MUTEX;
1384 /* get latest key */
1386 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1387 struct ktc_encryptionKey *akey)
1391 register struct afsconf_key *tk;
1392 register afs_int32 best;
1393 struct afsconf_key *bestk;
1394 register afs_int32 code;
1397 code = afsconf_Check(adir);
1399 UNLOCK_GLOBAL_MUTEX;
1400 return AFSCONF_FAILURE;
1402 maxa = adir->keystr->nkeys;
1404 best = -1; /* highest kvno we've seen yet */
1405 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1406 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1407 if (tk->kvno == 999)
1408 continue; /* skip bcrypt keys */
1409 if (tk->kvno > best) {
1414 if (bestk) { /* found any */
1416 memcpy(akey, bestk->key, 8); /* copy out latest key */
1418 *avno = bestk->kvno; /* and kvno to caller */
1419 UNLOCK_GLOBAL_MUTEX;
1422 UNLOCK_GLOBAL_MUTEX;
1423 return AFSCONF_NOTFOUND; /* didn't find any keys */
1426 /* get a particular key */
1428 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1430 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1431 register int i, maxa;
1432 register struct afsconf_key *tk;
1433 register afs_int32 code;
1436 code = afsconf_Check(adir);
1438 UNLOCK_GLOBAL_MUTEX;
1439 return AFSCONF_FAILURE;
1441 maxa = adir->keystr->nkeys;
1443 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1444 if (tk->kvno == avno) {
1445 memcpy(akey, tk->key, 8);
1446 UNLOCK_GLOBAL_MUTEX;
1451 UNLOCK_GLOBAL_MUTEX;
1452 return AFSCONF_NOTFOUND;
1455 /* save the key structure in the appropriate file */
1457 SaveKeys(struct afsconf_dir *adir)
1459 struct afsconf_keys tkeys;
1461 register afs_int32 i;
1464 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1466 /* convert it to net byte order */
1467 for (i = 0; i < tkeys.nkeys; i++)
1468 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1469 tkeys.nkeys = htonl(tkeys.nkeys);
1471 /* rewrite keys file */
1472 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1473 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1475 return AFSCONF_FAILURE;
1476 i = write(fd, &tkeys, sizeof(tkeys));
1477 if (i != sizeof(tkeys)) {
1479 return AFSCONF_FAILURE;
1482 return AFSCONF_FAILURE;
1487 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1488 afs_int32 overwrite)
1490 register struct afsconf_keys *tk;
1491 register struct afsconf_key *tkey;
1492 register afs_int32 i;
1499 if (akvno < 0 || akvno > 255) {
1500 UNLOCK_GLOBAL_MUTEX;
1505 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1506 if (tkey->kvno == akvno) {
1508 UNLOCK_GLOBAL_MUTEX;
1509 return AFSCONF_KEYINUSE;
1516 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1517 UNLOCK_GLOBAL_MUTEX;
1518 return AFSCONF_FULL;
1520 tkey = &tk->key[tk->nkeys++];
1523 memcpy(tkey->key, akey, 8);
1525 afsconf_Touch(adir);
1526 UNLOCK_GLOBAL_MUTEX;
1530 /* this proc works by sliding the other guys down, rather than using a funny
1531 kvno value, so that callers can count on getting a good key in key[0].
1534 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1536 register struct afsconf_keys *tk;
1537 register struct afsconf_key *tkey;
1544 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1545 if (tkey->kvno == akvno) {
1551 UNLOCK_GLOBAL_MUTEX;
1552 return AFSCONF_NOTFOUND;
1555 /* otherwise slide the others down. i and tkey point at the guy to delete */
1556 for (; i < tk->nkeys - 1; i++, tkey++) {
1557 tkey->kvno = (tkey + 1)->kvno;
1558 memcpy(tkey->key, (tkey + 1)->key, 8);
1562 afsconf_Touch(adir);
1563 UNLOCK_GLOBAL_MUTEX;