author | Sunil Nimmagadda <sunil@nimmagadda.net> |
Thu, 28 Aug 2014 14:17:28 +0500 | |
changeset 35 | dcf7a837ec0b |
parent 32 | efb124b66dbd |
child 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" |
|
41 |
||
42 |
static void authenticate(struct imsgev *, struct imsg *); |
|
43 |
static void pop3e_imsgev(struct imsgev *, int , struct imsg *); |
|
44 |
static void needfd(struct imsgev *); |
|
45 |
static void sig_handler(int, short, void *); |
|
46 |
static enum m_type m_type(const char *); |
|
47 |
static void usage(void); |
|
48 |
||
49 |
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
|
50 |
const char *mpath; |
efb124b66dbd
mtype, mpath are accessible to maildrop, simplify setup parameters.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
31
diff
changeset
|
51 |
int mtype = M_MBOX; |
0 | 52 |
|
53 |
int |
|
54 |
main(int argc, char *argv[]) |
|
55 |
{ |
|
56 |
struct passwd *pw; |
|
57 |
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
|
58 |
const char *path = NULL, *mtype_str = "mbox"; |
0 | 59 |
int ch, d = 0, pair[2]; |
60 |
||
61 |
while ((ch = getopt(argc, argv, "dp:t:")) != -1) { |
|
62 |
switch (ch) { |
|
63 |
case 'd': |
|
64 |
d = 1; |
|
65 |
break; |
|
66 |
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
|
67 |
path = optarg; |
0 | 68 |
break; |
69 |
case 't': |
|
70 |
if ((mtype = m_type(optarg)) == -1) |
|
71 |
errx(1, "%s invalid argument", optarg); |
|
72 |
mtype_str = optarg; |
|
73 |
break; |
|
74 |
default: |
|
75 |
usage(); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
argc -= optind; |
|
80 |
argv += optind; |
|
81 |
if (argc > 0 || *argv) |
|
82 |
usage(); |
|
83 |
||
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
|
0 | 89 |
log_init(d); |
90 |
if (geteuid()) |
|
91 |
fatalx("need root privileges"); |
|
92 |
||
93 |
if (!d && daemon(1, 0) == -1) |
|
94 |
fatal("failed to daemonize"); |
|
95 |
||
96 |
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) == -1) |
|
97 |
fatal("socketpair"); |
|
98 |
||
99 |
set_nonblocking(pair[0]); |
|
100 |
set_nonblocking(pair[1]); |
|
101 |
if ((pw = getpwnam(POP3D_USER)) == NULL) |
|
102 |
fatalx("main: getpwnam " POP3D_USER); |
|
103 |
||
14
1f2f363d10cc
Since we now waitpid on WAIT_ANY, pop3_main needed return pid.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
11
diff
changeset
|
104 |
pop3_main(pair, pw); |
0 | 105 |
close(pair[1]); |
106 |
setproctitle("[priv]"); |
|
107 |
logit(LOG_INFO, "pop3d ready; type:%s, path:%s", mtype_str, mpath); |
|
108 |
event_init(); |
|
109 |
signal_set(&ev_sigint, SIGINT, sig_handler, NULL); |
|
110 |
signal_set(&ev_sighup, SIGHUP, sig_handler, NULL); |
|
111 |
signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL); |
|
112 |
signal_set(&ev_sigchld, SIGCHLD, sig_handler, NULL); |
|
113 |
signal_add(&ev_sigint, NULL); |
|
114 |
signal_add(&ev_sighup, NULL); |
|
115 |
signal_add(&ev_sigterm, NULL); |
|
116 |
signal_add(&ev_sigchld, NULL); |
|
117 |
imsgev_init(&iev_pop3e, pair[0], NULL, pop3e_imsgev, needfd); |
|
118 |
if (event_dispatch() < 0) |
|
119 |
fatal("event_dispatch"); |
|
120 |
||
121 |
logit(LOG_INFO, "pop3d exiting"); |
|
122 |
return (0); |
|
123 |
} |
|
124 |
||
125 |
static void |
|
126 |
pop3e_imsgev(struct imsgev *iev, int code, struct imsg *imsg) |
|
127 |
{ |
|
128 |
switch (code) { |
|
129 |
case IMSGEV_IMSG: |
|
130 |
switch (imsg->hdr.type) { |
|
131 |
case IMSG_AUTH: |
|
132 |
authenticate(iev, imsg); |
|
133 |
break; |
|
134 |
default: |
|
135 |
logit(LOG_DEBUG, "%s: unexpected imsg %u", |
|
136 |
__func__, imsg->hdr.type); |
|
137 |
break; |
|
138 |
} |
|
139 |
break; |
|
140 |
case IMSGEV_EREAD: |
|
141 |
case IMSGEV_EWRITE: |
|
142 |
case IMSGEV_EIMSG: |
|
143 |
fatal("pop3d: imsgev read/write error"); |
|
144 |
break; |
|
145 |
case IMSGEV_DONE: |
|
146 |
event_loopexit(NULL); |
|
147 |
break; |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
static void |
|
152 |
authenticate(struct imsgev *iev, struct imsg *imsg) |
|
153 |
{ |
|
154 |
struct auth_req *req = imsg->data; |
|
155 |
struct passwd *pw; |
|
156 |
int pair[2]; |
|
157 |
||
158 |
if (auth_userokay(req->user, NULL, "auth-pop3", req->pass) == 0) { |
|
159 |
logit(LOG_INFO, "%u: auth [%s] failed", |
|
160 |
imsg->hdr.peerid, req->user); |
|
161 |
pair[0] = -1; |
|
162 |
goto end; |
|
163 |
} |
|
164 |
||
165 |
logit(LOG_INFO, "%u: auth [%s] passed", imsg->hdr.peerid, |
|
166 |
req->user); |
|
167 |
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) == -1) |
|
168 |
fatal("socketpair"); |
|
169 |
||
170 |
set_nonblocking(pair[0]); |
|
171 |
set_nonblocking(pair[1]); |
|
172 |
if ((pw = getpwnam(req->user)) == NULL) |
|
173 |
fatalx("authenticate: getpwnam"); |
|
174 |
||
32
efb124b66dbd
mtype, mpath are accessible to maildrop, simplify setup parameters.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
31
diff
changeset
|
175 |
if (maildrop_setup(imsg->hdr.peerid, pair, pw) == -1) { |
4 | 176 |
logit(LOG_INFO, "%u: unable to fork maildrop process", |
177 |
imsg->hdr.peerid); |
|
178 |
pair[0] = -1; |
|
179 |
goto end; |
|
180 |
} |
|
181 |
||
0 | 182 |
close(pair[1]); |
183 |
end: |
|
184 |
imsgev_xcompose(iev, IMSG_AUTH, imsg->hdr.peerid, 0, |
|
185 |
pair[0], NULL, 0, "authenticate"); |
|
186 |
} |
|
187 |
||
188 |
static void |
|
189 |
needfd(struct imsgev *iev) |
|
190 |
{ |
|
191 |
fatalx("pop3d should never need an fd"); |
|
192 |
} |
|
193 |
||
194 |
static void |
|
195 |
sig_handler(int sig, short event, void *arg) |
|
196 |
{ |
|
197 |
int status; |
|
198 |
||
199 |
switch (sig) { |
|
200 |
case SIGINT: |
|
201 |
case SIGHUP: |
|
202 |
case SIGTERM: |
|
203 |
imsgev_clear(&iev_pop3e); |
|
204 |
event_loopexit(NULL); |
|
205 |
break; |
|
206 |
case SIGCHLD: |
|
11
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
207 |
if (waitpid(WAIT_ANY, &status, WNOHANG) > 0) |
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
208 |
if ((WIFEXITED(status) && WEXITSTATUS(status) != 0) || |
12ebfb79038d
Make sure zombie processes are left over
James Turner <james@calminferno.net>
parents:
5
diff
changeset
|
209 |
WIFSIGNALED(status)) { |
0 | 210 |
logit(LOG_ERR, "Lost pop3 engine"); |
211 |
event_loopexit(NULL); |
|
212 |
} |
|
213 |
break; |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
static enum m_type |
|
218 |
m_type(const char *str) |
|
219 |
{ |
|
220 |
if (strcasecmp(str, "mbox") == 0) |
|
221 |
return M_MBOX; |
|
222 |
||
223 |
if (strcasecmp(str, "maildir") == 0) |
|
224 |
return M_MAILDIR; |
|
225 |
||
226 |
return (-1); |
|
227 |
} |
|
228 |
||
229 |
static void |
|
230 |
usage(void) |
|
231 |
{ |
|
232 |
extern char *__progname; |
|
233 |
||
234 |
fprintf(stderr, "usage: %s [-d] [-p path] [-t type]\n", __progname); |
|
235 |
exit(EXIT_FAILURE); |
|
236 |
} |
|
237 |