Mercurial > hg > mlmmj
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 |
rev | line source |
---|---|
0 | 1 /* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk> |
2 * | |
3 * $Id$ | |
4 * | |
225 | 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. | |
0 | 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> | |
37 | 32 #include <libgen.h> |
0 | 33 |
34 #include "mlmmj.h" | |
35 #include "wrappers.h" | |
11 | 36 #include "mygetline.h" |
0 | 37 #include "strgen.h" |
25 | 38 #include "log_error.h" |
245 | 39 #include "memory.h" |
18 | 40 |
11 | 41 extern char *optarg; |
0 | 42 |
43 static void print_help(const char *prg) | |
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 | 46 " -h: This help\n" |
47 " -F: Don't fork in the background\n" | |
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 | 51 " -P: Don't execute mlmmj-process\n" |
52 " -V: Print version\n", prg); | |
11 | 53 exit(EXIT_SUCCESS); |
0 | 54 } |
55 | |
56 int main(int argc, char **argv) | |
57 { | |
157 | 58 char *infilename = NULL, *listdir = NULL; |
11 | 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 | 61 int fd, opt, noprocess = 0, nofork = 0; |
295 | 62 struct stat st; |
63 uid_t uid; | |
52 | 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 | 67 |
46 | 68 log_set_name(argv[0]); |
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 | 72 myfree(bindir); |
0 | 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 | 75 switch(opt) { |
76 case 'h': | |
77 print_help(argv[0]); | |
78 break; | |
79 case 'L': | |
80 listdir = optarg; | |
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 | 88 case 'P': |
11 | 89 noprocess = 1; |
3 | 90 break; |
64 | 91 case 'F': |
92 nofork = 1; | |
93 break; | |
0 | 94 case 'V': |
95 print_version(argv[0]); | |
96 exit(0); | |
97 } | |
98 } | |
295 | 99 |
11 | 100 if(listdir == NULL) { |
0 | 101 fprintf(stderr, "You have to specify -L\n"); |
102 fprintf(stderr, "%s -h for help\n", argv[0]); | |
103 exit(EXIT_FAILURE); | |
104 } | |
295 | 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 " | |
395 | 113 "or as the user owning listdir " |
114 "Invoked with uid = [%d]", (int)uid); | |
295 | 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 } | |
0 | 125 |
11 | 126 infilename = concatstr(3, listdir, "/incoming/", randomstr); |
245 | 127 myfree(randomstr); |
11 | 128 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
129 while(fd < 0 && errno == EEXIST) { | |
245 | 130 myfree(infilename); |
11 | 131 randomstr = random_str(); |
132 infilename = concatstr(3, listdir, "/incoming/", randomstr); | |
245 | 133 myfree(randomstr); |
11 | 134 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
0 | 135 } |
136 | |
11 | 137 if(fd < 0) { |
58 | 138 log_error(LOG_ARGS, "could not create mail file in " |
139 "%s/incoming directory", listdir); | |
245 | 140 myfree(infilename); |
0 | 141 exit(EXIT_FAILURE); |
142 } | |
11 | 143 |
170 | 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 | 146 exit(EXIT_FAILURE); |
11 | 147 } |
0 | 148 |
430 | 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 | 151 #endif |
157 | 152 fsync(fd); |
11 | 153 close(fd); |
0 | 154 |
11 | 155 if(noprocess) { |
245 | 156 myfree(infilename); |
11 | 157 exit(EXIT_SUCCESS); |
158 } | |
3 | 159 |
52 | 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 | |
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 | 165 */ |
64 | 166 if (!nofork) { |
167 childpid = fork(); | |
168 if(childpid < 0) | |
169 log_error(LOG_ARGS, "fork() failed! Proceeding anyway"); | |
52 | 170 |
64 | 171 if(childpid) |
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 | 177 } |
52 | 178 |
37 | 179 execlp(mlmmjprocess, mlmmjprocess, |
0 | 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 | 182 log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjprocess); |
11 | 183 |
58 | 184 exit(EXIT_FAILURE); |
0 | 185 } |