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 INFTIM
32 488f059a 2020-12-24 op # define INFTIM -1
33 488f059a 2020-12-24 op #endif
34 488f059a 2020-12-24 op
35 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
36 488f059a 2020-12-24 op
37 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
38 488f059a 2020-12-24 op #define PATHBUF 2048
39 488f059a 2020-12-24 op
40 488f059a 2020-12-24 op #define SUCCESS 20
41 488f059a 2020-12-24 op #define TEMP_FAILURE 40
42 488f059a 2020-12-24 op #define NOT_FOUND 51
43 7b1d9790 2021-01-11 op #define PROXY_REFUSED 53
44 488f059a 2020-12-24 op #define BAD_REQUEST 59
45 488f059a 2020-12-24 op
46 488f059a 2020-12-24 op #define MAX_USERS 64
47 488f059a 2020-12-24 op
48 15902770 2021-01-15 op #define HOSTSLEN 64
49 15902770 2021-01-15 op
50 dafb57b8 2021-01-15 op #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
51 dafb57b8 2021-01-15 op #define LOGW(c, fmt, ...) logs(LOG_WARNING, c, fmt, __VA_ARGS__)
52 dafb57b8 2021-01-15 op #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
53 dafb57b8 2021-01-15 op #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
54 dafb57b8 2021-01-15 op #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
55 dafb57b8 2021-01-15 op
56 15902770 2021-01-15 op struct vhost {
57 15902770 2021-01-15 op const char *domain;
58 15902770 2021-01-15 op const char *cert;
59 15902770 2021-01-15 op const char *key;
60 15902770 2021-01-15 op const char *dir;
61 15902770 2021-01-15 op const char *cgi;
62 15902770 2021-01-15 op int dirfd;
63 15902770 2021-01-15 op };
64 15902770 2021-01-15 op
65 15902770 2021-01-15 op extern struct vhost hosts[HOSTSLEN];
66 15902770 2021-01-15 op
67 15902770 2021-01-15 op struct conf {
68 15902770 2021-01-15 op int foreground;
69 15902770 2021-01-15 op int port;
70 15902770 2021-01-15 op int ipv6;
71 5bc3c98e 2021-01-15 op uint32_t protos;
72 15902770 2021-01-15 op };
73 15902770 2021-01-15 op
74 15902770 2021-01-15 op extern struct conf conf;
75 881a9dd9 2021-01-16 op extern int exfd;
76 15902770 2021-01-15 op
77 488f059a 2020-12-24 op enum {
78 9862b637 2021-01-13 op S_HANDSHAKE,
79 488f059a 2020-12-24 op S_OPEN,
80 488f059a 2020-12-24 op S_INITIALIZING,
81 488f059a 2020-12-24 op S_SENDING,
82 488f059a 2020-12-24 op S_CLOSING,
83 488f059a 2020-12-24 op };
84 488f059a 2020-12-24 op
85 488f059a 2020-12-24 op struct client {
86 488f059a 2020-12-24 op struct tls *ctx;
87 488f059a 2020-12-24 op int state;
88 488f059a 2020-12-24 op int code;
89 488f059a 2020-12-24 op const char *meta;
90 488f059a 2020-12-24 op int fd, waiting_on_child;
91 881a9dd9 2021-01-16 op int child;
92 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
93 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
94 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
95 488f059a 2020-12-24 op int af;
96 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
97 f7b816dc 2021-01-15 op struct vhost *host; /* host she's talking to */
98 488f059a 2020-12-24 op };
99 488f059a 2020-12-24 op
100 3c1cf9d0 2021-01-11 op struct iri {
101 33d32d1f 2020-12-25 op char *schema;
102 33d32d1f 2020-12-25 op char *host;
103 33d32d1f 2020-12-25 op char *port;
104 33d32d1f 2020-12-25 op uint16_t port_no;
105 33d32d1f 2020-12-25 op char *path;
106 33d32d1f 2020-12-25 op char *query;
107 33d32d1f 2020-12-25 op char *fragment;
108 33d32d1f 2020-12-25 op };
109 33d32d1f 2020-12-25 op
110 ef04b551 2021-01-09 op struct parser {
111 3c1cf9d0 2021-01-11 op char *iri;
112 3c1cf9d0 2021-01-11 op struct iri *parsed;
113 ef04b551 2021-01-09 op const char *err;
114 ef04b551 2021-01-09 op };
115 ef04b551 2021-01-09 op
116 488f059a 2020-12-24 op enum {
117 488f059a 2020-12-24 op FILE_EXISTS,
118 488f059a 2020-12-24 op FILE_EXECUTABLE,
119 488f059a 2020-12-24 op FILE_DIRECTORY,
120 488f059a 2020-12-24 op FILE_MISSING,
121 488f059a 2020-12-24 op };
122 488f059a 2020-12-24 op
123 33d32d1f 2020-12-25 op /* gmid.c */
124 dafb57b8 2021-01-15 op
125 d3a08f4d 2021-01-17 op __attribute__((format (printf, 1, 2)))
126 d3a08f4d 2021-01-17 op __attribute__((__noreturn__))
127 d3a08f4d 2021-01-17 op void fatal(const char*, ...);
128 d3a08f4d 2021-01-17 op
129 d3a08f4d 2021-01-17 op __attribute__((format (printf, 3, 4)))
130 d3a08f4d 2021-01-17 op void logs(int, struct client*, const char*, ...);
131 d3a08f4d 2021-01-17 op
132 4a28dd01 2020-12-28 op void sig_handler(int);
133 488f059a 2020-12-24 op int starts_with(const char*, const char*);
134 3d9a1c73 2020-12-28 op ssize_t filesize(int);
135 15902770 2021-01-15 op char *absolutify_path(const char*);
136 15902770 2021-01-15 op void yyerror(const char*);
137 15902770 2021-01-15 op int parse_portno(const char*);
138 15902770 2021-01-15 op void parse_conf(const char*);
139 15902770 2021-01-15 op void load_vhosts(struct tls_config*);
140 d3a08f4d 2021-01-17 op int make_soket(int);
141 881a9dd9 2021-01-16 op int listener_main();
142 488f059a 2020-12-24 op void usage(const char*);
143 488f059a 2020-12-24 op
144 f7b816dc 2021-01-15 op /* provided by lex/yacc */
145 15902770 2021-01-15 op extern FILE *yyin;
146 15902770 2021-01-15 op extern int yylineno;
147 15902770 2021-01-15 op extern int yyparse(void);
148 15902770 2021-01-15 op extern int yylex(void);
149 15902770 2021-01-15 op
150 0fbe79b3 2021-01-18 op /* mime.c */
151 0fbe79b3 2021-01-18 op void init_mime(void);
152 0fbe79b3 2021-01-18 op void add_mime(const char*, const char*);
153 0fbe79b3 2021-01-18 op void load_default_mime(void);
154 0fbe79b3 2021-01-18 op const char *mime(const char*);
155 0fbe79b3 2021-01-18 op
156 d3a08f4d 2021-01-17 op /* server.c */
157 d3a08f4d 2021-01-17 op int check_path(struct client*, const char*, int*);
158 d3a08f4d 2021-01-17 op int open_file(char*, char*, struct pollfd*, struct client*);
159 d3a08f4d 2021-01-17 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
160 d3a08f4d 2021-01-17 op void mark_nonblock(int);
161 d3a08f4d 2021-01-17 op void handle_handshake(struct pollfd*, struct client*);
162 d3a08f4d 2021-01-17 op void handle_open_conn(struct pollfd*, struct client*);
163 d3a08f4d 2021-01-17 op int start_reply(struct pollfd*, struct client*, int, const char*);
164 d3a08f4d 2021-01-17 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
165 d3a08f4d 2021-01-17 op void send_file(char*, char*, struct pollfd*, struct client*);
166 d3a08f4d 2021-01-17 op void send_dir(char*, struct pollfd*, struct client*);
167 d3a08f4d 2021-01-17 op void cgi_poll_on_child(struct pollfd*, struct client*);
168 d3a08f4d 2021-01-17 op void cgi_poll_on_client(struct pollfd*, struct client*);
169 d3a08f4d 2021-01-17 op void handle_cgi(struct pollfd*, struct client*);
170 d3a08f4d 2021-01-17 op void goodbye(struct pollfd*, struct client*);
171 d3a08f4d 2021-01-17 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
172 d3a08f4d 2021-01-17 op void handle(struct pollfd*, struct client*);
173 d3a08f4d 2021-01-17 op void loop(struct tls*, int, int);
174 d3a08f4d 2021-01-17 op
175 881a9dd9 2021-01-16 op /* ex.c */
176 881a9dd9 2021-01-16 op int send_string(int, const char*);
177 881a9dd9 2021-01-16 op int recv_string(int, char**);
178 881a9dd9 2021-01-16 op int send_vhost(int, struct vhost*);
179 881a9dd9 2021-01-16 op int recv_vhost(int, struct vhost**);
180 881a9dd9 2021-01-16 op int send_fd(int, int);
181 881a9dd9 2021-01-16 op int recv_fd(int);
182 881a9dd9 2021-01-16 op int executor_main(int);
183 881a9dd9 2021-01-16 op
184 dafb57b8 2021-01-15 op /* sandbox.c */
185 dafb57b8 2021-01-15 op void sandbox();
186 dafb57b8 2021-01-15 op
187 ef04b551 2021-01-09 op /* utf8.c */
188 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
189 ef04b551 2021-01-09 op
190 3c1cf9d0 2021-01-11 op /* iri.c */
191 3c1cf9d0 2021-01-11 op int parse_iri(char*, struct iri*, const char**);
192 3c1cf9d0 2021-01-11 op int trim_req_iri(char*);
193 33d32d1f 2020-12-25 op
194 488f059a 2020-12-24 op #endif