Blame


1 488f059a 2020-12-24 op #ifndef GMID_H
2 488f059a 2020-12-24 op #define GMID_H
3 488f059a 2020-12-24 op
4 488f059a 2020-12-24 op #include <arpa/inet.h>
5 488f059a 2020-12-24 op #include <netinet/in.h>
6 488f059a 2020-12-24 op
7 488f059a 2020-12-24 op #include <poll.h>
8 488f059a 2020-12-24 op #include <stdio.h>
9 488f059a 2020-12-24 op #include <stdlib.h>
10 488f059a 2020-12-24 op #include <syslog.h>
11 488f059a 2020-12-24 op #include <tls.h>
12 488f059a 2020-12-24 op #include <unistd.h>
13 488f059a 2020-12-24 op
14 488f059a 2020-12-24 op #ifndef __OpenBSD__
15 488f059a 2020-12-24 op # define pledge(a, b) 0
16 488f059a 2020-12-24 op # define unveil(a, b) 0
17 488f059a 2020-12-24 op #endif
18 488f059a 2020-12-24 op
19 488f059a 2020-12-24 op #ifndef INFTIM
20 488f059a 2020-12-24 op # define INFTIM -1
21 488f059a 2020-12-24 op #endif
22 488f059a 2020-12-24 op
23 488f059a 2020-12-24 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
24 488f059a 2020-12-24 op
25 488f059a 2020-12-24 op /* large enough to hold a copy of a gemini URL and still have extra room */
26 488f059a 2020-12-24 op #define PATHBUF 2048
27 488f059a 2020-12-24 op
28 488f059a 2020-12-24 op #define SUCCESS 20
29 488f059a 2020-12-24 op #define TEMP_FAILURE 40
30 488f059a 2020-12-24 op #define NOT_FOUND 51
31 488f059a 2020-12-24 op #define BAD_REQUEST 59
32 488f059a 2020-12-24 op
33 488f059a 2020-12-24 op #ifndef MAX_USERS
34 488f059a 2020-12-24 op #define MAX_USERS 64
35 488f059a 2020-12-24 op #endif
36 488f059a 2020-12-24 op
37 488f059a 2020-12-24 op #define SAFE_SETENV(var, val) do { \
38 488f059a 2020-12-24 op const char *_tmp = (val); \
39 488f059a 2020-12-24 op if (_tmp == NULL) \
40 488f059a 2020-12-24 op _tmp = ""; \
41 488f059a 2020-12-24 op setenv((var), _tmp, 1); \
42 488f059a 2020-12-24 op } while(0)
43 488f059a 2020-12-24 op
44 488f059a 2020-12-24 op #define LOG(priority, c, fmt, ...) \
45 488f059a 2020-12-24 op do { \
46 488f059a 2020-12-24 op char buf[INET_ADDRSTRLEN]; \
47 488f059a 2020-12-24 op if (inet_ntop((c)->af, &(c)->addr, \
48 488f059a 2020-12-24 op buf, sizeof(buf)) == NULL) \
49 488f059a 2020-12-24 op FATAL("inet_ntop: %s", strerror(errno)); \
50 488f059a 2020-12-24 op if (foreground) \
51 488f059a 2020-12-24 op fprintf(stderr, \
52 488f059a 2020-12-24 op "%s " fmt "\n", buf, __VA_ARGS__); \
53 488f059a 2020-12-24 op else \
54 488f059a 2020-12-24 op syslog((priority) | LOG_DAEMON, \
55 488f059a 2020-12-24 op "%s " fmt, buf, __VA_ARGS__); \
56 488f059a 2020-12-24 op } while (0)
57 488f059a 2020-12-24 op
58 488f059a 2020-12-24 op #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
59 488f059a 2020-12-24 op #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
60 488f059a 2020-12-24 op #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
61 488f059a 2020-12-24 op #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
62 488f059a 2020-12-24 op
63 488f059a 2020-12-24 op #define FATAL(fmt, ...) \
64 488f059a 2020-12-24 op do { \
65 488f059a 2020-12-24 op if (foreground) \
66 488f059a 2020-12-24 op fprintf(stderr, fmt "\n", __VA_ARGS__); \
67 488f059a 2020-12-24 op else \
68 488f059a 2020-12-24 op syslog(LOG_DAEMON | LOG_CRIT, \
69 488f059a 2020-12-24 op fmt, __VA_ARGS__); \
70 488f059a 2020-12-24 op exit(1); \
71 488f059a 2020-12-24 op } while (0)
72 488f059a 2020-12-24 op
73 488f059a 2020-12-24 op enum {
74 488f059a 2020-12-24 op S_OPEN,
75 488f059a 2020-12-24 op S_INITIALIZING,
76 488f059a 2020-12-24 op S_SENDING,
77 488f059a 2020-12-24 op S_CLOSING,
78 488f059a 2020-12-24 op };
79 488f059a 2020-12-24 op
80 488f059a 2020-12-24 op struct client {
81 488f059a 2020-12-24 op struct tls *ctx;
82 488f059a 2020-12-24 op int state;
83 488f059a 2020-12-24 op int code;
84 488f059a 2020-12-24 op const char *meta;
85 488f059a 2020-12-24 op int fd, waiting_on_child;
86 488f059a 2020-12-24 op pid_t child;
87 488f059a 2020-12-24 op char sbuf[1024]; /* static buffer */
88 488f059a 2020-12-24 op void *buf, *i; /* mmap buffer */
89 488f059a 2020-12-24 op ssize_t len, off; /* mmap/static buffer */
90 488f059a 2020-12-24 op int af;
91 488f059a 2020-12-24 op struct in_addr addr;
92 488f059a 2020-12-24 op };
93 488f059a 2020-12-24 op
94 488f059a 2020-12-24 op enum {
95 488f059a 2020-12-24 op FILE_EXISTS,
96 488f059a 2020-12-24 op FILE_EXECUTABLE,
97 488f059a 2020-12-24 op FILE_DIRECTORY,
98 488f059a 2020-12-24 op FILE_MISSING,
99 488f059a 2020-12-24 op };
100 488f059a 2020-12-24 op
101 488f059a 2020-12-24 op struct etm { /* file extension to mime */
102 488f059a 2020-12-24 op const char *mime;
103 488f059a 2020-12-24 op const char *ext;
104 488f059a 2020-12-24 op } filetypes[] = {
105 488f059a 2020-12-24 op {"application/pdf", "pdf"},
106 488f059a 2020-12-24 op
107 488f059a 2020-12-24 op {"image/gif", "gif"},
108 488f059a 2020-12-24 op {"image/jpeg", "jpg"},
109 488f059a 2020-12-24 op {"image/jpeg", "jpeg"},
110 488f059a 2020-12-24 op {"image/png", "png"},
111 488f059a 2020-12-24 op {"image/svg+xml", "svg"},
112 488f059a 2020-12-24 op
113 488f059a 2020-12-24 op {"text/gemini", "gemini"},
114 488f059a 2020-12-24 op {"text/gemini", "gmi"},
115 488f059a 2020-12-24 op {"text/markdown", "markdown"},
116 488f059a 2020-12-24 op {"text/markdown", "md"},
117 488f059a 2020-12-24 op {"text/plain", "txt"},
118 488f059a 2020-12-24 op {"text/xml", "xml"},
119 488f059a 2020-12-24 op
120 488f059a 2020-12-24 op {NULL, NULL}
121 488f059a 2020-12-24 op };
122 488f059a 2020-12-24 op
123 488f059a 2020-12-24 op void siginfo_handler(int);
124 488f059a 2020-12-24 op int starts_with(const char*, const char*);
125 488f059a 2020-12-24 op
126 488f059a 2020-12-24 op char *url_after_proto(char*);
127 488f059a 2020-12-24 op char *url_start_of_request(char*);
128 488f059a 2020-12-24 op int url_trim(struct client*, char*);
129 488f059a 2020-12-24 op char *adjust_path(char*);
130 488f059a 2020-12-24 op ssize_t filesize(int);
131 488f059a 2020-12-24 op
132 488f059a 2020-12-24 op int start_reply(struct pollfd*, struct client*, int, const char*);
133 488f059a 2020-12-24 op const char *path_ext(const char*);
134 488f059a 2020-12-24 op const char *mime(const char*);
135 488f059a 2020-12-24 op int check_path(struct client*, const char*, int*);
136 488f059a 2020-12-24 op int check_for_cgi(char *, char*, struct pollfd*, struct client*);
137 488f059a 2020-12-24 op int open_file(char*, char*, struct pollfd*, struct client*);
138 488f059a 2020-12-24 op int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
139 488f059a 2020-12-24 op void cgi_setpoll_on_child(struct pollfd*, struct client*);
140 488f059a 2020-12-24 op void cgi_setpoll_on_client(struct pollfd*, struct client*);
141 488f059a 2020-12-24 op void handle_cgi(struct pollfd*, struct client*);
142 488f059a 2020-12-24 op void send_file(char*, char*, struct pollfd*, struct client*);
143 488f059a 2020-12-24 op void send_dir(char*, struct pollfd*, struct client*);
144 488f059a 2020-12-24 op void handle(struct pollfd*, struct client*);
145 488f059a 2020-12-24 op
146 488f059a 2020-12-24 op void mark_nonblock(int);
147 488f059a 2020-12-24 op int make_soket(int);
148 488f059a 2020-12-24 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
149 488f059a 2020-12-24 op void goodbye(struct pollfd*, struct client*);
150 488f059a 2020-12-24 op void loop(struct tls*, int);
151 488f059a 2020-12-24 op
152 488f059a 2020-12-24 op void usage(const char*);
153 488f059a 2020-12-24 op
154 488f059a 2020-12-24 op #endif