2020-12-24 16:49:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Omar Polo <op@omarpolo.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 16:48:39 +01:00
|
|
|
#ifndef GMID_H
|
|
|
|
#define GMID_H
|
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2020-12-24 16:48:39 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
#include <dirent.h>
|
2021-02-04 14:23:15 +01:00
|
|
|
#include <signal.h>
|
2020-12-24 16:48:39 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-02-07 22:47:01 +01:00
|
|
|
#include <time.h>
|
2020-12-24 16:48:39 +01:00
|
|
|
#include <tls.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-01-21 12:57:46 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2020-12-24 16:48:39 +01:00
|
|
|
#define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
|
|
|
|
|
|
|
|
#define SUCCESS 20
|
2021-01-20 19:54:44 +01:00
|
|
|
#define TEMP_REDIRECT 30
|
2020-12-24 16:48:39 +01:00
|
|
|
#define TEMP_FAILURE 40
|
2021-02-01 23:04:51 +01:00
|
|
|
#define CGI_ERROR 42
|
2020-12-24 16:48:39 +01:00
|
|
|
#define NOT_FOUND 51
|
2021-01-11 15:45:57 +01:00
|
|
|
#define PROXY_REFUSED 53
|
2020-12-24 16:48:39 +01:00
|
|
|
#define BAD_REQUEST 59
|
|
|
|
|
|
|
|
#define MAX_USERS 64
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
#define HOSTSLEN 64
|
2021-01-24 15:11:40 +01:00
|
|
|
#define LOCLEN 32
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-01-28 17:25:59 +01:00
|
|
|
/* maximum hostname and label length, +1 for the NUL-terminator */
|
|
|
|
#define DOMAIN_NAME_LEN (253+1)
|
|
|
|
#define LABEL_LEN (63+1)
|
2021-01-27 11:47:49 +01:00
|
|
|
|
2021-01-24 15:11:40 +01:00
|
|
|
struct location {
|
2021-01-27 16:53:30 +01:00
|
|
|
const char *match;
|
|
|
|
const char *lang;
|
|
|
|
const char *default_mime;
|
|
|
|
const char *index;
|
2021-01-24 19:53:26 +01:00
|
|
|
int auto_index; /* 0 auto, -1 off, 1 on */
|
2021-02-06 18:22:37 +01:00
|
|
|
int block_code;
|
|
|
|
const char *block_fmt;
|
|
|
|
int strip;
|
2021-01-24 15:11:40 +01:00
|
|
|
};
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
struct vhost {
|
|
|
|
const char *domain;
|
|
|
|
const char *cert;
|
|
|
|
const char *key;
|
|
|
|
const char *dir;
|
|
|
|
const char *cgi;
|
2021-02-06 19:28:43 +01:00
|
|
|
const char *entrypoint;
|
2021-01-15 10:17:43 +01:00
|
|
|
int dirfd;
|
2021-01-30 13:04:20 +01:00
|
|
|
|
|
|
|
/* the first location rule is always '*' and holds the default
|
|
|
|
* settings for the vhost, from locations[1] onwards there are
|
|
|
|
* the "real" location rules specified in the configuration. */
|
2021-01-24 15:11:40 +01:00
|
|
|
struct location locations[LOCLEN];
|
2021-01-15 10:17:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extern struct vhost hosts[HOSTSLEN];
|
|
|
|
|
2021-01-18 19:48:37 +01:00
|
|
|
struct etm { /* extension to mime */
|
|
|
|
const char *mime;
|
|
|
|
const char *ext;
|
|
|
|
};
|
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
struct mime {
|
2021-01-18 19:48:37 +01:00
|
|
|
struct etm *t;
|
|
|
|
size_t len;
|
|
|
|
size_t cap;
|
|
|
|
};
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
struct conf {
|
2021-02-07 16:30:28 +01:00
|
|
|
/* from command line */
|
|
|
|
int foreground;
|
|
|
|
int verbose;
|
|
|
|
|
|
|
|
/* in the config */
|
2021-01-25 11:30:07 +01:00
|
|
|
int port;
|
|
|
|
int ipv6;
|
|
|
|
uint32_t protos;
|
|
|
|
struct mime mime;
|
|
|
|
char *chroot;
|
|
|
|
char *user;
|
2021-02-07 13:05:32 +01:00
|
|
|
int prefork;
|
2021-01-15 10:17:43 +01:00
|
|
|
};
|
|
|
|
|
2021-02-08 13:44:34 +01:00
|
|
|
extern const char *config_path;
|
2021-01-15 10:17:43 +01:00
|
|
|
extern struct conf conf;
|
2021-01-16 20:41:34 +01:00
|
|
|
extern int exfd;
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
extern volatile sig_atomic_t hupped;
|
|
|
|
|
2021-01-20 19:54:44 +01:00
|
|
|
struct iri {
|
|
|
|
char *schema;
|
|
|
|
char *host;
|
|
|
|
char *port;
|
|
|
|
uint16_t port_no;
|
|
|
|
char *path;
|
|
|
|
char *query;
|
|
|
|
char *fragment;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct parser {
|
|
|
|
char *iri;
|
|
|
|
struct iri *parsed;
|
|
|
|
const char *err;
|
|
|
|
};
|
|
|
|
|
2021-02-01 21:00:33 +01:00
|
|
|
struct client;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
typedef void (*statefn)(int, short, void*);
|
2020-12-24 16:48:39 +01:00
|
|
|
|
2021-02-01 21:24:14 +01:00
|
|
|
/*
|
|
|
|
* DFA: handle_handshake is the initial state, close_conn the final.
|
2021-02-02 10:48:32 +01:00
|
|
|
* Sometimes we have an enter_* function to handle the state switch.
|
2021-02-01 21:24:14 +01:00
|
|
|
*
|
|
|
|
* handle_handshake -> handle_open_conn
|
|
|
|
* handle_handshake -> close_conn // on err
|
|
|
|
*
|
2021-02-01 23:04:51 +01:00
|
|
|
* handle_open_conn -> handle_cgi_reply // via open_file/dir/...
|
2021-02-02 10:48:32 +01:00
|
|
|
* handle_open_conn -> handle_dirlist // ...same
|
2021-02-01 21:24:14 +01:00
|
|
|
* handle_open_conn -> send_file // ...same
|
2021-02-01 23:04:51 +01:00
|
|
|
* handle_open_conn -> start_reply // on error
|
|
|
|
*
|
|
|
|
* handle_cgi_reply -> handle_cgi // after logging the CGI reply
|
|
|
|
* handle_cgi_reply -> start_reply // on error
|
2021-02-01 21:24:14 +01:00
|
|
|
*
|
|
|
|
* handle_cgi -> close_conn
|
|
|
|
*
|
2021-02-02 10:48:32 +01:00
|
|
|
* handle_dirlist -> send_directory_listing
|
|
|
|
* handle_dirlist -> close_conn // on error
|
|
|
|
*
|
2021-02-01 21:24:14 +01:00
|
|
|
* send_directory_listing -> close_conn
|
|
|
|
*
|
|
|
|
* send_file -> close_conn
|
|
|
|
*/
|
2020-12-24 16:48:39 +01:00
|
|
|
struct client {
|
|
|
|
struct tls *ctx;
|
2021-01-20 19:54:44 +01:00
|
|
|
char req[GEMINI_URL_LEN];
|
|
|
|
struct iri iri;
|
2021-01-27 11:47:49 +01:00
|
|
|
char domain[DOMAIN_NAME_LEN];
|
2021-02-08 11:01:45 +01:00
|
|
|
statefn next;
|
2020-12-24 16:48:39 +01:00
|
|
|
int code;
|
|
|
|
const char *meta;
|
2021-02-08 11:01:45 +01:00
|
|
|
int fd, pfd;
|
|
|
|
DIR *dir;
|
2020-12-24 16:48:39 +01:00
|
|
|
char sbuf[1024]; /* static buffer */
|
|
|
|
void *buf, *i; /* mmap buffer */
|
|
|
|
ssize_t len, off; /* mmap/static buffer */
|
2021-01-10 23:29:22 +01:00
|
|
|
struct sockaddr_storage addr;
|
2021-01-15 16:21:51 +01:00
|
|
|
struct vhost *host; /* host she's talking to */
|
2020-12-24 16:48:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FILE_EXISTS,
|
|
|
|
FILE_EXECUTABLE,
|
|
|
|
FILE_DIRECTORY,
|
|
|
|
FILE_MISSING,
|
|
|
|
};
|
|
|
|
|
2020-12-25 13:13:12 +01:00
|
|
|
/* gmid.c */
|
2020-12-28 09:55:56 +01:00
|
|
|
void sig_handler(int);
|
2021-01-25 15:08:31 +01:00
|
|
|
void mkdirs(const char*);
|
|
|
|
char *data_dir(void);
|
|
|
|
void load_local_cert(const char*, const char*);
|
2021-01-25 11:30:07 +01:00
|
|
|
void load_vhosts(void);
|
2021-01-21 15:49:11 +01:00
|
|
|
int make_socket(int, int);
|
2021-01-25 11:30:07 +01:00
|
|
|
void setup_tls(void);
|
2021-01-24 15:11:40 +01:00
|
|
|
void init_config(void);
|
2021-02-04 14:23:15 +01:00
|
|
|
void free_config(void);
|
2021-01-25 11:30:07 +01:00
|
|
|
void drop_priv(void);
|
2020-12-24 16:48:39 +01:00
|
|
|
|
2021-01-15 16:21:51 +01:00
|
|
|
/* provided by lex/yacc */
|
2021-01-15 10:17:43 +01:00
|
|
|
extern FILE *yyin;
|
|
|
|
extern int yylineno;
|
|
|
|
extern int yyparse(void);
|
|
|
|
extern int yylex(void);
|
|
|
|
|
2021-02-06 18:22:37 +01:00
|
|
|
void yyerror(const char*, ...);
|
2021-01-27 17:45:45 +01:00
|
|
|
int parse_portno(const char*);
|
|
|
|
void parse_conf(const char*);
|
|
|
|
|
2021-02-07 16:30:28 +01:00
|
|
|
/* log.c */
|
|
|
|
void fatal(const char*, ...)
|
|
|
|
__attribute__((format (printf, 1, 2)))
|
|
|
|
__attribute__((__noreturn__));
|
|
|
|
|
|
|
|
#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3)))
|
|
|
|
void log_err(struct client*, const char*, ...) LOG_ATTR_FMT;
|
|
|
|
void log_warn(struct client*, const char*, ...) LOG_ATTR_FMT;
|
|
|
|
void log_notice(struct client*, const char*, ...) LOG_ATTR_FMT;
|
|
|
|
void log_info(struct client*, const char*, ...) LOG_ATTR_FMT;
|
|
|
|
void log_debug(struct client*, const char*, ...) LOG_ATTR_FMT;
|
|
|
|
void log_request(struct client*, char*, size_t);
|
|
|
|
|
2021-01-18 19:18:57 +01:00
|
|
|
/* mime.c */
|
2021-01-21 16:01:37 +01:00
|
|
|
void init_mime(struct mime*);
|
|
|
|
void add_mime(struct mime*, const char*, const char*);
|
|
|
|
void load_default_mime(struct mime*);
|
2021-01-19 12:28:41 +01:00
|
|
|
const char *mime(struct vhost*, const char*);
|
2021-01-18 19:18:57 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
/* server.c */
|
2021-01-24 15:11:40 +01:00
|
|
|
const char *vhost_lang(struct vhost*, const char*);
|
|
|
|
const char *vhost_default_mime(struct vhost*, const char*);
|
|
|
|
const char *vhost_index(struct vhost*, const char*);
|
2021-01-24 19:53:26 +01:00
|
|
|
int vhost_auto_index(struct vhost*, const char*);
|
2021-02-06 18:22:37 +01:00
|
|
|
int vhost_block_return(struct vhost*, const char*, int*, const char**);
|
|
|
|
int vhost_strip(struct vhost*, const char*);
|
2021-02-03 15:16:39 +01:00
|
|
|
void mark_nonblock(int);
|
2021-01-18 00:23:58 +01:00
|
|
|
void loop(struct tls*, int, int);
|
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
/* ex.c */
|
|
|
|
int send_string(int, const char*);
|
|
|
|
int recv_string(int, char**);
|
2021-02-01 12:11:43 +01:00
|
|
|
int send_iri(int, struct iri*);
|
|
|
|
int recv_iri(int, struct iri*);
|
|
|
|
void free_recvd_iri(struct iri*);
|
2021-01-16 20:41:34 +01:00
|
|
|
int send_vhost(int, struct vhost*);
|
|
|
|
int recv_vhost(int, struct vhost**);
|
2021-02-07 22:47:01 +01:00
|
|
|
int send_time(int, time_t);
|
|
|
|
int recv_time(int, time_t*);
|
2021-01-16 20:41:34 +01:00
|
|
|
int send_fd(int, int);
|
|
|
|
int recv_fd(int);
|
2021-02-03 17:37:53 +01:00
|
|
|
int executor_main(void);
|
2021-01-16 20:41:34 +01:00
|
|
|
|
2021-01-15 15:03:45 +01:00
|
|
|
/* sandbox.c */
|
2021-01-21 15:49:11 +01:00
|
|
|
void sandbox(void);
|
2021-01-15 15:03:45 +01:00
|
|
|
|
2021-01-09 21:32:23 +01:00
|
|
|
/* utf8.c */
|
|
|
|
int valid_multibyte_utf8(struct parser*);
|
2021-01-27 11:47:49 +01:00
|
|
|
char *utf8_nth(char*, size_t);
|
2021-01-09 21:32:23 +01:00
|
|
|
|
2021-01-11 14:08:00 +01:00
|
|
|
/* iri.c */
|
|
|
|
int parse_iri(char*, struct iri*, const char**);
|
2021-01-27 16:05:16 +01:00
|
|
|
int trim_req_iri(char*, const char **);
|
2021-02-01 12:11:43 +01:00
|
|
|
int serialize_iri(struct iri*, char*, size_t);
|
2021-02-07 19:55:04 +01:00
|
|
|
char *pct_decode_str(char *);
|
2020-12-25 13:13:12 +01:00
|
|
|
|
2021-01-27 11:47:49 +01:00
|
|
|
/* puny.c */
|
2021-01-29 18:11:03 +01:00
|
|
|
int puny_decode(const char*, char*, size_t, const char**);
|
2021-01-27 11:47:49 +01:00
|
|
|
|
2021-01-27 16:35:09 +01:00
|
|
|
/* utils.c */
|
2021-02-04 14:23:15 +01:00
|
|
|
void block_signals(void);
|
|
|
|
void unblock_signals(void);
|
2021-01-27 16:35:09 +01:00
|
|
|
int starts_with(const char*, const char*);
|
|
|
|
int ends_with(const char*, const char*);
|
|
|
|
ssize_t filesize(int);
|
2021-02-01 12:11:43 +01:00
|
|
|
char *absolutify_path(const char*);
|
2021-02-04 14:23:15 +01:00
|
|
|
char *xstrdup(const char*);
|
2021-02-07 16:30:28 +01:00
|
|
|
void gen_certificate(const char*, const char*, const char*);
|
2021-01-27 16:35:09 +01:00
|
|
|
|
2020-12-24 16:48:39 +01:00
|
|
|
#endif
|