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_reflist_head refs;
192 struct got_repository *repo;
193 struct repo_dir *repo_dir;
194 struct querystring *qs;
195 char *more_id;
196 char *next_id;
197 char *prev_id;
198 unsigned int repos_total;
199 unsigned int next_disp;
200 unsigned int prev_disp;
201 unsigned int tag_count;
202 const struct got_error *error;
203 struct got_blob_object *blob;
204 int fd;
205 FILE *fp;
206 struct dirent **repos;
207 int nrepos;
208 };
210 enum socket_priv_fds {
211 DIFF_FD_1,
212 DIFF_FD_2,
213 DIFF_FD_3,
214 DIFF_FD_4,
215 DIFF_FD_5,
216 BLAME_FD_1,
217 BLAME_FD_2,
218 BLAME_FD_3,
219 BLAME_FD_4,
220 BLAME_FD_5,
221 BLAME_FD_6,
222 BLOB_FD_1,
223 BLOB_FD_2,
224 PRIV_FDS__MAX,
225 };
227 struct template;
228 struct request {
229 struct socket *sock;
230 struct server *srv;
231 struct transport *t;
232 struct template *tp;
233 struct event ev;
234 struct event tmo;
236 uint16_t id;
237 int fd;
238 int priv_fd[PRIV_FDS__MAX];
240 uint8_t buf[FCGI_RECORD_SIZE];
241 size_t buf_pos;
242 size_t buf_len;
244 uint8_t outbuf[GOTWEBD_CACHESIZE];
245 size_t outbuf_len;
247 char querystring[MAX_QUERYSTRING];
248 char http_host[GOTWEBD_MAXTEXT];
249 char document_uri[MAX_DOCUMENT_URI];
250 char server_name[MAX_SERVER_NAME];
251 int https;
253 uint8_t request_started;
254 };
256 struct fcgi_begin_request_body {
257 uint16_t role;
258 uint8_t flags;
259 uint8_t reserved[5];
260 }__attribute__((__packed__));
262 struct fcgi_end_request_body {
263 uint32_t app_status;
264 uint8_t protocol_status;
265 uint8_t reserved[3];
266 }__attribute__((__packed__));
268 struct address {
269 TAILQ_ENTRY(address) entry;
270 struct sockaddr_storage ss;
271 int ipproto;
272 int prefixlen;
273 in_port_t port;
274 char ifname[IFNAMSIZ];
275 };
276 TAILQ_HEAD(addresslist, address);
278 struct cached_repo {
279 char path[PATH_MAX];
280 struct got_repository *repo;
281 };
283 struct server {
284 TAILQ_ENTRY(server) entry;
285 struct addresslist al;
287 struct cached_repo *cached_repos;
288 int ncached_repos;
290 char name[GOTWEBD_MAXTEXT];
292 char repos_path[PATH_MAX];
293 char site_name[GOTWEBD_MAXNAME];
294 char site_owner[GOTWEBD_MAXNAME];
295 char site_link[GOTWEBD_MAXTEXT];
296 char logo[GOTWEBD_MAXTEXT];
297 char logo_url[GOTWEBD_MAXTEXT];
298 char custom_css[PATH_MAX];
300 size_t max_repos;
301 size_t max_repos_display;
302 size_t max_commits_display;
304 int show_site_owner;
305 int show_repo_owner;
306 int show_repo_age;
307 int show_repo_description;
308 int show_repo_cloneurl;
309 int respect_exportok;
311 int unix_socket;
312 char unix_socket_name[PATH_MAX];
314 int fcgi_socket;
315 };
316 TAILQ_HEAD(serverlist, server);
318 enum client_action {
319 CLIENT_CONNECT,
320 CLIENT_DISCONNECT,
321 };
323 struct socket_conf {
324 struct address addr;
326 char name[GOTWEBD_MAXTEXT];
327 char srv_name[GOTWEBD_MAXTEXT];
329 int id;
330 int af_type;
331 char unix_socket_name[PATH_MAX];
332 in_port_t fcgi_socket_port;
333 };
335 struct socket {
336 TAILQ_ENTRY(socket) entry;
337 struct socket_conf conf;
339 int fd;
340 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
341 int priv_fd[PRIV_FDS__MAX];
343 struct event evt;
344 struct event ev;
345 struct event pause;
347 int client_status;
348 };
349 TAILQ_HEAD(socketlist, socket);
351 struct gotwebd {
352 struct serverlist servers;
353 struct socketlist sockets;
355 struct privsep *gotwebd_ps;
356 const char *gotwebd_conffile;
358 int gotwebd_debug;
359 int gotwebd_verbose;
360 int gotwebd_noaction;
362 uint16_t prefork_gotwebd;
363 int gotwebd_reload;
365 int server_cnt;
367 char httpd_chroot[PATH_MAX];
369 int unix_socket;
370 char unix_socket_name[PATH_MAX];
371 };
373 /*
374 * URL parameter for gotweb_render_url. NULL values and int set to -1
375 * are implicitly ignored, and string are properly escaped.
376 */
377 struct gotweb_url {
378 int action;
379 int index_page;
380 int page;
381 const char *commit;
382 const char *previd;
383 const char *prevset;
384 const char *file;
385 const char *folder;
386 const char *headref;
387 const char *path;
388 };
390 struct querystring {
391 uint8_t action;
392 char *commit;
393 char *previd;
394 char *prevset;
395 char *file;
396 char *folder;
397 char *headref;
398 int index_page;
399 char *path;
400 int page;
401 };
403 struct querystring_keys {
404 const char *name;
405 int element;
406 };
408 struct action_keys {
409 const char *name;
410 int action;
411 };
413 enum querystring_elements {
414 ACTION,
415 COMMIT,
416 RFILE,
417 FOLDER,
418 HEADREF,
419 INDEX_PAGE,
420 PATH,
421 PAGE,
422 PREVID,
423 QSELEM__MAX,
424 };
426 enum query_actions {
427 BLAME,
428 BLOB,
429 BLOBRAW,
430 BRIEFS,
431 COMMITS,
432 DIFF,
433 ERR,
434 INDEX,
435 SUMMARY,
436 TAG,
437 TAGS,
438 TREE,
439 RSS,
440 ACTIONS__MAX,
441 };
443 enum gotweb_ref_tm {
444 TM_DIFF,
445 TM_LONG,
446 TM_RFC822,
447 };
449 extern struct gotwebd *gotwebd_env;
451 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
452 struct blame_line *, int, int);
454 /* sockets.c */
455 void sockets(struct privsep *, struct privsep_proc *);
456 void sockets_shutdown(void);
457 void sockets_parse_sockets(struct gotwebd *);
458 void sockets_socket_accept(int, short, void *);
459 int sockets_privinit(struct gotwebd *, struct socket *);
461 /* gotweb.c */
462 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
463 struct gotweb_url *, int *);
464 int gotweb_render_age(struct template *, time_t, int);
465 const struct got_error *gotweb_init_transport(struct transport **);
466 const char *gotweb_action_name(int);
467 int gotweb_render_url(struct request *, struct gotweb_url *);
468 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
469 void gotweb_free_repo_commit(struct repo_commit *);
470 void gotweb_free_repo_tag(struct repo_tag *);
471 void gotweb_process_request(struct request *);
472 void gotweb_free_transport(struct transport *);
474 /* pages.tmpl */
475 int gotweb_render_page(struct template *, int (*)(struct template *));
476 int gotweb_render_error(struct template *);
477 int gotweb_render_repo_table_hdr(struct template *);
478 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
479 int gotweb_render_briefs(struct template *);
480 int gotweb_render_navs(struct template *);
481 int gotweb_render_commits(struct template *);
482 int gotweb_render_blob(struct template *);
483 int gotweb_render_tree(struct template *);
484 int gotweb_render_tags(struct template *);
485 int gotweb_render_tag(struct template *);
486 int gotweb_render_diff(struct template *);
487 int gotweb_render_branches(struct template *, struct got_reflist_head *);
488 int gotweb_render_summary(struct template *);
489 int gotweb_render_blame(struct template *);
490 int gotweb_render_rss(struct template *);
492 /* parse.y */
493 int parse_config(const char *, struct gotwebd *);
494 int cmdline_symset(char *);
496 /* fcgi.c */
497 void fcgi_request(int, short, void *);
498 void fcgi_timeout(int, short, void *);
499 void fcgi_cleanup_request(struct request *);
500 void fcgi_create_end_record(struct request *);
501 void dump_fcgi_record(const char *, struct fcgi_record_header *);
502 int fcgi_puts(struct template *, const char *);
503 int fcgi_putc(struct template *, int);
504 int fcgi_vprintf(struct request *, const char *, va_list);
505 int fcgi_printf(struct request *, const char *, ...)
506 __attribute__((__format__(printf, 2, 3)))
507 __attribute__((__nonnull__(2)));
508 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
510 /* got_operations.c */
511 const struct got_error *got_gotweb_flushfile(FILE *, int);
512 const struct got_error *got_get_repo_owner(char **, struct request *);
513 const struct got_error *got_get_repo_age(time_t *, struct request *,
514 const char *);
515 const struct got_error *got_get_repo_commits(struct request *, int);
516 const struct got_error *got_get_repo_tags(struct request *, int);
517 const struct got_error *got_get_repo_heads(struct request *);
518 const struct got_error *got_open_diff_for_output(FILE **, int *,
519 struct request *);
520 int got_output_repo_tree(struct request *,
521 int (*)(struct template *, struct got_tree_entry *));
522 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
523 int *, int *, struct request *);
524 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
525 int (*)(struct template *, const char *, size_t));
526 const struct got_error *got_output_file_blame(struct request *,
527 got_render_blame_line_cb);
529 /* config.c */
530 int config_setserver(struct gotwebd *, struct server *);
531 int config_getserver(struct gotwebd *, struct imsg *);
532 int config_setsock(struct gotwebd *, struct socket *);
533 int config_getsock(struct gotwebd *, struct imsg *);
534 int config_setfd(struct gotwebd *, struct socket *);
535 int config_getfd(struct gotwebd *, struct imsg *);
536 int config_getcfg(struct gotwebd *, struct imsg *);
537 int config_init(struct gotwebd *);