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 afs_int32 cellHosts[AFSMAXCELLHOSTS];
794 DNSce.cellInfo.numServers = 0;
796 rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
797 /* ignore the ttl here since this code is only called by transitory programs
804 for (i = 0; i < numServers; i++) {
805 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i],
807 acellInfo->hostAddr[i].sin_family = AF_INET;
809 /* sin_port supplied by connection code */
812 acellInfo->numServers = numServers;
813 strcpy(acellInfo->name, acellName);
815 LOCK_GLOBAL_MUTEX tservice = afsconf_FindService(aservice);
816 UNLOCK_GLOBAL_MUTEX if (tservice < 0) {
817 return AFSCONF_NOTFOUND; /* service not found */
819 for (i = 0; i < acellInfo->numServers; i++) {
820 acellInfo->hostAddr[i].sin_port = tservice;
823 acellInfo->linkedCell = NULL; /* no linked cell */
824 acellInfo->flags = 0;
828 #endif /* AFS_AFSDB_ENV */
831 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
832 struct afsconf_cell *acellInfo)
834 register struct afsconf_entry *tce;
835 struct afsconf_aliasentry *tcae;
836 struct afsconf_entry *bestce;
837 register afs_int32 i;
844 LOCK_GLOBAL_MUTEX if (adir)
848 cnLen = strlen(tcell) + 1;
849 lcstring(tcell, tcell, cnLen);
850 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
851 /* call to afsconf_GetLocalCell: like klog */
853 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
855 UNLOCK_GLOBAL_MUTEX return i;
859 cnLen = strlen(tcell);
860 bestce = (struct afsconf_entry *)0;
863 UNLOCK_GLOBAL_MUTEX return 0;
866 /* Look through the list of aliases */
867 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
868 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
869 tcell = tcae->aliasInfo.realName;
874 for (tce = adir->entries; tce; tce = tce->next) {
875 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
881 if (strlen(tce->cellInfo.name) < cnLen)
882 continue; /* clearly wrong */
883 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
885 ambig = 1; /* ambiguous unless we get exact match */
889 if (!ambig && bestce && bestce->cellInfo.numServers) {
890 *acellInfo = bestce->cellInfo; /* structure assignment */
892 tservice = afsconf_FindService(aservice);
894 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* service not found */
896 for (i = 0; i < acellInfo->numServers; i++) {
897 acellInfo->hostAddr[i].sin_port = tservice;
900 acellInfo->timeout = 0;
901 UNLOCK_GLOBAL_MUTEX return 0;
905 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
907 return AFSCONF_NOTFOUND;
908 #endif /* AFS_AFSDB_ENV */
913 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
916 static int afsconf_showcell = 0;
922 * If a cell switch was specified in a command, then it should override the
923 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
924 * is set and the cell name in the adir structure is used.
925 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
927 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
928 if (!afsconf_showcell) {
929 fprintf(stderr, "Note: Operation is performed on cell %s\n",
931 afsconf_showcell = 1;
933 strncpy(aname, afscell_path, alen);
936 if (adir->cellName) {
937 strncpy(aname, adir->cellName, alen);
939 code = AFSCONF_UNKNOWN;
942 UNLOCK_GLOBAL_MUTEX return (code);
946 afsconf_Close(struct afsconf_dir *adir)
948 LOCK_GLOBAL_MUTEX afsconf_CloseInternal(adir);
952 UNLOCK_GLOBAL_MUTEX return 0;
956 afsconf_CloseInternal(register struct afsconf_dir *adir)
958 register struct afsconf_entry *td, *nd;
959 struct afsconf_aliasentry *ta, *na;
960 register char *tname;
962 tname = adir->name; /* remember name, since that's all we preserve */
964 /* free everything we can find */
966 free(adir->cellName);
967 for (td = adir->entries; td; td = nd) {
969 if (td->cellInfo.linkedCell)
970 free(td->cellInfo.linkedCell);
973 for (ta = adir->alias_entries; ta; ta = na) {
981 memset(adir, 0, sizeof(struct afsconf_dir));
982 adir->name = tname; /* restore it */
987 afsconf_Reopen(register struct afsconf_dir *adir)
989 register afs_int32 code;
990 code = afsconf_CloseInternal(adir);
993 code = afsconf_OpenInternal(adir, 0, 0);
997 /* called during opening of config file */
999 afsconf_IntGetKeys(struct afsconf_dir *adir)
1003 struct afsconf_keys *tstr;
1004 register afs_int32 code;
1007 /* NT client config dir has no KeyFile; don't risk attempting open
1008 * because there might be a random file of this name if dir is shared.
1010 if (IsClientConfigDirectory(adir->name)) {
1011 adir->keystr = ((struct afsconf_keys *)
1012 malloc(sizeof(struct afsconf_keys)));
1013 adir->keystr->nkeys = 0;
1016 #endif /* AFS_NT40_ENV */
1019 /* compute the key name and other setup */
1020 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1021 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1022 adir->keystr = tstr;
1025 fd = open(tbuffer, O_RDONLY);
1028 UNLOCK_GLOBAL_MUTEX return 0;
1030 code = read(fd, tstr, sizeof(struct afsconf_keys));
1032 if (code < sizeof(afs_int32)) {
1034 UNLOCK_GLOBAL_MUTEX return 0;
1037 /* convert key structure to host order */
1038 tstr->nkeys = ntohl(tstr->nkeys);
1039 for (fd = 0; fd < tstr->nkeys; fd++)
1040 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1042 UNLOCK_GLOBAL_MUTEX return 0;
1045 /* get keys structure */
1047 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1049 register afs_int32 code;
1051 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1053 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1055 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1056 UNLOCK_GLOBAL_MUTEX return 0;
1059 /* get latest key */
1061 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1065 register struct afsconf_key *tk;
1066 register afs_int32 best;
1067 struct afsconf_key *bestk;
1068 register afs_int32 code;
1070 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1072 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1074 maxa = adir->keystr->nkeys;
1076 best = -1; /* highest kvno we've seen yet */
1077 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1078 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1079 if (tk->kvno == 999)
1080 continue; /* skip bcrypt keys */
1081 if (tk->kvno > best) {
1086 if (bestk) { /* found any */
1088 memcpy(akey, bestk->key, 8); /* copy out latest key */
1090 *avno = bestk->kvno; /* and kvno to caller */
1091 UNLOCK_GLOBAL_MUTEX return 0;
1093 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* didn't find any keys */
1096 /* get a particular key */
1098 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1100 register int i, maxa;
1101 register struct afsconf_key *tk;
1102 register afs_int32 code;
1104 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1106 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1108 maxa = adir->keystr->nkeys;
1110 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1111 if (tk->kvno == avno) {
1112 memcpy(akey, tk->key, 8);
1113 UNLOCK_GLOBAL_MUTEX return 0;
1117 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1120 /* save the key structure in the appropriate file */
1122 SaveKeys(struct afsconf_dir *adir)
1124 struct afsconf_keys tkeys;
1126 register afs_int32 i;
1129 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1131 /* convert it to net byte order */
1132 for (i = 0; i < tkeys.nkeys; i++)
1133 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1134 tkeys.nkeys = htonl(tkeys.nkeys);
1136 /* rewrite keys file */
1137 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1138 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1140 return AFSCONF_FAILURE;
1141 i = write(fd, &tkeys, sizeof(tkeys));
1142 if (i != sizeof(tkeys)) {
1144 return AFSCONF_FAILURE;
1147 return AFSCONF_FAILURE;
1152 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1153 afs_int32 overwrite)
1155 register struct afsconf_keys *tk;
1156 register struct afsconf_key *tkey;
1157 register afs_int32 i;
1160 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1163 if (akvno < 0 || akvno > 255) {
1164 UNLOCK_GLOBAL_MUTEX return ERANGE;
1168 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1169 if (tkey->kvno == akvno) {
1171 UNLOCK_GLOBAL_MUTEX return AFSCONF_KEYINUSE;
1178 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1179 UNLOCK_GLOBAL_MUTEX return AFSCONF_FULL;
1181 tkey = &tk->key[tk->nkeys++];
1184 memcpy(tkey->key, akey, 8);
1186 afsconf_Touch(adir);
1187 UNLOCK_GLOBAL_MUTEX return i;
1190 /* this proc works by sliding the other guys down, rather than using a funny
1191 kvno value, so that callers can count on getting a good key in key[0].
1194 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1196 register struct afsconf_keys *tk;
1197 register struct afsconf_key *tkey;
1201 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1203 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1204 if (tkey->kvno == akvno) {
1210 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1213 /* otherwise slide the others down. i and tkey point at the guy to delete */
1214 for (; i < tk->nkeys - 1; i++, tkey++) {
1215 tkey->kvno = (tkey + 1)->kvno;
1216 memcpy(tkey->key, (tkey + 1)->key, 8);
1220 afsconf_Touch(adir);
1221 UNLOCK_GLOBAL_MUTEX return i;