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>
61 #include <afs/afsutil.h>
62 #include "cellconfig.h"
66 /* cm_dns.h depends on cellconfig.h */
68 #endif /* AFS_AFSDB_ENV */
70 static struct afsconf_servPair serviceTable[] = {
81 {"afsres", 7010,}, /* residency database for MR-AFS */
82 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
83 {0, 0} /* insert new services before this spot */
87 static afs_int32 afsconf_FindService(register const char *aname);
88 static int TrimLine(char *abuffer);
90 static int IsClientConfigDirectory(const char *path);
91 static int GetCellNT(struct afsconf_dir *adir);
93 static int afsconf_Check(register struct afsconf_dir *adir);
94 static int afsconf_Touch(register struct afsconf_dir *adir);
95 static int GetCellUnix(struct afsconf_dir *adir);
96 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
98 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
99 char *aname, char *aclone);
100 static int ParseCellLine(register char *aline, register char *aname,
101 register char *alname);
102 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
103 static int afsconf_Reopen(register struct afsconf_dir *adir);
104 static int SaveKeys(struct afsconf_dir *adir);
107 #define T_AFSDB 18 /* per RFC1183 section 1 */
111 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
112 * our code can tell if there is new info in the key files, the cell server db
113 * files or any of the other files (and reopen the thing) if the date on
114 * CellServDB changes.
117 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
119 afsconf_FindService(register const char *aname)
121 /* lookup a service name */
123 register struct afsconf_servPair *tsp;
125 #if defined(AFS_OSF_ENV)
126 ts = getservbyname(aname, "");
128 ts = getservbyname(aname, NULL);
131 /* we found it in /etc/services, so we use this value */
132 return ts->s_port; /* already in network byte order */
135 /* not found in /etc/services, see if it is one of ours */
136 for (tsp = serviceTable;; tsp++) {
137 if (tsp->name == NULL)
139 if (!strcmp(tsp->name, aname))
140 return htons(tsp->port);
145 TrimLine(char *abuffer)
158 strcpy(abuffer, tbuffer);
164 * IsClientConfigDirectory() -- determine if path matches well-known
165 * client configuration directory.
168 IsClientConfigDirectory(const char *path)
170 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
173 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
174 int cc = tolower(cdir[i]);
175 int pc = tolower(path[i]);
188 /* hit end of one or both; allow mismatch in existence of trailing slash */
189 if (cdir[i] != '\0') {
190 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
194 if (path[i] != '\0') {
195 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
201 #endif /* AFS_NT40_ENV */
205 afsconf_Check(register struct afsconf_dir *adir)
207 char tbuffer[256], *p;
209 register afs_int32 code;
212 /* NT client CellServDB has different file name than NT server or Unix */
213 if (IsClientConfigDirectory(adir->name)) {
214 if (!afssw_GetClientCellServDBDir(&p)) {
215 strcompose(tbuffer, sizeof(tbuffer), p, "/",
216 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
220 strncpy(tbuffer, adir->name, sizeof(tbuffer));
221 len = (int)strlen(tbuffer);
222 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
223 strncat(tbuffer, "\\", sizeof(tbuffer));
225 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
227 tbuffer[sizeof(tbuffer) - 1] = '\0';
230 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
234 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
235 #endif /* AFS_NT40_ENV */
237 code = stat(tbuffer, &tstat);
241 /* did file change? */
242 if (tstat.st_mtime == adir->timeRead) {
245 /* otherwise file has changed, so reopen it */
246 return afsconf_Reopen(adir);
249 /* set modtime on file */
251 afsconf_Touch(register struct afsconf_dir *adir)
253 char tbuffer[256], *p;
255 struct timeval tvp[2];
258 adir->timeRead = 0; /* just in case */
261 /* NT client CellServDB has different file name than NT server or Unix */
263 if (IsClientConfigDirectory(adir->name)) {
264 if (!afssw_GetClientCellServDBDir(&p)) {
265 strcompose(tbuffer, sizeof(tbuffer), p, "/",
266 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
269 int len = (int)strlen(tbuffer);
270 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
271 strncat(tbuffer, "\\", sizeof(tbuffer));
273 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
275 tbuffer[sizeof(tbuffer) - 1] = '\0';
278 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
282 return _utime(tbuffer, NULL);
285 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
286 gettimeofday(&tvp[0], NULL);
288 return utimes(tbuffer, tvp);
289 #endif /* AFS_NT40_ENV */
293 afsconf_Open(register const char *adir)
295 register struct afsconf_dir *tdir;
296 register afs_int32 code;
299 /* zero structure and fill in name; rest is done by internal routine */
300 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
301 memset(tdir, 0, sizeof(struct afsconf_dir));
302 tdir->name = (char *)malloc(strlen(adir) + 1);
303 strcpy(tdir->name, adir);
305 code = afsconf_OpenInternal(tdir, 0, 0);
307 char *afsconf_path, afs_confdir[128];
310 /* Check global place only when local Open failed for whatever reason */
311 if (!(afsconf_path = getenv("AFSCONF"))) {
312 /* 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... */
317 if (!(home_dir = getenv("HOME"))) {
318 /* Our last chance is the "/.AFSCONF" file */
319 fp = fopen("/.AFSCONF", "r");
323 return (struct afsconf_dir *)0;
325 fgets(afs_confdir, 128, fp);
330 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
331 fp = fopen(pathname, "r");
333 /* Our last chance is the "/.AFSCONF" file */
334 fp = fopen("/.AFSCONF", "r");
338 return (struct afsconf_dir *)0;
341 fgets(afs_confdir, 128, fp);
344 len = strlen(afs_confdir);
348 return (struct afsconf_dir *)0;
350 if (afs_confdir[len - 1] == '\n') {
351 afs_confdir[len - 1] = 0;
353 afsconf_path = afs_confdir;
355 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
356 strcpy(tdir->name, afsconf_path);
357 code = afsconf_OpenInternal(tdir, 0, 0);
362 return (struct afsconf_dir *)0;
371 GetCellUnix(struct afsconf_dir *adir)
377 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
378 tf = fopen(tbuffer, "r");
380 /* FIXME: buffer overflow waiting to happen */
381 rc = fscanf(tf, "%s", tbuffer);
383 adir->cellName = (char *)malloc(strlen(tbuffer) + 1);
384 strcpy(adir->cellName, tbuffer);
396 GetCellNT(struct afsconf_dir *adir)
398 if (IsClientConfigDirectory(adir->name)) {
399 /* NT client config dir; ThisCell is in registry (no file). */
400 return afssw_GetClientCellName(&adir->cellName);
402 /* NT server config dir; works just like Unix */
403 return GetCellUnix(adir);
406 #endif /* AFS_NT40_ENV */
410 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
414 register char *tp, *bp;
415 register struct afsconf_entry *curEntry;
416 struct afsconf_aliasentry *curAlias;
417 register afs_int32 code;
419 char tbuffer[256], tbuf1[256];
422 /* figure out the cell name */
426 i = GetCellUnix(adir);
429 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
435 /* now parse the individual lines */
439 /* NT client/server have a CellServDB that is the same format as Unix.
440 * However, the NT client uses a different file name
442 if (IsClientConfigDirectory(adir->name)) {
443 /* NT client config dir */
445 if (!afssw_GetClientCellServDBDir(&p)) {
446 strcompose(tbuffer, sizeof(tbuffer), p, "/",
447 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
451 strncpy(tbuffer, adir->name, sizeof(tbuffer));
452 len = (int)strlen(tbuffer);
453 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
454 strncat(tbuffer, "\\", sizeof(tbuffer));
456 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
458 tbuffer[sizeof(tbuffer) - 1] = '\0';
461 /* NT server config dir */
462 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
466 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
467 #endif /* AFS_NT40_ENV */
469 if (!stat(tbuffer, &tstat)) {
470 adir->timeRead = tstat.st_mtime;
475 strcpy(tbuf1, tbuffer);
476 tf = fopen(tbuffer, "r");
481 tp = fgets(tbuffer, sizeof(tbuffer), tf);
484 TrimLine(tbuffer); /* remove white space */
485 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
486 continue; /* empty line */
487 if (tbuffer[0] == '>') {
488 char linkedcell[MAXCELLCHARS];
489 /* start new cell item */
491 /* thread this guy on the list */
492 curEntry->next = adir->entries;
493 adir->entries = curEntry;
497 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
498 memset(curEntry, 0, sizeof(struct afsconf_entry));
500 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
502 afsconf_CloseInternal(adir);
507 if (linkedcell[0] != '\0') {
508 curEntry->cellInfo.linkedCell =
509 (char *)malloc(strlen(linkedcell) + 1);
510 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
513 /* new host in the current cell */
515 afsconf_CloseInternal(adir);
519 i = curEntry->cellInfo.numServers;
520 if (cell && !strcmp(cell, curEntry->cellInfo.name))
522 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
523 curEntry->cellInfo.hostName[i], &clones[i]);
526 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
527 curEntry->cellInfo.hostName[i], 0);
529 if (code == AFSCONF_SYNTAX) {
530 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
536 "Can't properly parse host line \"%s\" in configuration file %s\n",
541 afsconf_CloseInternal(adir);
544 curEntry->cellInfo.numServers = ++i;
547 fclose(tf); /* close the file now */
549 /* end the last partially-completed cell */
551 curEntry->next = adir->entries;
552 adir->entries = curEntry;
555 /* Read in the alias list */
556 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
558 tf = fopen(tbuffer, "r");
562 tp = fgets(tbuffer, sizeof(tbuffer), tf);
565 TrimLine(tbuffer); /* remove white space */
567 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
568 continue; /* empty line */
571 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
574 continue; /* invalid line */
576 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
579 continue; /* invalid line */
582 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
587 curAlias = malloc(sizeof(*curAlias));
588 memset(curAlias, 0, sizeof(*curAlias));
590 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
591 strcpy(curAlias->aliasInfo.realName, tbuffer);
593 curAlias->next = adir->alias_entries;
594 adir->alias_entries = curAlias;
599 /* now read the fs keys, if possible */
600 adir->keystr = (struct afsconf_keys *)0;
601 afsconf_IntGetKeys(adir);
606 /* parse a line of the form
607 *"128.2.1.3 #hostname" or
608 *"[128.2.1.3] #hostname" for clones
609 * into the appropriate pieces.
612 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
616 register afs_int32 code;
622 /* FIXME: length of aname unknown here */
623 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
627 /* FIXME: length of aname unknown here */
628 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
631 return AFSCONF_SYNTAX;
632 addr->sin_family = AF_INET;
634 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
635 addr->sin_len = sizeof(struct sockaddr_in);
637 tp = (char *)&addr->sin_addr;
645 /* parse a line of the form
646 * ">cellname [linkedcellname] [#comments]"
647 * into the appropriate pieces.
650 ParseCellLine(register char *aline, register char *aname,
651 register char *alname)
654 /* FIXME: length of aname, alname unknown here */
655 code = sscanf(aline, ">%s %s", aname, alname);
659 if (*alname == '#') {
663 return (code > 0 ? 0 : AFSCONF_SYNTAX);
666 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
668 afsconf_CellApply(struct afsconf_dir *adir,
669 int (*aproc) (struct afsconf_cell * cell, char *arock,
670 struct afsconf_dir * dir), char *arock)
672 register struct afsconf_entry *tde;
673 register afs_int32 code;
675 for (tde = adir->entries; tde; tde = tde->next) {
676 code = (*aproc) (&tde->cellInfo, arock, adir);
686 /* call aproc(entry, arock, adir) for all cell aliases.
687 * Proc must return 0, or we'll stop early and return the code it returns
690 afsconf_CellAliasApply(struct afsconf_dir *adir,
691 int (*aproc) (struct afsconf_cellalias * alias,
692 char *arock, struct afsconf_dir * dir),
695 register struct afsconf_aliasentry *tde;
696 register afs_int32 code;
698 for (tde = adir->alias_entries; tde; tde = tde->next) {
699 code = (*aproc) (&tde->aliasInfo, arock, adir);
709 afs_int32 afsconf_SawCell = 0;
712 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
713 char *aservice, struct afsconf_cell *acellInfo,
719 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
726 cell = (char *)&acellInfo->name;
728 code = afsconf_OpenInternal(adir, cell, clones);
733 #if !defined(AFS_NT40_ENV)
735 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
736 struct afsconf_cell *acellInfo)
741 unsigned char answer[1024];
745 char realCellName[256];
750 /* The resolver isn't always MT-safe.. Perhaps this ought to be
751 * replaced with a more fine-grained lock just for the resolver
755 if ( ! strchr(acellName,'.') ) {
756 cellnamelength=strlen(acellName);
757 dotcellname=malloc(cellnamelength+2);
758 memcpy(dotcellname,acellName,cellnamelength);
759 dotcellname[cellnamelength]='.';
760 dotcellname[cellnamelength+1]=0;
762 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
764 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
770 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
774 return AFSCONF_NOTFOUND;
776 p = answer + sizeof(HEADER); /* Skip header */
777 code = dn_expand(answer, answer + len, p, host, sizeof(host));
779 return AFSCONF_NOTFOUND;
781 p += code + QFIXEDSZ; /* Skip name */
783 while (p < answer + len) {
786 code = dn_expand(answer, answer + len, p, host, sizeof(host));
788 return AFSCONF_NOTFOUND;
790 p += code; /* Skip the name */
791 type = (p[0] << 8) | p[1];
792 p += 4; /* Skip type and class */
793 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
794 p += 4; /* Skip the TTL */
795 size = (p[0] << 8) | p[1];
796 p += 2; /* Skip the size */
798 if (type == T_AFSDB) {
802 afsdb_type = (p[0] << 8) | p[1];
803 if (afsdb_type == 1) {
805 * We know this is an AFSDB record for our cell, of the
806 * right AFSDB type. Write down the true cell name that
807 * the resolver gave us above.
809 strcpy(realCellName, host);
812 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
814 return AFSCONF_NOTFOUND;
816 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
817 /* Do we want to get TTL data for the A record as well? */
818 (he = gethostbyname(host))) {
820 memcpy(&ipaddr, he->h_addr, he->h_length);
821 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
822 strncpy(acellInfo->hostName[server_num], host,
823 sizeof(acellInfo->hostName[server_num]));
826 if (!minttl || ttl < minttl)
834 if (server_num == 0) /* No AFSDB records */
835 return AFSCONF_NOTFOUND;
837 /* Convert the real cell name to lowercase */
838 for (p = (unsigned char *)realCellName; *p; p++)
841 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
842 acellInfo->numServers = server_num;
845 tservice = afsconf_FindService(aservice);
847 return AFSCONF_NOTFOUND; /* service not found */
848 for (i = 0; i < acellInfo->numServers; i++) {
849 acellInfo->hostAddr[i].sin_port = tservice;
853 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
859 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
860 struct afsconf_cell *acellInfo)
862 register afs_int32 i;
864 struct afsconf_entry DNSce;
865 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
866 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
871 DNSce.cellInfo.numServers = 0;
873 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
875 /* ignore the ttl here since this code is only called by transitory programs
882 for (i = 0; i < numServers; i++) {
883 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
885 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
886 acellInfo->hostAddr[i].sin_family = AF_INET;
888 /* sin_port supplied by connection code */
891 acellInfo->numServers = numServers;
892 strcpy(acellInfo->name, acellName);
895 tservice = afsconf_FindService(aservice);
898 return AFSCONF_NOTFOUND; /* service not found */
900 for (i = 0; i < acellInfo->numServers; i++) {
901 acellInfo->hostAddr[i].sin_port = tservice;
904 acellInfo->linkedCell = NULL; /* no linked cell */
905 acellInfo->flags = 0;
909 #endif /* AFS_AFSDB_ENV */
912 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
913 struct afsconf_cell *acellInfo)
915 register struct afsconf_entry *tce;
916 struct afsconf_aliasentry *tcae;
917 struct afsconf_entry *bestce;
918 register afs_int32 i;
930 cnLen = (int)(strlen(tcell) + 1);
931 lcstring(tcell, tcell, cnLen);
932 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
933 /* call to afsconf_GetLocalCell: like klog */
935 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
942 cnLen = strlen(tcell);
943 bestce = (struct afsconf_entry *)0;
950 /* Look through the list of aliases */
951 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
952 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
953 tcell = tcae->aliasInfo.realName;
958 for (tce = adir->entries; tce; tce = tce->next) {
959 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
965 if (strlen(tce->cellInfo.name) < cnLen)
966 continue; /* clearly wrong */
967 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
969 ambig = 1; /* ambiguous unless we get exact match */
973 if (!ambig && bestce && bestce->cellInfo.numServers) {
974 *acellInfo = bestce->cellInfo; /* structure assignment */
976 tservice = afsconf_FindService(aservice);
979 return AFSCONF_NOTFOUND; /* service not found */
981 for (i = 0; i < acellInfo->numServers; i++) {
982 acellInfo->hostAddr[i].sin_port = tservice;
985 acellInfo->timeout = 0;
991 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
993 return AFSCONF_NOTFOUND;
994 #endif /* AFS_AFSDB_ENV */
999 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1002 static int afsconf_showcell = 0;
1008 * If a cell switch was specified in a command, then it should override the
1009 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1010 * is set and the cell name in the adir structure is used.
1011 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1013 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1014 if (!afsconf_showcell) {
1015 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1017 afsconf_showcell = 1;
1019 strncpy(aname, afscell_path, alen);
1021 afsconf_Check(adir);
1022 if (adir->cellName) {
1023 strncpy(aname, adir->cellName, alen);
1025 code = AFSCONF_UNKNOWN;
1028 UNLOCK_GLOBAL_MUTEX;
1033 afsconf_Close(struct afsconf_dir *adir)
1036 afsconf_CloseInternal(adir);
1040 UNLOCK_GLOBAL_MUTEX;
1045 afsconf_CloseInternal(register struct afsconf_dir *adir)
1047 register struct afsconf_entry *td, *nd;
1048 struct afsconf_aliasentry *ta, *na;
1049 register char *tname;
1051 tname = adir->name; /* remember name, since that's all we preserve */
1053 /* free everything we can find */
1055 free(adir->cellName);
1056 for (td = adir->entries; td; td = nd) {
1058 if (td->cellInfo.linkedCell)
1059 free(td->cellInfo.linkedCell);
1062 for (ta = adir->alias_entries; ta; ta = na) {
1070 memset(adir, 0, sizeof(struct afsconf_dir));
1071 adir->name = tname; /* restore it */
1076 afsconf_Reopen(register struct afsconf_dir *adir)
1078 register afs_int32 code;
1079 code = afsconf_CloseInternal(adir);
1082 code = afsconf_OpenInternal(adir, 0, 0);
1086 /* called during opening of config file */
1088 afsconf_IntGetKeys(struct afsconf_dir *adir)
1092 struct afsconf_keys *tstr;
1093 register afs_int32 code;
1096 /* NT client config dir has no KeyFile; don't risk attempting open
1097 * because there might be a random file of this name if dir is shared.
1099 if (IsClientConfigDirectory(adir->name)) {
1100 adir->keystr = ((struct afsconf_keys *)
1101 malloc(sizeof(struct afsconf_keys)));
1102 adir->keystr->nkeys = 0;
1105 #endif /* AFS_NT40_ENV */
1108 /* compute the key name and other setup */
1109 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1110 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1111 adir->keystr = tstr;
1114 fd = open(tbuffer, O_RDONLY);
1117 UNLOCK_GLOBAL_MUTEX;
1120 code = read(fd, tstr, sizeof(struct afsconf_keys));
1122 if (code < sizeof(afs_int32)) {
1124 UNLOCK_GLOBAL_MUTEX;
1128 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1130 UNLOCK_GLOBAL_MUTEX;
1134 /* convert key structure to host order */
1135 tstr->nkeys = ntohl(tstr->nkeys);
1136 for (fd = 0; fd < tstr->nkeys; fd++)
1137 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1139 UNLOCK_GLOBAL_MUTEX;
1143 /* get keys structure */
1145 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1147 register afs_int32 code;
1150 code = afsconf_Check(adir);
1152 UNLOCK_GLOBAL_MUTEX;
1153 return AFSCONF_FAILURE;
1155 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1156 UNLOCK_GLOBAL_MUTEX;
1160 /* get latest key */
1162 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1166 register struct afsconf_key *tk;
1167 register afs_int32 best;
1168 struct afsconf_key *bestk;
1169 register afs_int32 code;
1172 code = afsconf_Check(adir);
1174 UNLOCK_GLOBAL_MUTEX;
1175 return AFSCONF_FAILURE;
1177 maxa = adir->keystr->nkeys;
1179 best = -1; /* highest kvno we've seen yet */
1180 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1181 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1182 if (tk->kvno == 999)
1183 continue; /* skip bcrypt keys */
1184 if (tk->kvno > best) {
1189 if (bestk) { /* found any */
1191 memcpy(akey, bestk->key, 8); /* copy out latest key */
1193 *avno = bestk->kvno; /* and kvno to caller */
1194 UNLOCK_GLOBAL_MUTEX;
1197 UNLOCK_GLOBAL_MUTEX;
1198 return AFSCONF_NOTFOUND; /* didn't find any keys */
1201 /* get a particular key */
1203 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1205 register int i, maxa;
1206 register struct afsconf_key *tk;
1207 register afs_int32 code;
1210 code = afsconf_Check(adir);
1212 UNLOCK_GLOBAL_MUTEX;
1213 return AFSCONF_FAILURE;
1215 maxa = adir->keystr->nkeys;
1217 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1218 if (tk->kvno == avno) {
1219 memcpy(akey, tk->key, 8);
1220 UNLOCK_GLOBAL_MUTEX;
1225 UNLOCK_GLOBAL_MUTEX;
1226 return AFSCONF_NOTFOUND;
1229 /* save the key structure in the appropriate file */
1231 SaveKeys(struct afsconf_dir *adir)
1233 struct afsconf_keys tkeys;
1235 register afs_int32 i;
1238 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1240 /* convert it to net byte order */
1241 for (i = 0; i < tkeys.nkeys; i++)
1242 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1243 tkeys.nkeys = htonl(tkeys.nkeys);
1245 /* rewrite keys file */
1246 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1247 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1249 return AFSCONF_FAILURE;
1250 i = write(fd, &tkeys, sizeof(tkeys));
1251 if (i != sizeof(tkeys)) {
1253 return AFSCONF_FAILURE;
1256 return AFSCONF_FAILURE;
1261 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1262 afs_int32 overwrite)
1264 register struct afsconf_keys *tk;
1265 register struct afsconf_key *tkey;
1266 register afs_int32 i;
1273 if (akvno < 0 || akvno > 255) {
1274 UNLOCK_GLOBAL_MUTEX;
1279 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1280 if (tkey->kvno == akvno) {
1282 UNLOCK_GLOBAL_MUTEX;
1283 return AFSCONF_KEYINUSE;
1290 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1291 UNLOCK_GLOBAL_MUTEX;
1292 return AFSCONF_FULL;
1294 tkey = &tk->key[tk->nkeys++];
1297 memcpy(tkey->key, akey, 8);
1299 afsconf_Touch(adir);
1300 UNLOCK_GLOBAL_MUTEX;
1304 /* this proc works by sliding the other guys down, rather than using a funny
1305 kvno value, so that callers can count on getting a good key in key[0].
1308 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1310 register struct afsconf_keys *tk;
1311 register struct afsconf_key *tkey;
1318 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1319 if (tkey->kvno == akvno) {
1325 UNLOCK_GLOBAL_MUTEX;
1326 return AFSCONF_NOTFOUND;
1329 /* otherwise slide the others down. i and tkey point at the guy to delete */
1330 for (; i < tk->nkeys - 1; i++, tkey++) {
1331 tkey->kvno = (tkey + 1)->kvno;
1332 memcpy(tkey->key, (tkey + 1)->key, 8);
1336 afsconf_Touch(adir);
1337 UNLOCK_GLOBAL_MUTEX;