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 488f059a 2020-12-24 op
23 488f059a 2020-12-24 op #include <poll.h>
24 488f059a 2020-12-24 op #include <stdio.h>
25 488f059a 2020-12-24 op #include <stdlib.h>
26 488f059a 2020-12-24 op #include <syslog.h>
27 488f059a 2020-12-24 op #include <tls.h>
28 488f059a 2020-12-24 op #include <unistd.h>
29 488f059a 2020-12-24 op
30 488f059a 2020-12-24 op #ifndef __OpenBSD__
31 488f059a 2020-12-24 op # define pledge(a, b) 0
32 488f059a 2020-12-24 op # define unveil(a, b) 0
33 488f059a 2020-12-24 op #endif
34 488f059a 2020-12-24 op
35 488f059a 2020-12-24 op #ifndef INFTIM
36 488f059a 2020-12-24 op # define INFTIM -1
37 488f059a 2020-12-24 op #endif
38 488f059a 2020-12-24 op
39 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
40 488f059a 2020-12-24 op
41 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
42 488f059a 2020-12-24 op #define PATHBUF 2048
43 488f059a 2020-12-24 op
44 488f059a 2020-12-24 op #define SUCCESS 20
45 488f059a 2020-12-24 op #define TEMP_FAILURE 40
46 488f059a 2020-12-24 op #define NOT_FOUND 51
47 488f059a 2020-12-24 op #define BAD_REQUEST 59
48 488f059a 2020-12-24 op
49 488f059a 2020-12-24 op #ifndef MAX_USERS
50 488f059a 2020-12-24 op #define MAX_USERS 64
51 488f059a 2020-12-24 op #endif
52 488f059a 2020-12-24 op
53 488f059a 2020-12-24 op #define SAFE_SETENV(var, val) do { \
54 488f059a 2020-12-24 op const char *_tmp = (val); \
55 488f059a 2020-12-24 op if (_tmp == NULL) \
56 488f059a 2020-12-24 op _tmp = ""; \
57 488f059a 2020-12-24 op setenv((var), _tmp, 1); \
58 488f059a 2020-12-24 op } while(0)
59 488f059a 2020-12-24 op
60 488f059a 2020-12-24 op #define LOG(priority, c, fmt, ...) \
61 488f059a 2020-12-24 op do { \
62 488f059a 2020-12-24 op char buf[INET_ADDRSTRLEN]; \
63 488f059a 2020-12-24 op if (inet_ntop((c)->af, &(c)->addr, \
64 488f059a 2020-12-24 op buf, sizeof(buf)) == NULL) \
65 488f059a 2020-12-24 op FATAL("inet_ntop: %s", strerror(errno)); \
66 488f059a 2020-12-24 op if (foreground) \
67 488f059a 2020-12-24 op fprintf(stderr, \
68 488f059a 2020-12-24 op "%s " fmt "\n", buf, __VA_ARGS__); \
69 488f059a 2020-12-24 op else \
70 488f059a 2020-12-24 op syslog((priority) | LOG_DAEMON, \
71 488f059a 2020-12-24 op "%s " fmt, buf, __VA_ARGS__); \
72 488f059a 2020-12-24 op } while (0)
73 488f059a 2020-12-24 op
74 488f059a 2020-12-24 op #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
75 488f059a 2020-12-24 op #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
76 488f059a 2020-12-24 op #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
77 488f059a 2020-12-24 op #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
78 488f059a 2020-12-24 op
79 488f059a 2020-12-24 op #define FATAL(fmt, ...) \
80 488f059a 2020-12-24 op do { \
81 488f059a 2020-12-24 op if (foreground) \
82 488f059a 2020-12-24 op fprintf(stderr, fmt "\n", __VA_ARGS__); \
83 488f059a 2020-12-24 op else \
84 488f059a 2020-12-24 op syslog(LOG_DAEMON | LOG_CRIT, \
85 488f059a 2020-12-24 op fmt, __VA_ARGS__); \
86 488f059a 2020-12-24 op exit(1); \
87 488f059a 2020-12-24 op } while (0)
88 488f059a 2020-12-24 op
89 488f059a 2020-12-24 op enum {
90 488f059a 2020-12-24 op S_OPEN,
91 488f059a 2020-12-24 op S_INITIALIZING,
92 488f059a 2020-12-24 op S_SENDING,
93 488f059a 2020-12-24 op S_CLOSING,
94 488f059a 2020-12-24 op };
95 488f059a 2020-12-24 op
96 488f059a 2020-12-24 op struct client {
97 488f059a 2020-12-24 op struct tls *ctx;
98 488f059a 2020-12-24 op int state;
99 488f059a 2020-12-24 op int code;
100 488f059a 2020-12-24 op const char *meta;
101 488f059a 2020-12-24 op int fd, waiting_on_child;
102 488f059a 2020-12-24 op pid_t child;
103 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
104 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
105 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
106 488f059a 2020-12-24 op int af;
107 488f059a 2020-12-24 op struct in_addr addr;
108 488f059a 2020-12-24 op };
109 488f059a 2020-12-24 op
110 33d32d1f 2020-12-25 op struct uri {
111 33d32d1f 2020-12-25 op char *schema;
112 33d32d1f 2020-12-25 op char *host;
113 33d32d1f 2020-12-25 op char *port;
114 33d32d1f 2020-12-25 op uint16_t port_no;
115 33d32d1f 2020-12-25 op char *path;
116 33d32d1f 2020-12-25 op char *query;
117 33d32d1f 2020-12-25 op char *fragment;
118 33d32d1f 2020-12-25 op };
119 33d32d1f 2020-12-25 op
120 ef04b551 2021-01-09 op struct parser {
121 ef04b551 2021-01-09 op char *uri;
122 ef04b551 2021-01-09 op struct uri *parsed;
123 ef04b551 2021-01-09 op const char *err;
124 ef04b551 2021-01-09 op };
125 ef04b551 2021-01-09 op
126 488f059a 2020-12-24 op enum {
127 488f059a 2020-12-24 op FILE_EXISTS,
128 488f059a 2020-12-24 op FILE_EXECUTABLE,
129 488f059a 2020-12-24 op FILE_DIRECTORY,
130 488f059a 2020-12-24 op FILE_MISSING,
131 488f059a 2020-12-24 op };
132 488f059a 2020-12-24 op
133 33d32d1f 2020-12-25 op /* gmid.c */
134 4a28dd01 2020-12-28 op void sig_handler(int);
135 488f059a 2020-12-24 op int starts_with(const char*, const char*);
136 488f059a 2020-12-24 op
137 488f059a 2020-12-24 op int start_reply(struct pollfd*, struct client*, int, const char*);
138 3d9a1c73 2020-12-28 op ssize_t filesize(int);
139 488f059a 2020-12-24 op const char *path_ext(const char*);
140 488f059a 2020-12-24 op const char *mime(const char*);
141 488f059a 2020-12-24 op int check_path(struct client*, const char*, int*);
142 488f059a 2020-12-24 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
143 488f059a 2020-12-24 op int open_file(char*, char*, struct pollfd*, struct client*);
144 488f059a 2020-12-24 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
145 6c6c7a0e 2020-12-28 op void cgi_poll_on_child(struct pollfd*, struct client*);
146 6c6c7a0e 2020-12-28 op void cgi_poll_on_client(struct pollfd*, struct client*);
147 488f059a 2020-12-24 op void handle_cgi(struct pollfd*, struct client*);
148 488f059a 2020-12-24 op void send_file(char*, char*, struct pollfd*, struct client*);
149 488f059a 2020-12-24 op void send_dir(char*, struct pollfd*, struct client*);
150 488f059a 2020-12-24 op void handle(struct pollfd*, struct client*);
151 488f059a 2020-12-24 op
152 488f059a 2020-12-24 op void mark_nonblock(int);
153 488f059a 2020-12-24 op int make_soket(int);
154 488f059a 2020-12-24 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
155 488f059a 2020-12-24 op void goodbye(struct pollfd*, struct client*);
156 488f059a 2020-12-24 op void loop(struct tls*, int);
157 488f059a 2020-12-24 op
158 488f059a 2020-12-24 op void usage(const char*);
159 488f059a 2020-12-24 op
160 ef04b551 2021-01-09 op /* utf8.c */
161 ef04b551 2021-01-09 op int valid_multibyte_utf8(struct parser*);
162 ef04b551 2021-01-09 op
163 33d32d1f 2020-12-25 op /* uri.c */
164 33d32d1f 2020-12-25 op int parse_uri(char*, struct uri*, const char**);
165 33d32d1f 2020-12-25 op int trim_req_uri(char*);
166 33d32d1f 2020-12-25 op
167 488f059a 2020-12-24 op #endif