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_REPO_CACHESIZE 4
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
85 #define BUF 8192
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))
98 #define FD_RESERVE 5
99 #define FD_NEEDED 6
101 #define FCGI_BEGIN_REQUEST 1
102 #define FCGI_ABORT_REQUEST 2
103 #define FCGI_END_REQUEST 3
104 #define FCGI_PARAMS 4
105 #define FCGI_STDIN 5
106 #define FCGI_STDOUT 6
107 #define FCGI_STDERR 7
108 #define FCGI_DATA 8
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;
126 enum imsg_type {
127 IMSG_CFG_SRV,
128 IMSG_CFG_SOCK,
129 IMSG_CFG_FD,
130 IMSG_CFG_DONE,
131 IMSG_CTL_START,
132 };
134 struct imsgev {
135 struct imsgbuf ibuf;
136 void (*handler)(int, short, void *);
137 struct event ev;
138 void *data;
139 short events;
140 };
142 #define IMSG_SIZE_CHECK(imsg, p) do { \
143 if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
144 fatalx("bad length imsg received (%s)", #p); \
145 } while (0)
147 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
149 struct env_val {
150 SLIST_ENTRY(env_val) entry;
151 char *val;
152 };
153 SLIST_HEAD(env_head, env_val);
155 struct fcgi_record_header {
156 uint8_t version;
157 uint8_t type;
158 uint16_t id;
159 uint16_t content_len;
160 uint8_t padding_len;
161 uint8_t reserved;
162 }__attribute__((__packed__));
164 struct blame_line {
165 int annotated;
166 char *id_str;
167 char *committer;
168 char datebuf[11]; /* YYYY-MM-DD + NUL */
169 };
171 struct repo_dir {
172 char *name;
173 char *owner;
174 char *description;
175 char *url;
176 time_t age;
177 char *path;
178 };
180 struct repo_tag {
181 TAILQ_ENTRY(repo_tag) entry;
182 char *commit_id;
183 char *tag_name;
184 char *tag_commit;
185 char *commit_msg;
186 char *tagger;
187 time_t tagger_time;
188 };
190 struct repo_commit {
191 TAILQ_ENTRY(repo_commit) entry;
192 char *path;
193 char *refs_str;
194 char *commit_id; /* id_str1 */
195 char *parent_id; /* id_str2 */
196 char *tree_id;
197 char *author;
198 char *committer;
199 char *commit_msg;
200 time_t committer_time;
201 };
203 struct got_repository;
204 struct transport {
205 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
206 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
207 struct got_reflist_head refs;
208 struct got_repository *repo;
209 struct repo_dir *repo_dir;
210 struct querystring *qs;
211 char *more_id;
212 char *next_id;
213 char *prev_id;
214 unsigned int repos_total;
215 unsigned int next_disp;
216 unsigned int prev_disp;
217 unsigned int tag_count;
218 const struct got_error *error;
219 struct got_blob_object *blob;
220 int fd;
221 FILE *fp;
222 struct dirent **repos;
223 int nrepos;
224 };
226 enum socket_priv_fds {
227 DIFF_FD_1,
228 DIFF_FD_2,
229 DIFF_FD_3,
230 DIFF_FD_4,
231 DIFF_FD_5,
232 BLAME_FD_1,
233 BLAME_FD_2,
234 BLAME_FD_3,
235 BLAME_FD_4,
236 BLAME_FD_5,
237 BLAME_FD_6,
238 BLOB_FD_1,
239 BLOB_FD_2,
240 PRIV_FDS__MAX,
241 };
243 struct template;
244 struct request {
245 struct socket *sock;
246 struct server *srv;
247 struct transport *t;
248 struct template *tp;
249 struct event ev;
250 struct event tmo;
252 uint16_t id;
253 int fd;
254 int priv_fd[PRIV_FDS__MAX];
256 uint8_t buf[FCGI_RECORD_SIZE];
257 size_t buf_pos;
258 size_t buf_len;
260 uint8_t outbuf[GOTWEBD_CACHESIZE];
262 char querystring[MAX_QUERYSTRING];
263 char document_uri[MAX_DOCUMENT_URI];
264 char server_name[MAX_SERVER_NAME];
265 int https;
267 uint8_t request_started;
268 };
270 struct fcgi_begin_request_body {
271 uint16_t role;
272 uint8_t flags;
273 uint8_t reserved[5];
274 }__attribute__((__packed__));
276 struct fcgi_end_request_body {
277 uint32_t app_status;
278 uint8_t protocol_status;
279 uint8_t reserved[3];
280 }__attribute__((__packed__));
282 struct address {
283 TAILQ_ENTRY(address) entry;
284 struct sockaddr_storage ss;
285 socklen_t slen;
286 int ai_family;
287 int ai_socktype;
288 int ai_protocol;
289 in_port_t port;
290 char ifname[IFNAMSIZ];
291 };
292 TAILQ_HEAD(addresslist, address);
294 struct cached_repo {
295 char path[PATH_MAX];
296 struct got_repository *repo;
297 };
299 struct server {
300 TAILQ_ENTRY(server) entry;
301 struct addresslist al;
303 struct cached_repo *cached_repos;
304 int ncached_repos;
306 char name[GOTWEBD_MAXTEXT];
308 char repos_path[PATH_MAX];
309 char site_name[GOTWEBD_MAXNAME];
310 char site_owner[GOTWEBD_MAXNAME];
311 char site_link[GOTWEBD_MAXTEXT];
312 char logo[GOTWEBD_MAXTEXT];
313 char logo_url[GOTWEBD_MAXTEXT];
314 char custom_css[PATH_MAX];
316 size_t max_repos;
317 size_t max_repos_display;
318 size_t max_commits_display;
320 int show_site_owner;
321 int show_repo_owner;
322 int show_repo_age;
323 int show_repo_description;
324 int show_repo_cloneurl;
325 int respect_exportok;
327 int unix_socket;
328 char unix_socket_name[PATH_MAX];
330 int fcgi_socket;
331 };
332 TAILQ_HEAD(serverlist, server);
334 enum client_action {
335 CLIENT_CONNECT,
336 CLIENT_DISCONNECT,
337 };
339 struct socket_conf {
340 struct address addr;
342 char name[GOTWEBD_MAXTEXT];
343 char srv_name[GOTWEBD_MAXTEXT];
345 int id;
346 int af_type;
347 char unix_socket_name[PATH_MAX];
348 in_port_t fcgi_socket_port;
349 };
351 struct socket {
352 TAILQ_ENTRY(socket) entry;
353 struct socket_conf conf;
355 int fd;
356 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
357 int priv_fd[PRIV_FDS__MAX];
359 struct event evt;
360 struct event ev;
361 struct event pause;
363 int client_status;
364 };
365 TAILQ_HEAD(socketlist, socket);
367 struct passwd;
368 struct gotwebd {
369 struct serverlist servers;
370 struct socketlist sockets;
372 const char *gotwebd_conffile;
374 int gotwebd_debug;
375 int gotwebd_verbose;
377 struct imsgev *iev_parent;
378 struct imsgev *iev_server;
379 size_t nserver;
381 struct passwd *pw;
383 uint16_t prefork_gotwebd;
384 int gotwebd_reload;
386 int server_cnt;
388 char httpd_chroot[PATH_MAX];
390 int unix_socket;
391 char unix_socket_name[PATH_MAX];
392 };
394 /*
395 * URL parameter for gotweb_render_url. NULL values and int set to -1
396 * are implicitly ignored, and string are properly escaped.
397 */
398 struct gotweb_url {
399 int action;
400 int index_page;
401 int page;
402 const char *commit;
403 const char *previd;
404 const char *prevset;
405 const char *file;
406 const char *folder;
407 const char *headref;
408 const char *path;
409 };
411 struct querystring {
412 uint8_t action;
413 char *commit;
414 char *previd;
415 char *prevset;
416 char *file;
417 char *folder;
418 char *headref;
419 int index_page;
420 char *path;
421 int page;
422 };
424 struct querystring_keys {
425 const char *name;
426 int element;
427 };
429 struct action_keys {
430 const char *name;
431 int action;
432 };
434 enum querystring_elements {
435 ACTION,
436 COMMIT,
437 RFILE,
438 FOLDER,
439 HEADREF,
440 INDEX_PAGE,
441 PATH,
442 PAGE,
443 };
445 enum query_actions {
446 BLAME,
447 BLOB,
448 BLOBRAW,
449 BRIEFS,
450 COMMITS,
451 DIFF,
452 ERR,
453 INDEX,
454 SUMMARY,
455 TAG,
456 TAGS,
457 TREE,
458 RSS,
459 ACTIONS__MAX,
460 };
462 extern struct gotwebd *gotwebd_env;
464 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
465 struct blame_line *, int, int);
467 /* gotwebd.c */
468 void imsg_event_add(struct imsgev *);
469 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
470 pid_t, int, const void *, uint16_t);
471 int main_compose_sockets(struct gotwebd *, uint32_t, int,
472 const void *, uint16_t);
473 int sockets_compose_main(struct gotwebd *, uint32_t,
474 const void *, uint16_t);
476 /* sockets.c */
477 void sockets(struct gotwebd *, int);
478 void sockets_parse_sockets(struct gotwebd *);
479 void sockets_socket_accept(int, short, void *);
480 int sockets_privinit(struct gotwebd *, struct socket *);
482 /* gotweb.c */
483 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
484 struct gotweb_url *, int *);
485 int gotweb_render_age(struct template *, time_t);
486 const struct got_error *gotweb_init_transport(struct transport **);
487 const char *gotweb_action_name(int);
488 int gotweb_render_url(struct request *, struct gotweb_url *);
489 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
490 void gotweb_free_repo_commit(struct repo_commit *);
491 void gotweb_free_repo_tag(struct repo_tag *);
492 void gotweb_process_request(struct request *);
493 void gotweb_free_transport(struct transport *);
495 /* pages.tmpl */
496 int gotweb_render_page(struct template *, int (*)(struct template *));
497 int gotweb_render_error(struct template *);
498 int gotweb_render_repo_table_hdr(struct template *);
499 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
500 int gotweb_render_briefs(struct template *);
501 int gotweb_render_navs(struct template *);
502 int gotweb_render_commits(struct template *);
503 int gotweb_render_blob(struct template *);
504 int gotweb_render_tree(struct template *);
505 int gotweb_render_tags(struct template *);
506 int gotweb_render_tag(struct template *);
507 int gotweb_render_diff(struct template *);
508 int gotweb_render_branches(struct template *, struct got_reflist_head *);
509 int gotweb_render_summary(struct template *);
510 int gotweb_render_blame(struct template *);
511 int gotweb_render_rss(struct template *);
513 /* parse.y */
514 int parse_config(const char *, struct gotwebd *);
515 int cmdline_symset(char *);
517 /* fcgi.c */
518 void fcgi_request(int, short, void *);
519 void fcgi_timeout(int, short, void *);
520 void fcgi_cleanup_request(struct request *);
521 void fcgi_create_end_record(struct request *);
522 void dump_fcgi_record(const char *, struct fcgi_record_header *);
523 int fcgi_write(void *, const void *, size_t);
525 /* got_operations.c */
526 const struct got_error *got_gotweb_closefile(FILE *);
527 const struct got_error *got_get_repo_owner(char **, struct request *);
528 const struct got_error *got_get_repo_age(time_t *, struct request *,
529 const char *);
530 const struct got_error *got_get_repo_commits(struct request *, size_t);
531 const struct got_error *got_get_repo_tags(struct request *, size_t);
532 const struct got_error *got_get_repo_heads(struct request *);
533 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
534 int got_output_repo_tree(struct request *,
535 int (*)(struct template *, struct got_tree_entry *));
536 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
537 int *, int *, struct request *);
538 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
539 int (*)(struct template *, const char *, size_t));
540 const struct got_error *got_output_file_blame(struct request *,
541 got_render_blame_line_cb);
543 /* config.c */
544 int config_setserver(struct gotwebd *, struct server *);
545 int config_getserver(struct gotwebd *, struct imsg *);
546 int config_setsock(struct gotwebd *, struct socket *);
547 int config_getsock(struct gotwebd *, struct imsg *);
548 int config_setfd(struct gotwebd *, struct socket *);
549 int config_getfd(struct gotwebd *, struct imsg *);
550 int config_getcfg(struct gotwebd *, struct imsg *);
551 int config_init(struct gotwebd *);
553 /* log.c */
554 void log_init(int, int);
555 void log_procinit(const char *);
556 void log_setverbose(int);
557 int log_getverbose(void);
558 void log_warn(const char *, ...)
559 __attribute__((__format__ (printf, 1, 2)));
560 void log_warnx(const char *, ...)
561 __attribute__((__format__ (printf, 1, 2)));
562 void log_info(const char *, ...)
563 __attribute__((__format__ (printf, 1, 2)));
564 void log_debug(const char *, ...)
565 __attribute__((__format__ (printf, 1, 2)));
566 void logit(int, const char *, ...)
567 __attribute__((__format__ (printf, 2, 3)));
568 void vlog(int, const char *, va_list)
569 __attribute__((__format__ (printf, 2, 0)));
570 __dead void fatal(const char *, ...)
571 __attribute__((__format__ (printf, 1, 2)));
572 __dead void fatalx(const char *, ...)
573 __attribute__((__format__ (printf, 1, 2)));