Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
3 8a35f56c 2022-07-16 thomas *
4 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
5 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
6 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
7 8a35f56c 2022-07-16 thomas *
8 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 8a35f56c 2022-07-16 thomas */
16 8a35f56c 2022-07-16 thomas
17 8a35f56c 2022-07-16 thomas enum {
18 8a35f56c 2022-07-16 thomas IMSG_NONE,
19 8a35f56c 2022-07-16 thomas IMSG_CTL_OK,
20 8a35f56c 2022-07-16 thomas IMSG_CTL_FAIL,
21 8a35f56c 2022-07-16 thomas IMSG_CTL_VERBOSE,
22 8a35f56c 2022-07-16 thomas IMSG_CTL_NOTIFY,
23 8a35f56c 2022-07-16 thomas IMSG_CTL_RESET,
24 8a35f56c 2022-07-16 thomas IMSG_CTL_PROCFD,
25 8a35f56c 2022-07-16 thomas IMSG_PROC_MAX
26 8a35f56c 2022-07-16 thomas };
27 8a35f56c 2022-07-16 thomas
28 8a35f56c 2022-07-16 thomas /* imsg */
29 8a35f56c 2022-07-16 thomas struct imsgev {
30 8a35f56c 2022-07-16 thomas struct imsgbuf ibuf;
31 8a35f56c 2022-07-16 thomas void (*handler)(int, short, void *);
32 8a35f56c 2022-07-16 thomas struct event ev;
33 8a35f56c 2022-07-16 thomas struct privsep_proc *proc;
34 8a35f56c 2022-07-16 thomas void *data;
35 8a35f56c 2022-07-16 thomas short events;
36 8a35f56c 2022-07-16 thomas };
37 8a35f56c 2022-07-16 thomas
38 8a35f56c 2022-07-16 thomas #define IMSG_SIZE_CHECK(imsg, p) do { \
39 8a35f56c 2022-07-16 thomas if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
40 8a35f56c 2022-07-16 thomas fatalx("bad length imsg received (%s)", #p); \
41 8a35f56c 2022-07-16 thomas } while (0)
42 8a35f56c 2022-07-16 thomas #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
43 8a35f56c 2022-07-16 thomas
44 8a35f56c 2022-07-16 thomas struct ctl_conn {
45 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(ctl_conn) entry;
46 8a35f56c 2022-07-16 thomas uint8_t flags;
47 8a35f56c 2022-07-16 thomas unsigned int waiting;
48 8a35f56c 2022-07-16 thomas #define CTL_CONN_NOTIFY 0x01
49 8a35f56c 2022-07-16 thomas struct imsgev iev;
50 8a35f56c 2022-07-16 thomas uid_t uid;
51 8a35f56c 2022-07-16 thomas };
52 8a35f56c 2022-07-16 thomas TAILQ_HEAD(ctl_connlist, ctl_conn);
53 8a35f56c 2022-07-16 thomas extern struct ctl_connlist ctl_conns;
54 8a35f56c 2022-07-16 thomas
55 8a35f56c 2022-07-16 thomas /* privsep */
56 8a35f56c 2022-07-16 thomas enum privsep_procid {
57 8a35f56c 2022-07-16 thomas PROC_GOTWEBD = 0,
58 8a35f56c 2022-07-16 thomas PROC_SOCKS,
59 8a35f56c 2022-07-16 thomas PROC_MAX,
60 8a35f56c 2022-07-16 thomas };
61 8a35f56c 2022-07-16 thomas extern enum privsep_procid privsep_process;
62 8a35f56c 2022-07-16 thomas
63 8a35f56c 2022-07-16 thomas #define CONFIG_RELOAD 0x00
64 8a35f56c 2022-07-16 thomas #define CONFIG_SOCKS 0x01
65 8a35f56c 2022-07-16 thomas #define CONFIG_ALL 0xff
66 8a35f56c 2022-07-16 thomas
67 8a35f56c 2022-07-16 thomas struct privsep_pipes {
68 8a35f56c 2022-07-16 thomas int *pp_pipes[PROC_MAX];
69 8a35f56c 2022-07-16 thomas };
70 8a35f56c 2022-07-16 thomas
71 8a35f56c 2022-07-16 thomas struct privsep {
72 8a35f56c 2022-07-16 thomas struct privsep_pipes *ps_pipes[PROC_MAX];
73 8a35f56c 2022-07-16 thomas struct privsep_pipes *ps_pp;
74 8a35f56c 2022-07-16 thomas
75 8a35f56c 2022-07-16 thomas struct imsgev *ps_ievs[PROC_MAX];
76 8a35f56c 2022-07-16 thomas const char *ps_title[PROC_MAX];
77 8a35f56c 2022-07-16 thomas uint8_t ps_what[PROC_MAX];
78 8a35f56c 2022-07-16 thomas
79 8a35f56c 2022-07-16 thomas struct passwd *ps_pw;
80 8a35f56c 2022-07-16 thomas int ps_noaction;
81 8a35f56c 2022-07-16 thomas
82 8a35f56c 2022-07-16 thomas unsigned int ps_instances[PROC_MAX];
83 8a35f56c 2022-07-16 thomas unsigned int ps_instance;
84 8a35f56c 2022-07-16 thomas
85 8a35f56c 2022-07-16 thomas /* Event and signal handlers */
86 8a35f56c 2022-07-16 thomas struct event ps_evsigint;
87 8a35f56c 2022-07-16 thomas struct event ps_evsigterm;
88 8a35f56c 2022-07-16 thomas struct event ps_evsigchld;
89 8a35f56c 2022-07-16 thomas struct event ps_evsighup;
90 8a35f56c 2022-07-16 thomas struct event ps_evsigpipe;
91 8a35f56c 2022-07-16 thomas struct event ps_evsigusr1;
92 8a35f56c 2022-07-16 thomas
93 8a35f56c 2022-07-16 thomas void *ps_env;
94 8a35f56c 2022-07-16 thomas };
95 8a35f56c 2022-07-16 thomas
96 8a35f56c 2022-07-16 thomas struct privsep_proc {
97 8a35f56c 2022-07-16 thomas const char *p_title;
98 8a35f56c 2022-07-16 thomas enum privsep_procid p_id;
99 8a35f56c 2022-07-16 thomas int (*p_cb)(int, struct privsep_proc *,
100 8a35f56c 2022-07-16 thomas struct imsg *);
101 8a35f56c 2022-07-16 thomas void (*p_init)(struct privsep *,
102 8a35f56c 2022-07-16 thomas struct privsep_proc *);
103 8a35f56c 2022-07-16 thomas void (*p_shutdown)(void);
104 8a35f56c 2022-07-16 thomas const char *p_chroot;
105 8a35f56c 2022-07-16 thomas struct passwd *p_pw;
106 8a35f56c 2022-07-16 thomas struct privsep *p_ps;
107 8a35f56c 2022-07-16 thomas };
108 8a35f56c 2022-07-16 thomas
109 8a35f56c 2022-07-16 thomas struct privsep_fd {
110 8a35f56c 2022-07-16 thomas enum privsep_procid pf_procid;
111 8a35f56c 2022-07-16 thomas unsigned int pf_instance;
112 8a35f56c 2022-07-16 thomas };
113 8a35f56c 2022-07-16 thomas
114 8a35f56c 2022-07-16 thomas #if DEBUG
115 8a35f56c 2022-07-16 thomas #define DPRINTF log_debug
116 8a35f56c 2022-07-16 thomas #else
117 8a35f56c 2022-07-16 thomas #define DPRINTF(x...) do {} while(0)
118 8a35f56c 2022-07-16 thomas #endif
119 8a35f56c 2022-07-16 thomas
120 8a35f56c 2022-07-16 thomas #define PROC_GOTWEBD_SOCK_FILENO 3
121 8a35f56c 2022-07-16 thomas #define PROC_MAX_INSTANCES 32
122 8a35f56c 2022-07-16 thomas
123 8a35f56c 2022-07-16 thomas /* proc.c */
124 8a35f56c 2022-07-16 thomas void proc_init(struct privsep *, struct privsep_proc *, unsigned int,
125 8a35f56c 2022-07-16 thomas int, char **, enum privsep_procid);
126 8a35f56c 2022-07-16 thomas void proc_kill(struct privsep *);
127 8a35f56c 2022-07-16 thomas void proc_connect(struct privsep *ps);
128 8a35f56c 2022-07-16 thomas void proc_dispatch(int, short event, void *);
129 8a35f56c 2022-07-16 thomas void proc_range(struct privsep *, enum privsep_procid, int *, int *);
130 8a35f56c 2022-07-16 thomas void proc_run(struct privsep *, struct privsep_proc *,
131 8a35f56c 2022-07-16 thomas struct privsep_proc *, unsigned int,
132 8a35f56c 2022-07-16 thomas void (*)(struct privsep *, struct privsep_proc *, void *), void *);
133 8a35f56c 2022-07-16 thomas void imsg_event_add(struct imsgev *);
134 8a35f56c 2022-07-16 thomas int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
135 8a35f56c 2022-07-16 thomas pid_t, int, void *, uint16_t);
136 8a35f56c 2022-07-16 thomas int imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
137 8a35f56c 2022-07-16 thomas pid_t, int, const struct iovec *, int);
138 8a35f56c 2022-07-16 thomas int proc_compose_imsg(struct privsep *, enum privsep_procid, int,
139 8a35f56c 2022-07-16 thomas uint16_t, uint32_t, int, void *, uint16_t);
140 8a35f56c 2022-07-16 thomas int proc_compose(struct privsep *, enum privsep_procid,
141 8a35f56c 2022-07-16 thomas uint16_t, void *data, uint16_t);
142 8a35f56c 2022-07-16 thomas int proc_composev_imsg(struct privsep *, enum privsep_procid, int,
143 8a35f56c 2022-07-16 thomas uint16_t, uint32_t, int, const struct iovec *, int);
144 8a35f56c 2022-07-16 thomas int proc_composev(struct privsep *, enum privsep_procid,
145 8a35f56c 2022-07-16 thomas uint16_t, const struct iovec *, int);
146 8a35f56c 2022-07-16 thomas int proc_forward_imsg(struct privsep *, struct imsg *,
147 8a35f56c 2022-07-16 thomas enum privsep_procid, int);
148 8a35f56c 2022-07-16 thomas struct imsgbuf *
149 8a35f56c 2022-07-16 thomas proc_ibuf(struct privsep *, enum privsep_procid, int);
150 8a35f56c 2022-07-16 thomas struct imsgev *
151 8a35f56c 2022-07-16 thomas proc_iev(struct privsep *, enum privsep_procid, int);
152 8a35f56c 2022-07-16 thomas enum privsep_procid
153 8a35f56c 2022-07-16 thomas proc_getid(struct privsep_proc *, unsigned int, const char *);
154 8a35f56c 2022-07-16 thomas int proc_flush_imsg(struct privsep *, enum privsep_procid, int);
155 8a35f56c 2022-07-16 thomas
156 8a35f56c 2022-07-16 thomas /* log.c */
157 8a35f56c 2022-07-16 thomas void log_init(int, int);
158 8a35f56c 2022-07-16 thomas void log_procinit(const char *);
159 8a35f56c 2022-07-16 thomas void log_setverbose(int);
160 8a35f56c 2022-07-16 thomas int log_getverbose(void);
161 8a35f56c 2022-07-16 thomas void log_warn(const char *, ...)
162 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
163 8a35f56c 2022-07-16 thomas void log_warnx(const char *, ...)
164 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
165 8a35f56c 2022-07-16 thomas void log_info(const char *, ...)
166 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
167 8a35f56c 2022-07-16 thomas void log_debug(const char *, ...)
168 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
169 8a35f56c 2022-07-16 thomas void logit(int, const char *, ...)
170 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 2, 3)));
171 8a35f56c 2022-07-16 thomas void vlog(int, const char *, va_list)
172 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 2, 0)));
173 8a35f56c 2022-07-16 thomas __dead void fatal(const char *, ...)
174 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
175 8a35f56c 2022-07-16 thomas __dead void fatalx(const char *, ...)
176 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));