1/*	$OpenBSD: proc.h,v 1.24 2024/02/20 21:40:37 dv Exp $	*/
2
3/*
4 * Copyright (c) 2010-2015 Reyk Floeter <reyk@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/socket.h>
21#include <sys/uio.h>
22
23#include <event.h>
24#include <imsg.h>
25
26#ifndef _PROC_H
27#define _PROC_H
28
29enum {
30	IMSG_NONE,
31	IMSG_CTL_OK,
32	IMSG_CTL_FAIL,
33	IMSG_CTL_VERBOSE,
34	IMSG_CTL_END,
35	IMSG_CTL_RESET,
36	IMSG_CTL_PROCFD,
37	IMSG_PROC_MAX
38};
39
40/* imsg */
41struct imsgev {
42	struct imsgbuf		 ibuf;
43	void			(*handler)(int, short, void *);
44	struct event		 ev;
45	struct privsep_proc	*proc;
46	void			*data;
47	short			 events;
48};
49
50#define IMSG_SIZE_CHECK(imsg, p) do {					\
51	if (IMSG_DATA_SIZE(imsg) < sizeof(*p))				\
52		fatalx("bad length imsg received (%s)",	#p);		\
53} while (0)
54#define IMSG_DATA_SIZE(imsg)	((imsg)->hdr.len - IMSG_HEADER_SIZE)
55
56/* control socket */
57struct control_sock {
58	const char	*cs_name;
59	struct event	 cs_ev;
60	struct event	 cs_evt;
61	int		 cs_fd;
62	int		 cs_restricted;
63	void		*cs_env;
64	uid_t		 cs_uid;
65	gid_t		 cs_gid;
66
67	TAILQ_ENTRY(control_sock) cs_entry;
68};
69TAILQ_HEAD(control_socks, control_sock);
70
71struct ctl_conn {
72	TAILQ_ENTRY(ctl_conn)	 entry;
73	struct imsgev		 iev;
74	struct sockpeercred	 peercred;
75};
76TAILQ_HEAD(ctl_connlist, ctl_conn);
77
78/* privsep */
79enum privsep_procid {
80	PROC_PARENT	= 0,
81	PROC_CONTROL,
82	PROC_AGENTX,
83	PROC_VMM,
84	PROC_PRIV,
85	PROC_MAX,
86};
87extern enum privsep_procid privsep_process;
88
89#define CONFIG_RELOAD		0x00
90#define CONFIG_VMS		0x01
91#define CONFIG_SWITCHES		0x02
92#define CONFIG_ALL		0xff
93
94struct privsep_pipes {
95	int				*pp_pipes[PROC_MAX];
96};
97
98struct privsep {
99	struct privsep_pipes		*ps_pipes[PROC_MAX];
100	struct privsep_pipes		*ps_pp;
101
102	struct imsgev			*ps_ievs[PROC_MAX];
103	const char			*ps_title[PROC_MAX];
104	uint8_t				 ps_what[PROC_MAX];
105
106	struct passwd			*ps_pw;
107	int				 ps_noaction;
108
109	struct control_sock		 ps_csock;
110	struct control_socks		 ps_rcsocks;
111
112	unsigned int			 ps_instances[PROC_MAX];
113	unsigned int			 ps_instance;
114
115	/* Event and signal handlers */
116	struct event			 ps_evsigint;
117	struct event			 ps_evsigterm;
118	struct event			 ps_evsigchld;
119	struct event			 ps_evsighup;
120	struct event			 ps_evsigpipe;
121	struct event			 ps_evsigusr1;
122
123	void				*ps_env;
124};
125
126struct privsep_proc {
127	const char		*p_title;
128	enum privsep_procid	 p_id;
129	int			(*p_cb)(int, struct privsep_proc *,
130				    struct imsg *);
131	void			(*p_init)(struct privsep *,
132				    struct privsep_proc *);
133	void			(*p_shutdown)(void);
134	const char		*p_chroot;
135	struct passwd		*p_pw;
136	struct privsep		*p_ps;
137};
138
139struct privsep_fd {
140	enum privsep_procid		 pf_procid;
141	unsigned int			 pf_instance;
142};
143
144#if DEBUG
145#define DPRINTF		log_debug
146#else
147#define DPRINTF(x...)	do {} while(0)
148#endif
149
150#define PROC_PARENT_SOCK_FILENO	3
151#define PROC_MAX_INSTANCES	32
152
153/* proc.c */
154void	 proc_init(struct privsep *, struct privsep_proc *, unsigned int, int,
155	    int, char **, enum privsep_procid);
156void	 proc_kill(struct privsep *);
157void	 proc_connect(struct privsep *ps);
158void	 proc_dispatch(int, short event, void *);
159void	 proc_run(struct privsep *, struct privsep_proc *,
160	    struct privsep_proc *, unsigned int,
161	    void (*)(struct privsep *, struct privsep_proc *, void *), void *);
162void	 imsg_event_add(struct imsgev *);
163void	 imsg_event_add2(struct imsgev *, struct event_base *);
164int	 imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
165	    pid_t, int, void *, uint16_t);
166int	 imsg_compose_event2(struct imsgev *, uint16_t, uint32_t,
167    	    pid_t, int, void *, uint16_t, struct event_base *);
168int	 imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
169	    pid_t, int, const struct iovec *, int);
170int	 proc_compose_imsg(struct privsep *, enum privsep_procid, int,
171	    uint16_t, uint32_t, int, void *, uint16_t);
172int	 proc_compose(struct privsep *, enum privsep_procid,
173	    uint16_t, void *data, uint16_t);
174int	 proc_composev_imsg(struct privsep *, enum privsep_procid, int,
175	    uint16_t, uint32_t, int, const struct iovec *, int);
176int	 proc_composev(struct privsep *, enum privsep_procid,
177	    uint16_t, const struct iovec *, int);
178int	 proc_forward_imsg(struct privsep *, struct imsg *,
179	    enum privsep_procid, int);
180struct imsgbuf *
181	 proc_ibuf(struct privsep *, enum privsep_procid, int);
182struct imsgev *
183	 proc_iev(struct privsep *, enum privsep_procid, int);
184enum privsep_procid
185	 proc_getid(struct privsep_proc *, unsigned int, const char *);
186int	 proc_flush_imsg(struct privsep *, enum privsep_procid, int);
187
188/* control.c */
189void	 control(struct privsep *, struct privsep_proc *);
190int	 control_init(struct privsep *, struct control_sock *);
191int	 control_reset(struct control_sock *);
192int	 control_listen(struct control_sock *);
193
194/* log.c */
195void	log_init(int, int);
196void	log_procinit(const char *, ...);
197void	log_setverbose(int);
198int	log_getverbose(void);
199void	log_warn(const char *, ...)
200	    __attribute__((__format__ (printf, 1, 2)));
201void	log_warnx(const char *, ...)
202	    __attribute__((__format__ (printf, 1, 2)));
203void	log_info(const char *, ...)
204	    __attribute__((__format__ (printf, 1, 2)));
205void	log_debug(const char *, ...)
206	    __attribute__((__format__ (printf, 1, 2)));
207void	logit(int, const char *, ...)
208	    __attribute__((__format__ (printf, 2, 3)));
209void	vlog(int, const char *, va_list)
210	    __attribute__((__format__ (printf, 2, 0)));
211__dead void fatal(const char *, ...)
212	    __attribute__((__format__ (printf, 1, 2)));
213__dead void fatalx(const char *, ...)
214	    __attribute__((__format__ (printf, 1, 2)));
215
216#endif /* _PROC_H */
217