masqmail

annotate src/accept.c @ 395:5f0829f8e6c7

Removed log_max_pri limit. It makes no sense.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 19:39:11 +0100
parents 5781ba87df95
children 9b93c0a3bd8c
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 1999-2001 Oliver Kurth
meillo@367 4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
meillo@367 5 **
meillo@367 6 ** This program is free software; you can redistribute it and/or modify
meillo@367 7 ** it under the terms of the GNU General Public License as published by
meillo@367 8 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 9 ** (at your option) any later version.
meillo@367 10 **
meillo@367 11 ** This program is distributed in the hope that it will be useful,
meillo@367 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 14 ** GNU General Public License for more details.
meillo@367 15 **
meillo@367 16 ** You should have received a copy of the GNU General Public License
meillo@367 17 ** along with this program; if not, write to the Free Software
meillo@367 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 19 */
meillo@0 20
meillo@0 21 #include "masqmail.h"
meillo@0 22 #include "readsock.h"
meillo@0 23
meillo@299 24 /* must match PROT_* in masqmail.h */
meillo@10 25 gchar *prot_names[] = {
meillo@10 26 "local",
meillo@299 27 "SMTP",
meillo@299 28 "ESMTP",
meillo@10 29 "(unknown)" /* should not happen, but better than crashing. */
meillo@0 30 };
meillo@0 31
meillo@10 32 static gchar*
meillo@366 33 string_base62(gchar *res, guint value, gchar len)
meillo@0 34 {
meillo@388 35 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
meillo@388 36 "abcdefghijklmnopqrstuvwxyz";
meillo@10 37 gchar *p = res + len;
meillo@14 38 *p = '\0';
meillo@10 39 while (p > res) {
meillo@10 40 *(--p) = base62_chars[value % 62];
meillo@10 41 value /= 62;
meillo@10 42 }
meillo@10 43 return res;
meillo@0 44 }
meillo@0 45
meillo@367 46 /*
meillo@367 47 ** accept message from anywhere.
meillo@367 48 ** A message from local is indicated by msg->recieved_host == NULL
meillo@367 49 **
meillo@367 50 ** The -t option: With the ACC_RCPT_FROM_HEAD flag the addrs found found
meillo@367 51 ** in To/Cc/Bcc headers are added to the recipient list.
meillo@0 52 */
meillo@10 53 accept_error
meillo@366 54 accept_message_stream(FILE *in, message *msg, guint flags)
meillo@0 55 {
meillo@10 56 gchar *line, *line1;
meillo@10 57 int line_size = MAX_DATALINE;
meillo@10 58 gboolean in_headers = TRUE;
meillo@10 59 header *hdr = NULL;
meillo@10 60 gint line_cnt = 0, data_size = 0;
meillo@0 61
meillo@10 62 line = g_malloc(line_size);
meillo@388 63 *line = '\0';
meillo@0 64
meillo@388 65 while (1) {
meillo@388 66 int len = read_sockline1(in, &line, &line_size, 5 * 60,
meillo@388 67 READSOCKL_CVT_CRLF);
meillo@10 68 line1 = line;
meillo@0 69
meillo@388 70 if ((*line == '.') && (!(flags & ACC_DOT_IGNORE))) {
meillo@10 71 if (line[1] == '\n') {
meillo@10 72 g_free(line);
meillo@10 73 break;
meillo@10 74 }
meillo@10 75 line1++;
meillo@10 76 }
meillo@10 77
meillo@388 78 if (len==-1 && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
meillo@388 79 /* at EOF but last line was not terminated by CR */
meillo@388 80 /* some MUAs allow unterminated lines */
meillo@270 81 gint len1 = strlen(line1);
meillo@388 82 if (len1 > 0 && line1[len1-1] != '\n') {
meillo@388 83 line1[len1] = '\n';
meillo@388 84 line1[len1+1] = '\0';
meillo@388 85 msg->data_list = g_list_prepend(msg->data_list,
meillo@388 86 g_strdup(line1));
meillo@388 87 data_size += strlen(line1);
meillo@388 88 line_cnt++;
meillo@270 89 }
meillo@270 90 break;
meillo@270 91
meillo@270 92 } else if (len == -1) {
meillo@270 93 g_free(line);
meillo@270 94 return AERR_EOF;
meillo@270 95
meillo@270 96 } else if (len == -2) {
meillo@270 97 /* should not happen any more */
meillo@270 98 g_free(line);
meillo@270 99 return AERR_OVERFLOW;
meillo@270 100
meillo@270 101 } else if (len == -3) {
meillo@270 102 g_free(line);
meillo@270 103 return AERR_TIMEOUT;
meillo@270 104
meillo@270 105 } else if (len <= 0) {
meillo@270 106 /* does not happen */
meillo@270 107 g_free(line);
meillo@270 108 DEBUG(5) debugf("read_sockline returned %d\n", len);
meillo@270 109 return AERR_UNKNOWN;
meillo@270 110
meillo@270 111 }
meillo@270 112
meillo@270 113 if (in_headers) {
meillo@270 114 /* some pop servers send the 'From ' line, skip it: */
meillo@388 115 if (!msg->hdr_list && strncmp(line1, "From ", 5)==0) {
meillo@270 116 continue;
meillo@270 117 }
meillo@270 118
meillo@388 119 if (*line1 == ' ' || *line1 == '\t') {
meillo@270 120 /* continuation of 'folded' header: */
meillo@270 121 if (hdr) {
meillo@366 122 char *cp;
meillo@388 123 cp = g_strconcat(hdr->header, line1,
meillo@388 124 NULL);
meillo@388 125 hdr->value = cp + (hdr->value -
meillo@388 126 hdr->header);
meillo@323 127 free(hdr->header);
meillo@323 128 hdr->header = cp;
meillo@270 129 }
meillo@388 130 } else if (*line1 == '\n') {
meillo@270 131 /* an empty line marks end of headers */
meillo@270 132 in_headers = FALSE;
meillo@388 133
meillo@388 134 } else if ((hdr = get_header(line1))) {
meillo@388 135 /* another header */
meillo@388 136 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
meillo@10 137 } else {
meillo@388 138 /*
meillo@388 139 ** Should be another header but none was
meillo@388 140 ** recognized, so this seems to be the first
meillo@388 141 ** data line of a broken mailer which does
meillo@388 142 ** not add an empty line after the headers.
meillo@388 143 */
meillo@388 144 in_headers = FALSE;
meillo@388 145 msg->data_list = g_list_prepend(msg->data_list,
meillo@388 146 g_strdup(line1));
meillo@10 147 }
meillo@10 148 } else {
meillo@270 149 /* message body */
meillo@388 150 msg->data_list = g_list_prepend(msg->data_list,
meillo@388 151 g_strdup(line1));
meillo@270 152 data_size += strlen(line1);
meillo@270 153 line_cnt++;
meillo@270 154 }
meillo@10 155
meillo@120 156 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
meillo@117 157 DEBUG(4) debugf("accept_message_stream(): "
meillo@117 158 "received %d bytes (conf.max_msg_size=%d)\n",
meillo@117 159 data_size, conf.max_msg_size);
meillo@117 160 return AERR_SIZE;
meillo@117 161 }
meillo@0 162 }
meillo@388 163 DEBUG(4) debugf("received %d lines of data (%d bytes)\n",
meillo@388 164 line_cnt, data_size);
meillo@270 165
meillo@270 166 if (!msg->data_list) {
meillo@388 167 /* make sure data list is not NULL */
meillo@10 168 msg->data_list = g_list_append(NULL, g_strdup(""));
meillo@270 169 }
meillo@270 170 msg->data_list = g_list_reverse(msg->data_list);
meillo@0 171
meillo@388 172 /* we have succesfully received the mail data */
meillo@0 173
meillo@10 174 msg->data_size = data_size;
meillo@10 175 msg->received_time = time(NULL);
meillo@0 176
meillo@10 177 return AERR_OK;
meillo@0 178 }
meillo@0 179
meillo@388 180 static void
meillo@388 181 ensure_return_path(message *msg)
meillo@388 182 {
meillo@388 183 GList *hdr_list;
meillo@388 184 header *hdr;
meillo@388 185 gchar *addr;
meillo@388 186
meillo@388 187 if (msg->return_path) {
meillo@388 188 return;
meillo@388 189 }
meillo@388 190
meillo@388 191 DEBUG(3) debugf("return_path == NULL\n");
meillo@388 192
meillo@388 193 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
meillo@388 194 if (!hdr_list) {
meillo@388 195 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
meillo@388 196 }
meillo@388 197 if (hdr_list) {
meillo@388 198 hdr = (header *) (g_list_first(hdr_list)->data);
meillo@388 199
meillo@388 200 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
meillo@388 201
meillo@388 202 addr = g_strdup(hdr->value);
meillo@388 203 g_strchomp(addr);
meillo@388 204 msg->return_path = create_address_qualified(addr,
meillo@388 205 FALSE, msg->received_host);
meillo@388 206 if (msg->return_path) {
meillo@388 207 DEBUG(3) debugf("setting return_path to %s\n",
meillo@388 208 addr_string(msg->return_path));
meillo@388 209 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 210 create_header(HEAD_UNKNOWN,
meillo@388 211 "X-Warning: return path set from %s "
meillo@388 212 "address\n",
meillo@388 213 (hdr->id == HEAD_SENDER) ?
meillo@388 214 "Sender:" : "From:"));
meillo@388 215 }
meillo@388 216 g_free(addr);
meillo@388 217 }
meillo@388 218 if (!msg->return_path) {
meillo@388 219 /* no Sender: or From: or create_address_qualified failed */
meillo@388 220 msg->return_path = create_address_qualified("postmaster",
meillo@388 221 TRUE, conf.host_name);
meillo@388 222 DEBUG(3) debugf("setting return_path to %s\n",
meillo@388 223 addr_string(msg->return_path));
meillo@388 224 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 225 create_header(HEAD_UNKNOWN,
meillo@388 226 "X-Warning: real return path is unknown\n"));
meillo@388 227 }
meillo@388 228 }
meillo@388 229
meillo@388 230 static accept_error
meillo@388 231 scan_headers(message *msg, guint flags)
meillo@388 232 {
meillo@388 233 gboolean has_id = FALSE;
meillo@388 234 gboolean has_date = FALSE;
meillo@388 235 gboolean has_sender = FALSE;
meillo@388 236 gboolean has_from = FALSE;
meillo@388 237 gboolean has_to_or_cc = FALSE;
meillo@388 238 GList *hdr_node, *hdr_node_next;
meillo@388 239 header *hdr;
meillo@388 240
meillo@388 241 for (hdr_node = g_list_first(msg->hdr_list); hdr_node;
meillo@388 242 hdr_node = hdr_node_next) {
meillo@388 243 hdr_node_next = g_list_next(hdr_node);
meillo@388 244 hdr = ((header *) (hdr_node->data));
meillo@388 245 DEBUG(5) debugf("scanning headers: %s", hdr->header);
meillo@388 246 switch (hdr->id) {
meillo@388 247 case HEAD_MESSAGE_ID:
meillo@388 248 has_id = TRUE;
meillo@388 249 break;
meillo@388 250 case HEAD_DATE:
meillo@388 251 has_date = TRUE;
meillo@388 252 break;
meillo@388 253 case HEAD_FROM:
meillo@388 254 has_from = TRUE;
meillo@388 255 break;
meillo@388 256 case HEAD_SENDER:
meillo@388 257 has_sender = TRUE;
meillo@388 258 break;
meillo@388 259 case HEAD_TO:
meillo@388 260 case HEAD_CC:
meillo@388 261 has_to_or_cc = TRUE;
meillo@388 262 /* fall through */
meillo@388 263 case HEAD_BCC:
meillo@388 264 if (flags & ACC_RCPT_FROM_HEAD) {
meillo@388 265 /* -t option (see comment above) */
meillo@388 266 DEBUG(5) debugf("hdr->value = %s\n",
meillo@388 267 hdr->value);
meillo@388 268 if (hdr->value) {
meillo@388 269 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
meillo@388 270 }
meillo@388 271 }
meillo@388 272 if (hdr->id == HEAD_BCC) {
meillo@388 273 DEBUG(3) debugf("removing 'Bcc' header\n");
meillo@388 274 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@388 275 g_list_free_1(hdr_node);
meillo@388 276 destroy_header(hdr);
meillo@388 277 }
meillo@388 278 break;
meillo@388 279 case HEAD_ENVELOPE_TO:
meillo@388 280 if (flags & ACC_SAVE_ENVELOPE_TO) {
meillo@388 281 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
meillo@388 282 msg->hdr_list = g_list_prepend(msg->hdr_list,
meillo@388 283 create_header(HEAD_UNKNOWN,
meillo@388 284 "X-Orig-Envelope-To: %s",
meillo@388 285 hdr->value));
meillo@388 286 }
meillo@388 287 DEBUG(3) debugf("removing 'Envelope-To' header\n");
meillo@388 288 msg->hdr_list = g_list_remove_link(msg->hdr_list,
meillo@388 289 hdr_node);
meillo@388 290 g_list_free_1(hdr_node);
meillo@388 291 destroy_header(hdr);
meillo@388 292 break;
meillo@388 293 case HEAD_RETURN_PATH:
meillo@388 294 if (flags & ACC_MAIL_FROM_HEAD) {
meillo@388 295 /* usually POP3 accept */
meillo@388 296 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
meillo@388 297 DEBUG(3) debugf("setting return_path to %s\n",
meillo@388 298 addr_string(msg->return_path));
meillo@388 299 }
meillo@388 300 DEBUG(3) debugf("removing 'Return-Path' header\n");
meillo@388 301 msg->hdr_list = g_list_remove_link(msg->hdr_list,
meillo@388 302 hdr_node);
meillo@388 303 g_list_free_1(hdr_node);
meillo@388 304 destroy_header(hdr);
meillo@388 305 break;
meillo@388 306 default:
meillo@388 307 break; /* make compiler happy */
meillo@388 308 }
meillo@388 309 }
meillo@388 310
meillo@388 311 /*
meillo@388 312 ** TODO: do we still need this as we don't fetch
meillo@388 313 ** mail anymore?
meillo@388 314 ** This can happen for pop3 accept only and if no
meillo@388 315 ** Return-Path: header was given
meillo@388 316 */
meillo@388 317 ensure_return_path(msg);
meillo@388 318
meillo@388 319 /* here we should have our recipients, fail if not: */
meillo@388 320 if (!msg->rcpt_list) {
meillo@388 321 logwrite(LOG_WARNING, "no recipients found in message\n");
meillo@388 322 return AERR_NORCPT;
meillo@388 323 }
meillo@388 324
meillo@388 325 if (!has_sender && !has_from) {
meillo@388 326 DEBUG(3) debugf("adding 'From:' header\n");
meillo@388 327 if (msg->full_sender_name) {
meillo@388 328 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 329 create_header(HEAD_FROM,
meillo@388 330 "From: \"%s\" <%s@%s>\n",
meillo@388 331 msg->full_sender_name,
meillo@388 332 msg->return_path->local_part,
meillo@388 333 msg->return_path->domain));
meillo@388 334 } else {
meillo@388 335 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 336 create_header(HEAD_FROM,
meillo@388 337 "From: <%s@%s>\n",
meillo@388 338 msg->return_path->local_part,
meillo@388 339 msg->return_path->domain));
meillo@388 340 }
meillo@388 341 }
meillo@388 342 if (!has_to_or_cc) {
meillo@388 343 DEBUG(3) debugf("no To: or Cc: header, hence adding "
meillo@388 344 "`To: undisclosed recipients:;'\n");
meillo@388 345 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 346 create_header(HEAD_TO,
meillo@388 347 "To: undisclosed-recipients:;\n"));
meillo@388 348 }
meillo@388 349 if (!has_date) {
meillo@388 350 DEBUG(3) debugf("adding 'Date:' header\n");
meillo@388 351 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 352 create_header(HEAD_DATE, "Date: %s\n",
meillo@388 353 rec_timestamp()));
meillo@388 354 }
meillo@388 355 if (!has_id) {
meillo@388 356 DEBUG(3) debugf("adding 'Message-ID:' header\n");
meillo@388 357 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@388 358 create_header(HEAD_MESSAGE_ID,
meillo@388 359 "Message-ID: <%s@%s>\n",
meillo@388 360 msg->uid, conf.host_name));
meillo@388 361 }
meillo@388 362
meillo@388 363 return AERR_OK;
meillo@388 364 }
meillo@388 365
meillo@388 366 static void
meillo@388 367 add_received_hdr(message *msg)
meillo@388 368 {
meillo@388 369 gchar *for_string = NULL;
meillo@388 370 header *hdr = NULL;
meillo@388 371 address *addr;
meillo@388 372
meillo@388 373 DEBUG(3) debugf("adding 'Received:' header\n");
meillo@388 374 if (g_list_length(msg->rcpt_list) == 1) {
meillo@388 375 /* The `for' part only if exactly one rcpt is present */
meillo@388 376 addr = (address *) (g_list_first(msg->rcpt_list)->data);
meillo@388 377 for_string = g_strdup_printf("\n\tfor %s", addr_string(addr));
meillo@388 378 }
meillo@388 379 if (!msg->received_host) {
meillo@388 380 /* received locally */
meillo@388 381 hdr = create_header(HEAD_RECEIVED,
meillo@388 382 "Received: by %s (%s %s, from userid %d)\n"
meillo@388 383 "\tid %s%s; %s\n",
meillo@388 384 conf.host_name, PACKAGE, VERSION, geteuid(),
meillo@388 385 msg->uid,
meillo@388 386 for_string ? for_string : "", rec_timestamp());
meillo@388 387 } else {
meillo@388 388 /* received from remote */
meillo@388 389 DEBUG(5) debugf("adding 'Received:' header (5)\n");
meillo@388 390 hdr = create_header(HEAD_RECEIVED,
meillo@388 391 "Received: from %s\n"
meillo@388 392 "\tby %s with %s (%s %s)\n"
meillo@388 393 "\tid %s%s; %s\n",
meillo@388 394 msg->received_host, conf.host_name,
meillo@388 395 prot_names[msg->received_prot], PACKAGE,
meillo@388 396 VERSION, msg->uid,
meillo@388 397 for_string ? for_string : "", rec_timestamp());
meillo@388 398 }
meillo@388 399 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
meillo@388 400 if (for_string) {
meillo@388 401 g_free(for_string);
meillo@388 402 }
meillo@388 403 }
meillo@388 404
meillo@10 405 accept_error
meillo@366 406 accept_message_prepare(message *msg, guint flags)
meillo@0 407 {
meillo@10 408 DEBUG(5) debugf("accept_message_prepare()\n");
meillo@0 409
meillo@388 410 /* generate unique message id */
meillo@10 411 msg->uid = g_malloc(14);
meillo@388 412 string_base62(msg->uid, time(NULL), 6);
meillo@10 413 msg->uid[6] = '-';
meillo@298 414 string_base62(msg->uid + 7, getpid(), 3);
meillo@10 415 msg->uid[10] = '-';
meillo@298 416 string_base62(msg->uid + 11, msg->transfer_id, 2);
meillo@298 417 msg->uid[13] = '\0';
meillo@0 418
meillo@298 419 /* if local, get password entry and set return path if missing */
meillo@298 420 if (!msg->received_host) {
meillo@388 421 struct passwd *passwd = NULL;
meillo@388 422
meillo@10 423 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
meillo@10 424 msg->ident = g_strdup(passwd->pw_name);
meillo@298 425 if (!msg->return_path) {
meillo@388 426 gchar *path = g_strdup_printf("<%s@%s>",
meillo@388 427 passwd->pw_name, conf.host_name);
meillo@388 428 DEBUG(3) debugf("setting return_path for local "
meillo@388 429 "accept: %s\n", path);
meillo@298 430 msg->return_path = create_address(path, TRUE);
meillo@298 431 g_free(path);
meillo@298 432 }
meillo@10 433 }
meillo@0 434
meillo@388 435 if (scan_headers(msg, flags) == AERR_NORCPT) {
meillo@388 436 return AERR_NORCPT;
meillo@0 437 }
meillo@10 438
meillo@388 439 /* after the hdrs are scanned because we need to know the rcpts */
meillo@388 440 add_received_hdr(msg);
meillo@0 441
meillo@10 442 return AERR_OK;
meillo@0 443 }
meillo@0 444
meillo@10 445 accept_error
meillo@366 446 accept_message(FILE *in, message *msg, guint flags)
meillo@0 447 {
meillo@10 448 accept_error err;
meillo@0 449
meillo@10 450 err = accept_message_stream(in, msg, flags);
meillo@270 451 if (err == AERR_OK) {
meillo@10 452 err = accept_message_prepare(msg, flags);
meillo@270 453 }
meillo@0 454
meillo@10 455 return err;
meillo@0 456 }