masqmail-0.2

annotate src/accept.c @ 14:a8f3424347dc

replaced number 0 with character \0 where appropriate
author meillo@marmaro.de
date Wed, 29 Oct 2008 21:21:26 +0100
parents 49dab67fe461
children 7c1635972aa7
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include "readsock.h"
meillo@0 21
meillo@10 22 gchar *prot_names[] = {
meillo@10 23 "local",
meillo@10 24 "bsmtp",
meillo@10 25 "smtp",
meillo@10 26 "esmtp",
meillo@10 27 "pop3",
meillo@10 28 "apop",
meillo@10 29 "(unknown)" /* should not happen, but better than crashing. */
meillo@0 30 };
meillo@0 31
meillo@10 32 static gchar*
meillo@10 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@10 52 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0)
meillo@10 53 return strcmp(addr1->local_part, addr2->local_part);
meillo@10 54 else
meillo@10 55 return ret;
meillo@0 56 }
meillo@0 57
meillo@0 58 /* accept message from anywhere.
meillo@0 59 A locally originating message is indicated by msg->recieved_host == NULL
meillo@0 60
meillo@0 61 If the flags ACC_DEL_RCPTS is set, recipients in the msg->rcpt_list is
meillo@0 62 copied and items occuring in it will be removed from the newly constructed
meillo@0 63 (from To/Cc/Bcc headers if ACC_RCPT_TO is set) rcpt_list.
meillo@0 64 */
meillo@0 65
meillo@10 66 accept_error
meillo@10 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@10 83 if ((line[0] == '.') && (!(flags & ACC_NODOT_TERM))) {
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@10 91 if (len <= 0) {
meillo@10 92 if ((len == -1) && ((flags & ACC_NODOT_TERM) || (flags & ACC_NODOT_RELAX))) {
meillo@10 93 /* we got an EOF, and the last line was not terminated by a CR */
meillo@10 94 gint len1 = strlen(line1);
meillo@10 95 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
meillo@10 96 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
meillo@10 97 line1[len1] = '\n';
meillo@14 98 line1[len1 + 1] = '\0';
meillo@10 99 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@10 100 data_size += strlen(line1);
meillo@10 101 line_cnt++;
meillo@10 102 }
meillo@10 103 }
meillo@10 104 break;
meillo@10 105 } else {
meillo@10 106 g_free(line);
meillo@10 107 if (len == -1) {
meillo@10 108 return AERR_EOF;
meillo@10 109 } else if (len == -2) {
meillo@10 110 /* should not happen any more */
meillo@10 111 return AERR_OVERFLOW;
meillo@10 112 } else if (len == -3) {
meillo@10 113 return AERR_TIMEOUT;
meillo@10 114 } else {
meillo@10 115 /* does not happen */
meillo@10 116 DEBUG(5) debugf("read_sockline returned %d\n", len);
meillo@10 117 return AERR_UNKNOWN;
meillo@10 118 }
meillo@10 119 }
meillo@10 120 } else {
meillo@10 121 if (in_headers) {
meillo@10 122
meillo@10 123 /* some pop servers send the 'From ' line, skip it: */
meillo@10 124 if (msg->hdr_list == NULL)
meillo@10 125 if (strncmp(line1, "From ", 5) == 0)
meillo@10 126 continue;
meillo@10 127
meillo@10 128 if (line1[0] == ' ' || line1[0] == '\t') {
meillo@10 129 /* continuation of 'folded' header: */
meillo@10 130 if (hdr) {
meillo@10 131 hdr->header =
meillo@10 132 g_strconcat(hdr->header, line1, NULL);
meillo@10 133 }
meillo@10 134
meillo@10 135 } else if (line1[0] == '\n') {
meillo@10 136 /* an empty line marks end of headers */
meillo@10 137 in_headers = FALSE;
meillo@10 138 } else {
meillo@10 139 /* in all other cases we expect another header */
meillo@10 140 if ((hdr = get_header(line1)))
meillo@10 141 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
meillo@10 142 else {
meillo@10 143 /* if get_header() returns NULL, no header was recognized,
meillo@10 144 so this seems to be the first data line of a broken mailer
meillo@10 145 which does not send an empty line after the headers */
meillo@10 146 in_headers = FALSE;
meillo@10 147 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@10 148 }
meillo@10 149 }
meillo@10 150 } else {
meillo@10 151 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
meillo@10 152 data_size += strlen(line1);
meillo@10 153 line_cnt++;
meillo@10 154 }
meillo@10 155 }
meillo@0 156 }
meillo@0 157
meillo@10 158 if (msg->data_list != NULL)
meillo@10 159 msg->data_list = g_list_reverse(msg->data_list);
meillo@10 160 else
meillo@10 161 /* make sure data list is not NULL: */
meillo@10 162 msg->data_list = g_list_append(NULL, g_strdup(""));
meillo@0 163
meillo@10 164 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
meillo@10 165 /* we get here after we succesfully received the mail data */
meillo@0 166
meillo@10 167 msg->data_size = data_size;
meillo@10 168 msg->received_time = time(NULL);
meillo@0 169
meillo@10 170 return AERR_OK;
meillo@0 171 }
meillo@0 172
meillo@10 173 accept_error
meillo@10 174 accept_message_prepare(message * msg, guint flags)
meillo@0 175 {
meillo@10 176 struct passwd *passwd = NULL;
meillo@10 177 GList *non_rcpt_list = NULL;
meillo@10 178 time_t rec_time = time(NULL);
meillo@0 179
meillo@10 180 DEBUG(5) debugf("accept_message_prepare()\n");
meillo@0 181
meillo@10 182 /* create unique message id */
meillo@10 183 msg->uid = g_malloc(14);
meillo@0 184
meillo@10 185 string_base62(msg->uid, rec_time, 6);
meillo@10 186 msg->uid[6] = '-';
meillo@10 187 string_base62(&(msg->uid[7]), getpid(), 3);
meillo@10 188 msg->uid[10] = '-';
meillo@10 189 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
meillo@10 190 msg->uid[13] = 0;
meillo@0 191
meillo@10 192 /* if local, get password entry */
meillo@10 193 if (msg->received_host == NULL) {
meillo@10 194 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
meillo@10 195 msg->ident = g_strdup(passwd->pw_name);
meillo@10 196 }
meillo@0 197
meillo@10 198 /* set return path if local */
meillo@10 199 if (msg->return_path == NULL) {
meillo@0 200
meillo@10 201 if (msg->received_host == NULL) {
meillo@10 202 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
meillo@10 203 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
meillo@10 204 msg->return_path = create_address(path, TRUE);
meillo@10 205 g_free(path);
meillo@10 206 }
meillo@10 207 }
meillo@0 208
meillo@10 209 /* -t option */
meillo@10 210 if (flags & ACC_DEL_RCPTS) {
meillo@10 211 non_rcpt_list = msg->rcpt_list;
meillo@10 212 msg->rcpt_list = NULL;
meillo@10 213 }
meillo@0 214
meillo@10 215 /* scan headers */
meillo@10 216 {
meillo@10 217 gboolean has_id = FALSE;
meillo@10 218 gboolean has_date = FALSE;
meillo@10 219 gboolean has_sender = FALSE;
meillo@10 220 gboolean has_from = FALSE;
meillo@10 221 gboolean has_rcpt = FALSE;
meillo@10 222 gboolean has_to_or_cc = FALSE;
meillo@10 223 GList *hdr_node, *hdr_node_next;
meillo@10 224 header *hdr;
meillo@0 225
meillo@10 226 for (hdr_node = g_list_first(msg->hdr_list);
meillo@10 227 hdr_node != NULL; hdr_node = hdr_node_next) {
meillo@10 228 hdr_node_next = g_list_next(hdr_node);
meillo@10 229 hdr = ((header *) (hdr_node->data));
meillo@10 230 DEBUG(5) debugf("scanning headers: %s", hdr->header);
meillo@10 231 switch (hdr->id) {
meillo@10 232 case HEAD_MESSAGE_ID:
meillo@10 233 has_id = TRUE;
meillo@10 234 break;
meillo@10 235 case HEAD_DATE:
meillo@10 236 has_date = TRUE;
meillo@10 237 break;
meillo@10 238 case HEAD_FROM:
meillo@10 239 has_from = TRUE;
meillo@10 240 break;
meillo@10 241 case HEAD_SENDER:
meillo@10 242 has_sender = TRUE;
meillo@10 243 break;
meillo@10 244 case HEAD_TO:
meillo@10 245 case HEAD_CC:
meillo@10 246 case HEAD_BCC:
meillo@10 247 has_rcpt = TRUE;
meillo@10 248 if (flags & ACC_RCPT_FROM_HEAD) {
meillo@10 249 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
meillo@10 250 if (hdr->value) {
meillo@10 251 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
meillo@10 252 }
meillo@10 253 }
meillo@10 254 if ((flags & ACC_DEL_BCC) && (hdr->id == HEAD_BCC)) {
meillo@10 255 DEBUG(3) debugf("removing 'Bcc' header\n");
meillo@10 256 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@10 257 g_list_free_1(hdr_node);
meillo@10 258 destroy_header(hdr);
meillo@10 259 } else
meillo@10 260 has_to_or_cc = TRUE;
meillo@10 261 break;
meillo@10 262 case HEAD_ENVELOPE_TO:
meillo@10 263 if (flags & ACC_SAVE_ENVELOPE_TO) {
meillo@10 264 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
meillo@10 265 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Orig-Envelope-to: %s", hdr->value));
meillo@10 266 }
meillo@10 267 DEBUG(3) debugf("removing 'Envelope-To' header\n");
meillo@10 268 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
meillo@10 269 g_list_free_1(hdr_node);
meillo@10 270 destroy_header(hdr);
meillo@10 271 break;
meillo@10 272 case HEAD_RETURN_PATH:
meillo@10 273 if (flags & ACC_MAIL_FROM_HEAD) {
meillo@10 274 /* usually POP3 accept */
meillo@10 275 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
meillo@10 276 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@10 277 }
meillo@10 278 DEBUG(3) debugf("removing 'Return-Path' 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 default:
meillo@10 284 break; /* make compiler happy */
meillo@10 285 }
meillo@10 286 }
meillo@10 287
meillo@10 288 if (msg->return_path == NULL) {
meillo@10 289 /* this can happen for pop3 accept only and if no Return-path: header was given */
meillo@10 290 GList *hdr_list;
meillo@10 291 header *hdr;
meillo@10 292
meillo@10 293 DEBUG(3) debugf("return_path == NULL\n");
meillo@10 294
meillo@10 295 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
meillo@10 296 if (!hdr_list)
meillo@10 297 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
meillo@10 298 if (hdr_list) {
meillo@10 299 gchar *addr;
meillo@10 300 hdr = (header *) (g_list_first(hdr_list)->data);
meillo@10 301
meillo@10 302 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
meillo@10 303
meillo@10 304 addr = g_strdup(hdr->value);
meillo@10 305 g_strchomp(addr);
meillo@10 306
meillo@10 307 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
meillo@10 308 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@13 309 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
meillo@13 310 "X-Warning: return path set from %s address\n",
meillo@13 311 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
meillo@10 312 }
meillo@10 313 g_free(addr);
meillo@10 314 }
meillo@10 315 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
meillo@10 316 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
meillo@10 317 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
meillo@13 318 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
meillo@13 319 "X-Warning: real return path is unkown\n"));
meillo@10 320 }
meillo@10 321 }
meillo@10 322
meillo@10 323 if (flags & ACC_DEL_RCPTS) {
meillo@10 324 GList *rcpt_node;
meillo@10 325 foreach(non_rcpt_list, rcpt_node) {
meillo@10 326 address *rcpt = (address *) (rcpt_node->data);
meillo@10 327 GList *node;
meillo@10 328 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
meillo@10 329 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
meillo@10 330 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
meillo@10 331 destroy_address((address *) (node->data));
meillo@10 332 g_list_free_1(node);
meillo@10 333 }
meillo@10 334 }
meillo@10 335 }
meillo@10 336
meillo@10 337 /* here we should have our recipients, fail if not: */
meillo@10 338 if (msg->rcpt_list == NULL) {
meillo@10 339 logwrite(LOG_WARNING, "no recipients found in message\n");
meillo@10 340 return AERR_NORCPT;
meillo@10 341 }
meillo@10 342
meillo@10 343 if (!(has_sender || has_from)) {
meillo@10 344 DEBUG(3) debugf("adding 'From' header\n");
meillo@10 345 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@10 346 msg->full_sender_name
meillo@10 347 ?
meillo@13 348 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
meillo@13 349 msg->return_path->local_part, msg->return_path->domain)
meillo@10 350 :
meillo@10 351 create_header(HEAD_FROM, "From: <%s@%s>\n",
meillo@13 352 msg->return_path->local_part, msg->return_path->domain)
meillo@10 353 );
meillo@10 354 }
meillo@10 355 if ((flags & ACC_HEAD_FROM_RCPT) && !has_rcpt) {
meillo@10 356 GList *node;
meillo@10 357 DEBUG(3) debugf("adding 'To' header(s)\n");
meillo@10 358 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
meillo@13 359 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@13 360 create_header(HEAD_TO, "To: %s\n", addr_string(msg-> return_path)));
meillo@10 361 }
meillo@10 362 }
meillo@10 363 if ((flags & ACC_DEL_BCC) && !has_to_or_cc) {
meillo@10 364 /* Bcc headers have been removed, and there are no remaining rcpt headers */
meillo@10 365 DEBUG(3) debugf("adding empty 'Bcc:' header\n");
meillo@10 366 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_BCC, "Bcc:\n"));
meillo@10 367 }
meillo@10 368 if (!has_date) {
meillo@10 369 DEBUG(3) debugf("adding 'Date:' header\n");
meillo@10 370 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
meillo@10 371 }
meillo@10 372 if (!has_id) {
meillo@10 373 DEBUG(3) debugf("adding 'Message-ID:' header\n");
meillo@13 374 msg->hdr_list = g_list_append(msg->hdr_list,
meillo@13 375 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
meillo@10 376 }
meillo@0 377 }
meillo@10 378
meillo@10 379 /* Received header: */
meillo@10 380 /* At this point because we have to know the rcpts for the 'for' part */
meillo@10 381 if (!(flags & ACC_NO_RECVD_HDR)) {
meillo@10 382 gchar *for_string = NULL;
meillo@10 383 header *hdr = NULL;
meillo@10 384
meillo@10 385 DEBUG(3) debugf("adding 'Received:' header\n");
meillo@10 386
meillo@10 387 if (g_list_length(msg->rcpt_list) == 1) {
meillo@10 388 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
meillo@10 389 for_string = g_strdup_printf(" for %s", addr_string(addr));
meillo@10 390 }
meillo@10 391
meillo@10 392 if (msg->received_host == NULL) {
meillo@13 393 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
meillo@13 394 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
meillo@13 395 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
meillo@10 396 } else {
meillo@10 397 #ifdef ENABLE_IDENT
meillo@10 398 DEBUG(5) debugf("adding 'Received:' header (5)\n");
meillo@13 399 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
meillo@13 400 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
meillo@13 401 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
meillo@10 402 rec_timestamp());
meillo@10 403 #else
meillo@13 404 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
meillo@13 405 msg->received_host, conf.host_name, prot_names[msg->received_prot],
meillo@13 406 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
meillo@10 407 #endif
meillo@10 408 }
meillo@10 409 header_fold(hdr);
meillo@10 410 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
meillo@10 411
meillo@10 412 if (for_string)
meillo@10 413 g_free(for_string);
meillo@0 414 }
meillo@0 415
meillo@10 416 /* write message to spool: */
meillo@10 417 /* accept is no longer responsible for this
meillo@13 418 if (!spool_write(msg, TRUE))
meillo@13 419 return AERR_NOSPOOL;
meillo@10 420 */
meillo@10 421 return AERR_OK;
meillo@0 422 }
meillo@0 423
meillo@10 424 accept_error
meillo@10 425 accept_message(FILE * in, message * msg, guint flags)
meillo@0 426 {
meillo@10 427 accept_error err;
meillo@0 428
meillo@10 429 err = accept_message_stream(in, msg, flags);
meillo@10 430 if (err == AERR_OK)
meillo@10 431 err = accept_message_prepare(msg, flags);
meillo@0 432
meillo@10 433 return err;
meillo@0 434 }