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 <stdio.h>
|
meillo@0
|
20 #include <errno.h>
|
meillo@0
|
21 #include <stdlib.h>
|
meillo@0
|
22 #include <string.h>
|
meillo@0
|
23 #include <unistd.h>
|
meillo@0
|
24 #include <sys/types.h>
|
meillo@0
|
25 #include <sys/socket.h>
|
meillo@0
|
26 #include <sys/time.h>
|
meillo@0
|
27 #include <netinet/in.h>
|
meillo@0
|
28 #include <netdb.h>
|
meillo@0
|
29 #include <syslog.h>
|
meillo@0
|
30 #include <signal.h>
|
meillo@0
|
31
|
meillo@0
|
32 #include <glib.h>
|
meillo@0
|
33
|
meillo@0
|
34 #include "masqmail.h"
|
meillo@0
|
35
|
meillo@0
|
36 /* mutually exclusive modes. Note that there is neither a 'get' mode
|
meillo@0
|
37 nor a 'queue daemon' mode. These, as well as the distinction beween
|
meillo@0
|
38 the two (non exclusive) daemon (queue and listen) modes are handled
|
meillo@0
|
39 by flags.*/
|
meillo@10
|
40 typedef enum _mta_mode {
|
meillo@10
|
41 MODE_ACCEPT = 0, /* accept message on stdin */
|
meillo@10
|
42 MODE_DAEMON, /* run as daemon */
|
meillo@10
|
43 MODE_RUNQUEUE, /* single queue run, online or offline */
|
meillo@10
|
44 MODE_GET_DAEMON, /* run as get (retrieve) daemon */
|
meillo@10
|
45 MODE_SMTP, /* accept SMTP on stdin */
|
meillo@10
|
46 MODE_LIST, /* list queue */
|
meillo@10
|
47 MODE_MCMD, /* do queue manipulation */
|
meillo@10
|
48 MODE_VERSION, /* show version */
|
meillo@10
|
49 MODE_BI, /* fake ;-) */
|
meillo@10
|
50 MODE_NONE /* to prevent default MODE_ACCEPT */
|
meillo@10
|
51 } mta_mode;
|
meillo@0
|
52
|
meillo@0
|
53 char *pidfile = NULL;
|
meillo@0
|
54 volatile int sigterm_in_progress = 0;
|
meillo@0
|
55
|
meillo@10
|
56 static void
|
meillo@10
|
57 sigterm_handler(int sig)
|
meillo@0
|
58 {
|
meillo@10
|
59 if (sigterm_in_progress)
|
meillo@10
|
60 raise(sig);
|
meillo@10
|
61 sigterm_in_progress = 1;
|
meillo@0
|
62
|
meillo@10
|
63 if (pidfile) {
|
meillo@10
|
64 uid_t uid;
|
meillo@10
|
65 uid = seteuid(0);
|
meillo@10
|
66 if (unlink(pidfile) != 0)
|
meillo@10
|
67 logwrite(LOG_WARNING, "could not delete pid file %s: %s\n", pidfile, strerror(errno));
|
meillo@10
|
68 seteuid(uid); /* we exit anyway after this, just to be sure */
|
meillo@10
|
69 }
|
meillo@0
|
70
|
meillo@10
|
71 signal(sig, SIG_DFL);
|
meillo@10
|
72 raise(sig);
|
meillo@0
|
73 }
|
meillo@0
|
74
|
meillo@10
|
75 #ifdef ENABLE_IDENT /* so far used for that only */
|
meillo@10
|
76 static gboolean
|
meillo@10
|
77 is_in_netlist(gchar * host, GList * netlist)
|
meillo@0
|
78 {
|
meillo@10
|
79 guint hostip = inet_addr(host);
|
meillo@10
|
80 struct in_addr addr;
|
meillo@0
|
81
|
meillo@10
|
82 addr.s_addr = hostip;
|
meillo@10
|
83 if (addr.s_addr != INADDR_NONE) {
|
meillo@10
|
84 GList *node;
|
meillo@10
|
85 foreach(netlist, node) {
|
meillo@10
|
86 struct in_addr *net = (struct in_addr *) (node->data);
|
meillo@10
|
87 if ((addr.s_addr & net->s_addr) == net->s_addr)
|
meillo@10
|
88 return TRUE;
|
meillo@10
|
89 }
|
meillo@10
|
90 }
|
meillo@10
|
91 return FALSE;
|
meillo@0
|
92 }
|
meillo@0
|
93 #endif
|
meillo@0
|
94
|
meillo@10
|
95 gchar*
|
meillo@10
|
96 get_optarg(char *argv[], gint argc, gint * argp, gint * pos)
|
meillo@0
|
97 {
|
meillo@10
|
98 if (argv[*argp][*pos])
|
meillo@10
|
99 return &(argv[*argp][*pos]);
|
meillo@10
|
100 else {
|
meillo@10
|
101 if (*argp + 1 < argc) {
|
meillo@10
|
102 if (argv[(*argp) + 1][0] != '-') {
|
meillo@10
|
103 (*argp)++;
|
meillo@10
|
104 *pos = 0;
|
meillo@10
|
105 return &(argv[*argp][*pos]);
|
meillo@10
|
106 }
|
meillo@10
|
107 }
|
meillo@10
|
108 }
|
meillo@10
|
109 return NULL;
|
meillo@0
|
110 }
|
meillo@0
|
111
|
meillo@10
|
112 gchar*
|
meillo@10
|
113 get_progname(gchar * arg0)
|
meillo@0
|
114 {
|
meillo@10
|
115 gchar *p = arg0 + strlen(arg0) - 1;
|
meillo@10
|
116 while (p > arg0) {
|
meillo@10
|
117 if (*p == '/')
|
meillo@10
|
118 return p + 1;
|
meillo@10
|
119 p--;
|
meillo@10
|
120 }
|
meillo@10
|
121 return p;
|
meillo@0
|
122 }
|
meillo@0
|
123
|
meillo@10
|
124 gboolean
|
meillo@10
|
125 write_pidfile(gchar * name)
|
meillo@0
|
126 {
|
meillo@10
|
127 FILE *fptr;
|
meillo@0
|
128
|
meillo@10
|
129 if ((fptr = fopen(name, "wt"))) {
|
meillo@10
|
130 fprintf(fptr, "%d\n", getpid());
|
meillo@10
|
131 fclose(fptr);
|
meillo@10
|
132 pidfile = strdup(name);
|
meillo@10
|
133 return TRUE;
|
meillo@10
|
134 }
|
meillo@10
|
135 logwrite(LOG_WARNING, "could not write pid file: %s\n", strerror(errno));
|
meillo@10
|
136 return FALSE;
|
meillo@10
|
137 }
|
meillo@0
|
138
|
meillo@10
|
139 static void
|
meillo@10
|
140 mode_daemon(gboolean do_listen, gint queue_interval, char *argv[])
|
meillo@10
|
141 {
|
meillo@10
|
142 guint pid;
|
meillo@0
|
143
|
meillo@10
|
144 /* daemon */
|
meillo@10
|
145 if (!conf.run_as_user) {
|
meillo@10
|
146 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
|
meillo@10
|
147 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
|
meillo@10
|
148 exit(EXIT_FAILURE);
|
meillo@10
|
149 }
|
meillo@10
|
150 }
|
meillo@0
|
151
|
meillo@10
|
152 if ((pid = fork()) > 0) {
|
meillo@10
|
153 exit(EXIT_SUCCESS);
|
meillo@10
|
154 } else if (pid < 0) {
|
meillo@10
|
155 logwrite(LOG_ALERT, "could not fork!");
|
meillo@10
|
156 exit(EXIT_FAILURE);
|
meillo@10
|
157 }
|
meillo@0
|
158
|
meillo@10
|
159 signal(SIGTERM, sigterm_handler);
|
meillo@10
|
160 write_pidfile(PIDFILEDIR "/masqmail.pid");
|
meillo@0
|
161
|
meillo@10
|
162 conf.do_verbose = FALSE;
|
meillo@10
|
163
|
meillo@10
|
164 fclose(stdin);
|
meillo@10
|
165 fclose(stdout);
|
meillo@10
|
166 fclose(stderr);
|
meillo@10
|
167
|
meillo@10
|
168 listen_port(do_listen ? conf.listen_addresses : NULL, queue_interval, argv);
|
meillo@0
|
169 }
|
meillo@0
|
170
|
meillo@0
|
171 #ifdef ENABLE_POP3
|
meillo@10
|
172 static void
|
meillo@10
|
173 mode_get_daemon(gint get_interval, char *argv[])
|
meillo@0
|
174 {
|
meillo@10
|
175 guint pid;
|
meillo@0
|
176
|
meillo@10
|
177 /* daemon */
|
meillo@10
|
178 if (!conf.run_as_user) {
|
meillo@10
|
179 if ((conf.orig_uid != 0) && (conf.orig_uid != conf.mail_uid)) {
|
meillo@10
|
180 fprintf(stderr, "must be root or %s for daemon.\n", DEF_MAIL_USER);
|
meillo@10
|
181 exit(EXIT_FAILURE);
|
meillo@10
|
182 }
|
meillo@10
|
183 }
|
meillo@0
|
184
|
meillo@10
|
185 if ((pid = fork()) > 0) {
|
meillo@10
|
186 exit(EXIT_SUCCESS);
|
meillo@10
|
187 } else if (pid < 0) {
|
meillo@10
|
188 logwrite(LOG_ALERT, "could not fork!");
|
meillo@10
|
189 exit(EXIT_FAILURE);
|
meillo@10
|
190 }
|
meillo@0
|
191
|
meillo@10
|
192 signal(SIGTERM, sigterm_handler);
|
meillo@10
|
193 write_pidfile(PIDFILEDIR "/masqmail-get.pid");
|
meillo@0
|
194
|
meillo@10
|
195 conf.do_verbose = FALSE;
|
meillo@0
|
196
|
meillo@10
|
197 fclose(stdin);
|
meillo@10
|
198 fclose(stdout);
|
meillo@10
|
199 fclose(stderr);
|
meillo@0
|
200
|
meillo@10
|
201 get_daemon(get_interval, argv);
|
meillo@0
|
202 }
|
meillo@0
|
203 #endif
|
meillo@0
|
204
|
meillo@0
|
205 #ifdef ENABLE_SMTP_SERVER
|
meillo@10
|
206 static void
|
meillo@10
|
207 mode_smtp()
|
meillo@0
|
208 {
|
meillo@10
|
209 /* accept smtp message on stdin */
|
meillo@10
|
210 /* write responses to stderr. */
|
meillo@0
|
211
|
meillo@10
|
212 struct sockaddr_in saddr;
|
meillo@10
|
213 gchar *peername = NULL;
|
meillo@10
|
214 int dummy = sizeof(saddr);
|
meillo@0
|
215 #ifdef ENABLE_IDENT
|
meillo@10
|
216 gchar *ident = NULL;
|
meillo@0
|
217 #endif
|
meillo@0
|
218
|
meillo@10
|
219 conf.do_verbose = FALSE;
|
meillo@0
|
220
|
meillo@10
|
221 if (!conf.run_as_user) {
|
meillo@10
|
222 seteuid(conf.orig_uid);
|
meillo@10
|
223 setegid(conf.orig_gid);
|
meillo@10
|
224 }
|
meillo@0
|
225
|
meillo@10
|
226 DEBUG(5) debugf("accepting smtp message on stdin\n");
|
meillo@0
|
227
|
meillo@10
|
228 if (getpeername(0, (struct sockaddr *) (&saddr), &dummy) == 0) {
|
meillo@10
|
229 peername = g_strdup(inet_ntoa(saddr.sin_addr));
|
meillo@0
|
230 #ifdef ENABLE_IDENT
|
meillo@10
|
231 {
|
meillo@10
|
232 gchar *id = NULL;
|
meillo@10
|
233 if ((id = (gchar *) ident_id(0, 60))) {
|
meillo@10
|
234 ident = g_strdup(id);
|
meillo@10
|
235 }
|
meillo@10
|
236 }
|
meillo@0
|
237 #endif
|
meillo@10
|
238 } else if (errno != ENOTSOCK)
|
meillo@10
|
239 exit(EXIT_FAILURE);
|
meillo@0
|
240
|
meillo@10
|
241 //smtp_in(stdin, stdout, peername);
|
meillo@10
|
242 smtp_in(stdin, stderr, peername, NULL);
|
meillo@0
|
243
|
meillo@0
|
244 #ifdef ENABLE_IDENT
|
meillo@10
|
245 if (ident)
|
meillo@10
|
246 g_free(ident);
|
meillo@0
|
247 #endif
|
meillo@0
|
248 }
|
meillo@0
|
249 #endif
|
meillo@0
|
250
|
meillo@10
|
251 static void
|
meillo@10
|
252 mode_accept(address * return_path, gchar * full_sender_name, guint accept_flags, char **addresses, int addr_cnt)
|
meillo@0
|
253 {
|
meillo@10
|
254 /* accept message on stdin */
|
meillo@10
|
255 accept_error err;
|
meillo@10
|
256 message *msg = create_message();
|
meillo@10
|
257 gint i;
|
meillo@0
|
258
|
meillo@10
|
259 if (return_path != NULL) {
|
meillo@10
|
260 if ((conf.orig_uid != 0)
|
meillo@10
|
261 && (conf.orig_uid != conf.mail_uid)
|
meillo@10
|
262 && (!is_ingroup(conf.orig_uid, conf.mail_gid))) {
|
meillo@10
|
263 fprintf(stderr, "must be in root, %s or in group %s for setting return path.\n", DEF_MAIL_USER, DEF_MAIL_GROUP);
|
meillo@10
|
264 exit(EXIT_FAILURE);
|
meillo@10
|
265 }
|
meillo@10
|
266 }
|
meillo@0
|
267
|
meillo@10
|
268 if (!conf.run_as_user) {
|
meillo@10
|
269 seteuid(conf.orig_uid);
|
meillo@10
|
270 setegid(conf.orig_gid);
|
meillo@10
|
271 }
|
meillo@0
|
272
|
meillo@10
|
273 DEBUG(5) debugf("accepting message on stdin\n");
|
meillo@0
|
274
|
meillo@10
|
275 msg->received_prot = PROT_LOCAL;
|
meillo@10
|
276 for (i = 0; i < addr_cnt; i++) {
|
meillo@10
|
277 if (addresses[i][0] != '|')
|
meillo@10
|
278 msg->rcpt_list = g_list_append(msg->rcpt_list, create_address_qualified(addresses[i], TRUE, conf.host_name));
|
meillo@10
|
279 else {
|
meillo@10
|
280 logwrite(LOG_ALERT, "no pipe allowed as recipient address: %s\n", addresses[i]);
|
meillo@10
|
281 exit(EXIT_FAILURE);
|
meillo@10
|
282 }
|
meillo@10
|
283 }
|
meillo@0
|
284
|
meillo@10
|
285 /* -f option */
|
meillo@10
|
286 msg->return_path = return_path;
|
meillo@0
|
287
|
meillo@10
|
288 /* -F option */
|
meillo@10
|
289 msg->full_sender_name = full_sender_name;
|
meillo@0
|
290
|
meillo@10
|
291 if ((err = accept_message(stdin, msg, accept_flags)) == AERR_OK) {
|
meillo@10
|
292 if (spool_write(msg, TRUE)) {
|
meillo@10
|
293 pid_t pid;
|
meillo@10
|
294 logwrite(LOG_NOTICE, "%s <= %s with %s\n", msg->uid, addr_string(msg->return_path), prot_names[PROT_LOCAL]);
|
meillo@0
|
295
|
meillo@10
|
296 if (!conf.do_queue) {
|
meillo@10
|
297 if ((pid = fork()) == 0) {
|
meillo@10
|
298 conf.do_verbose = FALSE;
|
meillo@10
|
299 fclose(stdin);
|
meillo@10
|
300 fclose(stdout);
|
meillo@10
|
301 fclose(stderr);
|
meillo@10
|
302 if (deliver(msg)) {
|
meillo@10
|
303 exit(EXIT_SUCCESS);
|
meillo@10
|
304 } else
|
meillo@10
|
305 exit(EXIT_FAILURE);
|
meillo@10
|
306 } else if (pid < 0) {
|
meillo@10
|
307 logwrite(LOG_ALERT, "could not fork for delivery, id = %s", msg->uid);
|
meillo@10
|
308 }
|
meillo@10
|
309 }
|
meillo@10
|
310 } else {
|
meillo@10
|
311 fprintf(stderr, "Could not write spool file\n");
|
meillo@10
|
312 exit(EXIT_FAILURE);
|
meillo@10
|
313 }
|
meillo@10
|
314 } else {
|
meillo@10
|
315 switch (err) {
|
meillo@10
|
316 case AERR_EOF:
|
meillo@10
|
317 fprintf(stderr, "unexpected EOF.\n");
|
meillo@10
|
318 exit(EXIT_FAILURE);
|
meillo@10
|
319 case AERR_NORCPT:
|
meillo@10
|
320 fprintf(stderr, "no recipients.\n");
|
meillo@10
|
321 exit(EXIT_FAILURE);
|
meillo@10
|
322 default:
|
meillo@10
|
323 /* should never happen: */
|
meillo@10
|
324 fprintf(stderr, "Unknown error (%d)\r\n", err);
|
meillo@10
|
325 exit(EXIT_FAILURE);
|
meillo@10
|
326 }
|
meillo@10
|
327 exit(EXIT_FAILURE);
|
meillo@0
|
328 }
|
meillo@0
|
329 }
|
meillo@0
|
330
|
meillo@0
|
331 int
|
meillo@0
|
332 main(int argc, char *argv[])
|
meillo@0
|
333 {
|
meillo@10
|
334 /* cmd line flags */
|
meillo@10
|
335 gchar *conf_file = CONF_FILE;
|
meillo@10
|
336 gint arg = 1;
|
meillo@10
|
337 gboolean do_get = FALSE;
|
meillo@10
|
338 gboolean do_get_online = FALSE;
|
meillo@0
|
339
|
meillo@10
|
340 gboolean do_listen = FALSE;
|
meillo@10
|
341 gboolean do_runq = FALSE;
|
meillo@10
|
342 gboolean do_runq_online = FALSE;
|
meillo@0
|
343
|
meillo@10
|
344 gboolean do_queue = FALSE;
|
meillo@0
|
345
|
meillo@10
|
346 gboolean do_verbose = FALSE;
|
meillo@10
|
347 gint debug_level = -1;
|
meillo@0
|
348
|
meillo@10
|
349 mta_mode mta_mode = MODE_ACCEPT;
|
meillo@0
|
350
|
meillo@10
|
351 gint queue_interval = 0;
|
meillo@10
|
352 gint get_interval = 0;
|
meillo@10
|
353 gboolean opt_t = FALSE;
|
meillo@10
|
354 gboolean opt_i = FALSE;
|
meillo@10
|
355 gboolean opt_odb = FALSE;
|
meillo@10
|
356 gboolean opt_oem = FALSE;
|
meillo@10
|
357 gboolean exit_failure = FALSE;
|
meillo@0
|
358
|
meillo@10
|
359 gchar *M_cmd = NULL;
|
meillo@0
|
360
|
meillo@10
|
361 gint exit_code = EXIT_SUCCESS;
|
meillo@10
|
362 gchar *route_name = NULL;
|
meillo@10
|
363 gchar *get_name = NULL;
|
meillo@10
|
364 gchar *progname;
|
meillo@10
|
365 gchar *f_address = NULL;
|
meillo@10
|
366 gchar *full_sender_name = NULL;
|
meillo@10
|
367 address *return_path = NULL; /* may be changed by -f option */
|
meillo@0
|
368
|
meillo@10
|
369 progname = get_progname(argv[0]);
|
meillo@0
|
370
|
meillo@10
|
371 if (strcmp(progname, "mailq") == 0) {
|
meillo@10
|
372 mta_mode = MODE_LIST;
|
meillo@10
|
373 } else if (strcmp(progname, "mailrm") == 0) {
|
meillo@10
|
374 mta_mode = MODE_MCMD;
|
meillo@10
|
375 M_cmd = "rm";
|
meillo@10
|
376 } else if (strcmp(progname, "runq") == 0) {
|
meillo@10
|
377 mta_mode = MODE_RUNQUEUE;
|
meillo@10
|
378 do_runq = TRUE;
|
meillo@10
|
379 } else if (strcmp(progname, "rmail") == 0) {
|
meillo@10
|
380 mta_mode = MODE_ACCEPT;
|
meillo@10
|
381 opt_i = TRUE;
|
meillo@10
|
382 } else if (strcmp(progname, "smtpd") == 0 || strcmp(progname, "in.smtpd") == 0) {
|
meillo@10
|
383 mta_mode = MODE_SMTP;
|
meillo@10
|
384 }
|
meillo@0
|
385
|
meillo@10
|
386 /* parse cmd line */
|
meillo@10
|
387 while (arg < argc) {
|
meillo@10
|
388 gint pos = 0;
|
meillo@10
|
389 if ((argv[arg][pos] == '-') && (argv[arg][pos + 1] != '-')) {
|
meillo@10
|
390 pos++;
|
meillo@10
|
391 switch (argv[arg][pos++]) {
|
meillo@10
|
392 case 'b':
|
meillo@10
|
393 switch (argv[arg][pos++]) {
|
meillo@10
|
394 case 'd':
|
meillo@10
|
395 do_listen = TRUE;
|
meillo@10
|
396 mta_mode = MODE_DAEMON;
|
meillo@10
|
397 break;
|
meillo@10
|
398 case 'i':
|
meillo@10
|
399 /* ignored */
|
meillo@10
|
400 mta_mode = MODE_BI;
|
meillo@10
|
401 break;
|
meillo@10
|
402 case 's':
|
meillo@10
|
403 mta_mode = MODE_SMTP;
|
meillo@10
|
404 break;
|
meillo@10
|
405 case 'p':
|
meillo@10
|
406 mta_mode = MODE_LIST;
|
meillo@10
|
407 break;
|
meillo@10
|
408 case 'V':
|
meillo@10
|
409 mta_mode = MODE_VERSION;
|
meillo@10
|
410 break;
|
meillo@10
|
411 default:
|
meillo@10
|
412 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
|
meillo@10
|
413 exit(EXIT_FAILURE);
|
meillo@10
|
414 }
|
meillo@10
|
415 break;
|
meillo@10
|
416 case 'B':
|
meillo@10
|
417 /* we ignore this and throw the argument away */
|
meillo@10
|
418 get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
419 break;
|
meillo@10
|
420 case 'C':
|
meillo@10
|
421 if (!(conf_file = get_optarg(argv, argc, &arg, &pos))) {
|
meillo@10
|
422 fprintf(stderr, "-C requires a filename as argument.\n");
|
meillo@10
|
423 exit(EXIT_FAILURE);
|
meillo@10
|
424 }
|
meillo@10
|
425 break;
|
meillo@10
|
426 case 'F':
|
meillo@10
|
427 {
|
meillo@10
|
428 full_sender_name = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
429 if (!full_sender_name) {
|
meillo@10
|
430 fprintf(stderr, "-F requires a name as an argument\n");
|
meillo@10
|
431 exit(EXIT_FAILURE);
|
meillo@10
|
432 }
|
meillo@10
|
433 }
|
meillo@10
|
434 break;
|
meillo@10
|
435 case 'd':
|
meillo@10
|
436 if (getuid() == 0) {
|
meillo@10
|
437 char *lvl = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
438 if (lvl)
|
meillo@10
|
439 debug_level = atoi(lvl);
|
meillo@10
|
440 else {
|
meillo@10
|
441 fprintf(stderr, "-d requires a number as an argument.\n");
|
meillo@10
|
442 exit(EXIT_FAILURE);
|
meillo@10
|
443 }
|
meillo@10
|
444 } else {
|
meillo@10
|
445 fprintf(stderr, "only root may set the debug level.\n");
|
meillo@10
|
446 exit(EXIT_FAILURE);
|
meillo@10
|
447 }
|
meillo@10
|
448 break;
|
meillo@10
|
449 case 'f':
|
meillo@10
|
450 /* set return path */
|
meillo@10
|
451 {
|
meillo@10
|
452 gchar *address;
|
meillo@10
|
453 address = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
454 if (address) {
|
meillo@10
|
455 f_address = g_strdup(address);
|
meillo@10
|
456 } else {
|
meillo@10
|
457 fprintf(stderr, "-f requires an address as an argument\n");
|
meillo@10
|
458 exit(EXIT_FAILURE);
|
meillo@10
|
459 }
|
meillo@10
|
460 }
|
meillo@10
|
461 break;
|
meillo@10
|
462 case 'g':
|
meillo@10
|
463 do_get = TRUE;
|
meillo@10
|
464 if (!mta_mode)
|
meillo@10
|
465 mta_mode = MODE_NONE; /* to prevent default MODE_ACCEPT */
|
meillo@10
|
466 if (argv[arg][pos] == 'o') {
|
meillo@10
|
467 pos++;
|
meillo@10
|
468 do_get_online = TRUE;
|
meillo@10
|
469 /* can be NULL, then we use online detection method */
|
meillo@10
|
470 route_name = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
471
|
meillo@10
|
472 if (route_name != NULL) {
|
meillo@10
|
473 if (isdigit(route_name[0])) {
|
meillo@10
|
474 get_interval = time_interval(route_name, &pos);
|
meillo@10
|
475 route_name = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
476 mta_mode = MODE_GET_DAEMON;
|
meillo@10
|
477 do_get = FALSE;
|
meillo@10
|
478 }
|
meillo@10
|
479 }
|
meillo@10
|
480 } else {
|
meillo@10
|
481 if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
|
meillo@10
|
482 get_name = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
483 }
|
meillo@10
|
484 }
|
meillo@10
|
485 break;
|
meillo@10
|
486 case 'i':
|
meillo@10
|
487 if (argv[arg][pos] == 0) {
|
meillo@10
|
488 opt_i = TRUE;
|
meillo@10
|
489 exit_failure = FALSE; /* may override -oem */
|
meillo@10
|
490 } else {
|
meillo@10
|
491 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
|
meillo@10
|
492 exit(EXIT_FAILURE);
|
meillo@10
|
493 }
|
meillo@10
|
494 break;
|
meillo@10
|
495 case 'M':
|
meillo@10
|
496 {
|
meillo@10
|
497 mta_mode = MODE_MCMD;
|
meillo@10
|
498 M_cmd = g_strdup(&(argv[arg][pos]));
|
meillo@10
|
499 }
|
meillo@10
|
500 break;
|
meillo@10
|
501 case 'o':
|
meillo@10
|
502 switch (argv[arg][pos++]) {
|
meillo@10
|
503 case 'e':
|
meillo@10
|
504 if (argv[arg][pos++] == 'm') /* -oem */
|
meillo@10
|
505 if (!opt_i)
|
meillo@10
|
506 exit_failure = TRUE;
|
meillo@10
|
507 opt_oem = TRUE;
|
meillo@10
|
508 break;
|
meillo@10
|
509 case 'd':
|
meillo@10
|
510 if (argv[arg][pos] == 'b') /* -odb */
|
meillo@10
|
511 opt_odb = TRUE;
|
meillo@10
|
512 else if (argv[arg][pos] == 'q') /* -odq */
|
meillo@10
|
513 do_queue = TRUE;
|
meillo@10
|
514 break;
|
meillo@10
|
515 case 'i':
|
meillo@10
|
516 opt_i = TRUE;
|
meillo@10
|
517 exit_failure = FALSE; /* may override -oem */
|
meillo@10
|
518 break;
|
meillo@10
|
519 }
|
meillo@10
|
520 break;
|
meillo@10
|
521
|
meillo@10
|
522 case 'q':
|
meillo@10
|
523 {
|
meillo@10
|
524 gchar *optarg;
|
meillo@10
|
525
|
meillo@10
|
526 do_runq = TRUE;
|
meillo@10
|
527 mta_mode = MODE_RUNQUEUE;
|
meillo@10
|
528 if (argv[arg][pos] == 'o') {
|
meillo@10
|
529 pos++;
|
meillo@10
|
530 do_runq = FALSE;
|
meillo@10
|
531 do_runq_online = TRUE;
|
meillo@10
|
532 /* can be NULL, then we use online detection method */
|
meillo@10
|
533 route_name = get_optarg(argv, argc, &arg, &pos);
|
meillo@10
|
534 } else
|
meillo@10
|
535 if ((optarg = get_optarg(argv, argc, &arg, &pos))) {
|
meillo@10
|
536 mta_mode = MODE_DAEMON;
|
meillo@10
|
537 queue_interval = time_interval(optarg, &pos);
|
meillo@10
|
538 }
|
meillo@10
|
539 }
|
meillo@10
|
540 break;
|
meillo@10
|
541 case 't':
|
meillo@10
|
542 if (argv[arg][pos] == 0) {
|
meillo@10
|
543 opt_t = TRUE;
|
meillo@10
|
544 } else {
|
meillo@10
|
545 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
|
meillo@10
|
546 exit(EXIT_FAILURE);
|
meillo@10
|
547 }
|
meillo@10
|
548 break;
|
meillo@10
|
549 case 'v':
|
meillo@10
|
550 do_verbose = TRUE;
|
meillo@10
|
551 break;
|
meillo@10
|
552 default:
|
meillo@10
|
553 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
|
meillo@10
|
554 exit(EXIT_FAILURE);
|
meillo@10
|
555 }
|
meillo@10
|
556 } else {
|
meillo@10
|
557 if (argv[arg][pos + 1] == '-') {
|
meillo@10
|
558 if (argv[arg][pos + 2] != '\0') {
|
meillo@10
|
559 fprintf(stderr, "unrecognized option '%s'\n", argv[arg]);
|
meillo@10
|
560 exit(EXIT_FAILURE);
|
meillo@10
|
561 }
|
meillo@10
|
562 arg++;
|
meillo@10
|
563 }
|
meillo@10
|
564 break;
|
meillo@10
|
565 }
|
meillo@10
|
566 arg++;
|
meillo@0
|
567 }
|
meillo@0
|
568
|
meillo@10
|
569 if (mta_mode == MODE_VERSION) {
|
meillo@10
|
570 gchar *with_resolver = "", *with_smtp_server = "", *with_pop3 = "",
|
meillo@10
|
571 *with_auth = "", *with_maildir = "", *with_ident = "", *with_mserver = "";
|
meillo@0
|
572
|
meillo@0
|
573 #ifdef ENABLE_RESOLVER
|
meillo@10
|
574 with_resolver = " +resolver";
|
meillo@0
|
575 #endif
|
meillo@0
|
576 #ifdef ENABLE_SMTP_SERVER
|
meillo@10
|
577 with_smtp_server = " +smtp-server";
|
meillo@0
|
578 #endif
|
meillo@0
|
579 #ifdef ENABLE_POP3
|
meillo@10
|
580 with_pop3 = " +pop3";
|
meillo@0
|
581 #endif
|
meillo@0
|
582 #ifdef ENABLE_AUTH
|
meillo@10
|
583 with_auth = " +auth";
|
meillo@0
|
584 #endif
|
meillo@0
|
585 #ifdef ENABLE_MAILDIR
|
meillo@10
|
586 with_maildir = " +maildir";
|
meillo@0
|
587 #endif
|
meillo@0
|
588 #ifdef ENABLE_IDENT
|
meillo@10
|
589 with_ident = " +ident";
|
meillo@0
|
590 #endif
|
meillo@0
|
591 #ifdef ENABLE_MSERVER
|
meillo@10
|
592 with_mserver = " +mserver";
|
meillo@0
|
593 #endif
|
meillo@0
|
594
|
meillo@10
|
595 printf("%s %s%s%s%s%s%s%s%s\n", PACKAGE, VERSION, with_resolver, with_smtp_server,
|
meillo@10
|
596 with_pop3, with_auth, with_maildir, with_ident, with_mserver);
|
meillo@0
|
597
|
meillo@10
|
598 exit(EXIT_SUCCESS);
|
meillo@10
|
599 }
|
meillo@0
|
600
|
meillo@10
|
601 /* initialize random generator */
|
meillo@10
|
602 srand(time(NULL));
|
meillo@10
|
603 /* ignore SIGPIPE signal */
|
meillo@10
|
604 signal(SIGPIPE, SIG_IGN);
|
meillo@0
|
605
|
meillo@10
|
606 /* close all possibly open file descriptors */
|
meillo@10
|
607 {
|
meillo@10
|
608 int i, max_fd = sysconf(_SC_OPEN_MAX);
|
meillo@0
|
609
|
meillo@10
|
610 if (max_fd <= 0)
|
meillo@10
|
611 max_fd = 64;
|
meillo@10
|
612 for (i = 3; i < max_fd; i++)
|
meillo@10
|
613 close(i);
|
meillo@10
|
614 }
|
meillo@0
|
615
|
meillo@10
|
616 init_conf();
|
meillo@0
|
617
|
meillo@10
|
618 /* if we are not privileged, and the config file was changed we
|
meillo@10
|
619 implicetely set the the run_as_user flag and give up all
|
meillo@10
|
620 privileges.
|
meillo@0
|
621
|
meillo@10
|
622 So it is possible for a user to run his own daemon without
|
meillo@10
|
623 breaking security.
|
meillo@10
|
624 */
|
meillo@10
|
625 if (strcmp(conf_file, CONF_FILE) != 0) {
|
meillo@10
|
626 if (conf.orig_uid != 0) {
|
meillo@10
|
627 conf.run_as_user = TRUE;
|
meillo@10
|
628 seteuid(conf.orig_uid);
|
meillo@10
|
629 setegid(conf.orig_gid);
|
meillo@10
|
630 setuid(conf.orig_uid);
|
meillo@10
|
631 setgid(conf.orig_gid);
|
meillo@10
|
632 }
|
meillo@10
|
633 }
|
meillo@0
|
634
|
meillo@10
|
635 read_conf(conf_file);
|
meillo@0
|
636
|
meillo@10
|
637 if (do_queue)
|
meillo@10
|
638 conf.do_queue = TRUE;
|
meillo@10
|
639 if (do_verbose)
|
meillo@10
|
640 conf.do_verbose = TRUE;
|
meillo@10
|
641 if (debug_level >= 0) /* if >= 0, it was given by argument */
|
meillo@10
|
642 conf.debug_level = debug_level;
|
meillo@0
|
643
|
meillo@10
|
644 chdir("/");
|
meillo@0
|
645
|
meillo@10
|
646 if (!conf.run_as_user) {
|
meillo@10
|
647 if (setgid(0) != 0) {
|
meillo@10
|
648 fprintf(stderr, "could not set gid to 0. Is the setuid bit set? : %s\n", strerror(errno));
|
meillo@10
|
649 exit(EXIT_FAILURE);
|
meillo@10
|
650 }
|
meillo@10
|
651 if (setuid(0) != 0) {
|
meillo@10
|
652 fprintf(stderr, "could not gain root privileges. Is the setuid bit set? : %s\n", strerror(errno));
|
meillo@10
|
653 exit(EXIT_FAILURE);
|
meillo@10
|
654 }
|
meillo@10
|
655 }
|
meillo@0
|
656
|
meillo@10
|
657 if (!logopen()) {
|
meillo@10
|
658 fprintf(stderr, "could not open log file\n");
|
meillo@10
|
659 exit(EXIT_FAILURE);
|
meillo@10
|
660 }
|
meillo@0
|
661
|
meillo@10
|
662 DEBUG(1) debugf("masqmail %s starting\n", VERSION);
|
meillo@0
|
663
|
meillo@10
|
664 DEBUG(5) {
|
meillo@10
|
665 gchar **str = argv;
|
meillo@10
|
666 debugf("args: \n");
|
meillo@10
|
667 while (*str) {
|
meillo@10
|
668 debugf("%s \n", *str);
|
meillo@10
|
669 str++;
|
meillo@10
|
670 }
|
meillo@10
|
671 }
|
meillo@10
|
672 DEBUG(5) debugf("queue_interval = %d\n", queue_interval);
|
meillo@0
|
673
|
meillo@10
|
674 if (f_address) {
|
meillo@10
|
675 return_path = create_address_qualified(f_address, TRUE, conf.host_name);
|
meillo@10
|
676 g_free(f_address);
|
meillo@10
|
677 if (!return_path) {
|
meillo@10
|
678 fprintf(stderr, "invalid RFC821 address: %s\n", f_address);
|
meillo@10
|
679 exit(EXIT_FAILURE);
|
meillo@10
|
680 }
|
meillo@10
|
681 }
|
meillo@10
|
682
|
meillo@10
|
683 if (do_get) {
|
meillo@0
|
684 #ifdef ENABLE_POP3
|
meillo@10
|
685 if ((mta_mode == MODE_NONE) || (mta_mode == MODE_RUNQUEUE)) {
|
meillo@10
|
686 set_identity(conf.orig_uid, "getting mail");
|
meillo@10
|
687 if (do_get_online) {
|
meillo@10
|
688 if (route_name != NULL) {
|
meillo@10
|
689 conf.online_detect = g_strdup("argument");
|
meillo@10
|
690 set_online_name(route_name);
|
meillo@10
|
691 }
|
meillo@10
|
692 get_online();
|
meillo@10
|
693 } else {
|
meillo@10
|
694 if (get_name)
|
meillo@10
|
695 get_from_name(get_name);
|
meillo@10
|
696 else
|
meillo@10
|
697 get_all();
|
meillo@10
|
698 }
|
meillo@10
|
699 } else {
|
meillo@10
|
700 logwrite(LOG_ALERT, "get (-g) only allowed alone or together with queue run (-q)\n");
|
meillo@10
|
701 }
|
meillo@10
|
702 #else
|
meillo@10
|
703 fprintf(stderr, "get (pop) support not compiled in\n");
|
meillo@10
|
704 #endif
|
meillo@10
|
705 }
|
meillo@0
|
706
|
meillo@10
|
707 switch (mta_mode) {
|
meillo@10
|
708 case MODE_DAEMON:
|
meillo@10
|
709 mode_daemon(do_listen, queue_interval, argv);
|
meillo@10
|
710 break;
|
meillo@10
|
711 case MODE_RUNQUEUE:
|
meillo@10
|
712 {
|
meillo@10
|
713 /* queue runs */
|
meillo@10
|
714 set_identity(conf.orig_uid, "queue run");
|
meillo@0
|
715
|
meillo@10
|
716 if (do_runq)
|
meillo@10
|
717 exit_code = queue_run() ? EXIT_SUCCESS : EXIT_FAILURE;
|
meillo@10
|
718
|
meillo@10
|
719 if (do_runq_online) {
|
meillo@10
|
720 if (route_name != NULL) {
|
meillo@10
|
721 conf.online_detect = g_strdup("argument");
|
meillo@10
|
722 set_online_name(route_name);
|
meillo@10
|
723 }
|
meillo@10
|
724 exit_code =
|
meillo@10
|
725 queue_run_online() ? EXIT_SUCCESS : EXIT_FAILURE;
|
meillo@10
|
726 }
|
meillo@10
|
727 }
|
meillo@10
|
728 break;
|
meillo@10
|
729 case MODE_GET_DAEMON:
|
meillo@10
|
730 #ifdef ENABLE_POP3
|
meillo@10
|
731 if (route_name != NULL) {
|
meillo@10
|
732 conf.online_detect = g_strdup("argument");
|
meillo@10
|
733 set_online_name(route_name);
|
meillo@10
|
734 }
|
meillo@10
|
735 mode_get_daemon(get_interval, argv);
|
meillo@10
|
736 #endif
|
meillo@10
|
737 break;
|
meillo@10
|
738
|
meillo@10
|
739 case MODE_SMTP:
|
meillo@10
|
740 #ifdef ENABLE_SMTP_SERVER
|
meillo@10
|
741 mode_smtp();
|
meillo@10
|
742 #else
|
meillo@10
|
743 fprintf(stderr, "smtp server support not compiled in\n");
|
meillo@10
|
744 #endif
|
meillo@10
|
745 break;
|
meillo@10
|
746
|
meillo@10
|
747 case MODE_LIST:
|
meillo@10
|
748 queue_list();
|
meillo@10
|
749 break;
|
meillo@10
|
750
|
meillo@10
|
751 case MODE_BI:
|
meillo@10
|
752 exit(EXIT_SUCCESS);
|
meillo@10
|
753 break; /* well... */
|
meillo@10
|
754
|
meillo@10
|
755 case MODE_MCMD:
|
meillo@10
|
756 if (strcmp(M_cmd, "rm") == 0) {
|
meillo@10
|
757 gboolean ok = FALSE;
|
meillo@10
|
758
|
meillo@10
|
759 set_euidgid(conf.mail_uid, conf.mail_gid, NULL, NULL);
|
meillo@10
|
760
|
meillo@10
|
761 if (is_privileged_user(conf.orig_uid)) {
|
meillo@10
|
762 for (; arg < argc; arg++) {
|
meillo@10
|
763 if (queue_delete(argv[arg]))
|
meillo@10
|
764 ok = TRUE;
|
meillo@10
|
765 }
|
meillo@10
|
766 } else {
|
meillo@10
|
767 struct passwd *pw = getpwuid(conf.orig_uid);
|
meillo@10
|
768 if (pw) {
|
meillo@10
|
769 for (; arg < argc; arg++) {
|
meillo@10
|
770 message *msg = msg_spool_read(argv[arg], FALSE);
|
meillo@10
|
771 #ifdef ENABLE_IDENT
|
meillo@10
|
772 if (((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL))
|
meillo@10
|
773 || is_in_netlist(msg->received_host, conf.ident_trusted_nets)) {
|
meillo@10
|
774 #else
|
meillo@10
|
775 if ((msg->received_host == NULL) && (msg->received_prot == PROT_LOCAL)) {
|
meillo@10
|
776 #endif
|
meillo@10
|
777 if (msg->ident) {
|
meillo@10
|
778 if (strcmp(pw->pw_name, msg->ident) == 0) {
|
meillo@10
|
779 if (queue_delete(argv[arg]))
|
meillo@10
|
780 ok = TRUE;
|
meillo@10
|
781 } else {
|
meillo@10
|
782 fprintf(stderr, "you do not own message id %s\n", argv[arg]);
|
meillo@10
|
783 }
|
meillo@10
|
784 } else
|
meillo@10
|
785 fprintf(stderr, "message %s does not have an ident.\n", argv[arg]);
|
meillo@10
|
786 } else {
|
meillo@10
|
787 fprintf(stderr, "message %s was not received locally or from a trusted network.\n", argv[arg]);
|
meillo@10
|
788 }
|
meillo@10
|
789 }
|
meillo@10
|
790 } else {
|
meillo@10
|
791 fprintf(stderr, "could not find a passwd entry for uid %d: %s\n", conf.orig_uid, strerror(errno));
|
meillo@10
|
792 }
|
meillo@10
|
793 }
|
meillo@10
|
794 exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
meillo@10
|
795 } else {
|
meillo@10
|
796 fprintf(stderr, "unknown command %s\n", M_cmd);
|
meillo@10
|
797 exit(EXIT_FAILURE);
|
meillo@10
|
798 }
|
meillo@10
|
799 break;
|
meillo@10
|
800
|
meillo@10
|
801 case MODE_ACCEPT:
|
meillo@10
|
802 {
|
meillo@10
|
803 guint accept_flags = (opt_t ? ACC_DEL_RCPTS | ACC_DEL_BCC | ACC_RCPT_FROM_HEAD : ACC_HEAD_FROM_RCPT)
|
meillo@10
|
804 | (opt_i ? ACC_NODOT_TERM : ACC_NODOT_RELAX);
|
meillo@10
|
805 mode_accept(return_path, full_sender_name, accept_flags, &(argv[arg]), argc - arg);
|
meillo@10
|
806 exit(exit_failure ? EXIT_FAILURE : EXIT_SUCCESS);
|
meillo@10
|
807 }
|
meillo@10
|
808 break;
|
meillo@10
|
809 case MODE_NONE:
|
meillo@10
|
810 break;
|
meillo@10
|
811 default:
|
meillo@10
|
812 fprintf(stderr, "unknown mode: %d\n", mta_mode);
|
meillo@10
|
813 break;
|
meillo@0
|
814 }
|
meillo@0
|
815
|
meillo@10
|
816 logclose();
|
meillo@0
|
817
|
meillo@10
|
818 exit(exit_code);
|
meillo@0
|
819 }
|