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 #define GALILEO_USER "www"
25 #define GALILEO_SOCK "/var/www/run/galileo.sock"
26 #define CONF_FILE "/etc/galileo.conf"
27 #define FD_RESERVE 5
28 #define PROC_MAX_INSTANCES 32
29 #define PROXY_NUMPROC 3
30 #define PROC_PARENT_SOCK_FILENO 3
31 #define GEMINI_MAXLEN (1024 + 1) /* NULL */
32 #define FORM_URLENCODED "application/x-www-form-urlencoded"
34 enum {
35 METHOD_UNKNOWN,
36 METHOD_GET,
37 METHOD_POST,
38 };
40 enum {
41 IMSG_NONE,
42 IMSG_CFG_START,
43 IMSG_CFG_SRV,
44 IMSG_CFG_SOCK,
45 IMSG_CFG_DONE,
46 IMSG_CTL_START,
47 IMSG_CTL_RESET,
48 IMSG_CTL_RESTART,
49 IMSG_CTL_PROCFD,
50 };
52 struct galileo;
53 struct proxy_config;
55 struct imsg;
56 struct privsep;
57 struct privsep_proc;
58 struct template;
59 struct tls;
61 struct client {
62 uint32_t clt_id;
63 int clt_fd;
64 struct fcgi *clt_fcgi;
65 char *clt_server_name;
66 char *clt_script_name;
67 char *clt_path_info;
68 char *clt_query;
69 int clt_method;
70 int clt_bodydone;
71 char *clt_body;
72 int clt_bodylen;
73 struct proxy_config *clt_pc;
74 struct event_asr *clt_evasr;
75 struct addrinfo *clt_addrinfo;
76 struct addrinfo *clt_p;
77 struct event clt_evconn;
78 int clt_evconn_live;
79 struct tls *clt_ctx;
80 struct bufferevent *clt_bev;
81 int clt_headersdone;
82 struct template *clt_tp;
84 #define TR_ENABLED 0x1
85 #define TR_PRE 0x2
86 #define TR_LIST 0x4
87 #define TR_NAV 0x8
88 int clt_translate;
90 char clt_buf[1024];
91 size_t clt_buflen;
93 SPLAY_ENTRY(client) clt_nodes;
94 };
95 SPLAY_HEAD(client_tree, client);
97 struct fcgi {
98 uint32_t fcg_id;
99 int fcg_s;
100 struct client_tree fcg_clients;
101 struct bufferevent *fcg_bev;
102 int fcg_toread;
103 int fcg_want;
104 int fcg_padding;
105 int fcg_type;
106 uint16_t fcg_rec_id;
107 int fcg_keep_conn;
108 int fcg_done;
110 struct galileo *fcg_env;
112 SPLAY_ENTRY(fcgi) fcg_nodes;
113 };
114 SPLAY_HEAD(fcgi_tree, fcgi);
116 struct proxy_config {
117 char host[HOST_NAME_MAX + 1];
118 char stylesheet[PATH_MAX];
119 char proxy_addr[HOST_NAME_MAX + 1];
120 char proxy_name[HOST_NAME_MAX + 1];
121 char proxy_port[6];
122 };
124 struct proxy {
125 TAILQ_ENTRY(proxy) pr_entry;
126 struct proxy_config pr_conf;
127 };
128 TAILQ_HEAD(proxylist, proxy);
130 struct galileo {
131 char sc_conffile[PATH_MAX];
132 uint16_t sc_prefork;
133 char sc_chroot[PATH_MAX];
134 struct proxylist sc_proxies;
135 struct fcgi_tree sc_fcgi_socks;
137 struct privsep *sc_ps;
138 int sc_reload;
140 /* XXX: generalize */
141 int sc_sock_fd;
142 struct event sc_evsock;
143 struct event sc_evpause;
144 };
146 extern int privsep_process;
148 /* config.c */
149 int config_init(struct galileo *);
150 void config_purge(struct galileo *);
151 int config_setproxy(struct galileo *, struct proxy *);
152 int config_getproxy(struct galileo *, struct imsg *);
153 int config_setsock(struct galileo *);
154 int config_getsock(struct galileo *, struct imsg *);
155 int config_setreset(struct galileo *);
156 int config_getreset(struct galileo *, struct imsg *);
158 /* fcgi.c */
159 int fcgi_end_request(struct client *, int);
160 int fcgi_abort_request(struct client *);
161 void fcgi_accept(int, short, void *);
162 void fcgi_read(struct bufferevent *, void *);
163 void fcgi_write(struct bufferevent *, void *);
164 void fcgi_error(struct bufferevent *, short error, void *);
165 int clt_putc(struct client *, char);
166 int clt_puts(struct client *, const char *);
167 int clt_write_bufferevent(struct client *, struct bufferevent *);
168 int clt_flush(struct client *);
169 int clt_write(struct client *, const uint8_t *, size_t);
170 int clt_printf(struct client *, const char *, ...)
171 __attribute__((__format__(printf, 2, 3)))
172 __attribute__((__nonnull__(2)));
173 int clt_tp_puts(struct template *, const char *);
174 int clt_tp_putc(struct template *, int);
175 int fcgi_cmp(struct fcgi *, struct fcgi *);
176 int fcgi_client_cmp(struct client *, struct client *);
178 /* fragments.tmpl */
179 int tp_head(struct template *, const char *, const char *);
180 int tp_foot(struct template *);
181 int tp_figure(struct template *, const char *, const char *);
182 int tp_error(struct template *, int, const char *);
183 int tp_inputpage(struct template *, const char *);
185 /* galileo.c */
186 int accept_reserve(int, struct sockaddr *, socklen_t *, int,
187 volatile int *);
188 /* parse.y */
189 int parse_config(const char *, struct galileo *);
190 int cmdline_symset(char *);
192 /* proxy.c */
193 extern volatile int proxy_inflight;
194 extern uint32_t proxy_fcg_id;
196 void proxy(struct privsep *, struct privsep_proc *);
197 void proxy_purge(struct proxy *);
198 struct proxy_config *proxy_match(struct galileo *, const char *);
199 int proxy_start_request(struct galileo *, struct client *);
200 void proxy_client_free(struct client *);
202 SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
203 SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);