[c5c522c] | 1 | /* $Id: dspamc.c,v 1.19 2011/06/28 00:13:48 sbajic Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | DSPAM |
---|
| 5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
---|
| 6 | |
---|
| 7 | This program is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU Affero General Public License as |
---|
| 9 | published by the Free Software Foundation, either version 3 of the |
---|
| 10 | License, or (at your option) any later version. |
---|
| 11 | |
---|
| 12 | This program is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU Affero General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU Affero General Public License |
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * dspamc.c - lightweight dspam client |
---|
| 24 | * |
---|
| 25 | * DESCRIPTION |
---|
| 26 | * The lightweight client build is designed to perform identical to the full |
---|
| 27 | * agent, but without any linkage to libdspam. Instead, the client calls |
---|
| 28 | * the client-based functions (client.c) to perform processing by connecting |
---|
| 29 | * to a DSPAM server process (dspam in --daemon mode). |
---|
| 30 | * |
---|
| 31 | * This code-base is the client-only codebase. See dspam.c for the full |
---|
| 32 | * agent base. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #ifdef HAVE_CONFIG_H |
---|
| 36 | #include <auto-config.h> |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | #include <stdio.h> |
---|
| 40 | #include <stdlib.h> |
---|
| 41 | #include <string.h> |
---|
| 42 | #include <ctype.h> |
---|
| 43 | #include <errno.h> |
---|
| 44 | #ifdef HAVE_UNISTD_H |
---|
| 45 | #include <unistd.h> |
---|
| 46 | #include <pwd.h> |
---|
| 47 | #endif |
---|
| 48 | #include <sys/types.h> |
---|
| 49 | #include <signal.h> |
---|
| 50 | #include <sys/stat.h> |
---|
| 51 | #ifdef _WIN32 |
---|
| 52 | #include <io.h> |
---|
| 53 | #include <process.h> |
---|
| 54 | #define WIDEXITED(x) 1 |
---|
| 55 | #define WEXITSTATUS(x) (x) |
---|
| 56 | #include <windows.h> |
---|
| 57 | #else |
---|
| 58 | #include <sys/wait.h> |
---|
| 59 | #include <sys/param.h> |
---|
| 60 | #endif |
---|
| 61 | #include "config.h" |
---|
| 62 | #include "util.h" |
---|
| 63 | #include "read_config.h" |
---|
| 64 | #ifdef DAEMON |
---|
| 65 | #include "daemon.h" |
---|
| 66 | #include "client.h" |
---|
| 67 | #endif |
---|
| 68 | |
---|
| 69 | #ifdef TIME_WITH_SYS_TIME |
---|
| 70 | # include <sys/time.h> |
---|
| 71 | # include <time.h> |
---|
| 72 | #else |
---|
| 73 | # ifdef HAVE_SYS_TIME_H |
---|
| 74 | # include <sys/time.h> |
---|
| 75 | # else |
---|
| 76 | # include <time.h> |
---|
| 77 | # endif |
---|
| 78 | #endif |
---|
| 79 | |
---|
| 80 | #include "dspamc.h" |
---|
| 81 | #include "agent_shared.h" |
---|
| 82 | #include "pref.h" |
---|
| 83 | #include "libdspam.h" |
---|
| 84 | #include "language.h" |
---|
| 85 | #include "buffer.h" |
---|
| 86 | #include "pref.h" |
---|
| 87 | #include "error.h" |
---|
| 88 | |
---|
| 89 | #ifdef DEBUG |
---|
| 90 | int DO_DEBUG; |
---|
| 91 | char debug_text[1024]; |
---|
| 92 | #endif |
---|
| 93 | |
---|
| 94 | int |
---|
| 95 | main (int argc, char *argv[]) |
---|
| 96 | { |
---|
| 97 | AGENT_CTX ATX; /* agent configuration */ |
---|
| 98 | buffer *message = NULL; /* input Message */ |
---|
| 99 | int agent_init = 0; /* agent is initialized */ |
---|
| 100 | int pwent_cache_init = 0; /* cache for username and uid is initialized */ |
---|
| 101 | int exitcode = EXIT_SUCCESS; |
---|
| 102 | |
---|
| 103 | srand ((long) time(NULL) ^ (long) getpid()); |
---|
| 104 | umask (006); /* rw-rw---- */ |
---|
| 105 | |
---|
| 106 | setbuf (stdout, NULL); /* unbuffered output */ |
---|
| 107 | #ifdef DEBUG |
---|
| 108 | DO_DEBUG = 0; |
---|
| 109 | #endif |
---|
| 110 | |
---|
| 111 | #ifndef DAEMON |
---|
| 112 | LOG(LOG_ERR, ERR_DAEMON_NO_SUPPORT); |
---|
| 113 | exit(EXIT_FAILURE); |
---|
| 114 | #endif |
---|
| 115 | |
---|
| 116 | /* Cache my username and uid for trusted user security */ |
---|
| 117 | |
---|
| 118 | if (!init_pwent_cache()) { |
---|
| 119 | LOG(LOG_ERR, ERR_AGENT_RUNTIME_USER); |
---|
| 120 | exitcode = EXIT_FAILURE; |
---|
| 121 | goto BAIL; |
---|
| 122 | } else |
---|
| 123 | pwent_cache_init = 1; |
---|
| 124 | |
---|
| 125 | /* Read dspam.conf into global config structure (ds_config_t) */ |
---|
| 126 | |
---|
| 127 | agent_config = read_config(NULL); |
---|
| 128 | if (!agent_config) { |
---|
| 129 | LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); |
---|
| 130 | exitcode = EXIT_FAILURE; |
---|
| 131 | goto BAIL; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if (!_ds_read_attribute(agent_config, "Home")) { |
---|
| 135 | LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); |
---|
| 136 | exitcode = EXIT_FAILURE; |
---|
| 137 | goto BAIL; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /* Set up an agent context to define the behavior of the processor */ |
---|
| 141 | |
---|
| 142 | if (initialize_atx(&ATX)) { |
---|
| 143 | LOG(LOG_ERR, ERR_AGENT_INIT_ATX); |
---|
| 144 | exitcode = EXIT_FAILURE; |
---|
| 145 | goto BAIL; |
---|
| 146 | } else { |
---|
| 147 | agent_init = 1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | if (process_arguments(&ATX, argc, argv)) { |
---|
| 151 | LOG(LOG_ERR, ERR_AGENT_INIT_ATX); |
---|
| 152 | exitcode = EXIT_FAILURE; |
---|
| 153 | goto BAIL; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if (apply_defaults(&ATX)) { |
---|
| 157 | LOG(LOG_ERR, ERR_AGENT_INIT_ATX); |
---|
| 158 | exitcode = EXIT_FAILURE; |
---|
| 159 | goto BAIL; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if (check_configuration(&ATX)) { |
---|
| 163 | LOG(LOG_ERR, ERR_AGENT_MISCONFIGURED); |
---|
| 164 | exitcode = EXIT_FAILURE; |
---|
| 165 | goto BAIL; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* Read the message in and apply ParseTo services */ |
---|
| 169 | |
---|
| 170 | message = read_stdin(&ATX); |
---|
| 171 | if (message == NULL) { |
---|
| 172 | exitcode = EXIT_FAILURE; |
---|
| 173 | goto BAIL; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if (ATX.users->items == 0) |
---|
| 177 | { |
---|
| 178 | LOG (LOG_ERR, ERR_AGENT_USER_UNDEFINED); |
---|
| 179 | fprintf (stderr, "%s\n", SYNTAX); |
---|
| 180 | exitcode = EXIT_FAILURE; |
---|
| 181 | goto BAIL; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /* Perform client-based processing */ |
---|
| 185 | |
---|
| 186 | #ifdef DAEMON |
---|
| 187 | if (_ds_read_attribute(agent_config, "ClientIdent") && |
---|
| 188 | (_ds_read_attribute(agent_config, "ClientHost") || |
---|
| 189 | _ds_read_attribute(agent_config, "ServerDomainSocketPath"))) |
---|
| 190 | { |
---|
| 191 | exitcode = client_process(&ATX, message); |
---|
| 192 | if (exitcode<0) { |
---|
| 193 | LOG(LOG_ERR, ERR_CLIENT_EXIT, exitcode); |
---|
| 194 | } |
---|
| 195 | } else { |
---|
| 196 | LOG(LOG_ERR, ERR_CLIENT_INVALID_CONFIG); |
---|
| 197 | exitcode = EINVAL; |
---|
| 198 | } |
---|
| 199 | #endif |
---|
| 200 | |
---|
| 201 | BAIL: |
---|
| 202 | |
---|
| 203 | if (message) |
---|
| 204 | buffer_destroy(message); |
---|
| 205 | |
---|
| 206 | if (agent_init) { |
---|
| 207 | nt_destroy(ATX.users); |
---|
| 208 | nt_destroy(ATX.recipients); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | if (agent_config) |
---|
| 212 | _ds_destroy_config(agent_config); |
---|
| 213 | |
---|
| 214 | if (pwent_cache_init) |
---|
| 215 | free(__pw_name); |
---|
| 216 | |
---|
| 217 | exit (exitcode); |
---|
| 218 | } |
---|