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
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 repo_dir {
147 char *name;
148 char *owner;
149 char *description;
150 char *url;
151 char *age;
152 char *path;
153 };
155 struct repo_tag {
156 TAILQ_ENTRY(repo_tag) entry;
157 char *commit_id;
158 char *tag_name;
159 char *tag_commit;
160 char *commit_msg;
161 char *tagger;
162 time_t tagger_time;
163 };
165 struct repo_commit {
166 TAILQ_ENTRY(repo_commit) entry;
167 char *path;
168 char *refs_str;
169 char *commit_id; /* id_str1 */
170 char *parent_id; /* id_str2 */
171 char *tree_id;
172 char *author;
173 char *committer;
174 char *commit_msg;
175 time_t committer_time;
176 };
178 struct got_repository;
179 struct transport {
180 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
181 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
182 struct got_repository *repo;
183 struct repo_dir *repo_dir;
184 struct querystring *qs;
185 char *next_id;
186 char *prev_id;
187 unsigned int repos_total;
188 unsigned int next_disp;
189 unsigned int prev_disp;
190 unsigned int tag_count;
191 };
193 enum socket_priv_fds {
194 DIFF_FD_1,
195 DIFF_FD_2,
196 DIFF_FD_3,
197 DIFF_FD_4,
198 DIFF_FD_5,
199 BLAME_FD_1,
200 BLAME_FD_2,
201 BLAME_FD_3,
202 BLAME_FD_4,
203 BLAME_FD_5,
204 BLAME_FD_6,
205 BLOB_FD_1,
206 BLOB_FD_2,
207 PRIV_FDS__MAX,
208 };
210 struct template;
211 struct request {
212 struct socket *sock;
213 struct server *srv;
214 struct transport *t;
215 struct template *tp;
216 struct event ev;
217 struct event tmo;
219 uint16_t id;
220 int fd;
221 int priv_fd[PRIV_FDS__MAX];
223 uint8_t buf[FCGI_RECORD_SIZE];
224 size_t buf_pos;
225 size_t buf_len;
227 uint8_t outbuf[GOTWEBD_CACHESIZE];
228 size_t outbuf_len;
230 char querystring[MAX_QUERYSTRING];
231 char http_host[GOTWEBD_MAXTEXT];
232 char document_uri[MAX_DOCUMENT_URI];
233 char server_name[MAX_SERVER_NAME];
234 int https;
236 uint8_t request_started;
237 };
239 struct fcgi_begin_request_body {
240 uint16_t role;
241 uint8_t flags;
242 uint8_t reserved[5];
243 }__attribute__((__packed__));
245 struct fcgi_end_request_body {
246 uint32_t app_status;
247 uint8_t protocol_status;
248 uint8_t reserved[3];
249 }__attribute__((__packed__));
251 struct address {
252 TAILQ_ENTRY(address) entry;
253 struct sockaddr_storage ss;
254 int ipproto;
255 int prefixlen;
256 in_port_t port;
257 char ifname[IFNAMSIZ];
258 };
259 TAILQ_HEAD(addresslist, address);
261 struct cached_repo {
262 char path[PATH_MAX];
263 struct got_repository *repo;
264 };
266 struct server {
267 TAILQ_ENTRY(server) entry;
268 struct addresslist al;
270 struct cached_repo *cached_repos;
271 int ncached_repos;
273 char name[GOTWEBD_MAXTEXT];
275 char repos_path[PATH_MAX];
276 char site_name[GOTWEBD_MAXNAME];
277 char site_owner[GOTWEBD_MAXNAME];
278 char site_link[GOTWEBD_MAXTEXT];
279 char logo[GOTWEBD_MAXTEXT];
280 char logo_url[GOTWEBD_MAXTEXT];
281 char custom_css[PATH_MAX];
283 size_t max_repos;
284 size_t max_repos_display;
285 size_t max_commits_display;
287 int show_site_owner;
288 int show_repo_owner;
289 int show_repo_age;
290 int show_repo_description;
291 int show_repo_cloneurl;
292 int respect_exportok;
294 int unix_socket;
295 char unix_socket_name[PATH_MAX];
297 int fcgi_socket;
298 };
299 TAILQ_HEAD(serverlist, server);
301 enum client_action {
302 CLIENT_CONNECT,
303 CLIENT_DISCONNECT,
304 };
306 struct socket_conf {
307 struct address addr;
309 char name[GOTWEBD_MAXTEXT];
310 char srv_name[GOTWEBD_MAXTEXT];
312 int id;
313 int af_type;
314 char unix_socket_name[PATH_MAX];
315 in_port_t fcgi_socket_port;
316 };
318 struct socket {
319 TAILQ_ENTRY(socket) entry;
320 struct socket_conf conf;
322 int fd;
323 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
324 int priv_fd[PRIV_FDS__MAX];
326 struct event evt;
327 struct event ev;
328 struct event pause;
330 int client_status;
331 };
332 TAILQ_HEAD(socketlist, socket);
334 struct gotwebd {
335 struct serverlist servers;
336 struct socketlist sockets;
338 struct privsep *gotwebd_ps;
339 const char *gotwebd_conffile;
341 int gotwebd_debug;
342 int gotwebd_verbose;
343 int gotwebd_noaction;
345 uint16_t prefork_gotwebd;
346 int gotwebd_reload;
348 int server_cnt;
350 char httpd_chroot[PATH_MAX];
352 int unix_socket;
353 char unix_socket_name[PATH_MAX];
354 };
356 /*
357 * URL parameter for gotweb_link. NULL values and int set to -1 are
358 * implicitly ignored, and string are properly escaped.
359 */
360 struct gotweb_url {
361 int action;
362 int index_page;
363 int page;
364 const char *commit;
365 const char *previd;
366 const char *prevset;
367 const char *file;
368 const char *folder;
369 const char *headref;
370 const char *path;
371 };
373 struct querystring {
374 uint8_t action;
375 char *commit;
376 char *previd;
377 char *prevset;
378 char *file;
379 char *folder;
380 char *headref;
381 int index_page;
382 char *path;
383 int page;
384 };
386 struct querystring_keys {
387 const char *name;
388 int element;
389 };
391 struct action_keys {
392 const char *name;
393 int action;
394 };
396 enum querystring_elements {
397 ACTION,
398 COMMIT,
399 RFILE,
400 FOLDER,
401 HEADREF,
402 INDEX_PAGE,
403 PATH,
404 PAGE,
405 PREVID,
406 QSELEM__MAX,
407 };
409 enum query_actions {
410 BLAME,
411 BLOB,
412 BLOBRAW,
413 BRIEFS,
414 COMMITS,
415 DIFF,
416 ERR,
417 INDEX,
418 SUMMARY,
419 TAG,
420 TAGS,
421 TREE,
422 RSS,
423 ACTIONS__MAX,
424 };
426 enum gotweb_ref_tm {
427 TM_DIFF,
428 TM_LONG,
429 TM_RFC822,
430 };
432 extern struct gotwebd *gotwebd_env;
434 /* sockets.c */
435 void sockets(struct privsep *, struct privsep_proc *);
436 void sockets_shutdown(void);
437 void sockets_parse_sockets(struct gotwebd *);
438 void sockets_socket_accept(int, short, void *);
439 int sockets_privinit(struct gotwebd *, struct socket *);
441 /* gotweb.c */
442 const struct got_error *gotweb_render_content_type(struct request *,
443 const char *);
444 const struct got_error
445 *gotweb_render_content_type_file(struct request *, const char *,
446 const char *, const char *);
447 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
448 struct gotweb_url *, int *);
449 const struct got_error *gotweb_get_time_str(char **, time_t, int);
450 const struct got_error *gotweb_init_transport(struct transport **);
451 const struct got_error *gotweb_escape_html(char **, const char *);
452 const char *gotweb_action_name(int);
453 int gotweb_render_url(struct request *, struct gotweb_url *);
454 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
455 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
456 __attribute__((__format__(printf, 3, 4)))
457 __attribute__((__nonnull__(3)));
458 void gotweb_free_repo_commit(struct repo_commit *);
459 void gotweb_free_repo_tag(struct repo_tag *);
460 void gotweb_process_request(struct request *);
461 void gotweb_free_transport(struct transport *);
463 /* pages.tmpl */
464 int gotweb_render_header(struct template *);
465 int gotweb_render_footer(struct template *);
466 int gotweb_render_repo_table_hdr(struct template *);
467 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
468 int gotweb_render_briefs(struct template *);
469 int gotweb_render_navs(struct template *);
470 int gotweb_render_commits(struct template *);
471 int gotweb_render_blob(struct template *, struct got_blob_object *);
472 int gotweb_render_tree(struct template *);
473 int gotweb_render_tags_tmpl(struct template *);
474 int gotweb_render_tag(struct template *);
475 int gotweb_render_diff(struct template *, FILE *);
476 int gotweb_render_branches(struct template *, struct got_reflist_head *);
477 int gotweb_render_rss(struct template *);
479 /* parse.y */
480 int parse_config(const char *, struct gotwebd *);
481 int cmdline_symset(char *);
483 /* fcgi.c */
484 void fcgi_request(int, short, void *);
485 void fcgi_timeout(int, short, void *);
486 void fcgi_cleanup_request(struct request *);
487 void fcgi_create_end_record(struct request *);
488 void dump_fcgi_record(const char *, struct fcgi_record_header *);
489 int fcgi_puts(struct template *, const char *);
490 int fcgi_putc(struct template *, int);
491 int fcgi_vprintf(struct request *, const char *, va_list);
492 int fcgi_printf(struct request *, const char *, ...)
493 __attribute__((__format__(printf, 2, 3)))
494 __attribute__((__nonnull__(2)));
495 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
497 /* got_operations.c */
498 const struct got_error *got_gotweb_flushfile(FILE *, int);
499 const struct got_error *got_get_repo_owner(char **, struct request *);
500 const struct got_error *got_get_repo_age(char **, struct request *,
501 const char *, int);
502 const struct got_error *got_get_repo_commits(struct request *, int);
503 const struct got_error *got_get_repo_tags(struct request *, int);
504 const struct got_error *got_get_repo_heads(struct request *);
505 const struct got_error *got_open_diff_for_output(FILE **, int *,
506 struct request *);
507 int got_output_repo_tree(struct request *,
508 int (*)(struct template *, struct got_tree_entry *));
509 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
510 int *, int *, struct request *);
511 const struct got_error *got_output_file_blob(struct request *);
512 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
513 int (*)(struct template *, const char *, size_t));
514 const struct got_error *got_output_file_blame(struct request *);
516 /* config.c */
517 int config_setserver(struct gotwebd *, struct server *);
518 int config_getserver(struct gotwebd *, struct imsg *);
519 int config_setsock(struct gotwebd *, struct socket *);
520 int config_getsock(struct gotwebd *, struct imsg *);
521 int config_setfd(struct gotwebd *, struct socket *);
522 int config_getfd(struct gotwebd *, struct imsg *);
523 int config_getcfg(struct gotwebd *, struct imsg *);
524 int config_init(struct gotwebd *);