2 // NNTP server module FIXME THIS IS NOT FINISHED
4 // Copyright (c) 2014 by the citadel.org team
6 // This program is open source software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License version 3.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
24 #include <sys/types.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <libcitadel.h>
48 #include "citserver.h"
56 #include "internet_addressing.h"
59 #include "clientsocket.h"
60 #include "locate_host.h"
61 #include "citadel_dirs.h"
62 #include "ctdl_module.h"
63 #include "serv_nntp.h"
67 // ***************** BEGIN UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************
71 // Tests whether the supplied string is a valid newsgroup name
72 // Returns true (nonzero) or false (0)
74 int is_valid_newsgroup_name(char *name) {
79 if (!strncasecmp(name, "ctdl.", 5)) return(0);
83 if (isalpha(ptr[0])) {
87 if ( (isalnum(ptr[0]))
103 // Convert a Citadel room name to a valid newsgroup name
105 void room_to_newsgroup(char *target, char *source, size_t target_size) {
110 if (is_valid_newsgroup_name(source)) {
111 strncpy(target, source, target_size);
115 strcpy(target, "ctdl.");
120 while (ch=*ptr++, ch!=0) {
121 if (len >= target_size) return;
130 target[len++] = '+' ;
131 sprintf(&target[len], "%02x", ch);
140 // Convert a newsgroup name to a Citadel room name.
141 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
143 void newsgroup_to_room(char *target, char *source, size_t target_size) {
148 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
149 strncpy(target, source, target_size);
155 char *ptr = &source[5];
158 while (ch=*ptr++, ch!=0) {
159 if (len >= target_size) return;
166 digit = strtol(hex, NULL, 16);
175 // ***************** END UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************
180 // Here's where our NNTP session begins its happy day.
182 void nntp_greeting(void)
184 strcpy(CC->cs_clientname, "NNTP session");
185 CC->cs_flags |= CS_STEALTH;
187 CC->session_specific_data = malloc(sizeof(citnntp));
188 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
189 memset(nntpstate, 0, sizeof(citnntp));
191 if (CC->nologin==1) {
192 cprintf("451 Too many connections are already open; please try again later.\r\n");
193 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
197 // Display the standard greeting
198 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", config.c_fqdn);
203 // NNTPS is just like NNTP, except it goes crypto right away.
205 void nntps_greeting(void) {
206 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
208 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
215 // implements the STARTTLS command
217 void nntp_starttls(void)
219 char ok_response[SIZ];
220 char nosup_response[SIZ];
221 char error_response[SIZ];
223 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
224 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
225 sprintf(error_response, "580 Internal error\r\n");
226 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
231 // Implements the CAPABILITY command
233 void nntp_capabilities(void)
235 cprintf("101 Capability list:\r\n");
236 cprintf("IMPLEMENTATION Citadel v%d.%02d\r\n", (REV_LEVEL/100), (REV_LEVEL%100));
237 cprintf("VERSION 2\r\n");
238 cprintf("READER\r\n");
239 cprintf("MODE-READER\r\n");
240 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
242 cprintf("STARTTLS\r\n");
244 if (!CC->logged_in) {
245 cprintf("AUTHINFO USER\r\n");
252 // Implements the QUIT command
256 cprintf("221 Goodbye...\r\n");
257 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
262 // Cleanup hook for this module
264 void nntp_cleanup(void)
266 /* nothing here yet */
271 // Implements the AUTHINFO USER command (RFC 4643)
273 void nntp_authinfo_user(const char *username)
275 int a = CtdlLoginExistingUser(NULL, username);
277 case login_already_logged_in:
278 cprintf("482 Already logged in\r\n");
280 case login_too_many_users:
281 cprintf("481 Too many users are already online (maximum is %d)\r\n", config.c_maxsessions);
284 cprintf("381 Password required for %s\r\n", CC->curr_user);
286 case login_not_found:
287 cprintf("481 %s not found\r\n", username);
290 cprintf("502 Internal error\r\n");
296 // Implements the AUTHINFO PASS command (RFC 4643)
298 void nntp_authinfo_pass(const char *buf)
302 a = CtdlTryPassword(buf, strlen(buf));
305 case pass_already_logged_in:
306 cprintf("482 Already logged in\r\n");
309 cprintf("482 Authentication commands issued out of sequence\r\n");
311 case pass_wrong_password:
312 cprintf("481 Authentication failed\r\n");
315 cprintf("281 Authentication accepted\r\n");
322 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
324 void nntp_authinfo(const char *cmd) {
326 if (!strncasecmp(cmd, "authinfo user ", 14)) {
327 nntp_authinfo_user(&cmd[14]);
330 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
331 nntp_authinfo_pass(&cmd[14]);
335 cprintf("502 command unavailable\r\n");
341 // Utility function to fetch the current list of message numbers in a room
343 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
344 struct nntp_msglist nm;
345 struct cdbdata *cdbfr;
347 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
349 nm.msgnums = (long*)cdbfr->ptr;
351 nm.num_msgs = cdbfr->len / sizeof(long);
363 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
365 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
367 struct nntp_msglist nm;
368 long low_water_mark = 0;
369 long high_water_mark = 0;
371 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
373 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
374 if (!wildmat(n_name, wildmat_pattern)) {
379 nm = nntp_fetch_msglist(qrbuf);
380 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
381 low_water_mark = nm.msgnums[0];
382 high_water_mark = nm.msgnums[nm.num_msgs - 1];
385 // Only the mandatory formats are supported
386 switch(which_format) {
387 case NNTP_LIST_ACTIVE:
388 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
389 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
391 case NNTP_LIST_NEWSGROUPS:
392 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
396 if (nm.msgnums != NULL) {
403 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
405 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
409 time_t thetime = *(time_t *)data;
411 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
414 * The "created after <date/time>" heuristics depend on the happy coincidence
415 * that for a very long time we have used a unix timestamp as the room record's
416 * generation number (QRgen). When this module is merged into the master
417 * source tree we should rename QRgen to QR_create_time or something like that.
421 if (qrbuf->QRgen >= thetime) {
422 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
429 // Implements the NEWGROUPS command
431 void nntp_newgroups(const char *cmd) {
432 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
434 char stringy_date[16];
435 char stringy_time[16];
436 char stringy_gmt[16];
440 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
441 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
442 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
444 memset(&tm, 0, sizeof tm);
445 if (strlen(stringy_date) == 6) {
446 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
450 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
453 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
454 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
455 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
456 thetime = mktime(&tm);
457 if (!strcasecmp(stringy_gmt, "GMT")) {
463 cprintf("231 list of new newsgroups follows\r\n");
464 CtdlGetUser(&CC->user, CC->curr_user);
465 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
471 // Called once per room by nntp_list() to qualify and possibly output a single room
473 void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
477 struct nntp_list_data *nld = (struct nntp_list_data *)data;
479 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
481 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
487 // Implements the LIST commands
489 void nntp_list(const char *cmd) {
490 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
492 char list_format[64];
493 char wildmat_pattern[1024];
494 struct nntp_list_data nld;
496 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
497 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
499 if (strlen(wildmat_pattern) > 0) {
500 nld.wildmat_pattern = wildmat_pattern;
503 nld.wildmat_pattern = NULL;
506 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
507 nld.list_format = NNTP_LIST_ACTIVE;
509 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
510 nld.list_format = NNTP_LIST_NEWSGROUPS;
513 cprintf("501 syntax error , unsupported list format\r\n");
517 cprintf("215 list of newsgroups follows\r\n");
518 CtdlGetUser(&CC->user, CC->curr_user);
519 CtdlForEachRoom(nntp_list_backend, &nld);
525 // Implement HELP command.
527 void nntp_help(void) {
528 cprintf("100 This is the Citadel NNTP service.\r\n");
529 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
535 // back end for the LISTGROUP command , called for each message number
537 void nntp_listgroup_backend(long msgnum, void *userdata) {
539 struct listgroup_range *lr = (struct listgroup_range *)userdata;
541 // check range if supplied
542 if (msgnum < lr->lo) return;
543 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
545 cprintf("%ld\r\n", msgnum);
550 // Implements the GROUP and LISTGROUP commands
552 void nntp_group(const char *cmd) {
553 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
555 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
557 char requested_group[1024];
558 char message_range[256];
561 char requested_room[ROOMNAMELEN];
562 char augmented_roomname[ROOMNAMELEN];
566 struct ctdlroom QRscratch;
569 struct listgroup_range lr;
571 extract_token(verb, cmd, 0, ' ', sizeof verb);
572 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
573 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
574 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
575 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
576 lr.lo = atoi(range_lo);
577 lr.hi = atoi(range_hi);
579 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
580 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
581 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
584 /* First try a regular match */
585 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
586 c = CtdlGetRoom(&QRscratch, requested_room);
588 /* Then try a mailbox name match */
590 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
591 c = CtdlGetRoom(&QRscratch, augmented_roomname);
593 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
597 /* If the room exists, check security/access */
599 /* See if there is an existing user/room relationship */
600 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
602 /* normal clients have to pass through security */
608 /* Fail here if no such room */
610 cprintf("411 no such newsgroup\r\n");
616 * CtdlUserGoto() formally takes us to the desired room, happily returning
617 * the number of messages and number of new messages.
619 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
620 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
621 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
623 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
624 if (!strcasecmp(verb, "GROUP")) {
625 nntpstate->current_article_number = oldest;
629 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
630 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
636 // Implements the MODE command
638 void nntp_mode(const char *cmd) {
642 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
644 if (!strcasecmp(which_mode, "reader")) {
645 cprintf("201 Reader mode FIXME implement posting and change to 200\r\n");
648 cprintf("501 unknown mode\r\n");
654 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
655 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
657 void nntp_article(const char *cmd) {
658 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
660 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
661 char which_command[16];
663 char requested_article[256];
664 long requested_msgnum = 0;
665 char *lb, *rb = NULL;
666 int must_change_currently_selected_article = 0;
668 // We're going to store one of these values in the variable 'acmd' so that
669 // we can quickly check later which version of the output we want.
677 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
679 if (!strcasecmp(which_command, "article")) {
682 else if (!strcasecmp(which_command, "head")) {
685 else if (!strcasecmp(which_command, "body")) {
688 else if (!strcasecmp(which_command, "stat")) {
692 cprintf("500 I'm afraid I can't do that.\r\n");
696 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
697 int headers_only = HEADERS_ALL;
698 if (acmd == HEAD) headers_only = HEADERS_FAST;
699 else if (acmd == BODY) headers_only = HEADERS_NONE;
700 else if (acmd == STAT) headers_only = HEADERS_FAST;
702 // now figure out what the client is asking for.
703 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
704 lb = strchr(requested_article, '<');
705 rb = strchr(requested_article, '>');
706 requested_msgnum = atol(requested_article);
708 // If no article number or message-id is specified, the client wants the "currently selected article"
709 if (IsEmptyStr(requested_article)) {
710 if (nntpstate->current_article_number < 1) {
711 cprintf("420 No current article selected\r\n");
714 requested_msgnum = nntpstate->current_article_number;
715 must_change_currently_selected_article = 1;
716 // got it -- now fall through and keep going
719 // If the requested article is numeric, it maps directly to a message number. Good.
720 else if (requested_msgnum > 0) {
721 must_change_currently_selected_article = 1;
722 // good -- fall through and keep going
725 // If the requested article has angle brackets, the client wants a specific message-id.
726 // We don't know how to do that yet.
727 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
728 must_change_currently_selected_article = 0;
729 cprintf("500 FIXME I don't know how to fetch by message-id yet.\r\n");
733 // Anything else is noncompliant gobbledygook and should die in a car fire.
735 must_change_currently_selected_article = 0;
736 cprintf("500 syntax error\r\n");
740 // At this point we know the message number of the "article" being requested.
741 // We have an awesome API call that does all the heavy lifting for us.
742 char *fetched_message_id = NULL;
743 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
744 int fetch = CtdlOutputMsg(requested_msgnum,
745 MT_RFC822, // output in RFC822 format ... sort of
746 headers_only, // headers, body, or both?
747 0, // don't do Citadel protocol responses
749 NULL, // teh whole thing, not just a section
750 0, // no flags yet ... maybe new ones for Path: etc ?
753 &fetched_message_id // extract the message ID from the message as we go...
755 StrBuf *msgtext = CC->redirect_buffer;
756 CC->redirect_buffer = NULL;
758 if (fetch != om_ok) {
759 cprintf("423 no article with that number\r\n");
760 FreeStrBuf(&msgtext);
764 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
765 // MUST or MUST NOT be set to the message we just referenced.
766 if (must_change_currently_selected_article) {
767 nntpstate->current_article_number = requested_msgnum;
770 // Now give the client what it asked for.
771 if (acmd == ARTICLE) {
772 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
775 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
778 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
781 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
782 FreeStrBuf(&msgtext);
786 client_write(SKEY(msgtext));
787 cprintf(".\r\n"); // this protocol uses a dot terminator
788 FreeStrBuf(&msgtext);
789 if (fetched_message_id) free(fetched_message_id);
794 // The LAST and NEXT commands are so similar that they are handled by a single function.
796 void nntp_last_next(const char *cmd) {
797 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
799 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
800 char which_command[16];
803 // We're going to store one of these values in the variable 'acmd' so that
804 // we can quickly check later which version of the output we want.
810 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
812 if (!strcasecmp(which_command, "last")) {
815 else if (!strcasecmp(which_command, "next")) {
819 cprintf("500 I'm afraid I can't do that.\r\n");
823 cprintf("500 FIXME cmd=%d current_article_number=%ld\r\n", acmd, nntpstate->current_article_number);
828 // Main command loop for NNTP server sessions.
830 void nntp_command_loop(void)
832 StrBuf *Cmd = NewStrBuf();
836 if (CtdlClientGetLine(Cmd) < 1) {
837 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
838 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
842 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
843 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
845 // Rumpelstiltskin lookups are *awesome*
847 if (!strcasecmp(cmdname, "quit")) {
851 else if (!strcasecmp(cmdname, "help")) {
855 else if (!strcasecmp(cmdname, "capabilities")) {
859 else if (!strcasecmp(cmdname, "starttls")) {
863 else if (!strcasecmp(cmdname, "authinfo")) {
864 nntp_authinfo(ChrPtr(Cmd));
867 else if (!strcasecmp(cmdname, "newgroups")) {
868 nntp_newgroups(ChrPtr(Cmd));
871 else if (!strcasecmp(cmdname, "list")) {
872 nntp_list(ChrPtr(Cmd));
875 else if (!strcasecmp(cmdname, "group")) {
876 nntp_group(ChrPtr(Cmd));
879 else if (!strcasecmp(cmdname, "listgroup")) {
880 nntp_group(ChrPtr(Cmd));
883 else if (!strcasecmp(cmdname, "mode")) {
884 nntp_mode(ChrPtr(Cmd));
888 (!strcasecmp(cmdname, "article"))
889 || (!strcasecmp(cmdname, "head"))
890 || (!strcasecmp(cmdname, "body"))
891 || (!strcasecmp(cmdname, "stat"))
894 nntp_article(ChrPtr(Cmd));
898 (!strcasecmp(cmdname, "last"))
899 || (!strcasecmp(cmdname, "next"))
902 nntp_last_next(ChrPtr(Cmd));
906 cprintf("500 I'm afraid I can't do that.\r\n");
913 // ****************************************************************************
914 // MODULE INITIALIZATION STUFF
915 // ****************************************************************************
919 // This cleanup function blows away the temporary memory used by
922 void nntp_cleanup_function(void)
924 /* Don't do this stuff if this is not an NNTP session! */
925 if (CC->h_command_function != nntp_command_loop) return;
927 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
928 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
929 if (nntpstate != NULL) {
935 const char *CitadelServiceNNTP="NNTP";
937 CTDL_MODULE_INIT(nntp)
941 CtdlRegisterServiceHook(119, // FIXME config.c_nntp_port,
949 CtdlRegisterServiceHook(563, // FIXME config.c_nntps_port,
957 CtdlRegisterCleanupHook(nntp_cleanup);
958 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
961 /* return our module name for the log */