masqmail

annotate src/accept.c @ 370:d86d7b4b8995

Broke long lines.
author markus schnalke <meillo@marmaro.de>
date Tue, 25 Oct 2011 13:50:27 +0200
parents 41958685480d
children 9bc3e47b0222
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@10 35 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
meillo@10 36 gchar *p = res + len;
meillo@14 37 *p = '\0';
meillo@10 38 while (p > res) {
meillo@10 39 *(--p) = base62_chars[value % 62];
meillo@10 40 value /= 62;
meillo@10 41 }
meillo@10 42 return res;
meillo@0 43 }
meillo@0 44
meillo@10 45 static gint
meillo@10 46 _g_list_addr_isequal(gconstpointer a, gconstpointer b)
meillo@0 47 {
meillo@10 48 address *addr1 = (address *) a;
meillo@10 49 address *addr2 = (address *) b;
meillo@10 50 int ret;
meillo@0 51
meillo@270 52 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0) {
meillo@10 53 return strcmp(addr1->local_part, addr2->local_part);
meillo@270 54 }
meillo@270 55 return ret;
meillo@0 56 }
meillo@0 57
meillo@367 58 /*
meillo@367 59 ** accept message from anywhere.
meillo@367 60 ** A message from local is indicated by msg->recieved_host == NULL
meillo@367 61 **
meillo@367 62 ** The -t option: With the ACC_RCPT_FROM_HEAD flag the addrs found found
meillo@367 63 ** in To/Cc/Bcc headers are added to the recipient list.
meillo@0 64 */
meillo@0 65
meillo@10 66 accept_error
meillo@366 67 accept_message_stream(FILE *in, message *msg, guint flags)
meillo@0 68 {
meillo@10 69 gchar *line, *line1;
meillo@10 70 int line_size = MAX_DATALINE;
meillo@10 71 gboolean in_headers = TRUE;
meillo@10 72 header *hdr = NULL;
meillo@10 73 gint line_cnt = 0, data_size = 0;
meillo@0 74
meillo@10 75 line = g_malloc(line_size);
meillo@14 76 line[0] = '\0';
meillo@0 77
meillo@10 78 while (TRUE) {
meillo@10 79 int len = read_sockline1(in, &line, &line_size, 5 * 60, READSOCKL_CVT_CRLF);
meillo@0 80
meillo@10 81 line1 = line;
meillo@0 82
meillo@110 83 if ((line[0] == '.') && (!(flags & ACC_DOT_IGNORE))) {
meillo@10 84 if (line[1] == '\n') {
meillo@10 85 g_free(line);
meillo@10 86 break;
meillo@10 87 }
meillo@10 88 line1++;
meillo@10 89 }
meillo@10 90
meillo@270 91 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
meillo@270 92 /* we got an EOF, and the last line was not terminated by a CR */
meillo@270 93 gint len1 = strlen(line1);
meillo@270 94 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
meillo@270 95 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
meillo@270 96 line1[len1] = '\n';
meillo@270 97 line1[len1 + 1] = '\0';
meillo@270 98 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@270 99 data_size += strlen(line1);
meillo@270 100 line_cnt++;
meillo@10 101 }
meillo@270 102 }
meillo@270 103 break;
meillo@270 104
meillo@270 105 } else if (len == -1) {
meillo@270 106 g_free(line);
meillo@270 107 return AERR_EOF;
meillo@270 108
meillo@270 109 } else if (len == -2) {
meillo@270 110 /* should not happen any more */
meillo@270 111 g_free(line);
meillo@270 112 return AERR_OVERFLOW;
meillo@270 113
meillo@270 114 } else if (len == -3) {
meillo@270 115 g_free(line);
meillo@270 116 return AERR_TIMEOUT;
meillo@270 117
meillo@270 118 } else if (len <= 0) {
meillo@270 119 /* does not happen */
meillo@270 120 g_free(line);
meillo@270 121 DEBUG(5) debugf("read_sockline returned %d\n", len);
meillo@270 122 return AERR_UNKNOWN;
meillo@270 123
meillo@270 124 }
meillo@270 125
meillo@270 126 if (in_headers) {
meillo@270 127
meillo@270 128 /* some pop servers send the 'From ' line, skip it: */
meillo@270 129 if (!msg->hdr_list && strncmp(line1, "From ", 5) == 0) {
meillo@270 130 continue;
meillo@270 131 }
meillo@270 132
meillo@270 133 if (line1[0] == ' ' || line1[0] == '\t') {
meillo@270 134 /* continuation of 'folded' header: */
meillo@270 135 if (hdr) {
meillo@366 136 char *cp;
meillo@323 137 cp = g_strconcat(hdr->header, line1, NULL);
meillo@323 138 hdr->value = cp + (hdr->value - hdr->header);
meillo@323 139 free(hdr->header);
meillo@323 140 hdr->header = cp;
meillo@270 141 }
meillo@270 142
meillo@270 143 } else if (line1[0] == '\n') {
meillo@270 144 /* an empty line marks end of headers */
meillo@270 145 in_headers = FALSE;
meillo@10 146 } else {
meillo@270 147 /* in all other cases we expect another header */
meillo@270 148 if ((hdr = get_header(line1))) {
meillo@270 149 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
meillo@10 150 } else {
meillo@367 151 /*
meillo@367 152 ** if get_header() returns NULL,
meillo@367 153 ** no header was recognized,
meillo@367 154 ** so this seems to be the first
meillo@367 155 ** data line of a broken mailer
meillo@367 156 ** which does not send an empty
meillo@367 157 ** line after the headers
meillo@367 158 */
meillo@270 159 in_headers = FALSE;
meillo@270 160 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@10 161 }
meillo@10 162 }
meillo@10 163 } else {
meillo@270 164 /* message body */
meillo@270 165 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@270 166 data_size += strlen(line1);
meillo@270 167 line_cnt++;
meillo@270 168 }
meillo@10 169
meillo@120 170 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
meillo@117 171 DEBUG(4) debugf("accept_message_stream(): "
meillo@117 172 "received %d bytes (conf.max_msg_size=%d)\n",
meillo@117 173 data_size, conf.max_msg_size);
meillo@117 174 return AERR_SIZE;
meillo@117 175 }
meillo@117 176
meillo@0 177 }
meillo@0 178
meillo@270 179 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
meillo@270 180
meillo@270 181 if (!msg->data_list) {
meillo@10 182 /* make sure data list is not NULL: */
meillo@10 183 msg->data_list = g_list_append(NULL, g_strdup(""));
meillo@270 184 }
meillo@270 185 msg->data_list = g_list_reverse(msg->data_list);
meillo@0 186
meillo@10 187 /* we get here after we succesfully received the mail data */
meillo@0 188
meillo@10 189 msg->data_size = data_size;
meillo@10 190 msg->received_time = time(NULL);
meillo@0 191
meillo@10 192 return AERR_OK;
meillo@0 193 }
meillo@0 194
meillo@10 195 accept_error
meillo@366 196 accept_message_prepare(message *msg, guint flags)
meillo@0 197 {
meillo@10 198 struct passwd *passwd = NULL;
meillo@10 199 time_t rec_time = time(NULL);
meillo@0 200
meillo@10 201 DEBUG(5) debugf("accept_message_prepare()\n");
meillo@0 202
meillo@10 203 /* create unique message id */
meillo@10 204 msg->uid = g_malloc(14);
meillo@10 205 string_base62(msg->uid, rec_time, 6);
meillo@10 206 msg->uid[6] = '-';
meillo@298 207 string_base62(msg->uid + 7, getpid(), 3);
meillo@10 208 msg->uid[10] = '-';
meillo@298 209 string_base62(msg->uid + 11, msg->transfer_id, 2);
meillo@298 210 msg->uid[13] = '\0';
meillo@0 211
meillo@298 212 /* if local, get password entry and set return path if missing */
meillo@298 213 if (!msg->received_host) {
meillo@10 214 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
meillo@10 215 msg->ident = g_strdup(passwd->pw_name);
meillo@298 216 if (!msg->return_path) {
meillo@298 217 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
meillo@298 218 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
meillo@298 219 msg->return_path = create_address(path, TRUE);
meillo@298 220 g_free(path);
meillo@298 221 }
meillo@10 222 }
meillo@0 223
meillo@10 224 /* scan headers */
meillo@10 225 {
meillo@10 226 gboolean has_id = FALSE;
meillo@10 227 gboolean has_date = FALSE;
meillo@10 228 gboolean has_sender = FALSE;
meillo@10 229 gboolean has_from = FALSE;
meillo@10 230 gboolean has_to_or_cc = FALSE;
meillo@10 231 GList *hdr_node, *hdr_node_next;
meillo@10 232 header *hdr;
meillo@0 233
meillo@10 234 for (hdr_node = g_list_first(msg->hdr_list);
meillo@298 235 hdr_node;
meillo@298 236 hdr_node = hdr_node_next) {
meillo@10 237 hdr_node_next = g_list_next(hdr_node);
meillo@10 238 hdr = ((header *) (hdr_node->data));
meillo@10 239 DEBUG(5) debugf("scanning headers: %s", hdr->header);
meillo@10 240 switch (hdr->id) {
meillo@10 241 case HEAD_MESSAGE_ID:
meillo@10 242 has_id = TRUE;
meillo@10 243 break;
meillo@10 244 case HEAD_DATE:
meillo@10 245 has_date = TRUE;
meillo@10 246 break;
meillo@10 247 case HEAD_FROM:
meillo@10 248 has_from = TRUE;
meillo@10 249 break;
meillo@10 250 case HEAD_SENDER:
meillo@10 251 has_sender = TRUE;
meillo@10 252 break;
meillo@10 253 case HEAD_TO:
meillo@10 254 case HEAD_CC:
meillo@106 255 has_to_or_cc = TRUE;
meillo@106 256 /* fall through */
meillo@10 257 case HEAD_BCC:
meillo@10 258 if (flags & ACC_RCPT_FROM_HEAD) {
meillo@109 259 /* -t option (see comment above) */
meillo@10 260 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
meillo@10 261 if (hdr->value) {
meillo@10 262 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
meillo@10 263 }
meillo@10 264 }
meillo@106 265 if (hdr->id == HEAD_BCC) {
meillo@10 266 DEBUG(3) debugf("removing 'Bcc' header\n");
meillo@10 267 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@10 268 g_list_free_1(hdr_node);
meillo@10 269 destroy_header(hdr);
meillo@106 270 }
meillo@10 271 break;
meillo@10 272 case HEAD_ENVELOPE_TO:
meillo@10 273 if (flags & ACC_SAVE_ENVELOPE_TO) {
meillo@10 274 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
meillo@22 275 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
meillo@276 276 "X-Orig-Envelope-To: %s", hdr->value));
meillo@10 277 }
meillo@10 278 DEBUG(3) debugf("removing 'Envelope-To' header\n");
meillo@10 279 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@10 280 g_list_free_1(hdr_node);
meillo@10 281 destroy_header(hdr);
meillo@10 282 break;
meillo@10 283 case HEAD_RETURN_PATH:
meillo@10 284 if (flags & ACC_MAIL_FROM_HEAD) {
meillo@10 285 /* usually POP3 accept */
meillo@10 286 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
meillo@10 287 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@10 288 }
meillo@10 289 DEBUG(3) debugf("removing 'Return-Path' header\n");
meillo@10 290 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@10 291 g_list_free_1(hdr_node);
meillo@10 292 destroy_header(hdr);
meillo@10 293 break;
meillo@10 294 default:
meillo@10 295 break; /* make compiler happy */
meillo@10 296 }
meillo@10 297 }
meillo@10 298
meillo@298 299 if (!msg->return_path) {
meillo@367 300 /*
meillo@367 301 ** TODO: do we still need this as we don't fetch
meillo@367 302 ** mail anymore?
meillo@367 303 ** This can happen for pop3 accept only and if no
meillo@367 304 ** Return-Path: header was given
meillo@367 305 */
meillo@10 306 GList *hdr_list;
meillo@10 307 header *hdr;
meillo@10 308
meillo@10 309 DEBUG(3) debugf("return_path == NULL\n");
meillo@10 310
meillo@10 311 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
meillo@298 312 if (!hdr_list) {
meillo@10 313 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
meillo@298 314 }
meillo@10 315 if (hdr_list) {
meillo@10 316 gchar *addr;
meillo@10 317 hdr = (header *) (g_list_first(hdr_list)->data);
meillo@10 318
meillo@10 319 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
meillo@10 320
meillo@10 321 addr = g_strdup(hdr->value);
meillo@10 322 g_strchomp(addr);
meillo@10 323
meillo@299 324 msg->return_path = create_address_qualified(addr, FALSE, msg->received_host);
meillo@298 325 if (msg->return_path) {
meillo@10 326 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@298 327 msg->hdr_list = g_list_append( msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Warning: return path set from %s address\n", hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
meillo@10 328 }
meillo@10 329 g_free(addr);
meillo@10 330 }
meillo@298 331 if (!msg->return_path) {
meillo@298 332 /* no Sender: or From: or
meillo@298 333 create_address_qualified failed */
meillo@10 334 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
meillo@10 335 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@298 336 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Warning: real return path is unknown\n"));
meillo@10 337 }
meillo@10 338 }
meillo@10 339
meillo@10 340 /* here we should have our recipients, fail if not: */
meillo@276 341 if (!msg->rcpt_list) {
meillo@10 342 logwrite(LOG_WARNING, "no recipients found in message\n");
meillo@10 343 return AERR_NORCPT;
meillo@10 344 }
meillo@10 345
meillo@299 346 if (!has_sender && !has_from) {
meillo@298 347 DEBUG(3) debugf("adding 'From:' header\n");
meillo@298 348 if (msg->full_sender_name) {
meillo@298 349 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name, msg->return_path->local_part, msg->return_path->domain));
meillo@298 350 } else {
meillo@298 351 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_FROM, "From: <%s@%s>\n", msg->return_path->local_part, msg->return_path->domain));
meillo@298 352 }
meillo@10 353 }
meillo@106 354 if (!has_to_or_cc) {
meillo@106 355 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
meillo@105 356 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
meillo@10 357 }
meillo@10 358 if (!has_date) {
meillo@10 359 DEBUG(3) debugf("adding 'Date:' header\n");
meillo@10 360 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
meillo@10 361 }
meillo@10 362 if (!has_id) {
meillo@10 363 DEBUG(3) debugf("adding 'Message-ID:' header\n");
meillo@298 364 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
meillo@10 365 }
meillo@0 366 }
meillo@10 367
meillo@10 368 /* Received header: */
meillo@10 369 /* At this point because we have to know the rcpts for the 'for' part */
meillo@298 370 /* The `for' part will only be used if exactly one rcpt is present. */
meillo@102 371 gchar *for_string = NULL;
meillo@102 372 header *hdr = NULL;
meillo@10 373
meillo@102 374 DEBUG(3) debugf("adding 'Received:' header\n");
meillo@10 375
meillo@102 376 if (g_list_length(msg->rcpt_list) == 1) {
meillo@102 377 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
meillo@304 378 for_string = g_strdup_printf("\n\tfor %s", addr_string(addr));
meillo@102 379 }
meillo@10 380
meillo@289 381 if (!msg->received_host) {
meillo@102 382 /* received locally */
meillo@289 383 hdr = create_header(HEAD_RECEIVED,
meillo@304 384 "Received: by %s (%s %s, from userid %d)\n\tid %s%s; %s\n",
meillo@304 385 conf.host_name, PACKAGE, VERSION, geteuid(),
meillo@289 386 msg->uid, for_string ? for_string : "", rec_timestamp());
meillo@102 387 } else {
meillo@102 388 /* received from remote */
meillo@102 389 DEBUG(5) debugf("adding 'Received:' header (5)\n");
meillo@289 390 hdr = create_header(HEAD_RECEIVED,
meillo@362 391 #ifdef ENABLE_IDENT
meillo@304 392 "Received: from %s (ident=%s)\n\tby %s with %s (%s %s)\n\tid %s%s; %s\n",
meillo@289 393 msg->received_host, msg->ident ? msg->ident : "unknown",
meillo@362 394 #else
meillo@362 395 "Received: from %s\n\tby %s with %s (%s %s)\n\tid %s%s; %s\n",
meillo@362 396 msg->received_host,
meillo@362 397 #endif
meillo@289 398 conf.host_name, prot_names[msg->received_prot], PACKAGE,
meillo@289 399 VERSION, msg->uid, for_string ? for_string : "",
meillo@289 400 rec_timestamp());
meillo@102 401 }
meillo@102 402 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
meillo@10 403
meillo@102 404 if (for_string)
meillo@102 405 g_free(for_string);
meillo@0 406
meillo@10 407 return AERR_OK;
meillo@0 408 }
meillo@0 409
meillo@10 410 accept_error
meillo@366 411 accept_message(FILE *in, message *msg, guint flags)
meillo@0 412 {
meillo@10 413 accept_error err;
meillo@0 414
meillo@10 415 err = accept_message_stream(in, msg, flags);
meillo@270 416 if (err == AERR_OK) {
meillo@10 417 err = accept_message_prepare(msg, flags);
meillo@270 418 }
meillo@0 419
meillo@10 420 return err;
meillo@0 421 }