reindent-20030715
[openafs.git] / src / ntp / mkntpconf.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
14     ("$Header$");
15
16 #include <sys/param.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <ctype.h>
21 #include <netdb.h>
22 #include <stdio.h>
23 #include <strings.h>
24 #include <sysexits.h>
25
26 #ifdef vax
27 #define PRECISION       -7      /* HZ == 100 */
28 #endif
29 #ifdef sun
30 #define PRECISION       -6      /* HZ == 50 */
31 #endif
32 #ifdef romp
33 #define PRECISION       -6      /* HZ == 64 */
34 #endif
35 #ifdef multimax
36 #define PRECISION       -3      /* HZ == 10 */
37 #endif
38
39 struct server {
40     char *s_name;
41     char *s_addr;
42 };
43
44 /*
45  * primary servers
46  */
47
48 static struct server dcn5 = { "dcn5.udel.edu", "128.4.0.5" };
49 static struct server wwvb = { "wwvb.isi.edu", "128.9.2.129" };
50 static struct server sdsc = { "sdsc-fuzz.nsf.net", "192.12.207.1" };
51 static struct server umd1 = { "umd1.umd.edu", "128.8.10.1" };
52
53 /*
54  * secondary servers
55  */
56
57 static struct server papaya = { "papaya.srv.cs.cmu.edu", "128.2.222.199" };
58 static struct server guava = { "guava.srv.cs.cmu.edu", "128.2.250.187" };
59 static struct server cluster1 =
60     { "cluster1.fs.andrew.cmu.edu", "128.2.249.123" };
61
62 static char *name;
63
64 static char *headers1[] = {
65     "",
66     "  DO NOT EDIT THIS FILE MANUALLY.  It is maintained by mkntpconf.",
67     "",
68     "           Local clock parameters",
69     "",
70     "   Precision of the local clock to the nearest power of 2",
71     "           ex.",
72     "                   60-HZ   = 2**-6",
73     "                   100-HZ  = 2**-7",
74     "                   1000-HZ = 2**-10",
75     0
76 };
77
78 static char *headers2[] = {
79     "",
80     "   Peers",
81     "",
82     0
83 };
84
85 extern char *mktemp();
86 void peerline();
87
88
89 main(ac, av)
90      int ac;
91      char **av;
92 {
93     register char *p, **v;
94     register FILE *f, *g;
95     char hostname[MAXHOSTNAMELEN + 1];
96     char tempfn[MAXPATHLEN + 1], config[MAXPATHLEN + 1];
97     char line[BUFSIZ];
98     struct server serv;
99
100     name = (ac > 0) ? (ac--, *av++) : (ac = 0, "mkntpconf");
101     (void)strcpy(tempfn, name);
102     if ((p = strrchr(tempfn, '/')) == 0)
103         p = tempfn;
104     else
105         p += 1;
106     *p = 0;
107     (void)strcpy(config, tempfn);
108     (void)strcat(config, "ntp.conf");
109     (void)strcat(tempfn, "ntp.XXXXXX");
110     (void)mktemp(tempfn);
111     if (gethostname(hostname, MAXHOSTNAMELEN) == -1) {
112         perror("gethostname");
113         exit(EX_OSERR);
114     }
115     hostname[MAXHOSTNAMELEN] = 0;
116     for (p = hostname; *p; p++)
117         if (isupper(*p))
118             *p = tolower(*p);
119     if ((f = fopen(tempfn, "w")) == NULL) {
120         perror(tempfn);
121         exit(EX_OSERR);
122     }
123
124     for (v = headers1; *v; v++)
125         fprintf(f, "#%s\n", *v);
126     fprintf(f, "precision %d\n", PRECISION);
127     for (v = headers2; *v; v++)
128         fprintf(f, "#%s\n", *v);
129     if (strcmp(hostname, papaya.s_name) == 0
130         || strcmp(hostname, guava.s_name) == 0) {
131         if (strcmp(hostname, papaya.s_name) == 0) {
132             peerline(dcn5, f);
133             peerline(wwvb, f);
134         } else {
135             peerline(sdsc, f);
136             peerline(umd1, f);
137         }
138         peerline(papaya, f);
139         peerline(guava, f);
140         peerline(cluster1, f);
141     } else {
142         if ((p = strchr(hostname, '.')) != 0
143             && strcmp(p + 1, "srv.cs.cmu.edu") == 0) {
144             peerline(papaya, f);
145             peerline(guava, f);
146         }
147         serv.s_name = line;
148         serv.s_addr = 0;
149         if ((g = fopen("/etc/attributes", "r")) != NULL) {
150             while (fgets(line, sizeof(line), g) != NULL) {
151                 if ((p = strchr(line, ':')) != 0)
152                     *p = 0;
153                 if (strcmp(line, papaya.s_name) == 0
154                     || strcmp(line, guava.s_name) == 0)
155                     continue;
156                 if ((p = strchr(line, '.')) == 0
157                     || strcmp(p + 1, "srv.cs.cmu.edu") != 0)
158                     continue;
159                 peerline(serv, f);
160             }
161             (void)fclose(g);
162         }
163     }
164
165     (void)fclose(f);
166     if (rename(tempfn, config) == -1) {
167         perror("rename");
168         exit(EX_OSERR);
169     }
170     if (chmod(config, 0644) == -1) {
171         perror("chmod");
172         exit(EX_OSERR);
173     }
174     exit(EX_OK);
175 }
176
177
178 void
179 peerline(s, f)
180      struct server s;
181      register FILE *f;
182 {
183     register struct hostent *hp;
184
185     fprintf(f, "peer ");
186     if ((hp = gethostbyname(s.s_name)) != 0 && hp->h_addrtype == AF_INET)
187         fprintf(f, "%-15.15s # %s", inet_ntoa(*(struct in_addr *)hp->h_addr),
188                 s.s_name);
189     else if (s.s_addr)
190         fprintf(f, "%-15.15s # %s", s.s_addr, s.s_name);
191     else
192         fprintf(f, "%s", s.s_name);
193     (void)fputc('\n', f);
194 }