Rearranging code to prepare for multiple REPORT types
[citadel.git] / webcit-ng / server / webcit.h
1 // webcit.h - "header of headers"
2 //
3 // Copyright (c) 1996-2023 by the citadel.org team
4 //
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.
7
8 #define SHOW_ME_VAPPEND_PRINTF
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 <time.h>
26 #include <sys/time.h>
27 #include <string.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <pthread.h>
32 #include <signal.h>
33 #include <syslog.h>
34 #include <stdarg.h>
35 #include <limits.h>
36 #include <iconv.h>
37 #include <libical/ical.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 keyval {                                 // key/value pair (for array)
58         char *key;
59         char *val;
60 };
61
62 struct http_transaction {                       // The lifetime of an HTTP request goes through this data structure.
63         char *method;                           // The top half is built up by the web server and sent up to the
64         char *url;                              // application stack.  The second half is built up by the application
65         char *http_version;                     // stack and sent back down to the web server, which transmits it to
66         char *site_prefix;                      // the client.
67         Array *request_headers;
68         char *request_body_with_synth_headers;  // This is the request body with some synthetic headers prepended into it.
69         char *request_body;                     // this is just going to be a pointer into request_body_with_synth_headers
70         long request_body_length;
71         Array *request_parms;                   // anything after the "?" in the URL
72         int response_code;
73         char *response_string;
74         Array *response_headers;
75         char *response_body;
76         long response_body_length;
77 };
78
79 #define AUTH_MAX 256                            // Maximum length of an HTTP AUTH header or equivalent cookie data
80 struct ctdlsession {
81         struct ctdlsession *next;
82         int is_bound;                           // Nonzero if this record is currently bound to a running thread
83         int sock;                               // Socket connection to Citadel server
84         char auth[AUTH_MAX];                    // Auth string (empty if not logged in)
85         char whoami[64];                        // Display name of currently logged in user (empty if not logged in)
86         char room[128];                         // What room we are currently in
87         int room_current_view;
88         int room_default_view;
89         int is_trash_folder;                    // nonzero if this room is the user's Trash folder
90         int is_room_aide;                       // nonzero if the user has aide rights to THIS room
91         int can_delete_messages;                // nonzero if the user is permitted to delete messages in THIS room
92         long last_seen;
93         int new_messages;
94         int total_messages;
95         time_t last_access;                     // Timestamp of last request that used this session
96         time_t num_requests_handled;
97         time_t room_mtime;                      // Timestampt of the most recent write activity in this room
98 };
99
100 struct uploaded_file {                          // things that have been uploaded to the server (such as email attachments)
101         char id[10];
102         char filename[256];
103         char content_type[256];
104         long length;
105         FILE *fp;
106 };
107
108 extern char *ssl_cipher_list;
109 extern int is_https;                            // nonzero if we are an HTTPS server today
110 extern char *ctdl_dir;                          // directory where Citadel Server is running
111 void init_ssl(void);
112 void starttls(struct client_handle *);
113 void endtls(struct client_handle *);
114 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes);
115 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes);
116
117 enum {
118         WEBSERVER_HTTP,
119         WEBSERVER_HTTPS,
120         WEBSERVER_UDS
121 };
122
123 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
124 #define SLEEPING                180             // TCP connection timeout
125 #define MAX_WORKER_THREADS      32              // Maximum number of worker threads permitted to exist
126 #define DEFAULT_SSL_CIPHER_LIST "DEFAULT"       // See http://openssl.org/docs/apps/ciphers.html
127 #define WEBSERVER_PORT          80
128 #define WEBSERVER_INTERFACE     "*"
129 #define CTDL_DIR                "/usr/local/citadel"
130 #define DEVELOPER_ID            0
131 #define CLIENT_ID               4
132 #define TARGET                  "webcit02"      // Window target for inline URL's
133 #define ROOMNAMELEN             128             // The size of a roomname string
134 #define DAV_MOVE                0               // MOVE=0 COPY=1 don't change these!
135 #define DAV_COPY                1               // they are the values used in the Citadel Server MOVE command
136
137
138 // Everything below here is generated with this command:
139 // cproto -f2 *.c 2>/dev/null |sed 's/^\/\*/\n\/\//g' | sed 's/\ \*\/$//g'
140
141 // admin_functions.c
142 void try_login(struct http_transaction *, struct ctdlsession *);
143 void logout(struct http_transaction *, struct ctdlsession *);
144 void whoami(struct http_transaction *, struct ctdlsession *);
145 void biff(struct http_transaction *, struct ctdlsession *);
146 void ctdl_a(struct http_transaction *, struct ctdlsession *);
147
148 // caldav_reports.c
149 void caldav_xml_start(void *, const char *, const char **);
150 void caldav_xml_end(void *, const char *);
151 void caldav_xml_chardata(void *, const XML_Char *, int);
152 StrBuf *fetch_ical(struct ctdlsession *, long);
153 void caldav_report_one_item(struct http_transaction *, struct ctdlsession *, StrBuf *, StrBuf *);
154 void caldav_report(struct http_transaction *, struct ctdlsession *);
155
156 // ctdlclient.c
157 int ctdl_read_binary(struct ctdlsession *, char *, int);
158 int ctdl_readline(struct ctdlsession *, char *, int);
159 StrBuf *ctdl_readtextmsg(struct ctdlsession *);
160 ssize_t ctdl_write(struct ctdlsession *, const void *, size_t);
161 void ctdl_printf(struct ctdlsession *, const char *, ...);
162 int uds_connectsock(char *);
163 void extract_auth(struct http_transaction *, char *, int);
164 int login_to_citadel(struct ctdlsession *, char *, char *);
165 struct ctdlsession *connect_to_citadel(struct http_transaction *);
166 void disconnect_from_citadel(struct ctdlsession *);
167
168 // ctdl_commands.c
169 void serv_info(struct http_transaction *, struct ctdlsession *);
170 void ctdl_c(struct http_transaction *, struct ctdlsession *);
171
172 // ctdlfunctions.c
173 void ctdl_delete_msgs(struct ctdlsession *, long *, int);
174
175 // floor_functions.c
176 void floor_list(struct http_transaction *, struct ctdlsession *);
177 void ctdl_f(struct http_transaction *, struct ctdlsession *);
178
179 // forum_view.c
180 void setup_for_forum_view(struct ctdlsession *);
181 JsonValue *json_tokenize_recipients(const char *, long, char *);
182 void json_render_one_message(struct http_transaction *, struct ctdlsession *, long);
183
184 // html2html.c
185 void stripquotes(char *);
186 void extract_charset_from_meta(char *, char *, char *);
187 StrBuf *html2html(const char *, int, char *, long, StrBuf *);
188 void UrlizeText(StrBuf *, StrBuf *, StrBuf *);
189 void url(char *, size_t);
190
191 // http.c
192 int client_write(struct client_handle *, char *, int);
193 int client_read(struct client_handle *, char *, int);
194 int client_readline(struct client_handle *, char *, int);
195 void client_printf(struct client_handle *, const char *, ...);
196 void add_response_header(struct http_transaction *, char *, char *);
197 void perform_one_http_transaction(struct client_handle *);
198 char *header_val(struct http_transaction *, char *);
199 char *get_url_param(struct http_transaction *, char *);
200
201 // main.c
202 int main(int, char **);
203
204 // messages.c
205 long locate_message_by_uid(struct ctdlsession *, char *);
206 void dav_delete_message(struct http_transaction *, struct ctdlsession *, long);
207 void dav_move_or_copy_message(struct http_transaction *, struct ctdlsession *, long, int);
208 void dav_get_message(struct http_transaction *, struct ctdlsession *, long);
209 void dav_put_message(struct http_transaction *, struct ctdlsession *, char *, long);
210 void download_mime_component(struct http_transaction *, struct ctdlsession *, long, char *);
211
212 // request.c
213 void do_404(struct http_transaction *);
214 void do_405(struct http_transaction *);
215 void do_412(struct http_transaction *);
216 void do_204(struct http_transaction *);
217 void do_502(struct http_transaction *);
218 void request_http_authenticate(struct http_transaction *);
219 void http_redirect(struct http_transaction *, char *);
220 void perform_request(struct http_transaction *);
221
222 // room_functions.c
223 Array *get_msglist(struct ctdlsession *, char *);
224 int match_etags(char *, long);
225 void json_stat(struct http_transaction *, struct ctdlsession *);
226 void json_mailbox(struct http_transaction *, struct ctdlsession *);
227 void json_msglist(struct http_transaction *, struct ctdlsession *, char *);
228 void read_room_info_banner(struct http_transaction *, struct ctdlsession *);
229 void set_last_read_pointer(struct http_transaction *, struct ctdlsession *);
230 void object_in_room(struct http_transaction *, struct ctdlsession *);
231 void report_the_room_itself(struct http_transaction *, struct ctdlsession *);
232 void options_the_room_itself(struct http_transaction *, struct ctdlsession *);
233 void propfind_the_room_itself(struct http_transaction *, struct ctdlsession *);
234 void get_the_room_itself(struct http_transaction *, struct ctdlsession *);
235 void the_room_itself(struct http_transaction *, struct ctdlsession *);
236 void room_list(struct http_transaction *, struct ctdlsession *);
237 void ctdl_r(struct http_transaction *, struct ctdlsession *);
238
239 // static.c
240 void output_static(struct http_transaction *);
241
242 // tcp_sockets.c
243 int lingering_close(int);
244 int webcit_tcp_server(const char *, int, int);
245 int webcit_uds_server(char *, int);
246
247 // text2html.c
248 StrBuf *text2html(const char *, int, char *, long, StrBuf *);
249 StrBuf *variformat2html(StrBuf *);
250
251 // tls.c
252 void bind_to_key_and_certificate(void);
253 void init_ssl(void);
254 void update_key_and_cert_if_needed(void);
255 void starttls(struct client_handle *);
256 void endtls(struct client_handle *);
257 int client_write_ssl(struct client_handle *, char *, int);
258 int client_read_ssl(struct client_handle *, char *, int);
259
260 // upload.c
261 void upload_handler(char *, char *, char *, char *, void *, char *, char *, size_t, char *, char *, void *);
262 void upload_files(struct http_transaction *, struct ctdlsession *);
263 void ctdl_p_base(struct http_transaction *, struct ctdlsession *);
264 void delete_upload(struct uploaded_file);
265 void dav_delete_upload(struct http_transaction *, struct ctdlsession *, struct uploaded_file);
266 struct uploaded_file pop_upload(char *);
267 void attachment_filter(char *, char *, char *, char *, void *, char *, char *, size_t, char *, char *, void *);
268 void load_attachments_from_message(struct http_transaction *, struct ctdlsession *, char *);
269 void specific_upload(struct http_transaction *, struct ctdlsession *, char *);
270 void ctdl_p(struct http_transaction *, struct ctdlsession *);
271
272 // user_functions.c
273 void fetch_user_photo(struct http_transaction *, struct ctdlsession *, char *);
274 void fetch_user_bio(struct http_transaction *, struct ctdlsession *, char *);
275 void object_in_user(struct http_transaction *, struct ctdlsession *, char *);
276 void the_user_itself(struct http_transaction *, struct ctdlsession *, char *);
277 void user_list(struct http_transaction *, struct ctdlsession *);
278 void ctdl_u(struct http_transaction *, struct ctdlsession *);
279
280 // util.c
281 int unescape_input(char *);
282 char *http_datestring(time_t);
283
284 // webserver.c
285 void spawn_another_worker_thread(int *);
286 void worker_entry(int *);
287 int webserver(char *, int, int);