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