1 /* 2 * Copyright (c) 1999-2004, 2006 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * 5 * By using this file, you agree to the terms and conditions set 6 * forth in the LICENSE file which can be found at the top level of 7 * the sendmail distribution. 8 * 9 */ 10 11 #pragma ident "%Z%%M% %I% %E% SMI" 12 13 #include <sm/gen.h> 14 SM_RCSID("@(#)$Id: engine.c,v 8.157 2007/03/26 18:10:04 ca Exp $") 15 16 #include "libmilter.h" 17 18 #if NETINET || NETINET6 19 # include <arpa/inet.h> 20 #endif /* NETINET || NETINET6 */ 21 22 /* generic argument for functions in the command table */ 23 struct arg_struct 24 { 25 size_t a_len; /* length of buffer */ 26 char *a_buf; /* argument string */ 27 int a_idx; /* index for macro array */ 28 SMFICTX_PTR a_ctx; /* context */ 29 }; 30 31 typedef struct arg_struct genarg; 32 33 /* structure for commands received from MTA */ 34 struct cmdfct_t 35 { 36 char cm_cmd; /* command */ 37 int cm_argt; /* type of arguments expected */ 38 int cm_next; /* next state */ 39 int cm_todo; /* what to do next */ 40 int cm_macros; /* index for macros */ 41 int (*cm_fct) __P((genarg *)); /* function to execute */ 42 }; 43 44 typedef struct cmdfct_t cmdfct; 45 46 /* possible values for cm_argt */ 47 #define CM_ARG0 0 /* no args */ 48 #define CM_ARG1 1 /* one arg (string) */ 49 #define CM_ARG2 2 /* two args (strings) */ 50 #define CM_ARGA 4 /* one string and _SOCK_ADDR */ 51 #define CM_ARGO 5 /* two integers */ 52 #define CM_ARGV 8 /* \0 separated list of args, NULL-terminated */ 53 #define CM_ARGN 9 /* \0 separated list of args (strings) */ 54 55 /* possible values for cm_todo */ 56 #define CT_CONT 0x0000 /* continue reading commands */ 57 #define CT_IGNO 0x0001 /* continue even when error */ 58 59 /* not needed right now, done via return code instead */ 60 #define CT_KEEP 0x0004 /* keep buffer (contains symbols) */ 61 #define CT_END 0x0008 /* last command of session, stop replying */ 62 63 /* index in macro array: macros only for these commands */ 64 #define CI_NONE (-1) 65 #define CI_CONN 0 66 #define CI_HELO 1 67 #define CI_MAIL 2 68 #define CI_RCPT 3 69 #define CI_DATA 4 70 #define CI_EOM 5 71 #define CI_EOH 6 72 #define CI_LAST CI_EOH 73 #if CI_LAST < CI_DATA 74 ERROR: do not compile with CI_LAST < CI_DATA 75 #endif 76 #if CI_LAST < CI_EOM 77 ERROR: do not compile with CI_LAST < CI_EOM 78 #endif 79 #if CI_LAST < CI_EOH 80 ERROR: do not compile with CI_LAST < CI_EOH 81 #endif 82 #if CI_LAST < CI_ENVRCPT 83 ERROR: do not compile with CI_LAST < CI_ENVRCPT 84 #endif 85 #if CI_LAST < CI_ENVFROM 86 ERROR: do not compile with CI_LAST < CI_ENVFROM 87 #endif 88 #if CI_LAST < CI_HELO 89 ERROR: do not compile with CI_LAST < CI_HELO 90 #endif 91 #if CI_LAST < CI_CONNECT 92 ERROR: do not compile with CI_LAST < CI_CONNECT 93 #endif 94 #if CI_LAST >= MAX_MACROS_ENTRIES 95 ERROR: do not compile with CI_LAST >= MAX_MACROS_ENTRIES 96 #endif 97 98 /* function prototypes */ 99 static int st_abortfct __P((genarg *)); 100 static int st_macros __P((genarg *)); 101 static int st_optionneg __P((genarg *)); 102 static int st_bodychunk __P((genarg *)); 103 static int st_connectinfo __P((genarg *)); 104 static int st_bodyend __P((genarg *)); 105 static int st_helo __P((genarg *)); 106 static int st_header __P((genarg *)); 107 static int st_sender __P((genarg *)); 108 static int st_rcpt __P((genarg *)); 109 static int st_unknown __P((genarg *)); 110 static int st_data __P((genarg *)); 111 static int st_eoh __P((genarg *)); 112 static int st_quit __P((genarg *)); 113 static int sendreply __P((sfsistat, socket_t, struct timeval *, SMFICTX_PTR)); 114 static void fix_stm __P((SMFICTX_PTR)); 115 static bool trans_ok __P((int, int)); 116 static char **dec_argv __P((char *, size_t)); 117 static int dec_arg2 __P((char *, size_t, char **, char **)); 118 119 #if _FFR_WORKERS_POOL 120 static bool mi_rd_socket_ready __P((int)); 121 #endif /* _FFR_WORKERS_POOL */ 122 123 /* states */ 124 #define ST_NONE (-1) 125 #define ST_INIT 0 /* initial state */ 126 #define ST_OPTS 1 /* option negotiation */ 127 #define ST_CONN 2 /* connection info */ 128 #define ST_HELO 3 /* helo */ 129 #define ST_MAIL 4 /* mail from */ 130 #define ST_RCPT 5 /* rcpt to */ 131 #define ST_DATA 6 /* data */ 132 #define ST_HDRS 7 /* headers */ 133 #define ST_EOHS 8 /* end of headers */ 134 #define ST_BODY 9 /* body */ 135 #define ST_ENDM 10 /* end of message */ 136 #define ST_QUIT 11 /* quit */ 137 #define ST_ABRT 12 /* abort */ 138 #define ST_UNKN 13 /* unknown SMTP command */ 139 #define ST_Q_NC 14 /* quit, new connection follows */ 140 #define ST_LAST ST_Q_NC /* last valid state */ 141 #define ST_SKIP 16 /* not a state but required for the state table */ 142 143 /* in a mail transaction? must be before eom according to spec. */ 144 #define ST_IN_MAIL(st) ((st) >= ST_MAIL && (st) < ST_ENDM) 145 146 /* 147 ** set of next states 148 ** each state (ST_*) corresponds to bit in an int value (1 << state) 149 ** each state has a set of allowed transitions ('or' of bits of states) 150 ** so a state transition is valid if the mask of the next state 151 ** is set in the NX_* value 152 ** this function is coded in trans_ok(), see below. 153 */ 154 155 #define MI_MASK(x) (0x0001 << (x)) /* generate a bit "mask" for a state */ 156 #define NX_INIT (MI_MASK(ST_OPTS)) 157 #define NX_OPTS (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN)) 158 #define NX_CONN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN)) 159 #define NX_HELO (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN)) 160 #define NX_MAIL (MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN)) 161 #define NX_RCPT (MI_MASK(ST_HDRS) | MI_MASK(ST_EOHS) | MI_MASK(ST_DATA) | \ 162 MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | \ 163 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | MI_MASK(ST_UNKN)) 164 #define NX_DATA (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT)) 165 #define NX_HDRS (MI_MASK(ST_EOHS) | MI_MASK(ST_HDRS) | MI_MASK(ST_ABRT)) 166 #define NX_EOHS (MI_MASK(ST_BODY) | MI_MASK(ST_ENDM) | MI_MASK(ST_ABRT)) 167 #define NX_BODY (MI_MASK(ST_ENDM) | MI_MASK(ST_BODY) | MI_MASK(ST_ABRT)) 168 #define NX_ENDM (MI_MASK(ST_QUIT) | MI_MASK(ST_MAIL) | MI_MASK(ST_UNKN) | \ 169 MI_MASK(ST_Q_NC)) 170 #define NX_QUIT 0 171 #define NX_ABRT 0 172 #define NX_UNKN (MI_MASK(ST_HELO) | MI_MASK(ST_MAIL) | \ 173 MI_MASK(ST_RCPT) | MI_MASK(ST_ABRT) | \ 174 MI_MASK(ST_DATA) | \ 175 MI_MASK(ST_BODY) | MI_MASK(ST_UNKN) | \ 176 MI_MASK(ST_ABRT) | MI_MASK(ST_QUIT) | MI_MASK(ST_Q_NC)) 177 #define NX_Q_NC (MI_MASK(ST_CONN) | MI_MASK(ST_UNKN)) 178 #define NX_SKIP MI_MASK(ST_SKIP) 179 180 static int next_states[] = 181 { 182 NX_INIT 183 , NX_OPTS 184 , NX_CONN 185 , NX_HELO 186 , NX_MAIL 187 , NX_RCPT 188 , NX_DATA 189 , NX_HDRS 190 , NX_EOHS 191 , NX_BODY 192 , NX_ENDM 193 , NX_QUIT 194 , NX_ABRT 195 , NX_UNKN 196 , NX_Q_NC 197 }; 198 199 #define SIZE_NEXT_STATES (sizeof(next_states) / sizeof(next_states[0])) 200 201 /* commands received by milter */ 202 static cmdfct cmds[] = 203 { 204 {SMFIC_ABORT, CM_ARG0, ST_ABRT, CT_CONT, CI_NONE, st_abortfct } 205 , {SMFIC_MACRO, CM_ARGV, ST_NONE, CT_KEEP, CI_NONE, st_macros } 206 , {SMFIC_BODY, CM_ARG1, ST_BODY, CT_CONT, CI_NONE, st_bodychunk } 207 , {SMFIC_CONNECT, CM_ARG2, ST_CONN, CT_CONT, CI_CONN, st_connectinfo } 208 , {SMFIC_BODYEOB, CM_ARG1, ST_ENDM, CT_CONT, CI_EOM, st_bodyend } 209 , {SMFIC_HELO, CM_ARG1, ST_HELO, CT_CONT, CI_HELO, st_helo } 210 , {SMFIC_HEADER, CM_ARG2, ST_HDRS, CT_CONT, CI_NONE, st_header } 211 , {SMFIC_MAIL, CM_ARGV, ST_MAIL, CT_CONT, CI_MAIL, st_sender } 212 , {SMFIC_OPTNEG, CM_ARGO, ST_OPTS, CT_CONT, CI_NONE, st_optionneg } 213 , {SMFIC_EOH, CM_ARG0, ST_EOHS, CT_CONT, CI_EOH, st_eoh } 214 , {SMFIC_QUIT, CM_ARG0, ST_QUIT, CT_END, CI_NONE, st_quit } 215 , {SMFIC_DATA, CM_ARG0, ST_DATA, CT_CONT, CI_DATA, st_data } 216 , {SMFIC_RCPT, CM_ARGV, ST_RCPT, CT_IGNO, CI_RCPT, st_rcpt } 217 , {SMFIC_UNKNOWN, CM_ARG1, ST_UNKN, CT_IGNO, CI_NONE, st_unknown } 218 , {SMFIC_QUIT_NC, CM_ARG0, ST_Q_NC, CT_CONT, CI_NONE, st_quit } 219 }; 220 221 /* 222 ** Additional (internal) reply codes; 223 ** must be coordinated wit libmilter/mfapi.h 224 */ 225 226 #define _SMFIS_KEEP 20 227 #define _SMFIS_ABORT 21 228 #define _SMFIS_OPTIONS 22 229 #define _SMFIS_NOREPLY SMFIS_NOREPLY 230 #define _SMFIS_FAIL (-1) 231 #define _SMFIS_NONE (-2) 232 233 /* 234 ** MI_ENGINE -- receive commands and process them 235 ** 236 ** Parameters: 237 ** ctx -- context structure 238 ** 239 ** Returns: 240 ** MI_FAILURE/MI_SUCCESS 241 */ 242 243 int 244 mi_engine(ctx) 245 SMFICTX_PTR ctx; 246 { 247 size_t len; 248 int i; 249 socket_t sd; 250 int ret = MI_SUCCESS; 251 int ncmds = sizeof(cmds) / sizeof(cmdfct); 252 int curstate = ST_INIT; 253 int newstate; 254 bool call_abort; 255 sfsistat r; 256 char cmd; 257 char *buf = NULL; 258 genarg arg; 259 struct timeval timeout; 260 int (*f) __P((genarg *)); 261 sfsistat (*fi_abort) __P((SMFICTX *)); 262 sfsistat (*fi_close) __P((SMFICTX *)); 263 264 arg.a_ctx = ctx; 265 sd = ctx->ctx_sd; 266 fi_abort = ctx->ctx_smfi->xxfi_abort; 267 #if _FFR_WORKERS_POOL 268 curstate = ctx->ctx_state; 269 if (curstate == ST_INIT) 270 { 271 mi_clr_macros(ctx, 0); 272 fix_stm(ctx); 273 } 274 #else /* _FFR_WORKERS_POOL */ 275 mi_clr_macros(ctx, 0); 276 fix_stm(ctx); 277 #endif /* _FFR_WORKERS_POOL */ 278 r = _SMFIS_NONE; 279 do 280 { 281 /* call abort only if in a mail transaction */ 282 call_abort = ST_IN_MAIL(curstate); 283 timeout.tv_sec = ctx->ctx_timeout; 284 timeout.tv_usec = 0; 285 if (mi_stop() == MILTER_ABRT) 286 { 287 if (ctx->ctx_dbg > 3) 288 sm_dprintf("[%ld] milter_abort\n", 289 (long) ctx->ctx_id); 290 ret = MI_FAILURE; 291 break; 292 } 293 294 /* 295 ** Notice: buf is allocated by mi_rd_cmd() and it will 296 ** usually be free()d after it has been used in f(). 297 ** However, if the function returns _SMFIS_KEEP then buf 298 ** contains macros and will not be free()d. 299 ** Hence r must be set to _SMFIS_NONE if a new buf is 300 ** allocated to avoid problem with housekeeping, esp. 301 ** if the code "break"s out of the loop. 302 */ 303 304 #if _FFR_WORKERS_POOL 305 /* Is the socket ready to be read ??? */ 306 if (!mi_rd_socket_ready(sd)) 307 { 308 ret = MI_CONTINUE; 309 break; 310 } 311 #endif /* _FFR_WORKERS_POOL */ 312 313 r = _SMFIS_NONE; 314 if ((buf = mi_rd_cmd(sd, &timeout, &cmd, &len, 315 ctx->ctx_smfi->xxfi_name)) == NULL && 316 cmd < SMFIC_VALIDCMD) 317 { 318 if (ctx->ctx_dbg > 5) 319 sm_dprintf("[%ld] mi_engine: mi_rd_cmd error (%x)\n", 320 (long) ctx->ctx_id, (int) cmd); 321 322 /* 323 ** eof is currently treated as failure -> 324 ** abort() instead of close(), otherwise use: 325 ** if (cmd != SMFIC_EOF) 326 */ 327 328 ret = MI_FAILURE; 329 break; 330 } 331 if (ctx->ctx_dbg > 4) 332 sm_dprintf("[%ld] got cmd '%c' len %d\n", 333 (long) ctx->ctx_id, cmd, (int) len); 334 for (i = 0; i < ncmds; i++) 335 { 336 if (cmd == cmds[i].cm_cmd) 337 break; 338 } 339 if (i >= ncmds) 340 { 341 /* unknown command */ 342 if (ctx->ctx_dbg > 1) 343 sm_dprintf("[%ld] cmd '%c' unknown\n", 344 (long) ctx->ctx_id, cmd); 345 ret = MI_FAILURE; 346 break; 347 } 348 if ((f = cmds[i].cm_fct) == NULL) 349 { 350 /* stop for now */ 351 if (ctx->ctx_dbg > 1) 352 sm_dprintf("[%ld] cmd '%c' not impl\n", 353 (long) ctx->ctx_id, cmd); 354 ret = MI_FAILURE; 355 break; 356 } 357 358 /* is new state ok? */ 359 newstate = cmds[i].cm_next; 360 if (ctx->ctx_dbg > 5) 361 sm_dprintf("[%ld] cur %x new %x nextmask %x\n", 362 (long) ctx->ctx_id, 363 curstate, newstate, next_states[curstate]); 364 365 if (newstate != ST_NONE && !trans_ok(curstate, newstate)) 366 { 367 if (ctx->ctx_dbg > 1) 368 sm_dprintf("[%ld] abort: cur %d (%x) new %d (%x) next %x\n", 369 (long) ctx->ctx_id, 370 curstate, MI_MASK(curstate), 371 newstate, MI_MASK(newstate), 372 next_states[curstate]); 373 374 /* call abort only if in a mail transaction */ 375 if (fi_abort != NULL && call_abort) 376 (void) (*fi_abort)(ctx); 377 378 /* 379 ** try to reach the new state from HELO 380 ** if it can't be reached, ignore the command. 381 */ 382 383 curstate = ST_HELO; 384 if (!trans_ok(curstate, newstate)) 385 { 386 if (buf != NULL) 387 { 388 free(buf); 389 buf = NULL; 390 } 391 continue; 392 } 393 } 394 arg.a_len = len; 395 arg.a_buf = buf; 396 if (newstate != ST_NONE) 397 { 398 curstate = newstate; 399 ctx->ctx_state = curstate; 400 } 401 arg.a_idx = cmds[i].cm_macros; 402 call_abort = ST_IN_MAIL(curstate); 403 404 /* call function to deal with command */ 405 MI_MONITOR_BEGIN(ctx, cmd); 406 r = (*f)(&arg); 407 MI_MONITOR_END(ctx, cmd); 408 if (r != _SMFIS_KEEP && buf != NULL) 409 { 410 free(buf); 411 buf = NULL; 412 } 413 if (sendreply(r, sd, &timeout, ctx) != MI_SUCCESS) 414 { 415 ret = MI_FAILURE; 416 break; 417 } 418 419 if (r == SMFIS_ACCEPT) 420 { 421 /* accept mail, no further actions taken */ 422 curstate = ST_HELO; 423 } 424 else if (r == SMFIS_REJECT || r == SMFIS_DISCARD || 425 r == SMFIS_TEMPFAIL) 426 { 427 /* 428 ** further actions depend on current state 429 ** if the IGNO bit is set: "ignore" the error, 430 ** i.e., stay in the current state 431 */ 432 if (!bitset(CT_IGNO, cmds[i].cm_todo)) 433 curstate = ST_HELO; 434 } 435 else if (r == _SMFIS_ABORT) 436 { 437 if (ctx->ctx_dbg > 5) 438 sm_dprintf("[%ld] function returned abort\n", 439 (long) ctx->ctx_id); 440 ret = MI_FAILURE; 441 break; 442 } 443 } while (!bitset(CT_END, cmds[i].cm_todo)); 444 445 ctx->ctx_state = curstate; 446 447 if (ret == MI_FAILURE) 448 { 449 /* call abort only if in a mail transaction */ 450 if (fi_abort != NULL && call_abort) 451 (void) (*fi_abort)(ctx); 452 } 453 454 /* has close been called? */ 455 if (ctx->ctx_state != ST_QUIT 456 #if _FFR_WORKERS_POOL 457 && ret != MI_CONTINUE 458 #endif /* _FFR_WORKERS_POOL */ 459 ) 460 { 461 if ((fi_close = ctx->ctx_smfi->xxfi_close) != NULL) 462 (void) (*fi_close)(ctx); 463 } 464 if (r != _SMFIS_KEEP && buf != NULL) 465 free(buf); 466 #if !_FFR_WORKERS_POOL 467 mi_clr_macros(ctx, 0); 468 #endif /* _FFR_WORKERS_POOL */ 469 return ret; 470 } 471 472 static size_t milter_addsymlist __P((SMFICTX_PTR, char *, char **)); 473 474 static size_t 475 milter_addsymlist(ctx, buf, newbuf) 476 SMFICTX_PTR ctx; 477 char *buf; 478 char **newbuf; 479 { 480 size_t len; 481 int i; 482 mi_int32 v; 483 char *buffer; 484 485 SM_ASSERT(ctx != NULL); 486 SM_ASSERT(buf != NULL); 487 SM_ASSERT(newbuf != NULL); 488 len = 0; 489 for (i = 0; i < MAX_MACROS_ENTRIES; i++) 490 { 491 if (ctx->ctx_mac_list[i] != NULL) 492 { 493 len += strlen(ctx->ctx_mac_list[i]) + 1 + 494 MILTER_LEN_BYTES; 495 } 496 } 497 if (len > 0) 498 { 499 size_t offset; 500 501 SM_ASSERT(len + MILTER_OPTLEN > len); 502 len += MILTER_OPTLEN; 503 buffer = malloc(len); 504 if (buffer != NULL) 505 { 506 (void) memcpy(buffer, buf, MILTER_OPTLEN); 507 offset = MILTER_OPTLEN; 508 for (i = 0; i < MAX_MACROS_ENTRIES; i++) 509 { 510 size_t l; 511 512 if (ctx->ctx_mac_list[i] == NULL) 513 continue; 514 515 SM_ASSERT(offset + MILTER_LEN_BYTES < len); 516 v = htonl(i); 517 (void) memcpy(buffer + offset, (void *) &v, 518 MILTER_LEN_BYTES); 519 offset += MILTER_LEN_BYTES; 520 l = strlen(ctx->ctx_mac_list[i]) + 1; 521 SM_ASSERT(offset + l <= len); 522 (void) memcpy(buffer + offset, 523 ctx->ctx_mac_list[i], l); 524 offset += l; 525 } 526 } 527 else 528 { 529 /* oops ... */ 530 } 531 } 532 else 533 { 534 len = MILTER_OPTLEN; 535 buffer = buf; 536 } 537 *newbuf = buffer; 538 return len; 539 } 540 541 /* 542 ** GET_NR_BIT -- get "no reply" bit matching state 543 ** 544 ** Parameters: 545 ** state -- current protocol stage 546 ** 547 ** Returns: 548 ** 0: no matching bit 549 ** >0: the matching "no reply" bit 550 */ 551 552 static unsigned long get_nr_bit __P((int)); 553 554 static unsigned long 555 get_nr_bit(state) 556 int state; 557 { 558 unsigned long bit; 559 560 switch (state) 561 { 562 case ST_CONN: 563 bit = SMFIP_NR_CONN; 564 break; 565 case ST_HELO: 566 bit = SMFIP_NR_HELO; 567 break; 568 case ST_MAIL: 569 bit = SMFIP_NR_MAIL; 570 break; 571 case ST_RCPT: 572 bit = SMFIP_NR_RCPT; 573 break; 574 case ST_DATA: 575 bit = SMFIP_NR_DATA; 576 break; 577 case ST_UNKN: 578 bit = SMFIP_NR_UNKN; 579 break; 580 case ST_HDRS: 581 bit = SMFIP_NR_HDR; 582 break; 583 case ST_EOHS: 584 bit = SMFIP_NR_EOH; 585 break; 586 case ST_BODY: 587 bit = SMFIP_NR_BODY; 588 break; 589 default: 590 bit = 0; 591 break; 592 } 593 return bit; 594 } 595 596 /* 597 ** SENDREPLY -- send a reply to the MTA 598 ** 599 ** Parameters: 600 ** r -- reply code 601 ** sd -- socket descriptor 602 ** timeout_ptr -- (ptr to) timeout to use for sending 603 ** ctx -- context structure 604 ** 605 ** Returns: 606 ** MI_SUCCESS/MI_FAILURE 607 */ 608 609 static int 610 sendreply(r, sd, timeout_ptr, ctx) 611 sfsistat r; 612 socket_t sd; 613 struct timeval *timeout_ptr; 614 SMFICTX_PTR ctx; 615 { 616 int ret; 617 unsigned long bit; 618 619 ret = MI_SUCCESS; 620 621 bit = get_nr_bit(ctx->ctx_state); 622 if (bit != 0 && (ctx->ctx_pflags & bit) != 0 && r != SMFIS_NOREPLY) 623 { 624 if (r >= SMFIS_CONTINUE && r < _SMFIS_KEEP) 625 { 626 /* milter said it wouldn't reply, but it lied... */ 627 smi_log(SMI_LOG_ERR, 628 "%s: milter claimed not to reply in state %d but did anyway %d\n", 629 ctx->ctx_smfi->xxfi_name, 630 ctx->ctx_state, r); 631 632 } 633 634 /* 635 ** Force specified behavior, otherwise libmilter 636 ** and MTA will fail to communicate properly. 637 */ 638 639 switch (r) 640 { 641 case SMFIS_CONTINUE: 642 case SMFIS_TEMPFAIL: 643 case SMFIS_REJECT: 644 case SMFIS_DISCARD: 645 case SMFIS_ACCEPT: 646 case SMFIS_SKIP: 647 case _SMFIS_OPTIONS: 648 r = SMFIS_NOREPLY; 649 break; 650 } 651 } 652 653 switch (r) 654 { 655 case SMFIS_CONTINUE: 656 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 0); 657 break; 658 case SMFIS_TEMPFAIL: 659 case SMFIS_REJECT: 660 if (ctx->ctx_reply != NULL && 661 ((r == SMFIS_TEMPFAIL && *ctx->ctx_reply == '4') || 662 (r == SMFIS_REJECT && *ctx->ctx_reply == '5'))) 663 { 664 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_REPLYCODE, 665 ctx->ctx_reply, 666 strlen(ctx->ctx_reply) + 1); 667 free(ctx->ctx_reply); 668 ctx->ctx_reply = NULL; 669 } 670 else 671 { 672 ret = mi_wr_cmd(sd, timeout_ptr, r == SMFIS_REJECT ? 673 SMFIR_REJECT : SMFIR_TEMPFAIL, NULL, 0); 674 } 675 break; 676 case SMFIS_DISCARD: 677 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_DISCARD, NULL, 0); 678 break; 679 case SMFIS_ACCEPT: 680 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_ACCEPT, NULL, 0); 681 break; 682 case SMFIS_SKIP: 683 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_SKIP, NULL, 0); 684 break; 685 case _SMFIS_OPTIONS: 686 { 687 mi_int32 v; 688 size_t len; 689 char *buffer; 690 char buf[MILTER_OPTLEN]; 691 692 v = htonl(ctx->ctx_prot_vers2mta); 693 (void) memcpy(&(buf[0]), (void *) &v, 694 MILTER_LEN_BYTES); 695 v = htonl(ctx->ctx_aflags); 696 (void) memcpy(&(buf[MILTER_LEN_BYTES]), (void *) &v, 697 MILTER_LEN_BYTES); 698 v = htonl(ctx->ctx_pflags2mta); 699 (void) memcpy(&(buf[MILTER_LEN_BYTES * 2]), 700 (void *) &v, MILTER_LEN_BYTES); 701 len = milter_addsymlist(ctx, buf, &buffer); 702 if (buffer != NULL) 703 ret = mi_wr_cmd(sd, timeout_ptr, SMFIC_OPTNEG, 704 buffer, len); 705 else 706 ret = MI_FAILURE; 707 } 708 break; 709 case SMFIS_NOREPLY: 710 if (bit != 0 && 711 (ctx->ctx_pflags & bit) != 0 && 712 (ctx->ctx_mta_pflags & bit) == 0) 713 { 714 /* 715 ** milter doesn't want to send a reply, 716 ** but the MTA doesn't have that feature: fake it. 717 */ 718 719 ret = mi_wr_cmd(sd, timeout_ptr, SMFIR_CONTINUE, NULL, 720 0); 721 } 722 break; 723 default: /* don't send a reply */ 724 break; 725 } 726 return ret; 727 } 728 729 /* 730 ** CLR_MACROS -- clear set of macros starting from a given index 731 ** 732 ** Parameters: 733 ** ctx -- context structure 734 ** m -- index from which to clear all macros 735 ** 736 ** Returns: 737 ** None. 738 */ 739 740 void 741 mi_clr_macros(ctx, m) 742 SMFICTX_PTR ctx; 743 int m; 744 { 745 int i; 746 747 for (i = m; i < MAX_MACROS_ENTRIES; i++) 748 { 749 if (ctx->ctx_mac_ptr[i] != NULL) 750 { 751 free(ctx->ctx_mac_ptr[i]); 752 ctx->ctx_mac_ptr[i] = NULL; 753 } 754 if (ctx->ctx_mac_buf[i] != NULL) 755 { 756 free(ctx->ctx_mac_buf[i]); 757 ctx->ctx_mac_buf[i] = NULL; 758 } 759 } 760 } 761 762 /* 763 ** ST_OPTIONNEG -- negotiate options 764 ** 765 ** Parameters: 766 ** g -- generic argument structure 767 ** 768 ** Returns: 769 ** abort/send options/continue 770 */ 771 772 static int 773 st_optionneg(g) 774 genarg *g; 775 { 776 mi_int32 i, v, fake_pflags; 777 SMFICTX_PTR ctx; 778 int (*fi_negotiate) __P((SMFICTX *, 779 unsigned long, unsigned long, 780 unsigned long, unsigned long, 781 unsigned long *, unsigned long *, 782 unsigned long *, unsigned long *)); 783 784 if (g == NULL || g->a_ctx->ctx_smfi == NULL) 785 return SMFIS_CONTINUE; 786 ctx = g->a_ctx; 787 mi_clr_macros(ctx, g->a_idx + 1); 788 ctx->ctx_prot_vers = SMFI_PROT_VERSION; 789 790 /* check for minimum length */ 791 if (g->a_len < MILTER_OPTLEN) 792 { 793 smi_log(SMI_LOG_ERR, 794 "%s: st_optionneg[%ld]: len too short %d < %d", 795 ctx->ctx_smfi->xxfi_name, 796 (long) ctx->ctx_id, (int) g->a_len, 797 MILTER_OPTLEN); 798 return _SMFIS_ABORT; 799 } 800 801 /* protocol version */ 802 (void) memcpy((void *) &i, (void *) &(g->a_buf[0]), MILTER_LEN_BYTES); 803 v = ntohl(i); 804 805 #define SMFI_PROT_VERSION_MIN 2 806 807 /* check for minimum version */ 808 if (v < SMFI_PROT_VERSION_MIN) 809 { 810 smi_log(SMI_LOG_ERR, 811 "%s: st_optionneg[%ld]: protocol version too old %d < %d", 812 ctx->ctx_smfi->xxfi_name, 813 (long) ctx->ctx_id, v, SMFI_PROT_VERSION_MIN); 814 return _SMFIS_ABORT; 815 } 816 ctx->ctx_mta_prot_vers = v; 817 if (ctx->ctx_prot_vers < ctx->ctx_mta_prot_vers) 818 ctx->ctx_prot_vers2mta = ctx->ctx_prot_vers; 819 else 820 ctx->ctx_prot_vers2mta = ctx->ctx_mta_prot_vers; 821 822 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES]), 823 MILTER_LEN_BYTES); 824 v = ntohl(i); 825 826 /* no flags? set to default value for V1 actions */ 827 if (v == 0) 828 v = SMFI_V1_ACTS; 829 ctx->ctx_mta_aflags = v; /* MTA action flags */ 830 831 (void) memcpy((void *) &i, (void *) &(g->a_buf[MILTER_LEN_BYTES * 2]), 832 MILTER_LEN_BYTES); 833 v = ntohl(i); 834 835 /* no flags? set to default value for V1 protocol */ 836 if (v == 0) 837 v = SMFI_V1_PROT; 838 ctx->ctx_mta_pflags = v; /* MTA protocol flags */ 839 840 /* 841 ** Copy flags from milter struct into libmilter context; 842 ** this variable will be used later on to check whether 843 ** the MTA "actions" can fulfill the milter requirements, 844 ** but it may be overwritten by the negotiate callback. 845 */ 846 847 ctx->ctx_aflags = ctx->ctx_smfi->xxfi_flags; 848 fake_pflags = SMFIP_NR_CONN 849 |SMFIP_NR_HELO 850 |SMFIP_NR_MAIL 851 |SMFIP_NR_RCPT 852 |SMFIP_NR_DATA 853 |SMFIP_NR_UNKN 854 |SMFIP_NR_HDR 855 |SMFIP_NR_EOH 856 |SMFIP_NR_BODY 857 ; 858 859 if (g->a_ctx->ctx_smfi != NULL && 860 (fi_negotiate = g->a_ctx->ctx_smfi->xxfi_negotiate) != NULL) 861 { 862 int r; 863 unsigned long m_aflags, m_pflags, m_f2, m_f3; 864 865 /* 866 ** let milter decide whether the features offered by the 867 ** MTA are "good enough". 868 ** Notes: 869 ** - libmilter can "fake" some features (e.g., SMFIP_NR_HDR) 870 ** - m_f2, m_f3 are for future extensions 871 */ 872 873 m_f2 = m_f3 = 0; 874 m_aflags = ctx->ctx_mta_aflags; 875 m_pflags = ctx->ctx_pflags; 876 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0) 877 m_pflags |= SMFIP_SKIP; 878 r = fi_negotiate(g->a_ctx, 879 ctx->ctx_mta_aflags, 880 ctx->ctx_mta_pflags|fake_pflags, 881 0, 0, 882 &m_aflags, &m_pflags, &m_f2, &m_f3); 883 884 /* 885 ** Types of protocol flags (pflags): 886 ** 1. do NOT send protocol step X 887 ** 2. MTA can do/understand something extra (SKIP, 888 ** send unknown RCPTs) 889 ** 3. MTA can deal with "no reply" for various protocol steps 890 ** Note: this mean that it isn't possible to simply set all 891 ** flags to get "everything": 892 ** setting a flag of type 1 turns off a step 893 ** (it should be the other way around: 894 ** a flag means a protocol step can be sent) 895 ** setting a flag of type 3 requires that milter 896 ** never sends a reply for the corresponding step. 897 ** Summary: the "negation" of protocol flags is causing 898 ** problems, but at least for type 3 there is no simple 899 ** solution. 900 ** 901 ** What should "all options" mean? 902 ** send all protocol steps _except_ those for which there is 903 ** no callback (currently registered in ctx_pflags) 904 ** expect SKIP as return code? Yes 905 ** send unknown RCPTs? No, 906 ** must be explicitly requested? 907 ** "no reply" for some protocol steps? No, 908 ** must be explicitly requested. 909 */ 910 911 if (SMFIS_ALL_OPTS == r) 912 { 913 ctx->ctx_aflags = ctx->ctx_mta_aflags; 914 ctx->ctx_pflags2mta = ctx->ctx_pflags; 915 if ((SMFIP_SKIP & ctx->ctx_mta_pflags) != 0) 916 ctx->ctx_pflags2mta |= SMFIP_SKIP; 917 } 918 else if (r != SMFIS_CONTINUE) 919 { 920 smi_log(SMI_LOG_ERR, 921 "%s: st_optionneg[%ld]: xxfi_negotiate returned %d (protocol options=0x%lx, actions=0x%lx)", 922 ctx->ctx_smfi->xxfi_name, 923 (long) ctx->ctx_id, r, ctx->ctx_mta_pflags, 924 ctx->ctx_mta_aflags); 925 return _SMFIS_ABORT; 926 } 927 else 928 { 929 ctx->ctx_aflags = m_aflags; 930 ctx->ctx_pflags = m_pflags; 931 ctx->ctx_pflags2mta = m_pflags; 932 } 933 934 /* check whether some flags need to be "faked" */ 935 i = ctx->ctx_pflags2mta; 936 if ((ctx->ctx_mta_pflags & i) != i) 937 { 938 unsigned int idx; 939 unsigned long b; 940 941 /* 942 ** If some behavior can be faked (set in fake_pflags), 943 ** but the MTA doesn't support it, then unset 944 ** that flag in the value that is sent to the MTA. 945 */ 946 947 for (idx = 0; idx < 32; idx++) 948 { 949 b = 1 << idx; 950 if ((ctx->ctx_mta_pflags & b) != b && 951 (fake_pflags & b) == b) 952 ctx->ctx_pflags2mta &= ~b; 953 } 954 } 955 } 956 else 957 { 958 /* 959 ** Set the protocol flags based on the values determined 960 ** in mi_listener() which checked the defined callbacks. 961 */ 962 963 ctx->ctx_pflags2mta = ctx->ctx_pflags; 964 } 965 966 /* check whether actions and protocol requirements can be satisfied */ 967 i = ctx->ctx_aflags; 968 if ((i & ctx->ctx_mta_aflags) != i) 969 { 970 smi_log(SMI_LOG_ERR, 971 "%s: st_optionneg[%ld]: 0x%lx does not fulfill action requirements 0x%x", 972 ctx->ctx_smfi->xxfi_name, 973 (long) ctx->ctx_id, ctx->ctx_mta_aflags, i); 974 return _SMFIS_ABORT; 975 } 976 977 i = ctx->ctx_pflags2mta; 978 if ((ctx->ctx_mta_pflags & i) != i) 979 { 980 /* 981 ** Older MTAs do not support some protocol steps. 982 ** As this protocol is a bit "wierd" (it asks for steps 983 ** NOT to be taken/sent) we have to check whether we 984 ** should turn off those "negative" requests. 985 ** Currently these are only SMFIP_NODATA and SMFIP_NOUNKNOWN. 986 */ 987 988 if (bitset(SMFIP_NODATA, ctx->ctx_pflags2mta) && 989 !bitset(SMFIP_NODATA, ctx->ctx_mta_pflags)) 990 ctx->ctx_pflags2mta &= ~SMFIP_NODATA; 991 if (bitset(SMFIP_NOUNKNOWN, ctx->ctx_pflags2mta) && 992 !bitset(SMFIP_NOUNKNOWN, ctx->ctx_mta_pflags)) 993 ctx->ctx_pflags2mta &= ~SMFIP_NOUNKNOWN; 994 i = ctx->ctx_pflags2mta; 995 } 996 997 if ((ctx->ctx_mta_pflags & i) != i) 998 { 999 smi_log(SMI_LOG_ERR, 1000 "%s: st_optionneg[%ld]: 0x%lx does not fulfill protocol requirements 0x%x", 1001 ctx->ctx_smfi->xxfi_name, 1002 (long) ctx->ctx_id, ctx->ctx_mta_pflags, i); 1003 return _SMFIS_ABORT; 1004 } 1005 1006 if (ctx->ctx_dbg > 3) 1007 sm_dprintf("[%ld] milter_negotiate:" 1008 " mta_actions=0x%lx, mta_flags=0x%lx" 1009 " actions=0x%lx, flags=0x%lx\n" 1010 , (long) ctx->ctx_id 1011 , ctx->ctx_mta_aflags, ctx->ctx_mta_pflags 1012 , ctx->ctx_aflags, ctx->ctx_pflags); 1013 1014 return _SMFIS_OPTIONS; 1015 } 1016 1017 /* 1018 ** ST_CONNECTINFO -- receive connection information 1019 ** 1020 ** Parameters: 1021 ** g -- generic argument structure 1022 ** 1023 ** Returns: 1024 ** continue or filter-specified value 1025 */ 1026 1027 static int 1028 st_connectinfo(g) 1029 genarg *g; 1030 { 1031 size_t l; 1032 size_t i; 1033 char *s, family; 1034 unsigned short port = 0; 1035 _SOCK_ADDR sockaddr; 1036 sfsistat (*fi_connect) __P((SMFICTX *, char *, _SOCK_ADDR *)); 1037 1038 if (g == NULL) 1039 return _SMFIS_ABORT; 1040 mi_clr_macros(g->a_ctx, g->a_idx + 1); 1041 if (g->a_ctx->ctx_smfi == NULL || 1042 (fi_connect = g->a_ctx->ctx_smfi->xxfi_connect) == NULL) 1043 return SMFIS_CONTINUE; 1044 1045 s = g->a_buf; 1046 i = 0; 1047 l = g->a_len; 1048 while (s[i] != '\0' && i <= l) 1049 ++i; 1050 if (i + 1 >= l) 1051 return _SMFIS_ABORT; 1052 1053 /* Move past trailing \0 in host string */ 1054 i++; 1055 family = s[i++]; 1056 (void) memset(&sockaddr, '\0', sizeof sockaddr); 1057 if (family != SMFIA_UNKNOWN) 1058 { 1059 if (i + sizeof port >= l) 1060 { 1061 smi_log(SMI_LOG_ERR, 1062 "%s: connect[%ld]: wrong len %d >= %d", 1063 g->a_ctx->ctx_smfi->xxfi_name, 1064 (long) g->a_ctx->ctx_id, (int) i, (int) l); 1065 return _SMFIS_ABORT; 1066 } 1067 (void) memcpy((void *) &port, (void *) (s + i), 1068 sizeof port); 1069 i += sizeof port; 1070 1071 /* make sure string is terminated */ 1072 if (s[l - 1] != '\0') 1073 return _SMFIS_ABORT; 1074 # if NETINET 1075 if (family == SMFIA_INET) 1076 { 1077 if (inet_aton(s + i, (struct in_addr *) &sockaddr.sin.sin_addr) 1078 != 1) 1079 { 1080 smi_log(SMI_LOG_ERR, 1081 "%s: connect[%ld]: inet_aton failed", 1082 g->a_ctx->ctx_smfi->xxfi_name, 1083 (long) g->a_ctx->ctx_id); 1084 return _SMFIS_ABORT; 1085 } 1086 sockaddr.sa.sa_family = AF_INET; 1087 if (port > 0) 1088 sockaddr.sin.sin_port = port; 1089 } 1090 else 1091 # endif /* NETINET */ 1092 # if NETINET6 1093 if (family == SMFIA_INET6) 1094 { 1095 if (mi_inet_pton(AF_INET6, s + i, 1096 &sockaddr.sin6.sin6_addr) != 1) 1097 { 1098 smi_log(SMI_LOG_ERR, 1099 "%s: connect[%ld]: mi_inet_pton failed", 1100 g->a_ctx->ctx_smfi->xxfi_name, 1101 (long) g->a_ctx->ctx_id); 1102 return _SMFIS_ABORT; 1103 } 1104 sockaddr.sa.sa_family = AF_INET6; 1105 if (port > 0) 1106 sockaddr.sin6.sin6_port = port; 1107 } 1108 else 1109 # endif /* NETINET6 */ 1110 # if NETUNIX 1111 if (family == SMFIA_UNIX) 1112 { 1113 if (sm_strlcpy(sockaddr.sunix.sun_path, s + i, 1114 sizeof sockaddr.sunix.sun_path) >= 1115 sizeof sockaddr.sunix.sun_path) 1116 { 1117 smi_log(SMI_LOG_ERR, 1118 "%s: connect[%ld]: path too long", 1119 g->a_ctx->ctx_smfi->xxfi_name, 1120 (long) g->a_ctx->ctx_id); 1121 return _SMFIS_ABORT; 1122 } 1123 sockaddr.sunix.sun_family = AF_UNIX; 1124 } 1125 else 1126 # endif /* NETUNIX */ 1127 { 1128 smi_log(SMI_LOG_ERR, 1129 "%s: connect[%ld]: unknown family %d", 1130 g->a_ctx->ctx_smfi->xxfi_name, 1131 (long) g->a_ctx->ctx_id, family); 1132 return _SMFIS_ABORT; 1133 } 1134 } 1135 return (*fi_connect)(g->a_ctx, g->a_buf, 1136 family != SMFIA_UNKNOWN ? &sockaddr : NULL); 1137 } 1138 1139 /* 1140 ** ST_EOH -- end of headers 1141 ** 1142 ** Parameters: 1143 ** g -- generic argument structure 1144 ** 1145 ** Returns: 1146 ** continue or filter-specified value 1147 */ 1148 1149 static int 1150 st_eoh(g) 1151 genarg *g; 1152 { 1153 sfsistat (*fi_eoh) __P((SMFICTX *)); 1154 1155 if (g == NULL) 1156 return _SMFIS_ABORT; 1157 if (g->a_ctx->ctx_smfi != NULL && 1158 (fi_eoh = g->a_ctx->ctx_smfi->xxfi_eoh) != NULL) 1159 return (*fi_eoh)(g->a_ctx); 1160 return SMFIS_CONTINUE; 1161 } 1162 1163 /* 1164 ** ST_DATA -- DATA command 1165 ** 1166 ** Parameters: 1167 ** g -- generic argument structure 1168 ** 1169 ** Returns: 1170 ** continue or filter-specified value 1171 */ 1172 1173 static int 1174 st_data(g) 1175 genarg *g; 1176 { 1177 sfsistat (*fi_data) __P((SMFICTX *)); 1178 1179 if (g == NULL) 1180 return _SMFIS_ABORT; 1181 if (g->a_ctx->ctx_smfi != NULL && 1182 (fi_data = g->a_ctx->ctx_smfi->xxfi_data) != NULL) 1183 return (*fi_data)(g->a_ctx); 1184 return SMFIS_CONTINUE; 1185 } 1186 1187 /* 1188 ** ST_HELO -- helo/ehlo command 1189 ** 1190 ** Parameters: 1191 ** g -- generic argument structure 1192 ** 1193 ** Returns: 1194 ** continue or filter-specified value 1195 */ 1196 1197 static int 1198 st_helo(g) 1199 genarg *g; 1200 { 1201 sfsistat (*fi_helo) __P((SMFICTX *, char *)); 1202 1203 if (g == NULL) 1204 return _SMFIS_ABORT; 1205 mi_clr_macros(g->a_ctx, g->a_idx + 1); 1206 if (g->a_ctx->ctx_smfi != NULL && 1207 (fi_helo = g->a_ctx->ctx_smfi->xxfi_helo) != NULL) 1208 { 1209 /* paranoia: check for terminating '\0' */ 1210 if (g->a_len == 0 || g->a_buf[g->a_len - 1] != '\0') 1211 return MI_FAILURE; 1212 return (*fi_helo)(g->a_ctx, g->a_buf); 1213 } 1214 return SMFIS_CONTINUE; 1215 } 1216 1217 /* 1218 ** ST_HEADER -- header line 1219 ** 1220 ** Parameters: 1221 ** g -- generic argument structure 1222 ** 1223 ** Returns: 1224 ** continue or filter-specified value 1225 */ 1226 1227 static int 1228 st_header(g) 1229 genarg *g; 1230 { 1231 char *hf, *hv; 1232 sfsistat (*fi_header) __P((SMFICTX *, char *, char *)); 1233 1234 if (g == NULL) 1235 return _SMFIS_ABORT; 1236 if (g->a_ctx->ctx_smfi == NULL || 1237 (fi_header = g->a_ctx->ctx_smfi->xxfi_header) == NULL) 1238 return SMFIS_CONTINUE; 1239 if (dec_arg2(g->a_buf, g->a_len, &hf, &hv) == MI_SUCCESS) 1240 return (*fi_header)(g->a_ctx, hf, hv); 1241 else 1242 return _SMFIS_ABORT; 1243 } 1244 1245 #define ARGV_FCT(lf, rf, idx) \ 1246 char **argv; \ 1247 sfsistat (*lf) __P((SMFICTX *, char **)); \ 1248 int r; \ 1249 \ 1250 if (g == NULL) \ 1251 return _SMFIS_ABORT; \ 1252 mi_clr_macros(g->a_ctx, g->a_idx + 1); \ 1253 if (g->a_ctx->ctx_smfi == NULL || \ 1254 (lf = g->a_ctx->ctx_smfi->rf) == NULL) \ 1255 return SMFIS_CONTINUE; \ 1256 if ((argv = dec_argv(g->a_buf, g->a_len)) == NULL) \ 1257 return _SMFIS_ABORT; \ 1258 r = (*lf)(g->a_ctx, argv); \ 1259 free(argv); \ 1260 return r; 1261 1262 /* 1263 ** ST_SENDER -- MAIL FROM command 1264 ** 1265 ** Parameters: 1266 ** g -- generic argument structure 1267 ** 1268 ** Returns: 1269 ** continue or filter-specified value 1270 */ 1271 1272 static int 1273 st_sender(g) 1274 genarg *g; 1275 { 1276 ARGV_FCT(fi_envfrom, xxfi_envfrom, CI_MAIL) 1277 } 1278 1279 /* 1280 ** ST_RCPT -- RCPT TO command 1281 ** 1282 ** Parameters: 1283 ** g -- generic argument structure 1284 ** 1285 ** Returns: 1286 ** continue or filter-specified value 1287 */ 1288 1289 static int 1290 st_rcpt(g) 1291 genarg *g; 1292 { 1293 ARGV_FCT(fi_envrcpt, xxfi_envrcpt, CI_RCPT) 1294 } 1295 1296 /* 1297 ** ST_UNKNOWN -- unrecognized or unimplemented command 1298 ** 1299 ** Parameters: 1300 ** g -- generic argument structure 1301 ** 1302 ** Returns: 1303 ** continue or filter-specified value 1304 */ 1305 1306 static int 1307 st_unknown(g) 1308 genarg *g; 1309 { 1310 sfsistat (*fi_unknown) __P((SMFICTX *, const char *)); 1311 1312 if (g == NULL) 1313 return _SMFIS_ABORT; 1314 if (g->a_ctx->ctx_smfi != NULL && 1315 (fi_unknown = g->a_ctx->ctx_smfi->xxfi_unknown) != NULL) 1316 return (*fi_unknown)(g->a_ctx, (const char *) g->a_buf); 1317 return SMFIS_CONTINUE; 1318 } 1319 1320 /* 1321 ** ST_MACROS -- deal with macros received from the MTA 1322 ** 1323 ** Parameters: 1324 ** g -- generic argument structure 1325 ** 1326 ** Returns: 1327 ** continue/keep 1328 ** 1329 ** Side effects: 1330 ** set pointer in macro array to current values. 1331 */ 1332 1333 static int 1334 st_macros(g) 1335 genarg *g; 1336 { 1337 int i; 1338 char **argv; 1339 1340 if (g == NULL || g->a_len < 1) 1341 return _SMFIS_FAIL; 1342 if ((argv = dec_argv(g->a_buf + 1, g->a_len - 1)) == NULL) 1343 return _SMFIS_FAIL; 1344 switch (g->a_buf[0]) 1345 { 1346 case SMFIC_CONNECT: 1347 i = CI_CONN; 1348 break; 1349 case SMFIC_HELO: 1350 i = CI_HELO; 1351 break; 1352 case SMFIC_MAIL: 1353 i = CI_MAIL; 1354 break; 1355 case SMFIC_RCPT: 1356 i = CI_RCPT; 1357 break; 1358 case SMFIC_DATA: 1359 i = CI_DATA; 1360 break; 1361 case SMFIC_BODYEOB: 1362 i = CI_EOM; 1363 break; 1364 case SMFIC_EOH: 1365 i = CI_EOH; 1366 break; 1367 default: 1368 free(argv); 1369 return _SMFIS_FAIL; 1370 } 1371 if (g->a_ctx->ctx_mac_ptr[i] != NULL) 1372 free(g->a_ctx->ctx_mac_ptr[i]); 1373 if (g->a_ctx->ctx_mac_buf[i] != NULL) 1374 free(g->a_ctx->ctx_mac_buf[i]); 1375 g->a_ctx->ctx_mac_ptr[i] = argv; 1376 g->a_ctx->ctx_mac_buf[i] = g->a_buf; 1377 return _SMFIS_KEEP; 1378 } 1379 1380 /* 1381 ** ST_QUIT -- quit command 1382 ** 1383 ** Parameters: 1384 ** g -- generic argument structure 1385 ** 1386 ** Returns: 1387 ** noreply 1388 */ 1389 1390 /* ARGSUSED */ 1391 static int 1392 st_quit(g) 1393 genarg *g; 1394 { 1395 sfsistat (*fi_close) __P((SMFICTX *)); 1396 1397 if (g == NULL) 1398 return _SMFIS_ABORT; 1399 if (g->a_ctx->ctx_smfi != NULL && 1400 (fi_close = g->a_ctx->ctx_smfi->xxfi_close) != NULL) 1401 (void) (*fi_close)(g->a_ctx); 1402 mi_clr_macros(g->a_ctx, 0); 1403 return _SMFIS_NOREPLY; 1404 } 1405 1406 /* 1407 ** ST_BODYCHUNK -- deal with a piece of the mail body 1408 ** 1409 ** Parameters: 1410 ** g -- generic argument structure 1411 ** 1412 ** Returns: 1413 ** continue or filter-specified value 1414 */ 1415 1416 static int 1417 st_bodychunk(g) 1418 genarg *g; 1419 { 1420 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t)); 1421 1422 if (g == NULL) 1423 return _SMFIS_ABORT; 1424 if (g->a_ctx->ctx_smfi != NULL && 1425 (fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL) 1426 return (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf, 1427 g->a_len); 1428 return SMFIS_CONTINUE; 1429 } 1430 1431 /* 1432 ** ST_BODYEND -- deal with the last piece of the mail body 1433 ** 1434 ** Parameters: 1435 ** g -- generic argument structure 1436 ** 1437 ** Returns: 1438 ** continue or filter-specified value 1439 ** 1440 ** Side effects: 1441 ** sends a reply for the body part (if non-empty). 1442 */ 1443 1444 static int 1445 st_bodyend(g) 1446 genarg *g; 1447 { 1448 sfsistat r; 1449 sfsistat (*fi_body) __P((SMFICTX *, unsigned char *, size_t)); 1450 sfsistat (*fi_eom) __P((SMFICTX *)); 1451 1452 if (g == NULL) 1453 return _SMFIS_ABORT; 1454 r = SMFIS_CONTINUE; 1455 if (g->a_ctx->ctx_smfi != NULL) 1456 { 1457 if ((fi_body = g->a_ctx->ctx_smfi->xxfi_body) != NULL && 1458 g->a_len > 0) 1459 { 1460 socket_t sd; 1461 struct timeval timeout; 1462 1463 timeout.tv_sec = g->a_ctx->ctx_timeout; 1464 timeout.tv_usec = 0; 1465 sd = g->a_ctx->ctx_sd; 1466 r = (*fi_body)(g->a_ctx, (unsigned char *)g->a_buf, 1467 g->a_len); 1468 if (r != SMFIS_CONTINUE && 1469 sendreply(r, sd, &timeout, g->a_ctx) != MI_SUCCESS) 1470 return _SMFIS_ABORT; 1471 } 1472 } 1473 if (r == SMFIS_CONTINUE && 1474 (fi_eom = g->a_ctx->ctx_smfi->xxfi_eom) != NULL) 1475 return (*fi_eom)(g->a_ctx); 1476 return r; 1477 } 1478 1479 /* 1480 ** ST_ABORTFCT -- deal with aborts 1481 ** 1482 ** Parameters: 1483 ** g -- generic argument structure 1484 ** 1485 ** Returns: 1486 ** abort or filter-specified value 1487 */ 1488 1489 static int 1490 st_abortfct(g) 1491 genarg *g; 1492 { 1493 sfsistat (*fi_abort) __P((SMFICTX *)); 1494 1495 if (g == NULL) 1496 return _SMFIS_ABORT; 1497 if (g != NULL && g->a_ctx->ctx_smfi != NULL && 1498 (fi_abort = g->a_ctx->ctx_smfi->xxfi_abort) != NULL) 1499 (void) (*fi_abort)(g->a_ctx); 1500 return _SMFIS_NOREPLY; 1501 } 1502 1503 /* 1504 ** TRANS_OK -- is the state transition ok? 1505 ** 1506 ** Parameters: 1507 ** old -- old state 1508 ** new -- new state 1509 ** 1510 ** Returns: 1511 ** state transition ok 1512 */ 1513 1514 static bool 1515 trans_ok(old, new) 1516 int old, new; 1517 { 1518 int s, n; 1519 1520 s = old; 1521 if (s >= SIZE_NEXT_STATES) 1522 return false; 1523 do 1524 { 1525 /* is this state transition allowed? */ 1526 if ((MI_MASK(new) & next_states[s]) != 0) 1527 return true; 1528 1529 /* 1530 ** no: try next state; 1531 ** this works since the relevant states are ordered 1532 ** strict sequentially 1533 */ 1534 1535 n = s + 1; 1536 if (n >= SIZE_NEXT_STATES) 1537 return false; 1538 1539 /* 1540 ** can we actually "skip" this state? 1541 ** see fix_stm() which sets this bit for those 1542 ** states which the filter program is not interested in 1543 */ 1544 1545 if (bitset(NX_SKIP, next_states[n])) 1546 s = n; 1547 else 1548 return false; 1549 } while (s < SIZE_NEXT_STATES); 1550 return false; 1551 } 1552 1553 /* 1554 ** FIX_STM -- add "skip" bits to the state transition table 1555 ** 1556 ** Parameters: 1557 ** ctx -- context structure 1558 ** 1559 ** Returns: 1560 ** None. 1561 ** 1562 ** Side effects: 1563 ** may change state transition table. 1564 */ 1565 1566 static void 1567 fix_stm(ctx) 1568 SMFICTX_PTR ctx; 1569 { 1570 unsigned long fl; 1571 1572 if (ctx == NULL || ctx->ctx_smfi == NULL) 1573 return; 1574 fl = ctx->ctx_pflags; 1575 if (bitset(SMFIP_NOCONNECT, fl)) 1576 next_states[ST_CONN] |= NX_SKIP; 1577 if (bitset(SMFIP_NOHELO, fl)) 1578 next_states[ST_HELO] |= NX_SKIP; 1579 if (bitset(SMFIP_NOMAIL, fl)) 1580 next_states[ST_MAIL] |= NX_SKIP; 1581 if (bitset(SMFIP_NORCPT, fl)) 1582 next_states[ST_RCPT] |= NX_SKIP; 1583 if (bitset(SMFIP_NOHDRS, fl)) 1584 next_states[ST_HDRS] |= NX_SKIP; 1585 if (bitset(SMFIP_NOEOH, fl)) 1586 next_states[ST_EOHS] |= NX_SKIP; 1587 if (bitset(SMFIP_NOBODY, fl)) 1588 next_states[ST_BODY] |= NX_SKIP; 1589 if (bitset(SMFIP_NODATA, fl)) 1590 next_states[ST_DATA] |= NX_SKIP; 1591 if (bitset(SMFIP_NOUNKNOWN, fl)) 1592 next_states[ST_UNKN] |= NX_SKIP; 1593 } 1594 1595 /* 1596 ** DEC_ARGV -- split a buffer into a list of strings, NULL terminated 1597 ** 1598 ** Parameters: 1599 ** buf -- buffer with several strings 1600 ** len -- length of buffer 1601 ** 1602 ** Returns: 1603 ** array of pointers to the individual strings 1604 */ 1605 1606 static char ** 1607 dec_argv(buf, len) 1608 char *buf; 1609 size_t len; 1610 { 1611 char **s; 1612 size_t i; 1613 int elem, nelem; 1614 1615 nelem = 0; 1616 for (i = 0; i < len; i++) 1617 { 1618 if (buf[i] == '\0') 1619 ++nelem; 1620 } 1621 if (nelem == 0) 1622 return NULL; 1623 1624 /* last entry is only for the name */ 1625 s = (char **)malloc((nelem + 1) * (sizeof *s)); 1626 if (s == NULL) 1627 return NULL; 1628 s[0] = buf; 1629 for (i = 0, elem = 0; i < len && elem < nelem; i++) 1630 { 1631 if (buf[i] == '\0') 1632 { 1633 ++elem; 1634 if (i + 1 >= len) 1635 s[elem] = NULL; 1636 else 1637 s[elem] = &(buf[i + 1]); 1638 } 1639 } 1640 1641 /* overwrite last entry (already done above, just paranoia) */ 1642 s[elem] = NULL; 1643 return s; 1644 } 1645 1646 /* 1647 ** DEC_ARG2 -- split a buffer into two strings 1648 ** 1649 ** Parameters: 1650 ** buf -- buffer with two strings 1651 ** len -- length of buffer 1652 ** s1,s2 -- pointer to result strings 1653 ** 1654 ** Returns: 1655 ** MI_FAILURE/MI_SUCCESS 1656 */ 1657 1658 static int 1659 dec_arg2(buf, len, s1, s2) 1660 char *buf; 1661 size_t len; 1662 char **s1; 1663 char **s2; 1664 { 1665 size_t i; 1666 1667 /* paranoia: check for terminating '\0' */ 1668 if (len == 0 || buf[len - 1] != '\0') 1669 return MI_FAILURE; 1670 *s1 = buf; 1671 for (i = 1; i < len && buf[i] != '\0'; i++) 1672 continue; 1673 if (i >= len - 1) 1674 return MI_FAILURE; 1675 *s2 = buf + i + 1; 1676 return MI_SUCCESS; 1677 } 1678 1679 /* 1680 ** SENDOK -- is it ok for the filter to send stuff to the MTA? 1681 ** 1682 ** Parameters: 1683 ** ctx -- context structure 1684 ** flag -- flag to check 1685 ** 1686 ** Returns: 1687 ** sending allowed (in current state) 1688 */ 1689 1690 bool 1691 mi_sendok(ctx, flag) 1692 SMFICTX_PTR ctx; 1693 int flag; 1694 { 1695 if (ctx == NULL || ctx->ctx_smfi == NULL) 1696 return false; 1697 1698 /* did the milter request this operation? */ 1699 if (flag != 0 && !bitset(flag, ctx->ctx_aflags)) 1700 return false; 1701 1702 /* are we in the correct state? It must be "End of Message". */ 1703 return ctx->ctx_state == ST_ENDM; 1704 } 1705 1706 #if _FFR_WORKERS_POOL 1707 /* 1708 ** MI_RD_SOCKET_READY - checks if the socket is ready for read(2) 1709 ** 1710 ** Parameters: 1711 ** sd -- socket_t 1712 ** 1713 ** Returns: 1714 ** true iff socket is ready for read(2) 1715 */ 1716 1717 #define MI_RD_CMD_TO 1 1718 #define MI_RD_MAX_ERR 16 1719 1720 static bool 1721 mi_rd_socket_ready (sd) 1722 socket_t sd; 1723 { 1724 int n; 1725 int nerr = 0; 1726 #if SM_CONF_POLL 1727 struct pollfd pfd; 1728 #else /* SM_CONF_POLL */ 1729 fd_set rd_set, exc_set; 1730 #endif /* SM_CONF_POLL */ 1731 1732 do 1733 { 1734 #if SM_CONF_POLL 1735 pfd.fd = sd; 1736 pfd.events = POLLIN; 1737 pfd.revents = 0; 1738 1739 n = poll(&pfd, 1, MI_RD_CMD_TO); 1740 #else /* SM_CONF_POLL */ 1741 struct timeval timeout; 1742 1743 FD_ZERO(&rd_set); 1744 FD_ZERO(&exc_set); 1745 FD_SET(sd, &rd_set); 1746 FD_SET(sd, &exc_set); 1747 1748 timeout.tv_sec = MI_RD_CMD_TO / 1000; 1749 timeout.tv_usec = 0; 1750 n = select(sd + 1, &rd_set, NULL, &exc_set, &timeout); 1751 #endif /* SM_CONF_POLL */ 1752 1753 if (n < 0) 1754 { 1755 if (errno == EINTR) 1756 { 1757 nerr++; 1758 continue; 1759 } 1760 return true; 1761 } 1762 1763 if (n == 0) 1764 return false; 1765 break; 1766 } while (nerr < MI_RD_MAX_ERR); 1767 if (nerr >= MI_RD_MAX_ERR) 1768 return false; 1769 1770 #if SM_CONF_POLL 1771 return (pfd.revents != 0); 1772 #else /* SM_CONF_POLL */ 1773 return FD_ISSET(sd, &rd_set) || FD_ISSET(sd, &exc_set); 1774 #endif /* SM_CONF_POLL */ 1775 } 1776 #endif /* _FFR_WORKERS_POOL */ 1777