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