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_SOCK_FILENO 3
54 #define PROC_MAX_INSTANCES 32
56 /* GOTWEB DEFAULTS */
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
76 #define D_SHOWAGE 1
77 #define D_SHOWDESC 1
78 #define D_SHOWURL 1
79 #define D_RESPECTEXPORTOK 0
80 #define D_MAXREPO 0
81 #define D_MAXREPODISP 25
82 #define D_MAXSLCOMMDISP 10
83 #define D_MAXCOMMITDISP 25
84 #define D_MAXSLTAGDISP 3
86 #define BUF 8192
88 #define TIMEOUT_DEFAULT 120
90 #define FCGI_CONTENT_SIZE 65535
91 #define FCGI_PADDING_SIZE 255
92 #define FCGI_RECORD_SIZE \
93 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
95 #define FCGI_ALIGNMENT 8
96 #define FCGI_ALIGN(n) \
97 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
99 #define FD_RESERVE 5
100 #define FD_NEEDED 6
102 #define FCGI_BEGIN_REQUEST 1
103 #define FCGI_ABORT_REQUEST 2
104 #define FCGI_END_REQUEST 3
105 #define FCGI_PARAMS 4
106 #define FCGI_STDIN 5
107 #define FCGI_STDOUT 6
108 #define FCGI_STDERR 7
109 #define FCGI_DATA 8
110 #define FCGI_GET_VALUES 9
111 #define FCGI_GET_VALUES_RESULT 10
112 #define FCGI_UNKNOWN_TYPE 11
113 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
115 #define FCGI_REQUEST_COMPLETE 0
116 #define FCGI_CANT_MPX_CONN 1
117 #define FCGI_OVERLOADED 2
118 #define FCGI_UNKNOWN_ROLE 3
120 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
122 /* Forward declaration */
123 struct got_blob_object;
124 struct got_tree_entry;
125 struct got_reflist_head;
127 enum imsg_type {
128 IMSG_CFG_SRV,
129 IMSG_CFG_SOCK,
130 IMSG_CFG_FD,
131 IMSG_CFG_DONE,
132 IMSG_CTL_START,
133 };
135 struct imsgev {
136 struct imsgbuf ibuf;
137 void (*handler)(int, short, void *);
138 struct event ev;
139 void *data;
140 short events;
141 };
143 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
145 struct env_val {
146 SLIST_ENTRY(env_val) entry;
147 char *val;
148 };
149 SLIST_HEAD(env_head, env_val);
151 struct fcgi_record_header {
152 uint8_t version;
153 uint8_t type;
154 uint16_t id;
155 uint16_t content_len;
156 uint8_t padding_len;
157 uint8_t reserved;
158 }__attribute__((__packed__));
160 struct blame_line {
161 int annotated;
162 char *id_str;
163 char *committer;
164 char datebuf[11]; /* YYYY-MM-DD + NUL */
165 };
167 struct repo_dir {
168 char *name;
169 char *owner;
170 char *description;
171 char *url;
172 time_t age;
173 char *path;
174 };
176 struct repo_tag {
177 TAILQ_ENTRY(repo_tag) entry;
178 char *commit_id;
179 char *tag_name;
180 char *tag_commit;
181 char *commit_msg;
182 char *tagger;
183 time_t tagger_time;
184 };
186 struct repo_commit {
187 TAILQ_ENTRY(repo_commit) entry;
188 char *path;
189 char *refs_str;
190 char *commit_id; /* id_str1 */
191 char *parent_id; /* id_str2 */
192 char *tree_id;
193 char *author;
194 char *committer;
195 char *commit_msg;
196 time_t committer_time;
197 };
199 struct got_repository;
200 struct transport {
201 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
202 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
203 struct got_reflist_head refs;
204 struct got_repository *repo;
205 struct repo_dir *repo_dir;
206 struct querystring *qs;
207 char *more_id;
208 char *tags_more_id;
209 unsigned int repos_total;
210 unsigned int next_disp;
211 unsigned int prev_disp;
212 unsigned int tag_count;
213 const struct got_error *error;
214 struct got_blob_object *blob;
215 int fd;
216 FILE *fp;
217 struct dirent **repos;
218 int nrepos;
219 };
221 enum socket_priv_fds {
222 DIFF_FD_1,
223 DIFF_FD_2,
224 DIFF_FD_3,
225 DIFF_FD_4,
226 DIFF_FD_5,
227 BLAME_FD_1,
228 BLAME_FD_2,
229 BLAME_FD_3,
230 BLAME_FD_4,
231 BLAME_FD_5,
232 BLAME_FD_6,
233 BLOB_FD_1,
234 BLOB_FD_2,
235 PRIV_FDS__MAX,
236 };
238 struct template;
239 struct request {
240 struct socket *sock;
241 struct server *srv;
242 struct transport *t;
243 struct template *tp;
244 struct event ev;
245 struct event tmo;
247 uint16_t id;
248 int fd;
249 int priv_fd[PRIV_FDS__MAX];
251 uint8_t buf[FCGI_RECORD_SIZE];
252 size_t buf_pos;
253 size_t buf_len;
255 uint8_t outbuf[GOTWEBD_CACHESIZE];
257 char querystring[MAX_QUERYSTRING];
258 char document_uri[MAX_DOCUMENT_URI];
259 char server_name[MAX_SERVER_NAME];
260 int https;
262 uint8_t request_started;
263 };
265 struct fcgi_begin_request_body {
266 uint16_t role;
267 uint8_t flags;
268 uint8_t reserved[5];
269 }__attribute__((__packed__));
271 struct fcgi_end_request_body {
272 uint32_t app_status;
273 uint8_t protocol_status;
274 uint8_t reserved[3];
275 }__attribute__((__packed__));
277 struct address {
278 TAILQ_ENTRY(address) entry;
279 struct sockaddr_storage ss;
280 socklen_t slen;
281 int ai_family;
282 int ai_socktype;
283 int ai_protocol;
284 in_port_t port;
285 char ifname[IFNAMSIZ];
286 };
287 TAILQ_HEAD(addresslist, address);
289 struct server {
290 TAILQ_ENTRY(server) entry;
291 struct addresslist al;
293 char name[GOTWEBD_MAXTEXT];
295 char repos_path[PATH_MAX];
296 char site_name[GOTWEBD_MAXNAME];
297 char site_owner[GOTWEBD_MAXNAME];
298 char site_link[GOTWEBD_MAXTEXT];
299 char logo[GOTWEBD_MAXTEXT];
300 char logo_url[GOTWEBD_MAXTEXT];
301 char custom_css[PATH_MAX];
303 size_t max_repos;
304 size_t max_repos_display;
305 size_t max_commits_display;
307 int show_site_owner;
308 int show_repo_owner;
309 int show_repo_age;
310 int show_repo_description;
311 int show_repo_cloneurl;
312 int respect_exportok;
314 int unix_socket;
315 char unix_socket_name[PATH_MAX];
317 int fcgi_socket;
318 };
319 TAILQ_HEAD(serverlist, server);
321 enum client_action {
322 CLIENT_CONNECT,
323 CLIENT_DISCONNECT,
324 };
326 struct socket_conf {
327 struct address addr;
329 char name[GOTWEBD_MAXTEXT];
330 char srv_name[GOTWEBD_MAXTEXT];
332 int id;
333 int af_type;
334 char unix_socket_name[PATH_MAX];
335 in_port_t fcgi_socket_port;
336 };
338 struct socket {
339 TAILQ_ENTRY(socket) entry;
340 struct socket_conf conf;
342 int fd;
343 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
344 int priv_fd[PRIV_FDS__MAX];
346 struct event evt;
347 struct event ev;
348 struct event pause;
350 int client_status;
351 };
352 TAILQ_HEAD(socketlist, socket);
354 struct passwd;
355 struct gotwebd {
356 struct serverlist servers;
357 struct socketlist sockets;
359 const char *gotwebd_conffile;
361 int gotwebd_debug;
362 int gotwebd_verbose;
364 struct imsgev *iev_parent;
365 struct imsgev *iev_server;
366 size_t nserver;
368 struct passwd *pw;
370 uint16_t prefork_gotwebd;
371 int gotwebd_reload;
373 int server_cnt;
375 char httpd_chroot[PATH_MAX];
377 int unix_socket;
378 char unix_socket_name[PATH_MAX];
379 };
381 /*
382 * URL parameter for gotweb_render_url. NULL values and int set to -1
383 * are implicitly ignored, and string are properly escaped.
384 */
385 struct gotweb_url {
386 int action;
387 int index_page;
388 int page;
389 const char *commit;
390 const char *previd;
391 const char *prevset;
392 const char *file;
393 const char *folder;
394 const char *headref;
395 const char *path;
396 };
398 struct querystring {
399 uint8_t action;
400 char *commit;
401 char *previd;
402 char *prevset;
403 char *file;
404 char *folder;
405 char *headref;
406 int index_page;
407 char *path;
408 int page;
409 };
411 struct querystring_keys {
412 const char *name;
413 int element;
414 };
416 struct action_keys {
417 const char *name;
418 int action;
419 };
421 enum querystring_elements {
422 ACTION,
423 COMMIT,
424 RFILE,
425 FOLDER,
426 HEADREF,
427 INDEX_PAGE,
428 PATH,
429 PAGE,
430 };
432 enum query_actions {
433 BLAME,
434 BLOB,
435 BLOBRAW,
436 BRIEFS,
437 COMMITS,
438 DIFF,
439 ERR,
440 INDEX,
441 PATCH,
442 SUMMARY,
443 TAG,
444 TAGS,
445 TREE,
446 RSS,
447 };
449 extern struct gotwebd *gotwebd_env;
451 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
452 struct blame_line *, int, int);
454 /* gotwebd.c */
455 void imsg_event_add(struct imsgev *);
456 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
457 pid_t, int, const void *, uint16_t);
458 int main_compose_sockets(struct gotwebd *, uint32_t, int,
459 const void *, uint16_t);
460 int sockets_compose_main(struct gotwebd *, uint32_t,
461 const void *, uint16_t);
463 /* sockets.c */
464 void sockets(struct gotwebd *, int);
465 void sockets_parse_sockets(struct gotwebd *);
466 void sockets_socket_accept(int, short, void *);
467 int sockets_privinit(struct gotwebd *, struct socket *);
469 /* gotweb.c */
470 void gotweb_index_navs(struct request *, struct gotweb_url *, int *,
471 struct gotweb_url *, int *);
472 int gotweb_render_age(struct template *, time_t);
473 const struct got_error *gotweb_init_transport(struct transport **);
474 const char *gotweb_action_name(int);
475 int gotweb_render_url(struct request *, struct gotweb_url *);
476 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
477 void gotweb_free_repo_commit(struct repo_commit *);
478 void gotweb_free_repo_tag(struct repo_tag *);
479 void gotweb_process_request(struct request *);
480 void gotweb_free_transport(struct transport *);
482 /* pages.tmpl */
483 int gotweb_render_page(struct template *, int (*)(struct template *));
484 int gotweb_render_error(struct template *);
485 int gotweb_render_repo_table_hdr(struct template *);
486 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
487 int gotweb_render_briefs(struct template *);
488 int gotweb_render_navs(struct template *);
489 int gotweb_render_commits(struct template *);
490 int gotweb_render_blob(struct template *);
491 int gotweb_render_tree(struct template *);
492 int gotweb_render_tags(struct template *);
493 int gotweb_render_tag(struct template *);
494 int gotweb_render_diff(struct template *);
495 int gotweb_render_branches(struct template *, struct got_reflist_head *);
496 int gotweb_render_summary(struct template *);
497 int gotweb_render_blame(struct template *);
498 int gotweb_render_patch(struct template *);
499 int gotweb_render_rss(struct template *);
501 /* parse.y */
502 int parse_config(const char *, struct gotwebd *);
503 int cmdline_symset(char *);
505 /* fcgi.c */
506 void fcgi_request(int, short, void *);
507 void fcgi_timeout(int, short, void *);
508 void fcgi_cleanup_request(struct request *);
509 void fcgi_create_end_record(struct request *);
510 void dump_fcgi_record(const char *, struct fcgi_record_header *);
511 int fcgi_write(void *, const void *, size_t);
513 /* got_operations.c */
514 const struct got_error *got_gotweb_closefile(FILE *);
515 const struct got_error *got_get_repo_owner(char **, struct request *);
516 const struct got_error *got_get_repo_age(time_t *, struct request *,
517 const char *);
518 const struct got_error *got_get_repo_commits(struct request *, size_t);
519 const struct got_error *got_get_repo_tags(struct request *, size_t);
520 const struct got_error *got_get_repo_heads(struct request *);
521 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
522 int got_output_repo_tree(struct request *, char **,
523 int (*)(struct template *, struct got_tree_entry *));
524 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
525 int *, int *, struct request *, const char *, const char *, const char *);
526 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
527 int (*)(struct template *, const char *, size_t));
528 const struct got_error *got_output_file_blame(struct request *,
529 got_render_blame_line_cb);
531 /* config.c */
532 int config_setserver(struct gotwebd *, struct server *);
533 int config_getserver(struct gotwebd *, struct imsg *);
534 int config_setsock(struct gotwebd *, struct socket *);
535 int config_getsock(struct gotwebd *, struct imsg *);
536 int config_setfd(struct gotwebd *, struct socket *);
537 int config_getfd(struct gotwebd *, struct imsg *);
538 int config_getcfg(struct gotwebd *, struct imsg *);
539 int config_init(struct gotwebd *);
541 /* log.c */
542 void log_init(int, int);
543 void log_procinit(const char *);
544 void log_setverbose(int);
545 int log_getverbose(void);
546 void log_warn(const char *, ...)
547 __attribute__((__format__ (printf, 1, 2)));
548 void log_warnx(const char *, ...)
549 __attribute__((__format__ (printf, 1, 2)));
550 void log_info(const char *, ...)
551 __attribute__((__format__ (printf, 1, 2)));
552 void log_debug(const char *, ...)
553 __attribute__((__format__ (printf, 1, 2)));
554 void logit(int, const char *, ...)
555 __attribute__((__format__ (printf, 2, 3)));
556 void vlog(int, const char *, va_list)
557 __attribute__((__format__ (printf, 2, 0)));
558 __dead void fatal(const char *, ...)
559 __attribute__((__format__ (printf, 1, 2)));
560 __dead void fatalx(const char *, ...)
561 __attribute__((__format__ (printf, 1, 2)));