Blame


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