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*51908Seric static char sccsid [] = "@(#)udb.c 5.12 (Berkeley) 12/12/91 (with USERDB)"; 1251360Seric #else 13*51908Seric static char sccsid [] = "@(#)udb.c 5.12 (Berkeley) 12/12/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> 2351360Seric #include <fcntl.h> 2451360Seric #include <netdb.h> 2550581Seric #include <db.h> 2650581Seric 2750581Seric /* 2851363Seric ** UDBEXPAND.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 */ 3851360Seric union 3951360Seric { 4051360Seric /* type UE_REMOTE -- do remote call for lookup */ 4151360Seric struct 4251360Seric { 4351360Seric struct sockaddr_in _udb_addr; /* address */ 4451360Seric int _udb_timeout; /* timeout */ 4551360Seric } udb_remote; 4651360Seric #define udb_addr udb_u.udb_remote._udb_addr 4751360Seric #define udb_timeout udb_u.udb_remote._udb_timeout 4851360Seric 4951360Seric /* type UE_FORWARD -- forward message to remote */ 5051360Seric struct 5151360Seric { 5251360Seric char *_udb_fwdhost; /* name of forward host */ 5351360Seric } udb_forward; 5451360Seric #define udb_fwdhost udb_u.udb_forward._udb_fwdhost 5551360Seric 5651360Seric /* type UE_LOOKUP -- lookup in local database */ 5751360Seric struct 5851360Seric { 5951360Seric char *_udb_dbname; /* pathname of database */ 6051360Seric DB *_udb_dbp; /* open database ptr */ 6151360Seric } udb_lookup; 6251360Seric #define udb_dbname udb_u.udb_lookup._udb_dbname 6351360Seric #define udb_dbp udb_u.udb_lookup._udb_dbp 6451360Seric } udb_u; 6551360Seric }; 6651360Seric 6751360Seric #define UDB_EOLIST 0 /* end of list */ 6851360Seric #define UDB_SKIP 1 /* skip this entry */ 6951360Seric #define UDB_REMOTE 2 /* look up in remote database */ 7051360Seric #define UDB_LOOKUP 3 /* look up in local database */ 7151360Seric #define UDB_FORWARD 4 /* forward to remote host */ 7251360Seric 7351360Seric #define MAXUDBENT 10 /* maximum number of UDB entries */ 7451360Seric 7551363Seric 7651363Seric struct option 7751363Seric { 7851363Seric char *name; 7951363Seric char *val; 8051363Seric }; 8151363Seric /* 8251363Seric ** UDBEXPAND -- look up user in database and expand 8351363Seric ** 8451363Seric ** Parameters: 8551363Seric ** a -- address to expand. 8651363Seric ** sendq -- pointer to head of sendq to put the expansions in. 8751363Seric ** 8851363Seric ** Returns: 8951363Seric ** none. 9051363Seric ** 9151363Seric ** Side Effects: 9251363Seric ** Modifies sendq. 9351363Seric */ 9451363Seric 9551363Seric int UdbPort = 1616; 9651363Seric int UdbTimeout = 10; 9751363Seric 9851362Seric struct udbent UdbEnts[MAXUDBENT + 1]; 9951362Seric int UdbSock = -1; 10051360Seric 10150581Seric void 10250581Seric udbexpand(a, sendq) 10350581Seric register ADDRESS *a; 10450581Seric ADDRESS **sendq; 10550581Seric { 10650581Seric int i; 10750581Seric register char *p; 10850581Seric DBT key; 10950581Seric DBT info; 11051360Seric static bool firstcall = TRUE; 11151360Seric bool breakout; 11251360Seric register struct udbent *up; 11351362Seric int keylen; 11451362Seric char keybuf[128]; 11550581Seric char buf[8192]; 11650581Seric 11750581Seric if (tTd(28, 1)) 11850581Seric printf("expand(%s)\n", a->q_paddr); 11950581Seric 12050581Seric /* make certain we are supposed to send to this address */ 12151360Seric if (bitset(QDONTSEND, a->q_flags) || 12251360Seric UdbSpec == NULL || UdbSpec[0] == '\0') 12350581Seric return; 12450581Seric CurEnv->e_to = a->q_paddr; 12550581Seric 12651360Seric /* on first call, locate the database */ 12751360Seric if (firstcall) 12850581Seric { 12951362Seric extern void _udbx_init(); 13051362Seric 13151362Seric _udbx_init(); 13251360Seric firstcall = FALSE; 13351362Seric } 13450581Seric 13551362Seric /* if name is too long, assume it won't match */ 13651362Seric if (strlen(a->q_user) > sizeof keybuf - 12) 13751362Seric return; 13851360Seric 13951362Seric /* if name begins with a colon, it indicates our metadata */ 14051362Seric if (a->q_user[0] == ':') 14151362Seric return; 14251360Seric 14351362Seric /* build actual database key */ 14451362Seric (void) strcpy(keybuf, a->q_user); 14551362Seric (void) strcat(keybuf, ":maildrop"); 14651362Seric keylen = strlen(keybuf); 14751360Seric 14851360Seric breakout = FALSE; 14951362Seric for (up = UdbEnts; !breakout; up++) 15050581Seric { 15151360Seric char *user; 15251360Seric struct timeval timeout; 15351360Seric fd_set fdset; 15450581Seric 15551360Seric /* 15651360Seric ** Select action based on entry type. 15751360Seric ** 15851360Seric ** On dropping out of this switch, "class" should 15951360Seric ** explain the type of the data, and "user" should 16051360Seric ** contain the user information. 16151360Seric */ 16250581Seric 16351360Seric switch (up->udb_type) 16451360Seric { 16551360Seric case UDB_LOOKUP: 16651362Seric key.data = keybuf; 16751362Seric key.size = keylen; 16851362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR); 16951360Seric if (i != 0 || info.size <= 0) 17051360Seric { 17151360Seric if (i < 0) 17251360Seric syserr("udbexpand: db-get stat %s"); 17351360Seric if (tTd(28, 2)) 17451362Seric printf("expand: no match on %s\n", keybuf); 17551360Seric continue; 17651360Seric } 17750581Seric 17851830Seric while (i == 0 && key.size == keylen && 17951830Seric bcmp(key.data, keybuf, keylen) == 0) 18051362Seric { 18151830Seric breakout = TRUE; 18251362Seric if (info.size < sizeof buf) 18351362Seric user = buf; 18451362Seric else 18551362Seric user = xalloc(info.size + 1); 18651362Seric bcopy(info.data, user, info.size); 18751362Seric user[info.size] = '\0'; 18850581Seric 18951362Seric message(Arpa_Info, "expanded to %s", user); 19051362Seric AliasLevel++; 19151362Seric sendtolist(user, a, sendq); 19251362Seric AliasLevel--; 19351362Seric 19451362Seric if (user != buf) 19551362Seric free(user); 19651362Seric 19751362Seric /* get the next record */ 19851362Seric i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT); 19951830Seric } 20051360Seric break; 20151360Seric 20251360Seric case UDB_REMOTE: 20351741Seric /* not yet implemented */ 20451741Seric continue; 20551362Seric 20651360Seric case UDB_FORWARD: 20751360Seric i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1; 20851360Seric if (i < sizeof buf) 20951360Seric user = buf; 21051360Seric else 21151360Seric user = xalloc(i + 1); 21251360Seric (void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost); 21351362Seric message(Arpa_Info, "expanded to %s", user); 21451362Seric AliasLevel++; 21551362Seric sendtolist(user, a, sendq); 21651362Seric AliasLevel--; 21751362Seric if (user != buf) 21851362Seric free(user); 21951362Seric breakout = TRUE; 22051360Seric break; 22151360Seric 22251360Seric case UDB_EOLIST: 22351360Seric breakout = TRUE; 22451360Seric continue; 22551360Seric 22651360Seric default: 22751360Seric /* unknown entry type */ 22851360Seric continue; 22951360Seric } 23051362Seric } 23151362Seric } 23251360Seric 23351363Seric #define MAXUDBOPTS 27 23451363Seric 23551362Seric void 23651362Seric _udbx_init() 23751362Seric { 23851362Seric register char *p; 23951362Seric int i; 24051362Seric register struct udbent *up; 24151362Seric char buf[8192]; 24251360Seric 243*51908Seric # ifdef UDB_DEFAULT_SPEC 244*51908Seric if (UdbSpec == NULL) 245*51908Seric UdbSpec = UDB_DEFAULT_SPEC; 246*51908Seric # endif 247*51908Seric 24851362Seric p = UdbSpec; 24951362Seric up = UdbEnts; 25051762Seric while (p != NULL) 25151362Seric { 25251362Seric char *spec; 25351362Seric auto int rcode; 25451363Seric int nopts; 25551362Seric int nmx; 25651362Seric register struct hostent *h; 25751362Seric char *mxhosts[MAXMXHOSTS + 1]; 25851363Seric struct option opts[MAXUDBOPTS + 1]; 25951362Seric 26051362Seric while (*p == ' ' || *p == '\t' || *p == ',') 26151362Seric p++; 26251362Seric if (*p == '\0') 26351362Seric break; 26451362Seric spec = p; 26551362Seric p = index(p, ','); 26651761Seric if (p != NULL) 26751362Seric *p++ = '\0'; 26851363Seric 26951363Seric /* extract options */ 27051363Seric nopts = _udb_parsespec(spec, opts, MAXUDBOPTS); 27151363Seric 27251363Seric /* 27351363Seric ** Decode database specification. 27451363Seric ** 27551363Seric ** In the sendmail tradition, the leading character 27651363Seric ** defines the semantics of the rest of the entry. 27751363Seric ** 27851363Seric ** +hostname -- send a datagram to the udb server 27951363Seric ** on host "hostname" asking for the 28051363Seric ** home mail server for this user. 28151363Seric ** *hostname -- similar to +hostname, except that the 28251363Seric ** hostname is searched as an MX record; 28351363Seric ** resulting hosts are searched as for 28451363Seric ** +mxhostname. If no MX host is found, 28551363Seric ** this is the same as +hostname. 28651363Seric ** @hostname -- forward email to the indicated host. 28751363Seric ** This should be the last in the list, 28851363Seric ** since it always matches the input. 28951363Seric ** /dbname -- search the named database on the local 29051363Seric ** host using the Berkeley db package. 29151363Seric */ 29251363Seric 29351362Seric switch (*spec) 29451360Seric { 29551362Seric case '+': /* search remote database */ 29651363Seric case '*': /* search remote database (expand MX) */ 29751363Seric if (*spec == '*') 29851363Seric { 29951363Seric nmx = getmxrr(spec + 1, mxhosts, "", &rcode); 30051363Seric if (tTd(28, 16)) 30151363Seric { 30251363Seric int i; 30351362Seric 30451363Seric printf("getmxrr(%s): %d", spec + 1, nmx); 30551363Seric for (i = 0; i <= nmx; i++) 30651363Seric printf(" %s", mxhosts[i]); 30751363Seric printf("\n"); 30851363Seric } 30951363Seric } 31051363Seric else 31151362Seric { 31251363Seric nmx = 1; 31351363Seric mxhosts[0] = spec + 1; 31451362Seric } 31551362Seric 31651362Seric for (i = 0; i < nmx; i++) 31751362Seric { 31851362Seric h = gethostbyname(mxhosts[i]); 31951362Seric if (h == NULL) 32051362Seric continue; 32151362Seric up->udb_type = UDB_REMOTE; 32251362Seric up->udb_addr.sin_family = h->h_addrtype; 32351362Seric bcopy(h->h_addr_list[0], 32451362Seric (char *) &up->udb_addr.sin_addr, 32551362Seric h->h_length); 32651362Seric up->udb_addr.sin_port = UdbPort; 32751362Seric up->udb_timeout = UdbTimeout; 32851362Seric up++; 32951362Seric } 33051362Seric 33151362Seric /* set up a datagram socket */ 33251362Seric if (UdbSock < 0) 33351362Seric { 33451362Seric UdbSock = socket(AF_INET, SOCK_DGRAM, 0); 33551362Seric (void) fcntl(UdbSock, F_SETFD, 1); 33651362Seric } 33751362Seric break; 33851362Seric 33951362Seric case '@': /* forward to remote host */ 34051362Seric up->udb_type = UDB_FORWARD; 34151362Seric up->udb_fwdhost = spec + 1; 34251362Seric up++; 34351362Seric break; 34451362Seric 34551362Seric case '/': /* look up remote name */ 34651831Seric up->udb_dbname = spec; 34751362Seric up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL); 34851362Seric if (up->udb_dbp == NULL) 34951362Seric break; 35051362Seric up->udb_type = UDB_LOOKUP; 35151362Seric up++; 35251362Seric break; 35351360Seric } 35451362Seric } 35551362Seric up->udb_type = UDB_EOLIST; 35651360Seric 35751362Seric if (tTd(28, 4)) 35851362Seric { 35951362Seric for (up = UdbEnts; ; up++) 36051360Seric { 36151362Seric switch (up->udb_type) 36251362Seric { 36351362Seric case UDB_EOLIST: 36451362Seric return; 36551362Seric 36651362Seric case UDB_REMOTE: 36751362Seric printf("REMOTE: addr %s, timeo %d\n", 36851362Seric inet_ntoa(up->udb_addr.sin_addr), 36951362Seric up->udb_timeout); 37051362Seric break; 37151362Seric 37251362Seric case UDB_LOOKUP: 37351830Seric printf("LOOKUP: file %s\n", 37451830Seric up->udb_dbname); 37551362Seric break; 37651362Seric 37751362Seric case UDB_FORWARD: 37851362Seric printf("FORWARD: host %s\n", 37951362Seric up->udb_fwdhost); 38051362Seric break; 38151362Seric 38251362Seric default: 38351362Seric printf("UNKNOWN\n"); 38451362Seric break; 38551362Seric } 38651360Seric } 38750581Seric } 38851360Seric } 38950581Seric 39051363Seric int 39151363Seric _udb_parsespec(udbspec, opt, maxopts) 39251363Seric char *udbspec; 39351363Seric struct option opt[]; 39451363Seric int maxopts; 39551363Seric { 39651363Seric register char *spec; 39751363Seric register char *spec_end; 39851363Seric register int optnum; 39951363Seric 40051363Seric spec_end = index(udbspec, ':'); 40151363Seric for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++) 40251363Seric { 40351363Seric register char *p; 40451363Seric 40551363Seric while (isspace(*spec)) 40651363Seric spec++; 40751363Seric spec_end = index(spec, ':'); 40851363Seric if (spec_end != NULL) 40951363Seric *spec_end++ = '\0'; 41051363Seric 41151363Seric opt[optnum].name = spec; 41251363Seric opt[optnum].val = NULL; 41351363Seric p = index(spec, '='); 41451363Seric if (p != NULL) 41551363Seric opt[optnum].val = ++p; 41651363Seric } 41751363Seric return optnum; 41851363Seric } 41951363Seric 42051360Seric #else /* not USERDB */ 42151360Seric 42251360Seric void 42351360Seric udbexpand(a, sendq) 42451360Seric ADDRESS *a; 42551360Seric ADDRESS **sendq; 42651360Seric { 42751360Seric return; 42850581Seric } 42950581Seric 43050581Seric #endif /* USERDB */ 431