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"
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>
37 #include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 #include <arpa/nameser_compat.h>
42 #endif /* AFS_AFSDB_ENV */
43 #endif /* AFS_NT40_ENV */
44 #include <afs/afsint.h>
59 #include <afs/afsutil.h>
60 #include "cellconfig.h"
64 /* cm_dns.h depends on cellconfig.h */
67 #endif /* AFS_AFSDB_ENV */
72 static struct afsconf_servPair serviceTable[] = {
83 {"afsres", 7010,}, /* residency database for MR-AFS */
84 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
85 {0, 0} /* insert new services before this spot */
89 static afs_int32 afsconf_FindService(register const char *aname);
90 static int TrimLine(char *abuffer, int abufsize);
92 static int IsClientConfigDirectory(const char *path);
93 static int GetCellNT(struct afsconf_dir *adir);
95 static int afsconf_Check(register struct afsconf_dir *adir);
96 static int afsconf_Touch(register struct afsconf_dir *adir);
97 static int GetCellUnix(struct afsconf_dir *adir);
98 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
100 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
101 char *aname, char *aclone);
102 static int ParseCellLine(register char *aline, register char *aname,
103 register char *alname);
104 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
105 static int afsconf_Reopen(register struct afsconf_dir *adir);
106 static int SaveKeys(struct afsconf_dir *adir);
109 #define T_AFSDB 18 /* per RFC1183 section 1 */
113 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
114 * our code can tell if there is new info in the key files, the cell server db
115 * files or any of the other files (and reopen the thing) if the date on
116 * CellServDB changes.
119 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
120 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
121 get an fd <= 255. We allow the fileserver to claim more fds than that.
122 This has always been a problem since pr_Initialize would have the same
123 issue, but hpr_Initialize makes it more likely that we would see this.
124 Work around it. This is not generic. It's coded with the needs of
125 afsconf_* in mind only.
127 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
132 struct afsconf_iobuffer {
139 typedef struct afsconf_iobuffer afsconf_FILE;
141 static afsconf_FILE *
142 afsconf_fopen(const char *fname, const char *fmode)
147 if ((fd = open(fname, O_RDONLY)) == -1) {
151 iop = malloc(sizeof(struct afsconf_iobuffer));
158 iop->buffer = malloc(BUFFER);
159 if (iop->buffer == NULL) {
165 iop->ptr = iop->buffer;
166 iop->endptr = iop->buffer;
171 afsconf_fclose(afsconf_FILE *iop)
177 free((void *)iop->buffer);
183 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
191 if (iop->ptr == iop->endptr) {
194 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
204 iop->ptr = iop->buffer;
205 iop->endptr = iop->buffer + len;
209 if ((p - s) == (n - 1)) {
219 #define fopen afsconf_fopen
220 #define fclose afsconf_fclose
221 #define fgets afsconf_fgets
223 #define afsconf_FILE FILE
224 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
226 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
228 afsconf_FindService(register const char *aname)
230 /* lookup a service name */
232 register struct afsconf_servPair *tsp;
234 #if defined(AFS_OSF_ENV)
235 ts = getservbyname(aname, "");
237 ts = getservbyname(aname, NULL);
240 /* we found it in /etc/services, so we use this value */
241 return ts->s_port; /* already in network byte order */
244 /* not found in /etc/services, see if it is one of ours */
245 for (tsp = serviceTable;; tsp++) {
246 if (tsp->name == NULL)
248 if (!strcmp(tsp->name, aname))
249 return htons(tsp->port);
254 TrimLine(char *abuffer, int abufsize)
266 strlcpy(tbuffer, tp, sizeof tbuffer);
267 strlcpy(abuffer, tbuffer, abufsize);
273 * IsClientConfigDirectory() -- determine if path matches well-known
274 * client configuration directory.
277 IsClientConfigDirectory(const char *path)
279 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
282 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
283 int cc = tolower(cdir[i]);
284 int pc = tolower(path[i]);
297 /* hit end of one or both; allow mismatch in existence of trailing slash */
298 if (cdir[i] != '\0') {
299 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
303 if (path[i] != '\0') {
304 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
310 #endif /* AFS_NT40_ENV */
314 afsconf_Check(register struct afsconf_dir *adir)
321 register afs_int32 code;
324 /* NT client CellServDB has different file name than NT server or Unix */
325 if (IsClientConfigDirectory(adir->name)) {
326 if (!afssw_GetClientCellServDBDir(&p)) {
327 strcompose(tbuffer, sizeof(tbuffer), p, "/",
328 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
332 strncpy(tbuffer, adir->name, sizeof(tbuffer));
333 len = (int)strlen(tbuffer);
334 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
335 strncat(tbuffer, "\\", sizeof(tbuffer));
337 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
339 tbuffer[sizeof(tbuffer) - 1] = '\0';
342 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
346 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
347 #endif /* AFS_NT40_ENV */
349 code = stat(tbuffer, &tstat);
353 /* did file change? */
354 if (tstat.st_mtime == adir->timeRead) {
357 /* otherwise file has changed, so reopen it */
358 return afsconf_Reopen(adir);
361 /* set modtime on file */
363 afsconf_Touch(register struct afsconf_dir *adir)
367 struct timeval tvp[2];
372 adir->timeRead = 0; /* just in case */
375 /* NT client CellServDB has different file name than NT server or Unix */
377 if (IsClientConfigDirectory(adir->name)) {
378 if (!afssw_GetClientCellServDBDir(&p)) {
379 strcompose(tbuffer, sizeof(tbuffer), p, "/",
380 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
383 int len = (int)strlen(tbuffer);
384 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
385 strncat(tbuffer, "\\", sizeof(tbuffer));
387 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
389 tbuffer[sizeof(tbuffer) - 1] = '\0';
392 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
396 return _utime(tbuffer, NULL);
399 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
400 gettimeofday(&tvp[0], NULL);
402 return utimes(tbuffer, tvp);
403 #endif /* AFS_NT40_ENV */
407 afsconf_Open(register const char *adir)
409 register struct afsconf_dir *tdir;
410 register afs_int32 code;
413 /* zero structure and fill in name; rest is done by internal routine */
414 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
415 memset(tdir, 0, sizeof(struct afsconf_dir));
416 tdir->name = strdup(adir);
418 code = afsconf_OpenInternal(tdir, 0, 0);
420 char *afsconf_path, afs_confdir[128];
423 /* Check global place only when local Open failed for whatever reason */
424 if (!(afsconf_path = getenv("AFSCONF"))) {
425 /* 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... */
430 if (!(home_dir = getenv("HOME"))) {
431 /* Our last chance is the "/.AFSCONF" file */
432 fp = fopen("/.AFSCONF", "r");
436 return (struct afsconf_dir *)0;
438 fgets(afs_confdir, 128, fp);
443 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
444 fp = fopen(pathname, "r");
446 /* Our last chance is the "/.AFSCONF" file */
447 fp = fopen("/.AFSCONF", "r");
451 return (struct afsconf_dir *)0;
454 fgets(afs_confdir, 128, fp);
457 len = strlen(afs_confdir);
461 return (struct afsconf_dir *)0;
463 if (afs_confdir[len - 1] == '\n') {
464 afs_confdir[len - 1] = 0;
466 afsconf_path = afs_confdir;
468 tdir->name = strdup(afsconf_path);
469 code = afsconf_OpenInternal(tdir, 0, 0);
474 return (struct afsconf_dir *)0;
482 GetCellUnix(struct afsconf_dir *adir)
489 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
490 fp = fopen(tbuffer, "r");
494 rc = fgets(tbuffer, 256, fp);
500 while (*start != '\0' && isspace(*start))
503 while (*p != '\0' && !isspace(*p))
509 adir->cellName = strdup(start);
516 GetCellNT(struct afsconf_dir *adir)
518 if (IsClientConfigDirectory(adir->name)) {
519 /* NT client config dir; ThisCell is in registry (no file). */
520 return afssw_GetClientCellName(&adir->cellName);
522 /* NT server config dir; works just like Unix */
523 return GetCellUnix(adir);
526 #endif /* AFS_NT40_ENV */
530 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
534 register char *tp, *bp;
535 register struct afsconf_entry *curEntry;
536 struct afsconf_aliasentry *curAlias;
537 register afs_int32 code;
539 char tbuffer[256], tbuf1[256];
542 /* figure out the local cell name */
546 i = GetCellUnix(adir);
549 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
555 /* now parse the individual lines */
559 /* NT client/server have a CellServDB that is the same format as Unix.
560 * However, the NT client uses a different file name
562 if (IsClientConfigDirectory(adir->name)) {
563 /* NT client config dir */
565 if (!afssw_GetClientCellServDBDir(&p)) {
566 strcompose(tbuffer, sizeof(tbuffer), p, "/",
567 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
571 strncpy(tbuffer, adir->name, sizeof(tbuffer));
572 len = (int)strlen(tbuffer);
573 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
574 strncat(tbuffer, "\\", sizeof(tbuffer));
576 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
578 tbuffer[sizeof(tbuffer) - 1] = '\0';
581 /* NT server config dir */
582 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
586 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
587 #endif /* AFS_NT40_ENV */
589 if (!stat(tbuffer, &tstat)) {
590 adir->timeRead = tstat.st_mtime;
595 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
596 tf = fopen(tbuffer, "r");
601 tp = fgets(tbuffer, sizeof(tbuffer), tf);
604 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
605 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
606 continue; /* empty line */
607 if (tbuffer[0] == '>') {
608 char linkedcell[MAXCELLCHARS];
609 /* start new cell item */
611 /* thread this guy on the list */
612 curEntry->next = adir->entries;
613 adir->entries = curEntry;
617 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
618 memset(curEntry, 0, sizeof(struct afsconf_entry));
620 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
622 afsconf_CloseInternal(adir);
627 if (linkedcell[0] != '\0')
628 curEntry->cellInfo.linkedCell = strdup(linkedcell);
630 /* new host in the current cell */
632 afsconf_CloseInternal(adir);
636 i = curEntry->cellInfo.numServers;
637 if (cell && !strcmp(cell, curEntry->cellInfo.name))
639 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
640 curEntry->cellInfo.hostName[i], &clones[i]);
643 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
644 curEntry->cellInfo.hostName[i], 0);
646 if (code == AFSCONF_SYNTAX) {
647 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
653 "Can't properly parse host line \"%s\" in configuration file %s\n",
658 afsconf_CloseInternal(adir);
661 curEntry->cellInfo.numServers = ++i;
664 fclose(tf); /* close the file now */
666 /* end the last partially-completed cell */
668 curEntry->next = adir->entries;
669 adir->entries = curEntry;
672 /* Read in the alias list */
673 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
675 tf = fopen(tbuffer, "r");
679 tp = fgets(tbuffer, sizeof(tbuffer), tf);
682 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
684 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
685 continue; /* empty line */
688 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
691 continue; /* invalid line */
693 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
696 continue; /* invalid line */
699 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
704 curAlias = malloc(sizeof(*curAlias));
705 memset(curAlias, 0, sizeof(*curAlias));
707 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
708 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
710 curAlias->next = adir->alias_entries;
711 adir->alias_entries = curAlias;
716 /* now read the fs keys, if possible */
717 adir->keystr = (struct afsconf_keys *)0;
718 afsconf_IntGetKeys(adir);
723 /* parse a line of the form
724 *"128.2.1.3 #hostname" or
725 *"[128.2.1.3] #hostname" for clones
726 * into the appropriate pieces.
729 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
733 register afs_int32 code;
739 /* FIXME: length of aname unknown here */
740 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
744 /* FIXME: length of aname unknown here */
745 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
748 return AFSCONF_SYNTAX;
749 addr->sin_family = AF_INET;
751 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
752 addr->sin_len = sizeof(struct sockaddr_in);
754 tp = (char *)&addr->sin_addr;
762 /* parse a line of the form
763 * ">cellname [linkedcellname] [#comments]"
764 * into the appropriate pieces.
767 ParseCellLine(register char *aline, register char *aname,
768 register char *alname)
771 /* FIXME: length of aname, alname unknown here */
772 code = sscanf(aline, ">%s %s", aname, alname);
776 if (*alname == '#') {
780 return (code > 0 ? 0 : AFSCONF_SYNTAX);
783 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
785 afsconf_CellApply(struct afsconf_dir *adir,
786 int (*aproc) (struct afsconf_cell * cell, void *arock,
787 struct afsconf_dir * dir), void *arock)
789 register struct afsconf_entry *tde;
790 register afs_int32 code;
792 for (tde = adir->entries; tde; tde = tde->next) {
793 code = (*aproc) (&tde->cellInfo, arock, adir);
803 /* call aproc(entry, arock, adir) for all cell aliases.
804 * Proc must return 0, or we'll stop early and return the code it returns
807 afsconf_CellAliasApply(struct afsconf_dir *adir,
808 int (*aproc) (struct afsconf_cellalias * alias,
809 void *arock, struct afsconf_dir * dir),
812 register struct afsconf_aliasentry *tde;
813 register afs_int32 code;
815 for (tde = adir->alias_entries; tde; tde = tde->next) {
816 code = (*aproc) (&tde->aliasInfo, arock, adir);
826 afs_int32 afsconf_SawCell = 0;
829 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
830 char *aservice, struct afsconf_cell *acellInfo,
836 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
843 cell = (char *)&acellInfo->name;
845 code = afsconf_OpenInternal(adir, cell, clones);
850 #if !defined(AFS_NT40_ENV)
852 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
853 struct afsconf_cell *acellInfo)
856 int tservice, i, len;
857 unsigned char answer[1024];
861 char realCellName[256];
867 /* The resolver isn't always MT-safe.. Perhaps this ought to be
868 * replaced with a more fine-grained lock just for the resolver
873 if ( ! strchr(acellName,'.') ) {
874 cellnamelength=strlen(acellName);
875 dotcellname=malloc(cellnamelength+2);
876 memcpy(dotcellname,acellName,cellnamelength);
877 dotcellname[cellnamelength]='.';
878 dotcellname[cellnamelength+1]=0;
880 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
882 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
888 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
897 return AFSCONF_NOTFOUND;
900 p = answer + sizeof(HEADER); /* Skip header */
901 code = dn_expand(answer, answer + len, p, host, sizeof(host));
903 return AFSCONF_NOTFOUND;
905 p += code + QFIXEDSZ; /* Skip name */
907 while (p < answer + len) {
910 code = dn_expand(answer, answer + len, p, host, sizeof(host));
912 return AFSCONF_NOTFOUND;
914 p += code; /* Skip the name */
915 type = (p[0] << 8) | p[1];
916 p += 4; /* Skip type and class */
917 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
918 p += 4; /* Skip the TTL */
919 size = (p[0] << 8) | p[1];
920 p += 2; /* Skip the size */
922 if (type == T_AFSDB) {
926 afsdb_type = (p[0] << 8) | p[1];
927 if (afsdb_type == 1) {
929 * We know this is an AFSDB record for our cell, of the
930 * right AFSDB type. Write down the true cell name that
931 * the resolver gave us above.
933 strlcpy(realCellName, host, sizeof realCellName);
936 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
938 return AFSCONF_NOTFOUND;
940 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
941 /* Do we want to get TTL data for the A record as well? */
942 (he = gethostbyname(host))) {
944 memcpy(&ipaddr, he->h_addr, he->h_length);
945 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
946 strncpy(acellInfo->hostName[server_num], host,
947 sizeof(acellInfo->hostName[server_num]));
950 if (!minttl || ttl < minttl)
958 if (server_num == 0) /* No AFSDB records */
959 return AFSCONF_NOTFOUND;
961 /* Convert the real cell name to lowercase */
962 for (p = (unsigned char *)realCellName; *p; p++)
965 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
966 acellInfo->numServers = server_num;
969 tservice = afsconf_FindService(aservice);
971 return AFSCONF_NOTFOUND; /* service not found */
972 for (i = 0; i < acellInfo->numServers; i++) {
973 acellInfo->hostAddr[i].sin_port = tservice;
977 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
983 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
984 struct afsconf_cell *acellInfo)
986 register afs_int32 i;
988 struct afsconf_entry DNSce;
989 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
990 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
995 DNSce.cellInfo.numServers = 0;
997 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
999 /* ignore the ttl here since this code is only called by transitory programs
1000 * like klog, etc. */
1003 if (numServers == 0)
1006 for (i = 0; i < numServers; i++) {
1007 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1009 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1010 acellInfo->hostAddr[i].sin_family = AF_INET;
1012 /* sin_port supplied by connection code */
1015 acellInfo->numServers = numServers;
1016 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1019 tservice = afsconf_FindService(aservice);
1020 UNLOCK_GLOBAL_MUTEX;
1022 return AFSCONF_NOTFOUND; /* service not found */
1024 for (i = 0; i < acellInfo->numServers; i++) {
1025 acellInfo->hostAddr[i].sin_port = tservice;
1028 acellInfo->linkedCell = NULL; /* no linked cell */
1029 acellInfo->flags = 0;
1032 #endif /* windows */
1033 #endif /* AFS_AFSDB_ENV */
1036 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1037 struct afsconf_cell *acellInfo)
1039 register struct afsconf_entry *tce;
1040 struct afsconf_aliasentry *tcae;
1041 struct afsconf_entry *bestce;
1042 register afs_int32 i;
1051 afsconf_Check(adir);
1054 cnLen = (int)(strlen(tcell) + 1);
1055 lcstring(tcell, tcell, cnLen);
1056 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1057 /* call to afsconf_GetLocalCell: like klog */
1059 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1061 UNLOCK_GLOBAL_MUTEX;
1066 cnLen = strlen(tcell);
1067 bestce = (struct afsconf_entry *)0;
1070 UNLOCK_GLOBAL_MUTEX;
1074 /* Look through the list of aliases */
1075 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1076 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1077 tcell = tcae->aliasInfo.realName;
1082 for (tce = adir->entries; tce; tce = tce->next) {
1083 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1084 /* found our cell */
1089 if (strlen(tce->cellInfo.name) < cnLen)
1090 continue; /* clearly wrong */
1091 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1093 ambig = 1; /* ambiguous unless we get exact match */
1097 if (!ambig && bestce && bestce->cellInfo.numServers) {
1098 *acellInfo = bestce->cellInfo; /* structure assignment */
1100 tservice = afsconf_FindService(aservice);
1102 UNLOCK_GLOBAL_MUTEX;
1103 return AFSCONF_NOTFOUND; /* service not found */
1105 for (i = 0; i < acellInfo->numServers; i++) {
1106 acellInfo->hostAddr[i].sin_port = tservice;
1109 acellInfo->timeout = 0;
1110 UNLOCK_GLOBAL_MUTEX;
1113 UNLOCK_GLOBAL_MUTEX;
1114 #ifdef AFS_AFSDB_ENV
1115 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1117 return AFSCONF_NOTFOUND;
1118 #endif /* AFS_AFSDB_ENV */
1123 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1126 static int afsconf_showcell = 0;
1132 * If a cell switch was specified in a command, then it should override the
1133 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1134 * is set and the cell name in the adir structure is used.
1135 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1137 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1138 if (!afsconf_showcell) {
1139 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1141 afsconf_showcell = 1;
1143 strncpy(aname, afscell_path, alen);
1145 afsconf_Check(adir);
1146 if (adir->cellName) {
1147 strncpy(aname, adir->cellName, alen);
1149 code = AFSCONF_UNKNOWN;
1152 UNLOCK_GLOBAL_MUTEX;
1157 afsconf_Close(struct afsconf_dir *adir)
1160 afsconf_CloseInternal(adir);
1164 UNLOCK_GLOBAL_MUTEX;
1169 afsconf_CloseInternal(register struct afsconf_dir *adir)
1171 register struct afsconf_entry *td, *nd;
1172 struct afsconf_aliasentry *ta, *na;
1173 register char *tname;
1175 tname = adir->name; /* remember name, since that's all we preserve */
1177 /* free everything we can find */
1179 free(adir->cellName);
1180 for (td = adir->entries; td; td = nd) {
1182 if (td->cellInfo.linkedCell)
1183 free(td->cellInfo.linkedCell);
1186 for (ta = adir->alias_entries; ta; ta = na) {
1194 memset(adir, 0, sizeof(struct afsconf_dir));
1195 adir->name = tname; /* restore it */
1200 afsconf_Reopen(register struct afsconf_dir *adir)
1202 register afs_int32 code;
1203 code = afsconf_CloseInternal(adir);
1206 code = afsconf_OpenInternal(adir, 0, 0);
1210 /* called during opening of config file */
1212 afsconf_IntGetKeys(struct afsconf_dir *adir)
1216 struct afsconf_keys *tstr;
1217 register afs_int32 code;
1220 /* NT client config dir has no KeyFile; don't risk attempting open
1221 * because there might be a random file of this name if dir is shared.
1223 if (IsClientConfigDirectory(adir->name)) {
1224 adir->keystr = ((struct afsconf_keys *)
1225 malloc(sizeof(struct afsconf_keys)));
1226 adir->keystr->nkeys = 0;
1229 #endif /* AFS_NT40_ENV */
1232 /* compute the key name and other setup */
1233 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1234 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1235 adir->keystr = tstr;
1238 fd = open(tbuffer, O_RDONLY);
1241 UNLOCK_GLOBAL_MUTEX;
1244 code = read(fd, tstr, sizeof(struct afsconf_keys));
1246 if (code < sizeof(afs_int32)) {
1248 UNLOCK_GLOBAL_MUTEX;
1252 /* convert key structure to host order */
1253 tstr->nkeys = ntohl(tstr->nkeys);
1255 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1257 UNLOCK_GLOBAL_MUTEX;
1261 for (fd = 0; fd < tstr->nkeys; fd++)
1262 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1264 UNLOCK_GLOBAL_MUTEX;
1268 /* get keys structure */
1270 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1272 register afs_int32 code;
1275 code = afsconf_Check(adir);
1277 UNLOCK_GLOBAL_MUTEX;
1278 return AFSCONF_FAILURE;
1280 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1281 UNLOCK_GLOBAL_MUTEX;
1285 /* get latest key */
1287 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1288 struct ktc_encryptionKey *akey)
1292 register struct afsconf_key *tk;
1293 register afs_int32 best;
1294 struct afsconf_key *bestk;
1295 register afs_int32 code;
1298 code = afsconf_Check(adir);
1300 UNLOCK_GLOBAL_MUTEX;
1301 return AFSCONF_FAILURE;
1303 maxa = adir->keystr->nkeys;
1305 best = -1; /* highest kvno we've seen yet */
1306 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1307 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1308 if (tk->kvno == 999)
1309 continue; /* skip bcrypt keys */
1310 if (tk->kvno > best) {
1315 if (bestk) { /* found any */
1317 memcpy(akey, bestk->key, 8); /* copy out latest key */
1319 *avno = bestk->kvno; /* and kvno to caller */
1320 UNLOCK_GLOBAL_MUTEX;
1323 UNLOCK_GLOBAL_MUTEX;
1324 return AFSCONF_NOTFOUND; /* didn't find any keys */
1327 /* get a particular key */
1329 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1331 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1332 register int i, maxa;
1333 register struct afsconf_key *tk;
1334 register afs_int32 code;
1337 code = afsconf_Check(adir);
1339 UNLOCK_GLOBAL_MUTEX;
1340 return AFSCONF_FAILURE;
1342 maxa = adir->keystr->nkeys;
1344 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1345 if (tk->kvno == avno) {
1346 memcpy(akey, tk->key, 8);
1347 UNLOCK_GLOBAL_MUTEX;
1352 UNLOCK_GLOBAL_MUTEX;
1353 return AFSCONF_NOTFOUND;
1356 /* save the key structure in the appropriate file */
1358 SaveKeys(struct afsconf_dir *adir)
1360 struct afsconf_keys tkeys;
1362 register afs_int32 i;
1365 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1367 /* convert it to net byte order */
1368 for (i = 0; i < tkeys.nkeys; i++)
1369 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1370 tkeys.nkeys = htonl(tkeys.nkeys);
1372 /* rewrite keys file */
1373 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1374 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1376 return AFSCONF_FAILURE;
1377 i = write(fd, &tkeys, sizeof(tkeys));
1378 if (i != sizeof(tkeys)) {
1380 return AFSCONF_FAILURE;
1383 return AFSCONF_FAILURE;
1388 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1389 afs_int32 overwrite)
1391 register struct afsconf_keys *tk;
1392 register struct afsconf_key *tkey;
1393 register afs_int32 i;
1400 if (akvno < 0 || akvno > 255) {
1401 UNLOCK_GLOBAL_MUTEX;
1406 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1407 if (tkey->kvno == akvno) {
1409 UNLOCK_GLOBAL_MUTEX;
1410 return AFSCONF_KEYINUSE;
1417 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1418 UNLOCK_GLOBAL_MUTEX;
1419 return AFSCONF_FULL;
1421 tkey = &tk->key[tk->nkeys++];
1424 memcpy(tkey->key, akey, 8);
1426 afsconf_Touch(adir);
1427 UNLOCK_GLOBAL_MUTEX;
1431 /* this proc works by sliding the other guys down, rather than using a funny
1432 kvno value, so that callers can count on getting a good key in key[0].
1435 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1437 register struct afsconf_keys *tk;
1438 register struct afsconf_key *tkey;
1445 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1446 if (tkey->kvno == akvno) {
1452 UNLOCK_GLOBAL_MUTEX;
1453 return AFSCONF_NOTFOUND;
1456 /* otherwise slide the others down. i and tkey point at the guy to delete */
1457 for (; i < tk->nkeys - 1; i++, tkey++) {
1458 tkey->kvno = (tkey + 1)->kvno;
1459 memcpy(tkey->key, (tkey + 1)->key, 8);
1463 afsconf_Touch(adir);
1464 UNLOCK_GLOBAL_MUTEX;