Mercurial > hg > mlmmj
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 |
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 { | |
756 | 45 printf("Usage: %s -L /path/to/listdir\n" |
46 " [-s sender@example.org] [-e extension] [-h] [-V] [-P] [-F]\n" | |
102 | 47 " -h: This help\n" |
48 " -F: Don't fork in the background\n" | |
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 | 52 " -P: Don't execute mlmmj-process\n" |
53 " -V: Print version\n", prg); | |
11 | 54 exit(EXIT_SUCCESS); |
0 | 55 } |
56 | |
57 int main(int argc, char **argv) | |
58 { | |
157 | 59 char *infilename = NULL, *listdir = NULL; |
11 | 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 | 62 int fd, opt, noprocess = 0, nofork = 0; |
295 | 63 struct stat st; |
64 uid_t uid; | |
52 | 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 | 68 |
46 | 69 log_set_name(argv[0]); |
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 | 73 myfree(bindir); |
0 | 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 | 76 switch(opt) { |
77 case 'h': | |
78 print_help(argv[0]); | |
79 break; | |
80 case 'L': | |
81 listdir = optarg; | |
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 | 89 case 'P': |
11 | 90 noprocess = 1; |
3 | 91 break; |
64 | 92 case 'F': |
93 nofork = 1; | |
94 break; | |
0 | 95 case 'V': |
96 print_version(argv[0]); | |
97 exit(0); | |
98 } | |
99 } | |
295 | 100 |
11 | 101 if(listdir == NULL) { |
0 | 102 fprintf(stderr, "You have to specify -L\n"); |
103 fprintf(stderr, "%s -h for help\n", argv[0]); | |
104 exit(EXIT_FAILURE); | |
105 } | |
295 | 106 |
107 /* Lets make sure no random user tries to send mail to the list */ | |
108 if(listdir) { | |
109 if(stat(listdir, &st) == 0) { | |
110 uid = getuid(); | |
111 if(uid && uid != st.st_uid) { | |
112 log_error(LOG_ARGS, | |
113 "Have to invoke either as root " | |
395 | 114 "or as the user owning listdir " |
115 "Invoked with uid = [%d]", (int)uid); | |
295 | 116 writen(STDERR_FILENO, |
117 "Have to invoke either as root " | |
118 "or as the user owning listdir\n", 60); | |
119 exit(EXIT_FAILURE); | |
120 } | |
121 } else { | |
122 log_error(LOG_ARGS, "Could not stat %s", listdir); | |
123 exit(EXIT_FAILURE); | |
124 } | |
125 } | |
0 | 126 |
11 | 127 infilename = concatstr(3, listdir, "/incoming/", randomstr); |
245 | 128 myfree(randomstr); |
11 | 129 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
130 while(fd < 0 && errno == EEXIST) { | |
245 | 131 myfree(infilename); |
11 | 132 randomstr = random_str(); |
133 infilename = concatstr(3, listdir, "/incoming/", randomstr); | |
245 | 134 myfree(randomstr); |
11 | 135 fd = open(infilename, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
0 | 136 } |
137 | |
11 | 138 if(fd < 0) { |
58 | 139 log_error(LOG_ARGS, "could not create mail file in " |
140 "%s/incoming directory", listdir); | |
245 | 141 myfree(infilename); |
0 | 142 exit(EXIT_FAILURE); |
143 } | |
11 | 144 |
170 | 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 | 147 exit(EXIT_FAILURE); |
11 | 148 } |
0 | 149 |
430 | 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 | 152 #endif |
157 | 153 fsync(fd); |
11 | 154 close(fd); |
0 | 155 |
11 | 156 if(noprocess) { |
245 | 157 myfree(infilename); |
11 | 158 exit(EXIT_SUCCESS); |
159 } | |
3 | 160 |
52 | 161 /* |
162 * Now we fork so we can exit with success since it could potentially | |
163 * take a long time for mlmmj-send to finish delivering the mails and | |
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 | 166 */ |
64 | 167 if (!nofork) { |
168 childpid = fork(); | |
169 if(childpid < 0) | |
170 log_error(LOG_ARGS, "fork() failed! Proceeding anyway"); | |
52 | 171 |
64 | 172 if(childpid) |
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 | 178 } |
52 | 179 |
37 | 180 execlp(mlmmjprocess, mlmmjprocess, |
0 | 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 | 183 log_error(LOG_ARGS, "execlp() of '%s' failed", mlmmjprocess); |
11 | 184 |
58 | 185 exit(EXIT_FAILURE); |
0 | 186 } |