1 /* 2 * Copyright (c) 1998-2002 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14 #include <sendmail.h> 15 #if MILTER 16 # include <libmilter/mfdef.h> 17 #endif /* MILTER */ 18 19 SM_RCSID("@(#)$Id: srvrsmtp.c,v 8.829 2002/06/17 21:54:57 gshapiro Exp $") 20 21 #if SASL || STARTTLS 22 # include <sys/time.h> 23 # include "sfsasl.h" 24 #endif /* SASL || STARTTLS */ 25 #if SASL 26 # define ENC64LEN(l) (((l) + 2) * 4 / 3 + 1) 27 static int saslmechs __P((sasl_conn_t *, char **)); 28 #endif /* SASL */ 29 #if STARTTLS 30 # include <sysexits.h> 31 32 static SSL_CTX *srv_ctx = NULL; /* TLS server context */ 33 static SSL *srv_ssl = NULL; /* per connection context */ 34 35 static bool tls_ok_srv = false; 36 37 extern void tls_set_verify __P((SSL_CTX *, SSL *, bool)); 38 # define TLS_VERIFY_CLIENT() tls_set_verify(srv_ctx, srv_ssl, \ 39 bitset(SRV_VRFY_CLT, features)) 40 #endif /* STARTTLS */ 41 42 /* server features */ 43 #define SRV_NONE 0x0000 /* none... */ 44 #define SRV_OFFER_TLS 0x0001 /* offer STARTTLS */ 45 #define SRV_VRFY_CLT 0x0002 /* request a cert */ 46 #define SRV_OFFER_AUTH 0x0004 /* offer AUTH */ 47 #define SRV_OFFER_ETRN 0x0008 /* offer ETRN */ 48 #define SRV_OFFER_VRFY 0x0010 /* offer VRFY (not yet used) */ 49 #define SRV_OFFER_EXPN 0x0020 /* offer EXPN */ 50 #define SRV_OFFER_VERB 0x0040 /* offer VERB */ 51 #define SRV_OFFER_DSN 0x0080 /* offer DSN */ 52 #if PIPELINING 53 # define SRV_OFFER_PIPE 0x0100 /* offer PIPELINING */ 54 # if _FFR_NO_PIPE 55 # define SRV_NO_PIPE 0x0200 /* disable PIPELINING, sleep if used */ 56 # endif /* _FFR_NO_PIPE */ 57 #endif /* PIPELINING */ 58 #define SRV_REQ_AUTH 0x0400 /* require AUTH */ 59 #define SRV_TMP_FAIL 0x1000 /* ruleset caused a temporary failure */ 60 61 static unsigned int srvfeatures __P((ENVELOPE *, char *, unsigned int)); 62 63 static time_t checksmtpattack __P((volatile unsigned int *, int, bool, 64 char *, ENVELOPE *)); 65 static void mail_esmtp_args __P((char *, char *, ENVELOPE *)); 66 static void printvrfyaddr __P((ADDRESS *, bool, bool)); 67 static void rcpt_esmtp_args __P((ADDRESS *, char *, char *, ENVELOPE *)); 68 static char *skipword __P((char *volatile, char *)); 69 static void setup_smtpd_io __P((void)); 70 extern ENVELOPE BlankEnvelope; 71 72 #define SKIP_SPACE(s) while (isascii(*s) && isspace(*s)) \ 73 (s)++ 74 75 /* 76 ** SMTP -- run the SMTP protocol. 77 ** 78 ** Parameters: 79 ** nullserver -- if non-NULL, rejection message for 80 ** (almost) all SMTP commands. 81 ** d_flags -- daemon flags 82 ** e -- the envelope. 83 ** 84 ** Returns: 85 ** never. 86 ** 87 ** Side Effects: 88 ** Reads commands from the input channel and processes them. 89 */ 90 91 /* 92 ** Notice: The smtp server doesn't have a session context like the client 93 ** side has (mci). Therefore some data (session oriented) is allocated 94 ** or assigned to the "wrong" structure (esp. STARTTLS, AUTH). 95 ** This should be fixed in a successor version. 96 */ 97 98 struct cmd 99 { 100 char *cmd_name; /* command name */ 101 int cmd_code; /* internal code, see below */ 102 }; 103 104 /* values for cmd_code */ 105 #define CMDERROR 0 /* bad command */ 106 #define CMDMAIL 1 /* mail -- designate sender */ 107 #define CMDRCPT 2 /* rcpt -- designate recipient */ 108 #define CMDDATA 3 /* data -- send message text */ 109 #define CMDRSET 4 /* rset -- reset state */ 110 #define CMDVRFY 5 /* vrfy -- verify address */ 111 #define CMDEXPN 6 /* expn -- expand address */ 112 #define CMDNOOP 7 /* noop -- do nothing */ 113 #define CMDQUIT 8 /* quit -- close connection and die */ 114 #define CMDHELO 9 /* helo -- be polite */ 115 #define CMDHELP 10 /* help -- give usage info */ 116 #define CMDEHLO 11 /* ehlo -- extended helo (RFC 1425) */ 117 #define CMDETRN 12 /* etrn -- flush queue */ 118 #if SASL 119 # define CMDAUTH 13 /* auth -- SASL authenticate */ 120 #endif /* SASL */ 121 #if STARTTLS 122 # define CMDSTLS 14 /* STARTTLS -- start TLS session */ 123 #endif /* STARTTLS */ 124 /* non-standard commands */ 125 #define CMDVERB 17 /* verb -- go into verbose mode */ 126 /* unimplemented commands from RFC 821 */ 127 #define CMDUNIMPL 19 /* unimplemented rfc821 commands */ 128 /* use this to catch and log "door handle" attempts on your system */ 129 #define CMDLOGBOGUS 23 /* bogus command that should be logged */ 130 /* debugging-only commands, only enabled if SMTPDEBUG is defined */ 131 #define CMDDBGQSHOW 24 /* showq -- show send queue */ 132 #define CMDDBGDEBUG 25 /* debug -- set debug mode */ 133 134 /* 135 ** Note: If you change this list, remember to update 'helpfile' 136 */ 137 138 static struct cmd CmdTab[] = 139 { 140 { "mail", CMDMAIL }, 141 { "rcpt", CMDRCPT }, 142 { "data", CMDDATA }, 143 { "rset", CMDRSET }, 144 { "vrfy", CMDVRFY }, 145 { "expn", CMDEXPN }, 146 { "help", CMDHELP }, 147 { "noop", CMDNOOP }, 148 { "quit", CMDQUIT }, 149 { "helo", CMDHELO }, 150 { "ehlo", CMDEHLO }, 151 { "etrn", CMDETRN }, 152 { "verb", CMDVERB }, 153 { "send", CMDUNIMPL }, 154 { "saml", CMDUNIMPL }, 155 { "soml", CMDUNIMPL }, 156 { "turn", CMDUNIMPL }, 157 #if SASL 158 { "auth", CMDAUTH, }, 159 #endif /* SASL */ 160 #if STARTTLS 161 { "starttls", CMDSTLS, }, 162 #endif /* STARTTLS */ 163 /* remaining commands are here only to trap and log attempts to use them */ 164 { "showq", CMDDBGQSHOW }, 165 { "debug", CMDDBGDEBUG }, 166 { "wiz", CMDLOGBOGUS }, 167 168 { NULL, CMDERROR } 169 }; 170 171 static char *CurSmtpClient; /* who's at the other end of channel */ 172 173 #ifndef MAXBADCOMMANDS 174 # define MAXBADCOMMANDS 25 /* maximum number of bad commands */ 175 #endif /* ! MAXBADCOMMANDS */ 176 #ifndef MAXNOOPCOMMANDS 177 # define MAXNOOPCOMMANDS 20 /* max "noise" commands before slowdown */ 178 #endif /* ! MAXNOOPCOMMANDS */ 179 #ifndef MAXHELOCOMMANDS 180 # define MAXHELOCOMMANDS 3 /* max HELO/EHLO commands before slowdown */ 181 #endif /* ! MAXHELOCOMMANDS */ 182 #ifndef MAXVRFYCOMMANDS 183 # define MAXVRFYCOMMANDS 6 /* max VRFY/EXPN commands before slowdown */ 184 #endif /* ! MAXVRFYCOMMANDS */ 185 #ifndef MAXETRNCOMMANDS 186 # define MAXETRNCOMMANDS 8 /* max ETRN commands before slowdown */ 187 #endif /* ! MAXETRNCOMMANDS */ 188 #ifndef MAXTIMEOUT 189 # define MAXTIMEOUT (4 * 60) /* max timeout for bad commands */ 190 #endif /* ! MAXTIMEOUT */ 191 192 #if SM_HEAP_CHECK 193 static SM_DEBUG_T DebugLeakSmtp = SM_DEBUG_INITIALIZER("leak_smtp", 194 "@(#)$Debug: leak_smtp - trace memory leaks during SMTP processing $"); 195 #endif /* SM_HEAP_CHECK */ 196 197 typedef struct 198 { 199 bool sm_gotmail; /* mail command received */ 200 unsigned int sm_nrcpts; /* number of successful RCPT commands */ 201 #if _FFR_ADAPTIVE_EOL 202 WARNING: do NOT use this FFR, it is most likely broken 203 bool sm_crlf; /* input in CRLF form? */ 204 #endif /* _FFR_ADAPTIVE_EOL */ 205 bool sm_discard; 206 #if MILTER 207 bool sm_milterize; 208 bool sm_milterlist; /* any filters in the list? */ 209 #endif /* MILTER */ 210 #if _FFR_QUARANTINE 211 char *sm_quarmsg; /* carry quarantining across messages */ 212 #endif /* _FFR_QUARANTINE */ 213 } SMTP_T; 214 215 static void smtp_data __P((SMTP_T *, ENVELOPE *)); 216 217 #define MSG_TEMPFAIL "451 4.7.1 Please try again later" 218 219 #if MILTER 220 # define MILTER_ABORT(e) milter_abort((e)) 221 # define MILTER_REPLY(str) \ 222 { \ 223 int savelogusrerrs = LogUsrErrs; \ 224 \ 225 switch (state) \ 226 { \ 227 case SMFIR_REPLYCODE: \ 228 if (MilterLogLevel > 3) \ 229 { \ 230 sm_syslog(LOG_INFO, e->e_id, \ 231 "Milter: %s=%s, reject=%s", \ 232 str, addr, response); \ 233 LogUsrErrs = false; \ 234 } \ 235 usrerr(response); \ 236 break; \ 237 \ 238 case SMFIR_REJECT: \ 239 if (MilterLogLevel > 3) \ 240 { \ 241 sm_syslog(LOG_INFO, e->e_id, \ 242 "Milter: %s=%s, reject=550 5.7.1 Command rejected", \ 243 str, addr); \ 244 LogUsrErrs = false; \ 245 } \ 246 usrerr("550 5.7.1 Command rejected"); \ 247 break; \ 248 \ 249 case SMFIR_DISCARD: \ 250 if (MilterLogLevel > 3) \ 251 sm_syslog(LOG_INFO, e->e_id, \ 252 "Milter: %s=%s, discard", \ 253 str, addr); \ 254 e->e_flags |= EF_DISCARD; \ 255 break; \ 256 \ 257 case SMFIR_TEMPFAIL: \ 258 if (MilterLogLevel > 3) \ 259 { \ 260 sm_syslog(LOG_INFO, e->e_id, \ 261 "Milter: %s=%s, reject=%s", \ 262 str, addr, MSG_TEMPFAIL); \ 263 LogUsrErrs = false; \ 264 } \ 265 usrerr(MSG_TEMPFAIL); \ 266 break; \ 267 } \ 268 LogUsrErrs = savelogusrerrs; \ 269 if (response != NULL) \ 270 sm_free(response); /* XXX */ \ 271 } 272 273 #else /* MILTER */ 274 # define MILTER_ABORT(e) 275 #endif /* MILTER */ 276 277 /* clear all SMTP state (for HELO/EHLO/RSET) */ 278 #define CLEAR_STATE(cmd) \ 279 { \ 280 /* abort milter filters */ \ 281 MILTER_ABORT(e); \ 282 \ 283 if (smtp.sm_nrcpts > 0) \ 284 { \ 285 logundelrcpts(e, cmd, 10, false); \ 286 smtp.sm_nrcpts = 0; \ 287 macdefine(&e->e_macro, A_PERM, \ 288 macid("{nrcpts}"), "0"); \ 289 } \ 290 \ 291 e->e_sendqueue = NULL; \ 292 e->e_flags |= EF_CLRQUEUE; \ 293 \ 294 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) \ 295 logsender(e, NULL); \ 296 e->e_flags &= ~EF_LOGSENDER; \ 297 \ 298 /* clean up a bit */ \ 299 smtp.sm_gotmail = false; \ 300 SuprErrs = true; \ 301 dropenvelope(e, true, false); \ 302 sm_rpool_free(e->e_rpool); \ 303 e = newenvelope(e, CurEnv, sm_rpool_new_x(NULL)); \ 304 CurEnv = e; \ 305 } 306 307 /* sleep to flatten out connection load */ 308 #define MIN_DELAY_LOG 15 /* wait before logging this again */ 309 310 /* is it worth setting the process title for 1s? */ 311 #define DELAY_CONN(cmd) \ 312 if (DelayLA > 0 && (CurrentLA = getla()) >= DelayLA) \ 313 { \ 314 time_t dnow; \ 315 \ 316 sm_setproctitle(true, e, \ 317 "%s: %s: delaying %s: load average: %d", \ 318 qid_printname(e), CurSmtpClient, \ 319 cmd, DelayLA); \ 320 if (LogLevel > 8 && (dnow = curtime()) > log_delay) \ 321 { \ 322 sm_syslog(LOG_INFO, e->e_id, \ 323 "delaying=%s, load average=%d >= %d", \ 324 cmd, CurrentLA, DelayLA); \ 325 log_delay = dnow + MIN_DELAY_LOG; \ 326 } \ 327 (void) sleep(1); \ 328 sm_setproctitle(true, e, "%s %s: %.80s", \ 329 qid_printname(e), CurSmtpClient, inp); \ 330 } 331 332 333 void 334 smtp(nullserver, d_flags, e) 335 char *volatile nullserver; 336 BITMAP256 d_flags; 337 register ENVELOPE *volatile e; 338 { 339 register char *volatile p; 340 register struct cmd *volatile c = NULL; 341 char *cmd; 342 auto ADDRESS *vrfyqueue; 343 ADDRESS *a; 344 volatile bool gothello; /* helo command received */ 345 bool vrfy; /* set if this is a vrfy command */ 346 char *volatile protocol; /* sending protocol */ 347 char *volatile sendinghost; /* sending hostname */ 348 char *volatile peerhostname; /* name of SMTP peer or "localhost" */ 349 auto char *delimptr; 350 char *id; 351 volatile unsigned int n_badcmds = 0; /* count of bad commands */ 352 volatile unsigned int n_badrcpts = 0; /* number of rejected RCPT */ 353 volatile unsigned int n_verifies = 0; /* count of VRFY/EXPN */ 354 volatile unsigned int n_etrn = 0; /* count of ETRN */ 355 volatile unsigned int n_noop = 0; /* count of NOOP/VERB/etc */ 356 volatile unsigned int n_helo = 0; /* count of HELO/EHLO */ 357 bool ok; 358 #if _FFR_ADAPTIVE_EOL 359 volatile bool first; 360 #endif /* _FFR_ADAPTIVE_EOL */ 361 volatile bool tempfail = false; 362 volatile time_t wt; /* timeout after too many commands */ 363 volatile time_t previous; /* time after checksmtpattack() */ 364 volatile bool lognullconnection = true; 365 register char *q; 366 SMTP_T smtp; 367 char *addr; 368 char *greetcode = "220"; 369 char *hostname; /* my hostname ($j) */ 370 QUEUE_CHAR *new; 371 int argno; 372 char *args[MAXSMTPARGS]; 373 char inp[MAXLINE]; 374 char cmdbuf[MAXLINE]; 375 #if SASL 376 sasl_conn_t *conn; 377 volatile bool sasl_ok; 378 volatile unsigned int n_auth = 0; /* count of AUTH commands */ 379 bool ismore; 380 int result; 381 volatile int authenticating; 382 char *user; 383 char *in, *out2; 384 # if SASL >= 20000 385 char *auth_id; 386 const char *out; 387 sasl_ssf_t ext_ssf; 388 # else /* SASL >= 20000 */ 389 char *out; 390 const char *errstr; 391 sasl_external_properties_t ext_ssf; 392 # endif /* SASL >= 20000 */ 393 sasl_security_properties_t ssp; 394 sasl_ssf_t *ssf; 395 unsigned int inlen, out2len; 396 unsigned int outlen; 397 char *volatile auth_type; 398 char *mechlist; 399 volatile unsigned int n_mechs; 400 unsigned int len; 401 #endif /* SASL */ 402 #if STARTTLS 403 int r; 404 int rfd, wfd; 405 volatile bool tls_active = false; 406 # if _FFR_SMTP_SSL 407 volatile bool smtps = false; 408 # endif /* _FFR_SMTP_SSL */ 409 bool saveQuickAbort; 410 bool saveSuprErrs; 411 time_t tlsstart; 412 #endif /* STARTTLS */ 413 volatile unsigned int features; 414 #if PIPELINING 415 # if _FFR_NO_PIPE 416 int np_log = 0; 417 # endif /* _FFR_NO_PIPE */ 418 #endif /* PIPELINING */ 419 volatile time_t log_delay = (time_t) 0; 420 421 smtp.sm_nrcpts = 0; 422 #if MILTER 423 smtp.sm_milterize = (nullserver == NULL); 424 smtp.sm_milterlist = false; 425 #endif /* MILTER */ 426 427 /* setup I/O fd correctly for the SMTP server */ 428 setup_smtpd_io(); 429 430 #if SM_HEAP_CHECK 431 if (sm_debug_active(&DebugLeakSmtp, 1)) 432 { 433 sm_heap_newgroup(); 434 sm_dprintf("smtp() heap group #%d\n", sm_heap_group()); 435 } 436 #endif /* SM_HEAP_CHECK */ 437 438 /* XXX the rpool should be set when e is initialized in main() */ 439 e->e_rpool = sm_rpool_new_x(NULL); 440 e->e_macro.mac_rpool = e->e_rpool; 441 442 settime(e); 443 sm_getla(); 444 peerhostname = RealHostName; 445 if (peerhostname == NULL) 446 peerhostname = "localhost"; 447 CurHostName = peerhostname; 448 CurSmtpClient = macvalue('_', e); 449 if (CurSmtpClient == NULL) 450 CurSmtpClient = CurHostName; 451 452 /* check_relay may have set discard bit, save for later */ 453 smtp.sm_discard = bitset(EF_DISCARD, e->e_flags); 454 455 #if PIPELINING 456 /* auto-flush output when reading input */ 457 (void) sm_io_autoflush(InChannel, OutChannel); 458 #endif /* PIPELINING */ 459 460 sm_setproctitle(true, e, "server %s startup", CurSmtpClient); 461 462 /* Set default features for server. */ 463 features = ((bitset(PRIV_NOETRN, PrivacyFlags) || 464 bitnset(D_NOETRN, d_flags)) ? SRV_NONE : SRV_OFFER_ETRN) 465 | (bitnset(D_AUTHREQ, d_flags) ? SRV_REQ_AUTH : SRV_NONE) 466 | (bitset(PRIV_NOEXPN, PrivacyFlags) ? SRV_NONE 467 : (SRV_OFFER_EXPN 468 | (bitset(PRIV_NOVERB, PrivacyFlags) 469 ? SRV_NONE : SRV_OFFER_VERB))) 470 | (bitset(PRIV_NORECEIPTS, PrivacyFlags) ? SRV_NONE 471 : SRV_OFFER_DSN) 472 #if SASL 473 | (bitnset(D_NOAUTH, d_flags) ? SRV_NONE : SRV_OFFER_AUTH) 474 #endif /* SASL */ 475 #if PIPELINING 476 | SRV_OFFER_PIPE 477 #endif /* PIPELINING */ 478 #if STARTTLS 479 | (bitnset(D_NOTLS, d_flags) ? SRV_NONE : SRV_OFFER_TLS) 480 | (bitset(TLS_I_NO_VRFY, TLS_Srv_Opts) ? SRV_NONE 481 : SRV_VRFY_CLT) 482 #endif /* STARTTLS */ 483 ; 484 if (nullserver == NULL) 485 { 486 features = srvfeatures(e, CurSmtpClient, features); 487 if (bitset(SRV_TMP_FAIL, features)) 488 { 489 if (LogLevel > 4) 490 sm_syslog(LOG_ERR, NOQID, 491 "ERROR: srv_features=tempfail, relay=%.100s, access temporarily disabled", 492 CurSmtpClient); 493 nullserver = "450 4.3.0 Please try again later."; 494 } 495 #if PIPELINING 496 # if _FFR_NO_PIPE 497 else if (bitset(SRV_NO_PIPE, features)) 498 { 499 /* for consistency */ 500 features &= ~SRV_OFFER_PIPE; 501 } 502 # endif /* _FFR_NO_PIPE */ 503 #endif /* PIPELINING */ 504 } 505 506 hostname = macvalue('j', e); 507 508 509 #if SASL 510 sasl_ok = bitset(SRV_OFFER_AUTH, features); 511 n_mechs = 0; 512 authenticating = SASL_NOT_AUTH; 513 514 /* SASL server new connection */ 515 if (sasl_ok) 516 { 517 # if SASL >= 20000 518 result = sasl_server_new("smtp", hostname, NULL, NULL, NULL, 519 NULL, 0, &conn); 520 # elif SASL > 10505 521 /* use empty realm: only works in SASL > 1.5.5 */ 522 result = sasl_server_new("smtp", hostname, "", NULL, 0, &conn); 523 # else /* SASL >= 20000 */ 524 /* use no realm -> realm is set to hostname by SASL lib */ 525 result = sasl_server_new("smtp", hostname, NULL, NULL, 0, 526 &conn); 527 # endif /* SASL >= 20000 */ 528 sasl_ok = result == SASL_OK; 529 if (!sasl_ok) 530 { 531 if (LogLevel > 9) 532 sm_syslog(LOG_WARNING, NOQID, 533 "AUTH error: sasl_server_new failed=%d", 534 result); 535 } 536 } 537 if (sasl_ok) 538 { 539 /* 540 ** SASL set properties for sasl 541 ** set local/remote IP 542 ** XXX Cyrus SASL v1 only supports IPv4 543 ** 544 ** XXX where exactly are these used/required? 545 ** Kerberos_v4 546 */ 547 548 # if SASL >= 20000 549 # if NETINET || NETINET6 550 in = macvalue(macid("{daemon_family}"), e); 551 if (in != NULL && ( 552 # if NETINET6 553 strcmp(in, "inet6") == 0 || 554 # endif /* NETINET6 */ 555 strcmp(in, "inet") == 0)) 556 { 557 SOCKADDR_LEN_T addrsize; 558 SOCKADDR saddr_l; 559 SOCKADDR saddr_r; 560 char localip[60], remoteip[60]; 561 562 addrsize = sizeof(saddr_r); 563 if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD, 564 NULL), 565 (struct sockaddr *) &saddr_r, 566 &addrsize) == 0) 567 { 568 if (iptostring(&saddr_r, addrsize, 569 remoteip, sizeof remoteip)) 570 { 571 sasl_setprop(conn, SASL_IPREMOTEPORT, 572 remoteip); 573 } 574 addrsize = sizeof(saddr_l); 575 if (getsockname(sm_io_getinfo(InChannel, 576 SM_IO_WHAT_FD, 577 NULL), 578 (struct sockaddr *) &saddr_l, 579 &addrsize) == 0) 580 { 581 if (iptostring(&saddr_l, addrsize, 582 localip, 583 sizeof localip)) 584 { 585 sasl_setprop(conn, 586 SASL_IPLOCALPORT, 587 localip); 588 } 589 } 590 } 591 } 592 # endif /* NETINET || NETINET6 */ 593 # else /* SASL >= 20000 */ 594 # if NETINET 595 in = macvalue(macid("{daemon_family}"), e); 596 if (in != NULL && strcmp(in, "inet") == 0) 597 { 598 SOCKADDR_LEN_T addrsize; 599 struct sockaddr_in saddr_l; 600 struct sockaddr_in saddr_r; 601 602 addrsize = sizeof(struct sockaddr_in); 603 if (getpeername(sm_io_getinfo(InChannel, SM_IO_WHAT_FD, 604 NULL), 605 (struct sockaddr *)&saddr_r, 606 &addrsize) == 0) 607 { 608 sasl_setprop(conn, SASL_IP_REMOTE, &saddr_r); 609 addrsize = sizeof(struct sockaddr_in); 610 if (getsockname(sm_io_getinfo(InChannel, 611 SM_IO_WHAT_FD, 612 NULL), 613 (struct sockaddr *)&saddr_l, 614 &addrsize) == 0) 615 sasl_setprop(conn, SASL_IP_LOCAL, 616 &saddr_l); 617 } 618 } 619 # endif /* NETINET */ 620 # endif /* SASL >= 20000 */ 621 622 auth_type = NULL; 623 mechlist = NULL; 624 user = NULL; 625 # if 0 626 macdefine(&BlankEnvelope.e_macro, A_PERM, 627 macid("{auth_author}"), NULL); 628 # endif /* 0 */ 629 630 /* set properties */ 631 (void) memset(&ssp, '\0', sizeof ssp); 632 633 /* XXX should these be options settable via .cf ? */ 634 /* ssp.min_ssf = 0; is default due to memset() */ 635 # if STARTTLS 636 # endif /* STARTTLS */ 637 { 638 ssp.max_ssf = MaxSLBits; 639 ssp.maxbufsize = MAXOUTLEN; 640 } 641 ssp.security_flags = SASLOpts & SASL_SEC_MASK; 642 sasl_ok = sasl_setprop(conn, SASL_SEC_PROPS, &ssp) == SASL_OK; 643 644 if (sasl_ok) 645 { 646 /* 647 ** external security strength factor; 648 ** currently we have none so zero 649 */ 650 651 # if SASL >= 20000 652 ext_ssf = 0; 653 auth_id = NULL; 654 sasl_ok = ((sasl_setprop(conn, SASL_SSF_EXTERNAL, 655 &ext_ssf) == SASL_OK) && 656 (sasl_setprop(conn, SASL_AUTH_EXTERNAL, 657 auth_id) == SASL_OK)); 658 # else /* SASL >= 20000 */ 659 ext_ssf.ssf = 0; 660 ext_ssf.auth_id = NULL; 661 sasl_ok = sasl_setprop(conn, SASL_SSF_EXTERNAL, 662 &ext_ssf) == SASL_OK; 663 # endif /* SASL >= 20000 */ 664 } 665 if (sasl_ok) 666 n_mechs = saslmechs(conn, &mechlist); 667 } 668 #endif /* SASL */ 669 670 #if MILTER 671 if (smtp.sm_milterize) 672 { 673 char state; 674 675 /* initialize mail filter connection */ 676 smtp.sm_milterlist = milter_init(e, &state); 677 switch (state) 678 { 679 case SMFIR_REJECT: 680 if (MilterLogLevel > 3) 681 sm_syslog(LOG_INFO, e->e_id, 682 "Milter: initialization failed, rejecting commands"); 683 greetcode = "554"; 684 nullserver = "Command rejected"; 685 smtp.sm_milterize = false; 686 break; 687 688 case SMFIR_TEMPFAIL: 689 if (MilterLogLevel > 3) 690 sm_syslog(LOG_INFO, e->e_id, 691 "Milter: initialization failed, temp failing commands"); 692 tempfail = true; 693 smtp.sm_milterize = false; 694 break; 695 } 696 } 697 698 if (smtp.sm_milterlist && smtp.sm_milterize && 699 !bitset(EF_DISCARD, e->e_flags)) 700 { 701 char state; 702 char *response; 703 704 response = milter_connect(peerhostname, RealHostAddr, 705 e, &state); 706 switch (state) 707 { 708 case SMFIR_REPLYCODE: /* REPLYCODE shouldn't happen */ 709 case SMFIR_REJECT: 710 if (MilterLogLevel > 3) 711 sm_syslog(LOG_INFO, e->e_id, 712 "Milter: connect: host=%s, addr=%s, rejecting commands", 713 peerhostname, 714 anynet_ntoa(&RealHostAddr)); 715 greetcode = "554"; 716 nullserver = "Command rejected"; 717 smtp.sm_milterize = false; 718 break; 719 720 case SMFIR_TEMPFAIL: 721 if (MilterLogLevel > 3) 722 sm_syslog(LOG_INFO, e->e_id, 723 "Milter: connect: host=%s, addr=%s, temp failing commands", 724 peerhostname, 725 anynet_ntoa(&RealHostAddr)); 726 tempfail = true; 727 smtp.sm_milterize = false; 728 break; 729 } 730 if (response != NULL) 731 732 sm_free(response); /* XXX */ 733 } 734 #endif /* MILTER */ 735 736 #if STARTTLS 737 # if _FFR_SMTP_SSL 738 /* If this an smtps connection, start TLS now */ 739 smtps = bitnset(D_SMTPS, d_flags); 740 if (smtps) 741 goto starttls; 742 743 greeting: 744 745 # endif /* _FFR_SMTP_SSL */ 746 #endif /* STARTTLS */ 747 748 /* output the first line, inserting "ESMTP" as second word */ 749 if (*greetcode == '5') 750 (void) sm_snprintf(inp, sizeof inp, "%s not accepting messages", 751 hostname); 752 else 753 expand(SmtpGreeting, inp, sizeof inp, e); 754 755 p = strchr(inp, '\n'); 756 if (p != NULL) 757 *p++ = '\0'; 758 id = strchr(inp, ' '); 759 if (id == NULL) 760 id = &inp[strlen(inp)]; 761 if (p == NULL) 762 (void) sm_snprintf(cmdbuf, sizeof cmdbuf, 763 "%s %%.*s ESMTP%%s", greetcode); 764 else 765 (void) sm_snprintf(cmdbuf, sizeof cmdbuf, 766 "%s-%%.*s ESMTP%%s", greetcode); 767 message(cmdbuf, (int) (id - inp), inp, id); 768 769 /* output remaining lines */ 770 while ((id = p) != NULL && (p = strchr(id, '\n')) != NULL) 771 { 772 *p++ = '\0'; 773 if (isascii(*id) && isspace(*id)) 774 id++; 775 (void) sm_strlcpyn(cmdbuf, sizeof cmdbuf, 2, greetcode, "-%s"); 776 message(cmdbuf, id); 777 } 778 if (id != NULL) 779 { 780 if (isascii(*id) && isspace(*id)) 781 id++; 782 (void) sm_strlcpyn(cmdbuf, sizeof cmdbuf, 2, greetcode, " %s"); 783 message(cmdbuf, id); 784 } 785 786 protocol = NULL; 787 sendinghost = macvalue('s', e); 788 789 #if _FFR_QUARANTINE 790 /* If quarantining by a connect/ehlo action, save between messages */ 791 if (e->e_quarmsg == NULL) 792 smtp.sm_quarmsg = NULL; 793 else 794 smtp.sm_quarmsg = newstr(e->e_quarmsg); 795 #endif /* _FFR_QUARANTINE */ 796 797 /* sendinghost's storage must outlive the current envelope */ 798 if (sendinghost != NULL) 799 sendinghost = sm_strdup_x(sendinghost); 800 #if _FFR_ADAPTIVE_EOL 801 first = true; 802 #endif /* _FFR_ADAPTIVE_EOL */ 803 gothello = false; 804 smtp.sm_gotmail = false; 805 for (;;) 806 { 807 SM_TRY 808 { 809 QuickAbort = false; 810 HoldErrs = false; 811 SuprErrs = false; 812 LogUsrErrs = false; 813 OnlyOneError = true; 814 e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS); 815 816 /* setup for the read */ 817 e->e_to = NULL; 818 Errors = 0; 819 FileName = NULL; 820 (void) sm_io_flush(smioout, SM_TIME_DEFAULT); 821 822 /* read the input line */ 823 SmtpPhase = "server cmd read"; 824 sm_setproctitle(true, e, "server %s cmd read", CurSmtpClient); 825 #if SASL 826 /* 827 ** XXX SMTP AUTH requires accepting any length, 828 ** at least for challenge/response 829 */ 830 #endif /* SASL */ 831 832 /* handle errors */ 833 if (sm_io_error(OutChannel) || 834 (p = sfgets(inp, sizeof inp, InChannel, 835 TimeOuts.to_nextcommand, SmtpPhase)) == NULL) 836 { 837 char *d; 838 839 d = macvalue(macid("{daemon_name}"), e); 840 if (d == NULL) 841 d = "stdin"; 842 /* end of file, just die */ 843 disconnect(1, e); 844 845 #if MILTER 846 /* close out milter filters */ 847 milter_quit(e); 848 #endif /* MILTER */ 849 850 message("421 4.4.1 %s Lost input channel from %s", 851 MyHostName, CurSmtpClient); 852 if (LogLevel > (smtp.sm_gotmail ? 1 : 19)) 853 sm_syslog(LOG_NOTICE, e->e_id, 854 "lost input channel from %.100s to %s after %s", 855 CurSmtpClient, d, 856 (c == NULL || c->cmd_name == NULL) ? "startup" : c->cmd_name); 857 /* 858 ** If have not accepted mail (DATA), do not bounce 859 ** bad addresses back to sender. 860 */ 861 862 if (bitset(EF_CLRQUEUE, e->e_flags)) 863 e->e_sendqueue = NULL; 864 goto doquit; 865 } 866 867 #if _FFR_ADAPTIVE_EOL 868 if (first) 869 { 870 char *p; 871 872 smtp.sm_crlf = true; 873 p = strchr(inp, '\n'); 874 if (p == NULL || p <= inp || p[-1] != '\r') 875 { 876 smtp.sm_crlf = false; 877 if (tTd(66, 1) && LogLevel > 8) 878 { 879 /* how many bad guys are there? */ 880 sm_syslog(LOG_INFO, NOQID, 881 "%.100s did not use CRLF", 882 CurSmtpClient); 883 } 884 } 885 first = false; 886 } 887 #endif /* _FFR_ADAPTIVE_EOL */ 888 889 /* clean up end of line */ 890 fixcrlf(inp, true); 891 892 #if PIPELINING 893 # if _FFR_NO_PIPE 894 /* 895 ** if there is more input and pipelining is disabled: 896 ** delay ... (and maybe discard the input?) 897 ** XXX this doesn't really work, at least in tests using 898 ** telnet SM_IO_IS_READABLE only returns 1 if there were 899 ** more than 2 input lines available. 900 */ 901 902 if (bitset(SRV_NO_PIPE, features) && 903 sm_io_getinfo(InChannel, SM_IO_IS_READABLE, NULL)) 904 { 905 if (++np_log < 3) 906 sm_syslog(LOG_INFO, NOQID, 907 "unauthorized PIPELINING, sleeping"); 908 sleep(1); 909 } 910 911 # endif /* _FFR_NO_PIPE */ 912 #endif /* PIPELINING */ 913 914 #if SASL 915 if (authenticating == SASL_PROC_AUTH) 916 { 917 # if 0 918 if (*inp == '\0') 919 { 920 authenticating = SASL_NOT_AUTH; 921 message("501 5.5.2 missing input"); 922 continue; 923 } 924 # endif /* 0 */ 925 if (*inp == '*' && *(inp + 1) == '\0') 926 { 927 authenticating = SASL_NOT_AUTH; 928 929 /* rfc 2254 4. */ 930 message("501 5.0.0 AUTH aborted"); 931 continue; 932 } 933 934 /* could this be shorter? XXX */ 935 # if SASL >= 20000 936 in = xalloc(strlen(inp) + 1); 937 result = sasl_decode64(inp, strlen(inp), in, 938 strlen(inp), &inlen); 939 # else /* SASL >= 20000 */ 940 out = xalloc(strlen(inp)); 941 result = sasl_decode64(inp, strlen(inp), out, &outlen); 942 # endif /* SASL >= 20000 */ 943 if (result != SASL_OK) 944 { 945 authenticating = SASL_NOT_AUTH; 946 947 /* rfc 2254 4. */ 948 message("501 5.5.4 cannot decode AUTH parameter %s", 949 inp); 950 # if SASL >= 20000 951 sm_free(in); 952 # endif /* SASL >= 20000 */ 953 continue; 954 } 955 956 # if SASL >= 20000 957 result = sasl_server_step(conn, in, inlen, 958 &out, &outlen); 959 sm_free(in); 960 # else /* SASL >= 20000 */ 961 result = sasl_server_step(conn, out, outlen, 962 &out, &outlen, &errstr); 963 # endif /* SASL >= 20000 */ 964 965 /* get an OK if we're done */ 966 if (result == SASL_OK) 967 { 968 authenticated: 969 message("235 2.0.0 OK Authenticated"); 970 authenticating = SASL_IS_AUTH; 971 macdefine(&BlankEnvelope.e_macro, A_TEMP, 972 macid("{auth_type}"), auth_type); 973 974 # if SASL >= 20000 975 user = macvalue(macid("{auth_authen}"), e); 976 977 /* get security strength (features) */ 978 result = sasl_getprop(conn, SASL_SSF, 979 (const void **) &ssf); 980 # else /* SASL >= 20000 */ 981 result = sasl_getprop(conn, SASL_USERNAME, 982 (void **)&user); 983 if (result != SASL_OK) 984 { 985 user = ""; 986 macdefine(&BlankEnvelope.e_macro, 987 A_PERM, 988 macid("{auth_authen}"), NULL); 989 } 990 else 991 { 992 macdefine(&BlankEnvelope.e_macro, 993 A_TEMP, 994 macid("{auth_authen}"), user); 995 } 996 997 # if 0 998 /* get realm? */ 999 sasl_getprop(conn, SASL_REALM, (void **) &data); 1000 # endif /* 0 */ 1001 1002 /* get security strength (features) */ 1003 result = sasl_getprop(conn, SASL_SSF, 1004 (void **) &ssf); 1005 # endif /* SASL >= 20000 */ 1006 if (result != SASL_OK) 1007 { 1008 macdefine(&BlankEnvelope.e_macro, 1009 A_PERM, 1010 macid("{auth_ssf}"), "0"); 1011 ssf = NULL; 1012 } 1013 else 1014 { 1015 char pbuf[8]; 1016 1017 (void) sm_snprintf(pbuf, sizeof pbuf, 1018 "%u", *ssf); 1019 macdefine(&BlankEnvelope.e_macro, 1020 A_TEMP, 1021 macid("{auth_ssf}"), pbuf); 1022 if (tTd(95, 8)) 1023 sm_dprintf("AUTH auth_ssf: %u\n", 1024 *ssf); 1025 } 1026 1027 /* 1028 ** Only switch to encrypted connection 1029 ** if a security layer has been negotiated 1030 */ 1031 1032 if (ssf != NULL && *ssf > 0) 1033 { 1034 /* 1035 ** Convert I/O layer to use SASL. 1036 ** If the call fails, the connection 1037 ** is aborted. 1038 */ 1039 1040 if (sfdcsasl(&InChannel, &OutChannel, 1041 conn) == 0) 1042 { 1043 /* restart dialogue */ 1044 n_helo = 0; 1045 # if PIPELINING 1046 (void) sm_io_autoflush(InChannel, 1047 OutChannel); 1048 # endif /* PIPELINING */ 1049 } 1050 else 1051 syserr("503 5.3.3 SASL TLS failed"); 1052 } 1053 1054 /* NULL pointer ok since it's our function */ 1055 if (LogLevel > 8) 1056 sm_syslog(LOG_INFO, NOQID, 1057 "AUTH=server, relay=%.100s, authid=%.128s, mech=%.16s, bits=%d", 1058 CurSmtpClient, 1059 shortenstring(user, 128), 1060 auth_type, *ssf); 1061 } 1062 else if (result == SASL_CONTINUE) 1063 { 1064 len = ENC64LEN(outlen); 1065 out2 = xalloc(len); 1066 result = sasl_encode64(out, outlen, out2, len, 1067 &out2len); 1068 if (result != SASL_OK) 1069 { 1070 /* correct code? XXX */ 1071 /* 454 Temp. authentication failure */ 1072 message("454 4.5.4 Internal error: unable to encode64"); 1073 if (LogLevel > 5) 1074 sm_syslog(LOG_WARNING, e->e_id, 1075 "AUTH encode64 error [%d for \"%s\"]", 1076 result, out); 1077 /* start over? */ 1078 authenticating = SASL_NOT_AUTH; 1079 } 1080 else 1081 { 1082 message("334 %s", out2); 1083 if (tTd(95, 2)) 1084 sm_dprintf("AUTH continue: msg='%s' len=%u\n", 1085 out2, out2len); 1086 } 1087 # if SASL >= 20000 1088 sm_free(out2); 1089 # endif /* SASL >= 20000 */ 1090 } 1091 else 1092 { 1093 /* not SASL_OK or SASL_CONT */ 1094 message("535 5.7.0 authentication failed"); 1095 if (LogLevel > 9) 1096 sm_syslog(LOG_WARNING, e->e_id, 1097 "AUTH failure (%s): %s (%d) %s", 1098 auth_type, 1099 sasl_errstring(result, NULL, 1100 NULL), 1101 result, 1102 # if SASL >= 20000 1103 sasl_errdetail(conn)); 1104 # else /* SASL >= 20000 */ 1105 errstr == NULL ? "" : errstr); 1106 # endif /* SASL >= 20000 */ 1107 authenticating = SASL_NOT_AUTH; 1108 } 1109 } 1110 else 1111 { 1112 /* don't want to do any of this if authenticating */ 1113 #endif /* SASL */ 1114 1115 /* echo command to transcript */ 1116 if (e->e_xfp != NULL) 1117 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 1118 "<<< %s\n", inp); 1119 1120 if (LogLevel > 14) 1121 sm_syslog(LOG_INFO, e->e_id, "<-- %s", inp); 1122 1123 /* break off command */ 1124 for (p = inp; isascii(*p) && isspace(*p); p++) 1125 continue; 1126 cmd = cmdbuf; 1127 while (*p != '\0' && 1128 !(isascii(*p) && isspace(*p)) && 1129 cmd < &cmdbuf[sizeof cmdbuf - 2]) 1130 *cmd++ = *p++; 1131 *cmd = '\0'; 1132 1133 /* throw away leading whitespace */ 1134 SKIP_SPACE(p); 1135 1136 /* decode command */ 1137 for (c = CmdTab; c->cmd_name != NULL; c++) 1138 { 1139 if (sm_strcasecmp(c->cmd_name, cmdbuf) == 0) 1140 break; 1141 } 1142 1143 /* reset errors */ 1144 errno = 0; 1145 1146 /* check whether a "non-null" command has been used */ 1147 switch (c->cmd_code) 1148 { 1149 #if SASL 1150 case CMDAUTH: 1151 /* avoid information leak; take first two words? */ 1152 q = "AUTH"; 1153 break; 1154 #endif /* SASL */ 1155 1156 case CMDMAIL: 1157 case CMDEXPN: 1158 case CMDVRFY: 1159 case CMDETRN: 1160 lognullconnection = false; 1161 /* FALLTHROUGH */ 1162 default: 1163 q = inp; 1164 break; 1165 } 1166 1167 if (e->e_id == NULL) 1168 sm_setproctitle(true, e, "%s: %.80s", 1169 CurSmtpClient, q); 1170 else 1171 sm_setproctitle(true, e, "%s %s: %.80s", 1172 qid_printname(e), 1173 CurSmtpClient, q); 1174 1175 /* 1176 ** Process command. 1177 ** 1178 ** If we are running as a null server, return 550 1179 ** to almost everything. 1180 */ 1181 1182 if (nullserver != NULL || bitnset(D_ETRNONLY, d_flags)) 1183 { 1184 switch (c->cmd_code) 1185 { 1186 case CMDQUIT: 1187 case CMDHELO: 1188 case CMDEHLO: 1189 case CMDNOOP: 1190 case CMDRSET: 1191 /* process normally */ 1192 break; 1193 1194 case CMDETRN: 1195 if (bitnset(D_ETRNONLY, d_flags) && 1196 nullserver == NULL) 1197 break; 1198 DELAY_CONN("ETRN"); 1199 /* FALLTHROUGH */ 1200 1201 default: 1202 #if MAXBADCOMMANDS > 0 1203 /* theoretically this could overflow */ 1204 if (nullserver != NULL && 1205 ++n_badcmds > MAXBADCOMMANDS) 1206 { 1207 message("421 4.7.0 %s Too many bad commands; closing connection", 1208 MyHostName); 1209 1210 /* arrange to ignore send list */ 1211 e->e_sendqueue = NULL; 1212 goto doquit; 1213 } 1214 #endif /* MAXBADCOMMANDS > 0 */ 1215 if (nullserver != NULL) 1216 { 1217 if (ISSMTPREPLY(nullserver)) 1218 usrerr(nullserver); 1219 else 1220 usrerr("550 5.0.0 %s", 1221 nullserver); 1222 } 1223 else 1224 usrerr("452 4.4.5 Insufficient disk space; try again later"); 1225 continue; 1226 } 1227 } 1228 1229 switch (c->cmd_code) 1230 { 1231 #if SASL 1232 case CMDAUTH: /* sasl */ 1233 DELAY_CONN("AUTH"); 1234 if (!sasl_ok || n_mechs <= 0) 1235 { 1236 message("503 5.3.3 AUTH not available"); 1237 break; 1238 } 1239 if (authenticating == SASL_IS_AUTH) 1240 { 1241 message("503 5.5.0 Already Authenticated"); 1242 break; 1243 } 1244 if (smtp.sm_gotmail) 1245 { 1246 message("503 5.5.0 AUTH not permitted during a mail transaction"); 1247 break; 1248 } 1249 if (tempfail) 1250 { 1251 if (LogLevel > 9) 1252 sm_syslog(LOG_INFO, e->e_id, 1253 "SMTP AUTH command (%.100s) from %.100s tempfailed (due to previous checks)", 1254 p, CurSmtpClient); 1255 usrerr("454 4.7.1 Please try again later"); 1256 break; 1257 } 1258 1259 ismore = false; 1260 1261 /* crude way to avoid crack attempts */ 1262 (void) checksmtpattack(&n_auth, n_mechs + 1, true, 1263 "AUTH", e); 1264 1265 /* make sure mechanism (p) is a valid string */ 1266 for (q = p; *q != '\0' && isascii(*q); q++) 1267 { 1268 if (isspace(*q)) 1269 { 1270 *q = '\0'; 1271 while (*++q != '\0' && 1272 isascii(*q) && isspace(*q)) 1273 continue; 1274 *(q - 1) = '\0'; 1275 ismore = (*q != '\0'); 1276 break; 1277 } 1278 } 1279 1280 if (*p == '\0') 1281 { 1282 message("501 5.5.2 AUTH mechanism must be specified"); 1283 break; 1284 } 1285 1286 /* check whether mechanism is available */ 1287 if (iteminlist(p, mechlist, " ") == NULL) 1288 { 1289 message("504 5.3.3 AUTH mechanism %.32s not available", 1290 p); 1291 break; 1292 } 1293 1294 if (ismore) 1295 { 1296 /* could this be shorter? XXX */ 1297 # if SASL >= 20000 1298 in = xalloc(strlen(q) + 1); 1299 result = sasl_decode64(q, strlen(q), in, 1300 strlen(q), &inlen); 1301 # else /* SASL >= 20000 */ 1302 in = sm_rpool_malloc(e->e_rpool, strlen(q)); 1303 result = sasl_decode64(q, strlen(q), in, 1304 &inlen); 1305 # endif /* SASL >= 20000 */ 1306 if (result != SASL_OK) 1307 { 1308 message("501 5.5.4 cannot BASE64 decode '%s'", 1309 q); 1310 if (LogLevel > 5) 1311 sm_syslog(LOG_WARNING, e->e_id, 1312 "AUTH decode64 error [%d for \"%s\"]", 1313 result, q); 1314 /* start over? */ 1315 authenticating = SASL_NOT_AUTH; 1316 # if SASL >= 20000 1317 sm_free(in); 1318 # endif /* SASL >= 20000 */ 1319 in = NULL; 1320 inlen = 0; 1321 break; 1322 } 1323 } 1324 else 1325 { 1326 in = NULL; 1327 inlen = 0; 1328 } 1329 1330 /* see if that auth type exists */ 1331 # if SASL >= 20000 1332 result = sasl_server_start(conn, p, in, inlen, 1333 &out, &outlen); 1334 if (in != NULL) 1335 sm_free(in); 1336 # else /* SASL >= 20000 */ 1337 result = sasl_server_start(conn, p, in, inlen, 1338 &out, &outlen, &errstr); 1339 # endif /* SASL >= 20000 */ 1340 1341 if (result != SASL_OK && result != SASL_CONTINUE) 1342 { 1343 message("535 5.7.0 authentication failed"); 1344 if (LogLevel > 9) 1345 sm_syslog(LOG_ERR, e->e_id, 1346 "AUTH failure (%s): %s (%d) %s", 1347 p, 1348 sasl_errstring(result, NULL, 1349 NULL), 1350 result, 1351 # if SASL >= 20000 1352 sasl_errdetail(conn)); 1353 # else /* SASL >= 20000 */ 1354 errstr); 1355 # endif /* SASL >= 20000 */ 1356 break; 1357 } 1358 auth_type = newstr(p); 1359 1360 if (result == SASL_OK) 1361 { 1362 /* ugly, but same code */ 1363 goto authenticated; 1364 /* authenticated by the initial response */ 1365 } 1366 1367 /* len is at least 2 */ 1368 len = ENC64LEN(outlen); 1369 out2 = xalloc(len); 1370 result = sasl_encode64(out, outlen, out2, len, 1371 &out2len); 1372 1373 if (result != SASL_OK) 1374 { 1375 message("454 4.5.4 Temporary authentication failure"); 1376 if (LogLevel > 5) 1377 sm_syslog(LOG_WARNING, e->e_id, 1378 "AUTH encode64 error [%d for \"%s\"]", 1379 result, out); 1380 1381 /* start over? */ 1382 authenticating = SASL_NOT_AUTH; 1383 } 1384 else 1385 { 1386 message("334 %s", out2); 1387 authenticating = SASL_PROC_AUTH; 1388 } 1389 # if SASL >= 20000 1390 sm_free(out2); 1391 # endif /* SASL >= 20000 */ 1392 break; 1393 #endif /* SASL */ 1394 1395 #if STARTTLS 1396 case CMDSTLS: /* starttls */ 1397 DELAY_CONN("STARTTLS"); 1398 if (*p != '\0') 1399 { 1400 message("501 5.5.2 Syntax error (no parameters allowed)"); 1401 break; 1402 } 1403 if (!bitset(SRV_OFFER_TLS, features)) 1404 { 1405 message("503 5.5.0 TLS not available"); 1406 break; 1407 } 1408 if (!tls_ok_srv) 1409 { 1410 message("454 4.3.3 TLS not available after start"); 1411 break; 1412 } 1413 if (smtp.sm_gotmail) 1414 { 1415 message("503 5.5.0 TLS not permitted during a mail transaction"); 1416 break; 1417 } 1418 if (tempfail) 1419 { 1420 if (LogLevel > 9) 1421 sm_syslog(LOG_INFO, e->e_id, 1422 "SMTP STARTTLS command (%.100s) from %.100s tempfailed (due to previous checks)", 1423 p, CurSmtpClient); 1424 usrerr("454 4.7.1 Please try again later"); 1425 break; 1426 } 1427 # if _FFR_SMTP_SSL 1428 starttls: 1429 # endif /* _FFR_SMTP_SSL */ 1430 # if TLS_NO_RSA 1431 /* 1432 ** XXX do we need a temp key ? 1433 */ 1434 # else /* TLS_NO_RSA */ 1435 # endif /* TLS_NO_RSA */ 1436 1437 # if TLS_VRFY_PER_CTX 1438 /* 1439 ** Note: this sets the verification globally 1440 ** (per SSL_CTX) 1441 ** it's ok since it applies only to one transaction 1442 */ 1443 1444 TLS_VERIFY_CLIENT(); 1445 # endif /* TLS_VRFY_PER_CTX */ 1446 1447 if (srv_ssl != NULL) 1448 SSL_clear(srv_ssl); 1449 else if ((srv_ssl = SSL_new(srv_ctx)) == NULL) 1450 { 1451 message("454 4.3.3 TLS not available: error generating SSL handle"); 1452 # if _FFR_SMTP_SSL 1453 goto tls_done; 1454 # else /* _FFR_SMTP_SSL */ 1455 break; 1456 # endif /* _FFR_SMTP_SSL */ 1457 } 1458 1459 # if !TLS_VRFY_PER_CTX 1460 /* 1461 ** this could be used if it were possible to set 1462 ** verification per SSL (connection) 1463 ** not just per SSL_CTX (global) 1464 */ 1465 1466 TLS_VERIFY_CLIENT(); 1467 # endif /* !TLS_VRFY_PER_CTX */ 1468 1469 rfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL); 1470 wfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL); 1471 1472 if (rfd < 0 || wfd < 0 || 1473 SSL_set_rfd(srv_ssl, rfd) <= 0 || 1474 SSL_set_wfd(srv_ssl, wfd) <= 0) 1475 { 1476 message("454 4.3.3 TLS not available: error set fd"); 1477 SSL_free(srv_ssl); 1478 srv_ssl = NULL; 1479 # if _FFR_SMTP_SSL 1480 goto tls_done; 1481 # else /* _FFR_SMTP_SSL */ 1482 break; 1483 # endif /* _FFR_SMTP_SSL */ 1484 } 1485 # if _FFR_SMTP_SSL 1486 if (!smtps) 1487 # endif /* _FFR_SMTP_SSL */ 1488 message("220 2.0.0 Ready to start TLS"); 1489 # if PIPELINING 1490 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT); 1491 # endif /* PIPELINING */ 1492 1493 SSL_set_accept_state(srv_ssl); 1494 1495 # define SSL_ACC(s) SSL_accept(s) 1496 1497 tlsstart = curtime(); 1498 ssl_retry: 1499 if ((r = SSL_ACC(srv_ssl)) <= 0) 1500 { 1501 int i; 1502 bool timedout; 1503 time_t left; 1504 time_t now = curtime(); 1505 struct timeval tv; 1506 1507 /* what to do in this case? */ 1508 i = SSL_get_error(srv_ssl, r); 1509 1510 /* 1511 ** For SSL_ERROR_WANT_{READ,WRITE}: 1512 ** There is no SSL record available yet 1513 ** or there is only a partial SSL record 1514 ** removed from the network (socket) buffer 1515 ** into the SSL buffer. The SSL_accept will 1516 ** only succeed when a full SSL record is 1517 ** available (assuming a "real" error 1518 ** doesn't happen). To handle when a "real" 1519 ** error does happen the select is set for 1520 ** exceptions too. 1521 ** The connection may be re-negotiated 1522 ** during this time so both read and write 1523 ** "want errors" need to be handled. 1524 ** A select() exception loops back so that 1525 ** a proper SSL error message can be gotten. 1526 */ 1527 1528 left = TimeOuts.to_starttls - (now - tlsstart); 1529 timedout = left <= 0; 1530 if (!timedout) 1531 { 1532 tv.tv_sec = left; 1533 tv.tv_usec = 0; 1534 } 1535 1536 /* XXX what about SSL_pending() ? */ 1537 if (!timedout && i == SSL_ERROR_WANT_READ) 1538 { 1539 fd_set ssl_maskr, ssl_maskx; 1540 1541 FD_ZERO(&ssl_maskr); 1542 FD_SET(rfd, &ssl_maskr); 1543 FD_ZERO(&ssl_maskx); 1544 FD_SET(rfd, &ssl_maskx); 1545 if (select(rfd + 1, &ssl_maskr, NULL, 1546 &ssl_maskx, &tv) > 0) 1547 goto ssl_retry; 1548 } 1549 if (!timedout && i == SSL_ERROR_WANT_WRITE) 1550 { 1551 fd_set ssl_maskw, ssl_maskx; 1552 1553 FD_ZERO(&ssl_maskw); 1554 FD_SET(wfd, &ssl_maskw); 1555 FD_ZERO(&ssl_maskx); 1556 FD_SET(rfd, &ssl_maskx); 1557 if (select(wfd + 1, NULL, &ssl_maskw, 1558 &ssl_maskx, &tv) > 0) 1559 goto ssl_retry; 1560 } 1561 if (LogLevel > 5) 1562 { 1563 sm_syslog(LOG_WARNING, NOQID, 1564 "STARTTLS=server, error: accept failed=%d, SSL_error=%d, timedout=%d", 1565 r, i, (int) timedout); 1566 if (LogLevel > 8) 1567 tlslogerr("server"); 1568 } 1569 tls_ok_srv = false; 1570 SSL_free(srv_ssl); 1571 srv_ssl = NULL; 1572 1573 /* 1574 ** according to the next draft of 1575 ** RFC 2487 the connection should be dropped 1576 */ 1577 1578 /* arrange to ignore any current send list */ 1579 e->e_sendqueue = NULL; 1580 goto doquit; 1581 } 1582 1583 /* ignore return code for now, it's in {verify} */ 1584 (void) tls_get_info(srv_ssl, true, 1585 CurSmtpClient, 1586 &BlankEnvelope.e_macro, 1587 bitset(SRV_VRFY_CLT, features)); 1588 1589 /* 1590 ** call Stls_client to find out whether 1591 ** to accept the connection from the client 1592 */ 1593 1594 saveQuickAbort = QuickAbort; 1595 saveSuprErrs = SuprErrs; 1596 SuprErrs = true; 1597 QuickAbort = false; 1598 if (rscheck("tls_client", 1599 macvalue(macid("{verify}"), e), 1600 "STARTTLS", e, true, true, 5, 1601 NULL, NOQID) != EX_OK || 1602 Errors > 0) 1603 { 1604 extern char MsgBuf[]; 1605 1606 if (MsgBuf[0] != '\0' && ISSMTPREPLY(MsgBuf)) 1607 nullserver = newstr(MsgBuf); 1608 else 1609 nullserver = "503 5.7.0 Authentication required."; 1610 } 1611 QuickAbort = saveQuickAbort; 1612 SuprErrs = saveSuprErrs; 1613 1614 tls_ok_srv = false; /* don't offer STARTTLS again */ 1615 n_helo = 0; 1616 # if SASL 1617 if (sasl_ok) 1618 { 1619 char *s; 1620 1621 s = macvalue(macid("{cipher_bits}"), e); 1622 # if SASL >= 20000 1623 if (s != NULL && (ext_ssf = atoi(s)) > 0) 1624 { 1625 auth_id = macvalue(macid("{cert_subject}"), 1626 e); 1627 sasl_ok = ((sasl_setprop(conn, SASL_SSF_EXTERNAL, 1628 &ext_ssf) == SASL_OK) && 1629 (sasl_setprop(conn, SASL_AUTH_EXTERNAL, 1630 auth_id) == SASL_OK)); 1631 # else /* SASL >= 20000 */ 1632 if (s != NULL && (ext_ssf.ssf = atoi(s)) > 0) 1633 { 1634 ext_ssf.auth_id = macvalue(macid("{cert_subject}"), 1635 e); 1636 sasl_ok = sasl_setprop(conn, SASL_SSF_EXTERNAL, 1637 &ext_ssf) == SASL_OK; 1638 # endif /* SASL >= 20000 */ 1639 mechlist = NULL; 1640 if (sasl_ok) 1641 n_mechs = saslmechs(conn, 1642 &mechlist); 1643 } 1644 } 1645 # endif /* SASL */ 1646 1647 /* switch to secure connection */ 1648 if (sfdctls(&InChannel, &OutChannel, srv_ssl) == 0) 1649 { 1650 tls_active = true; 1651 # if PIPELINING 1652 (void) sm_io_autoflush(InChannel, OutChannel); 1653 # endif /* PIPELINING */ 1654 } 1655 else 1656 { 1657 /* 1658 ** XXX this is an internal error 1659 ** how to deal with it? 1660 ** we can't generate an error message 1661 ** since the other side switched to an 1662 ** encrypted layer, but we could not... 1663 ** just "hang up"? 1664 */ 1665 1666 nullserver = "454 4.3.3 TLS not available: can't switch to encrypted layer"; 1667 syserr("STARTTLS: can't switch to encrypted layer"); 1668 } 1669 # if _FFR_SMTP_SSL 1670 tls_done: 1671 if (smtps) 1672 { 1673 if (tls_active) 1674 goto greeting; 1675 else 1676 goto doquit; 1677 } 1678 # endif /* _FFR_SMTP_SSL */ 1679 break; 1680 #endif /* STARTTLS */ 1681 1682 case CMDHELO: /* hello -- introduce yourself */ 1683 case CMDEHLO: /* extended hello */ 1684 DELAY_CONN("EHLO"); 1685 if (c->cmd_code == CMDEHLO) 1686 { 1687 protocol = "ESMTP"; 1688 SmtpPhase = "server EHLO"; 1689 } 1690 else 1691 { 1692 protocol = "SMTP"; 1693 SmtpPhase = "server HELO"; 1694 } 1695 1696 /* avoid denial-of-service */ 1697 (void) checksmtpattack(&n_helo, MAXHELOCOMMANDS, true, 1698 "HELO/EHLO", e); 1699 1700 #if 0 1701 /* RFC2821 4.1.4 allows duplicate HELO/EHLO */ 1702 /* check for duplicate HELO/EHLO per RFC 1651 4.2 */ 1703 if (gothello) 1704 { 1705 usrerr("503 %s Duplicate HELO/EHLO", 1706 MyHostName); 1707 break; 1708 } 1709 #endif /* 0 */ 1710 1711 /* check for valid domain name (re 1123 5.2.5) */ 1712 if (*p == '\0' && !AllowBogusHELO) 1713 { 1714 usrerr("501 %s requires domain address", 1715 cmdbuf); 1716 break; 1717 } 1718 1719 /* check for long domain name (hides Received: info) */ 1720 if (strlen(p) > MAXNAME) 1721 { 1722 usrerr("501 Invalid domain name"); 1723 if (LogLevel > 9) 1724 sm_syslog(LOG_INFO, CurEnv->e_id, 1725 "invalid domain name (too long) from %.100s", 1726 CurSmtpClient); 1727 break; 1728 } 1729 1730 ok = true; 1731 for (q = p; *q != '\0'; q++) 1732 { 1733 if (!isascii(*q)) 1734 break; 1735 if (isalnum(*q)) 1736 continue; 1737 if (isspace(*q)) 1738 { 1739 *q = '\0'; 1740 1741 /* only complain if strict check */ 1742 ok = AllowBogusHELO; 1743 break; 1744 } 1745 if (strchr("[].-_#", *q) == NULL) 1746 break; 1747 } 1748 1749 if (*q == '\0' && ok) 1750 { 1751 q = "pleased to meet you"; 1752 sendinghost = sm_strdup_x(p); 1753 } 1754 else if (!AllowBogusHELO) 1755 { 1756 usrerr("501 Invalid domain name"); 1757 if (LogLevel > 9) 1758 sm_syslog(LOG_INFO, CurEnv->e_id, 1759 "invalid domain name (%.100s) from %.100s", 1760 p, CurSmtpClient); 1761 break; 1762 } 1763 else 1764 { 1765 q = "accepting invalid domain name"; 1766 } 1767 1768 if (gothello) 1769 { 1770 CLEAR_STATE(cmdbuf); 1771 1772 #if _FFR_QUARANTINE 1773 /* restore connection quarantining */ 1774 if (smtp.sm_quarmsg == NULL) 1775 { 1776 e->e_quarmsg = NULL; 1777 macdefine(&e->e_macro, A_PERM, 1778 macid("{quarantine}"), ""); 1779 } 1780 else 1781 { 1782 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, 1783 smtp.sm_quarmsg); 1784 macdefine(&e->e_macro, A_PERM, 1785 macid("{quarantine}"), 1786 e->e_quarmsg); 1787 } 1788 #endif /* _FFR_QUARANTINE */ 1789 } 1790 1791 #if MILTER 1792 if (smtp.sm_milterlist && smtp.sm_milterize && 1793 !bitset(EF_DISCARD, e->e_flags)) 1794 { 1795 char state; 1796 char *response; 1797 1798 response = milter_helo(p, e, &state); 1799 switch (state) 1800 { 1801 case SMFIR_REPLYCODE: 1802 if (MilterLogLevel > 3) 1803 sm_syslog(LOG_INFO, e->e_id, 1804 "Milter: helo=%s, reject=%s", 1805 p, response); 1806 nullserver = newstr(response); 1807 smtp.sm_milterize = false; 1808 break; 1809 1810 case SMFIR_REJECT: 1811 if (MilterLogLevel > 3) 1812 sm_syslog(LOG_INFO, e->e_id, 1813 "Milter: helo=%s, reject=Command rejected", 1814 p); 1815 nullserver = "Command rejected"; 1816 smtp.sm_milterize = false; 1817 break; 1818 1819 case SMFIR_TEMPFAIL: 1820 if (MilterLogLevel > 3) 1821 sm_syslog(LOG_INFO, e->e_id, 1822 "Milter: helo=%s, reject=%s", 1823 p, MSG_TEMPFAIL); 1824 tempfail = true; 1825 smtp.sm_milterize = false; 1826 break; 1827 } 1828 if (response != NULL) 1829 sm_free(response); 1830 1831 # if _FFR_QUARANTINE 1832 /* 1833 ** If quarantining by a connect/ehlo action, 1834 ** save between messages 1835 */ 1836 1837 if (smtp.sm_quarmsg == NULL && 1838 e->e_quarmsg != NULL) 1839 smtp.sm_quarmsg = newstr(e->e_quarmsg); 1840 # endif /* _FFR_QUARANTINE */ 1841 } 1842 #endif /* MILTER */ 1843 gothello = true; 1844 1845 /* print HELO response message */ 1846 if (c->cmd_code != CMDEHLO) 1847 { 1848 message("250 %s Hello %s, %s", 1849 MyHostName, CurSmtpClient, q); 1850 break; 1851 } 1852 1853 message("250-%s Hello %s, %s", 1854 MyHostName, CurSmtpClient, q); 1855 1856 /* offer ENHSC even for nullserver */ 1857 if (nullserver != NULL) 1858 { 1859 message("250 ENHANCEDSTATUSCODES"); 1860 break; 1861 } 1862 1863 /* 1864 ** print EHLO features list 1865 ** 1866 ** Note: If you change this list, 1867 ** remember to update 'helpfile' 1868 */ 1869 1870 message("250-ENHANCEDSTATUSCODES"); 1871 #if PIPELINING 1872 if (bitset(SRV_OFFER_PIPE, features)) 1873 message("250-PIPELINING"); 1874 #endif /* PIPELINING */ 1875 if (bitset(SRV_OFFER_EXPN, features)) 1876 { 1877 message("250-EXPN"); 1878 if (bitset(SRV_OFFER_VERB, features)) 1879 message("250-VERB"); 1880 } 1881 #if MIME8TO7 1882 message("250-8BITMIME"); 1883 #endif /* MIME8TO7 */ 1884 if (MaxMessageSize > 0) 1885 message("250-SIZE %ld", MaxMessageSize); 1886 else 1887 message("250-SIZE"); 1888 #if DSN 1889 if (SendMIMEErrors && bitset(SRV_OFFER_DSN, features)) 1890 message("250-DSN"); 1891 #endif /* DSN */ 1892 if (bitset(SRV_OFFER_ETRN, features)) 1893 message("250-ETRN"); 1894 #if SASL 1895 if (sasl_ok && mechlist != NULL && *mechlist != '\0') 1896 message("250-AUTH %s", mechlist); 1897 #endif /* SASL */ 1898 #if STARTTLS 1899 if (tls_ok_srv && 1900 bitset(SRV_OFFER_TLS, features)) 1901 message("250-STARTTLS"); 1902 #endif /* STARTTLS */ 1903 if (DeliverByMin > 0) 1904 message("250-DELIVERBY %ld", 1905 (long) DeliverByMin); 1906 else if (DeliverByMin == 0) 1907 message("250-DELIVERBY"); 1908 1909 /* < 0: no deliver-by */ 1910 1911 message("250 HELP"); 1912 break; 1913 1914 case CMDMAIL: /* mail -- designate sender */ 1915 SmtpPhase = "server MAIL"; 1916 DELAY_CONN("MAIL"); 1917 1918 /* check for validity of this command */ 1919 if (!gothello && bitset(PRIV_NEEDMAILHELO, PrivacyFlags)) 1920 { 1921 usrerr("503 5.0.0 Polite people say HELO first"); 1922 break; 1923 } 1924 if (smtp.sm_gotmail) 1925 { 1926 usrerr("503 5.5.0 Sender already specified"); 1927 break; 1928 } 1929 #if SASL 1930 if (bitset(SRV_REQ_AUTH, features) && 1931 authenticating != SASL_IS_AUTH) 1932 { 1933 usrerr("530 5.7.0 Authentication required"); 1934 break; 1935 } 1936 #endif /* SASL */ 1937 1938 p = skipword(p, "from"); 1939 if (p == NULL) 1940 break; 1941 if (tempfail) 1942 { 1943 if (LogLevel > 9) 1944 sm_syslog(LOG_INFO, e->e_id, 1945 "SMTP MAIL command (%.100s) from %.100s tempfailed (due to previous checks)", 1946 p, CurSmtpClient); 1947 usrerr(MSG_TEMPFAIL); 1948 break; 1949 } 1950 1951 /* make sure we know who the sending host is */ 1952 if (sendinghost == NULL) 1953 sendinghost = peerhostname; 1954 1955 1956 #if SM_HEAP_CHECK 1957 if (sm_debug_active(&DebugLeakSmtp, 1)) 1958 { 1959 sm_heap_newgroup(); 1960 sm_dprintf("smtp() heap group #%d\n", 1961 sm_heap_group()); 1962 } 1963 #endif /* SM_HEAP_CHECK */ 1964 1965 if (Errors > 0) 1966 goto undo_no_pm; 1967 if (!gothello) 1968 { 1969 auth_warning(e, "%s didn't use HELO protocol", 1970 CurSmtpClient); 1971 } 1972 #ifdef PICKY_HELO_CHECK 1973 if (sm_strcasecmp(sendinghost, peerhostname) != 0 && 1974 (sm_strcasecmp(peerhostname, "localhost") != 0 || 1975 sm_strcasecmp(sendinghost, MyHostName) != 0)) 1976 { 1977 auth_warning(e, "Host %s claimed to be %s", 1978 CurSmtpClient, sendinghost); 1979 } 1980 #endif /* PICKY_HELO_CHECK */ 1981 1982 if (protocol == NULL) 1983 protocol = "SMTP"; 1984 macdefine(&e->e_macro, A_PERM, 'r', protocol); 1985 macdefine(&e->e_macro, A_PERM, 's', sendinghost); 1986 1987 if (Errors > 0) 1988 goto undo_no_pm; 1989 smtp.sm_nrcpts = 0; 1990 n_badrcpts = 0; 1991 macdefine(&e->e_macro, A_PERM, macid("{ntries}"), "0"); 1992 macdefine(&e->e_macro, A_PERM, macid("{nrcpts}"), "0"); 1993 e->e_flags |= EF_CLRQUEUE; 1994 sm_setproctitle(true, e, "%s %s: %.80s", 1995 qid_printname(e), 1996 CurSmtpClient, inp); 1997 1998 /* do the processing */ 1999 SM_TRY 2000 { 2001 extern char *FullName; 2002 2003 QuickAbort = true; 2004 SM_FREE_CLR(FullName); 2005 2006 /* must parse sender first */ 2007 delimptr = NULL; 2008 setsender(p, e, &delimptr, ' ', false); 2009 if (delimptr != NULL && *delimptr != '\0') 2010 *delimptr++ = '\0'; 2011 if (Errors > 0) 2012 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2013 2014 /* Successfully set e_from, allow logging */ 2015 e->e_flags |= EF_LOGSENDER; 2016 2017 /* put resulting triple from parseaddr() into macros */ 2018 if (e->e_from.q_mailer != NULL) 2019 macdefine(&e->e_macro, A_PERM, 2020 macid("{mail_mailer}"), 2021 e->e_from.q_mailer->m_name); 2022 else 2023 macdefine(&e->e_macro, A_PERM, 2024 macid("{mail_mailer}"), NULL); 2025 if (e->e_from.q_host != NULL) 2026 macdefine(&e->e_macro, A_PERM, 2027 macid("{mail_host}"), 2028 e->e_from.q_host); 2029 else 2030 macdefine(&e->e_macro, A_PERM, 2031 macid("{mail_host}"), "localhost"); 2032 if (e->e_from.q_user != NULL) 2033 macdefine(&e->e_macro, A_PERM, 2034 macid("{mail_addr}"), 2035 e->e_from.q_user); 2036 else 2037 macdefine(&e->e_macro, A_PERM, 2038 macid("{mail_addr}"), NULL); 2039 if (Errors > 0) 2040 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2041 2042 /* check for possible spoofing */ 2043 if (RealUid != 0 && OpMode == MD_SMTP && 2044 !wordinclass(RealUserName, 't') && 2045 (!bitnset(M_LOCALMAILER, 2046 e->e_from.q_mailer->m_flags) || 2047 strcmp(e->e_from.q_user, RealUserName) != 0)) 2048 { 2049 auth_warning(e, "%s owned process doing -bs", 2050 RealUserName); 2051 } 2052 2053 /* now parse ESMTP arguments */ 2054 e->e_msgsize = 0; 2055 addr = p; 2056 argno = 0; 2057 args[argno++] = p; 2058 p = delimptr; 2059 while (p != NULL && *p != '\0') 2060 { 2061 char *kp; 2062 char *vp = NULL; 2063 char *equal = NULL; 2064 2065 /* locate the beginning of the keyword */ 2066 SKIP_SPACE(p); 2067 if (*p == '\0') 2068 break; 2069 kp = p; 2070 2071 /* skip to the value portion */ 2072 while ((isascii(*p) && isalnum(*p)) || *p == '-') 2073 p++; 2074 if (*p == '=') 2075 { 2076 equal = p; 2077 *p++ = '\0'; 2078 vp = p; 2079 2080 /* skip to the end of the value */ 2081 while (*p != '\0' && *p != ' ' && 2082 !(isascii(*p) && iscntrl(*p)) && 2083 *p != '=') 2084 p++; 2085 } 2086 2087 if (*p != '\0') 2088 *p++ = '\0'; 2089 2090 if (tTd(19, 1)) 2091 sm_dprintf("MAIL: got arg %s=\"%s\"\n", kp, 2092 vp == NULL ? "<null>" : vp); 2093 2094 mail_esmtp_args(kp, vp, e); 2095 if (equal != NULL) 2096 *equal = '='; 2097 args[argno++] = kp; 2098 if (argno >= MAXSMTPARGS - 1) 2099 usrerr("501 5.5.4 Too many parameters"); 2100 if (Errors > 0) 2101 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2102 } 2103 args[argno] = NULL; 2104 if (Errors > 0) 2105 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2106 2107 #if SASL 2108 # if _FFR_AUTH_PASSING 2109 /* set the default AUTH= if the sender didn't */ 2110 if (e->e_auth_param == NULL) 2111 { 2112 /* XXX only do this for an MSA? */ 2113 e->e_auth_param = macvalue(macid("{auth_authen}"), 2114 e); 2115 if (e->e_auth_param == NULL) 2116 e->e_auth_param = "<>"; 2117 2118 /* 2119 ** XXX should we invoke Strust_auth now? 2120 ** authorizing as the client that just 2121 ** authenticated, so we'll trust implicitly 2122 */ 2123 } 2124 # endif /* _FFR_AUTH_PASSING */ 2125 #endif /* SASL */ 2126 2127 /* do config file checking of the sender */ 2128 macdefine(&e->e_macro, A_PERM, 2129 macid("{addr_type}"), "e s"); 2130 #if _FFR_MAIL_MACRO 2131 /* make the "real" sender address available */ 2132 macdefine(&e->e_macro, A_TEMP, macid("{mail_from}"), 2133 e->e_from.q_paddr); 2134 #endif /* _FFR_MAIL_MACRO */ 2135 if (rscheck("check_mail", addr, 2136 NULL, e, true, true, 3, NULL, 2137 e->e_id) != EX_OK || 2138 Errors > 0) 2139 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2140 macdefine(&e->e_macro, A_PERM, 2141 macid("{addr_type}"), NULL); 2142 2143 if (MaxMessageSize > 0 && 2144 (e->e_msgsize > MaxMessageSize || 2145 e->e_msgsize < 0)) 2146 { 2147 usrerr("552 5.2.3 Message size exceeds fixed maximum message size (%ld)", 2148 MaxMessageSize); 2149 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2150 } 2151 2152 /* 2153 ** XXX always check whether there is at least one fs 2154 ** with enough space? 2155 ** However, this may not help much: the queue group 2156 ** selection may later on select a FS that hasn't 2157 ** enough space. 2158 */ 2159 2160 if ((NumFileSys == 1 || NumQueue == 1) && 2161 !enoughdiskspace(e->e_msgsize, e) 2162 #if _FFR_ANY_FREE_FS 2163 && !filesys_free(e->e_msgsize) 2164 #endif /* _FFR_ANY_FREE_FS */ 2165 ) 2166 { 2167 /* 2168 ** We perform this test again when the 2169 ** queue directory is selected, in collect. 2170 */ 2171 2172 usrerr("452 4.4.5 Insufficient disk space; try again later"); 2173 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2174 } 2175 if (Errors > 0) 2176 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2177 2178 LogUsrErrs = true; 2179 #if MILTER 2180 if (smtp.sm_milterlist && smtp.sm_milterize && 2181 !bitset(EF_DISCARD, e->e_flags)) 2182 { 2183 char state; 2184 char *response; 2185 2186 response = milter_envfrom(args, e, &state); 2187 MILTER_REPLY("from"); 2188 } 2189 #endif /* MILTER */ 2190 if (Errors > 0) 2191 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2192 2193 message("250 2.1.0 Sender ok"); 2194 smtp.sm_gotmail = true; 2195 } 2196 SM_EXCEPT(exc, "[!F]*") 2197 { 2198 /* 2199 ** An error occurred while processing a MAIL command. 2200 ** Jump to the common error handling code. 2201 */ 2202 2203 sm_exc_free(exc); 2204 goto undo_no_pm; 2205 } 2206 SM_END_TRY 2207 break; 2208 2209 undo_no_pm: 2210 e->e_flags &= ~EF_PM_NOTIFY; 2211 undo: 2212 break; 2213 2214 case CMDRCPT: /* rcpt -- designate recipient */ 2215 DELAY_CONN("RCPT"); 2216 if (!smtp.sm_gotmail) 2217 { 2218 usrerr("503 5.0.0 Need MAIL before RCPT"); 2219 break; 2220 } 2221 SmtpPhase = "server RCPT"; 2222 SM_TRY 2223 { 2224 QuickAbort = true; 2225 LogUsrErrs = true; 2226 2227 /* limit flooding of our machine */ 2228 if (MaxRcptPerMsg > 0 && 2229 smtp.sm_nrcpts >= MaxRcptPerMsg) 2230 { 2231 /* sleep(1); / * slow down? */ 2232 usrerr("452 4.5.3 Too many recipients"); 2233 goto rcpt_done; 2234 } 2235 2236 if (e->e_sendmode != SM_DELIVER) 2237 e->e_flags |= EF_VRFYONLY; 2238 2239 #if MILTER 2240 /* 2241 ** If the filter will be deleting recipients, 2242 ** don't expand them at RCPT time (in the call 2243 ** to recipient()). If they are expanded, it 2244 ** is impossible for removefromlist() to figure 2245 ** out the expanded members of the original 2246 ** recipient and mark them as QS_DONTSEND. 2247 */ 2248 2249 if (milter_can_delrcpts()) 2250 e->e_flags |= EF_VRFYONLY; 2251 #endif /* MILTER */ 2252 2253 p = skipword(p, "to"); 2254 if (p == NULL) 2255 goto rcpt_done; 2256 macdefine(&e->e_macro, A_PERM, 2257 macid("{addr_type}"), "e r"); 2258 a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr, 2259 e, true); 2260 macdefine(&e->e_macro, A_PERM, 2261 macid("{addr_type}"), NULL); 2262 if (BadRcptThrottle > 0 && 2263 n_badrcpts >= BadRcptThrottle) 2264 { 2265 if (LogLevel > 5 && 2266 n_badrcpts == BadRcptThrottle) 2267 { 2268 sm_syslog(LOG_INFO, e->e_id, 2269 "%.100s: Possible SMTP RCPT flood, throttling.", 2270 CurSmtpClient); 2271 2272 /* To avoid duplicated message */ 2273 n_badrcpts++; 2274 } 2275 2276 /* 2277 ** Don't use exponential backoff for now. 2278 ** Some servers will open more connections 2279 ** and actually overload the receiver even 2280 ** more. 2281 */ 2282 2283 (void) sleep(1); 2284 } 2285 if (Errors > 0) 2286 goto rcpt_done; 2287 if (a == NULL) 2288 { 2289 usrerr("501 5.0.0 Missing recipient"); 2290 goto rcpt_done; 2291 } 2292 2293 if (delimptr != NULL && *delimptr != '\0') 2294 *delimptr++ = '\0'; 2295 2296 /* put resulting triple from parseaddr() into macros */ 2297 if (a->q_mailer != NULL) 2298 macdefine(&e->e_macro, A_PERM, 2299 macid("{rcpt_mailer}"), 2300 a->q_mailer->m_name); 2301 else 2302 macdefine(&e->e_macro, A_PERM, 2303 macid("{rcpt_mailer}"), NULL); 2304 if (a->q_host != NULL) 2305 macdefine(&e->e_macro, A_PERM, 2306 macid("{rcpt_host}"), a->q_host); 2307 else 2308 macdefine(&e->e_macro, A_PERM, 2309 macid("{rcpt_host}"), "localhost"); 2310 if (a->q_user != NULL) 2311 macdefine(&e->e_macro, A_PERM, 2312 macid("{rcpt_addr}"), a->q_user); 2313 else 2314 macdefine(&e->e_macro, A_PERM, 2315 macid("{rcpt_addr}"), NULL); 2316 if (Errors > 0) 2317 goto rcpt_done; 2318 2319 /* now parse ESMTP arguments */ 2320 addr = p; 2321 argno = 0; 2322 args[argno++] = p; 2323 p = delimptr; 2324 while (p != NULL && *p != '\0') 2325 { 2326 char *kp; 2327 char *vp = NULL; 2328 char *equal = NULL; 2329 2330 /* locate the beginning of the keyword */ 2331 SKIP_SPACE(p); 2332 if (*p == '\0') 2333 break; 2334 kp = p; 2335 2336 /* skip to the value portion */ 2337 while ((isascii(*p) && isalnum(*p)) || *p == '-') 2338 p++; 2339 if (*p == '=') 2340 { 2341 equal = p; 2342 *p++ = '\0'; 2343 vp = p; 2344 2345 /* skip to the end of the value */ 2346 while (*p != '\0' && *p != ' ' && 2347 !(isascii(*p) && iscntrl(*p)) && 2348 *p != '=') 2349 p++; 2350 } 2351 2352 if (*p != '\0') 2353 *p++ = '\0'; 2354 2355 if (tTd(19, 1)) 2356 sm_dprintf("RCPT: got arg %s=\"%s\"\n", kp, 2357 vp == NULL ? "<null>" : vp); 2358 2359 rcpt_esmtp_args(a, kp, vp, e); 2360 if (equal != NULL) 2361 *equal = '='; 2362 args[argno++] = kp; 2363 if (argno >= MAXSMTPARGS - 1) 2364 usrerr("501 5.5.4 Too many parameters"); 2365 if (Errors > 0) 2366 break; 2367 } 2368 args[argno] = NULL; 2369 if (Errors > 0) 2370 goto rcpt_done; 2371 2372 /* do config file checking of the recipient */ 2373 macdefine(&e->e_macro, A_PERM, 2374 macid("{addr_type}"), "e r"); 2375 if (rscheck("check_rcpt", addr, 2376 NULL, e, true, true, 3, NULL, 2377 e->e_id) != EX_OK || 2378 Errors > 0) 2379 goto rcpt_done; 2380 macdefine(&e->e_macro, A_PERM, 2381 macid("{addr_type}"), NULL); 2382 2383 #if MILTER 2384 if (smtp.sm_milterlist && smtp.sm_milterize && 2385 !bitset(EF_DISCARD, e->e_flags)) 2386 { 2387 char state; 2388 char *response; 2389 2390 response = milter_envrcpt(args, e, &state); 2391 MILTER_REPLY("to"); 2392 } 2393 #endif /* MILTER */ 2394 2395 macdefine(&e->e_macro, A_PERM, 2396 macid("{rcpt_mailer}"), NULL); 2397 macdefine(&e->e_macro, A_PERM, 2398 macid("{rcpt_host}"), NULL); 2399 macdefine(&e->e_macro, A_PERM, 2400 macid("{rcpt_addr}"), NULL); 2401 macdefine(&e->e_macro, A_PERM, 2402 macid("{dsn_notify}"), NULL); 2403 if (Errors > 0) 2404 goto rcpt_done; 2405 2406 /* save in recipient list after ESMTP mods */ 2407 a = recipient(a, &e->e_sendqueue, 0, e); 2408 if (Errors > 0) 2409 goto rcpt_done; 2410 2411 /* no errors during parsing, but might be a duplicate */ 2412 e->e_to = a->q_paddr; 2413 if (!QS_IS_BADADDR(a->q_state)) 2414 { 2415 if (smtp.sm_nrcpts == 0) 2416 initsys(e); 2417 message("250 2.1.5 Recipient ok%s", 2418 QS_IS_QUEUEUP(a->q_state) ? 2419 " (will queue)" : ""); 2420 smtp.sm_nrcpts++; 2421 } 2422 else 2423 { 2424 /* punt -- should keep message in ADDRESS.... */ 2425 usrerr("550 5.1.1 Addressee unknown"); 2426 } 2427 rcpt_done: 2428 if (Errors > 0) 2429 ++n_badrcpts; 2430 } 2431 SM_EXCEPT(exc, "[!F]*") 2432 { 2433 /* An exception occurred while processing RCPT */ 2434 e->e_flags &= ~(EF_FATALERRS|EF_PM_NOTIFY); 2435 ++n_badrcpts; 2436 } 2437 SM_END_TRY 2438 break; 2439 2440 case CMDDATA: /* data -- text of mail */ 2441 DELAY_CONN("DATA"); 2442 smtp_data(&smtp, e); 2443 break; 2444 2445 case CMDRSET: /* rset -- reset state */ 2446 if (tTd(94, 100)) 2447 message("451 4.0.0 Test failure"); 2448 else 2449 message("250 2.0.0 Reset state"); 2450 CLEAR_STATE(cmdbuf); 2451 #if _FFR_QUARANTINE 2452 /* restore connection quarantining */ 2453 if (smtp.sm_quarmsg == NULL) 2454 { 2455 e->e_quarmsg = NULL; 2456 macdefine(&e->e_macro, A_PERM, 2457 macid("{quarantine}"), ""); 2458 } 2459 else 2460 { 2461 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, 2462 smtp.sm_quarmsg); 2463 macdefine(&e->e_macro, A_PERM, 2464 macid("{quarantine}"), e->e_quarmsg); 2465 } 2466 #endif /* _FFR_QUARANTINE */ 2467 break; 2468 2469 case CMDVRFY: /* vrfy -- verify address */ 2470 case CMDEXPN: /* expn -- expand address */ 2471 vrfy = c->cmd_code == CMDVRFY; 2472 DELAY_CONN(vrfy ? "VRFY" : "EXPN"); 2473 if (tempfail) 2474 { 2475 if (LogLevel > 9) 2476 sm_syslog(LOG_INFO, e->e_id, 2477 "SMTP %s command (%.100s) from %.100s tempfailed (due to previous checks)", 2478 vrfy ? "VRFY" : "EXPN", 2479 p, CurSmtpClient); 2480 2481 /* RFC 821 doesn't allow 4xy reply code */ 2482 usrerr("550 5.7.1 Please try again later"); 2483 break; 2484 } 2485 wt = checksmtpattack(&n_verifies, MAXVRFYCOMMANDS, 2486 false, vrfy ? "VRFY" : "EXPN", e); 2487 previous = curtime(); 2488 if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 2489 PrivacyFlags)) 2490 { 2491 if (vrfy) 2492 message("252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)"); 2493 else 2494 message("502 5.7.0 Sorry, we do not allow this operation"); 2495 if (LogLevel > 5) 2496 sm_syslog(LOG_INFO, e->e_id, 2497 "%.100s: %s [rejected]", 2498 CurSmtpClient, 2499 shortenstring(inp, MAXSHORTSTR)); 2500 break; 2501 } 2502 else if (!gothello && 2503 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 2504 PrivacyFlags)) 2505 { 2506 usrerr("503 5.0.0 I demand that you introduce yourself first"); 2507 break; 2508 } 2509 if (Errors > 0) 2510 break; 2511 if (LogLevel > 5) 2512 sm_syslog(LOG_INFO, e->e_id, "%.100s: %s", 2513 CurSmtpClient, 2514 shortenstring(inp, MAXSHORTSTR)); 2515 SM_TRY 2516 { 2517 QuickAbort = true; 2518 vrfyqueue = NULL; 2519 if (vrfy) 2520 e->e_flags |= EF_VRFYONLY; 2521 while (*p != '\0' && isascii(*p) && isspace(*p)) 2522 p++; 2523 if (*p == '\0') 2524 { 2525 usrerr("501 5.5.2 Argument required"); 2526 } 2527 else 2528 { 2529 /* do config file checking of the address */ 2530 if (rscheck(vrfy ? "check_vrfy" : "check_expn", 2531 p, NULL, e, true, false, 3, NULL, 2532 NOQID) != EX_OK || 2533 Errors > 0) 2534 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2535 (void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e); 2536 } 2537 if (wt > 0) 2538 { 2539 time_t t; 2540 2541 t = wt - (curtime() - previous); 2542 if (t > 0) 2543 (void) sleep(t); 2544 } 2545 if (Errors > 0) 2546 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2547 if (vrfyqueue == NULL) 2548 { 2549 usrerr("554 5.5.2 Nothing to %s", vrfy ? "VRFY" : "EXPN"); 2550 } 2551 while (vrfyqueue != NULL) 2552 { 2553 if (!QS_IS_UNDELIVERED(vrfyqueue->q_state)) 2554 { 2555 vrfyqueue = vrfyqueue->q_next; 2556 continue; 2557 } 2558 2559 /* see if there is more in the vrfy list */ 2560 a = vrfyqueue; 2561 while ((a = a->q_next) != NULL && 2562 (!QS_IS_UNDELIVERED(a->q_state))) 2563 continue; 2564 printvrfyaddr(vrfyqueue, a == NULL, vrfy); 2565 vrfyqueue = a; 2566 } 2567 } 2568 SM_EXCEPT(exc, "[!F]*") 2569 { 2570 /* 2571 ** An exception occurred while processing VRFY/EXPN 2572 */ 2573 2574 sm_exc_free(exc); 2575 goto undo; 2576 } 2577 SM_END_TRY 2578 break; 2579 2580 case CMDETRN: /* etrn -- force queue flush */ 2581 DELAY_CONN("ETRN"); 2582 2583 /* Don't leak queue information via debug flags */ 2584 if (!bitset(SRV_OFFER_ETRN, features) || UseMSP || 2585 (RealUid != 0 && RealUid != TrustedUid && 2586 OpMode == MD_SMTP)) 2587 { 2588 /* different message for MSA ? */ 2589 message("502 5.7.0 Sorry, we do not allow this operation"); 2590 if (LogLevel > 5) 2591 sm_syslog(LOG_INFO, e->e_id, 2592 "%.100s: %s [rejected]", 2593 CurSmtpClient, 2594 shortenstring(inp, MAXSHORTSTR)); 2595 break; 2596 } 2597 if (tempfail) 2598 { 2599 if (LogLevel > 9) 2600 sm_syslog(LOG_INFO, e->e_id, 2601 "SMTP ETRN command (%.100s) from %.100s tempfailed (due to previous checks)", 2602 p, CurSmtpClient); 2603 usrerr(MSG_TEMPFAIL); 2604 break; 2605 } 2606 2607 if (strlen(p) <= 0) 2608 { 2609 usrerr("500 5.5.2 Parameter required"); 2610 break; 2611 } 2612 2613 /* crude way to avoid denial-of-service attacks */ 2614 (void) checksmtpattack(&n_etrn, MAXETRNCOMMANDS, true, 2615 "ETRN", e); 2616 2617 /* 2618 ** Do config file checking of the parameter. 2619 ** Even though we have srv_features now, we still 2620 ** need this ruleset because the former is called 2621 ** when the connection has been established, while 2622 ** this ruleset is called when the command is 2623 ** actually issued and therefore has all information 2624 ** available to make a decision. 2625 */ 2626 2627 if (rscheck("check_etrn", p, NULL, e, true, false, 3, 2628 NULL, NOQID) != EX_OK || Errors > 0) 2629 break; 2630 2631 if (LogLevel > 5) 2632 sm_syslog(LOG_INFO, e->e_id, 2633 "%.100s: ETRN %s", CurSmtpClient, 2634 shortenstring(p, MAXSHORTSTR)); 2635 2636 id = p; 2637 if (*id == '#') 2638 { 2639 int wgrp; 2640 2641 id++; 2642 wgrp = name2qid(id); 2643 if (!ISVALIDQGRP(wgrp)) 2644 { 2645 usrerr("459 4.5.4 Queue %s unknown", 2646 id); 2647 break; 2648 } 2649 ok = run_work_group(wgrp, true, false, 2650 false, true); 2651 if (ok && Errors == 0) 2652 message("250 2.0.0 Queuing for queue group %s started", id); 2653 break; 2654 } 2655 2656 if (*id == '@') 2657 id++; 2658 else 2659 *--id = '@'; 2660 2661 new = (QUEUE_CHAR *) sm_malloc(sizeof(QUEUE_CHAR)); 2662 if (new == NULL) 2663 { 2664 syserr("500 5.5.0 ETRN out of memory"); 2665 break; 2666 } 2667 new->queue_match = id; 2668 new->queue_negate = false; 2669 new->queue_next = NULL; 2670 QueueLimitRecipient = new; 2671 ok = runqueue(true, false, false, true); 2672 sm_free(QueueLimitRecipient); /* XXX */ 2673 QueueLimitRecipient = NULL; 2674 if (ok && Errors == 0) 2675 message("250 2.0.0 Queuing for node %s started", p); 2676 break; 2677 2678 case CMDHELP: /* help -- give user info */ 2679 DELAY_CONN("HELP"); 2680 help(p, e); 2681 break; 2682 2683 case CMDNOOP: /* noop -- do nothing */ 2684 DELAY_CONN("NOOP"); 2685 (void) checksmtpattack(&n_noop, MAXNOOPCOMMANDS, true, 2686 "NOOP", e); 2687 message("250 2.0.0 OK"); 2688 break; 2689 2690 case CMDQUIT: /* quit -- leave mail */ 2691 message("221 2.0.0 %s closing connection", MyHostName); 2692 #if PIPELINING 2693 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT); 2694 #endif /* PIPELINING */ 2695 2696 if (smtp.sm_nrcpts > 0) 2697 logundelrcpts(e, "aborted by sender", 9, false); 2698 2699 /* arrange to ignore any current send list */ 2700 e->e_sendqueue = NULL; 2701 2702 #if STARTTLS 2703 /* shutdown TLS connection */ 2704 if (tls_active) 2705 { 2706 (void) endtls(srv_ssl, "server"); 2707 tls_active = false; 2708 } 2709 #endif /* STARTTLS */ 2710 #if SASL 2711 if (authenticating == SASL_IS_AUTH) 2712 { 2713 sasl_dispose(&conn); 2714 authenticating = SASL_NOT_AUTH; 2715 /* XXX sasl_done(); this is a child */ 2716 } 2717 #endif /* SASL */ 2718 2719 doquit: 2720 /* avoid future 050 messages */ 2721 disconnect(1, e); 2722 2723 #if MILTER 2724 /* close out milter filters */ 2725 milter_quit(e); 2726 #endif /* MILTER */ 2727 2728 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 2729 logsender(e, NULL); 2730 e->e_flags &= ~EF_LOGSENDER; 2731 2732 if (lognullconnection && LogLevel > 5 && 2733 nullserver == NULL) 2734 { 2735 char *d; 2736 2737 d = macvalue(macid("{daemon_name}"), e); 2738 if (d == NULL) 2739 d = "stdin"; 2740 2741 /* 2742 ** even though this id is "bogus", it makes 2743 ** it simpler to "grep" related events, e.g., 2744 ** timeouts for the same connection. 2745 */ 2746 2747 sm_syslog(LOG_INFO, e->e_id, 2748 "%.100s did not issue MAIL/EXPN/VRFY/ETRN during connection to %s", 2749 CurSmtpClient, d); 2750 } 2751 #if PROFILING 2752 return; 2753 #endif /* PROFILING */ 2754 finis(true, true, ExitStat); 2755 /* NOTREACHED */ 2756 2757 case CMDVERB: /* set verbose mode */ 2758 DELAY_CONN("VERB"); 2759 if (bitset(PRIV_NOEXPN, PrivacyFlags) || 2760 !bitset(SRV_OFFER_VERB, features) || 2761 bitset(PRIV_NOVERB, PrivacyFlags)) 2762 { 2763 /* this would give out the same info */ 2764 message("502 5.7.0 Verbose unavailable"); 2765 break; 2766 } 2767 (void) checksmtpattack(&n_noop, MAXNOOPCOMMANDS, true, 2768 "VERB", e); 2769 Verbose = 1; 2770 set_delivery_mode(SM_DELIVER, e); 2771 message("250 2.0.0 Verbose mode"); 2772 break; 2773 2774 #if SMTPDEBUG 2775 case CMDDBGQSHOW: /* show queues */ 2776 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2777 "Send Queue="); 2778 printaddr(e->e_sendqueue, true); 2779 break; 2780 2781 case CMDDBGDEBUG: /* set debug mode */ 2782 tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 2783 tTflag(p); 2784 message("200 2.0.0 Debug set"); 2785 break; 2786 2787 #else /* SMTPDEBUG */ 2788 case CMDDBGQSHOW: /* show queues */ 2789 case CMDDBGDEBUG: /* set debug mode */ 2790 #endif /* SMTPDEBUG */ 2791 case CMDLOGBOGUS: /* bogus command */ 2792 DELAY_CONN("Bogus"); 2793 if (LogLevel > 0) 2794 sm_syslog(LOG_CRIT, e->e_id, 2795 "\"%s\" command from %.100s (%.100s)", 2796 c->cmd_name, CurSmtpClient, 2797 anynet_ntoa(&RealHostAddr)); 2798 /* FALLTHROUGH */ 2799 2800 case CMDERROR: /* unknown command */ 2801 #if MAXBADCOMMANDS > 0 2802 if (++n_badcmds > MAXBADCOMMANDS) 2803 { 2804 message("421 4.7.0 %s Too many bad commands; closing connection", 2805 MyHostName); 2806 2807 /* arrange to ignore any current send list */ 2808 e->e_sendqueue = NULL; 2809 goto doquit; 2810 } 2811 #endif /* MAXBADCOMMANDS > 0 */ 2812 2813 usrerr("500 5.5.1 Command unrecognized: \"%s\"", 2814 shortenstring(inp, MAXSHORTSTR)); 2815 break; 2816 2817 case CMDUNIMPL: 2818 DELAY_CONN("Unimpl"); 2819 usrerr("502 5.5.1 Command not implemented: \"%s\"", 2820 shortenstring(inp, MAXSHORTSTR)); 2821 break; 2822 2823 default: 2824 DELAY_CONN("default"); 2825 errno = 0; 2826 syserr("500 5.5.0 smtp: unknown code %d", c->cmd_code); 2827 break; 2828 } 2829 #if SASL 2830 } 2831 #endif /* SASL */ 2832 } 2833 SM_EXCEPT(exc, "[!F]*") 2834 { 2835 /* 2836 ** The only possible exception is "E:mta.quickabort". 2837 ** There is nothing to do except fall through and loop. 2838 */ 2839 } 2840 SM_END_TRY 2841 } 2842 } 2843 /* 2844 ** SMTP_DATA -- implement the SMTP DATA command. 2845 ** 2846 ** Parameters: 2847 ** smtp -- status of SMTP connection. 2848 ** e -- envelope. 2849 ** 2850 ** Returns: 2851 ** none. 2852 ** 2853 ** Side Effects: 2854 ** possibly sends message. 2855 */ 2856 2857 static void 2858 smtp_data(smtp, e) 2859 SMTP_T *smtp; 2860 ENVELOPE *e; 2861 { 2862 #if MILTER 2863 bool milteraccept; 2864 #endif /* MILTER */ 2865 bool aborting; 2866 bool doublequeue; 2867 ADDRESS *a; 2868 ENVELOPE *ee; 2869 char *id; 2870 char *oldid; 2871 char buf[32]; 2872 2873 SmtpPhase = "server DATA"; 2874 if (!smtp->sm_gotmail) 2875 { 2876 usrerr("503 5.0.0 Need MAIL command"); 2877 return; 2878 } 2879 else if (smtp->sm_nrcpts <= 0) 2880 { 2881 usrerr("503 5.0.0 Need RCPT (recipient)"); 2882 return; 2883 } 2884 (void) sm_snprintf(buf, sizeof buf, "%u", smtp->sm_nrcpts); 2885 if (rscheck("check_data", buf, NULL, e, 2886 true, false, 3, NULL, e->e_id) != EX_OK) 2887 return; 2888 2889 /* put back discard bit */ 2890 if (smtp->sm_discard) 2891 e->e_flags |= EF_DISCARD; 2892 2893 /* check to see if we need to re-expand aliases */ 2894 /* also reset QS_BADADDR on already-diagnosted addrs */ 2895 doublequeue = false; 2896 for (a = e->e_sendqueue; a != NULL; a = a->q_next) 2897 { 2898 if (QS_IS_VERIFIED(a->q_state) && 2899 !bitset(EF_DISCARD, e->e_flags)) 2900 { 2901 /* need to re-expand aliases */ 2902 doublequeue = true; 2903 } 2904 if (QS_IS_BADADDR(a->q_state)) 2905 { 2906 /* make this "go away" */ 2907 a->q_state = QS_DONTSEND; 2908 } 2909 } 2910 2911 /* collect the text of the message */ 2912 SmtpPhase = "collect"; 2913 buffer_errors(); 2914 2915 #if _FFR_ADAPTIVE_EOL 2916 /* triggers error in collect, disabled for now */ 2917 if (smtp->sm_crlf) 2918 e->e_flags |= EF_NL_NOT_EOL; 2919 #endif /* _FFR_ADAPTIVE_EOL */ 2920 2921 collect(InChannel, true, NULL, e); 2922 2923 /* redefine message size */ 2924 (void) sm_snprintf(buf, sizeof buf, "%ld", e->e_msgsize); 2925 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf); 2926 2927 #if _FFR_CHECK_EOM 2928 /* rscheck() will set Errors or EF_DISCARD if it trips */ 2929 (void) rscheck("check_eom", buf, NULL, e, false, 2930 true, 3, NULL, e->e_id); 2931 #endif /* _FFR_CHECK_EOM */ 2932 2933 #if MILTER 2934 milteraccept = true; 2935 if (smtp->sm_milterlist && smtp->sm_milterize && 2936 Errors <= 0 && 2937 !bitset(EF_DISCARD, e->e_flags)) 2938 { 2939 char state; 2940 char *response; 2941 2942 response = milter_data(e, &state); 2943 switch (state) 2944 { 2945 case SMFIR_REPLYCODE: 2946 if (MilterLogLevel > 3) 2947 sm_syslog(LOG_INFO, e->e_id, 2948 "Milter: data, reject=%s", 2949 response); 2950 milteraccept = false; 2951 usrerr(response); 2952 break; 2953 2954 case SMFIR_REJECT: 2955 milteraccept = false; 2956 if (MilterLogLevel > 3) 2957 sm_syslog(LOG_INFO, e->e_id, 2958 "Milter: data, reject=554 5.7.1 Command rejected"); 2959 usrerr("554 5.7.1 Command rejected"); 2960 break; 2961 2962 case SMFIR_DISCARD: 2963 if (MilterLogLevel > 3) 2964 sm_syslog(LOG_INFO, e->e_id, 2965 "Milter: data, discard"); 2966 milteraccept = false; 2967 e->e_flags |= EF_DISCARD; 2968 break; 2969 2970 case SMFIR_TEMPFAIL: 2971 if (MilterLogLevel > 3) 2972 sm_syslog(LOG_INFO, e->e_id, 2973 "Milter: data, reject=%s", 2974 MSG_TEMPFAIL); 2975 milteraccept = false; 2976 usrerr(MSG_TEMPFAIL); 2977 break; 2978 } 2979 if (response != NULL) 2980 sm_free(response); 2981 } 2982 2983 /* Milter may have changed message size */ 2984 (void) sm_snprintf(buf, sizeof buf, "%ld", e->e_msgsize); 2985 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf); 2986 2987 /* abort message filters that didn't get the body & log msg is OK */ 2988 if (smtp->sm_milterlist && smtp->sm_milterize) 2989 { 2990 milter_abort(e); 2991 if (milteraccept && MilterLogLevel > 9) 2992 sm_syslog(LOG_INFO, e->e_id, "Milter accept: message"); 2993 } 2994 #endif /* MILTER */ 2995 2996 #if _FFR_QUARANTINE 2997 /* Check if quarantining stats should be updated */ 2998 if (e->e_quarmsg != NULL) 2999 markstats(e, NULL, STATS_QUARANTINE); 3000 #endif /* _FFR_QUARANTINE */ 3001 3002 /* 3003 ** If a header/body check (header checks or milter) 3004 ** set EF_DISCARD, don't queueup the message -- 3005 ** that would lose the EF_DISCARD bit and deliver 3006 ** the message. 3007 */ 3008 3009 if (bitset(EF_DISCARD, e->e_flags)) 3010 doublequeue = false; 3011 3012 aborting = Errors > 0; 3013 if (!aborting && 3014 #if _FFR_QUARANTINE 3015 (QueueMode == QM_QUARANTINE || e->e_quarmsg == NULL) && 3016 #endif /* _FFR_QUARANTINE */ 3017 !split_by_recipient(e)) 3018 aborting = bitset(EF_FATALERRS, e->e_flags); 3019 3020 if (aborting) 3021 { 3022 /* Log who the mail would have gone to */ 3023 logundelrcpts(e, e->e_message, 8, false); 3024 flush_errors(true); 3025 buffer_errors(); 3026 goto abortmessage; 3027 } 3028 3029 /* from now on, we have to operate silently */ 3030 buffer_errors(); 3031 3032 #if 0 3033 /* 3034 ** Clear message, it may contain an error from the SMTP dialogue. 3035 ** This error must not show up in the queue. 3036 ** Some error message should show up, e.g., alias database 3037 ** not available, but others shouldn't, e.g., from check_rcpt. 3038 */ 3039 3040 e->e_message = NULL; 3041 #endif /* 0 */ 3042 3043 /* 3044 ** Arrange to send to everyone. 3045 ** If sending to multiple people, mail back 3046 ** errors rather than reporting directly. 3047 ** In any case, don't mail back errors for 3048 ** anything that has happened up to 3049 ** now (the other end will do this). 3050 ** Truncate our transcript -- the mail has gotten 3051 ** to us successfully, and if we have 3052 ** to mail this back, it will be easier 3053 ** on the reader. 3054 ** Then send to everyone. 3055 ** Finally give a reply code. If an error has 3056 ** already been given, don't mail a 3057 ** message back. 3058 ** We goose error returns by clearing error bit. 3059 */ 3060 3061 SmtpPhase = "delivery"; 3062 (void) sm_io_setinfo(e->e_xfp, SM_BF_TRUNCATE, NULL); 3063 id = e->e_id; 3064 3065 #if NAMED_BIND 3066 _res.retry = TimeOuts.res_retry[RES_TO_FIRST]; 3067 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST]; 3068 #endif /* NAMED_BIND */ 3069 3070 for (ee = e; ee != NULL; ee = ee->e_sibling) 3071 { 3072 /* make sure we actually do delivery */ 3073 ee->e_flags &= ~EF_CLRQUEUE; 3074 3075 /* from now on, operate silently */ 3076 ee->e_errormode = EM_MAIL; 3077 3078 if (doublequeue) 3079 { 3080 /* make sure it is in the queue */ 3081 queueup(ee, false, true); 3082 } 3083 else 3084 { 3085 /* send to all recipients */ 3086 sendall(ee, SM_DEFAULT); 3087 } 3088 ee->e_to = NULL; 3089 } 3090 3091 /* put back id for SMTP logging in putoutmsg() */ 3092 oldid = CurEnv->e_id; 3093 CurEnv->e_id = id; 3094 3095 /* issue success message */ 3096 message("250 2.0.0 %s Message accepted for delivery", id); 3097 CurEnv->e_id = oldid; 3098 3099 /* if we just queued, poke it */ 3100 if (doublequeue) 3101 { 3102 bool anything_to_send = false; 3103 3104 sm_getla(); 3105 for (ee = e; ee != NULL; ee = ee->e_sibling) 3106 { 3107 if (WILL_BE_QUEUED(ee->e_sendmode)) 3108 continue; 3109 if (shouldqueue(ee->e_msgpriority, ee->e_ctime)) 3110 { 3111 ee->e_sendmode = SM_QUEUE; 3112 continue; 3113 } 3114 #if _FFR_QUARANTINE 3115 else if (QueueMode != QM_QUARANTINE && 3116 ee->e_quarmsg != NULL) 3117 { 3118 ee->e_sendmode = SM_QUEUE; 3119 continue; 3120 } 3121 #endif /* _FFR_QUARANTINE */ 3122 anything_to_send = true; 3123 3124 /* close all the queue files */ 3125 closexscript(ee); 3126 if (ee->e_dfp != NULL) 3127 { 3128 (void) sm_io_close(ee->e_dfp, SM_TIME_DEFAULT); 3129 ee->e_dfp = NULL; 3130 } 3131 unlockqueue(ee); 3132 } 3133 if (anything_to_send) 3134 { 3135 #if PIPELINING 3136 /* 3137 ** XXX if we don't do this, we get 250 twice 3138 ** because it is also flushed in the child. 3139 */ 3140 3141 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT); 3142 #endif /* PIPELINING */ 3143 (void) doworklist(e, true, true); 3144 } 3145 } 3146 3147 abortmessage: 3148 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 3149 logsender(e, NULL); 3150 e->e_flags &= ~EF_LOGSENDER; 3151 3152 /* clean up a bit */ 3153 smtp->sm_gotmail = false; 3154 3155 /* 3156 ** Call dropenvelope if and only if the envelope is *not* 3157 ** being processed by the child process forked by doworklist(). 3158 */ 3159 3160 if (aborting || bitset(EF_DISCARD, e->e_flags)) 3161 dropenvelope(e, true, false); 3162 else 3163 { 3164 for (ee = e; ee != NULL; ee = ee->e_sibling) 3165 { 3166 #if _FFR_QUARANTINE 3167 if (!doublequeue && 3168 QueueMode != QM_QUARANTINE && 3169 ee->e_quarmsg != NULL) 3170 { 3171 dropenvelope(ee, true, false); 3172 continue; 3173 } 3174 #endif /* _FFR_QUARANTINE */ 3175 if (WILL_BE_QUEUED(ee->e_sendmode)) 3176 dropenvelope(ee, true, false); 3177 } 3178 } 3179 sm_rpool_free(e->e_rpool); 3180 3181 /* 3182 ** At this point, e == &MainEnvelope, but if we did splitting, 3183 ** then CurEnv may point to an envelope structure that was just 3184 ** freed with the rpool. So reset CurEnv *before* calling 3185 ** newenvelope. 3186 */ 3187 3188 CurEnv = e; 3189 newenvelope(e, e, sm_rpool_new_x(NULL)); 3190 e->e_flags = BlankEnvelope.e_flags; 3191 3192 #if _FFR_QUARANTINE 3193 /* restore connection quarantining */ 3194 if (smtp->sm_quarmsg == NULL) 3195 { 3196 e->e_quarmsg = NULL; 3197 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), ""); 3198 } 3199 else 3200 { 3201 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, smtp->sm_quarmsg); 3202 macdefine(&e->e_macro, A_PERM, 3203 macid("{quarantine}"), e->e_quarmsg); 3204 } 3205 #endif /* _FFR_QUARANTINE */ 3206 } 3207 /* 3208 ** LOGUNDELRCPTS -- log undelivered (or all) recipients. 3209 ** 3210 ** Parameters: 3211 ** e -- envelope. 3212 ** msg -- message for Stat= 3213 ** level -- log level. 3214 ** all -- log all recipients. 3215 ** 3216 ** Returns: 3217 ** none. 3218 ** 3219 ** Side Effects: 3220 ** logs undelivered (or all) recipients 3221 */ 3222 3223 void 3224 logundelrcpts(e, msg, level, all) 3225 ENVELOPE *e; 3226 char *msg; 3227 int level; 3228 bool all; 3229 { 3230 ADDRESS *a; 3231 3232 if (LogLevel <= level || msg == NULL || *msg == '\0') 3233 return; 3234 3235 /* Clear $h so relay= doesn't get mislogged by logdelivery() */ 3236 macdefine(&e->e_macro, A_PERM, 'h', NULL); 3237 3238 /* Log who the mail would have gone to */ 3239 for (a = e->e_sendqueue; a != NULL; a = a->q_next) 3240 { 3241 if (!QS_IS_UNDELIVERED(a->q_state) && !all) 3242 continue; 3243 e->e_to = a->q_paddr; 3244 logdelivery(NULL, NULL, a->q_status, msg, NULL, 3245 (time_t) 0, e); 3246 } 3247 e->e_to = NULL; 3248 } 3249 /* 3250 ** CHECKSMTPATTACK -- check for denial-of-service attack by repetition 3251 ** 3252 ** Parameters: 3253 ** pcounter -- pointer to a counter for this command. 3254 ** maxcount -- maximum value for this counter before we 3255 ** slow down. 3256 ** waitnow -- sleep now (in this routine)? 3257 ** cname -- command name for logging. 3258 ** e -- the current envelope. 3259 ** 3260 ** Returns: 3261 ** time to wait. 3262 ** 3263 ** Side Effects: 3264 ** Slows down if we seem to be under attack. 3265 */ 3266 3267 static time_t 3268 checksmtpattack(pcounter, maxcount, waitnow, cname, e) 3269 volatile unsigned int *pcounter; 3270 int maxcount; 3271 bool waitnow; 3272 char *cname; 3273 ENVELOPE *e; 3274 { 3275 if (maxcount <= 0) /* no limit */ 3276 return (time_t) 0; 3277 3278 if (++(*pcounter) >= maxcount) 3279 { 3280 time_t s; 3281 3282 if (*pcounter == maxcount && LogLevel > 5) 3283 { 3284 sm_syslog(LOG_INFO, e->e_id, 3285 "%.100s: possible SMTP attack: command=%.40s, count=%u", 3286 CurSmtpClient, cname, *pcounter); 3287 } 3288 s = 1 << (*pcounter - maxcount); 3289 if (s >= MAXTIMEOUT || s <= 0) 3290 s = MAXTIMEOUT; 3291 3292 /* sleep at least 1 second before returning */ 3293 (void) sleep(*pcounter / maxcount); 3294 s -= *pcounter / maxcount; 3295 if (waitnow) 3296 { 3297 (void) sleep(s); 3298 return 0; 3299 } 3300 return s; 3301 } 3302 return (time_t) 0; 3303 } 3304 /* 3305 ** SETUP_SMTPD_IO -- setup I/O fd correctly for the SMTP server 3306 ** 3307 ** Parameters: 3308 ** none. 3309 ** 3310 ** Returns: 3311 ** nothing. 3312 ** 3313 ** Side Effects: 3314 ** may change I/O fd. 3315 */ 3316 3317 static void 3318 setup_smtpd_io() 3319 { 3320 int inchfd, outchfd, outfd; 3321 3322 inchfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL); 3323 outchfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL); 3324 outfd = sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL); 3325 if (outchfd != outfd) 3326 { 3327 /* arrange for debugging output to go to remote host */ 3328 (void) dup2(outchfd, outfd); 3329 } 3330 3331 /* 3332 ** if InChannel and OutChannel are stdin/stdout 3333 ** and connected to ttys 3334 ** and fcntl(STDIN, F_SETFL, O_NONBLOCKING) also changes STDOUT, 3335 ** then "chain" them together. 3336 */ 3337 3338 if (inchfd == STDIN_FILENO && outchfd == STDOUT_FILENO && 3339 isatty(inchfd) && isatty(outchfd)) 3340 { 3341 int inmode, outmode; 3342 3343 inmode = fcntl(inchfd, F_GETFL, 0); 3344 if (inmode == -1) 3345 { 3346 if (LogLevel > 11) 3347 sm_syslog(LOG_INFO, NOQID, 3348 "fcntl(inchfd, F_GETFL) failed: %s", 3349 sm_errstring(errno)); 3350 return; 3351 } 3352 outmode = fcntl(outchfd, F_GETFL, 0); 3353 if (outmode == -1) 3354 { 3355 if (LogLevel > 11) 3356 sm_syslog(LOG_INFO, NOQID, 3357 "fcntl(outchfd, F_GETFL) failed: %s", 3358 sm_errstring(errno)); 3359 return; 3360 } 3361 if (bitset(O_NONBLOCK, inmode) || 3362 bitset(O_NONBLOCK, outmode) || 3363 fcntl(inchfd, F_SETFL, inmode | O_NONBLOCK) == -1) 3364 return; 3365 outmode = fcntl(outchfd, F_GETFL, 0); 3366 if (outmode != -1 && bitset(O_NONBLOCK, outmode)) 3367 { 3368 /* changing InChannel also changes OutChannel */ 3369 sm_io_automode(OutChannel, InChannel); 3370 if (tTd(97, 4) && LogLevel > 9) 3371 sm_syslog(LOG_INFO, NOQID, 3372 "set automode for I (%d)/O (%d) in SMTP server", 3373 inchfd, outchfd); 3374 } 3375 3376 /* undo change of inchfd */ 3377 (void) fcntl(inchfd, F_SETFL, inmode); 3378 } 3379 } 3380 /* 3381 ** SKIPWORD -- skip a fixed word. 3382 ** 3383 ** Parameters: 3384 ** p -- place to start looking. 3385 ** w -- word to skip. 3386 ** 3387 ** Returns: 3388 ** p following w. 3389 ** NULL on error. 3390 ** 3391 ** Side Effects: 3392 ** clobbers the p data area. 3393 */ 3394 3395 static char * 3396 skipword(p, w) 3397 register char *volatile p; 3398 char *w; 3399 { 3400 register char *q; 3401 char *firstp = p; 3402 3403 /* find beginning of word */ 3404 SKIP_SPACE(p); 3405 q = p; 3406 3407 /* find end of word */ 3408 while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 3409 p++; 3410 while (isascii(*p) && isspace(*p)) 3411 *p++ = '\0'; 3412 if (*p != ':') 3413 { 3414 syntax: 3415 usrerr("501 5.5.2 Syntax error in parameters scanning \"%s\"", 3416 shortenstring(firstp, MAXSHORTSTR)); 3417 return NULL; 3418 } 3419 *p++ = '\0'; 3420 SKIP_SPACE(p); 3421 3422 if (*p == '\0') 3423 goto syntax; 3424 3425 /* see if the input word matches desired word */ 3426 if (sm_strcasecmp(q, w)) 3427 goto syntax; 3428 3429 return p; 3430 } 3431 /* 3432 ** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line 3433 ** 3434 ** Parameters: 3435 ** kp -- the parameter key. 3436 ** vp -- the value of that parameter. 3437 ** e -- the envelope. 3438 ** 3439 ** Returns: 3440 ** none. 3441 */ 3442 3443 static void 3444 mail_esmtp_args(kp, vp, e) 3445 char *kp; 3446 char *vp; 3447 ENVELOPE *e; 3448 { 3449 if (sm_strcasecmp(kp, "size") == 0) 3450 { 3451 if (vp == NULL) 3452 { 3453 usrerr("501 5.5.2 SIZE requires a value"); 3454 /* NOTREACHED */ 3455 } 3456 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), vp); 3457 errno = 0; 3458 e->e_msgsize = strtol(vp, (char **) NULL, 10); 3459 if (e->e_msgsize == LONG_MAX && errno == ERANGE) 3460 { 3461 usrerr("552 5.2.3 Message size exceeds maximum value"); 3462 /* NOTREACHED */ 3463 } 3464 if (e->e_msgsize < 0) 3465 { 3466 usrerr("552 5.2.3 Message size invalid"); 3467 /* NOTREACHED */ 3468 } 3469 } 3470 else if (sm_strcasecmp(kp, "body") == 0) 3471 { 3472 if (vp == NULL) 3473 { 3474 usrerr("501 5.5.2 BODY requires a value"); 3475 /* NOTREACHED */ 3476 } 3477 else if (sm_strcasecmp(vp, "8bitmime") == 0) 3478 { 3479 SevenBitInput = false; 3480 } 3481 else if (sm_strcasecmp(vp, "7bit") == 0) 3482 { 3483 SevenBitInput = true; 3484 } 3485 else 3486 { 3487 usrerr("501 5.5.4 Unknown BODY type %s", vp); 3488 /* NOTREACHED */ 3489 } 3490 e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, vp); 3491 } 3492 else if (sm_strcasecmp(kp, "envid") == 0) 3493 { 3494 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3495 { 3496 usrerr("504 5.7.0 Sorry, ENVID not supported, we do not allow DSN"); 3497 /* NOTREACHED */ 3498 } 3499 if (vp == NULL) 3500 { 3501 usrerr("501 5.5.2 ENVID requires a value"); 3502 /* NOTREACHED */ 3503 } 3504 if (!xtextok(vp)) 3505 { 3506 usrerr("501 5.5.4 Syntax error in ENVID parameter value"); 3507 /* NOTREACHED */ 3508 } 3509 if (e->e_envid != NULL) 3510 { 3511 usrerr("501 5.5.0 Duplicate ENVID parameter"); 3512 /* NOTREACHED */ 3513 } 3514 e->e_envid = sm_rpool_strdup_x(e->e_rpool, vp); 3515 macdefine(&e->e_macro, A_PERM, 3516 macid("{dsn_envid}"), e->e_envid); 3517 } 3518 else if (sm_strcasecmp(kp, "ret") == 0) 3519 { 3520 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3521 { 3522 usrerr("504 5.7.0 Sorry, RET not supported, we do not allow DSN"); 3523 /* NOTREACHED */ 3524 } 3525 if (vp == NULL) 3526 { 3527 usrerr("501 5.5.2 RET requires a value"); 3528 /* NOTREACHED */ 3529 } 3530 if (bitset(EF_RET_PARAM, e->e_flags)) 3531 { 3532 usrerr("501 5.5.0 Duplicate RET parameter"); 3533 /* NOTREACHED */ 3534 } 3535 e->e_flags |= EF_RET_PARAM; 3536 if (sm_strcasecmp(vp, "hdrs") == 0) 3537 e->e_flags |= EF_NO_BODY_RETN; 3538 else if (sm_strcasecmp(vp, "full") != 0) 3539 { 3540 usrerr("501 5.5.2 Bad argument \"%s\" to RET", vp); 3541 /* NOTREACHED */ 3542 } 3543 macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), vp); 3544 } 3545 #if SASL 3546 else if (sm_strcasecmp(kp, "auth") == 0) 3547 { 3548 int len; 3549 char *q; 3550 char *auth_param; /* the value of the AUTH=x */ 3551 bool saveQuickAbort = QuickAbort; 3552 bool saveSuprErrs = SuprErrs; 3553 bool saveExitStat = ExitStat; 3554 char pbuf[256]; 3555 3556 if (vp == NULL) 3557 { 3558 usrerr("501 5.5.2 AUTH= requires a value"); 3559 /* NOTREACHED */ 3560 } 3561 if (e->e_auth_param != NULL) 3562 { 3563 usrerr("501 5.5.0 Duplicate AUTH parameter"); 3564 /* NOTREACHED */ 3565 } 3566 if ((q = strchr(vp, ' ')) != NULL) 3567 len = q - vp + 1; 3568 else 3569 len = strlen(vp) + 1; 3570 auth_param = xalloc(len); 3571 (void) sm_strlcpy(auth_param, vp, len); 3572 if (!xtextok(auth_param)) 3573 { 3574 usrerr("501 5.5.4 Syntax error in AUTH parameter value"); 3575 /* just a warning? */ 3576 /* NOTREACHED */ 3577 } 3578 3579 /* XXX this might be cut off */ 3580 (void) sm_strlcpy(pbuf, xuntextify(auth_param), sizeof pbuf); 3581 /* xalloc() the buffer instead? */ 3582 3583 /* XXX define this always or only if trusted? */ 3584 macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"), pbuf); 3585 3586 /* 3587 ** call Strust_auth to find out whether 3588 ** auth_param is acceptable (trusted) 3589 ** we shouldn't trust it if not authenticated 3590 ** (required by RFC, leave it to ruleset?) 3591 */ 3592 3593 SuprErrs = true; 3594 QuickAbort = false; 3595 if (strcmp(auth_param, "<>") != 0 && 3596 (rscheck("trust_auth", pbuf, NULL, e, true, false, 9, 3597 NULL, NOQID) != EX_OK || Errors > 0)) 3598 { 3599 if (tTd(95, 8)) 3600 { 3601 q = e->e_auth_param; 3602 sm_dprintf("auth=\"%.100s\" not trusted user=\"%.100s\"\n", 3603 pbuf, (q == NULL) ? "" : q); 3604 } 3605 3606 /* not trusted */ 3607 e->e_auth_param = "<>"; 3608 # if _FFR_AUTH_PASSING 3609 macdefine(&BlankEnvelope.e_macro, A_PERM, 3610 macid("{auth_author}"), NULL); 3611 # endif /* _FFR_AUTH_PASSING */ 3612 } 3613 else 3614 { 3615 if (tTd(95, 8)) 3616 sm_dprintf("auth=\"%.100s\" trusted\n", pbuf); 3617 e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, 3618 auth_param); 3619 } 3620 sm_free(auth_param); /* XXX */ 3621 3622 /* reset values */ 3623 Errors = 0; 3624 QuickAbort = saveQuickAbort; 3625 SuprErrs = saveSuprErrs; 3626 ExitStat = saveExitStat; 3627 } 3628 #endif /* SASL */ 3629 #define PRTCHAR(c) ((isascii(c) && isprint(c)) ? (c) : '?') 3630 3631 /* 3632 ** "by" is only accepted if DeliverByMin >= 0. 3633 ** We maybe could add this to the list of server_features. 3634 */ 3635 3636 else if (sm_strcasecmp(kp, "by") == 0 && DeliverByMin >= 0) 3637 { 3638 char *s; 3639 3640 if (vp == NULL) 3641 { 3642 usrerr("501 5.5.2 BY= requires a value"); 3643 /* NOTREACHED */ 3644 } 3645 errno = 0; 3646 e->e_deliver_by = strtol(vp, &s, 10); 3647 if (e->e_deliver_by == LONG_MIN || 3648 e->e_deliver_by == LONG_MAX || 3649 e->e_deliver_by > 999999999l || 3650 e->e_deliver_by < -999999999l) 3651 { 3652 usrerr("501 5.5.2 BY=%s out of range", vp); 3653 /* NOTREACHED */ 3654 } 3655 if (s == NULL || *s != ';') 3656 { 3657 usrerr("501 5.5.2 BY= missing ';'"); 3658 /* NOTREACHED */ 3659 } 3660 e->e_dlvr_flag = 0; 3661 ++s; /* XXX: spaces allowed? */ 3662 SKIP_SPACE(s); 3663 switch (tolower(*s)) 3664 { 3665 case 'n': 3666 e->e_dlvr_flag = DLVR_NOTIFY; 3667 break; 3668 case 'r': 3669 e->e_dlvr_flag = DLVR_RETURN; 3670 if (e->e_deliver_by <= 0) 3671 { 3672 usrerr("501 5.5.4 mode R requires BY time > 0"); 3673 /* NOTREACHED */ 3674 } 3675 if (DeliverByMin > 0 && e->e_deliver_by > 0 && 3676 e->e_deliver_by < DeliverByMin) 3677 { 3678 usrerr("555 5.5.2 time %ld less than %ld", 3679 e->e_deliver_by, (long) DeliverByMin); 3680 /* NOTREACHED */ 3681 } 3682 break; 3683 default: 3684 usrerr("501 5.5.2 illegal by-mode '%c'", PRTCHAR(*s)); 3685 /* NOTREACHED */ 3686 } 3687 ++s; /* XXX: spaces allowed? */ 3688 SKIP_SPACE(s); 3689 switch (tolower(*s)) 3690 { 3691 case 't': 3692 e->e_dlvr_flag |= DLVR_TRACE; 3693 break; 3694 case '\0': 3695 break; 3696 default: 3697 usrerr("501 5.5.2 illegal by-trace '%c'", PRTCHAR(*s)); 3698 /* NOTREACHED */ 3699 } 3700 3701 /* XXX: check whether more characters follow? */ 3702 } 3703 else 3704 { 3705 usrerr("555 5.5.4 %s parameter unrecognized", kp); 3706 /* NOTREACHED */ 3707 } 3708 } 3709 /* 3710 ** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line 3711 ** 3712 ** Parameters: 3713 ** a -- the address corresponding to the To: parameter. 3714 ** kp -- the parameter key. 3715 ** vp -- the value of that parameter. 3716 ** e -- the envelope. 3717 ** 3718 ** Returns: 3719 ** none. 3720 */ 3721 3722 static void 3723 rcpt_esmtp_args(a, kp, vp, e) 3724 ADDRESS *a; 3725 char *kp; 3726 char *vp; 3727 ENVELOPE *e; 3728 { 3729 if (sm_strcasecmp(kp, "notify") == 0) 3730 { 3731 char *p; 3732 3733 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3734 { 3735 usrerr("504 5.7.0 Sorry, NOTIFY not supported, we do not allow DSN"); 3736 /* NOTREACHED */ 3737 } 3738 if (vp == NULL) 3739 { 3740 usrerr("501 5.5.2 NOTIFY requires a value"); 3741 /* NOTREACHED */ 3742 } 3743 a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY); 3744 a->q_flags |= QHASNOTIFY; 3745 macdefine(&e->e_macro, A_TEMP, macid("{dsn_notify}"), vp); 3746 3747 if (sm_strcasecmp(vp, "never") == 0) 3748 return; 3749 for (p = vp; p != NULL; vp = p) 3750 { 3751 p = strchr(p, ','); 3752 if (p != NULL) 3753 *p++ = '\0'; 3754 if (sm_strcasecmp(vp, "success") == 0) 3755 a->q_flags |= QPINGONSUCCESS; 3756 else if (sm_strcasecmp(vp, "failure") == 0) 3757 a->q_flags |= QPINGONFAILURE; 3758 else if (sm_strcasecmp(vp, "delay") == 0) 3759 a->q_flags |= QPINGONDELAY; 3760 else 3761 { 3762 usrerr("501 5.5.4 Bad argument \"%s\" to NOTIFY", 3763 vp); 3764 /* NOTREACHED */ 3765 } 3766 } 3767 } 3768 else if (sm_strcasecmp(kp, "orcpt") == 0) 3769 { 3770 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3771 { 3772 usrerr("504 5.7.0 Sorry, ORCPT not supported, we do not allow DSN"); 3773 /* NOTREACHED */ 3774 } 3775 if (vp == NULL) 3776 { 3777 usrerr("501 5.5.2 ORCPT requires a value"); 3778 /* NOTREACHED */ 3779 } 3780 if (strchr(vp, ';') == NULL || !xtextok(vp)) 3781 { 3782 usrerr("501 5.5.4 Syntax error in ORCPT parameter value"); 3783 /* NOTREACHED */ 3784 } 3785 if (a->q_orcpt != NULL) 3786 { 3787 usrerr("501 5.5.0 Duplicate ORCPT parameter"); 3788 /* NOTREACHED */ 3789 } 3790 a->q_orcpt = sm_rpool_strdup_x(e->e_rpool, vp); 3791 } 3792 else 3793 { 3794 usrerr("555 5.5.4 %s parameter unrecognized", kp); 3795 /* NOTREACHED */ 3796 } 3797 } 3798 /* 3799 ** PRINTVRFYADDR -- print an entry in the verify queue 3800 ** 3801 ** Parameters: 3802 ** a -- the address to print. 3803 ** last -- set if this is the last one. 3804 ** vrfy -- set if this is a VRFY command. 3805 ** 3806 ** Returns: 3807 ** none. 3808 ** 3809 ** Side Effects: 3810 ** Prints the appropriate 250 codes. 3811 */ 3812 #define OFFF (3 + 1 + 5 + 1) /* offset in fmt: SMTP reply + enh. code */ 3813 3814 static void 3815 printvrfyaddr(a, last, vrfy) 3816 register ADDRESS *a; 3817 bool last; 3818 bool vrfy; 3819 { 3820 char fmtbuf[30]; 3821 3822 if (vrfy && a->q_mailer != NULL && 3823 !bitnset(M_VRFY250, a->q_mailer->m_flags)) 3824 (void) sm_strlcpy(fmtbuf, "252", sizeof fmtbuf); 3825 else 3826 (void) sm_strlcpy(fmtbuf, "250", sizeof fmtbuf); 3827 fmtbuf[3] = last ? ' ' : '-'; 3828 (void) sm_strlcpy(&fmtbuf[4], "2.1.5 ", sizeof fmtbuf - 4); 3829 if (a->q_fullname == NULL) 3830 { 3831 if ((a->q_mailer == NULL || 3832 a->q_mailer->m_addrtype == NULL || 3833 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) && 3834 strchr(a->q_user, '@') == NULL) 3835 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s@%s>", 3836 sizeof fmtbuf - OFFF); 3837 else 3838 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s>", 3839 sizeof fmtbuf - OFFF); 3840 message(fmtbuf, a->q_user, MyHostName); 3841 } 3842 else 3843 { 3844 if ((a->q_mailer == NULL || 3845 a->q_mailer->m_addrtype == NULL || 3846 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) && 3847 strchr(a->q_user, '@') == NULL) 3848 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s@%s>", 3849 sizeof fmtbuf - OFFF); 3850 else 3851 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s>", 3852 sizeof fmtbuf - OFFF); 3853 message(fmtbuf, a->q_fullname, a->q_user, MyHostName); 3854 } 3855 } 3856 3857 #if SASL 3858 /* 3859 ** SASLMECHS -- get list of possible AUTH mechanisms 3860 ** 3861 ** Parameters: 3862 ** conn -- SASL connection info. 3863 ** mechlist -- output parameter for list of mechanisms. 3864 ** 3865 ** Returns: 3866 ** number of mechs. 3867 */ 3868 3869 static int 3870 saslmechs(conn, mechlist) 3871 sasl_conn_t *conn; 3872 char **mechlist; 3873 { 3874 int len, num, result; 3875 3876 /* "user" is currently unused */ 3877 # if SASL >= 20000 3878 result = sasl_listmech(conn, NULL, 3879 "", " ", "", (const char **) mechlist, 3880 (unsigned int *)&len, (unsigned int *)&num); 3881 # else /* SASL >= 20000 */ 3882 result = sasl_listmech(conn, "user", /* XXX */ 3883 "", " ", "", mechlist, 3884 (unsigned int *)&len, (unsigned int *)&num); 3885 # endif /* SASL >= 20000 */ 3886 if (result != SASL_OK) 3887 { 3888 if (LogLevel > 9) 3889 sm_syslog(LOG_WARNING, NOQID, 3890 "AUTH error: listmech=%d, num=%d", 3891 result, num); 3892 num = 0; 3893 } 3894 if (num > 0) 3895 { 3896 if (LogLevel > 11) 3897 sm_syslog(LOG_INFO, NOQID, 3898 "AUTH: available mech=%s, allowed mech=%s", 3899 *mechlist, AuthMechanisms); 3900 *mechlist = intersect(AuthMechanisms, *mechlist, NULL); 3901 } 3902 else 3903 { 3904 *mechlist = NULL; /* be paranoid... */ 3905 if (result == SASL_OK && LogLevel > 9) 3906 sm_syslog(LOG_WARNING, NOQID, 3907 "AUTH warning: no mechanisms"); 3908 } 3909 return num; 3910 } 3911 3912 # if SASL >= 20000 3913 /* 3914 ** PROXY_POLICY -- define proxy policy for AUTH 3915 ** 3916 ** Parameters: 3917 ** conn -- unused. 3918 ** context -- unused. 3919 ** requested_user -- authorization identity. 3920 ** rlen -- authorization identity length. 3921 ** auth_identity -- authentication identity. 3922 ** alen -- authentication identity length. 3923 ** def_realm -- default user realm. 3924 ** urlen -- user realm length. 3925 ** propctx -- unused. 3926 ** 3927 ** Returns: 3928 ** ok? 3929 ** 3930 ** Side Effects: 3931 ** sets {auth_authen} macro. 3932 */ 3933 3934 int 3935 proxy_policy(conn, context, requested_user, rlen, auth_identity, alen, 3936 def_realm, urlen, propctx) 3937 sasl_conn_t *conn; 3938 void *context; 3939 const char *requested_user; 3940 unsigned rlen; 3941 const char *auth_identity; 3942 unsigned alen; 3943 const char *def_realm; 3944 unsigned urlen; 3945 struct propctx *propctx; 3946 { 3947 if (auth_identity == NULL) 3948 return SASL_FAIL; 3949 3950 macdefine(&BlankEnvelope.e_macro, A_TEMP, 3951 macid("{auth_authen}"), (char *) auth_identity); 3952 3953 return SASL_OK; 3954 } 3955 # else /* SASL >= 20000 */ 3956 3957 /* 3958 ** PROXY_POLICY -- define proxy policy for AUTH 3959 ** 3960 ** Parameters: 3961 ** context -- unused. 3962 ** auth_identity -- authentication identity. 3963 ** requested_user -- authorization identity. 3964 ** user -- allowed user (output). 3965 ** errstr -- possible error string (output). 3966 ** 3967 ** Returns: 3968 ** ok? 3969 */ 3970 3971 int 3972 proxy_policy(context, auth_identity, requested_user, user, errstr) 3973 void *context; 3974 const char *auth_identity; 3975 const char *requested_user; 3976 const char **user; 3977 const char **errstr; 3978 { 3979 if (user == NULL || auth_identity == NULL) 3980 return SASL_FAIL; 3981 *user = newstr(auth_identity); 3982 return SASL_OK; 3983 } 3984 # endif /* SASL >= 20000 */ 3985 #endif /* SASL */ 3986 3987 #if STARTTLS 3988 /* 3989 ** INITSRVTLS -- initialize server side TLS 3990 ** 3991 ** Parameters: 3992 ** tls_ok -- should tls initialization be done? 3993 ** 3994 ** Returns: 3995 ** succeeded? 3996 ** 3997 ** Side Effects: 3998 ** sets tls_ok_srv which is a static variable in this module. 3999 ** Do NOT remove assignments to it! 4000 */ 4001 4002 bool 4003 initsrvtls(tls_ok) 4004 bool tls_ok; 4005 { 4006 if (!tls_ok) 4007 return false; 4008 4009 /* do NOT remove assignment */ 4010 tls_ok_srv = inittls(&srv_ctx, TLS_Srv_Opts, true, SrvCERTfile, 4011 Srvkeyfile, CACERTpath, CACERTfile, DHParams); 4012 return tls_ok_srv; 4013 } 4014 #endif /* STARTTLS */ 4015 /* 4016 ** SRVFEATURES -- get features for SMTP server 4017 ** 4018 ** Parameters: 4019 ** e -- envelope (should be session context). 4020 ** clientname -- name of client. 4021 ** features -- default features for this invocation. 4022 ** 4023 ** Returns: 4024 ** server features. 4025 */ 4026 4027 /* table with options: it uses just one character, how about strings? */ 4028 static struct 4029 { 4030 char srvf_opt; 4031 unsigned int srvf_flag; 4032 } srv_feat_table[] = 4033 { 4034 { 'A', SRV_OFFER_AUTH }, 4035 { 'B', SRV_OFFER_VERB }, 4036 { 'D', SRV_OFFER_DSN }, 4037 { 'E', SRV_OFFER_ETRN }, 4038 { 'L', SRV_REQ_AUTH }, /* not documented in 8.12 */ 4039 #if PIPELINING 4040 # if _FFR_NO_PIPE 4041 { 'N', SRV_NO_PIPE }, 4042 # endif /* _FFR_NO_PIPE */ 4043 { 'P', SRV_OFFER_PIPE }, 4044 #endif /* PIPELINING */ 4045 { 'R', SRV_VRFY_CLT }, 4046 { 'S', SRV_OFFER_TLS }, 4047 /* { 'T', SRV_TMP_FAIL }, */ 4048 { 'V', SRV_VRFY_CLT }, 4049 { 'X', SRV_OFFER_EXPN }, 4050 /* { 'Y', SRV_OFFER_VRFY }, */ 4051 { '\0', SRV_NONE } 4052 }; 4053 4054 static unsigned int 4055 srvfeatures(e, clientname, features) 4056 ENVELOPE *e; 4057 char *clientname; 4058 unsigned int features; 4059 { 4060 int r, i, j; 4061 char **pvp, c, opt; 4062 char pvpbuf[PSBUFSIZE]; 4063 4064 pvp = NULL; 4065 r = rscap("srv_features", clientname, "", e, &pvp, pvpbuf, 4066 sizeof(pvpbuf)); 4067 if (r != EX_OK) 4068 return features; 4069 if (pvp == NULL || pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 4070 return features; 4071 if (pvp[1] != NULL && sm_strncasecmp(pvp[1], "temp", 4) == 0) 4072 return SRV_TMP_FAIL; 4073 4074 /* 4075 ** General rule (see sendmail.h, d_flags): 4076 ** lower case: required/offered, upper case: Not required/available 4077 ** 4078 ** Since we can change some features per daemon, we have both 4079 ** cases here: turn on/off a feature. 4080 */ 4081 4082 for (i = 1; pvp[i] != NULL; i++) 4083 { 4084 c = pvp[i][0]; 4085 j = 0; 4086 for (;;) 4087 { 4088 if ((opt = srv_feat_table[j].srvf_opt) == '\0') 4089 { 4090 if (LogLevel > 9) 4091 sm_syslog(LOG_WARNING, e->e_id, 4092 "srvfeatures: unknown feature %s", 4093 pvp[i]); 4094 break; 4095 } 4096 if (c == opt) 4097 { 4098 features &= ~(srv_feat_table[j].srvf_flag); 4099 break; 4100 } 4101 if (c == tolower(opt)) 4102 { 4103 features |= srv_feat_table[j].srvf_flag; 4104 break; 4105 } 4106 ++j; 4107 } 4108 } 4109 return features; 4110 } 4111 4112 /* 4113 ** HELP -- implement the HELP command. 4114 ** 4115 ** Parameters: 4116 ** topic -- the topic we want help for. 4117 ** e -- envelope. 4118 ** 4119 ** Returns: 4120 ** none. 4121 ** 4122 ** Side Effects: 4123 ** outputs the help file to message output. 4124 */ 4125 #define HELPVSTR "#vers " 4126 #define HELPVERSION 2 4127 4128 void 4129 help(topic, e) 4130 char *topic; 4131 ENVELOPE *e; 4132 { 4133 register SM_FILE_T *hf; 4134 register char *p; 4135 int len; 4136 bool noinfo; 4137 bool first = true; 4138 long sff = SFF_OPENASROOT|SFF_REGONLY; 4139 char buf[MAXLINE]; 4140 char inp[MAXLINE]; 4141 static int foundvers = -1; 4142 extern char Version[]; 4143 4144 if (DontLockReadFiles) 4145 sff |= SFF_NOLOCK; 4146 if (!bitnset(DBS_HELPFILEINUNSAFEDIRPATH, DontBlameSendmail)) 4147 sff |= SFF_SAFEDIRPATH; 4148 4149 if (HelpFile == NULL || 4150 (hf = safefopen(HelpFile, O_RDONLY, 0444, sff)) == NULL) 4151 { 4152 /* no help */ 4153 errno = 0; 4154 message("502 5.3.0 Sendmail %s -- HELP not implemented", 4155 Version); 4156 return; 4157 } 4158 4159 if (topic == NULL || *topic == '\0') 4160 { 4161 topic = "smtp"; 4162 noinfo = false; 4163 } 4164 else 4165 { 4166 makelower(topic); 4167 noinfo = true; 4168 } 4169 4170 len = strlen(topic); 4171 4172 while (sm_io_fgets(hf, SM_TIME_DEFAULT, buf, sizeof buf) != NULL) 4173 { 4174 if (buf[0] == '#') 4175 { 4176 if (foundvers < 0 && 4177 strncmp(buf, HELPVSTR, strlen(HELPVSTR)) == 0) 4178 { 4179 int h; 4180 4181 if (sm_io_sscanf(buf + strlen(HELPVSTR), "%d", 4182 &h) == 1) 4183 foundvers = h; 4184 } 4185 continue; 4186 } 4187 if (strncmp(buf, topic, len) == 0) 4188 { 4189 if (first) 4190 { 4191 first = false; 4192 4193 /* print version if no/old vers# in file */ 4194 if (foundvers < 2 && !noinfo) 4195 message("214-2.0.0 This is Sendmail version %s", Version); 4196 } 4197 p = strpbrk(buf, " \t"); 4198 if (p == NULL) 4199 p = buf + strlen(buf) - 1; 4200 else 4201 p++; 4202 fixcrlf(p, true); 4203 if (foundvers >= 2) 4204 { 4205 translate_dollars(p); 4206 expand(p, inp, sizeof inp, e); 4207 p = inp; 4208 } 4209 message("214-2.0.0 %s", p); 4210 noinfo = false; 4211 } 4212 } 4213 4214 if (noinfo) 4215 message("504 5.3.0 HELP topic \"%.10s\" unknown", topic); 4216 else 4217 message("214 2.0.0 End of HELP info"); 4218 4219 if (foundvers != 0 && foundvers < HELPVERSION) 4220 { 4221 if (LogLevel > 1) 4222 sm_syslog(LOG_WARNING, e->e_id, 4223 "%s too old (require version %d)", 4224 HelpFile, HELPVERSION); 4225 4226 /* avoid log next time */ 4227 foundvers = 0; 4228 } 4229 4230 (void) sm_io_close(hf, SM_TIME_DEFAULT); 4231 } 4232