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;
121 enum imsg_type {
122 IMSG_CFG_SRV = IMSG_PROC_MAX,
123 IMSG_CFG_SOCK,
124 IMSG_CFG_FD,
125 IMSG_CFG_DONE,
126 IMSG_CTL_START,
127 };
129 struct env_val {
130 SLIST_ENTRY(env_val) entry;
131 char *val;
132 };
133 SLIST_HEAD(env_head, env_val);
135 struct fcgi_record_header {
136 uint8_t version;
137 uint8_t type;
138 uint16_t id;
139 uint16_t content_len;
140 uint8_t padding_len;
141 uint8_t reserved;
142 }__attribute__((__packed__));
144 struct repo_dir {
145 char *name;
146 char *owner;
147 char *description;
148 char *url;
149 char *age;
150 char *path;
151 };
153 struct repo_tag {
154 TAILQ_ENTRY(repo_tag) entry;
155 char *commit_id;
156 char *tag_name;
157 char *tag_commit;
158 char *commit_msg;
159 char *tagger;
160 time_t tagger_time;
161 };
163 struct repo_commit {
164 TAILQ_ENTRY(repo_commit) entry;
165 char *path;
166 char *refs_str;
167 char *commit_id; /* id_str1 */
168 char *parent_id; /* id_str2 */
169 char *tree_id;
170 char *author;
171 char *committer;
172 char *commit_msg;
173 time_t committer_time;
174 };
176 struct got_repository;
177 struct transport {
178 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
179 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
180 struct got_repository *repo;
181 struct repo_dir *repo_dir;
182 struct querystring *qs;
183 char *next_id;
184 char *prev_id;
185 unsigned int repos_total;
186 unsigned int next_disp;
187 unsigned int prev_disp;
188 unsigned int tag_count;
189 };
191 enum socket_priv_fds {
192 DIFF_FD_1,
193 DIFF_FD_2,
194 DIFF_FD_3,
195 DIFF_FD_4,
196 DIFF_FD_5,
197 BLAME_FD_1,
198 BLAME_FD_2,
199 BLAME_FD_3,
200 BLAME_FD_4,
201 BLAME_FD_5,
202 BLAME_FD_6,
203 BLOB_FD_1,
204 BLOB_FD_2,
205 PRIV_FDS__MAX,
206 };
208 struct template;
209 struct request {
210 struct socket *sock;
211 struct server *srv;
212 struct transport *t;
213 struct template *tp;
214 struct event ev;
215 struct event tmo;
217 uint16_t id;
218 int fd;
219 int priv_fd[PRIV_FDS__MAX];
221 uint8_t buf[FCGI_RECORD_SIZE];
222 size_t buf_pos;
223 size_t buf_len;
225 uint8_t outbuf[GOTWEBD_CACHESIZE];
226 size_t outbuf_len;
228 char querystring[MAX_QUERYSTRING];
229 char http_host[GOTWEBD_MAXTEXT];
230 char document_uri[MAX_DOCUMENT_URI];
231 char server_name[MAX_SERVER_NAME];
232 int https;
234 uint8_t request_started;
235 };
237 struct fcgi_begin_request_body {
238 uint16_t role;
239 uint8_t flags;
240 uint8_t reserved[5];
241 }__attribute__((__packed__));
243 struct fcgi_end_request_body {
244 uint32_t app_status;
245 uint8_t protocol_status;
246 uint8_t reserved[3];
247 }__attribute__((__packed__));
249 struct address {
250 TAILQ_ENTRY(address) entry;
251 struct sockaddr_storage ss;
252 int ipproto;
253 int prefixlen;
254 in_port_t port;
255 char ifname[IFNAMSIZ];
256 };
257 TAILQ_HEAD(addresslist, address);
259 struct cached_repo {
260 char path[PATH_MAX];
261 struct got_repository *repo;
262 };
264 struct server {
265 TAILQ_ENTRY(server) entry;
266 struct addresslist al;
268 struct cached_repo *cached_repos;
269 int ncached_repos;
271 char name[GOTWEBD_MAXTEXT];
273 char repos_path[PATH_MAX];
274 char site_name[GOTWEBD_MAXNAME];
275 char site_owner[GOTWEBD_MAXNAME];
276 char site_link[GOTWEBD_MAXTEXT];
277 char logo[GOTWEBD_MAXTEXT];
278 char logo_url[GOTWEBD_MAXTEXT];
279 char custom_css[PATH_MAX];
281 size_t max_repos;
282 size_t max_repos_display;
283 size_t max_commits_display;
285 int show_site_owner;
286 int show_repo_owner;
287 int show_repo_age;
288 int show_repo_description;
289 int show_repo_cloneurl;
290 int respect_exportok;
292 int unix_socket;
293 char unix_socket_name[PATH_MAX];
295 int fcgi_socket;
296 };
297 TAILQ_HEAD(serverlist, server);
299 enum client_action {
300 CLIENT_CONNECT,
301 CLIENT_DISCONNECT,
302 };
304 struct socket_conf {
305 struct address addr;
307 char name[GOTWEBD_MAXTEXT];
308 char srv_name[GOTWEBD_MAXTEXT];
310 int id;
311 int af_type;
312 char unix_socket_name[PATH_MAX];
313 in_port_t fcgi_socket_port;
314 };
316 struct socket {
317 TAILQ_ENTRY(socket) entry;
318 struct socket_conf conf;
320 int fd;
321 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
322 int priv_fd[PRIV_FDS__MAX];
324 struct event evt;
325 struct event ev;
326 struct event pause;
328 int client_status;
329 };
330 TAILQ_HEAD(socketlist, socket);
332 struct gotwebd {
333 struct serverlist servers;
334 struct socketlist sockets;
336 struct privsep *gotwebd_ps;
337 const char *gotwebd_conffile;
339 int gotwebd_debug;
340 int gotwebd_verbose;
341 int gotwebd_noaction;
343 uint16_t prefork_gotwebd;
344 int gotwebd_reload;
346 int server_cnt;
348 char httpd_chroot[PATH_MAX];
350 int unix_socket;
351 char unix_socket_name[PATH_MAX];
352 };
354 /*
355 * URL parameter for gotweb_link. NULL values and int set to -1 are
356 * implicitly ignored, and string are properly escaped.
357 */
358 struct gotweb_url {
359 int action;
360 int index_page;
361 int page;
362 const char *commit;
363 const char *previd;
364 const char *prevset;
365 const char *file;
366 const char *folder;
367 const char *headref;
368 const char *path;
369 };
371 struct querystring {
372 uint8_t action;
373 char *commit;
374 char *previd;
375 char *prevset;
376 char *file;
377 char *folder;
378 char *headref;
379 int index_page;
380 char *path;
381 int page;
382 };
384 struct querystring_keys {
385 const char *name;
386 int element;
387 };
389 struct action_keys {
390 const char *name;
391 int action;
392 };
394 enum querystring_elements {
395 ACTION,
396 COMMIT,
397 RFILE,
398 FOLDER,
399 HEADREF,
400 INDEX_PAGE,
401 PATH,
402 PAGE,
403 PREVID,
404 QSELEM__MAX,
405 };
407 enum query_actions {
408 BLAME,
409 BLOB,
410 BLOBRAW,
411 BRIEFS,
412 COMMITS,
413 DIFF,
414 ERR,
415 INDEX,
416 SUMMARY,
417 TAG,
418 TAGS,
419 TREE,
420 RSS,
421 ACTIONS__MAX,
422 };
424 enum gotweb_ref_tm {
425 TM_DIFF,
426 TM_LONG,
427 TM_RFC822,
428 };
430 extern struct gotwebd *gotwebd_env;
432 /* sockets.c */
433 void sockets(struct privsep *, struct privsep_proc *);
434 void sockets_shutdown(void);
435 void sockets_parse_sockets(struct gotwebd *);
436 void sockets_socket_accept(int, short, void *);
437 int sockets_privinit(struct gotwebd *, struct socket *);
439 /* gotweb.c */
440 const struct got_error *gotweb_render_content_type(struct request *,
441 const char *);
442 const struct got_error
443 *gotweb_render_content_type_file(struct request *, const char *,
444 const char *, const char *);
445 void gotweb_get_navs(struct request *, struct gotweb_url *, int *,
446 struct gotweb_url *, int *);
447 const struct got_error *gotweb_get_time_str(char **, time_t, int);
448 const struct got_error *gotweb_init_transport(struct transport **);
449 const struct got_error *gotweb_escape_html(char **, const char *);
450 const char *gotweb_action_name(int);
451 int gotweb_render_url(struct request *, struct gotweb_url *);
452 int gotweb_render_absolute_url(struct request *, struct gotweb_url *);
453 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
454 __attribute__((__format__(printf, 3, 4)))
455 __attribute__((__nonnull__(3)));
456 void gotweb_free_repo_commit(struct repo_commit *);
457 void gotweb_free_repo_tag(struct repo_tag *);
458 void gotweb_process_request(struct request *);
459 void gotweb_free_transport(struct transport *);
461 /* pages.tmpl */
462 int gotweb_render_header(struct template *);
463 int gotweb_render_footer(struct template *);
464 int gotweb_render_repo_table_hdr(struct template *);
465 int gotweb_render_repo_fragment(struct template *, struct repo_dir *);
466 int gotweb_render_briefs(struct template *);
467 int gotweb_render_navs(struct template *);
468 int gotweb_render_commits(struct template *);
469 int gotweb_render_blob(struct template *, struct got_blob_object *);
470 int gotweb_render_rss(struct template *);
472 /* parse.y */
473 int parse_config(const char *, struct gotwebd *);
474 int cmdline_symset(char *);
476 /* fcgi.c */
477 void fcgi_request(int, short, void *);
478 void fcgi_timeout(int, short, void *);
479 void fcgi_cleanup_request(struct request *);
480 void fcgi_create_end_record(struct request *);
481 void dump_fcgi_record(const char *, struct fcgi_record_header *);
482 int fcgi_puts(struct template *, const char *);
483 int fcgi_putc(struct template *, int);
484 int fcgi_vprintf(struct request *, const char *, va_list);
485 int fcgi_printf(struct request *, const char *, ...)
486 __attribute__((__format__(printf, 2, 3)))
487 __attribute__((__nonnull__(2)));
488 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
490 /* got_operations.c */
491 const struct got_error *got_get_repo_owner(char **, struct request *);
492 const struct got_error *got_get_repo_age(char **, struct request *,
493 const char *, int);
494 const struct got_error *got_get_repo_commits(struct request *, int);
495 const struct got_error *got_get_repo_tags(struct request *, int);
496 const struct got_error *got_get_repo_heads(struct request *);
497 const struct got_error *got_output_repo_diff(struct request *);
498 const struct got_error *got_output_repo_tree(struct request *);
499 const struct got_error *got_open_blob_for_output(struct got_blob_object **,
500 int *, int *, struct request *);
501 const struct got_error *got_output_file_blob(struct request *);
502 int got_output_blob_by_lines(struct template *, struct got_blob_object *,
503 int (*)(struct template *, const char *, size_t));
504 const struct got_error *got_output_file_blame(struct request *);
506 /* config.c */
507 int config_setserver(struct gotwebd *, struct server *);
508 int config_getserver(struct gotwebd *, struct imsg *);
509 int config_setsock(struct gotwebd *, struct socket *);
510 int config_getsock(struct gotwebd *, struct imsg *);
511 int config_setfd(struct gotwebd *, struct socket *);
512 int config_getfd(struct gotwebd *, struct imsg *);
513 int config_getcfg(struct gotwebd *, struct imsg *);
514 int config_init(struct gotwebd *);