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"
25 #include <sys/types.h>
28 #include <sys/utime.h>
30 #include <WINNT/afssw.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
38 #include <arpa/nameser.h>
39 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
40 #include <arpa/nameser_compat.h>
43 #endif /* AFS_AFSDB_ENV */
44 #endif /* AFS_NT40_ENV */
45 #include <afs/afsint.h>
60 #include <afs/afsutil.h>
61 #include "cellconfig.h"
66 #include <cm_config.h>
67 /* cm_dns.h depends on cellconfig.h */
70 #endif /* AFS_AFSDB_ENV */
75 static struct afsconf_servPair serviceTable[] = {
86 {"afsres", 7010,}, /* residency database for MR-AFS */
87 {"afsremio", 7011,}, /* remote I/O interface for MR-AFS */
88 {0, 0} /* insert new services before this spot */
92 static afs_int32 afsconf_FindService(register const char *aname);
93 static int TrimLine(char *abuffer);
95 static int IsClientConfigDirectory(const char *path);
96 static int GetCellNT(struct afsconf_dir *adir);
98 static int afsconf_Check(register struct afsconf_dir *adir);
99 static int afsconf_Touch(register struct afsconf_dir *adir);
100 static int GetCellUnix(struct afsconf_dir *adir);
101 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
103 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
104 char *aname, char *aclone);
105 static int ParseCellLine(register char *aline, register char *aname,
106 register char *alname);
107 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
108 static int afsconf_Reopen(register struct afsconf_dir *adir);
109 static int SaveKeys(struct afsconf_dir *adir);
112 #define T_AFSDB 18 /* per RFC1183 section 1 */
116 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
117 * our code can tell if there is new info in the key files, the cell server db
118 * files or any of the other files (and reopen the thing) if the date on
119 * CellServDB changes.
122 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
123 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
124 get an fd <= 255. We allow the fileserver to claim more fds than that.
125 This has always been a problem since pr_Initialize would have the same
126 issue, but hpr_Initialize makes it more likely that we would see this.
127 Work around it. This is not generic. It's coded with the needs of
128 afsconf_* in mind only.
130 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
135 struct afsconf_iobuffer {
142 typedef struct afsconf_iobuffer afsconf_FILE;
144 static afsconf_FILE *
145 afsconf_fopen(const char *fname, const char *fmode)
150 if ((fd = open(fname, O_RDONLY)) == -1) {
154 iop = malloc(sizeof(struct afsconf_iobuffer));
161 iop->buffer = malloc(BUFFER);
162 if (iop->buffer == NULL) {
168 iop->ptr = iop->buffer;
169 iop->endptr = iop->buffer;
174 afsconf_fclose(afsconf_FILE *iop)
180 free((void *)iop->buffer);
186 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
194 if (iop->ptr == iop->endptr) {
197 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
207 iop->ptr = iop->buffer;
208 iop->endptr = iop->buffer + len;
212 if ((p - s) == (n - 1)) {
222 #define fopen afsconf_fopen
223 #define fclose afsconf_fclose
224 #define fgets afsconf_fgets
226 #define afsconf_FILE FILE
227 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
229 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
231 afsconf_FindService(register const char *aname)
233 /* lookup a service name */
235 register struct afsconf_servPair *tsp;
237 #if defined(AFS_OSF_ENV)
238 ts = getservbyname(aname, "");
240 ts = (struct servent *) getservbyname(aname, NULL);
243 /* we found it in /etc/services, so we use this value */
244 return ts->s_port; /* already in network byte order */
247 /* not found in /etc/services, see if it is one of ours */
248 for (tsp = serviceTable;; tsp++) {
249 if (tsp->name == NULL)
251 if (!strcmp(tsp->name, aname))
252 return htons(tsp->port);
257 TrimLine(char *abuffer)
270 strcpy(abuffer, tbuffer);
276 * IsClientConfigDirectory() -- determine if path matches well-known
277 * client configuration directory.
280 IsClientConfigDirectory(const char *path)
282 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
285 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
286 int cc = tolower(cdir[i]);
287 int pc = tolower(path[i]);
300 /* hit end of one or both; allow mismatch in existence of trailing slash */
301 if (cdir[i] != '\0') {
302 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
306 if (path[i] != '\0') {
307 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
313 #endif /* AFS_NT40_ENV */
317 afsconf_Check(register struct afsconf_dir *adir)
324 register afs_int32 code;
327 /* NT client CellServDB has different file name than NT server or Unix */
328 if (IsClientConfigDirectory(adir->name)) {
329 if (!afssw_GetClientCellServDBDir(&p)) {
330 strcompose(tbuffer, sizeof(tbuffer), p, "/",
331 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
335 strncpy(tbuffer, adir->name, sizeof(tbuffer));
336 len = (int)strlen(tbuffer);
337 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
338 strncat(tbuffer, "\\", sizeof(tbuffer));
340 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
342 tbuffer[sizeof(tbuffer) - 1] = '\0';
345 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
349 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
350 #endif /* AFS_NT40_ENV */
352 code = stat(tbuffer, &tstat);
356 /* did file change? */
357 if (tstat.st_mtime == adir->timeRead) {
360 /* otherwise file has changed, so reopen it */
361 return afsconf_Reopen(adir);
364 /* set modtime on file */
366 afsconf_Touch(register struct afsconf_dir *adir)
370 struct timeval tvp[2];
375 adir->timeRead = 0; /* just in case */
378 /* NT client CellServDB has different file name than NT server or Unix */
380 if (IsClientConfigDirectory(adir->name)) {
381 if (!afssw_GetClientCellServDBDir(&p)) {
382 strcompose(tbuffer, sizeof(tbuffer), p, "/",
383 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
386 int len = (int)strlen(tbuffer);
387 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
388 strncat(tbuffer, "\\", sizeof(tbuffer));
390 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
392 tbuffer[sizeof(tbuffer) - 1] = '\0';
395 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
399 return _utime(tbuffer, NULL);
402 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
403 gettimeofday(&tvp[0], NULL);
405 return utimes(tbuffer, tvp);
406 #endif /* AFS_NT40_ENV */
410 afsconf_Open(register const char *adir)
412 register struct afsconf_dir *tdir;
413 register afs_int32 code;
416 /* zero structure and fill in name; rest is done by internal routine */
417 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
418 memset(tdir, 0, sizeof(struct afsconf_dir));
419 tdir->name = (char *)malloc(strlen(adir) + 1);
420 strcpy(tdir->name, adir);
422 code = afsconf_OpenInternal(tdir, 0, 0);
424 char *afsconf_path, afs_confdir[128];
427 /* Check global place only when local Open failed for whatever reason */
428 if (!(afsconf_path = getenv("AFSCONF"))) {
429 /* 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... */
434 if (!(home_dir = getenv("HOME"))) {
435 /* Our last chance is the "/.AFSCONF" file */
436 fp = fopen("/.AFSCONF", "r");
440 return (struct afsconf_dir *)0;
442 fgets(afs_confdir, 128, fp);
447 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
448 fp = fopen(pathname, "r");
450 /* Our last chance is the "/.AFSCONF" file */
451 fp = fopen("/.AFSCONF", "r");
455 return (struct afsconf_dir *)0;
458 fgets(afs_confdir, 128, fp);
461 len = strlen(afs_confdir);
465 return (struct afsconf_dir *)0;
467 if (afs_confdir[len - 1] == '\n') {
468 afs_confdir[len - 1] = 0;
470 afsconf_path = afs_confdir;
472 tdir->name = (char *)malloc(strlen(afsconf_path) + 1);
473 strcpy(tdir->name, afsconf_path);
474 code = afsconf_OpenInternal(tdir, 0, 0);
479 return (struct afsconf_dir *)0;
487 GetCellUnix(struct afsconf_dir *adir)
494 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
495 fp = fopen(tbuffer, "r");
499 rc = fgets(tbuffer, 256, fp);
505 while (*start != '\0' && isspace(*start))
508 while (*p != '\0' && !isspace(*p))
514 adir->cellName = strdup(start);
521 GetCellNT(struct afsconf_dir *adir)
523 if (IsClientConfigDirectory(adir->name)) {
524 /* NT client config dir; ThisCell is in registry (no file). */
525 return afssw_GetClientCellName(&adir->cellName);
527 /* NT server config dir; works just like Unix */
528 return GetCellUnix(adir);
532 /* The following procedures and structs are used on Windows only
533 * to enumerate the Cell information distributed within the
534 * Windows registry. (See src/WINNT/afsd/cm_config.c)
536 typedef struct _cm_enumCellRegistry {
537 afs_uint32 client; /* non-zero if client query */
538 struct afsconf_dir *adir;
539 } cm_enumCellRegistry_t;
542 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
543 char *hostNamep, unsigned short rank)
545 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
547 if (cellInfop->numServers == MAXHOSTSPERCELL)
550 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
551 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
552 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
553 cellInfop->numServers++;
559 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
562 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
563 char linkedName[256] = "";
565 struct afsconf_entry *newEntry;
568 newEntry = malloc(sizeof(struct afsconf_entry));
569 if (newEntry == NULL)
571 newEntry->cellInfo.numServers = 0;
573 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
574 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
575 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
578 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
579 newEntry->cellInfo.name[MAXCELLCHARS-1];
581 newEntry->cellInfo.linkedCell = strdup(linkedName);
583 newEntry->cellInfo.linkedCell = NULL;
584 newEntry->cellInfo.timeout = timeout;
585 newEntry->cellInfo.flags = 0;
587 newEntry->next = enump->adir->entries;
588 enump->adir->entries = newEntry;
594 #endif /* AFS_NT40_ENV */
598 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
602 register char *tp, *bp;
603 register struct afsconf_entry *curEntry;
604 struct afsconf_aliasentry *curAlias;
605 register afs_int32 code;
607 char tbuffer[256], tbuf1[256];
610 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
611 #endif /* AFS_NT40_ENV */
613 /* figure out the local cell name */
616 enumCellRegistry.adir = adir;
618 i = GetCellUnix(adir);
621 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
627 /* now parse the individual lines */
631 /* NT client/server have a CellServDB that is the same format as Unix.
632 * However, the NT client uses a different file name
634 if (IsClientConfigDirectory(adir->name)) {
635 /* NT client config dir */
638 enumCellRegistry.client = 1;
640 if (!afssw_GetClientCellServDBDir(&p)) {
641 strcompose(tbuffer, sizeof(tbuffer), p, "/",
642 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
646 strncpy(tbuffer, adir->name, sizeof(tbuffer));
647 len = (int)strlen(tbuffer);
648 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
649 strncat(tbuffer, "\\", sizeof(tbuffer));
651 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
653 tbuffer[sizeof(tbuffer) - 1] = '\0';
656 /* NT server config dir */
657 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
661 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
662 #endif /* AFS_NT40_ENV */
664 if (!stat(tbuffer, &tstat)) {
665 adir->timeRead = tstat.st_mtime;
670 strcpy(tbuf1, tbuffer);
671 tf = fopen(tbuffer, "r");
676 /* The CellServDB file is now open.
677 * The following code parses the contents of the
678 * file and creates a list with the first cell entry
679 * in the CellServDB file at the end of the list.
681 * No checking is performed for duplicates.
682 * The side effects of this process are that duplicate
683 * entries appended to the end of the CellServDB file
684 * take precedence and are found in a shorter period
689 tp = fgets(tbuffer, sizeof(tbuffer), tf);
692 TrimLine(tbuffer); /* remove white space */
693 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
694 continue; /* empty line */
695 if (tbuffer[0] == '>') {
696 char linkedcell[MAXCELLCHARS];
697 /* start new cell item */
699 /* thread this guy on the list */
700 curEntry->next = adir->entries;
701 adir->entries = curEntry;
705 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
706 memset(curEntry, 0, sizeof(struct afsconf_entry));
708 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
710 afsconf_CloseInternal(adir);
715 if (linkedcell[0] != '\0') {
716 curEntry->cellInfo.linkedCell =
717 (char *)malloc(strlen(linkedcell) + 1);
718 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
721 /* new host in the current cell */
723 afsconf_CloseInternal(adir);
727 i = curEntry->cellInfo.numServers;
728 if (cell && !strcmp(cell, curEntry->cellInfo.name))
730 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
731 curEntry->cellInfo.hostName[i], &clones[i]);
734 ParseHostLine(tbuffer, &curEntry->cellInfo.hostAddr[i],
735 curEntry->cellInfo.hostName[i], 0);
737 if (code == AFSCONF_SYNTAX) {
738 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
744 "Can't properly parse host line \"%s\" in configuration file %s\n",
749 afsconf_CloseInternal(adir);
752 curEntry->cellInfo.numServers = ++i;
755 fclose(tf); /* close the file now */
757 /* end the last partially-completed cell */
759 curEntry->next = adir->entries;
760 adir->entries = curEntry;
765 * Windows maintains a CellServDB list in the Registry
766 * that supercedes the contents of the CellServDB file.
767 * Prepending these entries to the head of the list
768 * is sufficient to enforce the precedence.
770 cm_EnumerateCellRegistry( enumCellRegistry.client,
771 cm_enumCellRegistryProc,
773 #endif /* AFS_NT40_ENV */
775 /* Read in the alias list */
776 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
778 tf = fopen(tbuffer, "r");
782 tp = fgets(tbuffer, sizeof(tbuffer), tf);
785 TrimLine(tbuffer); /* remove white space */
787 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
788 continue; /* empty line */
791 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
794 continue; /* invalid line */
796 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
799 continue; /* invalid line */
802 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
807 curAlias = malloc(sizeof(*curAlias));
808 memset(curAlias, 0, sizeof(*curAlias));
810 strcpy(curAlias->aliasInfo.aliasName, aliasPtr);
811 strcpy(curAlias->aliasInfo.realName, tbuffer);
813 curAlias->next = adir->alias_entries;
814 adir->alias_entries = curAlias;
819 /* now read the fs keys, if possible */
820 adir->keystr = (struct afsconf_keys *)0;
821 afsconf_IntGetKeys(adir);
826 /* parse a line of the form
827 *"128.2.1.3 #hostname" or
828 *"[128.2.1.3] #hostname" for clones
829 * into the appropriate pieces.
832 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
836 register afs_int32 code;
842 /* FIXME: length of aname unknown here */
843 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
847 /* FIXME: length of aname unknown here */
848 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
851 return AFSCONF_SYNTAX;
852 addr->sin_family = AF_INET;
854 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
855 addr->sin_len = sizeof(struct sockaddr_in);
857 tp = (char *)&addr->sin_addr;
865 /* parse a line of the form
866 * ">cellname [linkedcellname] [#comments]"
867 * into the appropriate pieces.
870 ParseCellLine(register char *aline, register char *aname,
871 register char *alname)
874 /* FIXME: length of aname, alname unknown here */
875 code = sscanf(aline, ">%s %s", aname, alname);
879 if (*alname == '#') {
883 return (code > 0 ? 0 : AFSCONF_SYNTAX);
886 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
888 afsconf_CellApply(struct afsconf_dir *adir,
889 int (*aproc) (struct afsconf_cell * cell, void *arock,
890 struct afsconf_dir * dir), void *arock)
892 register struct afsconf_entry *tde;
893 register afs_int32 code;
895 for (tde = adir->entries; tde; tde = tde->next) {
896 code = (*aproc) (&tde->cellInfo, arock, adir);
906 /* call aproc(entry, arock, adir) for all cell aliases.
907 * Proc must return 0, or we'll stop early and return the code it returns
910 afsconf_CellAliasApply(struct afsconf_dir *adir,
911 int (*aproc) (struct afsconf_cellalias * alias,
912 void *arock, struct afsconf_dir * dir),
915 register struct afsconf_aliasentry *tde;
916 register afs_int32 code;
918 for (tde = adir->alias_entries; tde; tde = tde->next) {
919 code = (*aproc) (&tde->aliasInfo, arock, adir);
929 afs_int32 afsconf_SawCell = 0;
932 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
933 char *aservice, struct afsconf_cell *acellInfo,
939 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
946 cell = (char *)&acellInfo->name;
948 code = afsconf_OpenInternal(adir, cell, clones);
953 #if !defined(AFS_NT40_ENV)
955 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
956 struct afsconf_cell *acellInfo)
959 int tservice, i, len;
960 unsigned char answer[1024];
964 char realCellName[256];
970 /* The resolver isn't always MT-safe.. Perhaps this ought to be
971 * replaced with a more fine-grained lock just for the resolver
976 if ( ! strchr(acellName,'.') ) {
977 cellnamelength=strlen(acellName);
978 dotcellname=malloc(cellnamelength+2);
979 memcpy(dotcellname,acellName,cellnamelength);
980 dotcellname[cellnamelength]='.';
981 dotcellname[cellnamelength+1]=0;
983 len = res_search(dotcellname, C_IN, T_AFSDB, answer, sizeof(answer));
985 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
991 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
1000 return AFSCONF_NOTFOUND;
1003 p = answer + sizeof(HEADER); /* Skip header */
1004 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1006 return AFSCONF_NOTFOUND;
1008 p += code + QFIXEDSZ; /* Skip name */
1010 while (p < answer + len) {
1011 int type, ttl, size;
1013 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1015 return AFSCONF_NOTFOUND;
1017 p += code; /* Skip the name */
1018 type = (p[0] << 8) | p[1];
1019 p += 4; /* Skip type and class */
1020 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1021 p += 4; /* Skip the TTL */
1022 size = (p[0] << 8) | p[1];
1023 p += 2; /* Skip the size */
1025 if (type == T_AFSDB) {
1029 afsdb_type = (p[0] << 8) | p[1];
1030 if (afsdb_type == 1) {
1032 * We know this is an AFSDB record for our cell, of the
1033 * right AFSDB type. Write down the true cell name that
1034 * the resolver gave us above.
1036 strcpy(realCellName, host);
1039 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1041 return AFSCONF_NOTFOUND;
1043 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1044 /* Do we want to get TTL data for the A record as well? */
1045 (he = gethostbyname(host))) {
1047 memcpy(&ipaddr, he->h_addr, he->h_length);
1048 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
1049 strncpy(acellInfo->hostName[server_num], host,
1050 sizeof(acellInfo->hostName[server_num]));
1053 if (!minttl || ttl < minttl)
1061 if (server_num == 0) /* No AFSDB records */
1062 return AFSCONF_NOTFOUND;
1064 /* Convert the real cell name to lowercase */
1065 for (p = (unsigned char *)realCellName; *p; p++)
1068 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
1069 acellInfo->numServers = server_num;
1072 tservice = afsconf_FindService(aservice);
1074 return AFSCONF_NOTFOUND; /* service not found */
1075 for (i = 0; i < acellInfo->numServers; i++) {
1076 acellInfo->hostAddr[i].sin_port = tservice;
1080 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
1086 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1087 struct afsconf_cell *acellInfo)
1089 register afs_int32 i;
1091 struct afsconf_entry DNSce;
1092 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1093 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1094 unsigned short ipRanks[AFSMAXCELLHOSTS];
1099 DNSce.cellInfo.numServers = 0;
1101 rc = getAFSServer(acellName, cellHostAddrs, cellHostNames, ipRanks, &numServers,
1103 /* ignore the ttl here since this code is only called by transitory programs
1104 * like klog, etc. */
1107 if (numServers == 0)
1110 for (i = 0; i < numServers; i++) {
1111 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1113 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1114 acellInfo->hostAddr[i].sin_family = AF_INET;
1116 /* sin_port supplied by connection code */
1119 acellInfo->numServers = numServers;
1120 strcpy(acellInfo->name, acellName);
1123 tservice = afsconf_FindService(aservice);
1124 UNLOCK_GLOBAL_MUTEX;
1126 return AFSCONF_NOTFOUND; /* service not found */
1128 for (i = 0; i < acellInfo->numServers; i++) {
1129 acellInfo->hostAddr[i].sin_port = tservice;
1132 acellInfo->linkedCell = NULL; /* no linked cell */
1133 acellInfo->flags = 0;
1136 #endif /* windows */
1137 #endif /* AFS_AFSDB_ENV */
1140 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1141 struct afsconf_cell *acellInfo)
1143 register struct afsconf_entry *tce;
1144 struct afsconf_aliasentry *tcae;
1145 struct afsconf_entry *bestce;
1146 register afs_int32 i;
1155 afsconf_Check(adir);
1158 cnLen = (int)(strlen(tcell) + 1);
1159 lcstring(tcell, tcell, cnLen);
1160 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1161 /* call to afsconf_GetLocalCell: like klog */
1163 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1165 UNLOCK_GLOBAL_MUTEX;
1170 cnLen = strlen(tcell);
1171 bestce = (struct afsconf_entry *)0;
1174 UNLOCK_GLOBAL_MUTEX;
1178 /* Look through the list of aliases */
1179 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1180 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1181 tcell = tcae->aliasInfo.realName;
1186 for (tce = adir->entries; tce; tce = tce->next) {
1187 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1188 /* found our cell */
1193 if (strlen(tce->cellInfo.name) < cnLen)
1194 continue; /* clearly wrong */
1195 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1197 ambig = 1; /* ambiguous unless we get exact match */
1201 if (!ambig && bestce && bestce->cellInfo.numServers) {
1202 *acellInfo = bestce->cellInfo; /* structure assignment */
1204 tservice = afsconf_FindService(aservice);
1206 UNLOCK_GLOBAL_MUTEX;
1207 return AFSCONF_NOTFOUND; /* service not found */
1209 for (i = 0; i < acellInfo->numServers; i++) {
1210 acellInfo->hostAddr[i].sin_port = tservice;
1213 acellInfo->timeout = 0;
1214 UNLOCK_GLOBAL_MUTEX;
1217 UNLOCK_GLOBAL_MUTEX;
1218 #ifdef AFS_AFSDB_ENV
1219 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1221 return AFSCONF_NOTFOUND;
1222 #endif /* AFS_AFSDB_ENV */
1227 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1230 static int afsconf_showcell = 0;
1236 * If a cell switch was specified in a command, then it should override the
1237 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1238 * is set and the cell name in the adir structure is used.
1239 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1241 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1242 if (!afsconf_showcell) {
1243 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1245 afsconf_showcell = 1;
1247 strncpy(aname, afscell_path, alen);
1249 afsconf_Check(adir);
1250 if (adir->cellName) {
1251 strncpy(aname, adir->cellName, alen);
1253 code = AFSCONF_UNKNOWN;
1256 UNLOCK_GLOBAL_MUTEX;
1261 afsconf_Close(struct afsconf_dir *adir)
1264 afsconf_CloseInternal(adir);
1268 UNLOCK_GLOBAL_MUTEX;
1273 afsconf_CloseInternal(register struct afsconf_dir *adir)
1275 register struct afsconf_entry *td, *nd;
1276 struct afsconf_aliasentry *ta, *na;
1277 register char *tname;
1279 tname = adir->name; /* remember name, since that's all we preserve */
1281 /* free everything we can find */
1283 free(adir->cellName);
1284 for (td = adir->entries; td; td = nd) {
1286 if (td->cellInfo.linkedCell)
1287 free(td->cellInfo.linkedCell);
1290 for (ta = adir->alias_entries; ta; ta = na) {
1298 memset(adir, 0, sizeof(struct afsconf_dir));
1299 adir->name = tname; /* restore it */
1304 afsconf_Reopen(register struct afsconf_dir *adir)
1306 register afs_int32 code;
1307 code = afsconf_CloseInternal(adir);
1310 code = afsconf_OpenInternal(adir, 0, 0);
1314 /* called during opening of config file */
1316 afsconf_IntGetKeys(struct afsconf_dir *adir)
1320 struct afsconf_keys *tstr;
1321 register afs_int32 code;
1324 /* NT client config dir has no KeyFile; don't risk attempting open
1325 * because there might be a random file of this name if dir is shared.
1327 if (IsClientConfigDirectory(adir->name)) {
1328 adir->keystr = ((struct afsconf_keys *)
1329 malloc(sizeof(struct afsconf_keys)));
1330 adir->keystr->nkeys = 0;
1333 #endif /* AFS_NT40_ENV */
1336 /* compute the key name and other setup */
1337 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1338 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1339 adir->keystr = tstr;
1342 fd = open(tbuffer, O_RDONLY);
1345 UNLOCK_GLOBAL_MUTEX;
1348 code = read(fd, tstr, sizeof(struct afsconf_keys));
1350 if (code < sizeof(afs_int32)) {
1352 UNLOCK_GLOBAL_MUTEX;
1356 /* convert key structure to host order */
1357 tstr->nkeys = ntohl(tstr->nkeys);
1359 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1361 UNLOCK_GLOBAL_MUTEX;
1365 for (fd = 0; fd < tstr->nkeys; fd++)
1366 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1368 UNLOCK_GLOBAL_MUTEX;
1372 /* get keys structure */
1374 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1376 register afs_int32 code;
1379 code = afsconf_Check(adir);
1381 UNLOCK_GLOBAL_MUTEX;
1382 return AFSCONF_FAILURE;
1384 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1385 UNLOCK_GLOBAL_MUTEX;
1389 /* get latest key */
1391 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1392 struct ktc_encryptionKey *akey)
1396 register struct afsconf_key *tk;
1397 register afs_int32 best;
1398 struct afsconf_key *bestk;
1399 register afs_int32 code;
1402 code = afsconf_Check(adir);
1404 UNLOCK_GLOBAL_MUTEX;
1405 return AFSCONF_FAILURE;
1407 maxa = adir->keystr->nkeys;
1409 best = -1; /* highest kvno we've seen yet */
1410 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1411 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1412 if (tk->kvno == 999)
1413 continue; /* skip bcrypt keys */
1414 if (tk->kvno > best) {
1419 if (bestk) { /* found any */
1421 memcpy(akey, bestk->key, 8); /* copy out latest key */
1423 *avno = bestk->kvno; /* and kvno to caller */
1424 UNLOCK_GLOBAL_MUTEX;
1427 UNLOCK_GLOBAL_MUTEX;
1428 return AFSCONF_NOTFOUND; /* didn't find any keys */
1431 /* get a particular key */
1433 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1435 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1436 register int i, maxa;
1437 register struct afsconf_key *tk;
1438 register afs_int32 code;
1441 code = afsconf_Check(adir);
1443 UNLOCK_GLOBAL_MUTEX;
1444 return AFSCONF_FAILURE;
1446 maxa = adir->keystr->nkeys;
1448 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1449 if (tk->kvno == avno) {
1450 memcpy(akey, tk->key, 8);
1451 UNLOCK_GLOBAL_MUTEX;
1456 UNLOCK_GLOBAL_MUTEX;
1457 return AFSCONF_NOTFOUND;
1460 /* save the key structure in the appropriate file */
1462 SaveKeys(struct afsconf_dir *adir)
1464 struct afsconf_keys tkeys;
1466 register afs_int32 i;
1469 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1471 /* convert it to net byte order */
1472 for (i = 0; i < tkeys.nkeys; i++)
1473 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1474 tkeys.nkeys = htonl(tkeys.nkeys);
1476 /* rewrite keys file */
1477 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1478 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1480 return AFSCONF_FAILURE;
1481 i = write(fd, &tkeys, sizeof(tkeys));
1482 if (i != sizeof(tkeys)) {
1484 return AFSCONF_FAILURE;
1487 return AFSCONF_FAILURE;
1492 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1493 afs_int32 overwrite)
1495 register struct afsconf_keys *tk;
1496 register struct afsconf_key *tkey;
1497 register afs_int32 i;
1504 if (akvno < 0 || akvno > 255) {
1505 UNLOCK_GLOBAL_MUTEX;
1510 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1511 if (tkey->kvno == akvno) {
1513 UNLOCK_GLOBAL_MUTEX;
1514 return AFSCONF_KEYINUSE;
1521 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1522 UNLOCK_GLOBAL_MUTEX;
1523 return AFSCONF_FULL;
1525 tkey = &tk->key[tk->nkeys++];
1528 memcpy(tkey->key, akey, 8);
1530 afsconf_Touch(adir);
1531 UNLOCK_GLOBAL_MUTEX;
1535 /* this proc works by sliding the other guys down, rather than using a funny
1536 kvno value, so that callers can count on getting a good key in key[0].
1539 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1541 register struct afsconf_keys *tk;
1542 register struct afsconf_key *tkey;
1549 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1550 if (tkey->kvno == akvno) {
1556 UNLOCK_GLOBAL_MUTEX;
1557 return AFSCONF_NOTFOUND;
1560 /* otherwise slide the others down. i and tkey point at the guy to delete */
1561 for (; i < tk->nkeys - 1; i++, tkey++) {
1562 tkey->kvno = (tkey + 1)->kvno;
1563 memcpy(tkey->key, (tkey + 1)->key, 8);
1567 afsconf_Touch(adir);
1568 UNLOCK_GLOBAL_MUTEX;