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) {
1151 code = dn_expand(answer, answer + len, p + 6, host, sizeof(host));
1153 code = AFSCONF_NOTFOUND;
1154 goto findservererror;
1157 if ((server_num < MAXHOSTSPERCELL) &&
1158 /* Do we want to get TTL data for the A record as well? */
1159 (he = gethostbyname(host))) {
1161 memcpy(&ipaddr, he->h_addr, he->h_length);
1162 cellHostAddrs[server_num] = ipaddr;
1163 ipRanks[server_num] = (p[0] << 8) | p[1];
1164 ports[server_num] = (p[4] << 8) | p[5];
1165 /* weight = (p[2] << 8) | p[3]; */
1166 strncpy(cellHostNames[server_num], host,
1167 sizeof(cellHostNames[server_num]));
1170 if (!minttl || ttl < minttl)
1178 if (server_num == 0) { /* No AFSDB or SRV records */
1179 code = AFSCONF_NOTFOUND;
1180 goto findservererror;
1184 /* Convert the real cell name to lowercase */
1185 for (p = (unsigned char *)realCellName; *p; p++)
1189 *arealCellName = realCellName;
1191 *numServers = server_num;
1192 *ttl = minttl ? (time(0) + minttl) : 0;
1194 if ( *numServers > 0 )
1197 code = AFSCONF_NOTFOUND;
1205 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1206 struct afsconf_cell *acellInfo)
1208 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1209 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1210 unsigned short ipRanks[AFSMAXCELLHOSTS];
1211 unsigned short ports[AFSMAXCELLHOSTS];
1212 char *realCellName = NULL;
1213 int ttl, numServers, i;
1214 char *service = aservice;
1216 unsigned short afsdbport;
1218 service = "afs3-vlserver";
1219 afsdbport = htons(7003);
1222 afsdbport = afsconf_FindService(service);
1224 code = afsconf_LookupServer((const char *)service, "udp",
1225 (const char *)acellName, afsdbport,
1226 cellHostAddrs, cellHostNames,
1227 ports, ipRanks, &numServers, &ttl,
1231 acellInfo->timeout = ttl;
1232 acellInfo->numServers = numServers;
1233 for (i = 0; i < numServers; i++) {
1234 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1236 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1237 acellInfo->hostAddr[i].sin_family = AF_INET;
1238 acellInfo->hostAddr[i].sin_port = ports[i];
1241 strlcpy(acellInfo->name, realCellName,
1242 sizeof(acellInfo->name));
1244 acellInfo->linkedCell = NULL; /* no linked cell */
1245 acellInfo->flags = 0;
1251 afsconf_GetAfsdbInfo(char *acellName, char *aservice,
1252 struct afsconf_cell *acellInfo)
1255 int tservice = afsconf_FindService(aservice); /* network byte order */
1256 const char *ianaName = afsconf_FindIANAName(aservice);
1257 struct afsconf_entry DNSce;
1258 afs_int32 cellHostAddrs[AFSMAXCELLHOSTS];
1259 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
1260 unsigned short ipRanks[AFSMAXCELLHOSTS];
1261 unsigned short ports[AFSMAXCELLHOSTS]; /* network byte order */
1268 return AFSCONF_NOTFOUND;
1270 tservice = 0; /* port will be assigned by caller */
1273 if (ianaName == NULL)
1274 ianaName = "afs3-vlserver";
1276 DNSce.cellInfo.numServers = 0;
1279 rc = getAFSServer(ianaName, "udp", acellName, tservice,
1280 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1282 /* ignore the ttl here since this code is only called by transitory programs
1283 * like klog, etc. */
1285 /* If we couldn't find an entry for the requested service
1286 * and that service happens to be the prservice or kaservice
1287 * then fallback to searching for afs3-vlserver and assigning
1288 * the port number here. */
1289 if (rc < 0 && tservice == htons(7002) || tservice == htons(7004)) {
1290 rc = getAFSServer("afs3-vlserver", "udp", acellName, tservice,
1291 cellHostAddrs, cellHostNames, ports, ipRanks, &numServers,
1294 for (i = 0; i < numServers; i++)
1295 ports[i] = tservice;
1299 if (rc < 0 || numServers == 0)
1302 for (i = 0; i < numServers; i++) {
1303 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHostAddrs[i],
1305 memcpy(acellInfo->hostName[i], cellHostNames[i], MAXHOSTCHARS);
1306 acellInfo->hostAddr[i].sin_family = AF_INET;
1308 acellInfo->hostAddr[i].sin_port = ports[i];
1310 acellInfo->hostAddr[i].sin_port = 0;
1313 acellInfo->numServers = numServers;
1314 strlcpy(acellInfo->name, acellName, sizeof acellInfo->name);
1315 acellInfo->linkedCell = NULL; /* no linked cell */
1316 acellInfo->flags = 0;
1319 #endif /* windows */
1322 afsconf_GetCellInfo(struct afsconf_dir *adir, char *acellName, char *aservice,
1323 struct afsconf_cell *acellInfo)
1325 register struct afsconf_entry *tce;
1326 struct afsconf_aliasentry *tcae;
1327 struct afsconf_entry *bestce;
1328 register afs_int32 i;
1337 afsconf_Check(adir);
1340 cnLen = (int)(strlen(tcell) + 1);
1341 lcstring(tcell, tcell, cnLen);
1342 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
1343 /* call to afsconf_GetLocalCell: like klog */
1345 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
1347 UNLOCK_GLOBAL_MUTEX;
1352 cnLen = strlen(tcell);
1353 bestce = (struct afsconf_entry *)0;
1356 UNLOCK_GLOBAL_MUTEX;
1360 /* Look through the list of aliases */
1361 for (tcae = adir->alias_entries; tcae; tcae = tcae->next) {
1362 if (strcasecmp(tcae->aliasInfo.aliasName, tcell) == 0) {
1363 tcell = tcae->aliasInfo.realName;
1368 for (tce = adir->entries; tce; tce = tce->next) {
1369 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
1370 /* found our cell */
1375 if (strlen(tce->cellInfo.name) < cnLen)
1376 continue; /* clearly wrong */
1377 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
1379 ambig = 1; /* ambiguous unless we get exact match */
1383 if (!ambig && bestce && bestce->cellInfo.numServers) {
1384 *acellInfo = bestce->cellInfo; /* structure assignment */
1386 tservice = afsconf_FindService(aservice);
1388 UNLOCK_GLOBAL_MUTEX;
1389 return AFSCONF_NOTFOUND; /* service not found */
1391 for (i = 0; i < acellInfo->numServers; i++) {
1392 acellInfo->hostAddr[i].sin_port = tservice;
1395 acellInfo->timeout = 0;
1398 * Until we figure out how to separate out ubik server
1399 * queries from other server queries, only perform gethostbyname()
1400 * lookup on the specified hostnames for the client CellServDB files.
1402 if (IsClientConfigDirectory(adir->name) &&
1403 !(acellInfo->flags & AFSCONF_CELL_FLAG_DNS_QUERIED)) {
1405 short numServers=0; /*Num active servers for the cell */
1406 struct sockaddr_in hostAddr[MAXHOSTSPERCELL]; /*IP addresses for cell's servers */
1407 char hostName[MAXHOSTSPERCELL][MAXHOSTCHARS]; /*Names for cell's servers */
1409 memset(&hostAddr, 0, sizeof(hostAddr));
1410 memset(&hostName, 0, sizeof(hostName));
1412 for ( j=0; j<acellInfo->numServers && numServers < MAXHOSTSPERCELL; j++ ) {
1413 struct hostent *he = gethostbyname(acellInfo->hostName[j]);
1416 if (he && he->h_addrtype == AF_INET) {
1418 /* obtain all the valid address from the list */
1419 for (i=0 ; he->h_addr_list[i] && numServers < MAXHOSTSPERCELL; i++) {
1420 /* check to see if this is a new address; if so insert it into the list */
1422 for (k=0, dup=0; !dup && k < numServers; k++) {
1423 if (hostAddr[k].sin_addr.s_addr == *(u_long *)he->h_addr_list[i])
1429 hostAddr[numServers].sin_family = AF_INET;
1430 hostAddr[numServers].sin_port = acellInfo->hostAddr[0].sin_port;
1431 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
1432 hostAddr[numServers].sin_len = sizeof(struct sockaddr_in);
1434 memcpy(&hostAddr[numServers].sin_addr.s_addr, he->h_addr_list[i], sizeof(long));
1435 strcpy(hostName[numServers], acellInfo->hostName[j]);
1441 hostAddr[numServers] = acellInfo->hostAddr[j];
1442 strcpy(hostName[numServers], acellInfo->hostName[j]);
1447 for (i=0; i<numServers; i++) {
1448 acellInfo->hostAddr[i] = hostAddr[i];
1449 strcpy(acellInfo->hostName[i], hostName[i]);
1451 acellInfo->numServers = numServers;
1452 acellInfo->flags |= AFSCONF_CELL_FLAG_DNS_QUERIED;
1454 UNLOCK_GLOBAL_MUTEX;
1457 UNLOCK_GLOBAL_MUTEX;
1458 return afsconf_GetAfsdbInfo(tcell, aservice, acellInfo);
1463 afsconf_GetLocalCell(register struct afsconf_dir *adir, char *aname,
1466 static int afsconf_showcell = 0;
1472 * If a cell switch was specified in a command, then it should override the
1473 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
1474 * is set and the cell name in the adir structure is used.
1475 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
1477 if (!afsconf_SawCell && (afscell_path = getenv("AFSCELL"))) {
1478 if (!afsconf_showcell) {
1479 fprintf(stderr, "Note: Operation is performed on cell %s\n",
1481 afsconf_showcell = 1;
1483 strncpy(aname, afscell_path, alen);
1485 afsconf_Check(adir);
1486 if (adir->cellName) {
1487 strncpy(aname, adir->cellName, alen);
1489 code = AFSCONF_UNKNOWN;
1492 UNLOCK_GLOBAL_MUTEX;
1497 afsconf_Close(struct afsconf_dir *adir)
1500 afsconf_CloseInternal(adir);
1504 UNLOCK_GLOBAL_MUTEX;
1509 afsconf_CloseInternal(register struct afsconf_dir *adir)
1511 register struct afsconf_entry *td, *nd;
1512 struct afsconf_aliasentry *ta, *na;
1513 register char *tname;
1515 tname = adir->name; /* remember name, since that's all we preserve */
1517 /* free everything we can find */
1519 free(adir->cellName);
1520 for (td = adir->entries; td; td = nd) {
1522 if (td->cellInfo.linkedCell)
1523 free(td->cellInfo.linkedCell);
1526 for (ta = adir->alias_entries; ta; ta = na) {
1534 memset(adir, 0, sizeof(struct afsconf_dir));
1535 adir->name = tname; /* restore it */
1540 afsconf_Reopen(register struct afsconf_dir *adir)
1542 register afs_int32 code;
1543 code = afsconf_CloseInternal(adir);
1546 code = afsconf_OpenInternal(adir, 0, 0);
1550 /* called during opening of config file */
1552 afsconf_IntGetKeys(struct afsconf_dir *adir)
1556 struct afsconf_keys *tstr;
1557 register afs_int32 code;
1560 /* NT client config dir has no KeyFile; don't risk attempting open
1561 * because there might be a random file of this name if dir is shared.
1563 if (IsClientConfigDirectory(adir->name)) {
1564 adir->keystr = ((struct afsconf_keys *)
1565 malloc(sizeof(struct afsconf_keys)));
1566 adir->keystr->nkeys = 0;
1569 #endif /* AFS_NT40_ENV */
1572 /* compute the key name and other setup */
1573 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1574 tstr = (struct afsconf_keys *)malloc(sizeof(struct afsconf_keys));
1575 adir->keystr = tstr;
1578 fd = open(tbuffer, O_RDONLY);
1581 UNLOCK_GLOBAL_MUTEX;
1584 code = read(fd, tstr, sizeof(struct afsconf_keys));
1586 if (code < sizeof(afs_int32)) {
1588 UNLOCK_GLOBAL_MUTEX;
1592 /* convert key structure to host order */
1593 tstr->nkeys = ntohl(tstr->nkeys);
1595 if (code < sizeof(afs_int32) + (tstr->nkeys*sizeof(struct afsconf_key))) {
1597 UNLOCK_GLOBAL_MUTEX;
1601 for (fd = 0; fd < tstr->nkeys; fd++)
1602 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
1604 UNLOCK_GLOBAL_MUTEX;
1608 /* get keys structure */
1610 afsconf_GetKeys(struct afsconf_dir *adir, struct afsconf_keys *astr)
1612 register afs_int32 code;
1615 code = afsconf_Check(adir);
1617 UNLOCK_GLOBAL_MUTEX;
1618 return AFSCONF_FAILURE;
1620 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
1621 UNLOCK_GLOBAL_MUTEX;
1625 /* get latest key */
1627 afsconf_GetLatestKey(struct afsconf_dir * adir, afs_int32 * avno,
1628 struct ktc_encryptionKey *akey)
1632 register struct afsconf_key *tk;
1633 register afs_int32 best;
1634 struct afsconf_key *bestk;
1635 register afs_int32 code;
1638 code = afsconf_Check(adir);
1640 UNLOCK_GLOBAL_MUTEX;
1641 return AFSCONF_FAILURE;
1643 maxa = adir->keystr->nkeys;
1645 best = -1; /* highest kvno we've seen yet */
1646 bestk = (struct afsconf_key *)0; /* ptr to structure providing best */
1647 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1648 if (tk->kvno == 999)
1649 continue; /* skip bcrypt keys */
1650 if (tk->kvno > best) {
1655 if (bestk) { /* found any */
1657 memcpy(akey, bestk->key, 8); /* copy out latest key */
1659 *avno = bestk->kvno; /* and kvno to caller */
1660 UNLOCK_GLOBAL_MUTEX;
1663 UNLOCK_GLOBAL_MUTEX;
1664 return AFSCONF_NOTFOUND; /* didn't find any keys */
1667 /* get a particular key */
1669 afsconf_GetKey(void *rock, int avno, struct ktc_encryptionKey *akey)
1671 struct afsconf_dir *adir = (struct afsconf_dir *) rock;
1672 register int i, maxa;
1673 register struct afsconf_key *tk;
1674 register afs_int32 code;
1677 code = afsconf_Check(adir);
1679 UNLOCK_GLOBAL_MUTEX;
1680 return AFSCONF_FAILURE;
1682 maxa = adir->keystr->nkeys;
1684 for (tk = adir->keystr->key, i = 0; i < maxa; i++, tk++) {
1685 if (tk->kvno == avno) {
1686 memcpy(akey, tk->key, 8);
1687 UNLOCK_GLOBAL_MUTEX;
1692 UNLOCK_GLOBAL_MUTEX;
1693 return AFSCONF_NOTFOUND;
1696 /* save the key structure in the appropriate file */
1698 SaveKeys(struct afsconf_dir *adir)
1700 struct afsconf_keys tkeys;
1702 register afs_int32 i;
1705 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1707 /* convert it to net byte order */
1708 for (i = 0; i < tkeys.nkeys; i++)
1709 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1710 tkeys.nkeys = htonl(tkeys.nkeys);
1712 /* rewrite keys file */
1713 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1714 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1716 return AFSCONF_FAILURE;
1717 i = write(fd, &tkeys, sizeof(tkeys));
1718 if (i != sizeof(tkeys)) {
1720 return AFSCONF_FAILURE;
1723 return AFSCONF_FAILURE;
1728 afsconf_AddKey(struct afsconf_dir *adir, afs_int32 akvno, char akey[8],
1729 afs_int32 overwrite)
1731 register struct afsconf_keys *tk;
1732 register struct afsconf_key *tkey;
1733 register afs_int32 i;
1740 if (akvno < 0 || akvno > 255) {
1741 UNLOCK_GLOBAL_MUTEX;
1746 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1747 if (tkey->kvno == akvno) {
1749 UNLOCK_GLOBAL_MUTEX;
1750 return AFSCONF_KEYINUSE;
1757 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1758 UNLOCK_GLOBAL_MUTEX;
1759 return AFSCONF_FULL;
1761 tkey = &tk->key[tk->nkeys++];
1764 memcpy(tkey->key, akey, 8);
1766 afsconf_Touch(adir);
1767 UNLOCK_GLOBAL_MUTEX;
1771 /* this proc works by sliding the other guys down, rather than using a funny
1772 kvno value, so that callers can count on getting a good key in key[0].
1775 afsconf_DeleteKey(struct afsconf_dir *adir, afs_int32 akvno)
1777 register struct afsconf_keys *tk;
1778 register struct afsconf_key *tkey;
1785 for (i = 0, tkey = tk->key; i < tk->nkeys; i++, tkey++) {
1786 if (tkey->kvno == akvno) {
1792 UNLOCK_GLOBAL_MUTEX;
1793 return AFSCONF_NOTFOUND;
1796 /* otherwise slide the others down. i and tkey point at the guy to delete */
1797 for (; i < tk->nkeys - 1; i++, tkey++) {
1798 tkey->kvno = (tkey + 1)->kvno;
1799 memcpy(tkey->key, (tkey + 1)->key, 8);
1803 afsconf_Touch(adir);
1804 UNLOCK_GLOBAL_MUTEX;