annotate src/lda/main.c @ 9434:e374a365521d HEAD

Initial commit for making mail_storage:mailbox_list to be from 1:1 to n:n. This will allow namespaces to share the same storage and a single namespace to have multiple storages (multiple mailbox formats). Neither works currently, and this commit probably breaks some things.
author Timo Sirainen <tss@iki.fi>
date Tue, 02 Jun 2009 22:30:28 -0400
parents e4429faabf59
children 653183a81b6c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (c) 2005-2009 Dovecot authors, see the included COPYING file */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "lib-signals.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "env-util.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "fd-set-nonblock.h"
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
7 #include "close-keep-errno.h"
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "istream.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "istream-seekable.h"
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
10 #include "safe-mkstemp.h"
9372
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
11 #include "eacces-error.h"
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
12 #include "mkdir-parents.h"
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 #include "str.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 #include "str-sanitize.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 #include "strescape.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #include "rfc822-parser.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #include "message-address.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 #include "imap-utf7.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 #include "settings-parser.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 #include "master-service.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 #include "master-service-settings.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 #include "mail-storage-service.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 #include "mail-namespace.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 #include "raw-storage.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 #include "mail-deliver.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 #include "mail-send.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 #include "duplicate.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 #include "mbox-from.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 #include "lda-settings.h"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 #include <stdio.h>
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 #include <stdlib.h>
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 #include <pwd.h>
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 #include <sysexits.h>
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 #ifndef EX_CONFIG
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 # define EX_CONFIG 78 /* for HP-UX */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 #endif
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 #define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 /* After buffer grows larger than this, create a temporary file to /tmp
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 where to read the mail. */
9372
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
44 //#define MAIL_MAX_MEMORY_BUFFER (1024*128)
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
45 #define MAIL_MAX_MEMORY_BUFFER 10
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 static const char *wanted_headers[] = {
9117
a9f61b63dc3d lmtp: Minor cleanup.
Timo Sirainen <tss@iki.fi>
parents: 9106
diff changeset
48 "From", "To", "Message-ID", "Subject", "Return-Path",
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 NULL
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 };
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 static const char *escape_local_part(const char *local_part)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 const char *p;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 /* if there are non-atext chars, we need to return quoted-string */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 for (p = local_part; *p != '\0'; p++) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 if (!IS_ATEXT(*p)) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 return t_strdup_printf("\"%s\"",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60 str_escape(local_part));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 return local_part;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 static const char *address_sanitize(const char *address)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 struct message_address *addr;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 const char *ret, *mailbox;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 pool_t pool;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 pool = pool_alloconly_create("address sanitizer", 256);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 addr = message_address_parse(pool, (const unsigned char *)address,
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 strlen(address), 1, FALSE);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 if (addr == NULL || addr->mailbox == NULL || addr->domain == NULL ||
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 *addr->mailbox == '\0')
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 ret = DEFAULT_ENVELOPE_SENDER;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 else {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 mailbox = escape_local_part(addr->mailbox);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 if (*addr->domain == '\0')
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 ret = t_strdup(mailbox);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 else
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 ret = t_strdup_printf("%s@%s", mailbox, addr->domain);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 pool_unref(&pool);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 return ret;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89
9372
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
90 static int deliver_create_dir(struct mail_user *user, const char *dir)
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
91 {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
92 struct mail_namespace *ns;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
93 mode_t mode;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
94 gid_t gid;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
95
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
96 ns = mail_namespace_find_inbox(user->namespaces);
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
97 if (ns == NULL)
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
98 ns = user->namespaces;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
99
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
100 mailbox_list_get_dir_permissions(ns->list, NULL, &mode, &gid);
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
101 if (mkdir_parents_chown(dir, mode, (uid_t)-1, gid) == 0) {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
102 return 0;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
103 } else if (errno == EACCES) {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
104 i_error("%s", eacces_error_get_creating("mkdir_parents_chown",
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
105 dir));
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
106 return -1;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
107 } else {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
108 i_error("mkdir_parents_chown(%s, gid=%s) failed: %m",
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
109 dir, dec2str(gid));
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
110 return -1;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
111 }
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
112 }
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
113
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
114 static int seekable_fd_callback(const char **path_r, void *context)
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
115 {
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
116 struct mail_deliver_context *ctx = context;
9372
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
117 const char *dir, *p;
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
118 string_t *path;
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
119 int fd;
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
120
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
121 path = t_str_new(128);
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
122 str_append(path, mail_user_get_temp_prefix(ctx->dest_user));
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
123 fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1);
9372
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
124 if (fd == -1 && errno == ENOENT) {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
125 dir = str_c(path);
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
126 p = strrchr(dir, '/');
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
127 if (p != NULL) {
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
128 dir = t_strdup_until(dir, p);
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
129 if (deliver_create_dir(ctx->dest_user, dir) < 0)
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
130 return -1;
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
131 fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1);
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
132 }
b1c584654a0e lda: If temp file can't be created because home dir doesn't exist, create it.
timo@timo-desktop
parents: 9371
diff changeset
133 }
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
134 if (fd == -1) {
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
135 i_error("safe_mkstemp(%s) failed: %m", str_c(path));
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
136 return -1;
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
137 }
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
138
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
139 /* we just want the fd, unlink it */
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
140 if (unlink(str_c(path)) < 0) {
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
141 /* shouldn't happen.. */
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
142 i_error("unlink(%s) failed: %m", str_c(path));
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
143 close_keep_errno(fd);
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
144 return -1;
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
145 }
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
146
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
147 *path_r = str_c(path);
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
148 return fd;
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
149 }
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 static struct istream *
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 create_raw_stream(struct mail_deliver_context *ctx,
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
153 int fd, time_t *mtime_r)
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 struct istream *input, *input2, *input_list[2];
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 const unsigned char *data;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 char *sender = NULL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 size_t i, size;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 int ret, tz;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 *mtime_r = (time_t)-1;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 fd_set_nonblock(fd, FALSE);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 input = i_stream_create_fd(fd, 4096, FALSE);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 input->blocking = TRUE;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 /* If input begins with a From-line, drop it */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 ret = i_stream_read_data(input, &data, &size, 5);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 if (ret > 0 && size >= 5 && memcmp(data, "From ", 5) == 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 /* skip until the first LF */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 i_stream_skip(input, 5);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 for (i = 0; i < size; i++) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 if (data[i] == '\n')
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 if (i != size) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 (void)mbox_from_parse(data, i, mtime_r, &tz,
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 &sender);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 i_stream_skip(input, i + 1);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 i_stream_skip(input, size);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 if (sender != NULL && ctx->src_envelope_sender == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187 /* use the envelope sender from From_-line, but only if it
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 hasn't been specified with -f already. */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 ctx->src_envelope_sender = p_strdup(ctx->pool, sender);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 i_free(sender);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 if (input->v_offset == 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 input2 = input;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 i_stream_ref(input2);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 } else {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197 input2 = i_stream_create_limit(input, (uoff_t)-1);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 i_stream_unref(&input);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 input_list[0] = input2; input_list[1] = NULL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 input = i_stream_create_seekable(input_list, MAIL_MAX_MEMORY_BUFFER,
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
203 seekable_fd_callback, ctx);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 i_stream_unref(&input2);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 return input;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 static void failure_exit_callback(int *status)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 /* we want all our exit codes to be sysexits.h compatible.
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 if we failed because of a logging related error, we most likely
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 aren't writing to stderr, so try writing there to give some kind of
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 a clue what's wrong. FATAL_LOGOPEN failure already wrote to
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 stderr, so don't duplicate it. */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 switch (*status) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 case FATAL_LOGWRITE:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 fputs("Failed to write to log file", stderr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 case FATAL_LOGERROR:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 fputs("Internal logging error", stderr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 case FATAL_LOGOPEN:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 case FATAL_OUTOFMEM:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 case FATAL_EXEC:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 case FATAL_DEFAULT:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 default:
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 return;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 *status = EX_TEMPFAIL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 static void print_help(void)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235 printf(
9103
5c12eac2c3ca s/deliver/lda/
Timo Sirainen <tss@iki.fi>
parents: 9092
diff changeset
236 "Usage: dovecot-lda [-c <config file>] [-a <address>] [-d <username>] [-p <path>]\n"
5c12eac2c3ca s/deliver/lda/
Timo Sirainen <tss@iki.fi>
parents: 9092
diff changeset
237 " [-f <envelope sender>] [-m <mailbox>] [-e] [-k]\n");
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240 int main(int argc, char *argv[])
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241 {
9106
fce3926fe910 mail_storage_service_init*() can now take multiple set_roots.
Timo Sirainen <tss@iki.fi>
parents: 9103
diff changeset
242 const struct setting_parser_info *set_roots[] = {
fce3926fe910 mail_storage_service_init*() can now take multiple set_roots.
Timo Sirainen <tss@iki.fi>
parents: 9103
diff changeset
243 &lda_setting_parser_info,
fce3926fe910 mail_storage_service_init*() can now take multiple set_roots.
Timo Sirainen <tss@iki.fi>
parents: 9103
diff changeset
244 NULL
fce3926fe910 mail_storage_service_init*() can now take multiple set_roots.
Timo Sirainen <tss@iki.fi>
parents: 9103
diff changeset
245 };
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 struct mail_deliver_context ctx;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 enum mail_storage_service_flags service_flags = 0;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248 const char *user, *errstr, *path, *getopt_str;
9147
bf448752f6c4 Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.
Timo Sirainen <tss@iki.fi>
parents: 9117
diff changeset
249 struct mail_storage_service_input service_input;
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 struct mail_user *raw_mail_user;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 struct mail_namespace *raw_ns;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 struct mail_namespace_settings raw_ns_set;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 struct mail_storage *storage;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 struct mailbox *box;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 struct raw_mailbox *raw_box;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256 struct istream *input;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 struct mailbox_transaction_context *t;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 struct mailbox_header_lookup_ctx *headers_ctx;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259 char cwd[PATH_MAX];
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260 void **sets;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 uid_t process_euid;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 bool stderr_rejection = FALSE;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 time_t mtime;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 int ret, c;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 string_t *str;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 enum mail_error error;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 if (getuid() != geteuid() && geteuid() == 0) {
9103
5c12eac2c3ca s/deliver/lda/
Timo Sirainen <tss@iki.fi>
parents: 9092
diff changeset
269 /* running setuid - don't allow this if the binary is
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 executable by anyone */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 struct stat st;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 if (stat(argv[0], &st) < 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 fprintf(stderr, "stat(%s) failed: %s\n",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 argv[0], strerror(errno));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 return EX_CONFIG;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 } else if ((st.st_mode & 1) != 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278 fprintf(stderr, "%s must not be both world-executable "
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279 "and setuid-root. This allows root exploits. "
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 "See http://wiki.dovecot.org/LDA#multipleuids\n",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281 argv[0]);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 return EX_CONFIG;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
285
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 i_set_failure_exit_callback(failure_exit_callback);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
288 master_service = master_service_init("lda",
9371
ad0b052aef5e lda: Log to specified log file, not to stderr.
Timo Sirainen <tss@iki.fi>
parents: 9370
diff changeset
289 MASTER_SERVICE_FLAG_STANDALONE |
ad0b052aef5e lda: Log to specified log file, not to stderr.
Timo Sirainen <tss@iki.fi>
parents: 9370
diff changeset
290 MASTER_SERVICE_FLAG_DONT_LOG_TO_STDERR,
ad0b052aef5e lda: Log to specified log file, not to stderr.
Timo Sirainen <tss@iki.fi>
parents: 9370
diff changeset
291 argc, argv);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 memset(&ctx, 0, sizeof(ctx));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 ctx.pool = pool_alloconly_create("mail deliver context", 256);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295 ctx.dest_mailbox_name = "INBOX";
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 path = NULL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 user = getenv("USER");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 getopt_str = t_strconcat("a:d:p:ekm:nsf:",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 master_service_getopt_string(), NULL);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301 while ((c = getopt(argc, argv, getopt_str)) > 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 switch (c) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 case 'a':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 /* destination address */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 ctx.dest_addr = optarg;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 case 'd':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 /* destination user */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309 user = optarg;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 service_flags |= MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 case 'p':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 /* input path */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 path = optarg;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
315 if (*path != '/') {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
316 /* expand relative paths before we chdir */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 if (getcwd(cwd, sizeof(cwd)) == NULL)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 i_fatal("getcwd() failed: %m");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 path = t_strconcat(cwd, "/", path, NULL);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322 case 'e':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323 stderr_rejection = TRUE;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325 case 'm':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326 /* destination mailbox.
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 Ignore -m "". This allows doing -m ${extension}
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328 in Postfix to handle user+mailbox */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 if (*optarg != '\0') T_BEGIN {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330 str = t_str_new(256);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 if (imap_utf8_to_utf7(optarg, str) < 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 i_fatal("Mailbox name not UTF-8: %s",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333 optarg);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335 ctx.dest_mailbox_name =
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 p_strdup(ctx.pool, str_c(str));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 } T_END;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 case 'f':
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 /* envelope sender address */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341 ctx.src_envelope_sender =
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342 p_strdup(ctx.pool, address_sanitize(optarg));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 default:
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
345 if (!master_service_parse_option(master_service,
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
346 c, optarg)) {
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 print_help();
9159
6324a79d3ee1 Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents: 9147
diff changeset
348 exit(EX_USAGE);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
350 break;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 if (optind != argc) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 print_help();
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355 i_fatal_status(EX_USAGE, "Unknown argument: %s", argv[optind]);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 process_euid = geteuid();
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 if ((service_flags & MAIL_STORAGE_SERVICE_FLAG_USERDB_LOOKUP) != 0)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 ;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 else if (process_euid != 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 /* we're non-root. get our username and possibly our home. */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 struct passwd *pw;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364 const char *home;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 home = getenv("HOME");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367 if (user != NULL && home != NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368 /* no need for a pw lookup */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 } else if ((pw = getpwuid(process_euid)) != NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370 user = t_strdup(pw->pw_name);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 if (home == NULL)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 env_put(t_strconcat("HOME=", pw->pw_dir, NULL));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373 } else if (user == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 i_fatal_status(EX_USAGE,
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 "Couldn't lookup our username (uid=%s)",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
376 dec2str(process_euid));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378 } else {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 i_fatal_status(EX_USAGE,
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 "destination user parameter (-d user) not given");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
381 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382
9147
bf448752f6c4 Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.
Timo Sirainen <tss@iki.fi>
parents: 9117
diff changeset
383 memset(&service_input, 0, sizeof(service_input));
9263
5d0a69504867 config handling fixes and improvements. Separated module/service lookups. Added support for per-lip/rip settings.
Timo Sirainen <tss@iki.fi>
parents: 9159
diff changeset
384 service_input.module = "lda";
5d0a69504867 config handling fixes and improvements. Separated module/service lookups. Added support for per-lip/rip settings.
Timo Sirainen <tss@iki.fi>
parents: 9159
diff changeset
385 service_input.service = "lda";
9147
bf448752f6c4 Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.
Timo Sirainen <tss@iki.fi>
parents: 9117
diff changeset
386 service_input.username = user;
bf448752f6c4 Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.
Timo Sirainen <tss@iki.fi>
parents: 9117
diff changeset
387
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
388 service_flags |= MAIL_STORAGE_SERVICE_FLAG_DISALLOW_ROOT;
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
389 ctx.dest_user = mail_storage_service_init_user(master_service,
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
390 &service_input,
9147
bf448752f6c4 Pass remote/local IPs to mail_users. Standalone mail programs now log with mail_log_prefix.
Timo Sirainen <tss@iki.fi>
parents: 9117
diff changeset
391 set_roots,
9106
fce3926fe910 mail_storage_service_init*() can now take multiple set_roots.
Timo Sirainen <tss@iki.fi>
parents: 9103
diff changeset
392 service_flags);
9159
6324a79d3ee1 Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents: 9147
diff changeset
393 #ifdef SIGXFSZ
6324a79d3ee1 Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents: 9147
diff changeset
394 lib_signals_ignore(SIGXFSZ, TRUE);
6324a79d3ee1 Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents: 9147
diff changeset
395 #endif
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
396 ctx.set = mail_storage_service_get_settings(master_service);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
397 duplicate_init(mail_user_set_get_storage_set(ctx.dest_user->set));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 /* create a separate mail user for the internal namespace */
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
400 if (master_service_set(master_service,
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
401 "mail_full_filesystem_access=yes") < 0)
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402 i_unreached();
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
403 sets = master_service_settings_get_others(master_service);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
404 raw_mail_user = mail_user_alloc(user, sets[0]);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
405 mail_user_set_home(raw_mail_user, "/");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406 if (mail_user_init(raw_mail_user, &errstr) < 0)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
407 i_fatal("Raw user initialization failed: %s", errstr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
408
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 memset(&raw_ns_set, 0, sizeof(raw_ns_set));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410 raw_ns_set.location = "/tmp";
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
411
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
412 raw_ns = mail_namespaces_init_empty(raw_mail_user);
9374
e4429faabf59 Split NAMESPACE_FLAG_INTERNAL into distinct _NOQUOTA and _NOACL flags.
Timo Sirainen <tss@iki.fi>
parents: 9372
diff changeset
413 raw_ns->flags |= NAMESPACE_FLAG_NOQUOTA | NAMESPACE_FLAG_NOACL;
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414 raw_ns->set = &raw_ns_set;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
415 if (mail_storage_create(raw_ns, "raw", 0, &errstr) < 0)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
416 i_fatal("Couldn't create internal raw storage: %s", errstr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
417 if (path == NULL) {
9370
8fa79cdb2ef3 istream-seekable: Changed API to use a callback function to create the temp file.
Timo Sirainen <tss@iki.fi>
parents: 9348
diff changeset
418 input = create_raw_stream(&ctx, 0, &mtime);
9434
e374a365521d Initial commit for making mail_storage:mailbox_list to be from 1:1 to n:n.
Timo Sirainen <tss@iki.fi>
parents: 9374
diff changeset
419 box = mailbox_open(raw_ns->list, "Dovecot Delivery Mail",
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
420 input, MAILBOX_OPEN_NO_INDEX_FILES);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
421 i_stream_unref(&input);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
422 } else {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
423 mtime = (time_t)-1;
9434
e374a365521d Initial commit for making mail_storage:mailbox_list to be from 1:1 to n:n.
Timo Sirainen <tss@iki.fi>
parents: 9374
diff changeset
424 box = mailbox_open(raw_ns->list, path, NULL,
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
425 MAILBOX_OPEN_NO_INDEX_FILES);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
426 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
427 if (box == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
428 i_fatal("Can't open delivery mail as raw: %s",
9434
e374a365521d Initial commit for making mail_storage:mailbox_list to be from 1:1 to n:n.
Timo Sirainen <tss@iki.fi>
parents: 9374
diff changeset
429 mailbox_list_get_last_error(raw_ns->list, &error));
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
431 if (mailbox_sync(box, 0, 0, NULL) < 0) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
432 i_fatal("Can't sync delivery mail: %s",
9434
e374a365521d Initial commit for making mail_storage:mailbox_list to be from 1:1 to n:n.
Timo Sirainen <tss@iki.fi>
parents: 9374
diff changeset
433 mail_storage_get_last_error(box->storage, &error));
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
434 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
435 raw_box = (struct raw_mailbox *)box;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
436 raw_box->envelope_sender = ctx.src_envelope_sender != NULL ?
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
437 ctx.src_envelope_sender : DEFAULT_ENVELOPE_SENDER;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438 raw_box->mtime = mtime;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
440 t = mailbox_transaction_begin(box, 0);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
441 headers_ctx = mailbox_header_lookup_init(box, wanted_headers);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
442 ctx.src_mail = mail_alloc(t, 0, headers_ctx);
9117
a9f61b63dc3d lmtp: Minor cleanup.
Timo Sirainen <tss@iki.fi>
parents: 9106
diff changeset
443 mailbox_header_lookup_unref(&headers_ctx);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
444 mail_set_seq(ctx.src_mail, 1);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
445
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
446 if (ctx.dest_addr == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
447 ctx.dest_addr = mail_deliver_get_address(&ctx, "Envelope-To");
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448 if (ctx.dest_addr == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 ctx.dest_addr = strchr(user, '@') != NULL ? user :
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450 t_strconcat(user, "@", ctx.set->hostname, NULL);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
451 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
453
9117
a9f61b63dc3d lmtp: Minor cleanup.
Timo Sirainen <tss@iki.fi>
parents: 9106
diff changeset
454 if (mail_deliver(&ctx, &storage) < 0) {
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
455 if (storage == NULL) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
456 /* This shouldn't happen */
9117
a9f61b63dc3d lmtp: Minor cleanup.
Timo Sirainen <tss@iki.fi>
parents: 9106
diff changeset
457 i_error("BUG: Saving failed to unknown storage");
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
458 return EX_TEMPFAIL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
459 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
460
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
461 errstr = mail_storage_get_last_error(storage, &error);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
463 if (stderr_rejection) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
464 /* write to stderr also for tempfails so that MTA
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 can log the reason if it wants to. */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
466 fprintf(stderr, "%s\n", errstr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
467 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
468
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
469 if (error != MAIL_ERROR_NOSPACE ||
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
470 ctx.set->quota_full_tempfail) {
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
471 /* Saving to INBOX should always work unless
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
472 we're over quota. If it didn't, it's probably a
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
473 configuration problem. */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
474 return EX_TEMPFAIL;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
475 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
477 /* we'll have to reply with permanent failure */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 mail_deliver_log(&ctx, "rejected: %s",
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
479 str_sanitize(errstr, 512));
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
480
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481 if (stderr_rejection)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
482 return EX_NOPERM;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
483 ret = mail_send_rejection(&ctx, user, errstr);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
484 if (ret != 0)
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
485 return ret < 0 ? EX_TEMPFAIL : ret;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
486 /* ok, rejection sent */
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
487 }
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
488
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
489 mail_free(&ctx.src_mail);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
490 mailbox_transaction_rollback(&t);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
491 mailbox_close(&box);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
492
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 mail_user_unref(&ctx.dest_user);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
494 mail_user_unref(&raw_mail_user);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
495 duplicate_deinit();
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
496 pool_unref(&ctx.pool);
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
497
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 mail_storage_service_deinit_user();
9348
0c587f108916 lib-master has now a global master_service variable that all binaries use.
Timo Sirainen <tss@iki.fi>
parents: 9263
diff changeset
499 master_service_deinit(&master_service);
9092
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
500 return EX_OK;
e4832f128738 Renamed deliver to dovecot-lda and moved most of its code to lib-lda.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
501 }