GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/npppd/npppd/control.c Lines: 0 157 0.0 %
Date: 2017-11-13 Branches: 0 94 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: control.c,v 1.7 2017/01/09 14:04:31 krw Exp $	*/
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/queue.h>
20
#include <sys/stat.h>
21
#include <sys/socket.h>
22
#include <sys/un.h>
23
#include <net/if.h>
24
#include <net/if_dl.h>
25
#include <netinet/in.h>
26
27
#include <errno.h>
28
#include <event.h>
29
#include <fcntl.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <time.h>
33
#include <unistd.h>
34
35
#include <imsg.h>
36
37
#include "log.h"
38
39
#define	CONTROL_BACKLOG	5
40
41
#include "npppd_local.h"
42
#include "npppd_ctl.h"
43
44
struct ctl_conn_list ctl_conns;
45
46
struct ctl_conn	*control_connbyfd(int);
47
void		 control_close(int, struct control_sock *);
48
void             control_accept (int, short, void *);
49
void             control_close (int, struct control_sock *);
50
void             control_dispatch_imsg (int, short, void *);
51
void             control_imsg_forward (struct imsg *);
52
53
int
54
control_init(struct control_sock *cs)
55
{
56
	struct sockaddr_un	 sun;
57
	int			 fd;
58
	mode_t			 old_umask, mode;
59
60
	if (cs->cs_name == NULL)
61
		return (0);
62
63
	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
64
		log_warn("control_init: socket");
65
		return (-1);
66
	}
67
68
	sun.sun_family = AF_UNIX;
69
	if (strlcpy(sun.sun_path, cs->cs_name,
70
	    sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
71
		log_warn("control_init: %s name too long", cs->cs_name);
72
		close(fd);
73
		return (-1);
74
	}
75
76
	if (unlink(cs->cs_name) == -1)
77
		if (errno != ENOENT) {
78
			log_warn("control_init: unlink %s", cs->cs_name);
79
			close(fd);
80
			return (-1);
81
		}
82
83
	if (cs->cs_restricted) {
84
		old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
85
		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
86
	} else {
87
		old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
88
		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
89
	}
90
91
	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
92
		log_warn("control_init: bind: %s", cs->cs_name);
93
		close(fd);
94
		(void)umask(old_umask);
95
		return (-1);
96
	}
97
	(void)umask(old_umask);
98
99
	if (chmod(cs->cs_name, mode) == -1) {
100
		log_warn("control_init: chmod");
101
		close(fd);
102
		(void)unlink(cs->cs_name);
103
		return (-1);
104
	}
105
	TAILQ_INIT(&ctl_conns);
106
107
	cs->cs_fd = fd;
108
109
	return (0);
110
}
111
112
int
113
control_listen(struct control_sock *cs)
114
{
115
	if (cs->cs_name == NULL)
116
		return (0);
117
118
	if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
119
		log_warn("control_listen: listen");
120
		return (-1);
121
	}
122
123
	event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
124
	    control_accept, cs);
125
	event_add(&cs->cs_ev, NULL);
126
	evtimer_set(&cs->cs_evt, control_accept, cs);
127
128
	return (0);
