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 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
118 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
119 get an fd <= 255. We allow the fileserver to claim more fds than that.
120 This has always been a problem since pr_Initialize would have the same
121 issue, but hpr_Initialize makes it more likely that we would see this.
122 Work around it. This is not generic. It's coded with the needs of
123 afsconf_* in mind only.
125 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
130 struct afsconf_iobuffer {
137 typedef struct afsconf_iobuffer afsconf_FILE;
139 static afsconf_FILE *
140 afsconf_fopen(const char *fname, const char *fmode)
145 if ((fd = open(fname, O_RDONLY)) == -1) {
149 iop = malloc(sizeof(struct afsconf_iobuffer));
156 iop->buffer = malloc(BUFFER);
157 if (iop->buffer == NULL) {
163 iop->ptr = iop->buffer;
164 iop->endptr = iop->buffer;
169 afsconf_fclose(afsconf_FILE *iop)
175 free((void *)iop->buffer);
181 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
189 if (iop->ptr == iop->endptr) {
192 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
202 iop->ptr = iop->buffer;
203 iop->endptr = iop->buffer + len;
207 if ((p - s) == (n - 1)) {
217 #define fopen afsconf_fopen
218 #define fclose afsconf_fclose
219 #define fgets afsconf_fgets
221 #define afsconf_FILE FILE
222 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
224 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
226 afsconf_FindService(register const char *aname)
228 /* lookup a service name */
230 register struct afsconf_servPair *tsp;
232 #if defined(AFS_OSF_ENV)
233 ts = getservbyname(aname, "");
235 ts = getservbyname(aname, NULL);
238 /* we found it in /etc/services, so we use this value */
239 return ts->s_port; /* already in network byte order */
242 /* not found in /etc/services, see if it is one of ours */
243 for (tsp = serviceTable;; tsp++) {
244 if (tsp->name == NULL)
246 if (!strcmp(tsp->name, aname))
247 return htons(tsp->port);
252 TrimLine(char *abuffer)
265 strcpy(abuffer, tbuffer);
271 * IsClientConfigDirectory() -- determine if path matches well-known
272 * client configuration directory.
275 IsClientConfigDirectory(const char *path)
277 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
280 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
281 int cc = tolower(cdir[i]);
282 int pc = tolower(path[i]);
295 /* hit end of one or both; allow mismatch in existence of trailing slash */
296 if (cdir[i] != '\0') {
297 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
301 if (path[i] != '\0') {
302 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
308 #endif /* AFS_NT40_ENV */
312 afsconf_Check(register struct afsconf_dir *adir)
314 char tbuffer[256], *p;
316 register afs_int32 code;
319 /* NT client CellServDB has different file name than NT server or Unix */
320 if (IsClientConfigDirectory(adir->name)) {
321 if (!afssw_GetClientCellServDBDir(&p)) {
322 strcompose(tbuffer, sizeof(tbuffer), p, "/",
323 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
327 strncpy(tbuffer, adir->name, sizeof(tbuffer));
328 len = (int)strlen(tbuffer);
329 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
330 strncat(tbuffer, "\\", sizeof(tbuffer));
332 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
334 tbuffer[sizeof(tbuffer) - 1] = '\0';
337 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
341 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
342 #endif /* AFS_NT40_ENV */
344 code = stat(tbuffer, &tstat);
348 /* did file change? */
349 if (tstat.st_mtime == adir->timeRead) {
352 /* otherwise file has changed, so reopen it */
353 return afsconf_Reopen(adir);
356 /* set modtime on file */
358 afsconf_Touch(register struct afsconf_dir *adir)
360 char tbuffer[256], *p;
362 struct timeval tvp[2];
365 adir->timeRead = 0; /* just in case */
368 /* NT client CellServDB has different file name than NT server or Unix */
370 if (IsClientConfigDirectory(adir->name)) {
371 if (!afssw_GetClientCellServDBDir(&p)) {
372 strcompose(tbuffer, sizeof(tbuffer), p, "/",
373 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
376 int len = (int)strlen(tbuffer);
377 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
378 strncat(tbuffer, "\\", sizeof(tbuffer));
380 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
382 tbuffer[sizeof(tbuffer) - 1] = '\0';
385 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
389 return _utime(tbuffer, NULL);
392 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
393 gettimeofday(&tvp[0], NULL);
395 return utimes(tbuffer, tvp);
396 #endif /* AFS_NT40_ENV */
400 afsconf_Open(register const char *adir)
402 register struct afsconf_dir *tdir;
403 register afs_int32 code;
406 /* zero structure and fill in name; rest is done by internal routine */
407 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
408 memset(tdir, 0, sizeof(struct afsconf_dir));
409 tdir->name = (char *)malloc(strlen(adir) + 1);
410 strcpy(tdir->name, adir);
412 code = afsconf_OpenInternal(tdir, 0, 0);
414 char *afsconf_path, afs_confdir[128];
417 /* Check global place only when local Open failed for whatever reason */
418 if (!(afsconf_path = getenv("AFSCONF"))) {
419 /* 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... */
424 if (!(home_dir = getenv("HOME"))) {
425 /* Our last chance is the "/.AFSCONF" file */
426 fp = fopen("/.AFSCONF", "r");
430 return (struct afsconf_dir *)0;
432 fgets(afs_confdir, 128, fp);
437 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
438 fp = fopen(pathname, "r");
440 /* Our last chance is the "/.AFSCONF" file */
441 fp = fopen("/.AFSCONF", "r");
445 return (struct afsconf_dir *)0;
448 fgets(afs_confdir, 128, fp);
451 len = strlen(afs_confdir);
455 return (struct afsconf_dir *)0;
457 if (afs_confdir[len - 1] == '\n') {
458 afs_confdir[len - 1] = 0;
460 afsconf_path = afs_confdir;
462 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
463 strcpy(tdir->name, afsconf_path);
464 code = afsconf_OpenInternal(tdir, 0, 0);
469 return (struct afsconf_dir *)0;
477 GetCellUnix(struct afsconf_dir *adir)
484 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
485 fp = fopen(tbuffer, "r");
489 rc = fgets(tbuffer, 256, fp);
495 while (*start != '\0' && isspace(*start))
498 while (*p != '\0' && !isspace(*p))
504 adir->cellName = strdup(start);
511 GetCellNT(struct afsconf_dir *adir)
513 if (IsClientConfigDirectory(adir->name)) {
514 /* NT client config dir; ThisCell is in registry (no file). */
515 return afssw_GetClientCellName(&adir->cellName);
517 /* NT server config dir; works just like Unix */
518 return GetCellUnix(adir);
521 #endif /* AFS_NT40_ENV */
525 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
529 register char *tp, *bp;
530 register struct afsconf_entry *curEntry;
531 struct afsconf_aliasentry *curAlias;
532 register afs_int32 code;
534 char tbuffer[256], tbuf1[256];
537 /* figure out the local cell name */
541 i = GetCellUnix(adir);
544 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
550 /* now parse the individual lines */
554 /* NT client/server have a CellServDB that is the same format as Unix.
555 * However, the NT client uses a different file name
557 if (IsClientConfigDirectory(adir->name)) {
558 /* NT client config dir */
560 if (!afssw_GetClientCellServDBDir(&p)) {
561 strcompose(tbuffer, sizeof(tbuffer), p, "/",
562 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
566 strncpy(tbuffer, adir->name, sizeof(tbuffer));
567 len = (int)strlen(tbuffer);
568 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
569 strncat(tbuffer, "\\", sizeof(tbuffer));
571 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
573 tbuffer[sizeof(tbuffer) - 1] = '\0';
576 /* NT server config dir */
577 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
581 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
582 #endif /* AFS_NT40_ENV */
584 if (!stat(tbuffer, &tstat)) {
585 adir->timeRead = tstat.st_mtime;
590 strcpy(tbuf1, tbuffer);
591 tf = fopen(tbuffer, "r");
596 tp = fgets(tbuffer, sizeof(tbuffer), tf);
599 TrimLine(tbuffer); /* remove white space */
600 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
601 continue; /* empty line */
602 if (tbuffer[0] == '>') {
603 char linkedcell[MAXCELLCHARS];
604 /* start new cell item */
606 /* thread this guy on the list */
607 curEntry->next = adir->entries;
608 adir->entries = curEntry;
612 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
613 memset(curEntry, 0, sizeof(struct afsconf_entry));
615 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
617 afsconf_CloseInternal(adir);
622 if (linkedcell[0] != '\0') {
623 curEntry->cellInfo.linkedCell =
624 (char *)malloc(strlen(linkedcell) + 1);
625 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
628 /* new host in the current cell */
630 afsconf_CloseInternal(adir);
634 i = curEntry->cellInfo.numServers;
635 if (cell && !strcmp(cell, curEntry->cellInfo.name))
637 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
638 curEntry->cellInfo.hostName[i], &clones[i]);
641 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
642 curEntry->cellInfo.hostName[i], 0);
644 if (code == AFSCONF_SYNTAX) {
645 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
651 "Can't properly parse host line \"%s\" in configuration file %s\n",
656 afsconf_CloseInternal(adir);
659 curEntry->cellInfo.numServers = ++i;
662 fclose(tf); /* close the file now */
664 /* end the last partially-completed cell */
666 curEntry->next = adir->entries;
667 adir->entries = curEntry;
670 /* Read in the alias list */
671 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
673 tf = fopen(tbuffer, "r");
677 tp = fgets(tbuffer, sizeof(tbuffer), tf);
680 TrimLine(tbuffer); /* remove white space */
682 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
683 continue; /* empty line */
686 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
689 continue; /* invalid line */
691 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
694 continue; /* invalid line */
697 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
702 curAlias = malloc(sizeof(*curAlias));
703 memset(curAlias, 0, sizeof(*curAlias));
705 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
706 strcpy(curAlias->aliasInfo.realName, tbuffer);
708 curAlias->next = adir->alias_entries;
709 adir->alias_entries = curAlias;
714 /* now read the fs keys, if possible */
715 adir->keystr = (struct afsconf_keys *)0;
716 afsconf_IntGetKeys(adir);
721 /* parse a line of the form
722 *"128.2.1.3 #hostname" or
723 *"[128.2.1.3] #hostname" for clones
724 * into the appropriate pieces.
727 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
731 register afs_int32 code;
737 /* FIXME: length of aname unknown here */
738 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
742 /* FIXME: length of aname unknown here */
743 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
746 return AFSCONF_SYNTAX;
747 addr->sin_family = AF_INET;
749 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
750 addr->sin_len = sizeof(struct sockaddr_in);
752 tp = (char *)&addr->sin_addr;
760 /* parse a line of the form
761 * ">cellname [linkedcellname] [#comments]"
762 * into the appropriate pieces.
765 ParseCellLine(register char *aline, register char *aname,
766 register char *alname)
769 /* FIXME: length of aname, alname unknown here */
770 code = sscanf(aline, ">%s %s", aname, alname);
774 if (*alname == '#') {
778 return (code > 0 ? 0 : AFSCONF_SYNTAX);
781 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
783 afsconf_CellApply(struct afsconf_dir *adir,
784 int (*aproc) (struct afsconf_cell * cell, char *arock,
785 struct afsconf_dir * dir), char *arock)
787 register struct afsconf_entry *tde;
788 register afs_int32 code;
790 for (tde = adir->entries; tde; tde = tde->next) {
791 code = (*aproc) (&tde->cellInfo, arock, adir);
801 /* call aproc(entry, arock, adir) for all cell aliases.
802 * Proc must return 0, or we'll stop early and return the code it returns
805 afsconf_CellAliasApply(struct afsconf_dir *adir,
806 int (*aproc) (struct afsconf_cellalias * alias,
807 char *arock, struct afsconf_dir * dir),
810 register struct afsconf_aliasentry *tde;
811 register afs_int32 code;
813 for (tde = adir->alias_entries; tde; tde = tde->next) {
814 code = (*aproc) (&tde->aliasInfo, arock, adir);
824 afs_int32 afsconf_SawCell = 0;
827 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
828 char *aservice, struct afsconf_cell *acellInfo,
834 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
841 cell = (char *)&acellInfo->name;
843 code = afsconf_OpenInternal(adir, cell, clones);
848 #if !defined(AFS_NT40_ENV)
850 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
851 struct afsconf_cell *acellInfo)
854 int tservice, i, len;
855 unsigned char answer[1024];
859 char realCellName[256];
865 /* The resolver isn't always MT-safe.. Perhaps this ought to be
866 * replaced with a more fine-grained lock just for the resolver
871 if ( ! strchr(acellName,'.') ) {
872 cellnamelength=strlen(acellName);
873 dotcellname=malloc(cellnamelength+2);
874 memcpy(dotcellname,acellName,cellnamelength);
875 dotcellname[cellnamelength]='.';
876 dotcellname[cellnamelength+1]=0;
878 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
880 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
886 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
895 return AFSCONF_NOTFOUND;
898 p = answer + sizeof(HEADER); /* Skip header */
899 code = dn_expand(answer, answer + len, p, host, sizeof(host));
901 return AFSCONF_NOTFOUND;
903 p += code + QFIXEDSZ; /* Skip name */
905 while (p < answer + len) {
908 code = dn_expand(answer, answer + len, p, host, sizeof(host));
910 return AFSCONF_NOTFOUND;
912 p += code; /* Skip the name */
913 type = (p[0] << 8) | p[1];
914 p += 4; /* Skip type and class */
915 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
916 p += 4; /* Skip the TTL */
917 size = (p[0] << 8) | p[1];
918 p += 2; /* Skip the size */
920 if (type == T_AFSDB) {
924 afsdb_type = (p[0] << 8) | p[1];
925 if (afsdb_type == 1) {
927 * We know this is an AFSDB record for our cell, of the
928 * right AFSDB type. Write down the true cell name that
929 * the resolver gave us above.
931 strcpy(realCellName, host);
934 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
936 return AFSCONF_NOTFOUND;
938 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
939 /* Do we want to get TTL data for the A record as well? */
940 (he = gethostbyname(host))) {
942 memcpy(&ipaddr, he->h_addr, he->h_length);
943 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
944 strncpy(acellInfo->hostName[server_num], host,
945 sizeof(acellInfo->hostName[server_num]));
948 if (!minttl || ttl < minttl)
956 if (server_num == 0) /* No AFSDB records */
957 return AFSCONF_NOTFOUND;
959 /* Convert the real cell name to lowercase */
960 for (p = (unsigned char *)realCellName; *p; p++)
963 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
964 acellInfo->numServers = server_num;
967 tservice = afsconf_FindService(aservice);
969 return AFSCONF_NOTFOUND; /* service not found */
970 for (i = 0; i < acellInfo->numServers; i++) {
971 acellInfo->hostAddr[i].sin_port = tservice;
975 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
981 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
982 struct afsconf_cell *acellInfo)
984 register afs_int32 i;
986 struct afsconf_entry DNSce;
987 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
988 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
993 DNSce.cellInfo.numServers = 0;
995 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, &numServers,
997 /* ignore the ttl here since this code is only called by transitory programs
1001 if (numServers == 0)
1004 for (i = 0; i < numServers; i++) {
1005 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1007 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1008 acellInfo->hostAddr[i].sin_family = AF_INET;
1010 /* sin_port supplied by connection code */
1013 acellInfo->numServers = numServers;
1014 strcpy(acellInfo->name, acellName);
1017 tservice = afsconf_FindService(aservice);
1018 UNLOCK_GLOBAL_MUTEX;
1020 return AFSCONF_NOTFOUND; /* service not found */
1022 for (i = 0; i < acellInfo->numServers; i++) {
1023 acellInfo->hostAddr[i].sin_port = tservice;
1026 acellInfo->linkedCell = NULL; /* no linked cell */
1027 acellInfo->flags = 0;
1030 #endif /* windows */
1031 #endif /* AFS_AFSDB_ENV */
1034 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1035 struct afsconf_cell *acellInfo)
1037 register struct afsconf_entry *tce;
1038 struct afsconf_aliasentry *tcae;
1039 struct afsconf_entry *bestce;
1040 register afs_int32 i;
1049 afsconf_Check(adir);
1052 cnLen = (int)(strlen(tcell) + 1);
1053 lcstring(tcell, tcell, cnLen);
1054 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1055 /* call to afsconf_GetLocalCell: like klog */
1057 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1059 UNLOCK_GLOBAL_MUTEX;
1064 cnLen = strlen(tcell);
1065 bestce = (struct afsconf_entry *)0;
1068 UNLOCK_GLOBAL_MUTEX;
1072 /* Look through the list of aliases */
1073 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1074 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1075 tcell = tcae->aliasInfo.realName;
1080 for (tce = adir->entries; tce; tce = tce->next) {
1081 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1082 /* found our cell */
1087 if (strlen(tce->cellInfo.name) < cnLen)
1088 continue; /* clearly wrong */
1089 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1091 ambig = 1; /* ambiguous unless we get exact match */
1095 if (!ambig && bestce && bestce->cellInfo.numServers) {
1096 *acellInfo = bestce->cellInfo; /* structure assignment */
1098 tservice = afsconf_FindService(aservice);
1100 UNLOCK_GLOBAL_MUTEX;
1101 return AFSCONF_NOTFOUND; /* service not found */
1103 for (i = 0; i < acellInfo->numServers; i++) {
1104 acellInfo->hostAddr[i].sin_port = tservice;
1107 acellInfo->timeout = 0;
1108 UNLOCK_GLOBAL_MUTEX;
1111 UNLOCK_GLOBAL_MUTEX;
1112 #ifdef AFS_AFSDB_ENV
1113 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1115 return AFSCONF_NOTFOUND;
1116 #endif /* AFS_AFSDB_ENV */
1121 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1124 static int afsconf_showcell = 0;
1130 * If a cell switch was specified in a command, then it should override the
1131 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1132 * is set and the cell name in the adir structure is used.
1133 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1135 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1136 if (!afsconf_showcell) {
1137 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1139 afsconf_showcell = 1;
1141 strncpy(aname, afscell_path, alen);
1143 afsconf_Check(adir);
1144 if (adir->cellName) {
1145 strncpy(aname, adir->cellName, alen);
1147 code = AFSCONF_UNKNOWN;
1150 UNLOCK_GLOBAL_MUTEX;
1155 afsconf_Close(struct afsconf_dir *adir)
1158 afsconf_CloseInternal(adir);
1162 UNLOCK_GLOBAL_MUTEX;
1167 afsconf_CloseInternal(register struct afsconf_dir *adir)
1169 register struct afsconf_entry *td, *nd;
1170 struct afsconf_aliasentry *ta, *na;
1171 register char *tname;
1173 tname = adir->name; /* remember name, since that's all we preserve */
1175 /* free everything we can find */
1177 free(adir->cellName);
1178 for (td = adir->entries; td; td = nd) {
1180 if (td->cellInfo.linkedCell)
1181 free(td->cellInfo.linkedCell);
1184 for (ta = adir->alias_entries; ta; ta = na) {
1192 memset(adir, 0, sizeof(struct afsconf_dir));
1193 adir->name = tname; /* restore it */
1198 afsconf_Reopen(register struct afsconf_dir *adir)
1200 register afs_int32 code;
1201 code = afsconf_CloseInternal(adir);
1204 code = afsconf_OpenInternal(adir, 0, 0);
1208 /* called during opening of config file */
1210 afsconf_IntGetKeys(struct afsconf_dir *adir)
1214 struct afsconf_keys *tstr;
1215 register afs_int32 code;
1218 /* NT client config dir has no KeyFile; don't risk attempting open
1219 * because there might be a random file of this name if dir is shared.
1221 if (IsClientConfigDirectory(adir->name)) {
1222 adir->keystr = ((struct afsconf_keys *)
1223 malloc(sizeof(struct afsconf_keys)));
1224 adir->keystr->nkeys = 0;
1227 #endif /* AFS_NT40_ENV */
1230 /* compute the key name and other setup */
1231 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1232 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1233 adir->keystr = tstr;
1236 fd = open(tbuffer, O_RDONLY);
1239 UNLOCK_GLOBAL_MUTEX;
1242 code = read(fd, tstr, sizeof(struct afsconf_keys));
1244 if (code < sizeof(afs_int32)) {
1246 UNLOCK_GLOBAL_MUTEX;
1250 /* convert key structure to host order */
1251 tstr->nkeys = ntohl(tstr->nkeys);
1253 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1255 UNLOCK_GLOBAL_MUTEX;
1259 for (fd = 0; fd < tstr->nkeys; fd++)
1260 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1262 UNLOCK_GLOBAL_MUTEX;
1266 /* get keys structure */
1268 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1270 register afs_int32 code;
1273 code = afsconf_Check(adir);
1275 UNLOCK_GLOBAL_MUTEX;
1276 return AFSCONF_FAILURE;
1278 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1279 UNLOCK_GLOBAL_MUTEX;
1283 /* get latest key */
1285 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno, char *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(struct afsconf_dir *adir, afs_int32 avno, char *akey)
1328 register int i, maxa;
1329 register struct afsconf_key *tk;
1330 register afs_int32 code;
1333 code = afsconf_Check(adir);
1335 UNLOCK_GLOBAL_MUTEX;
1336 return AFSCONF_FAILURE;
1338 maxa = adir->keystr->nkeys;
1340 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1341 if (tk->kvno == avno) {
1342 memcpy(akey, tk->key, 8);
1343 UNLOCK_GLOBAL_MUTEX;
1348 UNLOCK_GLOBAL_MUTEX;
1349 return AFSCONF_NOTFOUND;
1352 /* save the key structure in the appropriate file */
1354 SaveKeys(struct afsconf_dir *adir)
1356 struct afsconf_keys tkeys;
1358 register afs_int32 i;
1361 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1363 /* convert it to net byte order */
1364 for (i = 0; i < tkeys.nkeys; i++)
1365 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1366 tkeys.nkeys = htonl(tkeys.nkeys);
1368 /* rewrite keys file */
1369 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1370 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1372 return AFSCONF_FAILURE;
1373 i = write(fd, &tkeys, sizeof(tkeys));
1374 if (i != sizeof(tkeys)) {
1376 return AFSCONF_FAILURE;
1379 return AFSCONF_FAILURE;
1384 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1385 afs_int32 overwrite)
1387 register struct afsconf_keys *tk;
1388 register struct afsconf_key *tkey;
1389 register afs_int32 i;
1396 if (akvno < 0 || akvno > 255) {
1397 UNLOCK_GLOBAL_MUTEX;
1402 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1403 if (tkey->kvno == akvno) {
1405 UNLOCK_GLOBAL_MUTEX;
1406 return AFSCONF_KEYINUSE;
1413 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1414 UNLOCK_GLOBAL_MUTEX;
1415 return AFSCONF_FULL;
1417 tkey = &tk->key[tk->nkeys++];
1420 memcpy(tkey->key, akey, 8);
1422 afsconf_Touch(adir);
1423 UNLOCK_GLOBAL_MUTEX;
1427 /* this proc works by sliding the other guys down, rather than using a funny
1428 kvno value, so that callers can count on getting a good key in key[0].
1431 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1433 register struct afsconf_keys *tk;
1434 register struct afsconf_key *tkey;
1441 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1442 if (tkey->kvno == akvno) {
1448 UNLOCK_GLOBAL_MUTEX;
1449 return AFSCONF_NOTFOUND;
1452 /* otherwise slide the others down. i and tkey point at the guy to delete */
1453 for (; i < tk->nkeys - 1; i++, tkey++) {
1454 tkey->kvno = (tkey + 1)->kvno;
1455 memcpy(tkey->key, (tkey + 1)->key, 8);
1459 afsconf_Touch(adir);
1460 UNLOCK_GLOBAL_MUTEX;