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 /* privsep */
34 enum privsep_procid {
35 PROC_PARENT,
36 PROC_PROXY,
37 PROC_MAX,
38 };
40 #define CONFIG_RELOAD 0x00
41 #define CONFIG_SOCKS 0x01
42 #define CONFIG_ALL 0xff
44 struct privsep_pipes {
45 int *pp_pipes[PROC_MAX];
46 };
48 struct privsep {
49 struct privsep_pipes *ps_pipes[PROC_MAX];
50 struct privsep_pipes *ps_pp;
52 struct imsgev *ps_ievs[PROC_MAX];
53 const char *ps_title[PROC_MAX];
54 uint8_t ps_what[PROC_MAX];
56 struct passwd *ps_pw;
57 int ps_noaction;
59 unsigned int ps_instances[PROC_MAX];
60 unsigned int ps_instance;
62 /* Event and signal handlers */
63 struct event ps_evsigint;
64 struct event ps_evsigterm;
65 struct event ps_evsigchld;
66 struct event ps_evsighup;
68 void *ps_env;
69 };
71 struct privsep_proc {
72 const char *p_title;
73 enum privsep_procid p_id;
74 int (*p_cb)(int, struct privsep_proc *,
75 struct imsg *);
76 void (*p_init)(struct privsep *,
77 struct privsep_proc *);
78 void (*p_shutdown)(void);
79 const char *p_chroot;
80 struct passwd *p_pw;
81 struct privsep *p_ps;
82 };
84 struct privsep_fd {
85 enum privsep_procid pf_procid;
86 unsigned int pf_instance;
87 };
89 /* proc.c */
90 void proc_init(struct privsep *, struct privsep_proc *, unsigned int,
91 int, int, char **, enum privsep_procid);
92 void proc_kill(struct privsep *);
93 void proc_connect(struct privsep *ps);
94 void proc_dispatch(int, short event, void *);
95 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
96 void proc_run(struct privsep *, struct privsep_proc *,
97 struct privsep_proc *, unsigned int,
98 void (*)(struct privsep *, struct privsep_proc *, void *), void *);
99 void imsg_event_add(struct imsgev *);
100 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
101 pid_t, int, void *, uint16_t);
102 int imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
103 pid_t, int, const struct iovec *, int);
104 int proc_compose_imsg(struct privsep *, enum privsep_procid, int,
105 uint16_t, uint32_t, int, void *, uint16_t);
106 int proc_compose(struct privsep *, enum privsep_procid,
107 uint16_t, void *data, uint16_t);
108 int proc_composev_imsg(struct privsep *, enum privsep_procid, int,
109 uint16_t, uint32_t, int, const struct iovec *, int);
110 int proc_composev(struct privsep *, enum privsep_procid,
111 uint16_t, const struct iovec *, int);
112 int proc_forward_imsg(struct privsep *, struct imsg *,
113 enum privsep_procid, int);
114 struct imsgbuf *
115 proc_ibuf(struct privsep *, enum privsep_procid, int);
116 struct imsgev *
117 proc_iev(struct privsep *, enum privsep_procid, int);
118 enum privsep_procid
119 proc_getid(struct privsep_proc *, unsigned int, const char *);
120 int proc_flush_imsg(struct privsep *, enum privsep_procid, int);