Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
4 * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
20 #ifndef nitems
21 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
22 #endif
24 #ifndef GALILEO_USER
25 #define GALILEO_USER "www"
26 #endif
28 #ifndef GALILEO_CONF
29 #define GALILEO_CONF "/etc/galileo.conf"
30 #endif
32 #ifndef GALILEO_SOCK
33 #define GALILEO_SOCK "/var/www/run/galileo.sock"
34 #endif
36 #define FD_RESERVE 5
37 #define PROC_MAX_INSTANCES 32
38 #define PROXY_NUMPROC 3
39 #define PROC_PARENT_SOCK_FILENO 3
40 #define GEMINI_MAXLEN (1024 + 1) /* NULL */
41 #define FORM_URLENCODED "application/x-www-form-urlencoded"
43 #ifdef DEBUG
44 #define DPRINTF log_debug
45 #else
46 #define DPRINTF(x...) do {} while (0)
47 #endif
49 enum {
50 METHOD_UNKNOWN,
51 METHOD_GET,
52 METHOD_POST,
53 };
55 enum {
56 IMSG_NONE,
57 IMSG_CFG_START,
58 IMSG_CFG_SRV,
59 IMSG_CFG_SOCK,
60 IMSG_CFG_DONE,
61 IMSG_CTL_START,
62 IMSG_CTL_RESET,
63 IMSG_CTL_RESTART,
64 IMSG_CTL_PROCFD,
65 };
67 struct galileo;
68 struct proxy_config;
70 struct imsg;
71 struct privsep;
72 struct privsep_proc;
73 struct template;
74 struct tls;
76 struct client {
77 uint32_t clt_id;
78 int clt_fd;
79 struct fcgi *clt_fcgi;
80 char *clt_server_name;
81 char *clt_script_name;
82 char *clt_path_info;
83 char *clt_query;
84 int clt_method;
85 int clt_bodydone;
86 char *clt_body;
87 int clt_bodylen;
88 struct proxy_config *clt_pc;
89 struct event_asr *clt_evasr;
90 struct addrinfo *clt_addrinfo;
91 struct addrinfo *clt_p;
92 struct event clt_evconn;
93 int clt_evconn_live;
94 struct tls *clt_ctx;
95 struct bufferevent *clt_bev;
96 int clt_headersdone;
97 struct template *clt_tp;
99 #define TR_ENABLED 0x1
100 #define TR_PRE 0x2
101 #define TR_LIST 0x4
102 #define TR_NAV 0x8
103 int clt_translate;
105 char clt_buf[1024];
106 size_t clt_buflen;
108 SPLAY_ENTRY(client) clt_nodes;
109 };
110 SPLAY_HEAD(client_tree, client);
112 struct fcgi {
113 uint32_t fcg_id;
114 int fcg_s;
115 struct client_tree fcg_clients;
116 struct bufferevent *fcg_bev;
117 int fcg_toread;
118 int fcg_want;
119 int fcg_padding;
120 int fcg_type;
121 uint16_t fcg_rec_id;
122 int fcg_keep_conn;
123 int fcg_done;
125 struct galileo *fcg_env;
127 SPLAY_ENTRY(fcgi) fcg_nodes;
128 };
129 SPLAY_HEAD(fcgi_tree, fcgi);
131 struct proxy_config {
132 char host[HOST_NAME_MAX + 1];
133 char stylesheet[PATH_MAX];
134 char proxy_addr[HOST_NAME_MAX + 1];
135 char proxy_name[HOST_NAME_MAX + 1];
136 char proxy_port[6];
138 #define PROXY_NO_TLS 0x1
139 #define PROXY_NO_NAVBAR 0x2
140 #define PROXY_NO_FOOTER 0x4
141 #define PROXY_NO_IMGPRV 0x8
142 int flags;
143 };
145 struct proxy {
146 TAILQ_ENTRY(proxy) pr_entry;
147 struct proxy_config pr_conf;
148 };
149 TAILQ_HEAD(proxylist, proxy);
151 struct galileo {
152 char sc_conffile[PATH_MAX];
153 uint16_t sc_prefork;
154 char sc_chroot[PATH_MAX];
155 struct proxylist sc_proxies;
156 struct fcgi_tree sc_fcgi_socks;
158 struct privsep *sc_ps;
159 int sc_reload;
161 /* XXX: generalize */
162 int sc_sock_fd;
163 struct event sc_evsock;
164 struct event sc_evpause;
165 };
167 extern int privsep_process;
169 /* config.c */
170 int config_init(struct galileo *);
171 void config_purge(struct galileo *);
172 int config_setproxy(struct galileo *, struct proxy *);
173 int config_getproxy(struct galileo *, struct imsg *);
174 int config_setsock(struct galileo *);
175 int config_getsock(struct galileo *, struct imsg *);
176 int config_setreset(struct galileo *);
177 int config_getreset(struct galileo *, struct imsg *);
178 int config_getcfg(struct galileo *, struct imsg *);
180 /* fcgi.c */
181 int fcgi_end_request(struct client *, int);
182 int fcgi_abort_request(struct client *);
183 void fcgi_accept(int, short, void *);
184 void fcgi_read(struct bufferevent *, void *);
185 void fcgi_write(struct bufferevent *, void *);
186 void fcgi_error(struct bufferevent *, short error, void *);
187 void fcgi_free(struct fcgi *);
188 int clt_putc(struct client *, char);
189 int clt_puts(struct client *, const char *);
190 int clt_write_bufferevent(struct client *, struct bufferevent *);
191 int clt_flush(struct client *);
192 int clt_write(struct client *, const uint8_t *, size_t);
193 int clt_printf(struct client *, const char *, ...)
194 __attribute__((__format__(printf, 2, 3)))
195 __attribute__((__nonnull__(2)));
196 int clt_tp_puts(struct template *, const char *);
197 int clt_tp_putc(struct template *, int);
198 int fcgi_cmp(struct fcgi *, struct fcgi *);
199 int fcgi_client_cmp(struct client *, struct client *);
201 /* fragments.tmpl */
202 int tp_head(struct template *, const char *, const char *);
203 int tp_foot(struct template *);
204 int tp_figure(struct template *, const char *, const char *);
205 int tp_pre_open(struct template *, const char *);
206 int tp_pre_close(struct template *);
207 int tp_error(struct template *, int, const char *);
208 int tp_inputpage(struct template *, const char *);
210 /* galileo.c */
211 int accept_reserve(int, struct sockaddr *, socklen_t *, int,
212 volatile int *);
213 /* parse.y */
214 int parse_config(const char *, struct galileo *);
215 int cmdline_symset(char *);
217 /* proxy.c */
218 extern volatile int proxy_inflight;
219 extern uint32_t proxy_fcg_id;
221 void proxy(struct privsep *, struct privsep_proc *);
222 void proxy_purge(struct proxy *);
223 struct proxy_config *proxy_match(struct galileo *, const char *);
224 int proxy_start_request(struct galileo *, struct client *);
225 void proxy_client_free(struct client *);
227 SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
228 SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);