1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 1996, by Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* 28*0Sstevel@tonic-gate * mkmap - program to convert the mail.aliases map into an 29*0Sstevel@tonic-gate * inverse map of <user@host> back to <preferred-alias> 30*0Sstevel@tonic-gate */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include <stdlib.h> 35*0Sstevel@tonic-gate #include <unistd.h> 36*0Sstevel@tonic-gate #include <string.h> 37*0Sstevel@tonic-gate #include <fcntl.h> 38*0Sstevel@tonic-gate #include <ndbm.h> 39*0Sstevel@tonic-gate #include <stdio.h> 40*0Sstevel@tonic-gate #include <ctype.h> 41*0Sstevel@tonic-gate #include <netdb.h> 42*0Sstevel@tonic-gate #include <sys/systeminfo.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #include "ypdefs.h" 45*0Sstevel@tonic-gate USE_YP_PREFIX 46*0Sstevel@tonic-gate USE_YP_MASTER_NAME 47*0Sstevel@tonic-gate USE_YP_LAST_MODIFIED 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #define MKAL_INCLUDE ":include:" 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate void CopyName(char *dst, char *src, int len); 52*0Sstevel@tonic-gate int HostCheck(char *h, char *a); 53*0Sstevel@tonic-gate void DoName(char *cp); 54*0Sstevel@tonic-gate void UpperCase(char *cp); 55*0Sstevel@tonic-gate void AddYPEntries(void); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate int Verbose = 0; /* to get the gory details */ 58*0Sstevel@tonic-gate int UucpOK = 0; /* pass all UUCP names right through */ 59*0Sstevel@tonic-gate int DomainOK = 0; /* pass all Domain names (with dots) */ 60*0Sstevel@tonic-gate int ErrorCheck = 0; /* check carefully for errors */ 61*0Sstevel@tonic-gate int NoOutput = 0; /* no output, just do the check */ 62*0Sstevel@tonic-gate int Simple = 0; /* Do not do the user name preference step */ 63*0Sstevel@tonic-gate int NameMode = 0; /* Try to capitalize as names */ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate DBM *Indbm=NULL, *Scandbm=NULL, *Outdbm=NULL; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate int 68*0Sstevel@tonic-gate IsMailingList(char *s) 69*0Sstevel@tonic-gate { 70*0Sstevel@tonic-gate /* 71*0Sstevel@tonic-gate * returns true if the given string is a mailing list 72*0Sstevel@tonic-gate */ 73*0Sstevel@tonic-gate char *p; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate if (strchr(s, ',')) 76*0Sstevel@tonic-gate return (1); 77*0Sstevel@tonic-gate if (strchr(s, '|')) 78*0Sstevel@tonic-gate return (1); 79*0Sstevel@tonic-gate p = strchr(s, ':'); 80*0Sstevel@tonic-gate if (p && strncmp(p, MKAL_INCLUDE, sizeof (MKAL_INCLUDE))) 81*0Sstevel@tonic-gate return (1); 82*0Sstevel@tonic-gate return (0); 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate int 86*0Sstevel@tonic-gate IsQualified(char *s, char *p, char *h) 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate /* 89*0Sstevel@tonic-gate * returns true if the given string is qualified with a host name 90*0Sstevel@tonic-gate */ 91*0Sstevel@tonic-gate register char *middle; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate middle = strchr(s, '@'); 94*0Sstevel@tonic-gate if (middle) { 95*0Sstevel@tonic-gate for (middle = s; *middle != '@'; *p++ = *middle++) 96*0Sstevel@tonic-gate continue; 97*0Sstevel@tonic-gate *p = '\0'; 98*0Sstevel@tonic-gate CopyName(h, middle+1, strlen(middle + 1)); 99*0Sstevel@tonic-gate return (1); 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate middle = strrchr(s, '!'); 102*0Sstevel@tonic-gate if (middle) { 103*0Sstevel@tonic-gate strcpy(p, middle+1); 104*0Sstevel@tonic-gate *middle = '\0'; 105*0Sstevel@tonic-gate CopyName(h, s, strlen(s)); 106*0Sstevel@tonic-gate *middle = '!'; 107*0Sstevel@tonic-gate return (1); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate return (0); 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate int 113*0Sstevel@tonic-gate IsMaint(char *s) 114*0Sstevel@tonic-gate { 115*0Sstevel@tonic-gate /* 116*0Sstevel@tonic-gate * returns true if the given string is one of the maintenence 117*0Sstevel@tonic-gate * strings used in sendmail or NIS. 118*0Sstevel@tonic-gate */ 119*0Sstevel@tonic-gate if (*s == '@') 120*0Sstevel@tonic-gate return (1); 121*0Sstevel@tonic-gate if (strncmp(s, yp_prefix, yp_prefix_sz) == 0) 122*0Sstevel@tonic-gate return (1); 123*0Sstevel@tonic-gate return (0); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate void 127*0Sstevel@tonic-gate CopyName(char *dst, char *src, int len) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate /* 130*0Sstevel@tonic-gate * copy a string, but ignore white space 131*0Sstevel@tonic-gate */ 132*0Sstevel@tonic-gate while (*src && len--) { 133*0Sstevel@tonic-gate if (isspace(*src)) 134*0Sstevel@tonic-gate src++; 135*0Sstevel@tonic-gate else 136*0Sstevel@tonic-gate *dst++ = *src++; 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate *dst = '\0'; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate int 142*0Sstevel@tonic-gate Compare(char *s1, char *s2) 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate /* 145*0Sstevel@tonic-gate * compare strings, but ignore white space 146*0Sstevel@tonic-gate */ 147*0Sstevel@tonic-gate while (*s1 != '\0' && isspace(*s1)) 148*0Sstevel@tonic-gate s1++; 149*0Sstevel@tonic-gate while (*s2 != '\0' && isspace(*s2)) 150*0Sstevel@tonic-gate s2++; 151*0Sstevel@tonic-gate return (strcmp(s1, s2)); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate void 155*0Sstevel@tonic-gate ProcessMap(void) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate datum key, value, part, partvalue; 158*0Sstevel@tonic-gate char address[PBLKSIZ]; /* qualified version */ 159*0Sstevel@tonic-gate char user[PBLKSIZ]; /* unqualified version */ 160*0Sstevel@tonic-gate char userpart[PBLKSIZ]; /* unqualified part of qualified addr. */ 161*0Sstevel@tonic-gate char hostpart[PBLKSIZ]; /* rest of qualified addr. */ 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate for (key = dbm_firstkey(Scandbm); key.dptr != NULL; 164*0Sstevel@tonic-gate key = dbm_nextkey(Scandbm)) { 165*0Sstevel@tonic-gate value = dbm_fetch(Indbm, key); 166*0Sstevel@tonic-gate CopyName(address, value.dptr, value.dsize); 167*0Sstevel@tonic-gate CopyName(user, key.dptr, key.dsize); 168*0Sstevel@tonic-gate if (address == NULL) continue; 169*0Sstevel@tonic-gate if (IsMailingList(address)) continue; 170*0Sstevel@tonic-gate if (!IsQualified(address, userpart, hostpart)) continue; 171*0Sstevel@tonic-gate if (IsMaint(user)) continue; 172*0Sstevel@tonic-gate if (ErrorCheck && HostCheck(hostpart, address)) { 173*0Sstevel@tonic-gate printf("Invalid host %s in %s:%s\n", 174*0Sstevel@tonic-gate hostpart, user, address); 175*0Sstevel@tonic-gate continue; 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate part.dptr = userpart; 178*0Sstevel@tonic-gate part.dsize = strlen(userpart) + 1; 179*0Sstevel@tonic-gate if (Simple) 180*0Sstevel@tonic-gate partvalue.dptr = NULL; 181*0Sstevel@tonic-gate else 182*0Sstevel@tonic-gate partvalue = dbm_fetch(Indbm, part); 183*0Sstevel@tonic-gate value.dptr = address; 184*0Sstevel@tonic-gate value.dsize = strlen(address) + 1; 185*0Sstevel@tonic-gate if (partvalue.dptr != NULL && 186*0Sstevel@tonic-gate Compare(partvalue.dptr, user) == 0) { 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate if (NameMode) 189*0Sstevel@tonic-gate DoName(userpart); 190*0Sstevel@tonic-gate if (!NoOutput) 191*0Sstevel@tonic-gate dbm_store(Outdbm, value, part, DBM_REPLACE); 192*0Sstevel@tonic-gate if (Verbose) printf("%s --> %s --> %s\n", 193*0Sstevel@tonic-gate userpart, user, address); 194*0Sstevel@tonic-gate } else { 195*0Sstevel@tonic-gate if (NameMode) 196*0Sstevel@tonic-gate DoName(user); 197*0Sstevel@tonic-gate key.dptr = user; 198*0Sstevel@tonic-gate key.dsize = strlen(user) + 1; 199*0Sstevel@tonic-gate if (!NoOutput) 200*0Sstevel@tonic-gate dbm_store(Outdbm, value, key, DBM_REPLACE); 201*0Sstevel@tonic-gate if (Verbose) 202*0Sstevel@tonic-gate printf("%s --> %s\n", user, address); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate /* 209*0Sstevel@tonic-gate * Returns true if this is an invalid host 210*0Sstevel@tonic-gate */ 211*0Sstevel@tonic-gate int 212*0Sstevel@tonic-gate HostCheck(char *h, char *a) 213*0Sstevel@tonic-gate { 214*0Sstevel@tonic-gate struct hostent *hp; 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate if (DomainOK && strchr(a, '.')) 217*0Sstevel@tonic-gate return (0); 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate if (UucpOK && strchr(a, '!')) 220*0Sstevel@tonic-gate return (0); 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate hp = gethostbyname(h); 223*0Sstevel@tonic-gate return (hp == NULL); 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate /* 227*0Sstevel@tonic-gate * Apply some Heurisitcs to upper case-ify the name 228*0Sstevel@tonic-gate * If it has a dot in it. 229*0Sstevel@tonic-gate */ 230*0Sstevel@tonic-gate void 231*0Sstevel@tonic-gate DoName(char *cp) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate if (strchr(cp, '.') == NULL) 234*0Sstevel@tonic-gate return; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate while (*cp) { 237*0Sstevel@tonic-gate UpperCase(cp); 238*0Sstevel@tonic-gate while (*cp && *cp != '-' && *cp != '.') 239*0Sstevel@tonic-gate cp++; 240*0Sstevel@tonic-gate if (*cp) 241*0Sstevel@tonic-gate cp++; /* skip past punctuation */ 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate /* 246*0Sstevel@tonic-gate * upper cases one name - stops at a . 247*0Sstevel@tonic-gate */ 248*0Sstevel@tonic-gate void 249*0Sstevel@tonic-gate UpperCase(char *cp) 250*0Sstevel@tonic-gate { 251*0Sstevel@tonic-gate register ch = cp[0]; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if (isupper(ch)) 254*0Sstevel@tonic-gate ch = tolower(ch); 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate if (ch == 'f' && cp[1] == 'f') 257*0Sstevel@tonic-gate return; /* handle ff */ 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate if (ch == 'm' && cp[1] == 'c' && islower(cp[2])) 260*0Sstevel@tonic-gate cp[2] = toupper(cp[2]); 261*0Sstevel@tonic-gate if (islower(ch)) 262*0Sstevel@tonic-gate cp[0] = toupper(ch); 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate void 266*0Sstevel@tonic-gate AddYPEntries(void) 267*0Sstevel@tonic-gate { 268*0Sstevel@tonic-gate datum key, value; 269*0Sstevel@tonic-gate char last_modified[PBLKSIZ]; 270*0Sstevel@tonic-gate char host_name[PBLKSIZ]; 271*0Sstevel@tonic-gate time_t now; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate /* 274*0Sstevel@tonic-gate * Add the special NIS entries. 275*0Sstevel@tonic-gate */ 276*0Sstevel@tonic-gate key.dptr = yp_last_modified; 277*0Sstevel@tonic-gate key.dsize = yp_last_modified_sz; 278*0Sstevel@tonic-gate time(&now); 279*0Sstevel@tonic-gate sprintf(last_modified, "%10.10d", now); 280*0Sstevel@tonic-gate value.dptr = last_modified; 281*0Sstevel@tonic-gate value.dsize = strlen(value.dptr); 282*0Sstevel@tonic-gate dbm_store(Outdbm, key, value, DBM_REPLACE); 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate key.dptr = yp_master_name; 285*0Sstevel@tonic-gate key.dsize = yp_master_name_sz; 286*0Sstevel@tonic-gate sysinfo(SI_HOSTNAME, host_name, sizeof (host_name)); 287*0Sstevel@tonic-gate value.dptr = host_name; 288*0Sstevel@tonic-gate value.dsize = strlen(value.dptr); 289*0Sstevel@tonic-gate dbm_store(Outdbm, key, value, DBM_REPLACE); 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate int 293*0Sstevel@tonic-gate main(int argc, char *argv[]) 294*0Sstevel@tonic-gate { 295*0Sstevel@tonic-gate while (argc > 1 && argv[1][0] == '-') { 296*0Sstevel@tonic-gate switch (argv[1][1]) { 297*0Sstevel@tonic-gate case 'v': 298*0Sstevel@tonic-gate Verbose = 1; 299*0Sstevel@tonic-gate break; 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate case 'u': 302*0Sstevel@tonic-gate UucpOK = 1; 303*0Sstevel@tonic-gate break; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate case 'd': 306*0Sstevel@tonic-gate DomainOK = 1; 307*0Sstevel@tonic-gate break; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate case 'e': 310*0Sstevel@tonic-gate ErrorCheck = 1; 311*0Sstevel@tonic-gate break; 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate case 's': 314*0Sstevel@tonic-gate Simple = 1; 315*0Sstevel@tonic-gate break; 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate case 'n': 318*0Sstevel@tonic-gate NameMode = 1; 319*0Sstevel@tonic-gate break; 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate default: 322*0Sstevel@tonic-gate printf("Unknown option %c\n", argv[1][1]); 323*0Sstevel@tonic-gate break; 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate argc--; argv++; 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate if (argc < 2) { 328*0Sstevel@tonic-gate printf("Usage: mkalias [-e] [-v] [-u] [-d] [-s] [-n] <input> <output>\n"); 329*0Sstevel@tonic-gate exit(1); 330*0Sstevel@tonic-gate } 331*0Sstevel@tonic-gate Indbm = dbm_open(argv[1], O_RDONLY, 0); 332*0Sstevel@tonic-gate if (Indbm == NULL) { 333*0Sstevel@tonic-gate printf("Unable to open input database %s\n", argv[1]); 334*0Sstevel@tonic-gate exit(1); 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate Scandbm = dbm_open(argv[1], O_RDONLY, 0); 337*0Sstevel@tonic-gate if (Scandbm == NULL) { 338*0Sstevel@tonic-gate printf("Unable to open input database %s\n", argv[1]); 339*0Sstevel@tonic-gate exit(1); 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate if (argv[2] == NULL) 342*0Sstevel@tonic-gate NoOutput = 1; 343*0Sstevel@tonic-gate else { 344*0Sstevel@tonic-gate Outdbm = dbm_open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0644); 345*0Sstevel@tonic-gate if (Outdbm == NULL) { 346*0Sstevel@tonic-gate printf("Unable to open output database %s\n", argv[2]); 347*0Sstevel@tonic-gate exit(1); 348*0Sstevel@tonic-gate } 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate ProcessMap(); 351*0Sstevel@tonic-gate dbm_close(Indbm); 352*0Sstevel@tonic-gate dbm_close(Scandbm); 353*0Sstevel@tonic-gate if (!NoOutput) { 354*0Sstevel@tonic-gate AddYPEntries(); 355*0Sstevel@tonic-gate dbm_close(Outdbm); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate return (0); 358*0Sstevel@tonic-gate } 359