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