GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ypserv/mkalias/mkalias.c Lines: 0 125 0.0 %
Date: 2017-11-07 Branches: 0 108 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mkalias.c,v 1.29 2016/09/22 00:09:47 jsg Exp $ */
2
3
/*
4
 * Copyright (c) 1997 Mats O Jansson <moj@stacken.kth.se>
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
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.
15
 *
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
26
 * SUCH DAMAGE.
27
 */
28
29
#include <ctype.h>
30
#include <fcntl.h>
31
#include <netdb.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <unistd.h>
36
#include <sys/types.h>
37
#include <netinet/in.h>
38
#include <arpa/nameser.h>
39
#include <resolv.h>
40
#include <err.h>
41
#include <time.h>
42
#include "ypdb.h"
43
#include "ypdef.h"
44
45
static void
46
split_address(char *address, size_t len, char *user, char *host)
47
{
48
	char *c, *s, *r;
49
	size_t  i = 0;
50
51
	if (memchr(address, '@', len)) {
52
		s = user;
53
		for (c = address; i < len; i++) {
54
			if (*c == '@') {
55
				*s = '\0';
56
				s = host;
57
			} else {
58
				*s++ = *c;
59
			}
60
			c++;
61
		}
62
		*s = '\0';
63
	}
64
65
	if ((r = memrchr(address, '!',  len))) {
66
		s = host;
67
		for (c = address; i < len; i++) {
68
			if (c == r) {
69
				*s = '\0';
70
				s = user;
71
			} else {
72
				*s++ = *c;
73
			}
74
			c++;
75
		}
76
		*s = '\0';
77
	}
78
}
79
80
static int
81
check_host(char *address, size_t len, char *host, int dflag, int uflag, int Eflag)
82
{
83
	union {
84
		HEADER hdr;
85
		u_char buf[PACKETSZ];
86
	} answer;
87
	int  status;
88
89
	if ((dflag && memchr(address, '@', len)) ||
90
	    (uflag && memchr(address, '!', len)))
91
		return(0);
92
93
	if ((_res.options & RES_INIT) == 0)
94
		res_init();
95
96
	status = res_search(host, C_IN, T_AAAA, answer.buf, sizeof(answer.buf));
97
98
	if (status == -1)
99
		status = res_search(host, C_IN, T_A, answer.buf, sizeof(answer.buf));
100
101
	if (status == -1 && Eflag)
102
		status = res_search(host, C_IN, T_MX, answer.buf, sizeof(answer.buf));
103
104
	return(status == -1);
105
}
106
107
static void
108
capitalize(char *name, int len)
109
{
110
	char last = ' ';
111
	char *c;
112
	int  i = 0;
113
114
	for (c = name; i < len; i++) {
115
		if (*c == '.')
116
			last = '.';
117
		c++;
118
	}
119
120
	i = 0;
121
	if (last == '.') {
122
		for (c = name; i < len; i++) {
123
			if (last == '.')
124
				*c = (char)toupper(*c);
125
			last = *c++;
126
		}
127
	}
128
}
129
130
static void
131
usage(void)
132
{
133
	fprintf(stderr,
134
	    "usage: mkalias [-nv] [-E | -e [-du]] input [output]\n");
135
	exit(1);
136
}
137
138
int
139
main(int argc, char *argv[])
140
{
141
	int	eflag = 0, dflag = 0, nflag = 0;
142
	int	uflag = 0, vflag = 0, Eflag = 0;
143
	int	status, ch, fd;
144
	char	*input = NULL, *output = NULL;
145
	DBM	*db;
146
	datum	key, val;
147
	DBM	*new_db = NULL;
148
	static	char mapname[] = "ypdbXXXXXXXXXX";
149
	char	db_mapname[PATH_MAX], db_outfile[PATH_MAX];
150
	char	db_tempname[PATH_MAX];
151
	char	user[4096], host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
152
	char	myname[HOST_NAME_MAX+1], datestr[11], *slash;
153
154
	while ((ch = getopt(argc, argv, "Edensuv")) != -1)
155
		switch (ch) {
156
		case 'E':
157
			eflag = 1;	/* Check hostname */
158
			Eflag = 1;	/* .. even check MX records */
159
			break;
160
		case 'd':
161
			dflag = 1;	/* Don't check DNS hostname */
162
			break;
163
		case 'e':
164
			eflag = 1;	/* Check hostname */
165
			break;
166
		case 'n':
167
			nflag = 1;	/* Capitalize name parts */
168
			break;
169
		case 's':		/* Ignore */
170
			break;
171
		case 'u':
172
			uflag = 1;	/* Don't check UUCP hostname */
173
			break;
174
		case 'v':
175
			vflag = 1;	/* Verbose */
176
			break;
177
		default:
178
			usage();
179
			break;
180
		}
181
182
	if (optind == argc)
183
		usage();
184
185
	input = argv[optind++];
186
	if (optind < argc)
187
		output = argv[optind++];
188
	if (optind < argc)
189
		usage();
190
191
	db = ypdb_open(input, O_RDONLY, 0444);
192
	if (db == NULL) {
193
		err(1, "Unable to open input database %s", input);
194
		/* NOTREACHED */
195
	}
196
197
	if (output != NULL) {
198
		if (strlen(output) + strlen(YPDB_SUFFIX) > PATH_MAX) {
199
			errx(1, "%s: file name too long", output);
200
			/* NOTREACHED */
201
		}
202
203
		snprintf(db_outfile, sizeof(db_outfile),
204
		    "%s%s", output, YPDB_SUFFIX);
205
206
		slash = strrchr(output, '/');
207
		if (slash != NULL)
208
			slash[1] = 0;			/* truncate to dir */
209
		else
210
			*output = 0;			/* eliminate */
211
212
		/* note: output is now directory where map goes ! */
213
214
		if (strlen(output) + strlen(mapname) +
215
		    strlen(YPDB_SUFFIX) > PATH_MAX) {
216
			errx(1, "%s: directory name too long", output);
217
			/* NOTREACHED */
218
		}
219
220
		snprintf(db_tempname, sizeof(db_tempname), "%s%s%s", output,
221
		    mapname, YPDB_SUFFIX);
222
		fd = mkstemps(db_tempname, 3);
223
		if (fd == -1)
224
			goto fail;
225
		close(fd);
226
227
		strncpy(db_mapname, db_tempname, strlen(db_tempname) - 3);
228
		db_mapname[sizeof(db_mapname) - 1] = '\0';
229
230
		new_db = ypdb_open(db_mapname, O_RDWR|O_TRUNC, 0444);
231
		if (new_db == NULL) {
232
fail:
233
			if (fd != -1)
234
				unlink(db_tempname);
235
			err(1, "Unable to open output database %s",
236
			    db_outfile);
237
			/* NOTREACHED */
238
		}
239
	}
240
241
	for (key = ypdb_firstkey(db); key.dptr != NULL;
242
	    key = ypdb_nextkey(db)) {
243
		val = ypdb_fetch(db, key);
244
245
		if (val.dptr == NULL)
246
			continue;			/* No value */
247
		if (*key.dptr == '@' && key.dsize == 1)
248
			continue;			/* Sendmail token */
249
		if (strncmp(key.dptr, "YP_", 3)==0)	/* YP token */
250
			continue;
251
		if (memchr(val.dptr, ',', val.dsize))
252
			continue;			/* List... */
253
		if (memchr(val.dptr, '|', val.dsize))
254
			continue;			/* Pipe... */
255
256
		if (!(memchr(val.dptr, '@', val.dsize) ||
257
		    memchr(val.dptr, '!', val.dsize)))
258
			continue;		/* Skip local users */
259
260
		split_address(val.dptr, val.dsize, user, host);
261
262
		if (eflag && check_host(val.dptr, val.dsize, host, dflag, uflag, Eflag)) {
263
			warnx("Invalid host %s in %*.*s:%*.*s",
264
			    host, key.dsize, key.dsize, key.dptr,
265
			    val.dsize, val.dsize, val.dptr);
266
			continue;
267
		}
268
269
		if (nflag)
270
			capitalize(key.dptr, key.dsize);
271
272
		if (new_db != NULL) {
273
			status = ypdb_store(new_db, val, key, YPDB_INSERT);
274
			if (status != 0) {
275
				warnx("problem storing %*.*s %*.*s",
276
				    val.dsize, val.dsize, val.dptr,
277
				    key.dsize, key.dsize, key.dptr);
278
			}
279
		}
280
281
		if (vflag) {
282
			printf("%*.*s --> %*.*s\n",
283
			    val.dsize, val.dsize, val.dptr,
284
			    key.dsize, key.dsize, key.dptr);
285
		}
286
287
	}
288
289
	if (new_db != NULL) {
290
		snprintf(datestr, sizeof datestr, "%010lld",
291
		    (long long)time(NULL));
292
		key.dptr = YP_LAST_KEY;
293
		key.dsize = strlen(YP_LAST_KEY);
294
		val.dptr = datestr;
295
		val.dsize = strlen(datestr);
296
		status = ypdb_store(new_db, key, val, YPDB_INSERT);
297
		if (status != 0) {
298
			warnx("problem storing %*.*s %*.*s",
299
			    key.dsize, key.dsize, key.dptr,
300
			    val.dsize, val.dsize, val.dptr);
301
		}
302
	}
303
304
	if (new_db != NULL) {
305
		gethostname(myname, sizeof(myname));
306
		key.dptr = YP_MASTER_KEY;
307
		key.dsize = strlen(YP_MASTER_KEY);
308
		val.dptr = myname;
309
		val.dsize = strlen(myname);
310
		status = ypdb_store(new_db, key, val, YPDB_INSERT);
311
		if (status != 0) {
312
			warnx("problem storing %*.*s %*.*s",
313
			    key.dsize, key.dsize, key.dptr,
314
			    val.dsize, val.dsize, val.dptr);
315
		}
316
	}
317
318
	ypdb_close(db);
319
320
	if (new_db != NULL) {
321
		ypdb_close(new_db);
322
		if (rename(db_tempname, db_outfile) < 0) {
323
			err(1, "rename %s -> %s failed", db_tempname,
324
			    db_outfile);
325
			/* NOTREACHED */
326
		}
327
	}
328
	return(0);
329
}