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>
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.
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.
20 #include <netinet/in.h>
22 #include <sys/queue.h>
30 #define dprintf(x...) do { log_debug(x); } while(0)
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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_REPO_CACHESIZE 4
55 #define MAX_QUERYSTRING 2048
56 #define MAX_DOCUMENT_URI 255
57 #define MAX_SERVER_NAME 255
59 #define GOTWEB_GIT_DIR ".git"
61 #define D_HTTPD_CHROOT "/var/www"
62 #define D_UNIX_SOCKET "/run/gotweb.sock"
63 #define D_FCGI_PORT "9000"
64 #define D_GOTPATH "/got/public"
65 #define D_SITENAME "Gotweb"
66 #define D_SITEOWNER "Got Owner"
67 #define D_SITELINK "Repos"
68 #define D_GOTLOGO "got.png"
69 #define D_GOTURL "https://gameoftrees.org"
70 #define D_GOTWEBCSS "gotweb.css"
72 #define D_SHOWROWNER 1
73 #define D_SHOWSOWNER 1
77 #define D_RESPECTEXPORTOK 0
79 #define D_MAXREPODISP 25
80 #define D_MAXSLCOMMDISP 10
81 #define D_MAXCOMMITDISP 25
85 #define TIMEOUT_DEFAULT 120
87 #define FCGI_CONTENT_SIZE 65535
88 #define FCGI_PADDING_SIZE 255
89 #define FCGI_RECORD_SIZE \
90 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
92 #define FCGI_ALIGNMENT 8
93 #define FCGI_ALIGN(n) \
94 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
99 #define FCGI_BEGIN_REQUEST 1
100 #define FCGI_ABORT_REQUEST 2
101 #define FCGI_END_REQUEST 3
102 #define FCGI_PARAMS 4
104 #define FCGI_STDOUT 6
105 #define FCGI_STDERR 7
107 #define FCGI_GET_VALUES 9
108 #define FCGI_GET_VALUES_RESULT 10
109 #define FCGI_UNKNOWN_TYPE 11
110 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
112 #define FCGI_REQUEST_COMPLETE 0
113 #define FCGI_CANT_MPX_CONN 1
114 #define FCGI_OVERLOADED 2
115 #define FCGI_UNKNOWN_ROLE 3
117 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
119 /* Forward declaration */
120 struct got_blob_object;
121 struct got_tree_entry;
122 struct got_reflist_head;
125 IMSG_CFG_SRV = IMSG_PROC_MAX,
133 SLIST_ENTRY(env_val) entry;
136 SLIST_HEAD(env_head, env_val);
138 struct fcgi_record_header {
142 uint16_t content_len;
145 }__attribute__((__packed__));
151 char datebuf[11]; /* YYYY-MM-DD + NUL */
164 TAILQ_ENTRY(repo_tag) entry;
174 TAILQ_ENTRY(repo_commit) entry;
177 char *commit_id; /* id_str1 */
178 char *parent_id; /* id_str2 */
183 time_t committer_time;
186 struct got_repository;
188 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
189 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
190 struct got_reflist_head refs;
191 struct got_repository *repo;
192 struct repo_dir *repo_dir;
193 struct querystring *qs;
197 unsigned int repos_total;
198 unsigned int next_disp;
199 unsigned int prev_disp;
200 unsigned int tag_count;
201 const struct got_error *error;
202 struct got_blob_object *blob;
205 struct dirent **repos;
209 enum socket_priv_fds {
237 int priv_fd[PRIV_FDS__MAX];
239 uint8_t buf[FCGI_RECORD_SIZE];
243 uint8_t outbuf[GOTWEBD_CACHESIZE];
245 char querystring[MAX_QUERYSTRING];
246 char document_uri[MAX_DOCUMENT_URI];
247 char server_name[MAX_SERVER_NAME];
250 uint8_t request_started;
253 struct fcgi_begin_request_body {
257 }__attribute__((__packed__));
259 struct fcgi_end_request_body {
261 uint8_t protocol_status;
263 }__attribute__((__packed__));
266 TAILQ_ENTRY(address) entry;
267 struct sockaddr_storage ss;
273 char ifname[IFNAMSIZ];
275 TAILQ_HEAD(addresslist, address);
279 struct got_repository *repo;
283 TAILQ_ENTRY(server) entry;
284 struct addresslist al;
286 struct cached_repo *cached_repos;
289 char name[GOTWEBD_MAXTEXT];
291 char repos_path[PATH_MAX];
292 char site_name[GOTWEBD_MAXNAME];
293 char site_owner[GOTWEBD_MAXNAME];
294 char site_link[GOTWEBD_MAXTEXT];
295 char logo[GOTWEBD_MAXTEXT];
296 char logo_url[GOTWEBD_MAXTEXT];
297 char custom_css[PATH_MAX];
300 size_t max_repos_display;
301 size_t max_commits_display;
306 int show_repo_description;
307 int show_repo_cloneurl;
308 int respect_exportok;
311 char unix_socket_name[PATH_MAX];
315 TAILQ_HEAD(serverlist, server);
325 char name[GOTWEBD_MAXTEXT];
326 char srv_name[GOTWEBD_MAXTEXT];
330 char unix_socket_name[PATH_MAX];
331 in_port_t fcgi_socket_port;
335 TAILQ_ENTRY(socket) entry;
336 struct socket_conf conf;
339 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
340 int priv_fd[PRIV_FDS__MAX];
348 TAILQ_HEAD(socketlist, socket);
351 struct serverlist servers;
352 struct socketlist sockets;
354 struct privsep *gotwebd_ps;
355 const char *gotwebd_conffile;
359 int gotwebd_noaction;
361 uint16_t prefork_gotwebd;
366 char httpd_chroot[PATH_MAX];
369 char unix_socket_name[PATH_MAX];
373 * URL parameter for gotweb_render_url. NULL values and int set to -1
374 * are implicitly ignored, and string are properly escaped.
402 struct querystring_keys {
412 enum querystring_elements {
441 extern struct gotwebd *gotwebd_env;
443 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
444 struct blame_line *, int, int);
447 void sockets(struct privsep *, struct privsep_proc *);
448 void sockets_shutdown(void);
449 void sockets_parse_sockets(struct gotwebd *);
450 void sockets_socket_accept(int, short, void *);
451 int sockets_privinit(struct gotwebd *, struct socket *);
454 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
455 struct gotweb_url *, int *);
456 int gotweb_render_age(struct template *, time_t);
457 const struct got_error *gotweb_init_transport(struct transport **);
458 const char *gotweb_action_name(int);
459 int gotweb_render_url(struct request *, struct gotweb_url *);
460 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
461 void gotweb_free_repo_commit(struct repo_commit *);
462 void gotweb_free_repo_tag(struct repo_tag *);
463 void gotweb_process_request(struct request *);
464 void gotweb_free_transport(struct transport *);
467 int gotweb_render_page(struct template *, int (*)(struct template *));
468 int gotweb_render_error(struct template *);
469 int gotweb_render_repo_table_hdr(struct template *);
470 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
471 int gotweb_render_briefs(struct template *);
472 int gotweb_render_navs(struct template *);
473 int gotweb_render_commits(struct template *);
474 int gotweb_render_blob(struct template *);
475 int gotweb_render_tree(struct template *);
476 int gotweb_render_tags(struct template *);
477 int gotweb_render_tag(struct template *);
478 int gotweb_render_diff(struct template *);
479 int gotweb_render_branches(struct template *, struct got_reflist_head *);
480 int gotweb_render_summary(struct template *);
481 int gotweb_render_blame(struct template *);
482 int gotweb_render_rss(struct template *);
485 int parse_config(const char *, struct gotwebd *);
486 int cmdline_symset(char *);
489 void fcgi_request(int, short, void *);
490 void fcgi_timeout(int, short, void *);
491 void fcgi_cleanup_request(struct request *);
492 void fcgi_create_end_record(struct request *);
493 void dump_fcgi_record(const char *, struct fcgi_record_header *);
494 int fcgi_write(void *, const void *, size_t);
496 /* got_operations.c */
497 const struct got_error *got_gotweb_closefile(FILE *);
498 const struct got_error *got_get_repo_owner(char **, struct request *);
499 const struct got_error *got_get_repo_age(time_t *, struct request *,
501 const struct got_error *got_get_repo_commits(struct request *, size_t);
502 const struct got_error *got_get_repo_tags(struct request *, size_t);
503 const struct got_error *got_get_repo_heads(struct request *);
504 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
505 int got_output_repo_tree(struct request *,
506 int (*)(struct template *, struct got_tree_entry *));
507 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
508 int *, int *, struct request *);
509 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
510 int (*)(struct template *, const char *, size_t));
511 const struct got_error *got_output_file_blame(struct request *,
512 got_render_blame_line_cb);
515 int config_setserver(struct gotwebd *, struct server *);
516 int config_getserver(struct gotwebd *, struct imsg *);
517 int config_setsock(struct gotwebd *, struct socket *);
518 int config_getsock(struct gotwebd *, struct imsg *);
519 int config_setfd(struct gotwebd *, struct socket *);
520 int config_getfd(struct gotwebd *, struct imsg *);
521 int config_getcfg(struct gotwebd *, struct imsg *);
522 int config_init(struct gotwebd *);