Blob


1 /*
2 * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
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 */
17 /* imsg */
18 struct imsgev {
19 struct imsgbuf ibuf;
20 void (*handler)(int, short, void *);
21 struct event ev;
22 struct privsep_proc *proc;
23 void *data;
24 short events;
25 };
27 #define IMSG_SIZE_CHECK(imsg, p) do { \
28 if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
29 fatalx("bad length imsg received (%s)", #p); \
30 } while (0)
31 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
33 #define PROC_PARENT_SOCK_FILENO 3
35 /* privsep */
36 enum privsep_procid {
37 PROC_PARENT,
38 PROC_SERVER,
39 PROC_LOGGER,
40 PROC_MAX,
41 };
43 #define CONFIG_RELOAD 0x00
44 #define CONFIG_SOCKS 0x01
45 #define CONFIG_ALL 0xff
47 struct privsep_pipes {
48 int *pp_pipes[PROC_MAX];
49 };
51 struct privsep {
52 struct privsep_pipes *ps_pipes[PROC_MAX];
53 struct privsep_pipes *ps_pp;
55 struct imsgev *ps_ievs[PROC_MAX];
56 const char *ps_title[PROC_MAX];
57 uint8_t ps_what[PROC_MAX];
59 struct passwd *ps_pw;
60 int ps_noaction;
62 unsigned int ps_instances[PROC_MAX];
63 unsigned int ps_instance;
65 /* Event and signal handlers */
66 struct event ps_evsigint;
67 struct event ps_evsigterm;
68 struct event ps_evsigchld;
69 struct event ps_evsighup;
71 void *ps_env;
72 };
74 struct privsep_proc {
75 const char *p_title;
76 enum privsep_procid p_id;
77 int (*p_cb)(int, struct privsep_proc *,
78 struct imsg *);
79 void (*p_init)(struct privsep *,
80 struct privsep_proc *);
81 void (*p_shutdown)(void);
82 const char *p_chroot;
83 struct passwd *p_pw;
84 struct privsep *p_ps;
85 };
87 struct privsep_fd {
88 enum privsep_procid pf_procid;
89 unsigned int pf_instance;
90 };
92 /* proc.c */
93 void proc_init(struct privsep *, struct privsep_proc *, unsigned int,
94 int, int, char **, enum privsep_procid);
95 void proc_kill(struct privsep *);
96 void proc_connect(struct privsep *ps);
97 void proc_dispatch(int, short event, void *);
98 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
99 void proc_run(struct privsep *, struct privsep_proc *,
100 struct privsep_proc *, unsigned int,
101 void (*)(struct privsep *, struct privsep_proc *, void *), void *);
102 void imsg_event_add(struct imsgev *);
103 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
104 pid_t, int, void *, uint16_t);
105 int imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
106 pid_t, int, const struct iovec *, int);
107 int proc_compose_imsg(struct privsep *, enum privsep_procid, int,
108 uint16_t, uint32_t, int, void *, uint16_t);
109 int proc_compose(struct privsep *, enum privsep_procid,
110 uint16_t, void *data, uint16_t);
111 int proc_composev_imsg(struct privsep *, enum privsep_procid, int,
112 uint16_t, uint32_t, int, const struct iovec *, int);
113 int proc_composev(struct privsep *, enum privsep_procid,
114 uint16_t, const struct iovec *, int);
115 int proc_forward_imsg(struct privsep *, struct imsg *,
116 enum privsep_procid, int);
117 struct imsgbuf *
118 proc_ibuf(struct privsep *, enum privsep_procid, int);
119 struct imsgev *
120 proc_iev(struct privsep *, enum privsep_procid, int);
121 enum privsep_procid
122 proc_getid(struct privsep_proc *, unsigned int, const char *);
123 int proc_flush_imsg(struct privsep *, enum privsep_procid, int);