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>
30 #endif /* AFS_AFSDB_ENV */
32 #include <sys/socket.h>
33 #include <netinet/in.h>
38 #include <arpa/nameser.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"
65 static struct afsconf_servPair serviceTable[] = {
76 {"afsres", 7010,}, /* residency database for MR-AFS */
77 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
78 {0, 0} /* insert new services before this spot */
82 static afs_int32 afsconf_FindService(register const char *aname);
83 static int TrimLine(char *abuffer);
85 static int IsClientConfigDirectory(const char *path);
86 static int GetCellNT(struct afsconf_dir *adir);
88 static int afsconf_Check(register struct afsconf_dir *adir);
89 static int afsconf_Touch(register struct afsconf_dir *adir);
90 static int GetCellUnix(struct afsconf_dir *adir);
91 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
93 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
94 char *aname, char *aclone);
95 static int ParseCellLine(register char *aline, register char *aname,
96 register char *alname);
97 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
98 static int afsconf_Reopen(register struct afsconf_dir *adir);
99 static int SaveKeys(struct afsconf_dir *adir);
102 #define T_AFSDB 18 /* per RFC1183 section 1 */
106 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
107 * our code can tell if there is new info in the key files, the cell server db
108 * files or any of the other files (and reopen the thing) if the date on
109 * CellServDB changes.
112 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
114 afsconf_FindService(register const char *aname)
116 /* lookup a service name */
118 register struct afsconf_servPair *tsp;
120 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
121 ts = getservbyname(aname, "");
123 ts = getservbyname(aname, NULL);
126 /* we found it in /etc/services, so we use this value */
127 return ts->s_port; /* already in network byte order */
130 /* not found in /etc/services, see if it is one of ours */
131 for (tsp = serviceTable;; tsp++) {
132 if (tsp->name == NULL)
134 if (!strcmp(tsp->name, aname))
135 return htons(tsp->port);
140 TrimLine(char *abuffer)
153 strcpy(abuffer, tbuffer);
159 * IsClientConfigDirectory() -- determine if path matches well-known
160 * client configuration directory.
163 IsClientConfigDirectory(const char *path)
165 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
168 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
169 int cc = tolower(cdir[i]);
170 int pc = tolower(path[i]);
183 /* hit end of one or both; allow mismatch in existence of trailing slash */
184 if (cdir[i] != '\0') {
185 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
189 if (path[i] != '\0') {
190 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
196 #endif /* AFS_NT40_ENV */
200 afsconf_Check(register struct afsconf_dir *adir)
204 register afs_int32 code;
207 /* NT client CellServDB has different file name than NT server or Unix */
208 if (IsClientConfigDirectory(adir->name)) {
209 strcompose(tbuffer, 256, adir->name, "/",
210 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
212 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
216 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
217 #endif /* AFS_NT40_ENV */
219 code = stat(tbuffer, &tstat);
223 /* did file change? */
224 if (tstat.st_mtime == adir->timeRead) {
227 /* otherwise file has changed, so reopen it */
228 return afsconf_Reopen(adir);
231 /* set modtime on file */
233 afsconf_Touch(register struct afsconf_dir *adir)
237 struct timeval tvp[2];
240 adir->timeRead = 0; /* just in case */
243 /* NT client CellServDB has different file name than NT server or Unix */
245 if (IsClientConfigDirectory(adir->name)) {
246 strcompose(tbuffer, 256, adir->name, "/",
247 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
249 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
253 return _utime(tbuffer, NULL);
256 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
257 gettimeofday(&tvp[0], NULL);
259 return utimes(tbuffer, tvp);
260 #endif /* AFS_NT40_ENV */
264 afsconf_Open(register const char *adir)
266 register struct afsconf_dir *tdir;
267 register afs_int32 code;
270 /* zero structure and fill in name; rest is done by internal routine */
271 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
272 memset(tdir, 0, sizeof(struct afsconf_dir));
273 tdir->name = (char *)malloc(strlen(adir) + 1);
274 strcpy(tdir->name, adir);
276 code = afsconf_OpenInternal(tdir, 0, 0);
278 char *afsconf_path, afs_confdir[128];
281 /* Check global place only when local Open failed for whatever reason */
282 if (!(afsconf_path = getenv("AFSCONF"))) {
283 /* 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... */
288 if (!(home_dir = getenv("HOME"))) {
289 /* Our last chance is the "/.AFSCONF" file */
290 fp = fopen("/.AFSCONF", "r");
293 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
295 fgets(afs_confdir, 128, fp);
300 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
301 fp = fopen(pathname, "r");
303 /* Our last chance is the "/.AFSCONF" file */
304 fp = fopen("/.AFSCONF", "r");
307 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
310 fgets(afs_confdir, 128, fp);
313 len = strlen(afs_confdir);
316 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
318 if (afs_confdir[len - 1] == '\n') {
319 afs_confdir[len - 1] = 0;
321 afsconf_path = afs_confdir;
323 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
324 strcpy(tdir->name, afsconf_path);
325 code = afsconf_OpenInternal(tdir, 0, 0);
329 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
332 UNLOCK_GLOBAL_MUTEX return tdir;
337 GetCellUnix(struct afsconf_dir *adir)
343 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
344 tf = fopen(tbuffer, "r");
346 /* FIXME: buffer overflow waiting to happen */
347 rc = fscanf(tf, "%s", tbuffer);
349 adir->cellName = (char *)malloc(strlen(tbuffer) + 1);
350 strcpy(adir->cellName, tbuffer);
362 GetCellNT(struct afsconf_dir *adir)
364 if (IsClientConfigDirectory(adir->name)) {
365 /* NT client config dir; ThisCell is in registry (no file). */
366 return afssw_GetClientCellName(&adir->cellName);
368 /* NT server config dir; works just like Unix */
369 return GetCellUnix(adir);
372 #endif /* AFS_NT40_ENV */
376 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
380 register char *tp, *bp;
381 register struct afsconf_entry *curEntry;
382 struct afsconf_aliasentry *curAlias;
383 register afs_int32 code;
385 char tbuffer[256], tbuf1[256];
388 /* figure out the cell name */
392 i = GetCellUnix(adir);
395 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
401 /* now parse the individual lines */
405 /* NT client/server have a CellServDB that is the same format as Unix.
406 * However, the NT client uses a different file name
408 if (IsClientConfigDirectory(adir->name)) {
409 /* NT client config dir */
410 strcompose(tbuffer, 256, adir->name, "/",
411 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
413 /* NT server config dir */
414 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
418 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
419 #endif /* AFS_NT40_ENV */
421 if (!stat(tbuffer, &tstat)) {
422 adir->timeRead = tstat.st_mtime;
427 strcpy(tbuf1, tbuffer);
428 tf = fopen(tbuffer, "r");
433 tp = fgets(tbuffer, sizeof(tbuffer), tf);
436 TrimLine(tbuffer); /* remove white space */
437 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
438 continue; /* empty line */
439 if (tbuffer[0] == '>') {
440 char linkedcell[MAXCELLCHARS];
441 /* start new cell item */
443 /* thread this guy on the list */
444 curEntry->next = adir->entries;
445 adir->entries = curEntry;
449 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
450 memset(curEntry, 0, sizeof(struct afsconf_entry));
452 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
454 afsconf_CloseInternal(adir);
459 if (linkedcell[0] != '\0') {
460 curEntry->cellInfo.linkedCell =
461 (char *)malloc(strlen(linkedcell) + 1);
462 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
465 /* new host in the current cell */
467 afsconf_CloseInternal(adir);
471 i = curEntry->cellInfo.numServers;
472 if (cell && !strcmp(cell, curEntry->cellInfo.name))
474 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
475 curEntry->cellInfo.hostName[i], &clones[i]);
478 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
479 curEntry->cellInfo.hostName[i], 0);
481 if (code == AFSCONF_SYNTAX) {
482 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
488 "Can't properly parse host line \"%s\" in configuration file %s\n",
493 afsconf_CloseInternal(adir);
496 curEntry->cellInfo.numServers = ++i;
499 fclose(tf); /* close the file now */
501 /* end the last partially-completed cell */
503 curEntry->next = adir->entries;
504 adir->entries = curEntry;
507 /* Read in the alias list */
508 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
510 tf = fopen(tbuffer, "r");
514 tp = fgets(tbuffer, sizeof(tbuffer), tf);
517 TrimLine(tbuffer); /* remove white space */
519 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
520 continue; /* empty line */
523 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
526 continue; /* invalid line */
528 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
531 continue; /* invalid line */
534 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
539 curAlias = malloc(sizeof(*curAlias));
540 memset(curAlias, 0, sizeof(*curAlias));
542 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
543 strcpy(curAlias->aliasInfo.realName, tbuffer);
545 curAlias->next = adir->alias_entries;
546 adir->alias_entries = curAlias;
551 /* now read the fs keys, if possible */
552 adir->keystr = (struct afsconf_keys *)0;
553 afsconf_IntGetKeys(adir);
558 /* parse a line of the form
559 *"128.2.1.3 #hostname" or
560 *"[128.2.1.3] #hostname" for clones
561 * into the appropriate pieces.
564 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
568 register afs_int32 code;
574 /* FIXME: length of aname unknown here */
575 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
579 /* FIXME: length of aname unknown here */
580 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
583 return AFSCONF_SYNTAX;
584 addr->sin_family = AF_INET;
586 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
587 addr->sin_len = sizeof(struct sockaddr_in);
589 tp = (char *)&addr->sin_addr;
597 /* parse a line of the form
598 * ">cellname [linkedcellname] [#comments]"
599 * into the appropriate pieces.
602 ParseCellLine(register char *aline, register char *aname,
603 register char *alname)
606 /* FIXME: length of aname, alname unknown here */
607 code = sscanf(aline, ">%s %s", aname, alname);
611 if (*alname == '#') {
615 return (code > 0 ? 0 : AFSCONF_SYNTAX);
618 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
620 afsconf_CellApply(struct afsconf_dir *adir,
621 int (*aproc) (struct afsconf_cell * cell, char *arock,
622 struct afsconf_dir * dir), char *arock)
624 register struct afsconf_entry *tde;
625 register afs_int32 code;
626 LOCK_GLOBAL_MUTEX for (tde = adir->entries; tde; tde = tde->next) {
627 code = (*aproc) (&tde->cellInfo, arock, adir);
629 UNLOCK_GLOBAL_MUTEX return code;
632 UNLOCK_GLOBAL_MUTEX return 0;
635 /* call aproc(entry, arock, adir) for all cell aliases.
636 * Proc must return 0, or we'll stop early and return the code it returns
639 afsconf_CellAliasApply(struct afsconf_dir *adir,
640 int (*aproc) (struct afsconf_cellalias * alias,
641 char *arock, struct afsconf_dir * dir),
644 register struct afsconf_aliasentry *tde;
645 register afs_int32 code;
646 LOCK_GLOBAL_MUTEX for (tde = adir->alias_entries; tde; tde = tde->next) {
647 code = (*aproc) (&tde->aliasInfo, arock, adir);
649 UNLOCK_GLOBAL_MUTEX return code;
652 UNLOCK_GLOBAL_MUTEX return 0;
655 afs_int32 afsconf_SawCell = 0;
658 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
659 char *aservice, struct afsconf_cell *acellInfo,
665 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
672 cell = (char *)&acellInfo->name;
674 code = afsconf_OpenInternal(adir, cell, clones);
679 #if !defined(AFS_NT40_ENV)
681 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
682 struct afsconf_cell *acellInfo)
687 unsigned char answer[1024];
689 char realCellName[256];
694 /* The resolver isn't always MT-safe.. Perhaps this ought to be
695 * replaced with a more fine-grained lock just for the resolver
698 LOCK_GLOBAL_MUTEX len =
699 res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
700 UNLOCK_GLOBAL_MUTEX if (len < 0)
701 return AFSCONF_NOTFOUND;
703 p = answer + sizeof(HEADER); /* Skip header */
704 code = dn_expand(answer, answer + len, p, host, sizeof(host));
706 return AFSCONF_NOTFOUND;
708 p += code + QFIXEDSZ; /* Skip name */
710 while (p < answer + len) {
713 code = dn_expand(answer, answer + len, p, host, sizeof(host));
715 return AFSCONF_NOTFOUND;
717 p += code; /* Skip the name */
718 type = (p[0] << 8) | p[1];
719 p += 4; /* Skip type and class */
720 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
721 p += 4; /* Skip the TTL */
722 size = (p[0] << 8) | p[1];
723 p += 2; /* Skip the size */
725 if (type == T_AFSDB) {
729 afsdb_type = (p[0] << 8) | p[1];
730 if (afsdb_type == 1) {
732 * We know this is an AFSDB record for our cell, of the
733 * right AFSDB type. Write down the true cell name that
734 * the resolver gave us above.
736 strcpy(realCellName, host);
739 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
741 return AFSCONF_NOTFOUND;
743 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
744 /* Do we want to get TTL data for the A record as well? */
745 (he = gethostbyname(host))) {
747 memcpy(&ipaddr, he->h_addr, he->h_length);
748 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
749 strncpy(acellInfo->hostName[server_num], host,
750 sizeof(acellInfo->hostName[server_num]));
753 if (!minttl || ttl < minttl)
761 if (server_num == 0) /* No AFSDB records */
762 return AFSCONF_NOTFOUND;
764 /* Convert the real cell name to lowercase */
765 for (p = (unsigned char *)realCellName; *p; p++)
768 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
769 acellInfo->numServers = server_num;
772 tservice = afsconf_FindService(aservice);
774 return AFSCONF_NOTFOUND; /* service not found */
775 for (i = 0; i < acellInfo->numServers; i++) {
776 acellInfo->hostAddr[i].sin_port = tservice;
780 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
786 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
787 struct afsconf_cell *acellInfo)
789 register afs_int32 i;
791 struct afsconf_entry DNSce;
792 afs_int32 cellHosts[AFSMAXCELLHOSTS];
797 DNSce.cellInfo.numServers = 0;
799 rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
800 /* ignore the ttl here since this code is only called by transitory programs
807 for (i = 0; i < numServers; i++) {
808 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i],
810 acellInfo->hostAddr[i].sin_family = AF_INET;
812 /* sin_port supplied by connection code */
815 acellInfo->numServers = numServers;
816 strcpy(acellInfo->name, acellName);
818 LOCK_GLOBAL_MUTEX tservice = afsconf_FindService(aservice);
819 UNLOCK_GLOBAL_MUTEX if (tservice < 0) {
820 return AFSCONF_NOTFOUND; /* service not found */
822 for (i = 0; i < acellInfo->numServers; i++) {
823 acellInfo->hostAddr[i].sin_port = tservice;
826 acellInfo->linkedCell = NULL; /* no linked cell */
827 acellInfo->flags = 0;
831 #endif /* AFS_AFSDB_ENV */
834 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
835 struct afsconf_cell *acellInfo)
837 register struct afsconf_entry *tce;
838 struct afsconf_aliasentry *tcae;
839 struct afsconf_entry *bestce;
840 register afs_int32 i;
847 LOCK_GLOBAL_MUTEX if (adir)
851 cnLen = strlen(tcell) + 1;
852 lcstring(tcell, tcell, cnLen);
853 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
854 /* call to afsconf_GetLocalCell: like klog */
856 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
858 UNLOCK_GLOBAL_MUTEX return i;
862 cnLen = strlen(tcell);
863 bestce = (struct afsconf_entry *)0;
866 UNLOCK_GLOBAL_MUTEX return 0;
869 /* Look through the list of aliases */
870 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
871 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
872 tcell = tcae->aliasInfo.realName;
877 for (tce = adir->entries; tce; tce = tce->next) {
878 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
884 if (strlen(tce->cellInfo.name) < cnLen)
885 continue; /* clearly wrong */
886 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
888 ambig = 1; /* ambiguous unless we get exact match */
892 if (!ambig && bestce && bestce->cellInfo.numServers) {
893 *acellInfo = bestce->cellInfo; /* structure assignment */
895 tservice = afsconf_FindService(aservice);
897 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* service not found */
899 for (i = 0; i < acellInfo->numServers; i++) {
900 acellInfo->hostAddr[i].sin_port = tservice;
903 acellInfo->timeout = 0;
904 UNLOCK_GLOBAL_MUTEX return 0;
908 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
910 return AFSCONF_NOTFOUND;
911 #endif /* AFS_AFSDB_ENV */
916 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
919 static int afsconf_showcell = 0;
925 * If a cell switch was specified in a command, then it should override the
926 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
927 * is set and the cell name in the adir structure is used.
928 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
930 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
931 if (!afsconf_showcell) {
932 fprintf(stderr, "Note: Operation is performed on cell %s\n",
934 afsconf_showcell = 1;
936 strncpy(aname, afscell_path, alen);
939 if (adir->cellName) {
940 strncpy(aname, adir->cellName, alen);
942 code = AFSCONF_UNKNOWN;
945 UNLOCK_GLOBAL_MUTEX return (code);
949 afsconf_Close(struct afsconf_dir *adir)
951 LOCK_GLOBAL_MUTEX afsconf_CloseInternal(adir);
955 UNLOCK_GLOBAL_MUTEX return 0;
959 afsconf_CloseInternal(register struct afsconf_dir *adir)
961 register struct afsconf_entry *td, *nd;
962 struct afsconf_aliasentry *ta, *na;
963 register char *tname;
965 tname = adir->name; /* remember name, since that's all we preserve */
967 /* free everything we can find */
969 free(adir->cellName);
970 for (td = adir->entries; td; td = nd) {
972 if (td->cellInfo.linkedCell)
973 free(td->cellInfo.linkedCell);
976 for (ta = adir->alias_entries; ta; ta = na) {
984 memset(adir, 0, sizeof(struct afsconf_dir));
985 adir->name = tname; /* restore it */
990 afsconf_Reopen(register struct afsconf_dir *adir)
992 register afs_int32 code;
993 code = afsconf_CloseInternal(adir);
996 code = afsconf_OpenInternal(adir, 0, 0);
1000 /* called during opening of config file */
1002 afsconf_IntGetKeys(struct afsconf_dir *adir)
1006 struct afsconf_keys *tstr;
1007 register afs_int32 code;
1010 /* NT client config dir has no KeyFile; don't risk attempting open
1011 * because there might be a random file of this name if dir is shared.
1013 if (IsClientConfigDirectory(adir->name)) {
1014 adir->keystr = ((struct afsconf_keys *)
1015 malloc(sizeof(struct afsconf_keys)));
1016 adir->keystr->nkeys = 0;
1019 #endif /* AFS_NT40_ENV */
1022 /* compute the key name and other setup */
1023 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1024 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1025 adir->keystr = tstr;
1028 fd = open(tbuffer, O_RDONLY);
1031 UNLOCK_GLOBAL_MUTEX return 0;
1033 code = read(fd, tstr, sizeof(struct afsconf_keys));
1035 if (code < sizeof(afs_int32)) {
1037 UNLOCK_GLOBAL_MUTEX return 0;
1040 /* convert key structure to host order */
1041 tstr->nkeys = ntohl(tstr->nkeys);
1042 for (fd = 0; fd < tstr->nkeys; fd++)
1043 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1045 UNLOCK_GLOBAL_MUTEX return 0;
1048 /* get keys structure */
1050 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1052 register afs_int32 code;
1054 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1056 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1058 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1059 UNLOCK_GLOBAL_MUTEX return 0;
1062 /* get latest key */
1064 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1068 register struct afsconf_key *tk;
1069 register afs_int32 best;
1070 struct afsconf_key *bestk;
1071 register afs_int32 code;
1073 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1075 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1077 maxa = adir->keystr->nkeys;
1079 best = -1; /* highest kvno we've seen yet */
1080 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1081 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1082 if (tk->kvno == 999)
1083 continue; /* skip bcrypt keys */
1084 if (tk->kvno > best) {
1089 if (bestk) { /* found any */
1091 memcpy(akey, bestk->key, 8); /* copy out latest key */
1093 *avno = bestk->kvno; /* and kvno to caller */
1094 UNLOCK_GLOBAL_MUTEX return 0;
1096 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* didn't find any keys */
1099 /* get a particular key */
1101 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1103 register int i, maxa;
1104 register struct afsconf_key *tk;
1105 register afs_int32 code;
1107 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1109 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1111 maxa = adir->keystr->nkeys;
1113 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1114 if (tk->kvno == avno) {
1115 memcpy(akey, tk->key, 8);
1116 UNLOCK_GLOBAL_MUTEX return 0;
1120 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1123 /* save the key structure in the appropriate file */
1125 SaveKeys(struct afsconf_dir *adir)
1127 struct afsconf_keys tkeys;
1129 register afs_int32 i;
1132 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1134 /* convert it to net byte order */
1135 for (i = 0; i < tkeys.nkeys; i++)
1136 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1137 tkeys.nkeys = htonl(tkeys.nkeys);
1139 /* rewrite keys file */
1140 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1141 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1143 return AFSCONF_FAILURE;
1144 i = write(fd, &tkeys, sizeof(tkeys));
1145 if (i != sizeof(tkeys)) {
1147 return AFSCONF_FAILURE;
1150 return AFSCONF_FAILURE;
1155 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1156 afs_int32 overwrite)
1158 register struct afsconf_keys *tk;
1159 register struct afsconf_key *tkey;
1160 register afs_int32 i;
1163 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1166 if (akvno < 0 || akvno > 255) {
1167 UNLOCK_GLOBAL_MUTEX return ERANGE;
1171 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1172 if (tkey->kvno == akvno) {
1174 UNLOCK_GLOBAL_MUTEX return AFSCONF_KEYINUSE;
1181 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1182 UNLOCK_GLOBAL_MUTEX return AFSCONF_FULL;
1184 tkey = &tk->key[tk->nkeys++];
1187 memcpy(tkey->key, akey, 8);
1189 afsconf_Touch(adir);
1190 UNLOCK_GLOBAL_MUTEX return i;
1193 /* this proc works by sliding the other guys down, rather than using a funny
1194 kvno value, so that callers can count on getting a good key in key[0].
1197 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1199 register struct afsconf_keys *tk;
1200 register struct afsconf_key *tkey;
1204 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1206 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1207 if (tkey->kvno == akvno) {
1213 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1216 /* otherwise slide the others down. i and tkey point at the guy to delete */
1217 for (; i < tk->nkeys - 1; i++, tkey++) {
1218 tkey->kvno = (tkey + 1)->kvno;
1219 memcpy(tkey->key, (tkey + 1)->key, 8);
1223 afsconf_Touch(adir);
1224 UNLOCK_GLOBAL_MUTEX return i;