1 /* $NetBSD: mkalias.c,v 1.16 2009/04/19 06:06:40 lukem Exp $ */
4 * Copyright (c) 1997 Mats O Jansson <moj@stacken.kth.se>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: mkalias.c,v 1.16 2009/04/19 06:06:40 lukem Exp $");
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
55 void capitalize(char *, int);
56 int check_host(char *, char *, int, int, int);
57 int main(int, char *[]);
58 void split_address(char *, int, char *, char *);
62 split_address(char *address
, int len
, char *user
, char *host
)
67 if ((strchr(address
, '@')) != NULL
) {
71 for(c
= address
; i
< len
; i
++) {
82 if ((r
= strrchr(address
, '!')) != NULL
) {
86 for(c
= address
; i
< len
; i
++) {
99 check_host(char *address
, char *host
, int dflag
, int uflag
, int Eflag
)
101 u_char answer
[PACKETSZ
];
104 if ((dflag
&& strchr(address
, '@')) ||
105 (uflag
&& strchr(address
, '!')))
108 if ((_res
.options
& RES_INIT
) == 0)
111 status
= res_search(host
, C_IN
, T_AAAA
, answer
, sizeof(answer
));
114 status
= res_search(host
, C_IN
, T_A
, answer
, sizeof(answer
));
116 if ((status
== -1) && Eflag
)
117 status
= res_search(host
, C_IN
, T_MX
, answer
, sizeof(answer
));
119 return(status
== -1);
123 capitalize(char *name
, int len
)
129 for(c
= name
; i
< len
; i
++) {
130 if (*c
== '.') last
= '.';
136 for(c
= name
; i
< len
; i
++) {
138 *c
= toupper((unsigned char)*c
);
145 main(int argc
, char *argv
[])
161 static const char template[] = "ypdbXXXXXX";
162 char db_mapname
[MAXPATHLEN
], db_outfile
[MAXPATHLEN
];
164 char user
[4096], host
[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
166 char myname
[MAXHOSTNAMELEN
];
168 while ((ch
= getopt(argc
, argv
, "Edensuv")) != -1) {
171 dflag
++; /* Don't check DNS hostname */
175 eflag
++; /* Check hostname */
179 eflag
++; /* Check hostname */
180 Eflag
++; /* .. even check MX records */
184 nflag
++; /* Capitalize name parts */
188 sflag
++; /* Don't know... */
192 uflag
++; /* Don't check UUCP hostname */
196 vflag
++; /* Verbose */
207 input
= argv
[optind
++];
209 output
= argv
[optind
++];
213 db
= ypdb_open(input
);
215 err(1, "Unable to open input database `%s'", input
);
217 if (output
!= NULL
) {
218 if (strlen(output
) + strlen(YPDB_SUFFIX
) >
219 (sizeof(db_outfile
) + 1))
220 warnx("file name `%s' too long", output
);
221 snprintf(db_outfile
, sizeof(db_outfile
),
222 "%s%s", output
, YPDB_SUFFIX
);
224 slash
= strrchr(output
, '/');
226 slash
[1] = 0; /* truncate to dir */
228 *output
= 0; /* elminate */
230 /* note: output is now directory where map goes ! */
232 if (strlen(output
) + strlen(template) + strlen(YPDB_SUFFIX
) >
233 (sizeof(db_mapname
) - 1))
234 errx(1, "Directory name `%s' too long", output
);
236 snprintf(db_mapname
, sizeof(db_mapname
), "%s%s",
239 new_db
= ypdb_mktemp(db_mapname
);
241 err(1, "Unable to open output database `%s'",
245 for (key
= ypdb_firstkey(db
);
247 key
= ypdb_nextkey(db
)) {
249 val
= ypdb_fetch(db
, key
);
251 if (val
.dptr
== NULL
)
252 continue; /* No value */
253 if ((*key
.dptr
== '@') && (key
.dsize
== 1))
254 continue; /* Sendmail token */
255 if (strncmp(key
.dptr
, "YP_", 3)==0) /* YP token */
257 if (strchr(val
.dptr
, ',')) /* List... */
259 if (strchr(val
.dptr
, '|')) /* Pipe... */
262 if (!((strchr(val
.dptr
, '@')) ||
263 (strchr(val
.dptr
, '!'))))
264 continue; /* Skip local users */
266 split_address(val
.dptr
, val
.dsize
, user
, host
);
268 if (eflag
&& check_host(val
.dptr
, host
, dflag
, uflag
, Eflag
)) {
269 printf("Invalid host %s in %*.*s:%*.*s\n", host
,
270 key
.dsize
, key
.dsize
, key
.dptr
,
271 val
.dsize
, val
.dsize
, val
.dptr
);
276 capitalize(key
.dptr
, key
.dsize
);
278 if (new_db
!= NULL
) {
279 status
= ypdb_store(new_db
, val
, key
, YPDB_INSERT
);
281 printf("%s: problem storing %*.*s %*.*s\n",
283 val
.dsize
, val
.dsize
, val
.dptr
,
284 key
.dsize
, key
.dsize
, key
.dptr
);
289 printf("%*.*s --> %*.*s\n",
290 val
.dsize
, val
.dsize
, val
.dptr
,
291 key
.dsize
, key
.dsize
, key
.dptr
);
296 if (new_db
!= NULL
) {
297 snprintf(datestr
, sizeof(datestr
), "%010d", (int)time(NULL
));
298 key
.dptr
= __UNCONST(YP_LAST_KEY
);
299 key
.dsize
= strlen(YP_LAST_KEY
);
301 val
.dsize
= strlen(datestr
);
302 status
= ypdb_store(new_db
, key
, val
, YPDB_INSERT
);
304 warnx("problem storing %*.*s %*.*s",
305 key
.dsize
, key
.dsize
, key
.dptr
,
306 val
.dsize
, val
.dsize
, val
.dptr
);
309 if (new_db
!= NULL
) {
310 localhostname(myname
, sizeof(myname
) - 1);
311 key
.dptr
= __UNCONST(YP_MASTER_KEY
);
312 key
.dsize
= strlen(YP_MASTER_KEY
);
314 val
.dsize
= strlen(myname
);
315 status
= ypdb_store(new_db
, key
, val
, YPDB_INSERT
);
317 warnx("problem storing %*.*s %*.*s",
318 key
.dsize
, key
.dsize
, key
.dptr
,
319 val
.dsize
, val
.dsize
, val
.dptr
);
324 if (new_db
!= NULL
) {
326 if (rename(db_mapname
, db_outfile
) < 0)
327 err(1, "rename `%s' to `%s' failed", db_mapname
,
338 "usage: %s [-e|-E [-d] [-u]] [-n] [-v] input [output]\n",