mkalias.c revision 1.4 1 1.4 lukem /* $NetBSD: mkalias.c,v 1.4 1997/10/13 03:48:49 lukem 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.2 lukem #include <sys/cdefs.h>
35 1.4 lukem #ifndef lint
36 1.4 lukem __RCSID("$NetBSD: mkalias.c,v 1.4 1997/10/13 03:48:49 lukem Exp $");
37 1.1 lukem #endif
38 1.1 lukem
39 1.2 lukem #include <sys/types.h>
40 1.2 lukem #include <sys/param.h>
41 1.2 lukem #include <netinet/in.h>
42 1.2 lukem #include <arpa/nameser.h>
43 1.2 lukem
44 1.1 lukem #include <ctype.h>
45 1.2 lukem #include <err.h>
46 1.1 lukem #include <fcntl.h>
47 1.1 lukem #include <netdb.h>
48 1.2 lukem #include <resolv.h>
49 1.1 lukem #include <stdio.h>
50 1.1 lukem #include <string.h>
51 1.1 lukem #include <unistd.h>
52 1.2 lukem
53 1.2 lukem #include <rpc/rpc.h>
54 1.2 lukem
55 1.1 lukem #include "ypdb.h"
56 1.1 lukem #include "ypdef.h"
57 1.1 lukem
58 1.2 lukem void capitalize __P((char *, int));
59 1.2 lukem int check_host __P((char *, char *, int, int, int));
60 1.2 lukem int main __P((int, char *[]));
61 1.2 lukem void split_address __P((char *, int, char *, char *));
62 1.2 lukem void usage __P((void));
63 1.2 lukem
64 1.1 lukem extern char *__progname; /* from crt0.o */
65 1.1 lukem
66 1.1 lukem void
67 1.1 lukem split_address(address, len, user, host)
68 1.2 lukem char *address;
69 1.2 lukem int len;
70 1.2 lukem char *user, *host;
71 1.1 lukem {
72 1.2 lukem char *c, *s, *r;
73 1.1 lukem int i = 0;
74 1.1 lukem
75 1.2 lukem if ((strchr(address, '@')) != NULL) {
76 1.1 lukem
77 1.1 lukem s = user;
78 1.1 lukem
79 1.1 lukem for(c = address; i < len; i++) {
80 1.1 lukem if (*c == '@') {
81 1.1 lukem *s = '\0';
82 1.1 lukem s = host;
83 1.2 lukem } else
84 1.1 lukem *s++ = *c;
85 1.1 lukem c++;
86 1.1 lukem }
87 1.1 lukem *s = '\0';
88 1.1 lukem }
89 1.1 lukem
90 1.2 lukem if ((r = strrchr(address, '!')) != NULL) {
91 1.1 lukem
92 1.1 lukem s = host;
93 1.1 lukem
94 1.1 lukem for(c = address; i < len; i++) {
95 1.1 lukem if (c == r) {
96 1.1 lukem *s = '\0';
97 1.1 lukem s = user;
98 1.2 lukem } else
99 1.1 lukem *s++ = *c;
100 1.1 lukem c++;
101 1.1 lukem }
102 1.1 lukem *s = '\0';
103 1.1 lukem }
104 1.1 lukem }
105 1.1 lukem
106 1.1 lukem int
107 1.1 lukem check_host(address, host, dflag, uflag, Eflag)
108 1.2 lukem char *address, *host;
109 1.2 lukem int dflag, uflag, Eflag;
110 1.1 lukem {
111 1.1 lukem char answer[PACKETSZ];
112 1.1 lukem int status;
113 1.1 lukem
114 1.2 lukem if ((dflag && strchr(address, '@')) ||
115 1.2 lukem (uflag && strchr(address, '!')))
116 1.2 lukem return(0);
117 1.1 lukem
118 1.1 lukem if ((_res.options & RES_INIT) == 0)
119 1.1 lukem res_init();
120 1.1 lukem
121 1.1 lukem status = res_search(host, C_IN, T_AAAA, answer, sizeof(answer));
122 1.1 lukem
123 1.1 lukem if (status == -1)
124 1.1 lukem status = res_search(host, C_IN, T_A, answer, sizeof(answer));
125 1.1 lukem
126 1.1 lukem if ((status == -1) && Eflag)
127 1.1 lukem status = res_search(host, C_IN, T_MX, answer, sizeof(answer));
128 1.1 lukem
129 1.1 lukem return(status == -1);
130 1.1 lukem }
131 1.1 lukem
132 1.1 lukem void
133 1.2 lukem capitalize(name, len)
134 1.2 lukem char *name;
135 1.2 lukem int len;
136 1.1 lukem {
137 1.1 lukem char last = ' ';
138 1.1 lukem char *c;
139 1.1 lukem int i = 0;
140 1.1 lukem
141 1.1 lukem for(c = name; i < len; i++) {
142 1.1 lukem if (*c == '.') last = '.';
143 1.1 lukem c++;
144 1.1 lukem }
145 1.1 lukem
146 1.1 lukem i = 0;
147 1.1 lukem if (last == '.') {
148 1.1 lukem for(c = name; i < len; i++) {
149 1.2 lukem if (last == '.')
150 1.1 lukem *c = toupper(*c);
151 1.1 lukem last = *c++;
152 1.1 lukem }
153 1.1 lukem }
154 1.1 lukem }
155 1.1 lukem
156 1.1 lukem int
157 1.2 lukem main(argc, argv)
158 1.2 lukem int argc;
159 1.2 lukem char *argv[];
160 1.1 lukem {
161 1.1 lukem int eflag = 0;
162 1.1 lukem int dflag = 0;
163 1.1 lukem int nflag = 0;
164 1.1 lukem int sflag = 0;
165 1.1 lukem int uflag = 0;
166 1.1 lukem int vflag = 0;
167 1.1 lukem int Eflag = 0;
168 1.1 lukem int ch;
169 1.1 lukem char *input = NULL;
170 1.1 lukem char *output = NULL;
171 1.1 lukem DBM *db;
172 1.2 lukem datum key, val;
173 1.1 lukem char *slash;
174 1.1 lukem DBM *new_db = NULL;
175 1.1 lukem static char mapname[] = "ypdbXXXXXXXXXX";
176 1.2 lukem char db_mapname[MAXPATHLEN], db_outfile[MAXPATHLEN],
177 1.1 lukem db_tempname[MAXPATHLEN];
178 1.1 lukem int status;
179 1.2 lukem char user[4096], host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
180 1.1 lukem char datestr[11];
181 1.1 lukem char myname[MAXHOSTNAMELEN];
182 1.1 lukem
183 1.2 lukem while ((ch = getopt(argc, argv, "Edensuv")) != -1) {
184 1.2 lukem switch(ch) {
185 1.2 lukem case 'd':
186 1.2 lukem dflag++; /* Don't check DNS hostname */
187 1.2 lukem break;
188 1.2 lukem
189 1.2 lukem case 'e':
190 1.2 lukem eflag++; /* Check hostname */
191 1.2 lukem break;
192 1.2 lukem
193 1.2 lukem case 'E':
194 1.2 lukem eflag++; /* Check hostname */
195 1.2 lukem Eflag++; /* .. even check MX records */
196 1.2 lukem break;
197 1.2 lukem
198 1.2 lukem case 'n':
199 1.2 lukem nflag++; /* Capitalize name parts */
200 1.2 lukem break;
201 1.2 lukem
202 1.2 lukem case 's':
203 1.2 lukem sflag++; /* Don't know... */
204 1.2 lukem break;
205 1.2 lukem
206 1.2 lukem case 'u':
207 1.2 lukem uflag++; /* Don't check UUCP hostname */
208 1.2 lukem break;
209 1.2 lukem
210 1.2 lukem case 'v':
211 1.2 lukem vflag++; /* Verbose */
212 1.2 lukem break;
213 1.2 lukem
214 1.2 lukem default:
215 1.2 lukem usage();
216 1.2 lukem }
217 1.1 lukem }
218 1.1 lukem
219 1.2 lukem if (optind == argc)
220 1.2 lukem usage();
221 1.2 lukem
222 1.2 lukem input = argv[optind++];
223 1.2 lukem if (optind < argc)
224 1.2 lukem output = argv[optind++];
225 1.2 lukem if (optind < argc)
226 1.2 lukem usage();
227 1.1 lukem
228 1.1 lukem db = ypdb_open(input, O_RDONLY, 0444);
229 1.2 lukem if (db == NULL)
230 1.3 lukem err(1, "Unable to open input database `%s'", input);
231 1.1 lukem
232 1.1 lukem if (output != NULL) {
233 1.2 lukem if (strlen(output) + strlen(YPDB_SUFFIX) > MAXPATHLEN)
234 1.2 lukem warnx("file name `%s' too long", output);
235 1.1 lukem snprintf(db_outfile, sizeof(db_outfile),
236 1.1 lukem "%s%s", output, YPDB_SUFFIX);
237 1.1 lukem
238 1.1 lukem slash = strrchr(output, '/');
239 1.1 lukem if (slash != NULL)
240 1.1 lukem slash[1] = 0; /* truncate to dir */
241 1.1 lukem else
242 1.1 lukem *output = 0; /* elminate */
243 1.1 lukem
244 1.1 lukem /* note: output is now directory where map goes ! */
245 1.1 lukem
246 1.2 lukem if (strlen(output) + strlen(mapname)
247 1.2 lukem + strlen(YPDB_SUFFIX) > MAXPATHLEN)
248 1.3 lukem errx(1, "Directory name `%s' too long", output);
249 1.1 lukem
250 1.1 lukem snprintf(db_tempname, sizeof(db_tempname), "%s%s", output,
251 1.1 lukem mapname);
252 1.1 lukem mktemp(db_tempname);
253 1.1 lukem snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname,
254 1.1 lukem YPDB_SUFFIX);
255 1.1 lukem
256 1.1 lukem new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444);
257 1.2 lukem if (new_db == NULL)
258 1.3 lukem err(1, "Unable to open output database `%s'",
259 1.2 lukem db_outfile);
260 1.1 lukem }
261 1.1 lukem
262 1.1 lukem for (key = ypdb_firstkey(db);
263 1.1 lukem key.dptr != NULL;
264 1.1 lukem key = ypdb_nextkey(db)) {
265 1.1 lukem
266 1.2 lukem val = ypdb_fetch(db, key);
267 1.1 lukem
268 1.2 lukem if (val.dptr == NULL)
269 1.2 lukem continue; /* No value */
270 1.1 lukem if ((*key.dptr == '@') && (key.dsize == 1))
271 1.1 lukem continue; /* Sendmail token */
272 1.1 lukem if (strncmp(key.dptr, "YP_", 3)==0) /* YP token */
273 1.1 lukem continue;
274 1.2 lukem if (strchr(val.dptr, ',')) /* List... */
275 1.2 lukem continue;
276 1.2 lukem if (strchr(val.dptr, '|')) /* Pipe... */
277 1.2 lukem continue;
278 1.1 lukem
279 1.2 lukem if (!((strchr(val.dptr, '@')) ||
280 1.2 lukem (strchr(val.dptr, '!'))))
281 1.2 lukem continue; /* Skip local users */
282 1.1 lukem
283 1.2 lukem split_address(val.dptr, val.dsize, user, host);
284 1.1 lukem
285 1.1 lukem if (eflag && check_host(val.dptr, host, dflag, uflag, Eflag)) {
286 1.2 lukem printf("Invalid host %s in %*.*s:%*.*s\n", host,
287 1.1 lukem key.dsize, key.dsize, key.dptr,
288 1.1 lukem val.dsize, val.dsize, val.dptr);
289 1.1 lukem continue;
290 1.1 lukem }
291 1.1 lukem
292 1.2 lukem if (nflag)
293 1.2 lukem capitalize(key.dptr, key.dsize);
294 1.1 lukem
295 1.1 lukem if (new_db != NULL) {
296 1.1 lukem status = ypdb_store(new_db, val, key, YPDB_INSERT);
297 1.1 lukem if (status != 0) {
298 1.1 lukem printf("%s: problem storing %*.*s %*.*s\n",
299 1.1 lukem __progname,
300 1.1 lukem val.dsize, val.dsize, val.dptr,
301 1.1 lukem key.dsize, key.dsize, key.dptr);
302 1.1 lukem }
303 1.1 lukem }
304 1.1 lukem
305 1.1 lukem if (vflag) {
306 1.1 lukem printf("%*.*s --> %*.*s\n",
307 1.1 lukem val.dsize, val.dsize, val.dptr,
308 1.1 lukem key.dsize, key.dsize, key.dptr);
309 1.1 lukem }
310 1.1 lukem
311 1.1 lukem }
312 1.1 lukem
313 1.1 lukem if (new_db != NULL) {
314 1.2 lukem sprintf(datestr, "%010d", (int)time(NULL));
315 1.1 lukem key.dptr = YP_LAST_KEY;
316 1.1 lukem key.dsize = strlen(YP_LAST_KEY);
317 1.1 lukem val.dptr = datestr;
318 1.1 lukem val.dsize = strlen(datestr);
319 1.1 lukem status = ypdb_store(new_db, key, val, YPDB_INSERT);
320 1.2 lukem if (status != 0)
321 1.2 lukem warnx("problem storing %*.*s %*.*s",
322 1.1 lukem key.dsize, key.dsize, key.dptr,
323 1.1 lukem val.dsize, val.dsize, val.dptr);
324 1.1 lukem }
325 1.1 lukem
326 1.1 lukem if (new_db != NULL) {
327 1.1 lukem gethostname(myname, sizeof(myname) - 1);
328 1.1 lukem key.dptr = YP_MASTER_KEY;
329 1.1 lukem key.dsize = strlen(YP_MASTER_KEY);
330 1.1 lukem val.dptr = myname;
331 1.1 lukem val.dsize = strlen(myname);
332 1.1 lukem status = ypdb_store(new_db, key, val, YPDB_INSERT);
333 1.2 lukem if (status != 0)
334 1.2 lukem warnx("problem storing %*.*s %*.*s",
335 1.1 lukem key.dsize, key.dsize, key.dptr,
336 1.1 lukem val.dsize, val.dsize, val.dptr);
337 1.1 lukem }
338 1.1 lukem
339 1.1 lukem ypdb_close(db);
340 1.1 lukem
341 1.1 lukem if (new_db != NULL) {
342 1.1 lukem ypdb_close(new_db);
343 1.2 lukem if (rename(db_mapname, db_outfile) < 0)
344 1.2 lukem err(1, "rename `%s' to `%s' failed", db_mapname,
345 1.1 lukem db_outfile);
346 1.1 lukem }
347 1.1 lukem
348 1.2 lukem exit(0);
349 1.2 lukem }
350 1.2 lukem
351 1.2 lukem void
352 1.2 lukem usage()
353 1.2 lukem {
354 1.2 lukem fprintf(stderr,
355 1.2 lukem "usage: %s [-e|-E [-d] [-u]] [-n] [-v] input [output]\n",
356 1.2 lukem __progname);
357 1.2 lukem exit(1);
358 1.1 lukem }
359