Obsolete sys/time.h and HAVE_TIME_WITH_SYS_TIME
[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 <string.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <syslog.h>
33 #include <stdarg.h>
34 #include <limits.h>
35 #include <iconv.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>
41 #include <expat.h>
42 #define _(x)    x                               // temporary hack until we add i18n back in
43 //#define DEBUG_HTTP                            // uncomment to debug HTTP headers
44
45 // XML_StopParser is present in expat 2.x
46 #if XML_MAJOR_VERSION > 1
47 #define HAVE_XML_STOPPARSER
48 #endif
49
50 struct client_handle {                          // this gets passed up the stack from the webserver to the application code
51         int sock;
52         SSL *ssl_handle;
53 };
54
55 struct keyval {                                 // key/value pair (for array)
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 *url;                              // 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         Array *request_headers;
66         char *request_body;
67         long request_body_length;
68         Array *request_parms;                   // anything after the "?" in the URL
69         int response_code;
70         char *response_string;
71         Array *response_headers;
72         char *response_body;
73         long response_body_length;
74 };
75
76 #define AUTH_MAX 256                            // Maximum length of an HTTP AUTH header or equivalent cookie data
77 struct ctdlsession {
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;
86         int is_trash_folder;                    // nonzero if this room is the user's Trash folder
87         int is_room_aide;                       // nonzero if the user has aide rights to THIS room
88         int can_delete_messages;                // nonzero if the user is permitted to delete messages in THIS room
89         long last_seen;
90         int new_messages;
91         int total_messages;
92         time_t last_access;                     // Timestamp of last request that used this session
93         time_t num_requests_handled;
94         time_t room_mtime;                      // Timestampt of the most recent write activity in this room
95 };
96
97 extern char *ssl_cipher_list;
98 extern int is_https;                            // nonzero if we are an HTTPS server today
99 extern char *ctdl_dir;                          // directory where Citadel Server is running
100 void init_ssl(void);
101 void starttls(struct client_handle *);
102 void endtls(struct client_handle *);
103 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes);
104 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes);
105
106 enum {
107         WEBSERVER_HTTP,
108         WEBSERVER_HTTPS,
109         WEBSERVER_UDS
110 };
111
112 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
113 #define SLEEPING                180             // TCP connection timeout
114 #define MAX_WORKER_THREADS      32              // Maximum number of worker threads permitted to exist
115 #define DEFAULT_SSL_CIPHER_LIST "DEFAULT"       // See http://openssl.org/docs/apps/ciphers.html
116 #define WEBSERVER_PORT          80
117 #define WEBSERVER_INTERFACE     "*"
118 #define CTDL_DIR                "/usr/local/citadel"
119 #define DEVELOPER_ID            0
120 #define CLIENT_ID               4
121 #define TARGET                  "webcit02"      /* Window target for inline URL's */
122 #define ROOMNAMELEN             128             // The size of a roomname string
123 #define DAV_MOVE                0               // MOVE=0 COPY=1 don't change these!
124 #define DAV_COPY                1               // they are the values used in the Citadel Server MOVE command
125
126 void worker_entry(int *);
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_204(struct http_transaction *);
131 void do_404(struct http_transaction *);
132 void do_412(struct http_transaction *);
133 void do_502(struct http_transaction *);
134 void output_static(struct http_transaction *);
135 int uds_connectsock(char *);
136 void ctdl_a(struct http_transaction *, struct ctdlsession *);
137 void ctdl_f(struct http_transaction *, struct ctdlsession *);
138 void ctdl_r(struct http_transaction *, struct ctdlsession *);
139 void ctdl_u(struct http_transaction *, struct ctdlsession *);
140 struct ctdlsession *connect_to_citadel(struct http_transaction *);
141 void disconnect_from_citadel(struct ctdlsession *);
142 char *header_val(struct http_transaction *h, char *requested_header);
143 char *get_url_param(struct http_transaction *h, char *requested_param);
144 int unescape_input(char *);
145 void http_redirect(struct http_transaction *h, char *to_where);
146 char *http_datestring(time_t xtime);
147 long *get_msglist(struct ctdlsession *c, char *which_msgs);
148 void caldav_report(struct http_transaction *h, struct ctdlsession *c);
149 long locate_message_by_uid(struct ctdlsession *c, char *uid);
150 void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs);
151 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
152 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
153 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum);
154 void dav_move_or_copy_message(struct http_transaction *h, struct ctdlsession *c, long msgnum, int move_or_copy);
155 ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count);
156 int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf);
157 StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl);
158 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
159 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum);
160 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
161 StrBuf *variformat2html(StrBuf *Source);
162 int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes);
163 int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested);
164 void ctdl_c(struct http_transaction *h, struct ctdlsession *c);
165 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol);
166 void ctdl_printf(struct ctdlsession *ctdl, const char *format,...);
167 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len);
168 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
169 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);