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 <afs/param.h>
12 #include <afs/pthread_glock.h>
14 #include "../afs/sysincludes.h"
15 #include "../afs/afsincludes.h"
17 #include <sys/types.h>
20 #include <sys/utime.h>
22 #include <WINNT/afssw.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
30 #include <arpa/nameser.h>
32 #endif /* AFS_AFSDB_ENV */
33 #endif /* AFS_NT40_ENV */
41 #include <afs/afsutil.h>
42 #include "cellconfig.h"
45 static ParseHostLine();
46 static ParseCellLine();
47 static afsconf_OpenInternal();
48 static afsconf_CloseInternal();
49 static afsconf_Reopen();
51 static struct afsconf_servPair serviceTable [] = {
62 "afsres", 7010, /* residency database for MR-AFS */
63 "afsremio", 7011, /* remote I/O interface for MR-AFS */
64 0, 0 /* insert new services before this spot */
68 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
69 * our code can tell if there is new info in the key files, the cell server db
70 * files or any of the other files (and reopen the thing) if the date on
74 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
75 static afs_int32 afsconf_FindService(aname)
76 register char *aname; {
77 /* lookup a service name */
79 register struct afsconf_servPair *tsp;
81 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
82 ts = getservbyname(aname, "");
84 ts = getservbyname(aname, (char *) 0);
87 /* we found it in /etc/services, so we use this value */
88 return ts->s_port; /* already in network byte order */
91 /* not found in /etc/services, see if it is one of ours */
92 for(tsp = serviceTable;; tsp++) {
93 if (tsp->name == (char *) 0) return -1;
94 if (!strcmp(tsp->name, aname)) return htons(tsp->port);
98 static int TrimLine(abuffer)
106 if (!isspace(tc)) break;
110 strcpy(abuffer, tbuffer);
116 * IsClientConfigDirectory() -- determine if path matches well-known
117 * client configuration directory.
119 static int IsClientConfigDirectory(const char *path)
121 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
124 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
125 int cc = tolower(cdir[i]);
126 int pc = tolower(path[i]);
139 /* hit end of one or both; allow mismatch in existence of trailing slash */
140 if (cdir[i] != '\0') {
141 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
145 if (path[i] != '\0') {
146 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
152 #endif /* AFS_NT40_ENV */
155 static int afsconf_Check(adir)
156 register struct afsconf_dir *adir; {
159 register afs_int32 code;
162 /* NT client CellServDB has different file name than NT server or Unix */
163 if (IsClientConfigDirectory(adir->name)) {
164 strcompose(tbuffer, 256,
165 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
167 strcompose(tbuffer, 256,
168 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
171 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
172 #endif /* AFS_NT40_ENV */
174 code = stat(tbuffer, &tstat);
178 /* did file change? */
179 if (tstat.st_mtime == adir->timeRead) {
182 /* otherwise file has changed, so reopen it */
183 return afsconf_Reopen(adir);
186 /* set modtime on file */
187 static afsconf_Touch(adir)
188 register struct afsconf_dir *adir; {
190 struct timeval tvp[2];
193 /* NT client CellServDB has different file name than NT server or Unix */
194 if (IsClientConfigDirectory(adir->name)) {
195 strcompose(tbuffer, 256,
196 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
198 strcompose(tbuffer, 256,
199 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
202 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
203 #endif /* AFS_NT40_ENV */
205 adir->timeRead = 0; /* just in case */
207 return _utime(tbuffer, NULL);
209 gettimeofday(&tvp[0], NULL);
211 return utimes(tbuffer, tvp);
212 #endif /* AFS_NT40_ENV */
215 struct afsconf_dir *afsconf_Open(adir)
216 register char *adir; {
217 register struct afsconf_dir *tdir;
218 register afs_int32 code;
221 /* zero structure and fill in name; rest is done by internal routine */
222 tdir = (struct afsconf_dir *) malloc(sizeof(struct afsconf_dir));
223 bzero(tdir, sizeof(struct afsconf_dir));
224 tdir->name = (char *) malloc(strlen(adir)+1);
225 strcpy(tdir->name, adir);
227 code = afsconf_OpenInternal(tdir, 0, 0);
229 char *afsconf_path, *getenv(), afs_confdir[128];
232 /* Check global place only when local Open failed for whatever reason */
233 if (!(afsconf_path = getenv("AFSCONF"))) {
234 /* 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... */
239 if (!(home_dir = getenv("HOME"))) {
240 /* Our last chance is the "/.AFSCONF" file */
241 fp = fopen("/.AFSCONF", "r");
245 return (struct afsconf_dir *) 0;
247 fgets(afs_confdir, 128, fp);
252 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
253 fp = fopen(pathname, "r");
255 /* Our last chance is the "/.AFSCONF" file */
256 fp = fopen("/.AFSCONF", "r");
260 return (struct afsconf_dir *) 0;
262 fgets(afs_confdir, 128, fp);
265 fgets(afs_confdir, 128, fp);
268 len = strlen(afs_confdir);
272 return (struct afsconf_dir *) 0;
274 if (afs_confdir[len-1] == '\n') {
275 afs_confdir[len-1] = 0;
277 afsconf_path = afs_confdir;
279 tdir->name = (char *) malloc(strlen(afsconf_path)+1);
280 strcpy(tdir->name, afsconf_path);
281 code = afsconf_OpenInternal(tdir, 0, 0);
286 return (struct afsconf_dir *) 0;
294 static int GetCellUnix(struct afsconf_dir *adir)
300 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
301 tf = fopen(tbuffer, "r");
303 rc = fscanf(tf, "%s", tbuffer);
305 adir->cellName = (char *) malloc(strlen(tbuffer)+1);
306 strcpy(adir->cellName, tbuffer);
318 static int GetCellNT(struct afsconf_dir *adir)
320 if (IsClientConfigDirectory(adir->name)) {
321 /* NT client config dir; ThisCell is in registry (no file). */
322 return afssw_GetClientCellName(&adir->cellName);
324 /* NT server config dir; works just like Unix */
325 return GetCellUnix(adir);
328 #endif /* AFS_NT40_ENV */
331 static int afsconf_OpenInternal(adir, cell, clones)
332 register struct afsconf_dir *adir;
337 register char *tp, *bp;
338 register struct afsconf_entry *curEntry;
339 register afs_int32 code;
341 char tbuffer[256], tbuf1[256];
344 /* figure out the cell name */
348 i = GetCellUnix(adir);
354 /* now parse the individual lines */
358 /* NT client/server have a CellServDB that is the same format as Unix.
359 * However, the NT client uses a different file name
361 if (IsClientConfigDirectory(adir->name)) {
362 /* NT client config dir */
363 strcompose(tbuffer, 256,
364 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
366 /* NT server config dir */
367 strcompose(tbuffer, 256,
368 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
371 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
372 #endif /* AFS_NT40_ENV */
374 if (!stat(tbuffer, &tstat)) {
375 adir->timeRead = tstat.st_mtime;
380 strcpy(tbuf1, tbuffer);
381 tf = fopen(tbuffer, "r");
386 tp = fgets(tbuffer, sizeof(tbuffer), tf);
388 TrimLine(tbuffer); /* remove white space */
389 if (tbuffer[0] == 0 || tbuffer[0] == '\n') continue; /* empty line */
390 if (tbuffer[0] == '>') {
391 char linkedcell[MAXCELLCHARS];
392 /* start new cell item */
394 /* thread this guy on the list */
395 curEntry->next = adir->entries;
396 adir->entries = curEntry;
399 curEntry = (struct afsconf_entry *) malloc(sizeof(struct afsconf_entry));
400 bzero(curEntry, sizeof(struct afsconf_entry));
401 code = ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
403 afsconf_CloseInternal(adir);
407 if (linkedcell[0] != '\0') {
408 curEntry->cellInfo.linkedCell =
409 (char *) malloc(strlen(linkedcell) + 1);
410 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
414 /* new host in the current cell */
416 afsconf_CloseInternal(adir);
420 i = curEntry->cellInfo.numServers;
421 if (cell && !strcmp(cell, curEntry->cellInfo.name))
422 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], &clones[i]);
424 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], 0);
426 if (code == AFSCONF_SYNTAX) {
427 for (bp=tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
431 fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
435 afsconf_CloseInternal(adir);
438 curEntry->cellInfo.numServers = ++i;
441 fclose(tf); /* close the file now */
443 /* end the last partially-completed cell */
445 curEntry->next = adir->entries;
446 adir->entries = curEntry;
449 /* now read the fs keys, if possible */
450 adir->keystr = (struct afsconf_keys *) 0;
451 afsconf_IntGetKeys(adir);
456 /* parse a line of the form
457 *"128.2.1.3 #hostname" or
458 *"[128.2.1.3] #hostname" for clones
459 * into the appropriate pieces.
461 static ParseHostLine(aline, addr, aname, aclone)
463 register struct sockaddr_in *addr;
467 register afs_int32 code;
471 if (aclone) *aclone = 1;
472 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
474 if (aclone) *aclone = 0;
475 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
477 if (code != 5) return AFSCONF_SYNTAX;
478 addr->sin_family = AF_INET;
480 tp = (char *) &addr->sin_addr;
488 /* parse a line of the form
489 * ">cellname [linkedcellname] [#comments]"
490 * into the appropriate pieces.
492 static ParseCellLine(aline, aname, alname)
493 register char *aline, *aname, *alname; {
495 code = sscanf(aline, ">%s %s", aname, alname);
496 if (code == 1) *alname = '\0';
498 if (*alname == '#') {
502 return (code > 0 ? 0 : AFSCONF_SYNTAX);
505 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
506 afsconf_CellApply(adir, aproc, arock)
507 struct afsconf_dir *adir;
510 register struct afsconf_entry *tde;
511 register afs_int32 code;
513 for(tde=adir->entries; tde; tde=tde->next) {
514 code = (*aproc)(&tde->cellInfo, arock, adir);
524 afs_int32 afsconf_SawCell = 0;
526 afsconf_GetExtendedCellInfo(adir, acellName, aservice, acellInfo, clones)
527 struct afsconf_dir *adir;
530 struct afsconf_cell *acellInfo;
536 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
543 cell = (char *) &acellInfo->name;
545 code = afsconf_OpenInternal(adir, cell, clones);
550 afsconf_GetAfsdbInfo(acellName, aservice, acellInfo)
553 struct afsconf_cell *acellInfo;
556 int tservice, len, i;
557 unsigned char answer[1024];
563 /* The resolver isn't always MT-safe.. Perhaps this ought to be
564 * replaced with a more fine-grained lock just for the resolver
568 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
572 return AFSCONF_NOTFOUND;
574 p = answer + sizeof(HEADER); /* Skip header */
575 code = dn_expand(answer, answer + len, p, host, sizeof(host));
577 return AFSCONF_NOTFOUND;
578 strncpy(acellInfo->name, host, sizeof(acellInfo->name));
580 p += code + QFIXEDSZ; /* Skip name */
582 while (p < answer + len) {
585 code = dn_expand(answer, answer + len, p, host, sizeof(host));
587 return AFSCONF_NOTFOUND;
589 p += code; /* Skip the name */
590 type = (p[0] << 8) | p[1];
591 p += 4; /* Skip type and class */
592 ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
593 p += 4; /* Skip the TTL */
594 size = (p[0] << 8) | p[1];
595 p += 2; /* Skip the size */
597 if (type == T_AFSDB) {
600 code = dn_expand(answer, answer+len, p+2, host, sizeof(host));
602 return AFSCONF_NOTFOUND;
604 /* Do we want to get TTL data for the A record as well? */
605 he = gethostbyname(host);
606 if (he && server_num < MAXHOSTSPERCELL) {
608 memcpy(&ipaddr, he->h_addr, he->h_length);
609 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
610 strncpy(acellInfo->hostName[server_num], host,
611 sizeof(acellInfo->hostName[server_num]));
614 if (!minttl || ttl < minttl) minttl = ttl;
621 if (server_num == 0) /* No AFSDB records */
622 return AFSCONF_NOTFOUND;
623 acellInfo->numServers = server_num;
626 tservice = afsconf_FindService(aservice);
628 return AFSCONF_NOTFOUND; /* service not found */
629 for (i=0; i<acellInfo->numServers; i++) {
630 acellInfo->hostAddr[i].sin_port = tservice;
634 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
638 #endif /* AFS_AFSDB_ENV */
640 afsconf_GetCellInfo(adir, acellName, aservice, acellInfo)
641 struct afsconf_dir *adir;
644 struct afsconf_cell *acellInfo; {
645 register struct afsconf_entry *tce;
646 struct afsconf_entry *bestce;
647 register afs_int32 i;
654 if (adir) afsconf_Check(adir);
657 cnLen = strlen(tcell)+1;
658 lcstring (tcell, tcell, cnLen);
659 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
660 /* call to afsconf_GetLocalCell: like klog */
662 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
669 cnLen = strlen(tcell);
670 bestce = (struct afsconf_entry *) 0;
676 for(tce=adir->entries;tce;tce=tce->next) {
677 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
683 if (strlen(tce->cellInfo.name) < cnLen) continue; /* clearly wrong */
684 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
685 if (bestce) ambig = 1; /* ambiguous unless we get exact match */
689 if (!ambig && bestce) {
690 *acellInfo = bestce->cellInfo; /* structure assignment */
692 tservice = afsconf_FindService(aservice);
695 return AFSCONF_NOTFOUND; /* service not found */
697 for(i=0;i<acellInfo->numServers;i++) {
698 acellInfo->hostAddr[i].sin_port = tservice;
701 acellInfo->timeout = 0;
708 return afsconf_GetAfsdbInfo(acellName, aservice, acellInfo);
710 return AFSCONF_NOTFOUND;
711 #endif /* AFS_AFSDB_ENV */
715 afsconf_GetLocalCell(adir, aname, alen)
716 register struct afsconf_dir *adir;
719 static int afsconf_showcell = 0;
726 * If a cell switch was specified in a command, then it should override the
727 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
728 * is set and the cell name in the adir structure is used.
729 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
731 if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {
732 if ( !afsconf_showcell ) {
733 fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
734 afsconf_showcell = 1;
736 strncpy(aname, afscell_path, alen);
739 if (adir->cellName) {
740 strncpy(aname, adir->cellName, alen);
742 else code = AFSCONF_UNKNOWN;
750 struct afsconf_dir *adir; {
752 afsconf_CloseInternal(adir);
753 if (adir->name) free(adir->name);
759 static int afsconf_CloseInternal(adir)
760 register struct afsconf_dir *adir; {
761 register struct afsconf_entry *td, *nd;
762 register char *tname;
764 tname = adir->name; /* remember name, since that's all we preserve */
766 /* free everything we can find */
767 if (adir->cellName) free(adir->cellName);
768 for(td=adir->entries;td;td=nd) {
770 if (td->cellInfo.linkedCell)
771 free(td->cellInfo.linkedCell);
774 if (adir->keystr) free(adir->keystr);
777 bzero(adir, sizeof(struct afsconf_dir));
778 adir->name = tname; /* restore it */
782 static int afsconf_Reopen(adir)
783 register struct afsconf_dir *adir; {
784 register afs_int32 code;
785 code = afsconf_CloseInternal(adir);
786 if (code) return code;
787 code = afsconf_OpenInternal(adir, 0, 0);
791 /* called during opening of config file */
792 afsconf_IntGetKeys(adir)
793 struct afsconf_dir *adir;
797 struct afsconf_keys *tstr;
798 register afs_int32 code;
801 /* NT client config dir has no KeyFile; don't risk attempting open
802 * because there might be a random file of this name if dir is shared.
804 if (IsClientConfigDirectory(adir->name)) {
805 adir->keystr = ((struct afsconf_keys *)
806 malloc(sizeof(struct afsconf_keys)));
807 adir->keystr->nkeys = 0;
810 #endif /* AFS_NT40_ENV */
813 /* compute the key name and other setup */
815 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
816 tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
820 fd = open(tbuffer, O_RDONLY);
826 code = read(fd, tstr, sizeof(struct afsconf_keys));
828 if (code < sizeof(afs_int32)) {
834 /* convert key structure to host order */
835 tstr->nkeys = ntohl(tstr->nkeys);
836 for(fd=0;fd<tstr->nkeys;fd++)
837 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
843 /* get keys structure */
844 afsconf_GetKeys(adir, astr)
845 struct afsconf_dir *adir;
846 struct afsconf_keys *astr;
850 bcopy(adir->keystr, astr, sizeof(struct afsconf_keys));
856 afs_int32 afsconf_GetLatestKey(adir, avno, akey)
857 IN struct afsconf_dir *adir;
863 register struct afsconf_key *tk;
864 register afs_int32 best;
865 struct afsconf_key *bestk;
869 maxa = adir->keystr->nkeys;
871 best = -1; /* highest kvno we've seen yet */
872 bestk = (struct afsconf_key *) 0; /* ptr to structure providing best */
873 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
874 if (tk->kvno == 999) continue; /* skip bcrypt keys */
875 if (tk->kvno > best) {
880 if (bestk) { /* found any */
881 if (akey) bcopy(bestk->key, akey, 8); /* copy out latest key */
882 if (avno) *avno = bestk->kvno; /* and kvno to caller */
887 return AFSCONF_NOTFOUND; /* didn't find any keys */
890 /* get a particular key */
891 afsconf_GetKey(adir, avno, akey)
892 struct afsconf_dir *adir;
896 register int i, maxa;
897 register struct afsconf_key *tk;
901 maxa = adir->keystr->nkeys;
903 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
904 if (tk->kvno == avno) {
905 bcopy(tk->key, akey, 8);
912 return AFSCONF_NOTFOUND;
915 /* save the key structure in the appropriate file */
916 static SaveKeys(adir)
917 struct afsconf_dir *adir;
919 struct afsconf_keys tkeys;
921 register afs_int32 i;
924 bcopy(adir->keystr, &tkeys, sizeof(struct afsconf_keys));
926 /* convert it to net byte order */
927 for(i = 0; i<tkeys.nkeys; i++ )
928 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
929 tkeys.nkeys = htonl(tkeys.nkeys);
931 /* rewrite keys file */
932 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
933 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
934 if (fd < 0) return AFSCONF_FAILURE;
935 i = write(fd, &tkeys, sizeof(tkeys));
936 if (i != sizeof(tkeys)) {
938 return AFSCONF_FAILURE;
940 if (close(fd) < 0) return AFSCONF_FAILURE;
944 afsconf_AddKey(adir, akvno, akey, overwrite)
945 struct afsconf_dir *adir;
946 afs_int32 akvno, overwrite;
949 register struct afsconf_keys *tk;
950 register struct afsconf_key *tkey;
951 register afs_int32 i;
958 if (akvno < 0 || akvno > 255) {
964 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
965 if (tkey->kvno == akvno) {
968 return AFSCONF_KEYINUSE;
975 if (tk->nkeys >= AFSCONF_MAXKEYS) {
979 tkey = &tk->key[tk->nkeys++];
982 bcopy(akey, tkey->key, 8);
989 /* this proc works by sliding the other guys down, rather than using a funny
990 kvno value, so that callers can count on getting a good key in key[0].
992 afsconf_DeleteKey(adir, akvno)
993 struct afsconf_dir *adir;
996 register struct afsconf_keys *tk;
997 register struct afsconf_key *tkey;
1004 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1005 if (tkey->kvno == akvno) {
1012 return AFSCONF_NOTFOUND;
1015 /* otherwise slide the others down. i and tkey point at the guy to delete */
1016 for(;i<tk->nkeys-1; i++,tkey++) {
1017 tkey->kvno = (tkey+1)->kvno;
1018 bcopy((tkey+1)->key, tkey->key, 8);
1022 afsconf_Touch(adir);