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>
37 #include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 #include <arpa/nameser_compat.h>
42 #endif /* AFS_NT40_ENV */
43 #include <afs/afsint.h>
58 #include <afs/afsutil.h>
59 #include "cellconfig.h"
63 #include <cm_config.h>
64 /* cm_dns.h depends on cellconfig.h */
71 struct afsconf_servPair {
77 static struct afsconf_servPair serviceTable[] = {
78 {"afs", "afs3-fileserver", 7000,},
79 {"afscb", "afs3-callback", 7001,},
80 {"afsprot", "afs3-prserver", 7002,},
81 {"afsvldb", "afs3-vlserver", 7003,},
82 {"afskauth", "afs3-kaserver", 7004,},
83 {"afsvol", "afs3-volserver", 7005,},
84 {"afserror", "afs3-errors", 7006,},
85 {"afsnanny", "afs3-bos", 7007,},
86 {"afsupdate", "afs3-update", 7008,},
87 {"afsrmtsys", "afs3-rmtsys", 7009,},
88 {"afsres", NULL, 7010,},/* residency database for MR-AFS */
89 {"afsremio", NULL, 7011,}, /* remote I/O interface for MR-AFS */
90 {0, 0, 0} /* insert new services before this spot */
94 static int TrimLine(char *abuffer, int abufsize);
95 static int IsClientConfigDirectory(const char *path);
97 static int GetCellNT(struct afsconf_dir *adir);
99 static int afsconf_Check(register struct afsconf_dir *adir);
100 static int afsconf_Touch(register struct afsconf_dir *adir);
101 static int GetCellUnix(struct afsconf_dir *adir);
102 static int afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
104 static int ParseHostLine(char *aline, register struct sockaddr_in *addr,
105 char *aname, char *aclone);
106 static int ParseCellLine(register char *aline, register char *aname,
107 register char *alname);
108 static int afsconf_CloseInternal(register struct afsconf_dir *adir);
109 static int afsconf_Reopen(register struct afsconf_dir *adir);
110 static int SaveKeys(struct afsconf_dir *adir);
113 #define T_AFSDB 18 /* per RFC1183 section 1 */
116 #define T_SRV 33 /* RFC2782 */
120 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
121 * our code can tell if there is new info in the key files, the cell server db
122 * files or any of the other files (and reopen the thing) if the date on
123 * CellServDB changes.
126 #if defined(AFS_SUN5_ENV) && !defined(__sparcv9)
127 /* Solaris through 10 in 32 bit mode will return EMFILE if fopen can't
128 get an fd <= 255. We allow the fileserver to claim more fds than that.
129 This has always been a problem since pr_Initialize would have the same
130 issue, but hpr_Initialize makes it more likely that we would see this.
131 Work around it. This is not generic. It's coded with the needs of
132 afsconf_* in mind only.
134 http://www.opensolaris.org/os/community/onnv/flag-days/pages/2006042001/
139 struct afsconf_iobuffer {
146 typedef struct afsconf_iobuffer afsconf_FILE;
148 static afsconf_FILE *
149 afsconf_fopen(const char *fname, const char *fmode)
154 if ((fd = open(fname, O_RDONLY)) == -1) {
158 iop = malloc(sizeof(struct afsconf_iobuffer));
165 iop->buffer = malloc(BUFFER);
166 if (iop->buffer == NULL) {
172 iop->ptr = iop->buffer;
173 iop->endptr = iop->buffer;
178 afsconf_fclose(afsconf_FILE *iop)
184 free((void *)iop->buffer);
190 afsconf_fgets(char *s, int n, afsconf_FILE *iop)
198 if (iop->ptr == iop->endptr) {
201 if ((len = read(iop->_file, (void *)iop->buffer, BUFFER)) == -1) {
211 iop->ptr = iop->buffer;
212 iop->endptr = iop->buffer + len;
216 if ((p - s) == (n - 1)) {
226 #define fopen afsconf_fopen
227 #define fclose afsconf_fclose
228 #define fgets afsconf_fgets
230 #define afsconf_FILE FILE
231 #endif /* AFS_SUN5_ENV && ! __sparcv9 */
233 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
235 afsconf_FindService(const char *aname)
237 /* lookup a service name */
239 struct afsconf_servPair *tsp;
241 if (aname == NULL || aname[0] == '\0')
244 #if defined(AFS_OSF_ENV)
245 ts = getservbyname(aname, "");
247 ts = (struct servent *) getservbyname(aname, NULL);
250 /* we found it in /etc/services, so we use this value */
251 return ts->s_port; /* already in network byte order */
254 /* not found in /etc/services, see if it is one of ours */
255 for (tsp = serviceTable; tsp->port; tsp++) {
256 if ((tsp->name && (!strcmp(tsp->name, aname)))
257 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
258 return htons(tsp->port);
264 afsconf_FindIANAName(const char *aname)
266 /* lookup a service name */
267 struct afsconf_servPair *tsp;
269 if (aname == NULL || aname[0] == '\0')
272 /* see if it is one of ours */
273 for (tsp = serviceTable; tsp->port; tsp++) {
274 if ((tsp->name && (!strcmp(tsp->name, aname)))
275 || (tsp->ianaName && (!strcmp(tsp->ianaName, aname))))
276 return tsp->ianaName;
282 TrimLine(char *abuffer, int abufsize)
294 strlcpy(tbuffer, tp, sizeof tbuffer);
295 strlcpy(abuffer, tbuffer, abufsize);
300 * IsClientConfigDirectory() -- determine if path matches well-known
301 * client configuration directory.
304 #define IS_SEP(x) ((x) == '\\' || (x) == '/')
305 #else /* AFS_NT40_ENV */
306 #define IS_SEP(x) ((x) == '/')
307 #endif /* AFS_NT40_ENV */
309 IsClientConfigDirectory(const char *path)
311 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
314 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
316 cc = tolower(cdir[i]);
317 pc = tolower(path[i]);
325 #else /* AFS_NT40_ENV */
328 #endif /* AFS_NT40_ENV */
334 /* hit end of one or both; allow mismatch in existence of trailing slash */
335 if (cdir[i] != '\0') {
336 if (!IS_SEP(cdir[i]) || (cdir[i + 1] != '\0')) {
340 if (path[i] != '\0') {
341 if (!IS_SEP(path[i]) || (path[i + 1] != '\0')) {
350 afsconf_Check(register struct afsconf_dir *adir)
357 register afs_int32 code;
360 /* NT client CellServDB has different file name than NT server or Unix */
361 if (IsClientConfigDirectory(adir->name)) {
362 if (!afssw_GetClientCellServDBDir(&p)) {
363 strcompose(tbuffer, sizeof(tbuffer), p, "/",
364 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
368 strncpy(tbuffer, adir->name, sizeof(tbuffer));
369 len = (int)strlen(tbuffer);
370 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
371 strncat(tbuffer, "\\", sizeof(tbuffer));
373 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
375 tbuffer[sizeof(tbuffer) - 1] = '\0';
378 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
382 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
383 #endif /* AFS_NT40_ENV */
385 code = stat(tbuffer, &tstat);
389 /* did file change? */
390 if (tstat.st_mtime == adir->timeRead) {
393 /* otherwise file has changed, so reopen it */
394 return afsconf_Reopen(adir);
397 /* set modtime on file */
399 afsconf_Touch(register struct afsconf_dir *adir)
403 struct timeval tvp[2];
408 adir->timeRead = 0; /* just in case */
411 /* NT client CellServDB has different file name than NT server or Unix */
413 if (IsClientConfigDirectory(adir->name)) {
414 if (!afssw_GetClientCellServDBDir(&p)) {
415 strcompose(tbuffer, sizeof(tbuffer), p, "/",
416 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
419 int len = (int)strlen(tbuffer);
420 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
421 strncat(tbuffer, "\\", sizeof(tbuffer));
423 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
425 tbuffer[sizeof(tbuffer) - 1] = '\0';
428 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
432 return _utime(tbuffer, NULL);
435 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
436 gettimeofday(&tvp[0], NULL);
438 return utimes(tbuffer, tvp);
439 #endif /* AFS_NT40_ENV */
443 afsconf_Open(register const char *adir)
445 register struct afsconf_dir *tdir;
446 register afs_int32 code;
449 /* zero structure and fill in name; rest is done by internal routine */
450 tdir = (struct afsconf_dir *)malloc(sizeof(struct afsconf_dir));
451 memset(tdir, 0, sizeof(struct afsconf_dir));
452 tdir->name = strdup(adir);
454 code = afsconf_OpenInternal(tdir, 0, 0);
456 char *afsconf_path, afs_confdir[128];
459 /* Check global place only when local Open failed for whatever reason */
460 if (!(afsconf_path = getenv("AFSCONF"))) {
461 /* 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... */
466 if (!(home_dir = getenv("HOME"))) {
467 /* Our last chance is the "/.AFSCONF" file */
468 fp = fopen("/.AFSCONF", "r");
472 return (struct afsconf_dir *)0;
474 fgets(afs_confdir, 128, fp);
479 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
480 fp = fopen(pathname, "r");
482 /* Our last chance is the "/.AFSCONF" file */
483 fp = fopen("/.AFSCONF", "r");
487 return (struct afsconf_dir *)0;
490 fgets(afs_confdir, 128, fp);
493 len = strlen(afs_confdir);
497 return (struct afsconf_dir *)0;
499 if (afs_confdir[len - 1] == '\n') {
500 afs_confdir[len - 1] = 0;
502 afsconf_path = afs_confdir;
504 tdir->name = strdup(afsconf_path);
505 code = afsconf_OpenInternal(tdir, 0, 0);
510 return (struct afsconf_dir *)0;
518 GetCellUnix(struct afsconf_dir *adir)
525 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
526 fp = fopen(tbuffer, "r");
530 rc = fgets(tbuffer, 256, fp);
536 while (*start != '\0' && isspace(*start))
539 while (*p != '\0' && !isspace(*p))
545 adir->cellName = strdup(start);
552 GetCellNT(struct afsconf_dir *adir)
554 if (IsClientConfigDirectory(adir->name)) {
555 /* NT client config dir; ThisCell is in registry (no file). */
556 return afssw_GetClientCellName(&adir->cellName);
558 /* NT server config dir; works just like Unix */
559 return GetCellUnix(adir);
563 /* The following procedures and structs are used on Windows only
564 * to enumerate the Cell information distributed within the
565 * Windows registry. (See src/WINNT/afsd/cm_config.c)
567 typedef struct _cm_enumCellRegistry {
568 afs_uint32 client; /* non-zero if client query */
569 struct afsconf_dir *adir;
570 } cm_enumCellRegistry_t;
573 cm_serverConfigProc(void *rockp, struct sockaddr_in *addrp,
574 char *hostNamep, unsigned short rank)
576 struct afsconf_cell *cellInfop = (struct afsconf_cell *)rockp;
578 if (cellInfop->numServers == MAXHOSTSPERCELL)
581 cellInfop->hostAddr[cellInfop->numServers] = *addrp;
582 strncpy(cellInfop->hostName[cellInfop->numServers], hostNamep, MAXHOSTCHARS);
583 cellInfop->hostName[cellInfop->numServers][MAXHOSTCHARS-1] = '\0';
584 cellInfop->numServers++;
590 cm_enumCellRegistryProc(void *rockp, char * cellNamep)
593 cm_enumCellRegistry_t *enump = (cm_enumCellRegistry_t *)rockp;
594 char linkedName[256] = "";
596 struct afsconf_entry *newEntry;
599 newEntry = malloc(sizeof(struct afsconf_entry));
600 if (newEntry == NULL)
602 newEntry->cellInfo.numServers = 0;
604 code = cm_SearchCellRegistry(enump->client, cellNamep, NULL, linkedName, cm_serverConfigProc, &newEntry->cellInfo);
605 if (code == CM_ERROR_FORCE_DNS_LOOKUP)
606 code = cm_SearchCellByDNS(cellNamep, NULL, &timeout, cm_serverConfigProc, &newEntry->cellInfo);
609 strncpy(newEntry->cellInfo.name, cellNamep, MAXCELLCHARS);
610 newEntry->cellInfo.name[MAXCELLCHARS-1];
612 newEntry->cellInfo.linkedCell = strdup(linkedName);
614 newEntry->cellInfo.linkedCell = NULL;
615 newEntry->cellInfo.timeout = timeout;
616 newEntry->cellInfo.flags = 0;
618 newEntry->next = enump->adir->entries;
619 enump->adir->entries = newEntry;
625 #endif /* AFS_NT40_ENV */
629 afsconf_OpenInternal(register struct afsconf_dir *adir, char *cell,
633 register char *tp, *bp;
634 register struct afsconf_entry *curEntry;
635 struct afsconf_aliasentry *curAlias;
636 register afs_int32 code;
638 char tbuffer[256], tbuf1[256];
641 cm_enumCellRegistry_t enumCellRegistry = {0, 0};
642 #endif /* AFS_NT40_ENV */
644 /* figure out the local cell name */
647 enumCellRegistry.adir = adir;
649 i = GetCellUnix(adir);
652 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
658 /* now parse the individual lines */
662 /* NT client/server have a CellServDB that is the same format as Unix.
663 * However, the NT client uses a different file name
665 if (IsClientConfigDirectory(adir->name)) {
666 /* NT client config dir */
669 enumCellRegistry.client = 1;
671 if (!afssw_GetClientCellServDBDir(&p)) {
672 strcompose(tbuffer, sizeof(tbuffer), p, "/",
673 AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
677 strncpy(tbuffer, adir->name, sizeof(tbuffer));
678 len = (int)strlen(tbuffer);
679 if (tbuffer[len - 1] != '\\' && tbuffer[len - 1] != '/') {
680 strncat(tbuffer, "\\", sizeof(tbuffer));
682 strncat(tbuffer, AFSDIR_CELLSERVDB_FILE_NTCLIENT,
684 tbuffer[sizeof(tbuffer) - 1] = '\0';
687 /* NT server config dir */
688 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE,
692 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
693 #endif /* AFS_NT40_ENV */
695 if (!stat(tbuffer, &tstat)) {
696 adir->timeRead = tstat.st_mtime;
701 strlcpy(tbuf1, tbuffer, sizeof tbuf1);
702 tf = fopen(tbuffer, "r");
707 /* The CellServDB file is now open.
708 * The following code parses the contents of the
709 * file and creates a list with the first cell entry
710 * in the CellServDB file at the end of the list.
712 * No checking is performed for duplicates.
713 * The side effects of this process are that duplicate
714 * entries appended to the end of the CellServDB file
715 * take precedence and are found in a shorter period
720 tp = fgets(tbuffer, sizeof(tbuffer), tf);
723 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
724 if (tbuffer[0] == 0 || tbuffer[0] == '\n')
725 continue; /* empty line */
726 if (tbuffer[0] == '>') {
727 char linkedcell[MAXCELLCHARS];
728 /* start new cell item */
730 /* thread this guy on the list */
731 curEntry->next = adir->entries;
732 adir->entries = curEntry;
736 (struct afsconf_entry *)malloc(sizeof(struct afsconf_entry));
737 memset(curEntry, 0, sizeof(struct afsconf_entry));
739 ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
741 afsconf_CloseInternal(adir);
746 if (linkedcell[0] != '\0')
747 curEntry->cellInfo.linkedCell = strdup(linkedcell);
749 /* new host in the current cell */
751 afsconf_CloseInternal(adir);
755 i = curEntry->cellInfo.numServers;
756 if (i < MAXHOSTSPERCELL) {
757 if (cell && !strcmp(cell, curEntry->cellInfo.name))
759 ParseHostLine(tbuffer,
760 &curEntry->cellInfo.hostAddr[i],
761 curEntry->cellInfo.hostName[i],
765 ParseHostLine(tbuffer,
766 &curEntry->cellInfo.hostAddr[i],
767 curEntry->cellInfo.hostName[i], 0);
770 if (code == AFSCONF_SYNTAX) {
771 for (bp = tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
777 "Can't properly parse host line \"%s\" in configuration file %s\n",
782 afsconf_CloseInternal(adir);
785 curEntry->cellInfo.numServers = ++i;
788 "Too many hosts for cell %s in configuration file %s\n",
789 curEntry->cellInfo.name, tbuf1);
793 fclose(tf); /* close the file now */
795 /* end the last partially-completed cell */
797 curEntry->next = adir->entries;
798 adir->entries = curEntry;
803 * Windows maintains a CellServDB list in the Registry
804 * that supercedes the contents of the CellServDB file.
805 * Prepending these entries to the head of the list
806 * is sufficient to enforce the precedence.
808 cm_EnumerateCellRegistry( enumCellRegistry.client,
809 cm_enumCellRegistryProc,
811 #endif /* AFS_NT40_ENV */
813 /* Read in the alias list */
814 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLALIAS_FILE, NULL);
816 tf = fopen(tbuffer, "r");
820 tp = fgets(tbuffer, sizeof(tbuffer), tf);
823 TrimLine(tbuffer, sizeof tbuffer); /* remove white space */
825 if (tbuffer[0] == '\0' || tbuffer[0] == '\n' || tbuffer[0] == '#')
826 continue; /* empty line */
829 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t')
832 continue; /* invalid line */
834 while (tp[0] != '\0' && (tp[0] == ' ' || tp[0] == '\t'))
837 continue; /* invalid line */
840 while (tp[0] != '\0' && tp[0] != ' ' && tp[0] != '\t' && tp[0] != '\r'
845 curAlias = malloc(sizeof(*curAlias));
846 memset(curAlias, 0, sizeof(*curAlias));
848 strlcpy(curAlias->aliasInfo.aliasName, aliasPtr, sizeof curAlias->aliasInfo.aliasName);
849 strlcpy(curAlias->aliasInfo.realName, tbuffer, sizeof curAlias->aliasInfo.realName);
851 curAlias->next = adir->alias_entries;
852 adir->alias_entries = curAlias;
857 /* now read the fs keys, if possible */
858 adir->keystr = (struct afsconf_keys *)0;
859 afsconf_IntGetKeys(adir);
864 /* parse a line of the form
865 *"128.2.1.3 #hostname" or
866 *"[128.2.1.3] #hostname" for clones
867 * into the appropriate pieces.
870 ParseHostLine(char *aline, register struct sockaddr_in *addr, char *aname,
874 register afs_int32 code;
880 /* FIXME: length of aname unknown here */
881 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
885 /* FIXME: length of aname unknown here */
886 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
889 return AFSCONF_SYNTAX;
890 addr->sin_family = AF_INET;
892 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
893 addr->sin_len = sizeof(struct sockaddr_in);
895 tp = (char *)&addr->sin_addr;
903 /* parse a line of the form
904 * ">cellname [linkedcellname] [#comments]"
905 * into the appropriate pieces.
908 ParseCellLine(register char *aline, register char *aname,
909 register char *alname)
912 /* FIXME: length of aname, alname unknown here */
913 code = sscanf(aline, ">%s %s", aname, alname);
917 if (*alname == '#') {
921 return (code > 0 ? 0 : AFSCONF_SYNTAX);
924 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
926 afsconf_CellApply(struct afsconf_dir *adir,
927 int (*aproc) (struct afsconf_cell * cell, void *arock,
928 struct afsconf_dir * dir), void *arock)
930 register struct afsconf_entry *tde;
931 register afs_int32 code;
933 for (tde = adir->entries; tde; tde = tde->next) {
934 code = (*aproc) (&tde->cellInfo, arock, adir);
944 /* call aproc(entry, arock, adir) for all cell aliases.
945 * Proc must return 0, or we'll stop early and return the code it returns
948 afsconf_CellAliasApply(struct afsconf_dir *adir,
949 int (*aproc) (struct afsconf_cellalias * alias,
950 void *arock, struct afsconf_dir * dir),
953 register struct afsconf_aliasentry *tde;
954 register afs_int32 code;
956 for (tde = adir->alias_entries; tde; tde = tde->next) {
957 code = (*aproc) (&tde->aliasInfo, arock, adir);
967 afs_int32 afsconf_SawCell = 0;
970 afsconf_GetExtendedCellInfo(struct afsconf_dir *adir, char *acellName,
971 char *aservice, struct afsconf_cell *acellInfo,
977 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
984 cell = (char *)&acellInfo->name;
986 code = afsconf_OpenInternal(adir, cell, clones);
990 #if !defined(AFS_NT40_ENV)
992 afsconf_LookupServer(const char *service, const char *protocol,
993 const char *cellName, unsigned short afsdbPort,
994 int *cellHostAddrs, char cellHostNames[][MAXHOSTCHARS],
995 unsigned short ports[], unsigned short ipRanks[],
996 int *numServers, int *ttl, char **arealCellName)
1000 unsigned char answer[1024];
1004 int cellnamelength, fullnamelength;
1011 char *IANAname = (char *) afsconf_FindIANAName(service);
1012 int tservice = afsconf_FindService(service);
1014 realCellName = NULL;
1018 if (tservice <= 0 || !IANAname)
1019 return AFSCONF_NOTFOUND; /* service not found */
1021 if (strchr(cellName,'.'))
1024 cellnamelength=strlen(cellName); /* _ ._ . . \0 */
1025 fullnamelength=cellnamelength+strlen(protocol)+strlen(IANAname)+6;
1026 dotcellname=malloc(fullnamelength);
1028 return AFSCONF_NOTFOUND; /* service not found */
1030 #ifdef HAVE_RES_RETRANSRETRY
1031 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
1035 * Rx timeout is typically 56 seconds; limit user experience to
1046 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s.",
1047 IANAname, protocol, cellName);
1051 code = snprintf(dotcellname, fullnamelength, "%s.",
1056 code = snprintf(dotcellname, fullnamelength, "_%s._%s.%s",
1057 IANAname, protocol, cellName);
1061 code = snprintf(dotcellname, fullnamelength, "%s",
1065 if ((code < 0) || (code >= fullnamelength))
1066 goto findservererror;
1068 len = res_search(dotcellname, C_IN, dnstype, answer, sizeof(answer));
1069 UNLOCK_GLOBAL_MUTEX;
1081 code = AFSCONF_NOTFOUND;
1082 goto findservererror;
1086 p = answer + sizeof(HEADER); /* Skip header */
1087 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1089 code = AFSCONF_NOTFOUND;
1090 goto findservererror;
1093 p += code + QFIXEDSZ; /* Skip name */
1095 while (p < answer + len) {
1096 int type, ttl, size;
1098 code = dn_expand(answer, answer + len, p, host, sizeof(host));
1100 code = AFSCONF_NOTFOUND;
1101 goto findservererror;
1104 p += code; /* Skip the name */
1105 type = (p[0] << 8) | p[1];
1106 p += 4; /* Skip type and class */
1107 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1108 p += 4; /* Skip the TTL */
1109 size = (p[0] << 8) | p[1];
1110 p += 2; /* Skip the size */
1112 if (type == T_AFSDB) {
1116 afsdb_type = (p[0] << 8) | p[1];
1117 if (afsdb_type == 1) {
1119 * We know this is an AFSDB record for our cell, of the
1120 * right AFSDB type. Write down the true cell name that
1121 * the resolver gave us above.
1123 realCellName = strdup(host);
1126 code = dn_expand(answer, answer + len, p + 2, host, sizeof(host));
1128 code = AFSCONF_NOTFOUND;
1129 goto findservererror;
1132 if ((afsdb_type == 1) && (server_num < MAXHOSTSPERCELL) &&
1133 /* Do we want to get TTL data for the A record as well? */
1134 (he = gethostbyname(host))) {
1136 memcpy(&ipaddr, he->h_addr, he->h_length);
1137 cellHostAddrs[server_num] = ipaddr;
1138 ports[server_num] = afsdbPort;
1139 ipRanks[server_num] = 0;
1140 strncpy(cellHostNames[server_num], host,
1141 sizeof(cellHostNames[server_num]));
1144 if (!minttl || ttl < minttl)
1148 if (type == T_SRV) {
1150 /* math here: _ is 1, _ ._ is 3, _ ._ . is 4. then the domain. */
1151 if ((strncmp(host + 1, IANAname, strlen(IANAname)) == 0) &&
1152 (strncmp(host + strlen(IANAname) + 3, protocol,
1153 strlen(protocol)) == 0)) {
1154 realCellName = strdup(host + strlen(IANAname) +
1155 strlen(protocol) + 4);
1158 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1160 code = AFSCONF_NOTFOUND;
1161 goto findservererror;
1164 if ((server_num < MAXHOSTSPERCELL) &&
1165 /* Do we want to get TTL data for the A record as well? */
1166 (he = gethostbyname(host))) {
1168 memcpy(&ipaddr, he->h_addr, he->h_length);
1169 cellHostAddrs[server_num] = ipaddr;
1170 ipRanks[server_num] = (p[0] << 8) | p[1];
1171 ports[server_num] = (p[4] << 8) | p[5];
1172 /* weight = (p[2] << 8) | p[3]; */
1173 strncpy(cellHostNames[server_num], host,
1174 sizeof(cellHostNames[server_num]));
1177 if (!minttl || ttl < minttl)
1185 if (server_num == 0) { /* No AFSDB or SRV records */
1186 code = AFSCONF_NOTFOUND;
1187 goto findservererror;
1191 /* Convert the real cell name to lowercase */
1192 for (p = (unsigned char *)realCellName; *p; p++)
1196 *arealCellName = realCellName;
1198 *numServers = server_num;
1199 *ttl = minttl ? (time(0) + minttl) : 0;
1201 if ( *numServers > 0 )
1204 code = AFSCONF_NOTFOUND;
1212 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1213 struct afsconf_cell *acellInfo)
1215 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1216 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1217 unsigned short ipRanks[AFSMAXCELLHOSTS];
1218 unsigned short ports[AFSMAXCELLHOSTS];
1219 char *realCellName = NULL;
1220 int ttl, numServers, i;
1221 char *service = aservice;
1223 unsigned short afsdbport;
1225 service = "afs3-vlserver";
1226 afsdbport = htons(7003);
1229 afsdbport = afsconf_FindService(service);
1231 code = afsconf_LookupServer((const char *)service, "udp",
1232 (const char *)acellName, afsdbport,
1233 cellHostAddrs, cellHostNames,
1234 ports, ipRanks, &numServers, &ttl,
1238 acellInfo->timeout = ttl;
1239 acellInfo->numServers = numServers;
1240 for (i = 0; i < numServers; i++) {
1241 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1243 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1244 acellInfo->hostAddr[i].sin_family = AF_INET;
1245 acellInfo->hostAddr[i].sin_port = ports[i];
1248 strlcpy(acellInfo->name, realCellName,
1249 sizeof(acellInfo->name));
1251 acellInfo->linkedCell = NULL; /* no linked cell */
1252 acellInfo->flags = 0;
1258 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1259 struct afsconf_cell *acellInfo)
1262 int tservice = afsconf_FindService(aservice); /* network byte order */
1263 const char *ianaName = afsconf_FindIANAName(aservice);
1264 struct afsconf_entry DNSce;
1265 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1266 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1267 unsigned short ipRanks[AFSMAXCELLHOSTS];
1268 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1275 return AFSCONF_NOTFOUND;
1277 tservice = 0; /* port will be assigned by caller */
1280 if (ianaName == NULL)
1281 ianaName = "afs3-vlserver";
1283 DNSce.cellInfo.numServers = 0;
1286 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1287 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1289 /* ignore the ttl here since this code is only called by transitory programs
1290 * like klog, etc. */
1292 /* If we couldn't find an entry for the requested service
1293 * and that service happens to be the prservice or kaservice
1294 * then fallback to searching for afs3-vlserver and assigning
1295 * the port number here. */
1296 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1297 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1298 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1301 for (i = 0; i < numServers; i++)
1302 ports[i] = tservice;
1306 if (rc < 0 || numServers == 0)
1309 for (i = 0; i < numServers; i++) {
1310 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1312 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1313 acellInfo->hostAddr[i].sin_family = AF_INET;
1315 acellInfo->hostAddr[i].sin_port = ports[i];
1317 acellInfo->hostAddr[i].sin_port = 0;
1320 acellInfo->numServers = numServers;
1321 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1322 acellInfo->linkedCell = NULL; /* no linked cell */
1323 acellInfo->flags = 0;
1326 #endif /* windows */
1329 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1330 struct afsconf_cell *acellInfo)
1332 register struct afsconf_entry *tce;
1333 struct afsconf_aliasentry *tcae;
1334 struct afsconf_entry *bestce;
1335 register afs_int32 i;
1344 afsconf_Check(adir);
1347 cnLen = (int)(strlen(tcell) + 1);
1348 lcstring(tcell, tcell, cnLen);
1349 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1350 /* call to afsconf_GetLocalCell: like klog */
1352 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1354 UNLOCK_GLOBAL_MUTEX;
1359 cnLen = strlen(tcell);
1360 bestce = (struct afsconf_entry *)0;
1363 UNLOCK_GLOBAL_MUTEX;
1367 /* Look through the list of aliases */
1368 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1369 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1370 tcell = tcae->aliasInfo.realName;
1375 for (tce = adir->entries; tce; tce = tce->next) {
1376 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1377 /* found our cell */
1382 if (strlen(tce->cellInfo.name) < cnLen)
1383 continue; /* clearly wrong */
1384 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1386 ambig = 1; /* ambiguous unless we get exact match */
1390 if (!ambig && bestce && bestce->cellInfo.numServers) {
1391 *acellInfo = bestce->cellInfo; /* structure assignment */
1393 tservice = afsconf_FindService(aservice);
1395 UNLOCK_GLOBAL_MUTEX;
1396 return AFSCONF_NOTFOUND; /* service not found */
1398 for (i = 0; i < acellInfo->numServers; i++) {
1399 acellInfo->hostAddr[i].sin_port = tservice;
1402 acellInfo->timeout = 0;
1405 * Until we figure out how to separate out ubik server
1406 * queries from other server queries, only perform gethostbyname()
1407 * lookup on the specified hostnames for the client CellServDB files.
1409 if (IsClientConfigDirectory(adir->name) &&
1410 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1412 short numServers=0; /*Num active servers for the cell */
1413 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1414 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1416 memset(&hostAddr, 0, sizeof(hostAddr));
1417 memset(&hostName, 0, sizeof(hostName));
1419 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1420 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1423 if (he && he->h_addrtype == AF_INET) {
1425 /* obtain all the valid address from the list */
1426 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1427 /* check to see if this is a new address; if so insert it into the list */
1429 for (k=0, dup=0; !dup && k < numServers; k++) {
1430 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1436 hostAddr[numServers].sin_family = AF_INET;
1437 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1438 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1439 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1441 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1442 strcpy(hostName[numServers], acellInfo->hostName[j]);
1448 hostAddr[numServers] = acellInfo->hostAddr[j];
1449 strcpy(hostName[numServers], acellInfo->hostName[j]);
1454 for (i=0; i<numServers; i++) {
1455 acellInfo->hostAddr[i] = hostAddr[i];
1456 strcpy(acellInfo->hostName[i], hostName[i]);
1458 acellInfo->numServers = numServers;
1459 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1461 UNLOCK_GLOBAL_MUTEX;
1464 UNLOCK_GLOBAL_MUTEX;
1465 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1470 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1473 static int afsconf_showcell = 0;
1479 * If a cell switch was specified in a command, then it should override the
1480 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1481 * is set and the cell name in the adir structure is used.
1482 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1484 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1485 if (!afsconf_showcell) {
1486 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1488 afsconf_showcell = 1;
1490 strncpy(aname, afscell_path, alen);
1492 afsconf_Check(adir);
1493 if (adir->cellName) {
1494 strncpy(aname, adir->cellName, alen);
1496 code = AFSCONF_UNKNOWN;
1499 UNLOCK_GLOBAL_MUTEX;
1504 afsconf_Close(struct afsconf_dir *adir)
1507 afsconf_CloseInternal(adir);
1511 UNLOCK_GLOBAL_MUTEX;
1516 afsconf_CloseInternal(register struct afsconf_dir *adir)
1518 register struct afsconf_entry *td, *nd;
1519 struct afsconf_aliasentry *ta, *na;
1520 register char *tname;
1522 tname = adir->name; /* remember name, since that's all we preserve */
1524 /* free everything we can find */
1526 free(adir->cellName);
1527 for (td = adir->entries; td; td = nd) {
1529 if (td->cellInfo.linkedCell)
1530 free(td->cellInfo.linkedCell);
1533 for (ta = adir->alias_entries; ta; ta = na) {
1541 memset(adir, 0, sizeof(struct afsconf_dir));
1542 adir->name = tname; /* restore it */
1547 afsconf_Reopen(register struct afsconf_dir *adir)
1549 register afs_int32 code;
1550 code = afsconf_CloseInternal(adir);
1553 code = afsconf_OpenInternal(adir, 0, 0);
1557 /* called during opening of config file */
1559 afsconf_IntGetKeys(struct afsconf_dir *adir)
1563 struct afsconf_keys *tstr;
1564 register afs_int32 code;
1567 /* NT client config dir has no KeyFile; don't risk attempting open
1568 * because there might be a random file of this name if dir is shared.
1570 if (IsClientConfigDirectory(adir->name)) {
1571 adir->keystr = ((struct afsconf_keys *)
1572 malloc(sizeof(struct afsconf_keys)));
1573 adir->keystr->nkeys = 0;
1576 #endif /* AFS_NT40_ENV */
1579 /* compute the key name and other setup */
1580 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1581 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1582 adir->keystr = tstr;
1585 fd = open(tbuffer, O_RDONLY);
1588 UNLOCK_GLOBAL_MUTEX;
1591 code = read(fd, tstr, sizeof(struct afsconf_keys));
1593 if (code < sizeof(afs_int32)) {
1595 UNLOCK_GLOBAL_MUTEX;
1599 /* convert key structure to host order */
1600 tstr->nkeys = ntohl(tstr->nkeys);
1602 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1604 UNLOCK_GLOBAL_MUTEX;
1608 for (fd = 0; fd < tstr->nkeys; fd++)
1609 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1611 UNLOCK_GLOBAL_MUTEX;
1615 /* get keys structure */
1617 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1619 register afs_int32 code;
1622 code = afsconf_Check(adir);
1624 UNLOCK_GLOBAL_MUTEX;
1625 return AFSCONF_FAILURE;
1627 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1628 UNLOCK_GLOBAL_MUTEX;
1632 /* get latest key */
1634 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1635 struct ktc_encryptionKey *akey)
1639 register struct afsconf_key *tk;
1640 register afs_int32 best;
1641 struct afsconf_key *bestk;
1642 register afs_int32 code;
1645 code = afsconf_Check(adir);
1647 UNLOCK_GLOBAL_MUTEX;
1648 return AFSCONF_FAILURE;
1650 maxa = adir->keystr->nkeys;
1652 best = -1; /* highest kvno we've seen yet */
1653 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1654 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1655 if (tk->kvno == 999)
1656 continue; /* skip bcrypt keys */
1657 if (tk->kvno > best) {
1662 if (bestk) { /* found any */
1664 memcpy(akey, bestk->key, 8); /* copy out latest key */
1666 *avno = bestk->kvno; /* and kvno to caller */
1667 UNLOCK_GLOBAL_MUTEX;
1670 UNLOCK_GLOBAL_MUTEX;
1671 return AFSCONF_NOTFOUND; /* didn't find any keys */
1674 /* get a particular key */
1676 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1678 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1679 register int i, maxa;
1680 register struct afsconf_key *tk;
1681 register afs_int32 code;
1684 code = afsconf_Check(adir);
1686 UNLOCK_GLOBAL_MUTEX;
1687 return AFSCONF_FAILURE;
1689 maxa = adir->keystr->nkeys;
1691 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1692 if (tk->kvno == avno) {
1693 memcpy(akey, tk->key, 8);
1694 UNLOCK_GLOBAL_MUTEX;
1699 UNLOCK_GLOBAL_MUTEX;
1700 return AFSCONF_NOTFOUND;
1703 /* save the key structure in the appropriate file */
1705 SaveKeys(struct afsconf_dir *adir)
1707 struct afsconf_keys tkeys;
1709 register afs_int32 i;
1712 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1714 /* convert it to net byte order */
1715 for (i = 0; i < tkeys.nkeys; i++)
1716 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1717 tkeys.nkeys = htonl(tkeys.nkeys);
1719 /* rewrite keys file */
1720 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1721 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1723 return AFSCONF_FAILURE;
1724 i = write(fd, &tkeys, sizeof(tkeys));
1725 if (i != sizeof(tkeys)) {
1727 return AFSCONF_FAILURE;
1730 return AFSCONF_FAILURE;
1735 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1736 afs_int32 overwrite)
1738 register struct afsconf_keys *tk;
1739 register struct afsconf_key *tkey;
1740 register afs_int32 i;
1747 if (akvno < 0 || akvno > 255) {
1748 UNLOCK_GLOBAL_MUTEX;
1753 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1754 if (tkey->kvno == akvno) {
1756 UNLOCK_GLOBAL_MUTEX;
1757 return AFSCONF_KEYINUSE;
1764 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1765 UNLOCK_GLOBAL_MUTEX;
1766 return AFSCONF_FULL;
1768 tkey = &tk->key[tk->nkeys++];
1771 memcpy(tkey->key, akey, 8);
1773 afsconf_Touch(adir);
1774 UNLOCK_GLOBAL_MUTEX;
1778 /* this proc works by sliding the other guys down, rather than using a funny
1779 kvno value, so that callers can count on getting a good key in key[0].
1782 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1784 register struct afsconf_keys *tk;
1785 register struct afsconf_key *tkey;
1792 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1793 if (tkey->kvno == akvno) {
1799 UNLOCK_GLOBAL_MUTEX;
1800 return AFSCONF_NOTFOUND;
1803 /* otherwise slide the others down. i and tkey point at the guy to delete */
1804 for (; i < tk->nkeys - 1; i++, tkey++) {
1805 tkey->kvno = (tkey + 1)->kvno;
1806 memcpy(tkey->key, (tkey + 1)->key, 8);
1810 afsconf_Touch(adir);
1811 UNLOCK_GLOBAL_MUTEX;