|
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/stat.h> |
|
21 |
|
22 #include <event.h> |
|
23 #include <fcntl.h> |
|
24 #include <pwd.h> |
|
25 #include <signal.h> |
|
26 #include <stdio.h> |
|
27 #include <stdlib.h> |
|
28 #include <string.h> |
|
29 #include <syslog.h> |
|
30 #include <unistd.h> |
|
31 |
|
32 #include "imsgev.h" |
|
33 #include "pop3d.h" |
|
34 |
|
35 static void session_imsgev(struct imsgev *, int, struct imsg *); |
|
36 static void update(struct imsgev *, struct imsg *, struct m_backend *); |
|
37 static void retr(struct imsgev *, struct imsg *, struct m_backend *); |
|
38 static void dele(struct imsgev *, struct imsg *, struct m_backend *); |
|
39 static void rset(struct imsgev *, struct imsg *, struct m_backend *); |
|
40 static void list(struct imsgev *, struct imsg *, struct m_backend *); |
|
41 static void list_all(struct imsgev *, struct imsg *, struct m_backend *, int); |
|
42 static void do_list(unsigned int, size_t *, char *, size_t); |
|
43 static void *do_list_all(int, size_t *); |
|
44 static struct m_backend *m_backend_lookup(enum m_type); |
|
45 static void sig_handler(int, short, void *); |
|
46 static void needfd(struct imsgev *); |
|
47 static size_t expand(char *, const char *, size_t, struct passwd *); |
|
48 |
|
49 static struct mdrop m; |
|
50 |
|
51 void |
|
52 maildrop_init(uint32_t session_id, int pair[2], struct passwd *pw, |
|
53 int type, const char *path) |
|
54 { |
|
55 struct imsgev iev_session; |
|
56 struct event ev_sigint, ev_sigterm; |
|
57 struct stats stats; |
|
58 struct m_backend *mb; |
|
59 char buf[MAXPATHLEN]; |
|
60 pid_t pid; |
|
61 mode_t old_mask; |
|
62 int fd, flags, res = -1; |
|
63 |
|
64 if (seteuid(pw->pw_uid) < 0) |
|
65 fatal("cannot lower privileges"); |
|
66 |
|
67 pid = fork(); |
|
68 if (seteuid(0) < 0) |
|
69 fatal("cannot restore privileges"); |
|
70 |
|
71 if (pid < 0) |
|
72 fatal("maildrop: fork"); |
|
73 |
|
74 if (pid > 0) |
|
75 return; |
|
76 |
|
77 if (seteuid(pw->pw_uid) < 0) |
|
78 fatal("cannot lower privileges"); |
|
79 |
|
80 close(pair[0]); |
|
81 setproctitle("maildrop"); |
|
82 if ((mb = m_backend_lookup(type)) == NULL) |
|
83 fatalx("maildrop: invalid backend"); |
|
84 |
|
85 if (expand(buf, path, sizeof(buf), pw) >= sizeof(buf)) |
|
86 fatalx("maildrop: path truncation"); |
|
87 |
|
88 flags = O_CREAT; |
|
89 if (type == M_MBOX) |
|
90 flags |= O_RDWR; |
|
91 else |
|
92 flags |= O_RDONLY; |
|
93 |
|
94 old_mask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); |
|
95 if ((fd = open(buf, flags)) == -1) |
|
96 logit(LOG_CRIT, "%zu: failed to open %s", session_id , buf); |
|
97 |
|
98 if (fd != -1) { |
|
99 m.fd = fd; |
|
100 res = mb->init(&m, &stats.nmsgs, &stats.sz); |
|
101 } |
|
102 |
|
103 umask(old_mask); |
|
104 if (seteuid(0) < 0) |
|
105 fatal("cannot restore privileges"); |
|
106 |
|
107 if (setgroups(1, &pw->pw_gid) || |
|
108 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || |
|
109 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) |
|
110 fatal("cannot drop privileges"); |
|
111 |
|
112 event_init(); |
|
113 signal_set(&ev_sigint, SIGINT, sig_handler, NULL); |
|
114 signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL); |
|
115 signal_add(&ev_sigint, NULL); |
|
116 signal_add(&ev_sigterm, NULL); |
|
117 imsgev_init(&iev_session, pair[1], mb, session_imsgev, needfd); |
|
118 |
|
119 if (res == 0) { |
|
120 imsgev_xcompose(&iev_session, IMSG_MAILDROP_INIT, session_id, |
|
121 0, -1, &stats, sizeof(struct stats), "maildrop_init"); |
|
122 } else { |
|
123 logit(LOG_CRIT, "%zu: maildrop init failed %s", |
|
124 session_id, buf); |
|
125 imsgev_xcompose(&iev_session, IMSG_MAILDROP_INIT, session_id, |
|
126 0, -1, NULL, 0, "maildrop_init"); |
|
127 } |
|
128 |
|
129 if (event_dispatch() < 0) |
|
130 fatal("event_dispatch"); |
|
131 |
|
132 logit(LOG_INFO, "maildrop process exiting"); |
|
133 _exit(0); |
|
134 } |
|
135 |
|
136 /* |
|
137 * Build dst by substituting '~' with user's home dir and '%u' with user name |
|
138 * in src. Return the length of string built. If return value >= dst_sz then |
|
139 * dst is truncated. |
|
140 */ |
|
141 static size_t |
|
142 expand(char *dst, const char *src, size_t dst_sz, struct passwd *pw) |
|
143 { |
|
144 size_t i = 0, r; |
|
145 int c; |
|
146 |
|
147 while ((c = *src++)) { |
|
148 if (i >= dst_sz) |
|
149 break; |
|
150 |
|
151 switch (c) { |
|
152 case '~': |
|
153 if ((r = strlcpy(&dst[i], pw->pw_dir, |
|
154 (dst_sz - i))) >= (dst_sz - i)) { |
|
155 i += r; |
|
156 goto end; |
|
157 } |
|
158 i += r; |
|
159 break; |
|
160 case '%': |
|
161 if (*src == 'u') { |
|
162 if ((r = strlcpy(&dst[i], pw->pw_name, |
|
163 (dst_sz - i))) >= (dst_sz - i)) { |
|
164 i += r; |
|
165 goto end; |
|
166 } |
|
167 i += r; |
|
168 src++; |
|
169 } else |
|
170 dst[i++] = c; |
|
171 break; |
|
172 default: |
|
173 dst[i++] = c; |
|
174 break; |
|
175 } |
|
176 } |
|
177 |
|
178 end: |
|
179 if (c) |
|
180 while ((c = *src++)) |
|
181 i++; |
|
182 |
|
183 dst[dst_sz - 1] = '\0'; |
|
184 return (i); |
|
185 } |
|
186 |
|
187 static void |
|
188 session_imsgev(struct imsgev *iev, int code, struct imsg *imsg) |
|
189 { |
|
190 struct m_backend *mb = iev->data; |
|
191 int uidl = 0; |
|
192 |
|
193 switch (code) { |
|
194 case IMSGEV_IMSG: |
|
195 switch (imsg->hdr.type) { |
|
196 case IMSG_MAILDROP_UPDATE: |
|
197 update(iev, imsg, mb); |
|
198 break; |
|
199 case IMSG_MAILDROP_RETR: |
|
200 retr(iev, imsg, mb); |
|
201 break; |
|
202 case IMSG_MAILDROP_DELE: |
|
203 dele(iev, imsg, mb); |
|
204 break; |
|
205 case IMSG_MAILDROP_RSET: |
|
206 rset(iev, imsg, mb); |
|
207 break; |
|
208 case IMSG_MAILDROP_LIST: |
|
209 list(iev, imsg, mb); |
|
210 break; |
|
211 case IMSG_MAILDROP_UIDLALL: |
|
212 uidl = 1; |
|
213 /* FALLTHROUGH */ |
|
214 case IMSG_MAILDROP_LISTALL: |
|
215 list_all(iev, imsg, mb, uidl); |
|
216 break; |
|
217 default: |
|
218 logit(LOG_DEBUG, "%s: unexpected imsg %u", |
|
219 __func__, imsg->hdr.type); |
|
220 break; |
|
221 } |
|
222 break; |
|
223 case IMSGEV_EREAD: |
|
224 case IMSGEV_EWRITE: |
|
225 case IMSGEV_EIMSG: |
|
226 fatal("maildrop: imsgev read/write error"); |
|
227 break; |
|
228 case IMSGEV_DONE: |
|
229 event_loopexit(NULL); |
|
230 break; |
|
231 } |
|
232 } |
|
233 |
|
234 static void |
|
235 update(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) |
|
236 { |
|
237 int res; |
|
238 uint32_t session_id = imsg->hdr.peerid; |
|
239 |
|
240 if ((res = mb->update(&m)) == 0) |
|
241 logit(LOG_INFO, "%zu: maildrop updated", session_id); |
|
242 else |
|
243 logit(LOG_CRIT, "%zu: maildrop updated failed", session_id); |
|
244 |
|
245 imsgev_xcompose(iev, IMSG_MAILDROP_UPDATE, session_id, 0, |
|
246 -1, &res, sizeof(res), "maildrop_update"); |
|
247 } |
|
248 |
|
249 static void |
|
250 retr(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) |
|
251 { |
|
252 struct retr_res res; |
|
253 struct retr_req *req = imsg->data; |
|
254 int fd; |
|
255 |
|
256 fd = mb->retr(&m, req->idx, &res.nlines, &res.offset); |
|
257 /* pass on top arguments */ |
|
258 res.top = req->top; |
|
259 res.ntop = req->ntop; |
|
260 imsgev_xcompose(iev, IMSG_MAILDROP_RETR, imsg->hdr.peerid, 0, |
|
261 fd, &res, sizeof(res), "maildrop_retr"); |
|
262 } |
|
263 |
|
264 static void |
|
265 dele(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) |
|
266 { |
|
267 unsigned int *idx = imsg->data; |
|
268 int res = 0; |
|
269 |
|
270 if (m.msgs_index[*idx]->flags & F_DELE) { |
|
271 res = -1; |
|
272 goto end; |
|
273 } |
|
274 |
|
275 m.msgs_index[*idx]->flags |= F_DELE; |
|
276 end: |
|
277 imsgev_xcompose(iev, IMSG_MAILDROP_DELE, imsg->hdr.peerid, 0, |
|
278 -1, &res, sizeof(res), "maildrop_dele"); |
|
279 } |
|
280 |
|
281 static void |
|
282 rset(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) |
|
283 { |
|
284 size_t i; |
|
285 |
|
286 for (i = 0; i < m.nmsgs; i++) |
|
287 m.msgs_index[i]->flags = 0; |
|
288 |
|
289 imsgev_xcompose(iev, IMSG_MAILDROP_RSET, imsg->hdr.peerid, 0, |
|
290 -1, NULL, 0, "maildrop_rset"); |
|
291 } |
|
292 |
|
293 static void |
|
294 list(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb) |
|
295 { |
|
296 struct list_req *req = imsg->data; |
|
297 struct list_res res; |
|
298 char hash[SHA1_DIGEST_STRING_LENGTH]; |
|
299 size_t sz; |
|
300 |
|
301 res.idx = req->idx; |
|
302 do_list(req->idx, &sz, hash, sizeof(hash)); |
|
303 res.uidl = req->uidl; |
|
304 if (res.uidl) |
|
305 strlcpy(res.u.hash, hash, sizeof(res.u.hash)); |
|
306 else |
|
307 res.u.sz = sz; |
|
308 |
|
309 imsgev_xcompose(iev, IMSG_MAILDROP_LIST, imsg->hdr.peerid, 0, |
|
310 -1, &res, sizeof(res), "maildrop_list"); |
|
311 |
|
312 } |
|
313 |
|
314 static void |
|
315 list_all(struct imsgev *iev, struct imsg *imsg, struct m_backend *mb, int uidl) |
|
316 { |
|
317 void *res; |
|
318 size_t sz; |
|
319 |
|
320 res = do_list_all(uidl, &sz); |
|
321 /* XXX watchout for sz > MAX_IMSGSIZE */ |
|
322 imsgev_xcompose(iev, |
|
323 (uidl) ? IMSG_MAILDROP_UIDLALL : IMSG_MAILDROP_LISTALL, |
|
324 imsg->hdr.peerid, 0, -1, res, sz, "maildrop_list"); |
|
325 } |
|
326 |
|
327 static void |
|
328 do_list(unsigned int idx, size_t *sz, char *hash, size_t hash_sz) |
|
329 { |
|
330 if (m.msgs_index[idx]->flags & F_DELE) { |
|
331 *sz = 0; |
|
332 strlcpy(hash, "", hash_sz); |
|
333 return; |
|
334 } |
|
335 |
|
336 *sz = m.msgs_index[idx]->sz; |
|
337 strlcpy(hash, m.msgs_index[idx]->hash, hash_sz); |
|
338 } |
|
339 |
|
340 static void * |
|
341 do_list_all(int uidl, size_t *sz) |
|
342 { |
|
343 size_t i, j, *nsz = NULL; |
|
344 char *nhash = NULL; |
|
345 |
|
346 if (uidl) { |
|
347 nhash = xcalloc(m.nmsgs, SHA1_DIGEST_STRING_LENGTH, "list_all"); |
|
348 } else |
|
349 nsz = xcalloc(m.nmsgs, sizeof(size_t), "list_all"); |
|
350 |
|
351 for (i = 0; i < m.nmsgs; i++) { |
|
352 |
|
353 if (uidl) { |
|
354 j = i * SHA1_DIGEST_STRING_LENGTH; |
|
355 if (m.msgs_index[i]->flags & F_DELE) |
|
356 nhash[j] = '\0'; |
|
357 else |
|
358 strlcpy(nhash + j, m.msgs_index[i]->hash, |
|
359 SHA1_DIGEST_STRING_LENGTH); |
|
360 } else { |
|
361 if (m.msgs_index[i]->flags & F_DELE) |
|
362 nsz[i] = 0; |
|
363 else |
|
364 nsz[i] = m.msgs_index[i]->sz; |
|
365 } |
|
366 } |
|
367 |
|
368 if (uidl) { |
|
369 *sz = m.nmsgs * SHA1_DIGEST_STRING_LENGTH; |
|
370 return (nhash); |
|
371 } else { |
|
372 *sz = m.nmsgs * sizeof(size_t); |
|
373 return (nsz); |
|
374 } |
|
375 } |
|
376 |
|
377 static void |
|
378 needfd(struct imsgev *iev) |
|
379 { |
|
380 fatalx("maildrop should never need an fd"); |
|
381 } |
|
382 |
|
383 static void |
|
384 sig_handler(int sig, short event, void *arg) |
|
385 { |
|
386 switch (sig) { |
|
387 case SIGINT: |
|
388 case SIGTERM: |
|
389 event_loopexit(NULL); |
|
390 } |
|
391 } |
|
392 |
|
393 extern struct m_backend m_backend_mbox; |
|
394 extern struct m_backend m_backend_maildir; |
|
395 |
|
396 static struct m_backend * |
|
397 m_backend_lookup(enum m_type type) |
|
398 { |
|
399 switch (type) { |
|
400 case M_MBOX: |
|
401 return &m_backend_mbox; |
|
402 break; |
|
403 case M_MAILDIR: |
|
404 return &m_backend_maildir; |
|
405 break; |
|
406 default: |
|
407 fatalx("m_backend_lookup: invalid m_type"); |
|
408 }; |
|
409 |
|
410 return (NULL); |
|
411 } |
|
412 |