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