author | Sunil Nimmagadda <sunil@esdenera.com> |
Mon, 09 Jan 2017 15:31:05 +0500 | |
changeset 43 | 6903f7870c4c |
parent 40 | a0c73de318ed |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (c) 2014 Sunil Nimmagadda <sunil@nimmagadda.net> |
|
3 |
* |
|
4 |
* Permission to use, copy, modify, and distribute this software for any |
|
5 |
* purpose with or without fee is hereby granted, provided that the above |
|
6 |
* copyright notice and this permission notice appear in all copies. |
|
7 |
* |
|
8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
15 |
*/ |
|
16 |
||
17 |
#include <sys/param.h> |
|
18 |
#include <sys/types.h> |
|
19 |
#include <sys/socket.h> |
|
20 |
#include <sys/time.h> |
|
21 |
#include <sys/wait.h> |
|
22 |
||
23 |
#include <bsd_auth.h> |
|
24 |
#include <err.h> |
|
25 |
#include <event.h> |
|
26 |
#include <login_cap.h> |
|
27 |
#include <pwd.h> |
|
28 |
#include <signal.h> |
|
29 |
#include <stdio.h> |
|
30 |
#include <stdlib.h> |
|
31 |
#include <string.h> |
|
32 |
#include <syslog.h> |
|
33 |
#include <unistd.h> |
|
34 |
||
35 |
#include "imsgev.h" |
|
36 |
#include "pop3d.h" |
|
37 |
||
38 |
#define MBOX_PATH "/var/mail/%u" |
|
39 |
#define MAILDIR_PATH "~/Maildir" |
|
40 |
#define POP3D_USER "_pop3d" |
|
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
41 |
#define CERTFILE "/etc/ssl/server.crt" |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
42 |
#define KEYFILE "/etc/ssl/private/server.key" |
0 | 43 |
|
44 |
static void authenticate(struct imsgev *, struct imsg *); |
|
45 |
static void pop3e_imsgev(struct imsgev *, int , struct imsg *); |
|
46 |
static void needfd(struct imsgev *); |
|
47 |
static void sig_handler(int, short, void *); |
|
48 |
static enum m_type m_type(const char *); |
|
49 |
static void usage(void); |
|
50 |
||
51 |
static struct imsgev iev_pop3e; |
|
32
efb124b66dbd
mtype, mpath are accessible to maildrop, simplify setup parameters.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
31
diff
changeset
|
52 |
const char *mpath; |
efb124b66dbd
mtype, mpath are accessible to maildrop, simplify setup parameters.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
31
diff
changeset
|
53 |
int mtype = M_MBOX; |
0 | 54 |
|
55 |
int |
|
56 |
main(int argc, char *argv[]) |
|
57 |
{ |
|
58 |
struct passwd *pw; |
|
59 |
struct event ev_sigint, ev_sigterm, ev_sighup, ev_sigchld; |
|
12
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
60 |
const char *path = NULL, *mtype_str = "mbox"; |
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
61 |
const char *cert = CERTFILE, *key = KEYFILE; |
0 | 62 |
int ch, d = 0, pair[2]; |
63 |
||
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
64 |
while ((ch = getopt(argc, argv, "c:dk:p:t:")) != -1) { |
0 | 65 |
switch (ch) { |
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
66 |
case 'c': |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
67 |
cert = optarg; |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
68 |
break; |
0 | 69 |
case 'd': |
70 |
d = 1; |
|
71 |
break; |
|
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
72 |
case 'k': |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
73 |
key = optarg; |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
74 |
break; |
0 | 75 |
case 'p': |
12
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
76 |
path = optarg; |
0 | 77 |
break; |
78 |
case 't': |
|
79 |
if ((mtype = m_type(optarg)) == -1) |
|
80 |
errx(1, "%s invalid argument", optarg); |
|
81 |
mtype_str = optarg; |
|
82 |
break; |
|
83 |
default: |
|
84 |
usage(); |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
argc -= optind; |
|
89 |
argv += optind; |
|
90 |
if (argc > 0 || *argv) |
|
91 |
usage(); |
|
92 |
||
12
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
93 |
if (path) |
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
94 |
mpath = path; |
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
95 |
else |
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
96 |
mpath = (mtype == M_MAILDIR) ? MAILDIR_PATH : MBOX_PATH; |
1d8309e51034
Simplify maildrop path handling. Regardless of the order of options path is now correctly handled.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
5
diff
changeset
|
97 |
|
0 | 98 |
log_init(d); |
99 |
if (geteuid()) |
|
100 |
fatalx("need root privileges"); |
|
101 |
||
102 |
if (!d && daemon(1, 0) == -1) |
|
103 |
fatal("failed to daemonize"); |
|
104 |
||
105 |
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) == -1) |
|
106 |
fatal("socketpair"); |
|
107 |
||
108 |
set_nonblocking(pair[0]); |
|
109 |
set_nonblocking(pair[1]); |
|
110 |
if ((pw = getpwnam(POP3D_USER)) == NULL) |
|
111 |
fatalx("main: getpwnam " POP3D_USER); |
|
112 |
||
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
113 |
pop3_main(pair, pw, cert, key); |
0 | 114 |
close(pair[1]); |
115 |
setproctitle("[priv]"); |
|
116 |
logit(LOG_INFO, "pop3d ready; type:%s, path:%s", mtype_str, mpath); |
|
117 |
event_init(); |
|
118 |
signal_set(&ev_sigint, SIGINT, sig_handler, NULL); |
|
119 |
signal_set(&ev_sighup, SIGHUP, sig_handler, NULL); |
|
120 |
signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL); |
|
121 |
signal_set(&ev_sigchld, SIGCHLD, sig_handler, NULL); |
|
122 |
signal_add(&ev_sigint, NULL); |
|
123 |
signal_add(&ev_sighup, NULL); |
|
124 |
signal_add(&ev_sigterm, NULL); |
|
125 |
signal_add(&ev_sigchld, NULL); |
|
126 |
imsgev_init(&iev_pop3e, pair[0], NULL, pop3e_imsgev, needfd); |
|
127 |
if (event_dispatch() < 0) |
|
128 |
fatal("event_dispatch"); |
|
129 |
||
130 |
logit(LOG_INFO, "pop3d exiting"); |
|
131 |
return (0); |
|
132 |
} |
|
133 |
||
134 |
static void |
|
135 |
pop3e_imsgev(struct imsgev *iev, int code, struct imsg *imsg) |
|
136 |
{ |
|
137 |
switch (code) { |
|
138 |
case IMSGEV_IMSG: |
|
139 |
switch (imsg->hdr.type) { |
|
140 |
case IMSG_AUTH: |
|
141 |
authenticate(iev, imsg); |
|
142 |
break; |
|
143 |
default: |
|
144 |
logit(LOG_DEBUG, "%s: unexpected imsg %u", |
|
145 |
__func__, imsg->hdr.type); |
|
146 |
break; |
|
147 |
} |
|
148 |
break; |
|
149 |
case IMSGEV_EREAD: |
|
150 |
case IMSGEV_EWRITE: |
|
151 |
case IMSGEV_EIMSG: |
|
152 |
fatal("pop3d: imsgev read/write error"); |
|
153 |
break; |
|
154 |
case IMSGEV_DONE: |
|
155 |
event_loopexit(NULL); |
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
static void |
|
161 |
authenticate(struct imsgev *iev, struct imsg *imsg) |
|
162 |
{ |
|
163 |
struct auth_req *req = imsg->data; |
|
164 |
struct passwd *pw; |
|
165 |
int pair[2]; |
|
166 |
||
167 |
if (auth_userokay(req->user, NULL, "auth-pop3", req->pass) == 0) { |
|
168 |
logit(LOG_INFO, "%u: auth [%s] failed", |
|
169 |
imsg->hdr.peerid, req->user); |
|
170 |
pair[0] = -1; |
|
171 |
goto end; |
|
172 |
} |
|
173 |
||
174 |
logit(LOG_INFO, "%u: auth [%s] passed", imsg->hdr.peerid, |
|
175 |
req->user); |
|
176 |
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) == -1) |
|
177 |
fatal("socketpair"); |
|
178 |
||
179 |
set_nonblocking(pair[0]); |
|
180 |
set_nonblocking(pair[1]); |
|
181 |
if ((pw = getpwnam(req->user)) == NULL) |
|
182 |
fatalx("authenticate: getpwnam"); |
|
183 |
||
32
efb124b66dbd
mtype, mpath are accessible to maildrop, simplify setup parameters.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
31
diff
changeset
|
184 |
if (maildrop_setup(imsg->hdr.peerid, pair, pw) == -1) { |
4 | 185 |
logit(LOG_INFO, "%u: unable to fork maildrop process", |
186 |
imsg->hdr.peerid); |
|
40
a0c73de318ed
Stop leaking sock descriptors in error path.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
32
diff
changeset
|
187 |
close(pair[0]); |
a0c73de318ed
Stop leaking sock descriptors in error path.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
32
diff
changeset
|
188 |
close(pair[1]); |
4 | 189 |
pair[0] = -1; |
190 |
goto end; |
|
191 |
} |
|
192 |
||
0 | 193 |
close(pair[1]); |
194 |
end: |
|
195 |
imsgev_xcompose(iev, IMSG_AUTH, imsg->hdr.peerid, 0, |
|
196 |
pair[0], NULL, 0, "authenticate"); |
|
197 |
} |
|
198 |
||
199 |
static void |
|
200 |
needfd(struct imsgev *iev) |
|
201 |
{ |
|
202 |
fatalx("pop3d should never need an fd"); |
|
203 |
} |
|
204 |
||
205 |
static void |
|
206 |
sig_handler(int sig, short event, void *arg) |
|
207 |
{ |
|
208 |
int status; |
|
209 |
||
210 |
switch (sig) { |
|
211 |
case SIGINT: |
|
212 |
case SIGHUP: |
|
213 |
case SIGTERM: |
|
214 |
imsgev_clear(&iev_pop3e); |
|
215 |
event_loopexit(NULL); |
|
216 |
break; |
|
217 |
case SIGCHLD: |
|
11
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
218 |
if (waitpid(WAIT_ANY, &status, WNOHANG) > 0) |
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
219 |
if ((WIFEXITED(status) && WEXITSTATUS(status) != 0) || |
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
220 |
WIFSIGNALED(status)) { |
0 | 221 |
logit(LOG_ERR, "Lost pop3 engine"); |
222 |
event_loopexit(NULL); |
|
223 |
} |
|
224 |
break; |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
static enum m_type |
|
229 |
m_type(const char *str) |
|
230 |
{ |
|
231 |
if (strcasecmp(str, "mbox") == 0) |
|
232 |
return M_MBOX; |
|
233 |
||
234 |
if (strcasecmp(str, "maildir") == 0) |
|
235 |
return M_MAILDIR; |
|
236 |
||
237 |
return (-1); |
|
238 |
} |
|
239 |
||
240 |
static void |
|
241 |
usage(void) |
|
242 |
{ |
|
243 |
extern char *__progname; |
|
244 |
||
43
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
245 |
fprintf(stderr, "usage: %s [-c certfile] [-d] " |
6903f7870c4c
Provide an option to specify cert/key on commandline.
Sunil Nimmagadda <sunil@esdenera.com>
parents:
40
diff
changeset
|
246 |
"[-k keyfile] [-p path] [-t type]\n", __progname); |
0 | 247 |
exit(EXIT_FAILURE); |
248 |
} |
|
249 |