praliases.c revision 38033
1/* 2 * Copyright (c) 1998 Sendmail, Inc. All rights reserved. 3 * Copyright (c) 1983 Eric P. Allman. All rights reserved. 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * By using this file, you agree to the terms and conditions set 8 * forth in the LICENSE file which can be found at the top level of 9 * the sendmail distribution. 10 * 11 */ 12 13#ifndef lint 14static char copyright[] = 15"@(#) Copyright (c) 1988, 1993\n\ 16 The Regents of the University of California. All rights reserved.\n"; 17#endif /* not lint */ 18 19#ifndef lint 20static char sccsid[] = "@(#)praliases.c 8.17 (Berkeley) 6/25/98"; 21#endif /* not lint */ 22 23#if !defined(NDBM) && !defined(NEWDB) 24 ERROR README: You must define one of NDBM or NEWDB in order to compile 25 ERROR README: praliases. 26#endif 27 28#ifdef NDBM 29# include <ndbm.h> 30#endif 31#ifndef NOT_SENDMAIL 32# define NOT_SENDMAIL 33#endif 34#include <sendmail.h> 35#ifdef NEWDB 36# include <db.h> 37# ifndef DB_VERSION_MAJOR 38# define DB_VERSION_MAJOR 1 39# endif 40#endif 41 42#if defined(IRIX64) || defined(IRIX5) || defined(IRIX6) || \ 43 defined(BSD4_4) || defined(__osf__) || defined(__GNU_LIBRARY__) 44# ifndef HASSTRERROR 45# define HASSTRERROR 1 /* has strerror(3) */ 46# endif 47#endif 48 49#if !HASSTRERROR 50extern char *strerror __P((int)); 51#endif 52 53int 54main(argc, argv) 55 int argc; 56 char **argv; 57{ 58 extern char *optarg; 59 extern int optind; 60#ifdef NDBM 61 DBM *dbp; 62 datum content, key; 63#endif 64 char *filename; 65 int ch; 66#ifdef NEWDB 67 DB *db; 68 DBT newdbkey, newdbcontent; 69 char buf[MAXNAME]; 70#endif 71 72 filename = "/etc/aliases"; 73 while ((ch = getopt(argc, argv, "f:")) != EOF) 74 switch((char)ch) { 75 case 'f': 76 filename = optarg; 77 break; 78 case '?': 79 default: 80 (void)fprintf(stderr, "usage: praliases [-f file]\n"); 81 exit(EX_USAGE); 82 } 83 argc -= optind; 84 argv += optind; 85 86#ifdef NEWDB 87 if (strlen(filename) + 4 >= sizeof buf) 88 { 89 fprintf(stderr, "Alias filename too long: %.30s...\n", filename); 90 exit(EX_USAGE); 91 } 92 (void) strcpy(buf, filename); 93 (void) strcat(buf, ".db"); 94# if DB_VERSION_MAJOR < 2 95 db = dbopen(buf, O_RDONLY, 0444, DB_HASH, NULL); 96# else 97 db = NULL; 98 errno = db_open(buf, DB_HASH, DB_RDONLY, 0444, NULL, NULL, &db); 99# endif 100 if (db != NULL) 101 { 102 if (!argc) { 103# if DB_VERSION_MAJOR > 1 104 DBC *dbc; 105# endif 106 bzero(&newdbkey, sizeof newdbkey); 107 bzero(&newdbcontent, sizeof newdbcontent); 108 109# if DB_VERSION_MAJOR < 2 110 while(!db->seq(db, &newdbkey, &newdbcontent, R_NEXT)) 111# else 112 if ((errno = db->cursor(db, NULL, &dbc)) == 0) 113 { 114 while ((errno = dbc->c_get(dbc, &newdbkey, 115 &newdbcontent, 116 DB_NEXT)) == 0) 117# endif 118 printf("%.*s:%.*s\n", 119 (int) newdbkey.size, 120 (char *) newdbkey.data, 121 (int) newdbcontent.size, 122 (char *) newdbcontent.data); 123# if DB_VERSION_MAJOR > 1 124 (void) dbc->c_close(dbc); 125 } 126 else 127 { 128 fprintf(stderr, 129 "praliases: %s: Could not set cursor: %s\n", 130 buf, strerror(errno)); 131 exit(EX_DATAERR); 132 } 133# endif 134 } 135 else for (; *argv; ++argv) { 136 bzero(&newdbkey, sizeof newdbkey); 137 bzero(&newdbcontent, sizeof newdbcontent); 138 newdbkey.data = *argv; 139 newdbkey.size = strlen(*argv) + 1; 140# if DB_VERSION_MAJOR < 2 141 if (!db->get(db, &newdbkey, &newdbcontent, 0)) 142# else 143 if ((errno = db->get(db, NULL, &newdbkey, 144 &newdbcontent, 0)) == 0) 145# endif 146 printf("%s:%.*s\n", (char *) newdbkey.data, 147 (int) newdbcontent.size, 148 (char *) newdbcontent.data); 149 else 150 printf("%s: No such key\n", 151 (char *) newdbkey.data); 152 } 153# if DB_VERSION_MAJOR < 2 154 (void)db->close(db); 155# else 156 errno = db->close(db, 0); 157# endif 158 } 159 else { 160#endif 161#ifdef NDBM 162 if ((dbp = dbm_open(filename, O_RDONLY, 0)) == NULL) { 163 (void)fprintf(stderr, 164 "praliases: %s: %s\n", filename, strerror(errno)); 165 exit(EX_OSFILE); 166 } 167 if (!argc) 168 for (key = dbm_firstkey(dbp); 169 key.dptr != NULL; key = dbm_nextkey(dbp)) { 170 content = dbm_fetch(dbp, key); 171 (void)printf("%.*s:%.*s\n", 172 (int) key.dsize, key.dptr, 173 (int) content.dsize, content.dptr); 174 } 175 else for (; *argv; ++argv) { 176 key.dptr = *argv; 177 key.dsize = strlen(*argv) + 1; 178 content = dbm_fetch(dbp, key); 179 if (!content.dptr) 180 (void)printf("%s: No such key\n", key.dptr); 181 else 182 (void)printf("%s:%.*s\n", key.dptr, 183 (int) content.dsize, content.dptr); 184 } 185 dbm_close(dbp); 186#endif 187#ifdef NEWDB 188 } 189#endif 190 exit(EX_OK); 191} 192 193#if !HASSTRERROR 194 195char * 196strerror(eno) 197 int eno; 198{ 199 extern int sys_nerr; 200 extern char *sys_errlist[]; 201 static char ebuf[60]; 202 203 if (eno >= 0 && eno < sys_nerr) 204 return sys_errlist[eno]; 205 (void) sprintf(ebuf, "Error %d", eno); 206 return ebuf; 207} 208 209#endif /* !HASSTRERROR */ 210