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