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_SOCK_FILENO 3
54 #define PROC_MAX_INSTANCES 32
57 #define MAX_QUERYSTRING 2048
58 #define MAX_DOCUMENT_URI 255
59 #define MAX_SERVER_NAME 255
61 #define GOTWEB_GIT_DIR ".git"
63 #define D_HTTPD_CHROOT "/var/www"
64 #define D_UNIX_SOCKET "/run/gotweb.sock"
65 #define D_FCGI_PORT "9000"
66 #define D_GOTPATH "/got/public"
67 #define D_SITENAME "Gotweb"
68 #define D_SITEOWNER "Got Owner"
69 #define D_SITELINK "Repos"
70 #define D_GOTLOGO "got.png"
71 #define D_GOTURL "https://gameoftrees.org"
72 #define D_GOTWEBCSS "gotweb.css"
74 #define D_SHOWROWNER 1
75 #define D_SHOWSOWNER 1
79 #define D_RESPECTEXPORTOK 0
80 #define D_MAXREPODISP 25
81 #define D_MAXSLCOMMDISP 10
82 #define D_MAXCOMMITDISP 25
83 #define D_MAXSLTAGDISP 3
87 #define TIMEOUT_DEFAULT 120
89 #define FCGI_CONTENT_SIZE 65535
90 #define FCGI_PADDING_SIZE 255
91 #define FCGI_RECORD_SIZE \
92 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
94 #define FCGI_ALIGNMENT 8
95 #define FCGI_ALIGN(n) \
96 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
101 #define FCGI_BEGIN_REQUEST 1
102 #define FCGI_ABORT_REQUEST 2
103 #define FCGI_END_REQUEST 3
104 #define FCGI_PARAMS 4
106 #define FCGI_STDOUT 6
107 #define FCGI_STDERR 7
109 #define FCGI_GET_VALUES 9
110 #define FCGI_GET_VALUES_RESULT 10
111 #define FCGI_UNKNOWN_TYPE 11
112 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
114 #define FCGI_REQUEST_COMPLETE 0
115 #define FCGI_CANT_MPX_CONN 1
116 #define FCGI_OVERLOADED 2
117 #define FCGI_UNKNOWN_ROLE 3
119 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
121 /* Forward declaration */
122 struct got_blob_object
;
123 struct got_tree_entry
;
124 struct got_reflist_head
;
136 void (*handler
)(int, short, void *);
142 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
145 SLIST_ENTRY(env_val
) entry
;
148 SLIST_HEAD(env_head
, env_val
);
150 struct fcgi_record_header
{
154 uint16_t content_len
;
157 }__attribute__((__packed__
));
163 char datebuf
[11]; /* YYYY-MM-DD + NUL */
176 TAILQ_ENTRY(repo_tag
) entry
;
186 TAILQ_ENTRY(repo_commit
) entry
;
189 char *commit_id
; /* id_str1 */
190 char *parent_id
; /* id_str2 */
195 time_t committer_time
;
198 struct got_repository
;
200 TAILQ_HEAD(repo_commits_head
, repo_commit
) repo_commits
;
201 TAILQ_HEAD(repo_tags_head
, repo_tag
) repo_tags
;
202 struct got_reflist_head refs
;
203 struct got_repository
*repo
;
204 struct repo_dir
*repo_dir
;
205 struct querystring
*qs
;
208 unsigned int repos_total
;
209 unsigned int next_disp
;
210 unsigned int prev_disp
;
211 unsigned int tag_count
;
212 const struct got_error
*error
;
213 struct got_blob_object
*blob
;
216 struct dirent
**repos
;
220 enum socket_priv_fds
{
248 int priv_fd
[PRIV_FDS__MAX
];
250 uint8_t buf
[FCGI_RECORD_SIZE
];
254 uint8_t outbuf
[GOTWEBD_CACHESIZE
];
256 char querystring
[MAX_QUERYSTRING
];
257 char document_uri
[MAX_DOCUMENT_URI
];
258 char server_name
[MAX_SERVER_NAME
];
261 uint8_t request_started
;
264 struct fcgi_begin_request_body
{
268 }__attribute__((__packed__
));
270 struct fcgi_end_request_body
{
272 uint8_t protocol_status
;
274 }__attribute__((__packed__
));
277 TAILQ_ENTRY(address
) entry
;
278 struct sockaddr_storage ss
;
284 char ifname
[IFNAMSIZ
];
286 TAILQ_HEAD(addresslist
, address
);
289 TAILQ_ENTRY(server
) entry
;
290 struct addresslist al
;
292 char name
[GOTWEBD_MAXTEXT
];
294 char repos_path
[PATH_MAX
];
295 char site_name
[GOTWEBD_MAXNAME
];
296 char site_owner
[GOTWEBD_MAXNAME
];
297 char site_link
[GOTWEBD_MAXTEXT
];
298 char logo
[GOTWEBD_MAXTEXT
];
299 char logo_url
[GOTWEBD_MAXTEXT
];
300 char custom_css
[PATH_MAX
];
302 size_t max_repos_display
;
303 size_t max_commits_display
;
304 size_t summary_commits_display
;
305 size_t summary_tags_display
;
310 int show_repo_description
;
311 int show_repo_cloneurl
;
312 int respect_exportok
;
315 char unix_socket_name
[PATH_MAX
];
319 TAILQ_HEAD(serverlist
, server
);
329 char name
[GOTWEBD_MAXTEXT
];
330 char srv_name
[GOTWEBD_MAXTEXT
];
334 char unix_socket_name
[PATH_MAX
];
335 in_port_t fcgi_socket_port
;
339 TAILQ_ENTRY(socket
) entry
;
340 struct socket_conf conf
;
343 int pack_fds
[GOTWEB_PACK_NUM_TEMPFILES
];
344 int priv_fd
[PRIV_FDS__MAX
];
352 TAILQ_HEAD(socketlist
, socket
);
356 struct serverlist servers
;
357 struct socketlist sockets
;
359 const char *gotwebd_conffile
;
364 struct imsgev
*iev_parent
;
365 struct imsgev
*iev_server
;
370 uint16_t prefork_gotwebd
;
375 char httpd_chroot
[PATH_MAX
];
378 char unix_socket_name
[PATH_MAX
];
382 * URL parameter for gotweb_render_url. NULL values and int set to -1
383 * are implicitly ignored, and string are properly escaped.
409 struct querystring_keys
{
419 enum querystring_elements
{
446 extern struct gotwebd
*gotwebd_env
;
448 typedef int (*got_render_blame_line_cb
)(struct template *, const char *,
449 struct blame_line
*, int, int);
452 void imsg_event_add(struct imsgev
*);
453 int imsg_compose_event(struct imsgev
*, uint16_t, uint32_t,
454 pid_t
, int, const void *, uint16_t);
455 int main_compose_sockets(struct gotwebd
*, uint32_t, int,
456 const void *, uint16_t);
457 int sockets_compose_main(struct gotwebd
*, uint32_t,
458 const void *, uint16_t);
461 void sockets(struct gotwebd
*, int);
462 void sockets_parse_sockets(struct gotwebd
*);
463 void sockets_socket_accept(int, short, void *);
464 int sockets_privinit(struct gotwebd
*, struct socket
*);
467 void gotweb_index_navs(struct request
*, struct gotweb_url
*, int *,
468 struct gotweb_url
*, int *);
469 int gotweb_render_age(struct template *, time_t);
470 const struct got_error
*gotweb_init_transport(struct transport
**);
471 const char *gotweb_action_name(int);
472 int gotweb_render_url(struct request
*, struct gotweb_url
*);
473 int gotweb_render_absolute_url(struct request
*, struct gotweb_url
*);
474 void gotweb_free_repo_commit(struct repo_commit
*);
475 void gotweb_free_repo_tag(struct repo_tag
*);
476 void gotweb_process_request(struct request
*);
477 void gotweb_free_transport(struct transport
*);
480 int gotweb_render_page(struct template *, int (*)(struct template *));
481 int gotweb_render_error(struct template *);
482 int gotweb_render_repo_table_hdr(struct template *);
483 int gotweb_render_repo_fragment(struct template *, struct repo_dir
*);
484 int gotweb_render_briefs(struct template *);
485 int gotweb_render_navs(struct template *);
486 int gotweb_render_commits(struct template *);
487 int gotweb_render_blob(struct template *);
488 int gotweb_render_tree(struct template *);
489 int gotweb_render_tags(struct template *);
490 int gotweb_render_tag(struct template *);
491 int gotweb_render_diff(struct template *);
492 int gotweb_render_branches(struct template *, struct got_reflist_head
*);
493 int gotweb_render_summary(struct template *);
494 int gotweb_render_blame(struct template *);
495 int gotweb_render_patch(struct template *);
496 int gotweb_render_rss(struct template *);
499 int parse_config(const char *, struct gotwebd
*);
500 int cmdline_symset(char *);
503 void fcgi_request(int, short, void *);
504 void fcgi_timeout(int, short, void *);
505 void fcgi_cleanup_request(struct request
*);
506 void fcgi_create_end_record(struct request
*);
507 void dump_fcgi_record(const char *, struct fcgi_record_header
*);
508 int fcgi_write(void *, const void *, size_t);
510 /* got_operations.c */
511 const struct got_error
*got_gotweb_closefile(FILE *);
512 const struct got_error
*got_get_repo_owner(char **, struct request
*);
513 const struct got_error
*got_get_repo_age(time_t *, struct request
*,
515 const struct got_error
*got_get_repo_commits(struct request
*, size_t);
516 const struct got_error
*got_get_repo_tags(struct request
*, size_t);
517 const struct got_error
*got_get_repo_heads(struct request
*);
518 const struct got_error
*got_open_diff_for_output(FILE **, struct request
*);
519 int got_output_repo_tree(struct request
*, char **,
520 int (*)(struct template *, struct got_tree_entry
*));
521 const struct got_error
*got_open_blob_for_output(struct got_blob_object
**,
522 int *, int *, struct request
*, const char *, const char *, const char *);
523 int got_output_blob_by_lines(struct template *, struct got_blob_object
*,
524 int (*)(struct template *, const char *, size_t));
525 const struct got_error
*got_output_file_blame(struct request
*,
526 got_render_blame_line_cb
);
529 int config_setserver(struct gotwebd
*, struct server
*);
530 int config_getserver(struct gotwebd
*, struct imsg
*);
531 int config_setsock(struct gotwebd
*, struct socket
*);
532 int config_getsock(struct gotwebd
*, struct imsg
*);
533 int config_setfd(struct gotwebd
*, struct socket
*);
534 int config_getfd(struct gotwebd
*, struct imsg
*);
535 int config_getcfg(struct gotwebd
*, struct imsg
*);
536 int config_init(struct gotwebd
*);
539 void log_init(int, int);
540 void log_procinit(const char *);
541 void log_setverbose(int);
542 int log_getverbose(void);
543 void log_warn(const char *, ...)
544 __attribute__((__format__ (printf
, 1, 2)));
545 void log_warnx(const char *, ...)
546 __attribute__((__format__ (printf
, 1, 2)));
547 void log_info(const char *, ...)
548 __attribute__((__format__ (printf
, 1, 2)));
549 void log_debug(const char *, ...)
550 __attribute__((__format__ (printf
, 1, 2)));
551 void logit(int, const char *, ...)
552 __attribute__((__format__ (printf
, 2, 3)));
553 void vlog(int, const char *, va_list)
554 __attribute__((__format__ (printf
, 2, 0)));
555 __dead
void fatal(const char *, ...)
556 __attribute__((__format__ (printf
, 1, 2)));
557 __dead
void fatalx(const char *, ...)
558 __attribute__((__format__ (printf
, 1, 2)));