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 #include "tmpl.h"
29 #ifdef DEBUG
30 #define dprintf(x...) do { log_debug(x); } while(0)
31 #else
32 #define dprintf(x...)
33 #endif /* DEBUG */
35 #ifndef nitems
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 #endif
39 /* GOTWEBD DEFAULTS */
40 #define GOTWEBD_CONF "/etc/gotwebd.conf"
42 #define GOTWEBD_USER "www"
44 #define GOTWEBD_MAXDESCRSZ 1024
45 #define GOTWEBD_MAXCLONEURLSZ 1024
46 #define GOTWEBD_CACHESIZE 1024
47 #define GOTWEBD_MAXCLIENTS 1024
48 #define GOTWEBD_MAXTEXT 511
49 #define GOTWEBD_MAXNAME 64
50 #define GOTWEBD_MAXPORT 6
51 #define GOTWEBD_NUMPROC 3
52 #define GOTWEBD_MAXIFACE 16
53 #define GOTWEBD_REPO_CACHESIZE 4
55 /* GOTWEB DEFAULTS */
56 #define MAX_QUERYSTRING 2048
57 #define MAX_DOCUMENT_URI 255
58 #define MAX_SERVER_NAME 255
60 #define GOTWEB_GIT_DIR ".git"
62 #define D_HTTPD_CHROOT "/var/www"
63 #define D_UNIX_SOCKET "/run/gotweb.sock"
64 #define D_FCGI_PORT "9000"
65 #define D_GOTPATH "/got/public"
66 #define D_SITENAME "Gotweb"
67 #define D_SITEOWNER "Got Owner"
68 #define D_SITELINK "Repos"
69 #define D_GOTLOGO "got.png"
70 #define D_GOTURL "https://gameoftrees.org"
71 #define D_GOTWEBCSS "gotweb.css"
73 #define D_SHOWROWNER 1
74 #define D_SHOWSOWNER 1
75 #define D_SHOWAGE 1
76 #define D_SHOWDESC 1
77 #define D_SHOWURL 1
78 #define D_RESPECTEXPORTOK 0
79 #define D_MAXREPO 0
80 #define D_MAXREPODISP 25
81 #define D_MAXSLCOMMDISP 10
82 #define D_MAXCOMMITDISP 25
84 #define BUF 8192
86 #define TIMEOUT_DEFAULT 120
88 #define FCGI_CONTENT_SIZE 65535
89 #define FCGI_PADDING_SIZE 255
90 #define FCGI_RECORD_SIZE \
91 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
93 #define FCGI_ALIGNMENT 8
94 #define FCGI_ALIGN(n) \
95 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
97 #define FD_RESERVE 5
98 #define FD_NEEDED 6
100 #define FCGI_BEGIN_REQUEST 1
101 #define FCGI_ABORT_REQUEST 2
102 #define FCGI_END_REQUEST 3
103 #define FCGI_PARAMS 4
104 #define FCGI_STDIN 5
105 #define FCGI_STDOUT 6
106 #define FCGI_STDERR 7
107 #define FCGI_DATA 8
108 #define FCGI_GET_VALUES 9
109 #define FCGI_GET_VALUES_RESULT 10
110 #define FCGI_UNKNOWN_TYPE 11
111 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
113 #define FCGI_REQUEST_COMPLETE 0
114 #define FCGI_CANT_MPX_CONN 1
115 #define FCGI_OVERLOADED 2
116 #define FCGI_UNKNOWN_ROLE 3
118 #define GOTWEB_PACK_NUM_TEMPFILES 32
120 /* Forward declaration */
121 struct got_blob_object;
122 struct got_tree_entry;
124 enum imsg_type {
125 IMSG_CFG_SRV = IMSG_PROC_MAX,
126 IMSG_CFG_SOCK,
127 IMSG_CFG_FD,
128 IMSG_CFG_DONE,
129 IMSG_CTL_START,
130 };
132 struct env_val {
133 SLIST_ENTRY(env_val) entry;
134 char *val;
135 };
136 SLIST_HEAD(env_head, env_val);
138 struct fcgi_record_header {
139 uint8_t version;
140 uint8_t type;
141 uint16_t id;
142 uint16_t content_len;
143 uint8_t padding_len;
144 uint8_t reserved;
145 }__attribute__((__packed__));
147 struct repo_dir {
148 char *name;
149 char *owner;
150 char *description;
151 char *url;
152 char *age;
153 char *path;
154 };
156 struct repo_tag {
157 TAILQ_ENTRY(repo_tag) entry;
158 char *commit_id;
159 char *tag_name;
160 char *tag_commit;
161 char *commit_msg;
162 char *tagger;
163 time_t tagger_time;
164 };
166 struct repo_commit {
167 TAILQ_ENTRY(repo_commit) entry;
168 char *path;
169 char *refs_str;
170 char *commit_id; /* id_str1 */
171 char *parent_id; /* id_str2 */
172 char *tree_id;
173 char *author;
174 char *committer;
175 char *commit_msg;
176 time_t committer_time;
177 };
179 struct got_repository;
180 struct transport {
181 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
182 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
183 struct got_repository *repo;
184 struct repo_dir *repo_dir;
185 struct querystring *qs;
186 char *next_id;
187 char *prev_id;
188 unsigned int repos_total;
189 unsigned int next_disp;
190 unsigned int prev_disp;
191 unsigned int tag_count;
192 };
194 enum socket_priv_fds {
195 DIFF_FD_1,
196 DIFF_FD_2,
197 DIFF_FD_3,
198 DIFF_FD_4,
199 DIFF_FD_5,
200 BLAME_FD_1,
201 BLAME_FD_2,
202 BLAME_FD_3,
203 BLAME_FD_4,
204 BLAME_FD_5,
205 BLAME_FD_6,
206 BLOB_FD_1,
207 BLOB_FD_2,
208 PRIV_FDS__MAX,
209 };
211 struct template;
212 struct request {
213 struct socket *sock;
214 struct server *srv;
215 struct transport *t;
216 struct template *tp;
217 struct event ev;
218 struct event tmo;
220 uint16_t id;
221 int fd;
222 int priv_fd[PRIV_FDS__MAX];
224 uint8_t buf[FCGI_RECORD_SIZE];
225 size_t buf_pos;
226 size_t buf_len;
228 uint8_t outbuf[GOTWEBD_CACHESIZE];
229 size_t outbuf_len;
231 char querystring[MAX_QUERYSTRING];
232 char http_host[GOTWEBD_MAXTEXT];
233 char document_uri[MAX_DOCUMENT_URI];
234 char server_name[MAX_SERVER_NAME];
235 int https;
237 uint8_t request_started;
238 };
240 struct fcgi_begin_request_body {
241 uint16_t role;
242 uint8_t flags;
243 uint8_t reserved[5];
244 }__attribute__((__packed__));
246 struct fcgi_end_request_body {
247 uint32_t app_status;
248 uint8_t protocol_status;
249 uint8_t reserved[3];
250 }__attribute__((__packed__));
252 struct address {
253 TAILQ_ENTRY(address) entry;
254 struct sockaddr_storage ss;
255 int ipproto;
256 int prefixlen;
257 in_port_t port;
258 char ifname[IFNAMSIZ];
259 };
260 TAILQ_HEAD(addresslist, address);
262 struct cached_repo {
263 char path[PATH_MAX];
264 struct got_repository *repo;
265 };
267 struct server {
268 TAILQ_ENTRY(server) entry;
269 struct addresslist al;
271 struct cached_repo *cached_repos;
272 int ncached_repos;
274 char name[GOTWEBD_MAXTEXT];
276 char repos_path[PATH_MAX];
277 char site_name[GOTWEBD_MAXNAME];
278 char site_owner[GOTWEBD_MAXNAME];
279 char site_link[GOTWEBD_MAXTEXT];
280 char logo[GOTWEBD_MAXTEXT];
281 char logo_url[GOTWEBD_MAXTEXT];
282 char custom_css[PATH_MAX];
284 size_t max_repos;
285 size_t max_repos_display;
286 size_t max_commits_display;
288 int show_site_owner;
289 int show_repo_owner;
290 int show_repo_age;
291 int show_repo_description;
292 int show_repo_cloneurl;
293 int respect_exportok;
295 int unix_socket;
296 char unix_socket_name[PATH_MAX];
298 int fcgi_socket;
299 };
300 TAILQ_HEAD(serverlist, server);
302 enum client_action {
303 CLIENT_CONNECT,
304 CLIENT_DISCONNECT,
305 };
307 struct socket_conf {
308 struct address addr;
310 char name[GOTWEBD_MAXTEXT];
311 char srv_name[GOTWEBD_MAXTEXT];
313 int id;
314 int af_type;
315 char unix_socket_name[PATH_MAX];
316 in_port_t fcgi_socket_port;
317 };
319 struct socket {
320 TAILQ_ENTRY(socket) entry;
321 struct socket_conf conf;
323 int fd;
324 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
325 int priv_fd[PRIV_FDS__MAX];
327 struct event evt;
328 struct event ev;
329 struct event pause;
331 int client_status;
332 };
333 TAILQ_HEAD(socketlist, socket);
335 struct gotwebd {
336 struct serverlist servers;
337 struct socketlist sockets;
339 struct privsep *gotwebd_ps;
340 const char *gotwebd_conffile;
342 int gotwebd_debug;
343 int gotwebd_verbose;
344 int gotwebd_noaction;
346 uint16_t prefork_gotwebd;
347 int gotwebd_reload;
349 int server_cnt;
351 char httpd_chroot[PATH_MAX];
353 int unix_socket;
354 char unix_socket_name[PATH_MAX];
355 };
357 /*
358 * URL parameter for gotweb_link. NULL values and int set to -1 are
359 * implicitly ignored, and string are properly escaped.
360 */
361 struct gotweb_url {
362 int action;
363 int index_page;
364 int page;
365 const char *commit;
366 const char *previd;
367 const char *prevset;
368 const char *file;
369 const char *folder;
370 const char *headref;
371 const char *path;
372 };
374 struct querystring {
375 uint8_t action;
376 char *commit;
377 char *previd;
378 char *prevset;
379 char *file;
380 char *folder;
381 char *headref;
382 int index_page;
383 char *path;
384 int page;
385 };
387 struct querystring_keys {
388 const char *name;
389 int element;
390 };
392 struct action_keys {
393 const char *name;
394 int action;
395 };
397 enum querystring_elements {
398 ACTION,
399 COMMIT,
400 RFILE,
401 FOLDER,
402 HEADREF,
403 INDEX_PAGE,
404 PATH,
405 PAGE,
406 PREVID,
407 QSELEM__MAX,
408 };
410 enum query_actions {
411 BLAME,
412 BLOB,
413 BLOBRAW,
414 BRIEFS,
415 COMMITS,
416 DIFF,
417 ERR,
418 INDEX,
419 SUMMARY,
420 TAG,
421 TAGS,
422 TREE,
423 RSS,
424 ACTIONS__MAX,
425 };
427 enum gotweb_ref_tm {
428 TM_DIFF,
429 TM_LONG,
430 TM_RFC822,
431 };
433 extern struct gotwebd *gotwebd_env;
435 /* sockets.c */
436 void sockets(struct privsep *, struct privsep_proc *);
437 void sockets_shutdown(void);
438 void sockets_parse_sockets(struct gotwebd *);
439 void sockets_socket_accept(int, short, void *);
440 int sockets_privinit(struct gotwebd *, struct socket *);
442 /* gotweb.c */
443 const struct got_error *gotweb_render_content_type(struct request *,
444 const char *);
445 const struct got_error
446 *gotweb_render_content_type_file(struct request *, const char *,
447 const char *, const char *);
448 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
449 struct gotweb_url *, int *);
450 const struct got_error *gotweb_get_time_str(char **, time_t, int);
451 const struct got_error *gotweb_init_transport(struct transport **);
452 const struct got_error *gotweb_escape_html(char **, const char *);
453 const char *gotweb_action_name(int);
454 int gotweb_render_url(struct request *, struct gotweb_url *);
455 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
456 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
457 __attribute__((__format__(printf, 3, 4)))
458 __attribute__((__nonnull__(3)));
459 void gotweb_free_repo_commit(struct repo_commit *);
460 void gotweb_free_repo_tag(struct repo_tag *);
461 void gotweb_process_request(struct request *);
462 void gotweb_free_transport(struct transport *);
464 /* pages.tmpl */
465 int gotweb_render_header(struct template *);
466 int gotweb_render_footer(struct template *);
467 int gotweb_render_repo_table_hdr(struct template *);
468 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
469 int gotweb_render_briefs(struct template *);
470 int gotweb_render_navs(struct template *);
471 int gotweb_render_commits(struct template *);
472 int gotweb_render_blob(struct template *, struct got_blob_object *);
473 int gotweb_render_tree(struct template *);
474 int gotweb_render_rss(struct template *);
476 /* parse.y */
477 int parse_config(const char *, struct gotwebd *);
478 int cmdline_symset(char *);
480 /* fcgi.c */
481 void fcgi_request(int, short, void *);
482 void fcgi_timeout(int, short, void *);
483 void fcgi_cleanup_request(struct request *);
484 void fcgi_create_end_record(struct request *);
485 void dump_fcgi_record(const char *, struct fcgi_record_header *);
486 int fcgi_puts(struct template *, const char *);
487 int fcgi_putc(struct template *, int);
488 int fcgi_vprintf(struct request *, const char *, va_list);
489 int fcgi_printf(struct request *, const char *, ...)
490 __attribute__((__format__(printf, 2, 3)))
491 __attribute__((__nonnull__(2)));
492 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
494 /* got_operations.c */
495 const struct got_error *got_get_repo_owner(char **, struct request *);
496 const struct got_error *got_get_repo_age(char **, struct request *,
497 const char *, int);
498 const struct got_error *got_get_repo_commits(struct request *, int);
499 const struct got_error *got_get_repo_tags(struct request *, int);
500 const struct got_error *got_get_repo_heads(struct request *);
501 const struct got_error *got_output_repo_diff(struct request *);
502 int got_output_repo_tree(struct request *,
503 int (*)(struct template *, struct got_tree_entry *));
504 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
505 int *, int *, struct request *);
506 const struct got_error *got_output_file_blob(struct request *);
507 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
508 int (*)(struct template *, const char *, size_t));
509 const struct got_error *got_output_file_blame(struct request *);
511 /* config.c */
512 int config_setserver(struct gotwebd *, struct server *);
513 int config_getserver(struct gotwebd *, struct imsg *);
514 int config_setsock(struct gotwebd *, struct socket *);
515 int config_getsock(struct gotwebd *, struct imsg *);
516 int config_setfd(struct gotwebd *, struct socket *);
517 int config_getfd(struct gotwebd *, struct imsg *);
518 int config_getcfg(struct gotwebd *, struct imsg *);
519 int config_init(struct gotwebd *);