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"
22 #include <sys/types.h>
25 #include <sys/utime.h>
27 #include <WINNT/afssw.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 #include <arpa/nameser_compat.h>
40 #endif /* AFS_AFSDB_ENV */
41 #endif /* AFS_NT40_ENV */
42 #include <afs/afsint.h>
55 #include <afs/afsutil.h>
56 #include "cellconfig.h"
60 /* cm_dns.h depends on cellconfig.h */
62 #endif /* AFS_AFSDB_ENV */
64 static struct afsconf_servPair serviceTable[] = {
75 {"afsres", 7010,}, /* residency database for MR-AFS */
76 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
77 {0, 0} /* insert new services before this spot */
81 static afs_int32 afsconf_FindService(register const char *aname);
82 static int TrimLine(char *abuffer);
84 static int IsClientConfigDirectory(const char *path);
85 static int GetCellNT(struct afsconf_dir *adir);
87 static int afsconf_Check(register struct afsconf_dir *adir);
88 static int afsconf_Touch(register struct afsconf_dir *adir);
89 static int GetCellUnix(struct afsconf_dir *adir);
90 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
92 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
93 char *aname, char *aclone);
94 static int ParseCellLine(register char *aline, register char *aname,
95 register char *alname);
96 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
97 static int afsconf_Reopen(register struct afsconf_dir *adir);
98 static int SaveKeys(struct afsconf_dir *adir);
101 #define T_AFSDB 18 /* per RFC1183 section 1 */
105 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
106 * our code can tell if there is new info in the key files, the cell server db
107 * files or any of the other files (and reopen the thing) if the date on
108 * CellServDB changes.
111 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
112 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
113 get an fd <= 255. We allow the fileserver to claim more fds than that.
114 This has always been a problem since pr_Initialize would have the same
115 issue, but hpr_Initialize makes it more likely that we would see this.
116 Work around it. This is not generic. It's coded with the needs of
117 afsconf_* in mind only.
119 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
124 struct afsconf_iobuffer {
131 typedef struct afsconf_iobuffer afsconf_FILE;
133 static afsconf_FILE *
134 afsconf_fopen(const char *fname, const char *fmode)
139 if ((fd = open(fname, O_RDONLY)) == -1) {
143 iop = malloc(sizeof(struct afsconf_iobuffer));
150 iop->buffer = malloc(BUFFER);
151 if (iop->buffer == NULL) {
157 iop->ptr = iop->buffer;
158 iop->endptr = iop->buffer;
163 afsconf_fclose(afsconf_FILE *iop)
169 free((void *)iop->buffer);
175 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
183 if (iop->ptr == iop->endptr) {
186 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
196 iop->ptr = iop->buffer;
197 iop->endptr = iop->buffer + len;
201 if ((p - s) == (n - 1)) {
211 #define fopen afsconf_fopen
212 #define fclose afsconf_fclose
213 #define fgets afsconf_fgets
215 #define afsconf_FILE FILE
216 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
218 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
220 afsconf_FindService(register const char *aname)
222 /* lookup a service name */
224 register struct afsconf_servPair *tsp;
226 #if defined(AFS_OSF_ENV)
227 ts = getservbyname(aname, "");
229 ts = getservbyname(aname, NULL);
232 /* we found it in /etc/services, so we use this value */
233 return ts->s_port; /* already in network byte order */
236 /* not found in /etc/services, see if it is one of ours */
237 for (tsp = serviceTable;; tsp++) {
238 if (tsp->name == NULL)
240 if (!strcmp(tsp->name, aname))
241 return htons(tsp->port);
246 TrimLine(char *abuffer)
259 strcpy(abuffer, tbuffer);
265 * IsClientConfigDirectory() -- determine if path matches well-known
266 * client configuration directory.
269 IsClientConfigDirectory(const char *path)
271 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
274 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
275 int cc = tolower(cdir[i]);
276 int pc = tolower(path[i]);
289 /* hit end of one or both; allow mismatch in existence of trailing slash */
290 if (cdir[i] != '\0') {
291 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
295 if (path[i] != '\0') {
296 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
302 #endif /* AFS_NT40_ENV */
306 afsconf_Check(register struct afsconf_dir *adir)
308 char tbuffer[256], *p;
310 register afs_int32 code;
313 /* NT client CellServDB has different file name than NT server or Unix */
314 if (IsClientConfigDirectory(adir->name)) {
315 if (!afssw_GetClientCellServDBDir(&p)) {
316 strcompose(tbuffer, sizeof(tbuffer), p, "/",
317 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
321 strncpy(tbuffer, adir->name, sizeof(tbuffer));
322 len = (int)strlen(tbuffer);
323 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
324 strncat(tbuffer, "\\", sizeof(tbuffer));
326 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
328 tbuffer[sizeof(tbuffer) - 1] = '\0';
331 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
335 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
336 #endif /* AFS_NT40_ENV */
338 code = stat(tbuffer, &tstat);
342 /* did file change? */
343 if (tstat.st_mtime == adir->timeRead) {
346 /* otherwise file has changed, so reopen it */
347 return afsconf_Reopen(adir);
350 /* set modtime on file */
352 afsconf_Touch(register struct afsconf_dir *adir)
354 char tbuffer[256], *p;
356 struct timeval tvp[2];
359 adir->timeRead = 0; /* just in case */
362 /* NT client CellServDB has different file name than NT server or Unix */
364 if (IsClientConfigDirectory(adir->name)) {
365 if (!afssw_GetClientCellServDBDir(&p)) {
366 strcompose(tbuffer, sizeof(tbuffer), p, "/",
367 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
370 int len = (int)strlen(tbuffer);
371 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
372 strncat(tbuffer, "\\", sizeof(tbuffer));
374 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
376 tbuffer[sizeof(tbuffer) - 1] = '\0';
379 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
383 return _utime(tbuffer, NULL);
386 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
387 gettimeofday(&tvp[0], NULL);
389 return utimes(tbuffer, tvp);
390 #endif /* AFS_NT40_ENV */
394 afsconf_Open(register const char *adir)
396 register struct afsconf_dir *tdir;
397 register afs_int32 code;
400 /* zero structure and fill in name; rest is done by internal routine */
401 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
402 memset(tdir, 0, sizeof(struct afsconf_dir));
403 tdir->name = (char *)malloc(strlen(adir) + 1);
404 strcpy(tdir->name, adir);
406 code = afsconf_OpenInternal(tdir, 0, 0);
408 char *afsconf_path, afs_confdir[128];
411 /* Check global place only when local Open failed for whatever reason */
412 if (!(afsconf_path = getenv("AFSCONF"))) {
413 /* 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... */
418 if (!(home_dir = getenv("HOME"))) {
419 /* Our last chance is the "/.AFSCONF" file */
420 fp = fopen("/.AFSCONF", "r");
424 return (struct afsconf_dir *)0;
426 fgets(afs_confdir, 128, fp);
431 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
432 fp = fopen(pathname, "r");
434 /* Our last chance is the "/.AFSCONF" file */
435 fp = fopen("/.AFSCONF", "r");
439 return (struct afsconf_dir *)0;
442 fgets(afs_confdir, 128, fp);
445 len = strlen(afs_confdir);
449 return (struct afsconf_dir *)0;
451 if (afs_confdir[len - 1] == '\n') {
452 afs_confdir[len - 1] = 0;
454 afsconf_path = afs_confdir;
456 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
457 strcpy(tdir->name, afsconf_path);
458 code = afsconf_OpenInternal(tdir, 0, 0);
463 return (struct afsconf_dir *)0;
471 GetCellUnix(struct afsconf_dir *adir)
478 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
479 fp = fopen(tbuffer, "r");
483 rc = fgets(tbuffer, 256, fp);
489 while (*start != '\0' && isspace(*start))
492 while (*p != '\0' && !isspace(*p))
498 adir->cellName = strdup(start);
505 GetCellNT(struct afsconf_dir *adir)
507 if (IsClientConfigDirectory(adir->name)) {
508 /* NT client config dir; ThisCell is in registry (no file). */
509 return afssw_GetClientCellName(&adir->cellName);
511 /* NT server config dir; works just like Unix */
512 return GetCellUnix(adir);
515 #endif /* AFS_NT40_ENV */
519 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
523 register char *tp, *bp;
524 register struct afsconf_entry *curEntry;
525 struct afsconf_aliasentry *curAlias;
526 register afs_int32 code;
528 char tbuffer[256], tbuf1[256];
531 /* figure out the local cell name */
535 i = GetCellUnix(adir);
538 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
544 /* now parse the individual lines */
548 /* NT client/server have a CellServDB that is the same format as Unix.
549 * However, the NT client uses a different file name
551 if (IsClientConfigDirectory(adir->name)) {
552 /* NT client config dir */
554 if (!afssw_GetClientCellServDBDir(&p)) {
555 strcompose(tbuffer, sizeof(tbuffer), p, "/",
556 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
560 strncpy(tbuffer, adir->name, sizeof(tbuffer));
561 len = (int)strlen(tbuffer);
562 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
563 strncat(tbuffer, "\\", sizeof(tbuffer));
565 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
567 tbuffer[sizeof(tbuffer) - 1] = '\0';
570 /* NT server config dir */
571 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
575 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
576 #endif /* AFS_NT40_ENV */
578 if (!stat(tbuffer, &tstat)) {
579 adir->timeRead = tstat.st_mtime;
584 strcpy(tbuf1, tbuffer);
585 tf = fopen(tbuffer, "r");
590 tp = fgets(tbuffer, sizeof(tbuffer), tf);
593 TrimLine(tbuffer); /* remove white space */
594 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
595 continue; /* empty line */
596 if (tbuffer[0] == '>') {
597 char linkedcell[MAXCELLCHARS];
598 /* start new cell item */
600 /* thread this guy on the list */
601 curEntry->next = adir->entries;
602 adir->entries = curEntry;
606 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
607 memset(curEntry, 0, sizeof(struct afsconf_entry));
609 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
611 afsconf_CloseInternal(adir);
616 if (linkedcell[0] != '\0') {
617 curEntry->cellInfo.linkedCell =
618 (char *)malloc(strlen(linkedcell) + 1);
619 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
622 /* new host in the current cell */
624 afsconf_CloseInternal(adir);
628 i = curEntry->cellInfo.numServers;
629 if (cell && !strcmp(cell, curEntry->cellInfo.name))
631 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
632 curEntry->cellInfo.hostName[i], &clones[i]);
635 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
636 curEntry->cellInfo.hostName[i], 0);
638 if (code == AFSCONF_SYNTAX) {
639 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
645 "Can't properly parse host line \"%s\" in configuration file %s\n",
650 afsconf_CloseInternal(adir);
653 curEntry->cellInfo.numServers = ++i;
656 fclose(tf); /* close the file now */
658 /* end the last partially-completed cell */
660 curEntry->next = adir->entries;
661 adir->entries = curEntry;
664 /* Read in the alias list */
665 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
667 tf = fopen(tbuffer, "r");
671 tp = fgets(tbuffer, sizeof(tbuffer), tf);
674 TrimLine(tbuffer); /* remove white space */
676 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
677 continue; /* empty line */
680 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
683 continue; /* invalid line */
685 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
688 continue; /* invalid line */
691 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
696 curAlias = malloc(sizeof(*curAlias));
697 memset(curAlias, 0, sizeof(*curAlias));
699 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
700 strcpy(curAlias->aliasInfo.realName, tbuffer);
702 curAlias->next = adir->alias_entries;
703 adir->alias_entries = curAlias;
708 /* now read the fs keys, if possible */
709 adir->keystr = (struct afsconf_keys *)0;
710 afsconf_IntGetKeys(adir);
715 /* parse a line of the form
716 *"128.2.1.3 #hostname" or
717 *"[128.2.1.3] #hostname" for clones
718 * into the appropriate pieces.
721 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
725 register afs_int32 code;
731 /* FIXME: length of aname unknown here */
732 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
736 /* FIXME: length of aname unknown here */
737 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
740 return AFSCONF_SYNTAX;
741 addr->sin_family = AF_INET;
743 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
744 addr->sin_len = sizeof(struct sockaddr_in);
746 tp = (char *)&addr->sin_addr;
754 /* parse a line of the form
755 * ">cellname [linkedcellname] [#comments]"
756 * into the appropriate pieces.
759 ParseCellLine(register char *aline, register char *aname,
760 register char *alname)
763 /* FIXME: length of aname, alname unknown here */
764 code = sscanf(aline, ">%s %s", aname, alname);
768 if (*alname == '#') {
772 return (code > 0 ? 0 : AFSCONF_SYNTAX);
775 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
777 afsconf_CellApply(struct afsconf_dir *adir,
778 int (*aproc) (struct afsconf_cell * cell, char *arock,
779 struct afsconf_dir * dir), char *arock)
781 register struct afsconf_entry *tde;
782 register afs_int32 code;
784 for (tde = adir->entries; tde; tde = tde->next) {
785 code = (*aproc) (&tde->cellInfo, arock, adir);
795 /* call aproc(entry, arock, adir) for all cell aliases.
796 * Proc must return 0, or we'll stop early and return the code it returns
799 afsconf_CellAliasApply(struct afsconf_dir *adir,
800 int (*aproc) (struct afsconf_cellalias * alias,
801 char *arock, struct afsconf_dir * dir),
804 register struct afsconf_aliasentry *tde;
805 register afs_int32 code;
807 for (tde = adir->alias_entries; tde; tde = tde->next) {
808 code = (*aproc) (&tde->aliasInfo, arock, adir);
818 afs_int32 afsconf_SawCell = 0;
821 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
822 char *aservice, struct afsconf_cell *acellInfo,
828 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
835 cell = (char *)&acellInfo->name;
837 code = afsconf_OpenInternal(adir, cell, clones);
842 #if !defined(AFS_NT40_ENV)
844 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
845 struct afsconf_cell *acellInfo)
848 int tservice, i, len;
849 unsigned char answer[1024];
853 char realCellName[256];
859 /* The resolver isn't always MT-safe.. Perhaps this ought to be
860 * replaced with a more fine-grained lock just for the resolver
865 if ( ! strchr(acellName,'.') ) {
866 cellnamelength=strlen(acellName);
867 dotcellname=malloc(cellnamelength+2);
868 memcpy(dotcellname,acellName,cellnamelength);
869 dotcellname[cellnamelength]='.';
870 dotcellname[cellnamelength+1]=0;
872 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
874 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
880 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
889 return AFSCONF_NOTFOUND;
892 p = answer + sizeof(HEADER); /* Skip header */
893 code = dn_expand(answer, answer + len, p, host, sizeof(host));
895 return AFSCONF_NOTFOUND;
897 p += code + QFIXEDSZ; /* Skip name */
899 while (p < answer + len) {
902 code = dn_expand(answer, answer + len, p, host, sizeof(host));
904 return AFSCONF_NOTFOUND;
906 p += code; /* Skip the name */
907 type = (p[0] << 8) | p[1];
908 p += 4; /* Skip type and class */
909 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
910 p += 4; /* Skip the TTL */
911 size = (p[0] << 8) | p[1];
912 p += 2; /* Skip the size */
914 if (type == T_AFSDB) {
918 afsdb_type = (p[0] << 8) | p[1];
919 if (afsdb_type == 1) {
921 * We know this is an AFSDB record for our cell, of the
922 * right AFSDB type. Write down the true cell name that
923 * the resolver gave us above.
925 strcpy(realCellName, host);
928 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
930 return AFSCONF_NOTFOUND;
932 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
933 /* Do we want to get TTL data for the A record as well? */
934 (he = gethostbyname(host))) {
936 memcpy(&ipaddr, he->h_addr, he->h_length);
937 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
938 strncpy(acellInfo->hostName[server_num], host,
939 sizeof(acellInfo->hostName[server_num]));
942 if (!minttl || ttl < minttl)
950 if (server_num == 0) /* No AFSDB records */
951 return AFSCONF_NOTFOUND;
953 /* Convert the real cell name to lowercase */
954 for (p = (unsigned char *)realCellName; *p; p++)
957 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
958 acellInfo->numServers = server_num;
961 tservice = afsconf_FindService(aservice);
963 return AFSCONF_NOTFOUND; /* service not found */
964 for (i = 0; i < acellInfo->numServers; i++) {
965 acellInfo->hostAddr[i].sin_port = tservice;
969 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
975 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
976 struct afsconf_cell *acellInfo)
978 register afs_int32 i;
980 struct afsconf_entry DNSce;
981 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
982 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
987 DNSce.cellInfo.numServers = 0;
989 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
991 /* ignore the ttl here since this code is only called by transitory programs
998 for (i = 0; i < numServers; i++) {
999 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1001 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1002 acellInfo->hostAddr[i].sin_family = AF_INET;
1004 /* sin_port supplied by connection code */
1007 acellInfo->numServers = numServers;
1008 strcpy(acellInfo->name, acellName);
1011 tservice = afsconf_FindService(aservice);
1012 UNLOCK_GLOBAL_MUTEX;
1014 return AFSCONF_NOTFOUND; /* service not found */
1016 for (i = 0; i < acellInfo->numServers; i++) {
1017 acellInfo->hostAddr[i].sin_port = tservice;
1020 acellInfo->linkedCell = NULL; /* no linked cell */
1021 acellInfo->flags = 0;
1024 #endif /* windows */
1025 #endif /* AFS_AFSDB_ENV */
1028 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1029 struct afsconf_cell *acellInfo)
1031 register struct afsconf_entry *tce;
1032 struct afsconf_aliasentry *tcae;
1033 struct afsconf_entry *bestce;
1034 register afs_int32 i;
1043 afsconf_Check(adir);
1046 cnLen = (int)(strlen(tcell) + 1);
1047 lcstring(tcell, tcell, cnLen);
1048 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1049 /* call to afsconf_GetLocalCell: like klog */
1051 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1053 UNLOCK_GLOBAL_MUTEX;
1058 cnLen = strlen(tcell);
1059 bestce = (struct afsconf_entry *)0;
1062 UNLOCK_GLOBAL_MUTEX;
1066 /* Look through the list of aliases */
1067 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1068 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1069 tcell = tcae->aliasInfo.realName;
1074 for (tce = adir->entries; tce; tce = tce->next) {
1075 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1076 /* found our cell */
1081 if (strlen(tce->cellInfo.name) < cnLen)
1082 continue; /* clearly wrong */
1083 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1085 ambig = 1; /* ambiguous unless we get exact match */
1089 if (!ambig && bestce && bestce->cellInfo.numServers) {
1090 *acellInfo = bestce->cellInfo; /* structure assignment */
1092 tservice = afsconf_FindService(aservice);
1094 UNLOCK_GLOBAL_MUTEX;
1095 return AFSCONF_NOTFOUND; /* service not found */
1097 for (i = 0; i < acellInfo->numServers; i++) {
1098 acellInfo->hostAddr[i].sin_port = tservice;
1101 acellInfo->timeout = 0;
1102 UNLOCK_GLOBAL_MUTEX;
1105 UNLOCK_GLOBAL_MUTEX;
1106 #ifdef AFS_AFSDB_ENV
1107 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1109 return AFSCONF_NOTFOUND;
1110 #endif /* AFS_AFSDB_ENV */
1115 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1118 static int afsconf_showcell = 0;
1124 * If a cell switch was specified in a command, then it should override the
1125 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1126 * is set and the cell name in the adir structure is used.
1127 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1129 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1130 if (!afsconf_showcell) {
1131 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1133 afsconf_showcell = 1;
1135 strncpy(aname, afscell_path, alen);
1137 afsconf_Check(adir);
1138 if (adir->cellName) {
1139 strncpy(aname, adir->cellName, alen);
1141 code = AFSCONF_UNKNOWN;
1144 UNLOCK_GLOBAL_MUTEX;
1149 afsconf_Close(struct afsconf_dir *adir)
1152 afsconf_CloseInternal(adir);
1156 UNLOCK_GLOBAL_MUTEX;
1161 afsconf_CloseInternal(register struct afsconf_dir *adir)
1163 register struct afsconf_entry *td, *nd;
1164 struct afsconf_aliasentry *ta, *na;
1165 register char *tname;
1167 tname = adir->name; /* remember name, since that's all we preserve */
1169 /* free everything we can find */
1171 free(adir->cellName);
1172 for (td = adir->entries; td; td = nd) {
1174 if (td->cellInfo.linkedCell)
1175 free(td->cellInfo.linkedCell);
1178 for (ta = adir->alias_entries; ta; ta = na) {
1186 memset(adir, 0, sizeof(struct afsconf_dir));
1187 adir->name = tname; /* restore it */
1192 afsconf_Reopen(register struct afsconf_dir *adir)
1194 register afs_int32 code;
1195 code = afsconf_CloseInternal(adir);
1198 code = afsconf_OpenInternal(adir, 0, 0);
1202 /* called during opening of config file */
1204 afsconf_IntGetKeys(struct afsconf_dir *adir)
1208 struct afsconf_keys *tstr;
1209 register afs_int32 code;
1212 /* NT client config dir has no KeyFile; don't risk attempting open
1213 * because there might be a random file of this name if dir is shared.
1215 if (IsClientConfigDirectory(adir->name)) {
1216 adir->keystr = ((struct afsconf_keys *)
1217 malloc(sizeof(struct afsconf_keys)));
1218 adir->keystr->nkeys = 0;
1221 #endif /* AFS_NT40_ENV */
1224 /* compute the key name and other setup */
1225 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1226 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1227 adir->keystr = tstr;
1230 fd = open(tbuffer, O_RDONLY);
1233 UNLOCK_GLOBAL_MUTEX;
1236 code = read(fd, tstr, sizeof(struct afsconf_keys));
1238 if (code < sizeof(afs_int32)) {
1240 UNLOCK_GLOBAL_MUTEX;
1244 /* convert key structure to host order */
1245 tstr->nkeys = ntohl(tstr->nkeys);
1247 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1249 UNLOCK_GLOBAL_MUTEX;
1253 for (fd = 0; fd < tstr->nkeys; fd++)
1254 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1256 UNLOCK_GLOBAL_MUTEX;
1260 /* get keys structure */
1262 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1264 register afs_int32 code;
1267 code = afsconf_Check(adir);
1269 UNLOCK_GLOBAL_MUTEX;
1270 return AFSCONF_FAILURE;
1272 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1273 UNLOCK_GLOBAL_MUTEX;
1277 /* get latest key */
1279 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1283 register struct afsconf_key *tk;
1284 register afs_int32 best;
1285 struct afsconf_key *bestk;
1286 register afs_int32 code;
1289 code = afsconf_Check(adir);
1291 UNLOCK_GLOBAL_MUTEX;
1292 return AFSCONF_FAILURE;
1294 maxa = adir->keystr->nkeys;
1296 best = -1; /* highest kvno we've seen yet */
1297 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1298 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1299 if (tk->kvno == 999)
1300 continue; /* skip bcrypt keys */
1301 if (tk->kvno > best) {
1306 if (bestk) { /* found any */
1308 memcpy(akey, bestk->key, 8); /* copy out latest key */
1310 *avno = bestk->kvno; /* and kvno to caller */
1311 UNLOCK_GLOBAL_MUTEX;
1314 UNLOCK_GLOBAL_MUTEX;
1315 return AFSCONF_NOTFOUND; /* didn't find any keys */
1318 /* get a particular key */
1320 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1322 register int i, maxa;
1323 register struct afsconf_key *tk;
1324 register afs_int32 code;
1327 code = afsconf_Check(adir);
1329 UNLOCK_GLOBAL_MUTEX;
1330 return AFSCONF_FAILURE;
1332 maxa = adir->keystr->nkeys;
1334 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1335 if (tk->kvno == avno) {
1336 memcpy(akey, tk->key, 8);
1337 UNLOCK_GLOBAL_MUTEX;
1342 UNLOCK_GLOBAL_MUTEX;
1343 return AFSCONF_NOTFOUND;
1346 /* save the key structure in the appropriate file */
1348 SaveKeys(struct afsconf_dir *adir)
1350 struct afsconf_keys tkeys;
1352 register afs_int32 i;
1355 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1357 /* convert it to net byte order */
1358 for (i = 0; i < tkeys.nkeys; i++)
1359 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1360 tkeys.nkeys = htonl(tkeys.nkeys);
1362 /* rewrite keys file */
1363 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1364 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1366 return AFSCONF_FAILURE;
1367 i = write(fd, &tkeys, sizeof(tkeys));
1368 if (i != sizeof(tkeys)) {
1370 return AFSCONF_FAILURE;
1373 return AFSCONF_FAILURE;
1378 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1379 afs_int32 overwrite)
1381 register struct afsconf_keys *tk;
1382 register struct afsconf_key *tkey;
1383 register afs_int32 i;
1390 if (akvno < 0 || akvno > 255) {
1391 UNLOCK_GLOBAL_MUTEX;
1396 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1397 if (tkey->kvno == akvno) {
1399 UNLOCK_GLOBAL_MUTEX;
1400 return AFSCONF_KEYINUSE;
1407 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1408 UNLOCK_GLOBAL_MUTEX;
1409 return AFSCONF_FULL;
1411 tkey = &tk->key[tk->nkeys++];
1414 memcpy(tkey->key, akey, 8);
1416 afsconf_Touch(adir);
1417 UNLOCK_GLOBAL_MUTEX;
1421 /* this proc works by sliding the other guys down, rather than using a funny
1422 kvno value, so that callers can count on getting a good key in key[0].
1425 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1427 register struct afsconf_keys *tk;
1428 register struct afsconf_key *tkey;
1435 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1436 if (tkey->kvno == akvno) {
1442 UNLOCK_GLOBAL_MUTEX;
1443 return AFSCONF_NOTFOUND;
1446 /* otherwise slide the others down. i and tkey point at the guy to delete */
1447 for (; i < tk->nkeys - 1; i++, tkey++) {
1448 tkey->kvno = (tkey + 1)->kvno;
1449 memcpy(tkey->key, (tkey + 1)->key, 8);
1453 afsconf_Touch(adir);
1454 UNLOCK_GLOBAL_MUTEX;