Blame


1 b0a6bcf7 2022-09-13 op /*
2 b0a6bcf7 2022-09-13 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 b0a6bcf7 2022-09-13 op * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
4 b0a6bcf7 2022-09-13 op * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5 b0a6bcf7 2022-09-13 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 b0a6bcf7 2022-09-13 op *
7 b0a6bcf7 2022-09-13 op * Permission to use, copy, modify, and distribute this software for any
8 b0a6bcf7 2022-09-13 op * purpose with or without fee is hereby granted, provided that the above
9 b0a6bcf7 2022-09-13 op * copyright notice and this permission notice appear in all copies.
10 b0a6bcf7 2022-09-13 op *
11 b0a6bcf7 2022-09-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 b0a6bcf7 2022-09-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 b0a6bcf7 2022-09-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 b0a6bcf7 2022-09-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 b0a6bcf7 2022-09-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 b0a6bcf7 2022-09-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 b0a6bcf7 2022-09-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 b0a6bcf7 2022-09-13 op */
19 b0a6bcf7 2022-09-13 op
20 b0a6bcf7 2022-09-13 op #ifndef nitems
21 b0a6bcf7 2022-09-13 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
22 b0a6bcf7 2022-09-13 op #endif
23 b0a6bcf7 2022-09-13 op
24 b0a6bcf7 2022-09-13 op #define GALILEO_USER "www"
25 b0a6bcf7 2022-09-13 op #define GALILEO_SOCK "/var/www/run/galileo.sock"
26 b0a6bcf7 2022-09-13 op #define CONF_FILE "/etc/galileo.conf"
27 b0a6bcf7 2022-09-13 op #define FD_RESERVE 5
28 b0a6bcf7 2022-09-13 op #define PROC_MAX_INSTANCES 32
29 b0a6bcf7 2022-09-13 op #define PROXY_NUMPROC 3
30 b0a6bcf7 2022-09-13 op #define PROC_PARENT_SOCK_FILENO 3
31 b0a6bcf7 2022-09-13 op
32 b0a6bcf7 2022-09-13 op enum {
33 b0a6bcf7 2022-09-13 op IMSG_NONE,
34 b0a6bcf7 2022-09-13 op IMSG_CFG_START,
35 b0a6bcf7 2022-09-13 op IMSG_CFG_SRV,
36 b0a6bcf7 2022-09-13 op IMSG_CFG_SOCK,
37 b0a6bcf7 2022-09-13 op IMSG_CFG_DONE,
38 b0a6bcf7 2022-09-13 op IMSG_CTL_START,
39 b0a6bcf7 2022-09-13 op IMSG_CTL_RESET,
40 b0a6bcf7 2022-09-13 op IMSG_CTL_RESTART,
41 b0a6bcf7 2022-09-13 op IMSG_CTL_PROCFD,
42 b0a6bcf7 2022-09-13 op };
43 b0a6bcf7 2022-09-13 op
44 b0a6bcf7 2022-09-13 op struct galileo;
45 b0a6bcf7 2022-09-13 op struct proxy_config;
46 b0a6bcf7 2022-09-13 op
47 b0a6bcf7 2022-09-13 op struct imsg;
48 b0a6bcf7 2022-09-13 op struct privsep;
49 b0a6bcf7 2022-09-13 op struct privsep_proc;
50 bff58270 2022-09-19 op struct template;
51 b0a6bcf7 2022-09-13 op struct tls;
52 b0a6bcf7 2022-09-13 op
53 b0a6bcf7 2022-09-13 op struct client {
54 b0a6bcf7 2022-09-13 op uint32_t clt_id;
55 b0a6bcf7 2022-09-13 op int clt_fd;
56 b0a6bcf7 2022-09-13 op struct fcgi *clt_fcgi;
57 b0a6bcf7 2022-09-13 op char *clt_server_name;
58 b0a6bcf7 2022-09-13 op char *clt_script_name;
59 b0a6bcf7 2022-09-13 op char *clt_path_info;
60 b0a6bcf7 2022-09-13 op struct proxy_config *clt_pc;
61 b0a6bcf7 2022-09-13 op struct event_asr *clt_evasr;
62 b0a6bcf7 2022-09-13 op struct addrinfo *clt_addrinfo;
63 b0a6bcf7 2022-09-13 op struct addrinfo *clt_p;
64 b0a6bcf7 2022-09-13 op struct event clt_evconn;
65 b0a6bcf7 2022-09-13 op int clt_evconn_live;
66 b0a6bcf7 2022-09-13 op struct tls *clt_ctx;
67 b0a6bcf7 2022-09-13 op struct bufferevent *clt_bev;
68 b0a6bcf7 2022-09-13 op int clt_headersdone;
69 bff58270 2022-09-19 op struct template *clt_tp;
70 b9de0a74 2022-09-14 op
71 b9de0a74 2022-09-14 op #define TR_ENABLED 0x1
72 b9de0a74 2022-09-14 op #define TR_PRE 0x2
73 b9de0a74 2022-09-14 op #define TR_LIST 0x4
74 b9de0a74 2022-09-14 op #define TR_NAV 0x8
75 c9dc4bc6 2022-09-13 op int clt_translate;
76 b0a6bcf7 2022-09-13 op
77 b0a6bcf7 2022-09-13 op char clt_buf[1024];
78 b0a6bcf7 2022-09-13 op size_t clt_buflen;
79 b0a6bcf7 2022-09-13 op
80 b0a6bcf7 2022-09-13 op SPLAY_ENTRY(client) clt_nodes;
81 b0a6bcf7 2022-09-13 op };
82 b0a6bcf7 2022-09-13 op SPLAY_HEAD(client_tree, client);
83 b0a6bcf7 2022-09-13 op
84 b0a6bcf7 2022-09-13 op struct fcgi {
85 b0a6bcf7 2022-09-13 op uint32_t fcg_id;
86 b0a6bcf7 2022-09-13 op int fcg_s;
87 b0a6bcf7 2022-09-13 op struct client_tree fcg_clients;
88 b0a6bcf7 2022-09-13 op struct bufferevent *fcg_bev;
89 b0a6bcf7 2022-09-13 op int fcg_toread;
90 b0a6bcf7 2022-09-13 op int fcg_want;
91 b0a6bcf7 2022-09-13 op int fcg_padding;
92 b0a6bcf7 2022-09-13 op int fcg_type;
93 b0a6bcf7 2022-09-13 op uint16_t fcg_rec_id;
94 b0a6bcf7 2022-09-13 op int fcg_keep_conn;
95 247917be 2022-09-14 op int fcg_done;
96 b0a6bcf7 2022-09-13 op
97 b0a6bcf7 2022-09-13 op struct galileo *fcg_env;
98 b0a6bcf7 2022-09-13 op
99 b0a6bcf7 2022-09-13 op SPLAY_ENTRY(fcgi) fcg_nodes;
100 b0a6bcf7 2022-09-13 op };
101 b0a6bcf7 2022-09-13 op SPLAY_HEAD(fcgi_tree, fcgi);
102 b0a6bcf7 2022-09-13 op
103 b0a6bcf7 2022-09-13 op struct proxy_config {
104 b0a6bcf7 2022-09-13 op char host[HOST_NAME_MAX + 1];
105 b0a6bcf7 2022-09-13 op char stylesheet[PATH_MAX];
106 b0a6bcf7 2022-09-13 op char proxy_addr[HOST_NAME_MAX + 1];
107 b0a6bcf7 2022-09-13 op char proxy_name[HOST_NAME_MAX + 1];
108 b0a6bcf7 2022-09-13 op uint16_t proxy_port; /* TODO: turn into string */
109 b0a6bcf7 2022-09-13 op };
110 b0a6bcf7 2022-09-13 op
111 b0a6bcf7 2022-09-13 op struct server {
112 b0a6bcf7 2022-09-13 op TAILQ_ENTRY(server) srv_entry;
113 b0a6bcf7 2022-09-13 op struct proxy_config srv_conf;
114 b0a6bcf7 2022-09-13 op };
115 b0a6bcf7 2022-09-13 op TAILQ_HEAD(serverlist, server);
116 b0a6bcf7 2022-09-13 op
117 b0a6bcf7 2022-09-13 op struct galileo {
118 b0a6bcf7 2022-09-13 op char sc_conffile[PATH_MAX];
119 b0a6bcf7 2022-09-13 op uint16_t sc_prefork;
120 b0a6bcf7 2022-09-13 op char sc_chroot[PATH_MAX];
121 b0a6bcf7 2022-09-13 op struct serverlist sc_servers;
122 b0a6bcf7 2022-09-13 op struct fcgi_tree sc_fcgi_socks;
123 b0a6bcf7 2022-09-13 op
124 b0a6bcf7 2022-09-13 op struct privsep *sc_ps;
125 b0a6bcf7 2022-09-13 op int sc_reload;
126 b0a6bcf7 2022-09-13 op
127 b0a6bcf7 2022-09-13 op /* XXX: generalize */
128 b0a6bcf7 2022-09-13 op int sc_sock_fd;
129 b0a6bcf7 2022-09-13 op struct event sc_evsock;
130 b0a6bcf7 2022-09-13 op struct event sc_evpause;
131 b0a6bcf7 2022-09-13 op };
132 b0a6bcf7 2022-09-13 op
133 b0a6bcf7 2022-09-13 op extern int privsep_process;
134 b0a6bcf7 2022-09-13 op
135 b0a6bcf7 2022-09-13 op /* config.c */
136 b0a6bcf7 2022-09-13 op int config_init(struct galileo *);
137 b0a6bcf7 2022-09-13 op void config_purge(struct galileo *);
138 b0a6bcf7 2022-09-13 op int config_setserver(struct galileo *, struct server *);
139 b0a6bcf7 2022-09-13 op int config_getserver(struct galileo *, struct imsg *);
140 b0a6bcf7 2022-09-13 op int config_setsock(struct galileo *);
141 b0a6bcf7 2022-09-13 op int config_getsock(struct galileo *, struct imsg *);
142 b0a6bcf7 2022-09-13 op int config_setreset(struct galileo *);
143 b0a6bcf7 2022-09-13 op int config_getreset(struct galileo *, struct imsg *);
144 b0a6bcf7 2022-09-13 op
145 b0a6bcf7 2022-09-13 op /* fcgi.c */
146 b0a6bcf7 2022-09-13 op int fcgi_end_request(struct client *, int);
147 b0a6bcf7 2022-09-13 op int fcgi_abort_request(struct client *);
148 ea8874f1 2022-09-13 op void fcgi_accept(int, short, void *);
149 b0a6bcf7 2022-09-13 op void fcgi_read(struct bufferevent *, void *);
150 b0a6bcf7 2022-09-13 op void fcgi_write(struct bufferevent *, void *);
151 b0a6bcf7 2022-09-13 op void fcgi_error(struct bufferevent *, short error, void *);
152 c9dc4bc6 2022-09-13 op int clt_putc(struct client *, char);
153 c9dc4bc6 2022-09-13 op int clt_puts(struct client *, const char *);
154 b0a6bcf7 2022-09-13 op int clt_write_bufferevent(struct client *, struct bufferevent *);
155 b0a6bcf7 2022-09-13 op int clt_flush(struct client *);
156 b0a6bcf7 2022-09-13 op int clt_write(struct client *, const uint8_t *, size_t);
157 b0a6bcf7 2022-09-13 op int clt_printf(struct client *, const char *, ...)
158 b0a6bcf7 2022-09-13 op __attribute__((__format__(printf, 2, 3)))
159 b0a6bcf7 2022-09-13 op __attribute__((__nonnull__(2)));
160 232eee45 2022-09-20 op int clt_tp_puts(struct template *, const char *);
161 232eee45 2022-09-20 op int clt_tp_putc(struct template *, int);
162 b0a6bcf7 2022-09-13 op int fcgi_cmp(struct fcgi *, struct fcgi *);
163 b0a6bcf7 2022-09-13 op int fcgi_client_cmp(struct client *, struct client *);
164 b0a6bcf7 2022-09-13 op
165 bff58270 2022-09-19 op /* fragments.tmpl */
166 bff58270 2022-09-19 op int tp_head(struct template *, const char *, const char *);
167 bff58270 2022-09-19 op int tp_foot(struct template *);
168 bff58270 2022-09-19 op int tp_figure(struct template *, const char *, const char *);
169 e3c1cbab 2022-09-20 op int tp_error(struct template *, const char *);
170 bff58270 2022-09-19 op
171 b0a6bcf7 2022-09-13 op /* galileo.c */
172 b0a6bcf7 2022-09-13 op int accept_reserve(int, struct sockaddr *, socklen_t *, int,
173 b0a6bcf7 2022-09-13 op volatile int *);
174 b0a6bcf7 2022-09-13 op /* parse.y */
175 b0a6bcf7 2022-09-13 op int parse_config(const char *, struct galileo *);
176 b0a6bcf7 2022-09-13 op int cmdline_symset(char *);
177 b0a6bcf7 2022-09-13 op
178 b0a6bcf7 2022-09-13 op /* proxy.c */
179 b0a6bcf7 2022-09-13 op extern volatile int proxy_inflight;
180 b0a6bcf7 2022-09-13 op extern uint32_t proxy_fcg_id;
181 b0a6bcf7 2022-09-13 op
182 b0a6bcf7 2022-09-13 op void proxy(struct privsep *, struct privsep_proc *);
183 b0a6bcf7 2022-09-13 op void proxy_purge(struct server *);
184 b0a6bcf7 2022-09-13 op void proxy_start_request(struct galileo *, struct client *);
185 b0a6bcf7 2022-09-13 op void proxy_client_free(struct client *);
186 b0a6bcf7 2022-09-13 op
187 b0a6bcf7 2022-09-13 op SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
188 b0a6bcf7 2022-09-13 op SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);