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 = 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 = 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 = 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];
743 char realCellName[256];
748 /* The resolver isn't always MT-safe.. Perhaps this ought to be
749 * replaced with a more fine-grained lock just for the resolver
753 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
756 return AFSCONF_NOTFOUND;
758 p = answer + sizeof(HEADER); /* Skip header */
759 code = dn_expand(answer, answer + len, p, host, sizeof(host));
761 return AFSCONF_NOTFOUND;
763 p += code + QFIXEDSZ; /* Skip name */
765 while (p < answer + len) {
768 code = dn_expand(answer, answer + len, p, host, sizeof(host));
770 return AFSCONF_NOTFOUND;
772 p += code; /* Skip the name */
773 type = (p[0] << 8) | p[1];
774 p += 4; /* Skip type and class */
775 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
776 p += 4; /* Skip the TTL */
777 size = (p[0] << 8) | p[1];
778 p += 2; /* Skip the size */
780 if (type == T_AFSDB) {
784 afsdb_type = (p[0] << 8) | p[1];
785 if (afsdb_type == 1) {
787 * We know this is an AFSDB record for our cell, of the
788 * right AFSDB type. Write down the true cell name that
789 * the resolver gave us above.
791 strcpy(realCellName, host);
794 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
796 return AFSCONF_NOTFOUND;
798 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
799 /* Do we want to get TTL data for the A record as well? */
800 (he = gethostbyname(host))) {
802 memcpy(&ipaddr, he->h_addr, he->h_length);
803 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
804 strncpy(acellInfo->hostName[server_num], host,
805 sizeof(acellInfo->hostName[server_num]));
808 if (!minttl || ttl < minttl)
816 if (server_num == 0) /* No AFSDB records */
817 return AFSCONF_NOTFOUND;
819 /* Convert the real cell name to lowercase */
820 for (p = (unsigned char *)realCellName; *p; p++)
823 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
824 acellInfo->numServers = server_num;
827 tservice = afsconf_FindService(aservice);
829 return AFSCONF_NOTFOUND; /* service not found */
830 for (i = 0; i < acellInfo->numServers; i++) {
831 acellInfo->hostAddr[i].sin_port = tservice;
835 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
841 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
842 struct afsconf_cell *acellInfo)
844 register afs_int32 i;
846 struct afsconf_entry DNSce;
847 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
848 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
853 DNSce.cellInfo.numServers = 0;
855 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
857 /* ignore the ttl here since this code is only called by transitory programs
864 for (i = 0; i < numServers; i++) {
865 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
867 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
868 acellInfo->hostAddr[i].sin_family = AF_INET;
870 /* sin_port supplied by connection code */
873 acellInfo->numServers = numServers;
874 strcpy(acellInfo->name, acellName);
877 tservice = afsconf_FindService(aservice);
880 return AFSCONF_NOTFOUND; /* service not found */
882 for (i = 0; i < acellInfo->numServers; i++) {
883 acellInfo->hostAddr[i].sin_port = tservice;
886 acellInfo->linkedCell = NULL; /* no linked cell */
887 acellInfo->flags = 0;
891 #endif /* AFS_AFSDB_ENV */
894 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
895 struct afsconf_cell *acellInfo)
897 register struct afsconf_entry *tce;
898 struct afsconf_aliasentry *tcae;
899 struct afsconf_entry *bestce;
900 register afs_int32 i;
912 cnLen = strlen(tcell) + 1;
913 lcstring(tcell, tcell, cnLen);
914 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
915 /* call to afsconf_GetLocalCell: like klog */
917 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
924 cnLen = strlen(tcell);
925 bestce = (struct afsconf_entry *)0;
932 /* Look through the list of aliases */
933 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
934 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
935 tcell = tcae->aliasInfo.realName;
940 for (tce = adir->entries; tce; tce = tce->next) {
941 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
947 if (strlen(tce->cellInfo.name) < cnLen)
948 continue; /* clearly wrong */
949 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
951 ambig = 1; /* ambiguous unless we get exact match */
955 if (!ambig && bestce && bestce->cellInfo.numServers) {
956 *acellInfo = bestce->cellInfo; /* structure assignment */
958 tservice = afsconf_FindService(aservice);
961 return AFSCONF_NOTFOUND; /* service not found */
963 for (i = 0; i < acellInfo->numServers; i++) {
964 acellInfo->hostAddr[i].sin_port = tservice;
967 acellInfo->timeout = 0;
973 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
975 return AFSCONF_NOTFOUND;
976 #endif /* AFS_AFSDB_ENV */
981 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
984 static int afsconf_showcell = 0;
990 * If a cell switch was specified in a command, then it should override the
991 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
992 * is set and the cell name in the adir structure is used.
993 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
995 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
996 if (!afsconf_showcell) {
997 fprintf(stderr, "Note: Operation is performed on cell %s\n",
999 afsconf_showcell = 1;
1001 strncpy(aname, afscell_path, alen);
1003 afsconf_Check(adir);
1004 if (adir->cellName) {
1005 strncpy(aname, adir->cellName, alen);
1007 code = AFSCONF_UNKNOWN;
1010 UNLOCK_GLOBAL_MUTEX;
1015 afsconf_Close(struct afsconf_dir *adir)
1018 afsconf_CloseInternal(adir);
1022 UNLOCK_GLOBAL_MUTEX;
1027 afsconf_CloseInternal(register struct afsconf_dir *adir)
1029 register struct afsconf_entry *td, *nd;
1030 struct afsconf_aliasentry *ta, *na;
1031 register char *tname;
1033 tname = adir->name; /* remember name, since that's all we preserve */
1035 /* free everything we can find */
1037 free(adir->cellName);
1038 for (td = adir->entries; td; td = nd) {
1040 if (td->cellInfo.linkedCell)
1041 free(td->cellInfo.linkedCell);
1044 for (ta = adir->alias_entries; ta; ta = na) {
1052 memset(adir, 0, sizeof(struct afsconf_dir));
1053 adir->name = tname; /* restore it */
1058 afsconf_Reopen(register struct afsconf_dir *adir)
1060 register afs_int32 code;
1061 code = afsconf_CloseInternal(adir);
1064 code = afsconf_OpenInternal(adir, 0, 0);
1068 /* called during opening of config file */
1070 afsconf_IntGetKeys(struct afsconf_dir *adir)
1074 struct afsconf_keys *tstr;
1075 register afs_int32 code;
1078 /* NT client config dir has no KeyFile; don't risk attempting open
1079 * because there might be a random file of this name if dir is shared.
1081 if (IsClientConfigDirectory(adir->name)) {
1082 adir->keystr = ((struct afsconf_keys *)
1083 malloc(sizeof(struct afsconf_keys)));
1084 adir->keystr->nkeys = 0;
1087 #endif /* AFS_NT40_ENV */
1090 /* compute the key name and other setup */
1091 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1092 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1093 adir->keystr = tstr;
1096 fd = open(tbuffer, O_RDONLY);
1099 UNLOCK_GLOBAL_MUTEX;
1102 code = read(fd, tstr, sizeof(struct afsconf_keys));
1104 if (code < sizeof(afs_int32)) {
1106 UNLOCK_GLOBAL_MUTEX;
1110 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1112 UNLOCK_GLOBAL_MUTEX;
1116 /* convert key structure to host order */
1117 tstr->nkeys = ntohl(tstr->nkeys);
1118 for (fd = 0; fd < tstr->nkeys; fd++)
1119 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1121 UNLOCK_GLOBAL_MUTEX;
1125 /* get keys structure */
1127 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1129 register afs_int32 code;
1132 code = afsconf_Check(adir);
1134 UNLOCK_GLOBAL_MUTEX;
1135 return AFSCONF_FAILURE;
1137 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1138 UNLOCK_GLOBAL_MUTEX;
1142 /* get latest key */
1144 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1148 register struct afsconf_key *tk;
1149 register afs_int32 best;
1150 struct afsconf_key *bestk;
1151 register afs_int32 code;
1154 code = afsconf_Check(adir);
1156 UNLOCK_GLOBAL_MUTEX;
1157 return AFSCONF_FAILURE;
1159 maxa = adir->keystr->nkeys;
1161 best = -1; /* highest kvno we've seen yet */
1162 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1163 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1164 if (tk->kvno == 999)
1165 continue; /* skip bcrypt keys */
1166 if (tk->kvno > best) {
1171 if (bestk) { /* found any */
1173 memcpy(akey, bestk->key, 8); /* copy out latest key */
1175 *avno = bestk->kvno; /* and kvno to caller */
1176 UNLOCK_GLOBAL_MUTEX;
1179 UNLOCK_GLOBAL_MUTEX;
1180 return AFSCONF_NOTFOUND; /* didn't find any keys */
1183 /* get a particular key */
1185 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1187 register int i, maxa;
1188 register struct afsconf_key *tk;
1189 register afs_int32 code;
1192 code = afsconf_Check(adir);
1194 UNLOCK_GLOBAL_MUTEX;
1195 return AFSCONF_FAILURE;
1197 maxa = adir->keystr->nkeys;
1199 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1200 if (tk->kvno == avno) {
1201 memcpy(akey, tk->key, 8);
1202 UNLOCK_GLOBAL_MUTEX;
1207 UNLOCK_GLOBAL_MUTEX;
1208 return AFSCONF_NOTFOUND;
1211 /* save the key structure in the appropriate file */
1213 SaveKeys(struct afsconf_dir *adir)
1215 struct afsconf_keys tkeys;
1217 register afs_int32 i;
1220 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1222 /* convert it to net byte order */
1223 for (i = 0; i < tkeys.nkeys; i++)
1224 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1225 tkeys.nkeys = htonl(tkeys.nkeys);
1227 /* rewrite keys file */
1228 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1229 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1231 return AFSCONF_FAILURE;
1232 i = write(fd, &tkeys, sizeof(tkeys));
1233 if (i != sizeof(tkeys)) {
1235 return AFSCONF_FAILURE;
1238 return AFSCONF_FAILURE;
1243 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1244 afs_int32 overwrite)
1246 register struct afsconf_keys *tk;
1247 register struct afsconf_key *tkey;
1248 register afs_int32 i;
1255 if (akvno < 0 || akvno > 255) {
1256 UNLOCK_GLOBAL_MUTEX;
1261 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1262 if (tkey->kvno == akvno) {
1264 UNLOCK_GLOBAL_MUTEX;
1265 return AFSCONF_KEYINUSE;
1272 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1273 UNLOCK_GLOBAL_MUTEX;
1274 return AFSCONF_FULL;
1276 tkey = &tk->key[tk->nkeys++];
1279 memcpy(tkey->key, akey, 8);
1281 afsconf_Touch(adir);
1282 UNLOCK_GLOBAL_MUTEX;
1286 /* this proc works by sliding the other guys down, rather than using a funny
1287 kvno value, so that callers can count on getting a good key in key[0].
1290 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1292 register struct afsconf_keys *tk;
1293 register struct afsconf_key *tkey;
1300 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1301 if (tkey->kvno == akvno) {
1307 UNLOCK_GLOBAL_MUTEX;
1308 return AFSCONF_NOTFOUND;
1311 /* otherwise slide the others down. i and tkey point at the guy to delete */
1312 for (; i < tk->nkeys - 1; i++, tkey++) {
1313 tkey->kvno = (tkey + 1)->kvno;
1314 memcpy(tkey->key, (tkey + 1)->key, 8);
1318 afsconf_Touch(adir);
1319 UNLOCK_GLOBAL_MUTEX;