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_MAXIFACE 16
51 #define GOTWEBD_REPO_CACHESIZE 4
53 /* GOTWEB DEFAULTS */
54 #define MAX_QUERYSTRING 2048
55 #define MAX_DOCUMENT_URI 255
56 #define MAX_SERVER_NAME 255
58 #define GOTWEB_GIT_DIR ".git"
60 #define D_HTTPD_CHROOT "/var/www"
61 #define D_UNIX_SOCKET "/run/gotweb.sock"
62 #define D_FCGI_PORT "9000"
63 #define D_GOTPATH "/got/public"
64 #define D_SITENAME "Gotweb"
65 #define D_SITEOWNER "Got Owner"
66 #define D_SITELINK "Repos"
67 #define D_GOTLOGO "got.png"
68 #define D_GOTURL "https://gameoftrees.org"
69 #define D_GOTWEBCSS "gotweb.css"
71 #define D_SHOWROWNER 1
72 #define D_SHOWSOWNER 1
73 #define D_SHOWAGE 1
74 #define D_SHOWDESC 1
75 #define D_SHOWURL 1
76 #define D_RESPECTEXPORTOK 0
77 #define D_MAXREPO 0
78 #define D_MAXREPODISP 25
79 #define D_MAXSLCOMMDISP 10
80 #define D_MAXCOMMITDISP 25
82 #define BUF 8192
84 #define TIMEOUT_DEFAULT 120
86 #define FCGI_CONTENT_SIZE 65535
87 #define FCGI_PADDING_SIZE 255
88 #define FCGI_RECORD_SIZE \
89 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
91 #define FCGI_ALIGNMENT 8
92 #define FCGI_ALIGN(n) \
93 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
95 #define FD_RESERVE 5
96 #define FD_NEEDED 6
98 #define FCGI_BEGIN_REQUEST 1
99 #define FCGI_ABORT_REQUEST 2
100 #define FCGI_END_REQUEST 3
101 #define FCGI_PARAMS 4
102 #define FCGI_STDIN 5
103 #define FCGI_STDOUT 6
104 #define FCGI_STDERR 7
105 #define FCGI_DATA 8
106 #define FCGI_GET_VALUES 9
107 #define FCGI_GET_VALUES_RESULT 10
108 #define FCGI_UNKNOWN_TYPE 11
109 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
111 #define FCGI_REQUEST_COMPLETE 0
112 #define FCGI_CANT_MPX_CONN 1
113 #define FCGI_OVERLOADED 2
114 #define FCGI_UNKNOWN_ROLE 3
116 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
118 /* Forward declaration */
119 struct got_blob_object;
120 struct got_tree_entry;
121 struct got_reflist_head;
123 enum imsg_type {
124 IMSG_CFG_SRV = IMSG_PROC_MAX,
125 IMSG_CFG_SOCK,
126 IMSG_CFG_FD,
127 IMSG_CFG_DONE,
128 IMSG_CTL_START,
129 };
131 struct env_val {
132 SLIST_ENTRY(env_val) entry;
133 char *val;
134 };
135 SLIST_HEAD(env_head, env_val);
137 struct fcgi_record_header {
138 uint8_t version;
139 uint8_t type;
140 uint16_t id;
141 uint16_t content_len;
142 uint8_t padding_len;
143 uint8_t reserved;
144 }__attribute__((__packed__));
146 struct blame_line {
147 int annotated;
148 char *id_str;
149 char *committer;
150 char datebuf[11]; /* YYYY-MM-DD + NUL */
151 };
153 struct repo_dir {
154 char *name;
155 char *owner;
156 char *description;
157 char *url;
158 time_t age;
159 char *path;
160 };
162 struct repo_tag {
163 TAILQ_ENTRY(repo_tag) entry;
164 char *commit_id;
165 char *tag_name;
166 char *tag_commit;
167 char *commit_msg;
168 char *tagger;
169 time_t tagger_time;
170 };
172 struct repo_commit {
173 TAILQ_ENTRY(repo_commit) entry;
174 char *path;
175 char *refs_str;
176 char *commit_id; /* id_str1 */
177 char *parent_id; /* id_str2 */
178 char *tree_id;
179 char *author;
180 char *committer;
181 char *commit_msg;
182 time_t committer_time;
183 };
185 struct got_repository;
186 struct transport {
187 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
188 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
189 struct got_reflist_head refs;
190 struct got_repository *repo;
191 struct repo_dir *repo_dir;
192 struct querystring *qs;
193 char *more_id;
194 char *next_id;
195 char *prev_id;
196 unsigned int repos_total;
197 unsigned int next_disp;
198 unsigned int prev_disp;
199 unsigned int tag_count;
200 const struct got_error *error;
201 struct got_blob_object *blob;
202 int fd;
203 FILE *fp;
204 struct dirent **repos;
205 int nrepos;
206 };
208 enum socket_priv_fds {
209 DIFF_FD_1,
210 DIFF_FD_2,
211 DIFF_FD_3,
212 DIFF_FD_4,
213 DIFF_FD_5,
214 BLAME_FD_1,
215 BLAME_FD_2,
216 BLAME_FD_3,
217 BLAME_FD_4,
218 BLAME_FD_5,
219 BLAME_FD_6,
220 BLOB_FD_1,
221 BLOB_FD_2,
222 PRIV_FDS__MAX,
223 };
225 struct template;
226 struct request {
227 struct socket *sock;
228 struct server *srv;
229 struct transport *t;
230 struct template *tp;
231 struct event ev;
232 struct event tmo;
234 uint16_t id;
235 int fd;
236 int priv_fd[PRIV_FDS__MAX];
238 uint8_t buf[FCGI_RECORD_SIZE];
239 size_t buf_pos;
240 size_t buf_len;
242 uint8_t outbuf[GOTWEBD_CACHESIZE];
243 size_t outbuf_len;
245 char querystring[MAX_QUERYSTRING];
246 char http_host[GOTWEBD_MAXTEXT];
247 char document_uri[MAX_DOCUMENT_URI];
248 char server_name[MAX_SERVER_NAME];
249 int https;
251 uint8_t request_started;
252 };
254 struct fcgi_begin_request_body {
255 uint16_t role;
256 uint8_t flags;
257 uint8_t reserved[5];
258 }__attribute__((__packed__));
260 struct fcgi_end_request_body {
261 uint32_t app_status;
262 uint8_t protocol_status;
263 uint8_t reserved[3];
264 }__attribute__((__packed__));
266 struct address {
267 TAILQ_ENTRY(address) entry;
268 struct sockaddr_storage ss;
269 int ipproto;
270 int prefixlen;
271 in_port_t port;
272 char ifname[IFNAMSIZ];
273 };
274 TAILQ_HEAD(addresslist, address);
276 struct cached_repo {
277 char path[PATH_MAX];
278 struct got_repository *repo;
279 };
281 struct server {
282 TAILQ_ENTRY(server) entry;
283 struct addresslist al;
285 struct cached_repo *cached_repos;
286 int ncached_repos;
288 char name[GOTWEBD_MAXTEXT];
290 char repos_path[PATH_MAX];
291 char site_name[GOTWEBD_MAXNAME];
292 char site_owner[GOTWEBD_MAXNAME];
293 char site_link[GOTWEBD_MAXTEXT];
294 char logo[GOTWEBD_MAXTEXT];
295 char logo_url[GOTWEBD_MAXTEXT];
296 char custom_css[PATH_MAX];
298 size_t max_repos;
299 size_t max_repos_display;
300 size_t max_commits_display;
302 int show_site_owner;
303 int show_repo_owner;
304 int show_repo_age;
305 int show_repo_description;
306 int show_repo_cloneurl;
307 int respect_exportok;
309 int unix_socket;
310 char unix_socket_name[PATH_MAX];
312 int fcgi_socket;
313 };
314 TAILQ_HEAD(serverlist, server);
316 enum client_action {
317 CLIENT_CONNECT,
318 CLIENT_DISCONNECT,
319 };
321 struct socket_conf {
322 struct address addr;
324 char name[GOTWEBD_MAXTEXT];
325 char srv_name[GOTWEBD_MAXTEXT];
327 int id;
328 int af_type;
329 char unix_socket_name[PATH_MAX];
330 in_port_t fcgi_socket_port;
331 };
333 struct socket {
334 TAILQ_ENTRY(socket) entry;
335 struct socket_conf conf;
337 int fd;
338 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
339 int priv_fd[PRIV_FDS__MAX];
341 struct event evt;
342 struct event ev;
343 struct event pause;
345 int client_status;
346 };
347 TAILQ_HEAD(socketlist, socket);
349 struct gotwebd {
350 struct serverlist servers;
351 struct socketlist sockets;
353 struct privsep *gotwebd_ps;
354 const char *gotwebd_conffile;
356 int gotwebd_debug;
357 int gotwebd_verbose;
358 int gotwebd_noaction;
360 uint16_t prefork_gotwebd;
361 int gotwebd_reload;
363 int server_cnt;
365 char httpd_chroot[PATH_MAX];
367 int unix_socket;
368 char unix_socket_name[PATH_MAX];
369 };
371 /*
372 * URL parameter for gotweb_render_url. NULL values and int set to -1
373 * are implicitly ignored, and string are properly escaped.
374 */
375 struct gotweb_url {
376 int action;
377 int index_page;
378 int page;
379 const char *commit;
380 const char *previd;
381 const char *prevset;
382 const char *file;
383 const char *folder;
384 const char *headref;
385 const char *path;
386 };
388 struct querystring {
389 uint8_t action;
390 char *commit;
391 char *previd;
392 char *prevset;
393 char *file;
394 char *folder;
395 char *headref;
396 int index_page;
397 char *path;
398 int page;
399 };
401 struct querystring_keys {
402 const char *name;
403 int element;
404 };
406 struct action_keys {
407 const char *name;
408 int action;
409 };
411 enum querystring_elements {
412 ACTION,
413 COMMIT,
414 RFILE,
415 FOLDER,
416 HEADREF,
417 INDEX_PAGE,
418 PATH,
419 PAGE,
420 PREVID,
421 QSELEM__MAX,
422 };
424 enum query_actions {
425 BLAME,
426 BLOB,
427 BLOBRAW,
428 BRIEFS,
429 COMMITS,
430 DIFF,
431 ERR,
432 INDEX,
433 SUMMARY,
434 TAG,
435 TAGS,
436 TREE,
437 RSS,
438 ACTIONS__MAX,
439 };
441 enum gotweb_ref_tm {
442 TM_DIFF,
443 TM_LONG,
444 TM_RFC822,
445 };
447 extern struct gotwebd *gotwebd_env;
449 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
450 struct blame_line *, int, int);
452 /* sockets.c */
453 void sockets(struct privsep *, struct privsep_proc *);
454 void sockets_shutdown(void);
455 void sockets_parse_sockets(struct gotwebd *);
456 void sockets_socket_accept(int, short, void *);
457 int sockets_privinit(struct gotwebd *, struct socket *);
459 /* gotweb.c */
460 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
461 struct gotweb_url *, int *);
462 int gotweb_render_age(struct template *, time_t, int);
463 const struct got_error *gotweb_init_transport(struct transport **);
464 const char *gotweb_action_name(int);
465 int gotweb_render_url(struct request *, struct gotweb_url *);
466 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
467 void gotweb_free_repo_commit(struct repo_commit *);
468 void gotweb_free_repo_tag(struct repo_tag *);
469 void gotweb_process_request(struct request *);
470 void gotweb_free_transport(struct transport *);
472 /* pages.tmpl */
473 int gotweb_render_page(struct template *, int (*)(struct template *));
474 int gotweb_render_error(struct template *);
475 int gotweb_render_repo_table_hdr(struct template *);
476 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
477 int gotweb_render_briefs(struct template *);
478 int gotweb_render_navs(struct template *);
479 int gotweb_render_commits(struct template *);
480 int gotweb_render_blob(struct template *);
481 int gotweb_render_tree(struct template *);
482 int gotweb_render_tags(struct template *);
483 int gotweb_render_tag(struct template *);
484 int gotweb_render_diff(struct template *);
485 int gotweb_render_branches(struct template *, struct got_reflist_head *);
486 int gotweb_render_summary(struct template *);
487 int gotweb_render_blame(struct template *);
488 int gotweb_render_rss(struct template *);
490 /* parse.y */
491 int parse_config(const char *, struct gotwebd *);
492 int cmdline_symset(char *);
494 /* fcgi.c */
495 void fcgi_request(int, short, void *);
496 void fcgi_timeout(int, short, void *);
497 void fcgi_cleanup_request(struct request *);
498 void fcgi_create_end_record(struct request *);
499 void dump_fcgi_record(const char *, struct fcgi_record_header *);
500 int fcgi_puts(struct template *, const char *);
501 int fcgi_putc(struct template *, int);
502 int fcgi_vprintf(struct request *, const char *, va_list);
503 int fcgi_printf(struct request *, const char *, ...)
504 __attribute__((__format__(printf, 2, 3)))
505 __attribute__((__nonnull__(2)));
506 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
508 /* got_operations.c */
509 const struct got_error *got_gotweb_closefile(FILE *);
510 const struct got_error *got_get_repo_owner(char **, struct request *);
511 const struct got_error *got_get_repo_age(time_t *, struct request *,
512 const char *);
513 const struct got_error *got_get_repo_commits(struct request *, int);
514 const struct got_error *got_get_repo_tags(struct request *, int);
515 const struct got_error *got_get_repo_heads(struct request *);
516 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
517 int got_output_repo_tree(struct request *,
518 int (*)(struct template *, struct got_tree_entry *));
519 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
520 int *, int *, struct request *);
521 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
522 int (*)(struct template *, const char *, size_t));
523 const struct got_error *got_output_file_blame(struct request *,
524 got_render_blame_line_cb);
526 /* config.c */
527 int config_setserver(struct gotwebd *, struct server *);
528 int config_getserver(struct gotwebd *, struct imsg *);
529 int config_setsock(struct gotwebd *, struct socket *);
530 int config_getsock(struct gotwebd *, struct imsg *);
531 int config_setfd(struct gotwebd *, struct socket *);
532 int config_getfd(struct gotwebd *, struct imsg *);
533 int config_getcfg(struct gotwebd *, struct imsg *);
534 int config_init(struct gotwebd *);