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