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*51955Seric static char sccsid [] = "@(#)udb.c 5.17 (Berkeley) 12/15/91 (with USERDB)"; 1251360Seric #else 13*51955Seric static char sccsid [] = "@(#)udb.c 5.17 (Berkeley) 12/15/91 (without USERDB)"; 1450581Seric #endif 1551360Seric #endif 1650581Seric 1750581Seric #include "sendmail.h" 1850581Seric 1950581Seric #ifdef USERDB 2050581Seric 2150581Seric #include <sys/file.h> 2251360Seric #include <sys/time.h> 2351923Seric #include <errno.h> 2451360Seric #include <fcntl.h> 2551360Seric #include <netdb.h> 2650581Seric #include <db.h> 2750581Seric 2850581Seric /* 2951363Seric ** UDBEXPAND.C -- interface between sendmail and Berkeley User Data Base. 3050581Seric ** 3151363Seric ** This depends on the 4.4BSD db package. 3250581Seric */ 3350581Seric 3451362Seric 3551360Seric struct udbent 3651360Seric { 3751360Seric char *udb_spec; /* string version of spec */ 3851360Seric int udb_type; /* type of entry */ 3951951Seric char *udb_default; /* default host for outgoing mail */ 4051360Seric union 4151360Seric { 4251360Seric /* type UE_REMOTE -- do remote call for lookup */ 4351360Seric struct 4451360Seric { 4551360Seric struct sockaddr_in _udb_addr; /* address */ 4651360Seric int _udb_timeout; /* timeout */ 4751360Seric } udb_remote; 4851360Seric #define udb_addr udb_u.udb_remote._udb_addr 4951360Seric #define udb_timeout udb_u.udb_remote._udb_timeout 5051360Seric 5151360Seric /* type UE_FORWARD -- forward message to remote */ 5251360Seric struct 5351360Seric { 5451360Seric char *_udb_fwdhost; /* name of forward host */ 5551360Seric } udb_forward; 5651360Seric #define udb_fwdhost udb_u.udb_forward._udb_fwdhost 5751360Seric 5851951Seric /* type UE_FETCH -- lookup in local database */ 5951360Seric struct 6051360Seric { 6151360Seric char *_udb_dbname; /* pathname of database */ 6251360Seric DB *_udb_dbp; /* open database ptr */ 6351360Seric } udb_lookup; 6451360Seric #define udb_dbname udb_u.udb_lookup._udb_dbname 6551360Seric #define udb_dbp udb_u.udb_lookup._udb_dbp 6651360Seric } udb_u; 6751360Seric }; 6851360Seric 6951360Seric #define UDB_EOLIST 0 /* end of list */ 7051360Seric #define UDB_SKIP 1 /* skip this entry */ 7151360Seric #define UDB_REMOTE 2 /* look up in remote database */ 7251951Seric #define UDB_DBFETCH 3 /* look up in local database */ 7351360Seric #define UDB_FORWARD 4 /* forward to remote host */ 7451360Seric 7551360Seric #define MAXUDBENT 10 /* maximum number of UDB entries */ 7651360Seric 7751363Seric 7851363Seric struct option 7951363Seric { 8051363Seric char *name; 8151363Seric char *val; 8251363Seric }; 8351363Seric /* 8451363Seric ** UDBEXPAND -- look up user in database and expand 8551363Seric ** 8651363Seric ** Parameters: 8751363Seric ** a -- address to expand. 8851363Seric ** sendq -- pointer to head of sendq to put the expansions in. 8951363Seric ** 9051363Seric ** Returns: 9151923Seric ** EX_TEMPFAIL -- if something "odd" happened -- probably due 9251923Seric ** to accessing a file on an NFS server that is down. 9351923Seric ** EX_OK -- otherwise. 9451363Seric ** 9551363Seric ** Side Effects: 9651363Seric ** Modifies sendq. 9751363Seric */ 9851363Seric 9951363Seric int UdbPort = 1616; 10051363Seric int UdbTimeout = 10; 10151363Seric 10251953Seric struct udbent UdbEnts[MAXUDBENT + 1]; 10351953Seric int UdbSock = -1; 10451953Seric bool UdbInitialized = FALSE; 10551360Seric 10651923Seric int 10750581Seric udbexpand(a, sendq) 10850581Seric register ADDRESS *a; 10950581Seric ADDRESS **sendq; 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; 11851362Seric char keybuf[128]; 11950581Seric char buf[8192]; 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; 12750581Seric CurEnv->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; 15951360Seric struct timeval timeout; 16051360Seric fd_set fdset; 16150581Seric 16251360Seric /* 16351360Seric ** Select action based on entry type. 16451360Seric ** 16551360Seric ** On dropping out of this switch, "class" should 16651360Seric ** explain the type of the data, and "user" should 16751360Seric ** contain the user information. 16851360Seric */ 16950581Seric 17051360Seric switch (up->udb_type) 17151360Seric { 17251951Seric case UDB_DBFETCH: 17351362Seric key.data = keybuf; 17451362Seric key.size = keylen; 17551362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR); 17651923Seric if (i > 0 || info.size <= 0) 17751360Seric { 17851360Seric if (tTd(28, 2)) 17951362Seric printf("expand: no match on %s\n", keybuf); 18051360Seric continue; 18151360Seric } 18250581Seric 18351830Seric while (i == 0 && key.size == keylen && 18451830Seric bcmp(key.data, keybuf, keylen) == 0) 18551362Seric { 18651830Seric breakout = TRUE; 18751362Seric if (info.size < sizeof buf) 18851362Seric user = buf; 18951362Seric else 19051362Seric user = xalloc(info.size + 1); 19151362Seric bcopy(info.data, user, info.size); 19251362Seric user[info.size] = '\0'; 19350581Seric 19451362Seric message(Arpa_Info, "expanded to %s", user); 19551362Seric AliasLevel++; 19651362Seric sendtolist(user, a, sendq); 19751362Seric AliasLevel--; 19851362Seric 19951362Seric if (user != buf) 20051362Seric free(user); 20151362Seric 20251362Seric /* get the next record */ 20351362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT); 20451830Seric } 20551923Seric if (i < 0) 20651923Seric { 20751923Seric syserr("udbexpand: db-get stat %s"); 20851923Seric return EX_TEMPFAIL; 20951923Seric } 21051360Seric break; 21151360Seric 21251360Seric case UDB_REMOTE: 21351741Seric /* not yet implemented */ 21451741Seric continue; 21551362Seric 21651360Seric case UDB_FORWARD: 21751360Seric i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1; 21851360Seric if (i < sizeof buf) 21951360Seric user = buf; 22051360Seric else 22151360Seric user = xalloc(i + 1); 22251360Seric (void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost); 22351362Seric message(Arpa_Info, "expanded to %s", user); 22451362Seric AliasLevel++; 22551362Seric sendtolist(user, a, sendq); 22651362Seric AliasLevel--; 22751362Seric if (user != buf) 22851362Seric free(user); 22951362Seric breakout = TRUE; 23051360Seric break; 23151360Seric 23251360Seric case UDB_EOLIST: 23351360Seric breakout = TRUE; 23451360Seric continue; 23551360Seric 23651360Seric default: 23751360Seric /* unknown entry type */ 23851360Seric continue; 23951360Seric } 24051362Seric } 24151923Seric return EX_OK; 24251362Seric } 24351951Seric /* 24451951Seric ** UDBSENDER -- return canonical external name of sender, given local name 24551951Seric ** 24651951Seric ** Parameters: 24751951Seric ** sender -- the name of the sender on the local machine. 24851951Seric ** 24951951Seric ** Returns: 25051951Seric ** The external name for this sender, if derivable from the 25151951Seric ** database. 25251951Seric ** NULL -- if nothing is changed from the database. 25351951Seric ** 25451951Seric ** Side Effects: 25551951Seric ** none. 25651951Seric */ 25751360Seric 25851951Seric char * 25951951Seric udbsender(sender) 26051951Seric char *sender; 26151951Seric { 26251951Seric register char *p; 26351951Seric register struct udbent *up; 26451951Seric int i; 26551951Seric int keylen; 26651951Seric DBT key, info; 26751951Seric char keybuf[128]; 26851951Seric 26951951Seric if (tTd(28, 1)) 27051951Seric printf("udbsender(%s)\n", sender); 27151951Seric 27251951Seric if (!UdbInitialized) 27351951Seric { 27451951Seric if (_udbx_init() == EX_TEMPFAIL) 27551951Seric return NULL; 27651951Seric } 27751951Seric 27851951Seric /* short circuit if no spec */ 27951951Seric if (UdbSpec == NULL || UdbSpec[0] == '\0') 28051951Seric return NULL; 28151951Seric 28251951Seric /* long names can never match and are a pain to deal with */ 28351951Seric if (strlen(sender) > sizeof keybuf - 12) 28451951Seric return NULL; 28551951Seric 28651951Seric /* names beginning with colons indicate metadata */ 28751951Seric if (sender[0] == ':') 28851951Seric return NULL; 28951951Seric 29051951Seric /* build database key */ 29151951Seric (void) strcpy(keybuf, sender); 29251951Seric (void) strcat(keybuf, ":mailname"); 29351951Seric keylen = strlen(keybuf); 29451951Seric 29551951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 29651951Seric { 29751951Seric /* 29851951Seric ** Select action based on entry type. 29951951Seric */ 30051951Seric 30151951Seric switch (up->udb_type) 30251951Seric { 30351951Seric case UDB_DBFETCH: 30451951Seric key.data = keybuf; 30551951Seric key.size = keylen; 30651951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 30751951Seric if (i != 0 || info.size <= 0) 30851951Seric { 30951951Seric if (tTd(28, 2)) 31051951Seric printf("udbsender: no match on %s\n", 31151951Seric keybuf); 31251951Seric continue; 31351951Seric } 31451951Seric 31551951Seric p = xalloc(info.size + 1); 31651951Seric bcopy(info.data, p, info.size); 31751951Seric p[info.size] = '\0'; 31851951Seric if (tTd(28, 1)) 31951951Seric printf("udbsender ==> %s\n", p); 32051951Seric return p; 32151951Seric } 32251951Seric } 32351951Seric 32451951Seric /* 32551951Seric ** Nothing yet. Search again for a default case. But only 32651951Seric ** use it if we also have a forward (:maildrop) pointer already 32751951Seric ** in the database. 32851951Seric */ 32951951Seric 33051951Seric /* build database key */ 33151951Seric (void) strcpy(keybuf, sender); 33251951Seric (void) strcat(keybuf, ":maildrop"); 33351951Seric keylen = strlen(keybuf); 33451951Seric 33551951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 33651951Seric { 33751951Seric switch (up->udb_type) 33851951Seric { 33951951Seric case UDB_DBFETCH: 34051951Seric /* get the default case for this database */ 34151951Seric if (up->udb_default == NULL) 34251951Seric { 34351951Seric key.data = ":default:mailname"; 34451951Seric key.size = strlen(key.data); 34551951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 34651951Seric if (i != 0 || info.size <= 0) 34751951Seric { 34851951Seric /* no default case */ 34951951Seric up->udb_default = ""; 35051951Seric continue; 35151951Seric } 35251951Seric 35351951Seric /* save the default case */ 35451951Seric up->udb_default = xalloc(info.size + 1); 35551951Seric bcopy(info.data, up->udb_default, info.size); 35651951Seric up->udb_default[info.size] = '\0'; 35751951Seric } 35851951Seric else if (up->udb_default[0] == '\0') 35951951Seric continue; 36051951Seric 36151951Seric /* we have a default case -- verify user:maildrop */ 36251951Seric key.data = keybuf; 36351951Seric key.size = keylen; 36451951Seric i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 36551951Seric if (i != 0 || info.size <= 0) 36651951Seric { 36751951Seric /* nope -- no aliasing for this user */ 36851951Seric continue; 36951951Seric } 37051951Seric 37151951Seric /* they exist -- build the actual address */ 37251951Seric p = xalloc(strlen(sender) + strlen(up->udb_default) + 2); 37351951Seric (void) strcpy(p, sender); 37451951Seric (void) strcat(p, "@"); 37551951Seric (void) strcat(p, up->udb_default); 37651951Seric if (tTd(28, 1)) 37751951Seric printf("udbsender ==> %s\n", p); 37851951Seric return p; 37951951Seric } 38051951Seric } 38151951Seric 38251951Seric /* still nothing.... too bad */ 38351951Seric return NULL; 38451951Seric } 38551951Seric /* 38651951Seric ** _UDBX_INIT -- parse the UDB specification, opening any valid entries. 38751951Seric ** 38851951Seric ** Parameters: 38951951Seric ** none. 39051951Seric ** 39151951Seric ** Returns: 39251951Seric ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a 39351951Seric ** database due to a host being down or some similar 39451951Seric ** (recoverable) situation. 39551951Seric ** EX_OK -- otherwise. 39651951Seric ** 39751951Seric ** Side Effects: 39851951Seric ** Fills in the UdbEnts structure from UdbSpec. 39951951Seric */ 40051951Seric 40151363Seric #define MAXUDBOPTS 27 40251363Seric 40351953Seric int 40451362Seric _udbx_init() 40551362Seric { 40651362Seric register char *p; 40751362Seric int i; 40851362Seric register struct udbent *up; 40951362Seric char buf[8192]; 41051360Seric 41151951Seric if (UdbInitialized) 41251951Seric return EX_OK; 41351951Seric 41451908Seric # ifdef UDB_DEFAULT_SPEC 41551908Seric if (UdbSpec == NULL) 41651908Seric UdbSpec = UDB_DEFAULT_SPEC; 41751908Seric # endif 41851908Seric 41951362Seric p = UdbSpec; 42051362Seric up = UdbEnts; 42151762Seric while (p != NULL) 42251362Seric { 42351362Seric char *spec; 42451362Seric auto int rcode; 42551363Seric int nopts; 42651362Seric int nmx; 42751362Seric register struct hostent *h; 42851362Seric char *mxhosts[MAXMXHOSTS + 1]; 42951363Seric struct option opts[MAXUDBOPTS + 1]; 43051362Seric 43151362Seric while (*p == ' ' || *p == '\t' || *p == ',') 43251362Seric p++; 43351362Seric if (*p == '\0') 43451362Seric break; 43551362Seric spec = p; 43651362Seric p = index(p, ','); 43751761Seric if (p != NULL) 43851362Seric *p++ = '\0'; 43951363Seric 44051363Seric /* extract options */ 44151363Seric nopts = _udb_parsespec(spec, opts, MAXUDBOPTS); 44251363Seric 44351363Seric /* 44451363Seric ** Decode database specification. 44551363Seric ** 44651363Seric ** In the sendmail tradition, the leading character 44751363Seric ** defines the semantics of the rest of the entry. 44851363Seric ** 44951363Seric ** +hostname -- send a datagram to the udb server 45051363Seric ** on host "hostname" asking for the 45151363Seric ** home mail server for this user. 45251363Seric ** *hostname -- similar to +hostname, except that the 45351363Seric ** hostname is searched as an MX record; 45451363Seric ** resulting hosts are searched as for 45551363Seric ** +mxhostname. If no MX host is found, 45651363Seric ** this is the same as +hostname. 45751363Seric ** @hostname -- forward email to the indicated host. 45851363Seric ** This should be the last in the list, 45951363Seric ** since it always matches the input. 46051363Seric ** /dbname -- search the named database on the local 46151363Seric ** host using the Berkeley db package. 46251363Seric */ 46351363Seric 46451362Seric switch (*spec) 46551360Seric { 46651362Seric case '+': /* search remote database */ 46751363Seric case '*': /* search remote database (expand MX) */ 46851363Seric if (*spec == '*') 46951363Seric { 47051363Seric nmx = getmxrr(spec + 1, mxhosts, "", &rcode); 47151363Seric if (tTd(28, 16)) 47251363Seric { 47351363Seric int i; 47451362Seric 47551363Seric printf("getmxrr(%s): %d", spec + 1, nmx); 47651363Seric for (i = 0; i <= nmx; i++) 47751363Seric printf(" %s", mxhosts[i]); 47851363Seric printf("\n"); 47951363Seric } 48051363Seric } 48151363Seric else 48251362Seric { 48351363Seric nmx = 1; 48451363Seric mxhosts[0] = spec + 1; 48551362Seric } 48651362Seric 48751362Seric for (i = 0; i < nmx; i++) 48851362Seric { 48951362Seric h = gethostbyname(mxhosts[i]); 49051362Seric if (h == NULL) 49151362Seric continue; 49251362Seric up->udb_type = UDB_REMOTE; 49351362Seric up->udb_addr.sin_family = h->h_addrtype; 49451362Seric bcopy(h->h_addr_list[0], 49551362Seric (char *) &up->udb_addr.sin_addr, 49651362Seric h->h_length); 49751362Seric up->udb_addr.sin_port = UdbPort; 49851362Seric up->udb_timeout = UdbTimeout; 49951362Seric up++; 50051362Seric } 50151362Seric 50251362Seric /* set up a datagram socket */ 50351362Seric if (UdbSock < 0) 50451362Seric { 50551362Seric UdbSock = socket(AF_INET, SOCK_DGRAM, 0); 50651362Seric (void) fcntl(UdbSock, F_SETFD, 1); 50751362Seric } 50851362Seric break; 50951362Seric 51051362Seric case '@': /* forward to remote host */ 51151362Seric up->udb_type = UDB_FORWARD; 51251362Seric up->udb_fwdhost = spec + 1; 51351362Seric up++; 51451362Seric break; 51551362Seric 51651362Seric case '/': /* look up remote name */ 51751831Seric up->udb_dbname = spec; 51851923Seric errno = 0; 51951362Seric up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL); 52051362Seric if (up->udb_dbp == NULL) 52151923Seric { 52251923Seric if (errno != ENOENT && errno != EACCES) 52351951Seric { 52451951Seric up->udb_type = UDB_EOLIST; 52551951Seric goto tempfail; 52651951Seric } 52751362Seric break; 52851923Seric } 52951951Seric up->udb_type = UDB_DBFETCH; 53051362Seric up++; 53151362Seric break; 53251360Seric } 53351362Seric } 53451362Seric up->udb_type = UDB_EOLIST; 53551360Seric 53651362Seric if (tTd(28, 4)) 53751362Seric { 53851951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 53951360Seric { 54051362Seric switch (up->udb_type) 54151362Seric { 54251362Seric case UDB_REMOTE: 54351362Seric printf("REMOTE: addr %s, timeo %d\n", 54451362Seric inet_ntoa(up->udb_addr.sin_addr), 54551362Seric up->udb_timeout); 54651362Seric break; 54751362Seric 54851951Seric case UDB_DBFETCH: 54951951Seric printf("FETCH: file %s\n", 55051830Seric up->udb_dbname); 55151362Seric break; 55251362Seric 55351362Seric case UDB_FORWARD: 55451362Seric printf("FORWARD: host %s\n", 55551362Seric up->udb_fwdhost); 55651362Seric break; 55751362Seric 55851362Seric default: 55951362Seric printf("UNKNOWN\n"); 56051362Seric break; 56151362Seric } 56251360Seric } 56350581Seric } 56451951Seric 56551951Seric UdbInitialized = TRUE; 566*51955Seric errno = 0; 56751951Seric return EX_OK; 56851951Seric 56951951Seric /* 57051951Seric ** On temporary failure, back out anything we've already done 57151951Seric */ 57251951Seric 57351951Seric tempfail: 57451951Seric for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 57551951Seric { 57651951Seric if (up->udb_type == UDB_DBFETCH) 57751951Seric { 57851951Seric (*up->udb_dbp->close)(up->udb_dbp); 57951951Seric } 58051951Seric } 58151951Seric return EX_TEMPFAIL; 58251360Seric } 58350581Seric 58451363Seric int 58551363Seric _udb_parsespec(udbspec, opt, maxopts) 58651363Seric char *udbspec; 58751363Seric struct option opt[]; 58851363Seric int maxopts; 58951363Seric { 59051363Seric register char *spec; 59151363Seric register char *spec_end; 59251363Seric register int optnum; 59351363Seric 59451363Seric spec_end = index(udbspec, ':'); 59551363Seric for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++) 59651363Seric { 59751363Seric register char *p; 59851363Seric 59951363Seric while (isspace(*spec)) 60051363Seric spec++; 60151363Seric spec_end = index(spec, ':'); 60251363Seric if (spec_end != NULL) 60351363Seric *spec_end++ = '\0'; 60451363Seric 60551363Seric opt[optnum].name = spec; 60651363Seric opt[optnum].val = NULL; 60751363Seric p = index(spec, '='); 60851363Seric if (p != NULL) 60951363Seric opt[optnum].val = ++p; 61051363Seric } 61151363Seric return optnum; 61251363Seric } 61351363Seric 61451360Seric #else /* not USERDB */ 61551360Seric 61651923Seric int 61751360Seric udbexpand(a, sendq) 61851360Seric ADDRESS *a; 61951360Seric ADDRESS **sendq; 62051360Seric { 62151923Seric return EX_OK; 62250581Seric } 62350581Seric 62450581Seric #endif /* USERDB */ 625