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