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;
77 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
78 ts = getservbyname(aname, "");
80 ts = getservbyname(aname, (char *) 0);
83 /* we found it in /etc/services, so we use this value */
84 return ts->s_port; /* already in network byte order */
87 /* not found in /etc/services, see if it is one of ours */
88 for(tsp = serviceTable;; tsp++) {
89 if (tsp->name == (char *) 0) return -1;
90 if (!strcmp(tsp->name, aname)) return htons(tsp->port);
94 static int TrimLine(abuffer)
102 if (!isspace(tc)) break;
106 strcpy(abuffer, tbuffer);
112 * IsClientConfigDirectory() -- determine if path matches well-known
113 * client configuration directory.
115 static int IsClientConfigDirectory(const char *path)
117 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
120 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
121 int cc = tolower(cdir[i]);
122 int pc = tolower(path[i]);
135 /* hit end of one or both; allow mismatch in existence of trailing slash */
136 if (cdir[i] != '\0') {
137 if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
141 if (path[i] != '\0') {
142 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
148 #endif /* AFS_NT40_ENV */
151 static int afsconf_Check(adir)
152 register struct afsconf_dir *adir; {
155 register afs_int32 code;
158 /* NT client CellServDB has different file name than NT server or Unix */
159 if (IsClientConfigDirectory(adir->name)) {
160 strcompose(tbuffer, 256,
161 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
163 strcompose(tbuffer, 256,
164 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
167 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
168 #endif /* AFS_NT40_ENV */
170 code = stat(tbuffer, &tstat);
174 /* did file change? */
175 if (tstat.st_mtime == adir->timeRead) {
178 /* otherwise file has changed, so reopen it */
179 return afsconf_Reopen(adir);
182 /* set modtime on file */
183 static afsconf_Touch(adir)
184 register struct afsconf_dir *adir; {
186 struct timeval tvp[2];
189 /* NT client CellServDB has different file name than NT server or Unix */
190 if (IsClientConfigDirectory(adir->name)) {
191 strcompose(tbuffer, 256,
192 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
194 strcompose(tbuffer, 256,
195 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
198 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
199 #endif /* AFS_NT40_ENV */
201 adir->timeRead = 0; /* just in case */
203 return _utime(tbuffer, NULL);
205 gettimeofday(&tvp[0], NULL);
207 return utimes(tbuffer, tvp);
208 #endif /* AFS_NT40_ENV */
211 struct afsconf_dir *afsconf_Open(adir)
212 register char *adir; {
213 register struct afsconf_dir *tdir;
214 register afs_int32 code;
217 /* zero structure and fill in name; rest is done by internal routine */
218 tdir = (struct afsconf_dir *) malloc(sizeof(struct afsconf_dir));
219 bzero(tdir, sizeof(struct afsconf_dir));
220 tdir->name = (char *) malloc(strlen(adir)+1);
221 strcpy(tdir->name, adir);
223 code = afsconf_OpenInternal(tdir, 0, 0);
225 char *afsconf_path, *getenv(), afs_confdir[128];
228 /* Check global place only when local Open failed for whatever reason */
229 if (!(afsconf_path = getenv("AFSCONF"))) {
230 /* 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... */
235 if (!(home_dir = getenv("HOME"))) {
236 /* Our last chance is the "/.AFSCONF" file */
237 fp = fopen("/.AFSCONF", "r");
241 return (struct afsconf_dir *) 0;
243 fgets(afs_confdir, 128, fp);
248 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
249 fp = fopen(pathname, "r");
251 /* Our last chance is the "/.AFSCONF" file */
252 fp = fopen("/.AFSCONF", "r");
256 return (struct afsconf_dir *) 0;
258 fgets(afs_confdir, 128, fp);
261 fgets(afs_confdir, 128, fp);
264 len = strlen(afs_confdir);
268 return (struct afsconf_dir *) 0;
270 if (afs_confdir[len-1] == '\n') {
271 afs_confdir[len-1] = 0;
273 afsconf_path = afs_confdir;
275 tdir->name = (char *) malloc(strlen(afsconf_path)+1);
276 strcpy(tdir->name, afsconf_path);
277 code = afsconf_OpenInternal(tdir, 0, 0);
282 return (struct afsconf_dir *) 0;
290 static int GetCellUnix(struct afsconf_dir *adir)
296 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
297 tf = fopen(tbuffer, "r");
299 rc = fscanf(tf, "%s", tbuffer);
301 adir->cellName = (char *) malloc(strlen(tbuffer)+1);
302 strcpy(adir->cellName, tbuffer);
314 static int GetCellNT(struct afsconf_dir *adir)
316 if (IsClientConfigDirectory(adir->name)) {
317 /* NT client config dir; ThisCell is in registry (no file). */
318 return afssw_GetClientCellName(&adir->cellName);
320 /* NT server config dir; works just like Unix */
321 return GetCellUnix(adir);
324 #endif /* AFS_NT40_ENV */
327 static int afsconf_OpenInternal(adir, cell, clones)
328 register struct afsconf_dir *adir;
333 register char *tp, *bp;
334 register struct afsconf_entry *curEntry;
335 register afs_int32 code;
337 char tbuffer[256], tbuf1[256];
340 /* figure out the cell name */
344 i = GetCellUnix(adir);
350 /* now parse the individual lines */
354 /* NT client/server have a CellServDB that is the same format as Unix.
355 * However, the NT client uses a different file name
357 if (IsClientConfigDirectory(adir->name)) {
358 /* NT client config dir */
359 strcompose(tbuffer, 256,
360 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
362 /* NT server config dir */
363 strcompose(tbuffer, 256,
364 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
367 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
368 #endif /* AFS_NT40_ENV */
370 if (!stat(tbuffer, &tstat)) {
371 adir->timeRead = tstat.st_mtime;
376 strcpy(tbuf1, tbuffer);
377 tf = fopen(tbuffer, "r");
382 tp = fgets(tbuffer, sizeof(tbuffer), tf);
384 TrimLine(tbuffer); /* remove white space */
385 if (tbuffer[0] == 0 || tbuffer[0] == '\n') continue; /* empty line */
386 if (tbuffer[0] == '>') {
387 char linkedcell[MAXCELLCHARS];
388 /* start new cell item */
390 /* thread this guy on the list */
391 curEntry->next = adir->entries;
392 adir->entries = curEntry;
395 curEntry = (struct afsconf_entry *) malloc(sizeof(struct afsconf_entry));
396 bzero(curEntry, sizeof(struct afsconf_entry));
397 code = ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
399 afsconf_CloseInternal(adir);
403 if (linkedcell[0] != '\0') {
404 curEntry->cellInfo.linkedCell =
405 (char *) malloc(strlen(linkedcell) + 1);
406 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
410 /* new host in the current cell */
412 afsconf_CloseInternal(adir);
416 i = curEntry->cellInfo.numServers;
417 if (cell && !strcmp(cell, curEntry->cellInfo.name))
418 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], &clones[i]);
420 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], 0);
422 if (code == AFSCONF_SYNTAX) {
423 for (bp=tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
427 fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
431 afsconf_CloseInternal(adir);
434 curEntry->cellInfo.numServers = ++i;
437 fclose(tf); /* close the file now */
439 /* end the last partially-completed cell */
441 curEntry->next = adir->entries;
442 adir->entries = curEntry;
445 /* now read the fs keys, if possible */
446 adir->keystr = (struct afsconf_keys *) 0;
447 afsconf_IntGetKeys(adir);
452 /* parse a line of the form
453 *"128.2.1.3 #hostname" or
454 *"[128.2.1.3] #hostname" for clones
455 * into the appropriate pieces.
457 static ParseHostLine(aline, addr, aname, aclone)
459 register struct sockaddr_in *addr;
463 register afs_int32 code;
467 if (aclone) *aclone = 1;
468 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
470 if (aclone) *aclone = 0;
471 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
473 if (code != 5) return AFSCONF_SYNTAX;
474 addr->sin_family = AF_INET;
476 tp = (char *) &addr->sin_addr;
484 /* parse a line of the form
485 * ">cellname [linkedcellname] [#comments]"
486 * into the appropriate pieces.
488 static ParseCellLine(aline, aname, alname)
489 register char *aline, *aname, *alname; {
491 code = sscanf(aline, ">%s %s", aname, alname);
492 if (code == 1) *alname = '\0';
494 if (*alname == '#') {
498 return (code > 0 ? 0 : AFSCONF_SYNTAX);
501 /* call aproc(entry, arock, adir) for all cells. Proc must return 0, or we'll stop early and return the code it returns */
502 afsconf_CellApply(adir, aproc, arock)
503 struct afsconf_dir *adir;
506 register struct afsconf_entry *tde;
507 register afs_int32 code;
509 for(tde=adir->entries; tde; tde=tde->next) {
510 code = (*aproc)(&tde->cellInfo, arock, adir);
520 afs_int32 afsconf_SawCell = 0;
522 afsconf_GetExtendedCellInfo(adir, acellName, aservice, acellInfo, clones)
523 struct afsconf_dir *adir;
526 struct afsconf_cell *acellInfo;
532 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
539 cell = (char *) &acellInfo->name;
541 code = afsconf_OpenInternal(adir, cell, clones);
545 afsconf_GetCellInfo(adir, acellName, aservice, acellInfo)
546 struct afsconf_dir *adir;
549 struct afsconf_cell *acellInfo; {
550 register struct afsconf_entry *tce;
551 struct afsconf_entry *bestce;
552 register afs_int32 i;
559 if (adir) afsconf_Check(adir);
562 cnLen = strlen(tcell)+1;
563 lcstring (tcell, tcell, cnLen);
564 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
565 /* call to afsconf_GetLocalCell: like klog */
567 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
574 cnLen = strlen(tcell);
575 bestce = (struct afsconf_entry *) 0;
581 for(tce=adir->entries;tce;tce=tce->next) {
582 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
588 if (strlen(tce->cellInfo.name) < cnLen) continue; /* clearly wrong */
589 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
590 if (bestce) ambig = 1; /* ambiguous unless we get exact match */
594 if (!ambig && bestce) {
595 *acellInfo = bestce->cellInfo; /* structure assignment */
597 tservice = afsconf_FindService(aservice);
600 return AFSCONF_NOTFOUND; /* service not found */
602 for(i=0;i<acellInfo->numServers;i++) {
603 acellInfo->hostAddr[i].sin_port = tservice;
611 return AFSCONF_NOTFOUND;
615 afsconf_GetLocalCell(adir, aname, alen)
616 register struct afsconf_dir *adir;
619 static int afsconf_showcell = 0;
626 * If a cell switch was specified in a command, then it should override the
627 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
628 * is set and the cell name in the adir structure is used.
629 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
631 if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {
632 if ( !afsconf_showcell ) {
633 fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
634 afsconf_showcell = 1;
636 strncpy(aname, afscell_path, alen);
639 if (adir->cellName) {
640 strncpy(aname, adir->cellName, alen);
642 else code = AFSCONF_UNKNOWN;
650 struct afsconf_dir *adir; {
652 afsconf_CloseInternal(adir);
653 if (adir->name) free(adir->name);
659 static int afsconf_CloseInternal(adir)
660 register struct afsconf_dir *adir; {
661 register struct afsconf_entry *td, *nd;
662 register char *tname;
664 tname = adir->name; /* remember name, since that's all we preserve */
666 /* free everything we can find */
667 if (adir->cellName) free(adir->cellName);
668 for(td=adir->entries;td;td=nd) {
670 if (td->cellInfo.linkedCell)
671 free(td->cellInfo.linkedCell);
674 if (adir->keystr) free(adir->keystr);
677 bzero(adir, sizeof(struct afsconf_dir));
678 adir->name = tname; /* restore it */
682 static int afsconf_Reopen(adir)
683 register struct afsconf_dir *adir; {
684 register afs_int32 code;
685 code = afsconf_CloseInternal(adir);
686 if (code) return code;
687 code = afsconf_OpenInternal(adir, 0, 0);
691 /* called during opening of config file */
692 afsconf_IntGetKeys(adir)
693 struct afsconf_dir *adir;
697 struct afsconf_keys *tstr;
698 register afs_int32 code;
701 /* NT client config dir has no KeyFile; don't risk attempting open
702 * because there might be a random file of this name if dir is shared.
704 if (IsClientConfigDirectory(adir->name)) {
705 adir->keystr = ((struct afsconf_keys *)
706 malloc(sizeof(struct afsconf_keys)));
707 adir->keystr->nkeys = 0;
710 #endif /* AFS_NT40_ENV */
713 /* compute the key name and other setup */
715 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
716 tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
720 fd = open(tbuffer, O_RDONLY);
726 code = read(fd, tstr, sizeof(struct afsconf_keys));
728 if (code < sizeof(afs_int32)) {
734 /* convert key structure to host order */
735 tstr->nkeys = ntohl(tstr->nkeys);
736 for(fd=0;fd<tstr->nkeys;fd++)
737 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
743 /* get keys structure */
744 afsconf_GetKeys(adir, astr)
745 struct afsconf_dir *adir;
746 struct afsconf_keys *astr;
750 bcopy(adir->keystr, astr, sizeof(struct afsconf_keys));
756 afs_int32 afsconf_GetLatestKey(adir, avno, akey)
757 IN struct afsconf_dir *adir;
763 register struct afsconf_key *tk;
764 register afs_int32 best;
765 struct afsconf_key *bestk;
769 maxa = adir->keystr->nkeys;
771 best = -1; /* highest kvno we've seen yet */
772 bestk = (struct afsconf_key *) 0; /* ptr to structure providing best */
773 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
774 if (tk->kvno == 999) continue; /* skip bcrypt keys */
775 if (tk->kvno > best) {
780 if (bestk) { /* found any */
781 if (akey) bcopy(bestk->key, akey, 8); /* copy out latest key */
782 if (avno) *avno = bestk->kvno; /* and kvno to caller */
787 return AFSCONF_NOTFOUND; /* didn't find any keys */
790 /* get a particular key */
791 afsconf_GetKey(adir, avno, akey)
792 struct afsconf_dir *adir;
796 register int i, maxa;
797 register struct afsconf_key *tk;
801 maxa = adir->keystr->nkeys;
803 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
804 if (tk->kvno == avno) {
805 bcopy(tk->key, akey, 8);
812 return AFSCONF_NOTFOUND;
815 /* save the key structure in the appropriate file */
816 static SaveKeys(adir)
817 struct afsconf_dir *adir;
819 struct afsconf_keys tkeys;
821 register afs_int32 i;
824 bcopy(adir->keystr, &tkeys, sizeof(struct afsconf_keys));
826 /* convert it to net byte order */
827 for(i = 0; i<tkeys.nkeys; i++ )
828 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
829 tkeys.nkeys = htonl(tkeys.nkeys);
831 /* rewrite keys file */
832 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
833 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
834 if (fd < 0) return AFSCONF_FAILURE;
835 i = write(fd, &tkeys, sizeof(tkeys));
836 if (i != sizeof(tkeys)) {
838 return AFSCONF_FAILURE;
840 if (close(fd) < 0) return AFSCONF_FAILURE;
844 afsconf_AddKey(adir, akvno, akey, overwrite)
845 struct afsconf_dir *adir;
846 afs_int32 akvno, overwrite;
849 register struct afsconf_keys *tk;
850 register struct afsconf_key *tkey;
851 register afs_int32 i;
858 if (akvno < 0 || akvno > 255) {
864 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
865 if (tkey->kvno == akvno) {
868 return AFSCONF_KEYINUSE;
875 if (tk->nkeys >= AFSCONF_MAXKEYS) {
879 tkey = &tk->key[tk->nkeys++];
882 bcopy(akey, tkey->key, 8);
889 /* this proc works by sliding the other guys down, rather than using a funny
890 kvno value, so that callers can count on getting a good key in key[0].
892 afsconf_DeleteKey(adir, akvno)
893 struct afsconf_dir *adir;
896 register struct afsconf_keys *tk;
897 register struct afsconf_key *tkey;
904 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
905 if (tkey->kvno == akvno) {
912 return AFSCONF_NOTFOUND;
915 /* otherwise slide the others down. i and tkey point at the guy to delete */
916 for(;i<tk->nkeys-1; i++,tkey++) {
917 tkey->kvno = (tkey+1)->kvno;
918 bcopy((tkey+1)->key, tkey->key, 8);