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_MAXIFACE 16
53 #define GOTWEBD_REPO_CACHESIZE 4
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
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 = IMSG_PROC_MAX,
127 IMSG_CFG_SOCK,
128 IMSG_CFG_FD,
129 IMSG_CFG_DONE,
130 IMSG_CTL_START,
131 };
133 struct env_val {
134 SLIST_ENTRY(env_val) entry;
135 char *val;
136 };
137 SLIST_HEAD(env_head, env_val);
139 struct fcgi_record_header {
140 uint8_t version;
141 uint8_t type;
142 uint16_t id;
143 uint16_t content_len;
144 uint8_t padding_len;
145 uint8_t reserved;
146 }__attribute__((__packed__));
148 struct blame_line {
149 int annotated;
150 char *id_str;
151 char *committer;
152 char datebuf[11]; /* YYYY-MM-DD + NUL */
153 };
155 struct repo_dir {
156 char *name;
157 char *owner;
158 char *description;
159 char *url;
160 time_t age;
161 char *path;
162 };
164 struct repo_tag {
165 TAILQ_ENTRY(repo_tag) entry;
166 char *commit_id;
167 char *tag_name;
168 char *tag_commit;
169 char *commit_msg;
170 char *tagger;
171 time_t tagger_time;
172 };
174 struct repo_commit {
175 TAILQ_ENTRY(repo_commit) entry;
176 char *path;
177 char *refs_str;
178 char *commit_id; /* id_str1 */
179 char *parent_id; /* id_str2 */
180 char *tree_id;
181 char *author;
182 char *committer;
183 char *commit_msg;
184 time_t committer_time;
185 };
187 struct got_repository;
188 struct transport {
189 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
190 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
191 struct got_repository *repo;
192 struct repo_dir *repo_dir;
193 struct querystring *qs;
194 char *more_id;
195 char *next_id;
196 char *prev_id;
197 unsigned int repos_total;
198 unsigned int next_disp;
199 unsigned int prev_disp;
200 unsigned int tag_count;
201 };
203 enum socket_priv_fds {
204 DIFF_FD_1,
205 DIFF_FD_2,
206 DIFF_FD_3,
207 DIFF_FD_4,
208 DIFF_FD_5,
209 BLAME_FD_1,
210 BLAME_FD_2,
211 BLAME_FD_3,
212 BLAME_FD_4,
213 BLAME_FD_5,
214 BLAME_FD_6,
215 BLOB_FD_1,
216 BLOB_FD_2,
217 PRIV_FDS__MAX,
218 };
220 struct template;
221 struct request {
222 struct socket *sock;
223 struct server *srv;
224 struct transport *t;
225 struct template *tp;
226 struct event ev;
227 struct event tmo;
229 uint16_t id;
230 int fd;
231 int priv_fd[PRIV_FDS__MAX];
233 uint8_t buf[FCGI_RECORD_SIZE];
234 size_t buf_pos;
235 size_t buf_len;
237 uint8_t outbuf[GOTWEBD_CACHESIZE];
238 size_t outbuf_len;
240 char querystring[MAX_QUERYSTRING];
241 char http_host[GOTWEBD_MAXTEXT];
242 char document_uri[MAX_DOCUMENT_URI];
243 char server_name[MAX_SERVER_NAME];
244 int https;
246 uint8_t request_started;
247 };
249 struct fcgi_begin_request_body {
250 uint16_t role;
251 uint8_t flags;
252 uint8_t reserved[5];
253 }__attribute__((__packed__));
255 struct fcgi_end_request_body {
256 uint32_t app_status;
257 uint8_t protocol_status;
258 uint8_t reserved[3];
259 }__attribute__((__packed__));
261 struct address {
262 TAILQ_ENTRY(address) entry;
263 struct sockaddr_storage ss;
264 int ipproto;
265 int prefixlen;
266 in_port_t port;
267 char ifname[IFNAMSIZ];
268 };
269 TAILQ_HEAD(addresslist, address);
271 struct cached_repo {
272 char path[PATH_MAX];
273 struct got_repository *repo;
274 };
276 struct server {
277 TAILQ_ENTRY(server) entry;
278 struct addresslist al;
280 struct cached_repo *cached_repos;
281 int ncached_repos;
283 char name[GOTWEBD_MAXTEXT];
285 char repos_path[PATH_MAX];
286 char site_name[GOTWEBD_MAXNAME];
287 char site_owner[GOTWEBD_MAXNAME];
288 char site_link[GOTWEBD_MAXTEXT];
289 char logo[GOTWEBD_MAXTEXT];
290 char logo_url[GOTWEBD_MAXTEXT];
291 char custom_css[PATH_MAX];
293 size_t max_repos;
294 size_t max_repos_display;
295 size_t max_commits_display;
297 int show_site_owner;
298 int show_repo_owner;
299 int show_repo_age;
300 int show_repo_description;
301 int show_repo_cloneurl;
302 int respect_exportok;
304 int unix_socket;
305 char unix_socket_name[PATH_MAX];
307 int fcgi_socket;
308 };
309 TAILQ_HEAD(serverlist, server);
311 enum client_action {
312 CLIENT_CONNECT,
313 CLIENT_DISCONNECT,
314 };
316 struct socket_conf {
317 struct address addr;
319 char name[GOTWEBD_MAXTEXT];
320 char srv_name[GOTWEBD_MAXTEXT];
322 int id;
323 int af_type;
324 char unix_socket_name[PATH_MAX];
325 in_port_t fcgi_socket_port;
326 };
328 struct socket {
329 TAILQ_ENTRY(socket) entry;
330 struct socket_conf conf;
332 int fd;
333 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
334 int priv_fd[PRIV_FDS__MAX];
336 struct event evt;
337 struct event ev;
338 struct event pause;
340 int client_status;
341 };
342 TAILQ_HEAD(socketlist, socket);
344 struct gotwebd {
345 struct serverlist servers;
346 struct socketlist sockets;
348 struct privsep *gotwebd_ps;
349 const char *gotwebd_conffile;
351 int gotwebd_debug;
352 int gotwebd_verbose;
353 int gotwebd_noaction;
355 uint16_t prefork_gotwebd;
356 int gotwebd_reload;
358 int server_cnt;
360 char httpd_chroot[PATH_MAX];
362 int unix_socket;
363 char unix_socket_name[PATH_MAX];
364 };
366 /*
367 * URL parameter for gotweb_render_url. NULL values and int set to -1
368 * are implicitly ignored, and string are properly escaped.
369 */
370 struct gotweb_url {
371 int action;
372 int index_page;
373 int page;
374 const char *commit;
375 const char *previd;
376 const char *prevset;
377 const char *file;
378 const char *folder;
379 const char *headref;
380 const char *path;
381 };
383 struct querystring {
384 uint8_t action;
385 char *commit;
386 char *previd;
387 char *prevset;
388 char *file;
389 char *folder;
390 char *headref;
391 int index_page;
392 char *path;
393 int page;
394 };
396 struct querystring_keys {
397 const char *name;
398 int element;
399 };
401 struct action_keys {
402 const char *name;
403 int action;
404 };
406 enum querystring_elements {
407 ACTION,
408 COMMIT,
409 RFILE,
410 FOLDER,
411 HEADREF,
412 INDEX_PAGE,
413 PATH,
414 PAGE,
415 PREVID,
416 QSELEM__MAX,
417 };
419 enum query_actions {
420 BLAME,
421 BLOB,
422 BLOBRAW,
423 BRIEFS,
424 COMMITS,
425 DIFF,
426 ERR,
427 INDEX,
428 SUMMARY,
429 TAG,
430 TAGS,
431 TREE,
432 RSS,
433 ACTIONS__MAX,
434 };
436 enum gotweb_ref_tm {
437 TM_DIFF,
438 TM_LONG,
439 TM_RFC822,
440 };
442 extern struct gotwebd *gotwebd_env;
444 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
445 struct blame_line *, int, int);
447 /* sockets.c */
448 void sockets(struct privsep *, struct privsep_proc *);
449 void sockets_shutdown(void);
450 void sockets_parse_sockets(struct gotwebd *);
451 void sockets_socket_accept(int, short, void *);
452 int sockets_privinit(struct gotwebd *, struct socket *);
454 /* gotweb.c */
455 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
456 struct gotweb_url *, int *);
457 int gotweb_render_age(struct template *, time_t, int);
458 const struct got_error *gotweb_init_transport(struct transport **);
459 const char *gotweb_action_name(int);
460 int gotweb_render_url(struct request *, struct gotweb_url *);
461 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
462 void gotweb_free_repo_commit(struct repo_commit *);
463 void gotweb_free_repo_tag(struct repo_tag *);
464 void gotweb_process_request(struct request *);
465 void gotweb_free_transport(struct transport *);
467 /* pages.tmpl */
468 int gotweb_render_header(struct template *);
469 int gotweb_render_footer(struct template *);
470 int gotweb_render_repo_table_hdr(struct template *);
471 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
472 int gotweb_render_briefs(struct template *);
473 int gotweb_render_navs(struct template *);
474 int gotweb_render_commits(struct template *);
475 int gotweb_render_blob(struct template *, struct got_blob_object *);
476 int gotweb_render_tree(struct template *);
477 int gotweb_render_tags(struct template *);
478 int gotweb_render_tag(struct template *);
479 int gotweb_render_diff(struct template *, FILE *);
480 int gotweb_render_branches(struct template *, struct got_reflist_head *);
481 int gotweb_render_summary(struct template *, struct got_reflist_head *);
482 int gotweb_render_blame(struct template *);
483 int gotweb_render_rss(struct template *);
485 /* parse.y */
486 int parse_config(const char *, struct gotwebd *);
487 int cmdline_symset(char *);
489 /* fcgi.c */
490 void fcgi_request(int, short, void *);
491 void fcgi_timeout(int, short, void *);
492 void fcgi_cleanup_request(struct request *);
493 void fcgi_create_end_record(struct request *);
494 void dump_fcgi_record(const char *, struct fcgi_record_header *);
495 int fcgi_puts(struct template *, const char *);
496 int fcgi_putc(struct template *, int);
497 int fcgi_vprintf(struct request *, const char *, va_list);
498 int fcgi_printf(struct request *, const char *, ...)
499 __attribute__((__format__(printf, 2, 3)))
500 __attribute__((__nonnull__(2)));
501 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
503 /* got_operations.c */
504 const struct got_error *got_gotweb_flushfile(FILE *, int);
505 const struct got_error *got_get_repo_owner(char **, struct request *);
506 const struct got_error *got_get_repo_age(time_t *, struct request *,
507 const char *);
508 const struct got_error *got_get_repo_commits(struct request *, int);
509 const struct got_error *got_get_repo_tags(struct request *, int);
510 const struct got_error *got_get_repo_heads(struct request *);
511 const struct got_error *got_open_diff_for_output(FILE **, int *,
512 struct request *);
513 int got_output_repo_tree(struct request *,
514 int (*)(struct template *, struct got_tree_entry *));
515 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
516 int *, int *, struct request *);
517 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
518 int (*)(struct template *, const char *, size_t));
519 const struct got_error *got_output_file_blame(struct request *,
520 got_render_blame_line_cb);
522 /* config.c */
523 int config_setserver(struct gotwebd *, struct server *);
524 int config_getserver(struct gotwebd *, struct imsg *);
525 int config_setsock(struct gotwebd *, struct socket *);
526 int config_getsock(struct gotwebd *, struct imsg *);
527 int config_setfd(struct gotwebd *, struct socket *);
528 int config_getfd(struct gotwebd *, struct imsg *);
529 int config_getcfg(struct gotwebd *, struct imsg *);
530 int config_init(struct gotwebd *);