1 // webcit.h - "header of headers"
3 // Copyright (c) 1996-2021 by the citadel.org team
5 // This program is open source software. You can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
8 #define SHOW_ME_VAPPEND_PRINTF
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
36 #include <libcitadel.h>
37 #define OPENSSL_NO_KRB5 // Work around RedHat's b0rken OpenSSL includes
38 #include <openssl/ssl.h>
39 #include <openssl/err.h>
40 #include <openssl/rand.h>
42 #define _(x) x // temporary hack until we add i18n back in
43 //#define DEBUG_HTTP // uncomment to debug HTTP headers
45 // XML_StopParser is present in expat 2.x
46 #if XML_MAJOR_VERSION > 1
47 #define HAVE_XML_STOPPARSER
50 struct client_handle { // this gets passed up the stack from the webserver to the application code
55 struct http_header { // request and response headers in struct http_transaction use this format
56 struct http_header *next;
61 struct http_transaction { // The lifetime of an HTTP request goes through this data structure.
62 char *method; // The top half is built up by the web server and sent up to the
63 char *uri; // application stack. The second half is built up by the application
64 char *http_version; // stack and sent back down to the web server, which transmits it to
65 char *site_prefix; // the client.
66 struct http_header *request_headers;
68 long request_body_length;
70 char *response_string;
71 struct http_header *response_headers;
73 long response_body_length;
76 #define AUTH_MAX 256 // Maximum length of an HTTP AUTH header or equivalent cookie data
78 struct ctdlsession *next;
79 int is_bound; // Nonzero if this record is currently bound to a running thread
80 int sock; // Socket connection to Citadel server
81 char auth[AUTH_MAX]; // Auth string (empty if not logged in)
82 char whoami[64]; // Display name of currently logged in user (empty if not logged in)
83 char room[128]; // What room we are currently in
84 int room_current_view;
85 int room_default_view;
89 time_t last_access; // Timestamp of last request that used this session
90 time_t num_requests_handled;
93 extern char *ssl_cipher_list;
94 extern int is_https; // nonzero if we are an HTTPS server today
95 extern char *ctdlhost;
96 extern char *ctdlport;
98 void starttls(struct client_handle *);
99 void endtls(struct client_handle *);
100 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes);
101 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes);
109 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
110 #define SLEEPING 180 // TCP connection timeout
111 #define MAX_WORKER_THREADS 32 // Maximum number of worker threads permitted to exist
112 #define CTDL_CRYPTO_DIR "keys"
113 #define CTDL_KEY_PATH CTDL_CRYPTO_DIR "/webcit.key"
114 #define CTDL_CSR_PATH CTDL_CRYPTO_DIR "/webcit.csr"
115 #define CTDL_CER_PATH CTDL_CRYPTO_DIR "/webcit.cer"
116 #define SIGN_DAYS 3650 // how long our certificate should live
117 #define DEFAULT_SSL_CIPHER_LIST "DEFAULT" // See http://openssl.org/docs/apps/ciphers.html
118 #define WEBSERVER_PORT 80
119 #define WEBSERVER_INTERFACE "*"
120 #define CTDLHOST "uncensored.citadel.org"
121 #define CTDLPORT "504"
122 #define DEVELOPER_ID 0
124 #define TARGET "webcit01" /* Window target for inline URL's */
126 void worker_entry(int *pointer_to_master_socket);
127 void perform_one_http_transaction(struct client_handle *ch);
128 void add_response_header(struct http_transaction *h, char *key, char *val);
129 void perform_request(struct http_transaction *);
130 void do_404(struct http_transaction *);
131 void output_static(struct http_transaction *);
132 int uds_connectsock(char *sockpath);
133 int tcp_connectsock(char *host, char *service);
134 void ctdl_a(struct http_transaction *, struct ctdlsession *);
135 void ctdl_r(struct http_transaction *, struct ctdlsession *);
136 void ctdl_u(struct http_transaction *, struct ctdlsession *);
137 struct ctdlsession *connect_to_citadel(struct http_transaction *);
138 void disconnect_from_citadel(struct ctdlsession *);
139 char *header_val(struct http_transaction *h, char *requested_header);
140 int unescape_input(char *);
141 void http_redirect(struct http_transaction *h, char *to_where);
142 char *http_datestring(time_t xtime);
143 long *get_msglist(struct ctdlsession *c, char *which_msgs);
144 void caldav_report(struct http_transaction *h, struct ctdlsession *c);
145 long locate_message_by_uid(struct ctdlsession *c, char *uid);
146 void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs);
147 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
148 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
149 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum);
150 ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count);
151 int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf);
152 StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl);
153 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
154 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum);
155 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
156 StrBuf *variformat2html(StrBuf *Source);
157 int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes);
158 int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested);
159 void ctdl_c(struct http_transaction *h, struct ctdlsession *c);
160 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol);
161 void ctdl_printf(struct ctdlsession *ctdl, const char *format,...);
162 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len);
163 void do_502(struct http_transaction *h);
164 void do_404(struct http_transaction *h);
165 void do_412(struct http_transaction *h);
166 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
167 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);