Standardize License information
[openafs.git] / src / auth / writeconfig.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 <afs/param.h>
11 #include <afs/pthread_glock.h>
12 #include <afs/afsutil.h>
13 #include <sys/types.h>
14 #ifdef AFS_SUN5_ENV 
15 #include <fcntl.h>
16 #endif
17 #ifdef AFS_NT40_ENV
18 #include <winsock2.h>
19 #include <fcntl.h>
20 #include <io.h>
21 #else
22 #include <sys/file.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netdb.h>
26 #endif
27 #include <stdio.h>
28 #include <errno.h>
29 #include "cellconfig.h"
30 #include "keys.h"
31
32 /* write ThisCell and CellServDB containing exactly one cell's info specified
33     by acellInfo parm.   Useful only on the server (which describes only one cell).
34 */
35
36 static VerifyEntries(aci)
37 register struct afsconf_cell *aci; {
38     register int i;
39     register struct hostent *th;
40     
41     for(i=0;i<aci->numServers;i++) {
42         if (aci->hostAddr[i].sin_addr.s_addr == 0) {
43             /* no address spec'd */
44             if (*(aci->hostName[i]) != 0) {
45                 th = gethostbyname(aci->hostName[i]);
46                 if (!th) {
47                         printf("Host %s not found in host database...\n", aci->hostName[i]);
48                         return AFSCONF_FAILURE;
49                 }
50                 bcopy(th->h_addr, &aci->hostAddr[i].sin_addr, sizeof(afs_int32));
51             }
52             /* otherwise we're deleting this entry */
53         }
54         else {
55             /* address spec'd, perhaps no name known */
56             if (aci->hostName[i][0] != 0) continue; /* name known too */
57             /* figure out name, if possible */
58             th = gethostbyaddr((char *)(&aci->hostAddr[i].sin_addr), 4, AF_INET);
59             if (!th) {
60                 strcpy(aci->hostName[i], "UNKNOWNHOST");
61             }
62             else {
63                 strcpy(aci->hostName[i], th->h_name);
64             }
65         }
66     }
67     return 0;
68 }
69
70 /* Changed the interface to accept the afsconf_dir datastructure.
71    This is a handle to the internal cache that is maintained by the bosserver.
72    */
73    
74 afsconf_SetCellInfo(adir, apath, acellInfo)
75 struct afsconf_dir *adir;
76 char *apath;
77 struct afsconf_cell *acellInfo; {
78     register afs_int32 code;
79     register int fd;
80     char tbuffer[1024];
81     register FILE *tf;
82     register afs_int32 i;
83     
84     LOCK_GLOBAL_MUTEX
85     /* write ThisCell file */
86     strcompose(tbuffer, 1024, apath, "/", AFSDIR_THISCELL_FILE, NULL);
87
88     fd = open(tbuffer, O_RDWR | O_CREAT | O_TRUNC, 0666);
89     if (fd < 0) {
90         UNLOCK_GLOBAL_MUTEX
91         return errno;
92     }
93     i = strlen(acellInfo->name);
94     code = write(fd, acellInfo->name, i);
95     if (code != i) {
96         UNLOCK_GLOBAL_MUTEX
97         return AFSCONF_FAILURE;
98     }
99     if (close(fd) < 0) {
100         UNLOCK_GLOBAL_MUTEX
101         return errno;
102     }
103     
104     /* make sure we have both name and address for each host, looking up other
105         if need be */
106     code = VerifyEntries(acellInfo);
107     if (code) {
108         UNLOCK_GLOBAL_MUTEX
109         return code;
110     }
111
112     /* write CellServDB */
113     strcompose(tbuffer, 1024, apath, "/", AFSDIR_CELLSERVDB_FILE, NULL);
114     tf = fopen(tbuffer, "w");
115     if (!tf) {
116         UNLOCK_GLOBAL_MUTEX
117         return AFSCONF_NOTFOUND;
118     }
119     fprintf(tf, ">%s    #Cell name\n", acellInfo->name);
120     for(i=0;i < acellInfo->numServers; i++) {
121         code = acellInfo->hostAddr[i].sin_addr.s_addr;  /* net order */
122         if (code == 0) continue;    /* delete request */
123         code = ntohl(code);     /* convert to host order */
124         fprintf(tf, "%d.%d.%d.%d    #%s\n", (code>>24) & 0xff, (code>>16)&0xff, (code>>8)&0xff, code&0xff, acellInfo->hostName[i]);
125     }
126     if (ferror(tf)) {
127         fclose(tf);
128         UNLOCK_GLOBAL_MUTEX
129         return AFSCONF_FAILURE;
130     }
131     code = fclose(tf);
132
133     /* Reset the timestamp in the cache, so that
134        the CellServDB is read into the cache next time.
135        Resolves the lost update problem due to an inconsistent cache
136        */
137     if (adir) adir->timeRead = 0;
138
139     UNLOCK_GLOBAL_MUTEX
140     if (code == EOF) return AFSCONF_FAILURE;
141     return 0;
142 }