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"
24 #include <sys/types.h>
27 #include <sys/utime.h>
29 #include <WINNT/afssw.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
37 #include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 #include <arpa/nameser_compat.h>
42 #endif /* AFS_AFSDB_ENV */
43 #endif /* AFS_NT40_ENV */
44 #include <afs/afsint.h>
59 #include <afs/afsutil.h>
60 #include "cellconfig.h"
64 /* cm_dns.h depends on cellconfig.h */
67 #endif /* AFS_AFSDB_ENV */
69 static struct afsconf_servPair serviceTable[] = {
80 {"afsres", 7010,}, /* residency database for MR-AFS */
81 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
82 {0, 0} /* insert new services before this spot */
86 static afs_int32 afsconf_FindService(register const char *aname);
87 static int TrimLine(char *abuffer, int abufsize);
89 static int IsClientConfigDirectory(const char *path);
90 static int GetCellNT(struct afsconf_dir *adir);
92 static int afsconf_Check(register struct afsconf_dir *adir);
93 static int afsconf_Touch(register struct afsconf_dir *adir);
94 static int GetCellUnix(struct afsconf_dir *adir);
95 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
97 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
98 char *aname, char *aclone);
99 static int ParseCellLine(register char *aline, register char *aname,
100 register char *alname);
101 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
102 static int afsconf_Reopen(register struct afsconf_dir *adir);
103 static int SaveKeys(struct afsconf_dir *adir);
106 #define T_AFSDB 18 /* per RFC1183 section 1 */
110 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
111 * our code can tell if there is new info in the key files, the cell server db
112 * files or any of the other files (and reopen the thing) if the date on
113 * CellServDB changes.
116 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
117 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
118 get an fd <= 255. We allow the fileserver to claim more fds than that.
119 This has always been a problem since pr_Initialize would have the same
120 issue, but hpr_Initialize makes it more likely that we would see this.
121 Work around it. This is not generic. It's coded with the needs of
122 afsconf_* in mind only.
124 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
129 struct afsconf_iobuffer {
136 typedef struct afsconf_iobuffer afsconf_FILE;
138 static afsconf_FILE *
139 afsconf_fopen(const char *fname, const char *fmode)
144 if ((fd = open(fname, O_RDONLY)) == -1) {
148 iop = malloc(sizeof(struct afsconf_iobuffer));
155 iop->buffer = malloc(BUFFER);
156 if (iop->buffer == NULL) {
162 iop->ptr = iop->buffer;
163 iop->endptr = iop->buffer;
168 afsconf_fclose(afsconf_FILE *iop)
174 free((void *)iop->buffer);
180 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
188 if (iop->ptr == iop->endptr) {
191 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
201 iop->ptr = iop->buffer;
202 iop->endptr = iop->buffer + len;
206 if ((p - s) == (n - 1)) {
216 #define fopen afsconf_fopen
217 #define fclose afsconf_fclose
218 #define fgets afsconf_fgets
220 #define afsconf_FILE FILE
221 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
223 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
225 afsconf_FindService(register const char *aname)
227 /* lookup a service name */
229 register struct afsconf_servPair *tsp;
231 #if defined(AFS_OSF_ENV)
232 ts = getservbyname(aname, "");
234 ts = getservbyname(aname, NULL);
237 /* we found it in /etc/services, so we use this value */
238 return ts->s_port; /* already in network byte order */
241 /* not found in /etc/services, see if it is one of ours */
242 for (tsp = serviceTable;; tsp++) {
243 if (tsp->name == NULL)
245 if (!strcmp(tsp->name, aname))
246 return htons(tsp->port);
251 TrimLine(char *abuffer, int abufsize)
263 strlcpy(tbuffer, tp, sizeof tbuffer);
264 strlcpy(abuffer, tbuffer, abufsize);
270 * IsClientConfigDirectory() -- determine if path matches well-known
271 * client configuration directory.
274 IsClientConfigDirectory(const char *path)
276 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
279 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
280 int cc = tolower(cdir[i]);
281 int pc = tolower(path[i]);
294 /* hit end of one or both; allow mismatch in existence of trailing slash */
295 if (cdir[i] != '\0') {
296 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
300 if (path[i] != '\0') {
301 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
307 #endif /* AFS_NT40_ENV */
311 afsconf_Check(register struct afsconf_dir *adir)
318 register afs_int32 code;
321 /* NT client CellServDB has different file name than NT server or Unix */
322 if (IsClientConfigDirectory(adir->name)) {
323 if (!afssw_GetClientCellServDBDir(&p)) {
324 strcompose(tbuffer, sizeof(tbuffer), p, "/",
325 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
329 strncpy(tbuffer, adir->name, sizeof(tbuffer));
330 len = (int)strlen(tbuffer);
331 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
332 strncat(tbuffer, "\\", sizeof(tbuffer));
334 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
336 tbuffer[sizeof(tbuffer) - 1] = '\0';
339 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
343 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
344 #endif /* AFS_NT40_ENV */
346 code = stat(tbuffer, &tstat);
350 /* did file change? */
351 if (tstat.st_mtime == adir->timeRead) {
354 /* otherwise file has changed, so reopen it */
355 return afsconf_Reopen(adir);
358 /* set modtime on file */
360 afsconf_Touch(register struct afsconf_dir *adir)
364 struct timeval tvp[2];
369 adir->timeRead = 0; /* just in case */
372 /* NT client CellServDB has different file name than NT server or Unix */
374 if (IsClientConfigDirectory(adir->name)) {
375 if (!afssw_GetClientCellServDBDir(&p)) {
376 strcompose(tbuffer, sizeof(tbuffer), p, "/",
377 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
380 int len = (int)strlen(tbuffer);
381 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
382 strncat(tbuffer, "\\", sizeof(tbuffer));
384 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
386 tbuffer[sizeof(tbuffer) - 1] = '\0';
389 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
393 return _utime(tbuffer, NULL);
396 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
397 gettimeofday(&tvp[0], NULL);
399 return utimes(tbuffer, tvp);
400 #endif /* AFS_NT40_ENV */
404 afsconf_Open(register const char *adir)
406 register struct afsconf_dir *tdir;
407 register afs_int32 code;
410 /* zero structure and fill in name; rest is done by internal routine */
411 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
412 memset(tdir, 0, sizeof(struct afsconf_dir));
413 tdir->name = strdup(adir);
415 code = afsconf_OpenInternal(tdir, 0, 0);
417 char *afsconf_path, afs_confdir[128];
420 /* Check global place only when local Open failed for whatever reason */
421 if (!(afsconf_path = getenv("AFSCONF"))) {
422 /* 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... */
427 if (!(home_dir = getenv("HOME"))) {
428 /* Our last chance is the "/.AFSCONF" file */
429 fp = fopen("/.AFSCONF", "r");
433 return (struct afsconf_dir *)0;
435 fgets(afs_confdir, 128, fp);
440 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
441 fp = fopen(pathname, "r");
443 /* Our last chance is the "/.AFSCONF" file */
444 fp = fopen("/.AFSCONF", "r");
448 return (struct afsconf_dir *)0;
451 fgets(afs_confdir, 128, fp);
454 len = strlen(afs_confdir);
458 return (struct afsconf_dir *)0;
460 if (afs_confdir[len - 1] == '\n') {
461 afs_confdir[len - 1] = 0;
463 afsconf_path = afs_confdir;
465 tdir->name = strdup(afsconf_path);
466 code = afsconf_OpenInternal(tdir, 0, 0);
471 return (struct afsconf_dir *)0;
479 GetCellUnix(struct afsconf_dir *adir)
486 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
487 fp = fopen(tbuffer, "r");
491 rc = fgets(tbuffer, 256, fp);
497 while (*start != '\0' && isspace(*start))
500 while (*p != '\0' && !isspace(*p))
506 adir->cellName = strdup(start);
513 GetCellNT(struct afsconf_dir *adir)
515 if (IsClientConfigDirectory(adir->name)) {
516 /* NT client config dir; ThisCell is in registry (no file). */
517 return afssw_GetClientCellName(&adir->cellName);
519 /* NT server config dir; works just like Unix */
520 return GetCellUnix(adir);
523 #endif /* AFS_NT40_ENV */
527 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
531 register char *tp, *bp;
532 register struct afsconf_entry *curEntry;
533 struct afsconf_aliasentry *curAlias;
534 register afs_int32 code;
536 char tbuffer[256], tbuf1[256];
539 /* figure out the local cell name */
543 i = GetCellUnix(adir);
546 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
552 /* now parse the individual lines */
556 /* NT client/server have a CellServDB that is the same format as Unix.
557 * However, the NT client uses a different file name
559 if (IsClientConfigDirectory(adir->name)) {
560 /* NT client config dir */
562 if (!afssw_GetClientCellServDBDir(&p)) {
563 strcompose(tbuffer, sizeof(tbuffer), p, "/",
564 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
568 strncpy(tbuffer, adir->name, sizeof(tbuffer));
569 len = (int)strlen(tbuffer);
570 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
571 strncat(tbuffer, "\\", sizeof(tbuffer));
573 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
575 tbuffer[sizeof(tbuffer) - 1] = '\0';
578 /* NT server config dir */
579 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
583 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
584 #endif /* AFS_NT40_ENV */
586 if (!stat(tbuffer, &tstat)) {
587 adir->timeRead = tstat.st_mtime;
592 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
593 tf = fopen(tbuffer, "r");
598 tp = fgets(tbuffer, sizeof(tbuffer), tf);
601 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
602 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
603 continue; /* empty line */
604 if (tbuffer[0] == '>') {
605 char linkedcell[MAXCELLCHARS];
606 /* start new cell item */
608 /* thread this guy on the list */
609 curEntry->next = adir->entries;
610 adir->entries = curEntry;
614 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
615 memset(curEntry, 0, sizeof(struct afsconf_entry));
617 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
619 afsconf_CloseInternal(adir);
624 if (linkedcell[0] != '\0')
625 curEntry->cellInfo.linkedCell = strdup(linkedcell);
627 /* new host in the current cell */
629 afsconf_CloseInternal(adir);
633 i = curEntry->cellInfo.numServers;
634 if (cell && !strcmp(cell, curEntry->cellInfo.name))
636 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
637 curEntry->cellInfo.hostName[i], &clones[i]);
640 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
641 curEntry->cellInfo.hostName[i], 0);
643 if (code == AFSCONF_SYNTAX) {
644 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
650 "Can't properly parse host line \"%s\" in configuration file %s\n",
655 afsconf_CloseInternal(adir);
658 curEntry->cellInfo.numServers = ++i;
661 fclose(tf); /* close the file now */
663 /* end the last partially-completed cell */
665 curEntry->next = adir->entries;
666 adir->entries = curEntry;
669 /* Read in the alias list */
670 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
672 tf = fopen(tbuffer, "r");
676 tp = fgets(tbuffer, sizeof(tbuffer), tf);
679 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
681 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
682 continue; /* empty line */
685 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
688 continue; /* invalid line */
690 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
693 continue; /* invalid line */
696 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
701 curAlias = malloc(sizeof(*curAlias));
702 memset(curAlias, 0, sizeof(*curAlias));
704 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
705 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
707 curAlias->next = adir->alias_entries;
708 adir->alias_entries = curAlias;
713 /* now read the fs keys, if possible */
714 adir->keystr = (struct afsconf_keys *)0;
715 afsconf_IntGetKeys(adir);
720 /* parse a line of the form
721 *"128.2.1.3 #hostname" or
722 *"[128.2.1.3] #hostname" for clones
723 * into the appropriate pieces.
726 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
730 register afs_int32 code;
736 /* FIXME: length of aname unknown here */
737 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
741 /* FIXME: length of aname unknown here */
742 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
745 return AFSCONF_SYNTAX;
746 addr->sin_family = AF_INET;
748 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
749 addr->sin_len = sizeof(struct sockaddr_in);
751 tp = (char *)&addr->sin_addr;
759 /* parse a line of the form
760 * ">cellname [linkedcellname] [#comments]"
761 * into the appropriate pieces.
764 ParseCellLine(register char *aline, register char *aname,
765 register char *alname)
768 /* FIXME: length of aname, alname unknown here */
769 code = sscanf(aline, ">%s %s", aname, alname);
773 if (*alname == '#') {
777 return (code > 0 ? 0 : AFSCONF_SYNTAX);
780 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
782 afsconf_CellApply(struct afsconf_dir *adir,
783 int (*aproc) (struct afsconf_cell * cell, void *arock,
784 struct afsconf_dir * dir), void *arock)
786 register struct afsconf_entry *tde;
787 register afs_int32 code;
789 for (tde = adir->entries; tde; tde = tde->next) {
790 code = (*aproc) (&tde->cellInfo, arock, adir);
800 /* call aproc(entry, arock, adir) for all cell aliases.
801 * Proc must return 0, or we'll stop early and return the code it returns
804 afsconf_CellAliasApply(struct afsconf_dir *adir,
805 int (*aproc) (struct afsconf_cellalias * alias,
806 void *arock, struct afsconf_dir * dir),
809 register struct afsconf_aliasentry *tde;
810 register afs_int32 code;
812 for (tde = adir->alias_entries; tde; tde = tde->next) {
813 code = (*aproc) (&tde->aliasInfo, arock, adir);
823 afs_int32 afsconf_SawCell = 0;
826 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
827 char *aservice, struct afsconf_cell *acellInfo,
833 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
840 cell = (char *)&acellInfo->name;
842 code = afsconf_OpenInternal(adir, cell, clones);
847 #if !defined(AFS_NT40_ENV)
849 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
850 struct afsconf_cell *acellInfo)
853 int tservice, i, len;
854 unsigned char answer[1024];
858 char realCellName[256];
864 /* The resolver isn't always MT-safe.. Perhaps this ought to be
865 * replaced with a more fine-grained lock just for the resolver
870 if ( ! strchr(acellName,'.') ) {
871 cellnamelength=strlen(acellName);
872 dotcellname=malloc(cellnamelength+2);
873 memcpy(dotcellname,acellName,cellnamelength);
874 dotcellname[cellnamelength]='.';
875 dotcellname[cellnamelength+1]=0;
877 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
879 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
885 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
894 return AFSCONF_NOTFOUND;
897 p = answer + sizeof(HEADER); /* Skip header */
898 code = dn_expand(answer, answer + len, p, host, sizeof(host));
900 return AFSCONF_NOTFOUND;
902 p += code + QFIXEDSZ; /* Skip name */
904 while (p < answer + len) {
907 code = dn_expand(answer, answer + len, p, host, sizeof(host));
909 return AFSCONF_NOTFOUND;
911 p += code; /* Skip the name */
912 type = (p[0] << 8) | p[1];
913 p += 4; /* Skip type and class */
914 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
915 p += 4; /* Skip the TTL */
916 size = (p[0] << 8) | p[1];
917 p += 2; /* Skip the size */
919 if (type == T_AFSDB) {
923 afsdb_type = (p[0] << 8) | p[1];
924 if (afsdb_type == 1) {
926 * We know this is an AFSDB record for our cell, of the
927 * right AFSDB type. Write down the true cell name that
928 * the resolver gave us above.
930 strlcpy(realCellName, host, sizeof realCellName);
933 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
935 return AFSCONF_NOTFOUND;
937 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
938 /* Do we want to get TTL data for the A record as well? */
939 (he = gethostbyname(host))) {
941 memcpy(&ipaddr, he->h_addr, he->h_length);
942 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
943 strncpy(acellInfo->hostName[server_num], host,
944 sizeof(acellInfo->hostName[server_num]));
947 if (!minttl || ttl < minttl)
955 if (server_num == 0) /* No AFSDB records */
956 return AFSCONF_NOTFOUND;
958 /* Convert the real cell name to lowercase */
959 for (p = (unsigned char *)realCellName; *p; p++)
962 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
963 acellInfo->numServers = server_num;
966 tservice = afsconf_FindService(aservice);
968 return AFSCONF_NOTFOUND; /* service not found */
969 for (i = 0; i < acellInfo->numServers; i++) {
970 acellInfo->hostAddr[i].sin_port = tservice;
974 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
980 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
981 struct afsconf_cell *acellInfo)
983 register afs_int32 i;
985 struct afsconf_entry DNSce;
986 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
987 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
992 DNSce.cellInfo.numServers = 0;
994 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
996 /* ignore the ttl here since this code is only called by transitory programs
1000 if (numServers == 0)
1003 for (i = 0; i < numServers; i++) {
1004 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1006 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1007 acellInfo->hostAddr[i].sin_family = AF_INET;
1009 /* sin_port supplied by connection code */
1012 acellInfo->numServers = numServers;
1013 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1016 tservice = afsconf_FindService(aservice);
1017 UNLOCK_GLOBAL_MUTEX;
1019 return AFSCONF_NOTFOUND; /* service not found */
1021 for (i = 0; i < acellInfo->numServers; i++) {
1022 acellInfo->hostAddr[i].sin_port = tservice;
1025 acellInfo->linkedCell = NULL; /* no linked cell */
1026 acellInfo->flags = 0;
1029 #endif /* windows */
1030 #endif /* AFS_AFSDB_ENV */
1033 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1034 struct afsconf_cell *acellInfo)
1036 register struct afsconf_entry *tce;
1037 struct afsconf_aliasentry *tcae;
1038 struct afsconf_entry *bestce;
1039 register afs_int32 i;
1048 afsconf_Check(adir);
1051 cnLen = (int)(strlen(tcell) + 1);
1052 lcstring(tcell, tcell, cnLen);
1053 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1054 /* call to afsconf_GetLocalCell: like klog */
1056 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1058 UNLOCK_GLOBAL_MUTEX;
1063 cnLen = strlen(tcell);
1064 bestce = (struct afsconf_entry *)0;
1067 UNLOCK_GLOBAL_MUTEX;
1071 /* Look through the list of aliases */
1072 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1073 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1074 tcell = tcae->aliasInfo.realName;
1079 for (tce = adir->entries; tce; tce = tce->next) {
1080 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1081 /* found our cell */
1086 if (strlen(tce->cellInfo.name) < cnLen)
1087 continue; /* clearly wrong */
1088 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1090 ambig = 1; /* ambiguous unless we get exact match */
1094 if (!ambig && bestce && bestce->cellInfo.numServers) {
1095 *acellInfo = bestce->cellInfo; /* structure assignment */
1097 tservice = afsconf_FindService(aservice);
1099 UNLOCK_GLOBAL_MUTEX;
1100 return AFSCONF_NOTFOUND; /* service not found */
1102 for (i = 0; i < acellInfo->numServers; i++) {
1103 acellInfo->hostAddr[i].sin_port = tservice;
1106 acellInfo->timeout = 0;
1107 UNLOCK_GLOBAL_MUTEX;
1110 UNLOCK_GLOBAL_MUTEX;
1111 #ifdef AFS_AFSDB_ENV
1112 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1114 return AFSCONF_NOTFOUND;
1115 #endif /* AFS_AFSDB_ENV */
1120 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1123 static int afsconf_showcell = 0;
1129 * If a cell switch was specified in a command, then it should override the
1130 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1131 * is set and the cell name in the adir structure is used.
1132 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1134 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1135 if (!afsconf_showcell) {
1136 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1138 afsconf_showcell = 1;
1140 strncpy(aname, afscell_path, alen);
1142 afsconf_Check(adir);
1143 if (adir->cellName) {
1144 strncpy(aname, adir->cellName, alen);
1146 code = AFSCONF_UNKNOWN;
1149 UNLOCK_GLOBAL_MUTEX;
1154 afsconf_Close(struct afsconf_dir *adir)
1157 afsconf_CloseInternal(adir);
1161 UNLOCK_GLOBAL_MUTEX;
1166 afsconf_CloseInternal(register struct afsconf_dir *adir)
1168 register struct afsconf_entry *td, *nd;
1169 struct afsconf_aliasentry *ta, *na;
1170 register char *tname;
1172 tname = adir->name; /* remember name, since that's all we preserve */
1174 /* free everything we can find */
1176 free(adir->cellName);
1177 for (td = adir->entries; td; td = nd) {
1179 if (td->cellInfo.linkedCell)
1180 free(td->cellInfo.linkedCell);
1183 for (ta = adir->alias_entries; ta; ta = na) {
1191 memset(adir, 0, sizeof(struct afsconf_dir));
1192 adir->name = tname; /* restore it */
1197 afsconf_Reopen(register struct afsconf_dir *adir)
1199 register afs_int32 code;
1200 code = afsconf_CloseInternal(adir);
1203 code = afsconf_OpenInternal(adir, 0, 0);
1207 /* called during opening of config file */
1209 afsconf_IntGetKeys(struct afsconf_dir *adir)
1213 struct afsconf_keys *tstr;
1214 register afs_int32 code;
1217 /* NT client config dir has no KeyFile; don't risk attempting open
1218 * because there might be a random file of this name if dir is shared.
1220 if (IsClientConfigDirectory(adir->name)) {
1221 adir->keystr = ((struct afsconf_keys *)
1222 malloc(sizeof(struct afsconf_keys)));
1223 adir->keystr->nkeys = 0;
1226 #endif /* AFS_NT40_ENV */
1229 /* compute the key name and other setup */
1230 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1231 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1232 adir->keystr = tstr;
1235 fd = open(tbuffer, O_RDONLY);
1238 UNLOCK_GLOBAL_MUTEX;
1241 code = read(fd, tstr, sizeof(struct afsconf_keys));
1243 if (code < sizeof(afs_int32)) {
1245 UNLOCK_GLOBAL_MUTEX;
1249 /* convert key structure to host order */
1250 tstr->nkeys = ntohl(tstr->nkeys);
1252 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1254 UNLOCK_GLOBAL_MUTEX;
1258 for (fd = 0; fd < tstr->nkeys; fd++)
1259 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1261 UNLOCK_GLOBAL_MUTEX;
1265 /* get keys structure */
1267 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1269 register afs_int32 code;
1272 code = afsconf_Check(adir);
1274 UNLOCK_GLOBAL_MUTEX;
1275 return AFSCONF_FAILURE;
1277 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1278 UNLOCK_GLOBAL_MUTEX;
1282 /* get latest key */
1284 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1285 struct ktc_encryptionKey *akey)
1289 register struct afsconf_key *tk;
1290 register afs_int32 best;
1291 struct afsconf_key *bestk;
1292 register afs_int32 code;
1295 code = afsconf_Check(adir);
1297 UNLOCK_GLOBAL_MUTEX;
1298 return AFSCONF_FAILURE;
1300 maxa = adir->keystr->nkeys;
1302 best = -1; /* highest kvno we've seen yet */
1303 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1304 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1305 if (tk->kvno == 999)
1306 continue; /* skip bcrypt keys */
1307 if (tk->kvno > best) {
1312 if (bestk) { /* found any */
1314 memcpy(akey, bestk->key, 8); /* copy out latest key */
1316 *avno = bestk->kvno; /* and kvno to caller */
1317 UNLOCK_GLOBAL_MUTEX;
1320 UNLOCK_GLOBAL_MUTEX;
1321 return AFSCONF_NOTFOUND; /* didn't find any keys */
1324 /* get a particular key */
1326 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1328 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1329 register int i, maxa;
1330 register struct afsconf_key *tk;
1331 register afs_int32 code;
1334 code = afsconf_Check(adir);
1336 UNLOCK_GLOBAL_MUTEX;
1337 return AFSCONF_FAILURE;
1339 maxa = adir->keystr->nkeys;
1341 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1342 if (tk->kvno == avno) {
1343 memcpy(akey, tk->key, 8);
1344 UNLOCK_GLOBAL_MUTEX;
1349 UNLOCK_GLOBAL_MUTEX;
1350 return AFSCONF_NOTFOUND;
1353 /* save the key structure in the appropriate file */
1355 SaveKeys(struct afsconf_dir *adir)
1357 struct afsconf_keys tkeys;
1359 register afs_int32 i;
1362 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1364 /* convert it to net byte order */
1365 for (i = 0; i < tkeys.nkeys; i++)
1366 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1367 tkeys.nkeys = htonl(tkeys.nkeys);
1369 /* rewrite keys file */
1370 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1371 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1373 return AFSCONF_FAILURE;
1374 i = write(fd, &tkeys, sizeof(tkeys));
1375 if (i != sizeof(tkeys)) {
1377 return AFSCONF_FAILURE;
1380 return AFSCONF_FAILURE;
1385 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1386 afs_int32 overwrite)
1388 register struct afsconf_keys *tk;
1389 register struct afsconf_key *tkey;
1390 register afs_int32 i;
1397 if (akvno < 0 || akvno > 255) {
1398 UNLOCK_GLOBAL_MUTEX;
1403 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1404 if (tkey->kvno == akvno) {
1406 UNLOCK_GLOBAL_MUTEX;
1407 return AFSCONF_KEYINUSE;
1414 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1415 UNLOCK_GLOBAL_MUTEX;
1416 return AFSCONF_FULL;
1418 tkey = &tk->key[tk->nkeys++];
1421 memcpy(tkey->key, akey, 8);
1423 afsconf_Touch(adir);
1424 UNLOCK_GLOBAL_MUTEX;
1428 /* this proc works by sliding the other guys down, rather than using a funny
1429 kvno value, so that callers can count on getting a good key in key[0].
1432 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1434 register struct afsconf_keys *tk;
1435 register struct afsconf_key *tkey;
1442 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1443 if (tkey->kvno == akvno) {
1449 UNLOCK_GLOBAL_MUTEX;
1450 return AFSCONF_NOTFOUND;
1453 /* otherwise slide the others down. i and tkey point at the guy to delete */
1454 for (; i < tk->nkeys - 1; i++, tkey++) {
1455 tkey->kvno = (tkey + 1)->kvno;
1456 memcpy(tkey->key, (tkey + 1)->key, 8);
1460 afsconf_Touch(adir);
1461 UNLOCK_GLOBAL_MUTEX;