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>
37 #include <afs/afsutil.h>
38 #include "cellconfig.h"
41 static ParseHostLine();
42 static ParseCellLine();
43 static afsconf_OpenInternal();
44 static afsconf_CloseInternal();
45 static afsconf_Reopen();
47 static struct afsconf_servPair serviceTable [] = {
58 "afsres", 7010, /* residency database for MR-AFS */
59 "afsremio", 7011, /* remote I/O interface for MR-AFS */
60 0, 0 /* insert new services before this spot */
64 * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
65 * our code can tell if there is new info in the key files, the cell server db
66 * files or any of the other files (and reopen the thing) if the date on
70 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
71 static afs_int32 afsconf_FindService(aname)
72 register char *aname; {
73 /* lookup a service name */
75 register struct afsconf_servPair *tsp;
76 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
77 ts = getservbyname(aname, "");
79 ts = getservbyname(aname, (char *) 0);
82 /* we found it in /etc/services, so we use this value */
83 return ts->s_port; /* already in network byte order */
85 /* not found in /etc/services, see if it is one of ours */
86 for(tsp = serviceTable;; tsp++) {
87 if (tsp->name == (char *) 0) return -1;
88 if (!strcmp(tsp->name, aname)) return htons(tsp->port);
92 static int TrimLine(abuffer)
100 if (!isspace(tc)) break;
104 strcpy(abuffer, tbuffer);
110 * IsClientConfigDirectory() -- determine if path matches well-known
111 * client configuration directory.
113 static int IsClientConfigDirectory(const char *path)
115 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
118 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
119 int cc = tolower(cdir[i]);
120 int pc = tolower(path[i]);
133 /* hit end of one or both; allow mismatch in existence of trailing slash */
134 if (cdir[i] != '\0') {
135 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
139 if (path[i] != '\0') {
140 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
146 #endif /* AFS_NT40_ENV */
149 static int afsconf_Check(adir)
150 register struct afsconf_dir *adir; {
153 register afs_int32 code;
156 /* NT client CellServDB has different file name than NT server or Unix */
157 if (IsClientConfigDirectory(adir->name)) {
158 strcompose(tbuffer, 256,
159 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
161 strcompose(tbuffer, 256,
162 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
165 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
166 #endif /* AFS_NT40_ENV */
168 code = stat(tbuffer, &tstat);
172 /* did file change? */
173 if (tstat.st_mtime == adir->timeRead) {
176 /* otherwise file has changed, so reopen it */
177 return afsconf_Reopen(adir);
180 /* set modtime on file */
181 static afsconf_Touch(adir)
182 register struct afsconf_dir *adir; {
184 struct timeval tvp[2];
187 /* NT client CellServDB has different file name than NT server or Unix */
188 if (IsClientConfigDirectory(adir->name)) {
189 strcompose(tbuffer, 256,
190 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
192 strcompose(tbuffer, 256,
193 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
196 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
197 #endif /* AFS_NT40_ENV */
199 adir->timeRead = 0; /* just in case */
201 return _utime(tbuffer, NULL);
203 gettimeofday(&tvp[0], NULL);
205 return utimes(tbuffer, tvp);
206 #endif /* AFS_NT40_ENV */
209 struct afsconf_dir *afsconf_Open(adir)
210 register char *adir; {
211 register struct afsconf_dir *tdir;
212 register afs_int32 code;
215 /* zero structure and fill in name; rest is done by internal routine */
216 tdir = (struct afsconf_dir *) malloc(sizeof(struct afsconf_dir));
217 bzero(tdir, sizeof(struct afsconf_dir));
218 tdir->name = (char *) malloc(strlen(adir)+1);
219 strcpy(tdir->name, adir);
221 code = afsconf_OpenInternal(tdir);
223 char *afsconf_path, *getenv(), afs_confdir[128];
226 /* Check global place only when local Open failed for whatever reason */
227 if (!(afsconf_path = getenv("AFSCONF"))) {
228 /* 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... */
233 if (!(home_dir = getenv("HOME"))) {
234 /* Our last chance is the "/.AFSCONF" file */
235 fp = fopen("/.AFSCONF", "r");
239 return (struct afsconf_dir *) 0;
241 fgets(afs_confdir, 128, fp);
246 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
247 fp = fopen(pathname, "r");
249 /* Our last chance is the "/.AFSCONF" file */
250 fp = fopen("/.AFSCONF", "r");
254 return (struct afsconf_dir *) 0;
256 fgets(afs_confdir, 128, fp);
259 fgets(afs_confdir, 128, fp);
262 len = strlen(afs_confdir);
266 return (struct afsconf_dir *) 0;
268 if (afs_confdir[len-1] == '\n') {
269 afs_confdir[len-1] = 0;
271 afsconf_path = afs_confdir;
273 tdir->name = (char *) malloc(strlen(afsconf_path)+1);
274 strcpy(tdir->name, afsconf_path);
275 code = afsconf_OpenInternal(tdir);
280 return (struct afsconf_dir *) 0;
288 static int GetCellUnix(struct afsconf_dir *adir)
294 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
295 tf = fopen(tbuffer, "r");
297 rc = fscanf(tf, "%s", tbuffer);
299 adir->cellName = (char *) malloc(strlen(tbuffer)+1);
300 strcpy(adir->cellName, tbuffer);
312 static int GetCellNT(struct afsconf_dir *adir)
314 if (IsClientConfigDirectory(adir->name)) {
315 /* NT client config dir; ThisCell is in registry (no file). */
316 return afssw_GetClientCellName(&adir->cellName);
318 /* NT server config dir; works just like Unix */
319 return GetCellUnix(adir);
322 #endif /* AFS_NT40_ENV */
325 static int afsconf_OpenInternal(adir)
326 register struct afsconf_dir *adir; {
328 register char *tp, *bp;
329 register struct afsconf_entry *curEntry;
330 register afs_int32 code;
332 char tbuffer[256], tbuf1[256];
335 /* figure out the cell name */
339 i = GetCellUnix(adir);
345 /* now parse the individual lines */
349 /* NT client/server have a CellServDB that is the same format as Unix.
350 * However, the NT client uses a different file name
352 if (IsClientConfigDirectory(adir->name)) {
353 /* NT client config dir */
354 strcompose(tbuffer, 256,
355 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
357 /* NT server config dir */
358 strcompose(tbuffer, 256,
359 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
362 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
363 #endif /* AFS_NT40_ENV */
365 if (!stat(tbuffer, &tstat)) {
366 adir->timeRead = tstat.st_mtime;
371 strcpy(tbuf1, tbuffer);
372 tf = fopen(tbuffer, "r");
377 tp = fgets(tbuffer, sizeof(tbuffer), tf);
379 TrimLine(tbuffer); /* remove white space */
380 if (tbuffer[0] == 0 || tbuffer[0] == '\n') continue; /* empty line */
381 if (tbuffer[0] == '>') {
382 char linkedcell[MAXCELLCHARS];
383 /* start new cell item */
385 /* thread this guy on the list */
386 curEntry->next = adir->entries;
387 adir->entries = curEntry;
390 curEntry = (struct afsconf_entry *) malloc(sizeof(struct afsconf_entry));
391 bzero(curEntry, sizeof(struct afsconf_entry));
392 code = ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
394 afsconf_CloseInternal(adir);
398 if (linkedcell[0] != '\0') {
399 curEntry->cellInfo.linkedCell =
400 (char *) malloc(strlen(linkedcell) + 1);
401 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
405 /* new host in the current cell */
407 afsconf_CloseInternal(adir);
411 i = curEntry->cellInfo.numServers;
412 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i]);
414 if (code == AFSCONF_SYNTAX) {
415 for (bp=tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
419 fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
423 afsconf_CloseInternal(adir);
426 curEntry->cellInfo.numServers = ++i;
429 fclose(tf); /* close the file now */
431 /* end the last partially-completed cell */
433 curEntry->next = adir->entries;
434 adir->entries = curEntry;
437 /* now read the fs keys, if possible */
438 adir->keystr = (struct afsconf_keys *) 0;
439 afsconf_IntGetKeys(adir);
444 /* parse a line of the form
445 *"128.2.1.3 #hostname"
446 * into the appropriate pieces.
448 static ParseHostLine(aline, addr, aname)
449 register struct sockaddr_in *addr;
450 char *aline, *aname; {
452 register afs_int32 code;
455 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
456 if (code != 5) return AFSCONF_SYNTAX;
457 addr->sin_family = AF_INET;
459 tp = (char *) &addr->sin_addr;
467 /* parse a line of the form
468 * ">cellname [linkedcellname] [#comments]"
469 * into the appropriate pieces.
471 static ParseCellLine(aline, aname, alname)
472 register char *aline, *aname, *alname; {
474 code = sscanf(aline, ">%s %s", aname, alname);
475 if (code == 1) *alname = '\0';
477 if (*alname == '#') {
481 return (code > 0 ? 0 : AFSCONF_SYNTAX);
484 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
485 afsconf_CellApply(adir, aproc, arock)
486 struct afsconf_dir *adir;
489 register struct afsconf_entry *tde;
490 register afs_int32 code;
492 for(tde=adir->entries; tde; tde=tde->next) {
493 code = (*aproc)(&tde->cellInfo, arock, adir);
503 afs_int32 afsconf_SawCell = 0;
504 afsconf_GetCellInfo(adir, acellName, aservice, acellInfo)
505 struct afsconf_dir *adir;
508 struct afsconf_cell *acellInfo; {
509 register struct afsconf_entry *tce;
510 struct afsconf_entry *bestce;
511 register afs_int32 i;
518 if (adir) afsconf_Check(adir);
521 cnLen = strlen(tcell)+1;
522 lcstring (tcell, tcell, cnLen);
523 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
524 /* call to afsconf_GetLocalCell: like klog */
526 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
533 cnLen = strlen(tcell);
534 bestce = (struct afsconf_entry *) 0;
540 for(tce=adir->entries;tce;tce=tce->next) {
541 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
547 if (strlen(tce->cellInfo.name) < cnLen) continue; /* clearly wrong */
548 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
549 if (bestce) ambig = 1; /* ambiguous unless we get exact match */
553 if (!ambig && bestce) {
554 *acellInfo = bestce->cellInfo; /* structure assignment */
556 tservice = afsconf_FindService(aservice);
559 return AFSCONF_NOTFOUND; /* service not found */
561 for(i=0;i<acellInfo->numServers;i++) {
562 acellInfo->hostAddr[i].sin_port = tservice;
570 return AFSCONF_NOTFOUND;
574 afsconf_GetLocalCell(adir, aname, alen)
575 register struct afsconf_dir *adir;
578 static int afsconf_showcell = 0;
585 * If a cell switch was specified in a command, then it should override the
586 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
587 * is set and the cell name in the adir structure is used.
588 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
590 if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {
591 if ( !afsconf_showcell ) {
592 fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
593 afsconf_showcell = 1;
595 strncpy(aname, afscell_path, alen);
598 if (adir->cellName) {
599 strncpy(aname, adir->cellName, alen);
601 else code = AFSCONF_UNKNOWN;
609 struct afsconf_dir *adir; {
611 afsconf_CloseInternal(adir);
612 if (adir->name) free(adir->name);
618 static int afsconf_CloseInternal(adir)
619 register struct afsconf_dir *adir; {
620 register struct afsconf_entry *td, *nd;
621 register char *tname;
623 tname = adir->name; /* remember name, since that's all we preserve */
625 /* free everything we can find */
626 if (adir->cellName) free(adir->cellName);
627 for(td=adir->entries;td;td=nd) {
629 if (td->cellInfo.linkedCell)
630 free(td->cellInfo.linkedCell);
633 if (adir->keystr) free(adir->keystr);
636 bzero(adir, sizeof(struct afsconf_dir));
637 adir->name = tname; /* restore it */
641 static int afsconf_Reopen(adir)
642 register struct afsconf_dir *adir; {
643 register afs_int32 code;
644 code = afsconf_CloseInternal(adir);
645 if (code) return code;
646 code = afsconf_OpenInternal(adir);
650 /* called during opening of config file */
651 afsconf_IntGetKeys(adir)
652 struct afsconf_dir *adir;
656 struct afsconf_keys *tstr;
657 register afs_int32 code;
660 /* NT client config dir has no KeyFile; don't risk attempting open
661 * because there might be a random file of this name if dir is shared.
663 if (IsClientConfigDirectory(adir->name)) {
664 adir->keystr = ((struct afsconf_keys *)
665 malloc(sizeof(struct afsconf_keys)));
666 adir->keystr->nkeys = 0;
669 #endif /* AFS_NT40_ENV */
672 /* compute the key name and other setup */
674 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
675 tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
679 fd = open(tbuffer, O_RDONLY);
685 code = read(fd, tstr, sizeof(struct afsconf_keys));
687 if (code < sizeof(afs_int32)) {
693 /* convert key structure to host order */
694 tstr->nkeys = ntohl(tstr->nkeys);
695 for(fd=0;fd<tstr->nkeys;fd++)
696 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
702 /* get keys structure */
703 afsconf_GetKeys(adir, astr)
704 struct afsconf_dir *adir;
705 struct afsconf_keys *astr;
709 bcopy(adir->keystr, astr, sizeof(struct afsconf_keys));
715 afs_int32 afsconf_GetLatestKey(adir, avno, akey)
716 IN struct afsconf_dir *adir;
722 register struct afsconf_key *tk;
723 register afs_int32 best;
724 struct afsconf_key *bestk;
728 maxa = adir->keystr->nkeys;
730 best = -1; /* highest kvno we've seen yet */
731 bestk = (struct afsconf_key *) 0; /* ptr to structure providing best */
732 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
733 if (tk->kvno == 999) continue; /* skip bcrypt keys */
734 if (tk->kvno > best) {
739 if (bestk) { /* found any */
740 if (akey) bcopy(bestk->key, akey, 8); /* copy out latest key */
741 if (avno) *avno = bestk->kvno; /* and kvno to caller */
746 return AFSCONF_NOTFOUND; /* didn't find any keys */
749 /* get a particular key */
750 afsconf_GetKey(adir, avno, akey)
751 struct afsconf_dir *adir;
755 register int i, maxa;
756 register struct afsconf_key *tk;
760 maxa = adir->keystr->nkeys;
762 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
763 if (tk->kvno == avno) {
764 bcopy(tk->key, akey, 8);
771 return AFSCONF_NOTFOUND;
774 /* save the key structure in the appropriate file */
775 static SaveKeys(adir)
776 struct afsconf_dir *adir;
778 struct afsconf_keys tkeys;
780 register afs_int32 i;
783 bcopy(adir->keystr, &tkeys, sizeof(struct afsconf_keys));
785 /* convert it to net byte order */
786 for(i = 0; i<tkeys.nkeys; i++ )
787 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
788 tkeys.nkeys = htonl(tkeys.nkeys);
790 /* rewrite keys file */
791 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
792 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
793 if (fd < 0) return AFSCONF_FAILURE;
794 i = write(fd, &tkeys, sizeof(tkeys));
795 if (i != sizeof(tkeys)) {
797 return AFSCONF_FAILURE;
799 if (close(fd) < 0) return AFSCONF_FAILURE;
803 afsconf_AddKey(adir, akvno, akey, overwrite)
804 struct afsconf_dir *adir;
805 afs_int32 akvno, overwrite;
808 register struct afsconf_keys *tk;
809 register struct afsconf_key *tkey;
810 register afs_int32 i;
817 if (akvno < 0 || akvno > 255) {
823 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
824 if (tkey->kvno == akvno) {
827 return AFSCONF_KEYINUSE;
834 if (tk->nkeys >= AFSCONF_MAXKEYS) {
838 tkey = &tk->key[tk->nkeys++];
841 bcopy(akey, tkey->key, 8);
848 /* this proc works by sliding the other guys down, rather than using a funny
849 kvno value, so that callers can count on getting a good key in key[0].
851 afsconf_DeleteKey(adir, akvno)
852 struct afsconf_dir *adir;
855 register struct afsconf_keys *tk;
856 register struct afsconf_key *tkey;
863 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
864 if (tkey->kvno == akvno) {
871 return AFSCONF_NOTFOUND;
874 /* otherwise slide the others down. i and tkey point at the guy to delete */
875 for(;i<tk->nkeys-1; i++,tkey++) {
876 tkey->kvno = (tkey+1)->kvno;
877 bcopy((tkey+1)->key, tkey->key, 8);