1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* $OpenBSD: frontend.c,v 1.2 2018/09/05 17:32:56 eric Exp $ */
/*
* Copyright (c) 2017 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/tree.h>
#include <errno.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include "lpd.h"
#include "log.h"
#include "proc.h"
static void frontend_shutdown(void);
static void frontend_listen(struct listener *);
static void frontend_pause(struct listener *);
static void frontend_resume(struct listener *);
static void frontend_accept(int, short, void *);
static void frontend_dispatch_priv(struct imsgproc *, struct imsg *, void *);
static void frontend_dispatch_engine(struct imsgproc *, struct imsg *, void *);
struct conn {
SPLAY_ENTRY(conn) entry;
struct listener *listener;
uint32_t id;
};
static int conn_cmp(struct conn *, struct conn *);
SPLAY_HEAD(conntree, conn);
SPLAY_PROTOTYPE(conntree, conn, entry, conn_cmp);
static struct conntree conns;
static struct lpd_conf *tmpconf;
static int
conn_cmp(struct conn *a, struct conn *b)
{
if (a->id < b->id)
return (-1);
if (a->id > b->id)
return (1);
return (0);
}
SPLAY_GENERATE(conntree, conn, entry, conn_cmp);
void
frontend(int debug, int verbose)
{
struct passwd *pw;
/* Early initialisation. */
log_init(debug, LOG_LPR);
log_setverbose(verbose);
log_procinit("frontend");
setproctitle("frontend");
SPLAY_INIT(&conns);
lpr_init();
/* Drop priviledges. */
if ((pw = getpwnam(LPD_USER)) == NULL)
fatal("%s: getpwnam: %s", __func__, LPD_USER);
if (chroot(_PATH_VAREMPTY) == -1)
fatal("%s: chroot", __func__);
if (chdir("/") == -1)
fatal("%s: chdir", __func__);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("%s: cannot drop privileges", __func__);
if (pledge("stdio unix inet recvfd sendfd", NULL) == -1)
fatal("%s: pledge", __func__);
event_init();
signal(SIGPIPE, SIG_IGN);
/* Setup parent imsg socket. */
p_priv = proc_attach(PROC_PRIV, 3);
if (p_priv == NULL)
fatal("%s: proc_attach", __func__);
proc_setcallback(p_priv, frontend_dispatch_priv, NULL);
proc_enable(p_priv);
event_dispatch();
frontend_shutdown();
}
void
frontend_conn_closed(uint32_t connid)
{
struct listener *l;
struct conn key, *conn;
key.id = connid;
conn = SPLAY_FIND(conntree, &conns, &key);
if (conn == NULL)
fatalx("%s: %08x unknown connid", __func__, connid);
l = conn->listener;
if (log_getverbose() > LOGLEVEL_CONN)
log_debug("%08x close %s", conn->id,
log_fmt_proto(l->proto));
SPLAY_REMOVE(conntree, &conns, conn);
free(conn);
if (l->pause)
frontend_resume(l);
}
static void
frontend_shutdown()
{
struct listener *l;
TAILQ_FOREACH(l, &env->listeners, entry)
close(l->sock);
log_debug("exiting");
exit(0);
}
static void
frontend_listen(struct listener *l)
{
if (log_getverbose() > LOGLEVEL_CONN)
log_debug("listen %s %s", log_fmt_proto(l->proto),
log_fmt_sockaddr((struct sockaddr*)&l->ss));
if (listen(l->sock, 5) == -1)
fatal("%s: listen", __func__);
frontend_resume(l);
}
static void
frontend_pause(struct listener *l)
{
struct timeval tv;
event_del(&l->ev);
tv.tv_sec = 2;
tv.tv_usec = 0;
evtimer_set(&l->ev, frontend_accept, l);
evtimer_add(&l->ev, &tv);
l->pause = 1;
}
static void
frontend_resume(struct listener *l)
{
if (l->pause) {
evtimer_del(&l->ev);
l->pause = 0;
}
event_set(&l->ev, l->sock, EV_READ | EV_PERSIST, frontend_accept, l);
event_add(&l->ev, NULL);
}
static void
frontend_accept(int sock, short ev, void *arg)
{
struct listener *l = arg;
struct sockaddr_storage ss;
struct sockaddr *sa;
struct conn *conn;
socklen_t len;
if (l->pause) {
l->pause = 0;
frontend_resume(l);
return;
}
conn = calloc(1, sizeof(*conn));
if (conn == NULL)
log_warn("%s: calloc", __func__);
sa = (struct sockaddr *)&ss;
len = sizeof(ss);
sock = accept4(sock, sa, &len, SOCK_NONBLOCK);
if (sock == -1) {
if (errno == ENFILE || errno == EMFILE)
frontend_pause(l);
else if (errno != EWOULDBLOCK && errno != EINTR &&
errno != ECONNABORTED)
log_warn("%s: accept4", __func__);
free(conn);
return;
}
if (conn == NULL) {
close(sock);
return;
}
while (conn->id == 0 || SPLAY_FIND(conntree, &conns, conn))
conn->id = arc4random();
SPLAY_INSERT(conntree, &conns, conn);
conn->listener = l;
if (log_getverbose() > LOGLEVEL_CONN)
log_debug("%08x accept %s %s", conn->id,
log_fmt_proto(conn->listener->proto),
log_fmt_sockaddr((struct sockaddr*)&ss));
switch (l->proto) {
case PROTO_LPR:
lpr_conn(conn->id, l, sock, sa);
break;
default:
fatalx("%s: unexpected protocol %d", __func__, l->proto);
}
}
static void
frontend_dispatch_priv(struct imsgproc *proc, struct imsg *imsg, void *arg)
{
struct listener *l;
if (imsg == NULL) {
log_debug("%s: imsg connection lost", __func__);
event_loopexit(NULL);
return;
}
if (log_getverbose() > LOGLEVEL_IMSG)
log_imsg(proc, imsg);
switch (imsg->hdr.type) {
case IMSG_SOCK_ENGINE:
if (imsg->fd == -1)
fatalx("%s: engine socket not received", __func__);
m_end(proc);
p_engine = proc_attach(PROC_ENGINE, imsg->fd);
proc_setcallback(p_engine, frontend_dispatch_engine, NULL);
proc_enable(p_engine);
break;
case IMSG_CONF_START:
m_end(proc);
if ((tmpconf = calloc(1, sizeof(*tmpconf))) == NULL)
fatal("%s: calloc", __func__);
TAILQ_INIT(&tmpconf->listeners);
break;
case IMSG_CONF_LISTENER:
if (imsg->fd == -1)
fatalx("%s: listener socket not received", __func__);
if ((l = calloc(1, sizeof(*l))) == NULL)
fatal("%s: calloc", __func__);
m_get_int(proc, &l->proto);
m_get_sockaddr(proc, (struct sockaddr *)&l->ss);
m_end(proc);
l->sock = imsg->fd;
TAILQ_INSERT_TAIL(&tmpconf->listeners, l, entry);
break;
case IMSG_CONF_END:
m_end(proc);
TAILQ_FOREACH(l, &tmpconf->listeners, entry)
frontend_listen(l);
env = tmpconf;
break;
default:
fatalx("%s: unexpected imsg %s", __func__,
log_fmt_imsgtype(imsg->hdr.type));
}
}
static void
frontend_dispatch_engine(struct imsgproc *proc, struct imsg *imsg, void *arg)
{
if (imsg == NULL) {
log_debug("%s: imsg connection lost", __func__);
event_loopexit(NULL);
return;
}
if (log_getverbose() > LOGLEVEL_IMSG)
log_imsg(proc, imsg);
switch (imsg->hdr.type) {
case IMSG_GETADDRINFO:
case IMSG_GETADDRINFO_END:
case IMSG_GETNAMEINFO:
resolver_dispatch_result(proc, imsg);
break;
case IMSG_LPR_ALLOWEDHOST:
case IMSG_LPR_DISPLAYQ:
case IMSG_LPR_RECVJOB:
case IMSG_LPR_RECVJOB_CF:
case IMSG_LPR_RECVJOB_DF:
case IMSG_LPR_RMJOB:
lpr_dispatch_engine(proc, imsg);
break;
default:
fatalx("%s: unexpected imsg %s", __func__,
log_fmt_imsgtype(imsg->hdr.type));
}
}
|