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 enum {
44 METHOD_UNKNOWN,
45 METHOD_GET,
46 METHOD_POST,
47 };
49 enum {
50 IMSG_NONE,
51 IMSG_CFG_START,
52 IMSG_CFG_SRV,
53 IMSG_CFG_SOCK,
54 IMSG_CFG_DONE,
55 IMSG_CTL_START,
56 IMSG_CTL_RESET,
57 IMSG_CTL_RESTART,
58 IMSG_CTL_PROCFD,
59 };
61 struct galileo;
62 struct proxy_config;
64 struct imsg;
65 struct privsep;
66 struct privsep_proc;
67 struct template;
68 struct tls;
70 struct client {
71 uint32_t clt_id;
72 int clt_fd;
73 struct fcgi *clt_fcgi;
74 char *clt_server_name;
75 char *clt_script_name;
76 char *clt_path_info;
77 char *clt_query;
78 int clt_method;
79 int clt_bodydone;
80 char *clt_body;
81 int clt_bodylen;
82 struct proxy_config *clt_pc;
83 struct event_asr *clt_evasr;
84 struct addrinfo *clt_addrinfo;
85 struct addrinfo *clt_p;
86 struct event clt_evconn;
87 int clt_evconn_live;
88 struct tls *clt_ctx;
89 struct bufferevent *clt_bev;
90 int clt_headersdone;
91 struct template *clt_tp;
93 #define TR_ENABLED 0x1
94 #define TR_PRE 0x2
95 #define TR_LIST 0x4
96 #define TR_NAV 0x8
97 int clt_translate;
99 char clt_buf[1024];
100 size_t clt_buflen;
102 SPLAY_ENTRY(client) clt_nodes;
103 };
104 SPLAY_HEAD(client_tree, client);
106 struct fcgi {
107 uint32_t fcg_id;
108 int fcg_s;
109 struct client_tree fcg_clients;
110 struct bufferevent *fcg_bev;
111 int fcg_toread;
112 int fcg_want;
113 int fcg_padding;
114 int fcg_type;
115 uint16_t fcg_rec_id;
116 int fcg_keep_conn;
117 int fcg_done;
119 struct galileo *fcg_env;
121 SPLAY_ENTRY(fcgi) fcg_nodes;
122 };
123 SPLAY_HEAD(fcgi_tree, fcgi);
125 struct proxy_config {
126 char host[HOST_NAME_MAX + 1];
127 char stylesheet[PATH_MAX];
128 char proxy_addr[HOST_NAME_MAX + 1];
129 char proxy_name[HOST_NAME_MAX + 1];
130 char proxy_port[6];
131 int no_tls;
132 };
134 struct proxy {
135 TAILQ_ENTRY(proxy) pr_entry;
136 struct proxy_config pr_conf;
137 };
138 TAILQ_HEAD(proxylist, proxy);
140 struct galileo {
141 char sc_conffile[PATH_MAX];
142 uint16_t sc_prefork;
143 char sc_chroot[PATH_MAX];
144 struct proxylist sc_proxies;
145 struct fcgi_tree sc_fcgi_socks;
147 struct privsep *sc_ps;
148 int sc_reload;
150 /* XXX: generalize */
151 int sc_sock_fd;
152 struct event sc_evsock;
153 struct event sc_evpause;
154 };
156 extern int privsep_process;
158 /* config.c */
159 int config_init(struct galileo *);
160 void config_purge(struct galileo *);
161 int config_setproxy(struct galileo *, struct proxy *);
162 int config_getproxy(struct galileo *, struct imsg *);
163 int config_setsock(struct galileo *);
164 int config_getsock(struct galileo *, struct imsg *);
165 int config_setreset(struct galileo *);
166 int config_getreset(struct galileo *, struct imsg *);
167 int config_getcfg(struct galileo *, struct imsg *);
169 /* fcgi.c */
170 int fcgi_end_request(struct client *, int);
171 int fcgi_abort_request(struct client *);
172 void fcgi_accept(int, short, void *);
173 void fcgi_read(struct bufferevent *, void *);
174 void fcgi_write(struct bufferevent *, void *);
175 void fcgi_error(struct bufferevent *, short error, void *);
176 void fcgi_free(struct fcgi *);
177 int clt_putc(struct client *, char);
178 int clt_puts(struct client *, const char *);
179 int clt_write_bufferevent(struct client *, struct bufferevent *);
180 int clt_flush(struct client *);
181 int clt_write(struct client *, const uint8_t *, size_t);
182 int clt_printf(struct client *, const char *, ...)
183 __attribute__((__format__(printf, 2, 3)))
184 __attribute__((__nonnull__(2)));
185 int clt_tp_puts(struct template *, const char *);
186 int clt_tp_putc(struct template *, int);
187 int fcgi_cmp(struct fcgi *, struct fcgi *);
188 int fcgi_client_cmp(struct client *, struct client *);
190 /* fragments.tmpl */
191 int tp_head(struct template *, const char *, const char *);
192 int tp_foot(struct template *);
193 int tp_figure(struct template *, const char *, const char *);
194 int tp_error(struct template *, int, const char *);
195 int tp_inputpage(struct template *, const char *);
197 /* galileo.c */
198 int accept_reserve(int, struct sockaddr *, socklen_t *, int,
199 volatile int *);
200 /* parse.y */
201 int parse_config(const char *, struct galileo *);
202 int cmdline_symset(char *);
204 /* proxy.c */
205 extern volatile int proxy_inflight;
206 extern uint32_t proxy_fcg_id;
208 void proxy(struct privsep *, struct privsep_proc *);
209 void proxy_purge(struct proxy *);
210 struct proxy_config *proxy_match(struct galileo *, const char *);
211 int proxy_start_request(struct galileo *, struct client *);
212 void proxy_client_free(struct client *);
214 SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
215 SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);