1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
|
/*
* Copyright (c) 2004, Stefan Walter
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*
* CONTRIBUTORS
* Stef Walter <stef@memberwebs.com>
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include "usuals.h"
#include "compat.h"
#include "sock_any.h"
#include "stringx.h"
#include "smtppass.h"
/* -----------------------------------------------------------------------
* STRUCTURES
*/
typedef struct pxstate
{
/* Settings ------------------------------- */
int filter_type; /* Type of filter: pipe, file, reject */
const char* command; /* The command to pipe email through */
const char* reject; /* SMTP code for FILTER_REJECT*/
struct timeval timeout; /* The command timeout */
const char* directory; /* The directory for temp files */
const char* header; /* Header to include in output */
}
pxstate_t;
/* -----------------------------------------------------------------------
* STRINGS
*/
enum {
FILTER_PIPE = 1,
FILTER_FILE = 2,
FILTER_REJECT = 3
};
#define REJECTED "Content Rejected"
#define DEFAULT_REJECT "530 Email Rejected"
#define DEFAULT_CONFIG CONF_PREFIX "/proxsmtpd.conf"
#define DEFAULT_TIMEOUT 30
#define CFG_FILTERCMD "FilterCommand"
#define CFG_FILTERREJECT "FilterReject"
#define CFG_FILTERTYPE "FilterType"
#define CFG_DIRECTORY "TempDirectory"
#define CFG_DEBUGFILES "DebugFiles"
#define CFG_CMDTIMEOUT "FilterTimeout"
#define CFG_HEADER "Header"
#define TYPE_PIPE "pipe"
#define TYPE_FILE "file"
#define TYPE_REJECT "reject"
/* Poll time for waiting operations in milli seconds */
#define POLL_TIME 20
/* read & write ends of a pipe */
#define READ_END 0
#define WRITE_END 1
/* pre-set file descriptors */
#define STDIN 0
#define STDOUT 1
#define STDERR 2
/* -----------------------------------------------------------------------
* GLOBALS
*/
pxstate_t g_pxstate;
/* -----------------------------------------------------------------------
* FORWARD DECLARATIONS
*/
static void usage();
static int process_file_command(spctx_t* sp);
static int process_pipe_command(spctx_t* sp);
static void final_reject_message(char* buf, int buflen);
static void buffer_reject_message(char* data, char* buf, int buflen);
static int kill_process(spctx_t* sp, pid_t pid);
static int wait_process(spctx_t* sp, pid_t pid, int* status);
/* ----------------------------------------------------------------------------------
* STARTUP ETC...
*/
#ifndef HAVE___ARGV
char** __argv;
#endif
int main(int argc, char* argv[])
{
const char* configfile = DEFAULT_CONFIG;
const char* pidfile = NULL;
int dbg_level = -1;
int ch = 0;
int r;
char* t;
#ifndef HAVE___ARGV
__argv = argv;
#endif
/* Setup some defaults */
memset(&g_pxstate, 0, sizeof(g_pxstate));
g_pxstate.directory = _PATH_TMP;
g_pxstate.filter_type = FILTER_PIPE;
g_pxstate.timeout.tv_sec = DEFAULT_TIMEOUT;
g_pxstate.reject = DEFAULT_REJECT;
sp_init("proxsmtpd");
/*
* We still accept our old arguments for compatibility reasons.
* We fill them into the spstate structure directly
*/
/* Parse the arguments nicely */
while((ch = getopt(argc, argv, "d:f:p:v")) != -1)
{
switch(ch)
{
/* Don't daemonize */
case 'd':
dbg_level = strtol(optarg, &t, 10);
if(*t) /* parse error */
errx(1, "invalid debug log level");
dbg_level += LOG_ERR;
break;
/* The configuration file */
case 'f':
configfile = optarg;
break;
/* Write out a pid file */
case 'p':
pidfile = optarg;
break;
/* Print version number */
case 'v':
printf("proxsmtpd (version %s)\n", VERSION);
printf(" (config: %s)\n", DEFAULT_CONFIG);
exit(0);
break;
/* Usage information */
case '?':
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if(argc > 0)
usage();
r = sp_run(configfile, pidfile, dbg_level);
sp_done();
return r;
}
static void usage()
{
fprintf(stderr, "usage: proxsmtpd [-d debuglevel] [-f configfile] [-p pidfile]\n");
fprintf(stderr, " proxsmtpd -v\n");
exit(2);
}
/* ----------------------------------------------------------------------------------
* SP CALLBACKS
*/
int cb_check_pre(spctx_t* ctx)
{
if(g_pxstate.filter_type == FILTER_REJECT)
{
sp_add_log(ctx, "status=", "REJECTED");
if(sp_fail_msg(ctx, g_pxstate.reject) < 0)
return -1; /* Message already printed */
return 0;
}
return 1;
}
int cb_check_data(spctx_t* ctx)
{
int r = 0;
if(g_pxstate.filter_type == FILTER_REJECT)
{
sp_add_log(ctx, "status=", "REJECTED");
if(sp_fail_data(ctx, g_pxstate.reject) < 0)
return -1; /* Message already printed */
return 0;
}
/* Tell client to start sending data */
if(sp_start_data (ctx) < 0)
return -1; /* Message already printed */
if(!g_pxstate.command)
{
sp_messagex(ctx, LOG_WARNING, "no filter command specified. passing message through");
if(sp_cache_data(ctx) == -1 ||
sp_done_data(ctx, g_pxstate.header) == -1)
return -1; /* Message already printed */
return 0;
}
/* Cleanup any old filters hanging around */
while(waitpid(-1, &r, WNOHANG) > 0)
;
if(g_pxstate.filter_type == FILTER_PIPE)
r = process_pipe_command(ctx);
else
r = process_file_command(ctx);
if(r == -1)
{
if(sp_fail_data(ctx, NULL) == -1)
return -1;
}
return 0;
}
int cb_parse_option(const char* name, const char* value)
{
char* t;
if(strcasecmp(CFG_FILTERCMD, name) == 0)
{
g_pxstate.command = value;
return 1;
}
else if(strcasecmp(CFG_DIRECTORY, name) == 0)
{
g_pxstate.directory = value;
return 1;
}
else if(strcasecmp(CFG_CMDTIMEOUT, name) == 0)
{
g_pxstate.timeout.tv_sec = strtol(value, &t, 10);
if(*t || g_pxstate.timeout.tv_sec <= 0)
errx(2, "invalid setting: " CFG_CMDTIMEOUT);
return 1;
}
else if(strcasecmp(CFG_FILTERTYPE, name) == 0)
{
if(strcasecmp(value, TYPE_PIPE) == 0)
g_pxstate.filter_type = FILTER_PIPE;
else if(strcasecmp(value, TYPE_FILE) == 0)
g_pxstate.filter_type = FILTER_FILE;
else if(strcasecmp(value, TYPE_REJECT) == 0)
g_pxstate.filter_type = FILTER_REJECT;
else
errx(2, "invalid value for " CFG_FILTERTYPE
" (must specify '" TYPE_PIPE "' or '" TYPE_FILE "' or '" TYPE_REJECT "')");
return 1;
}
else if(strcasecmp(CFG_FILTERREJECT, name) == 0)
{
g_pxstate.reject = value;
return 1;
}
else if(strcasecmp(CFG_HEADER, name) == 0)
{
g_pxstate.header = trim_start(value);
if(strlen(g_pxstate.header) == 0)
g_pxstate.header = NULL;
return 1;
}
return 0;
}
spctx_t* cb_new_context()
{
spctx_t* ctx = (spctx_t*)calloc(1, sizeof(spctx_t));
if(!ctx)
sp_messagex(NULL, LOG_CRIT, "out of memory");
return ctx;
}
void cb_del_context(spctx_t* ctx)
{
free(ctx);
}
/* -----------------------------------------------------------------------------
* IMPLEMENTATION
*/
static void kill_myself()
{
while (1) {
kill(getpid(), SIGKILL);
sleep(1);
}
}
static pid_t fork_filter(spctx_t* sp, int* infd, int* outfd, int* errfd)
{
pid_t pid;
int ret = 0;
int r = 0;
/* Pipes for input, output, err */
int pipe_i[2];
int pipe_o[2];
int pipe_e[2];
memset(pipe_i, ~0, sizeof(pipe_i));
memset(pipe_o, ~0, sizeof(pipe_o));
memset(pipe_e, ~0, sizeof(pipe_e));
ASSERT(g_pxstate.command);
/* Create the pipes we need */
if((infd && pipe(pipe_i) == -1) ||
(outfd && pipe(pipe_o) == -1) ||
(errfd && pipe(pipe_e) == -1))
{
sp_message(sp, LOG_ERR, "couldn't create pipe for filter command");
RETURN(-1);
}
/* Now fork the pipes across processes */
switch(pid = fork())
{
case -1:
sp_message(sp, LOG_ERR, "couldn't fork for filter command");
RETURN(-1);
/* The child process */
case 0:
if(r >= 0 && infd)
{
close(pipe_i[WRITE_END]);
r = dup2(pipe_i[READ_END], STDIN);
close(pipe_i[READ_END]);
}
if(r >= 0 && outfd)
{
close(pipe_o[READ_END]);
r = dup2(pipe_o[WRITE_END], STDOUT);
close(pipe_o[WRITE_END]);
}
if(r >= 0 && errfd)
{
close(pipe_e[READ_END]);
r = dup2(pipe_e[WRITE_END], STDERR);
close(pipe_e[WRITE_END]);
}
if(r < 0)
{
sp_message(sp, LOG_ERR, "couldn't dup descriptors for filter command");
kill_myself();
}
for (r = 3; r < FD_SETSIZE; ++r)
close(r);
/* All the necessary environment vars */
sp_setup_forked(sp, 1);
/* Now run the filter command */
execl("/bin/sh", "sh", "-c", g_pxstate.command, NULL);
/* If that returned then there was an error */
sp_message(sp, LOG_ERR, "error executing the shell for filter command");
kill_myself();
break;
};
/* The parent process */
sp_messagex(sp, LOG_DEBUG, "executed filter command: %s (pid: %d)", g_pxstate.command, (int)pid);
/* Setup all our return values */
if(infd)
{
*infd = pipe_i[WRITE_END];
pipe_i[WRITE_END] = -1;
fcntl(*infd, F_SETFL, fcntl(*infd, F_GETFL, 0) | O_NONBLOCK);
}
if(outfd)
{
*outfd = pipe_o[READ_END];
pipe_o[READ_END] = -1;
fcntl(*outfd, F_SETFL, fcntl(*outfd, F_GETFL, 0) | O_NONBLOCK);
}
if(errfd)
{
*errfd = pipe_e[READ_END];
pipe_e[READ_END] = -1;
fcntl(*errfd, F_SETFL, fcntl(*errfd, F_GETFL, 0) | O_NONBLOCK);
}
cleanup:
if(pipe_i[READ_END] != -1)
close(pipe_i[READ_END]);
if(pipe_i[WRITE_END] != -1)
close(pipe_i[WRITE_END]);
if(pipe_o[READ_END] != -1)
close(pipe_o[READ_END]);
if(pipe_o[WRITE_END] != -1)
close(pipe_o[WRITE_END]);
if(pipe_e[READ_END] != -1)
close(pipe_e[READ_END]);
if(pipe_e[WRITE_END] != -1)
close(pipe_e[WRITE_END]);
return ret >= 0 ? pid : (pid_t)-1;
}
static int process_file_command(spctx_t* sp)
{
pid_t pid = 0;
int ret = 0, status, r;
struct timeval timeout;
/* For reading data from the process */
int errfd;
fd_set rmask;
char obuf[1024];
char ebuf[256];
memset(ebuf, 0, sizeof(ebuf));
if(sp_cache_data(sp) == -1)
RETURN(-1); /* message already printed */
pid = fork_filter(sp, NULL, NULL, &errfd);
if(pid == (pid_t)-1)
RETURN(-1);
/* Main read write loop */
while(errfd != -1)
{
FD_ZERO(&rmask);
FD_SET(errfd, &rmask);
/* Select can modify the timeout argument so we copy */
memcpy(&timeout, &(g_pxstate.timeout), sizeof(timeout));
r = select(FD_SETSIZE, &rmask, NULL, NULL, &timeout);
switch(r)
{
case -1:
sp_message(sp, LOG_ERR, "couldn't select while listening to filter command");
RETURN(-1);
case 0:
sp_messagex(sp, LOG_ERR, "timeout while listening to filter command");
RETURN(-1);
};
ASSERT(FD_ISSET(errfd, &rmask));
/* Note because we handle as string we save one byte for null-termination */
r = read(errfd, obuf, sizeof(obuf) - 1);
if(r < 0)
{
if(errno != EINTR && errno != EAGAIN)
{
sp_message(sp, LOG_ERR, "couldn't read data from filter command");
RETURN(-1);
}
continue;
}
if(r == 0)
{
close(errfd);
errfd = -1;
break;
}
/* Null terminate */
obuf[r] = 0;
/* And process */
buffer_reject_message(obuf, ebuf, sizeof(ebuf));
if(sp_is_quit())
RETURN(-1);
}
/* exit the process if not completed */
if(wait_process(sp, pid, &status) == -1)
{
sp_messagex(sp, LOG_ERR, "timeout waiting for filter command to exit");
RETURN(-1);
}
pid = 0;
/* We only trust well behaved programs */
if(!WIFEXITED(status))
{
sp_messagex(sp, LOG_ERR, "filter command terminated abnormally");
RETURN(-1);
}
sp_messagex(sp, LOG_DEBUG, "filter exit code: %d", (int)WEXITSTATUS(status));
/* A successful response */
if(WEXITSTATUS(status) == 0)
{
if(sp_done_data(sp, g_pxstate.header) == -1)
RETURN(-1); /* message already printed */
sp_add_log(sp, "status=", "FILTERED");
}
/* Check code and use stderr if bad code */
else
{
final_reject_message(ebuf, sizeof(ebuf));
if(sp_fail_data(sp, ebuf) == -1)
RETURN(-1); /* message already printed */
sp_add_log(sp, "status=", ebuf);
}
ret = 0;
cleanup:
if(pid != 0)
{
sp_messagex(sp, LOG_WARNING, "killing filter process (pid %d)", (int)pid);
kill_process(sp, pid);
}
if(errfd != -1)
close(errfd);
if(ret < 0)
sp_add_log(sp, "status=", "FILTER-ERROR");
return ret;
}
static int process_pipe_command(spctx_t* sp)
{
pid_t pid;
int ret = 0, status, r;
struct timeval timeout;
/* For sending data to the process */
const char* ibuf = NULL;
int ilen = 0;
int infd;
int icount = 0;
fd_set wmask;
/* For reading data from the process */
int outfd;
int errfd;
fd_set rmask;
char obuf[1024];
char ebuf[256];
int ocount = 0;
ASSERT(g_pxstate.command);
memset(ebuf, 0, sizeof(ebuf));
pid = fork_filter(sp, &infd, &outfd, &errfd);
if(pid == (pid_t)-1)
RETURN(-1);
/* Opens cache file */
if(sp_write_data(sp, obuf, 0) == -1)
RETURN(-1); /* message already printed */
/* Main read write loop */
while(infd != -1 || outfd != -1 || errfd != -1)
{
FD_ZERO(&rmask);
FD_ZERO(&wmask);
/* We only select on those that are still open */
if(infd != -1)
FD_SET(infd, &wmask);
if(outfd != -1)
FD_SET(outfd, &rmask);
if(errfd != -1)
FD_SET(errfd, &rmask);
/* Select can modify the timeout argument so we copy */
memcpy(&timeout, &(g_pxstate.timeout), sizeof(timeout));
r = select(FD_SETSIZE, &rmask, &wmask, NULL, &timeout);
switch(r)
{
case -1:
sp_message(sp, LOG_ERR, "couldn't select while listening to filter command");
RETURN(-1);
case 0:
sp_messagex(sp, LOG_WARNING, "timeout while listening to filter command");
RETURN(-1);
};
/* Handling of process's stdin */
if(infd != -1 && FD_ISSET(infd, &wmask))
{
if(ilen <= 0)
{
/* Read some more data into buffer */
switch(r = sp_read_data(sp, &ibuf))
{
case -1:
RETURN(-1); /* Message already printed */
case 0:
close(infd); /* Done with the input */
infd = -1;
break;
default:
ASSERT(r > 0);
ilen = r;
break;
};
}
if(ilen > 0)
{
/* Write data from buffer */
r = write(infd, ibuf, ilen);
if(r == -1)
{
if(errno == EPIPE)
{
sp_messagex(sp, LOG_INFO, "filter command closed input early");
/* Eat up the rest of the data */
while(sp_read_data(sp, &ibuf) > 0)
;
close(infd);
infd = -1;
}
else if(errno != EAGAIN && errno != EINTR)
{
/* Otherwise it's a normal error */
sp_message(sp, LOG_ERR, "couldn't write to filter command");
RETURN(-1);
}
}
/* A good normal write */
else
{
icount += r;
ilen -= r;
ibuf += r;
}
}
}
/* Handling of stdout, which should be email data */
if(outfd != -1 && FD_ISSET(outfd, &rmask))
{
r = read(outfd, obuf, sizeof(obuf));
if(r > 0)
{
if(sp_write_data(sp, obuf, r) == -1)
RETURN(-1); /* message already printed */
ocount += r;
}
else if(r == 0)
{
close(outfd);
outfd = -1;
}
else if(r < 0)
{
if(errno != EINTR && errno != EAGAIN)
{
sp_message(sp, LOG_ERR, "couldn't read data from filter command");
RETURN(-1);
}
}
}
/* Handling of stderr, the last line of which we use as an err message*/
if(errfd != -1 && FD_ISSET(errfd, &rmask))
{
/* Note because we handle as string we save one byte for null-termination */
r = read(errfd, obuf, sizeof(obuf) - 1);
if(r < 0)
{
if(errno != EINTR && errno != EAGAIN)
{
sp_message(sp, LOG_ERR, "couldn't read data from filter command");
RETURN(-1);
}
}
else if(r == 0)
{
close(errfd);
errfd = -1;
}
else if(r > 0)
{
/* Null terminate */
obuf[r] = 0;
/* And process */
buffer_reject_message(obuf, ebuf, sizeof(ebuf));
}
}
if(sp_is_quit())
RETURN(-1);
}
sp_messagex(sp, LOG_DEBUG, "wrote %d bytes to filter, read %d bytes", icount, ocount);
/* Close the cache file */
if(sp_write_data(sp, NULL, 0) == -1)
RETURN(-1); /* message already printed */
if(wait_process(sp, pid, &status) == -1)
{
sp_messagex(sp, LOG_ERR, "timeout waiting for filter command to exit");
RETURN(-1);
}
pid = 0;
/* We only trust well behaved programs */
if(!WIFEXITED(status))
{
sp_messagex(sp, LOG_ERR, "filter command terminated abnormally");
RETURN(-1);
}
sp_messagex(sp, LOG_DEBUG, "filter exit code: %d", (int)WEXITSTATUS(status));
/* A successful response */
if(WEXITSTATUS(status) == 0)
{
if(sp_done_data(sp, g_pxstate.header) == -1)
RETURN(-1); /* message already printed */
sp_add_log(sp, "status=", "FILTERED");
}
/* Check code and use stderr if bad code */
else
{
final_reject_message(ebuf, sizeof(ebuf));
if(sp_fail_data(sp, ebuf) == -1)
RETURN(-1); /* message already printed */
sp_add_log(sp, "status=", ebuf);
}
ret = 0;
cleanup:
if(infd != -1)
close(infd);
if(outfd != -1)
close(outfd);
if(errfd != -1)
close(errfd);
if(pid != 0)
{
sp_messagex(sp, LOG_WARNING, "killing filter process (pid %d)", (int)pid);
kill_process(sp, pid);
}
if(ret < 0)
sp_add_log(sp, "status=", "FILTER-ERROR");
return ret;
}
static void final_reject_message(char* buf, int buflen)
{
if(buf[0] == 0)
strlcpy(buf, REJECTED, buflen);
else
trim_end(buf);
}
static void buffer_reject_message(char* data, char* buf, int buflen)
{
int len = strlen(data);
char* t = data + len;
int newline = 0;
while(t > data && isspace(*(t - 1)))
{
t--;
if(*t == '\n')
newline = 1;
}
/* No valid line */
if(t > data)
{
if(newline)
*t = 0;
t = strrchr(data, '\n');
if(t == NULL)
{
t = trim_start(data);
/*
* Basically if we already have a newline at the end
* then we need to start a new line
*/
if(buf[strlen(buf) - 1] == '\n')
buf[0] = 0;
}
else
{
t = trim_start(t);
/* Start a new line */
buf[0] = 0;
}
/* t points to a valid line */
strlcat(buf, t, buflen);
}
/* Always append if we found a newline */
if(newline)
strlcat(buf, "\n", buflen);
}
static int wait_process(spctx_t* sp, pid_t pid, int* status)
{
/* We poll x times a second */
int waits = g_pxstate.timeout.tv_sec * (1000 / POLL_TIME);
*status = 0;
while(waits > 0)
{
switch(waitpid(pid, status, WNOHANG))
{
case 0:
/* Linux may return 0 if the task has already terminated and was
* caught by waitpid(-1) above, double check it still exists.
*/
if (kill(pid, 0) < 0 && errno == ESRCH)
return 0;
break;
case -1:
if(errno != ECHILD && errno != ESRCH)
{
sp_message(sp, LOG_CRIT, "error waiting on process");
return -1;
}
/* fall through */
default:
return 0;
}
usleep(POLL_TIME * 1000);
waits--;
}
return -1;
}
static int kill_process(spctx_t* sp, pid_t pid)
{
int status;
if(kill(pid, SIGTERM) == -1)
{
if(errno == ESRCH)
return 0;
sp_message(sp, LOG_ERR, "couldn't send signal to process");
return -1;
}
if(wait_process(sp, pid, &status) == -1)
{
if(kill(pid, SIGKILL) == -1)
{
if(errno == ESRCH)
return 0;
sp_message(sp, LOG_ERR, "couldn't send signal to process");
return -1;
}
sp_messagex(sp, LOG_ERR, "process wouldn't quit. forced termination");
}
return 0;
}
|