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