initial work on inline rendering setup
[citadel.git] / webcit-ng / webcit.h
1 /*
2  * webcit.h - "header of headers"
3  *
4  * Copyright (c) 1996-2018 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  */
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <syslog.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <netdb.h>
23 #include <sys/un.h>
24 #include <sys/poll.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <syslog.h>
32 #include <stdarg.h>
33 #include <limits.h>
34 #include <iconv.h>
35 #include <libcitadel.h>
36 #define OPENSSL_NO_KRB5                         // Work around RedHat's b0rken OpenSSL includes
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
39 #include <openssl/rand.h>
40 #include <expat.h>
41 #define _(x)    x                               // temporary hack until we add i18n back in
42 //#define DEBUG_HTTP                            // uncomment to debug HTTP headers
43
44 /* XML_StopParser is present in expat 2.x */
45 #if XML_MAJOR_VERSION > 1
46 #define HAVE_XML_STOPPARSER
47 #endif
48
49 struct client_handle {                          // this gets passed up the stack from the webserver to the application code
50         int sock;
51         SSL *ssl_handle;
52 };
53
54 struct http_header {                            // request and response headers in struct http_transaction use this format
55         struct http_header *next;
56         char *key;
57         char *val;
58 };
59
60 struct http_transaction {                       // The lifetime of an HTTP request goes through this data structure.
61         char *method;                           // The top half is built up by the web server and sent up to the
62         char *uri;                              // application stack.  The second half is built up by the application
63         char *http_version;                     // stack and sent back down to the web server, which transmits it to
64         char *site_prefix;                      // the client.
65         struct http_header *request_headers;
66         char *request_body;
67         long request_body_length;
68         int response_code;
69         char *response_string;
70         struct http_header *response_headers;
71         char *response_body;
72         long response_body_length;
73 };
74
75 #define AUTH_MAX 256                            // Maximum length of an HTTP AUTH header or equivalent cookie data
76 struct ctdlsession {
77         struct ctdlsession *next;
78         int is_bound;                           // Nonzero if this record is currently bound to a running thread
79         int sock;                               // Socket connection to Citadel server
80         char auth[AUTH_MAX];                    // Auth string (empty if not logged in)
81         char whoami[64];                        // Display name of currently logged in user (empty if not logged in)
82         char room[128];                         // What room we are currently in
83         int room_current_view;
84         int room_default_view;
85         long last_seen;
86         int new_messages;
87         int total_messages;
88         time_t last_access;                     // Timestamp of last request that used this session
89         time_t num_requests_handled;
90 };
91
92 extern char *ssl_cipher_list;
93 extern int is_https;                            // nonzero if we are an HTTPS server today
94 extern char *ctdlhost;
95 extern char *ctdlport;
96 void init_ssl(void);
97 void starttls(struct client_handle *);
98 void endtls(struct client_handle *);
99 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes);
100 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes);
101
102 enum {
103         WEBSERVER_HTTP,
104         WEBSERVER_HTTPS,
105         WEBSERVER_UDS
106 };
107
108 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
109 #define SLEEPING                180             // TCP connection timeout
110 #define MAX_WORKER_THREADS      32              // Maximum number of worker threads permitted to exist
111 #define CTDL_CRYPTO_DIR         "keys"
112 #define CTDL_KEY_PATH           CTDL_CRYPTO_DIR "/webcit.key"
113 #define CTDL_CSR_PATH           CTDL_CRYPTO_DIR "/webcit.csr"
114 #define CTDL_CER_PATH           CTDL_CRYPTO_DIR "/webcit.cer"
115 #define SIGN_DAYS               3650            // how long our certificate should live
116 #define DEFAULT_SSL_CIPHER_LIST "DEFAULT"       // See http://openssl.org/docs/apps/ciphers.html
117 #define WEBSERVER_PORT          80
118 #define WEBSERVER_INTERFACE     "*"
119 #define CTDLHOST                "uncensored.citadel.org"
120 #define CTDLPORT                "504"
121 #define DEVELOPER_ID            0
122 #define CLIENT_ID               4
123 #define TARGET                  "webcit01"      /* Window target for inline URL's */
124
125 void worker_entry(int *pointer_to_master_socket);
126 void perform_one_http_transaction(struct client_handle *ch);
127 void add_response_header(struct http_transaction *h, char *key, char *val);
128 void perform_request(struct http_transaction *);
129 void do_404(struct http_transaction *);
130 void output_static(struct http_transaction *);
131 int uds_connectsock(char *sockpath);
132 int tcp_connectsock(char *host, char *service);
133 void ctdl_a(struct http_transaction *, struct ctdlsession *);
134 void ctdl_r(struct http_transaction *, struct ctdlsession *);
135 struct ctdlsession *connect_to_citadel(struct http_transaction *);
136 void disconnect_from_citadel(struct ctdlsession *);
137 char *header_val(struct http_transaction *h, char *requested_header);
138 int unescape_input(char *);
139 void http_redirect(struct http_transaction *h, char *to_where);
140 char *http_datestring(time_t xtime);
141 long *get_msglist(struct ctdlsession *c, char *which_msgs);
142 void caldav_report(struct http_transaction *h, struct ctdlsession *c);
143 long locate_message_by_uid(struct ctdlsession *c, char *uid);
144 void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs);
145 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
146 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
147 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum);
148 ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count);
149 int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf);
150 void threaded_view(struct http_transaction *h, struct ctdlsession *c, char *which);
151 void flat_view(struct http_transaction *h, struct ctdlsession *c, char *which);
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 void ctdl_c(struct http_transaction *h, struct ctdlsession *c);
159 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol);
160 void ctdl_printf(struct ctdlsession *ctdl, const char *format,...);
161 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len);
162 void do_502(struct http_transaction *h);
163 void do_404(struct http_transaction *h);
164 void do_412(struct http_transaction *h);
165 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
166 void html_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);