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