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>
28 #define dprintf(x...) do { log_debug(x); } while(0)
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 /* GOTWEBD DEFAULTS */
38 #define GOTWEBD_CONF "/etc/gotwebd.conf"
40 #define GOTWEBD_USER "www"
42 #define GOTWEBD_MAXDESCRSZ 1024
43 #define GOTWEBD_MAXCLONEURLSZ 1024
44 #define GOTWEBD_CACHESIZE 1024
45 #define GOTWEBD_MAXCLIENTS 1024
46 #define GOTWEBD_MAXTEXT 511
47 #define GOTWEBD_MAXNAME 64
48 #define GOTWEBD_MAXPORT 6
49 #define GOTWEBD_NUMPROC 3
50 #define GOTWEBD_MAXIFACE 16
51 #define GOTWEBD_REPO_CACHESIZE 4
54 #define MAX_QUERYSTRING 2048
55 #define MAX_DOCUMENT_URI 255
56 #define MAX_SERVER_NAME 255
58 #define GOTWEB_GIT_DIR ".git"
60 #define D_HTTPD_CHROOT "/var/www"
61 #define D_UNIX_SOCKET "/run/gotweb.sock"
62 #define D_FCGI_PORT "9000"
63 #define D_GOTPATH "/got/public"
64 #define D_SITENAME "Gotweb"
65 #define D_SITEOWNER "Got Owner"
66 #define D_SITELINK "Repos"
67 #define D_GOTLOGO "got.png"
68 #define D_GOTURL "https://gameoftrees.org"
69 #define D_GOTWEBCSS "gotweb.css"
71 #define D_SHOWROWNER 1
72 #define D_SHOWSOWNER 1
76 #define D_RESPECTEXPORTOK 0
78 #define D_MAXREPODISP 25
79 #define D_MAXSLCOMMDISP 10
80 #define D_MAXCOMMITDISP 25
84 #define TIMEOUT_DEFAULT 120
86 #define FCGI_CONTENT_SIZE 65535
87 #define FCGI_PADDING_SIZE 255
88 #define FCGI_RECORD_SIZE \
89 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
91 #define FCGI_ALIGNMENT 8
92 #define FCGI_ALIGN(n) \
93 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
98 #define FCGI_BEGIN_REQUEST 1
99 #define FCGI_ABORT_REQUEST 2
100 #define FCGI_END_REQUEST 3
101 #define FCGI_PARAMS 4
103 #define FCGI_STDOUT 6
104 #define FCGI_STDERR 7
106 #define FCGI_GET_VALUES 9
107 #define FCGI_GET_VALUES_RESULT 10
108 #define FCGI_UNKNOWN_TYPE 11
109 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
111 #define FCGI_REQUEST_COMPLETE 0
112 #define FCGI_CANT_MPX_CONN 1
113 #define FCGI_OVERLOADED 2
114 #define FCGI_UNKNOWN_ROLE 3
116 #define GOTWEB_PACK_NUM_TEMPFILES 32
118 /* Forward declaration */
119 struct got_blob_object;
120 struct got_tree_entry;
123 IMSG_CFG_SRV = IMSG_PROC_MAX,
131 SLIST_ENTRY(env_val) entry;
134 SLIST_HEAD(env_head, env_val);
136 struct fcgi_record_header {
140 uint16_t content_len;
143 }__attribute__((__packed__));
155 TAILQ_ENTRY(repo_tag) entry;
165 TAILQ_ENTRY(repo_commit) entry;
168 char *commit_id; /* id_str1 */
169 char *parent_id; /* id_str2 */
174 time_t committer_time;
177 struct got_repository;
179 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
180 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
181 struct got_repository *repo;
182 struct repo_dir *repo_dir;
183 struct querystring *qs;
186 unsigned int repos_total;
187 unsigned int next_disp;
188 unsigned int prev_disp;
189 unsigned int tag_count;
192 enum socket_priv_fds {
220 int priv_fd[PRIV_FDS__MAX];
222 uint8_t buf[FCGI_RECORD_SIZE];
226 uint8_t outbuf[GOTWEBD_CACHESIZE];
229 char querystring[MAX_QUERYSTRING];
230 char http_host[GOTWEBD_MAXTEXT];
231 char document_uri[MAX_DOCUMENT_URI];
232 char server_name[MAX_SERVER_NAME];
235 uint8_t request_started;
238 struct fcgi_begin_request_body {
242 }__attribute__((__packed__));
244 struct fcgi_end_request_body {
246 uint8_t protocol_status;
248 }__attribute__((__packed__));
251 TAILQ_ENTRY(address) entry;
252 struct sockaddr_storage ss;
256 char ifname[IFNAMSIZ];
258 TAILQ_HEAD(addresslist, address);
262 struct got_repository *repo;
266 TAILQ_ENTRY(server) entry;
267 struct addresslist al;
269 struct cached_repo *cached_repos;
272 char name[GOTWEBD_MAXTEXT];
274 char repos_path[PATH_MAX];
275 char site_name[GOTWEBD_MAXNAME];
276 char site_owner[GOTWEBD_MAXNAME];
277 char site_link[GOTWEBD_MAXTEXT];
278 char logo[GOTWEBD_MAXTEXT];
279 char logo_url[GOTWEBD_MAXTEXT];
280 char custom_css[PATH_MAX];
283 size_t max_repos_display;
284 size_t max_commits_display;
289 int show_repo_description;
290 int show_repo_cloneurl;
291 int respect_exportok;
294 char unix_socket_name[PATH_MAX];
298 TAILQ_HEAD(serverlist, server);
308 char name[GOTWEBD_MAXTEXT];
309 char srv_name[GOTWEBD_MAXTEXT];
313 char unix_socket_name[PATH_MAX];
314 in_port_t fcgi_socket_port;
318 TAILQ_ENTRY(socket) entry;
319 struct socket_conf conf;
322 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
323 int priv_fd[PRIV_FDS__MAX];
331 TAILQ_HEAD(socketlist, socket);
334 struct serverlist servers;
335 struct socketlist sockets;
337 struct privsep *gotwebd_ps;
338 const char *gotwebd_conffile;
342 int gotwebd_noaction;
344 uint16_t prefork_gotwebd;
349 char httpd_chroot[PATH_MAX];
352 char unix_socket_name[PATH_MAX];
356 * URL parameter for gotweb_link. NULL values and int set to -1 are
357 * implicitly ignored, and string are properly escaped.
385 struct querystring_keys {
395 enum querystring_elements {
431 extern struct gotwebd *gotwebd_env;
434 void sockets(struct privsep *, struct privsep_proc *);
435 void sockets_shutdown(void);
436 void sockets_parse_sockets(struct gotwebd *);
437 void sockets_socket_accept(int, short, void *);
438 int sockets_privinit(struct gotwebd *, struct socket *);
441 const struct got_error *gotweb_render_content_type(struct request *,
443 const struct got_error
444 *gotweb_render_content_type_file(struct request *, const char *,
445 const char *, const char *);
446 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
447 struct gotweb_url *, int *);
448 const struct got_error *gotweb_get_time_str(char **, time_t, int);
449 const struct got_error *gotweb_init_transport(struct transport **);
450 const struct got_error *gotweb_escape_html(char **, const char *);
451 const char *gotweb_action_name(int);
452 int gotweb_render_url(struct request *, struct gotweb_url *);
453 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
454 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
455 __attribute__((__format__(printf, 3, 4)))
456 __attribute__((__nonnull__(3)));
457 void gotweb_free_repo_commit(struct repo_commit *);
458 void gotweb_free_repo_tag(struct repo_tag *);
459 void gotweb_process_request(struct request *);
460 void gotweb_free_transport(struct transport *);
463 int gotweb_render_header(struct template *);
464 int gotweb_render_footer(struct template *);
465 int gotweb_render_repo_table_hdr(struct template *);
466 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
467 int gotweb_render_briefs(struct template *);
468 int gotweb_render_navs(struct template *);
469 int gotweb_render_commits(struct template *);
470 int gotweb_render_blob(struct template *, struct got_blob_object *);
471 int gotweb_render_tree(struct template *);
472 int gotweb_render_rss(struct template *);
475 int parse_config(const char *, struct gotwebd *);
476 int cmdline_symset(char *);
479 void fcgi_request(int, short, void *);
480 void fcgi_timeout(int, short, void *);
481 void fcgi_cleanup_request(struct request *);
482 void fcgi_create_end_record(struct request *);
483 void dump_fcgi_record(const char *, struct fcgi_record_header *);
484 int fcgi_puts(struct template *, const char *);
485 int fcgi_putc(struct template *, int);
486 int fcgi_vprintf(struct request *, const char *, va_list);
487 int fcgi_printf(struct request *, const char *, ...)
488 __attribute__((__format__(printf, 2, 3)))
489 __attribute__((__nonnull__(2)));
490 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
492 /* got_operations.c */
493 const struct got_error *got_get_repo_owner(char **, struct request *);
494 const struct got_error *got_get_repo_age(char **, struct request *,
496 const struct got_error *got_get_repo_commits(struct request *, int);
497 const struct got_error *got_get_repo_tags(struct request *, int);
498 const struct got_error *got_get_repo_heads(struct request *);
499 const struct got_error *got_output_repo_diff(struct request *);
500 int got_output_repo_tree(struct request *,
501 int (*)(struct template *, struct got_tree_entry *));
502 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
503 int *, int *, struct request *);
504 const struct got_error *got_output_file_blob(struct request *);
505 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
506 int (*)(struct template *, const char *, size_t));
507 const struct got_error *got_output_file_blame(struct request *);
510 int config_setserver(struct gotwebd *, struct server *);
511 int config_getserver(struct gotwebd *, struct imsg *);
512 int config_setsock(struct gotwebd *, struct socket *);
513 int config_getsock(struct gotwebd *, struct imsg *);
514 int config_setfd(struct gotwebd *, struct socket *);
515 int config_getfd(struct gotwebd *, struct imsg *);
516 int config_getcfg(struct gotwebd *, struct imsg *);
517 int config_init(struct gotwebd *);