Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 * Copyright (c) 2013 Florian Obser <florian@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 #include <netinet/in.h>
21 #include <net/if.h>
22 #include <sys/queue.h>
24 #include <limits.h>
25 #include <stdio.h>
27 #ifdef DEBUG
28 #define dprintf(x...) do { log_debug(x); } while(0)
29 #else
30 #define dprintf(x...)
31 #endif /* DEBUG */
33 #ifndef nitems
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
35 #endif
37 /* GOTWEBD DEFAULTS */
38 #define GOTWEBD_CONF "/etc/gotwebd.conf"
40 #define GOTWEBD_USER "www"
42 #define GOTWEBD_CACHESIZE 1024
43 #define GOTWEBD_MAXCLIENTS 1024
44 #define GOTWEBD_MAXTEXT 511
45 #define GOTWEBD_MAXNAME 64
46 #define GOTWEBD_MAXPORT 6
47 #define GOTWEBD_NUMPROC 3
48 #define GOTWEBD_MAXIFACE 16
49 #define GOTWEBD_REPO_CACHESIZE 4
51 /* GOTWEB DEFAULTS */
52 #define MAX_QUERYSTRING 2048
53 #define MAX_SCRIPT_NAME 255
54 #define MAX_SERVER_NAME 255
56 #define GOTWEB_GIT_DIR ".git"
58 #define D_HTTPD_CHROOT "/var/www"
59 #define D_UNIX_SOCKET "/run/gotweb.sock"
60 #define D_FCGI_PORT "9000"
61 #define D_GOTPATH "/got/public"
62 #define D_SITENAME "Gotweb"
63 #define D_SITEOWNER "Got Owner"
64 #define D_SITELINK "Repos"
65 #define D_GOTLOGO "got.png"
66 #define D_GOTURL "https://gameoftrees.org"
67 #define D_GOTWEBCSS "gotweb.css"
69 #define D_SHOWROWNER 1
70 #define D_SHOWSOWNER 1
71 #define D_SHOWAGE 1
72 #define D_SHOWDESC 1
73 #define D_SHOWURL 1
74 #define D_MAXREPO 0
75 #define D_MAXREPODISP 25
76 #define D_MAXSLCOMMDISP 10
77 #define D_MAXCOMMITDISP 25
79 #define BUF 8192
81 #define TIMEOUT_DEFAULT 120
83 #define FCGI_CONTENT_SIZE 65535
84 #define FCGI_PADDING_SIZE 255
85 #define FCGI_RECORD_SIZE \
86 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
88 #define FCGI_ALIGNMENT 8
89 #define FCGI_ALIGN(n) \
90 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
92 #define FD_RESERVE 5
93 #define FD_NEEDED 6
95 #define FCGI_BEGIN_REQUEST 1
96 #define FCGI_ABORT_REQUEST 2
97 #define FCGI_END_REQUEST 3
98 #define FCGI_PARAMS 4
99 #define FCGI_STDIN 5
100 #define FCGI_STDOUT 6
101 #define FCGI_STDERR 7
102 #define FCGI_DATA 8
103 #define FCGI_GET_VALUES 9
104 #define FCGI_GET_VALUES_RESULT 10
105 #define FCGI_UNKNOWN_TYPE 11
106 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
108 #define FCGI_REQUEST_COMPLETE 0
109 #define FCGI_CANT_MPX_CONN 1
110 #define FCGI_OVERLOADED 2
111 #define FCGI_UNKNOWN_ROLE 3
113 #define GOTWEB_PACK_NUM_TEMPFILES 32
115 enum imsg_type {
116 IMSG_CFG_SRV = IMSG_PROC_MAX,
117 IMSG_CFG_SOCK,
118 IMSG_CFG_FD,
119 IMSG_CFG_DONE,
120 IMSG_CTL_START,
121 };
123 struct env_val {
124 SLIST_ENTRY(env_val) entry;
125 char *val;
126 };
127 SLIST_HEAD(env_head, env_val);
129 struct fcgi_record_header {
130 uint8_t version;
131 uint8_t type;
132 uint16_t id;
133 uint16_t content_len;
134 uint8_t padding_len;
135 uint8_t reserved;
136 }__attribute__((__packed__));
138 struct repo_dir {
139 char *name;
140 char *owner;
141 char *description;
142 char *url;
143 char *age;
144 char *path;
145 };
147 struct repo_tag {
148 TAILQ_ENTRY(repo_tag) entry;
149 char *commit_id;
150 char *tag_name;
151 char *tag_commit;
152 char *commit_msg;
153 char *tagger;
154 time_t tagger_time;
155 };
157 struct repo_commit {
158 TAILQ_ENTRY(repo_commit) entry;
159 char *path;
160 char *refs_str;
161 char *commit_id; /* id_str1 */
162 char *parent_id; /* id_str2 */
163 char *tree_id;
164 char *author;
165 char *committer;
166 char *commit_msg;
167 time_t committer_time;
168 };
170 struct got_repository;
171 struct transport {
172 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
173 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
174 struct got_repository *repo;
175 struct repo_dir *repo_dir;
176 struct querystring *qs;
177 char *next_id;
178 char *prev_id;
179 unsigned int repos_total;
180 unsigned int next_disp;
181 unsigned int prev_disp;
182 unsigned int tag_count;
183 };
185 enum socket_priv_fds {
186 DIFF_FD_1,
187 DIFF_FD_2,
188 DIFF_FD_3,
189 DIFF_FD_4,
190 DIFF_FD_5,
191 BLAME_FD_1,
192 BLAME_FD_2,
193 BLAME_FD_3,
194 BLAME_FD_4,
195 BLAME_FD_5,
196 BLAME_FD_6,
197 BLOB_FD_1,
198 BLOB_FD_2,
199 PRIV_FDS__MAX,
200 };
202 struct request {
203 struct socket *sock;
204 struct server *srv;
205 struct transport *t;
206 struct event ev;
207 struct event tmo;
209 uint16_t id;
210 int fd;
211 int priv_fd[PRIV_FDS__MAX];
213 uint8_t buf[FCGI_RECORD_SIZE];
214 size_t buf_pos;
215 size_t buf_len;
217 uint8_t outbuf[GOTWEBD_CACHESIZE];
218 size_t outbuf_len;
220 char querystring[MAX_QUERYSTRING];
221 char http_host[GOTWEBD_MAXTEXT];
222 char script_name[MAX_SCRIPT_NAME];
223 char server_name[MAX_SERVER_NAME];
225 uint8_t request_started;
226 };
228 struct fcgi_begin_request_body {
229 uint16_t role;
230 uint8_t flags;
231 uint8_t reserved[5];
232 }__attribute__((__packed__));
234 struct fcgi_end_request_body {
235 uint32_t app_status;
236 uint8_t protocol_status;
237 uint8_t reserved[3];
238 }__attribute__((__packed__));
240 struct address {
241 TAILQ_ENTRY(address) entry;
242 struct sockaddr_storage ss;
243 int ipproto;
244 int prefixlen;
245 in_port_t port;
246 char ifname[IFNAMSIZ];
247 };
248 TAILQ_HEAD(addresslist, address);
250 struct cached_repo {
251 char path[PATH_MAX];
252 struct got_repository *repo;
253 };
255 struct server {
256 TAILQ_ENTRY(server) entry;
257 struct addresslist al;
259 struct cached_repo *cached_repos;
260 int ncached_repos;
262 char name[GOTWEBD_MAXTEXT];
264 char repos_path[PATH_MAX];
265 char site_name[GOTWEBD_MAXNAME];
266 char site_owner[GOTWEBD_MAXNAME];
267 char site_link[GOTWEBD_MAXTEXT];
268 char logo[GOTWEBD_MAXTEXT];
269 char logo_url[GOTWEBD_MAXTEXT];
270 char custom_css[PATH_MAX];
272 size_t max_repos;
273 size_t max_repos_display;
274 size_t max_commits_display;
276 int show_site_owner;
277 int show_repo_owner;
278 int show_repo_age;
279 int show_repo_description;
280 int show_repo_cloneurl;
282 int unix_socket;
283 char unix_socket_name[PATH_MAX];
285 int fcgi_socket;
286 };
287 TAILQ_HEAD(serverlist, server);
289 enum client_action {
290 CLIENT_CONNECT,
291 CLIENT_DISCONNECT,
292 };
294 struct socket_conf {
295 struct address addr;
297 char name[GOTWEBD_MAXTEXT];
298 char srv_name[GOTWEBD_MAXTEXT];
300 int id;
301 int af_type;
302 char unix_socket_name[PATH_MAX];
303 in_port_t fcgi_socket_port;
304 };
306 struct socket {
307 TAILQ_ENTRY(socket) entry;
308 struct socket_conf conf;
310 int fd;
311 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
312 int priv_fd[PRIV_FDS__MAX];
314 struct event evt;
315 struct event ev;
316 struct event pause;
318 int client_status;
319 };
320 TAILQ_HEAD(socketlist, socket);
322 struct gotwebd {
323 struct serverlist servers;
324 struct socketlist sockets;
326 struct privsep *gotwebd_ps;
327 const char *gotwebd_conffile;
329 int gotwebd_debug;
330 int gotwebd_verbose;
331 int gotwebd_noaction;
333 uint16_t prefork_gotwebd;
334 int gotwebd_reload;
336 int server_cnt;
338 char httpd_chroot[PATH_MAX];
340 int unix_socket;
341 char unix_socket_name[PATH_MAX];
342 };
344 /*
345 * URL parameter for gotweb_link. NULL values and int set to -1 are
346 * implicitly ignored, and string are properly escaped.
347 */
348 struct gotweb_url {
349 int action;
350 int index_page;
351 int page;
352 const char *commit;
353 const char *previd;
354 const char *prevset;
355 const char *file;
356 const char *folder;
357 const char *headref;
358 const char *path;
359 };
361 struct querystring {
362 uint8_t action;
363 char *commit;
364 char *previd;
365 char *prevset;
366 char *file;
367 char *folder;
368 char *headref;
369 int index_page;
370 char *index_page_str;
371 char *path;
372 int page;
373 char *page_str;
374 };
376 struct querystring_keys {
377 const char *name;
378 int element;
379 };
381 struct action_keys {
382 const char *name;
383 int action;
384 };
386 enum querystring_elements {
387 ACTION,
388 COMMIT,
389 RFILE,
390 FOLDER,
391 HEADREF,
392 INDEX_PAGE,
393 PATH,
394 PAGE,
395 PREVID,
396 QSELEM__MAX,
397 };
399 enum query_actions {
400 BLAME,
401 BLOB,
402 BRIEFS,
403 COMMITS,
404 DIFF,
405 ERR,
406 INDEX,
407 SUMMARY,
408 TAG,
409 TAGS,
410 TREE,
411 ACTIONS__MAX,
412 };
414 extern struct gotwebd *gotwebd_env;
416 /* sockets.c */
417 void sockets(struct privsep *, struct privsep_proc *);
418 void sockets_shutdown(void);
419 void sockets_parse_sockets(struct gotwebd *);
420 void sockets_socket_accept(int, short, void *);
421 int sockets_privinit(struct gotwebd *, struct socket *);
423 /* gotweb.c */
424 const struct got_error *gotweb_render_content_type(struct request *,
425 const uint8_t *);
426 const struct got_error
427 *gotweb_render_content_type_file(struct request *, const uint8_t *, char *);
428 const struct got_error *gotweb_get_time_str(char **, time_t, int);
429 const struct got_error *gotweb_init_transport(struct transport **);
430 const struct got_error *gotweb_escape_html(char **, const char *);
431 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
432 __attribute__((__format__(printf, 3, 4)))
433 __attribute__((__nonnull__(3)));
434 void gotweb_free_repo_commit(struct repo_commit *);
435 void gotweb_free_repo_tag(struct repo_tag *);
436 void gotweb_process_request(struct request *);
437 void gotweb_free_transport(struct transport *);
439 /* parse.y */
440 int parse_config(const char *, struct gotwebd *);
441 int cmdline_symset(char *);
443 /* fcgi.c */
444 void fcgi_request(int, short, void *);
445 void fcgi_timeout(int, short, void *);
446 void fcgi_cleanup_request(struct request *);
447 void fcgi_create_end_record(struct request *);
448 void dump_fcgi_record(const char *, struct fcgi_record_header *);
449 int fcgi_vprintf(struct request *, const char *, va_list);
450 int fcgi_printf(struct request *, const char *, ...)
451 __attribute__((__format__(printf, 2, 3)))
452 __attribute__((__nonnull__(2)));
453 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
455 /* got_operations.c */
456 const struct got_error *got_get_repo_owner(char **, struct request *, char *);
457 const struct got_error *got_get_repo_age(char **, struct request *, char *,
458 const char *, int);
459 const struct got_error *got_get_repo_commits(struct request *, int);
460 const struct got_error *got_get_repo_tags(struct request *, int);
461 const struct got_error *got_get_repo_heads(struct request *);
462 const struct got_error *got_output_repo_diff(struct request *);
463 const struct got_error *got_output_repo_tree(struct request *);
464 const struct got_error *got_output_file_blob(struct request *);
465 const struct got_error *got_output_file_blame(struct request *);
467 /* config.c */
468 int config_setserver(struct gotwebd *, struct server *);
469 int config_getserver(struct gotwebd *, struct imsg *);
470 int config_setsock(struct gotwebd *, struct socket *);
471 int config_getsock(struct gotwebd *, struct imsg *);
472 int config_setfd(struct gotwebd *, struct socket *);
473 int config_getfd(struct gotwebd *, struct imsg *);
474 int config_getcfg(struct gotwebd *, struct imsg *);
475 int config_init(struct gotwebd *);