Blame


1 d5aba4c7 2020-12-24 op /*
2 d5aba4c7 2020-12-24 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 d5aba4c7 2020-12-24 op *
4 d5aba4c7 2020-12-24 op * Permission to use, copy, modify, and distribute this software for any
5 d5aba4c7 2020-12-24 op * purpose with or without fee is hereby granted, provided that the above
6 d5aba4c7 2020-12-24 op * copyright notice and this permission notice appear in all copies.
7 d5aba4c7 2020-12-24 op *
8 d5aba4c7 2020-12-24 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5aba4c7 2020-12-24 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5aba4c7 2020-12-24 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5aba4c7 2020-12-24 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5aba4c7 2020-12-24 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5aba4c7 2020-12-24 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5aba4c7 2020-12-24 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d5aba4c7 2020-12-24 op */
16 d5aba4c7 2020-12-24 op
17 488f059a 2020-12-24 op #ifndef GMID_H
18 488f059a 2020-12-24 op #define GMID_H
19 488f059a 2020-12-24 op
20 488f059a 2020-12-24 op #include <arpa/inet.h>
21 488f059a 2020-12-24 op #include <netinet/in.h>
22 28778244 2021-01-11 op #include <sys/socket.h>
23 488f059a 2020-12-24 op
24 488f059a 2020-12-24 op #include <poll.h>
25 488f059a 2020-12-24 op #include <stdio.h>
26 488f059a 2020-12-24 op #include <stdlib.h>
27 488f059a 2020-12-24 op #include <syslog.h>
28 488f059a 2020-12-24 op #include <tls.h>
29 488f059a 2020-12-24 op #include <unistd.h>
30 488f059a 2020-12-24 op
31 488f059a 2020-12-24 op #ifndef __OpenBSD__
32 488f059a 2020-12-24 op # define pledge(a, b) 0
33 488f059a 2020-12-24 op # define unveil(a, b) 0
34 488f059a 2020-12-24 op #endif
35 488f059a 2020-12-24 op
36 488f059a 2020-12-24 op #ifndef INFTIM
37 488f059a 2020-12-24 op # define INFTIM -1
38 488f059a 2020-12-24 op #endif
39 488f059a 2020-12-24 op
40 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
41 488f059a 2020-12-24 op
42 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
43 488f059a 2020-12-24 op #define PATHBUF 2048
44 488f059a 2020-12-24 op
45 488f059a 2020-12-24 op #define SUCCESS 20
46 488f059a 2020-12-24 op #define TEMP_FAILURE 40
47 488f059a 2020-12-24 op #define NOT_FOUND 51
48 7b1d9790 2021-01-11 op #define PROXY_REFUSED 53
49 488f059a 2020-12-24 op #define BAD_REQUEST 59
50 488f059a 2020-12-24 op
51 488f059a 2020-12-24 op #define MAX_USERS 64
52 488f059a 2020-12-24 op
53 15902770 2021-01-15 op #define HOSTSLEN 64
54 15902770 2021-01-15 op
55 15902770 2021-01-15 op struct vhost {
56 15902770 2021-01-15 op const char *domain;
57 15902770 2021-01-15 op const char *cert;
58 15902770 2021-01-15 op const char *key;
59 15902770 2021-01-15 op const char *dir;
60 15902770 2021-01-15 op const char *cgi;
61 15902770 2021-01-15 op int dirfd;
62 15902770 2021-01-15 op };
63 15902770 2021-01-15 op
64 15902770 2021-01-15 op extern struct vhost hosts[HOSTSLEN];
65 15902770 2021-01-15 op
66 15902770 2021-01-15 op struct conf {
67 15902770 2021-01-15 op int foreground;
68 15902770 2021-01-15 op int port;
69 15902770 2021-01-15 op int ipv6;
70 15902770 2021-01-15 op };
71 15902770 2021-01-15 op
72 15902770 2021-01-15 op extern struct conf conf;
73 15902770 2021-01-15 op
74 488f059a 2020-12-24 op enum {
75 9862b637 2021-01-13 op S_HANDSHAKE,
76 488f059a 2020-12-24 op S_OPEN,
77 488f059a 2020-12-24 op S_INITIALIZING,
78 488f059a 2020-12-24 op S_SENDING,
79 488f059a 2020-12-24 op S_CLOSING,
80 488f059a 2020-12-24 op };
81 488f059a 2020-12-24 op
82 488f059a 2020-12-24 op struct client {
83 488f059a 2020-12-24 op struct tls *ctx;
84 488f059a 2020-12-24 op int state;
85 488f059a 2020-12-24 op int code;
86 488f059a 2020-12-24 op const char *meta;
87 488f059a 2020-12-24 op int fd, waiting_on_child;
88 488f059a 2020-12-24 op pid_t child;
89 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
90 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
91 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
92 488f059a 2020-12-24 op int af;
93 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
94 15902770 2021-01-15 op struct vhost *host; /* host he's talking to */
95 488f059a 2020-12-24 op };
96 488f059a 2020-12-24 op
97 3c1cf9d0 2021-01-11 op struct iri {
98 33d32d1f 2020-12-25 op char *schema;
99 33d32d1f 2020-12-25 op char *host;
100 33d32d1f 2020-12-25 op char *port;
101 33d32d1f 2020-12-25 op uint16_t port_no;
102 33d32d1f 2020-12-25 op char *path;
103 33d32d1f 2020-12-25 op char *query;
104 33d32d1f 2020-12-25 op char *fragment;
105 33d32d1f 2020-12-25 op };
106 33d32d1f 2020-12-25 op
107 ef04b551 2021-01-09 op struct parser {
108 3c1cf9d0 2021-01-11 op char *iri;
109 3c1cf9d0 2021-01-11 op struct iri *parsed;
110 ef04b551 2021-01-09 op const char *err;
111 ef04b551 2021-01-09 op };
112 ef04b551 2021-01-09 op
113 488f059a 2020-12-24 op enum {
114 488f059a 2020-12-24 op FILE_EXISTS,
115 488f059a 2020-12-24 op FILE_EXECUTABLE,
116 488f059a 2020-12-24 op FILE_DIRECTORY,
117 488f059a 2020-12-24 op FILE_MISSING,
118 488f059a 2020-12-24 op };
119 488f059a 2020-12-24 op
120 33d32d1f 2020-12-25 op /* gmid.c */
121 4a28dd01 2020-12-28 op void sig_handler(int);
122 488f059a 2020-12-24 op int starts_with(const char*, const char*);
123 488f059a 2020-12-24 op
124 488f059a 2020-12-24 op int start_reply(struct pollfd*, struct client*, int, const char*);
125 3d9a1c73 2020-12-28 op ssize_t filesize(int);
126 488f059a 2020-12-24 op const char *path_ext(const char*);
127 488f059a 2020-12-24 op const char *mime(const char*);
128 488f059a 2020-12-24 op int check_path(struct client*, const char*, int*);
129 488f059a 2020-12-24 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
130 488f059a 2020-12-24 op int open_file(char*, char*, struct pollfd*, struct client*);
131 488f059a 2020-12-24 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
132 6c6c7a0e 2020-12-28 op void cgi_poll_on_child(struct pollfd*, struct client*);
133 6c6c7a0e 2020-12-28 op void cgi_poll_on_client(struct pollfd*, struct client*);
134 488f059a 2020-12-24 op void handle_cgi(struct pollfd*, struct client*);
135 488f059a 2020-12-24 op void send_file(char*, char*, struct pollfd*, struct client*);
136 488f059a 2020-12-24 op void send_dir(char*, struct pollfd*, struct client*);
137 9862b637 2021-01-13 op void handle_handshake(struct pollfd*, struct client*);
138 9862b637 2021-01-13 op void handle_open_conn(struct pollfd*, struct client*);
139 488f059a 2020-12-24 op void handle(struct pollfd*, struct client*);
140 488f059a 2020-12-24 op
141 488f059a 2020-12-24 op void mark_nonblock(int);
142 488f059a 2020-12-24 op int make_soket(int);
143 488f059a 2020-12-24 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
144 488f059a 2020-12-24 op void goodbye(struct pollfd*, struct client*);
145 33756bd2 2021-01-10 op void loop(struct tls*, int, int);
146 488f059a 2020-12-24 op
147 15902770 2021-01-15 op char *absolutify_path(const char*);
148 15902770 2021-01-15 op void yyerror(const char*);
149 15902770 2021-01-15 op int parse_portno(const char*);
150 15902770 2021-01-15 op void parse_conf(const char*);
151 15902770 2021-01-15 op void load_vhosts(struct tls_config*);
152 15902770 2021-01-15 op void sandbox();
153 15902770 2021-01-15 op
154 488f059a 2020-12-24 op void usage(const char*);
155 488f059a 2020-12-24 op
156 15902770 2021-01-15 op extern FILE *yyin;
157 15902770 2021-01-15 op extern int yylineno;
158 15902770 2021-01-15 op extern int yyparse(void);
159 15902770 2021-01-15 op extern int yylex(void);
160 15902770 2021-01-15 op
161 ef04b551 2021-01-09 op /* utf8.c */
162 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
163 ef04b551 2021-01-09 op
164 3c1cf9d0 2021-01-11 op /* iri.c */
165 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
166 3c1cf9d0 2021-01-11 op int trim_req_iri(char*);
167 33d32d1f 2020-12-25 op
168 488f059a 2020-12-24 op #endif