windows-afsdb-and-freelance-afs-root-support-20011005
[openafs.git] / src / auth / cellconfig.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <afs/stds.h>
16 #include <afs/pthread_glock.h>
17 #ifdef UKERNEL
18 #include "../afs/sysincludes.h"
19 #include "../afs/afsincludes.h"
20 #else /* UKERNEL */
21 #include <sys/types.h>
22 #ifdef AFS_NT40_ENV
23 #include <winsock2.h>
24 #include <sys/utime.h>
25 #include <io.h>
26 #include <WINNT/afssw.h>
27 #ifdef AFS_AFSDB_ENV
28 #include <cm_dns.h>
29 #endif /* AFS_AFSDB_ENV */
30 #else
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netdb.h>
34 #include <sys/file.h>
35 #include <sys/time.h>
36 #ifdef AFS_AFSDB_ENV
37 #include <arpa/nameser.h>
38 #include <resolv.h>
39 #endif /* AFS_AFSDB_ENV */
40 #endif /* AFS_NT40_ENV */
41 #include <errno.h>
42 #include <ctype.h>
43 #include <time.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #ifdef HAVE_STRING_H
49 #include <string.h>
50 #else
51 #ifdef HAVE_STRINGS_H
52 #include <strings.h>
53 #endif
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58 #endif /* UKERNEL */
59 #include <afs/afsutil.h>
60 #include "cellconfig.h"
61 #include "keys.h"
62 #include <afs/afsint.h>
63
64 static ParseHostLine();
65 static ParseCellLine();
66 static afsconf_OpenInternal();
67 static afsconf_CloseInternal();
68 static afsconf_Reopen();
69
70 static struct afsconf_servPair serviceTable [] = {
71     { "afs",       7000, },
72     { "afscb",     7001, },
73     { "afsprot",   7002, },
74     { "afsvldb",   7003, },
75     { "afskauth",  7004, },
76     { "afsvol",    7005, },
77     { "afserror",  7006, },
78     { "afsnanny",  7007, },
79     { "afsupdate", 7008, },
80     { "afsrmtsys", 7009, },
81     { "afsres",    7010, },  /* residency database for MR-AFS */
82     { "afsremio",  7011, },  /* remote I/O interface for MR-AFS */
83     { 0, 0 }                 /* insert new services before this spot */
84 };
85
86 /*
87  * Basic Rule: we touch "<AFSCONF_DIR>/CellServDB" every time we change anything, so
88  * our code can tell if there is new info in the key files, the cell server db
89  * files or any of the other files (and reopen the thing) if the date on
90  * CellServDB changes.
91  */
92
93 /* return port number in network byte order in the low 16 bits of a long; return -1 if not found */
94 static afs_int32 afsconf_FindService(aname)
95 register char *aname; {
96     /* lookup a service name */
97     struct servent *ts;
98     register struct afsconf_servPair *tsp;
99
100 #if     defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
101     ts = getservbyname(aname, "");
102 #else
103     ts = getservbyname(aname, (char *) 0);
104 #endif
105     if (ts) {
106         /* we found it in /etc/services, so we use this value */
107         return ts->s_port;  /* already in network byte order */
108     }
109
110     /* not found in /etc/services, see if it is one of ours */
111     for(tsp = serviceTable;; tsp++) {
112         if (tsp->name == (char *) 0) return -1;
113         if (!strcmp(tsp->name, aname)) return htons(tsp->port);
114     }
115 }
116
117 static int TrimLine(abuffer)
118 char *abuffer; {
119     char tbuffer[256];
120     register char *tp;
121     register int tc;
122
123     tp = abuffer;
124     while ((tc = *tp)) {
125         if (!isspace(tc)) break;
126         tp++;
127     }
128     strcpy(tbuffer, tp);
129     strcpy(abuffer, tbuffer);
130     return 0;
131 }
132
133 #ifdef AFS_NT40_ENV
134 /*
135  * IsClientConfigDirectory() -- determine if path matches well-known
136  *     client configuration directory.
137  */
138 static int IsClientConfigDirectory(const char *path)
139 {
140     const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
141     int i;
142
143     for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
144         int cc = tolower(cdir[i]);
145         int pc = tolower(path[i]);
146
147         if (cc == '\\') {
148             cc = '/';
149         }
150         if (pc == '\\') {
151             pc = '/';
152         }
153         if (cc != pc) {
154             return 0;
155         }
156     }
157
158     /* hit end of one or both; allow mismatch in existence of trailing slash */
159     if (cdir[i] != '\0') {
160         if ((cdir[i] != '\\' && cdir[i] != '/') || (cdir[i + 1] != '\0')) {
161             return 0;
162         }
163     }
164     if (path[i] != '\0') {
165         if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
166             return 0;
167         }
168     }
169     return 1;
170 }
171 #endif /* AFS_NT40_ENV */
172
173
174 static int afsconf_Check(adir)
175 register struct afsconf_dir *adir; {
176     char tbuffer[256];
177     struct stat tstat;
178     register afs_int32 code;
179
180 #ifdef AFS_NT40_ENV
181     /* NT client CellServDB has different file name than NT server or Unix */
182     if (IsClientConfigDirectory(adir->name)) {
183         strcompose(tbuffer, 256,
184                    adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
185     } else {
186         strcompose(tbuffer, 256,
187                    adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
188     }
189 #else
190     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
191 #endif /* AFS_NT40_ENV */
192
193     code = stat(tbuffer, &tstat);
194     if (code < 0) {
195         return code;
196     }
197     /* did file change? */
198     if (tstat.st_mtime == adir->timeRead) {
199         return 0;
200     }
201     /* otherwise file has changed, so reopen it */
202     return afsconf_Reopen(adir);
203 }
204
205 /* set modtime on file */
206 static afsconf_Touch(adir)
207 register struct afsconf_dir *adir; {
208     char tbuffer[256];
209 #ifndef AFS_NT40_ENV
210     struct timeval tvp[2];
211 #endif
212
213     adir->timeRead = 0; /* just in case */
214
215 #ifdef AFS_NT40_ENV
216     /* NT client CellServDB has different file name than NT server or Unix */
217
218     if (IsClientConfigDirectory(adir->name)) {
219         strcompose(tbuffer, 256,
220                    adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
221     } else {
222         strcompose(tbuffer, 256,
223                    adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
224     }
225
226     return _utime(tbuffer, NULL);
227
228 #else
229     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
230     gettimeofday(&tvp[0], NULL);
231     tvp[1] = tvp[0];
232     return utimes(tbuffer, tvp);
233 #endif  /* AFS_NT40_ENV */
234 }
235
236 struct afsconf_dir *afsconf_Open(adir)
237 register char *adir; {
238     register struct afsconf_dir *tdir;
239     register afs_int32 code;
240
241     LOCK_GLOBAL_MUTEX
242     /* zero structure and fill in name; rest is done by internal routine */
243     tdir = (struct afsconf_dir *) malloc(sizeof(struct afsconf_dir));
244     memset(tdir, 0, sizeof(struct afsconf_dir));
245     tdir->name = (char *) malloc(strlen(adir)+1);
246     strcpy(tdir->name, adir);
247
248     code = afsconf_OpenInternal(tdir, 0, 0);
249     if (code) {
250         char *afsconf_path, *getenv(), afs_confdir[128];
251
252         free(tdir->name);
253         /* Check global place only when local Open failed for whatever reason */
254         if (!(afsconf_path = getenv("AFSCONF"))) {
255             /* 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... */
256             char *home_dir;
257             FILE *fp;
258             size_t len;
259
260             if (!(home_dir = getenv("HOME"))) {
261                 /* Our last chance is the "/.AFSCONF" file */
262                 fp = fopen("/.AFSCONF", "r");
263                 if (fp == 0) {
264                     free(tdir);
265                     UNLOCK_GLOBAL_MUTEX
266                     return (struct afsconf_dir *) 0;
267                 }
268                 fgets(afs_confdir, 128, fp);
269                 fclose(fp);
270             } else {
271                 char pathname[256];
272
273                 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
274                 fp = fopen(pathname, "r");
275                 if (fp == 0) {
276                     /* Our last chance is the "/.AFSCONF" file */
277                     fp = fopen("/.AFSCONF", "r");
278                     if (fp == 0) {
279                         free(tdir);
280                         UNLOCK_GLOBAL_MUTEX
281                         return (struct afsconf_dir *) 0;
282                     }
283                     fgets(afs_confdir, 128, fp);
284                     fclose(fp);
285                 }
286                 fgets(afs_confdir, 128, fp);
287                 fclose(fp);             
288             }
289             len = strlen(afs_confdir);
290             if (len == 0) {
291                 free(tdir);
292                 UNLOCK_GLOBAL_MUTEX
293                 return (struct afsconf_dir *) 0;
294             }
295             if (afs_confdir[len-1] == '\n') {
296                 afs_confdir[len-1] = 0;
297             }
298             afsconf_path = afs_confdir;
299         }
300         tdir->name = (char *) malloc(strlen(afsconf_path)+1);
301         strcpy(tdir->name, afsconf_path);
302         code = afsconf_OpenInternal(tdir, 0, 0);
303         if (code) {
304             free(tdir->name);
305             free(tdir);
306             UNLOCK_GLOBAL_MUTEX
307             return (struct afsconf_dir *) 0;
308         }
309     }
310     UNLOCK_GLOBAL_MUTEX
311     return tdir;
312 }
313
314
315 static int GetCellUnix(struct afsconf_dir *adir)
316 {
317     int rc;
318     char tbuffer[256];
319     FILE *tf;
320
321     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
322     tf = fopen(tbuffer, "r");
323     if (tf) {
324         rc = fscanf(tf, "%s", tbuffer);
325         if (rc == 1) {
326             adir->cellName = (char *) malloc(strlen(tbuffer)+1);
327             strcpy(adir->cellName, tbuffer);
328         }
329         fclose(tf);
330     }
331     else {
332         return -1;
333     }
334     return 0;
335 }
336
337
338 #ifdef AFS_NT40_ENV
339 static int GetCellNT(struct afsconf_dir *adir)
340 {
341     if (IsClientConfigDirectory(adir->name)) {
342         /* NT client config dir; ThisCell is in registry (no file). */
343         return afssw_GetClientCellName(&adir->cellName);
344     } else {
345         /* NT server config dir; works just like Unix */
346         return GetCellUnix(adir);
347     }
348 }
349 #endif /* AFS_NT40_ENV */
350
351
352 static int afsconf_OpenInternal(adir, cell, clones)
353 register struct afsconf_dir *adir; 
354 char *cell;
355 char clones[];
356 {
357     FILE *tf;
358     register char *tp, *bp;
359     register struct afsconf_entry *curEntry;
360     register afs_int32 code;
361     afs_int32 i;
362     char tbuffer[256], tbuf1[256];
363     struct stat tstat;
364
365     /* figure out the cell name */
366 #ifdef AFS_NT40_ENV
367     i = GetCellNT(adir);
368 #else
369     i = GetCellUnix(adir);
370 #endif
371
372 #ifndef AFS_FREELANCE_CLIENT  /* no local cell not fatal in freelance */
373     if (i) {
374         return i;
375     }
376 #endif
377
378     /* now parse the individual lines */
379     curEntry = 0;
380
381 #ifdef AFS_NT40_ENV
382     /* NT client/server have a CellServDB that is the same format as Unix.
383      * However, the NT client uses a different file name
384      */
385     if (IsClientConfigDirectory(adir->name)) {
386         /* NT client config dir */
387         strcompose(tbuffer, 256,
388                    adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
389     } else {
390         /* NT server config dir */
391         strcompose(tbuffer, 256,
392                    adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
393     }
394 #else
395     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
396 #endif  /* AFS_NT40_ENV */
397
398     if (!stat(tbuffer, &tstat)) {
399         adir->timeRead = tstat.st_mtime;
400     } else {
401         adir->timeRead = 0;
402     }
403
404     strcpy(tbuf1, tbuffer);
405     tf = fopen(tbuffer, "r");
406     if (!tf) {
407         return -1;
408     }
409     while (1) {
410         tp = fgets(tbuffer, sizeof(tbuffer), tf);
411         if (!tp) break;
412         TrimLine(tbuffer);  /* remove white space */
413         if (tbuffer[0] == 0 || tbuffer[0] == '\n') continue;   /* empty line */
414         if (tbuffer[0] == '>') {
415             char linkedcell[MAXCELLCHARS];
416             /* start new cell item */
417             if (curEntry) {
418                 /* thread this guy on the list */
419                 curEntry->next = adir->entries;
420                 adir->entries = curEntry;
421                 curEntry = 0;
422             }
423             curEntry = (struct afsconf_entry *) malloc(sizeof(struct afsconf_entry));
424             memset(curEntry, 0, sizeof(struct afsconf_entry));
425             code = ParseCellLine(tbuffer, curEntry->cellInfo.name, linkedcell);
426             if (code) {
427                 afsconf_CloseInternal(adir);
428                 fclose(tf);
429                 return -1;
430             }
431             if (linkedcell[0] != '\0') {
432                 curEntry->cellInfo.linkedCell =
433                     (char *) malloc(strlen(linkedcell) + 1);
434                 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
435             }
436         }
437         else {
438             /* new host in the current cell */
439             if (!curEntry) {
440                 afsconf_CloseInternal(adir);
441                 fclose(tf);
442                 return -1;
443             }
444             i = curEntry->cellInfo.numServers;
445            if (cell && !strcmp(cell, curEntry->cellInfo.name)) 
446                 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], &clones[i]);
447            else
448                 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], 0);
449             if (code) {
450                 if (code == AFSCONF_SYNTAX) {
451                     for (bp=tbuffer; *bp != '\n'; bp++) {       /* Take out the <cr> from the buffer */
452                         if (!*bp) break;
453                     }
454                     *bp= '\0';
455                     fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
456                 }
457                 free(curEntry);
458                 fclose(tf);
459                 afsconf_CloseInternal(adir);
460                 return -1;
461             }
462             curEntry->cellInfo.numServers = ++i;
463         }
464     }
465     fclose(tf); /* close the file now */
466
467     /* end the last partially-completed cell */
468     if (curEntry) {
469         curEntry->next = adir->entries;
470         adir->entries = curEntry;
471     }
472     
473     /* now read the fs keys, if possible */
474     adir->keystr = (struct afsconf_keys *) 0;
475     afsconf_IntGetKeys(adir);
476
477     return 0;
478 }
479
480 /* parse a line of the form
481  *"128.2.1.3   #hostname" or
482  *"[128.2.1.3]  #hostname" for clones
483  * into the appropriate pieces.  
484  */
485 static ParseHostLine(aline, addr, aname, aclone)
486     char *aclone;
487     register struct sockaddr_in *addr;
488     char *aline, *aname; 
489 {
490     int c1, c2, c3, c4;
491     register afs_int32 code;
492     register char *tp;
493
494     if (*aline == '[') {
495         if (aclone) *aclone = 1;
496         code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
497     } else {
498         if (aclone) *aclone = 0;
499         code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
500     }
501     if (code != 5) return AFSCONF_SYNTAX;
502     addr->sin_family = AF_INET;
503     addr->sin_port = 0;
504     tp = (char *) &addr->sin_addr;
505     *tp++ = c1;
506     *tp++ = c2;
507     *tp++ = c3;
508     *tp++ = c4;
509     return 0;
510 }
511
512 /* parse a line of the form
513  * ">cellname [linkedcellname] [#comments]"
514  * into the appropriate pieces.
515  */
516 static ParseCellLine(aline, aname, alname)
517 register char *aline, *aname, *alname; {
518     register int code;
519     code = sscanf(aline, ">%s %s", aname, alname);
520     if (code == 1) *alname = '\0';
521     if (code == 2) {
522         if (*alname == '#') {
523             *alname = '\0';
524         }
525     }
526     return (code > 0 ? 0 : AFSCONF_SYNTAX);
527 }
528
529 /* call aproc(entry, arock, adir) for all cells.  Proc must return 0, or we'll stop early and return the code it returns */
530 afsconf_CellApply(adir, aproc, arock)
531 struct afsconf_dir *adir;
532 int (*aproc)();
533 char *arock; {
534     register struct afsconf_entry *tde;
535     register afs_int32 code;
536     LOCK_GLOBAL_MUTEX
537     for(tde=adir->entries; tde; tde=tde->next) {
538         code = (*aproc)(&tde->cellInfo, arock, adir);
539         if (code) {
540             UNLOCK_GLOBAL_MUTEX
541             return code;
542         }
543     }
544     UNLOCK_GLOBAL_MUTEX
545     return 0;
546 }
547
548 afs_int32 afsconf_SawCell = 0;
549
550 afsconf_GetExtendedCellInfo(adir, acellName, aservice, acellInfo, clones)
551     struct afsconf_dir *adir;
552     char *aservice;
553     char *acellName;
554     struct afsconf_cell *acellInfo; 
555     char clones[];
556 {
557     afs_int32 code;
558     char *cell;
559
560     code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
561     if (code) 
562        return code;
563
564     if (acellName) 
565        cell = acellName;
566     else
567        cell = (char *) &acellInfo->name;
568
569     code = afsconf_OpenInternal(adir, cell, clones);
570     return code;
571 }
572
573 #ifdef AFS_AFSDB_ENV
574 #if !defined(AFS_NT40_ENV)
575 afsconf_GetAfsdbInfo(acellName, aservice, acellInfo)
576     char *acellName;
577     char *aservice;
578     struct afsconf_cell *acellInfo;
579 {
580     afs_int32 code;
581     int tservice, i;
582     size_t len;
583     unsigned char answer[1024];
584     unsigned char *p;
585     char host[256];
586     int server_num = 0;
587     int minttl = 0;
588
589     /* The resolver isn't always MT-safe.. Perhaps this ought to be
590      * replaced with a more fine-grained lock just for the resolver
591      * operations.
592      */
593     LOCK_GLOBAL_MUTEX
594     len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
595     UNLOCK_GLOBAL_MUTEX
596
597     if (len < 0)
598         return AFSCONF_NOTFOUND;
599
600     p = answer + sizeof(HEADER);        /* Skip header */
601     code = dn_expand(answer, answer + len, p, host, sizeof(host));
602     if (code < 0)
603         return AFSCONF_NOTFOUND;
604     strncpy(acellInfo->name, host, sizeof(acellInfo->name));
605
606     p += code + QFIXEDSZ;       /* Skip name */
607
608     while (p < answer + len) {
609         int type, ttl, size;
610
611         code = dn_expand(answer, answer + len, p, host, sizeof(host));
612         if (code < 0)
613             return AFSCONF_NOTFOUND;
614
615         p += code;      /* Skip the name */
616         type = (p[0] << 8) | p[1];
617         p += 4;         /* Skip type and class */
618         ttl = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
619         p += 4;         /* Skip the TTL */
620         size = (p[0] << 8) | p[1];
621         p += 2;         /* Skip the size */
622
623         if (type == T_AFSDB) {
624             struct hostent *he;
625             short afsdb_type;
626
627             afsdb_type = (p[0] << 8) | p[1];
628             code = dn_expand(answer, answer+len, p+2, host, sizeof(host));
629             if (code < 0)
630                 return AFSCONF_NOTFOUND;
631
632             if ((afsdb_type == 1) &&
633                 (server_num < MAXHOSTSPERCELL) &&
634                 /* Do we want to get TTL data for the A record as well? */
635                 (he = gethostbyname(host))) {
636                 afs_int32 ipaddr;
637                 memcpy(&ipaddr, he->h_addr, he->h_length);
638                 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
639                 strncpy(acellInfo->hostName[server_num], host,
640                         sizeof(acellInfo->hostName[server_num]));
641                 server_num++;
642
643                 if (!minttl || ttl < minttl) minttl = ttl;
644             }
645         }
646
647         p += size;
648     }
649
650     if (server_num == 0)                /* No AFSDB records */
651         return AFSCONF_NOTFOUND;
652     acellInfo->numServers = server_num;
653
654     if (aservice) {
655         tservice = afsconf_FindService(aservice);
656         if (tservice < 0)
657             return AFSCONF_NOTFOUND;  /* service not found */
658         for (i=0; i<acellInfo->numServers; i++) {
659             acellInfo->hostAddr[i].sin_port = tservice;
660         }
661     }
662
663     acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
664
665     return 0;
666 }
667 #else  /* windows */
668 int afsconf_GetAfsdbInfo(acellName, aservice, acellInfo)
669   char *aservice;
670   char *acellName;
671   struct afsconf_cell *acellInfo;
672 {
673     register afs_int32 i;
674     int tservice;
675     struct afsconf_entry DNSce;
676     char *DNStmpStrp; /* a temp string pointer */
677     struct hostent *thp;
678     afs_int32 cellHosts[AFSMAXCELLHOSTS];
679     int numServers;
680     int rc;
681     int *ttl;
682
683     DNSce.cellInfo.numServers=0;
684     DNSce.next = NULL;
685     rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
686     /* ignore the ttl here since this code is only called by transitory programs
687        like klog, etc. */
688     if (rc < 0)
689       return -1;
690     if (numServers == 0)
691       return -1;
692
693     for (i = 0; i < numServers; i++)
694     {
695         memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i], sizeof(long));
696         acellInfo->hostAddr[i].sin_family = AF_INET;
697
698         /* sin_port supplied by connection code */
699     }
700
701     acellInfo->numServers = numServers;
702     strcpy(acellInfo->name, acellName);
703     if (aservice) {
704         LOCK_GLOBAL_MUTEX
705         tservice = afsconf_FindService(aservice);
706      UNLOCK_GLOBAL_MUTEX
707         if (tservice < 0) {
708             return AFSCONF_NOTFOUND;  /* service not found */
709      }
710      for(i=0; i< acellInfo->numServers; i++) {
711             acellInfo->hostAddr[i].sin_port = tservice;
712      }
713     }
714     acellInfo->linkedCell = NULL;    /* no linked cell */
715     acellInfo->flags = 0;
716     return 0;
717 }
718 #endif /* windows */
719 #endif /* AFS_AFSDB_ENV */
720
721 afsconf_GetCellInfo(adir, acellName, aservice, acellInfo)
722 struct afsconf_dir *adir;
723 char *aservice;
724 char *acellName;
725 struct afsconf_cell *acellInfo; {
726     register struct afsconf_entry *tce;
727     struct afsconf_entry *bestce;
728     register afs_int32 i;
729     int tservice;
730     char *tcell;
731     size_t cnLen;
732     int ambig;
733     char tbuffer[64];
734
735     LOCK_GLOBAL_MUTEX
736     if (adir) afsconf_Check(adir);
737     if (acellName) {
738         tcell = acellName;
739         cnLen = strlen(tcell)+1;
740         lcstring (tcell, tcell, cnLen);
741         afsconf_SawCell = 1;    /* will ignore the AFSCELL switch on future */
742                                 /* call to afsconf_GetLocalCell: like klog  */
743     } else {
744         i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
745         if (i) {
746             UNLOCK_GLOBAL_MUTEX
747             return i;
748         }
749         tcell = tbuffer;
750     }
751     cnLen = strlen(tcell);
752     bestce = (struct afsconf_entry *) 0;
753     ambig = 0;
754     if (!adir) {
755         UNLOCK_GLOBAL_MUTEX
756         return 0;
757     }
758     for(tce=adir->entries;tce;tce=tce->next) {
759         if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
760             /* found our cell */
761             bestce = tce;
762             ambig = 0;
763             break;
764         }
765         if (strlen(tce->cellInfo.name) < cnLen) continue;   /* clearly wrong */
766         if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
767             if (bestce) ambig = 1;  /* ambiguous unless we get exact match */
768             bestce = tce;
769         }
770     }
771     if (!ambig && bestce) {
772         *acellInfo = bestce->cellInfo;  /* structure assignment */
773         if (aservice) {
774             tservice = afsconf_FindService(aservice);
775             if (tservice < 0) {
776                 UNLOCK_GLOBAL_MUTEX
777                 return AFSCONF_NOTFOUND;  /* service not found */
778             }
779             for(i=0;i<acellInfo->numServers;i++) {
780                 acellInfo->hostAddr[i].sin_port = tservice;
781             }
782         }
783         acellInfo->timeout = 0;
784         UNLOCK_GLOBAL_MUTEX
785         return 0;
786     }
787     else {
788         UNLOCK_GLOBAL_MUTEX
789 #ifdef AFS_AFSDB_ENV
790         return afsconf_GetAfsdbInfo(acellName, aservice, acellInfo);
791 #else
792         return AFSCONF_NOTFOUND;
793 #endif /* AFS_AFSDB_ENV */
794     }
795 }
796
797 afsconf_GetLocalCell(adir, aname, alen)
798 register struct afsconf_dir *adir;
799 char *aname;
800 afs_int32 alen; {
801     static int  afsconf_showcell = 0;
802     char        *afscell_path;
803     char        *getenv();
804     afs_int32        code = 0;
805
806    LOCK_GLOBAL_MUTEX
807    /*
808     * If a cell switch was specified in a command, then it should override the 
809     * AFSCELL variable.  If a cell was specified, then the afsconf_SawCell flag
810     * is set and the cell name in the adir structure is used.
811     * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
812     */
813    if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {     
814         if ( !afsconf_showcell ) {
815             fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
816             afsconf_showcell = 1;
817         }
818         strncpy(aname, afscell_path, alen);
819     } else {                                    
820         afsconf_Check(adir);
821         if (adir->cellName) {
822             strncpy(aname, adir->cellName, alen);
823         }
824         else code = AFSCONF_UNKNOWN;
825     }
826
827     UNLOCK_GLOBAL_MUTEX
828     return(code);
829 }
830
831 afsconf_Close(adir)
832 struct afsconf_dir *adir; {
833     LOCK_GLOBAL_MUTEX
834     afsconf_CloseInternal(adir);
835     if (adir->name) free(adir->name);
836     free(adir);
837     UNLOCK_GLOBAL_MUTEX
838     return 0;
839 }
840
841 static int afsconf_CloseInternal(adir)
842 register struct afsconf_dir *adir; {
843     register struct afsconf_entry *td, *nd;
844     register char *tname;
845
846     tname = adir->name; /* remember name, since that's all we preserve */
847
848     /* free everything we can find */
849     if (adir->cellName) free(adir->cellName);
850     for(td=adir->entries;td;td=nd) {
851         nd = td->next;
852         if (td->cellInfo.linkedCell)
853             free(td->cellInfo.linkedCell);
854         free(td);
855     }
856     if (adir->keystr) free(adir->keystr);
857
858     /* reinit */
859     memset(adir, 0, sizeof(struct afsconf_dir));
860     adir->name = tname;     /* restore it */
861     return 0;
862 }
863
864 static int afsconf_Reopen(adir)
865 register struct afsconf_dir *adir; {
866     register afs_int32 code;
867     code = afsconf_CloseInternal(adir);
868     if (code) return code;
869     code = afsconf_OpenInternal(adir, 0, 0);
870     return code;
871 }
872
873 /* called during opening of config file */
874 afsconf_IntGetKeys(adir)
875 struct afsconf_dir *adir;
876 {
877     char tbuffer[256];
878     register int fd;
879     struct afsconf_keys *tstr;
880     register afs_int32 code;
881
882 #ifdef AFS_NT40_ENV
883     /* NT client config dir has no KeyFile; don't risk attempting open
884      * because there might be a random file of this name if dir is shared.
885      */
886     if (IsClientConfigDirectory(adir->name)) {
887         adir->keystr = ((struct afsconf_keys *)
888                         malloc(sizeof(struct afsconf_keys)));
889         adir->keystr->nkeys = 0;
890         return 0;
891     }
892 #endif /* AFS_NT40_ENV */
893
894     LOCK_GLOBAL_MUTEX
895     /* compute the key name and other setup */
896
897     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
898     tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
899     adir->keystr = tstr;
900
901     /* read key file */
902     fd = open(tbuffer, O_RDONLY);
903     if (fd < 0) {
904         tstr->nkeys = 0;
905         UNLOCK_GLOBAL_MUTEX
906         return 0;
907     }
908     code = read(fd, tstr, sizeof(struct afsconf_keys));
909     close(fd);
910     if (code < sizeof(afs_int32)) {
911         tstr->nkeys = 0;
912         UNLOCK_GLOBAL_MUTEX
913         return 0;
914     }
915
916     /* convert key structure to host order */
917     tstr->nkeys = ntohl(tstr->nkeys);
918     for(fd=0;fd<tstr->nkeys;fd++)
919         tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
920
921     UNLOCK_GLOBAL_MUTEX
922     return 0;
923 }
924
925 /* get keys structure */
926 afsconf_GetKeys(adir, astr)
927 struct afsconf_dir *adir;
928 struct afsconf_keys *astr;
929 {
930     register afs_int32 code;
931
932     LOCK_GLOBAL_MUTEX
933     code = afsconf_Check(adir);
934     if (code)
935         return AFSCONF_FAILURE;
936     memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
937     UNLOCK_GLOBAL_MUTEX
938     return 0;
939 }
940
941 /* get latest key */
942 afs_int32 afsconf_GetLatestKey(adir, avno, akey)
943   IN struct afsconf_dir *adir;
944   OUT afs_int32 *avno;
945   OUT char *akey;
946 {
947     register int i;
948     int maxa;
949     register struct afsconf_key *tk;
950     register afs_int32 best;
951     struct afsconf_key *bestk;
952     register afs_int32 code;
953     
954     LOCK_GLOBAL_MUTEX
955     code = afsconf_Check(adir);
956     if (code)
957         return AFSCONF_FAILURE;
958     maxa = adir->keystr->nkeys;
959
960     best = -1;      /* highest kvno we've seen yet */
961     bestk = (struct afsconf_key *) 0;   /* ptr to structure providing best */
962     for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
963         if (tk->kvno == 999) continue;  /* skip bcrypt keys */
964         if (tk->kvno > best) {
965             best = tk->kvno;
966             bestk = tk;
967         }
968     }
969     if (bestk) {    /* found any  */
970         if (akey) memcpy(akey, bestk->key, 8); /* copy out latest key */
971         if (avno) *avno = bestk->kvno;  /* and kvno to caller */
972         UNLOCK_GLOBAL_MUTEX
973         return 0;
974     }
975     UNLOCK_GLOBAL_MUTEX
976     return AFSCONF_NOTFOUND;    /* didn't find any keys */
977 }
978
979 /* get a particular key */
980 afsconf_GetKey(adir, avno, akey)
981 struct afsconf_dir *adir;
982 afs_int32 avno;
983 char *akey;
984 {
985     register int i, maxa;
986     register struct afsconf_key *tk;
987     register afs_int32 code;
988
989     LOCK_GLOBAL_MUTEX
990     code = afsconf_Check(adir);
991     if (code)
992         return AFSCONF_FAILURE;
993     maxa = adir->keystr->nkeys;
994
995     for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
996         if (tk->kvno == avno) {
997             memcpy(akey, tk->key, 8);
998             UNLOCK_GLOBAL_MUTEX
999             return 0;
1000         }
1001     }
1002
1003     UNLOCK_GLOBAL_MUTEX
1004     return AFSCONF_NOTFOUND;
1005 }
1006
1007 /* save the key structure in the appropriate file */
1008 static SaveKeys(adir)
1009 struct afsconf_dir *adir;
1010 {
1011     struct afsconf_keys tkeys;
1012     register int fd;
1013     register afs_int32 i;
1014     char tbuffer[256];
1015
1016     memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1017
1018     /* convert it to net byte order */
1019     for(i = 0; i<tkeys.nkeys; i++ )
1020         tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1021     tkeys.nkeys = htonl(tkeys.nkeys);
1022     
1023     /* rewrite keys file */
1024     strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1025     fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1026     if (fd < 0) return AFSCONF_FAILURE;
1027     i = write(fd, &tkeys, sizeof(tkeys));
1028     if (i != sizeof(tkeys)) {
1029         close(fd);
1030         return AFSCONF_FAILURE;
1031     }
1032     if (close(fd) < 0) return AFSCONF_FAILURE;
1033     return 0;
1034 }
1035
1036 afsconf_AddKey(adir, akvno, akey, overwrite)
1037 struct afsconf_dir *adir;
1038 afs_int32 akvno, overwrite;
1039 char akey[8];
1040 {
1041     register struct afsconf_keys *tk;
1042     register struct afsconf_key *tkey;
1043     register afs_int32 i;
1044     int foundSlot;
1045
1046     LOCK_GLOBAL_MUTEX
1047     tk = adir->keystr;
1048     
1049     if (akvno != 999) {
1050         if (akvno < 0 || akvno > 255) {
1051             UNLOCK_GLOBAL_MUTEX
1052             return ERANGE;
1053         }
1054     }
1055     foundSlot = 0;
1056     for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1057         if (tkey->kvno == akvno) {
1058             if (!overwrite) {
1059                 UNLOCK_GLOBAL_MUTEX
1060                 return AFSCONF_KEYINUSE;
1061             }
1062             foundSlot = 1;
1063             break;
1064         }
1065     }
1066     if (!foundSlot) {
1067         if (tk->nkeys >= AFSCONF_MAXKEYS) {
1068             UNLOCK_GLOBAL_MUTEX
1069             return AFSCONF_FULL;
1070         }
1071         tkey = &tk->key[tk->nkeys++];
1072     }
1073     tkey->kvno = akvno;
1074     memcpy(tkey->key, akey, 8);
1075     i = SaveKeys(adir);
1076     afsconf_Touch(adir);
1077     UNLOCK_GLOBAL_MUTEX
1078     return i;
1079 }
1080
1081 /* this proc works by sliding the other guys down, rather than using a funny
1082     kvno value, so that callers can count on getting a good key in key[0].
1083 */
1084 afsconf_DeleteKey(adir, akvno)
1085 struct afsconf_dir *adir;
1086 afs_int32 akvno;
1087 {
1088     register struct afsconf_keys *tk;
1089     register struct afsconf_key *tkey;
1090     register int i;
1091     int foundFlag = 0;
1092
1093     LOCK_GLOBAL_MUTEX
1094     tk = adir->keystr;
1095
1096     for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1097         if (tkey->kvno == akvno) {
1098             foundFlag = 1;
1099             break;
1100         }
1101     }
1102     if (!foundFlag) {
1103         UNLOCK_GLOBAL_MUTEX
1104         return AFSCONF_NOTFOUND;
1105     }
1106
1107     /* otherwise slide the others down.  i and tkey point at the guy to delete */
1108     for(;i<tk->nkeys-1; i++,tkey++) {
1109         tkey->kvno = (tkey+1)->kvno;
1110         memcpy(tkey->key, (tkey+1)->key, 8);
1111     }
1112     tk->nkeys--;
1113     i = SaveKeys(adir);
1114     afsconf_Touch(adir);
1115     UNLOCK_GLOBAL_MUTEX
1116     return i;
1117 }