150581Seric /* 250581Seric * Copyright (c) 1983 Eric P. Allman 350581Seric * Copyright (c) 1988 Regents of the University of California. 450581Seric * All rights reserved. 550581Seric * 650581Seric * %sccs.include.redist.c% 750581Seric */ 850581Seric 950581Seric #ifndef lint 1051360Seric #ifdef USERDB 11*58050Seric static char sccsid [] = "@(#)udb.c 6.7 (Berkeley) 02/18/93 (with USERDB)"; 1251360Seric #else 13*58050Seric static char sccsid [] = "@(#)udb.c 6.7 (Berkeley) 02/18/93 (without USERDB)"; 1450581Seric #endif 1551360Seric #endif 1650581Seric 1750581Seric #include "sendmail.h" 1850581Seric 1950581Seric #ifdef USERDB 2050581Seric 2151360Seric #include <sys/time.h> 2251923Seric #include <errno.h> 2351360Seric #include <fcntl.h> 2451360Seric #include <netdb.h> 2550581Seric #include <db.h> 2650581Seric 2750581Seric /* 2853654Seric ** UDB.C -- interface between sendmail and Berkeley User Data Base. 2950581Seric ** 3051363Seric ** This depends on the 4.4BSD db package. 3150581Seric */ 3250581Seric 3351362Seric 3451360Seric struct udbent 3551360Seric { 3651360Seric char *udb_spec; /* string version of spec */ 3751360Seric int udb_type; /* type of entry */ 3851951Seric char *udb_default; /* default host for outgoing mail */ 3951360Seric union 4051360Seric { 4151360Seric /* type UE_REMOTE -- do remote call for lookup */ 4251360Seric struct 4351360Seric { 4451360Seric struct sockaddr_in _udb_addr; /* address */ 4551360Seric int _udb_timeout; /* timeout */ 4651360Seric } udb_remote; 4751360Seric #define udb_addr udb_u.udb_remote._udb_addr 4851360Seric #define udb_timeout udb_u.udb_remote._udb_timeout 4951360Seric 5051360Seric /* type UE_FORWARD -- forward message to remote */ 5151360Seric struct 5251360Seric { 5351360Seric char *_udb_fwdhost; /* name of forward host */ 5451360Seric } udb_forward; 5551360Seric #define udb_fwdhost udb_u.udb_forward._udb_fwdhost 5651360Seric 5751951Seric /* type UE_FETCH -- lookup in local database */ 5851360Seric struct 5951360Seric { 6051360Seric char *_udb_dbname; /* pathname of database */ 6151360Seric DB *_udb_dbp; /* open database ptr */ 6251360Seric } udb_lookup; 6351360Seric #define udb_dbname udb_u.udb_lookup._udb_dbname 6451360Seric #define udb_dbp udb_u.udb_lookup._udb_dbp 6551360Seric } udb_u; 6651360Seric }; 6751360Seric 6851360Seric #define UDB_EOLIST 0 /* end of list */ 6951360Seric #define UDB_SKIP 1 /* skip this entry */ 7051360Seric #define UDB_REMOTE 2 /* look up in remote database */ 7151951Seric #define UDB_DBFETCH 3 /* look up in local database */ 7251360Seric #define UDB_FORWARD 4 /* forward to remote host */ 7351360Seric 7451360Seric #define MAXUDBENT 10 /* maximum number of UDB entries */ 7551360Seric 7651363Seric 7751363Seric struct option 7851363Seric { 7951363Seric char *name; 8051363Seric char *val; 8151363Seric }; 8251363Seric /* 8351363Seric ** UDBEXPAND -- look up user in database and expand 8451363Seric ** 8551363Seric ** Parameters: 8651363Seric ** a -- address to expand. 8751363Seric ** sendq -- pointer to head of sendq to put the expansions in. 8851363Seric ** 8951363Seric ** Returns: 9051923Seric ** EX_TEMPFAIL -- if something "odd" happened -- probably due 9151923Seric ** to accessing a file on an NFS server that is down. 9251923Seric ** EX_OK -- otherwise. 9351363Seric ** 9451363Seric ** Side Effects: 9551363Seric ** Modifies sendq. 9651363Seric */ 9751363Seric 9851363Seric int UdbPort = 1616; 9951363Seric int UdbTimeout = 10; 10051363Seric 10151953Seric struct udbent UdbEnts[MAXUDBENT + 1]; 10251953Seric int UdbSock = -1; 10351953Seric bool UdbInitialized = FALSE; 10451360Seric 10551923Seric int 10655012Seric udbexpand(a, sendq, e) 10750581Seric register ADDRESS *a; 10850581Seric ADDRESS **sendq; 10955012Seric register ENVELOPE *e; 11050581Seric { 11150581Seric int i; 11250581Seric register char *p; 11350581Seric DBT key; 11450581Seric DBT info; 11551360Seric bool breakout; 11651360Seric register struct udbent *up; 11751362Seric int keylen; 11857232Seric char keybuf[MAXKEY]; 11957232Seric char buf[BUFSIZ]; 12050581Seric 12150581Seric if (tTd(28, 1)) 12250581Seric printf("expand(%s)\n", a->q_paddr); 12350581Seric 12450581Seric /* make certain we are supposed to send to this address */ 12551909Seric if (bitset(QDONTSEND, a->q_flags)) 12651923Seric return EX_OK; 12755012Seric e->e_to = a->q_paddr; 12850581Seric 12951360Seric /* on first call, locate the database */ 13051951Seric if (!UdbInitialized) 13150581Seric { 13251923Seric extern int _udbx_init(); 13351362Seric 13451923Seric if (_udbx_init() == EX_TEMPFAIL) 13551923Seric return EX_TEMPFAIL; 13651362Seric } 13750581Seric 13851909Seric /* short circuit the process if no chance of a match */ 13951909Seric if (UdbSpec == NULL || UdbSpec[0] == '\0') 14051923Seric return EX_OK; 14151909Seric 14251362Seric /* if name is too long, assume it won't match */ 14351362Seric if (strlen(a->q_user) > sizeof keybuf - 12) 14451923Seric return EX_OK; 14551360Seric 14651362Seric /* if name begins with a colon, it indicates our metadata */ 14751362Seric if (a->q_user[0] == ':') 14851923Seric return EX_OK; 14951360Seric 15051362Seric /* build actual database key */ 15151362Seric (void) strcpy(keybuf, a->q_user); 15251362Seric (void) strcat(keybuf, ":maildrop"); 15351362Seric keylen = strlen(keybuf); 15451360Seric 15551360Seric breakout = FALSE; 15651362Seric for (up = UdbEnts; !breakout; up++) 15750581Seric { 15851360Seric char *user; 15950581Seric 16051360Seric /* 16151360Seric ** Select action based on entry type. 16251360Seric ** 16351360Seric ** On dropping out of this switch, "class" should 16451360Seric ** explain the type of the data, and "user" should 16551360Seric ** contain the user information. 16651360Seric */ 16750581Seric 16851360Seric switch (up->udb_type) 16951360Seric { 17051951Seric case UDB_DBFETCH: 17151362Seric key.data = keybuf; 17251362Seric key.size = keylen; 17351362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR); 17451923Seric if (i > 0 || info.size <= 0) 17551360Seric { 17651360Seric if (tTd(28, 2)) 17751362Seric printf("expand: no match on %s\n", keybuf); 17851360Seric continue; 17951360Seric } 18050581Seric 18151830Seric while (i == 0 && key.size == keylen && 18251830Seric bcmp(key.data, keybuf, keylen) == 0) 18351362Seric { 18451830Seric breakout = TRUE; 18551362Seric if (info.size < sizeof buf) 18651362Seric user = buf; 18751362Seric else 18851362Seric user = xalloc(info.size + 1); 18951362Seric bcopy(info.data, user, info.size); 19051362Seric user[info.size] = '\0'; 19150581Seric 19251362Seric message(Arpa_Info, "expanded to %s", user); 19357977Seric #ifdef LOG 19457977Seric if (LogLevel >= 10) 19557977Seric syslog(LOG_INFO, "%s: expand %s => %s", 19657977Seric e->e_id, e->e_to, user); 19757977Seric #endif 19851362Seric AliasLevel++; 19955012Seric sendtolist(user, a, sendq, e); 20051362Seric AliasLevel--; 20151362Seric 20251362Seric if (user != buf) 20351362Seric free(user); 20451362Seric 20551362Seric /* get the next record */ 20651362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT); 20751830Seric } 20851923Seric if (i < 0) 20951923Seric { 21058010Seric syserr("udbexpand: db-get %.*s stat %d", 21158010Seric key.size, key.data, i); 21251923Seric return EX_TEMPFAIL; 21351923Seric } 21451360Seric break; 21551360Seric 21651360Seric case UDB_REMOTE: 21751741Seric /* not yet implemented */ 21851741Seric continue; 21951362Seric 22051360Seric case UDB_FORWARD: 22151360Seric i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1; 22251360Seric if (i < sizeof buf) 22351360Seric user = buf; 22451360Seric else 22551360Seric user = xalloc(i + 1); 22651360Seric (void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost); 22751362Seric message(Arpa_Info, "expanded to %s", user); 22851362Seric AliasLevel++; 22955012Seric sendtolist(user, a, sendq, e); 23051362Seric AliasLevel--; 23151362Seric if (user != buf) 23251362Seric free(user); 23351362Seric breakout = TRUE; 23451360Seric break; 23551360Seric 23651360Seric case UDB_EOLIST: 23751360Seric breakout = TRUE; 23851360Seric continue; 23951360Seric 24051360Seric default: 24151360Seric /* unknown entry type */ 24251360Seric continue; 24351360Seric } 24451362Seric } 24551923Seric return EX_OK; 24651362Seric } 24751951Seric /* 24851951Seric ** UDBSENDER -- return canonical external name of sender, given local name 24951951Seric ** 25051951Seric ** Parameters: 25151951Seric ** sender -- the name of the sender on the local machine. 25251951Seric ** 25351951Seric ** Returns: 25451951Seric ** The external name for this sender, if derivable from the 25551951Seric ** database. 25651951Seric ** NULL -- if nothing is changed from the database. 25751951Seric ** 25851951Seric ** Side Effects: 25951951Seric ** none. 26051951Seric */ 26151360Seric 26251951Seric char * 26351951Seric udbsender(sender) 26451951Seric char *sender; 26551951Seric { 26651951Seric register char *p; 26751951Seric register struct udbent *up; 26851951Seric int i; 26951951Seric int keylen; 27051951Seric DBT key, info; 27157232Seric char keybuf[MAXKEY]; 27251951Seric 27351951Seric if (tTd(28, 1)) 27451951Seric printf("udbsender(%s)\n", sender); 27551951Seric 27651951Seric if (!UdbInitialized) 27751951Seric { 27851951Seric if (_udbx_init() == EX_TEMPFAIL) 27951951Seric return NULL; 28051951Seric } 28151951Seric 28251951Seric /* short circuit if no spec */ 28351951Seric if (UdbSpec == NULL || UdbSpec[0] == '\0') 28451951Seric return NULL; 28551951Seric 28651951Seric /* long names can never match and are a pain to deal with */ 28751951Seric if (strlen(sender) > sizeof keybuf - 12) 28851951Seric return NULL; 28951951Seric 29051951Seric /* names beginning with colons indicate metadata */ 29151951Seric if (sender[0] == ':') 29251951Seric return NULL; 29351951Seric 29451951Seric /* build database key */ 29551951Seric (void) strcpy(keybuf, sender); 29651951Seric (void) strcat(keybuf, ":mailname"); 29751951Seric keylen = strlen(keybuf); 29851951Seric 29951951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 30051951Seric { 30151951Seric /* 30251951Seric ** Select action based on entry type. 30351951Seric */ 30451951Seric 30551951Seric switch (up->udb_type) 30651951Seric { 30751951Seric case UDB_DBFETCH: 30851951Seric key.data = keybuf; 30951951Seric key.size = keylen; 31051951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 31151951Seric if (i != 0 || info.size <= 0) 31251951Seric { 31351951Seric if (tTd(28, 2)) 31451951Seric printf("udbsender: no match on %s\n", 31551951Seric keybuf); 31651951Seric continue; 31751951Seric } 31851951Seric 31951951Seric p = xalloc(info.size + 1); 32051951Seric bcopy(info.data, p, info.size); 32151951Seric p[info.size] = '\0'; 32251951Seric if (tTd(28, 1)) 32351951Seric printf("udbsender ==> %s\n", p); 32451951Seric return p; 32551951Seric } 32651951Seric } 32751951Seric 32851951Seric /* 32951951Seric ** Nothing yet. Search again for a default case. But only 33051951Seric ** use it if we also have a forward (:maildrop) pointer already 33151951Seric ** in the database. 33251951Seric */ 33351951Seric 33451951Seric /* build database key */ 33551951Seric (void) strcpy(keybuf, sender); 33651951Seric (void) strcat(keybuf, ":maildrop"); 33751951Seric keylen = strlen(keybuf); 33851951Seric 33951951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 34051951Seric { 34151951Seric switch (up->udb_type) 34251951Seric { 34351951Seric case UDB_DBFETCH: 34451951Seric /* get the default case for this database */ 34551951Seric if (up->udb_default == NULL) 34651951Seric { 34751951Seric key.data = ":default:mailname"; 34851951Seric key.size = strlen(key.data); 34951951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 35051951Seric if (i != 0 || info.size <= 0) 35151951Seric { 35251951Seric /* no default case */ 35351951Seric up->udb_default = ""; 35451951Seric continue; 35551951Seric } 35651951Seric 35751951Seric /* save the default case */ 35851951Seric up->udb_default = xalloc(info.size + 1); 35951951Seric bcopy(info.data, up->udb_default, info.size); 36051951Seric up->udb_default[info.size] = '\0'; 36151951Seric } 36251951Seric else if (up->udb_default[0] == '\0') 36351951Seric continue; 36451951Seric 36551951Seric /* we have a default case -- verify user:maildrop */ 36651951Seric key.data = keybuf; 36751951Seric key.size = keylen; 36851951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 36951951Seric if (i != 0 || info.size <= 0) 37051951Seric { 37151951Seric /* nope -- no aliasing for this user */ 37251951Seric continue; 37351951Seric } 37451951Seric 37551951Seric /* they exist -- build the actual address */ 37651951Seric p = xalloc(strlen(sender) + strlen(up->udb_default) + 2); 37751951Seric (void) strcpy(p, sender); 37851951Seric (void) strcat(p, "@"); 37951951Seric (void) strcat(p, up->udb_default); 38051951Seric if (tTd(28, 1)) 38151951Seric printf("udbsender ==> %s\n", p); 38251951Seric return p; 38351951Seric } 38451951Seric } 38551951Seric 38651951Seric /* still nothing.... too bad */ 38751951Seric return NULL; 38851951Seric } 38951951Seric /* 39051951Seric ** _UDBX_INIT -- parse the UDB specification, opening any valid entries. 39151951Seric ** 39251951Seric ** Parameters: 39351951Seric ** none. 39451951Seric ** 39551951Seric ** Returns: 39651951Seric ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a 39751951Seric ** database due to a host being down or some similar 39851951Seric ** (recoverable) situation. 39951951Seric ** EX_OK -- otherwise. 40051951Seric ** 40151951Seric ** Side Effects: 40251951Seric ** Fills in the UdbEnts structure from UdbSpec. 40351951Seric */ 40451951Seric 40551363Seric #define MAXUDBOPTS 27 40651363Seric 40751953Seric int 40851362Seric _udbx_init() 40951362Seric { 41051362Seric register char *p; 41151362Seric int i; 41251362Seric register struct udbent *up; 41357232Seric char buf[BUFSIZ]; 41451360Seric 41551951Seric if (UdbInitialized) 41651951Seric return EX_OK; 41751951Seric 41851908Seric # ifdef UDB_DEFAULT_SPEC 41951908Seric if (UdbSpec == NULL) 42051908Seric UdbSpec = UDB_DEFAULT_SPEC; 42151908Seric # endif 42251908Seric 42351362Seric p = UdbSpec; 42451362Seric up = UdbEnts; 42551762Seric while (p != NULL) 42651362Seric { 42751362Seric char *spec; 42851362Seric auto int rcode; 42951363Seric int nopts; 43051362Seric int nmx; 43151362Seric register struct hostent *h; 43251362Seric char *mxhosts[MAXMXHOSTS + 1]; 43351363Seric struct option opts[MAXUDBOPTS + 1]; 43451362Seric 43551362Seric while (*p == ' ' || *p == '\t' || *p == ',') 43651362Seric p++; 43751362Seric if (*p == '\0') 43851362Seric break; 43951362Seric spec = p; 44056795Seric p = strchr(p, ','); 44151761Seric if (p != NULL) 44251362Seric *p++ = '\0'; 44351363Seric 44451363Seric /* extract options */ 44551363Seric nopts = _udb_parsespec(spec, opts, MAXUDBOPTS); 44651363Seric 44751363Seric /* 44851363Seric ** Decode database specification. 44951363Seric ** 45051363Seric ** In the sendmail tradition, the leading character 45151363Seric ** defines the semantics of the rest of the entry. 45251363Seric ** 45351363Seric ** +hostname -- send a datagram to the udb server 45451363Seric ** on host "hostname" asking for the 45551363Seric ** home mail server for this user. 45651363Seric ** *hostname -- similar to +hostname, except that the 45751363Seric ** hostname is searched as an MX record; 45851363Seric ** resulting hosts are searched as for 45951363Seric ** +mxhostname. If no MX host is found, 46051363Seric ** this is the same as +hostname. 46151363Seric ** @hostname -- forward email to the indicated host. 46251363Seric ** This should be the last in the list, 46351363Seric ** since it always matches the input. 46451363Seric ** /dbname -- search the named database on the local 46551363Seric ** host using the Berkeley db package. 46651363Seric */ 46751363Seric 46851362Seric switch (*spec) 46951360Seric { 47051362Seric case '+': /* search remote database */ 47151363Seric case '*': /* search remote database (expand MX) */ 47251363Seric if (*spec == '*') 47351363Seric { 47457629Seric #ifdef NAMED_BIND 47551363Seric nmx = getmxrr(spec + 1, mxhosts, "", &rcode); 47657629Seric #else 47757629Seric mxhosts[0] = spec + 1; 47857629Seric nmx = 1; 47957629Seric rcode = 0; 48057629Seric #endif 48151363Seric if (tTd(28, 16)) 48251363Seric { 48351363Seric int i; 48451362Seric 48551363Seric printf("getmxrr(%s): %d", spec + 1, nmx); 48651363Seric for (i = 0; i <= nmx; i++) 48751363Seric printf(" %s", mxhosts[i]); 48851363Seric printf("\n"); 48951363Seric } 49051363Seric } 49151363Seric else 49251362Seric { 49351363Seric nmx = 1; 49451363Seric mxhosts[0] = spec + 1; 49551362Seric } 49651362Seric 49751362Seric for (i = 0; i < nmx; i++) 49851362Seric { 49951362Seric h = gethostbyname(mxhosts[i]); 50051362Seric if (h == NULL) 50151362Seric continue; 50251362Seric up->udb_type = UDB_REMOTE; 50351362Seric up->udb_addr.sin_family = h->h_addrtype; 50451362Seric bcopy(h->h_addr_list[0], 50551362Seric (char *) &up->udb_addr.sin_addr, 50651362Seric h->h_length); 50751362Seric up->udb_addr.sin_port = UdbPort; 50851362Seric up->udb_timeout = UdbTimeout; 50951362Seric up++; 51051362Seric } 51151362Seric 51251362Seric /* set up a datagram socket */ 51351362Seric if (UdbSock < 0) 51451362Seric { 51551362Seric UdbSock = socket(AF_INET, SOCK_DGRAM, 0); 51651362Seric (void) fcntl(UdbSock, F_SETFD, 1); 51751362Seric } 51851362Seric break; 51951362Seric 52051362Seric case '@': /* forward to remote host */ 52151362Seric up->udb_type = UDB_FORWARD; 52251362Seric up->udb_fwdhost = spec + 1; 52351362Seric up++; 52451362Seric break; 52551362Seric 52651362Seric case '/': /* look up remote name */ 52751831Seric up->udb_dbname = spec; 52851923Seric errno = 0; 52951362Seric up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL); 53051362Seric if (up->udb_dbp == NULL) 53151923Seric { 53251923Seric if (errno != ENOENT && errno != EACCES) 53351951Seric { 53451951Seric up->udb_type = UDB_EOLIST; 53551951Seric goto tempfail; 53651951Seric } 53751362Seric break; 53851923Seric } 53951951Seric up->udb_type = UDB_DBFETCH; 54051362Seric up++; 54151362Seric break; 54251360Seric } 54351362Seric } 54451362Seric up->udb_type = UDB_EOLIST; 54551360Seric 54651362Seric if (tTd(28, 4)) 54751362Seric { 54851951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 54951360Seric { 55051362Seric switch (up->udb_type) 55151362Seric { 55251362Seric case UDB_REMOTE: 55351362Seric printf("REMOTE: addr %s, timeo %d\n", 55451362Seric inet_ntoa(up->udb_addr.sin_addr), 55551362Seric up->udb_timeout); 55651362Seric break; 55751362Seric 55851951Seric case UDB_DBFETCH: 55951951Seric printf("FETCH: file %s\n", 56051830Seric up->udb_dbname); 56151362Seric break; 56251362Seric 56351362Seric case UDB_FORWARD: 56451362Seric printf("FORWARD: host %s\n", 56551362Seric up->udb_fwdhost); 56651362Seric break; 56751362Seric 56851362Seric default: 56951362Seric printf("UNKNOWN\n"); 57051362Seric break; 57151362Seric } 57251360Seric } 57350581Seric } 57451951Seric 57551951Seric UdbInitialized = TRUE; 57651955Seric errno = 0; 57751951Seric return EX_OK; 57851951Seric 57951951Seric /* 58051951Seric ** On temporary failure, back out anything we've already done 58151951Seric */ 58251951Seric 58351951Seric tempfail: 58451951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 58551951Seric { 58651951Seric if (up->udb_type == UDB_DBFETCH) 58751951Seric { 58851951Seric (*up->udb_dbp->close)(up->udb_dbp); 58951951Seric } 59051951Seric } 59151951Seric return EX_TEMPFAIL; 59251360Seric } 59350581Seric 59451363Seric int 59551363Seric _udb_parsespec(udbspec, opt, maxopts) 59651363Seric char *udbspec; 59751363Seric struct option opt[]; 59851363Seric int maxopts; 59951363Seric { 60051363Seric register char *spec; 60151363Seric register char *spec_end; 60251363Seric register int optnum; 60351363Seric 60456795Seric spec_end = strchr(udbspec, ':'); 60551363Seric for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++) 60651363Seric { 60751363Seric register char *p; 60851363Seric 609*58050Seric while (isascii(*spec) && isspace(*spec)) 61051363Seric spec++; 61156795Seric spec_end = strchr(spec, ':'); 61251363Seric if (spec_end != NULL) 61351363Seric *spec_end++ = '\0'; 61451363Seric 61551363Seric opt[optnum].name = spec; 61651363Seric opt[optnum].val = NULL; 61756795Seric p = strchr(spec, '='); 61851363Seric if (p != NULL) 61951363Seric opt[optnum].val = ++p; 62051363Seric } 62151363Seric return optnum; 62251363Seric } 62351363Seric 62451360Seric #else /* not USERDB */ 62551360Seric 62651923Seric int 62755012Seric udbexpand(a, sendq, e) 62851360Seric ADDRESS *a; 62951360Seric ADDRESS **sendq; 63055012Seric ENVELOPE *e; 63151360Seric { 63251923Seric return EX_OK; 63350581Seric } 63450581Seric 63550581Seric #endif /* USERDB */ 636