129
}
130
131
void
132
control_cleanup(struct control_sock *cs)
133
{
134
	struct ctl_conn *c, *nc;
135
136
	if (cs->cs_name == NULL)
137
		return;
138
139
	TAILQ_FOREACH_SAFE(c, &ctl_conns, entry, nc)
140
		control_close(c->iev.ibuf.fd, cs);
141
142
	event_del(&cs->cs_ev);
143
	event_del(&cs->cs_evt);
144
	(void)unlink(cs->cs_name);
145
}
146
147
#include <stdio.h>
148
/* ARGSUSED */
149
void
150
control_accept(int listenfd, short event, void *arg)
151
{
152
	struct control_sock	*cs = (struct control_sock *)arg;
153
	int			 connfd;
154
	socklen_t		 len;
155
	struct sockaddr_un	 sun;
156
	struct ctl_conn		*c;
157
158
	event_add(&cs->cs_ev, NULL);
159
	if ((event & EV_TIMEOUT))
160
		return;
161
162
	len = sizeof(sun);
163
	if ((connfd = accept4(listenfd,
164
	    (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) {
165
		/*
166
		 * Pause accept if we are out of file descriptors, or
167
		 * libevent will haunt us here too.
168
		 */
169
		if (errno == ENFILE || errno == EMFILE) {
170
			struct timeval evtpause = { 1, 0 };
171
172
			event_del(&cs->cs_ev);
173
			evtimer_add(&cs->cs_evt, &evtpause);
174
		} else if (errno != EWOULDBLOCK && errno != EINTR)
175
			log_warn("control_accept: accept");
176
		return;
177
	}
178
179
	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
180
		log_warn("control_accept");
181
		close(connfd);
182
		return;
183
	}
184
	if ((c->ctx = npppd_ctl_create(cs->cs_ctx)) == NULL) {
185
		free(c);
186
		log_warn("control_accept");
187
		close(connfd);
188
		return;
189
	}
190
191
	imsg_init(&c->iev.ibuf, connfd);
192
	c->iev.handler = control_dispatch_imsg;
193
	c->iev.events = EV_READ;
194
	c->iev.data = cs;
195
	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
196
	    c->iev.handler, cs);
197
	event_add(&c->iev.ev, NULL);
198
199
	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
200
}
201
202
struct ctl_conn *
203
control_connbyfd(int fd)
204
{
205
	struct ctl_conn	*c;
206
207
	TAILQ_FOREACH(c, &ctl_conns, entry) {
208
		if (c->iev.ibuf.fd == fd)
209
			break;
210
	}
211
212
	return (c);
213
}
214
215
void
216
control_close(int fd, struct control_sock *cs)
217
{
218
	struct ctl_conn	*c;
219
220
	if ((c = control_connbyfd(fd)) == NULL) {
221
		log_warn("control_close: fd %d: not found", fd);
222
		return;
223
	}
224
225
	msgbuf_clear(&c->iev.ibuf.w);
226
	TAILQ_REMOVE(&ctl_conns, c, entry);
227
228
	event_del(&c->iev.ev);
229
	close(c->iev.ibuf.fd);
230
231
	/* Some file descriptors are available again. */
232
	if (evtimer_pending(&cs->cs_evt, NULL)) {
233
		evtimer_del(&cs->cs_evt);
234
		event_add(&cs->cs_ev, NULL);
235
	}
236
	npppd_ctl_destroy(c->ctx);
237
238
	free(c);
239
}
240
241
/* ARGSUSED */
242
void
243
control_dispatch_imsg(int fd, short event, void *arg)
244
{
245
	struct control_sock	*cs = (struct control_sock *)arg;
246
	struct ctl_conn		*c;
247
	struct imsg		 imsg;
248
	int			 n, retval;
249
250
	if ((c = control_connbyfd(fd)) == NULL) {
251
		log_warn("control_dispatch_imsg: fd %d: not found", fd);
252
		return;
253
	}
254
255
256
	if (event & EV_WRITE) {
257
		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
258
			control_close(fd, cs);
259
			return;
260
		}
261
		if (!c->iev.ibuf.w.queued)
262
			npppd_ctl_imsg_compose(c->ctx, &c->iev.ibuf);
263
		imsg_event_add(&c->iev);
264
		if (!(event & EV_READ))
265
			return;
266
	}
267
	if (event & EV_READ) {
268
		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
269
		    n == 0) {
270
			control_close(fd, cs);
271
			return;
272
		}
273
	}
274
275
	for (;;) {
276
		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
277
			control_close(fd, cs);
278
			return;
279
		}
280
281
		if (n == 0)
282
			break;
283
284
		if (cs->cs_restricted || (c->flags & CTL_CONN_LOCKED)) {
285
			switch (imsg.hdr.type) {
286
			default:
287
				log_debug("control_dispatch_imsg: "
288
				    "client requested restricted command");
289
				imsg_free(&imsg);
290
				control_close(fd, cs);
291
				return;
292
			}
293
		}
294
295
		switch (imsg.hdr.type) {
296
		case IMSG_CTL_NOP:
297
			imsg_compose(&c->iev.ibuf, IMSG_CTL_OK, 0, 0, -1,
298
			    NULL, 0);
299
			break;
300
301
		case IMSG_CTL_WHO:
302
		case IMSG_CTL_MONITOR:
303
		case IMSG_CTL_WHO_AND_MONITOR:
304
			if (imsg.hdr.type == IMSG_CTL_WHO)
305
				retval = npppd_ctl_who(c->ctx);
306
			else if (imsg.hdr.type == IMSG_CTL_MONITOR)
307
				retval = npppd_ctl_monitor(c->ctx);
308
			else
309
				retval = npppd_ctl_who_and_monitor(c->ctx);
310
			imsg_compose(&c->iev.ibuf,
311
			    (retval == 0)? IMSG_CTL_OK : IMSG_CTL_FAIL, 0, 0,
312
			    -1, NULL, 0);
313
			break;
314
315
		case IMSG_CTL_DISCONNECT:
316
		    {
317
			struct npppd_disconnect_request  *req;
318
			struct npppd_disconnect_response  res;
319
320
			req = (struct npppd_disconnect_request *)imsg.data;
321
			retval = npppd_ctl_disconnect(c->ctx,
322
			    req->ppp_id, req->count);
323
			res.count = retval;
324
			imsg_compose(&c->iev.ibuf, IMSG_CTL_OK, 0, 0,
325
			    -1, &res, sizeof(res));
326
			break;
327
		    }
328
		default:
329
			imsg_compose(&c->iev.ibuf, IMSG_CTL_FAIL, 0, 0, -1,
330
			    NULL, 0);
331
			break;
332
		}
333
		imsg_free(&imsg);
334
	}
335
	if (!c->iev.ibuf.w.queued)
336
		npppd_ctl_imsg_compose(c->ctx, &c->iev.ibuf);
337
	imsg_event_add(&c->iev);
338
}
339
340
void
341
control_imsg_forward(struct imsg *imsg)
342
{
343
	struct ctl_conn *c;
344
345
	TAILQ_FOREACH(c, &ctl_conns, entry)
346
		if (c->flags & CTL_CONN_NOTIFY)
347
			imsg_compose(&c->iev.ibuf, imsg->hdr.type, 0,
348
			    imsg->hdr.pid, -1, imsg->data,
349
			    imsg->hdr.len - IMSG_HEADER_SIZE);
350
}