From dd1938e48f5b91b5383e0f2add96bf23a9d226be Mon Sep 17 00:00:00 2001 From: Mats O Jansson Date: Sat, 26 Jul 1997 12:12:30 +0000 Subject: The second of three steps to support main.aliases and mail.byaddr in ypserv. This time it's time for a new utility program mkalias. The last part is arriving any day now... -moj --- usr.sbin/ypserv/Makefile | 4 +- usr.sbin/ypserv/mkalias/Makefile | 9 ++ usr.sbin/ypserv/mkalias/mkalias.8 | 93 +++++++++++ usr.sbin/ypserv/mkalias/mkalias.c | 333 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 437 insertions(+), 2 deletions(-) create mode 100644 usr.sbin/ypserv/mkalias/Makefile create mode 100644 usr.sbin/ypserv/mkalias/mkalias.8 create mode 100644 usr.sbin/ypserv/mkalias/mkalias.c (limited to 'usr.sbin') diff --git a/usr.sbin/ypserv/Makefile b/usr.sbin/ypserv/Makefile index 33a3c1ddbea..2c99ca31d0c 100644 --- a/usr.sbin/ypserv/Makefile +++ b/usr.sbin/ypserv/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.5 1997/04/15 22:58:57 maja Exp $ +# $OpenBSD: Makefile,v 1.6 1997/07/26 12:12:26 maja Exp $ -SUBDIR= makedbm mknetid revnetgroup stdethers stdhosts \ +SUBDIR= makedbm mkalias mknetid revnetgroup stdethers stdhosts \ ypinit ypserv yptest yppush ypxfr .include diff --git a/usr.sbin/ypserv/mkalias/Makefile b/usr.sbin/ypserv/mkalias/Makefile new file mode 100644 index 00000000000..1948251f0bf --- /dev/null +++ b/usr.sbin/ypserv/mkalias/Makefile @@ -0,0 +1,9 @@ +# $OpenBSD: Makefile,v 1.1 1997/07/26 12:12:28 maja Exp $ + +PROG= mkalias +SRCS= mkalias.c ypdb.c +MAN= mkalias.8 +.PATH: ${.CURDIR}/../common +CFLAGS+=-I${.CURDIR}/../common + +.include diff --git a/usr.sbin/ypserv/mkalias/mkalias.8 b/usr.sbin/ypserv/mkalias/mkalias.8 new file mode 100644 index 00000000000..4d45396a7fb --- /dev/null +++ b/usr.sbin/ypserv/mkalias/mkalias.8 @@ -0,0 +1,93 @@ +.\" $OpenBSD: mkalias.8,v 1.1 1997/07/26 12:12:28 maja Exp $ +.\" Copyright (c) 1997 Mats O Jansson +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by Mats O Jansson +.\" 4. The name of the author may not be used to endorse or promote products +.\" derived from this software without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS +.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd July 21, 1997 +.Dt MKALIAS 8 +.Os +.Sh NAME +.Nm mkalias +.Nd a YP map convertion program +.Sh SYNOPSIS +.Nm mkalias +.Op Fl v +.Op Fl e +.Op Fl E +.Op Fl d +.Op Fl u +.Op Fl n +.Ar input +.Op Ar output +.Sh DESCRIPTION +.Nm Mkalias +is used to convert a mail.aliases map to a mail.byaddr map. This is a +a inverse map of user@host (or user!host) back to alias. +.Pp +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl v +Verbose mode. +.It Fl e +Check host to verify that it exists. +.It Fl E +Same as +.Fl e , +but also check for any MX-record. +.It Fl d +Assume Domain names are OK. Only useful together with +.Fl e +or +.Fl E . +.It Fl u +Assume UUCP names are OK. Only useful together with +.Fl e +or +.Fl E . +.It Fl n +Capitalize name. eg mats.o.jansson becomes Mats.O.Jansson. +.It Ar input +Use this map as input +.It Ar output +Use this map as output. If the output map isn't given don't create database. +Can be useful together with +.Fl e +or +.Fl E . +.El +.Sh BUGS +.Nm mkalias +on SunOS 4.1.x seems to have a +.Fl s . +Since I don't know what it is supposed to do I havn't implemented it. But it is accepted by the program. +.Sh SEE ALSO +.Xr yp 8 , +.Xr ypserv 8 +.Sh AUTHOR +Mats O Jansson diff --git a/usr.sbin/ypserv/mkalias/mkalias.c b/usr.sbin/ypserv/mkalias/mkalias.c new file mode 100644 index 00000000000..ee514366906 --- /dev/null +++ b/usr.sbin/ypserv/mkalias/mkalias.c @@ -0,0 +1,333 @@ +/* $OpenBSD: mkalias.c,v 1.1 1997/07/26 12:12:29 maja Exp $ */ + +/* + * Copyright (c) 1997 Mats O Jansson + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Mats O Jansson + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef LINT +static char rcsid[] = "$OpenBSD: mkalias.c,v 1.1 1997/07/26 12:12:29 maja Exp $"; +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ypdb.h" +#include "ypdef.h" + +extern char *__progname; /* from crt0.o */ + +void +split_address(address, len, user, host) +char *address; +int len; +char *user, *host; +{ + char *c,*s; + int i = 0; + + if (index(address,'@')) { + + s = user; + + for(c = address; i < len; i++) { + if (*c == '@') { + *s = '\0'; + s = host; + } else { + *s++ = *c; + } + c++; + } + *s = '\0'; + + } + + if (index(address,'!')) { + + s = host; + + for(c = address; i < len; i++) { + if (*c == '!') { + *s = '\0'; + s = user; + } else { + *s++ = *c; + } + c++; + } + *s = '\0'; + + } + +} + +int +check_host(address, host, dflag, uflag, Eflag) +char *address, *host; +int dflag, uflag, Eflag; +{ + char answer[PACKETSZ]; + int status; + + if ((dflag && index(address,'@')) || + (uflag && index(address,'!'))) return(0); + + if ((_res.options & RES_INIT) == 0) + res_init(); + + status = res_search(host, C_IN, T_AAAA, answer, sizeof(answer)); + + if (status == -1) + status = res_search(host, C_IN, T_A, answer, sizeof(answer)); + + if ((status == -1) && Eflag) + status = res_search(host, C_IN, T_MX, answer, sizeof(answer)); + + return(status == -1); +} + +void +capitalize(name,len) +char *name; +int len; +{ + char last = ' '; + char *c; + int i = 0; + + for(c = name; i < len; i++) { + if (*c == '.') last = '.'; + c++; + } + + i = 0; + if (last == '.') { + for(c = name; i < len; i++) { + if (last == '.') { + *c = toupper(*c); + } + last = *c++; + } + } + +} + +int +main (argc,argv) +int argc; +char *argv[]; +{ + int usage = 0; + int eflag = 0; + int dflag = 0; + int nflag = 0; + int sflag = 0; + int uflag = 0; + int vflag = 0; + int Eflag = 0; + int ch; + char *input = NULL; + char *output = NULL; + DBM *db; + datum key,val; + char *slash; + DBM *new_db = NULL; + static char mapname[] = "ypdbXXXXXXXXXX"; + char db_mapname[MAXPATHLEN],db_outfile[MAXPATHLEN], + db_tempname[MAXPATHLEN]; + int status; + char user[4096],host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */ + + while ((ch = getopt(argc, argv, "Edensuv")) != -1) + switch(ch) { + case 'E': + eflag++; /* Check hostname */ + Eflag++; /* .. even check MX records */ + break; + case 'd': + dflag++; /* Don't check DNS hostname */ + break; + case 'e': + eflag++; /* Check hostname */ + break; + case 'n': + nflag++; /* Capitalize name parts */ + break; + case 's': + sflag++; /* Don't know... */ + break; + case 'u': + uflag++; /* Don't check UUCP hostname */ + break; + case 'v': + vflag++; /* Verbose */ + break; + default: + usage++; + break; + } + + if (optind == argc) { + usage++; + } else { + input = argv[optind++]; + if (optind < argc) + output = argv[optind++]; + if (optind < argc) + usage++; + } + + if (usage) { + fprintf(stderr, + "usage: %s [-v] [-e|-E [-d] [-u]] [-n] input [output]\n", + __progname); + exit(1); + } + + db = ypdb_open(input, O_RDONLY, 0444); + if (db == NULL) { + fprintf(stderr, + "%s: Unable to open input database %s\n", + __progname, + input); + exit(1); + } + + if (output != NULL) { + if (strlen(output) + strlen(YPDB_SUFFIX) > MAXPATHLEN) { + fprintf(stderr,"%s: %s: file name too long\n", + __progname, output); + } + snprintf(db_outfile, sizeof(db_outfile), + "%s%s", output, YPDB_SUFFIX); + + slash = strrchr(output, '/'); + if (slash != NULL) + slash[1] = 0; /* truncate to dir */ + else + *output = 0; /* elminate */ + + /* note: output is now directory where map goes ! */ + + if (strlen(output) + strlen(mapname) + + strlen(YPDB_SUFFIX) > MAXPATHLEN) { + fprintf(stderr,"%s: %s: directory name too long\n", + __progname, output); + exit(1); + } + + snprintf(db_tempname, sizeof(db_tempname), "%s%s", output, + mapname); + mktemp(db_tempname); + snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname, + YPDB_SUFFIX); + + new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444); + if (new_db == NULL) { + fprintf(stderr, + "%s: Unable to open output database %s\n", + __progname, + db_outfile); + exit(1); + } + } + + for (key = ypdb_firstkey(db); + key.dptr != NULL; + key = ypdb_nextkey(db)) { + + val = ypdb_fetch(db,key); + + if (val.dptr == NULL) continue; /* No value */ + if ((*key.dptr == '@') && (key.dsize == 1)) + continue; /* Sendmail token */ + if (strncmp(key.dptr, "YP_", 3)==0) /* YP token */ + continue; + if (index(val.dptr,',')) continue; /* List... */ + if (index(val.dptr,'|')) continue; /* Pipe... */ + + if (!((index(val.dptr,'@')) || + (index(val.dptr,'!')))) continue; /* Skip local users */ + + split_address(val.dptr,val.dsize,user,host); + + if (eflag && check_host(val.dptr, host, dflag, uflag, Eflag)) { + printf("Invalid host %s in %*.*s:%*.*s\n", + host, + key.dsize, key.dsize, key.dptr, + val.dsize, val.dsize, val.dptr); + continue; + } + + if (nflag) { + capitalize(key.dptr,key.dsize); + } + + if (new_db != NULL) { + status = ypdb_store(new_db, val, key, YPDB_INSERT); + if (status != 0) { + printf("%s: problem storing %*.*s %*.*s\n", + __progname, + val.dsize, val.dsize, val.dptr, + key.dsize, key.dsize, key.dptr); + } + } + + if (vflag) { + printf("%*.*s --> %*.*s\n", + val.dsize, val.dsize, val.dptr, + key.dsize, key.dsize, key.dptr); + } + + } + + ypdb_close(db); + + if (new_db != NULL) { + ypdb_close(new_db); + if (rename(db_mapname,db_outfile) < 0) { + perror("rename"); + fprintf(stderr,"rename %s -> %s failed!\n", db_mapname, + db_outfile); + exit(1); + } + } + + return(0); + +} -- cgit v1.2.3