annotate src/mlmmj-receive.c @ 756:f8f3f4525a2b

Cosmetic changes to help messages
author Ben Schmidt
date Fri, 08 Oct 2010 01:47:46 +1100
parents dfc9ab125fd4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
1 /* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
2 *
21ce01de8109 Initial revision
mmj
parents:
diff changeset
3 * $Id$
21ce01de8109 Initial revision
mmj
parents:
diff changeset
4 *
225
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
6 * of this software and associated documentation files (the "Software"), to
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
7 * deal in the Software without restriction, including without limitation the
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
9 * sell copies of the Software, and to permit persons to whom the Software is
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
10 * furnished to do so, subject to the following conditions:
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
11 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
12 * The above copyright notice and this permission notice shall be included in
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
13 * all copies or substantial portions of the Software.
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
14 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 208
diff changeset
21 * IN THE SOFTWARE.
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
22 */
21ce01de8109 Initial revision
mmj
parents:
diff changeset
23
21ce01de8109 Initial revision
mmj
parents:
diff changeset
24 #include <stdio.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
25 #include <stdlib.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
26 #include <unistd.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
27 #include <errno.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
28 #include <string.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
29 #include <sys/types.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
30 #include <sys/stat.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
31 #include <fcntl.h>
37
6b61ded65896 Send around the full path to the binaries
mmj
parents: 27
diff changeset
32 #include <libgen.h>
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
33
21ce01de8109 Initial revision
mmj
parents:
diff changeset
34 #include "mlmmj.h"
21ce01de8109 Initial revision
mmj
parents:
diff changeset
35 #include "wrappers.h"
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
36 #include "mygetline.h"
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
37 #include "strgen.h"
25
65d01831c0eb log_error is it's own object now
mmj
parents: 18
diff changeset
38 #include "log_error.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
39 #include "memory.h"
18
f54f95a4c53e changed to use log_error() all over
mortenp
parents: 11
diff changeset
40
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
41 extern char *optarg;
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
42
21ce01de8109 Initial revision
mmj
parents:
diff changeset
43 static void print_help(const char *prg)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
44 {
756
f8f3f4525a2b Cosmetic changes to help messages
Ben Schmidt
parents: 748
diff changeset
45 printf("Usage: %s -L /path/to/listdir\n"
f8f3f4525a2b Cosmetic changes to help messages
Ben Schmidt
parents: 748
diff changeset
46 " [-s sender@example.org] [-e extension] [-h] [-V] [-P] [-F]\n"
102
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
47 " -h: This help\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
48 " -F: Don't fork in the background\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
49 " -L: Full path to list directory\n"
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
50 " -s: Specify sender address\n"
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
51 " -e: The foo part of user+foo@example.org\n"
102
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
52 " -P: Don't execute mlmmj-process\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
53 " -V: Print version\n", prg);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
54 exit(EXIT_SUCCESS);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
55 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
56
21ce01de8109 Initial revision
mmj
parents:
diff changeset
57 int main(int argc, char **argv)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
58 {
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
59 char *infilename = NULL, *listdir = NULL;
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
60 char *randomstr = random_str();
423
ec509ab83a4c Removed discardedname var since we don't need it anymore
mmj
parents: 416
diff changeset
61 char *mlmmjprocess, *bindir;
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
62 int fd, opt, noprocess = 0, nofork = 0;
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
63 struct stat st;
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
64 uid_t uid;
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
65 pid_t childpid;
208
688011dbc4b7 Add a macro to make sure mlmmj binaries are invoked with full path
mmj
parents: 170
diff changeset
66
688011dbc4b7 Add a macro to make sure mlmmj binaries are invoked with full path
mmj
parents: 170
diff changeset
67 CHECKFULLPATH(argv[0]);
38
7f4c75a4a346 dirname(argv[0]) -> dirname(argv0);
mmj
parents: 37
diff changeset
68
46
4a80ff540431 added a call to log_set_name()
mortenp
parents: 39
diff changeset
69 log_set_name(argv[0]);
4a80ff540431 added a call to log_set_name()
mortenp
parents: 39
diff changeset
70
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 102
diff changeset
71 bindir = mydirname(argv[0]);
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 102
diff changeset
72 mlmmjprocess = concatstr(2, bindir, "/mlmmj-process");
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
73 myfree(bindir);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
74
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
75 while ((opt = getopt(argc, argv, "hPVL:s:e:F")) != -1) {
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
76 switch(opt) {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
77 case 'h':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
78 print_help(argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
79 break;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
80 case 'L':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
81 listdir = optarg;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
82 break;
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
83 case 's':
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
84 setenv("SENDER", optarg, 1);
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
85 break;
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
86 case 'e':
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
87 setenv("EXTENSION", optarg, 1);
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
88 break;
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
89 case 'P':
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
90 noprocess = 1;
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
91 break;
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
92 case 'F':
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
93 nofork = 1;
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
94 break;
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
95 case 'V':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
96 print_version(argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
97 exit(0);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
98 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
99 }
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
100
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
101 if(listdir == NULL) {
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
102 fprintf(stderr, "You have to specify -L\n");
21ce01de8109 Initial revision
mmj
parents:
diff changeset
103 fprintf(stderr, "%s -h for help\n", argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
104 exit(EXIT_FAILURE);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
105 }
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
106
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
107 /* Lets make sure no random user tries to send mail to the list */
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
108 if(listdir) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
109 if(stat(listdir, &st) == 0) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
110 uid = getuid();
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
111 if(uid && uid != st.st_uid) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
112 log_error(LOG_ARGS,
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
113 "Have to invoke either as root "
395
f4d98bcb6f61 Inform about uid in the logs when invoked wrongly
mmj
parents: 312
diff changeset
114 "or as the user owning listdir "
f4d98bcb6f61 Inform about uid in the logs when invoked wrongly
mmj
parents: 312
diff changeset
115 "Invoked with uid = [%d]", (int)uid);
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
116 writen(STDERR_FILENO,
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
117 "Have to invoke either as root "
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
118 "or as the user owning listdir\n", 60);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
119 exit(EXIT_FAILURE);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
120 }
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
121 } else {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
122 log_error(LOG_ARGS, "Could not stat %s", listdir);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
123 exit(EXIT_FAILURE);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
124 }
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
125 }
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
126
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
127 infilename = concatstr(3, listdir, "/incoming/", randomstr);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
128 myfree(randomstr);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
129 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
130 while(fd < 0 && errno == EEXIST) {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
131 myfree(infilename);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
132 randomstr = random_str();
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
133 infilename = concatstr(3, listdir, "/incoming/", randomstr);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
134 myfree(randomstr);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
135 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
136 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
137
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
138 if(fd < 0) {
58
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
139 log_error(LOG_ARGS, "could not create mail file in "
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
140 "%s/incoming directory", listdir);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
141 myfree(infilename);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
142 exit(EXIT_FAILURE);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
143 }
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
144
170
a9115cfd8564 fileno(stdin) instead of FILENO_STDIN
mmj
parents: 157
diff changeset
145 if(dumpfd2fd(fileno(stdin), fd) != 0) {
748
dfc9ab125fd4 Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents: 689
diff changeset
146 log_error(LOG_ARGS, "Could not receive mail");
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
147 exit(EXIT_FAILURE);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
148 }
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
149
430
4ec6c541b7c0 Logging
mmj
parents: 423
diff changeset
150 #if 0
748
dfc9ab125fd4 Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents: 689
diff changeset
151 log_oper(listdir, OPLOGFNAME, "mlmmj-receive got %s", infilename);
430
4ec6c541b7c0 Logging
mmj
parents: 423
diff changeset
152 #endif
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
153 fsync(fd);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
154 close(fd);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
155
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
156 if(noprocess) {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
157 myfree(infilename);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
158 exit(EXIT_SUCCESS);
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
159 }
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
160
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
161 /*
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
162 * Now we fork so we can exit with success since it could potentially
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
163 * take a long time for mlmmj-send to finish delivering the mails and
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
164 * returning, making it susceptible to getting a SIGKILL from the
748
dfc9ab125fd4 Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents: 689
diff changeset
165 * mailserver invoking mlmmj-receive.
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
166 */
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
167 if (!nofork) {
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
168 childpid = fork();
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
169 if(childpid < 0)
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
170 log_error(LOG_ARGS, "fork() failed! Proceeding anyway");
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
171
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
172 if(childpid)
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
173 exit(EXIT_SUCCESS); /* Parent says: "bye bye kids!"*/
544
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
174
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
175 close(0);
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
176 close(1);
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
177 close(2);
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
178 }
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
179
37
6b61ded65896 Send around the full path to the binaries
mmj
parents: 27
diff changeset
180 execlp(mlmmjprocess, mlmmjprocess,
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
181 "-L", listdir,
661
497526dfbb70 Fixed mlmmj-recieve [sic] for architectures where sizeof(int) and sizeof(char *) differ
mortenp
parents: 544
diff changeset
182 "-m", infilename, (char *)NULL);
39
3509e8837988 changed log_error() to be a function (no C99 here)
mortenp
parents: 38
diff changeset
183 log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjprocess);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
184
58
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
185 exit(EXIT_FAILURE);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
186 }