/* mohawk - small HTTP server
**
** Copyright © 2010 by Baptiste Daroussin <bapt@FreeBSD.org>,
** Copyright © 2010 by Freddy Dissaux <freddy.dsx@free.fr>,
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/

#ifndef _MOHAWK_H
#define _MOHAWK_H

#ifndef MOHAWK_VERSION
#error "MOHAWK_VERSION not defined"
#endif

#include <sys/queue.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <stdbool.h>

#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#endif
#if defined(__NetBSD__)
#include <util.h>
#endif

#include <event2/util.h> /* for evutil_socket_t */

#if defined(__OpenBSD__)
#include "compat.h"
#endif

#ifndef DEFAULT_AUTH_FILE
#define DEFAULT_AUTH_FILE ".htpasswd"
#endif /* DEFAULT_AUTH_FILE */

#ifndef DEFAULT_HTTP_PORT
#define DEFAULT_HTTP_PORT "80"
#endif /* DEFAULT_HTTP_PORT */

#ifndef DEFAULT_LISTEN
#define DEFAULT_LISTEN "localhost"
#endif /* DEFAULT_LISTEN */

#ifndef DEFAULT_PIDFILE
#if defined(__OpenBSD__)
#define DEFAULT_PIDFILE "mohawk"
#else
#define DEFAULT_PIDFILE "/var/run/mohawk.pid"
#endif
#endif /* DEFAULT_PIDFILE */

#ifndef DEFAULT_ROOTDIR
#define DEFAULT_ROOTDIR "/tmp"
#endif /* DEFAULT_ROOTDIR */

#ifndef DEFAULT_SYSLOG_FACILITY
#define DEFAULT_SYSLOG_FACILITY "daemon"
#endif /* DEFAULT_SYSLOG_FACILITY */

#ifndef DEFAULT_USER
#if defined(__OpenBSD__)
#define DEFAULT_USER "_mohawk"
#else
#define DEFAULT_USER "nobody"
#endif
#endif /* DEFAULT_USER */

#ifndef IPPORT_MAX
#define IPPORT_MAX 65535
#endif

struct mohawk_list {
	char *item;
	SLIST_ENTRY(mohawk_list) entry;
};

struct mohawk_hash {
	char *key;
	char *value;
	SLIST_ENTRY(mohawk_hash) entry;
};

struct mohawk_address_info {
	struct addrinfo *ai;
	SLIST_ENTRY(mohawk_address_info) entry;
};

struct mohawk_network_inet {
    uint32_t prefix;
    uint32_t mask;
    int not;
};

struct mohawk_network_inet6 {
    uint8_t prefix[16];
    uint8_t mask[16];
    int not;
};

struct mohawk_network {
    const char *item;
    int family;
    void *network;
    SLIST_ENTRY(mohawk_network) entry;
};

struct vhost {
	SLIST_HEAD(, mohawk_list) auth_patterns;
	SLIST_HEAD(, mohawk_list) blacklist_patterns;
	SLIST_HEAD(, mohawk_list) cgi_patterns;
	SLIST_HEAD(, mohawk_list) index_names;
	SLIST_HEAD(, mohawk_list) no_auth_patterns;
	SLIST_HEAD(, mohawk_list) no_cgi_maps;
	SLIST_HEAD(, mohawk_list) no_log_patterns;
	SLIST_HEAD(, mohawk_hash) cgi_env;
	SLIST_HEAD(, mohawk_hash) cgi_map;
	SLIST_HEAD(, mohawk_address_info) address_infos;
	SLIST_HEAD(, mohawk_network) grant_access;
	SLIST_HEAD(, vhost) locations;
	const char *email_admin;
	const char *auth_path;
	const char *charset;
	const char *dirlist_css_url;
	const char *host;
	const char *location;
	const char *mohawk_name;
	const char *rootdir;
	const char *status_url;
	unsigned int cgi_env_count;
	unsigned int maxage;
	bool auth_blacklistd;
	bool authentication;
	bool can_quick;
	bool cgi_expose_mohawk_version;
	bool dirlist;
	bool external_symlinks;
	bool hostname_in_rootdir;
	bool x_forwarded_for;
	SLIST_ENTRY(vhost) entry;
};

struct callback_data {
    struct vhost *vh;
    struct evhttp *httpd;
    char *host;
    char *service;
    unsigned int port;
    SLIST_ENTRY(callback_data) entry;
};

struct mohawk_conf {
	SLIST_HEAD(, mohawk_hash) mime_type; 
	SLIST_HEAD(, vhost) vhosts;
	SLIST_HEAD(, callback_data) cb_datas;
	struct vhost *default_vhost;
	struct pidfh *pfh;
	char *chroot;
	const char *pidfile;
	const char *user;
	const char *syslog_facility;
	bool debug;
	bool blacklistd;
};

extern struct mohawk_conf conf;

void parse_command_line(const char *, const char *, const char *);
void parse_config(const char *);
int host(const char *host, const char *service);
unsigned int parse_number(const char *, unsigned int, unsigned int);
const char *facility_lookup(const char *, int *);

#endif
/* vim: set sw=4 sts=4 ts=4 : */
