150581Seric /* 250581Seric * Copyright (c) 1983 Eric P. Allman 362532Sbostic * Copyright (c) 1988, 1993 462532Sbostic * The Regents of the University of California. All rights reserved. 550581Seric * 650581Seric * %sccs.include.redist.c% 750581Seric */ 850581Seric 960567Seric #include "sendmail.h" 1060567Seric 1150581Seric #ifndef lint 1251360Seric #ifdef USERDB 13*68544Seric static char sccsid [] = "@(#)udb.c 8.15 (Berkeley) 03/20/95 (with USERDB)"; 1451360Seric #else 15*68544Seric static char sccsid [] = "@(#)udb.c 8.15 (Berkeley) 03/20/95 (without USERDB)"; 1650581Seric #endif 1751360Seric #endif 1850581Seric 1950581Seric #ifdef USERDB 2050581Seric 2151923Seric #include <errno.h> 2251360Seric #include <netdb.h> 2350581Seric #include <db.h> 2450581Seric 2566759Seric #ifdef HESIOD 2666759Seric #include <hesiod.h> 2766759Seric #endif /* HESIOD */ 2866759Seric 2950581Seric /* 3053654Seric ** UDB.C -- interface between sendmail and Berkeley User Data Base. 3150581Seric ** 3251363Seric ** This depends on the 4.4BSD db package. 3350581Seric */ 3450581Seric 3551362Seric 3651360Seric struct udbent 3751360Seric { 3851360Seric char *udb_spec; /* string version of spec */ 3951360Seric int udb_type; /* type of entry */ 4051951Seric char *udb_default; /* default host for outgoing mail */ 4151360Seric union 4251360Seric { 4351360Seric /* type UE_REMOTE -- do remote call for lookup */ 4451360Seric struct 4551360Seric { 4651360Seric struct sockaddr_in _udb_addr; /* address */ 4751360Seric int _udb_timeout; /* timeout */ 4851360Seric } udb_remote; 4951360Seric #define udb_addr udb_u.udb_remote._udb_addr 5051360Seric #define udb_timeout udb_u.udb_remote._udb_timeout 5151360Seric 5251360Seric /* type UE_FORWARD -- forward message to remote */ 5351360Seric struct 5451360Seric { 5551360Seric char *_udb_fwdhost; /* name of forward host */ 5651360Seric } udb_forward; 5751360Seric #define udb_fwdhost udb_u.udb_forward._udb_fwdhost 5851360Seric 5951951Seric /* type UE_FETCH -- lookup in local database */ 6051360Seric struct 6151360Seric { 6251360Seric char *_udb_dbname; /* pathname of database */ 6351360Seric DB *_udb_dbp; /* open database ptr */ 6451360Seric } udb_lookup; 6551360Seric #define udb_dbname udb_u.udb_lookup._udb_dbname 6651360Seric #define udb_dbp udb_u.udb_lookup._udb_dbp 6751360Seric } udb_u; 6851360Seric }; 6951360Seric 7051360Seric #define UDB_EOLIST 0 /* end of list */ 7151360Seric #define UDB_SKIP 1 /* skip this entry */ 7251360Seric #define UDB_REMOTE 2 /* look up in remote database */ 7351951Seric #define UDB_DBFETCH 3 /* look up in local database */ 7451360Seric #define UDB_FORWARD 4 /* forward to remote host */ 7566759Seric #define UDB_HESIOD 5 /* look up via hesiod */ 7651360Seric 7751360Seric #define MAXUDBENT 10 /* maximum number of UDB entries */ 7851360Seric 7951363Seric 8051363Seric struct option 8151363Seric { 8251363Seric char *name; 8351363Seric char *val; 8451363Seric }; 8551363Seric /* 8651363Seric ** UDBEXPAND -- look up user in database and expand 8751363Seric ** 8851363Seric ** Parameters: 8951363Seric ** a -- address to expand. 9051363Seric ** sendq -- pointer to head of sendq to put the expansions in. 9167982Seric ** aliaslevel -- the current alias nesting depth. 9267982Seric ** e -- the current envelope. 9351363Seric ** 9451363Seric ** Returns: 9551923Seric ** EX_TEMPFAIL -- if something "odd" happened -- probably due 9651923Seric ** to accessing a file on an NFS server that is down. 9751923Seric ** EX_OK -- otherwise. 9851363Seric ** 9951363Seric ** Side Effects: 10051363Seric ** Modifies sendq. 10151363Seric */ 10251363Seric 10351363Seric int UdbPort = 1616; 10451363Seric int UdbTimeout = 10; 10551363Seric 10651953Seric struct udbent UdbEnts[MAXUDBENT + 1]; 10751953Seric int UdbSock = -1; 10851953Seric bool UdbInitialized = FALSE; 10951360Seric 11051923Seric int 11167982Seric udbexpand(a, sendq, aliaslevel, e) 11250581Seric register ADDRESS *a; 11350581Seric ADDRESS **sendq; 11467982Seric int aliaslevel; 11555012Seric register ENVELOPE *e; 11650581Seric { 11750581Seric int i; 11850581Seric register char *p; 11950581Seric DBT key; 12050581Seric DBT info; 12151360Seric bool breakout; 12251360Seric register struct udbent *up; 12351362Seric int keylen; 12458082Seric int naddrs; 12557232Seric char keybuf[MAXKEY]; 12657232Seric char buf[BUFSIZ]; 12750581Seric 12850581Seric if (tTd(28, 1)) 12958065Seric printf("udbexpand(%s)\n", a->q_paddr); 13050581Seric 13150581Seric /* make certain we are supposed to send to this address */ 13258154Seric if (bitset(QDONTSEND|QVERIFIED, a->q_flags)) 13351923Seric return EX_OK; 13455012Seric e->e_to = a->q_paddr; 13550581Seric 13651360Seric /* on first call, locate the database */ 13751951Seric if (!UdbInitialized) 13850581Seric { 13951923Seric extern int _udbx_init(); 14051362Seric 14151923Seric if (_udbx_init() == EX_TEMPFAIL) 14251923Seric return EX_TEMPFAIL; 14351362Seric } 14450581Seric 14551909Seric /* short circuit the process if no chance of a match */ 14651909Seric if (UdbSpec == NULL || UdbSpec[0] == '\0') 14751923Seric return EX_OK; 14851909Seric 14966759Seric /* short circuit name begins with '\\' since it can't possibly match */ 15066759Seric if (a->q_user[0] == '\\') 15166759Seric return EX_OK; 15266759Seric 15351362Seric /* if name is too long, assume it won't match */ 15451362Seric if (strlen(a->q_user) > sizeof keybuf - 12) 15551923Seric return EX_OK; 15651360Seric 15751362Seric /* if name begins with a colon, it indicates our metadata */ 15851362Seric if (a->q_user[0] == ':') 15951923Seric return EX_OK; 16051360Seric 16151362Seric /* build actual database key */ 16251362Seric (void) strcpy(keybuf, a->q_user); 16351362Seric (void) strcat(keybuf, ":maildrop"); 16451362Seric keylen = strlen(keybuf); 16551360Seric 16651360Seric breakout = FALSE; 16751362Seric for (up = UdbEnts; !breakout; up++) 16850581Seric { 16951360Seric char *user; 17050581Seric 17151360Seric /* 17251360Seric ** Select action based on entry type. 17351360Seric ** 17451360Seric ** On dropping out of this switch, "class" should 17551360Seric ** explain the type of the data, and "user" should 17651360Seric ** contain the user information. 17751360Seric */ 17850581Seric 17951360Seric switch (up->udb_type) 18051360Seric { 18151951Seric case UDB_DBFETCH: 18251362Seric key.data = keybuf; 18351362Seric key.size = keylen; 18460990Seric if (tTd(28, 80)) 18566759Seric printf("udbexpand: trying %s (%d) via db\n", 18664067Seric keybuf, keylen); 18751362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR); 18851923Seric if (i > 0 || info.size <= 0) 18951360Seric { 19051360Seric if (tTd(28, 2)) 19164067Seric printf("udbexpand: no match on %s (%d)\n", 19264067Seric keybuf, keylen); 19351360Seric continue; 19451360Seric } 19560990Seric if (tTd(28, 80)) 19660990Seric printf("udbexpand: match %.*s: %.*s\n", 19760990Seric key.size, key.data, info.size, info.data); 19850581Seric 19958082Seric naddrs = 0; 20058082Seric a->q_flags &= ~QSELFREF; 20151830Seric while (i == 0 && key.size == keylen && 20251830Seric bcmp(key.data, keybuf, keylen) == 0) 20351362Seric { 20458099Seric if (bitset(EF_VRFYONLY, e->e_flags)) 20558154Seric { 20658154Seric a->q_flags |= QVERIFIED; 20758884Seric e->e_nrcpts++; 20858099Seric return EX_OK; 20958154Seric } 21058099Seric 21151830Seric breakout = TRUE; 21251362Seric if (info.size < sizeof buf) 21351362Seric user = buf; 21451362Seric else 21551362Seric user = xalloc(info.size + 1); 21651362Seric bcopy(info.data, user, info.size); 21751362Seric user[info.size] = '\0'; 21850581Seric 21958151Seric message("expanded to %s", user); 22057977Seric #ifdef LOG 22157977Seric if (LogLevel >= 10) 22257977Seric syslog(LOG_INFO, "%s: expand %s => %s", 22357977Seric e->e_id, e->e_to, user); 22457977Seric #endif 22567982Seric naddrs += sendtolist(user, a, sendq, aliaslevel + 1, e); 22651362Seric 22751362Seric if (user != buf) 22851362Seric free(user); 22951362Seric 23051362Seric /* get the next record */ 23151362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT); 23251830Seric } 23360990Seric 23460990Seric /* if nothing ever matched, try next database */ 23560990Seric if (!breakout) 23660990Seric continue; 23760990Seric 23858082Seric if (naddrs > 0 && !bitset(QSELFREF, a->q_flags)) 23958065Seric { 24058065Seric if (tTd(28, 5)) 24158065Seric { 24258065Seric printf("udbexpand: QDONTSEND "); 24358065Seric printaddr(a, FALSE); 24458065Seric } 24558065Seric a->q_flags |= QDONTSEND; 24658065Seric } 24751923Seric if (i < 0) 24851923Seric { 24958010Seric syserr("udbexpand: db-get %.*s stat %d", 25058010Seric key.size, key.data, i); 25151923Seric return EX_TEMPFAIL; 25251923Seric } 25359707Seric 25459707Seric /* 25559707Seric ** If this address has a -request address, reflect 25659707Seric ** it into the envelope. 25759707Seric */ 25859707Seric 25959707Seric (void) strcpy(keybuf, a->q_user); 26059707Seric (void) strcat(keybuf, ":mailsender"); 26159707Seric keylen = strlen(keybuf); 26259707Seric key.data = keybuf; 26359707Seric key.size = keylen; 26459707Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 26559707Seric if (i != 0 || info.size <= 0) 26659707Seric break; 26759707Seric a->q_owner = xalloc(info.size + 1); 26859707Seric bcopy(info.data, a->q_owner, info.size); 26959707Seric a->q_owner[info.size] = '\0'; 27066784Seric 27166784Seric /* announce delivery; NORECEIPT bit set later */ 27266784Seric if (e->e_xfp != NULL) 27366784Seric { 27466784Seric fprintf(e->e_xfp, 27566784Seric "Message delivered to mailing list %s\n", 27666784Seric a->q_paddr); 27766784Seric e->e_flags |= EF_SENDRECEIPT; 27866784Seric } 27951360Seric break; 28051360Seric 28166759Seric #ifdef HESIOD 28266759Seric case UDB_HESIOD: 28366759Seric key.data = keybuf; 28466759Seric key.size = keylen; 28566759Seric if (tTd(28, 80)) 28666759Seric printf("udbexpand: trying %s (%d) via hesiod\n", 28766759Seric keybuf, keylen); 28866759Seric /* look up the key via hesiod */ 28966759Seric i = hes_udb_get(&key, &info); 29066759Seric if (i > 0 || info.size <= 0) 29166759Seric { 29266759Seric if (tTd(28, 2)) 29366759Seric printf("udbexpand: no match on %s (%d)\n", 29466759Seric keybuf, keylen); 29566759Seric continue; 29666759Seric } 29766759Seric if (tTd(28, 80)) 29866759Seric printf("udbexpand: match %.*s: %.*s\n", 29966759Seric key.size, key.data, info.size, info.data); 30066759Seric a->q_flags &= ~QSELFREF; 30166759Seric 30266759Seric if (bitset(EF_VRFYONLY, e->e_flags)) 30366759Seric { 30466759Seric a->q_flags |= QVERIFIED; 30566759Seric e->e_nrcpts++; 30666759Seric free(info.data); 30766759Seric return EX_OK; 30866759Seric } 30966759Seric 31066759Seric breakout = TRUE; 31166759Seric if (info.size < sizeof buf) 31266759Seric user = buf; 31366759Seric else 31466759Seric user = xalloc(info.size + 1); 31566759Seric bcopy(info.data, user, info.size); 31666759Seric user[info.size] = '\0'; 31766759Seric free(info.data); 31866759Seric 31966759Seric message("hesioded to %s", user); 32066759Seric #ifdef LOG 32166759Seric if (LogLevel >= 10) 32266759Seric syslog(LOG_INFO, "%s: hesiod %s => %s", 32366759Seric e->e_id, e->e_to, user); 32466759Seric #endif 32567982Seric naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e); 32666759Seric 32766759Seric if (user != buf) 32866759Seric free(user); 32966759Seric 33066759Seric if (naddrs > 0 && !bitset(QSELFREF, a->q_flags)) 33166759Seric { 33266759Seric if (tTd(28, 5)) 33366759Seric { 33466759Seric printf("udbexpand: QDONTSEND "); 33566759Seric printaddr(a, FALSE); 33666759Seric } 33766759Seric a->q_flags |= QDONTSEND; 33866759Seric } 33966759Seric if (i < 0) 34066759Seric { 34166759Seric syserr("udbexpand: hesiod-get %.*s stat %d", 34266759Seric key.size, key.data, i); 34366759Seric return EX_TEMPFAIL; 34466759Seric } 34566759Seric 34666759Seric /* 34766759Seric ** If this address has a -request address, reflect 34866759Seric ** it into the envelope. 34966759Seric */ 35066759Seric 35166759Seric (void) strcpy(keybuf, a->q_user); 35266759Seric (void) strcat(keybuf, ":mailsender"); 35366759Seric keylen = strlen(keybuf); 35466759Seric key.data = keybuf; 35566759Seric key.size = keylen; 35666759Seric i = hes_udb_get(&key, &info); 35766759Seric if (i != 0 || info.size <= 0) 35866759Seric break; 35966759Seric a->q_owner = xalloc(info.size + 1); 36066759Seric bcopy(info.data, a->q_owner, info.size); 36166759Seric a->q_owner[info.size] = '\0'; 36266759Seric free(info.data); 36366759Seric break; 36466759Seric #endif /* HESIOD */ 36566759Seric 36651360Seric case UDB_REMOTE: 36751741Seric /* not yet implemented */ 36851741Seric continue; 36951362Seric 37051360Seric case UDB_FORWARD: 37158099Seric if (bitset(EF_VRFYONLY, e->e_flags)) 37258099Seric return EX_OK; 37351360Seric i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1; 37451360Seric if (i < sizeof buf) 37551360Seric user = buf; 37651360Seric else 37751360Seric user = xalloc(i + 1); 37851360Seric (void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost); 37958151Seric message("expanded to %s", user); 38058082Seric a->q_flags &= ~QSELFREF; 38167982Seric naddrs = sendtolist(user, a, sendq, aliaslevel + 1, e); 38258082Seric if (naddrs > 0 && !bitset(QSELFREF, a->q_flags)) 38358065Seric { 38458065Seric if (tTd(28, 5)) 38558065Seric { 38658065Seric printf("udbexpand: QDONTSEND "); 38758065Seric printaddr(a, FALSE); 38858065Seric } 38958065Seric a->q_flags |= QDONTSEND; 39058065Seric } 39151362Seric if (user != buf) 39251362Seric free(user); 39351362Seric breakout = TRUE; 39451360Seric break; 39551360Seric 39651360Seric case UDB_EOLIST: 39751360Seric breakout = TRUE; 39851360Seric continue; 39951360Seric 40051360Seric default: 40151360Seric /* unknown entry type */ 40251360Seric continue; 40351360Seric } 40451362Seric } 40551923Seric return EX_OK; 40651362Seric } 40751951Seric /* 40851951Seric ** UDBSENDER -- return canonical external name of sender, given local name 40951951Seric ** 41051951Seric ** Parameters: 41151951Seric ** sender -- the name of the sender on the local machine. 41251951Seric ** 41351951Seric ** Returns: 41451951Seric ** The external name for this sender, if derivable from the 41551951Seric ** database. 41651951Seric ** NULL -- if nothing is changed from the database. 41751951Seric ** 41851951Seric ** Side Effects: 41951951Seric ** none. 42051951Seric */ 42151360Seric 42251951Seric char * 42351951Seric udbsender(sender) 42451951Seric char *sender; 42551951Seric { 42664350Seric extern char *udbmatch(); 42764350Seric 42864350Seric return udbmatch(sender, "mailname"); 42964350Seric } 43064350Seric 43164350Seric 43264350Seric char * 43364350Seric udbmatch(user, field) 43464350Seric char *user; 43564350Seric char *field; 43664350Seric { 43751951Seric register char *p; 43851951Seric register struct udbent *up; 43951951Seric int i; 44051951Seric int keylen; 44151951Seric DBT key, info; 44257232Seric char keybuf[MAXKEY]; 44351951Seric 44451951Seric if (tTd(28, 1)) 44564350Seric printf("udbmatch(%s, %s)\n", user, field); 44651951Seric 44751951Seric if (!UdbInitialized) 44851951Seric { 44951951Seric if (_udbx_init() == EX_TEMPFAIL) 45051951Seric return NULL; 45151951Seric } 45251951Seric 45351951Seric /* short circuit if no spec */ 45451951Seric if (UdbSpec == NULL || UdbSpec[0] == '\0') 45551951Seric return NULL; 45651951Seric 45766759Seric /* short circuit name begins with '\\' since it can't possibly match */ 45866759Seric if (user[0] == '\\') 45966759Seric return NULL; 46066759Seric 46151951Seric /* long names can never match and are a pain to deal with */ 46264350Seric if ((strlen(user) + strlen(field)) > sizeof keybuf - 4) 46351951Seric return NULL; 46451951Seric 46551951Seric /* names beginning with colons indicate metadata */ 46664350Seric if (user[0] == ':') 46751951Seric return NULL; 46851951Seric 46951951Seric /* build database key */ 47064350Seric (void) strcpy(keybuf, user); 47164350Seric (void) strcat(keybuf, ":"); 47264350Seric (void) strcat(keybuf, field); 47351951Seric keylen = strlen(keybuf); 47451951Seric 47551951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 47651951Seric { 47751951Seric /* 47851951Seric ** Select action based on entry type. 47951951Seric */ 48051951Seric 48151951Seric switch (up->udb_type) 48251951Seric { 48351951Seric case UDB_DBFETCH: 48451951Seric key.data = keybuf; 48551951Seric key.size = keylen; 48651951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 48751951Seric if (i != 0 || info.size <= 0) 48851951Seric { 48951951Seric if (tTd(28, 2)) 49066759Seric printf("udbmatch: no match on %s (%d) via db\n", 49164067Seric keybuf, keylen); 49251951Seric continue; 49351951Seric } 49451951Seric 49551951Seric p = xalloc(info.size + 1); 49651951Seric bcopy(info.data, p, info.size); 49751951Seric p[info.size] = '\0'; 49851951Seric if (tTd(28, 1)) 49964350Seric printf("udbmatch ==> %s\n", p); 50051951Seric return p; 50166759Seric break; 50266759Seric 50366759Seric #ifdef HESIOD 50466759Seric case UDB_HESIOD: 50566759Seric key.data = keybuf; 50666759Seric key.size = keylen; 50766759Seric i = hes_udb_get(&key, &info); 50866759Seric if (i != 0 || info.size <= 0) 50966759Seric { 51066759Seric if (tTd(28, 2)) 51166759Seric printf("udbmatch: no match on %s (%d) via hesiod\n", 51266759Seric keybuf, keylen); 51366759Seric continue; 51466759Seric } 51566759Seric 51666759Seric p = xalloc(info.size + 1); 51766759Seric bcopy(info.data, p, info.size); 51866759Seric p[info.size] = '\0'; 51966759Seric free(info.data); 52066759Seric if (tTd(28, 1)) 52166759Seric printf("udbmatch ==> %s\n", p); 52266759Seric return p; 52366759Seric break; 52466759Seric #endif /* HESIOD */ 52551951Seric } 52651951Seric } 52751951Seric 52864350Seric if (strcmp(field, "mailname") != 0) 52964350Seric return NULL; 53064350Seric 53151951Seric /* 53251951Seric ** Nothing yet. Search again for a default case. But only 53351951Seric ** use it if we also have a forward (:maildrop) pointer already 53451951Seric ** in the database. 53551951Seric */ 53651951Seric 53751951Seric /* build database key */ 53864350Seric (void) strcpy(keybuf, user); 53951951Seric (void) strcat(keybuf, ":maildrop"); 54051951Seric keylen = strlen(keybuf); 54151951Seric 54251951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 54351951Seric { 54451951Seric switch (up->udb_type) 54551951Seric { 54651951Seric case UDB_DBFETCH: 54751951Seric /* get the default case for this database */ 54851951Seric if (up->udb_default == NULL) 54951951Seric { 55051951Seric key.data = ":default:mailname"; 55151951Seric key.size = strlen(key.data); 55251951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 55351951Seric if (i != 0 || info.size <= 0) 55451951Seric { 55551951Seric /* no default case */ 55651951Seric up->udb_default = ""; 55751951Seric continue; 55851951Seric } 55951951Seric 56051951Seric /* save the default case */ 56151951Seric up->udb_default = xalloc(info.size + 1); 56251951Seric bcopy(info.data, up->udb_default, info.size); 56351951Seric up->udb_default[info.size] = '\0'; 56451951Seric } 56551951Seric else if (up->udb_default[0] == '\0') 56651951Seric continue; 56751951Seric 56851951Seric /* we have a default case -- verify user:maildrop */ 56951951Seric key.data = keybuf; 57051951Seric key.size = keylen; 57151951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 57251951Seric if (i != 0 || info.size <= 0) 57351951Seric { 57451951Seric /* nope -- no aliasing for this user */ 57551951Seric continue; 57651951Seric } 57751951Seric 57851951Seric /* they exist -- build the actual address */ 57964350Seric p = xalloc(strlen(user) + strlen(up->udb_default) + 2); 58064350Seric (void) strcpy(p, user); 58151951Seric (void) strcat(p, "@"); 58251951Seric (void) strcat(p, up->udb_default); 58351951Seric if (tTd(28, 1)) 58464350Seric printf("udbmatch ==> %s\n", p); 58551951Seric return p; 58666759Seric break; 58766759Seric 58866759Seric #ifdef HESIOD 58966759Seric case UDB_HESIOD: 59066759Seric /* get the default case for this database */ 59166759Seric if (up->udb_default == NULL) 59266759Seric { 59366759Seric key.data = ":default:mailname"; 59466759Seric key.size = strlen(key.data); 59566759Seric i = hes_udb_get(&key, &info); 59666759Seric 59766759Seric if (i != 0 || info.size <= 0) 59866759Seric { 59966759Seric /* no default case */ 60066759Seric up->udb_default = ""; 60166759Seric continue; 60266759Seric } 60366759Seric 60466759Seric /* save the default case */ 60566759Seric up->udb_default = xalloc(info.size + 1); 60666759Seric bcopy(info.data, up->udb_default, info.size); 60766759Seric up->udb_default[info.size] = '\0'; 60866759Seric free(info.data); 60966759Seric } 61066759Seric else if (up->udb_default[0] == '\0') 61166759Seric continue; 61266759Seric 61366759Seric /* we have a default case -- verify user:maildrop */ 61466759Seric key.data = keybuf; 61566759Seric key.size = keylen; 61666759Seric i = hes_udb_get(&key, &info); 61766759Seric if (i != 0 || info.size <= 0) 61866759Seric { 61966759Seric /* nope -- no aliasing for this user */ 62066759Seric continue; 62166759Seric } 62266759Seric 62366759Seric free(info.data); 62466759Seric /* they exist -- build the actual address */ 62566759Seric p = xalloc(strlen(user) + strlen(up->udb_default) + 2); 62666759Seric (void) strcpy(p, user); 62766759Seric (void) strcat(p, "@"); 62866759Seric (void) strcat(p, up->udb_default); 62966759Seric if (tTd(28, 1)) 63066759Seric printf("udbmatch ==> %s\n", p); 63166759Seric return p; 63266759Seric break; 63366759Seric #endif /* HESIOD */ 63451951Seric } 63551951Seric } 63651951Seric 63751951Seric /* still nothing.... too bad */ 63851951Seric return NULL; 63951951Seric } 64051951Seric /* 64168521Seric ** UDB_MAP_LOOKUP -- look up arbitrary entry in user database map 64268521Seric ** 64368521Seric ** Parameters: 64468521Seric ** map -- the map being queried. 64568521Seric ** name -- the name to look up. 64668521Seric ** av -- arguments to the map lookup. 64768521Seric ** statp -- to get any error status. 64868521Seric ** 64968521Seric ** Returns: 65068521Seric ** NULL if name not found in map. 65168521Seric ** The rewritten name otherwise. 65268521Seric */ 65368521Seric 65468521Seric char * 65568521Seric udb_map_lookup(map, name, av, statp) 65668521Seric MAP *map; 65768521Seric char *name; 65868521Seric char **av; 65968521Seric int *statp; 66068521Seric { 66168521Seric char *val; 66268521Seric 66368521Seric if (tTd(38, 20)) 66468521Seric printf("udb_map_lookup(%s, %s)\n", map->map_mname, name); 66568521Seric val = udbmatch(name, map->map_file); 66668521Seric if (val == NULL) 66768521Seric return NULL; 66868521Seric if (bitset(MF_MATCHONLY, map->map_mflags)) 66968521Seric return map_rewrite(map, name, strlen(name), NULL); 67068521Seric else 67168521Seric return map_rewrite(map, val, strlen(val), av); 67268521Seric } 67368521Seric /* 67451951Seric ** _UDBX_INIT -- parse the UDB specification, opening any valid entries. 67551951Seric ** 67651951Seric ** Parameters: 67751951Seric ** none. 67851951Seric ** 67951951Seric ** Returns: 68051951Seric ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a 68151951Seric ** database due to a host being down or some similar 68251951Seric ** (recoverable) situation. 68351951Seric ** EX_OK -- otherwise. 68451951Seric ** 68551951Seric ** Side Effects: 68651951Seric ** Fills in the UdbEnts structure from UdbSpec. 68751951Seric */ 68851951Seric 68951363Seric #define MAXUDBOPTS 27 69051363Seric 69151953Seric int 69251362Seric _udbx_init() 69351362Seric { 69451362Seric register char *p; 69551362Seric int i; 69651362Seric register struct udbent *up; 69757232Seric char buf[BUFSIZ]; 69851360Seric 69951951Seric if (UdbInitialized) 70051951Seric return EX_OK; 70151951Seric 70251908Seric # ifdef UDB_DEFAULT_SPEC 70351908Seric if (UdbSpec == NULL) 70451908Seric UdbSpec = UDB_DEFAULT_SPEC; 70551908Seric # endif 70651908Seric 70751362Seric p = UdbSpec; 70851362Seric up = UdbEnts; 70951762Seric while (p != NULL) 71051362Seric { 71151362Seric char *spec; 71251362Seric auto int rcode; 71351363Seric int nopts; 71451362Seric int nmx; 71551362Seric register struct hostent *h; 71651362Seric char *mxhosts[MAXMXHOSTS + 1]; 71751363Seric struct option opts[MAXUDBOPTS + 1]; 71851362Seric 71951362Seric while (*p == ' ' || *p == '\t' || *p == ',') 72051362Seric p++; 72151362Seric if (*p == '\0') 72251362Seric break; 72351362Seric spec = p; 72456795Seric p = strchr(p, ','); 72551761Seric if (p != NULL) 72651362Seric *p++ = '\0'; 72751363Seric 72851363Seric /* extract options */ 72951363Seric nopts = _udb_parsespec(spec, opts, MAXUDBOPTS); 73051363Seric 73151363Seric /* 73251363Seric ** Decode database specification. 73351363Seric ** 73451363Seric ** In the sendmail tradition, the leading character 73551363Seric ** defines the semantics of the rest of the entry. 73651363Seric ** 73751363Seric ** +hostname -- send a datagram to the udb server 73851363Seric ** on host "hostname" asking for the 73951363Seric ** home mail server for this user. 74051363Seric ** *hostname -- similar to +hostname, except that the 74151363Seric ** hostname is searched as an MX record; 74251363Seric ** resulting hosts are searched as for 74351363Seric ** +mxhostname. If no MX host is found, 74451363Seric ** this is the same as +hostname. 74551363Seric ** @hostname -- forward email to the indicated host. 74651363Seric ** This should be the last in the list, 74751363Seric ** since it always matches the input. 74851363Seric ** /dbname -- search the named database on the local 74951363Seric ** host using the Berkeley db package. 75051363Seric */ 75151363Seric 75251362Seric switch (*spec) 75351360Seric { 75451362Seric case '+': /* search remote database */ 75551363Seric case '*': /* search remote database (expand MX) */ 75651363Seric if (*spec == '*') 75751363Seric { 75866334Seric #if NAMED_BIND 75959273Seric nmx = getmxrr(spec + 1, mxhosts, FALSE, &rcode); 76057629Seric #else 76157629Seric mxhosts[0] = spec + 1; 76257629Seric nmx = 1; 76357629Seric rcode = 0; 76457629Seric #endif 76551363Seric if (tTd(28, 16)) 76651363Seric { 76751363Seric int i; 76851362Seric 76951363Seric printf("getmxrr(%s): %d", spec + 1, nmx); 77051363Seric for (i = 0; i <= nmx; i++) 77151363Seric printf(" %s", mxhosts[i]); 77251363Seric printf("\n"); 77351363Seric } 77451363Seric } 77551363Seric else 77651362Seric { 77751363Seric nmx = 1; 77851363Seric mxhosts[0] = spec + 1; 77951362Seric } 78051362Seric 78151362Seric for (i = 0; i < nmx; i++) 78251362Seric { 78351362Seric h = gethostbyname(mxhosts[i]); 78451362Seric if (h == NULL) 78551362Seric continue; 78651362Seric up->udb_type = UDB_REMOTE; 78751362Seric up->udb_addr.sin_family = h->h_addrtype; 78851362Seric bcopy(h->h_addr_list[0], 78951362Seric (char *) &up->udb_addr.sin_addr, 79067421Seric INADDRSZ); 79151362Seric up->udb_addr.sin_port = UdbPort; 79251362Seric up->udb_timeout = UdbTimeout; 79351362Seric up++; 79451362Seric } 79551362Seric 79651362Seric /* set up a datagram socket */ 79751362Seric if (UdbSock < 0) 79851362Seric { 79951362Seric UdbSock = socket(AF_INET, SOCK_DGRAM, 0); 80051362Seric (void) fcntl(UdbSock, F_SETFD, 1); 80151362Seric } 80251362Seric break; 80351362Seric 80451362Seric case '@': /* forward to remote host */ 80551362Seric up->udb_type = UDB_FORWARD; 80651362Seric up->udb_fwdhost = spec + 1; 80751362Seric up++; 80851362Seric break; 80951362Seric 81066759Seric case 'h': /* use hesiod */ 81166759Seric case 'H': 81266759Seric #ifdef HESIOD 81366759Seric if (strcasecmp(spec, "hesiod") != 0) 81466759Seric break; 81566759Seric up->udb_type = UDB_HESIOD; 81666759Seric up++; 81766759Seric #endif /* HESIOD */ 81866759Seric break; 81966759Seric 82051362Seric case '/': /* look up remote name */ 82151831Seric up->udb_dbname = spec; 82251923Seric errno = 0; 82351362Seric up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL); 82451362Seric if (up->udb_dbp == NULL) 82551923Seric { 82667763Seric if (tTd(28, 1)) 82767763Seric { 82867763Seric int saveerrno = errno; 82967763Seric 83067763Seric printf("dbopen(%s): %s", 83167763Seric spec, errstring(errno)); 83267763Seric errno = saveerrno; 83367763Seric } 83451923Seric if (errno != ENOENT && errno != EACCES) 83551951Seric { 83659615Seric #ifdef LOG 83759615Seric if (LogLevel > 2) 83859625Seric syslog(LOG_ERR, "dbopen(%s): %s", 83959625Seric spec, errstring(errno)); 84059615Seric #endif 84151951Seric up->udb_type = UDB_EOLIST; 84251951Seric goto tempfail; 84351951Seric } 84451362Seric break; 84551923Seric } 84651951Seric up->udb_type = UDB_DBFETCH; 84751362Seric up++; 84851362Seric break; 84951360Seric } 85051362Seric } 85151362Seric up->udb_type = UDB_EOLIST; 85251360Seric 85351362Seric if (tTd(28, 4)) 85451362Seric { 85551951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 85651360Seric { 85751362Seric switch (up->udb_type) 85851362Seric { 85951362Seric case UDB_REMOTE: 86051362Seric printf("REMOTE: addr %s, timeo %d\n", 86160494Seric anynet_ntoa((SOCKADDR *) &up->udb_addr), 86251362Seric up->udb_timeout); 86351362Seric break; 86451362Seric 86551951Seric case UDB_DBFETCH: 86651951Seric printf("FETCH: file %s\n", 86751830Seric up->udb_dbname); 86851362Seric break; 86951362Seric 87051362Seric case UDB_FORWARD: 87151362Seric printf("FORWARD: host %s\n", 87251362Seric up->udb_fwdhost); 87351362Seric break; 87451362Seric 87566759Seric case UDB_HESIOD: 87666759Seric printf("HESIOD\n"); 87766759Seric break; 87866759Seric 87951362Seric default: 88051362Seric printf("UNKNOWN\n"); 88151362Seric break; 88251362Seric } 88351360Seric } 88450581Seric } 88551951Seric 88651951Seric UdbInitialized = TRUE; 88751955Seric errno = 0; 88851951Seric return EX_OK; 88951951Seric 89051951Seric /* 89151951Seric ** On temporary failure, back out anything we've already done 89251951Seric */ 89351951Seric 89451951Seric tempfail: 89551951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 89651951Seric { 89751951Seric if (up->udb_type == UDB_DBFETCH) 89851951Seric { 89951951Seric (*up->udb_dbp->close)(up->udb_dbp); 90051951Seric } 90151951Seric } 90251951Seric return EX_TEMPFAIL; 90351360Seric } 90450581Seric 90551363Seric int 90651363Seric _udb_parsespec(udbspec, opt, maxopts) 90751363Seric char *udbspec; 90851363Seric struct option opt[]; 90951363Seric int maxopts; 91051363Seric { 91151363Seric register char *spec; 91251363Seric register char *spec_end; 91351363Seric register int optnum; 91451363Seric 91556795Seric spec_end = strchr(udbspec, ':'); 91651363Seric for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++) 91751363Seric { 91851363Seric register char *p; 91951363Seric 92058050Seric while (isascii(*spec) && isspace(*spec)) 92151363Seric spec++; 92256795Seric spec_end = strchr(spec, ':'); 92351363Seric if (spec_end != NULL) 92451363Seric *spec_end++ = '\0'; 92551363Seric 92651363Seric opt[optnum].name = spec; 92751363Seric opt[optnum].val = NULL; 92856795Seric p = strchr(spec, '='); 92951363Seric if (p != NULL) 93051363Seric opt[optnum].val = ++p; 93151363Seric } 93251363Seric return optnum; 93351363Seric } 93451363Seric 93566759Seric #ifdef HESIOD 93666759Seric 93766759Seric int 93866759Seric hes_udb_get(key, info) 93966759Seric DBT *key; 94066759Seric DBT *info; 94166759Seric { 94266759Seric char *name, *type; 94366759Seric char *p, **hp; 94468533Seric char kbuf[MAXKEY + 1]; 94566759Seric 94668533Seric strcpy(kbuf, key->data); 94768533Seric name = kbuf; 94866759Seric type = strchr(name, ':'); 949*68544Seric if (type == NULL) 95066759Seric return 1; 95166759Seric *type++ = '\0'; 95266759Seric 95366759Seric if (tTd(28, 1)) 95466759Seric printf("hes_udb_get(%s, %s)\n", name, type); 95566759Seric 95666759Seric /* make the hesiod query */ 95766759Seric hp = hes_resolve(name, type); 95866759Seric if (hp == NULL) 95966759Seric { 96066759Seric /* network problem or timeout */ 96166759Seric if (hes_error() == HES_ER_NET) 96266759Seric return -1; 96366759Seric 96466759Seric return 1; 96566759Seric } 96666759Seric else 96766759Seric { 96866759Seric /* 96966759Seric ** If there are multiple matches, just return the 97066759Seric ** first one and free the others. 97166759Seric ** 97266759Seric ** XXX These should really be returned; for example, 97366759Seric ** XXX it is legal for :maildrop to be multi-valued. 97466759Seric */ 97566759Seric 97666759Seric for (p = hp[1]; p; p++) 97766759Seric free(p); 97866759Seric 97966759Seric info->data = hp[0]; 98066759Seric info->size = (size_t) strlen(info->data); 98166759Seric } 98266759Seric 98366759Seric return 0; 98466759Seric } 98566759Seric #endif /* HESIOD */ 98666759Seric 98751360Seric #else /* not USERDB */ 98851360Seric 98951923Seric int 99067982Seric udbexpand(a, sendq, aliaslevel, e) 99151360Seric ADDRESS *a; 99251360Seric ADDRESS **sendq; 99367982Seric int aliaslevel; 99455012Seric ENVELOPE *e; 99551360Seric { 99651923Seric return EX_OK; 99750581Seric } 99850581Seric 99950581Seric #endif /* USERDB */ 1000