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
51 #define GOTWEBD_SOCK_FILENO 3
53 #define PROC_MAX_INSTANCES 32
55 /* GOTWEB DEFAULTS */
56 #define MAX_QUERYSTRING 2048
57 #define MAX_DOCUMENT_URI 255
58 #define MAX_SERVER_NAME 255
60 #define GOTWEB_GIT_DIR ".git"
62 #define D_HTTPD_CHROOT "/var/www"
63 #define D_UNIX_SOCKET "/run/gotweb.sock"
64 #define D_FCGI_PORT "9000"
65 #define D_GOTPATH "/got/public"
66 #define D_SITENAME "Gotweb"
67 #define D_SITEOWNER "Got Owner"
68 #define D_SITELINK "Repos"
69 #define D_GOTLOGO "got.png"
70 #define D_GOTURL "https://gameoftrees.org"
71 #define D_GOTWEBCSS "gotweb.css"
73 #define D_SHOWROWNER 1
74 #define D_SHOWSOWNER 1
75 #define D_SHOWAGE 1
76 #define D_SHOWDESC 1
77 #define D_SHOWURL 1
78 #define D_RESPECTEXPORTOK 0
79 #define D_MAXREPO 0
80 #define D_MAXREPODISP 25
81 #define D_MAXSLCOMMDISP 10
82 #define D_MAXCOMMITDISP 25
84 #define BUF 8192
86 #define TIMEOUT_DEFAULT 120
88 #define FCGI_CONTENT_SIZE 65535
89 #define FCGI_PADDING_SIZE 255
90 #define FCGI_RECORD_SIZE \
91 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
93 #define FCGI_ALIGNMENT 8
94 #define FCGI_ALIGN(n) \
95 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
97 #define FD_RESERVE 5
98 #define FD_NEEDED 6
100 #define FCGI_BEGIN_REQUEST 1
101 #define FCGI_ABORT_REQUEST 2
102 #define FCGI_END_REQUEST 3
103 #define FCGI_PARAMS 4
104 #define FCGI_STDIN 5
105 #define FCGI_STDOUT 6
106 #define FCGI_STDERR 7
107 #define FCGI_DATA 8
108 #define FCGI_GET_VALUES 9
109 #define FCGI_GET_VALUES_RESULT 10
110 #define FCGI_UNKNOWN_TYPE 11
111 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
113 #define FCGI_REQUEST_COMPLETE 0
114 #define FCGI_CANT_MPX_CONN 1
115 #define FCGI_OVERLOADED 2
116 #define FCGI_UNKNOWN_ROLE 3
118 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
120 /* Forward declaration */
121 struct got_blob_object;
122 struct got_tree_entry;
123 struct got_reflist_head;
125 enum imsg_type {
126 IMSG_CFG_SRV,
127 IMSG_CFG_SOCK,
128 IMSG_CFG_FD,
129 IMSG_CFG_DONE,
130 IMSG_CTL_START,
131 };
133 struct imsgev {
134 struct imsgbuf ibuf;
135 void (*handler)(int, short, void *);
136 struct event ev;
137 void *data;
138 short events;
139 };
141 #define IMSG_SIZE_CHECK(imsg, p) do { \
142 if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \
143 fatalx("bad length imsg received (%s)", #p); \
144 } while (0)
146 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
148 struct env_val {
149 SLIST_ENTRY(env_val) entry;
150 char *val;
151 };
152 SLIST_HEAD(env_head, env_val);
154 struct fcgi_record_header {
155 uint8_t version;
156 uint8_t type;
157 uint16_t id;
158 uint16_t content_len;
159 uint8_t padding_len;
160 uint8_t reserved;
161 }__attribute__((__packed__));
163 struct blame_line {
164 int annotated;
165 char *id_str;
166 char *committer;
167 char datebuf[11]; /* YYYY-MM-DD + NUL */
168 };
170 struct repo_dir {
171 char *name;
172 char *owner;
173 char *description;
174 char *url;
175 time_t age;
176 char *path;
177 };
179 struct repo_tag {
180 TAILQ_ENTRY(repo_tag) entry;
181 char *commit_id;
182 char *tag_name;
183 char *tag_commit;
184 char *commit_msg;
185 char *tagger;
186 time_t tagger_time;
187 };
189 struct repo_commit {
190 TAILQ_ENTRY(repo_commit) entry;
191 char *path;
192 char *refs_str;
193 char *commit_id; /* id_str1 */
194 char *parent_id; /* id_str2 */
195 char *tree_id;
196 char *author;
197 char *committer;
198 char *commit_msg;
199 time_t committer_time;
200 };
202 struct got_repository;
203 struct transport {
204 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
205 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
206 struct got_reflist_head refs;
207 struct got_repository *repo;
208 struct repo_dir *repo_dir;
209 struct querystring *qs;
210 char *more_id;
211 char *next_id;
212 char *prev_id;
213 unsigned int repos_total;
214 unsigned int next_disp;
215 unsigned int prev_disp;
216 unsigned int tag_count;
217 const struct got_error *error;
218 struct got_blob_object *blob;
219 int fd;
220 FILE *fp;
221 struct dirent **repos;
222 int nrepos;
223 };
225 enum socket_priv_fds {
226 DIFF_FD_1,
227 DIFF_FD_2,
228 DIFF_FD_3,
229 DIFF_FD_4,
230 DIFF_FD_5,
231 BLAME_FD_1,
232 BLAME_FD_2,
233 BLAME_FD_3,
234 BLAME_FD_4,
235 BLAME_FD_5,
236 BLAME_FD_6,
237 BLOB_FD_1,
238 BLOB_FD_2,
239 PRIV_FDS__MAX,
240 };
242 struct template;
243 struct request {
244 struct socket *sock;
245 struct server *srv;
246 struct transport *t;
247 struct template *tp;
248 struct event ev;
249 struct event tmo;
251 uint16_t id;
252 int fd;
253 int priv_fd[PRIV_FDS__MAX];
255 uint8_t buf[FCGI_RECORD_SIZE];
256 size_t buf_pos;
257 size_t buf_len;
259 uint8_t outbuf[GOTWEBD_CACHESIZE];
261 char querystring[MAX_QUERYSTRING];
262 char document_uri[MAX_DOCUMENT_URI];
263 char server_name[MAX_SERVER_NAME];
264 int https;
266 uint8_t request_started;
267 };
269 struct fcgi_begin_request_body {
270 uint16_t role;
271 uint8_t flags;
272 uint8_t reserved[5];
273 }__attribute__((__packed__));
275 struct fcgi_end_request_body {
276 uint32_t app_status;
277 uint8_t protocol_status;
278 uint8_t reserved[3];
279 }__attribute__((__packed__));
281 struct address {
282 TAILQ_ENTRY(address) entry;
283 struct sockaddr_storage ss;
284 socklen_t slen;
285 int ai_family;
286 int ai_socktype;
287 int ai_protocol;
288 in_port_t port;
289 char ifname[IFNAMSIZ];
290 };
291 TAILQ_HEAD(addresslist, address);
293 struct cached_repo {
294 char path[PATH_MAX];
295 struct got_repository *repo;
296 };
298 struct server {
299 TAILQ_ENTRY(server) entry;
300 struct addresslist al;
302 struct cached_repo *cached_repos;
303 int ncached_repos;
305 char name[GOTWEBD_MAXTEXT];
307 char repos_path[PATH_MAX];
308 char site_name[GOTWEBD_MAXNAME];
309 char site_owner[GOTWEBD_MAXNAME];
310 char site_link[GOTWEBD_MAXTEXT];
311 char logo[GOTWEBD_MAXTEXT];
312 char logo_url[GOTWEBD_MAXTEXT];
313 char custom_css[PATH_MAX];
315 size_t max_repos;
316 size_t max_repos_display;
317 size_t max_commits_display;
319 int show_site_owner;
320 int show_repo_owner;
321 int show_repo_age;
322 int show_repo_description;
323 int show_repo_cloneurl;
324 int respect_exportok;
326 int unix_socket;
327 char unix_socket_name[PATH_MAX];
329 int fcgi_socket;
330 };
331 TAILQ_HEAD(serverlist, server);
333 enum client_action {
334 CLIENT_CONNECT,
335 CLIENT_DISCONNECT,
336 };
338 struct socket_conf {
339 struct address addr;
341 char name[GOTWEBD_MAXTEXT];
342 char srv_name[GOTWEBD_MAXTEXT];
344 int id;
345 int af_type;
346 char unix_socket_name[PATH_MAX];
347 in_port_t fcgi_socket_port;
348 };
350 struct socket {
351 TAILQ_ENTRY(socket) entry;
352 struct socket_conf conf;
354 int fd;
355 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
356 int priv_fd[PRIV_FDS__MAX];
358 struct event evt;
359 struct event ev;
360 struct event pause;
362 int client_status;
363 };
364 TAILQ_HEAD(socketlist, socket);
366 struct passwd;
367 struct gotwebd {
368 struct serverlist servers;
369 struct socketlist sockets;
371 const char *gotwebd_conffile;
373 int gotwebd_debug;
374 int gotwebd_verbose;
376 struct imsgev *iev_parent;
377 struct imsgev *iev_server;
378 size_t nserver;
380 struct passwd *pw;
382 uint16_t prefork_gotwebd;
383 int gotwebd_reload;
385 int server_cnt;
387 char httpd_chroot[PATH_MAX];
389 int unix_socket;
390 char unix_socket_name[PATH_MAX];
391 };
393 /*
394 * URL parameter for gotweb_render_url. NULL values and int set to -1
395 * are implicitly ignored, and string are properly escaped.
396 */
397 struct gotweb_url {
398 int action;
399 int index_page;
400 int page;
401 const char *commit;
402 const char *previd;
403 const char *prevset;
404 const char *file;
405 const char *folder;
406 const char *headref;
407 const char *path;
408 };
410 struct querystring {
411 uint8_t action;
412 char *commit;
413 char *previd;
414 char *prevset;
415 char *file;
416 char *folder;
417 char *headref;
418 int index_page;
419 char *path;
420 int page;
421 };
423 struct querystring_keys {
424 const char *name;
425 int element;
426 };
428 struct action_keys {
429 const char *name;
430 int action;
431 };
433 enum querystring_elements {
434 ACTION,
435 COMMIT,
436 RFILE,
437 FOLDER,
438 HEADREF,
439 INDEX_PAGE,
440 PATH,
441 PAGE,
442 };
444 enum query_actions {
445 BLAME,
446 BLOB,
447 BLOBRAW,
448 BRIEFS,
449 COMMITS,
450 DIFF,
451 ERR,
452 INDEX,
453 SUMMARY,
454 TAG,
455 TAGS,
456 TREE,
457 RSS,
458 ACTIONS__MAX,
459 };
461 extern struct gotwebd *gotwebd_env;
463 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
464 struct blame_line *, int, int);
466 /* gotwebd.c */
467 void imsg_event_add(struct imsgev *);
468 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
469 pid_t, int, const void *, uint16_t);
470 int main_compose_sockets(struct gotwebd *, uint32_t, int,
471 const void *, uint16_t);
472 int sockets_compose_main(struct gotwebd *, uint32_t,
473 const void *, uint16_t);
475 /* sockets.c */
476 void sockets(struct gotwebd *, int);
477 void sockets_parse_sockets(struct gotwebd *);
478 void sockets_socket_accept(int, short, void *);
479 int sockets_privinit(struct gotwebd *, struct socket *);
481 /* gotweb.c */
482 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
483 struct gotweb_url *, int *);
484 int gotweb_render_age(struct template *, time_t);
485 const struct got_error *gotweb_init_transport(struct transport **);
486 const char *gotweb_action_name(int);
487 int gotweb_render_url(struct request *, struct gotweb_url *);
488 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
489 void gotweb_free_repo_commit(struct repo_commit *);
490 void gotweb_free_repo_tag(struct repo_tag *);
491 void gotweb_process_request(struct request *);
492 void gotweb_free_transport(struct transport *);
494 /* pages.tmpl */
495 int gotweb_render_page(struct template *, int (*)(struct template *));
496 int gotweb_render_error(struct template *);
497 int gotweb_render_repo_table_hdr(struct template *);
498 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
499 int gotweb_render_briefs(struct template *);
500 int gotweb_render_navs(struct template *);
501 int gotweb_render_commits(struct template *);
502 int gotweb_render_blob(struct template *);
503 int gotweb_render_tree(struct template *);
504 int gotweb_render_tags(struct template *);
505 int gotweb_render_tag(struct template *);
506 int gotweb_render_diff(struct template *);
507 int gotweb_render_branches(struct template *, struct got_reflist_head *);
508 int gotweb_render_summary(struct template *);
509 int gotweb_render_blame(struct template *);
510 int gotweb_render_rss(struct template *);
512 /* parse.y */
513 int parse_config(const char *, struct gotwebd *);
514 int cmdline_symset(char *);
516 /* fcgi.c */
517 void fcgi_request(int, short, void *);
518 void fcgi_timeout(int, short, void *);
519 void fcgi_cleanup_request(struct request *);
520 void fcgi_create_end_record(struct request *);
521 void dump_fcgi_record(const char *, struct fcgi_record_header *);
522 int fcgi_write(void *, const void *, size_t);
524 /* got_operations.c */
525 const struct got_error *got_gotweb_closefile(FILE *);
526 const struct got_error *got_get_repo_owner(char **, struct request *);
527 const struct got_error *got_get_repo_age(time_t *, struct request *,
528 const char *);
529 const struct got_error *got_get_repo_commits(struct request *, size_t);
530 const struct got_error *got_get_repo_tags(struct request *, size_t);
531 const struct got_error *got_get_repo_heads(struct request *);
532 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
533 int got_output_repo_tree(struct request *,
534 int (*)(struct template *, struct got_tree_entry *));
535 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
536 int *, int *, struct request *);
537 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
538 int (*)(struct template *, const char *, size_t));
539 const struct got_error *got_output_file_blame(struct request *,
540 got_render_blame_line_cb);
542 /* config.c */
543 int config_setserver(struct gotwebd *, struct server *);
544 int config_getserver(struct gotwebd *, struct imsg *);
545 int config_setsock(struct gotwebd *, struct socket *);
546 int config_getsock(struct gotwebd *, struct imsg *);
547 int config_setfd(struct gotwebd *, struct socket *);
548 int config_getfd(struct gotwebd *, struct imsg *);
549 int config_getcfg(struct gotwebd *, struct imsg *);
550 int config_init(struct gotwebd *);
552 /* log.c */
553 void log_init(int, int);
554 void log_procinit(const char *);
555 void log_setverbose(int);
556 int log_getverbose(void);
557 void log_warn(const char *, ...)
558 __attribute__((__format__ (printf, 1, 2)));
559 void log_warnx(const char *, ...)
560 __attribute__((__format__ (printf, 1, 2)));
561 void log_info(const char *, ...)
562 __attribute__((__format__ (printf, 1, 2)));
563 void log_debug(const char *, ...)
564 __attribute__((__format__ (printf, 1, 2)));
565 void logit(int, const char *, ...)
566 __attribute__((__format__ (printf, 2, 3)));
567 void vlog(int, const char *, va_list)
568 __attribute__((__format__ (printf, 2, 0)));
569 __dead void fatal(const char *, ...)
570 __attribute__((__format__ (printf, 1, 2)));
571 __dead void fatalx(const char *, ...)
572 __attribute__((__format__ (printf, 1, 2)));