mkalias.c revision 1.1 1 1.1 lukem /* $OpenBSD: mkalias.c,v 1.3 1997/07/28 22:48:01 deraadt Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.1 lukem * Copyright (c) 1997 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted provided that the following conditions
9 1.1 lukem * are met:
10 1.1 lukem * 1. Redistributions of source code must retain the above copyright
11 1.1 lukem * notice, this list of conditions and the following disclaimer.
12 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer in the
14 1.1 lukem * documentation and/or other materials provided with the distribution.
15 1.1 lukem * 3. All advertising materials mentioning features or use of this software
16 1.1 lukem * must display the following acknowledgement:
17 1.1 lukem * This product includes software developed by Mats O Jansson
18 1.1 lukem * 4. The name of the author may not be used to endorse or promote products
19 1.1 lukem * derived from this software without specific prior written permission.
20 1.1 lukem *
21 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 lukem * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 lukem * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 lukem * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 lukem * SUCH DAMAGE.
32 1.1 lukem */
33 1.1 lukem
34 1.1 lukem #ifndef LINT
35 1.1 lukem static char rcsid[] = "$OpenBSD: mkalias.c,v 1.3 1997/07/28 22:48:01 deraadt Exp $";
36 1.1 lukem #endif
37 1.1 lukem
38 1.1 lukem #include <ctype.h>
39 1.1 lukem #include <fcntl.h>
40 1.1 lukem #include <netdb.h>
41 1.1 lukem #include <stdio.h>
42 1.1 lukem #include <string.h>
43 1.1 lukem #include <unistd.h>
44 1.1 lukem #include <sys/param.h>
45 1.1 lukem #include <sys/types.h>
46 1.1 lukem #include <netinet/in.h>
47 1.1 lukem #include <arpa/nameser.h>
48 1.1 lukem #include <resolv.h>
49 1.1 lukem #include "ypdb.h"
50 1.1 lukem #include "ypdef.h"
51 1.1 lukem
52 1.1 lukem extern char *__progname; /* from crt0.o */
53 1.1 lukem
54 1.1 lukem void
55 1.1 lukem split_address(address, len, user, host)
56 1.1 lukem char *address;
57 1.1 lukem int len;
58 1.1 lukem char *user, *host;
59 1.1 lukem {
60 1.1 lukem char *c,*s,*r;
61 1.1 lukem int i = 0;
62 1.1 lukem
63 1.1 lukem if (index(address,'@')) {
64 1.1 lukem
65 1.1 lukem s = user;
66 1.1 lukem
67 1.1 lukem for(c = address; i < len; i++) {
68 1.1 lukem if (*c == '@') {
69 1.1 lukem *s = '\0';
70 1.1 lukem s = host;
71 1.1 lukem } else {
72 1.1 lukem *s++ = *c;
73 1.1 lukem }
74 1.1 lukem c++;
75 1.1 lukem }
76 1.1 lukem *s = '\0';
77 1.1 lukem
78 1.1 lukem }
79 1.1 lukem
80 1.1 lukem if (r = rindex(address,'!')) {
81 1.1 lukem
82 1.1 lukem s = host;
83 1.1 lukem
84 1.1 lukem for(c = address; i < len; i++) {
85 1.1 lukem if (c == r) {
86 1.1 lukem *s = '\0';
87 1.1 lukem s = user;
88 1.1 lukem } else {
89 1.1 lukem *s++ = *c;
90 1.1 lukem }
91 1.1 lukem c++;
92 1.1 lukem }
93 1.1 lukem *s = '\0';
94 1.1 lukem
95 1.1 lukem }
96 1.1 lukem
97 1.1 lukem }
98 1.1 lukem
99 1.1 lukem int
100 1.1 lukem check_host(address, host, dflag, uflag, Eflag)
101 1.1 lukem char *address, *host;
102 1.1 lukem int dflag, uflag, Eflag;
103 1.1 lukem {
104 1.1 lukem char answer[PACKETSZ];
105 1.1 lukem int status;
106 1.1 lukem
107 1.1 lukem if ((dflag && index(address,'@')) ||
108 1.1 lukem (uflag && index(address,'!'))) return(0);
109 1.1 lukem
110 1.1 lukem if ((_res.options & RES_INIT) == 0)
111 1.1 lukem res_init();
112 1.1 lukem
113 1.1 lukem status = res_search(host, C_IN, T_AAAA, answer, sizeof(answer));
114 1.1 lukem
115 1.1 lukem if (status == -1)
116 1.1 lukem status = res_search(host, C_IN, T_A, answer, sizeof(answer));
117 1.1 lukem
118 1.1 lukem if ((status == -1) && Eflag)
119 1.1 lukem status = res_search(host, C_IN, T_MX, answer, sizeof(answer));
120 1.1 lukem
121 1.1 lukem return(status == -1);
122 1.1 lukem }
123 1.1 lukem
124 1.1 lukem void
125 1.1 lukem capitalize(name,len)
126 1.1 lukem char *name;
127 1.1 lukem int len;
128 1.1 lukem {
129 1.1 lukem char last = ' ';
130 1.1 lukem char *c;
131 1.1 lukem int i = 0;
132 1.1 lukem
133 1.1 lukem for(c = name; i < len; i++) {
134 1.1 lukem if (*c == '.') last = '.';
135 1.1 lukem c++;
136 1.1 lukem }
137 1.1 lukem
138 1.1 lukem i = 0;
139 1.1 lukem if (last == '.') {
140 1.1 lukem for(c = name; i < len; i++) {
141 1.1 lukem if (last == '.') {
142 1.1 lukem *c = toupper(*c);
143 1.1 lukem }
144 1.1 lukem last = *c++;
145 1.1 lukem }
146 1.1 lukem }
147 1.1 lukem
148 1.1 lukem }
149 1.1 lukem
150 1.1 lukem int
151 1.1 lukem main (argc,argv)
152 1.1 lukem int argc;
153 1.1 lukem char *argv[];
154 1.1 lukem {
155 1.1 lukem int usage = 0;
156 1.1 lukem int eflag = 0;
157 1.1 lukem int dflag = 0;
158 1.1 lukem int nflag = 0;
159 1.1 lukem int sflag = 0;
160 1.1 lukem int uflag = 0;
161 1.1 lukem int vflag = 0;
162 1.1 lukem int Eflag = 0;
163 1.1 lukem int ch;
164 1.1 lukem char *input = NULL;
165 1.1 lukem char *output = NULL;
166 1.1 lukem DBM *db;
167 1.1 lukem datum key,val;
168 1.1 lukem char *slash;
169 1.1 lukem DBM *new_db = NULL;
170 1.1 lukem static char mapname[] = "ypdbXXXXXXXXXX";
171 1.1 lukem char db_mapname[MAXPATHLEN],db_outfile[MAXPATHLEN],
172 1.1 lukem db_tempname[MAXPATHLEN];
173 1.1 lukem int status;
174 1.1 lukem char user[4096],host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
175 1.1 lukem char datestr[11];
176 1.1 lukem char myname[MAXHOSTNAMELEN];
177 1.1 lukem
178 1.1 lukem while ((ch = getopt(argc, argv, "Edensuv")) != -1)
179 1.1 lukem switch(ch) {
180 1.1 lukem case 'E':
181 1.1 lukem eflag++; /* Check hostname */
182 1.1 lukem Eflag++; /* .. even check MX records */
183 1.1 lukem break;
184 1.1 lukem case 'd':
185 1.1 lukem dflag++; /* Don't check DNS hostname */
186 1.1 lukem break;
187 1.1 lukem case 'e':
188 1.1 lukem eflag++; /* Check hostname */
189 1.1 lukem break;
190 1.1 lukem case 'n':
191 1.1 lukem nflag++; /* Capitalize name parts */
192 1.1 lukem break;
193 1.1 lukem case 's':
194 1.1 lukem sflag++; /* Don't know... */
195 1.1 lukem break;
196 1.1 lukem case 'u':
197 1.1 lukem uflag++; /* Don't check UUCP hostname */
198 1.1 lukem break;
199 1.1 lukem case 'v':
200 1.1 lukem vflag++; /* Verbose */
201 1.1 lukem break;
202 1.1 lukem default:
203 1.1 lukem usage++;
204 1.1 lukem break;
205 1.1 lukem }
206 1.1 lukem
207 1.1 lukem if (optind == argc) {
208 1.1 lukem usage++;
209 1.1 lukem } else {
210 1.1 lukem input = argv[optind++];
211 1.1 lukem if (optind < argc)
212 1.1 lukem output = argv[optind++];
213 1.1 lukem if (optind < argc)
214 1.1 lukem usage++;
215 1.1 lukem }
216 1.1 lukem
217 1.1 lukem if (usage) {
218 1.1 lukem fprintf(stderr,
219 1.1 lukem "usage: %s [-v] [-e|-E [-d] [-u]] [-n] input [output]\n",
220 1.1 lukem __progname);
221 1.1 lukem exit(1);
222 1.1 lukem }
223 1.1 lukem
224 1.1 lukem db = ypdb_open(input, O_RDONLY, 0444);
225 1.1 lukem if (db == NULL) {
226 1.1 lukem fprintf(stderr,
227 1.1 lukem "%s: Unable to open input database %s\n",
228 1.1 lukem __progname,
229 1.1 lukem input);
230 1.1 lukem exit(1);
231 1.1 lukem }
232 1.1 lukem
233 1.1 lukem if (output != NULL) {
234 1.1 lukem if (strlen(output) + strlen(YPDB_SUFFIX) > MAXPATHLEN) {
235 1.1 lukem fprintf(stderr,"%s: %s: file name too long\n",
236 1.1 lukem __progname, output);
237 1.1 lukem }
238 1.1 lukem snprintf(db_outfile, sizeof(db_outfile),
239 1.1 lukem "%s%s", output, YPDB_SUFFIX);
240 1.1 lukem
241 1.1 lukem slash = strrchr(output, '/');
242 1.1 lukem if (slash != NULL)
243 1.1 lukem slash[1] = 0; /* truncate to dir */
244 1.1 lukem else
245 1.1 lukem *output = 0; /* elminate */
246 1.1 lukem
247 1.1 lukem /* note: output is now directory where map goes ! */
248 1.1 lukem
249 1.1 lukem if (strlen(output) + strlen(mapname)
250 1.1 lukem + strlen(YPDB_SUFFIX) > MAXPATHLEN) {
251 1.1 lukem fprintf(stderr,"%s: %s: directory name too long\n",
252 1.1 lukem __progname, output);
253 1.1 lukem exit(1);
254 1.1 lukem }
255 1.1 lukem
256 1.1 lukem snprintf(db_tempname, sizeof(db_tempname), "%s%s", output,
257 1.1 lukem mapname);
258 1.1 lukem mktemp(db_tempname);
259 1.1 lukem snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname,
260 1.1 lukem YPDB_SUFFIX);
261 1.1 lukem
262 1.1 lukem new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444);
263 1.1 lukem if (new_db == NULL) {
264 1.1 lukem fprintf(stderr,
265 1.1 lukem "%s: Unable to open output database %s\n",
266 1.1 lukem __progname,
267 1.1 lukem db_outfile);
268 1.1 lukem exit(1);
269 1.1 lukem }
270 1.1 lukem }
271 1.1 lukem
272 1.1 lukem for (key = ypdb_firstkey(db);
273 1.1 lukem key.dptr != NULL;
274 1.1 lukem key = ypdb_nextkey(db)) {
275 1.1 lukem
276 1.1 lukem val = ypdb_fetch(db,key);
277 1.1 lukem
278 1.1 lukem if (val.dptr == NULL) continue; /* No value */
279 1.1 lukem if ((*key.dptr == '@') && (key.dsize == 1))
280 1.1 lukem continue; /* Sendmail token */
281 1.1 lukem if (strncmp(key.dptr, "YP_", 3)==0) /* YP token */
282 1.1 lukem continue;
283 1.1 lukem if (index(val.dptr,',')) continue; /* List... */
284 1.1 lukem if (index(val.dptr,'|')) continue; /* Pipe... */
285 1.1 lukem
286 1.1 lukem if (!((index(val.dptr,'@')) ||
287 1.1 lukem (index(val.dptr,'!')))) continue; /* Skip local users */
288 1.1 lukem
289 1.1 lukem split_address(val.dptr,val.dsize,user,host);
290 1.1 lukem
291 1.1 lukem if (eflag && check_host(val.dptr, host, dflag, uflag, Eflag)) {
292 1.1 lukem printf("Invalid host %s in %*.*s:%*.*s\n",
293 1.1 lukem host,
294 1.1 lukem key.dsize, key.dsize, key.dptr,
295 1.1 lukem val.dsize, val.dsize, val.dptr);
296 1.1 lukem continue;
297 1.1 lukem }
298 1.1 lukem
299 1.1 lukem if (nflag) {
300 1.1 lukem capitalize(key.dptr,key.dsize);
301 1.1 lukem }
302 1.1 lukem
303 1.1 lukem if (new_db != NULL) {
304 1.1 lukem status = ypdb_store(new_db, val, key, YPDB_INSERT);
305 1.1 lukem if (status != 0) {
306 1.1 lukem printf("%s: problem storing %*.*s %*.*s\n",
307 1.1 lukem __progname,
308 1.1 lukem val.dsize, val.dsize, val.dptr,
309 1.1 lukem key.dsize, key.dsize, key.dptr);
310 1.1 lukem }
311 1.1 lukem }
312 1.1 lukem
313 1.1 lukem if (vflag) {
314 1.1 lukem printf("%*.*s --> %*.*s\n",
315 1.1 lukem val.dsize, val.dsize, val.dptr,
316 1.1 lukem key.dsize, key.dsize, key.dptr);
317 1.1 lukem }
318 1.1 lukem
319 1.1 lukem }
320 1.1 lukem
321 1.1 lukem if (new_db != NULL) {
322 1.1 lukem sprintf(datestr, "%010d", time(NULL));
323 1.1 lukem key.dptr = YP_LAST_KEY;
324 1.1 lukem key.dsize = strlen(YP_LAST_KEY);
325 1.1 lukem val.dptr = datestr;
326 1.1 lukem val.dsize = strlen(datestr);
327 1.1 lukem status = ypdb_store(new_db, key, val, YPDB_INSERT);
328 1.1 lukem if (status != 0) {
329 1.1 lukem printf("%s: problem storing %*.*s %*.*s\n",
330 1.1 lukem __progname,
331 1.1 lukem key.dsize, key.dsize, key.dptr,
332 1.1 lukem val.dsize, val.dsize, val.dptr);
333 1.1 lukem }
334 1.1 lukem }
335 1.1 lukem
336 1.1 lukem if (new_db != NULL) {
337 1.1 lukem gethostname(myname, sizeof(myname) - 1);
338 1.1 lukem key.dptr = YP_MASTER_KEY;
339 1.1 lukem key.dsize = strlen(YP_MASTER_KEY);
340 1.1 lukem val.dptr = myname;
341 1.1 lukem val.dsize = strlen(myname);
342 1.1 lukem status = ypdb_store(new_db, key, val, YPDB_INSERT);
343 1.1 lukem if (status != 0) {
344 1.1 lukem printf("%s: problem storing %*.*s %*.*s\n",
345 1.1 lukem __progname,
346 1.1 lukem key.dsize, key.dsize, key.dptr,
347 1.1 lukem val.dsize, val.dsize, val.dptr);
348 1.1 lukem }
349 1.1 lukem }
350 1.1 lukem
351 1.1 lukem
352 1.1 lukem
353 1.1 lukem ypdb_close(db);
354 1.1 lukem
355 1.1 lukem if (new_db != NULL) {
356 1.1 lukem ypdb_close(new_db);
357 1.1 lukem if (rename(db_mapname,db_outfile) < 0) {
358 1.1 lukem perror("rename");
359 1.1 lukem fprintf(stderr,"rename %s -> %s failed!\n", db_mapname,
360 1.1 lukem db_outfile);
361 1.1 lukem exit(1);
362 1.1 lukem }
363 1.1 lukem }
364 1.1 lukem
365 1.1 lukem return(0);
366 1.1 lukem
367 1.1 lukem }
368