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>
16 #include <afs/pthread_glock.h>
18 #include "../afs/sysincludes.h"
19 #include "../afs/afsincludes.h"
21 #include <sys/types.h>
24 #include <sys/utime.h>
26 #include <WINNT/afssw.h>
29 #endif /* AFS_AFSDB_ENV */
31 #include <sys/socket.h>
32 #include <netinet/in.h>
37 #include <arpa/nameser.h>
39 #endif /* AFS_AFSDB_ENV */
40 #endif /* AFS_NT40_ENV */
41 #include <afs/afsint.h>
60 #include <afs/afsutil.h>
61 #include "cellconfig.h"
64 static struct afsconf_servPair serviceTable [] = {
69 { "afskauth", 7004, },
71 { "afserror", 7006, },
72 { "afsnanny", 7007, },
73 { "afsupdate", 7008, },
74 { "afsrmtsys", 7009, },
75 { "afsres", 7010, }, /* residency database for MR-AFS */
76 { "afsremio", 7011, }, /* remote I/O interface for MR-AFS */
77 { 0, 0 } /* insert new services before this spot */
81 static afs_int32 afsconf_FindService(register const char *aname);
82 static int TrimLine(char *abuffer);
84 static int IsClientConfigDirectory(const char *path);
85 static int GetCellNT(struct afsconf_dir *adir);
87 static int afsconf_Check(register struct afsconf_dir *adir);
88 static int afsconf_Touch(register struct afsconf_dir *adir);
89 static int GetCellUnix(struct afsconf_dir *adir);
90 static int afsconf_OpenInternal(register struct afsconf_dir *adir,
91 char *cell, char clones[]);
92 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
93 char *aname, char *aclone);
94 static int ParseCellLine(register char *aline, register char *aname,
95 register char *alname);
96 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
97 static int afsconf_Reopen(register struct afsconf_dir *adir);
98 static int SaveKeys(struct afsconf_dir *adir);
101 #define T_AFSDB 18 /* per RFC1183 section 1 */
105 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
106 * our code can tell if there is new info in the key files, the cell server db
107 * files or any of the other files (and reopen the thing) if the date on
108 * CellServDB changes.
111 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
112 static afs_int32 afsconf_FindService(register const char *aname)
114 /* lookup a service name */
116 register struct afsconf_servPair *tsp;
118 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
119 ts = getservbyname(aname, "");
121 ts = getservbyname(aname, NULL);
124 /* we found it in /etc/services, so we use this value */
125 return ts->s_port; /* already in network byte order */
128 /* not found in /etc/services, see if it is one of ours */
129 for(tsp = serviceTable;; tsp++) {
130 if (tsp->name == NULL) return -1;
131 if (!strcmp(tsp->name, aname)) return htons(tsp->port);
135 static int TrimLine(char *abuffer)
143 if (!isspace(tc)) break;
147 strcpy(abuffer, tbuffer);
153 * IsClientConfigDirectory() -- determine if path matches well-known
154 * client configuration directory.
156 static int IsClientConfigDirectory(const char *path)
158 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
161 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
162 int cc = tolower(cdir[i]);
163 int pc = tolower(path[i]);
176 /* hit end of one or both; allow mismatch in existence of trailing slash */
177 if (cdir[i] != '\0') {
178 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
182 if (path[i] != '\0') {
183 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
189 #endif /* AFS_NT40_ENV */
192 static int afsconf_Check(register struct afsconf_dir *adir)
196 register afs_int32 code;
199 /* NT client CellServDB has different file name than NT server or Unix */
200 if (IsClientConfigDirectory(adir->name)) {
201 strcompose(tbuffer, 256,
202 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
204 strcompose(tbuffer, 256,
205 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
208 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
209 #endif /* AFS_NT40_ENV */
211 code = stat(tbuffer, &tstat);
215 /* did file change? */
216 if (tstat.st_mtime == adir->timeRead) {
219 /* otherwise file has changed, so reopen it */
220 return afsconf_Reopen(adir);
223 /* set modtime on file */
224 static int afsconf_Touch(register struct afsconf_dir *adir)
228 struct timeval tvp[2];
231 adir->timeRead = 0; /* just in case */
234 /* NT client CellServDB has different file name than NT server or Unix */
236 if (IsClientConfigDirectory(adir->name)) {
237 strcompose(tbuffer, 256,
238 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
240 strcompose(tbuffer, 256,
241 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
244 return _utime(tbuffer, NULL);
247 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
248 gettimeofday(&tvp[0], NULL);
250 return utimes(tbuffer, tvp);
251 #endif /* AFS_NT40_ENV */
254 struct afsconf_dir *afsconf_Open(register const char *adir)
256 register struct afsconf_dir *tdir;
257 register afs_int32 code;
260 /* zero structure and fill in name; rest is done by internal routine */
261 tdir = (struct afsconf_dir *) malloc(sizeof(struct afsconf_dir));
262 memset(tdir, 0, sizeof(struct afsconf_dir));
263 tdir->name = (char *) malloc(strlen(adir)+1);
264 strcpy(tdir->name, adir);
266 code = afsconf_OpenInternal(tdir, 0, 0);
268 char *afsconf_path, afs_confdir[128];
271 /* Check global place only when local Open failed for whatever reason */
272 if (!(afsconf_path = getenv("AFSCONF"))) {
273 /* 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... */
278 if (!(home_dir = getenv("HOME"))) {
279 /* Our last chance is the "/.AFSCONF" file */
280 fp = fopen("/.AFSCONF", "r");
284 return (struct afsconf_dir *) 0;
286 fgets(afs_confdir, 128, fp);
291 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
292 fp = fopen(pathname, "r");
294 /* Our last chance is the "/.AFSCONF" file */
295 fp = fopen("/.AFSCONF", "r");
299 return (struct afsconf_dir *) 0;
301 fgets(afs_confdir, 128, fp);
304 fgets(afs_confdir, 128, fp);
307 len = strlen(afs_confdir);
311 return (struct afsconf_dir *) 0;
313 if (afs_confdir[len-1] == '\n') {
314 afs_confdir[len-1] = 0;
316 afsconf_path = afs_confdir;
318 tdir->name = (char *) malloc(strlen(afsconf_path)+1);
319 strcpy(tdir->name, afsconf_path);
320 code = afsconf_OpenInternal(tdir, 0, 0);
325 return (struct afsconf_dir *) 0;
333 static int GetCellUnix(struct afsconf_dir *adir)
339 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
340 tf = fopen(tbuffer, "r");
342 rc = fscanf(tf, "%s", tbuffer);
344 adir->cellName = (char *) malloc(strlen(tbuffer)+1);
345 strcpy(adir->cellName, tbuffer);
357 static int GetCellNT(struct afsconf_dir *adir)
359 if (IsClientConfigDirectory(adir->name)) {
360 /* NT client config dir; ThisCell is in registry (no file). */
361 return afssw_GetClientCellName(&adir->cellName);
363 /* NT server config dir; works just like Unix */
364 return GetCellUnix(adir);
367 #endif /* AFS_NT40_ENV */
370 static int afsconf_OpenInternal(register struct afsconf_dir *adir,
371 char *cell, char clones[])
374 register char *tp, *bp;
375 register struct afsconf_entry *curEntry;
376 struct afsconf_aliasentry *curAlias;
377 register afs_int32 code;
379 char tbuffer[256], tbuf1[256];
382 /* figure out the cell name */
386 i = GetCellUnix(adir);
389 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
395 /* now parse the individual lines */
399 /* NT client/server have a CellServDB that is the same format as Unix.
400 * However, the NT client uses a different file name
402 if (IsClientConfigDirectory(adir->name)) {
403 /* NT client config dir */
404 strcompose(tbuffer, 256,
405 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
407 /* NT server config dir */
408 strcompose(tbuffer, 256,
409 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
412 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
413 #endif /* AFS_NT40_ENV */
415 if (!stat(tbuffer, &tstat)) {
416 adir->timeRead = tstat.st_mtime;
421 strcpy(tbuf1, tbuffer);
422 tf = fopen(tbuffer, "r");
427 tp = fgets(tbuffer, sizeof(tbuffer), tf);
429 TrimLine(tbuffer); /* remove white space */
430 if (tbuffer[0] == 0 || tbuffer[0] == '\n') continue; /* empty line */
431 if (tbuffer[0] == '>') {
432 char linkedcell[MAXCELLCHARS];
433 /* start new cell item */
435 /* thread this guy on the list */
436 curEntry->next = adir->entries;
437 adir->entries = curEntry;
440 curEntry = (struct afsconf_entry *) malloc(sizeof(struct afsconf_entry));
441 memset(curEntry, 0, sizeof(struct afsconf_entry));
442 code = ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
444 afsconf_CloseInternal(adir);
448 if (linkedcell[0] != '\0') {
449 curEntry->cellInfo.linkedCell =
450 (char *) malloc(strlen(linkedcell) + 1);
451 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
455 /* new host in the current cell */
457 afsconf_CloseInternal(adir);
461 i = curEntry->cellInfo.numServers;
462 if (cell && !strcmp(cell, curEntry->cellInfo.name))
463 code = ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], &clones[i]);
465 code = ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], 0);
467 if (code == AFSCONF_SYNTAX) {
468 for (bp=tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
472 fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
476 afsconf_CloseInternal(adir);
479 curEntry->cellInfo.numServers = ++i;
482 fclose(tf); /* close the file now */
484 /* end the last partially-completed cell */
486 curEntry->next = adir->entries;
487 adir->entries = curEntry;
490 /* Read in the alias list */
491 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
493 tf = fopen(tbuffer, "r");
497 tp = fgets(tbuffer, sizeof(tbuffer), tf);
499 TrimLine(tbuffer); /* remove white space */
501 if (tbuffer[0] == '\0' ||
502 tbuffer[0] == '\n' ||
503 tbuffer[0] == '#') continue; /* empty line */
506 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t') tp++;
507 if (tp[0] == '\0') continue; /* invalid line */
509 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t')) 0[tp++] = '\0';
510 if (tp[0] == '\0') continue; /* invalid line */
513 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' &&
514 tp[0] != '\r' && tp[0] != '\n') tp++;
517 curAlias = malloc(sizeof(*curAlias));
518 memset(curAlias, 0, sizeof(*curAlias));
520 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
521 strcpy(curAlias->aliasInfo.realName, tbuffer);
523 curAlias->next = adir->alias_entries;
524 adir->alias_entries = curAlias;
527 /* now read the fs keys, if possible */
528 adir->keystr = (struct afsconf_keys *) 0;
529 afsconf_IntGetKeys(adir);
534 /* parse a line of the form
535 *"128.2.1.3 #hostname" or
536 *"[128.2.1.3] #hostname" for clones
537 * into the appropriate pieces.
539 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
540 char *aname, char *aclone)
543 register afs_int32 code;
547 if (aclone) *aclone = 1;
548 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
550 if (aclone) *aclone = 0;
551 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
553 if (code != 5) return AFSCONF_SYNTAX;
554 addr->sin_family = AF_INET;
556 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
557 addr->sin_len = sizeof(struct sockaddr_in);
559 tp = (char *) &addr->sin_addr;
567 /* parse a line of the form
568 * ">cellname [linkedcellname] [#comments]"
569 * into the appropriate pieces.
571 static int ParseCellLine(register char *aline, register char *aname,
572 register char *alname)
575 code = sscanf(aline, ">%s %s", aname, alname);
576 if (code == 1) *alname = '\0';
578 if (*alname == '#') {
582 return (code > 0 ? 0 : AFSCONF_SYNTAX);
585 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
586 int afsconf_CellApply(struct afsconf_dir *adir, int (*aproc)(), char *arock)
588 register struct afsconf_entry *tde;
589 register afs_int32 code;
591 for(tde=adir->entries; tde; tde=tde->next) {
592 code = (*aproc)(&tde->cellInfo, arock, adir);
602 /* call aproc(entry, arock, adir) for all cell aliases.
603 * Proc must return 0, or we'll stop early and return the code it returns
605 int afsconf_CellAliasApply(struct afsconf_dir *adir, int (*aproc)(), char *arock)
607 register struct afsconf_aliasentry *tde;
608 register afs_int32 code;
610 for(tde=adir->alias_entries; tde; tde=tde->next) {
611 code = (*aproc)(&tde->aliasInfo, arock, adir);
621 afs_int32 afsconf_SawCell = 0;
623 int afsconf_GetExtendedCellInfo(struct afsconf_dir *adir,
624 char *acellName, char *aservice, struct afsconf_cell *acellInfo,
630 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
637 cell = (char *) &acellInfo->name;
639 code = afsconf_OpenInternal(adir, cell, clones);
644 #if !defined(AFS_NT40_ENV)
645 int afsconf_GetAfsdbInfo(char *acellName, char *aservice,
646 struct afsconf_cell *acellInfo)
651 unsigned char answer[1024];
653 char realCellName[256];
658 /* The resolver isn't always MT-safe.. Perhaps this ought to be
659 * replaced with a more fine-grained lock just for the resolver
663 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
667 return AFSCONF_NOTFOUND;
669 p = answer + sizeof(HEADER); /* Skip header */
670 code = dn_expand(answer, answer + len, p, host, sizeof(host));
672 return AFSCONF_NOTFOUND;
674 p += code + QFIXEDSZ; /* Skip name */
676 while (p < answer + len) {
679 code = dn_expand(answer, answer + len, p, host, sizeof(host));
681 return AFSCONF_NOTFOUND;
683 p += code; /* Skip the name */
684 type = (p[0] << 8) | p[1];
685 p += 4; /* Skip type and class */
686 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
687 p += 4; /* Skip the TTL */
688 size = (p[0] << 8) | p[1];
689 p += 2; /* Skip the size */
691 if (type == T_AFSDB) {
695 afsdb_type = (p[0] << 8) | p[1];
696 if (afsdb_type == 1) {
698 * We know this is an AFSDB record for our cell, of the
699 * right AFSDB type. Write down the true cell name that
700 * the resolver gave us above.
702 strcpy(realCellName, host);
705 code = dn_expand(answer, answer+len, p+2, host, sizeof(host));
707 return AFSCONF_NOTFOUND;
709 if ((afsdb_type == 1) &&
710 (server_num < MAXHOSTSPERCELL) &&
711 /* Do we want to get TTL data for the A record as well? */
712 (he = gethostbyname(host))) {
714 memcpy(&ipaddr, he->h_addr, he->h_length);
715 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
716 strncpy(acellInfo->hostName[server_num], host,
717 sizeof(acellInfo->hostName[server_num]));
720 if (!minttl || ttl < minttl) minttl = ttl;
727 if (server_num == 0) /* No AFSDB records */
728 return AFSCONF_NOTFOUND;
730 /* Convert the real cell name to lowercase */
731 for (p = (unsigned char *) realCellName; *p; p++)
734 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
735 acellInfo->numServers = server_num;
738 tservice = afsconf_FindService(aservice);
740 return AFSCONF_NOTFOUND; /* service not found */
741 for (i=0; i<acellInfo->numServers; i++) {
742 acellInfo->hostAddr[i].sin_port = tservice;
746 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
751 int afsconf_GetAfsdbInfo(char *acellName, char *aservice,
752 struct afsconf_cell *acellInfo)
754 register afs_int32 i;
756 struct afsconf_entry DNSce;
757 char *DNStmpStrp; /* a temp string pointer */
759 afs_int32 cellHosts[AFSMAXCELLHOSTS];
764 DNSce.cellInfo.numServers=0;
766 rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
767 /* ignore the ttl here since this code is only called by transitory programs
774 for (i = 0; i < numServers; i++)
776 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i], sizeof(long));
777 acellInfo->hostAddr[i].sin_family = AF_INET;
779 /* sin_port supplied by connection code */
782 acellInfo->numServers = numServers;
783 strcpy(acellInfo->name, acellName);
786 tservice = afsconf_FindService(aservice);
789 return AFSCONF_NOTFOUND; /* service not found */
791 for(i=0; i< acellInfo->numServers; i++) {
792 acellInfo->hostAddr[i].sin_port = tservice;
795 acellInfo->linkedCell = NULL; /* no linked cell */
796 acellInfo->flags = 0;
800 #endif /* AFS_AFSDB_ENV */
802 int afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName,
803 char *aservice, struct afsconf_cell *acellInfo)
805 register struct afsconf_entry *tce;
806 struct afsconf_aliasentry *tcae;
807 struct afsconf_entry *bestce;
808 register afs_int32 i;
816 if (adir) afsconf_Check(adir);
819 cnLen = strlen(tcell)+1;
820 lcstring (tcell, tcell, cnLen);
821 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
822 /* call to afsconf_GetLocalCell: like klog */
824 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
831 cnLen = strlen(tcell);
832 bestce = (struct afsconf_entry *) 0;
839 /* Look through the list of aliases */
840 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
841 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
842 tcell = tcae->aliasInfo.realName;
847 for(tce=adir->entries;tce;tce=tce->next) {
848 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
854 if (strlen(tce->cellInfo.name) < cnLen) continue; /* clearly wrong */
855 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
856 if (bestce) ambig = 1; /* ambiguous unless we get exact match */
860 if (!ambig && bestce && bestce->cellInfo.numServers) {
861 *acellInfo = bestce->cellInfo; /* structure assignment */
863 tservice = afsconf_FindService(aservice);
866 return AFSCONF_NOTFOUND; /* service not found */
868 for(i=0;i<acellInfo->numServers;i++) {
869 acellInfo->hostAddr[i].sin_port = tservice;
872 acellInfo->timeout = 0;
879 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
881 return AFSCONF_NOTFOUND;
882 #endif /* AFS_AFSDB_ENV */
886 int afsconf_GetLocalCell(register struct afsconf_dir *adir,
887 char *aname, afs_int32 alen)
889 static int afsconf_showcell = 0;
895 * If a cell switch was specified in a command, then it should override the
896 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
897 * is set and the cell name in the adir structure is used.
898 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
900 if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {
901 if ( !afsconf_showcell ) {
902 fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
903 afsconf_showcell = 1;
905 strncpy(aname, afscell_path, alen);
908 if (adir->cellName) {
909 strncpy(aname, adir->cellName, alen);
911 else code = AFSCONF_UNKNOWN;
918 int afsconf_Close(struct afsconf_dir *adir)
921 afsconf_CloseInternal(adir);
922 if (adir->name) free(adir->name);
928 static int afsconf_CloseInternal(register struct afsconf_dir *adir)
930 register struct afsconf_entry *td, *nd;
931 register char *tname;
933 tname = adir->name; /* remember name, since that's all we preserve */
935 /* free everything we can find */
936 if (adir->cellName) free(adir->cellName);
937 for(td=adir->entries;td;td=nd) {
939 if (td->cellInfo.linkedCell)
940 free(td->cellInfo.linkedCell);
943 if (adir->keystr) free(adir->keystr);
946 memset(adir, 0, sizeof(struct afsconf_dir));
947 adir->name = tname; /* restore it */
951 static int afsconf_Reopen(register struct afsconf_dir *adir)
953 register afs_int32 code;
954 code = afsconf_CloseInternal(adir);
955 if (code) return code;
956 code = afsconf_OpenInternal(adir, 0, 0);
960 /* called during opening of config file */
961 int afsconf_IntGetKeys(struct afsconf_dir *adir)
965 struct afsconf_keys *tstr;
966 register afs_int32 code;
969 /* NT client config dir has no KeyFile; don't risk attempting open
970 * because there might be a random file of this name if dir is shared.
972 if (IsClientConfigDirectory(adir->name)) {
973 adir->keystr = ((struct afsconf_keys *)
974 malloc(sizeof(struct afsconf_keys)));
975 adir->keystr->nkeys = 0;
978 #endif /* AFS_NT40_ENV */
981 /* compute the key name and other setup */
983 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
984 tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
988 fd = open(tbuffer, O_RDONLY);
994 code = read(fd, tstr, sizeof(struct afsconf_keys));
996 if (code < sizeof(afs_int32)) {
1002 /* convert key structure to host order */
1003 tstr->nkeys = ntohl(tstr->nkeys);
1004 for(fd=0;fd<tstr->nkeys;fd++)
1005 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1011 /* get keys structure */
1012 int afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1014 register afs_int32 code;
1017 code = afsconf_Check(adir);
1019 return AFSCONF_FAILURE;
1020 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1025 /* get latest key */
1026 afs_int32 afsconf_GetLatestKey(struct afsconf_dir *adir,
1027 afs_int32 *avno, char *akey)
1031 register struct afsconf_key *tk;
1032 register afs_int32 best;
1033 struct afsconf_key *bestk;
1034 register afs_int32 code;
1037 code = afsconf_Check(adir);
1039 return AFSCONF_FAILURE;
1040 maxa = adir->keystr->nkeys;
1042 best = -1; /* highest kvno we've seen yet */
1043 bestk = (struct afsconf_key *) 0; /* ptr to structure providing best */
1044 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
1045 if (tk->kvno == 999) continue; /* skip bcrypt keys */
1046 if (tk->kvno > best) {
1051 if (bestk) { /* found any */
1052 if (akey) memcpy(akey, bestk->key, 8); /* copy out latest key */
1053 if (avno) *avno = bestk->kvno; /* and kvno to caller */
1058 return AFSCONF_NOTFOUND; /* didn't find any keys */
1061 /* get a particular key */
1062 int afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno,
1065 register int i, maxa;
1066 register struct afsconf_key *tk;
1067 register afs_int32 code;
1070 code = afsconf_Check(adir);
1072 return AFSCONF_FAILURE;
1073 maxa = adir->keystr->nkeys;
1075 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
1076 if (tk->kvno == avno) {
1077 memcpy(akey, tk->key, 8);
1084 return AFSCONF_NOTFOUND;
1087 /* save the key structure in the appropriate file */
1088 static int SaveKeys(struct afsconf_dir *adir)
1090 struct afsconf_keys tkeys;
1092 register afs_int32 i;
1095 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1097 /* convert it to net byte order */
1098 for(i = 0; i<tkeys.nkeys; i++ )
1099 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1100 tkeys.nkeys = htonl(tkeys.nkeys);
1102 /* rewrite keys file */
1103 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1104 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1105 if (fd < 0) return AFSCONF_FAILURE;
1106 i = write(fd, &tkeys, sizeof(tkeys));
1107 if (i != sizeof(tkeys)) {
1109 return AFSCONF_FAILURE;
1111 if (close(fd) < 0) return AFSCONF_FAILURE;
1115 int afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno,
1116 char akey[8], afs_int32 overwrite)
1118 register struct afsconf_keys *tk;
1119 register struct afsconf_key *tkey;
1120 register afs_int32 i;
1127 if (akvno < 0 || akvno > 255) {
1133 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1134 if (tkey->kvno == akvno) {
1137 return AFSCONF_KEYINUSE;
1144 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1146 return AFSCONF_FULL;
1148 tkey = &tk->key[tk->nkeys++];
1151 memcpy(tkey->key, akey, 8);
1153 afsconf_Touch(adir);
1158 /* this proc works by sliding the other guys down, rather than using a funny
1159 kvno value, so that callers can count on getting a good key in key[0].
1161 int afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1163 register struct afsconf_keys *tk;
1164 register struct afsconf_key *tkey;
1171 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1172 if (tkey->kvno == akvno) {
1179 return AFSCONF_NOTFOUND;
1182 /* otherwise slide the others down. i and tkey point at the guy to delete */
1183 for(;i<tk->nkeys-1; i++,tkey++) {
1184 tkey->kvno = (tkey+1)->kvno;
1185 memcpy(tkey->key, (tkey+1)->key, 8);
1189 afsconf_Touch(adir);