* Bumped internal version number to 6.31. Minimum Citadel server required
[citadel.git] / webcit / webcit.h
1 /* $Id$ */
2
3 #include <ctype.h>
4 #include <stdlib.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include <stdio.h>
9 #ifdef HAVE_FCNTL_H
10 #include <fcntl.h>
11 #endif
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #ifdef HAVE_SYS_TIME_H
17 #include <sys/time.h>
18 #endif
19 #include <sys/stat.h>
20 #ifdef HAVE_LIMITS_H
21 #include <limits.h>
22 #endif
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/un.h>
26 #include <netdb.h>
27 #include <sys/poll.h>
28 #include <string.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <pthread.h>
33 #include <signal.h>
34 #include <sys/utsname.h>
35
36 #ifdef HAVE_ICONV
37 #include <iconv.h>
38 #endif
39
40 #include "gettext.h"
41
42 #if ENABLE_NLS
43 #include <locale.h>
44 #define _(string)       gettext(string)
45 #else
46 #define _(string)       (string)
47 #endif
48
49 /*
50  * Uncomment to dump an HTTP trace to stderr
51 #define HTTP_TRACING 1
52  */
53
54 #ifdef HTTP_TRACING
55 #undef HAVE_ZLIB_H
56 #undef HAVE_ZLIB
57 #endif
58
59 #ifdef HAVE_ZLIB_H
60 #include <zlib.h>
61 #endif
62
63 #ifdef HAVE_ICAL_H
64 #ifdef HAVE_LIBICAL
65 #define WEBCIT_WITH_CALENDAR_SERVICE 1
66 #endif
67 #endif
68
69 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
70 #include <ical.h>
71 #endif
72
73 #ifdef HAVE_OPENSSL
74 /* Work around RedHat's b0rken OpenSSL includes */
75 #define OPENSSL_NO_KRB5
76 #include <openssl/ssl.h>
77 #include <openssl/err.h>
78 #include <openssl/rand.h>
79 #endif
80
81 #define CALENDAR_ROOM_NAME      "Calendar"
82 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
83
84 #define SIZ                     4096            /* generic buffer size */
85
86 #define TRACE fprintf(stderr, "Checkpoint: %s, %d\n", __FILE__, __LINE__)
87
88 #define SLEEPING                180             /* TCP connection timeout */
89 #define WEBCIT_TIMEOUT          900             /* WebCit session timeout */
90 #define PORT_NUM                2000            /* port number to listen on */
91 #define SERVER                  "WebCit v6.31"  /* who's in da house */
92 #define DEVELOPER_ID            0
93 #define CLIENT_ID               4
94 #define CLIENT_VERSION          631             /* This version of WebCit */
95 #define MINIMUM_CIT_VERSION     661             /* min required Citadel ver. */
96 #define DEFAULT_HOST            "localhost"     /* Default Citadel server */
97 #define DEFAULT_PORT            "504"
98 #define LB                      (1)             /* Internal escape chars */
99 #define RB                      (2)
100 #define QU                      (3)
101 #define TARGET                  "webcit01"      /* Target for inline URL's */
102 #define HOUSEKEEPING            15              /* Housekeeping frequency */
103 #define MIN_WORKER_THREADS      5
104 #define MAX_WORKER_THREADS      250
105 #define LISTEN_QUEUE_LENGTH     100             /* listen() backlog queue */
106
107 #define USERCONFIGROOM          "My Citadel Config"
108 #define DEFAULT_MAXMSGS         20
109
110
111 /*
112  * Room flags (from Citadel)
113  *
114  * bucket one...
115  */
116 #define QR_PERMANENT    1               /* Room does not purge        */
117 #define QR_INUSE        2               /* Set if in use, clear if avail    */
118 #define QR_PRIVATE      4               /* Set for any type of private room */
119 #define QR_PASSWORDED   8               /* Set if there's a password too    */
120 #define QR_GUESSNAME    16              /* Set if it's a guessname room     */
121 #define QR_DIRECTORY    32              /* Directory room                  */
122 #define QR_UPLOAD       64              /* Allowed to upload            */
123 #define QR_DOWNLOAD     128             /* Allowed to download        */
124 #define QR_VISDIR       256             /* Visible directory            */
125 #define QR_ANONONLY     512             /* Anonymous-Only room        */
126 #define QR_ANONOPT      1024            /* Anonymous-Option room            */
127 #define QR_NETWORK      2048            /* Shared network room        */
128 #define QR_PREFONLY     4096            /* Preferred status needed to enter */
129 #define QR_READONLY     8192            /* Aide status required to post     */
130 #define QR_MAILBOX      16384           /* Set if this is a private mailbox */
131
132 /*
133  * bucket two...
134  */
135 #define QR2_SYSTEM      1               /* System room; hide by default     */
136 #define QR2_SELFLIST    2               /* Self-service mailing list mgmt   */
137
138
139 #define UA_KNOWN                2
140 #define UA_GOTOALLOWED    4
141 #define UA_HASNEWMSGS      8
142 #define UA_ZAPPED              16
143
144
145
146
147
148
149 struct httprequest {
150         struct httprequest *next;
151         char line[SIZ];
152 };
153
154 struct urlcontent {
155         struct urlcontent *next;
156         char url_key[32];
157         char *url_data;
158 };
159
160 struct serv_info {
161         int serv_pid;
162         char serv_nodename[32];
163         char serv_humannode[64];
164         char serv_fqdn[64];
165         char serv_software[64];
166         int serv_rev_level;
167         char serv_bbs_city[64];
168         char serv_sysadm[64];
169         char serv_moreprompt[SIZ];
170         int serv_ok_floors;
171         int serv_supports_ldap;
172 };
173
174
175
176 /*
177  * This struct holds a list of rooms for <G>oto operations.
178  */
179 struct march {
180         struct march *next;
181         char march_name[128];
182         int march_floor;
183         int march_order;
184 };
185
186 /* 
187  * This struct holds a list of rooms for client display.
188  * (oooh, a tree!)
189  */
190 struct roomlisting {
191         struct roomlisting *lnext;
192         struct roomlisting *rnext;
193         char rlname[128];
194         unsigned rlflags;
195         int rlfloor;
196         int rlorder;
197 };
198
199
200
201 /*
202  * Dynamic content for variable substitution in templates
203  */
204 struct wcsubst {
205         struct wcsubst *next;
206         int wcs_type;
207         char wcs_key[32];
208         void *wcs_value;
209         void (*wcs_function)(void);
210 };
211
212 /*
213  * Values for wcs_type
214  */
215 enum {
216         WCS_STRING,
217         WCS_FUNCTION,
218         WCS_SERVCMD
219 };
220
221
222 struct wc_attachment {
223         struct wc_attachment *next;
224         size_t length;
225         char content_type[SIZ];
226         char filename[SIZ];
227         char *data;
228 };
229
230 struct message_summary {
231         time_t date;
232         long msgnum;
233         char from[128];
234         char to[128];
235         char subj[128];
236         int hasattachments;
237         int is_new;
238 };
239
240 /*
241  * One of these is kept for each active Citadel session.
242  * HTTP transactions are bound to one at a time.
243  */
244 struct wcsession {
245         struct wcsession *next;         /* Linked list */
246         int wc_session;                 /* WebCit session ID */
247         char wc_username[SIZ];
248         char wc_password[SIZ];
249         char wc_roomname[SIZ];
250         int connected;
251         int logged_in;
252         int axlevel;
253         int is_aide;
254         int is_room_aide;
255         int http_sock;
256         int serv_sock;
257         int chat_sock;
258         unsigned room_flags;
259         int wc_view;
260         int wc_default_view;
261         int wc_floor;
262         char ugname[128];
263         long uglsn;
264         int upload_length;
265         char *upload;
266         char upload_filename[SIZ];
267         char upload_content_type[SIZ];
268         int new_mail;
269         int remember_new_mail;
270         int need_regi;                  /* This user needs to register. */
271         int need_vali;                  /* New users require validation. */
272         char cs_inet_email[SIZ];        /* User's preferred Internet addr. */
273         pthread_mutex_t SessionMutex;   /* mutex for exclusive access */
274         time_t lastreq;                 /* Timestamp of most recent HTTP */
275         int killthis;                   /* Nonzero == purge this session */
276         struct march *march;            /* march mode room list */
277         char reply_to[SIZ];             /* reply-to address */
278
279         long msgarr[10000];             /* for read operations */
280         int num_summ;
281         struct message_summary *summ;
282
283         int is_wap;                     /* Client is a WAP gateway */
284         struct urlcontent *urlstrings;
285         int HaveInstantMessages;        /* Nonzero if incoming msgs exist */
286         struct wcsubst *vars;
287         char this_page[SIZ];            /* address of current page */
288         char http_host[SIZ];            /* HTTP Host: header */
289         char *preferences;
290 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
291         struct disp_cal {
292                 icalcomponent *cal;             /* cal items for display */
293                 long cal_msgnum;                /* cal msgids for display */
294         } *disp_cal;
295         int num_cal;
296 #endif
297         struct wc_attachment *first_attachment;
298         char ImportantMessage[SIZ];
299         char last_chat_user[SIZ];
300         int ctdl_pid;                   /* Session ID on the Citadel server */
301         char httpauth_user[SIZ];        /* only for GroupDAV sessions */
302         char httpauth_pass[SIZ];        /* only for GroupDAV sessions */
303
304         size_t burst_len;
305         char *burst;
306         int gzip_ok;                    /* Nonzero if Accept-encoding: gzip */
307         int is_mailbox;                 /* the current room is a private mailbox */
308 };
309
310 #define num_parms(source)               num_tokens(source, '|')
311
312 /* Per-session data */
313 #define WC ((struct wcsession *)pthread_getspecific(MyConKey))
314 extern pthread_key_t MyConKey;
315
316 /* Per-thread SSL context */
317 #ifdef HAVE_OPENSSL
318 #define THREADSSL ((SSL *)pthread_getspecific(ThreadSSL))
319 extern pthread_key_t ThreadSSL;
320 #endif
321
322 struct serv_info serv_info;
323 extern char floorlist[128][SIZ];
324 extern char *axdefs[];
325 extern char *ctdlhost, *ctdlport;
326 extern char *server_cookie;
327 extern int is_https;
328 extern int setup_wizard;
329 extern char wizard_filename[];
330 extern time_t if_modified_since;
331 void do_setup_wizard(void);
332
333 void stuff_to_cookie(char *cookie, int session,
334                         char *user, char *pass, char *room);
335 void cookie_to_stuff(char *cookie, int *session,
336                 char *user, size_t user_len,
337                 char *pass, size_t pass_len,
338                 char *room, size_t room_len);
339 void locate_host(char *, int);
340 void become_logged_in(char *, char *, char *);
341 void do_login(void);
342 void display_login(char *mesg);
343 void do_welcome(void);
344 void do_logout(void);
345 void display_main_menu(void);
346 void display_aide_menu(void);
347 void display_advanced_menu(void);
348 void slrp_highest(void);
349 void gotonext(void);
350 void ungoto(void);
351 void get_serv_info(char *, char *);
352 int uds_connectsock(char *);
353 int tcp_connectsock(char *, char *);
354 void serv_getln(char *strbuf, int bufsize);
355 void serv_puts(char *string);
356 void who(void);
357 void who_inner_div(void);
358 void fmout(char *align);
359 void pullquote_fmout(void);
360 void wDumpContent(int);
361 void serv_printf(const char *format,...);
362 char *bstr(char *key);
363 void urlesc(char *, char *);
364 void urlescputs(char *);
365 void jsesc(char *, char *);
366 void jsescputs(char *);
367 void output_headers(    int do_httpheaders,
368                         int do_htmlhead,
369                         int do_room_banner,
370                         int unset_cookies,
371                         int suppress_check,
372                         int cache);
373 void wprintf(const char *format,...);
374 void output_static(char *what);
375 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks);
376 void escputs(char *strbuf);
377 void url(char *buf);
378 void escputs1(char *strbuf, int nbsp, int nolinebreaks);
379 void msgesc(char *target, char *strbuf);
380 void msgescputs(char *strbuf);
381 int extract_int(const char *source, int parmnum);
382 long extract_long(const char *source, int parmnum);
383 void stripout(char *str, char leftboundary, char rightboundary);
384 void dump_vars(void);
385 void embed_main_menu(void);
386 void serv_read(char *buf, int bytes);
387 int haschar(char *, char);
388 void readloop(char *oper);
389 void embed_message(void);
390 void print_message(void);
391 void text_to_server(char *ptr, int convert_to_html);
392 void display_enter(void);
393 void post_message(void);
394 void confirm_delete_msg(void);
395 void delete_msg(void);
396 void confirm_move_msg(void);
397 void move_msg(void);
398 void userlist(void);
399 void showuser(void);
400 void display_page(void);
401 void page_user(void);
402 void do_chat(void);
403 void display_private(char *rname, int req_pass);
404 void goto_private(void);
405 void zapped_list(void);
406 void display_zap(void);
407 void zap(void);
408 void display_success(char *);
409 void authorization_required(const char *message);
410 void display_entroom(void);
411 void entroom(void);
412 void display_editroom(void);
413 void netedit(void);
414 void editroom(void);
415 void display_whok(void);
416 void do_invt_kick(void);
417 void server_to_text(void);
418 void save_edit(char *description, char *enter_cmd, int regoto);
419 void display_edit(char *description, char *check_cmd,
420                   char *read_cmd, char *save_cmd, int with_room_banner);
421 int gotoroom(char *gname);
422 void confirm_delete_room(void);
423 void delete_room(void);
424 void validate(void);
425 void display_graphics_upload(char *, char *, char *);
426 void do_graphics_upload(char *upl_cmd);
427 void serv_read(char *buf, int bytes);
428 void serv_gets(char *strbuf);
429 void serv_write(char *buf, int nbytes);
430 void serv_puts(char *string);
431 void serv_printf(const char *format,...);
432 void load_floorlist(void);
433 void display_reg(int);
434 void display_changepw(void);
435 void changepw(void);
436 void display_edit_node(void);
437 void edit_node(void);
438 void display_netconf(void);
439 void display_confirm_delete_node(void);
440 void delete_node(void);
441 void display_add_node(void);
442 void add_node(void);
443 void terminate_session(void);
444 void edit_me(void);
445 void display_siteconfig(void);
446 void siteconfig(void);
447 void display_generic(void);
448 void do_generic(void);
449 void display_menubar(int);
450 void smart_goto(char *);
451 void worker_entry(void);
452 void session_loop(struct httprequest *);
453 void fmt_date(char *buf, time_t thetime, int brief);
454 void fmt_time(char *buf, time_t thetime);
455 void httpdate(char *buf, time_t thetime);
456 time_t httpdate_to_timestamp(const char *buf);
457 void end_webcit_session(void);
458 void page_popup(void);
459 void chat_recv(void);
460 void chat_send(void);
461 void http_redirect(char *);
462 void clear_local_substs(void);
463 void svprintf(char *keyname, int keytype, const char *format,...);
464 void svcallback(char *keyname, void (*fcn_ptr)() );
465 void do_template(void *templatename);
466 int lingering_close(int fd);
467 char *memreadline(char *start, char *buf, int maxlen);
468 int num_tokens (char *source, char tok);
469 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
470 void remove_token(char *source, int parmnum, char separator);
471 char *load_mimepart(long msgnum, char *partnum);
472 int pattern2(char *search, char *patn);
473 void do_edit_vcard(long, char *, char *);
474 void edit_vcard(void);
475 void submit_vcard(void);
476 void striplt(char *);
477 void select_user_to_edit(char *message, char *preselect);
478 void delete_user(char *);
479 void display_edituser(char *who, int is_new);
480 void create_user(void);
481 void edituser(void);
482 void do_change_view(int);
483 void change_view(void);
484 void folders(void);
485 void do_stuff_to_msgs(void);
486 void load_preferences(void);
487 void save_preferences(void);
488 void get_preference(char *key, char *value, size_t value_len);
489 void set_preference(char *key, char *value, int save_to_server);
490 void knrooms(void);
491 int is_msg_in_mset(char *mset, long msgnum);
492 char *safestrncpy(char *dest, const char *src, size_t n);
493 void display_addressbook(long msgnum, char alpha);
494 void offer_start_page(void);
495 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext);
496 void change_start_page(void);
497 void output_html(char *);
498 void display_floorconfig(char *);
499 void delete_floor(void);
500 void create_floor(void);
501 void rename_floor(void);
502 void do_listsub(void);
503 void toggle_self_service(void);
504 void summary(void);
505 void summary_inner_div(void);
506 ssize_t write(int fd, const void *buf, size_t count);
507 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum);
508 void display_calendar(long msgnum);
509 void display_task(long msgnum);
510 void display_note(long msgnum);
511 void do_calendar_view(void);
512 void do_tasks_view(void);
513 void free_calendar_buffer(void);
514 void calendar_summary_view(void);
515 int load_msg_ptrs(char *servcmd, int with_headers);
516 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen);
517 int CtdlDecodeBase64(char *dest, const char *source, size_t length);
518 void free_attachments(struct wcsession *sess);
519 void set_room_policy(void);
520 void display_inetconf(void);
521 void save_inetconf(void);
522 void generate_uuid(char *);
523 void display_preferences(void);
524 void set_preferences(void);
525 void recp_autocomplete(char *);
526 void begin_ajax_response(void);
527 void end_ajax_response(void);
528
529 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
530 void display_edit_task(void);
531 void save_task(void);
532 void display_edit_event(void);
533 void save_event(void);
534 void display_icaltimetype_as_webform(struct icaltimetype *, char *);
535 void icaltime_from_webform(struct icaltimetype *result, char *prefix);
536 void icaltime_from_webform_dateonly(struct icaltimetype *result, char *prefix);
537 void display_edit_individual_event(icalcomponent *supplied_vtodo, long msgnum);
538 void save_individual_event(icalcomponent *supplied_vtodo, long msgnum);
539 void respond_to_request(void);
540 void handle_rsvp(void);
541 void ical_dezonify(icalcomponent *cal);
542 void partstat_as_string(char *buf, icalproperty *attendee);
543 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp);
544 void check_attendee_availability(icalcomponent *supplied_vevent);
545 void do_freebusy(char *req);
546 #endif
547
548 extern char *months[];
549 extern char *days[];
550 void read_server_binary(char *buffer, size_t total_len);
551 char *read_server_text(void);
552 int goto_config_room(void);
553 long locate_user_vcard(char *username, long usernum);
554 void sleeeeeeeeeep(int);
555 void http_transmit_thing(char *thing, size_t length, char *content_type,
556                          int is_static);
557 void unescape_input(char *buf);
558 void do_iconbar(void);
559 void display_customize_iconbar(void);
560 void commit_iconbar(void);
561 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen);
562 void spawn_another_worker_thread(void);
563 void display_rss(char *roomname, char *request_method);
564
565 void embed_room_banner(char *, int);
566 /* navbar types that can be passed to embed_room_banner */
567 enum {
568         navbar_none,
569         navbar_default
570 };
571
572
573 #ifdef HAVE_OPENSSL
574 void init_ssl(void);
575 void endtls(void);
576 void ssl_lock(int mode, int n, const char *file, int line);
577 int starttls(int sock);
578 extern SSL_CTX *ssl_ctx;  
579 int client_read_ssl(char *buf, int bytes, int timeout);
580 void client_write_ssl(char *buf, int nbytes);
581 #endif
582
583 #ifdef HAVE_ZLIB
584 #include <zlib.h>
585 int ZEXPORT compress_gzip(Bytef * dest, uLongf * destLen,
586                           const Bytef * source, uLong sourceLen, int level);
587 #endif
588
589 #ifdef HAVE_ICONV
590 void utf8ify_rfc822_string(char *buf);
591 #endif
592
593 void begin_burst(void);
594 void end_burst(void);
595
596 extern char *ascmonths[];
597 extern char *hourname[];
598 void http_datestring(char *buf, size_t n, time_t xtime);
599
600 /* Views (from citadel.h) */
601 #define VIEW_BBS                0       /* Traditional Citadel BBS view */
602 #define VIEW_MAILBOX            1       /* Mailbox summary */
603 #define VIEW_ADDRESSBOOK        2       /* Address book view */
604 #define VIEW_CALENDAR           3       /* Calendar view */
605 #define VIEW_TASKS              4       /* Tasks view */
606 #define VIEW_NOTES              5       /* Notes view */
607
608
609 /* These should be empty, but we have them for testing */
610 #define DEFAULT_HTTPAUTH_USER   ""
611 #define DEFAULT_HTTPAUTH_PASS   ""
612