annotate src/mlmmj-receive.c @ 748:dfc9ab125fd4

Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
author Chris Webb
date Sun, 03 Oct 2010 21:40:42 +1100
parents src/mlmmj-recieve.c@94429995a6a8
children f8f3f4525a2b
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 {
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
45 printf("Usage: %s -L /path/to/listdir [-s sender@example.org] [-e extension] [-h] [-V] [-P] [-F]\n"
102
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
46 " -h: This help\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
47 " -F: Don't fork in the background\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
48 " -L: Full path to list directory\n"
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
49 " -s: Specify sender address\n"
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
50 " -e: The foo part of user+foo@example.org\n"
102
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
51 " -P: Don't execute mlmmj-process\n"
713e9d076c26 Clean up and extend help everywhere.
mmj
parents: 64
diff changeset
52 " -V: Print version\n", prg);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
53 exit(EXIT_SUCCESS);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
54 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
55
21ce01de8109 Initial revision
mmj
parents:
diff changeset
56 int main(int argc, char **argv)
21ce01de8109 Initial revision
mmj
parents:
diff changeset
57 {
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
58 char *infilename = NULL, *listdir = NULL;
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
59 char *randomstr = random_str();
423
ec509ab83a4c Removed discardedname var since we don't need it anymore
mmj
parents: 416
diff changeset
60 char *mlmmjprocess, *bindir;
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
61 int fd, opt, noprocess = 0, nofork = 0;
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
62 struct stat st;
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
63 uid_t uid;
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
64 pid_t childpid;
208
688011dbc4b7 Add a macro to make sure mlmmj binaries are invoked with full path
mmj
parents: 170
diff changeset
65
688011dbc4b7 Add a macro to make sure mlmmj binaries are invoked with full path
mmj
parents: 170
diff changeset
66 CHECKFULLPATH(argv[0]);
38
7f4c75a4a346 dirname(argv[0]) -> dirname(argv0);
mmj
parents: 37
diff changeset
67
46
4a80ff540431 added a call to log_set_name()
mortenp
parents: 39
diff changeset
68 log_set_name(argv[0]);
4a80ff540431 added a call to log_set_name()
mortenp
parents: 39
diff changeset
69
117
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 102
diff changeset
70 bindir = mydirname(argv[0]);
9a39dff171fa added mydirname() and mybasename() which doesn't mess with their argument, and
mortenp
parents: 102
diff changeset
71 mlmmjprocess = concatstr(2, bindir, "/mlmmj-process");
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
72 myfree(bindir);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
73
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
74 while ((opt = getopt(argc, argv, "hPVL:s:e:F")) != -1) {
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
75 switch(opt) {
21ce01de8109 Initial revision
mmj
parents:
diff changeset
76 case 'h':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
77 print_help(argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
78 break;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
79 case 'L':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
80 listdir = optarg;
21ce01de8109 Initial revision
mmj
parents:
diff changeset
81 break;
689
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
82 case 's':
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
83 setenv("SENDER", optarg, 1);
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
84 break;
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
85 case 'e':
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
86 setenv("EXTENSION", optarg, 1);
94429995a6a8 Added Postfix pipe backend support (Niki Guldbrand)
mortenp
parents: 661
diff changeset
87 break;
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
88 case 'P':
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
89 noprocess = 1;
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
90 break;
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
91 case 'F':
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
92 nofork = 1;
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
93 break;
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
94 case 'V':
21ce01de8109 Initial revision
mmj
parents:
diff changeset
95 print_version(argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
96 exit(0);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
97 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
98 }
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
99
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
100 if(listdir == NULL) {
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
101 fprintf(stderr, "You have to specify -L\n");
21ce01de8109 Initial revision
mmj
parents:
diff changeset
102 fprintf(stderr, "%s -h for help\n", argv[0]);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
103 exit(EXIT_FAILURE);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
104 }
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
105
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
106 /* Lets make sure no random user tries to send mail to the list */
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
107 if(listdir) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
108 if(stat(listdir, &st) == 0) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
109 uid = getuid();
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
110 if(uid && uid != st.st_uid) {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
111 log_error(LOG_ARGS,
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
112 "Have to invoke either as root "
395
f4d98bcb6f61 Inform about uid in the logs when invoked wrongly
mmj
parents: 312
diff changeset
113 "or as the user owning listdir "
f4d98bcb6f61 Inform about uid in the logs when invoked wrongly
mmj
parents: 312
diff changeset
114 "Invoked with uid = [%d]", (int)uid);
295
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
115 writen(STDERR_FILENO,
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
116 "Have to invoke either as root "
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
117 "or as the user owning listdir\n", 60);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
118 exit(EXIT_FAILURE);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
119 }
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
120 } else {
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
121 log_error(LOG_ARGS, "Could not stat %s", listdir);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
122 exit(EXIT_FAILURE);
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
123 }
db3bd20566aa uid check et al
mmj
parents: 245
diff changeset
124 }
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
125
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
126 infilename = concatstr(3, listdir, "/incoming/", randomstr);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
127 myfree(randomstr);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
128 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
129 while(fd < 0 && errno == EEXIST) {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
130 myfree(infilename);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
131 randomstr = random_str();
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
132 infilename = concatstr(3, listdir, "/incoming/", randomstr);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
133 myfree(randomstr);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
134 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
135 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
136
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
137 if(fd < 0) {
58
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
138 log_error(LOG_ARGS, "could not create mail file in "
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
139 "%s/incoming directory", listdir);
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
140 myfree(infilename);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
141 exit(EXIT_FAILURE);
21ce01de8109 Initial revision
mmj
parents:
diff changeset
142 }
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
143
170
a9115cfd8564 fileno(stdin) instead of FILENO_STDIN
mmj
parents: 157
diff changeset
144 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
145 log_error(LOG_ARGS, "Could not receive mail");
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
146 exit(EXIT_FAILURE);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
147 }
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
148
430
4ec6c541b7c0 Logging
mmj
parents: 423
diff changeset
149 #if 0
748
dfc9ab125fd4 Fix spelling of 'receive' and 'voodoo'; make mlmmj-recieve a symlink
Chris Webb
parents: 689
diff changeset
150 log_oper(listdir, OPLOGFNAME, "mlmmj-receive got %s", infilename);
430
4ec6c541b7c0 Logging
mmj
parents: 423
diff changeset
151 #endif
157
af54da533809 mlmmj-recieve deFILEified
mmj
parents: 117
diff changeset
152 fsync(fd);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
153 close(fd);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
154
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
155 if(noprocess) {
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
156 myfree(infilename);
11
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
157 exit(EXIT_SUCCESS);
8e1ebc68ee62 do_all_the_voodo_here introducton plus a massive cleanup
mmj
parents: 3
diff changeset
158 }
3
bea555bef489 Mainly cleanups. More to follow.
mmj
parents: 1
diff changeset
159
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
160 /*
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
161 * Now we fork so we can exit with success since it could potentially
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
162 * take a long time for mlmmj-send to finish delivering the mails and
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
163 * 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
164 * mailserver invoking mlmmj-receive.
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
165 */
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
166 if (!nofork) {
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
167 childpid = fork();
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
168 if(childpid < 0)
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
169 log_error(LOG_ARGS, "fork() failed! Proceeding anyway");
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
170
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
171 if(childpid)
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
172 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
173
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
174 close(0);
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
175 close(1);
869a202ffb3b Close stdin, stdout and stderr in mlmmj-recieve after forking.
xi
parents: 430
diff changeset
176 close(2);
64
4a8c5a8cebbc added -F (no fork) option
mortenp
parents: 58
diff changeset
177 }
52
e8b0cd47fe1e mlmmj-recieve fork()
mmj
parents: 46
diff changeset
178
37
6b61ded65896 Send around the full path to the binaries
mmj
parents: 27
diff changeset
179 execlp(mlmmjprocess, mlmmjprocess,
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
180 "-L", listdir,
661
497526dfbb70 Fixed mlmmj-recieve [sic] for architectures where sizeof(int) and sizeof(char *) differ
mortenp
parents: 544
diff changeset
181 "-m", infilename, (char *)NULL);
39
3509e8837988 changed log_error() to be a function (no C99 here)
mortenp
parents: 38
diff changeset
182 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
183
58
990e1a4ae6d0 Small typo and bouncehandling now works
mmj
parents: 52
diff changeset
184 exit(EXIT_FAILURE);
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
185 }