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_SOCK_FILENO 3
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 server {
293 TAILQ_ENTRY(server) entry;
294 struct addresslist al;
296 char name[GOTWEBD_MAXTEXT];
298 char repos_path[PATH_MAX];
299 char site_name[GOTWEBD_MAXNAME];
300 char site_owner[GOTWEBD_MAXNAME];
301 char site_link[GOTWEBD_MAXTEXT];
302 char logo[GOTWEBD_MAXTEXT];
303 char logo_url[GOTWEBD_MAXTEXT];
304 char custom_css[PATH_MAX];
306 size_t max_repos;
307 size_t max_repos_display;
308 size_t max_commits_display;
310 int show_site_owner;
311 int show_repo_owner;
312 int show_repo_age;
313 int show_repo_description;
314 int show_repo_cloneurl;
315 int respect_exportok;
317 int unix_socket;
318 char unix_socket_name[PATH_MAX];
320 int fcgi_socket;
321 };
322 TAILQ_HEAD(serverlist, server);
324 enum client_action {
325 CLIENT_CONNECT,
326 CLIENT_DISCONNECT,
327 };
329 struct socket_conf {
330 struct address addr;
332 char name[GOTWEBD_MAXTEXT];
333 char srv_name[GOTWEBD_MAXTEXT];
335 int id;
336 int af_type;
337 char unix_socket_name[PATH_MAX];
338 in_port_t fcgi_socket_port;
339 };
341 struct socket {
342 TAILQ_ENTRY(socket) entry;
343 struct socket_conf conf;
345 int fd;
346 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
347 int priv_fd[PRIV_FDS__MAX];
349 struct event evt;
350 struct event ev;
351 struct event pause;
353 int client_status;
354 };
355 TAILQ_HEAD(socketlist, socket);
357 struct passwd;
358 struct gotwebd {
359 struct serverlist servers;
360 struct socketlist sockets;
362 const char *gotwebd_conffile;
364 int gotwebd_debug;
365 int gotwebd_verbose;
367 struct imsgev *iev_parent;
368 struct imsgev *iev_server;
369 size_t nserver;
371 struct passwd *pw;
373 uint16_t prefork_gotwebd;
374 int gotwebd_reload;
376 int server_cnt;
378 char httpd_chroot[PATH_MAX];
380 int unix_socket;
381 char unix_socket_name[PATH_MAX];
382 };
384 /*
385 * URL parameter for gotweb_render_url. NULL values and int set to -1
386 * are implicitly ignored, and string are properly escaped.
387 */
388 struct gotweb_url {
389 int action;
390 int index_page;
391 int page;
392 const char *commit;
393 const char *previd;
394 const char *prevset;
395 const char *file;
396 const char *folder;
397 const char *headref;
398 const char *path;
399 };
401 struct querystring {
402 uint8_t action;
403 char *commit;
404 char *previd;
405 char *prevset;
406 char *file;
407 char *folder;
408 char *headref;
409 int index_page;
410 char *path;
411 int page;
412 };
414 struct querystring_keys {
415 const char *name;
416 int element;
417 };
419 struct action_keys {
420 const char *name;
421 int action;
422 };
424 enum querystring_elements {
425 ACTION,
426 COMMIT,
427 RFILE,
428 FOLDER,
429 HEADREF,
430 INDEX_PAGE,
431 PATH,
432 PAGE,
433 };
435 enum query_actions {
436 BLAME,
437 BLOB,
438 BLOBRAW,
439 BRIEFS,
440 COMMITS,
441 DIFF,
442 ERR,
443 INDEX,
444 SUMMARY,
445 TAG,
446 TAGS,
447 TREE,
448 RSS,
449 ACTIONS__MAX,
450 };
452 extern struct gotwebd *gotwebd_env;
454 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
455 struct blame_line *, int, int);
457 /* gotwebd.c */
458 void imsg_event_add(struct imsgev *);
459 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
460 pid_t, int, const void *, uint16_t);
461 int main_compose_sockets(struct gotwebd *, uint32_t, int,
462 const void *, uint16_t);
463 int sockets_compose_main(struct gotwebd *, uint32_t,
464 const void *, uint16_t);
466 /* sockets.c */
467 void sockets(struct gotwebd *, int);
468 void sockets_parse_sockets(struct gotwebd *);
469 void sockets_socket_accept(int, short, void *);
470 int sockets_privinit(struct gotwebd *, struct socket *);
472 /* gotweb.c */
473 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
474 struct gotweb_url *, int *);
475 int gotweb_render_age(struct template *, time_t);
476 const struct got_error *gotweb_init_transport(struct transport **);
477 const char *gotweb_action_name(int);
478 int gotweb_render_url(struct request *, struct gotweb_url *);
479 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
480 void gotweb_free_repo_commit(struct repo_commit *);
481 void gotweb_free_repo_tag(struct repo_tag *);
482 void gotweb_process_request(struct request *);
483 void gotweb_free_transport(struct transport *);
485 /* pages.tmpl */
486 int gotweb_render_page(struct template *, int (*)(struct template *));
487 int gotweb_render_error(struct template *);
488 int gotweb_render_repo_table_hdr(struct template *);
489 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
490 int gotweb_render_briefs(struct template *);
491 int gotweb_render_navs(struct template *);
492 int gotweb_render_commits(struct template *);
493 int gotweb_render_blob(struct template *);
494 int gotweb_render_tree(struct template *);
495 int gotweb_render_tags(struct template *);
496 int gotweb_render_tag(struct template *);
497 int gotweb_render_diff(struct template *);
498 int gotweb_render_branches(struct template *, struct got_reflist_head *);
499 int gotweb_render_summary(struct template *);
500 int gotweb_render_blame(struct template *);
501 int gotweb_render_rss(struct template *);
503 /* parse.y */
504 int parse_config(const char *, struct gotwebd *);
505 int cmdline_symset(char *);
507 /* fcgi.c */
508 void fcgi_request(int, short, void *);
509 void fcgi_timeout(int, short, void *);
510 void fcgi_cleanup_request(struct request *);
511 void fcgi_create_end_record(struct request *);
512 void dump_fcgi_record(const char *, struct fcgi_record_header *);
513 int fcgi_write(void *, const void *, size_t);
515 /* got_operations.c */
516 const struct got_error *got_gotweb_closefile(FILE *);
517 const struct got_error *got_get_repo_owner(char **, struct request *);
518 const struct got_error *got_get_repo_age(time_t *, struct request *,
519 const char *);
520 const struct got_error *got_get_repo_commits(struct request *, size_t);
521 const struct got_error *got_get_repo_tags(struct request *, size_t);
522 const struct got_error *got_get_repo_heads(struct request *);
523 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
524 int got_output_repo_tree(struct request *,
525 int (*)(struct template *, struct got_tree_entry *));
526 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
527 int *, int *, struct request *);
528 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
529 int (*)(struct template *, const char *, size_t));
530 const struct got_error *got_output_file_blame(struct request *,
531 got_render_blame_line_cb);
533 /* config.c */
534 int config_setserver(struct gotwebd *, struct server *);
535 int config_getserver(struct gotwebd *, struct imsg *);
536 int config_setsock(struct gotwebd *, struct socket *);
537 int config_getsock(struct gotwebd *, struct imsg *);
538 int config_setfd(struct gotwebd *, struct socket *);
539 int config_getfd(struct gotwebd *, struct imsg *);
540 int config_getcfg(struct gotwebd *, struct imsg *);
541 int config_init(struct gotwebd *);
543 /* log.c */
544 void log_init(int, int);
545 void log_procinit(const char *);
546 void log_setverbose(int);
547 int log_getverbose(void);
548 void log_warn(const char *, ...)
549 __attribute__((__format__ (printf, 1, 2)));
550 void log_warnx(const char *, ...)
551 __attribute__((__format__ (printf, 1, 2)));
552 void log_info(const char *, ...)
553 __attribute__((__format__ (printf, 1, 2)));
554 void log_debug(const char *, ...)
555 __attribute__((__format__ (printf, 1, 2)));
556 void logit(int, const char *, ...)
557 __attribute__((__format__ (printf, 2, 3)));
558 void vlog(int, const char *, va_list)
559 __attribute__((__format__ (printf, 2, 0)));
560 __dead void fatal(const char *, ...)
561 __attribute__((__format__ (printf, 1, 2)));
562 __dead void fatalx(const char *, ...)
563 __attribute__((__format__ (printf, 1, 2)));