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 (i < MAXHOSTSPERCELL) {
724 if (cell && !strcmp(cell, curEntry->cellInfo.name))
726 ParseHostLine(tbuffer,
727 &curEntry->cellInfo.hostAddr[i],
728 curEntry->cellInfo.hostName[i],
732 ParseHostLine(tbuffer,
733 &curEntry->cellInfo.hostAddr[i],
734 curEntry->cellInfo.hostName[i], 0);
737 if (code == AFSCONF_SYNTAX) {
738 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
744 "Can't properly parse host line \"%s\" in configuration file %s\n",
749 afsconf_CloseInternal(adir);
752 curEntry->cellInfo.numServers = ++i;
755 "Too many hosts for cell %s in configuration file %s\n",
756 curEntry->cellInfo.name, tbuf1);
760 fclose(tf); /* close the file now */
762 /* end the last partially-completed cell */
764 curEntry->next = adir->entries;
765 adir->entries = curEntry;
770 * Windows maintains a CellServDB list in the Registry
771 * that supercedes the contents of the CellServDB file.
772 * Prepending these entries to the head of the list
773 * is sufficient to enforce the precedence.
775 cm_EnumerateCellRegistry( enumCellRegistry.client,
776 cm_enumCellRegistryProc,
778 #endif /* AFS_NT40_ENV */
780 /* Read in the alias list */
781 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
783 tf = fopen(tbuffer, "r");
787 tp = fgets(tbuffer, sizeof(tbuffer), tf);
790 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
792 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
793 continue; /* empty line */
796 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
799 continue; /* invalid line */
801 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
804 continue; /* invalid line */
807 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
812 curAlias = malloc(sizeof(*curAlias));
813 memset(curAlias, 0, sizeof(*curAlias));
815 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
816 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
818 curAlias->next = adir->alias_entries;
819 adir->alias_entries = curAlias;
824 /* now read the fs keys, if possible */
825 adir->keystr = (struct afsconf_keys *)0;
826 afsconf_IntGetKeys(adir);
831 /* parse a line of the form
832 *"128.2.1.3 #hostname" or
833 *"[128.2.1.3] #hostname" for clones
834 * into the appropriate pieces.
837 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
841 register afs_int32 code;
847 /* FIXME: length of aname unknown here */
848 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
852 /* FIXME: length of aname unknown here */
853 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
856 return AFSCONF_SYNTAX;
857 addr->sin_family = AF_INET;
859 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
860 addr->sin_len = sizeof(struct sockaddr_in);
862 tp = (char *)&addr->sin_addr;
870 /* parse a line of the form
871 * ">cellname [linkedcellname] [#comments]"
872 * into the appropriate pieces.
875 ParseCellLine(register char *aline, register char *aname,
876 register char *alname)
879 /* FIXME: length of aname, alname unknown here */
880 code = sscanf(aline, ">%s %s", aname, alname);
884 if (*alname == '#') {
888 return (code > 0 ? 0 : AFSCONF_SYNTAX);
891 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
893 afsconf_CellApply(struct afsconf_dir *adir,
894 int (*aproc) (struct afsconf_cell * cell, void *arock,
895 struct afsconf_dir * dir), void *arock)
897 register struct afsconf_entry *tde;
898 register afs_int32 code;
900 for (tde = adir->entries; tde; tde = tde->next) {
901 code = (*aproc) (&tde->cellInfo, arock, adir);
911 /* call aproc(entry, arock, adir) for all cell aliases.
912 * Proc must return 0, or we'll stop early and return the code it returns
915 afsconf_CellAliasApply(struct afsconf_dir *adir,
916 int (*aproc) (struct afsconf_cellalias * alias,
917 void *arock, struct afsconf_dir * dir),
920 register struct afsconf_aliasentry *tde;
921 register afs_int32 code;
923 for (tde = adir->alias_entries; tde; tde = tde->next) {
924 code = (*aproc) (&tde->aliasInfo, arock, adir);
934 afs_int32 afsconf_SawCell = 0;
937 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
938 char *aservice, struct afsconf_cell *acellInfo,
944 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
951 cell = (char *)&acellInfo->name;
953 code = afsconf_OpenInternal(adir, cell, clones);
958 #if !defined(AFS_NT40_ENV)
960 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
961 struct afsconf_cell *acellInfo)
964 int tservice, i, len;
965 unsigned char answer[1024];
969 char realCellName[256];
975 /* The resolver isn't always MT-safe.. Perhaps this ought to be
976 * replaced with a more fine-grained lock just for the resolver
981 if ( ! strchr(acellName,'.') ) {
982 cellnamelength=strlen(acellName);
983 dotcellname=malloc(cellnamelength+2);
984 memcpy(dotcellname,acellName,cellnamelength);
985 dotcellname[cellnamelength]='.';
986 dotcellname[cellnamelength+1]=0;
988 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
990 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
996 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1005 return AFSCONF_NOTFOUND;
1008 p = answer + sizeof(HEADER); /* Skip header */
1009 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1011 return AFSCONF_NOTFOUND;
1013 p += code + QFIXEDSZ; /* Skip name */
1015 while (p < answer + len) {
1016 int type, ttl, size;
1018 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1020 return AFSCONF_NOTFOUND;
1022 p += code; /* Skip the name */
1023 type = (p[0] << 8) | p[1];
1024 p += 4; /* Skip type and class */
1025 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1026 p += 4; /* Skip the TTL */
1027 size = (p[0] << 8) | p[1];
1028 p += 2; /* Skip the size */
1030 if (type == T_AFSDB) {
1034 afsdb_type = (p[0] << 8) | p[1];
1035 if (afsdb_type == 1) {
1037 * We know this is an AFSDB record for our cell, of the
1038 * right AFSDB type. Write down the true cell name that
1039 * the resolver gave us above.
1041 strlcpy(realCellName, host, sizeof realCellName);
1044 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1046 return AFSCONF_NOTFOUND;
1048 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1049 /* Do we want to get TTL data for the A record as well? */
1050 (he = gethostbyname(host))) {
1052 memcpy(&ipaddr, he->h_addr, he->h_length);
1053 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1054 strncpy(acellInfo->hostName[server_num], host,
1055 sizeof(acellInfo->hostName[server_num]));
1058 if (!minttl || ttl < minttl)
1066 if (server_num == 0) /* No AFSDB records */
1067 return AFSCONF_NOTFOUND;
1069 /* Convert the real cell name to lowercase */
1070 for (p = (unsigned char *)realCellName; *p; p++)
1073 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1074 acellInfo->numServers = server_num;
1077 tservice = afsconf_FindService(aservice);
1079 return AFSCONF_NOTFOUND; /* service not found */
1080 for (i = 0; i < acellInfo->numServers; i++) {
1081 acellInfo->hostAddr[i].sin_port = tservice;
1085 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1091 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1092 struct afsconf_cell *acellInfo)
1094 register afs_int32 i;
1096 struct afsconf_entry DNSce;
1097 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1098 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1099 unsigned short ipRanks[AFSMAXCELLHOSTS];
1104 DNSce.cellInfo.numServers = 0;
1106 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1108 /* ignore the ttl here since this code is only called by transitory programs
1109 * like klog, etc. */
1112 if (numServers == 0)
1115 for (i = 0; i < numServers; i++) {
1116 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1118 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1119 acellInfo->hostAddr[i].sin_family = AF_INET;
1121 /* sin_port supplied by connection code */
1124 acellInfo->numServers = numServers;
1125 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1128 tservice = afsconf_FindService(aservice);
1129 UNLOCK_GLOBAL_MUTEX;
1131 return AFSCONF_NOTFOUND; /* service not found */
1133 for (i = 0; i < acellInfo->numServers; i++) {
1134 acellInfo->hostAddr[i].sin_port = tservice;
1137 acellInfo->linkedCell = NULL; /* no linked cell */
1138 acellInfo->flags = 0;
1141 #endif /* windows */
1142 #endif /* AFS_AFSDB_ENV */
1145 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1146 struct afsconf_cell *acellInfo)
1148 register struct afsconf_entry *tce;
1149 struct afsconf_aliasentry *tcae;
1150 struct afsconf_entry *bestce;
1151 register afs_int32 i;
1160 afsconf_Check(adir);
1163 cnLen = (int)(strlen(tcell) + 1);
1164 lcstring(tcell, tcell, cnLen);
1165 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1166 /* call to afsconf_GetLocalCell: like klog */
1168 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1170 UNLOCK_GLOBAL_MUTEX;
1175 cnLen = strlen(tcell);
1176 bestce = (struct afsconf_entry *)0;
1179 UNLOCK_GLOBAL_MUTEX;
1183 /* Look through the list of aliases */
1184 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1185 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1186 tcell = tcae->aliasInfo.realName;
1191 for (tce = adir->entries; tce; tce = tce->next) {
1192 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1193 /* found our cell */
1198 if (strlen(tce->cellInfo.name) < cnLen)
1199 continue; /* clearly wrong */
1200 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1202 ambig = 1; /* ambiguous unless we get exact match */
1206 if (!ambig && bestce && bestce->cellInfo.numServers) {
1207 *acellInfo = bestce->cellInfo; /* structure assignment */
1209 tservice = afsconf_FindService(aservice);
1211 UNLOCK_GLOBAL_MUTEX;
1212 return AFSCONF_NOTFOUND; /* service not found */
1214 for (i = 0; i < acellInfo->numServers; i++) {
1215 acellInfo->hostAddr[i].sin_port = tservice;
1218 acellInfo->timeout = 0;
1219 UNLOCK_GLOBAL_MUTEX;
1222 UNLOCK_GLOBAL_MUTEX;
1223 #ifdef AFS_AFSDB_ENV
1224 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1226 return AFSCONF_NOTFOUND;
1227 #endif /* AFS_AFSDB_ENV */
1232 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1235 static int afsconf_showcell = 0;
1241 * If a cell switch was specified in a command, then it should override the
1242 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1243 * is set and the cell name in the adir structure is used.
1244 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1246 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1247 if (!afsconf_showcell) {
1248 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1250 afsconf_showcell = 1;
1252 strncpy(aname, afscell_path, alen);
1254 afsconf_Check(adir);
1255 if (adir->cellName) {
1256 strncpy(aname, adir->cellName, alen);
1258 code = AFSCONF_UNKNOWN;
1261 UNLOCK_GLOBAL_MUTEX;
1266 afsconf_Close(struct afsconf_dir *adir)
1269 afsconf_CloseInternal(adir);
1273 UNLOCK_GLOBAL_MUTEX;
1278 afsconf_CloseInternal(register struct afsconf_dir *adir)
1280 register struct afsconf_entry *td, *nd;
1281 struct afsconf_aliasentry *ta, *na;
1282 register char *tname;
1284 tname = adir->name; /* remember name, since that's all we preserve */
1286 /* free everything we can find */
1288 free(adir->cellName);
1289 for (td = adir->entries; td; td = nd) {
1291 if (td->cellInfo.linkedCell)
1292 free(td->cellInfo.linkedCell);
1295 for (ta = adir->alias_entries; ta; ta = na) {
1303 memset(adir, 0, sizeof(struct afsconf_dir));
1304 adir->name = tname; /* restore it */
1309 afsconf_Reopen(register struct afsconf_dir *adir)
1311 register afs_int32 code;
1312 code = afsconf_CloseInternal(adir);
1315 code = afsconf_OpenInternal(adir, 0, 0);
1319 /* called during opening of config file */
1321 afsconf_IntGetKeys(struct afsconf_dir *adir)
1325 struct afsconf_keys *tstr;
1326 register afs_int32 code;
1329 /* NT client config dir has no KeyFile; don't risk attempting open
1330 * because there might be a random file of this name if dir is shared.
1332 if (IsClientConfigDirectory(adir->name)) {
1333 adir->keystr = ((struct afsconf_keys *)
1334 malloc(sizeof(struct afsconf_keys)));
1335 adir->keystr->nkeys = 0;
1338 #endif /* AFS_NT40_ENV */
1341 /* compute the key name and other setup */
1342 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1343 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1344 adir->keystr = tstr;
1347 fd = open(tbuffer, O_RDONLY);
1350 UNLOCK_GLOBAL_MUTEX;
1353 code = read(fd, tstr, sizeof(struct afsconf_keys));
1355 if (code < sizeof(afs_int32)) {
1357 UNLOCK_GLOBAL_MUTEX;
1361 /* convert key structure to host order */
1362 tstr->nkeys = ntohl(tstr->nkeys);
1364 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1366 UNLOCK_GLOBAL_MUTEX;
1370 for (fd = 0; fd < tstr->nkeys; fd++)
1371 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1373 UNLOCK_GLOBAL_MUTEX;
1377 /* get keys structure */
1379 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1381 register afs_int32 code;
1384 code = afsconf_Check(adir);
1386 UNLOCK_GLOBAL_MUTEX;
1387 return AFSCONF_FAILURE;
1389 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1390 UNLOCK_GLOBAL_MUTEX;
1394 /* get latest key */
1396 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1397 struct ktc_encryptionKey *akey)
1401 register struct afsconf_key *tk;
1402 register afs_int32 best;
1403 struct afsconf_key *bestk;
1404 register afs_int32 code;
1407 code = afsconf_Check(adir);
1409 UNLOCK_GLOBAL_MUTEX;
1410 return AFSCONF_FAILURE;
1412 maxa = adir->keystr->nkeys;
1414 best = -1; /* highest kvno we've seen yet */
1415 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1416 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1417 if (tk->kvno == 999)
1418 continue; /* skip bcrypt keys */
1419 if (tk->kvno > best) {
1424 if (bestk) { /* found any */
1426 memcpy(akey, bestk->key, 8); /* copy out latest key */
1428 *avno = bestk->kvno; /* and kvno to caller */
1429 UNLOCK_GLOBAL_MUTEX;
1432 UNLOCK_GLOBAL_MUTEX;
1433 return AFSCONF_NOTFOUND; /* didn't find any keys */
1436 /* get a particular key */
1438 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1440 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1441 register int i, maxa;
1442 register struct afsconf_key *tk;
1443 register afs_int32 code;
1446 code = afsconf_Check(adir);
1448 UNLOCK_GLOBAL_MUTEX;
1449 return AFSCONF_FAILURE;
1451 maxa = adir->keystr->nkeys;
1453 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1454 if (tk->kvno == avno) {
1455 memcpy(akey, tk->key, 8);
1456 UNLOCK_GLOBAL_MUTEX;
1461 UNLOCK_GLOBAL_MUTEX;
1462 return AFSCONF_NOTFOUND;
1465 /* save the key structure in the appropriate file */
1467 SaveKeys(struct afsconf_dir *adir)
1469 struct afsconf_keys tkeys;
1471 register afs_int32 i;
1474 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1476 /* convert it to net byte order */
1477 for (i = 0; i < tkeys.nkeys; i++)
1478 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1479 tkeys.nkeys = htonl(tkeys.nkeys);
1481 /* rewrite keys file */
1482 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1483 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1485 return AFSCONF_FAILURE;
1486 i = write(fd, &tkeys, sizeof(tkeys));
1487 if (i != sizeof(tkeys)) {
1489 return AFSCONF_FAILURE;
1492 return AFSCONF_FAILURE;
1497 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1498 afs_int32 overwrite)
1500 register struct afsconf_keys *tk;
1501 register struct afsconf_key *tkey;
1502 register afs_int32 i;
1509 if (akvno < 0 || akvno > 255) {
1510 UNLOCK_GLOBAL_MUTEX;
1515 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1516 if (tkey->kvno == akvno) {
1518 UNLOCK_GLOBAL_MUTEX;
1519 return AFSCONF_KEYINUSE;
1526 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1527 UNLOCK_GLOBAL_MUTEX;
1528 return AFSCONF_FULL;
1530 tkey = &tk->key[tk->nkeys++];
1533 memcpy(tkey->key, akey, 8);
1535 afsconf_Touch(adir);
1536 UNLOCK_GLOBAL_MUTEX;
1540 /* this proc works by sliding the other guys down, rather than using a funny
1541 kvno value, so that callers can count on getting a good key in key[0].
1544 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1546 register struct afsconf_keys *tk;
1547 register struct afsconf_key *tkey;
1554 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1555 if (tkey->kvno == akvno) {
1561 UNLOCK_GLOBAL_MUTEX;
1562 return AFSCONF_NOTFOUND;
1565 /* otherwise slide the others down. i and tkey point at the guy to delete */
1566 for (; i < tk->nkeys - 1; i++, tkey++) {
1567 tkey->kvno = (tkey + 1)->kvno;
1568 memcpy(tkey->key, (tkey + 1)->key, 8);
1572 afsconf_Touch(adir);
1573 UNLOCK_GLOBAL_MUTEX;