Blame


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