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.827 2002/05/28 14:29:57 ca 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 && bitset(SRV_OFFER_TLS, features)) 1900 message("250-STARTTLS"); 1901 #endif /* STARTTLS */ 1902 if (DeliverByMin > 0) 1903 message("250-DELIVERBY %ld", 1904 (long) DeliverByMin); 1905 else if (DeliverByMin == 0) 1906 message("250-DELIVERBY"); 1907 1908 /* < 0: no deliver-by */ 1909 1910 message("250 HELP"); 1911 break; 1912 1913 case CMDMAIL: /* mail -- designate sender */ 1914 SmtpPhase = "server MAIL"; 1915 DELAY_CONN("MAIL"); 1916 1917 /* check for validity of this command */ 1918 if (!gothello && bitset(PRIV_NEEDMAILHELO, PrivacyFlags)) 1919 { 1920 usrerr("503 5.0.0 Polite people say HELO first"); 1921 break; 1922 } 1923 if (smtp.sm_gotmail) 1924 { 1925 usrerr("503 5.5.0 Sender already specified"); 1926 break; 1927 } 1928 #if SASL 1929 if (bitset(SRV_REQ_AUTH, features) && 1930 authenticating != SASL_IS_AUTH) 1931 { 1932 usrerr("530 5.7.0 Authentication required"); 1933 break; 1934 } 1935 #endif /* SASL */ 1936 1937 p = skipword(p, "from"); 1938 if (p == NULL) 1939 break; 1940 if (tempfail) 1941 { 1942 if (LogLevel > 9) 1943 sm_syslog(LOG_INFO, e->e_id, 1944 "SMTP MAIL command (%.100s) from %.100s tempfailed (due to previous checks)", 1945 p, CurSmtpClient); 1946 usrerr(MSG_TEMPFAIL); 1947 break; 1948 } 1949 1950 /* make sure we know who the sending host is */ 1951 if (sendinghost == NULL) 1952 sendinghost = peerhostname; 1953 1954 1955 #if SM_HEAP_CHECK 1956 if (sm_debug_active(&DebugLeakSmtp, 1)) 1957 { 1958 sm_heap_newgroup(); 1959 sm_dprintf("smtp() heap group #%d\n", 1960 sm_heap_group()); 1961 } 1962 #endif /* SM_HEAP_CHECK */ 1963 1964 if (Errors > 0) 1965 goto undo_no_pm; 1966 if (!gothello) 1967 { 1968 auth_warning(e, "%s didn't use HELO protocol", 1969 CurSmtpClient); 1970 } 1971 #ifdef PICKY_HELO_CHECK 1972 if (sm_strcasecmp(sendinghost, peerhostname) != 0 && 1973 (sm_strcasecmp(peerhostname, "localhost") != 0 || 1974 sm_strcasecmp(sendinghost, MyHostName) != 0)) 1975 { 1976 auth_warning(e, "Host %s claimed to be %s", 1977 CurSmtpClient, sendinghost); 1978 } 1979 #endif /* PICKY_HELO_CHECK */ 1980 1981 if (protocol == NULL) 1982 protocol = "SMTP"; 1983 macdefine(&e->e_macro, A_PERM, 'r', protocol); 1984 macdefine(&e->e_macro, A_PERM, 's', sendinghost); 1985 1986 if (Errors > 0) 1987 goto undo_no_pm; 1988 smtp.sm_nrcpts = 0; 1989 n_badrcpts = 0; 1990 macdefine(&e->e_macro, A_PERM, macid("{ntries}"), "0"); 1991 macdefine(&e->e_macro, A_PERM, macid("{nrcpts}"), "0"); 1992 e->e_flags |= EF_CLRQUEUE; 1993 sm_setproctitle(true, e, "%s %s: %.80s", 1994 qid_printname(e), 1995 CurSmtpClient, inp); 1996 1997 /* do the processing */ 1998 SM_TRY 1999 { 2000 extern char *FullName; 2001 2002 QuickAbort = true; 2003 SM_FREE_CLR(FullName); 2004 2005 /* must parse sender first */ 2006 delimptr = NULL; 2007 setsender(p, e, &delimptr, ' ', false); 2008 if (delimptr != NULL && *delimptr != '\0') 2009 *delimptr++ = '\0'; 2010 if (Errors > 0) 2011 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2012 2013 /* Successfully set e_from, allow logging */ 2014 e->e_flags |= EF_LOGSENDER; 2015 2016 /* put resulting triple from parseaddr() into macros */ 2017 if (e->e_from.q_mailer != NULL) 2018 macdefine(&e->e_macro, A_PERM, 2019 macid("{mail_mailer}"), 2020 e->e_from.q_mailer->m_name); 2021 else 2022 macdefine(&e->e_macro, A_PERM, 2023 macid("{mail_mailer}"), NULL); 2024 if (e->e_from.q_host != NULL) 2025 macdefine(&e->e_macro, A_PERM, 2026 macid("{mail_host}"), 2027 e->e_from.q_host); 2028 else 2029 macdefine(&e->e_macro, A_PERM, 2030 macid("{mail_host}"), "localhost"); 2031 if (e->e_from.q_user != NULL) 2032 macdefine(&e->e_macro, A_PERM, 2033 macid("{mail_addr}"), 2034 e->e_from.q_user); 2035 else 2036 macdefine(&e->e_macro, A_PERM, 2037 macid("{mail_addr}"), NULL); 2038 if (Errors > 0) 2039 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2040 2041 /* check for possible spoofing */ 2042 if (RealUid != 0 && OpMode == MD_SMTP && 2043 !wordinclass(RealUserName, 't') && 2044 (!bitnset(M_LOCALMAILER, 2045 e->e_from.q_mailer->m_flags) || 2046 strcmp(e->e_from.q_user, RealUserName) != 0)) 2047 { 2048 auth_warning(e, "%s owned process doing -bs", 2049 RealUserName); 2050 } 2051 2052 /* now parse ESMTP arguments */ 2053 e->e_msgsize = 0; 2054 addr = p; 2055 argno = 0; 2056 args[argno++] = p; 2057 p = delimptr; 2058 while (p != NULL && *p != '\0') 2059 { 2060 char *kp; 2061 char *vp = NULL; 2062 char *equal = NULL; 2063 2064 /* locate the beginning of the keyword */ 2065 SKIP_SPACE(p); 2066 if (*p == '\0') 2067 break; 2068 kp = p; 2069 2070 /* skip to the value portion */ 2071 while ((isascii(*p) && isalnum(*p)) || *p == '-') 2072 p++; 2073 if (*p == '=') 2074 { 2075 equal = p; 2076 *p++ = '\0'; 2077 vp = p; 2078 2079 /* skip to the end of the value */ 2080 while (*p != '\0' && *p != ' ' && 2081 !(isascii(*p) && iscntrl(*p)) && 2082 *p != '=') 2083 p++; 2084 } 2085 2086 if (*p != '\0') 2087 *p++ = '\0'; 2088 2089 if (tTd(19, 1)) 2090 sm_dprintf("MAIL: got arg %s=\"%s\"\n", kp, 2091 vp == NULL ? "<null>" : vp); 2092 2093 mail_esmtp_args(kp, vp, e); 2094 if (equal != NULL) 2095 *equal = '='; 2096 args[argno++] = kp; 2097 if (argno >= MAXSMTPARGS - 1) 2098 usrerr("501 5.5.4 Too many parameters"); 2099 if (Errors > 0) 2100 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2101 } 2102 args[argno] = NULL; 2103 if (Errors > 0) 2104 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2105 2106 #if SASL 2107 # if _FFR_AUTH_PASSING 2108 /* set the default AUTH= if the sender didn't */ 2109 if (e->e_auth_param == NULL) 2110 { 2111 /* XXX only do this for an MSA? */ 2112 e->e_auth_param = macvalue(macid("{auth_authen}"), 2113 e); 2114 if (e->e_auth_param == NULL) 2115 e->e_auth_param = "<>"; 2116 2117 /* 2118 ** XXX should we invoke Strust_auth now? 2119 ** authorizing as the client that just 2120 ** authenticated, so we'll trust implicitly 2121 */ 2122 } 2123 # endif /* _FFR_AUTH_PASSING */ 2124 #endif /* SASL */ 2125 2126 /* do config file checking of the sender */ 2127 macdefine(&e->e_macro, A_PERM, 2128 macid("{addr_type}"), "e s"); 2129 #if _FFR_MAIL_MACRO 2130 /* make the "real" sender address available */ 2131 macdefine(&e->e_macro, A_TEMP, macid("{mail_from}"), 2132 e->e_from.q_paddr); 2133 #endif /* _FFR_MAIL_MACRO */ 2134 if (rscheck("check_mail", addr, 2135 NULL, e, true, true, 3, NULL, 2136 e->e_id) != EX_OK || 2137 Errors > 0) 2138 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2139 macdefine(&e->e_macro, A_PERM, 2140 macid("{addr_type}"), NULL); 2141 2142 if (MaxMessageSize > 0 && 2143 (e->e_msgsize > MaxMessageSize || 2144 e->e_msgsize < 0)) 2145 { 2146 usrerr("552 5.2.3 Message size exceeds fixed maximum message size (%ld)", 2147 MaxMessageSize); 2148 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2149 } 2150 2151 /* 2152 ** XXX always check whether there is at least one fs 2153 ** with enough space? 2154 ** However, this may not help much: the queue group 2155 ** selection may later on select a FS that hasn't 2156 ** enough space. 2157 */ 2158 2159 if ((NumFileSys == 1 || NumQueue == 1) && 2160 !enoughdiskspace(e->e_msgsize, e) 2161 #if _FFR_ANY_FREE_FS 2162 && !filesys_free(e->e_msgsize) 2163 #endif /* _FFR_ANY_FREE_FS */ 2164 ) 2165 { 2166 /* 2167 ** We perform this test again when the 2168 ** queue directory is selected, in collect. 2169 */ 2170 2171 usrerr("452 4.4.5 Insufficient disk space; try again later"); 2172 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2173 } 2174 if (Errors > 0) 2175 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2176 2177 LogUsrErrs = true; 2178 #if MILTER 2179 if (smtp.sm_milterlist && smtp.sm_milterize && 2180 !bitset(EF_DISCARD, e->e_flags)) 2181 { 2182 char state; 2183 char *response; 2184 2185 response = milter_envfrom(args, e, &state); 2186 MILTER_REPLY("from"); 2187 } 2188 #endif /* MILTER */ 2189 if (Errors > 0) 2190 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2191 2192 message("250 2.1.0 Sender ok"); 2193 smtp.sm_gotmail = true; 2194 } 2195 SM_EXCEPT(exc, "[!F]*") 2196 { 2197 /* 2198 ** An error occurred while processing a MAIL command. 2199 ** Jump to the common error handling code. 2200 */ 2201 2202 sm_exc_free(exc); 2203 goto undo_no_pm; 2204 } 2205 SM_END_TRY 2206 break; 2207 2208 undo_no_pm: 2209 e->e_flags &= ~EF_PM_NOTIFY; 2210 undo: 2211 break; 2212 2213 case CMDRCPT: /* rcpt -- designate recipient */ 2214 DELAY_CONN("RCPT"); 2215 if (!smtp.sm_gotmail) 2216 { 2217 usrerr("503 5.0.0 Need MAIL before RCPT"); 2218 break; 2219 } 2220 SmtpPhase = "server RCPT"; 2221 SM_TRY 2222 { 2223 QuickAbort = true; 2224 LogUsrErrs = true; 2225 2226 /* limit flooding of our machine */ 2227 if (MaxRcptPerMsg > 0 && 2228 smtp.sm_nrcpts >= MaxRcptPerMsg) 2229 { 2230 /* sleep(1); / * slow down? */ 2231 usrerr("452 4.5.3 Too many recipients"); 2232 goto rcpt_done; 2233 } 2234 2235 if (e->e_sendmode != SM_DELIVER) 2236 e->e_flags |= EF_VRFYONLY; 2237 2238 #if MILTER 2239 /* 2240 ** If the filter will be deleting recipients, 2241 ** don't expand them at RCPT time (in the call 2242 ** to recipient()). If they are expanded, it 2243 ** is impossible for removefromlist() to figure 2244 ** out the expanded members of the original 2245 ** recipient and mark them as QS_DONTSEND. 2246 */ 2247 2248 if (milter_can_delrcpts()) 2249 e->e_flags |= EF_VRFYONLY; 2250 #endif /* MILTER */ 2251 2252 p = skipword(p, "to"); 2253 if (p == NULL) 2254 goto rcpt_done; 2255 macdefine(&e->e_macro, A_PERM, 2256 macid("{addr_type}"), "e r"); 2257 a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr, 2258 e, true); 2259 macdefine(&e->e_macro, A_PERM, 2260 macid("{addr_type}"), NULL); 2261 if (BadRcptThrottle > 0 && 2262 n_badrcpts >= BadRcptThrottle) 2263 { 2264 if (LogLevel > 5 && 2265 n_badrcpts == BadRcptThrottle) 2266 { 2267 sm_syslog(LOG_INFO, e->e_id, 2268 "%.100s: Possible SMTP RCPT flood, throttling.", 2269 CurSmtpClient); 2270 2271 /* To avoid duplicated message */ 2272 n_badrcpts++; 2273 } 2274 2275 /* 2276 ** Don't use exponential backoff for now. 2277 ** Some servers will open more connections 2278 ** and actually overload the receiver even 2279 ** more. 2280 */ 2281 2282 (void) sleep(1); 2283 } 2284 if (Errors > 0) 2285 goto rcpt_done; 2286 if (a == NULL) 2287 { 2288 usrerr("501 5.0.0 Missing recipient"); 2289 goto rcpt_done; 2290 } 2291 2292 if (delimptr != NULL && *delimptr != '\0') 2293 *delimptr++ = '\0'; 2294 2295 /* put resulting triple from parseaddr() into macros */ 2296 if (a->q_mailer != NULL) 2297 macdefine(&e->e_macro, A_PERM, 2298 macid("{rcpt_mailer}"), 2299 a->q_mailer->m_name); 2300 else 2301 macdefine(&e->e_macro, A_PERM, 2302 macid("{rcpt_mailer}"), NULL); 2303 if (a->q_host != NULL) 2304 macdefine(&e->e_macro, A_PERM, 2305 macid("{rcpt_host}"), a->q_host); 2306 else 2307 macdefine(&e->e_macro, A_PERM, 2308 macid("{rcpt_host}"), "localhost"); 2309 if (a->q_user != NULL) 2310 macdefine(&e->e_macro, A_PERM, 2311 macid("{rcpt_addr}"), a->q_user); 2312 else 2313 macdefine(&e->e_macro, A_PERM, 2314 macid("{rcpt_addr}"), NULL); 2315 if (Errors > 0) 2316 goto rcpt_done; 2317 2318 /* now parse ESMTP arguments */ 2319 addr = p; 2320 argno = 0; 2321 args[argno++] = p; 2322 p = delimptr; 2323 while (p != NULL && *p != '\0') 2324 { 2325 char *kp; 2326 char *vp = NULL; 2327 char *equal = NULL; 2328 2329 /* locate the beginning of the keyword */ 2330 SKIP_SPACE(p); 2331 if (*p == '\0') 2332 break; 2333 kp = p; 2334 2335 /* skip to the value portion */ 2336 while ((isascii(*p) && isalnum(*p)) || *p == '-') 2337 p++; 2338 if (*p == '=') 2339 { 2340 equal = p; 2341 *p++ = '\0'; 2342 vp = p; 2343 2344 /* skip to the end of the value */ 2345 while (*p != '\0' && *p != ' ' && 2346 !(isascii(*p) && iscntrl(*p)) && 2347 *p != '=') 2348 p++; 2349 } 2350 2351 if (*p != '\0') 2352 *p++ = '\0'; 2353 2354 if (tTd(19, 1)) 2355 sm_dprintf("RCPT: got arg %s=\"%s\"\n", kp, 2356 vp == NULL ? "<null>" : vp); 2357 2358 rcpt_esmtp_args(a, kp, vp, e); 2359 if (equal != NULL) 2360 *equal = '='; 2361 args[argno++] = kp; 2362 if (argno >= MAXSMTPARGS - 1) 2363 usrerr("501 5.5.4 Too many parameters"); 2364 if (Errors > 0) 2365 break; 2366 } 2367 args[argno] = NULL; 2368 if (Errors > 0) 2369 goto rcpt_done; 2370 2371 /* do config file checking of the recipient */ 2372 macdefine(&e->e_macro, A_PERM, 2373 macid("{addr_type}"), "e r"); 2374 if (rscheck("check_rcpt", addr, 2375 NULL, e, true, true, 3, NULL, 2376 e->e_id) != EX_OK || 2377 Errors > 0) 2378 goto rcpt_done; 2379 macdefine(&e->e_macro, A_PERM, 2380 macid("{addr_type}"), NULL); 2381 2382 #if MILTER 2383 if (smtp.sm_milterlist && smtp.sm_milterize && 2384 !bitset(EF_DISCARD, e->e_flags)) 2385 { 2386 char state; 2387 char *response; 2388 2389 response = milter_envrcpt(args, e, &state); 2390 MILTER_REPLY("to"); 2391 } 2392 #endif /* MILTER */ 2393 2394 macdefine(&e->e_macro, A_PERM, 2395 macid("{rcpt_mailer}"), NULL); 2396 macdefine(&e->e_macro, A_PERM, 2397 macid("{rcpt_host}"), NULL); 2398 macdefine(&e->e_macro, A_PERM, 2399 macid("{rcpt_addr}"), NULL); 2400 macdefine(&e->e_macro, A_PERM, 2401 macid("{dsn_notify}"), NULL); 2402 if (Errors > 0) 2403 goto rcpt_done; 2404 2405 /* save in recipient list after ESMTP mods */ 2406 a = recipient(a, &e->e_sendqueue, 0, e); 2407 if (Errors > 0) 2408 goto rcpt_done; 2409 2410 /* no errors during parsing, but might be a duplicate */ 2411 e->e_to = a->q_paddr; 2412 if (!QS_IS_BADADDR(a->q_state)) 2413 { 2414 if (smtp.sm_nrcpts == 0) 2415 initsys(e); 2416 message("250 2.1.5 Recipient ok%s", 2417 QS_IS_QUEUEUP(a->q_state) ? 2418 " (will queue)" : ""); 2419 smtp.sm_nrcpts++; 2420 } 2421 else 2422 { 2423 /* punt -- should keep message in ADDRESS.... */ 2424 usrerr("550 5.1.1 Addressee unknown"); 2425 } 2426 rcpt_done: 2427 if (Errors > 0) 2428 ++n_badrcpts; 2429 } 2430 SM_EXCEPT(exc, "[!F]*") 2431 { 2432 /* An exception occurred while processing RCPT */ 2433 e->e_flags &= ~(EF_FATALERRS|EF_PM_NOTIFY); 2434 ++n_badrcpts; 2435 } 2436 SM_END_TRY 2437 break; 2438 2439 case CMDDATA: /* data -- text of mail */ 2440 DELAY_CONN("DATA"); 2441 smtp_data(&smtp, e); 2442 break; 2443 2444 case CMDRSET: /* rset -- reset state */ 2445 if (tTd(94, 100)) 2446 message("451 4.0.0 Test failure"); 2447 else 2448 message("250 2.0.0 Reset state"); 2449 CLEAR_STATE(cmdbuf); 2450 #if _FFR_QUARANTINE 2451 /* restore connection quarantining */ 2452 if (smtp.sm_quarmsg == NULL) 2453 { 2454 e->e_quarmsg = NULL; 2455 macdefine(&e->e_macro, A_PERM, 2456 macid("{quarantine}"), ""); 2457 } 2458 else 2459 { 2460 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, 2461 smtp.sm_quarmsg); 2462 macdefine(&e->e_macro, A_PERM, 2463 macid("{quarantine}"), e->e_quarmsg); 2464 } 2465 #endif /* _FFR_QUARANTINE */ 2466 break; 2467 2468 case CMDVRFY: /* vrfy -- verify address */ 2469 case CMDEXPN: /* expn -- expand address */ 2470 vrfy = c->cmd_code == CMDVRFY; 2471 DELAY_CONN(vrfy ? "VRFY" : "EXPN"); 2472 if (tempfail) 2473 { 2474 if (LogLevel > 9) 2475 sm_syslog(LOG_INFO, e->e_id, 2476 "SMTP %s command (%.100s) from %.100s tempfailed (due to previous checks)", 2477 vrfy ? "VRFY" : "EXPN", 2478 p, CurSmtpClient); 2479 2480 /* RFC 821 doesn't allow 4xy reply code */ 2481 usrerr("550 5.7.1 Please try again later"); 2482 break; 2483 } 2484 wt = checksmtpattack(&n_verifies, MAXVRFYCOMMANDS, 2485 false, vrfy ? "VRFY" : "EXPN", e); 2486 previous = curtime(); 2487 if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 2488 PrivacyFlags)) 2489 { 2490 if (vrfy) 2491 message("252 2.5.2 Cannot VRFY user; try RCPT to attempt delivery (or try finger)"); 2492 else 2493 message("502 5.7.0 Sorry, we do not allow this operation"); 2494 if (LogLevel > 5) 2495 sm_syslog(LOG_INFO, e->e_id, 2496 "%.100s: %s [rejected]", 2497 CurSmtpClient, 2498 shortenstring(inp, MAXSHORTSTR)); 2499 break; 2500 } 2501 else if (!gothello && 2502 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 2503 PrivacyFlags)) 2504 { 2505 usrerr("503 5.0.0 I demand that you introduce yourself first"); 2506 break; 2507 } 2508 if (Errors > 0) 2509 break; 2510 if (LogLevel > 5) 2511 sm_syslog(LOG_INFO, e->e_id, "%.100s: %s", 2512 CurSmtpClient, 2513 shortenstring(inp, MAXSHORTSTR)); 2514 SM_TRY 2515 { 2516 QuickAbort = true; 2517 vrfyqueue = NULL; 2518 if (vrfy) 2519 e->e_flags |= EF_VRFYONLY; 2520 while (*p != '\0' && isascii(*p) && isspace(*p)) 2521 p++; 2522 if (*p == '\0') 2523 { 2524 usrerr("501 5.5.2 Argument required"); 2525 } 2526 else 2527 { 2528 /* do config file checking of the address */ 2529 if (rscheck(vrfy ? "check_vrfy" : "check_expn", 2530 p, NULL, e, true, false, 3, NULL, 2531 NOQID) != EX_OK || 2532 Errors > 0) 2533 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2534 (void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e); 2535 } 2536 if (wt > 0) 2537 { 2538 time_t t; 2539 2540 t = wt - (curtime() - previous); 2541 if (t > 0) 2542 (void) sleep(t); 2543 } 2544 if (Errors > 0) 2545 sm_exc_raisenew_x(&EtypeQuickAbort, 1); 2546 if (vrfyqueue == NULL) 2547 { 2548 usrerr("554 5.5.2 Nothing to %s", vrfy ? "VRFY" : "EXPN"); 2549 } 2550 while (vrfyqueue != NULL) 2551 { 2552 if (!QS_IS_UNDELIVERED(vrfyqueue->q_state)) 2553 { 2554 vrfyqueue = vrfyqueue->q_next; 2555 continue; 2556 } 2557 2558 /* see if there is more in the vrfy list */ 2559 a = vrfyqueue; 2560 while ((a = a->q_next) != NULL && 2561 (!QS_IS_UNDELIVERED(a->q_state))) 2562 continue; 2563 printvrfyaddr(vrfyqueue, a == NULL, vrfy); 2564 vrfyqueue = a; 2565 } 2566 } 2567 SM_EXCEPT(exc, "[!F]*") 2568 { 2569 /* 2570 ** An exception occurred while processing VRFY/EXPN 2571 */ 2572 2573 sm_exc_free(exc); 2574 goto undo; 2575 } 2576 SM_END_TRY 2577 break; 2578 2579 case CMDETRN: /* etrn -- force queue flush */ 2580 DELAY_CONN("ETRN"); 2581 2582 /* Don't leak queue information via debug flags */ 2583 if (!bitset(SRV_OFFER_ETRN, features) || UseMSP || 2584 (RealUid != 0 && RealUid != TrustedUid && 2585 OpMode == MD_SMTP)) 2586 { 2587 /* different message for MSA ? */ 2588 message("502 5.7.0 Sorry, we do not allow this operation"); 2589 if (LogLevel > 5) 2590 sm_syslog(LOG_INFO, e->e_id, 2591 "%.100s: %s [rejected]", 2592 CurSmtpClient, 2593 shortenstring(inp, MAXSHORTSTR)); 2594 break; 2595 } 2596 if (tempfail) 2597 { 2598 if (LogLevel > 9) 2599 sm_syslog(LOG_INFO, e->e_id, 2600 "SMTP ETRN command (%.100s) from %.100s tempfailed (due to previous checks)", 2601 p, CurSmtpClient); 2602 usrerr(MSG_TEMPFAIL); 2603 break; 2604 } 2605 2606 if (strlen(p) <= 0) 2607 { 2608 usrerr("500 5.5.2 Parameter required"); 2609 break; 2610 } 2611 2612 /* crude way to avoid denial-of-service attacks */ 2613 (void) checksmtpattack(&n_etrn, MAXETRNCOMMANDS, true, 2614 "ETRN", e); 2615 2616 /* 2617 ** Do config file checking of the parameter. 2618 ** Even though we have srv_features now, we still 2619 ** need this ruleset because the former is called 2620 ** when the connection has been established, while 2621 ** this ruleset is called when the command is 2622 ** actually issued and therefore has all information 2623 ** available to make a decision. 2624 */ 2625 2626 if (rscheck("check_etrn", p, NULL, e, true, false, 3, 2627 NULL, NOQID) != EX_OK || Errors > 0) 2628 break; 2629 2630 if (LogLevel > 5) 2631 sm_syslog(LOG_INFO, e->e_id, 2632 "%.100s: ETRN %s", CurSmtpClient, 2633 shortenstring(p, MAXSHORTSTR)); 2634 2635 id = p; 2636 if (*id == '#') 2637 { 2638 int wgrp; 2639 2640 id++; 2641 wgrp = name2qid(id); 2642 if (!ISVALIDQGRP(wgrp)) 2643 { 2644 usrerr("459 4.5.4 Queue %s unknown", 2645 id); 2646 break; 2647 } 2648 ok = run_work_group(wgrp, true, false, 2649 false, true); 2650 if (ok && Errors == 0) 2651 message("250 2.0.0 Queuing for queue group %s started", id); 2652 break; 2653 } 2654 2655 if (*id == '@') 2656 id++; 2657 else 2658 *--id = '@'; 2659 2660 new = (QUEUE_CHAR *) sm_malloc(sizeof(QUEUE_CHAR)); 2661 if (new == NULL) 2662 { 2663 syserr("500 5.5.0 ETRN out of memory"); 2664 break; 2665 } 2666 new->queue_match = id; 2667 new->queue_negate = false; 2668 new->queue_next = NULL; 2669 QueueLimitRecipient = new; 2670 ok = runqueue(true, false, false, true); 2671 sm_free(QueueLimitRecipient); /* XXX */ 2672 QueueLimitRecipient = NULL; 2673 if (ok && Errors == 0) 2674 message("250 2.0.0 Queuing for node %s started", p); 2675 break; 2676 2677 case CMDHELP: /* help -- give user info */ 2678 DELAY_CONN("HELP"); 2679 help(p, e); 2680 break; 2681 2682 case CMDNOOP: /* noop -- do nothing */ 2683 DELAY_CONN("NOOP"); 2684 (void) checksmtpattack(&n_noop, MAXNOOPCOMMANDS, true, 2685 "NOOP", e); 2686 message("250 2.0.0 OK"); 2687 break; 2688 2689 case CMDQUIT: /* quit -- leave mail */ 2690 message("221 2.0.0 %s closing connection", MyHostName); 2691 #if PIPELINING 2692 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT); 2693 #endif /* PIPELINING */ 2694 2695 if (smtp.sm_nrcpts > 0) 2696 logundelrcpts(e, "aborted by sender", 9, false); 2697 2698 /* arrange to ignore any current send list */ 2699 e->e_sendqueue = NULL; 2700 2701 #if STARTTLS 2702 /* shutdown TLS connection */ 2703 if (tls_active) 2704 { 2705 (void) endtls(srv_ssl, "server"); 2706 tls_active = false; 2707 } 2708 #endif /* STARTTLS */ 2709 #if SASL 2710 if (authenticating == SASL_IS_AUTH) 2711 { 2712 sasl_dispose(&conn); 2713 authenticating = SASL_NOT_AUTH; 2714 /* XXX sasl_done(); this is a child */ 2715 } 2716 #endif /* SASL */ 2717 2718 doquit: 2719 /* avoid future 050 messages */ 2720 disconnect(1, e); 2721 2722 #if MILTER 2723 /* close out milter filters */ 2724 milter_quit(e); 2725 #endif /* MILTER */ 2726 2727 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 2728 logsender(e, NULL); 2729 e->e_flags &= ~EF_LOGSENDER; 2730 2731 if (lognullconnection && LogLevel > 5 && 2732 nullserver == NULL) 2733 { 2734 char *d; 2735 2736 d = macvalue(macid("{daemon_name}"), e); 2737 if (d == NULL) 2738 d = "stdin"; 2739 2740 /* 2741 ** even though this id is "bogus", it makes 2742 ** it simpler to "grep" related events, e.g., 2743 ** timeouts for the same connection. 2744 */ 2745 2746 sm_syslog(LOG_INFO, e->e_id, 2747 "%.100s did not issue MAIL/EXPN/VRFY/ETRN during connection to %s", 2748 CurSmtpClient, d); 2749 } 2750 #if PROFILING 2751 return; 2752 #endif /* PROFILING */ 2753 finis(true, true, ExitStat); 2754 /* NOTREACHED */ 2755 2756 case CMDVERB: /* set verbose mode */ 2757 DELAY_CONN("VERB"); 2758 if (bitset(PRIV_NOEXPN, PrivacyFlags) || 2759 !bitset(SRV_OFFER_VERB, features) || 2760 bitset(PRIV_NOVERB, PrivacyFlags)) 2761 { 2762 /* this would give out the same info */ 2763 message("502 5.7.0 Verbose unavailable"); 2764 break; 2765 } 2766 (void) checksmtpattack(&n_noop, MAXNOOPCOMMANDS, true, 2767 "VERB", e); 2768 Verbose = 1; 2769 set_delivery_mode(SM_DELIVER, e); 2770 message("250 2.0.0 Verbose mode"); 2771 break; 2772 2773 #if SMTPDEBUG 2774 case CMDDBGQSHOW: /* show queues */ 2775 (void) sm_io_fprintf(smioout, SM_TIME_DEFAULT, 2776 "Send Queue="); 2777 printaddr(e->e_sendqueue, true); 2778 break; 2779 2780 case CMDDBGDEBUG: /* set debug mode */ 2781 tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 2782 tTflag(p); 2783 message("200 2.0.0 Debug set"); 2784 break; 2785 2786 #else /* SMTPDEBUG */ 2787 case CMDDBGQSHOW: /* show queues */ 2788 case CMDDBGDEBUG: /* set debug mode */ 2789 #endif /* SMTPDEBUG */ 2790 case CMDLOGBOGUS: /* bogus command */ 2791 DELAY_CONN("Bogus"); 2792 if (LogLevel > 0) 2793 sm_syslog(LOG_CRIT, e->e_id, 2794 "\"%s\" command from %.100s (%.100s)", 2795 c->cmd_name, CurSmtpClient, 2796 anynet_ntoa(&RealHostAddr)); 2797 /* FALLTHROUGH */ 2798 2799 case CMDERROR: /* unknown command */ 2800 #if MAXBADCOMMANDS > 0 2801 if (++n_badcmds > MAXBADCOMMANDS) 2802 { 2803 message("421 4.7.0 %s Too many bad commands; closing connection", 2804 MyHostName); 2805 2806 /* arrange to ignore any current send list */ 2807 e->e_sendqueue = NULL; 2808 goto doquit; 2809 } 2810 #endif /* MAXBADCOMMANDS > 0 */ 2811 2812 usrerr("500 5.5.1 Command unrecognized: \"%s\"", 2813 shortenstring(inp, MAXSHORTSTR)); 2814 break; 2815 2816 case CMDUNIMPL: 2817 DELAY_CONN("Unimpl"); 2818 usrerr("502 5.5.1 Command not implemented: \"%s\"", 2819 shortenstring(inp, MAXSHORTSTR)); 2820 break; 2821 2822 default: 2823 DELAY_CONN("default"); 2824 errno = 0; 2825 syserr("500 5.5.0 smtp: unknown code %d", c->cmd_code); 2826 break; 2827 } 2828 #if SASL 2829 } 2830 #endif /* SASL */ 2831 } 2832 SM_EXCEPT(exc, "[!F]*") 2833 { 2834 /* 2835 ** The only possible exception is "E:mta.quickabort". 2836 ** There is nothing to do except fall through and loop. 2837 */ 2838 } 2839 SM_END_TRY 2840 } 2841 } 2842 /* 2843 ** SMTP_DATA -- implement the SMTP DATA command. 2844 ** 2845 ** Parameters: 2846 ** smtp -- status of SMTP connection. 2847 ** e -- envelope. 2848 ** 2849 ** Returns: 2850 ** none. 2851 ** 2852 ** Side Effects: 2853 ** possibly sends message. 2854 */ 2855 2856 static void 2857 smtp_data(smtp, e) 2858 SMTP_T *smtp; 2859 ENVELOPE *e; 2860 { 2861 #if MILTER 2862 bool milteraccept; 2863 #endif /* MILTER */ 2864 bool aborting; 2865 bool doublequeue; 2866 ADDRESS *a; 2867 ENVELOPE *ee; 2868 char *id; 2869 char *oldid; 2870 char buf[32]; 2871 2872 SmtpPhase = "server DATA"; 2873 if (!smtp->sm_gotmail) 2874 { 2875 usrerr("503 5.0.0 Need MAIL command"); 2876 return; 2877 } 2878 else if (smtp->sm_nrcpts <= 0) 2879 { 2880 usrerr("503 5.0.0 Need RCPT (recipient)"); 2881 return; 2882 } 2883 (void) sm_snprintf(buf, sizeof buf, "%u", smtp->sm_nrcpts); 2884 if (rscheck("check_data", buf, NULL, e, 2885 true, false, 3, NULL, e->e_id) != EX_OK) 2886 return; 2887 2888 /* put back discard bit */ 2889 if (smtp->sm_discard) 2890 e->e_flags |= EF_DISCARD; 2891 2892 /* check to see if we need to re-expand aliases */ 2893 /* also reset QS_BADADDR on already-diagnosted addrs */ 2894 doublequeue = false; 2895 for (a = e->e_sendqueue; a != NULL; a = a->q_next) 2896 { 2897 if (QS_IS_VERIFIED(a->q_state) && 2898 !bitset(EF_DISCARD, e->e_flags)) 2899 { 2900 /* need to re-expand aliases */ 2901 doublequeue = true; 2902 } 2903 if (QS_IS_BADADDR(a->q_state)) 2904 { 2905 /* make this "go away" */ 2906 a->q_state = QS_DONTSEND; 2907 } 2908 } 2909 2910 /* collect the text of the message */ 2911 SmtpPhase = "collect"; 2912 buffer_errors(); 2913 2914 #if _FFR_ADAPTIVE_EOL 2915 /* triggers error in collect, disabled for now */ 2916 if (smtp->sm_crlf) 2917 e->e_flags |= EF_NL_NOT_EOL; 2918 #endif /* _FFR_ADAPTIVE_EOL */ 2919 2920 collect(InChannel, true, NULL, e); 2921 2922 /* redefine message size */ 2923 (void) sm_snprintf(buf, sizeof buf, "%ld", e->e_msgsize); 2924 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf); 2925 2926 #if _FFR_CHECK_EOM 2927 /* rscheck() will set Errors or EF_DISCARD if it trips */ 2928 (void) rscheck("check_eom", buf, NULL, e, false, 2929 true, 3, NULL, e->e_id); 2930 #endif /* _FFR_CHECK_EOM */ 2931 2932 #if MILTER 2933 milteraccept = true; 2934 if (smtp->sm_milterlist && smtp->sm_milterize && 2935 Errors <= 0 && 2936 !bitset(EF_DISCARD, e->e_flags)) 2937 { 2938 char state; 2939 char *response; 2940 2941 response = milter_data(e, &state); 2942 switch (state) 2943 { 2944 case SMFIR_REPLYCODE: 2945 if (MilterLogLevel > 3) 2946 sm_syslog(LOG_INFO, e->e_id, 2947 "Milter: data, reject=%s", 2948 response); 2949 milteraccept = false; 2950 usrerr(response); 2951 break; 2952 2953 case SMFIR_REJECT: 2954 milteraccept = false; 2955 if (MilterLogLevel > 3) 2956 sm_syslog(LOG_INFO, e->e_id, 2957 "Milter: data, reject=554 5.7.1 Command rejected"); 2958 usrerr("554 5.7.1 Command rejected"); 2959 break; 2960 2961 case SMFIR_DISCARD: 2962 if (MilterLogLevel > 3) 2963 sm_syslog(LOG_INFO, e->e_id, 2964 "Milter: data, discard"); 2965 milteraccept = false; 2966 e->e_flags |= EF_DISCARD; 2967 break; 2968 2969 case SMFIR_TEMPFAIL: 2970 if (MilterLogLevel > 3) 2971 sm_syslog(LOG_INFO, e->e_id, 2972 "Milter: data, reject=%s", 2973 MSG_TEMPFAIL); 2974 milteraccept = false; 2975 usrerr(MSG_TEMPFAIL); 2976 break; 2977 } 2978 if (response != NULL) 2979 sm_free(response); 2980 } 2981 2982 /* Milter may have changed message size */ 2983 (void) sm_snprintf(buf, sizeof buf, "%ld", e->e_msgsize); 2984 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), buf); 2985 2986 /* abort message filters that didn't get the body & log msg is OK */ 2987 if (smtp->sm_milterlist && smtp->sm_milterize) 2988 { 2989 milter_abort(e); 2990 if (milteraccept && MilterLogLevel > 9) 2991 sm_syslog(LOG_INFO, e->e_id, "Milter accept: message"); 2992 } 2993 #endif /* MILTER */ 2994 2995 #if _FFR_QUARANTINE 2996 /* Check if quarantining stats should be updated */ 2997 if (e->e_quarmsg != NULL) 2998 markstats(e, NULL, STATS_QUARANTINE); 2999 #endif /* _FFR_QUARANTINE */ 3000 3001 /* 3002 ** If a header/body check (header checks or milter) 3003 ** set EF_DISCARD, don't queueup the message -- 3004 ** that would lose the EF_DISCARD bit and deliver 3005 ** the message. 3006 */ 3007 3008 if (bitset(EF_DISCARD, e->e_flags)) 3009 doublequeue = false; 3010 3011 aborting = Errors > 0; 3012 if (!aborting && 3013 #if _FFR_QUARANTINE 3014 (QueueMode == QM_QUARANTINE || e->e_quarmsg == NULL) && 3015 #endif /* _FFR_QUARANTINE */ 3016 !split_by_recipient(e)) 3017 aborting = bitset(EF_FATALERRS, e->e_flags); 3018 3019 if (aborting) 3020 { 3021 /* Log who the mail would have gone to */ 3022 logundelrcpts(e, e->e_message, 8, false); 3023 flush_errors(true); 3024 buffer_errors(); 3025 goto abortmessage; 3026 } 3027 3028 /* from now on, we have to operate silently */ 3029 buffer_errors(); 3030 3031 #if 0 3032 /* 3033 ** Clear message, it may contain an error from the SMTP dialogue. 3034 ** This error must not show up in the queue. 3035 ** Some error message should show up, e.g., alias database 3036 ** not available, but others shouldn't, e.g., from check_rcpt. 3037 */ 3038 3039 e->e_message = NULL; 3040 #endif /* 0 */ 3041 3042 /* 3043 ** Arrange to send to everyone. 3044 ** If sending to multiple people, mail back 3045 ** errors rather than reporting directly. 3046 ** In any case, don't mail back errors for 3047 ** anything that has happened up to 3048 ** now (the other end will do this). 3049 ** Truncate our transcript -- the mail has gotten 3050 ** to us successfully, and if we have 3051 ** to mail this back, it will be easier 3052 ** on the reader. 3053 ** Then send to everyone. 3054 ** Finally give a reply code. If an error has 3055 ** already been given, don't mail a 3056 ** message back. 3057 ** We goose error returns by clearing error bit. 3058 */ 3059 3060 SmtpPhase = "delivery"; 3061 (void) sm_io_setinfo(e->e_xfp, SM_BF_TRUNCATE, NULL); 3062 id = e->e_id; 3063 3064 #if NAMED_BIND 3065 _res.retry = TimeOuts.res_retry[RES_TO_FIRST]; 3066 _res.retrans = TimeOuts.res_retrans[RES_TO_FIRST]; 3067 #endif /* NAMED_BIND */ 3068 3069 for (ee = e; ee != NULL; ee = ee->e_sibling) 3070 { 3071 /* make sure we actually do delivery */ 3072 ee->e_flags &= ~EF_CLRQUEUE; 3073 3074 /* from now on, operate silently */ 3075 ee->e_errormode = EM_MAIL; 3076 3077 if (doublequeue) 3078 { 3079 /* make sure it is in the queue */ 3080 queueup(ee, false, true); 3081 } 3082 else 3083 { 3084 /* send to all recipients */ 3085 sendall(ee, SM_DEFAULT); 3086 } 3087 ee->e_to = NULL; 3088 } 3089 3090 /* put back id for SMTP logging in putoutmsg() */ 3091 oldid = CurEnv->e_id; 3092 CurEnv->e_id = id; 3093 3094 /* issue success message */ 3095 message("250 2.0.0 %s Message accepted for delivery", id); 3096 CurEnv->e_id = oldid; 3097 3098 /* if we just queued, poke it */ 3099 if (doublequeue) 3100 { 3101 bool anything_to_send = false; 3102 3103 sm_getla(); 3104 for (ee = e; ee != NULL; ee = ee->e_sibling) 3105 { 3106 if (WILL_BE_QUEUED(ee->e_sendmode)) 3107 continue; 3108 if (shouldqueue(ee->e_msgpriority, ee->e_ctime)) 3109 { 3110 ee->e_sendmode = SM_QUEUE; 3111 continue; 3112 } 3113 #if _FFR_QUARANTINE 3114 else if (QueueMode != QM_QUARANTINE && 3115 ee->e_quarmsg != NULL) 3116 { 3117 ee->e_sendmode = SM_QUEUE; 3118 continue; 3119 } 3120 #endif /* _FFR_QUARANTINE */ 3121 anything_to_send = true; 3122 3123 /* close all the queue files */ 3124 closexscript(ee); 3125 if (ee->e_dfp != NULL) 3126 { 3127 (void) sm_io_close(ee->e_dfp, SM_TIME_DEFAULT); 3128 ee->e_dfp = NULL; 3129 } 3130 unlockqueue(ee); 3131 } 3132 if (anything_to_send) 3133 { 3134 #if PIPELINING 3135 /* 3136 ** XXX if we don't do this, we get 250 twice 3137 ** because it is also flushed in the child. 3138 */ 3139 3140 (void) sm_io_flush(OutChannel, SM_TIME_DEFAULT); 3141 #endif /* PIPELINING */ 3142 (void) doworklist(e, true, true); 3143 } 3144 } 3145 3146 abortmessage: 3147 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 3148 logsender(e, NULL); 3149 e->e_flags &= ~EF_LOGSENDER; 3150 3151 /* clean up a bit */ 3152 smtp->sm_gotmail = false; 3153 3154 /* 3155 ** Call dropenvelope if and only if the envelope is *not* 3156 ** being processed by the child process forked by doworklist(). 3157 */ 3158 3159 if (aborting || bitset(EF_DISCARD, e->e_flags)) 3160 dropenvelope(e, true, false); 3161 else 3162 { 3163 for (ee = e; ee != NULL; ee = ee->e_sibling) 3164 { 3165 #if _FFR_QUARANTINE 3166 if (!doublequeue && 3167 QueueMode != QM_QUARANTINE && 3168 ee->e_quarmsg != NULL) 3169 { 3170 dropenvelope(ee, true, false); 3171 continue; 3172 } 3173 #endif /* _FFR_QUARANTINE */ 3174 if (WILL_BE_QUEUED(ee->e_sendmode)) 3175 dropenvelope(ee, true, false); 3176 } 3177 } 3178 sm_rpool_free(e->e_rpool); 3179 3180 /* 3181 ** At this point, e == &MainEnvelope, but if we did splitting, 3182 ** then CurEnv may point to an envelope structure that was just 3183 ** freed with the rpool. So reset CurEnv *before* calling 3184 ** newenvelope. 3185 */ 3186 3187 CurEnv = e; 3188 newenvelope(e, e, sm_rpool_new_x(NULL)); 3189 e->e_flags = BlankEnvelope.e_flags; 3190 3191 #if _FFR_QUARANTINE 3192 /* restore connection quarantining */ 3193 if (smtp->sm_quarmsg == NULL) 3194 { 3195 e->e_quarmsg = NULL; 3196 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), ""); 3197 } 3198 else 3199 { 3200 e->e_quarmsg = sm_rpool_strdup_x(e->e_rpool, smtp->sm_quarmsg); 3201 macdefine(&e->e_macro, A_PERM, 3202 macid("{quarantine}"), e->e_quarmsg); 3203 } 3204 #endif /* _FFR_QUARANTINE */ 3205 } 3206 /* 3207 ** LOGUNDELRCPTS -- log undelivered (or all) recipients. 3208 ** 3209 ** Parameters: 3210 ** e -- envelope. 3211 ** msg -- message for Stat= 3212 ** level -- log level. 3213 ** all -- log all recipients. 3214 ** 3215 ** Returns: 3216 ** none. 3217 ** 3218 ** Side Effects: 3219 ** logs undelivered (or all) recipients 3220 */ 3221 3222 void 3223 logundelrcpts(e, msg, level, all) 3224 ENVELOPE *e; 3225 char *msg; 3226 int level; 3227 bool all; 3228 { 3229 ADDRESS *a; 3230 3231 if (LogLevel <= level || msg == NULL || *msg == '\0') 3232 return; 3233 3234 /* Clear $h so relay= doesn't get mislogged by logdelivery() */ 3235 macdefine(&e->e_macro, A_PERM, 'h', NULL); 3236 3237 /* Log who the mail would have gone to */ 3238 for (a = e->e_sendqueue; a != NULL; a = a->q_next) 3239 { 3240 if (!QS_IS_UNDELIVERED(a->q_state) && !all) 3241 continue; 3242 e->e_to = a->q_paddr; 3243 logdelivery(NULL, NULL, a->q_status, msg, NULL, 3244 (time_t) 0, e); 3245 } 3246 e->e_to = NULL; 3247 } 3248 /* 3249 ** CHECKSMTPATTACK -- check for denial-of-service attack by repetition 3250 ** 3251 ** Parameters: 3252 ** pcounter -- pointer to a counter for this command. 3253 ** maxcount -- maximum value for this counter before we 3254 ** slow down. 3255 ** waitnow -- sleep now (in this routine)? 3256 ** cname -- command name for logging. 3257 ** e -- the current envelope. 3258 ** 3259 ** Returns: 3260 ** time to wait. 3261 ** 3262 ** Side Effects: 3263 ** Slows down if we seem to be under attack. 3264 */ 3265 3266 static time_t 3267 checksmtpattack(pcounter, maxcount, waitnow, cname, e) 3268 volatile unsigned int *pcounter; 3269 int maxcount; 3270 bool waitnow; 3271 char *cname; 3272 ENVELOPE *e; 3273 { 3274 if (maxcount <= 0) /* no limit */ 3275 return (time_t) 0; 3276 3277 if (++(*pcounter) >= maxcount) 3278 { 3279 time_t s; 3280 3281 if (*pcounter == maxcount && LogLevel > 5) 3282 { 3283 sm_syslog(LOG_INFO, e->e_id, 3284 "%.100s: possible SMTP attack: command=%.40s, count=%u", 3285 CurSmtpClient, cname, *pcounter); 3286 } 3287 s = 1 << (*pcounter - maxcount); 3288 if (s >= MAXTIMEOUT || s <= 0) 3289 s = MAXTIMEOUT; 3290 3291 /* sleep at least 1 second before returning */ 3292 (void) sleep(*pcounter / maxcount); 3293 s -= *pcounter / maxcount; 3294 if (waitnow) 3295 { 3296 (void) sleep(s); 3297 return 0; 3298 } 3299 return s; 3300 } 3301 return (time_t) 0; 3302 } 3303 /* 3304 ** SETUP_SMTPD_IO -- setup I/O fd correctly for the SMTP server 3305 ** 3306 ** Parameters: 3307 ** none. 3308 ** 3309 ** Returns: 3310 ** nothing. 3311 ** 3312 ** Side Effects: 3313 ** may change I/O fd. 3314 */ 3315 3316 static void 3317 setup_smtpd_io() 3318 { 3319 int inchfd, outchfd, outfd; 3320 3321 inchfd = sm_io_getinfo(InChannel, SM_IO_WHAT_FD, NULL); 3322 outchfd = sm_io_getinfo(OutChannel, SM_IO_WHAT_FD, NULL); 3323 outfd = sm_io_getinfo(smioout, SM_IO_WHAT_FD, NULL); 3324 if (outchfd != outfd) 3325 { 3326 /* arrange for debugging output to go to remote host */ 3327 (void) dup2(outchfd, outfd); 3328 } 3329 3330 /* 3331 ** if InChannel and OutChannel are stdin/stdout 3332 ** and connected to ttys 3333 ** and fcntl(STDIN, F_SETFL, O_NONBLOCKING) also changes STDOUT, 3334 ** then "chain" them together. 3335 */ 3336 3337 if (inchfd == STDIN_FILENO && outchfd == STDOUT_FILENO && 3338 isatty(inchfd) && isatty(outchfd)) 3339 { 3340 int inmode, outmode; 3341 3342 inmode = fcntl(inchfd, F_GETFL, 0); 3343 if (inmode == -1) 3344 { 3345 if (LogLevel > 11) 3346 sm_syslog(LOG_INFO, NOQID, 3347 "fcntl(inchfd, F_GETFL) failed: %s", 3348 sm_errstring(errno)); 3349 return; 3350 } 3351 outmode = fcntl(outchfd, F_GETFL, 0); 3352 if (outmode == -1) 3353 { 3354 if (LogLevel > 11) 3355 sm_syslog(LOG_INFO, NOQID, 3356 "fcntl(outchfd, F_GETFL) failed: %s", 3357 sm_errstring(errno)); 3358 return; 3359 } 3360 if (bitset(O_NONBLOCK, inmode) || 3361 bitset(O_NONBLOCK, outmode) || 3362 fcntl(inchfd, F_SETFL, inmode | O_NONBLOCK) == -1) 3363 return; 3364 outmode = fcntl(outchfd, F_GETFL, 0); 3365 if (outmode != -1 && bitset(O_NONBLOCK, outmode)) 3366 { 3367 /* changing InChannel also changes OutChannel */ 3368 sm_io_automode(OutChannel, InChannel); 3369 if (tTd(97, 4) && LogLevel > 9) 3370 sm_syslog(LOG_INFO, NOQID, 3371 "set automode for I (%d)/O (%d) in SMTP server", 3372 inchfd, outchfd); 3373 } 3374 3375 /* undo change of inchfd */ 3376 (void) fcntl(inchfd, F_SETFL, inmode); 3377 } 3378 } 3379 /* 3380 ** SKIPWORD -- skip a fixed word. 3381 ** 3382 ** Parameters: 3383 ** p -- place to start looking. 3384 ** w -- word to skip. 3385 ** 3386 ** Returns: 3387 ** p following w. 3388 ** NULL on error. 3389 ** 3390 ** Side Effects: 3391 ** clobbers the p data area. 3392 */ 3393 3394 static char * 3395 skipword(p, w) 3396 register char *volatile p; 3397 char *w; 3398 { 3399 register char *q; 3400 char *firstp = p; 3401 3402 /* find beginning of word */ 3403 SKIP_SPACE(p); 3404 q = p; 3405 3406 /* find end of word */ 3407 while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 3408 p++; 3409 while (isascii(*p) && isspace(*p)) 3410 *p++ = '\0'; 3411 if (*p != ':') 3412 { 3413 syntax: 3414 usrerr("501 5.5.2 Syntax error in parameters scanning \"%s\"", 3415 shortenstring(firstp, MAXSHORTSTR)); 3416 return NULL; 3417 } 3418 *p++ = '\0'; 3419 SKIP_SPACE(p); 3420 3421 if (*p == '\0') 3422 goto syntax; 3423 3424 /* see if the input word matches desired word */ 3425 if (sm_strcasecmp(q, w)) 3426 goto syntax; 3427 3428 return p; 3429 } 3430 /* 3431 ** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line 3432 ** 3433 ** Parameters: 3434 ** kp -- the parameter key. 3435 ** vp -- the value of that parameter. 3436 ** e -- the envelope. 3437 ** 3438 ** Returns: 3439 ** none. 3440 */ 3441 3442 static void 3443 mail_esmtp_args(kp, vp, e) 3444 char *kp; 3445 char *vp; 3446 ENVELOPE *e; 3447 { 3448 if (sm_strcasecmp(kp, "size") == 0) 3449 { 3450 if (vp == NULL) 3451 { 3452 usrerr("501 5.5.2 SIZE requires a value"); 3453 /* NOTREACHED */ 3454 } 3455 macdefine(&e->e_macro, A_TEMP, macid("{msg_size}"), vp); 3456 errno = 0; 3457 e->e_msgsize = strtol(vp, (char **) NULL, 10); 3458 if (e->e_msgsize == LONG_MAX && errno == ERANGE) 3459 { 3460 usrerr("552 5.2.3 Message size exceeds maximum value"); 3461 /* NOTREACHED */ 3462 } 3463 if (e->e_msgsize < 0) 3464 { 3465 usrerr("552 5.2.3 Message size invalid"); 3466 /* NOTREACHED */ 3467 } 3468 } 3469 else if (sm_strcasecmp(kp, "body") == 0) 3470 { 3471 if (vp == NULL) 3472 { 3473 usrerr("501 5.5.2 BODY requires a value"); 3474 /* NOTREACHED */ 3475 } 3476 else if (sm_strcasecmp(vp, "8bitmime") == 0) 3477 { 3478 SevenBitInput = false; 3479 } 3480 else if (sm_strcasecmp(vp, "7bit") == 0) 3481 { 3482 SevenBitInput = true; 3483 } 3484 else 3485 { 3486 usrerr("501 5.5.4 Unknown BODY type %s", vp); 3487 /* NOTREACHED */ 3488 } 3489 e->e_bodytype = sm_rpool_strdup_x(e->e_rpool, vp); 3490 } 3491 else if (sm_strcasecmp(kp, "envid") == 0) 3492 { 3493 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3494 { 3495 usrerr("504 5.7.0 Sorry, ENVID not supported, we do not allow DSN"); 3496 /* NOTREACHED */ 3497 } 3498 if (vp == NULL) 3499 { 3500 usrerr("501 5.5.2 ENVID requires a value"); 3501 /* NOTREACHED */ 3502 } 3503 if (!xtextok(vp)) 3504 { 3505 usrerr("501 5.5.4 Syntax error in ENVID parameter value"); 3506 /* NOTREACHED */ 3507 } 3508 if (e->e_envid != NULL) 3509 { 3510 usrerr("501 5.5.0 Duplicate ENVID parameter"); 3511 /* NOTREACHED */ 3512 } 3513 e->e_envid = sm_rpool_strdup_x(e->e_rpool, vp); 3514 macdefine(&e->e_macro, A_PERM, 3515 macid("{dsn_envid}"), e->e_envid); 3516 } 3517 else if (sm_strcasecmp(kp, "ret") == 0) 3518 { 3519 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3520 { 3521 usrerr("504 5.7.0 Sorry, RET not supported, we do not allow DSN"); 3522 /* NOTREACHED */ 3523 } 3524 if (vp == NULL) 3525 { 3526 usrerr("501 5.5.2 RET requires a value"); 3527 /* NOTREACHED */ 3528 } 3529 if (bitset(EF_RET_PARAM, e->e_flags)) 3530 { 3531 usrerr("501 5.5.0 Duplicate RET parameter"); 3532 /* NOTREACHED */ 3533 } 3534 e->e_flags |= EF_RET_PARAM; 3535 if (sm_strcasecmp(vp, "hdrs") == 0) 3536 e->e_flags |= EF_NO_BODY_RETN; 3537 else if (sm_strcasecmp(vp, "full") != 0) 3538 { 3539 usrerr("501 5.5.2 Bad argument \"%s\" to RET", vp); 3540 /* NOTREACHED */ 3541 } 3542 macdefine(&e->e_macro, A_TEMP, macid("{dsn_ret}"), vp); 3543 } 3544 #if SASL 3545 else if (sm_strcasecmp(kp, "auth") == 0) 3546 { 3547 int len; 3548 char *q; 3549 char *auth_param; /* the value of the AUTH=x */ 3550 bool saveQuickAbort = QuickAbort; 3551 bool saveSuprErrs = SuprErrs; 3552 bool saveExitStat = ExitStat; 3553 char pbuf[256]; 3554 3555 if (vp == NULL) 3556 { 3557 usrerr("501 5.5.2 AUTH= requires a value"); 3558 /* NOTREACHED */ 3559 } 3560 if (e->e_auth_param != NULL) 3561 { 3562 usrerr("501 5.5.0 Duplicate AUTH parameter"); 3563 /* NOTREACHED */ 3564 } 3565 if ((q = strchr(vp, ' ')) != NULL) 3566 len = q - vp + 1; 3567 else 3568 len = strlen(vp) + 1; 3569 auth_param = xalloc(len); 3570 (void) sm_strlcpy(auth_param, vp, len); 3571 if (!xtextok(auth_param)) 3572 { 3573 usrerr("501 5.5.4 Syntax error in AUTH parameter value"); 3574 /* just a warning? */ 3575 /* NOTREACHED */ 3576 } 3577 3578 /* XXX this might be cut off */ 3579 (void) sm_strlcpy(pbuf, xuntextify(auth_param), sizeof pbuf); 3580 /* xalloc() the buffer instead? */ 3581 3582 /* XXX define this always or only if trusted? */ 3583 macdefine(&e->e_macro, A_TEMP, macid("{auth_author}"), pbuf); 3584 3585 /* 3586 ** call Strust_auth to find out whether 3587 ** auth_param is acceptable (trusted) 3588 ** we shouldn't trust it if not authenticated 3589 ** (required by RFC, leave it to ruleset?) 3590 */ 3591 3592 SuprErrs = true; 3593 QuickAbort = false; 3594 if (strcmp(auth_param, "<>") != 0 && 3595 (rscheck("trust_auth", pbuf, NULL, e, true, false, 9, 3596 NULL, NOQID) != EX_OK || Errors > 0)) 3597 { 3598 if (tTd(95, 8)) 3599 { 3600 q = e->e_auth_param; 3601 sm_dprintf("auth=\"%.100s\" not trusted user=\"%.100s\"\n", 3602 pbuf, (q == NULL) ? "" : q); 3603 } 3604 3605 /* not trusted */ 3606 e->e_auth_param = "<>"; 3607 # if _FFR_AUTH_PASSING 3608 macdefine(&BlankEnvelope.e_macro, A_PERM, 3609 macid("{auth_author}"), NULL); 3610 # endif /* _FFR_AUTH_PASSING */ 3611 } 3612 else 3613 { 3614 if (tTd(95, 8)) 3615 sm_dprintf("auth=\"%.100s\" trusted\n", pbuf); 3616 e->e_auth_param = sm_rpool_strdup_x(e->e_rpool, 3617 auth_param); 3618 } 3619 sm_free(auth_param); /* XXX */ 3620 3621 /* reset values */ 3622 Errors = 0; 3623 QuickAbort = saveQuickAbort; 3624 SuprErrs = saveSuprErrs; 3625 ExitStat = saveExitStat; 3626 } 3627 #endif /* SASL */ 3628 #define PRTCHAR(c) ((isascii(c) && isprint(c)) ? (c) : '?') 3629 3630 /* 3631 ** "by" is only accepted if DeliverByMin >= 0. 3632 ** We maybe could add this to the list of server_features. 3633 */ 3634 3635 else if (sm_strcasecmp(kp, "by") == 0 && DeliverByMin >= 0) 3636 { 3637 char *s; 3638 3639 if (vp == NULL) 3640 { 3641 usrerr("501 5.5.2 BY= requires a value"); 3642 /* NOTREACHED */ 3643 } 3644 errno = 0; 3645 e->e_deliver_by = strtol(vp, &s, 10); 3646 if (e->e_deliver_by == LONG_MIN || 3647 e->e_deliver_by == LONG_MAX || 3648 e->e_deliver_by > 999999999l || 3649 e->e_deliver_by < -999999999l) 3650 { 3651 usrerr("501 5.5.2 BY=%s out of range", vp); 3652 /* NOTREACHED */ 3653 } 3654 if (s == NULL || *s != ';') 3655 { 3656 usrerr("501 5.5.2 BY= missing ';'"); 3657 /* NOTREACHED */ 3658 } 3659 e->e_dlvr_flag = 0; 3660 ++s; /* XXX: spaces allowed? */ 3661 SKIP_SPACE(s); 3662 switch (tolower(*s)) 3663 { 3664 case 'n': 3665 e->e_dlvr_flag = DLVR_NOTIFY; 3666 break; 3667 case 'r': 3668 e->e_dlvr_flag = DLVR_RETURN; 3669 if (e->e_deliver_by <= 0) 3670 { 3671 usrerr("501 5.5.4 mode R requires BY time > 0"); 3672 /* NOTREACHED */ 3673 } 3674 if (DeliverByMin > 0 && e->e_deliver_by > 0 && 3675 e->e_deliver_by < DeliverByMin) 3676 { 3677 usrerr("555 5.5.2 time %ld less than %ld", 3678 e->e_deliver_by, (long) DeliverByMin); 3679 /* NOTREACHED */ 3680 } 3681 break; 3682 default: 3683 usrerr("501 5.5.2 illegal by-mode '%c'", PRTCHAR(*s)); 3684 /* NOTREACHED */ 3685 } 3686 ++s; /* XXX: spaces allowed? */ 3687 SKIP_SPACE(s); 3688 switch (tolower(*s)) 3689 { 3690 case 't': 3691 e->e_dlvr_flag |= DLVR_TRACE; 3692 break; 3693 case '\0': 3694 break; 3695 default: 3696 usrerr("501 5.5.2 illegal by-trace '%c'", PRTCHAR(*s)); 3697 /* NOTREACHED */ 3698 } 3699 3700 /* XXX: check whether more characters follow? */ 3701 } 3702 else 3703 { 3704 usrerr("555 5.5.4 %s parameter unrecognized", kp); 3705 /* NOTREACHED */ 3706 } 3707 } 3708 /* 3709 ** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line 3710 ** 3711 ** Parameters: 3712 ** a -- the address corresponding to the To: parameter. 3713 ** kp -- the parameter key. 3714 ** vp -- the value of that parameter. 3715 ** e -- the envelope. 3716 ** 3717 ** Returns: 3718 ** none. 3719 */ 3720 3721 static void 3722 rcpt_esmtp_args(a, kp, vp, e) 3723 ADDRESS *a; 3724 char *kp; 3725 char *vp; 3726 ENVELOPE *e; 3727 { 3728 if (sm_strcasecmp(kp, "notify") == 0) 3729 { 3730 char *p; 3731 3732 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3733 { 3734 usrerr("504 5.7.0 Sorry, NOTIFY not supported, we do not allow DSN"); 3735 /* NOTREACHED */ 3736 } 3737 if (vp == NULL) 3738 { 3739 usrerr("501 5.5.2 NOTIFY requires a value"); 3740 /* NOTREACHED */ 3741 } 3742 a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY); 3743 a->q_flags |= QHASNOTIFY; 3744 macdefine(&e->e_macro, A_TEMP, macid("{dsn_notify}"), vp); 3745 3746 if (sm_strcasecmp(vp, "never") == 0) 3747 return; 3748 for (p = vp; p != NULL; vp = p) 3749 { 3750 p = strchr(p, ','); 3751 if (p != NULL) 3752 *p++ = '\0'; 3753 if (sm_strcasecmp(vp, "success") == 0) 3754 a->q_flags |= QPINGONSUCCESS; 3755 else if (sm_strcasecmp(vp, "failure") == 0) 3756 a->q_flags |= QPINGONFAILURE; 3757 else if (sm_strcasecmp(vp, "delay") == 0) 3758 a->q_flags |= QPINGONDELAY; 3759 else 3760 { 3761 usrerr("501 5.5.4 Bad argument \"%s\" to NOTIFY", 3762 vp); 3763 /* NOTREACHED */ 3764 } 3765 } 3766 } 3767 else if (sm_strcasecmp(kp, "orcpt") == 0) 3768 { 3769 if (bitset(PRIV_NORECEIPTS, PrivacyFlags)) 3770 { 3771 usrerr("504 5.7.0 Sorry, ORCPT not supported, we do not allow DSN"); 3772 /* NOTREACHED */ 3773 } 3774 if (vp == NULL) 3775 { 3776 usrerr("501 5.5.2 ORCPT requires a value"); 3777 /* NOTREACHED */ 3778 } 3779 if (strchr(vp, ';') == NULL || !xtextok(vp)) 3780 { 3781 usrerr("501 5.5.4 Syntax error in ORCPT parameter value"); 3782 /* NOTREACHED */ 3783 } 3784 if (a->q_orcpt != NULL) 3785 { 3786 usrerr("501 5.5.0 Duplicate ORCPT parameter"); 3787 /* NOTREACHED */ 3788 } 3789 a->q_orcpt = sm_rpool_strdup_x(e->e_rpool, vp); 3790 } 3791 else 3792 { 3793 usrerr("555 5.5.4 %s parameter unrecognized", kp); 3794 /* NOTREACHED */ 3795 } 3796 } 3797 /* 3798 ** PRINTVRFYADDR -- print an entry in the verify queue 3799 ** 3800 ** Parameters: 3801 ** a -- the address to print. 3802 ** last -- set if this is the last one. 3803 ** vrfy -- set if this is a VRFY command. 3804 ** 3805 ** Returns: 3806 ** none. 3807 ** 3808 ** Side Effects: 3809 ** Prints the appropriate 250 codes. 3810 */ 3811 #define OFFF (3 + 1 + 5 + 1) /* offset in fmt: SMTP reply + enh. code */ 3812 3813 static void 3814 printvrfyaddr(a, last, vrfy) 3815 register ADDRESS *a; 3816 bool last; 3817 bool vrfy; 3818 { 3819 char fmtbuf[30]; 3820 3821 if (vrfy && a->q_mailer != NULL && 3822 !bitnset(M_VRFY250, a->q_mailer->m_flags)) 3823 (void) sm_strlcpy(fmtbuf, "252", sizeof fmtbuf); 3824 else 3825 (void) sm_strlcpy(fmtbuf, "250", sizeof fmtbuf); 3826 fmtbuf[3] = last ? ' ' : '-'; 3827 (void) sm_strlcpy(&fmtbuf[4], "2.1.5 ", sizeof fmtbuf - 4); 3828 if (a->q_fullname == NULL) 3829 { 3830 if ((a->q_mailer == NULL || 3831 a->q_mailer->m_addrtype == NULL || 3832 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) && 3833 strchr(a->q_user, '@') == NULL) 3834 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s@%s>", 3835 sizeof fmtbuf - OFFF); 3836 else 3837 (void) sm_strlcpy(&fmtbuf[OFFF], "<%s>", 3838 sizeof fmtbuf - OFFF); 3839 message(fmtbuf, a->q_user, MyHostName); 3840 } 3841 else 3842 { 3843 if ((a->q_mailer == NULL || 3844 a->q_mailer->m_addrtype == NULL || 3845 sm_strcasecmp(a->q_mailer->m_addrtype, "rfc822") == 0) && 3846 strchr(a->q_user, '@') == NULL) 3847 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s@%s>", 3848 sizeof fmtbuf - OFFF); 3849 else 3850 (void) sm_strlcpy(&fmtbuf[OFFF], "%s <%s>", 3851 sizeof fmtbuf - OFFF); 3852 message(fmtbuf, a->q_fullname, a->q_user, MyHostName); 3853 } 3854 } 3855 3856 #if SASL 3857 /* 3858 ** SASLMECHS -- get list of possible AUTH mechanisms 3859 ** 3860 ** Parameters: 3861 ** conn -- SASL connection info. 3862 ** mechlist -- output parameter for list of mechanisms. 3863 ** 3864 ** Returns: 3865 ** number of mechs. 3866 */ 3867 3868 static int 3869 saslmechs(conn, mechlist) 3870 sasl_conn_t *conn; 3871 char **mechlist; 3872 { 3873 int len, num, result; 3874 3875 /* "user" is currently unused */ 3876 # if SASL >= 20000 3877 result = sasl_listmech(conn, NULL, 3878 "", " ", "", (const char **) mechlist, 3879 (unsigned int *)&len, (unsigned int *)&num); 3880 # else /* SASL >= 20000 */ 3881 result = sasl_listmech(conn, "user", /* XXX */ 3882 "", " ", "", mechlist, 3883 (unsigned int *)&len, (unsigned int *)&num); 3884 # endif /* SASL >= 20000 */ 3885 if (result != SASL_OK) 3886 { 3887 if (LogLevel > 9) 3888 sm_syslog(LOG_WARNING, NOQID, 3889 "AUTH error: listmech=%d, num=%d", 3890 result, num); 3891 num = 0; 3892 } 3893 if (num > 0) 3894 { 3895 if (LogLevel > 11) 3896 sm_syslog(LOG_INFO, NOQID, 3897 "AUTH: available mech=%s, allowed mech=%s", 3898 *mechlist, AuthMechanisms); 3899 *mechlist = intersect(AuthMechanisms, *mechlist, NULL); 3900 } 3901 else 3902 { 3903 *mechlist = NULL; /* be paranoid... */ 3904 if (result == SASL_OK && LogLevel > 9) 3905 sm_syslog(LOG_WARNING, NOQID, 3906 "AUTH warning: no mechanisms"); 3907 } 3908 return num; 3909 } 3910 3911 # if SASL >= 20000 3912 /* 3913 ** PROXY_POLICY -- define proxy policy for AUTH 3914 ** 3915 ** Parameters: 3916 ** conn -- unused. 3917 ** context -- unused. 3918 ** requested_user -- authorization identity. 3919 ** rlen -- authorization identity length. 3920 ** auth_identity -- authentication identity. 3921 ** alen -- authentication identity length. 3922 ** def_realm -- default user realm. 3923 ** urlen -- user realm length. 3924 ** propctx -- unused. 3925 ** 3926 ** Returns: 3927 ** ok? 3928 ** 3929 ** Side Effects: 3930 ** sets {auth_authen} macro. 3931 */ 3932 3933 int 3934 proxy_policy(conn, context, requested_user, rlen, auth_identity, alen, 3935 def_realm, urlen, propctx) 3936 sasl_conn_t *conn; 3937 void *context; 3938 const char *requested_user; 3939 unsigned rlen; 3940 const char *auth_identity; 3941 unsigned alen; 3942 const char *def_realm; 3943 unsigned urlen; 3944 struct propctx *propctx; 3945 { 3946 if (auth_identity == NULL) 3947 return SASL_FAIL; 3948 3949 macdefine(&BlankEnvelope.e_macro, A_TEMP, 3950 macid("{auth_authen}"), (char *) auth_identity); 3951 3952 return SASL_OK; 3953 } 3954 # else /* SASL >= 20000 */ 3955 3956 /* 3957 ** PROXY_POLICY -- define proxy policy for AUTH 3958 ** 3959 ** Parameters: 3960 ** context -- unused. 3961 ** auth_identity -- authentication identity. 3962 ** requested_user -- authorization identity. 3963 ** user -- allowed user (output). 3964 ** errstr -- possible error string (output). 3965 ** 3966 ** Returns: 3967 ** ok? 3968 */ 3969 3970 int 3971 proxy_policy(context, auth_identity, requested_user, user, errstr) 3972 void *context; 3973 const char *auth_identity; 3974 const char *requested_user; 3975 const char **user; 3976 const char **errstr; 3977 { 3978 if (user == NULL || auth_identity == NULL) 3979 return SASL_FAIL; 3980 *user = newstr(auth_identity); 3981 return SASL_OK; 3982 } 3983 # endif /* SASL >= 20000 */ 3984 #endif /* SASL */ 3985 3986 #if STARTTLS 3987 /* 3988 ** INITSRVTLS -- initialize server side TLS 3989 ** 3990 ** Parameters: 3991 ** tls_ok -- should tls initialization be done? 3992 ** 3993 ** Returns: 3994 ** succeeded? 3995 ** 3996 ** Side Effects: 3997 ** sets tls_ok_srv which is a static variable in this module. 3998 ** Do NOT remove assignments to it! 3999 */ 4000 4001 bool 4002 initsrvtls(tls_ok) 4003 bool tls_ok; 4004 { 4005 if (!tls_ok) 4006 return false; 4007 4008 /* do NOT remove assignment */ 4009 tls_ok_srv = inittls(&srv_ctx, TLS_Srv_Opts, true, SrvCERTfile, 4010 Srvkeyfile, CACERTpath, CACERTfile, DHParams); 4011 return tls_ok_srv; 4012 } 4013 #endif /* STARTTLS */ 4014 /* 4015 ** SRVFEATURES -- get features for SMTP server 4016 ** 4017 ** Parameters: 4018 ** e -- envelope (should be session context). 4019 ** clientname -- name of client. 4020 ** features -- default features for this invocation. 4021 ** 4022 ** Returns: 4023 ** server features. 4024 */ 4025 4026 /* table with options: it uses just one character, how about strings? */ 4027 static struct 4028 { 4029 char srvf_opt; 4030 unsigned int srvf_flag; 4031 } srv_feat_table[] = 4032 { 4033 { 'A', SRV_OFFER_AUTH }, 4034 { 'B', SRV_OFFER_VERB }, 4035 { 'D', SRV_OFFER_DSN }, 4036 { 'E', SRV_OFFER_ETRN }, 4037 { 'L', SRV_REQ_AUTH }, /* not documented in 8.12 */ 4038 #if PIPELINING 4039 # if _FFR_NO_PIPE 4040 { 'N', SRV_NO_PIPE }, 4041 # endif /* _FFR_NO_PIPE */ 4042 { 'P', SRV_OFFER_PIPE }, 4043 #endif /* PIPELINING */ 4044 { 'R', SRV_VRFY_CLT }, 4045 { 'S', SRV_OFFER_TLS }, 4046 /* { 'T', SRV_TMP_FAIL }, */ 4047 { 'V', SRV_VRFY_CLT }, 4048 { 'X', SRV_OFFER_EXPN }, 4049 /* { 'Y', SRV_OFFER_VRFY }, */ 4050 { '\0', SRV_NONE } 4051 }; 4052 4053 static unsigned int 4054 srvfeatures(e, clientname, features) 4055 ENVELOPE *e; 4056 char *clientname; 4057 unsigned int features; 4058 { 4059 int r, i, j; 4060 char **pvp, c, opt; 4061 char pvpbuf[PSBUFSIZE]; 4062 4063 pvp = NULL; 4064 r = rscap("srv_features", clientname, "", e, &pvp, pvpbuf, 4065 sizeof(pvpbuf)); 4066 if (r != EX_OK) 4067 return features; 4068 if (pvp == NULL || pvp[0] == NULL || (pvp[0][0] & 0377) != CANONNET) 4069 return features; 4070 if (pvp[1] != NULL && sm_strncasecmp(pvp[1], "temp", 4) == 0) 4071 return SRV_TMP_FAIL; 4072 4073 /* 4074 ** General rule (see sendmail.h, d_flags): 4075 ** lower case: required/offered, upper case: Not required/available 4076 ** 4077 ** Since we can change some features per daemon, we have both 4078 ** cases here: turn on/off a feature. 4079 */ 4080 4081 for (i = 1; pvp[i] != NULL; i++) 4082 { 4083 c = pvp[i][0]; 4084 j = 0; 4085 for (;;) 4086 { 4087 if ((opt = srv_feat_table[j].srvf_opt) == '\0') 4088 { 4089 if (LogLevel > 9) 4090 sm_syslog(LOG_WARNING, e->e_id, 4091 "srvfeatures: unknown feature %s", 4092 pvp[i]); 4093 break; 4094 } 4095 if (c == opt) 4096 { 4097 features &= ~(srv_feat_table[j].srvf_flag); 4098 break; 4099 } 4100 if (c == tolower(opt)) 4101 { 4102 features |= srv_feat_table[j].srvf_flag; 4103 break; 4104 } 4105 ++j; 4106 } 4107 } 4108 return features; 4109 } 4110 4111 /* 4112 ** HELP -- implement the HELP command. 4113 ** 4114 ** Parameters: 4115 ** topic -- the topic we want help for. 4116 ** e -- envelope. 4117 ** 4118 ** Returns: 4119 ** none. 4120 ** 4121 ** Side Effects: 4122 ** outputs the help file to message output. 4123 */ 4124 #define HELPVSTR "#vers " 4125 #define HELPVERSION 2 4126 4127 void 4128 help(topic, e) 4129 char *topic; 4130 ENVELOPE *e; 4131 { 4132 register SM_FILE_T *hf; 4133 register char *p; 4134 int len; 4135 bool noinfo; 4136 bool first = true; 4137 long sff = SFF_OPENASROOT|SFF_REGONLY; 4138 char buf[MAXLINE]; 4139 char inp[MAXLINE]; 4140 static int foundvers = -1; 4141 extern char Version[]; 4142 4143 if (DontLockReadFiles) 4144 sff |= SFF_NOLOCK; 4145 if (!bitnset(DBS_HELPFILEINUNSAFEDIRPATH, DontBlameSendmail)) 4146 sff |= SFF_SAFEDIRPATH; 4147 4148 if (HelpFile == NULL || 4149 (hf = safefopen(HelpFile, O_RDONLY, 0444, sff)) == NULL) 4150 { 4151 /* no help */ 4152 errno = 0; 4153 message("502 5.3.0 Sendmail %s -- HELP not implemented", 4154 Version); 4155 return; 4156 } 4157 4158 if (topic == NULL || *topic == '\0') 4159 { 4160 topic = "smtp"; 4161 noinfo = false; 4162 } 4163 else 4164 { 4165 makelower(topic); 4166 noinfo = true; 4167 } 4168 4169 len = strlen(topic); 4170 4171 while (sm_io_fgets(hf, SM_TIME_DEFAULT, buf, sizeof buf) != NULL) 4172 { 4173 if (buf[0] == '#') 4174 { 4175 if (foundvers < 0 && 4176 strncmp(buf, HELPVSTR, strlen(HELPVSTR)) == 0) 4177 { 4178 int h; 4179 4180 if (sm_io_sscanf(buf + strlen(HELPVSTR), "%d", 4181 &h) == 1) 4182 foundvers = h; 4183 } 4184 continue; 4185 } 4186 if (strncmp(buf, topic, len) == 0) 4187 { 4188 if (first) 4189 { 4190 first = false; 4191 4192 /* print version if no/old vers# in file */ 4193 if (foundvers < 2 && !noinfo) 4194 message("214-2.0.0 This is Sendmail version %s", Version); 4195 } 4196 p = strpbrk(buf, " \t"); 4197 if (p == NULL) 4198 p = buf + strlen(buf) - 1; 4199 else 4200 p++; 4201 fixcrlf(p, true); 4202 if (foundvers >= 2) 4203 { 4204 translate_dollars(p); 4205 expand(p, inp, sizeof inp, e); 4206 p = inp; 4207 } 4208 message("214-2.0.0 %s", p); 4209 noinfo = false; 4210 } 4211 } 4212 4213 if (noinfo) 4214 message("504 5.3.0 HELP topic \"%.10s\" unknown", topic); 4215 else 4216 message("214 2.0.0 End of HELP info"); 4217 4218 if (foundvers != 0 && foundvers < HELPVERSION) 4219 { 4220 if (LogLevel > 1) 4221 sm_syslog(LOG_WARNING, e->e_id, 4222 "%s too old (require version %d)", 4223 HelpFile, HELPVERSION); 4224 4225 /* avoid log next time */ 4226 foundvers = 0; 4227 } 4228 4229 (void) sm_io_close(hf, SM_TIME_DEFAULT); 4230 } 4231