1 // webcit.h - "header of headers"
3 // Copyright (c) 1996-2023 by the citadel.org team
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.
8 #define SHOW_ME_VAPPEND_PRINTF
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
37 #include <libcitadel.h>
38 #define OPENSSL_NO_KRB5 // Work around RedHat's b0rken OpenSSL includes
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/rand.h>
43 #define _(x) x // temporary hack until we add i18n back in
44 //#define DEBUG_HTTP // uncomment to debug HTTP headers
46 // XML_StopParser is present in expat 2.x
47 #if XML_MAJOR_VERSION > 1
48 #define HAVE_XML_STOPPARSER
51 struct client_handle { // this gets passed up the stack from the webserver to the application code
56 struct keyval { // key/value pair (for array)
61 struct http_transaction { // The lifetime of an HTTP request goes through this data structure.
62 char *method; // The top half is built up by the web server and sent up to the
63 char *url; // application stack. The second half is built up by the application
64 char *http_version; // stack and sent back down to the web server, which transmits it to
65 char *site_prefix; // the client.
66 Array *request_headers;
68 long request_body_length;
69 Array *request_parms; // anything after the "?" in the URL
71 char *response_string;
72 Array *response_headers;
74 long response_body_length;
77 #define AUTH_MAX 256 // Maximum length of an HTTP AUTH header or equivalent cookie data
79 struct ctdlsession *next;
80 int is_bound; // Nonzero if this record is currently bound to a running thread
81 int sock; // Socket connection to Citadel server
82 char auth[AUTH_MAX]; // Auth string (empty if not logged in)
83 char whoami[64]; // Display name of currently logged in user (empty if not logged in)
84 char room[128]; // What room we are currently in
85 int room_current_view;
86 int room_default_view;
87 int is_trash_folder; // nonzero if this room is the user's Trash folder
88 int is_room_aide; // nonzero if the user has aide rights to THIS room
89 int can_delete_messages; // nonzero if the user is permitted to delete messages in THIS room
93 time_t last_access; // Timestamp of last request that used this session
94 time_t num_requests_handled;
95 time_t room_mtime; // Timestampt of the most recent write activity in this room
98 extern char *ssl_cipher_list;
99 extern int is_https; // nonzero if we are an HTTPS server today
100 extern char *ctdl_dir; // directory where Citadel Server is running
102 void starttls(struct client_handle *);
103 void endtls(struct client_handle *);
104 int client_write_ssl(struct client_handle *ch, char *buf, int nbytes);
105 int client_read_ssl(struct client_handle *ch, char *buf, int nbytes);
113 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
114 #define SLEEPING 180 // TCP connection timeout
115 #define MAX_WORKER_THREADS 32 // Maximum number of worker threads permitted to exist
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 CTDL_DIR "/usr/local/citadel"
120 #define DEVELOPER_ID 0
122 #define TARGET "webcit02" /* Window target for inline URL's */
123 #define ROOMNAMELEN 128 // The size of a roomname string
124 #define DAV_MOVE 0 // MOVE=0 COPY=1 don't change these!
125 #define DAV_COPY 1 // they are the values used in the Citadel Server MOVE command
127 void worker_entry(int *);
128 void perform_one_http_transaction(struct client_handle *ch);
129 void add_response_header(struct http_transaction *h, char *key, char *val);
130 void perform_request(struct http_transaction *);
131 void do_204(struct http_transaction *);
132 void do_404(struct http_transaction *);
133 void do_405(struct http_transaction *);
134 void do_412(struct http_transaction *);
135 void do_502(struct http_transaction *);
136 void output_static(struct http_transaction *);
137 int uds_connectsock(char *);
138 void ctdl_a(struct http_transaction *, struct ctdlsession *);
139 void ctdl_f(struct http_transaction *, struct ctdlsession *);
140 void ctdl_r(struct http_transaction *, struct ctdlsession *);
141 void ctdl_u(struct http_transaction *, struct ctdlsession *);
142 void ctdl_p(struct http_transaction *, struct ctdlsession *);
143 struct ctdlsession *connect_to_citadel(struct http_transaction *);
144 void disconnect_from_citadel(struct ctdlsession *);
145 char *header_val(struct http_transaction *h, char *requested_header);
146 char *get_url_param(struct http_transaction *h, char *requested_param);
147 int unescape_input(char *);
148 void http_redirect(struct http_transaction *h, char *to_where);
149 char *http_datestring(time_t xtime);
150 long *get_msglist(struct ctdlsession *c, char *which_msgs);
151 void caldav_report(struct http_transaction *h, struct ctdlsession *c);
152 long locate_message_by_uid(struct ctdlsession *c, char *uid);
153 void ctdl_delete_msgs(struct ctdlsession *c, long *msgnums, int num_msgs);
154 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
155 void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
156 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum);
157 void dav_move_or_copy_message(struct http_transaction *h, struct ctdlsession *c, long msgnum, int move_or_copy);
158 ssize_t ctdl_write(struct ctdlsession *ctdl, const void *buf, size_t count);
159 int login_to_citadel(struct ctdlsession *c, char *auth, char *resultbuf);
160 StrBuf *ctdl_readtextmsg(struct ctdlsession *ctdl);
161 StrBuf *html2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
162 void download_mime_component(struct http_transaction *h, struct ctdlsession *c, long msgnum, char *partnum);
163 StrBuf *text2html(const char *supplied_charset, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source);
164 StrBuf *variformat2html(StrBuf *Source);
165 int ctdl_readline(struct ctdlsession *ctdl, char *buf, int maxbytes);
166 int ctdl_read_binary(struct ctdlsession *ctdl, char *buf, int bytes_requested);
167 void ctdl_c(struct http_transaction *h, struct ctdlsession *c);
168 int webserver(char *webserver_interface, int webserver_port, int webserver_protocol);
169 void ctdl_printf(struct ctdlsession *ctdl, const char *format,...);
170 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len);
171 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
172 void json_render_one_message(struct http_transaction *h, struct ctdlsession *c, long msgnum);
173 void upload_files(struct http_transaction *h, struct ctdlsession *c);