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