2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <afs/pthread_glock.h>
19 #include "afs/sysincludes.h"
20 #include "afsincludes.h"
22 #include <sys/types.h>
25 #include <sys/utime.h>
27 #include <WINNT/afssw.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
35 #include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 #include <arpa/nameser_compat.h>
40 #endif /* AFS_AFSDB_ENV */
41 #endif /* AFS_NT40_ENV */
42 #include <afs/afsint.h>
61 #include <afs/afsutil.h>
62 #include "cellconfig.h"
66 /* cm_dns.h depends on cellconfig.h */
68 #endif /* AFS_AFSDB_ENV */
70 static struct afsconf_servPair serviceTable[] = {
81 {"afsres", 7010,}, /* residency database for MR-AFS */
82 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
83 {0, 0} /* insert new services before this spot */
87 static afs_int32 afsconf_FindService(register const char *aname);
88 static int TrimLine(char *abuffer);
90 static int IsClientConfigDirectory(const char *path);
91 static int GetCellNT(struct afsconf_dir *adir);
93 static int afsconf_Check(register struct afsconf_dir *adir);
94 static int afsconf_Touch(register struct afsconf_dir *adir);
95 static int GetCellUnix(struct afsconf_dir *adir);
96 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
98 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
99 char *aname, char *aclone);
100 static int ParseCellLine(register char *aline, register char *aname,
101 register char *alname);
102 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
103 static int afsconf_Reopen(register struct afsconf_dir *adir);
104 static int SaveKeys(struct afsconf_dir *adir);
107 #define T_AFSDB 18 /* per RFC1183 section 1 */
111 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
112 * our code can tell if there is new info in the key files, the cell server db
113 * files or any of the other files (and reopen the thing) if the date on
114 * CellServDB changes.
117 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
119 afsconf_FindService(register const char *aname)
121 /* lookup a service name */
123 register struct afsconf_servPair *tsp;
125 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
126 ts = getservbyname(aname, "");
128 ts = getservbyname(aname, NULL);
131 /* we found it in /etc/services, so we use this value */
132 return ts->s_port; /* already in network byte order */
135 /* not found in /etc/services, see if it is one of ours */
136 for (tsp = serviceTable;; tsp++) {
137 if (tsp->name == NULL)
139 if (!strcmp(tsp->name, aname))
140 return htons(tsp->port);
145 TrimLine(char *abuffer)
158 strcpy(abuffer, tbuffer);
164 * IsClientConfigDirectory() -- determine if path matches well-known
165 * client configuration directory.
168 IsClientConfigDirectory(const char *path)
170 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
173 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
174 int cc = tolower(cdir[i]);
175 int pc = tolower(path[i]);
188 /* hit end of one or both; allow mismatch in existence of trailing slash */
189 if (cdir[i] != '\0') {
190 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
194 if (path[i] != '\0') {
195 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
201 #endif /* AFS_NT40_ENV */
205 afsconf_Check(register struct afsconf_dir *adir)
207 char tbuffer[256], *p;
209 register afs_int32 code;
212 /* NT client CellServDB has different file name than NT server or Unix */
213 if (IsClientConfigDirectory(adir->name)) {
214 if ( !afssw_GetClientCellServDBDir(&p) ) {
215 strcompose(tbuffer, sizeof(tbuffer), p, "/",
216 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
220 strncpy(tbuffer, adir->name, sizeof(tbuffer));
221 len = strlen(tbuffer);
222 if ( tbuffer[len-1] != '\\' && tbuffer[len-1] != '/' ) {
223 strncat(tbuffer, "\\", sizeof(tbuffer));
225 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT, sizeof(tbuffer));
226 tbuffer[sizeof(tbuffer)-1] = '\0';
229 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
233 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
234 #endif /* AFS_NT40_ENV */
236 code = stat(tbuffer, &tstat);
240 /* did file change? */
241 if (tstat.st_mtime == adir->timeRead) {
244 /* otherwise file has changed, so reopen it */
245 return afsconf_Reopen(adir);
248 /* set modtime on file */
250 afsconf_Touch(register struct afsconf_dir *adir)
252 char tbuffer[256], *p;
254 struct timeval tvp[2];
257 adir->timeRead = 0; /* just in case */
260 /* NT client CellServDB has different file name than NT server or Unix */
262 if (IsClientConfigDirectory(adir->name)) {
263 if ( !afssw_GetClientCellServDBDir(&p) ) {
264 strcompose(tbuffer, sizeof(tbuffer), p, "/",
265 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
268 int len = strlen(tbuffer);
269 if ( tbuffer[len-1] != '\\' && tbuffer[len-1] != '/' ) {
270 strncat(tbuffer, "\\", sizeof(tbuffer));
272 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT, sizeof(tbuffer));
273 tbuffer[sizeof(tbuffer)-1] = '\0';
276 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
280 return _utime(tbuffer, NULL);
283 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
284 gettimeofday(&tvp[0], NULL);
286 return utimes(tbuffer, tvp);
287 #endif /* AFS_NT40_ENV */
291 afsconf_Open(register const char *adir)
293 register struct afsconf_dir *tdir;
294 register afs_int32 code;
297 /* zero structure and fill in name; rest is done by internal routine */
298 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
299 memset(tdir, 0, sizeof(struct afsconf_dir));
300 tdir->name = (char *)malloc(strlen(adir) + 1);
301 strcpy(tdir->name, adir);
303 code = afsconf_OpenInternal(tdir, 0, 0);
305 char *afsconf_path, afs_confdir[128];
308 /* Check global place only when local Open failed for whatever reason */
309 if (!(afsconf_path = getenv("AFSCONF"))) {
310 /* 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... */
315 if (!(home_dir = getenv("HOME"))) {
316 /* Our last chance is the "/.AFSCONF" file */
317 fp = fopen("/.AFSCONF", "r");
320 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
322 fgets(afs_confdir, 128, fp);
327 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
328 fp = fopen(pathname, "r");
330 /* Our last chance is the "/.AFSCONF" file */
331 fp = fopen("/.AFSCONF", "r");
334 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
337 fgets(afs_confdir, 128, fp);
340 len = strlen(afs_confdir);
343 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
345 if (afs_confdir[len - 1] == '\n') {
346 afs_confdir[len - 1] = 0;
348 afsconf_path = afs_confdir;
350 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
351 strcpy(tdir->name, afsconf_path);
352 code = afsconf_OpenInternal(tdir, 0, 0);
356 UNLOCK_GLOBAL_MUTEX return (struct afsconf_dir *)0;
359 UNLOCK_GLOBAL_MUTEX return tdir;
364 GetCellUnix(struct afsconf_dir *adir)
370 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
371 tf = fopen(tbuffer, "r");
373 /* FIXME: buffer overflow waiting to happen */
374 rc = fscanf(tf, "%s", tbuffer);
376 adir->cellName = (char *)malloc(strlen(tbuffer) + 1);
377 strcpy(adir->cellName, tbuffer);
389 GetCellNT(struct afsconf_dir *adir)
391 if (IsClientConfigDirectory(adir->name)) {
392 /* NT client config dir; ThisCell is in registry (no file). */
393 return afssw_GetClientCellName(&adir->cellName);
395 /* NT server config dir; works just like Unix */
396 return GetCellUnix(adir);
399 #endif /* AFS_NT40_ENV */
403 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
407 register char *tp, *bp;
408 register struct afsconf_entry *curEntry;
409 struct afsconf_aliasentry *curAlias;
410 register afs_int32 code;
412 char tbuffer[256], tbuf1[256];
415 /* figure out the cell name */
419 i = GetCellUnix(adir);
422 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
428 /* now parse the individual lines */
432 /* NT client/server have a CellServDB that is the same format as Unix.
433 * However, the NT client uses a different file name
435 if (IsClientConfigDirectory(adir->name)) {
436 /* NT client config dir */
438 if ( !afssw_GetClientCellServDBDir(&p) ) {
439 strcompose(tbuffer, sizeof(tbuffer), p, "/",
440 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
444 strncpy(tbuffer, adir->name, sizeof(tbuffer));
445 len = strlen(tbuffer);
446 if ( tbuffer[len-1] != '\\' && tbuffer[len-1] != '/' ) {
447 strncat(tbuffer, "\\", sizeof(tbuffer));
449 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT, sizeof(tbuffer));
450 tbuffer[sizeof(tbuffer)-1] = '\0';
453 /* NT server config dir */
454 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
458 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
459 #endif /* AFS_NT40_ENV */
461 if (!stat(tbuffer, &tstat)) {
462 adir->timeRead = tstat.st_mtime;
467 strcpy(tbuf1, tbuffer);
468 tf = fopen(tbuffer, "r");
473 tp = fgets(tbuffer, sizeof(tbuffer), tf);
476 TrimLine(tbuffer); /* remove white space */
477 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
478 continue; /* empty line */
479 if (tbuffer[0] == '>') {
480 char linkedcell[MAXCELLCHARS];
481 /* start new cell item */
483 /* thread this guy on the list */
484 curEntry->next = adir->entries;
485 adir->entries = curEntry;
489 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
490 memset(curEntry, 0, sizeof(struct afsconf_entry));
492 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
494 afsconf_CloseInternal(adir);
499 if (linkedcell[0] != '\0') {
500 curEntry->cellInfo.linkedCell =
501 (char *)malloc(strlen(linkedcell) + 1);
502 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
505 /* new host in the current cell */
507 afsconf_CloseInternal(adir);
511 i = curEntry->cellInfo.numServers;
512 if (cell && !strcmp(cell, curEntry->cellInfo.name))
514 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
515 curEntry->cellInfo.hostName[i], &clones[i]);
518 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
519 curEntry->cellInfo.hostName[i], 0);
521 if (code == AFSCONF_SYNTAX) {
522 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
528 "Can't properly parse host line \"%s\" in configuration file %s\n",
533 afsconf_CloseInternal(adir);
536 curEntry->cellInfo.numServers = ++i;
539 fclose(tf); /* close the file now */
541 /* end the last partially-completed cell */
543 curEntry->next = adir->entries;
544 adir->entries = curEntry;
547 /* Read in the alias list */
548 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
550 tf = fopen(tbuffer, "r");
554 tp = fgets(tbuffer, sizeof(tbuffer), tf);
557 TrimLine(tbuffer); /* remove white space */
559 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
560 continue; /* empty line */
563 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
566 continue; /* invalid line */
568 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
571 continue; /* invalid line */
574 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
579 curAlias = malloc(sizeof(*curAlias));
580 memset(curAlias, 0, sizeof(*curAlias));
582 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
583 strcpy(curAlias->aliasInfo.realName, tbuffer);
585 curAlias->next = adir->alias_entries;
586 adir->alias_entries = curAlias;
591 /* now read the fs keys, if possible */
592 adir->keystr = (struct afsconf_keys *)0;
593 afsconf_IntGetKeys(adir);
598 /* parse a line of the form
599 *"128.2.1.3 #hostname" or
600 *"[128.2.1.3] #hostname" for clones
601 * into the appropriate pieces.
604 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
608 register afs_int32 code;
614 /* FIXME: length of aname unknown here */
615 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
619 /* FIXME: length of aname unknown here */
620 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
623 return AFSCONF_SYNTAX;
624 addr->sin_family = AF_INET;
626 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
627 addr->sin_len = sizeof(struct sockaddr_in);
629 tp = (char *)&addr->sin_addr;
637 /* parse a line of the form
638 * ">cellname [linkedcellname] [#comments]"
639 * into the appropriate pieces.
642 ParseCellLine(register char *aline, register char *aname,
643 register char *alname)
646 /* FIXME: length of aname, alname unknown here */
647 code = sscanf(aline, ">%s %s", aname, alname);
651 if (*alname == '#') {
655 return (code > 0 ? 0 : AFSCONF_SYNTAX);
658 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
660 afsconf_CellApply(struct afsconf_dir *adir,
661 int (*aproc) (struct afsconf_cell * cell, char *arock,
662 struct afsconf_dir * dir), char *arock)
664 register struct afsconf_entry *tde;
665 register afs_int32 code;
666 LOCK_GLOBAL_MUTEX for (tde = adir->entries; tde; tde = tde->next) {
667 code = (*aproc) (&tde->cellInfo, arock, adir);
669 UNLOCK_GLOBAL_MUTEX return code;
672 UNLOCK_GLOBAL_MUTEX return 0;
675 /* call aproc(entry, arock, adir) for all cell aliases.
676 * Proc must return 0, or we'll stop early and return the code it returns
679 afsconf_CellAliasApply(struct afsconf_dir *adir,
680 int (*aproc) (struct afsconf_cellalias * alias,
681 char *arock, struct afsconf_dir * dir),
684 register struct afsconf_aliasentry *tde;
685 register afs_int32 code;
686 LOCK_GLOBAL_MUTEX for (tde = adir->alias_entries; tde; tde = tde->next) {
687 code = (*aproc) (&tde->aliasInfo, arock, adir);
689 UNLOCK_GLOBAL_MUTEX return code;
692 UNLOCK_GLOBAL_MUTEX return 0;
695 afs_int32 afsconf_SawCell = 0;
698 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
699 char *aservice, struct afsconf_cell *acellInfo,
705 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
712 cell = (char *)&acellInfo->name;
714 code = afsconf_OpenInternal(adir, cell, clones);
719 #if !defined(AFS_NT40_ENV)
721 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
722 struct afsconf_cell *acellInfo)
727 unsigned char answer[1024];
729 char realCellName[256];
734 /* The resolver isn't always MT-safe.. Perhaps this ought to be
735 * replaced with a more fine-grained lock just for the resolver
738 LOCK_GLOBAL_MUTEX len =
739 res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
740 UNLOCK_GLOBAL_MUTEX if (len < 0)
741 return AFSCONF_NOTFOUND;
743 p = answer + sizeof(HEADER); /* Skip header */
744 code = dn_expand(answer, answer + len, p, host, sizeof(host));
746 return AFSCONF_NOTFOUND;
748 p += code + QFIXEDSZ; /* Skip name */
750 while (p < answer + len) {
753 code = dn_expand(answer, answer + len, p, host, sizeof(host));
755 return AFSCONF_NOTFOUND;
757 p += code; /* Skip the name */
758 type = (p[0] << 8) | p[1];
759 p += 4; /* Skip type and class */
760 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
761 p += 4; /* Skip the TTL */
762 size = (p[0] << 8) | p[1];
763 p += 2; /* Skip the size */
765 if (type == T_AFSDB) {
769 afsdb_type = (p[0] << 8) | p[1];
770 if (afsdb_type == 1) {
772 * We know this is an AFSDB record for our cell, of the
773 * right AFSDB type. Write down the true cell name that
774 * the resolver gave us above.
776 strcpy(realCellName, host);
779 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
781 return AFSCONF_NOTFOUND;
783 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
784 /* Do we want to get TTL data for the A record as well? */
785 (he = gethostbyname(host))) {
787 memcpy(&ipaddr, he->h_addr, he->h_length);
788 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
789 strncpy(acellInfo->hostName[server_num], host,
790 sizeof(acellInfo->hostName[server_num]));
793 if (!minttl || ttl < minttl)
801 if (server_num == 0) /* No AFSDB records */
802 return AFSCONF_NOTFOUND;
804 /* Convert the real cell name to lowercase */
805 for (p = (unsigned char *)realCellName; *p; p++)
808 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
809 acellInfo->numServers = server_num;
812 tservice = afsconf_FindService(aservice);
814 return AFSCONF_NOTFOUND; /* service not found */
815 for (i = 0; i < acellInfo->numServers; i++) {
816 acellInfo->hostAddr[i].sin_port = tservice;
820 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
826 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
827 struct afsconf_cell *acellInfo)
829 register afs_int32 i;
831 struct afsconf_entry DNSce;
832 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
833 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
838 DNSce.cellInfo.numServers = 0;
840 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers, &ttl);
841 /* ignore the ttl here since this code is only called by transitory programs
848 for (i = 0; i < numServers; i++) {
849 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
851 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
852 acellInfo->hostAddr[i].sin_family = AF_INET;
854 /* sin_port supplied by connection code */
857 acellInfo->numServers = numServers;
858 strcpy(acellInfo->name, acellName);
860 LOCK_GLOBAL_MUTEX tservice = afsconf_FindService(aservice);
861 UNLOCK_GLOBAL_MUTEX if (tservice < 0) {
862 return AFSCONF_NOTFOUND; /* service not found */
864 for (i = 0; i < acellInfo->numServers; i++) {
865 acellInfo->hostAddr[i].sin_port = tservice;
868 acellInfo->linkedCell = NULL; /* no linked cell */
869 acellInfo->flags = 0;
873 #endif /* AFS_AFSDB_ENV */
876 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
877 struct afsconf_cell *acellInfo)
879 register struct afsconf_entry *tce;
880 struct afsconf_aliasentry *tcae;
881 struct afsconf_entry *bestce;
882 register afs_int32 i;
889 LOCK_GLOBAL_MUTEX if (adir)
893 cnLen = strlen(tcell) + 1;
894 lcstring(tcell, tcell, cnLen);
895 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
896 /* call to afsconf_GetLocalCell: like klog */
898 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
900 UNLOCK_GLOBAL_MUTEX return i;
904 cnLen = strlen(tcell);
905 bestce = (struct afsconf_entry *)0;
908 UNLOCK_GLOBAL_MUTEX return 0;
911 /* Look through the list of aliases */
912 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
913 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
914 tcell = tcae->aliasInfo.realName;
919 for (tce = adir->entries; tce; tce = tce->next) {
920 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
926 if (strlen(tce->cellInfo.name) < cnLen)
927 continue; /* clearly wrong */
928 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
930 ambig = 1; /* ambiguous unless we get exact match */
934 if (!ambig && bestce && bestce->cellInfo.numServers) {
935 *acellInfo = bestce->cellInfo; /* structure assignment */
937 tservice = afsconf_FindService(aservice);
939 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* service not found */
941 for (i = 0; i < acellInfo->numServers; i++) {
942 acellInfo->hostAddr[i].sin_port = tservice;
945 acellInfo->timeout = 0;
946 UNLOCK_GLOBAL_MUTEX return 0;
950 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
952 return AFSCONF_NOTFOUND;
953 #endif /* AFS_AFSDB_ENV */
958 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
961 static int afsconf_showcell = 0;
967 * If a cell switch was specified in a command, then it should override the
968 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
969 * is set and the cell name in the adir structure is used.
970 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
972 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
973 if (!afsconf_showcell) {
974 fprintf(stderr, "Note: Operation is performed on cell %s\n",
976 afsconf_showcell = 1;
978 strncpy(aname, afscell_path, alen);
981 if (adir->cellName) {
982 strncpy(aname, adir->cellName, alen);
984 code = AFSCONF_UNKNOWN;
987 UNLOCK_GLOBAL_MUTEX return (code);
991 afsconf_Close(struct afsconf_dir *adir)
993 LOCK_GLOBAL_MUTEX afsconf_CloseInternal(adir);
997 UNLOCK_GLOBAL_MUTEX return 0;
1001 afsconf_CloseInternal(register struct afsconf_dir *adir)
1003 register struct afsconf_entry *td, *nd;
1004 struct afsconf_aliasentry *ta, *na;
1005 register char *tname;
1007 tname = adir->name; /* remember name, since that's all we preserve */
1009 /* free everything we can find */
1011 free(adir->cellName);
1012 for (td = adir->entries; td; td = nd) {
1014 if (td->cellInfo.linkedCell)
1015 free(td->cellInfo.linkedCell);
1018 for (ta = adir->alias_entries; ta; ta = na) {
1026 memset(adir, 0, sizeof(struct afsconf_dir));
1027 adir->name = tname; /* restore it */
1032 afsconf_Reopen(register struct afsconf_dir *adir)
1034 register afs_int32 code;
1035 code = afsconf_CloseInternal(adir);
1038 code = afsconf_OpenInternal(adir, 0, 0);
1042 /* called during opening of config file */
1044 afsconf_IntGetKeys(struct afsconf_dir *adir)
1048 struct afsconf_keys *tstr;
1049 register afs_int32 code;
1052 /* NT client config dir has no KeyFile; don't risk attempting open
1053 * because there might be a random file of this name if dir is shared.
1055 if (IsClientConfigDirectory(adir->name)) {
1056 adir->keystr = ((struct afsconf_keys *)
1057 malloc(sizeof(struct afsconf_keys)));
1058 adir->keystr->nkeys = 0;
1061 #endif /* AFS_NT40_ENV */
1064 /* compute the key name and other setup */
1065 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1066 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1067 adir->keystr = tstr;
1070 fd = open(tbuffer, O_RDONLY);
1073 UNLOCK_GLOBAL_MUTEX return 0;
1075 code = read(fd, tstr, sizeof(struct afsconf_keys));
1077 if (code < sizeof(afs_int32)) {
1079 UNLOCK_GLOBAL_MUTEX return 0;
1082 /* convert key structure to host order */
1083 tstr->nkeys = ntohl(tstr->nkeys);
1084 for (fd = 0; fd < tstr->nkeys; fd++)
1085 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1087 UNLOCK_GLOBAL_MUTEX return 0;
1090 /* get keys structure */
1092 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1094 register afs_int32 code;
1096 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1098 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1100 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1101 UNLOCK_GLOBAL_MUTEX return 0;
1104 /* get latest key */
1106 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *akey)
1110 register struct afsconf_key *tk;
1111 register afs_int32 best;
1112 struct afsconf_key *bestk;
1113 register afs_int32 code;
1115 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1117 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1119 maxa = adir->keystr->nkeys;
1121 best = -1; /* highest kvno we've seen yet */
1122 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1123 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1124 if (tk->kvno == 999)
1125 continue; /* skip bcrypt keys */
1126 if (tk->kvno > best) {
1131 if (bestk) { /* found any */
1133 memcpy(akey, bestk->key, 8); /* copy out latest key */
1135 *avno = bestk->kvno; /* and kvno to caller */
1136 UNLOCK_GLOBAL_MUTEX return 0;
1138 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND; /* didn't find any keys */
1141 /* get a particular key */
1143 afsconf_GetKey(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1145 register int i, maxa;
1146 register struct afsconf_key *tk;
1147 register afs_int32 code;
1149 LOCK_GLOBAL_MUTEX code = afsconf_Check(adir);
1151 UNLOCK_GLOBAL_MUTEX return AFSCONF_FAILURE;
1153 maxa = adir->keystr->nkeys;
1155 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1156 if (tk->kvno == avno) {
1157 memcpy(akey, tk->key, 8);
1158 UNLOCK_GLOBAL_MUTEX return 0;
1162 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1165 /* save the key structure in the appropriate file */
1167 SaveKeys(struct afsconf_dir *adir)
1169 struct afsconf_keys tkeys;
1171 register afs_int32 i;
1174 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1176 /* convert it to net byte order */
1177 for (i = 0; i < tkeys.nkeys; i++)
1178 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1179 tkeys.nkeys = htonl(tkeys.nkeys);
1181 /* rewrite keys file */
1182 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1183 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1185 return AFSCONF_FAILURE;
1186 i = write(fd, &tkeys, sizeof(tkeys));
1187 if (i != sizeof(tkeys)) {
1189 return AFSCONF_FAILURE;
1192 return AFSCONF_FAILURE;
1197 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1198 afs_int32 overwrite)
1200 register struct afsconf_keys *tk;
1201 register struct afsconf_key *tkey;
1202 register afs_int32 i;
1205 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1208 if (akvno < 0 || akvno > 255) {
1209 UNLOCK_GLOBAL_MUTEX return ERANGE;
1213 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1214 if (tkey->kvno == akvno) {
1216 UNLOCK_GLOBAL_MUTEX return AFSCONF_KEYINUSE;
1223 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1224 UNLOCK_GLOBAL_MUTEX return AFSCONF_FULL;
1226 tkey = &tk->key[tk->nkeys++];
1229 memcpy(tkey->key, akey, 8);
1231 afsconf_Touch(adir);
1232 UNLOCK_GLOBAL_MUTEX return i;
1235 /* this proc works by sliding the other guys down, rather than using a funny
1236 kvno value, so that callers can count on getting a good key in key[0].
1239 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1241 register struct afsconf_keys *tk;
1242 register struct afsconf_key *tkey;
1246 LOCK_GLOBAL_MUTEX tk = adir->keystr;
1248 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1249 if (tkey->kvno == akvno) {
1255 UNLOCK_GLOBAL_MUTEX return AFSCONF_NOTFOUND;
1258 /* otherwise slide the others down. i and tkey point at the guy to delete */
1259 for (; i < tk->nkeys - 1; i++, tkey++) {
1260 tkey->kvno = (tkey + 1)->kvno;
1261 memcpy(tkey->key, (tkey + 1)->key, 8);
1265 afsconf_Touch(adir);
1266 UNLOCK_GLOBAL_MUTEX return i;