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__)
126 #define SLEEPING 180 /* TCP connection timeout */
127 #define WEBCIT_TIMEOUT 900 /* WebCit session timeout */
128 #define PORT_NUM 2000 /* port number to listen on */
129 #define DEVELOPER_ID 0
131 #define CLIENT_VERSION 813 /* This version of WebCit */
132 #define MINIMUM_CIT_VERSION 813 /* Minimum required version of Citadel server */
133 #define LIBCITADEL_MIN 813 /* Minimum required version of libcitadel */
134 #define DEFAULT_HOST "localhost" /* Default Citadel server */
135 #define DEFAULT_PORT "504"
136 #define TARGET "webcit01" /* Window target for inline URL's */
137 #define HOUSEKEEPING 15 /* Housekeeping frequency */
138 #define MAX_WORKER_THREADS 250
139 #define LISTEN_QUEUE_LENGTH 100 /* listen() backlog queue */
141 #define USERCONFIGROOM "My Citadel Config"
142 #define DEFAULT_MAXMSGS 20
145 #ifdef LIBCITADEL_VERSION_NUMBER
146 #if LIBCITADEL_VERSION_NUMBER < LIBCITADEL_MIN
147 #error libcitadel is too old. Please upgrade it before continuing.
154 #define SRV_STATUS_MSG(ServerLineBuf) (ChrPtr(ServerLineBuf) + 4), (StrLength(ServerLineBuf) - 4)
155 #define MAJORCODE(a) (((int)(a / 100) ) * 100)
157 #define LISTING_FOLLOWS 100
159 #define MORE_DATA 300
160 #define SEND_LISTING 400
162 #define BINARY_FOLLOWS 600
163 #define SEND_BINARY 700
164 #define START_CHAT_MODE 800
165 #define ASYNC_MSG 900
167 #define MINORCODE(a) (a % 100)
168 #define ASYNC_GEXP 02
169 #define INTERNAL_ERROR 10
171 #define ILLEGAL_VALUE 12
172 #define NOT_LOGGED_IN 20
173 #define CMD_NOT_SUPPORTED 30
174 #define SERVER_SHUTTING_DOWN 31
175 #define PASSWORD_REQUIRED 40
176 #define ALREADY_LOGGED_IN 41
177 #define USERNAME_REQUIRED 42
178 #define HIGHER_ACCESS_REQUIRED 50
179 #define MAX_SESSIONS_EXCEEDED 51
180 #define RESOURCE_BUSY 52
181 #define RESOURCE_NOT_OPEN 53
183 #define INVALID_FLOOR_OPERATION 61
184 #define NO_SUCH_USER 70
185 #define FILE_NOT_FOUND 71
186 #define ROOM_NOT_FOUND 72
187 #define NO_SUCH_SYSTEM 73
188 #define ALREADY_EXISTS 74
189 #define MESSAGE_NOT_FOUND 75
192 * NLI is the string that shows up in a who's online listing for sessions
193 * that are active but do not (yet) have a user logged in.
195 #define NLI "(not logged in)"
198 * Expiry policy for the autopurger
200 #define EXPIRE_NEXTLEVEL 0 /* Inherit expiration policy */
201 #define EXPIRE_MANUAL 1 /* Don't expire messages at all */
202 #define EXPIRE_NUMMSGS 2 /* Keep only latest n messages */
203 #define EXPIRE_AGE 3 /* Expire messages after n days */
205 typedef struct __ExpirePolicy {
209 void LoadExpirePolicy(GPEXWhichPolicy which);
210 void SaveExpirePolicyFromHTTP(GPEXWhichPolicy which);
213 * Linked list of session variables encoded in an x-www-urlencoded content type
215 typedef struct urlcontent urlcontent;
217 char url_key[32]; /* key */
218 StrBuf *url_data; /* value */
222 * Information about the Citadel server to which we are connected
224 typedef struct _serv_info {
225 int serv_pid; /* Process ID of the Citadel server */
226 StrBuf *serv_nodename; /* Node name of the Citadel server */
227 StrBuf *serv_humannode; /* Juman readable node name of the Citadel server */
228 StrBuf *serv_fqdn; /* Fully qualified Domain Name (such as uncensored.citadel.org) */
229 StrBuf *serv_software; /* Free form text description of the server software in use */
230 int serv_rev_level; /* Server version number (times 100) */
231 StrBuf *serv_bbs_city; /* Geographic location of the Citadel server */
232 StrBuf *serv_sysadm; /* Name of system administrator */
233 int serv_supports_ldap; /* is the server linked against an ldap tree for adresses? */
234 int serv_newuser_disabled; /* Has the server disabled self-service new user creation? */
235 StrBuf *serv_default_cal_zone; /* Default timezone for unspecified calendar items */
236 int serv_supports_sieve; /* Server supports Sieve mail filtering */
237 int serv_fulltext_enabled; /* Full text index is enabled */
238 StrBuf *serv_svn_revision; /* svn or git revision of the server */
239 int serv_supports_openid; /* Server supports authentication via OpenID */
240 int serv_supports_guest; /* Server supports unauthenticated guest logins */
244 typedef struct _disp_cal {
245 icalcomponent *cal; /* cal items for display */
246 long cal_msgnum; /* cal msgids for display */
247 char *from; /* owner of this component */
248 int unread; /* already seen by the user? */
255 icalcomponent *SortBy; /* cal items for display */
256 icalproperty_status Status;
259 typedef struct _IcalEnumMap {
262 icalproperty_kind map;
266 * Address book entry (keep it short and sweet, it's just a quickie lookup
267 * which we can use to get to the real meat and bones later)
269 typedef struct _addrbookent {
270 char ab_name[64]; /* name string */
271 long ab_msgnum; /* message number of address book entry */
276 #define ANONYMOUS (1<<1)
277 #define NEED_URL (1<<2)
278 #define XHTTP_COMMANDS (1<<3)
280 #define URLNAMESPACE (1<<4)
281 #define LOGCHATTY (1<<5)
282 #define COOKIEUNNEEDED (1<<6)
283 #define ISSTATIC (1<<7)
284 #define FORCE_SESSIONCLOSE (1<<8)
285 #define PARSE_REST_URL (1<<9)
286 #define PROHIBIT_STARTPAGE (1<<10)
289 #define DATEFMT_FULL 0
290 #define DATEFMT_BRIEF 1
291 #define DATEFMT_RAWDATE 2
292 #define DATEFMT_LOCALEDATE 3
293 void webcit_fmt_date(char *buf, size_t siz, time_t thetime, int Format);
296 typedef enum _RESTDispatchID {
302 typedef int (*WebcitRESTDispatchID)(RESTDispatchID WhichAction, int IgnoreFloor);
303 typedef void (*WebcitHandlerFunc)(void);
304 typedef struct _WebcitHandler{
306 WebcitRESTDispatchID RID;
313 void WebcitAddUrlHandler(const char * UrlString, long UrlSLen, const char *DisplayName, long dslen, WebcitHandlerFunc F, long Flags);
315 typedef struct _headereval {
316 ExamineMsgHeaderFunc evaluator;
346 extern const char *ReqStrs[eNONE];
349 #define AUTH_COOKIE 1
354 typedef struct _HdrRefs {
355 long eReqType; /* HTTP method */
362 time_t if_modified_since;
363 int gzip_ok; /* Nonzero if Accept-encoding: gzip */
364 int prohibit_caching;
368 /* these are references into Hdr->HTTPHeaders, so we don't need to free them. */
372 StrBuf *browser_host;
373 StrBuf *browser_language;
378 const WebcitHandler *Handler;
381 typedef struct _ParsedHttpHdrs {
382 int http_sock; /* HTTP server socket */
390 StrBuf *this_page; /* URL of current page */
394 HashList *urlstrings; /* variables passed to webcit in a URL */
395 HashList *HTTPHeaders; /* the headers the client sent us */
396 int nWildfireHeaders; /* how many wildfire headers did we already send? */
403 * One of these is kept for each active Citadel session.
404 * HTTP transactions are bound to one at a time.
407 /* infrastructural members */
408 wcsession *next; /* Linked list */
409 pthread_mutex_t SessionMutex; /* mutex for exclusive access */
410 int wc_session; /* WebCit session ID */
411 int killthis; /* Nonzero == purge this session */
412 int ctdl_pid; /* Session ID on the Citadel server */
413 int nonce; /* session nonce (to prevent session riding) */
414 int inuse; /* set to nonzero if bound to a running thread */
416 /* Session local Members */
417 int serv_sock; /* Client socket to Citadel server */
418 StrBuf *ReadBuf; /* linebuffered reads from the server */
419 StrBuf *MigrateReadLineBuf; /* here we buffer legacy server read stuff */
420 const char *ReadPos; /* whats our read position in ReadBuf? */
421 int last_chat_seq; /* When in chat - last message seq# we saw */
422 time_t lastreq; /* Timestamp of most recent HTTP */
423 time_t last_pager_check; /* last time we polled for instant msgs */
424 ServInfo *serv_info; /* Information about the citserver we're connected to */
425 StrBuf *PushedDestination; /* Where to go after login, registration, etc. */
427 /* Request local Members */
428 StrBuf *CLineBuf; /* linebuffering client stuff */
430 StrBuf *WBuf; /* Our output buffer */
431 StrBuf *HBuf; /* Our HeaderBuffer */
432 StrBuf *WFBuf; /* Wildfire error logging buffer */
433 StrBuf *trailing_javascript; /* extra javascript to be appended to page */
434 StrBuf *ImportantMsg;
435 HashList *Directory; /* Parts of the directory URL in snippets */
436 const Floor *CurrentFloor; /* when Parsing REST, which floor are we on? */
439 StrBuf *wc_username; /* login name of current user */
440 StrBuf *wc_fullname; /* Screen name of current user */
441 StrBuf *wc_password; /* Password of current user */
442 StrBuf *httpauth_pass; /* only for GroupDAV sessions */
443 int axlevel; /* this user's access level */
444 int is_aide; /* nonzero == this user is an Aide */
445 int connected; /* nonzero == we are connected to Citadel */
446 int logged_in; /* nonzero == we are logged in */
447 int need_regi; /* This user needs to register. */
448 int need_vali; /* New users require validation. */
451 StrBuf *cs_inet_email; /* User's preferred Internet addr. */
452 HashList *hash_prefs; /* WebCit preferences for this user */
453 StrBuf *DefaultCharset; /* Charset the user preferes */
454 int downloaded_prefs; /* Has the client download its prefs yet? */
455 int SavePrefsToServer; /* Should we save our preferences to the server at the end of the request? */
456 int selected_language; /* Language selected by user */
457 int time_format_cache; /* which timeformat does our user like? */
459 folder CurRoom; /* information about our current room */
460 const folder *ThisRoom; /* if REST found a room, remember it here. */
461 /* next/previous room thingabob */
462 struct march *march; /* march mode room list */
463 char ugname[128]; /* where does 'ungoto' take us */
464 long uglsn; /* last seen message number for ungoto */
466 /* Uploading; mime attachments for composing messages */
467 HashList *attachments; /* list of attachments for 'enter message' */
468 int upload_length; /* content length of http-uploaded data */
469 StrBuf *upload; /* pointer to http-uploaded data */
470 StrBuf *upload_filename; /* filename of http-uploaded data */
471 char upload_content_type[256]; /* content type of http-uploaded data */
473 int remember_new_mail; /* last count of new mail messages */
475 /* Roomiew control */
476 HashList *Floors; /* floors our citserver has hashed numeric for quicker access*/
477 HashList *FloorsByName; /* same but hashed by its name */
478 HashList *Rooms; /* our directory structure as loaded by LKRA */
479 HashList *summ; /* list of messages for mailbox summary view */
480 /** Perhaps these should be within a struct instead */
481 long startmsg; /* message number to start at */
482 long maxmsgs; /* maximum messages to display */
483 long num_displayed; /* number of messages actually displayed */
484 HashList *disp_cal_items; /* sorted list of calendar items; startdate is the sort criteria. */
487 char last_chat_user[256];
489 /* Iconbar controls */
490 int cache_max_folders;
491 int cache_num_floors;
492 long *IBSettingsVec; /* which icons should be shown / not shown? */
493 const StrBuf *floordiv_expanded; /* which floordiv currently expanded */
494 int ib_wholist_expanded;
495 int ib_roomlist_expanded;
497 /* our known Sieve scripts; loaded by SIEVE:SCRIPTS iterator. */
498 HashList *KnownSieveScripts;
500 /* Transcoding cache buffers; used to avoid to frequent realloc */
504 /* cache stuff for templates. TODO: find a smarter way */
505 HashList *ServCfg; /* cache our server config for editing */
506 HashList *InetCfg; /* Our inet server config for editing */
507 ExpirePolicy Policy[maxpolicy];
509 /* used by the blog viewer */
510 int bptlid; /* hash of thread currently being rendered */
514 typedef void (*Header_Evaluator)(StrBuf *Line, ParsedHttpHdrs *hdr);
516 typedef struct _HttpHeader {
522 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F);
532 #define num_parms(source) num_tokens(source, '|')
535 #define site_prefix (WC ? (WC->Hdr->HostHeader) : NULL)
537 /* Per-session data */
538 #define WC ((struct wcsession *)pthread_getspecific(MyConKey))
539 extern pthread_key_t MyConKey;
541 /* Per-thread SSL context */
543 #define THREADSSL ((SSL *)pthread_getspecific(ThreadSSL))
544 extern pthread_key_t ThreadSSL;
545 extern char ctdl_key_dir[PATH_MAX];
546 extern char file_crpt_file_key[PATH_MAX];
547 extern char file_crpt_file_csr[PATH_MAX];
548 extern char file_crpt_file_cer[PATH_MAX];
552 void ssl_lock(int mode, int n, const char *file, int line);
553 int starttls(int sock);
554 extern SSL_CTX *ssl_ctx;
555 int client_read_sslbuffer(StrBuf *buf, int timeout);
556 void client_write_ssl(const StrBuf *Buf);
560 extern int follow_xff;
561 extern char *server_cookie;
562 extern char *ctdlhost, *ctdlport;
563 extern char *axdefs[];
564 extern int num_threads_existing;
565 extern int num_threads_executing;
566 extern int setup_wizard;
567 extern char wizard_filename[];
569 void InitialiseSemaphores(void);
570 void begin_critical_section(int which_one);
571 void end_critical_section(int which_one);
574 extern void do_404(void);
575 void http_redirect(const char *);
578 #ifdef UBER_VERBOSE_DEBUGGING
579 #define wc_printf(...) wcc_printf(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
580 void wcc_printf(const char *FILE, const char *FUNCTION, long LINE, const char *format, ...);
582 void wc_printf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
585 void hprintf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
587 void CheckAuthBasic(ParsedHttpHdrs *hdr);
588 void GetAuthBasic(ParsedHttpHdrs *hdr);
590 void sleeeeeeeeeep(int);
592 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm);
593 void fmt_time(char *buf, size_t siz, time_t thetime);
594 void httpdate(char *buf, time_t thetime);
595 time_t httpdate_to_timestamp(StrBuf *buf);
600 void end_webcit_session(void);
605 void cookie_to_stuff(StrBuf *cookie,
612 void locate_host(StrBuf *TBuf, int);
613 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response);
615 void display_login(void);
616 void display_openids(void);
617 void display_default_landing_page(void);
618 void do_welcome(void);
620 void display_reg(int during_login);
621 void display_main_menu(void);
622 void display_aide_menu(void);
624 void RegisterEmbeddableMimeType(const char *MimeType, long MTLen, int Priority);
625 void CreateMimeStr(void);
628 void pop_destination(void);
630 void FmOut(StrBuf *Target, const char *align, const StrBuf *Source);
631 void wDumpContent(int);
634 void PutRequestLocalMem(void *Data, DeleteHashDataFunc DeleteIt);
636 void output_headers( int do_httpheaders,
642 void output_custom_content_header(const char *ctype);
643 void cdataout(char *rawdata);
646 void url(char *buf, size_t bufsize);
647 void UrlizeText(StrBuf* Target, StrBuf *Source, StrBuf *WrkBuf);
650 void display_vcard(StrBuf *Target, wc_mime_attachment *Mime, char alpha, int full, char **storename, long msgnum);
652 void display_success(const char *successmessage);
654 void shutdown_sessions(void);
658 StrBuf *load_mimepart(long msgnum, char *partnum);
659 void MimeLoadData(wc_mime_attachment *Mime);
660 void do_edit_vcard(long msgnum, char *partnum,
661 message_summary *VCMsg,
662 wc_mime_attachment *VCAtt,
663 const char *return_to,
664 const char *force_room);
666 void select_user_to_edit(const char *preselect);
668 void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext);
669 void output_html(const char *, int, int, StrBuf *, StrBuf *);
671 ssize_t write(int fd, const void *buf, size_t count);
672 void cal_process_attachment(wc_mime_attachment *Mime);
674 void address_book_popup(void);
675 void begin_ajax_response(void);
676 void end_ajax_response(void);
678 extern char *months[];
680 long locate_user_vcard_in_this_room(message_summary **VCMsg,
681 wc_mime_attachment **VCAtt);
682 void http_transmit_thing(const char *content_type, int is_static);
683 long unescape_input(char *buf);
684 void check_thread_pool_size(void);
685 void StrEndTab(StrBuf *Target, int tabnum, int num_tabs);
686 void StrBeginTab(StrBuf *Target, int tabnum, int num_tabs, StrBuf **Names);
687 void StrTabbedDialog(StrBuf *Target, int num_tabs, StrBuf *tabnames[]);
688 void tabbed_dialog(int num_tabs, char *tabnames[]);
689 void begin_tab(int tabnum, int num_tabs);
690 void end_tab(int tabnum, int num_tabs);
692 int get_time_format_cached (void);
693 void display_wiki_pagelist(void);
694 void str_wiki_index(char *);
696 HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP);
698 /* actual supported locales */
699 void TmplGettext(StrBuf *Target, WCTemplputParams *TP);
701 void set_selected_language(const char *);
702 void go_selected_language(void);
703 void stop_selected_language(void);
704 const char *get_selected_language(void);
706 void utf8ify_rfc822_string(char **buf);
708 void begin_burst(void);
709 long end_burst(void);
711 void AppendImportantMessage(const char *pch, long len);
713 void http_datestring(char *buf, size_t n, time_t xtime);
716 /* These should be empty, but we have them for testing */
717 #define DEFAULT_HTTPAUTH_USER ""
718 #define DEFAULT_HTTPAUTH_PASS ""
721 /* Exit codes 101 through 109 are initialization failures so we don't want to
722 * just keep respawning indefinitely.
724 #define WC_EXIT_BIND 101 /* Can't bind to the port */
725 #define WC_EXIT_SSL 102 /* Can't initialize SSL */
728 #define WC_TIMEFORMAT_NONE 0
729 #define WC_TIMEFORMAT_AMPM 1
730 #define WC_TIMEFORMAT_24 2
732 extern int time_to_die; /* Nonzero if server is shutting down */
733 extern int DisableGzip;
736 * Array type for a blog post. The first message is the post; the rest are comments
740 long *msgs; /* Array of msgnums for messages we are displaying */
741 int num_msgs; /* Number of msgnums stored in 'msgs' */
742 int alloc_msgs; /* Currently allocated size of array */
747 * Data which gets returned from a call to blogview_learn_thread_references()
755 struct bltr blogview_learn_thread_references(long msgnum);
756 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP);
757 void display_summary_page(void);