Blob


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