2 * Copyright (c) 1987-2012 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include <sys/select.h>
25 #include <sys/types.h>
27 #include <sys/socket.h>
28 #ifdef HAVE_SYS_TIME_H
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
48 #include <sys/utsname.h>
50 #include <libcitadel.h>
62 #define _(string) gettext(string)
64 #define _(string) (string)
67 #define IsEmptyStr(a) ((a)[0] == '\0')
70 #define DBG_QR(x) if(DO_DBG_QR) _DBG_QR(x)
71 #define DBG_QR2(x) if(DO_DBG_QR) _DBG_QR2(x)
75 #include <libical/ical.h>
81 #undef PACKAGE_TARNAME
82 #undef PACKAGE_VERSION
83 #undef PACKAGE_BUGREPORT
85 typedef struct wcsession wcsession;
91 #include "paramhandling.h"
93 #include "preferences.h"
95 #include "tcp_sockets.h"
98 /* Work around RedHat's b0rken OpenSSL includes */
99 #define OPENSSL_NO_KRB5
100 #include <openssl/ssl.h>
101 #include <openssl/err.h>
102 #include <openssl/rand.h>
103 extern char *ssl_cipher_list;
104 #define DEFAULT_SSL_CIPHER_LIST "DEFAULT" /* See http://openssl.org/docs/apps/ciphers.html */
107 #if SIZEOF_SIZE_T == SIZEOF_INT
108 #define SIZE_T_FMT "%d"
110 #define SIZE_T_FMT "%ld"
113 #if SIZEOF_LONG_UNSIGNED_INT == SIZEOF_INT
114 #define ULONG_FMT "%d"
116 #define ULONG_FMT "%ld"
119 #define CALENDAR_ROOM_NAME "Calendar"
120 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
122 #define SIZ 4096 /* generic buffer size */
124 #define TRACE syslog(LOG_DEBUG, "\033[3%dmCHECKPOINT: %s:%d\033[0m", ((__LINE__%6)+1), __FILE__, __LINE__)
130 #define SLEEPING 180 /* TCP connection timeout */
131 #define WEBCIT_TIMEOUT 900 /* WebCit session timeout */
132 #define PORT_NUM 2000 /* port number to listen on */
133 #define DEVELOPER_ID 0
135 #define CLIENT_VERSION 813 /* This version of WebCit */
136 #define MINIMUM_CIT_VERSION 813 /* Minimum required version of Citadel server */
137 #define LIBCITADEL_MIN 813 /* Minimum required version of libcitadel */
138 #define DEFAULT_HOST "localhost" /* Default Citadel server */
139 #define DEFAULT_PORT "504"
140 #define TARGET "webcit01" /* Window target for inline URL's */
141 #define HOUSEKEEPING 15 /* Housekeeping frequency */
142 #define MAX_WORKER_THREADS 250
143 #define LISTEN_QUEUE_LENGTH 100 /* listen() backlog queue */
145 #define USERCONFIGROOM "My Citadel Config"
146 #define DEFAULT_MAXMSGS 20
149 #ifdef LIBCITADEL_VERSION_NUMBER
150 #if LIBCITADEL_VERSION_NUMBER < LIBCITADEL_MIN
151 #error libcitadel is too old. Please upgrade it before continuing.
158 #define SRV_STATUS_MSG(ServerLineBuf) (ChrPtr(ServerLineBuf) + 4), (StrLength(ServerLineBuf) - 4)
159 #define MAJORCODE(a) (((int)(a / 100) ) * 100)
161 #define LISTING_FOLLOWS 100
163 #define MORE_DATA 300
164 #define SEND_LISTING 400
166 #define BINARY_FOLLOWS 600
167 #define SEND_BINARY 700
168 #define START_CHAT_MODE 800
169 #define ASYNC_MSG 900
171 #define MINORCODE(a) (a % 100)
172 #define ASYNC_GEXP 02
173 #define INTERNAL_ERROR 10
175 #define ILLEGAL_VALUE 12
176 #define NOT_LOGGED_IN 20
177 #define CMD_NOT_SUPPORTED 30
178 #define SERVER_SHUTTING_DOWN 31
179 #define PASSWORD_REQUIRED 40
180 #define ALREADY_LOGGED_IN 41
181 #define USERNAME_REQUIRED 42
182 #define HIGHER_ACCESS_REQUIRED 50
183 #define MAX_SESSIONS_EXCEEDED 51
184 #define RESOURCE_BUSY 52
185 #define RESOURCE_NOT_OPEN 53
187 #define INVALID_FLOOR_OPERATION 61
188 #define NO_SUCH_USER 70
189 #define FILE_NOT_FOUND 71
190 #define ROOM_NOT_FOUND 72
191 #define NO_SUCH_SYSTEM 73
192 #define ALREADY_EXISTS 74
193 #define MESSAGE_NOT_FOUND 75
196 * NLI is the string that shows up in a who's online listing for sessions
197 * that are active but do not (yet) have a user logged in.
199 #define NLI "(not logged in)"
202 * Expiry policy for the autopurger
204 #define EXPIRE_NEXTLEVEL 0 /* Inherit expiration policy */
205 #define EXPIRE_MANUAL 1 /* Don't expire messages at all */
206 #define EXPIRE_NUMMSGS 2 /* Keep only latest n messages */
207 #define EXPIRE_AGE 3 /* Expire messages after n days */
209 typedef struct __ExpirePolicy {
213 void LoadExpirePolicy(GPEXWhichPolicy which);
214 void SaveExpirePolicyFromHTTP(GPEXWhichPolicy which);
217 * Linked list of session variables encoded in an x-www-urlencoded content type
219 typedef struct urlcontent urlcontent;
221 char url_key[32]; /* key */
222 StrBuf *url_data; /* value */
226 * Information about the Citadel server to which we are connected
228 typedef struct _serv_info {
229 int serv_pid; /* Process ID of the Citadel server */
230 StrBuf *serv_nodename; /* Node name of the Citadel server */
231 StrBuf *serv_humannode; /* Juman readable node name of the Citadel server */
232 StrBuf *serv_fqdn; /* Fully qualified Domain Name (such as uncensored.citadel.org) */
233 StrBuf *serv_software; /* Free form text description of the server software in use */
234 int serv_rev_level; /* Server version number (times 100) */
235 StrBuf *serv_bbs_city; /* Geographic location of the Citadel server */
236 StrBuf *serv_sysadm; /* Name of system administrator */
237 int serv_supports_ldap; /* is the server linked against an ldap tree for adresses? */
238 int serv_newuser_disabled; /* Has the server disabled self-service new user creation? */
239 StrBuf *serv_default_cal_zone; /* Default timezone for unspecified calendar items */
240 int serv_supports_sieve; /* Server supports Sieve mail filtering */
241 int serv_fulltext_enabled; /* Full text index is enabled */
242 StrBuf *serv_svn_revision; /* svn or git revision of the server */
243 int serv_supports_openid; /* Server supports authentication via OpenID */
244 int serv_supports_guest; /* Server supports unauthenticated guest logins */
248 typedef struct _disp_cal {
249 icalcomponent *cal; /* cal items for display */
250 long cal_msgnum; /* cal msgids for display */
251 char *from; /* owner of this component */
252 int unread; /* already seen by the user? */
259 icalcomponent *SortBy; /* cal items for display */
260 icalproperty_status Status;
263 typedef struct _IcalEnumMap {
266 icalproperty_kind map;
270 * Address book entry (keep it short and sweet, it's just a quickie lookup
271 * which we can use to get to the real meat and bones later)
273 typedef struct _addrbookent {
274 char ab_name[64]; /* name string */
275 long ab_msgnum; /* message number of address book entry */
280 #define ANONYMOUS (1<<1)
281 #define NEED_URL (1<<2)
282 #define XHTTP_COMMANDS (1<<3)
284 #define URLNAMESPACE (1<<4)
285 #define LOGCHATTY (1<<5)
286 #define COOKIEUNNEEDED (1<<6)
287 #define ISSTATIC (1<<7)
288 #define FORCE_SESSIONCLOSE (1<<8)
289 #define PARSE_REST_URL (1<<9)
290 #define PROHIBIT_STARTPAGE (1<<10)
293 #define DATEFMT_FULL 0
294 #define DATEFMT_BRIEF 1
295 #define DATEFMT_RAWDATE 2
296 #define DATEFMT_LOCALEDATE 3
297 void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format);
300 typedef enum _RESTDispatchID {
306 typedef int (*WebcitRESTDispatchID)(RESTDispatchID WhichAction, int IgnoreFloor);
307 typedef void (*WebcitHandlerFunc)(void);
308 typedef struct _WebcitHandler{
310 WebcitRESTDispatchID RID;
317 void WebcitAddUrlHandler(const char * UrlString, long UrlSLen, const char *DisplayName, long dslen, WebcitHandlerFunc F, long Flags);
319 typedef struct _headereval {
320 ExamineMsgHeaderFunc evaluator;
350 extern const char *ReqStrs[eNONE];
353 #define AUTH_COOKIE 1
358 typedef struct _HdrRefs {
359 long eReqType; /* HTTP method */
366 time_t if_modified_since;
367 int gzip_ok; /* Nonzero if Accept-encoding: gzip */
368 int prohibit_caching;
372 /* these are references into Hdr->HTTPHeaders, so we don't need to free them. */
376 StrBuf *browser_host;
377 StrBuf *browser_language;
382 const WebcitHandler *Handler;
385 typedef struct _ParsedHttpHdrs {
386 int http_sock; /* HTTP server socket */
394 StrBuf *this_page; /* URL of current page */
398 HashList *urlstrings; /* variables passed to webcit in a URL */
399 HashList *HTTPHeaders; /* the headers the client sent us */
400 int nWildfireHeaders; /* how many wildfire headers did we already send? */
407 * One of these is kept for each active Citadel session.
408 * HTTP transactions are bound to one at a time.
411 /* infrastructural members */
412 wcsession *next; /* Linked list */
413 pthread_mutex_t SessionMutex; /* mutex for exclusive access */
414 int wc_session; /* WebCit session ID */
415 int killthis; /* Nonzero == purge this session */
416 int ctdl_pid; /* Session ID on the Citadel server */
417 int nonce; /* session nonce (to prevent session riding) */
418 int inuse; /* set to nonzero if bound to a running thread */
420 /* Session local Members */
421 int serv_sock; /* Client socket to Citadel server */
422 StrBuf *ReadBuf; /* linebuffered reads from the server */
423 StrBuf *MigrateReadLineBuf; /* here we buffer legacy server read stuff */
424 const char *ReadPos; /* whats our read position in ReadBuf? */
425 int last_chat_seq; /* When in chat - last message seq# we saw */
426 time_t lastreq; /* Timestamp of most recent HTTP */
427 time_t last_pager_check; /* last time we polled for instant msgs */
428 ServInfo *serv_info; /* Information about the citserver we're connected to */
429 StrBuf *PushedDestination; /* Where to go after login, registration, etc. */
431 /* Request local Members */
432 StrBuf *CLineBuf; /* linebuffering client stuff */
434 StrBuf *WBuf; /* Our output buffer */
435 StrBuf *HBuf; /* Our HeaderBuffer */
436 StrBuf *WFBuf; /* Wildfire error logging buffer */
437 StrBuf *trailing_javascript; /* extra javascript to be appended to page */
438 StrBuf *ImportantMsg;
439 HashList *Directory; /* Parts of the directory URL in snippets */
440 const Floor *CurrentFloor; /* when Parsing REST, which floor are we on? */
443 StrBuf *wc_username; /* login name of current user */
444 StrBuf *wc_fullname; /* Screen name of current user */
445 StrBuf *wc_password; /* Password of current user */
446 StrBuf *httpauth_pass; /* only for GroupDAV sessions */
447 int axlevel; /* this user's access level */
448 int is_aide; /* nonzero == this user is an Admin */
449 int connected; /* nonzero == we are connected to Citadel */
450 int logged_in; /* nonzero == we are logged in */
451 int need_regi; /* This user needs to register. */
452 int need_vali; /* New users require validation. */
455 StrBuf *cs_inet_email; /* User's preferred Internet addr. */
456 HashList *hash_prefs; /* WebCit preferences for this user */
457 StrBuf *DefaultCharset; /* Charset the user preferes */
458 int downloaded_prefs; /* Has the client download its prefs yet? */
459 int SavePrefsToServer; /* Should we save our preferences to the server at the end of the request? */
460 int selected_language; /* Language selected by user */
461 int time_format_cache; /* which timeformat does our user like? */
463 folder CurRoom; /* information about our current room */
464 const folder *ThisRoom; /* if REST found a room, remember it here. */
465 /* next/previous room thingabob */
466 struct march *march; /* march mode room list */
467 char ugname[128]; /* where does 'ungoto' take us */
468 long uglsn; /* last seen message number for ungoto */
470 /* Uploading; mime attachments for composing messages */
471 HashList *attachments; /* list of attachments for 'enter message' */
472 int upload_length; /* content length of http-uploaded data */
473 StrBuf *upload; /* pointer to http-uploaded data */
474 StrBuf *upload_filename; /* filename of http-uploaded data */
475 char upload_content_type[256]; /* content type of http-uploaded data */
477 int remember_new_mail; /* last count of new mail messages */
479 /* Roomiew control */
480 HashList *Floors; /* floors our citserver has hashed numeric for quicker access*/
481 HashList *FloorsByName; /* same but hashed by its name */
482 HashList *Rooms; /* our directory structure as loaded by LKRA */
483 HashList *summ; /* list of messages for mailbox summary view */
484 /** Perhaps these should be within a struct instead */
485 long startmsg; /* message number to start at */
486 long maxmsgs; /* maximum messages to display */
487 long num_displayed; /* number of messages actually displayed */
488 HashList *disp_cal_items; /* sorted list of calendar items; startdate is the sort criteria. */
491 char last_chat_user[256];
493 /* Iconbar controls */
494 int cache_max_folders;
495 int cache_num_floors;
496 long *IBSettingsVec; /* which icons should be shown / not shown? */
497 const StrBuf *floordiv_expanded; /* which floordiv currently expanded */
498 int ib_wholist_expanded;
499 int ib_roomlist_expanded;
501 /* our known Sieve scripts; loaded by SIEVE:SCRIPTS iterator. */
502 HashList *KnownSieveScripts;
504 /* Transcoding cache buffers; used to avoid to frequent realloc */
508 /* cache stuff for templates. TODO: find a smarter way */
509 HashList *ServCfg; /* cache our server config for editing */
510 HashList *InetCfg; /* Our inet server config for editing */
511 ExpirePolicy Policy[maxpolicy];
513 /* used by the blog viewer */
514 int bptlid; /* hash of thread currently being rendered */
518 typedef void (*Header_Evaluator)(StrBuf *Line, ParsedHttpHdrs *hdr);
520 typedef struct _HttpHeader {
526 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F);
536 #define num_parms(source) num_tokens(source, '|')
539 #define site_prefix (WC ? (WC->Hdr->HostHeader) : NULL)
541 /* Per-session data */
542 #define WC ((struct wcsession *)pthread_getspecific(MyConKey))
543 extern pthread_key_t MyConKey;
545 /* Per-thread SSL context */
547 #define THREADSSL ((SSL *)pthread_getspecific(ThreadSSL))
548 extern pthread_key_t ThreadSSL;
549 extern char ctdl_key_dir[PATH_MAX];
550 extern char file_crpt_file_key[PATH_MAX];
551 extern char file_crpt_file_csr[PATH_MAX];
552 extern char file_crpt_file_cer[PATH_MAX];
556 void ssl_lock(int mode, int n, const char *file, int line);
557 int starttls(int sock);
558 extern SSL_CTX *ssl_ctx;
559 int client_read_sslbuffer(StrBuf *buf, int timeout);
560 void client_write_ssl(const StrBuf *Buf);
564 extern int follow_xff;
565 extern char *server_cookie;
566 extern char *ctdlhost, *ctdlport;
567 extern char *axdefs[];
568 extern int num_threads_existing;
569 extern int num_threads_executing;
570 extern int setup_wizard;
571 extern char wizard_filename[];
573 void InitialiseSemaphores(void);
574 void begin_critical_section(int which_one);
575 void end_critical_section(int which_one);
578 extern void do_404(void);
579 void http_redirect(const char *);
582 #ifdef UBER_VERBOSE_DEBUGGING
583 #define wc_printf(...) wcc_printf(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
584 void wcc_printf(const char *FILE, const char *FUNCTION, long LINE, const char *format, ...);
586 void wc_printf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
589 void hprintf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
591 void CheckAuthBasic(ParsedHttpHdrs *hdr);
592 void GetAuthBasic(ParsedHttpHdrs *hdr);
594 void sleeeeeeeeeep(int);
596 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm);
597 void fmt_time(char *buf, size_t siz, time_t thetime);
598 void httpdate(char *buf, time_t thetime);
599 time_t httpdate_to_timestamp(StrBuf *buf);
604 void end_webcit_session(void);
609 void cookie_to_stuff(StrBuf *cookie,
616 void locate_host(StrBuf *TBuf, int);
617 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response);
619 void display_login(void);
620 void display_openids(void);
621 void display_default_landing_page(void);
622 void do_welcome(void);
624 void display_reg(int during_login);
625 void display_main_menu(void);
626 void display_aide_menu(void);
628 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority);
629 void CreateMimeStr(void);
632 void pop_destination(void);
634 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source);
635 void wDumpContent(int);
638 void PutRequestLocalMem(void *Data, DeleteHashDataFunc DeleteIt);
640 void output_headers( int do_httpheaders,
646 void output_custom_content_header(const char *ctype);
647 void cdataout(char *rawdata);
650 void url(char *buf, size_t bufsize);
651 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
654 void display_vcard(StrBuf *Target, wc_mime_attachment *Mime, char alpha, int full, char **storename, long msgnum);
656 void display_success(const char *successmessage);
658 void shutdown_sessions(void);
662 StrBuf *load_mimepart(long msgnum, char *partnum);
663 void MimeLoadData(wc_mime_attachment *Mime);
664 void do_edit_vcard(long msgnum, char *partnum,
665 message_summary *VCMsg,
666 wc_mime_attachment *VCAtt,
667 const char *return_to,
668 const char *force_room);
670 void select_user_to_edit(const char *preselect);
672 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext);
673 void output_html(const char *, int, int, StrBuf *, StrBuf *);
675 ssize_t write(int fd, const void *buf, size_t count);
676 void cal_process_attachment(wc_mime_attachment *Mime);
678 void address_book_popup(void);
679 void begin_ajax_response(void);
680 void end_ajax_response(void);
682 extern char *months[];
684 long locate_user_vcard_in_this_room(message_summary **VCMsg,
685 wc_mime_attachment **VCAtt);
686 void http_transmit_thing(const char *content_type, int is_static);
687 long unescape_input(char *buf);
688 void check_thread_pool_size(void);
689 void StrEndTab(StrBuf *Target, int tabnum, int num_tabs);
690 void StrBeginTab(StrBuf *Target, int tabnum, int num_tabs, StrBuf **Names);
691 void StrTabbedDialog(StrBuf *Target, int num_tabs, StrBuf *tabnames[]);
692 void tabbed_dialog(int num_tabs, char *tabnames[]);
693 void begin_tab(int tabnum, int num_tabs);
694 void end_tab(int tabnum, int num_tabs);
696 int get_time_format_cached (void);
697 void display_wiki_pagelist(void);
698 void str_wiki_index(char *);
700 HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP);
702 /* actual supported locales */
703 void TmplGettext(StrBuf *Target, WCTemplputParams *TP);
705 void set_selected_language(const char *);
706 void go_selected_language(void);
707 void stop_selected_language(void);
708 const char *get_selected_language(void);
710 void utf8ify_rfc822_string(char **buf);
712 void begin_burst(void);
713 long end_burst(void);
715 void AppendImportantMessage(const char *pch, long len);
717 void http_datestring(char *buf, size_t n, time_t xtime);
720 /* These should be empty, but we have them for testing */
721 #define DEFAULT_HTTPAUTH_USER ""
722 #define DEFAULT_HTTPAUTH_PASS ""
725 /* Exit codes 101 through 109 are initialization failures so we don't want to
726 * just keep respawning indefinitely.
728 #define WC_EXIT_BIND 101 /* Can't bind to the port */
729 #define WC_EXIT_SSL 102 /* Can't initialize SSL */
732 #define WC_TIMEFORMAT_NONE 0
733 #define WC_TIMEFORMAT_AMPM 1
734 #define WC_TIMEFORMAT_24 2
736 extern int time_to_die; /* Nonzero if server is shutting down */
737 extern int DisableGzip;
740 * Array type for a blog post. The first message is the post; the rest are comments
744 long *msgs; /* Array of msgnums for messages we are displaying */
745 int num_msgs; /* Number of msgnums stored in 'msgs' */
746 int alloc_msgs; /* Currently allocated size of array */
751 * Data which gets returned from a call to blogview_learn_thread_references()
759 struct bltr blogview_learn_thread_references(long msgnum);
760 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP);
761 void display_summary_page(void);