1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 #ifdef USERDB 11 static char sccsid [] = "@(#)udb.c 6.5 (Berkeley) 02/14/93 (with USERDB)"; 12 #else 13 static char sccsid [] = "@(#)udb.c 6.5 (Berkeley) 02/14/93 (without USERDB)"; 14 #endif 15 #endif 16 17 #include "sendmail.h" 18 19 #ifdef USERDB 20 21 #include <sys/time.h> 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <netdb.h> 25 #include <db.h> 26 27 /* 28 ** UDB.C -- interface between sendmail and Berkeley User Data Base. 29 ** 30 ** This depends on the 4.4BSD db package. 31 */ 32 33 34 struct udbent 35 { 36 char *udb_spec; /* string version of spec */ 37 int udb_type; /* type of entry */ 38 char *udb_default; /* default host for outgoing mail */ 39 union 40 { 41 /* type UE_REMOTE -- do remote call for lookup */ 42 struct 43 { 44 struct sockaddr_in _udb_addr; /* address */ 45 int _udb_timeout; /* timeout */ 46 } udb_remote; 47 #define udb_addr udb_u.udb_remote._udb_addr 48 #define udb_timeout udb_u.udb_remote._udb_timeout 49 50 /* type UE_FORWARD -- forward message to remote */ 51 struct 52 { 53 char *_udb_fwdhost; /* name of forward host */ 54 } udb_forward; 55 #define udb_fwdhost udb_u.udb_forward._udb_fwdhost 56 57 /* type UE_FETCH -- lookup in local database */ 58 struct 59 { 60 char *_udb_dbname; /* pathname of database */ 61 DB *_udb_dbp; /* open database ptr */ 62 } udb_lookup; 63 #define udb_dbname udb_u.udb_lookup._udb_dbname 64 #define udb_dbp udb_u.udb_lookup._udb_dbp 65 } udb_u; 66 }; 67 68 #define UDB_EOLIST 0 /* end of list */ 69 #define UDB_SKIP 1 /* skip this entry */ 70 #define UDB_REMOTE 2 /* look up in remote database */ 71 #define UDB_DBFETCH 3 /* look up in local database */ 72 #define UDB_FORWARD 4 /* forward to remote host */ 73 74 #define MAXUDBENT 10 /* maximum number of UDB entries */ 75 76 77 struct option 78 { 79 char *name; 80 char *val; 81 }; 82 /* 83 ** UDBEXPAND -- look up user in database and expand 84 ** 85 ** Parameters: 86 ** a -- address to expand. 87 ** sendq -- pointer to head of sendq to put the expansions in. 88 ** 89 ** Returns: 90 ** EX_TEMPFAIL -- if something "odd" happened -- probably due 91 ** to accessing a file on an NFS server that is down. 92 ** EX_OK -- otherwise. 93 ** 94 ** Side Effects: 95 ** Modifies sendq. 96 */ 97 98 int UdbPort = 1616; 99 int UdbTimeout = 10; 100 101 struct udbent UdbEnts[MAXUDBENT + 1]; 102 int UdbSock = -1; 103 bool UdbInitialized = FALSE; 104 105 int 106 udbexpand(a, sendq, e) 107 register ADDRESS *a; 108 ADDRESS **sendq; 109 register ENVELOPE *e; 110 { 111 int i; 112 register char *p; 113 DBT key; 114 DBT info; 115 bool breakout; 116 register struct udbent *up; 117 int keylen; 118 char keybuf[MAXKEY]; 119 char buf[BUFSIZ]; 120 121 if (tTd(28, 1)) 122 printf("expand(%s)\n", a->q_paddr); 123 124 /* make certain we are supposed to send to this address */ 125 if (bitset(QDONTSEND, a->q_flags)) 126 return EX_OK; 127 e->e_to = a->q_paddr; 128 129 /* on first call, locate the database */ 130 if (!UdbInitialized) 131 { 132 extern int _udbx_init(); 133 134 if (_udbx_init() == EX_TEMPFAIL) 135 return EX_TEMPFAIL; 136 } 137 138 /* short circuit the process if no chance of a match */ 139 if (UdbSpec == NULL || UdbSpec[0] == '\0') 140 return EX_OK; 141 142 /* if name is too long, assume it won't match */ 143 if (strlen(a->q_user) > sizeof keybuf - 12) 144 return EX_OK; 145 146 /* if name begins with a colon, it indicates our metadata */ 147 if (a->q_user[0] == ':') 148 return EX_OK; 149 150 /* build actual database key */ 151 (void) strcpy(keybuf, a->q_user); 152 (void) strcat(keybuf, ":maildrop"); 153 keylen = strlen(keybuf); 154 155 breakout = FALSE; 156 for (up = UdbEnts; !breakout; up++) 157 { 158 char *user; 159 160 /* 161 ** Select action based on entry type. 162 ** 163 ** On dropping out of this switch, "class" should 164 ** explain the type of the data, and "user" should 165 ** contain the user information. 166 */ 167 168 switch (up->udb_type) 169 { 170 case UDB_DBFETCH: 171 key.data = keybuf; 172 key.size = keylen; 173 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_CURSOR); 174 if (i > 0 || info.size <= 0) 175 { 176 if (tTd(28, 2)) 177 printf("expand: no match on %s\n", keybuf); 178 continue; 179 } 180 181 while (i == 0 && key.size == keylen && 182 bcmp(key.data, keybuf, keylen) == 0) 183 { 184 breakout = TRUE; 185 if (info.size < sizeof buf) 186 user = buf; 187 else 188 user = xalloc(info.size + 1); 189 bcopy(info.data, user, info.size); 190 user[info.size] = '\0'; 191 192 message(Arpa_Info, "expanded to %s", user); 193 #ifdef LOG 194 if (LogLevel >= 10) 195 syslog(LOG_INFO, "%s: expand %s => %s", 196 e->e_id, e->e_to, user); 197 #endif 198 AliasLevel++; 199 sendtolist(user, a, sendq, e); 200 AliasLevel--; 201 202 if (user != buf) 203 free(user); 204 205 /* get the next record */ 206 i = (*up->udb_dbp->seq)(up->udb_dbp, &key, &info, R_NEXT); 207 } 208 if (i < 0) 209 { 210 syserr("udbexpand: db-get stat %s"); 211 return EX_TEMPFAIL; 212 } 213 break; 214 215 case UDB_REMOTE: 216 /* not yet implemented */ 217 continue; 218 219 case UDB_FORWARD: 220 i = strlen(up->udb_fwdhost) + strlen(a->q_user) + 1; 221 if (i < sizeof buf) 222 user = buf; 223 else 224 user = xalloc(i + 1); 225 (void) sprintf(user, "%s@%s", a->q_user, up->udb_fwdhost); 226 message(Arpa_Info, "expanded to %s", user); 227 AliasLevel++; 228 sendtolist(user, a, sendq, e); 229 AliasLevel--; 230 if (user != buf) 231 free(user); 232 breakout = TRUE; 233 break; 234 235 case UDB_EOLIST: 236 breakout = TRUE; 237 continue; 238 239 default: 240 /* unknown entry type */ 241 continue; 242 } 243 } 244 return EX_OK; 245 } 246 /* 247 ** UDBSENDER -- return canonical external name of sender, given local name 248 ** 249 ** Parameters: 250 ** sender -- the name of the sender on the local machine. 251 ** 252 ** Returns: 253 ** The external name for this sender, if derivable from the 254 ** database. 255 ** NULL -- if nothing is changed from the database. 256 ** 257 ** Side Effects: 258 ** none. 259 */ 260 261 char * 262 udbsender(sender) 263 char *sender; 264 { 265 register char *p; 266 register struct udbent *up; 267 int i; 268 int keylen; 269 DBT key, info; 270 char keybuf[MAXKEY]; 271 272 if (tTd(28, 1)) 273 printf("udbsender(%s)\n", sender); 274 275 if (!UdbInitialized) 276 { 277 if (_udbx_init() == EX_TEMPFAIL) 278 return NULL; 279 } 280 281 /* short circuit if no spec */ 282 if (UdbSpec == NULL || UdbSpec[0] == '\0') 283 return NULL; 284 285 /* long names can never match and are a pain to deal with */ 286 if (strlen(sender) > sizeof keybuf - 12) 287 return NULL; 288 289 /* names beginning with colons indicate metadata */ 290 if (sender[0] == ':') 291 return NULL; 292 293 /* build database key */ 294 (void) strcpy(keybuf, sender); 295 (void) strcat(keybuf, ":mailname"); 296 keylen = strlen(keybuf); 297 298 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 299 { 300 /* 301 ** Select action based on entry type. 302 */ 303 304 switch (up->udb_type) 305 { 306 case UDB_DBFETCH: 307 key.data = keybuf; 308 key.size = keylen; 309 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 310 if (i != 0 || info.size <= 0) 311 { 312 if (tTd(28, 2)) 313 printf("udbsender: no match on %s\n", 314 keybuf); 315 continue; 316 } 317 318 p = xalloc(info.size + 1); 319 bcopy(info.data, p, info.size); 320 p[info.size] = '\0'; 321 if (tTd(28, 1)) 322 printf("udbsender ==> %s\n", p); 323 return p; 324 } 325 } 326 327 /* 328 ** Nothing yet. Search again for a default case. But only 329 ** use it if we also have a forward (:maildrop) pointer already 330 ** in the database. 331 */ 332 333 /* build database key */ 334 (void) strcpy(keybuf, sender); 335 (void) strcat(keybuf, ":maildrop"); 336 keylen = strlen(keybuf); 337 338 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 339 { 340 switch (up->udb_type) 341 { 342 case UDB_DBFETCH: 343 /* get the default case for this database */ 344 if (up->udb_default == NULL) 345 { 346 key.data = ":default:mailname"; 347 key.size = strlen(key.data); 348 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 349 if (i != 0 || info.size <= 0) 350 { 351 /* no default case */ 352 up->udb_default = ""; 353 continue; 354 } 355 356 /* save the default case */ 357 up->udb_default = xalloc(info.size + 1); 358 bcopy(info.data, up->udb_default, info.size); 359 up->udb_default[info.size] = '\0'; 360 } 361 else if (up->udb_default[0] == '\0') 362 continue; 363 364 /* we have a default case -- verify user:maildrop */ 365 key.data = keybuf; 366 key.size = keylen; 367 i = (*up->udb_dbp->get)(up->udb_dbp, &key, &info, 0); 368 if (i != 0 || info.size <= 0) 369 { 370 /* nope -- no aliasing for this user */ 371 continue; 372 } 373 374 /* they exist -- build the actual address */ 375 p = xalloc(strlen(sender) + strlen(up->udb_default) + 2); 376 (void) strcpy(p, sender); 377 (void) strcat(p, "@"); 378 (void) strcat(p, up->udb_default); 379 if (tTd(28, 1)) 380 printf("udbsender ==> %s\n", p); 381 return p; 382 } 383 } 384 385 /* still nothing.... too bad */ 386 return NULL; 387 } 388 /* 389 ** _UDBX_INIT -- parse the UDB specification, opening any valid entries. 390 ** 391 ** Parameters: 392 ** none. 393 ** 394 ** Returns: 395 ** EX_TEMPFAIL -- if it appeared it couldn't get hold of a 396 ** database due to a host being down or some similar 397 ** (recoverable) situation. 398 ** EX_OK -- otherwise. 399 ** 400 ** Side Effects: 401 ** Fills in the UdbEnts structure from UdbSpec. 402 */ 403 404 #define MAXUDBOPTS 27 405 406 int 407 _udbx_init() 408 { 409 register char *p; 410 int i; 411 register struct udbent *up; 412 char buf[BUFSIZ]; 413 414 if (UdbInitialized) 415 return EX_OK; 416 417 # ifdef UDB_DEFAULT_SPEC 418 if (UdbSpec == NULL) 419 UdbSpec = UDB_DEFAULT_SPEC; 420 # endif 421 422 p = UdbSpec; 423 up = UdbEnts; 424 while (p != NULL) 425 { 426 char *spec; 427 auto int rcode; 428 int nopts; 429 int nmx; 430 register struct hostent *h; 431 char *mxhosts[MAXMXHOSTS + 1]; 432 struct option opts[MAXUDBOPTS + 1]; 433 434 while (*p == ' ' || *p == '\t' || *p == ',') 435 p++; 436 if (*p == '\0') 437 break; 438 spec = p; 439 p = strchr(p, ','); 440 if (p != NULL) 441 *p++ = '\0'; 442 443 /* extract options */ 444 nopts = _udb_parsespec(spec, opts, MAXUDBOPTS); 445 446 /* 447 ** Decode database specification. 448 ** 449 ** In the sendmail tradition, the leading character 450 ** defines the semantics of the rest of the entry. 451 ** 452 ** +hostname -- send a datagram to the udb server 453 ** on host "hostname" asking for the 454 ** home mail server for this user. 455 ** *hostname -- similar to +hostname, except that the 456 ** hostname is searched as an MX record; 457 ** resulting hosts are searched as for 458 ** +mxhostname. If no MX host is found, 459 ** this is the same as +hostname. 460 ** @hostname -- forward email to the indicated host. 461 ** This should be the last in the list, 462 ** since it always matches the input. 463 ** /dbname -- search the named database on the local 464 ** host using the Berkeley db package. 465 */ 466 467 switch (*spec) 468 { 469 case '+': /* search remote database */ 470 case '*': /* search remote database (expand MX) */ 471 if (*spec == '*') 472 { 473 #ifdef NAMED_BIND 474 nmx = getmxrr(spec + 1, mxhosts, "", &rcode); 475 #else 476 mxhosts[0] = spec + 1; 477 nmx = 1; 478 rcode = 0; 479 #endif 480 if (tTd(28, 16)) 481 { 482 int i; 483 484 printf("getmxrr(%s): %d", spec + 1, nmx); 485 for (i = 0; i <= nmx; i++) 486 printf(" %s", mxhosts[i]); 487 printf("\n"); 488 } 489 } 490 else 491 { 492 nmx = 1; 493 mxhosts[0] = spec + 1; 494 } 495 496 for (i = 0; i < nmx; i++) 497 { 498 h = gethostbyname(mxhosts[i]); 499 if (h == NULL) 500 continue; 501 up->udb_type = UDB_REMOTE; 502 up->udb_addr.sin_family = h->h_addrtype; 503 bcopy(h->h_addr_list[0], 504 (char *) &up->udb_addr.sin_addr, 505 h->h_length); 506 up->udb_addr.sin_port = UdbPort; 507 up->udb_timeout = UdbTimeout; 508 up++; 509 } 510 511 /* set up a datagram socket */ 512 if (UdbSock < 0) 513 { 514 UdbSock = socket(AF_INET, SOCK_DGRAM, 0); 515 (void) fcntl(UdbSock, F_SETFD, 1); 516 } 517 break; 518 519 case '@': /* forward to remote host */ 520 up->udb_type = UDB_FORWARD; 521 up->udb_fwdhost = spec + 1; 522 up++; 523 break; 524 525 case '/': /* look up remote name */ 526 up->udb_dbname = spec; 527 errno = 0; 528 up->udb_dbp = dbopen(spec, O_RDONLY, 0644, DB_BTREE, NULL); 529 if (up->udb_dbp == NULL) 530 { 531 if (errno != ENOENT && errno != EACCES) 532 { 533 up->udb_type = UDB_EOLIST; 534 goto tempfail; 535 } 536 break; 537 } 538 up->udb_type = UDB_DBFETCH; 539 up++; 540 break; 541 } 542 } 543 up->udb_type = UDB_EOLIST; 544 545 if (tTd(28, 4)) 546 { 547 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 548 { 549 switch (up->udb_type) 550 { 551 case UDB_REMOTE: 552 printf("REMOTE: addr %s, timeo %d\n", 553 inet_ntoa(up->udb_addr.sin_addr), 554 up->udb_timeout); 555 break; 556 557 case UDB_DBFETCH: 558 printf("FETCH: file %s\n", 559 up->udb_dbname); 560 break; 561 562 case UDB_FORWARD: 563 printf("FORWARD: host %s\n", 564 up->udb_fwdhost); 565 break; 566 567 default: 568 printf("UNKNOWN\n"); 569 break; 570 } 571 } 572 } 573 574 UdbInitialized = TRUE; 575 errno = 0; 576 return EX_OK; 577 578 /* 579 ** On temporary failure, back out anything we've already done 580 */ 581 582 tempfail: 583 for (up = UdbEnts; up->udb_type != UDB_EOLIST; up++) 584 { 585 if (up->udb_type == UDB_DBFETCH) 586 { 587 (*up->udb_dbp->close)(up->udb_dbp); 588 } 589 } 590 return EX_TEMPFAIL; 591 } 592 593 int 594 _udb_parsespec(udbspec, opt, maxopts) 595 char *udbspec; 596 struct option opt[]; 597 int maxopts; 598 { 599 register char *spec; 600 register char *spec_end; 601 register int optnum; 602 603 spec_end = strchr(udbspec, ':'); 604 for (optnum = 0; optnum < maxopts && (spec = spec_end) != NULL; optnum++) 605 { 606 register char *p; 607 608 while (isspace(*spec)) 609 spec++; 610 spec_end = strchr(spec, ':'); 611 if (spec_end != NULL) 612 *spec_end++ = '\0'; 613 614 opt[optnum].name = spec; 615 opt[optnum].val = NULL; 616 p = strchr(spec, '='); 617 if (p != NULL) 618 opt[optnum].val = ++p; 619 } 620 return optnum; 621 } 622 623 #else /* not USERDB */ 624 625 int 626 udbexpand(a, sendq, e) 627 ADDRESS *a; 628 ADDRESS **sendq; 629 ENVELOPE *e; 630 { 631 return EX_OK; 632 } 633 634 #endif /* USERDB */ 635