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_SOCK_FILENO 3
52 #define PROC_MAX_INSTANCES 32
54 /* GOTWEB DEFAULTS */
55 #define MAX_QUERYSTRING 2048
56 #define MAX_DOCUMENT_URI 255
57 #define MAX_SERVER_NAME 255
59 #define GOTWEB_GIT_DIR ".git"
61 #define D_HTTPD_CHROOT "/var/www"
62 #define D_UNIX_SOCKET "/run/gotweb.sock"
63 #define D_FCGI_PORT "9000"
64 #define D_GOTPATH "/got/public"
65 #define D_SITENAME "Gotweb"
66 #define D_SITEOWNER "Got Owner"
67 #define D_SITELINK "Repos"
68 #define D_GOTLOGO "got.png"
69 #define D_GOTURL "https://gameoftrees.org"
70 #define D_GOTWEBCSS "gotweb.css"
72 #define D_SHOWROWNER 1
73 #define D_SHOWSOWNER 1
74 #define D_SHOWAGE 1
75 #define D_SHOWDESC 1
76 #define D_SHOWURL 1
77 #define D_RESPECTEXPORTOK 0
78 #define D_MAXREPODISP 25
79 #define D_MAXSLCOMMDISP 10
80 #define D_MAXCOMMITDISP 25
81 #define D_MAXSLTAGDISP 3
83 #define BUF 8192
85 #define TIMEOUT_DEFAULT 120
87 #define FCGI_CONTENT_SIZE 65535
88 #define FCGI_PADDING_SIZE 255
89 #define FCGI_RECORD_SIZE \
90 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
92 #define FCGI_ALIGNMENT 8
93 #define FCGI_ALIGN(n) \
94 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
96 #define FD_RESERVE 5
97 #define FD_NEEDED 6
99 #define FCGI_BEGIN_REQUEST 1
100 #define FCGI_ABORT_REQUEST 2
101 #define FCGI_END_REQUEST 3
102 #define FCGI_PARAMS 4
103 #define FCGI_STDIN 5
104 #define FCGI_STDOUT 6
105 #define FCGI_STDERR 7
106 #define FCGI_DATA 8
107 #define FCGI_GET_VALUES 9
108 #define FCGI_GET_VALUES_RESULT 10
109 #define FCGI_UNKNOWN_TYPE 11
110 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
112 #define FCGI_REQUEST_COMPLETE 0
113 #define FCGI_CANT_MPX_CONN 1
114 #define FCGI_OVERLOADED 2
115 #define FCGI_UNKNOWN_ROLE 3
117 #define GOTWEB_PACK_NUM_TEMPFILES (32 * 2)
119 /* Forward declaration */
120 struct got_blob_object;
121 struct got_tree_entry;
122 struct got_reflist_head;
124 enum imsg_type {
125 IMSG_CFG_SRV,
126 IMSG_CFG_SOCK,
127 IMSG_CFG_FD,
128 IMSG_CFG_DONE,
129 IMSG_CTL_START,
130 };
132 struct imsgev {
133 struct imsgbuf ibuf;
134 void (*handler)(int, short, void *);
135 struct event ev;
136 void *data;
137 short events;
138 };
140 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE)
142 struct env_val {
143 SLIST_ENTRY(env_val) entry;
144 char *val;
145 };
146 SLIST_HEAD(env_head, env_val);
148 struct fcgi_record_header {
149 uint8_t version;
150 uint8_t type;
151 uint16_t id;
152 uint16_t content_len;
153 uint8_t padding_len;
154 uint8_t reserved;
155 }__attribute__((__packed__));
157 struct blame_line {
158 int annotated;
159 char *id_str;
160 char *committer;
161 char datebuf[11]; /* YYYY-MM-DD + NUL */
162 };
164 struct repo_dir {
165 char *name;
166 char *owner;
167 char *description;
168 char *url;
169 time_t age;
170 char *path;
171 };
173 struct repo_tag {
174 TAILQ_ENTRY(repo_tag) entry;
175 char *commit_id;
176 char *tag_name;
177 char *tag_commit;
178 char *commit_msg;
179 char *tagger;
180 time_t tagger_time;
181 };
183 struct repo_commit {
184 TAILQ_ENTRY(repo_commit) entry;
185 char *path;
186 char *refs_str;
187 char *commit_id; /* id_str1 */
188 char *parent_id; /* id_str2 */
189 char *tree_id;
190 char *author;
191 char *committer;
192 char *commit_msg;
193 time_t committer_time;
194 };
196 struct got_repository;
197 struct transport {
198 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
199 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
200 struct got_reflist_head refs;
201 struct got_repository *repo;
202 struct repo_dir *repo_dir;
203 struct querystring *qs;
204 char *more_id;
205 char *tags_more_id;
206 unsigned int repos_total;
207 unsigned int next_disp;
208 unsigned int prev_disp;
209 unsigned int tag_count;
210 const struct got_error *error;
211 struct got_blob_object *blob;
212 int fd;
213 FILE *fp;
214 struct dirent **repos;
215 int nrepos;
216 };
218 enum socket_priv_fds {
219 DIFF_FD_1,
220 DIFF_FD_2,
221 DIFF_FD_3,
222 DIFF_FD_4,
223 DIFF_FD_5,
224 BLAME_FD_1,
225 BLAME_FD_2,
226 BLAME_FD_3,
227 BLAME_FD_4,
228 BLAME_FD_5,
229 BLAME_FD_6,
230 BLOB_FD_1,
231 BLOB_FD_2,
232 PRIV_FDS__MAX,
233 };
235 struct template;
236 struct request {
237 struct socket *sock;
238 struct server *srv;
239 struct transport *t;
240 struct template *tp;
241 struct event ev;
242 struct event tmo;
244 uint16_t id;
245 int fd;
246 int priv_fd[PRIV_FDS__MAX];
248 uint8_t buf[FCGI_RECORD_SIZE];
249 size_t buf_pos;
250 size_t buf_len;
252 uint8_t outbuf[GOTWEBD_CACHESIZE];
254 char querystring[MAX_QUERYSTRING];
255 char document_uri[MAX_DOCUMENT_URI];
256 char server_name[MAX_SERVER_NAME];
257 int https;
259 uint8_t request_started;
260 };
262 struct fcgi_begin_request_body {
263 uint16_t role;
264 uint8_t flags;
265 uint8_t reserved[5];
266 }__attribute__((__packed__));
268 struct fcgi_end_request_body {
269 uint32_t app_status;
270 uint8_t protocol_status;
271 uint8_t reserved[3];
272 }__attribute__((__packed__));
274 struct address {
275 TAILQ_ENTRY(address) entry;
276 struct sockaddr_storage ss;
277 socklen_t slen;
278 int ai_family;
279 int ai_socktype;
280 int ai_protocol;
281 in_port_t port;
282 char ifname[IFNAMSIZ];
283 };
284 TAILQ_HEAD(addresslist, address);
286 struct server {
287 TAILQ_ENTRY(server) entry;
289 char name[GOTWEBD_MAXTEXT];
291 char repos_path[PATH_MAX];
292 char site_name[GOTWEBD_MAXNAME];
293 char site_owner[GOTWEBD_MAXNAME];
294 char site_link[GOTWEBD_MAXTEXT];
295 char logo[GOTWEBD_MAXTEXT];
296 char logo_url[GOTWEBD_MAXTEXT];
297 char custom_css[PATH_MAX];
299 size_t max_repos_display;
300 size_t max_commits_display;
301 size_t summary_commits_display;
302 size_t summary_tags_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;
310 };
311 TAILQ_HEAD(serverlist, server);
313 enum client_action {
314 CLIENT_CONNECT,
315 CLIENT_DISCONNECT,
316 };
318 struct socket_conf {
319 struct address addr;
321 int id;
322 int af_type;
323 char unix_socket_name[PATH_MAX];
324 in_port_t fcgi_socket_port;
325 };
327 struct socket {
328 TAILQ_ENTRY(socket) entry;
329 struct socket_conf conf;
331 int fd;
332 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
333 int priv_fd[PRIV_FDS__MAX];
335 struct event evt;
336 struct event ev;
337 struct event pause;
339 int client_status;
340 };
341 TAILQ_HEAD(socketlist, socket);
343 struct passwd;
344 struct gotwebd {
345 struct serverlist servers;
346 struct socketlist sockets;
347 struct addresslist addresses;
349 const char *gotwebd_conffile;
351 int gotwebd_debug;
352 int gotwebd_verbose;
354 struct imsgev *iev_parent;
355 struct imsgev *iev_server;
356 size_t nserver;
358 struct passwd *pw;
360 uint16_t prefork_gotwebd;
361 int gotwebd_reload;
363 int server_cnt;
365 char httpd_chroot[PATH_MAX];
366 };
368 /*
369 * URL parameter for gotweb_render_url. NULL values and int set to -1
370 * are implicitly ignored, and string are properly escaped.
371 */
372 struct gotweb_url {
373 int action;
374 int index_page;
375 const char *commit;
376 const char *previd;
377 const char *prevset;
378 const char *file;
379 const char *folder;
380 const char *headref;
381 const char *path;
382 };
384 struct querystring {
385 uint8_t action;
386 char *commit;
387 char *previd;
388 char *prevset;
389 char *file;
390 char *folder;
391 char *headref;
392 int index_page;
393 char *path;
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 };
416 enum query_actions {
417 BLAME,
418 BLOB,
419 BLOBRAW,
420 BRIEFS,
421 COMMITS,
422 DIFF,
423 ERR,
424 INDEX,
425 PATCH,
426 SUMMARY,
427 TAG,
428 TAGS,
429 TREE,
430 RSS,
431 };
433 extern struct gotwebd *gotwebd_env;
435 typedef int (*got_render_blame_line_cb)(struct template *, const char *,
436 struct blame_line *, int, int);
438 /* gotwebd.c */
439 void imsg_event_add(struct imsgev *);
440 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
441 pid_t, int, const void *, uint16_t);
442 int main_compose_sockets(struct gotwebd *, uint32_t, int,
443 const void *, uint16_t);
444 int sockets_compose_main(struct gotwebd *, uint32_t,
445 const void *, uint16_t);
447 /* sockets.c */
448 void sockets(struct gotwebd *, int);
449 void sockets_parse_sockets(struct gotwebd *);
450 void sockets_socket_accept(int, short, void *);
451 int sockets_privinit(struct gotwebd *, struct socket *);
453 /* gotweb.c */
454 void gotweb_index_navs(struct request *, struct gotweb_url *, int *,
455 struct gotweb_url *, int *);
456 int gotweb_render_age(struct template *, time_t);
457 const struct got_error *gotweb_init_transport(struct transport **);
458 const char *gotweb_action_name(int);
459 int gotweb_render_url(struct request *, struct gotweb_url *);
460 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
461 void gotweb_free_repo_commit(struct repo_commit *);
462 void gotweb_free_repo_tag(struct repo_tag *);
463 void gotweb_process_request(struct request *);
464 void gotweb_free_transport(struct transport *);
466 /* pages.tmpl */
467 int gotweb_render_page(struct template *, int (*)(struct template *));
468 int gotweb_render_error(struct template *);
469 int gotweb_render_repo_table_hdr(struct template *);
470 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
471 int gotweb_render_briefs(struct template *);
472 int gotweb_render_navs(struct template *);
473 int gotweb_render_commits(struct template *);
474 int gotweb_render_blob(struct template *);
475 int gotweb_render_tree(struct template *);
476 int gotweb_render_tags(struct template *);
477 int gotweb_render_tag(struct template *);
478 int gotweb_render_diff(struct template *);
479 int gotweb_render_branches(struct template *, struct got_reflist_head *);
480 int gotweb_render_summary(struct template *);
481 int gotweb_render_blame(struct template *);
482 int gotweb_render_patch(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_write(void *, const void *, size_t);
497 /* got_operations.c */
498 const struct got_error *got_gotweb_closefile(FILE *);
499 const struct got_error *got_get_repo_owner(char **, struct request *);
500 const struct got_error *got_get_repo_age(time_t *, struct request *,
501 const char *);
502 const struct got_error *got_get_repo_commits(struct request *, size_t);
503 const struct got_error *got_get_repo_tags(struct request *, size_t);
504 const struct got_error *got_get_repo_heads(struct request *);
505 const struct got_error *got_open_diff_for_output(FILE **, struct request *);
506 int got_output_repo_tree(struct request *, char **,
507 int (*)(struct template *, struct got_tree_entry *));
508 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
509 int *, int *, struct request *, const char *, const char *, const char *);
510 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
511 int (*)(struct template *, const char *, size_t));
512 const struct got_error *got_output_file_blame(struct request *,
513 got_render_blame_line_cb);
515 /* config.c */
516 int config_setserver(struct gotwebd *, struct server *);
517 int config_getserver(struct gotwebd *, struct imsg *);
518 int config_setsock(struct gotwebd *, struct socket *);
519 int config_getsock(struct gotwebd *, struct imsg *);
520 int config_setfd(struct gotwebd *, struct socket *);
521 int config_getfd(struct gotwebd *, struct imsg *);
522 int config_getcfg(struct gotwebd *, struct imsg *);
523 int config_init(struct gotwebd *);
525 /* log.c */
526 void log_init(int, int);
527 void log_procinit(const char *);
528 void log_setverbose(int);
529 int log_getverbose(void);
530 void log_warn(const char *, ...)
531 __attribute__((__format__ (printf, 1, 2)));
532 void log_warnx(const char *, ...)
533 __attribute__((__format__ (printf, 1, 2)));
534 void log_info(const char *, ...)
535 __attribute__((__format__ (printf, 1, 2)));
536 void log_debug(const char *, ...)
537 __attribute__((__format__ (printf, 1, 2)));
538 void logit(int, const char *, ...)
539 __attribute__((__format__ (printf, 2, 3)));
540 void vlog(int, const char *, va_list)
541 __attribute__((__format__ (printf, 2, 0)));
542 __dead void fatal(const char *, ...)
543 __attribute__((__format__ (printf, 1, 2)));
544 __dead void fatalx(const char *, ...)
545 __attribute__((__format__ (printf, 1, 2)));