comparison 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
comparison
equal deleted inserted replaced
747:fd77dd58bde1 748:dfc9ab125fd4
1 /* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
2 *
3 * $Id$
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <libgen.h>
33
34 #include "mlmmj.h"
35 #include "wrappers.h"
36 #include "mygetline.h"
37 #include "strgen.h"
38 #include "log_error.h"
39 #include "memory.h"
40
41 extern char *optarg;
42
43 static void print_help(const char *prg)
44 {
45 printf("Usage: %s -L /path/to/listdir [-s sender@example.org] [-e extension] [-h] [-V] [-P] [-F]\n"
46 " -h: This help\n"
47 " -F: Don't fork in the background\n"
48 " -L: Full path to list directory\n"
49 " -s: Specify sender address\n"
50 " -e: The foo part of user+foo@example.org\n"
51 " -P: Don't execute mlmmj-process\n"
52 " -V: Print version\n", prg);
53 exit(EXIT_SUCCESS);
54 }
55
56 int main(int argc, char **argv)
57 {
58 char *infilename = NULL, *listdir = NULL;
59 char *randomstr = random_str();
60 char *mlmmjprocess, *bindir;
61 int fd, opt, noprocess = 0, nofork = 0;
62 struct stat st;
63 uid_t uid;
64 pid_t childpid;
65
66 CHECKFULLPATH(argv[0]);
67
68 log_set_name(argv[0]);
69
70 bindir = mydirname(argv[0]);
71 mlmmjprocess = concatstr(2, bindir, "/mlmmj-process");
72 myfree(bindir);
73
74 while ((opt = getopt(argc, argv, "hPVL:s:e:F")) != -1) {
75 switch(opt) {
76 case 'h':
77 print_help(argv[0]);
78 break;
79 case 'L':
80 listdir = optarg;
81 break;
82 case 's':
83 setenv("SENDER", optarg, 1);
84 break;
85 case 'e':
86 setenv("EXTENSION", optarg, 1);
87 break;
88 case 'P':
89 noprocess = 1;
90 break;
91 case 'F':
92 nofork = 1;
93 break;
94 case 'V':
95 print_version(argv[0]);
96 exit(0);
97 }
98 }
99
100 if(listdir == NULL) {
101 fprintf(stderr, "You have to specify -L\n");
102 fprintf(stderr, "%s -h for help\n", argv[0]);
103 exit(EXIT_FAILURE);
104 }
105
106 /* Lets make sure no random user tries to send mail to the list */
107 if(listdir) {
108 if(stat(listdir, &st) == 0) {
109 uid = getuid();
110 if(uid && uid != st.st_uid) {
111 log_error(LOG_ARGS,
112 "Have to invoke either as root "
113 "or as the user owning listdir "
114 "Invoked with uid = [%d]", (int)uid);
115 writen(STDERR_FILENO,
116 "Have to invoke either as root "
117 "or as the user owning listdir\n", 60);
118 exit(EXIT_FAILURE);
119 }
120 } else {
121 log_error(LOG_ARGS, "Could not stat %s", listdir);
122 exit(EXIT_FAILURE);
123 }
124 }
125
126 infilename = concatstr(3, listdir, "/incoming/", randomstr);
127 myfree(randomstr);
128 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
129 while(fd < 0 && errno == EEXIST) {
130 myfree(infilename);
131 randomstr = random_str();
132 infilename = concatstr(3, listdir, "/incoming/", randomstr);
133 myfree(randomstr);
134 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
135 }
136
137 if(fd < 0) {
138 log_error(LOG_ARGS, "could not create mail file in "
139 "%s/incoming directory", listdir);
140 myfree(infilename);
141 exit(EXIT_FAILURE);
142 }
143
144 if(dumpfd2fd(fileno(stdin), fd) != 0) {
145 log_error(LOG_ARGS, "Could not receive mail");
146 exit(EXIT_FAILURE);
147 }
148
149 #if 0
150 log_oper(listdir, OPLOGFNAME, "mlmmj-receive got %s", infilename);
151 #endif
152 fsync(fd);
153 close(fd);
154
155 if(noprocess) {
156 myfree(infilename);
157 exit(EXIT_SUCCESS);
158 }
159
160 /*
161 * Now we fork so we can exit with success since it could potentially
162 * take a long time for mlmmj-send to finish delivering the mails and
163 * returning, making it susceptible to getting a SIGKILL from the
164 * mailserver invoking mlmmj-receive.
165 */
166 if (!nofork) {
167 childpid = fork();
168 if(childpid < 0)
169 log_error(LOG_ARGS, "fork() failed! Proceeding anyway");
170
171 if(childpid)
172 exit(EXIT_SUCCESS); /* Parent says: "bye bye kids!"*/
173
174 close(0);
175 close(1);
176 close(2);
177 }
178
179 execlp(mlmmjprocess, mlmmjprocess,
180 "-L", listdir,
181 "-m", infilename, (char *)NULL);
182 log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjprocess);
183
184 exit(EXIT_FAILURE);
185 }