|
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/types.h> |
|
18 #include <sys/socket.h> |
|
19 #include <sys/time.h> |
|
20 #include <sys/tree.h> |
|
21 |
|
22 #include <err.h> |
|
23 #include <errno.h> |
|
24 #include <event.h> |
|
25 #include <netdb.h> |
|
26 #include <pwd.h> |
|
27 #include <signal.h> |
|
28 #include <stdlib.h> |
|
29 #include <string.h> |
|
30 #include <syslog.h> |
|
31 #include <unistd.h> |
|
32 |
|
33 #include "imsgev.h" |
|
34 #include "pop3d.h" |
|
35 #include "ssl.h" |
|
36 |
|
37 #define BACKLOG 5 |
|
38 |
|
39 static void auth_response(struct session *, int); |
|
40 static void pop3_accept(int, short, void *); |
|
41 static void pop3_listen(const char *); |
|
42 static void pop3_pause(int, short, void *); |
|
43 static void pop3d_imsgev(struct imsgev *, int, struct imsg *); |
|
44 static void needfd(struct imsgev *); |
|
45 static void sig_handler(int, short, void *); |
|
46 |
|
47 struct imsgev iev_pop3d; |
|
48 void *ssl_ctx; |
|
49 |
|
50 pid_t |
|
51 pop3_main(int pair[2], struct passwd *pw) |
|
52 { |
|
53 extern struct session_tree sessions; |
|
54 struct event ev_sigint, ev_sigterm; |
|
55 pid_t pid; |
|
56 |
|
57 pid = fork(); |
|
58 if (pid < 0) |
|
59 fatal("pop3e: fork"); |
|
60 |
|
61 if (pid > 0) |
|
62 return (pid); |
|
63 |
|
64 close(pair[0]); |
|
65 setproctitle("pop3 engine"); |
|
66 SPLAY_INIT(&sessions); |
|
67 event_init(); |
|
68 signal_set(&ev_sigint, SIGINT, sig_handler, NULL); |
|
69 signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL); |
|
70 signal_add(&ev_sigint, NULL); |
|
71 signal_add(&ev_sigterm, NULL); |
|
72 imsgev_init(&iev_pop3d, pair[1], NULL, pop3d_imsgev, needfd); |
|
73 pop3_listen("pop3"); |
|
74 |
|
75 ssl_init(); |
|
76 if ((ssl_ctx = ssl_setup()) == NULL) |
|
77 fatal("ssl_setup failed"); |
|
78 pop3_listen("pop3s"); |
|
79 |
|
80 if (chroot(pw->pw_dir) == -1 || chdir("/") == -1) |
|
81 fatal("chroot"); |
|
82 |
|
83 if (setgroups(1, &pw->pw_gid) || |
|
84 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || |
|
85 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) |
|
86 fatal("cannot drop privileges"); |
|
87 |
|
88 if (event_dispatch() < 0) |
|
89 fatal("event_dispatch"); |
|
90 |
|
91 logit(LOG_INFO, "pop3 engine exiting"); |
|
92 _exit(0); |
|
93 } |
|
94 |
|
95 static void |
|
96 pop3_listen(const char *port) |
|
97 { |
|
98 struct listener *l = NULL; |
|
99 struct addrinfo hints, *res, *res0; |
|
100 int error, opt, serrno, s; |
|
101 const char *cause = NULL; |
|
102 |
|
103 memset(&hints, 0, sizeof(hints)); |
|
104 hints.ai_family = PF_UNSPEC; |
|
105 hints.ai_socktype = SOCK_STREAM; |
|
106 hints.ai_flags = AI_PASSIVE; |
|
107 error = getaddrinfo(NULL, port, &hints, &res0); |
|
108 if (error) |
|
109 errx(1, "%s", gai_strerror(error)); |
|
110 |
|
111 for (res = res0; res != NULL; res = res->ai_next) { |
|
112 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
|
113 if (s == -1) { |
|
114 cause = "socket"; |
|
115 continue; |
|
116 } |
|
117 |
|
118 opt = 1; |
|
119 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
|
120 &opt, sizeof(opt)) == -1) |
|
121 fatal("listener setsockopt(SO_REUSEADDR)"); |
|
122 |
|
123 if (bind(s, res->ai_addr, res->ai_addrlen) == -1) { |
|
124 serrno = errno; |
|
125 cause = "bind"; |
|
126 close(s); |
|
127 errno = serrno; |
|
128 continue; |
|
129 } |
|
130 |
|
131 set_nonblocking(s); |
|
132 if (listen(s, BACKLOG) == -1) |
|
133 fatal("listen"); |
|
134 |
|
135 l = xcalloc(1, sizeof(*l), "pop3_listen"); |
|
136 l->sock = s; |
|
137 if (strcmp(port, "pop3s") == 0) |
|
138 l->flags |= POP3S; |
|
139 |
|
140 event_set(&l->ev, s, EV_READ|EV_PERSIST, pop3_accept, l); |
|
141 event_add(&l->ev, NULL); |
|
142 evtimer_set(&l->pause, pop3_pause, l); |
|
143 } |
|
144 |
|
145 if (l == NULL) |
|
146 errx(1, "%s", cause); |
|
147 |
|
148 freeaddrinfo(res0); |
|
149 } |
|
150 |
|
151 static void |
|
152 pop3_accept(int fd, short events, void *arg) |
|
153 { |
|
154 struct sockaddr_storage ss; |
|
155 struct listener *l = arg; |
|
156 struct timeval timeout = {1, 0}; |
|
157 socklen_t len; |
|
158 int s; |
|
159 |
|
160 len = sizeof(ss); |
|
161 s = accept(fd, (struct sockaddr *)&ss, &len); |
|
162 if (s == -1) { |
|
163 switch (errno) { |
|
164 case EINTR: |
|
165 case EWOULDBLOCK: |
|
166 case ECONNABORTED: |
|
167 return; |
|
168 case EMFILE: |
|
169 case ENFILE: |
|
170 event_del(&l->ev); |
|
171 evtimer_add(&l->pause, &timeout); |
|
172 return; |
|
173 default: |
|
174 fatalx("accept"); |
|
175 } |
|
176 } |
|
177 |
|
178 set_nonblocking(s); |
|
179 l->ss = ss; |
|
180 session_init(l, s); |
|
181 } |
|
182 |
|
183 static void |
|
184 pop3_pause(int fd, short events, void *arg) |
|
185 { |
|
186 struct listener *l = arg; |
|
187 |
|
188 event_add(&l->ev, NULL); |
|
189 } |
|
190 |
|
191 static void |
|
192 pop3d_imsgev(struct imsgev *iev, int code, struct imsg *imsg) |
|
193 { |
|
194 extern struct session_tree sessions; |
|
195 struct session key, *r; |
|
196 |
|
197 switch (code) { |
|
198 case IMSGEV_IMSG: |
|
199 key.id = imsg->hdr.peerid; |
|
200 r = SPLAY_FIND(session_tree, &sessions, &key); |
|
201 if (r == NULL) { |
|
202 logit(LOG_INFO, "%u: session not found", key.id); |
|
203 fatalx("pop3e: session lost"); |
|
204 } |
|
205 switch (imsg->hdr.type) { |
|
206 case IMSG_AUTH: |
|
207 auth_response(r, imsg->fd); |
|
208 break; |
|
209 default: |
|
210 logit(LOG_DEBUG, "%s: unexpected imsg %d", |
|
211 __func__, imsg->hdr.type); |
|
212 break; |
|
213 } |
|
214 break; |
|
215 case IMSGEV_EREAD: |
|
216 case IMSGEV_EWRITE: |
|
217 case IMSGEV_EIMSG: |
|
218 fatal("pop3e: imsgev read/write error"); |
|
219 break; |
|
220 case IMSGEV_DONE: |
|
221 event_loopexit(NULL); |
|
222 break; |
|
223 } |
|
224 } |
|
225 |
|
226 static void |
|
227 auth_response(struct session *s, int fd) |
|
228 { |
|
229 if (fd == -1) { |
|
230 session_reply(s, "%s", "-ERR auth failed"); |
|
231 io_set_write(&s->io); |
|
232 session_close(s, 1); |
|
233 return; |
|
234 } |
|
235 |
|
236 session_imsgev_init(s, fd); |
|
237 } |
|
238 |
|
239 static void |
|
240 needfd(struct imsgev *iev) |
|
241 { |
|
242 /* XXX can anything be done to handle fd exhaustion? */ |
|
243 fatalx("pop3e needs an fd"); |
|
244 } |
|
245 |
|
246 static void |
|
247 sig_handler(int sig, short event, void *arg) |
|
248 { |
|
249 switch (sig) { |
|
250 case SIGINT: |
|
251 case SIGTERM: |
|
252 event_loopexit(NULL); |
|
253 } |
|
254 } |
|
255 |