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>
15 #include <afs/pthread_glock.h>
17 #include "afs/sysincludes.h"
18 #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>
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 struct afsconf_servPair {
81 static struct afsconf_servPair serviceTable[] = {
82 {"afs", "afs3-fileserver", 7000,},
83 {"afscb", "afs3-callback", 7001,},
84 {"afsprot", "afs3-prserver", 7002,},
85 {"afsvldb", "afs3-vlserver", 7003,},
86 {"afskauth", "afs3-kaserver", 7004,},
87 {"afsvol", "afs3-volserver", 7005,},
88 {"afserror", "afs3-errors", 7006,},
89 {"afsnanny", "afs3-bos", 7007,},
90 {"afsupdate", "afs3-update", 7008,},
91 {"afsrmtsys", "afs3-rmtsys", 7009,},
92 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
93 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
94 {0, 0, 0} /* insert new services before this spot */
98 static int TrimLine(char *abuffer, int abufsize);
99 static int IsClientConfigDirectory(const char *path);
101 static int GetCellNT(struct afsconf_dir *adir);
103 static int afsconf_Check(register struct afsconf_dir *adir);
104 static int afsconf_Touch(register struct afsconf_dir *adir);
105 static int GetCellUnix(struct afsconf_dir *adir);
106 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
108 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
109 char *aname, char *aclone);
110 static int ParseCellLine(register char *aline, register char *aname,
111 register char *alname);
112 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
113 static int afsconf_Reopen(register struct afsconf_dir *adir);
114 static int SaveKeys(struct afsconf_dir *adir);
117 #define T_AFSDB 18 /* per RFC1183 section 1 */
121 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
122 * our code can tell if there is new info in the key files, the cell server db
123 * files or any of the other files (and reopen the thing) if the date on
124 * CellServDB changes.
127 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
128 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
129 get an fd <= 255. We allow the fileserver to claim more fds than that.
130 This has always been a problem since pr_Initialize would have the same
131 issue, but hpr_Initialize makes it more likely that we would see this.
132 Work around it. This is not generic. It's coded with the needs of
133 afsconf_* in mind only.
135 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
140 struct afsconf_iobuffer {
147 typedef struct afsconf_iobuffer afsconf_FILE;
149 static afsconf_FILE *
150 afsconf_fopen(const char *fname, const char *fmode)
155 if ((fd = open(fname, O_RDONLY)) == -1) {
159 iop = malloc(sizeof(struct afsconf_iobuffer));
166 iop->buffer = malloc(BUFFER);
167 if (iop->buffer == NULL) {
173 iop->ptr = iop->buffer;
174 iop->endptr = iop->buffer;
179 afsconf_fclose(afsconf_FILE *iop)
185 free((void *)iop->buffer);
191 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
199 if (iop->ptr == iop->endptr) {
202 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
212 iop->ptr = iop->buffer;
213 iop->endptr = iop->buffer + len;
217 if ((p - s) == (n - 1)) {
227 #define fopen afsconf_fopen
228 #define fclose afsconf_fclose
229 #define fgets afsconf_fgets
231 #define afsconf_FILE FILE
232 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
234 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
236 afsconf_FindService(const char *aname)
238 /* lookup a service name */
240 struct afsconf_servPair *tsp;
242 if (aname == NULL || aname[0] == '\0')
245 #if defined(AFS_OSF_ENV)
246 ts = getservbyname(aname, "");
248 ts = (struct servent *) getservbyname(aname, NULL);
251 /* we found it in /etc/services, so we use this value */
252 return ts->s_port; /* already in network byte order */
255 /* not found in /etc/services, see if it is one of ours */
256 for (tsp = serviceTable; tsp->port; tsp++) {
257 if ((tsp->name && (!strcmp(tsp->name, aname)))
258 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
259 return htons(tsp->port);
265 afsconf_FindIANAName(const char *aname)
267 /* lookup a service name */
268 struct afsconf_servPair *tsp;
270 if (aname == NULL || aname[0] == '\0')
273 /* see if it is one of ours */
274 for (tsp = serviceTable; tsp->port; tsp++) {
275 if ((tsp->name && (!strcmp(tsp->name, aname)))
276 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
277 return tsp->ianaName;
283 TrimLine(char *abuffer, int abufsize)
295 strlcpy(tbuffer, tp, sizeof tbuffer);
296 strlcpy(abuffer, tbuffer, abufsize);
301 * IsClientConfigDirectory() -- determine if path matches well-known
302 * client configuration directory.
305 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
306 #else /* AFS_NT40_ENV */
307 #define IS_SEP(x) ((x) == '/')
308 #endif /* AFS_NT40_ENV */
310 IsClientConfigDirectory(const char *path)
312 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
315 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
317 cc = tolower(cdir[i]);
318 pc = tolower(path[i]);
326 #else /* AFS_NT40_ENV */
329 #endif /* AFS_NT40_ENV */
335 /* hit end of one or both; allow mismatch in existence of trailing slash */
336 if (cdir[i] != '\0') {
337 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
341 if (path[i] != '\0') {
342 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
351 afsconf_Check(register struct afsconf_dir *adir)
358 register afs_int32 code;
361 /* NT client CellServDB has different file name than NT server or Unix */
362 if (IsClientConfigDirectory(adir->name)) {
363 if (!afssw_GetClientCellServDBDir(&p)) {
364 strcompose(tbuffer, sizeof(tbuffer), p, "/",
365 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
369 strncpy(tbuffer, adir->name, sizeof(tbuffer));
370 len = (int)strlen(tbuffer);
371 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
372 strncat(tbuffer, "\\", sizeof(tbuffer));
374 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
376 tbuffer[sizeof(tbuffer) - 1] = '\0';
379 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
383 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
384 #endif /* AFS_NT40_ENV */
386 code = stat(tbuffer, &tstat);
390 /* did file change? */
391 if (tstat.st_mtime == adir->timeRead) {
394 /* otherwise file has changed, so reopen it */
395 return afsconf_Reopen(adir);
398 /* set modtime on file */
400 afsconf_Touch(register struct afsconf_dir *adir)
404 struct timeval tvp[2];
409 adir->timeRead = 0; /* just in case */
412 /* NT client CellServDB has different file name than NT server or Unix */
414 if (IsClientConfigDirectory(adir->name)) {
415 if (!afssw_GetClientCellServDBDir(&p)) {
416 strcompose(tbuffer, sizeof(tbuffer), p, "/",
417 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
420 int len = (int)strlen(tbuffer);
421 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
422 strncat(tbuffer, "\\", sizeof(tbuffer));
424 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
426 tbuffer[sizeof(tbuffer) - 1] = '\0';
429 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
433 return _utime(tbuffer, NULL);
436 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
437 gettimeofday(&tvp[0], NULL);
439 return utimes(tbuffer, tvp);
440 #endif /* AFS_NT40_ENV */
444 afsconf_Open(register const char *adir)
446 register struct afsconf_dir *tdir;
447 register afs_int32 code;
450 /* zero structure and fill in name; rest is done by internal routine */
451 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
452 memset(tdir, 0, sizeof(struct afsconf_dir));
453 tdir->name = strdup(adir);
455 code = afsconf_OpenInternal(tdir, 0, 0);
457 char *afsconf_path, afs_confdir[128];
460 /* Check global place only when local Open failed for whatever reason */
461 if (!(afsconf_path = getenv("AFSCONF"))) {
462 /* 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... */
467 if (!(home_dir = getenv("HOME"))) {
468 /* Our last chance is the "/.AFSCONF" file */
469 fp = fopen("/.AFSCONF", "r");
473 return (struct afsconf_dir *)0;
475 fgets(afs_confdir, 128, fp);
480 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
481 fp = fopen(pathname, "r");
483 /* Our last chance is the "/.AFSCONF" file */
484 fp = fopen("/.AFSCONF", "r");
488 return (struct afsconf_dir *)0;
491 fgets(afs_confdir, 128, fp);
494 len = strlen(afs_confdir);
498 return (struct afsconf_dir *)0;
500 if (afs_confdir[len - 1] == '\n') {
501 afs_confdir[len - 1] = 0;
503 afsconf_path = afs_confdir;
505 tdir->name = strdup(afsconf_path);
506 code = afsconf_OpenInternal(tdir, 0, 0);
511 return (struct afsconf_dir *)0;
519 GetCellUnix(struct afsconf_dir *adir)
526 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
527 fp = fopen(tbuffer, "r");
531 rc = fgets(tbuffer, 256, fp);
537 while (*start != '\0' && isspace(*start))
540 while (*p != '\0' && !isspace(*p))
546 adir->cellName = strdup(start);
553 GetCellNT(struct afsconf_dir *adir)
555 if (IsClientConfigDirectory(adir->name)) {
556 /* NT client config dir; ThisCell is in registry (no file). */
557 return afssw_GetClientCellName(&adir->cellName);
559 /* NT server config dir; works just like Unix */
560 return GetCellUnix(adir);
564 /* The following procedures and structs are used on Windows only
565 * to enumerate the Cell information distributed within the
566 * Windows registry. (See src/WINNT/afsd/cm_config.c)
568 typedef struct _cm_enumCellRegistry {
569 afs_uint32 client; /* non-zero if client query */
570 struct afsconf_dir *adir;
571 } cm_enumCellRegistry_t;
574 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
575 char *hostNamep, unsigned short rank)
577 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
579 if (cellInfop->numServers == MAXHOSTSPERCELL)
582 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
583 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
584 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
585 cellInfop->numServers++;
591 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
594 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
595 char linkedName[256] = "";
597 struct afsconf_entry *newEntry;
600 newEntry = malloc(sizeof(struct afsconf_entry));
601 if (newEntry == NULL)
603 newEntry->cellInfo.numServers = 0;
605 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
606 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
607 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
610 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
611 newEntry->cellInfo.name[MAXCELLCHARS-1];
613 newEntry->cellInfo.linkedCell = strdup(linkedName);
615 newEntry->cellInfo.linkedCell = NULL;
616 newEntry->cellInfo.timeout = timeout;
617 newEntry->cellInfo.flags = 0;
619 newEntry->next = enump->adir->entries;
620 enump->adir->entries = newEntry;
626 #endif /* AFS_NT40_ENV */
630 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
634 register char *tp, *bp;
635 register struct afsconf_entry *curEntry;
636 struct afsconf_aliasentry *curAlias;
637 register afs_int32 code;
639 char tbuffer[256], tbuf1[256];
642 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
643 #endif /* AFS_NT40_ENV */
645 /* figure out the local cell name */
648 enumCellRegistry.adir = adir;
650 i = GetCellUnix(adir);
653 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
659 /* now parse the individual lines */
663 /* NT client/server have a CellServDB that is the same format as Unix.
664 * However, the NT client uses a different file name
666 if (IsClientConfigDirectory(adir->name)) {
667 /* NT client config dir */
670 enumCellRegistry.client = 1;
672 if (!afssw_GetClientCellServDBDir(&p)) {
673 strcompose(tbuffer, sizeof(tbuffer), p, "/",
674 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
678 strncpy(tbuffer, adir->name, sizeof(tbuffer));
679 len = (int)strlen(tbuffer);
680 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
681 strncat(tbuffer, "\\", sizeof(tbuffer));
683 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
685 tbuffer[sizeof(tbuffer) - 1] = '\0';
688 /* NT server config dir */
689 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
693 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
694 #endif /* AFS_NT40_ENV */
696 if (!stat(tbuffer, &tstat)) {
697 adir->timeRead = tstat.st_mtime;
702 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
703 tf = fopen(tbuffer, "r");
708 /* The CellServDB file is now open.
709 * The following code parses the contents of the
710 * file and creates a list with the first cell entry
711 * in the CellServDB file at the end of the list.
713 * No checking is performed for duplicates.
714 * The side effects of this process are that duplicate
715 * entries appended to the end of the CellServDB file
716 * take precedence and are found in a shorter period
721 tp = fgets(tbuffer, sizeof(tbuffer), tf);
724 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
725 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
726 continue; /* empty line */
727 if (tbuffer[0] == '>') {
728 char linkedcell[MAXCELLCHARS];
729 /* start new cell item */
731 /* thread this guy on the list */
732 curEntry->next = adir->entries;
733 adir->entries = curEntry;
737 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
738 memset(curEntry, 0, sizeof(struct afsconf_entry));
740 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
742 afsconf_CloseInternal(adir);
747 if (linkedcell[0] != '\0')
748 curEntry->cellInfo.linkedCell = strdup(linkedcell);
750 /* new host in the current cell */
752 afsconf_CloseInternal(adir);
756 i = curEntry->cellInfo.numServers;
757 if (i < MAXHOSTSPERCELL) {
758 if (cell && !strcmp(cell, curEntry->cellInfo.name))
760 ParseHostLine(tbuffer,
761 &curEntry->cellInfo.hostAddr[i],
762 curEntry->cellInfo.hostName[i],
766 ParseHostLine(tbuffer,
767 &curEntry->cellInfo.hostAddr[i],
768 curEntry->cellInfo.hostName[i], 0);
771 if (code == AFSCONF_SYNTAX) {
772 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
778 "Can't properly parse host line \"%s\" in configuration file %s\n",
783 afsconf_CloseInternal(adir);
786 curEntry->cellInfo.numServers = ++i;
789 "Too many hosts for cell %s in configuration file %s\n",
790 curEntry->cellInfo.name, tbuf1);
794 fclose(tf); /* close the file now */
796 /* end the last partially-completed cell */
798 curEntry->next = adir->entries;
799 adir->entries = curEntry;
804 * Windows maintains a CellServDB list in the Registry
805 * that supercedes the contents of the CellServDB file.
806 * Prepending these entries to the head of the list
807 * is sufficient to enforce the precedence.
809 cm_EnumerateCellRegistry( enumCellRegistry.client,
810 cm_enumCellRegistryProc,
812 #endif /* AFS_NT40_ENV */
814 /* Read in the alias list */
815 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
817 tf = fopen(tbuffer, "r");
821 tp = fgets(tbuffer, sizeof(tbuffer), tf);
824 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
826 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
827 continue; /* empty line */
830 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
833 continue; /* invalid line */
835 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
838 continue; /* invalid line */
841 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
846 curAlias = malloc(sizeof(*curAlias));
847 memset(curAlias, 0, sizeof(*curAlias));
849 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
850 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
852 curAlias->next = adir->alias_entries;
853 adir->alias_entries = curAlias;
858 /* now read the fs keys, if possible */
859 adir->keystr = (struct afsconf_keys *)0;
860 afsconf_IntGetKeys(adir);
865 /* parse a line of the form
866 *"128.2.1.3 #hostname" or
867 *"[128.2.1.3] #hostname" for clones
868 * into the appropriate pieces.
871 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
875 register afs_int32 code;
881 /* FIXME: length of aname unknown here */
882 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
886 /* FIXME: length of aname unknown here */
887 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
890 return AFSCONF_SYNTAX;
891 addr->sin_family = AF_INET;
893 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
894 addr->sin_len = sizeof(struct sockaddr_in);
896 tp = (char *)&addr->sin_addr;
904 /* parse a line of the form
905 * ">cellname [linkedcellname] [#comments]"
906 * into the appropriate pieces.
909 ParseCellLine(register char *aline, register char *aname,
910 register char *alname)
913 /* FIXME: length of aname, alname unknown here */
914 code = sscanf(aline, ">%s %s", aname, alname);
918 if (*alname == '#') {
922 return (code > 0 ? 0 : AFSCONF_SYNTAX);
925 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
927 afsconf_CellApply(struct afsconf_dir *adir,
928 int (*aproc) (struct afsconf_cell * cell, void *arock,
929 struct afsconf_dir * dir), void *arock)
931 register struct afsconf_entry *tde;
932 register afs_int32 code;
934 for (tde = adir->entries; tde; tde = tde->next) {
935 code = (*aproc) (&tde->cellInfo, arock, adir);
945 /* call aproc(entry, arock, adir) for all cell aliases.
946 * Proc must return 0, or we'll stop early and return the code it returns
949 afsconf_CellAliasApply(struct afsconf_dir *adir,
950 int (*aproc) (struct afsconf_cellalias * alias,
951 void *arock, struct afsconf_dir * dir),
954 register struct afsconf_aliasentry *tde;
955 register afs_int32 code;
957 for (tde = adir->alias_entries; tde; tde = tde->next) {
958 code = (*aproc) (&tde->aliasInfo, arock, adir);
968 afs_int32 afsconf_SawCell = 0;
971 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
972 char *aservice, struct afsconf_cell *acellInfo,
978 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
985 cell = (char *)&acellInfo->name;
987 code = afsconf_OpenInternal(adir, cell, clones);
992 #if !defined(AFS_NT40_ENV)
994 afsconf_LookupServer(const char *service, const char *protocol,
995 const char *cellName, unsigned short afsdbPort,
996 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
997 unsigned short ports[], unsigned short ipRanks[],
998 int *numServers, int *ttl, char *realCellName)
1002 unsigned char answer[1024];
1005 int cellnamelength, fullnamelength;
1012 char *IANAname = (char *) afsconf_FindIANAName(service);
1013 int tservice = afsconf_FindService(service);
1015 realCellName = NULL;
1019 if (tservice <= 0 || !IANAname)
1020 return AFSCONF_NOTFOUND; /* service not found */
1022 if (strchr(cellName,'.'))
1025 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1026 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1027 dotcellname=malloc(fullnamelength);
1029 return AFSCONF_NOTFOUND; /* service not found */
1035 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1036 IANAname, protocol, cellName);
1040 code = snprintf(dotcellname, fullnamelength, "%s.",
1045 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1046 IANAname, protocol, cellName);
1050 code = snprintf(dotcellname, fullnamelength, "%s",
1054 if ((code < 0) || (code >= fullnamelength))
1055 goto findservererror;
1057 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1058 UNLOCK_GLOBAL_MUTEX;
1070 code = AFSCONF_NOTFOUND;
1071 goto findservererror;
1075 p = answer + sizeof(HEADER); /* Skip header */
1076 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1078 code = AFSCONF_NOTFOUND;
1079 goto findservererror;
1082 p += code + QFIXEDSZ; /* Skip name */
1084 while (p < answer + len) {
1085 int type, ttl, size;
1087 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1089 code = AFSCONF_NOTFOUND;
1090 goto findservererror;
1093 p += code; /* Skip the name */
1094 type = (p[0] << 8) | p[1];
1095 p += 4; /* Skip type and class */
1096 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1097 p += 4; /* Skip the TTL */
1098 size = (p[0] << 8) | p[1];
1099 p += 2; /* Skip the size */
1101 if (type == T_AFSDB) {
1105 afsdb_type = (p[0] << 8) | p[1];
1106 if (afsdb_type == 1) {
1108 * We know this is an AFSDB record for our cell, of the
1109 * right AFSDB type. Write down the true cell name that
1110 * the resolver gave us above.
1112 realCellName = strdup(host);
1115 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1117 code = AFSCONF_NOTFOUND;
1118 goto findservererror;
1121 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1122 /* Do we want to get TTL data for the A record as well? */
1123 (he = gethostbyname(host))) {
1125 memcpy(&ipaddr, he->h_addr, he->h_length);
1126 cellHostAddrs[server_num] = ipaddr;
1127 ports[server_num] = htons(afsdbPort);
1128 ipRanks[server_num] = 0;
1129 strncpy(cellHostNames[server_num], host,
1130 sizeof(cellHostNames[server_num]));
1133 if (!minttl || ttl < minttl)
1137 if (type == T_SRV) {
1140 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1142 code = AFSCONF_NOTFOUND;
1143 goto findservererror;
1146 if ((server_num < MAXHOSTSPERCELL) &&
1147 /* Do we want to get TTL data for the A record as well? */
1148 (he = gethostbyname(host))) {
1150 memcpy(&ipaddr, he->h_addr, he->h_length);
1151 cellHostAddrs[server_num] = ipaddr;
1152 ipRanks[server_num] = (p[0] << 8) | p[1];
1153 ports[server_num] = (p[4] << 8) | p[5];
1154 /* weight = (p[2] << 8) | p[3]; */
1155 strncpy(cellHostNames[server_num], host,
1156 sizeof(cellHostNames[server_num]));
1159 if (!minttl || ttl < minttl)
1167 if (server_num == 0) { /* No AFSDB or SRV records */
1168 code = AFSCONF_NOTFOUND;
1169 goto findservererror;
1173 /* Convert the real cell name to lowercase */
1174 for (p = (unsigned char *)realCellName; *p; p++)
1178 *numServers = server_num;
1179 *ttl = minttl ? (time(0) + minttl) : 0;
1181 if ( *numServers > 0 )
1184 code = AFSCONF_NOTFOUND;
1192 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1193 struct afsconf_cell *acellInfo)
1195 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1196 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1197 unsigned short ipRanks[AFSMAXCELLHOSTS];
1198 unsigned short ports[AFSMAXCELLHOSTS];
1199 char *realCellName = NULL;
1200 int ttl, numServers, i;
1201 char *service = aservice;
1203 unsigned short afsdbport;
1205 service = "afs3-vlserver";
1209 afsdbport = afsconf_FindService(service);
1211 code = afsconf_LookupServer((const char *)service, "udp",
1212 (const char *)acellName, afsdbport,
1213 cellHostAddrs, cellHostNames,
1214 ports, ipRanks, &numServers, &ttl,
1218 acellInfo->timeout = ttl;
1219 acellInfo->numServers = numServers;
1220 for (i = 0; i < numServers; i++) {
1221 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1223 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1224 acellInfo->hostAddr[i].sin_family = AF_INET;
1225 acellInfo->hostAddr[i].sin_port = ports[i];
1228 strlcpy(acellInfo->name, realCellName,
1229 sizeof(acellInfo->name));
1231 acellInfo->linkedCell = NULL; /* no linked cell */
1232 acellInfo->flags = 0;
1238 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1239 struct afsconf_cell *acellInfo)
1242 int tservice = afsconf_FindService(aservice);
1243 const char *ianaName = afsconf_FindIANAName(aservice);
1244 struct afsconf_entry DNSce;
1245 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1246 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1247 unsigned short ipRanks[AFSMAXCELLHOSTS];
1248 unsigned short ports[AFSMAXCELLHOSTS];
1255 return AFSCONF_NOTFOUND;
1257 tservice = 0; /* port will be assigned by caller */
1260 if (ianaName == NULL)
1261 ianaName = "afs3-vlserver";
1263 DNSce.cellInfo.numServers = 0;
1266 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1267 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1269 /* ignore the ttl here since this code is only called by transitory programs
1270 * like klog, etc. */
1272 /* If we couldn't find an entry for the requested service
1273 * and that service happens to be the prservice or kaservice
1274 * then fallback to searching for afs3-vlserver and assigning
1275 * the port number here. */
1276 if (rc < 0 && tservice == 7002 || tservice == 7004) {
1277 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1278 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1281 for (i = 0; i < numServers; i++)
1282 ports[i] = tservice;
1286 if (rc < 0 || numServers == 0)
1289 for (i = 0; i < numServers; i++) {
1290 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1292 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1293 acellInfo->hostAddr[i].sin_family = AF_INET;
1295 acellInfo->hostAddr[i].sin_port = ports[i];
1297 acellInfo->hostAddr[i].sin_port = 0;
1300 acellInfo->numServers = numServers;
1301 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1302 acellInfo->linkedCell = NULL; /* no linked cell */
1303 acellInfo->flags = 0;
1306 #endif /* windows */
1307 #endif /* AFS_AFSDB_ENV */
1310 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1311 struct afsconf_cell *acellInfo)
1313 register struct afsconf_entry *tce;
1314 struct afsconf_aliasentry *tcae;
1315 struct afsconf_entry *bestce;
1316 register afs_int32 i;
1325 afsconf_Check(adir);
1328 cnLen = (int)(strlen(tcell) + 1);
1329 lcstring(tcell, tcell, cnLen);
1330 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1331 /* call to afsconf_GetLocalCell: like klog */
1333 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1335 UNLOCK_GLOBAL_MUTEX;
1340 cnLen = strlen(tcell);
1341 bestce = (struct afsconf_entry *)0;
1344 UNLOCK_GLOBAL_MUTEX;
1348 /* Look through the list of aliases */
1349 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1350 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1351 tcell = tcae->aliasInfo.realName;
1356 for (tce = adir->entries; tce; tce = tce->next) {
1357 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1358 /* found our cell */
1363 if (strlen(tce->cellInfo.name) < cnLen)
1364 continue; /* clearly wrong */
1365 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1367 ambig = 1; /* ambiguous unless we get exact match */
1371 if (!ambig && bestce && bestce->cellInfo.numServers) {
1372 *acellInfo = bestce->cellInfo; /* structure assignment */
1374 tservice = afsconf_FindService(aservice);
1376 UNLOCK_GLOBAL_MUTEX;
1377 return AFSCONF_NOTFOUND; /* service not found */
1379 for (i = 0; i < acellInfo->numServers; i++) {
1380 acellInfo->hostAddr[i].sin_port = tservice;
1383 acellInfo->timeout = 0;
1386 * Until we figure out how to separate out ubik server
1387 * queries from other server queries, only perform gethostbyname()
1388 * lookup on the specified hostnames for the client CellServDB files.
1390 if (IsClientConfigDirectory(adir->name) &&
1391 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1393 short numServers=0; /*Num active servers for the cell */
1394 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1395 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1397 memset(&hostAddr, 0, sizeof(hostAddr));
1398 memset(&hostName, 0, sizeof(hostName));
1400 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1401 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1404 if (he && he->h_addrtype == AF_INET) {
1406 /* obtain all the valid address from the list */
1407 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1408 /* check to see if this is a new address; if so insert it into the list */
1410 for (k=0, dup=0; !dup && k < numServers; k++) {
1411 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1417 hostAddr[numServers].sin_family = AF_INET;
1418 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1419 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1420 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1422 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1423 strcpy(hostName[numServers], acellInfo->hostName[j]);
1429 hostAddr[numServers] = acellInfo->hostAddr[j];
1430 strcpy(hostName[numServers], acellInfo->hostName[j]);
1435 for (i=0; i<numServers; i++) {
1436 acellInfo->hostAddr[i] = hostAddr[i];
1437 strcpy(acellInfo->hostName[i], hostName[i]);
1439 acellInfo->numServers = numServers;
1440 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1442 UNLOCK_GLOBAL_MUTEX;
1445 UNLOCK_GLOBAL_MUTEX;
1446 #ifdef AFS_AFSDB_ENV
1447 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1449 return AFSCONF_NOTFOUND;
1450 #endif /* AFS_AFSDB_ENV */
1455 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1458 static int afsconf_showcell = 0;
1464 * If a cell switch was specified in a command, then it should override the
1465 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1466 * is set and the cell name in the adir structure is used.
1467 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1469 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1470 if (!afsconf_showcell) {
1471 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1473 afsconf_showcell = 1;
1475 strncpy(aname, afscell_path, alen);
1477 afsconf_Check(adir);
1478 if (adir->cellName) {
1479 strncpy(aname, adir->cellName, alen);
1481 code = AFSCONF_UNKNOWN;
1484 UNLOCK_GLOBAL_MUTEX;
1489 afsconf_Close(struct afsconf_dir *adir)
1492 afsconf_CloseInternal(adir);
1496 UNLOCK_GLOBAL_MUTEX;
1501 afsconf_CloseInternal(register struct afsconf_dir *adir)
1503 register struct afsconf_entry *td, *nd;
1504 struct afsconf_aliasentry *ta, *na;
1505 register char *tname;
1507 tname = adir->name; /* remember name, since that's all we preserve */
1509 /* free everything we can find */
1511 free(adir->cellName);
1512 for (td = adir->entries; td; td = nd) {
1514 if (td->cellInfo.linkedCell)
1515 free(td->cellInfo.linkedCell);
1518 for (ta = adir->alias_entries; ta; ta = na) {
1526 memset(adir, 0, sizeof(struct afsconf_dir));
1527 adir->name = tname; /* restore it */
1532 afsconf_Reopen(register struct afsconf_dir *adir)
1534 register afs_int32 code;
1535 code = afsconf_CloseInternal(adir);
1538 code = afsconf_OpenInternal(adir, 0, 0);
1542 /* called during opening of config file */
1544 afsconf_IntGetKeys(struct afsconf_dir *adir)
1548 struct afsconf_keys *tstr;
1549 register afs_int32 code;
1552 /* NT client config dir has no KeyFile; don't risk attempting open
1553 * because there might be a random file of this name if dir is shared.
1555 if (IsClientConfigDirectory(adir->name)) {
1556 adir->keystr = ((struct afsconf_keys *)
1557 malloc(sizeof(struct afsconf_keys)));
1558 adir->keystr->nkeys = 0;
1561 #endif /* AFS_NT40_ENV */
1564 /* compute the key name and other setup */
1565 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1566 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1567 adir->keystr = tstr;
1570 fd = open(tbuffer, O_RDONLY);
1573 UNLOCK_GLOBAL_MUTEX;
1576 code = read(fd, tstr, sizeof(struct afsconf_keys));
1578 if (code < sizeof(afs_int32)) {
1580 UNLOCK_GLOBAL_MUTEX;
1584 /* convert key structure to host order */
1585 tstr->nkeys = ntohl(tstr->nkeys);
1587 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1589 UNLOCK_GLOBAL_MUTEX;
1593 for (fd = 0; fd < tstr->nkeys; fd++)
1594 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1596 UNLOCK_GLOBAL_MUTEX;
1600 /* get keys structure */
1602 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1604 register afs_int32 code;
1607 code = afsconf_Check(adir);
1609 UNLOCK_GLOBAL_MUTEX;
1610 return AFSCONF_FAILURE;
1612 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1613 UNLOCK_GLOBAL_MUTEX;
1617 /* get latest key */
1619 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1620 struct ktc_encryptionKey *akey)
1624 register struct afsconf_key *tk;
1625 register afs_int32 best;
1626 struct afsconf_key *bestk;
1627 register afs_int32 code;
1630 code = afsconf_Check(adir);
1632 UNLOCK_GLOBAL_MUTEX;
1633 return AFSCONF_FAILURE;
1635 maxa = adir->keystr->nkeys;
1637 best = -1; /* highest kvno we've seen yet */
1638 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1639 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1640 if (tk->kvno == 999)
1641 continue; /* skip bcrypt keys */
1642 if (tk->kvno > best) {
1647 if (bestk) { /* found any */
1649 memcpy(akey, bestk->key, 8); /* copy out latest key */
1651 *avno = bestk->kvno; /* and kvno to caller */
1652 UNLOCK_GLOBAL_MUTEX;
1655 UNLOCK_GLOBAL_MUTEX;
1656 return AFSCONF_NOTFOUND; /* didn't find any keys */
1659 /* get a particular key */
1661 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1663 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1664 register int i, maxa;
1665 register struct afsconf_key *tk;
1666 register afs_int32 code;
1669 code = afsconf_Check(adir);
1671 UNLOCK_GLOBAL_MUTEX;
1672 return AFSCONF_FAILURE;
1674 maxa = adir->keystr->nkeys;
1676 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1677 if (tk->kvno == avno) {
1678 memcpy(akey, tk->key, 8);
1679 UNLOCK_GLOBAL_MUTEX;
1684 UNLOCK_GLOBAL_MUTEX;
1685 return AFSCONF_NOTFOUND;
1688 /* save the key structure in the appropriate file */
1690 SaveKeys(struct afsconf_dir *adir)
1692 struct afsconf_keys tkeys;
1694 register afs_int32 i;
1697 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1699 /* convert it to net byte order */
1700 for (i = 0; i < tkeys.nkeys; i++)
1701 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1702 tkeys.nkeys = htonl(tkeys.nkeys);
1704 /* rewrite keys file */
1705 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1706 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1708 return AFSCONF_FAILURE;
1709 i = write(fd, &tkeys, sizeof(tkeys));
1710 if (i != sizeof(tkeys)) {
1712 return AFSCONF_FAILURE;
1715 return AFSCONF_FAILURE;
1720 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1721 afs_int32 overwrite)
1723 register struct afsconf_keys *tk;
1724 register struct afsconf_key *tkey;
1725 register afs_int32 i;
1732 if (akvno < 0 || akvno > 255) {
1733 UNLOCK_GLOBAL_MUTEX;
1738 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1739 if (tkey->kvno == akvno) {
1741 UNLOCK_GLOBAL_MUTEX;
1742 return AFSCONF_KEYINUSE;
1749 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1750 UNLOCK_GLOBAL_MUTEX;
1751 return AFSCONF_FULL;
1753 tkey = &tk->key[tk->nkeys++];
1756 memcpy(tkey->key, akey, 8);
1758 afsconf_Touch(adir);
1759 UNLOCK_GLOBAL_MUTEX;
1763 /* this proc works by sliding the other guys down, rather than using a funny
1764 kvno value, so that callers can count on getting a good key in key[0].
1767 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1769 register struct afsconf_keys *tk;
1770 register struct afsconf_key *tkey;
1777 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1778 if (tkey->kvno == akvno) {
1784 UNLOCK_GLOBAL_MUTEX;
1785 return AFSCONF_NOTFOUND;
1788 /* otherwise slide the others down. i and tkey point at the guy to delete */
1789 for (; i < tk->nkeys - 1; i++, tkey++) {
1790 tkey->kvno = (tkey + 1)->kvno;
1791 memcpy(tkey->key, (tkey + 1)->key, 8);
1795 afsconf_Touch(adir);
1796 UNLOCK_GLOBAL_MUTEX;