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/types.h> |
|
18 |
#include <sys/socket.h> |
|
19 |
#include <sys/uio.h> |
|
20 |
|
|
21 |
#include <err.h> |
|
22 |
#include <errno.h> |
|
23 |
#include <fcntl.h> |
|
24 |
#include <netdb.h> |
|
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include <syslog.h> |
|
29 |
|
|
30 |
#include "pop3d.h" |
|
31 |
|
|
32 |
int debug = 0; |
|
33 |
|
|
34 |
void |
|
35 |
set_nonblocking(int fd) |
|
36 |
{ |
|
37 |
int flags; |
|
38 |
|
|
39 |
if ((flags = fcntl(fd, F_GETFL, 0)) == -1) |
|
40 |
fatal("fcntl F_GETFL"); |
|
41 |
|
|
42 |
flags |= O_NONBLOCK; |
|
43 |
if ((flags = fcntl(fd, F_SETFL, flags)) == -1) |
|
44 |
fatal("fcntl F_SETFL"); |
|
45 |
} |
|
46 |
|
|
47 |
void |
|
48 |
log_init(int n_debug) |
|
49 |
{ |
|
50 |
extern char *__progname; |
|
51 |
|
|
52 |
debug = n_debug; |
|
53 |
if (!debug) |
|
54 |
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
|
55 |
|
|
56 |
tzset(); |
|
57 |
} |
|
58 |
|
|
59 |
void |
|
60 |
fatal(const char *emsg) |
|
61 |
{ |
|
62 |
if (errno) |
|
63 |
logit(LOG_CRIT, "fatal: %s: %s\n", emsg, strerror(errno)); |
|
64 |
else |
|
65 |
logit(LOG_CRIT, "fatal: %s\n", emsg); |
|
66 |
|
|
67 |
exit(EXIT_FAILURE); |
|
68 |
} |
|
69 |
|
|
70 |
void |
|
71 |
fatalx(const char *emsg) |
|
72 |
{ |
|
73 |
errno = 0; |
|
74 |
fatal(emsg); |
|
75 |
} |
|
76 |
|
|
77 |
void |
|
78 |
logit(int pri, const char *fmt, ...) |
|
79 |
{ |
|
80 |
va_list ap; |
|
81 |
|
|
82 |
va_start(ap, fmt); |
|
83 |
vlog(pri, fmt, ap); |
|
84 |
va_end(ap); |
|
85 |
} |
|
86 |
|
|
87 |
void |
|
88 |
vlog(int pri, const char *fmt, va_list ap) |
|
89 |
{ |
|
90 |
char *nfmt; |
|
91 |
|
|
92 |
if (debug) { |
|
93 |
/* best effort in out of mem situations */ |
|
94 |
if (asprintf(&nfmt, "%s\n", fmt) == -1) { |
|
95 |
vfprintf(stderr, fmt, ap); |
|
96 |
fprintf(stderr, "\n"); |
|
97 |
} else { |
|
98 |
vfprintf(stderr, nfmt, ap); |
|
99 |
free(nfmt); |
|
100 |
} |
|
101 |
fflush(stderr); |
|
102 |
} else |
|
103 |
vsyslog(pri, fmt, ap); |
|
104 |
} |
|
105 |
|
|
106 |
void * |
|
107 |
xcalloc(size_t nmemb, size_t size, const char *where) |
|
108 |
{ |
|
109 |
void *r; |
|
110 |
|
|
111 |
if ((r = calloc(nmemb, size)) == NULL) { |
|
112 |
logit(LOG_CRIT, "%s: calloc(%zu, %zu)", where, nmemb, size); |
|
113 |
err(1, "exiting"); |
|
114 |
} |
|
115 |
|
|
116 |
return (r); |
|
117 |
} |
|
118 |
|
|
119 |
int |
|
120 |
imsgev_xcompose(struct imsgev *iev, u_int16_t type, u_int32_t peerid, |
|
121 |
uint32_t pid, int fd, void *data, u_int16_t datalen, const char *where) |
|
122 |
{ |
|
123 |
int r; |
|
124 |
r = imsgev_compose(iev, type, peerid, pid, fd, data, datalen); |
|
125 |
if (r == -1) { |
|
126 |
logit(LOG_CRIT, "imsgev_xcompose: %s", where); |
|
127 |
errx(1, "maildrop exiting"); |
|
128 |
} |
|
129 |
|
|
130 |
return (r); |
|
131 |
} |
|
132 |
|
|
133 |
void |
|
134 |
iobuf_xfqueue(struct iobuf *io, const char *where, const char *fmt, ...) |
|
135 |
{ |
|
136 |
va_list ap; |
|
137 |
int len; |
|
138 |
|
|
139 |
va_start(ap, fmt); |
|
140 |
len = iobuf_vfqueue(io, fmt, ap); |
|
141 |
va_end(ap); |
|
142 |
|
|
143 |
if (len == -1) |
|
144 |
errx(1, "%s: iobuf_xfqueue(%p, %s, ...)", where, io, fmt); |
|
145 |
} |
|
146 |
|
|
147 |
void |
|
148 |
iobuf_xqueue(struct iobuf *io, const char *where, const void *data, size_t len) |
|
149 |
{ |
|
150 |
if (iobuf_queue(io, data, len) == -1) |
|
151 |
errx(1, "%s: iobuf_xqueue(%p, data, %zu)", where, io, len); |
|
152 |
} |
|
153 |
|
|
154 |
int |
|
155 |
get_index(struct session *s, const char *args, unsigned int *idx) |
|
156 |
{ |
|
157 |
const char *errstr; |
|
158 |
|
|
159 |
*idx = strtonum(args, 1, UINT_MAX, &errstr); |
|
160 |
if (errstr || *idx < 1 || *idx > s->nmsgs) { |
|
161 |
logit(LOG_INFO, "%zu: Invalid index", s->id); |
|
162 |
session_reply(s, "%s", "-ERR invalid index"); |
|
163 |
return (0); |
|
164 |
} |
|
165 |
|
|
166 |
*idx -= 1; /* make it zero based */ |
|
167 |
return (1); |
|
168 |
} |
|
169 |
|
|
170 |
void |
|
171 |
log_connect(uint32_t id, struct sockaddr_storage *s, socklen_t s_len) |
|
172 |
{ |
|
173 |
char hbuf[NI_MAXHOST]; |
|
174 |
int e; |
|
175 |
|
|
176 |
e = getnameinfo((struct sockaddr *)s, s_len, hbuf, sizeof(hbuf), |
|
177 |
NULL, 0, NI_NUMERICHOST); |
|
178 |
if (e) { |
|
179 |
logit(LOG_DEBUG, "getnameinfo: %s", gai_strerror(e)); |
|
180 |
logit(LOG_INFO, "new session with id %u", id); |
|
181 |
} else |
|
182 |
logit(LOG_INFO, "new session with id %u from %s", id, hbuf); |
|
183 |
} |