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