xref: /onnv-gate/usr/src/cmd/ypcmd/mkalias.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*702Sth160488  * Use is subject to license terms.
250Sstevel@tonic-gate  */
26*702Sth160488 
27*702Sth160488 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * mkmap - program to convert the mail.aliases map into an
300Sstevel@tonic-gate  * inverse map of <user@host> back to <preferred-alias>
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <ndbm.h>
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <ctype.h>
420Sstevel@tonic-gate #include <netdb.h>
430Sstevel@tonic-gate #include <sys/systeminfo.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "ypdefs.h"
460Sstevel@tonic-gate USE_YP_PREFIX
470Sstevel@tonic-gate USE_YP_MASTER_NAME
480Sstevel@tonic-gate USE_YP_LAST_MODIFIED
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	MKAL_INCLUDE ":include:"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate void CopyName(char *dst, char *src, int len);
530Sstevel@tonic-gate int HostCheck(char *h, char *a);
540Sstevel@tonic-gate void DoName(char *cp);
550Sstevel@tonic-gate void UpperCase(char *cp);
560Sstevel@tonic-gate void AddYPEntries(void);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate int Verbose = 0;	/* to get the gory details */
590Sstevel@tonic-gate int UucpOK = 0;		/* pass all UUCP names right through */
600Sstevel@tonic-gate int DomainOK = 0;	/* pass all Domain names (with dots) */
610Sstevel@tonic-gate int ErrorCheck = 0;	/* check carefully for errors */
620Sstevel@tonic-gate int NoOutput = 0;	/* no output, just do the check */
630Sstevel@tonic-gate int Simple = 0;		/* Do not do the user name preference step */
640Sstevel@tonic-gate int NameMode = 0;	/* Try to capitalize as names */
650Sstevel@tonic-gate 
66*702Sth160488 DBM *Indbm = NULL, *Scandbm = NULL, *Outdbm = NULL;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int
IsMailingList(char * s)690Sstevel@tonic-gate IsMailingList(char *s)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * returns true if the given string is a mailing list
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	char *p;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (strchr(s, ','))
770Sstevel@tonic-gate 		return (1);
780Sstevel@tonic-gate 	if (strchr(s, '|'))
790Sstevel@tonic-gate 		return (1);
800Sstevel@tonic-gate 	p = strchr(s, ':');
810Sstevel@tonic-gate 	if (p && strncmp(p, MKAL_INCLUDE, sizeof (MKAL_INCLUDE)))
820Sstevel@tonic-gate 		return (1);
830Sstevel@tonic-gate 	return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate int
IsQualified(char * s,char * p,char * h)870Sstevel@tonic-gate IsQualified(char *s, char *p, char *h)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * returns true if the given string is qualified with a host name
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	register char *middle;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	middle = strchr(s, '@');
950Sstevel@tonic-gate 	if (middle) {
960Sstevel@tonic-gate 		for (middle = s; *middle != '@'; *p++ = *middle++)
970Sstevel@tonic-gate 			continue;
980Sstevel@tonic-gate 		*p = '\0';
990Sstevel@tonic-gate 		CopyName(h, middle+1, strlen(middle + 1));
1000Sstevel@tonic-gate 		return (1);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	middle = strrchr(s, '!');
1030Sstevel@tonic-gate 	if (middle) {
1040Sstevel@tonic-gate 		strcpy(p, middle+1);
1050Sstevel@tonic-gate 		*middle = '\0';
1060Sstevel@tonic-gate 		CopyName(h, s, strlen(s));
1070Sstevel@tonic-gate 		*middle = '!';
1080Sstevel@tonic-gate 		return (1);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 	return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
IsMaint(char * s)1140Sstevel@tonic-gate IsMaint(char *s)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	/*
1170Sstevel@tonic-gate 	 * returns true if the given string is one of the maintenence
1180Sstevel@tonic-gate 	 * strings used in sendmail or NIS.
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	if (*s == '@')
1210Sstevel@tonic-gate 		return (1);
1220Sstevel@tonic-gate 	if (strncmp(s, yp_prefix, yp_prefix_sz) == 0)
1230Sstevel@tonic-gate 		return (1);
1240Sstevel@tonic-gate 	return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate void
CopyName(char * dst,char * src,int len)1280Sstevel@tonic-gate CopyName(char *dst, char *src, int len)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	/*
131*702Sth160488 	 * copy a string, but ignore white space
132*702Sth160488 	 */
1330Sstevel@tonic-gate 	while (*src && len--) {
1340Sstevel@tonic-gate 		if (isspace(*src))
1350Sstevel@tonic-gate 			src++;
1360Sstevel@tonic-gate 		else
1370Sstevel@tonic-gate 			*dst++ = *src++;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	*dst = '\0';
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate int
Compare(char * s1,char * s2)1430Sstevel@tonic-gate Compare(char *s1, char *s2)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * compare strings, but ignore white space
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	while (*s1 != '\0' && isspace(*s1))
1490Sstevel@tonic-gate 		s1++;
1500Sstevel@tonic-gate 	while (*s2 != '\0' && isspace(*s2))
1510Sstevel@tonic-gate 		s2++;
1520Sstevel@tonic-gate 	return (strcmp(s1, s2));
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate void
ProcessMap(void)1560Sstevel@tonic-gate ProcessMap(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	datum key, value, part, partvalue;
1590Sstevel@tonic-gate 	char address[PBLKSIZ];	/* qualified version */
1600Sstevel@tonic-gate 	char user[PBLKSIZ];		/* unqualified version */
1610Sstevel@tonic-gate 	char userpart[PBLKSIZ];	/* unqualified part of qualified addr. */
1620Sstevel@tonic-gate 	char hostpart[PBLKSIZ];	/* rest of qualified addr. */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	for (key = dbm_firstkey(Scandbm); key.dptr != NULL;
1650Sstevel@tonic-gate 						key = dbm_nextkey(Scandbm)) {
1660Sstevel@tonic-gate 		value = dbm_fetch(Indbm, key);
1670Sstevel@tonic-gate 		CopyName(address, value.dptr, value.dsize);
1680Sstevel@tonic-gate 		CopyName(user, key.dptr, key.dsize);
1690Sstevel@tonic-gate 		if (address == NULL) continue;
1700Sstevel@tonic-gate 		if (IsMailingList(address)) continue;
1710Sstevel@tonic-gate 		if (!IsQualified(address, userpart, hostpart)) continue;
1720Sstevel@tonic-gate 		if (IsMaint(user)) continue;
1730Sstevel@tonic-gate 		if (ErrorCheck && HostCheck(hostpart, address)) {
1740Sstevel@tonic-gate 			printf("Invalid host %s in %s:%s\n",
1750Sstevel@tonic-gate 				hostpart, user, address);
1760Sstevel@tonic-gate 			continue;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 		part.dptr = userpart;
1790Sstevel@tonic-gate 		part.dsize = strlen(userpart) + 1;
1800Sstevel@tonic-gate 		if (Simple)
1810Sstevel@tonic-gate 			partvalue.dptr = NULL;
1820Sstevel@tonic-gate 		else
1830Sstevel@tonic-gate 			partvalue = dbm_fetch(Indbm, part);
1840Sstevel@tonic-gate 		value.dptr = address;
1850Sstevel@tonic-gate 		value.dsize = strlen(address) + 1;
1860Sstevel@tonic-gate 		if (partvalue.dptr != NULL &&
1870Sstevel@tonic-gate 			Compare(partvalue.dptr, user) == 0) {
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			if (NameMode)
1900Sstevel@tonic-gate 				DoName(userpart);
1910Sstevel@tonic-gate 			if (!NoOutput)
1920Sstevel@tonic-gate 				dbm_store(Outdbm, value, part, DBM_REPLACE);
1930Sstevel@tonic-gate 			if (Verbose) printf("%s --> %s --> %s\n",
1940Sstevel@tonic-gate 						userpart, user, address);
1950Sstevel@tonic-gate 		} else {
1960Sstevel@tonic-gate 			if (NameMode)
1970Sstevel@tonic-gate 				DoName(user);
1980Sstevel@tonic-gate 			key.dptr = user;
1990Sstevel@tonic-gate 			key.dsize = strlen(user) + 1;
2000Sstevel@tonic-gate 			if (!NoOutput)
2010Sstevel@tonic-gate 				dbm_store(Outdbm, value, key, DBM_REPLACE);
2020Sstevel@tonic-gate 			if (Verbose)
2030Sstevel@tonic-gate 				printf("%s --> %s\n", user, address);
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Returns true if this is an invalid host
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate int
HostCheck(char * h,char * a)2130Sstevel@tonic-gate HostCheck(char *h, char *a)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	struct hostent *hp;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (DomainOK && strchr(a, '.'))
2180Sstevel@tonic-gate 		return (0);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (UucpOK && strchr(a, '!'))
2210Sstevel@tonic-gate 		return (0);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	hp = gethostbyname(h);
2240Sstevel@tonic-gate 	return (hp == NULL);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * Apply some Heurisitcs to upper case-ify the name
2290Sstevel@tonic-gate  * If it has a dot in it.
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate void
DoName(char * cp)2320Sstevel@tonic-gate DoName(char *cp)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	if (strchr(cp, '.') == NULL)
2350Sstevel@tonic-gate 		return;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	while (*cp) {
2380Sstevel@tonic-gate 		UpperCase(cp);
2390Sstevel@tonic-gate 		while (*cp && *cp != '-' && *cp != '.')
2400Sstevel@tonic-gate 			cp++;
2410Sstevel@tonic-gate 		if (*cp)
2420Sstevel@tonic-gate 			cp++;	/* skip past punctuation */
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * upper cases one name - stops at a .
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate void
UpperCase(char * cp)2500Sstevel@tonic-gate UpperCase(char *cp)
2510Sstevel@tonic-gate {
252*702Sth160488 	int ch = cp[0];
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (isupper(ch))
2550Sstevel@tonic-gate 		ch = tolower(ch);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (ch == 'f' && cp[1] == 'f')
2580Sstevel@tonic-gate 		return; /* handle ff */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (ch == 'm' && cp[1] == 'c' && islower(cp[2]))
2610Sstevel@tonic-gate 		cp[2] = toupper(cp[2]);
2620Sstevel@tonic-gate 	if (islower(ch))
2630Sstevel@tonic-gate 		cp[0] = toupper(ch);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate void
AddYPEntries(void)2670Sstevel@tonic-gate AddYPEntries(void)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	datum key, value;
2700Sstevel@tonic-gate 	char last_modified[PBLKSIZ];
2710Sstevel@tonic-gate 	char host_name[PBLKSIZ];
2720Sstevel@tonic-gate 	time_t now;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Add the special NIS entries.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	key.dptr = yp_last_modified;
2780Sstevel@tonic-gate 	key.dsize = yp_last_modified_sz;
2790Sstevel@tonic-gate 	time(&now);
2800Sstevel@tonic-gate 	sprintf(last_modified, "%10.10d", now);
2810Sstevel@tonic-gate 	value.dptr = last_modified;
2820Sstevel@tonic-gate 	value.dsize = strlen(value.dptr);
2830Sstevel@tonic-gate 	dbm_store(Outdbm, key, value, DBM_REPLACE);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	key.dptr = yp_master_name;
2860Sstevel@tonic-gate 	key.dsize = yp_master_name_sz;
2870Sstevel@tonic-gate 	sysinfo(SI_HOSTNAME, host_name, sizeof (host_name));
2880Sstevel@tonic-gate 	value.dptr = host_name;
2890Sstevel@tonic-gate 	value.dsize = strlen(value.dptr);
2900Sstevel@tonic-gate 	dbm_store(Outdbm, key, value, DBM_REPLACE);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate int
main(int argc,char * argv[])2940Sstevel@tonic-gate main(int argc, char *argv[])
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-') {
2970Sstevel@tonic-gate 	switch (argv[1][1]) {
2980Sstevel@tonic-gate 		case 'v':
2990Sstevel@tonic-gate 			Verbose = 1;
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		case 'u':
3030Sstevel@tonic-gate 			UucpOK = 1;
3040Sstevel@tonic-gate 			break;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		case 'd':
3070Sstevel@tonic-gate 			DomainOK = 1;
3080Sstevel@tonic-gate 			break;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		case 'e':
3110Sstevel@tonic-gate 			ErrorCheck = 1;
3120Sstevel@tonic-gate 			break;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		case 's':
3150Sstevel@tonic-gate 			Simple = 1;
3160Sstevel@tonic-gate 			break;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		case 'n':
3190Sstevel@tonic-gate 			NameMode = 1;
3200Sstevel@tonic-gate 			break;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		default:
3230Sstevel@tonic-gate 			printf("Unknown option %c\n", argv[1][1]);
3240Sstevel@tonic-gate 			break;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 		argc--; argv++;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	if (argc < 2) {
3290Sstevel@tonic-gate printf("Usage: mkalias [-e] [-v] [-u] [-d] [-s] [-n] <input> <output>\n");
3300Sstevel@tonic-gate 		exit(1);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	Indbm = dbm_open(argv[1], O_RDONLY, 0);
3330Sstevel@tonic-gate 	if (Indbm == NULL) {
3340Sstevel@tonic-gate 		printf("Unable to open input database %s\n", argv[1]);
3350Sstevel@tonic-gate 		exit(1);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	Scandbm = dbm_open(argv[1], O_RDONLY, 0);
3380Sstevel@tonic-gate 	if (Scandbm == NULL) {
3390Sstevel@tonic-gate 		printf("Unable to open input database %s\n", argv[1]);
3400Sstevel@tonic-gate 		exit(1);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 	if (argv[2] == NULL)
3430Sstevel@tonic-gate 		NoOutput = 1;
3440Sstevel@tonic-gate 	else {
3450Sstevel@tonic-gate 		Outdbm = dbm_open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0644);
3460Sstevel@tonic-gate 		if (Outdbm == NULL) {
3470Sstevel@tonic-gate 			printf("Unable to open output database %s\n", argv[2]);
3480Sstevel@tonic-gate 			exit(1);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 	ProcessMap();
3520Sstevel@tonic-gate 	dbm_close(Indbm);
3530Sstevel@tonic-gate 	dbm_close(Scandbm);
3540Sstevel@tonic-gate 	if (!NoOutput) {
3550Sstevel@tonic-gate 		AddYPEntries();
3560Sstevel@tonic-gate 		dbm_close(Outdbm);
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 	return (0);
3590Sstevel@tonic-gate }
360