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