2024-09-25 18:45:13 -07:00
|
|
|
/* $OpenBSD: proc.h,v 1.25 2024/09/26 01:45:13 jsg Exp $ */
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/queue.h>
|
2021-06-16 09:55:02 -07:00
|
|
|
#include <sys/socket.h>
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
#include <event.h>
|
2021-06-16 09:55:02 -07:00
|
|
|
#include <imsg.h>
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
#ifndef _PROC_H
|
|
|
|
#define _PROC_H
|
|
|
|
|
|
|
|
enum {
|
2015-12-03 00:42:11 -08:00
|
|
|
IMSG_NONE,
|
2015-12-02 01:14:25 -08:00
|
|
|
IMSG_CTL_OK,
|
|
|
|
IMSG_CTL_FAIL,
|
|
|
|
IMSG_CTL_VERBOSE,
|
|
|
|
IMSG_CTL_END,
|
2015-12-02 14:19:11 -08:00
|
|
|
IMSG_CTL_RESET,
|
2016-09-29 15:42:04 -07:00
|
|
|
IMSG_CTL_PROCFD,
|
2015-12-02 01:14:25 -08:00
|
|
|
IMSG_PROC_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
/* imsg */
|
|
|
|
struct imsgev {
|
|
|
|
struct imsgbuf ibuf;
|
|
|
|
void (*handler)(int, short, void *);
|
|
|
|
struct event ev;
|
|
|
|
struct privsep_proc *proc;
|
|
|
|
void *data;
|
|
|
|
short events;
|
|
|
|
};
|
|
|
|
|
2016-10-05 10:30:13 -07:00
|
|
|
#define IMSG_SIZE_CHECK(imsg, p) do { \
|
|
|
|
if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
|
|
|
|
fatalx("bad length imsg received (%s)", #p); \
|
2015-12-02 01:14:25 -08:00
|
|
|
} while (0)
|
|
|
|
#define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
|
|
|
|
|
|
|
|
/* control socket */
|
|
|
|
struct control_sock {
|
|
|
|
const char *cs_name;
|
|
|
|
struct event cs_ev;
|
|
|
|
struct event cs_evt;
|
|
|
|
int cs_fd;
|
|
|
|
int cs_restricted;
|
|
|
|
void *cs_env;
|
2018-06-26 03:00:08 -07:00
|
|
|
uid_t cs_uid;
|
|
|
|
gid_t cs_gid;
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
TAILQ_ENTRY(control_sock) cs_entry;
|
|
|
|
};
|
|
|
|
TAILQ_HEAD(control_socks, control_sock);
|
|
|
|
|
|
|
|
struct ctl_conn {
|
|
|
|
TAILQ_ENTRY(ctl_conn) entry;
|
|
|
|
struct imsgev iev;
|
2015-12-03 05:08:44 -08:00
|
|
|
struct sockpeercred peercred;
|
2015-12-02 01:14:25 -08:00
|
|
|
};
|
|
|
|
TAILQ_HEAD(ctl_connlist, ctl_conn);
|
|
|
|
|
|
|
|
/* privsep */
|
|
|
|
enum privsep_procid {
|
|
|
|
PROC_PARENT = 0,
|
|
|
|
PROC_CONTROL,
|
2022-09-13 03:28:19 -07:00
|
|
|
PROC_AGENTX,
|
2015-12-02 14:19:11 -08:00
|
|
|
PROC_VMM,
|
2016-10-04 10:17:30 -07:00
|
|
|
PROC_PRIV,
|
2015-12-02 01:14:25 -08:00
|
|
|
PROC_MAX,
|
2021-01-26 23:21:52 -08:00
|
|
|
};
|
|
|
|
extern enum privsep_procid privsep_process;
|
2015-12-02 01:14:25 -08:00
|
|
|
|
2015-12-02 14:19:11 -08:00
|
|
|
#define CONFIG_RELOAD 0x00
|
|
|
|
#define CONFIG_VMS 0x01
|
2016-10-05 10:30:13 -07:00
|
|
|
#define CONFIG_SWITCHES 0x02
|
2015-12-02 14:19:11 -08:00
|
|
|
#define CONFIG_ALL 0xff
|
|
|
|
|
2015-12-02 01:14:25 -08:00
|
|
|
struct privsep_pipes {
|
|
|
|
int *pp_pipes[PROC_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct privsep {
|
|
|
|
struct privsep_pipes *ps_pipes[PROC_MAX];
|
|
|
|
struct privsep_pipes *ps_pp;
|
|
|
|
|
|
|
|
struct imsgev *ps_ievs[PROC_MAX];
|
|
|
|
const char *ps_title[PROC_MAX];
|
2015-12-03 00:42:11 -08:00
|
|
|
uint8_t ps_what[PROC_MAX];
|
2015-12-02 14:19:11 -08:00
|
|
|
|
2015-12-02 01:14:25 -08:00
|
|
|
struct passwd *ps_pw;
|
|
|
|
int ps_noaction;
|
|
|
|
|
|
|
|
struct control_sock ps_csock;
|
|
|
|
struct control_socks ps_rcsocks;
|
|
|
|
|
2015-12-03 00:42:11 -08:00
|
|
|
unsigned int ps_instances[PROC_MAX];
|
|
|
|
unsigned int ps_instance;
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
/* Event and signal handlers */
|
|
|
|
struct event ps_evsigint;
|
|
|
|
struct event ps_evsigterm;
|
|
|
|
struct event ps_evsigchld;
|
|
|
|
struct event ps_evsighup;
|
|
|
|
struct event ps_evsigpipe;
|
|
|
|
struct event ps_evsigusr1;
|
|
|
|
|
|
|
|
void *ps_env;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct privsep_proc {
|
|
|
|
const char *p_title;
|
|
|
|
enum privsep_procid p_id;
|
|
|
|
int (*p_cb)(int, struct privsep_proc *,
|
|
|
|
struct imsg *);
|
2016-09-29 15:42:04 -07:00
|
|
|
void (*p_init)(struct privsep *,
|
2015-12-02 01:14:25 -08:00
|
|
|
struct privsep_proc *);
|
2016-10-04 10:17:30 -07:00
|
|
|
void (*p_shutdown)(void);
|
2015-12-02 01:14:25 -08:00
|
|
|
const char *p_chroot;
|
2016-10-04 10:17:30 -07:00
|
|
|
struct passwd *p_pw;
|
2015-12-02 01:14:25 -08:00
|
|
|
struct privsep *p_ps;
|
|
|
|
};
|
|
|
|
|
2016-09-29 15:42:04 -07:00
|
|
|
struct privsep_fd {
|
|
|
|
enum privsep_procid pf_procid;
|
|
|
|
unsigned int pf_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#define DPRINTF log_debug
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x...) do {} while(0)
|
|
|
|
#endif
|
|
|
|
|
2017-03-26 17:28:04 -07:00
|
|
|
#define PROC_PARENT_SOCK_FILENO 3
|
|
|
|
#define PROC_MAX_INSTANCES 32
|
2016-09-29 15:42:04 -07:00
|
|
|
|
2015-12-02 01:14:25 -08:00
|
|
|
/* proc.c */
|
2018-09-10 03:36:01 -07:00
|
|
|
void proc_init(struct privsep *, struct privsep_proc *, unsigned int, int,
|
2016-09-29 15:42:04 -07:00
|
|
|
int, char **, enum privsep_procid);
|
2015-12-02 01:14:25 -08:00
|
|
|
void proc_kill(struct privsep *);
|
2016-09-29 15:42:04 -07:00
|
|
|
void proc_connect(struct privsep *ps);
|
2015-12-02 01:14:25 -08:00
|
|
|
void proc_dispatch(int, short event, void *);
|
2016-09-29 15:42:04 -07:00
|
|
|
void proc_run(struct privsep *, struct privsep_proc *,
|
2015-12-02 01:14:25 -08:00
|
|
|
struct privsep_proc *, unsigned int,
|
|
|
|
void (*)(struct privsep *, struct privsep_proc *, void *), void *);
|
|
|
|
void imsg_event_add(struct imsgev *);
|
2024-02-20 13:40:37 -08:00
|
|
|
void imsg_event_add2(struct imsgev *, struct event_base *);
|
2015-12-02 01:14:25 -08:00
|
|
|
int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
|
|
|
|
pid_t, int, void *, uint16_t);
|
2024-02-20 13:40:37 -08:00
|
|
|
int imsg_compose_event2(struct imsgev *, uint16_t, uint32_t,
|
|
|
|
pid_t, int, void *, uint16_t, struct event_base *);
|
2015-12-02 01:14:25 -08:00
|
|
|
int imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
|
|
|
|
pid_t, int, const struct iovec *, int);
|
|
|
|
int proc_compose_imsg(struct privsep *, enum privsep_procid, int,
|
2015-12-02 01:39:41 -08:00
|
|
|
uint16_t, uint32_t, int, void *, uint16_t);
|
2015-12-02 05:43:36 -08:00
|
|
|
int proc_compose(struct privsep *, enum privsep_procid,
|
|
|
|
uint16_t, void *data, uint16_t);
|
2015-12-02 01:14:25 -08:00
|
|
|
int proc_composev_imsg(struct privsep *, enum privsep_procid, int,
|
2015-12-02 01:39:41 -08:00
|
|
|
uint16_t, uint32_t, int, const struct iovec *, int);
|
2015-12-02 05:43:36 -08:00
|
|
|
int proc_composev(struct privsep *, enum privsep_procid,
|
|
|
|
uint16_t, const struct iovec *, int);
|
2015-12-02 01:14:25 -08:00
|
|
|
int proc_forward_imsg(struct privsep *, struct imsg *,
|
|
|
|
enum privsep_procid, int);
|
|
|
|
struct imsgbuf *
|
|
|
|
proc_ibuf(struct privsep *, enum privsep_procid, int);
|
|
|
|
struct imsgev *
|
|
|
|
proc_iev(struct privsep *, enum privsep_procid, int);
|
2016-09-29 15:42:04 -07:00
|
|
|
enum privsep_procid
|
|
|
|
proc_getid(struct privsep_proc *, unsigned int, const char *);
|
2016-11-16 07:32:42 -08:00
|
|
|
int proc_flush_imsg(struct privsep *, enum privsep_procid, int);
|
2015-12-02 01:14:25 -08:00
|
|
|
|
|
|
|
/* control.c */
|
2016-09-29 15:42:04 -07:00
|
|
|
void control(struct privsep *, struct privsep_proc *);
|
2015-12-02 01:14:25 -08:00
|
|
|
int control_init(struct privsep *, struct control_sock *);
|
2018-06-26 03:00:08 -07:00
|
|
|
int control_reset(struct control_sock *);
|
2015-12-02 01:14:25 -08:00
|
|
|
int control_listen(struct control_sock *);
|
|
|
|
|
|
|
|
/* log.c */
|
|
|
|
void log_init(int, int);
|
2023-09-25 18:53:54 -07:00
|
|
|
void log_procinit(const char *, ...);
|
2017-01-09 06:49:21 -08:00
|
|
|
void log_setverbose(int);
|
|
|
|
int log_getverbose(void);
|
2015-12-02 01:14:25 -08:00
|
|
|
void log_warn(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
void log_warnx(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
void log_info(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
void log_debug(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
void logit(int, const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 2, 3)));
|
|
|
|
void vlog(int, const char *, va_list)
|
|
|
|
__attribute__((__format__ (printf, 2, 0)));
|
|
|
|
__dead void fatal(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
__dead void fatalx(const char *, ...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
|
|
|
|
|
|
|
#endif /* _PROC_H */
|