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>
16 #include <afs/pthread_glock.h>
18 #include "../afs/sysincludes.h"
19 #include "../afs/afsincludes.h"
21 #include <sys/types.h>
24 #include <sys/utime.h>
26 #include <WINNT/afssw.h>
29 #endif /* AFS_AFSDB_ENV */
31 #include <sys/socket.h>
32 #include <netinet/in.h>
37 #include <arpa/nameser.h>
39 #endif /* AFS_AFSDB_ENV */
40 #endif /* AFS_NT40_ENV */
41 #include <afs/afsint.h>
60 #include <afs/afsutil.h>
61 #include "cellconfig.h"
64 static ParseHostLine();
65 static ParseCellLine();
66 static afsconf_OpenInternal();
67 static afsconf_CloseInternal();
68 static afsconf_Reopen();
70 static struct afsconf_servPair serviceTable [] = {
75 { "afskauth", 7004, },
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 */
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
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 */
98 register struct afsconf_servPair *tsp;
100 #if defined(AFS_OSF_ENV) || defined(AFS_DEC_ENV)
101 ts = getservbyname(aname, "");
103 ts = getservbyname(aname, (char *) 0);
106 /* we found it in /etc/services, so we use this value */
107 return ts->s_port; /* already in network byte order */
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);
117 static int TrimLine(abuffer)
125 if (!isspace(tc)) break;
129 strcpy(abuffer, tbuffer);
135 * IsClientConfigDirectory() -- determine if path matches well-known
136 * client configuration directory.
138 static int IsClientConfigDirectory(const char *path)
140 const char *cdir = AFSDIR_CLIENT_ETC_DIRPATH;
143 for (i = 0; cdir[i] != '\0' && path[i] != '\0'; i++) {
144 int cc = tolower(cdir[i]);
145 int pc = tolower(path[i]);
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')) {
164 if (path[i] != '\0') {
165 if ((path[i] != '\\' && path[i] != '/') || (path[i + 1] != '\0')) {
171 #endif /* AFS_NT40_ENV */
174 static int afsconf_Check(adir)
175 register struct afsconf_dir *adir; {
178 register afs_int32 code;
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);
186 strcompose(tbuffer, 256,
187 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
190 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
191 #endif /* AFS_NT40_ENV */
193 code = stat(tbuffer, &tstat);
197 /* did file change? */
198 if (tstat.st_mtime == adir->timeRead) {
201 /* otherwise file has changed, so reopen it */
202 return afsconf_Reopen(adir);
205 /* set modtime on file */
206 static afsconf_Touch(adir)
207 register struct afsconf_dir *adir; {
210 struct timeval tvp[2];
213 adir->timeRead = 0; /* just in case */
216 /* NT client CellServDB has different file name than NT server or Unix */
218 if (IsClientConfigDirectory(adir->name)) {
219 strcompose(tbuffer, 256,
220 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
222 strcompose(tbuffer, 256,
223 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
226 return _utime(tbuffer, NULL);
229 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
230 gettimeofday(&tvp[0], NULL);
232 return utimes(tbuffer, tvp);
233 #endif /* AFS_NT40_ENV */
236 struct afsconf_dir *afsconf_Open(adir)
237 register char *adir; {
238 register struct afsconf_dir *tdir;
239 register afs_int32 code;
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);
248 code = afsconf_OpenInternal(tdir, 0, 0);
250 char *afsconf_path, *getenv(), afs_confdir[128];
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... */
260 if (!(home_dir = getenv("HOME"))) {
261 /* Our last chance is the "/.AFSCONF" file */
262 fp = fopen("/.AFSCONF", "r");
266 return (struct afsconf_dir *) 0;
268 fgets(afs_confdir, 128, fp);
273 sprintf(pathname, "%s/%s", home_dir, ".AFSCONF");
274 fp = fopen(pathname, "r");
276 /* Our last chance is the "/.AFSCONF" file */
277 fp = fopen("/.AFSCONF", "r");
281 return (struct afsconf_dir *) 0;
283 fgets(afs_confdir, 128, fp);
286 fgets(afs_confdir, 128, fp);
289 len = strlen(afs_confdir);
293 return (struct afsconf_dir *) 0;
295 if (afs_confdir[len-1] == '\n') {
296 afs_confdir[len-1] = 0;
298 afsconf_path = afs_confdir;
300 tdir->name = (char *) malloc(strlen(afsconf_path)+1);
301 strcpy(tdir->name, afsconf_path);
302 code = afsconf_OpenInternal(tdir, 0, 0);
307 return (struct afsconf_dir *) 0;
315 static int GetCellUnix(struct afsconf_dir *adir)
321 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_THISCELL_FILE, NULL);
322 tf = fopen(tbuffer, "r");
324 rc = fscanf(tf, "%s", tbuffer);
326 adir->cellName = (char *) malloc(strlen(tbuffer)+1);
327 strcpy(adir->cellName, tbuffer);
339 static int GetCellNT(struct afsconf_dir *adir)
341 if (IsClientConfigDirectory(adir->name)) {
342 /* NT client config dir; ThisCell is in registry (no file). */
343 return afssw_GetClientCellName(&adir->cellName);
345 /* NT server config dir; works just like Unix */
346 return GetCellUnix(adir);
349 #endif /* AFS_NT40_ENV */
352 static int afsconf_OpenInternal(adir, cell, clones)
353 register struct afsconf_dir *adir;
358 register char *tp, *bp;
359 register struct afsconf_entry *curEntry;
360 register afs_int32 code;
362 char tbuffer[256], tbuf1[256];
365 /* figure out the cell name */
369 i = GetCellUnix(adir);
372 #ifndef AFS_FREELANCE_CLIENT /* no local cell not fatal in freelance */
378 /* now parse the individual lines */
382 /* NT client/server have a CellServDB that is the same format as Unix.
383 * However, the NT client uses a different file name
385 if (IsClientConfigDirectory(adir->name)) {
386 /* NT client config dir */
387 strcompose(tbuffer, 256,
388 adir->name, "/", AFSDIR_CELLSERVDB_FILE_NTCLIENT, NULL);
390 /* NT server config dir */
391 strcompose(tbuffer, 256,
392 adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
395 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_CELLSERVDB_FILE, NULL);
396 #endif /* AFS_NT40_ENV */
398 if (!stat(tbuffer, &tstat)) {
399 adir->timeRead = tstat.st_mtime;
404 strcpy(tbuf1, tbuffer);
405 tf = fopen(tbuffer, "r");
410 tp = fgets(tbuffer, sizeof(tbuffer), tf);
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 */
418 /* thread this guy on the list */
419 curEntry->next = adir->entries;
420 adir->entries = curEntry;
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);
427 afsconf_CloseInternal(adir);
431 if (linkedcell[0] != '\0') {
432 curEntry->cellInfo.linkedCell =
433 (char *) malloc(strlen(linkedcell) + 1);
434 strcpy(curEntry->cellInfo.linkedCell, linkedcell);
438 /* new host in the current cell */
440 afsconf_CloseInternal(adir);
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]);
448 code = ParseHostLine(tbuffer, (char *) &curEntry->cellInfo.hostAddr[i], curEntry->cellInfo.hostName[i], 0);
450 if (code == AFSCONF_SYNTAX) {
451 for (bp=tbuffer; *bp != '\n'; bp++) { /* Take out the <cr> from the buffer */
455 fprintf(stderr, "Can't properly parse host line \"%s\" in configuration file %s\n", tbuffer, tbuf1);
459 afsconf_CloseInternal(adir);
462 curEntry->cellInfo.numServers = ++i;
465 fclose(tf); /* close the file now */
467 /* end the last partially-completed cell */
469 curEntry->next = adir->entries;
470 adir->entries = curEntry;
473 /* now read the fs keys, if possible */
474 adir->keystr = (struct afsconf_keys *) 0;
475 afsconf_IntGetKeys(adir);
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.
485 static ParseHostLine(aline, addr, aname, aclone)
487 register struct sockaddr_in *addr;
491 register afs_int32 code;
495 if (aclone) *aclone = 1;
496 code = sscanf(aline, "[%d.%d.%d.%d] #%s", &c1, &c2, &c3, &c4, aname);
498 if (aclone) *aclone = 0;
499 code = sscanf(aline, "%d.%d.%d.%d #%s", &c1, &c2, &c3, &c4, aname);
501 if (code != 5) return AFSCONF_SYNTAX;
502 addr->sin_family = AF_INET;
504 tp = (char *) &addr->sin_addr;
512 /* parse a line of the form
513 * ">cellname [linkedcellname] [#comments]"
514 * into the appropriate pieces.
516 static ParseCellLine(aline, aname, alname)
517 register char *aline, *aname, *alname; {
519 code = sscanf(aline, ">%s %s", aname, alname);
520 if (code == 1) *alname = '\0';
522 if (*alname == '#') {
526 return (code > 0 ? 0 : AFSCONF_SYNTAX);
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;
534 register struct afsconf_entry *tde;
535 register afs_int32 code;
537 for(tde=adir->entries; tde; tde=tde->next) {
538 code = (*aproc)(&tde->cellInfo, arock, adir);
548 afs_int32 afsconf_SawCell = 0;
550 afsconf_GetExtendedCellInfo(adir, acellName, aservice, acellInfo, clones)
551 struct afsconf_dir *adir;
554 struct afsconf_cell *acellInfo;
560 code = afsconf_GetCellInfo(adir, acellName, aservice, acellInfo);
567 cell = (char *) &acellInfo->name;
569 code = afsconf_OpenInternal(adir, cell, clones);
574 #if !defined(AFS_NT40_ENV)
575 afsconf_GetAfsdbInfo(acellName, aservice, acellInfo)
578 struct afsconf_cell *acellInfo;
583 unsigned char answer[1024];
585 char realCellName[256];
590 /* The resolver isn't always MT-safe.. Perhaps this ought to be
591 * replaced with a more fine-grained lock just for the resolver
595 len = res_search(acellName, C_IN, T_AFSDB, answer, sizeof(answer));
599 return AFSCONF_NOTFOUND;
601 p = answer + sizeof(HEADER); /* Skip header */
602 code = dn_expand(answer, answer + len, p, host, sizeof(host));
604 return AFSCONF_NOTFOUND;
606 p += code + QFIXEDSZ; /* Skip name */
608 while (p < answer + len) {
611 code = dn_expand(answer, answer + len, p, host, sizeof(host));
613 return AFSCONF_NOTFOUND;
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 */
623 if (type == T_AFSDB) {
627 afsdb_type = (p[0] << 8) | p[1];
628 if (afsdb_type == 1) {
630 * We know this is an AFSDB record for our cell, of the
631 * right AFSDB type. Write down the true cell name that
632 * the resolver gave us above.
634 strcpy(realCellName, host);
637 code = dn_expand(answer, answer+len, p+2, host, sizeof(host));
639 return AFSCONF_NOTFOUND;
641 if ((afsdb_type == 1) &&
642 (server_num < MAXHOSTSPERCELL) &&
643 /* Do we want to get TTL data for the A record as well? */
644 (he = gethostbyname(host))) {
646 memcpy(&ipaddr, he->h_addr, he->h_length);
647 acellInfo->hostAddr[server_num].sin_addr.s_addr = ipaddr;
648 strncpy(acellInfo->hostName[server_num], host,
649 sizeof(acellInfo->hostName[server_num]));
652 if (!minttl || ttl < minttl) minttl = ttl;
659 if (server_num == 0) /* No AFSDB records */
660 return AFSCONF_NOTFOUND;
661 strncpy(acellInfo->name, realCellName, sizeof(acellInfo->name));
662 acellInfo->numServers = server_num;
665 tservice = afsconf_FindService(aservice);
667 return AFSCONF_NOTFOUND; /* service not found */
668 for (i=0; i<acellInfo->numServers; i++) {
669 acellInfo->hostAddr[i].sin_port = tservice;
673 acellInfo->timeout = minttl ? (time(0) + minttl) : 0;
678 int afsconf_GetAfsdbInfo(acellName, aservice, acellInfo)
681 struct afsconf_cell *acellInfo;
683 register afs_int32 i;
685 struct afsconf_entry DNSce;
686 char *DNStmpStrp; /* a temp string pointer */
688 afs_int32 cellHosts[AFSMAXCELLHOSTS];
693 DNSce.cellInfo.numServers=0;
695 rc = getAFSServer(acellName, cellHosts, &numServers, &ttl);
696 /* ignore the ttl here since this code is only called by transitory programs
703 for (i = 0; i < numServers; i++)
705 memcpy(&acellInfo->hostAddr[i].sin_addr.s_addr, &cellHosts[i], sizeof(long));
706 acellInfo->hostAddr[i].sin_family = AF_INET;
708 /* sin_port supplied by connection code */
711 acellInfo->numServers = numServers;
712 strcpy(acellInfo->name, acellName);
715 tservice = afsconf_FindService(aservice);
718 return AFSCONF_NOTFOUND; /* service not found */
720 for(i=0; i< acellInfo->numServers; i++) {
721 acellInfo->hostAddr[i].sin_port = tservice;
724 acellInfo->linkedCell = NULL; /* no linked cell */
725 acellInfo->flags = 0;
729 #endif /* AFS_AFSDB_ENV */
731 afsconf_GetCellInfo(adir, acellName, aservice, acellInfo)
732 struct afsconf_dir *adir;
735 struct afsconf_cell *acellInfo; {
736 register struct afsconf_entry *tce;
737 struct afsconf_entry *bestce;
738 register afs_int32 i;
746 if (adir) afsconf_Check(adir);
749 cnLen = strlen(tcell)+1;
750 lcstring (tcell, tcell, cnLen);
751 afsconf_SawCell = 1; /* will ignore the AFSCELL switch on future */
752 /* call to afsconf_GetLocalCell: like klog */
754 i = afsconf_GetLocalCell(adir, tbuffer, sizeof(tbuffer));
761 cnLen = strlen(tcell);
762 bestce = (struct afsconf_entry *) 0;
768 for(tce=adir->entries;tce;tce=tce->next) {
769 if (strcasecmp(tce->cellInfo.name, tcell) == 0) {
775 if (strlen(tce->cellInfo.name) < cnLen) continue; /* clearly wrong */
776 if (strncasecmp(tce->cellInfo.name, tcell, cnLen) == 0) {
777 if (bestce) ambig = 1; /* ambiguous unless we get exact match */
781 if (!ambig && bestce) {
782 *acellInfo = bestce->cellInfo; /* structure assignment */
784 tservice = afsconf_FindService(aservice);
787 return AFSCONF_NOTFOUND; /* service not found */
789 for(i=0;i<acellInfo->numServers;i++) {
790 acellInfo->hostAddr[i].sin_port = tservice;
793 acellInfo->timeout = 0;
800 return afsconf_GetAfsdbInfo(acellName, aservice, acellInfo);
802 return AFSCONF_NOTFOUND;
803 #endif /* AFS_AFSDB_ENV */
807 afsconf_GetLocalCell(adir, aname, alen)
808 register struct afsconf_dir *adir;
811 static int afsconf_showcell = 0;
818 * If a cell switch was specified in a command, then it should override the
819 * AFSCELL variable. If a cell was specified, then the afsconf_SawCell flag
820 * is set and the cell name in the adir structure is used.
821 * Read the AFSCELL var each time: in case it changes (unsetenv AFSCELL).
823 if ( !afsconf_SawCell && (afscell_path= getenv("AFSCELL")) ) {
824 if ( !afsconf_showcell ) {
825 fprintf(stderr, "Note: Operation is performed on cell %s\n", afscell_path);
826 afsconf_showcell = 1;
828 strncpy(aname, afscell_path, alen);
831 if (adir->cellName) {
832 strncpy(aname, adir->cellName, alen);
834 else code = AFSCONF_UNKNOWN;
842 struct afsconf_dir *adir; {
844 afsconf_CloseInternal(adir);
845 if (adir->name) free(adir->name);
851 static int afsconf_CloseInternal(adir)
852 register struct afsconf_dir *adir; {
853 register struct afsconf_entry *td, *nd;
854 register char *tname;
856 tname = adir->name; /* remember name, since that's all we preserve */
858 /* free everything we can find */
859 if (adir->cellName) free(adir->cellName);
860 for(td=adir->entries;td;td=nd) {
862 if (td->cellInfo.linkedCell)
863 free(td->cellInfo.linkedCell);
866 if (adir->keystr) free(adir->keystr);
869 memset(adir, 0, sizeof(struct afsconf_dir));
870 adir->name = tname; /* restore it */
874 static int afsconf_Reopen(adir)
875 register struct afsconf_dir *adir; {
876 register afs_int32 code;
877 code = afsconf_CloseInternal(adir);
878 if (code) return code;
879 code = afsconf_OpenInternal(adir, 0, 0);
883 /* called during opening of config file */
884 afsconf_IntGetKeys(adir)
885 struct afsconf_dir *adir;
889 struct afsconf_keys *tstr;
890 register afs_int32 code;
893 /* NT client config dir has no KeyFile; don't risk attempting open
894 * because there might be a random file of this name if dir is shared.
896 if (IsClientConfigDirectory(adir->name)) {
897 adir->keystr = ((struct afsconf_keys *)
898 malloc(sizeof(struct afsconf_keys)));
899 adir->keystr->nkeys = 0;
902 #endif /* AFS_NT40_ENV */
905 /* compute the key name and other setup */
907 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
908 tstr = (struct afsconf_keys *) malloc(sizeof (struct afsconf_keys));
912 fd = open(tbuffer, O_RDONLY);
918 code = read(fd, tstr, sizeof(struct afsconf_keys));
920 if (code < sizeof(afs_int32)) {
926 /* convert key structure to host order */
927 tstr->nkeys = ntohl(tstr->nkeys);
928 for(fd=0;fd<tstr->nkeys;fd++)
929 tstr->key[fd].kvno = ntohl(tstr->key[fd].kvno);
935 /* get keys structure */
936 afsconf_GetKeys(adir, astr)
937 struct afsconf_dir *adir;
938 struct afsconf_keys *astr;
940 register afs_int32 code;
943 code = afsconf_Check(adir);
945 return AFSCONF_FAILURE;
946 memcpy(astr, adir->keystr, sizeof(struct afsconf_keys));
952 afs_int32 afsconf_GetLatestKey(adir, avno, akey)
953 IN struct afsconf_dir *adir;
959 register struct afsconf_key *tk;
960 register afs_int32 best;
961 struct afsconf_key *bestk;
962 register afs_int32 code;
965 code = afsconf_Check(adir);
967 return AFSCONF_FAILURE;
968 maxa = adir->keystr->nkeys;
970 best = -1; /* highest kvno we've seen yet */
971 bestk = (struct afsconf_key *) 0; /* ptr to structure providing best */
972 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
973 if (tk->kvno == 999) continue; /* skip bcrypt keys */
974 if (tk->kvno > best) {
979 if (bestk) { /* found any */
980 if (akey) memcpy(akey, bestk->key, 8); /* copy out latest key */
981 if (avno) *avno = bestk->kvno; /* and kvno to caller */
986 return AFSCONF_NOTFOUND; /* didn't find any keys */
989 /* get a particular key */
990 afsconf_GetKey(adir, avno, akey)
991 struct afsconf_dir *adir;
995 register int i, maxa;
996 register struct afsconf_key *tk;
997 register afs_int32 code;
1000 code = afsconf_Check(adir);
1002 return AFSCONF_FAILURE;
1003 maxa = adir->keystr->nkeys;
1005 for(tk = adir->keystr->key,i=0;i<maxa;i++,tk++) {
1006 if (tk->kvno == avno) {
1007 memcpy(akey, tk->key, 8);
1014 return AFSCONF_NOTFOUND;
1017 /* save the key structure in the appropriate file */
1018 static SaveKeys(adir)
1019 struct afsconf_dir *adir;
1021 struct afsconf_keys tkeys;
1023 register afs_int32 i;
1026 memcpy(&tkeys, adir->keystr, sizeof(struct afsconf_keys));
1028 /* convert it to net byte order */
1029 for(i = 0; i<tkeys.nkeys; i++ )
1030 tkeys.key[i].kvno = htonl(tkeys.key[i].kvno);
1031 tkeys.nkeys = htonl(tkeys.nkeys);
1033 /* rewrite keys file */
1034 strcompose(tbuffer, 256, adir->name, "/", AFSDIR_KEY_FILE, NULL);
1035 fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0600);
1036 if (fd < 0) return AFSCONF_FAILURE;
1037 i = write(fd, &tkeys, sizeof(tkeys));
1038 if (i != sizeof(tkeys)) {
1040 return AFSCONF_FAILURE;
1042 if (close(fd) < 0) return AFSCONF_FAILURE;
1046 afsconf_AddKey(adir, akvno, akey, overwrite)
1047 struct afsconf_dir *adir;
1048 afs_int32 akvno, overwrite;
1051 register struct afsconf_keys *tk;
1052 register struct afsconf_key *tkey;
1053 register afs_int32 i;
1060 if (akvno < 0 || akvno > 255) {
1066 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1067 if (tkey->kvno == akvno) {
1070 return AFSCONF_KEYINUSE;
1077 if (tk->nkeys >= AFSCONF_MAXKEYS) {
1079 return AFSCONF_FULL;
1081 tkey = &tk->key[tk->nkeys++];
1084 memcpy(tkey->key, akey, 8);
1086 afsconf_Touch(adir);
1091 /* this proc works by sliding the other guys down, rather than using a funny
1092 kvno value, so that callers can count on getting a good key in key[0].
1094 afsconf_DeleteKey(adir, akvno)
1095 struct afsconf_dir *adir;
1098 register struct afsconf_keys *tk;
1099 register struct afsconf_key *tkey;
1106 for(i=0, tkey = tk->key; i<tk->nkeys; i++, tkey++) {
1107 if (tkey->kvno == akvno) {
1114 return AFSCONF_NOTFOUND;
1117 /* otherwise slide the others down. i and tkey point at the guy to delete */
1118 for(;i<tk->nkeys-1; i++,tkey++) {
1119 tkey->kvno = (tkey+1)->kvno;
1120 memcpy(tkey->key, (tkey+1)->key, 8);
1124 afsconf_Touch(adir);