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