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