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
32 enum {
33 IMSG_NONE,
34 IMSG_CFG_START,
35 IMSG_CFG_SRV,
36 IMSG_CFG_SOCK,
37 IMSG_CFG_DONE,
38 IMSG_CTL_START,
39 IMSG_CTL_RESET,
40 IMSG_CTL_RESTART,
41 IMSG_CTL_PROCFD,
42 };
44 struct galileo;
45 struct proxy_config;
47 struct imsg;
48 struct privsep;
49 struct privsep_proc;
50 struct tls;
52 struct client {
53 uint32_t clt_id;
54 int clt_fd;
55 struct fcgi *clt_fcgi;
56 char *clt_server_name;
57 char *clt_script_name;
58 char *clt_path_info;
59 struct proxy_config *clt_pc;
60 struct event_asr *clt_evasr;
61 struct addrinfo *clt_addrinfo;
62 struct addrinfo *clt_p;
63 struct event clt_evconn;
64 int clt_evconn_live;
65 struct tls *clt_ctx;
66 struct bufferevent *clt_bev;
67 int clt_headersdone;
69 #define TR_ENABLED 0x1
70 #define TR_PRE 0x2
71 #define TR_LIST 0x4
72 #define TR_NAV 0x8
73 int clt_translate;
75 char clt_buf[1024];
76 size_t clt_buflen;
78 SPLAY_ENTRY(client) clt_nodes;
79 };
80 SPLAY_HEAD(client_tree, client);
82 struct fcgi {
83 uint32_t fcg_id;
84 int fcg_s;
85 struct client_tree fcg_clients;
86 struct bufferevent *fcg_bev;
87 int fcg_toread;
88 int fcg_want;
89 int fcg_padding;
90 int fcg_type;
91 uint16_t fcg_rec_id;
92 int fcg_keep_conn;
93 int fcg_done;
95 struct galileo *fcg_env;
97 SPLAY_ENTRY(fcgi) fcg_nodes;
98 };
99 SPLAY_HEAD(fcgi_tree, fcgi);
101 struct proxy_config {
102 char host[HOST_NAME_MAX + 1];
103 char stylesheet[PATH_MAX];
104 char proxy_addr[HOST_NAME_MAX + 1];
105 char proxy_name[HOST_NAME_MAX + 1];
106 uint16_t proxy_port; /* TODO: turn into string */
107 };
109 struct server {
110 TAILQ_ENTRY(server) srv_entry;
111 struct proxy_config srv_conf;
112 };
113 TAILQ_HEAD(serverlist, server);
115 struct galileo {
116 char sc_conffile[PATH_MAX];
117 uint16_t sc_prefork;
118 char sc_chroot[PATH_MAX];
119 struct serverlist sc_servers;
120 struct fcgi_tree sc_fcgi_socks;
122 struct privsep *sc_ps;
123 int sc_reload;
125 /* XXX: generalize */
126 int sc_sock_fd;
127 struct event sc_evsock;
128 struct event sc_evpause;
129 };
131 extern int privsep_process;
133 /* config.c */
134 int config_init(struct galileo *);
135 void config_purge(struct galileo *);
136 int config_setserver(struct galileo *, struct server *);
137 int config_getserver(struct galileo *, struct imsg *);
138 int config_setsock(struct galileo *);
139 int config_getsock(struct galileo *, struct imsg *);
140 int config_setreset(struct galileo *);
141 int config_getreset(struct galileo *, struct imsg *);
143 /* fcgi.c */
144 int fcgi_end_request(struct client *, int);
145 int fcgi_abort_request(struct client *);
146 void fcgi_accept(int, short, void *);
147 void fcgi_read(struct bufferevent *, void *);
148 void fcgi_write(struct bufferevent *, void *);
149 void fcgi_error(struct bufferevent *, short error, void *);
150 int clt_putc(struct client *, char);
151 int clt_puts(struct client *, const char *);
152 int clt_write_bufferevent(struct client *, struct bufferevent *);
153 int clt_flush(struct client *);
154 int clt_write(struct client *, const uint8_t *, size_t);
155 int clt_printf(struct client *, const char *, ...)
156 __attribute__((__format__(printf, 2, 3)))
157 __attribute__((__nonnull__(2)));
158 int fcgi_cmp(struct fcgi *, struct fcgi *);
159 int fcgi_client_cmp(struct client *, struct client *);
161 /* galileo.c */
162 int accept_reserve(int, struct sockaddr *, socklen_t *, int,
163 volatile int *);
164 /* parse.y */
165 int parse_config(const char *, struct galileo *);
166 int cmdline_symset(char *);
168 /* proxy.c */
169 extern volatile int proxy_inflight;
170 extern uint32_t proxy_fcg_id;
172 void proxy(struct privsep *, struct privsep_proc *);
173 void proxy_purge(struct server *);
174 void proxy_start_request(struct galileo *, struct client *);
175 void proxy_client_free(struct client *);
177 SPLAY_PROTOTYPE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
178 SPLAY_PROTOTYPE(client_tree, client, clt_nodes, fcgi_client_cmp);