1999-06-07 07:06:08 +04:00
|
|
|
/* $NetBSD: mkalias.c,v 1.8 1999/06/07 03:06:09 mrg Exp $ */
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1997 Mats O Jansson <moj@stacken.kth.se>
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-10-13 07:48:49 +04:00
|
|
|
#ifndef lint
|
1999-06-07 07:06:08 +04:00
|
|
|
__RCSID("$NetBSD: mkalias.c,v 1.8 1999/06/07 03:06:09 mrg Exp $");
|
1997-10-06 08:59:13 +04:00
|
|
|
#endif
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
|
1997-10-06 08:59:13 +04:00
|
|
|
#include <ctype.h>
|
1997-10-06 10:10:15 +04:00
|
|
|
#include <err.h>
|
1997-10-06 08:59:13 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
1997-10-06 10:10:15 +04:00
|
|
|
#include <resolv.h>
|
1997-10-06 08:59:13 +04:00
|
|
|
#include <stdio.h>
|
1998-06-01 18:05:35 +04:00
|
|
|
#include <stdlib.h>
|
1997-10-06 08:59:13 +04:00
|
|
|
#include <string.h>
|
1998-06-24 14:26:08 +04:00
|
|
|
#include <time.h>
|
1997-10-06 08:59:13 +04:00
|
|
|
#include <unistd.h>
|
1997-10-06 10:10:15 +04:00
|
|
|
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
|
1997-11-04 14:43:49 +03:00
|
|
|
#include "protos.h"
|
1997-10-06 08:59:13 +04:00
|
|
|
#include "ypdb.h"
|
|
|
|
#include "ypdef.h"
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
void capitalize __P((char *, int));
|
|
|
|
int check_host __P((char *, char *, int, int, int));
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
void split_address __P((char *, int, char *, char *));
|
|
|
|
void usage __P((void));
|
|
|
|
|
1997-10-06 08:59:13 +04:00
|
|
|
extern char *__progname; /* from crt0.o */
|
|
|
|
|
|
|
|
void
|
|
|
|
split_address(address, len, user, host)
|
1997-10-06 10:10:15 +04:00
|
|
|
char *address;
|
|
|
|
int len;
|
|
|
|
char *user, *host;
|
1997-10-06 08:59:13 +04:00
|
|
|
{
|
1997-10-06 10:10:15 +04:00
|
|
|
char *c, *s, *r;
|
1997-10-06 08:59:13 +04:00
|
|
|
int i = 0;
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if ((strchr(address, '@')) != NULL) {
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
s = user;
|
|
|
|
|
|
|
|
for(c = address; i < len; i++) {
|
|
|
|
if (*c == '@') {
|
|
|
|
*s = '\0';
|
|
|
|
s = host;
|
1997-10-06 10:10:15 +04:00
|
|
|
} else
|
1997-10-06 08:59:13 +04:00
|
|
|
*s++ = *c;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if ((r = strrchr(address, '!')) != NULL) {
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
s = host;
|
|
|
|
|
|
|
|
for(c = address; i < len; i++) {
|
|
|
|
if (c == r) {
|
|
|
|
*s = '\0';
|
|
|
|
s = user;
|
1997-10-06 10:10:15 +04:00
|
|
|
} else
|
1997-10-06 08:59:13 +04:00
|
|
|
*s++ = *c;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_host(address, host, dflag, uflag, Eflag)
|
1997-10-06 10:10:15 +04:00
|
|
|
char *address, *host;
|
|
|
|
int dflag, uflag, Eflag;
|
1997-10-06 08:59:13 +04:00
|
|
|
{
|
|
|
|
char answer[PACKETSZ];
|
|
|
|
int status;
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if ((dflag && strchr(address, '@')) ||
|
|
|
|
(uflag && strchr(address, '!')))
|
|
|
|
return(0);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
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
|
1997-10-06 10:10:15 +04:00
|
|
|
capitalize(name, len)
|
|
|
|
char *name;
|
|
|
|
int len;
|
1997-10-06 08:59:13 +04:00
|
|
|
{
|
|
|
|
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++) {
|
1997-10-06 10:10:15 +04:00
|
|
|
if (last == '.')
|
1997-10-06 08:59:13 +04:00
|
|
|
*c = toupper(*c);
|
|
|
|
last = *c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-10-06 10:10:15 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1997-10-06 08:59:13 +04:00
|
|
|
{
|
|
|
|
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;
|
1997-10-06 10:10:15 +04:00
|
|
|
datum key, val;
|
1997-10-06 08:59:13 +04:00
|
|
|
char *slash;
|
|
|
|
DBM *new_db = NULL;
|
|
|
|
static char mapname[] = "ypdbXXXXXXXXXX";
|
1997-10-06 10:10:15 +04:00
|
|
|
char db_mapname[MAXPATHLEN], db_outfile[MAXPATHLEN],
|
1997-10-06 08:59:13 +04:00
|
|
|
db_tempname[MAXPATHLEN];
|
|
|
|
int status;
|
1997-10-06 10:10:15 +04:00
|
|
|
char user[4096], host[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
|
1997-10-06 08:59:13 +04:00
|
|
|
char datestr[11];
|
|
|
|
char myname[MAXHOSTNAMELEN];
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
while ((ch = getopt(argc, argv, "Edensuv")) != -1) {
|
|
|
|
switch(ch) {
|
|
|
|
case 'd':
|
|
|
|
dflag++; /* Don't check DNS hostname */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
eflag++; /* Check hostname */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
eflag++; /* Check hostname */
|
|
|
|
Eflag++; /* .. even check MX records */
|
|
|
|
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();
|
|
|
|
}
|
1997-10-06 08:59:13 +04:00
|
|
|
}
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if (optind == argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
input = argv[optind++];
|
|
|
|
if (optind < argc)
|
|
|
|
output = argv[optind++];
|
|
|
|
if (optind < argc)
|
|
|
|
usage();
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
db = ypdb_open(input, O_RDONLY, 0444);
|
1997-10-06 10:10:15 +04:00
|
|
|
if (db == NULL)
|
1997-10-07 18:45:18 +04:00
|
|
|
err(1, "Unable to open input database `%s'", input);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
if (output != NULL) {
|
1997-10-06 10:10:15 +04:00
|
|
|
if (strlen(output) + strlen(YPDB_SUFFIX) > MAXPATHLEN)
|
|
|
|
warnx("file name `%s' too long", output);
|
1997-10-06 08:59:13 +04:00
|
|
|
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 ! */
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if (strlen(output) + strlen(mapname)
|
|
|
|
+ strlen(YPDB_SUFFIX) > MAXPATHLEN)
|
1997-10-07 18:45:18 +04:00
|
|
|
errx(1, "Directory name `%s' too long", output);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
snprintf(db_tempname, sizeof(db_tempname), "%s%s", output,
|
|
|
|
mapname);
|
1999-06-07 07:06:08 +04:00
|
|
|
mktemp(db_tempname); /* OK */
|
1997-10-06 08:59:13 +04:00
|
|
|
snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname,
|
|
|
|
YPDB_SUFFIX);
|
|
|
|
|
|
|
|
new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444);
|
1997-10-06 10:10:15 +04:00
|
|
|
if (new_db == NULL)
|
1997-10-07 18:45:18 +04:00
|
|
|
err(1, "Unable to open output database `%s'",
|
1997-10-06 10:10:15 +04:00
|
|
|
db_outfile);
|
1997-10-06 08:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (key = ypdb_firstkey(db);
|
|
|
|
key.dptr != NULL;
|
|
|
|
key = ypdb_nextkey(db)) {
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
val = ypdb_fetch(db, key);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if (val.dptr == NULL)
|
|
|
|
continue; /* No value */
|
1997-10-06 08:59:13 +04:00
|
|
|
if ((*key.dptr == '@') && (key.dsize == 1))
|
|
|
|
continue; /* Sendmail token */
|
|
|
|
if (strncmp(key.dptr, "YP_", 3)==0) /* YP token */
|
|
|
|
continue;
|
1997-10-06 10:10:15 +04:00
|
|
|
if (strchr(val.dptr, ',')) /* List... */
|
|
|
|
continue;
|
|
|
|
if (strchr(val.dptr, '|')) /* Pipe... */
|
|
|
|
continue;
|
1997-10-06 08:59:13 +04:00
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if (!((strchr(val.dptr, '@')) ||
|
|
|
|
(strchr(val.dptr, '!'))))
|
|
|
|
continue; /* Skip local users */
|
1997-10-06 08:59:13 +04:00
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
split_address(val.dptr, val.dsize, user, host);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
if (eflag && check_host(val.dptr, host, dflag, uflag, Eflag)) {
|
1997-10-06 10:10:15 +04:00
|
|
|
printf("Invalid host %s in %*.*s:%*.*s\n", host,
|
1997-10-06 08:59:13 +04:00
|
|
|
key.dsize, key.dsize, key.dptr,
|
|
|
|
val.dsize, val.dsize, val.dptr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
if (nflag)
|
|
|
|
capitalize(key.dptr, key.dsize);
|
1997-10-06 08:59:13 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_db != NULL) {
|
1997-10-06 10:10:15 +04:00
|
|
|
sprintf(datestr, "%010d", (int)time(NULL));
|
1997-10-06 08:59:13 +04:00
|
|
|
key.dptr = YP_LAST_KEY;
|
|
|
|
key.dsize = strlen(YP_LAST_KEY);
|
|
|
|
val.dptr = datestr;
|
|
|
|
val.dsize = strlen(datestr);
|
|
|
|
status = ypdb_store(new_db, key, val, YPDB_INSERT);
|
1997-10-06 10:10:15 +04:00
|
|
|
if (status != 0)
|
|
|
|
warnx("problem storing %*.*s %*.*s",
|
1997-10-06 08:59:13 +04:00
|
|
|
key.dsize, key.dsize, key.dptr,
|
|
|
|
val.dsize, val.dsize, val.dptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_db != NULL) {
|
1997-11-04 14:43:49 +03:00
|
|
|
localhostname(myname, sizeof(myname) - 1);
|
1997-10-06 08:59:13 +04:00
|
|
|
key.dptr = YP_MASTER_KEY;
|
|
|
|
key.dsize = strlen(YP_MASTER_KEY);
|
|
|
|
val.dptr = myname;
|
|
|
|
val.dsize = strlen(myname);
|
|
|
|
status = ypdb_store(new_db, key, val, YPDB_INSERT);
|
1997-10-06 10:10:15 +04:00
|
|
|
if (status != 0)
|
|
|
|
warnx("problem storing %*.*s %*.*s",
|
1997-10-06 08:59:13 +04:00
|
|
|
key.dsize, key.dsize, key.dptr,
|
|
|
|
val.dsize, val.dsize, val.dptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ypdb_close(db);
|
|
|
|
|
|
|
|
if (new_db != NULL) {
|
|
|
|
ypdb_close(new_db);
|
1997-10-06 10:10:15 +04:00
|
|
|
if (rename(db_mapname, db_outfile) < 0)
|
|
|
|
err(1, "rename `%s' to `%s' failed", db_mapname,
|
1997-10-06 08:59:13 +04:00
|
|
|
db_outfile);
|
|
|
|
}
|
|
|
|
|
1997-10-06 10:10:15 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-e|-E [-d] [-u]] [-n] [-v] input [output]\n",
|
|
|
|
__progname);
|
|
|
|
exit(1);
|
1997-10-06 08:59:13 +04:00
|
|
|
}
|