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;
309 fgets(afs_confdir, 128, fp);
312 fgets(afs_confdir, 128, fp);
315 len = strlen(afs_confdir);
318 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
320 if (afs_confdir[len - 1] == '\n') {
321 afs_confdir[len - 1] = 0;
323 afsconf_path = afs_confdir;
325 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
326 strcpy(tdir->name, afsconf_path);
327 code = afsconf_OpenInternal(tdir, 0, 0);
331 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
334 UNLOCK_GLOBAL_MUTEX return tdir;
339 GetCellUnix(struct afsconf_dir *adir)
345 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
346 tf = fopen(tbuffer, "r");
348 rc = fscanf(tf, "%s", tbuffer);
350 adir->cellName = (char *)malloc(strlen(tbuffer) + 1);
351 strcpy(adir->cellName, tbuffer);
363 GetCellNT(struct afsconf_dir *adir)
365 if (IsClientConfigDirectory(adir->name)) {
366 /* NT client config dir; ThisCell is in registry (no file). */
367 return afssw_GetClientCellName(&adir->cellName);
369 /* NT server config dir; works just like Unix */
370 return GetCellUnix(adir);
373 #endif /* AFS_NT40_ENV */
377 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
381 register char *tp, *bp;
382 register struct afsconf_entry *curEntry;
383 struct afsconf_aliasentry *curAlias;
384 register afs_int32 code;
386 char tbuffer[256], tbuf1[256];
389 /* figure out the cell name */
393 i = GetCellUnix(adir);
396 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
402 /* now parse the individual lines */
406 /* NT client/server have a CellServDB that is the same format as Unix.
407 * However, the NT client uses a different file name
409 if (IsClientConfigDirectory(adir->name)) {
410 /* NT client config dir */
411 strcompose(tbuffer, 256, adir->name, "/",
412 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
414 /* NT server config dir */
415 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
419 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
420 #endif /* AFS_NT40_ENV */
422 if (!stat(tbuffer, &tstat)) {
423 adir->timeRead = tstat.st_mtime;
428 strcpy(tbuf1, tbuffer);
429 tf = fopen(tbuffer, "r");
434 tp = fgets(tbuffer, sizeof(tbuffer), tf);
437 TrimLine(tbuffer); /* remove white space */
438 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
439 continue; /* empty line */
440 if (tbuffer[0] == '>') {
441 char linkedcell[MAXCELLCHARS];
442 /* start new cell item */
444 /* thread this guy on the list */
445 curEntry->next = adir->entries;
446 adir->entries = curEntry;
450 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
451 memset(curEntry, 0, sizeof(struct afsconf_entry));
453 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
455 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 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
578 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
581 return AFSCONF_SYNTAX;
582 addr->sin_family = AF_INET;
584 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
585 addr->sin_len = sizeof(struct sockaddr_in);
587 tp = (char *)&addr->sin_addr;
595 /* parse a line of the form
596 * ">cellname [linkedcellname] [#comments]"
597 * into the appropriate pieces.
600 ParseCellLine(register char *aline, register char *aname,
601 register char *alname)
604 code = sscanf(aline, ">%s %s", aname, alname);
608 if (*alname == '#') {
612 return (code > 0 ? 0 : AFSCONF_SYNTAX);
615 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
617 afsconf_CellApply(struct afsconf_dir *adir,
618 int (*aproc) (struct afsconf_cell * cell, char *arock,
619 struct afsconf_dir * dir), char *arock)
621 register struct afsconf_entry *tde;
622 register afs_int32 code;
623 LOCK_GLOBAL_MUTEX for (tde = adir->entries; tde; tde = tde->next) {
624 code = (*aproc) (&tde->cellInfo, arock, adir);
626 UNLOCK_GLOBAL_MUTEX return code;
629 UNLOCK_GLOBAL_MUTEX return 0;
632 /* call aproc(entry, arock, adir) for all cell aliases.
633 * Proc must return 0, or we'll stop early and return the code it returns
636 afsconf_CellAliasApply(struct afsconf_dir *adir,
637 int (*aproc) (struct afsconf_cellalias * alias,
638 char *arock, struct afsconf_dir * dir),
641 register struct afsconf_aliasentry *tde;
642 register afs_int32 code;
643 LOCK_GLOBAL_MUTEX for (tde = adir->alias_entries; tde; tde = tde->next) {
644 code = (*aproc) (&tde->aliasInfo, arock, adir);
646 UNLOCK_GLOBAL_MUTEX return code;
649 UNLOCK_GLOBAL_MUTEX return 0;
652 afs_int32 afsconf_SawCell = 0;
655 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
656 char *aservice, struct afsconf_cell *acellInfo,
662 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
669 cell = (char *)&acellInfo->name;
671 code = afsconf_OpenInternal(adir, cell, clones);
676 #if !defined(AFS_NT40_ENV)
678 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
679 struct afsconf_cell *acellInfo)
684 unsigned char answer[1024];
686 char realCellName[256];
691 /* The resolver isn't always MT-safe.. Perhaps this ought to be
692 * replaced with a more fine-grained lock just for the resolver
695 LOCK_GLOBAL_MUTEX len =
696 res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
697 UNLOCK_GLOBAL_MUTEX if (len < 0)
698 return AFSCONF_NOTFOUND;
700 p = answer + sizeof(HEADER); /* Skip header */
701 code = dn_expand(answer, answer + len, p, host, sizeof(host));
703 return AFSCONF_NOTFOUND;
705 p += code + QFIXEDSZ; /* Skip name */
707 while (p < answer + len) {
710 code = dn_expand(answer, answer + len, p, host, sizeof(host));
712 return AFSCONF_NOTFOUND;
714 p += code; /* Skip the name */
715 type = (p[0] << 8) | p[1];
716 p += 4; /* Skip type and class */
717 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
718 p += 4; /* Skip the TTL */
719 size = (p[0] << 8) | p[1];
720 p += 2; /* Skip the size */
722 if (type == T_AFSDB) {
726 afsdb_type = (p[0] << 8) | p[1];
727 if (afsdb_type == 1) {
729 * We know this is an AFSDB record for our cell, of the
730 * right AFSDB type. Write down the true cell name that
731 * the resolver gave us above.
733 strcpy(realCellName, host);
736 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
738 return AFSCONF_NOTFOUND;
740 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
741 /* Do we want to get TTL data for the A record as well? */
742 (he = gethostbyname(host))) {
744 memcpy(&ipaddr, he->h_addr, he->h_length);
745 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
746 strncpy(acellInfo->hostName[server_num], host,
747 sizeof(acellInfo->hostName[server_num]));
750 if (!minttl || ttl < minttl)
758 if (server_num == 0) /* No AFSDB records */
759 return AFSCONF_NOTFOUND;
761 /* Convert the real cell name to lowercase */
762 for (p = (unsigned char *)realCellName; *p; p++)
765 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
766 acellInfo->numServers = server_num;
769 tservice = afsconf_FindService(aservice);
771 return AFSCONF_NOTFOUND; /* service not found */
772 for (i = 0; i < acellInfo->numServers; i++) {
773 acellInfo->hostAddr[i].sin_port = tservice;
777 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
783 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
784 struct afsconf_cell *acellInfo)
786 register afs_int32 i;
788 struct afsconf_entry DNSce;
789 char *DNStmpStrp; /* a temp string pointer */
791 afs_int32 cellHosts[AFSMAXCELLHOSTS];
796 DNSce.cellInfo.numServers = 0;
798 rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
799 /* ignore the ttl here since this code is only called by transitory programs
806 for (i = 0; i < numServers; i++) {
807 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i],
809 acellInfo->hostAddr[i].sin_family = AF_INET;
811 /* sin_port supplied by connection code */
814 acellInfo->numServers = numServers;
815 strcpy(acellInfo->name, acellName);
817 LOCK_GLOBAL_MUTEX tservice = afsconf_FindService(aservice);
818 UNLOCK_GLOBAL_MUTEX if (tservice < 0) {
819 return AFSCONF_NOTFOUND; /* service not found */
821 for (i = 0; i < acellInfo->numServers; i++) {
822 acellInfo->hostAddr[i].sin_port = tservice;
825 acellInfo->linkedCell = NULL; /* no linked cell */
826 acellInfo->flags = 0;
830 #endif /* AFS_AFSDB_ENV */
833 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
834 struct afsconf_cell *acellInfo)
836 register struct afsconf_entry *tce;
837 struct afsconf_aliasentry *tcae;
838 struct afsconf_entry *bestce;
839 register afs_int32 i;
846 LOCK_GLOBAL_MUTEX if (adir)
850 cnLen = strlen(tcell) + 1;
851 lcstring(tcell, tcell, cnLen);
852 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
853 /* call to afsconf_GetLocalCell: like klog */
855 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
857 UNLOCK_GLOBAL_MUTEX return i;
861 cnLen = strlen(tcell);
862 bestce = (struct afsconf_entry *)0;
865 UNLOCK_GLOBAL_MUTEX return 0;
868 /* Look through the list of aliases */
869 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
870 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
871 tcell = tcae->aliasInfo.realName;
876 for (tce = adir->entries; tce; tce = tce->next) {
877 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
883 if (strlen(tce->cellInfo.name) < cnLen)
884 continue; /* clearly wrong */
885 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
887 ambig = 1; /* ambiguous unless we get exact match */
891 if (!ambig && bestce && bestce->cellInfo.numServers) {
892 *acellInfo = bestce->cellInfo; /* structure assignment */
894 tservice = afsconf_FindService(aservice);
896 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* service not found */
898 for (i = 0; i < acellInfo->numServers; i++) {
899 acellInfo->hostAddr[i].sin_port = tservice;
902 acellInfo->timeout = 0;
903 UNLOCK_GLOBAL_MUTEX return 0;
907 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
909 return AFSCONF_NOTFOUND;
910 #endif /* AFS_AFSDB_ENV */
915 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
918 static int afsconf_showcell = 0;
924 * If a cell switch was specified in a command, then it should override the
925 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
926 * is set and the cell name in the adir structure is used.
927 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
929 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
930 if (!afsconf_showcell) {
931 fprintf(stderr, "Note: Operation is performed on cell %s\n",
933 afsconf_showcell = 1;
935 strncpy(aname, afscell_path, alen);
938 if (adir->cellName) {
939 strncpy(aname, adir->cellName, alen);
941 code = AFSCONF_UNKNOWN;
944 UNLOCK_GLOBAL_MUTEX return (code);
948 afsconf_Close(struct afsconf_dir *adir)
950 LOCK_GLOBAL_MUTEX afsconf_CloseInternal(adir);
954 UNLOCK_GLOBAL_MUTEX return 0;
958 afsconf_CloseInternal(register struct afsconf_dir *adir)
960 register struct afsconf_entry *td, *nd;
961 struct afsconf_aliasentry *ta, *na;
962 register char *tname;
964 tname = adir->name; /* remember name, since that's all we preserve */
966 /* free everything we can find */
968 free(adir->cellName);
969 for (td = adir->entries; td; td = nd) {
971 if (td->cellInfo.linkedCell)
972 free(td->cellInfo.linkedCell);
975 for (ta = adir->alias_entries; ta; ta = na) {
983 memset(adir, 0, sizeof(struct afsconf_dir));
984 adir->name = tname; /* restore it */
989 afsconf_Reopen(register struct afsconf_dir *adir)
991 register afs_int32 code;
992 code = afsconf_CloseInternal(adir);
995 code = afsconf_OpenInternal(adir, 0, 0);
999 /* called during opening of config file */
1001 afsconf_IntGetKeys(struct afsconf_dir *adir)
1005 struct afsconf_keys *tstr;
1006 register afs_int32 code;
1009 /* NT client config dir has no KeyFile; don't risk attempting open
1010 * because there might be a random file of this name if dir is shared.
1012 if (IsClientConfigDirectory(adir->name)) {
1013 adir->keystr = ((struct afsconf_keys *)
1014 malloc(sizeof(struct afsconf_keys)));
1015 adir->keystr->nkeys = 0;
1018 #endif /* AFS_NT40_ENV */
1021 /* compute the key name and other setup */
1022 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1023 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1024 adir->keystr = tstr;
1027 fd = open(tbuffer, O_RDONLY);
1030 UNLOCK_GLOBAL_MUTEX return 0;
1032 code = read(fd, tstr, sizeof(struct afsconf_keys));
1034 if (code < sizeof(afs_int32)) {
1036 UNLOCK_GLOBAL_MUTEX return 0;
1039 /* convert key structure to host order */
1040 tstr->nkeys = ntohl(tstr->nkeys);
1041 for (fd = 0; fd < tstr->nkeys; fd++)
1042 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1044 UNLOCK_GLOBAL_MUTEX return 0;
1047 /* get keys structure */
1049 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1051 register afs_int32 code;
1053 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1055 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1057 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1058 UNLOCK_GLOBAL_MUTEX return 0;
1061 /* get latest key */
1063 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1067 register struct afsconf_key *tk;
1068 register afs_int32 best;
1069 struct afsconf_key *bestk;
1070 register afs_int32 code;
1072 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1074 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1076 maxa = adir->keystr->nkeys;
1078 best = -1; /* highest kvno we've seen yet */
1079 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1080 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1081 if (tk->kvno == 999)
1082 continue; /* skip bcrypt keys */
1083 if (tk->kvno > best) {
1088 if (bestk) { /* found any */
1090 memcpy(akey, bestk->key, 8); /* copy out latest key */
1092 *avno = bestk->kvno; /* and kvno to caller */
1093 UNLOCK_GLOBAL_MUTEX return 0;
1095 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* didn't find any keys */
1098 /* get a particular key */
1100 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1102 register int i, maxa;
1103 register struct afsconf_key *tk;
1104 register afs_int32 code;
1106 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1108 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1110 maxa = adir->keystr->nkeys;
1112 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1113 if (tk->kvno == avno) {
1114 memcpy(akey, tk->key, 8);
1115 UNLOCK_GLOBAL_MUTEX return 0;
1119 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1122 /* save the key structure in the appropriate file */
1124 SaveKeys(struct afsconf_dir *adir)
1126 struct afsconf_keys tkeys;
1128 register afs_int32 i;
1131 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1133 /* convert it to net byte order */
1134 for (i = 0; i < tkeys.nkeys; i++)
1135 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1136 tkeys.nkeys = htonl(tkeys.nkeys);
1138 /* rewrite keys file */
1139 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1140 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1142 return AFSCONF_FAILURE;
1143 i = write(fd, &tkeys, sizeof(tkeys));
1144 if (i != sizeof(tkeys)) {
1146 return AFSCONF_FAILURE;
1149 return AFSCONF_FAILURE;
1154 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1155 afs_int32 overwrite)
1157 register struct afsconf_keys *tk;
1158 register struct afsconf_key *tkey;
1159 register afs_int32 i;
1162 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1165 if (akvno < 0 || akvno > 255) {
1166 UNLOCK_GLOBAL_MUTEX return ERANGE;
1170 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1171 if (tkey->kvno == akvno) {
1173 UNLOCK_GLOBAL_MUTEX return AFSCONF_KEYINUSE;
1180 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1181 UNLOCK_GLOBAL_MUTEX return AFSCONF_FULL;
1183 tkey = &tk->key[tk->nkeys++];
1186 memcpy(tkey->key, akey, 8);
1188 afsconf_Touch(adir);
1189 UNLOCK_GLOBAL_MUTEX return i;
1192 /* this proc works by sliding the other guys down, rather than using a funny
1193 kvno value, so that callers can count on getting a good key in key[0].
1196 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1198 register struct afsconf_keys *tk;
1199 register struct afsconf_key *tkey;
1203 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1205 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1206 if (tkey->kvno == akvno) {
1212 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1215 /* otherwise slide the others down. i and tkey point at the guy to delete */
1216 for (; i < tk->nkeys - 1; i++, tkey++) {
1217 tkey->kvno = (tkey + 1)->kvno;
1218 memcpy(tkey->key, (tkey + 1)->key, 8);
1222 afsconf_Touch(adir);
1223 UNLOCK_GLOBAL_MUTEX return i;