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 ff36aeea 2022-07-16 thomas #include "got_compat.h"
18 ff36aeea 2022-07-16 thomas
19 8a35f56c 2022-07-16 thomas enum {
20 8a35f56c 2022-07-16 thomas IMSG_NONE,
21 8a35f56c 2022-07-16 thomas IMSG_CTL_OK,
22 8a35f56c 2022-07-16 thomas IMSG_CTL_FAIL,
23 8a35f56c 2022-07-16 thomas IMSG_CTL_VERBOSE,
24 8a35f56c 2022-07-16 thomas IMSG_CTL_NOTIFY,
25 8a35f56c 2022-07-16 thomas IMSG_CTL_RESET,
26 8a35f56c 2022-07-16 thomas IMSG_CTL_PROCFD,
27 8a35f56c 2022-07-16 thomas IMSG_PROC_MAX
28 8a35f56c 2022-07-16 thomas };
29 8a35f56c 2022-07-16 thomas
30 8a35f56c 2022-07-16 thomas /* imsg */
31 8a35f56c 2022-07-16 thomas struct imsgev {
32 8a35f56c 2022-07-16 thomas struct imsgbuf ibuf;
33 8a35f56c 2022-07-16 thomas void (*handler)(int, short, void *);
34 8a35f56c 2022-07-16 thomas struct event ev;
35 8a35f56c 2022-07-16 thomas struct privsep_proc *proc;
36 8a35f56c 2022-07-16 thomas void *data;
37 8a35f56c 2022-07-16 thomas short events;
38 8a35f56c 2022-07-16 thomas };
39 8a35f56c 2022-07-16 thomas
40 8a35f56c 2022-07-16 thomas #define IMSG_SIZE_CHECK(imsg, p) do { \
41 8a35f56c 2022-07-16 thomas if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
42 8a35f56c 2022-07-16 thomas fatalx("bad length imsg received (%s)", #p); \
43 8a35f56c 2022-07-16 thomas } while (0)
44 8a35f56c 2022-07-16 thomas #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
45 8a35f56c 2022-07-16 thomas
46 8a35f56c 2022-07-16 thomas struct ctl_conn {
47 8a35f56c 2022-07-16 thomas TAILQ_ENTRY(ctl_conn) entry;
48 8a35f56c 2022-07-16 thomas uint8_t flags;
49 8a35f56c 2022-07-16 thomas unsigned int waiting;
50 8a35f56c 2022-07-16 thomas #define CTL_CONN_NOTIFY 0x01
51 8a35f56c 2022-07-16 thomas struct imsgev iev;
52 8a35f56c 2022-07-16 thomas uid_t uid;
53 8a35f56c 2022-07-16 thomas };
54 8a35f56c 2022-07-16 thomas TAILQ_HEAD(ctl_connlist, ctl_conn);
55 8a35f56c 2022-07-16 thomas extern struct ctl_connlist ctl_conns;
56 8a35f56c 2022-07-16 thomas
57 8a35f56c 2022-07-16 thomas /* privsep */
58 8a35f56c 2022-07-16 thomas enum privsep_procid {
59 8a35f56c 2022-07-16 thomas PROC_GOTWEBD = 0,
60 8a35f56c 2022-07-16 thomas PROC_SOCKS,
61 8a35f56c 2022-07-16 thomas PROC_MAX,
62 8a35f56c 2022-07-16 thomas };
63 8a35f56c 2022-07-16 thomas extern enum privsep_procid privsep_process;
64 8a35f56c 2022-07-16 thomas
65 8a35f56c 2022-07-16 thomas #define CONFIG_RELOAD 0x00
66 8a35f56c 2022-07-16 thomas #define CONFIG_SOCKS 0x01
67 8a35f56c 2022-07-16 thomas #define CONFIG_ALL 0xff
68 8a35f56c 2022-07-16 thomas
69 8a35f56c 2022-07-16 thomas struct privsep_pipes {
70 8a35f56c 2022-07-16 thomas int *pp_pipes[PROC_MAX];
71 8a35f56c 2022-07-16 thomas };
72 8a35f56c 2022-07-16 thomas
73 8a35f56c 2022-07-16 thomas struct privsep {
74 8a35f56c 2022-07-16 thomas struct privsep_pipes *ps_pipes[PROC_MAX];
75 8a35f56c 2022-07-16 thomas struct privsep_pipes *ps_pp;
76 8a35f56c 2022-07-16 thomas
77 8a35f56c 2022-07-16 thomas struct imsgev *ps_ievs[PROC_MAX];
78 8a35f56c 2022-07-16 thomas const char *ps_title[PROC_MAX];
79 8a35f56c 2022-07-16 thomas uint8_t ps_what[PROC_MAX];
80 8a35f56c 2022-07-16 thomas
81 8a35f56c 2022-07-16 thomas struct passwd *ps_pw;
82 8a35f56c 2022-07-16 thomas int ps_noaction;
83 8a35f56c 2022-07-16 thomas
84 8a35f56c 2022-07-16 thomas unsigned int ps_instances[PROC_MAX];
85 8a35f56c 2022-07-16 thomas unsigned int ps_instance;
86 8a35f56c 2022-07-16 thomas
87 8a35f56c 2022-07-16 thomas /* Event and signal handlers */
88 8a35f56c 2022-07-16 thomas struct event ps_evsigint;
89 8a35f56c 2022-07-16 thomas struct event ps_evsigterm;
90 8a35f56c 2022-07-16 thomas struct event ps_evsigchld;
91 8a35f56c 2022-07-16 thomas struct event ps_evsighup;
92 8a35f56c 2022-07-16 thomas struct event ps_evsigpipe;
93 8a35f56c 2022-07-16 thomas struct event ps_evsigusr1;
94 8a35f56c 2022-07-16 thomas
95 8a35f56c 2022-07-16 thomas void *ps_env;
96 8a35f56c 2022-07-16 thomas };
97 8a35f56c 2022-07-16 thomas
98 8a35f56c 2022-07-16 thomas struct privsep_proc {
99 8a35f56c 2022-07-16 thomas const char *p_title;
100 8a35f56c 2022-07-16 thomas enum privsep_procid p_id;
101 8a35f56c 2022-07-16 thomas int (*p_cb)(int, struct privsep_proc *,
102 8a35f56c 2022-07-16 thomas struct imsg *);
103 8a35f56c 2022-07-16 thomas void (*p_init)(struct privsep *,
104 8a35f56c 2022-07-16 thomas struct privsep_proc *);
105 8a35f56c 2022-07-16 thomas void (*p_shutdown)(void);
106 8a35f56c 2022-07-16 thomas const char *p_chroot;
107 8a35f56c 2022-07-16 thomas struct passwd *p_pw;
108 8a35f56c 2022-07-16 thomas struct privsep *p_ps;
109 8a35f56c 2022-07-16 thomas };
110 8a35f56c 2022-07-16 thomas
111 8a35f56c 2022-07-16 thomas struct privsep_fd {
112 8a35f56c 2022-07-16 thomas enum privsep_procid pf_procid;
113 8a35f56c 2022-07-16 thomas unsigned int pf_instance;
114 8a35f56c 2022-07-16 thomas };
115 8a35f56c 2022-07-16 thomas
116 8a35f56c 2022-07-16 thomas #if DEBUG
117 8a35f56c 2022-07-16 thomas #define DPRINTF log_debug
118 8a35f56c 2022-07-16 thomas #else
119 8a35f56c 2022-07-16 thomas #define DPRINTF(x...) do {} while(0)
120 8a35f56c 2022-07-16 thomas #endif
121 8a35f56c 2022-07-16 thomas
122 8a35f56c 2022-07-16 thomas #define PROC_GOTWEBD_SOCK_FILENO 3
123 8a35f56c 2022-07-16 thomas #define PROC_MAX_INSTANCES 32
124 8a35f56c 2022-07-16 thomas
125 8a35f56c 2022-07-16 thomas /* proc.c */
126 8a35f56c 2022-07-16 thomas void proc_init(struct privsep *, struct privsep_proc *, unsigned int,
127 8a35f56c 2022-07-16 thomas int, char **, enum privsep_procid);
128 8a35f56c 2022-07-16 thomas void proc_kill(struct privsep *);
129 8a35f56c 2022-07-16 thomas void proc_connect(struct privsep *ps);
130 8a35f56c 2022-07-16 thomas void proc_dispatch(int, short event, void *);
131 8a35f56c 2022-07-16 thomas void proc_range(struct privsep *, enum privsep_procid, int *, int *);
132 8a35f56c 2022-07-16 thomas void proc_run(struct privsep *, struct privsep_proc *,
133 8a35f56c 2022-07-16 thomas struct privsep_proc *, unsigned int,
134 8a35f56c 2022-07-16 thomas void (*)(struct privsep *, struct privsep_proc *, void *), void *);
135 8a35f56c 2022-07-16 thomas void imsg_event_add(struct imsgev *);
136 8a35f56c 2022-07-16 thomas int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
137 8a35f56c 2022-07-16 thomas pid_t, int, void *, uint16_t);
138 8a35f56c 2022-07-16 thomas int imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
139 8a35f56c 2022-07-16 thomas pid_t, int, const struct iovec *, int);
140 8a35f56c 2022-07-16 thomas int proc_compose_imsg(struct privsep *, enum privsep_procid, int,
141 8a35f56c 2022-07-16 thomas uint16_t, uint32_t, int, void *, uint16_t);
142 8a35f56c 2022-07-16 thomas int proc_compose(struct privsep *, enum privsep_procid,
143 8a35f56c 2022-07-16 thomas uint16_t, void *data, uint16_t);
144 8a35f56c 2022-07-16 thomas int proc_composev_imsg(struct privsep *, enum privsep_procid, int,
145 8a35f56c 2022-07-16 thomas uint16_t, uint32_t, int, const struct iovec *, int);
146 8a35f56c 2022-07-16 thomas int proc_composev(struct privsep *, enum privsep_procid,
147 8a35f56c 2022-07-16 thomas uint16_t, const struct iovec *, int);
148 8a35f56c 2022-07-16 thomas int proc_forward_imsg(struct privsep *, struct imsg *,
149 8a35f56c 2022-07-16 thomas enum privsep_procid, int);
150 8a35f56c 2022-07-16 thomas struct imsgbuf *
151 8a35f56c 2022-07-16 thomas proc_ibuf(struct privsep *, enum privsep_procid, int);
152 8a35f56c 2022-07-16 thomas struct imsgev *
153 8a35f56c 2022-07-16 thomas proc_iev(struct privsep *, enum privsep_procid, int);
154 8a35f56c 2022-07-16 thomas enum privsep_procid
155 8a35f56c 2022-07-16 thomas proc_getid(struct privsep_proc *, unsigned int, const char *);
156 8a35f56c 2022-07-16 thomas int proc_flush_imsg(struct privsep *, enum privsep_procid, int);
157 8a35f56c 2022-07-16 thomas
158 8a35f56c 2022-07-16 thomas /* log.c */
159 8a35f56c 2022-07-16 thomas void log_init(int, int);
160 8a35f56c 2022-07-16 thomas void log_procinit(const char *);
161 8a35f56c 2022-07-16 thomas void log_setverbose(int);
162 8a35f56c 2022-07-16 thomas int log_getverbose(void);
163 8a35f56c 2022-07-16 thomas void log_warn(const char *, ...)
164 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
165 8a35f56c 2022-07-16 thomas void log_warnx(const char *, ...)
166 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
167 8a35f56c 2022-07-16 thomas void log_info(const char *, ...)
168 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
169 8a35f56c 2022-07-16 thomas void log_debug(const char *, ...)
170 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
171 8a35f56c 2022-07-16 thomas void logit(int, const char *, ...)
172 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 2, 3)));
173 8a35f56c 2022-07-16 thomas void vlog(int, const char *, va_list)
174 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 2, 0)));
175 8a35f56c 2022-07-16 thomas __dead void fatal(const char *, ...)
176 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));
177 8a35f56c 2022-07-16 thomas __dead void fatalx(const char *, ...)
178 8a35f56c 2022-07-16 thomas __attribute__((__format__ (printf, 1, 2)));