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_CACHESIZE 1024
43 #define GOTWEBD_MAXCLIENTS 1024
44 #define GOTWEBD_MAXTEXT 511
45 #define GOTWEBD_MAXNAME 64
46 #define GOTWEBD_MAXPORT 6
47 #define GOTWEBD_NUMPROC 3
48 #define GOTWEBD_MAXIFACE 16
49 #define GOTWEBD_REPO_CACHESIZE 4
51 /* GOTWEB DEFAULTS */
52 #define MAX_QUERYSTRING 2048
53 #define MAX_SCRIPT_NAME 255
54 #define MAX_SERVER_NAME 255
56 #define GOTWEB_GIT_DIR ".git"
58 #define D_HTTPD_CHROOT "/var/www"
59 #define D_UNIX_SOCKET "/run/gotweb.sock"
60 #define D_FCGI_PORT "9000"
61 #define D_GOTPATH "/got/public"
62 #define D_SITENAME "Gotweb"
63 #define D_SITEOWNER "Got Owner"
64 #define D_SITELINK "Repos"
65 #define D_GOTLOGO "got.png"
66 #define D_GOTURL "https://gameoftrees.org"
67 #define D_GOTWEBCSS "gotweb.css"
69 #define D_SHOWROWNER 1
70 #define D_SHOWSOWNER 1
71 #define D_SHOWAGE 1
72 #define D_SHOWDESC 1
73 #define D_SHOWURL 1
74 #define D_RESPECTEXPORTOK 0
75 #define D_MAXREPO 0
76 #define D_MAXREPODISP 25
77 #define D_MAXSLCOMMDISP 10
78 #define D_MAXCOMMITDISP 25
80 #define BUF 8192
82 #define TIMEOUT_DEFAULT 120
84 #define FCGI_CONTENT_SIZE 65535
85 #define FCGI_PADDING_SIZE 255
86 #define FCGI_RECORD_SIZE \
87 (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
89 #define FCGI_ALIGNMENT 8
90 #define FCGI_ALIGN(n) \
91 (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
93 #define FD_RESERVE 5
94 #define FD_NEEDED 6
96 #define FCGI_BEGIN_REQUEST 1
97 #define FCGI_ABORT_REQUEST 2
98 #define FCGI_END_REQUEST 3
99 #define FCGI_PARAMS 4
100 #define FCGI_STDIN 5
101 #define FCGI_STDOUT 6
102 #define FCGI_STDERR 7
103 #define FCGI_DATA 8
104 #define FCGI_GET_VALUES 9
105 #define FCGI_GET_VALUES_RESULT 10
106 #define FCGI_UNKNOWN_TYPE 11
107 #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
109 #define FCGI_REQUEST_COMPLETE 0
110 #define FCGI_CANT_MPX_CONN 1
111 #define FCGI_OVERLOADED 2
112 #define FCGI_UNKNOWN_ROLE 3
114 #define GOTWEB_PACK_NUM_TEMPFILES 32
116 enum imsg_type {
117 IMSG_CFG_SRV = IMSG_PROC_MAX,
118 IMSG_CFG_SOCK,
119 IMSG_CFG_FD,
120 IMSG_CFG_DONE,
121 IMSG_CTL_START,
122 };
124 struct env_val {
125 SLIST_ENTRY(env_val) entry;
126 char *val;
127 };
128 SLIST_HEAD(env_head, env_val);
130 struct fcgi_record_header {
131 uint8_t version;
132 uint8_t type;
133 uint16_t id;
134 uint16_t content_len;
135 uint8_t padding_len;
136 uint8_t reserved;
137 }__attribute__((__packed__));
139 struct repo_dir {
140 char *name;
141 char *owner;
142 char *description;
143 char *url;
144 char *age;
145 char *path;
146 };
148 struct repo_tag {
149 TAILQ_ENTRY(repo_tag) entry;
150 char *commit_id;
151 char *tag_name;
152 char *tag_commit;
153 char *commit_msg;
154 char *tagger;
155 time_t tagger_time;
156 };
158 struct repo_commit {
159 TAILQ_ENTRY(repo_commit) entry;
160 char *path;
161 char *refs_str;
162 char *commit_id; /* id_str1 */
163 char *parent_id; /* id_str2 */
164 char *tree_id;
165 char *author;
166 char *committer;
167 char *commit_msg;
168 time_t committer_time;
169 };
171 struct got_repository;
172 struct transport {
173 TAILQ_HEAD(repo_commits_head, repo_commit) repo_commits;
174 TAILQ_HEAD(repo_tags_head, repo_tag) repo_tags;
175 struct got_repository *repo;
176 struct repo_dir *repo_dir;
177 struct querystring *qs;
178 char *next_id;
179 char *prev_id;
180 unsigned int repos_total;
181 unsigned int next_disp;
182 unsigned int prev_disp;
183 unsigned int tag_count;
184 };
186 enum socket_priv_fds {
187 DIFF_FD_1,
188 DIFF_FD_2,
189 DIFF_FD_3,
190 DIFF_FD_4,
191 DIFF_FD_5,
192 BLAME_FD_1,
193 BLAME_FD_2,
194 BLAME_FD_3,
195 BLAME_FD_4,
196 BLAME_FD_5,
197 BLAME_FD_6,
198 BLOB_FD_1,
199 BLOB_FD_2,
200 PRIV_FDS__MAX,
201 };
203 struct request {
204 struct socket *sock;
205 struct server *srv;
206 struct transport *t;
207 struct event ev;
208 struct event tmo;
210 uint16_t id;
211 int fd;
212 int priv_fd[PRIV_FDS__MAX];
214 uint8_t buf[FCGI_RECORD_SIZE];
215 size_t buf_pos;
216 size_t buf_len;
218 uint8_t outbuf[GOTWEBD_CACHESIZE];
219 size_t outbuf_len;
221 char querystring[MAX_QUERYSTRING];
222 char http_host[GOTWEBD_MAXTEXT];
223 char script_name[MAX_SCRIPT_NAME];
224 char server_name[MAX_SERVER_NAME];
226 uint8_t request_started;
227 };
229 struct fcgi_begin_request_body {
230 uint16_t role;
231 uint8_t flags;
232 uint8_t reserved[5];
233 }__attribute__((__packed__));
235 struct fcgi_end_request_body {
236 uint32_t app_status;
237 uint8_t protocol_status;
238 uint8_t reserved[3];
239 }__attribute__((__packed__));
241 struct address {
242 TAILQ_ENTRY(address) entry;
243 struct sockaddr_storage ss;
244 int ipproto;
245 int prefixlen;
246 in_port_t port;
247 char ifname[IFNAMSIZ];
248 };
249 TAILQ_HEAD(addresslist, address);
251 struct cached_repo {
252 char path[PATH_MAX];
253 struct got_repository *repo;
254 };
256 struct server {
257 TAILQ_ENTRY(server) entry;
258 struct addresslist al;
260 struct cached_repo *cached_repos;
261 int ncached_repos;
263 char name[GOTWEBD_MAXTEXT];
265 char repos_path[PATH_MAX];
266 char site_name[GOTWEBD_MAXNAME];
267 char site_owner[GOTWEBD_MAXNAME];
268 char site_link[GOTWEBD_MAXTEXT];
269 char logo[GOTWEBD_MAXTEXT];
270 char logo_url[GOTWEBD_MAXTEXT];
271 char custom_css[PATH_MAX];
273 size_t max_repos;
274 size_t max_repos_display;
275 size_t max_commits_display;
277 int show_site_owner;
278 int show_repo_owner;
279 int show_repo_age;
280 int show_repo_description;
281 int show_repo_cloneurl;
282 int respect_exportok;
284 int unix_socket;
285 char unix_socket_name[PATH_MAX];
287 int fcgi_socket;
288 };
289 TAILQ_HEAD(serverlist, server);
291 enum client_action {
292 CLIENT_CONNECT,
293 CLIENT_DISCONNECT,
294 };
296 struct socket_conf {
297 struct address addr;
299 char name[GOTWEBD_MAXTEXT];
300 char srv_name[GOTWEBD_MAXTEXT];
302 int id;
303 int af_type;
304 char unix_socket_name[PATH_MAX];
305 in_port_t fcgi_socket_port;
306 };
308 struct socket {
309 TAILQ_ENTRY(socket) entry;
310 struct socket_conf conf;
312 int fd;
313 int pack_fds[GOTWEB_PACK_NUM_TEMPFILES];
314 int priv_fd[PRIV_FDS__MAX];
316 struct event evt;
317 struct event ev;
318 struct event pause;
320 int client_status;
321 };
322 TAILQ_HEAD(socketlist, socket);
324 struct gotwebd {
325 struct serverlist servers;
326 struct socketlist sockets;
328 struct privsep *gotwebd_ps;
329 const char *gotwebd_conffile;
331 int gotwebd_debug;
332 int gotwebd_verbose;
333 int gotwebd_noaction;
335 uint16_t prefork_gotwebd;
336 int gotwebd_reload;
338 int server_cnt;
340 char httpd_chroot[PATH_MAX];
342 int unix_socket;
343 char unix_socket_name[PATH_MAX];
344 };
346 /*
347 * URL parameter for gotweb_link. NULL values and int set to -1 are
348 * implicitly ignored, and string are properly escaped.
349 */
350 struct gotweb_url {
351 int action;
352 int index_page;
353 int page;
354 const char *commit;
355 const char *previd;
356 const char *prevset;
357 const char *file;
358 const char *folder;
359 const char *headref;
360 const char *path;
361 };
363 struct querystring {
364 uint8_t action;
365 char *commit;
366 char *previd;
367 char *prevset;
368 char *file;
369 char *folder;
370 char *headref;
371 int index_page;
372 char *index_page_str;
373 char *path;
374 int page;
375 char *page_str;
376 };
378 struct querystring_keys {
379 const char *name;
380 int element;
381 };
383 struct action_keys {
384 const char *name;
385 int action;
386 };
388 enum querystring_elements {
389 ACTION,
390 COMMIT,
391 RFILE,
392 FOLDER,
393 HEADREF,
394 INDEX_PAGE,
395 PATH,
396 PAGE,
397 PREVID,
398 QSELEM__MAX,
399 };
401 enum query_actions {
402 BLAME,
403 BLOB,
404 BRIEFS,
405 COMMITS,
406 DIFF,
407 ERR,
408 INDEX,
409 SUMMARY,
410 TAG,
411 TAGS,
412 TREE,
413 ACTIONS__MAX,
414 };
416 extern struct gotwebd *gotwebd_env;
418 /* sockets.c */
419 void sockets(struct privsep *, struct privsep_proc *);
420 void sockets_shutdown(void);
421 void sockets_parse_sockets(struct gotwebd *);
422 void sockets_socket_accept(int, short, void *);
423 int sockets_privinit(struct gotwebd *, struct socket *);
425 /* gotweb.c */
426 const struct got_error *gotweb_render_content_type(struct request *,
427 const uint8_t *);
428 const struct got_error
429 *gotweb_render_content_type_file(struct request *, const uint8_t *, char *);
430 const struct got_error *gotweb_get_time_str(char **, time_t, int);
431 const struct got_error *gotweb_init_transport(struct transport **);
432 const struct got_error *gotweb_escape_html(char **, const char *);
433 int gotweb_link(struct request *, struct gotweb_url *, const char *, ...)
434 __attribute__((__format__(printf, 3, 4)))
435 __attribute__((__nonnull__(3)));
436 void gotweb_free_repo_commit(struct repo_commit *);
437 void gotweb_free_repo_tag(struct repo_tag *);
438 void gotweb_process_request(struct request *);
439 void gotweb_free_transport(struct transport *);
441 /* parse.y */
442 int parse_config(const char *, struct gotwebd *);
443 int cmdline_symset(char *);
445 /* fcgi.c */
446 void fcgi_request(int, short, void *);
447 void fcgi_timeout(int, short, void *);
448 void fcgi_cleanup_request(struct request *);
449 void fcgi_create_end_record(struct request *);
450 void dump_fcgi_record(const char *, struct fcgi_record_header *);
451 int fcgi_vprintf(struct request *, const char *, va_list);
452 int fcgi_printf(struct request *, const char *, ...)
453 __attribute__((__format__(printf, 2, 3)))
454 __attribute__((__nonnull__(2)));
455 int fcgi_gen_binary_response(struct request *, const uint8_t *, int);
457 /* got_operations.c */
458 const struct got_error *got_get_repo_owner(char **, struct request *, char *);
459 const struct got_error *got_get_repo_age(char **, struct request *, char *,
460 const char *, int);
461 const struct got_error *got_get_repo_commits(struct request *, int);
462 const struct got_error *got_get_repo_tags(struct request *, int);
463 const struct got_error *got_get_repo_heads(struct request *);
464 const struct got_error *got_output_repo_diff(struct request *);
465 const struct got_error *got_output_repo_tree(struct request *);
466 const struct got_error *got_output_file_blob(struct request *);
467 const struct got_error *got_output_file_blame(struct request *);
469 /* config.c */
470 int config_setserver(struct gotwebd *, struct server *);
471 int config_getserver(struct gotwebd *, struct imsg *);
472 int config_setsock(struct gotwebd *, struct socket *);
473 int config_getsock(struct gotwebd *, struct imsg *);
474 int config_setfd(struct gotwebd *, struct socket *);
475 int config_getfd(struct gotwebd *, struct imsg *);
476 int config_getcfg(struct gotwebd *, struct imsg *);
477 int config_init(struct gotwebd *);