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]))
104 * Convert a Citadel room name to a valid newsgroup name
106 void room_to_newsgroup(char *target, char *source, size_t target_size) {
111 if (is_valid_newsgroup_name(source)) {
112 strncpy(target, source, target_size);
116 strcpy(target, "ctdl.");
121 while (ch=*ptr++, ch!=0) {
122 if (len >= target_size) return;
131 target[len++] = '+' ;
132 sprintf(&target[len], "%02x", ch);
141 * Convert a newsgroup name to a Citadel room name.
142 * This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
144 void newsgroup_to_room(char *target, char *source, size_t target_size) {
149 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
150 strncpy(target, source, target_size);
156 char *ptr = &source[5];
159 while (ch=*ptr++, ch!=0) {
160 if (len >= target_size) return;
167 digit = strtol(hex, NULL, 16);
176 /****************** END UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************/
181 * Here's where our NNTP session begins its happy day.
183 void nntp_greeting(void)
185 strcpy(CC->cs_clientname, "NNTP session");
186 CC->cs_flags |= CS_STEALTH;
188 /* CC->session_specific_data = malloc(sizeof(citnntp));
189 memset(NNTP, 0, sizeof(citnntp));
192 if (CC->nologin==1) {
193 cprintf("451 Too many connections are already open; please try again later.\r\n");
194 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
198 /* Note: the FQDN *must* appear as the first thing after the 220 code.
199 * Some clients (including citmail.c) depend on it being there.
201 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", config.c_fqdn);
206 * NNTPS is just like NNTP, except it goes crypto right away.
208 void nntps_greeting(void) {
209 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
211 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
219 * implements the STARTTLS command
221 void nntp_starttls(void)
223 char ok_response[SIZ];
224 char nosup_response[SIZ];
225 char error_response[SIZ];
227 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
228 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
229 sprintf(error_response, "580 Internal error\r\n");
230 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
234 void nntp_capabilities(void)
236 cprintf("101 Capability list:\r\n");
237 cprintf("IMPLEMENTATION Citadel v%d.%02d\r\n", (REV_LEVEL/100), (REV_LEVEL%100));
238 cprintf("VERSION 2\r\n");
239 cprintf("READER\r\n");
240 cprintf("MODE-READER\r\n");
241 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
243 cprintf("STARTTLS\r\n");
245 if (!CC->logged_in) {
246 cprintf("AUTHINFO USER\r\n");
254 cprintf("221 Goodbye...\r\n");
255 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
259 void nntp_cleanup(void)
261 /* nothing here yet */
267 * Implements the AUTHINFO USER command (RFC 4643)
269 void nntp_authinfo_user(const char *username)
271 int a = CtdlLoginExistingUser(NULL, username);
273 case login_already_logged_in:
274 cprintf("482 Already logged in\r\n");
276 case login_too_many_users:
277 cprintf("481 Too many users are already online (maximum is %d)\r\n", config.c_maxsessions);
280 cprintf("381 Password required for %s\r\n", CC->curr_user);
282 case login_not_found:
283 cprintf("481 %s not found\r\n", username);
286 cprintf("502 Internal error\r\n");
292 * Implements the AUTHINFO PASS command (RFC 4643)
294 void nntp_authinfo_pass(const char *buf)
298 a = CtdlTryPassword(buf, strlen(buf));
301 case pass_already_logged_in:
302 cprintf("482 Already logged in\r\n");
305 cprintf("482 Authentication commands issued out of sequence\r\n");
307 case pass_wrong_password:
308 cprintf("481 Authentication failed\r\n");
311 cprintf("281 Authentication accepted\r\n");
319 * Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
321 void nntp_authinfo(const char *cmd) {
323 if (!strncasecmp(cmd, "authinfo user ", 14)) {
324 nntp_authinfo_user(&cmd[14]);
327 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
328 nntp_authinfo_pass(&cmd[14]);
332 cprintf("502 command unavailable\r\n");
338 * Utility function to fetch the current list of message numbers in a room
340 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
341 struct nntp_msglist nm;
342 struct cdbdata *cdbfr;
344 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
346 nm.msgnums = (long*)cdbfr->ptr;
348 nm.num_msgs = cdbfr->len / sizeof(long);
361 * Various output formats for the LIST commands
365 NNTP_LIST_ACTIVE_TIMES,
366 NNTP_LIST_DISTRIB_PATS,
368 NNTP_LIST_NEWSGROUPS,
369 NNTP_LIST_OVERVIEW_FMT
374 * Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
376 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
378 struct nntp_msglist nm;
379 long low_water_mark = 0;
380 long high_water_mark = 0;
382 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
384 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
385 if (!wildmat(n_name, wildmat_pattern)) {
390 nm = nntp_fetch_msglist(qrbuf);
391 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
392 low_water_mark = nm.msgnums[0];
393 high_water_mark = nm.msgnums[nm.num_msgs - 1];
396 // Only the mandatory formats are supported
397 switch(which_format) {
398 case NNTP_LIST_ACTIVE:
399 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
400 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
402 case NNTP_LIST_NEWSGROUPS:
403 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
407 if (nm.msgnums != NULL) {
415 * Called once per room by nntp_newgroups() to qualify and possibly output a single room
417 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
421 time_t thetime = *(time_t *)data;
423 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
426 * The "created after <date/time>" heuristics depend on the happy coincidence
427 * that for a very long time we have used a unix timestamp as the room record's
428 * generation number (QRgen). When this module is merged into the master
429 * source tree we should rename QRgen to QR_create_time or something like that.
433 if (qrbuf->QRgen >= thetime) {
434 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
441 * Implements the NEWGROUPS command
443 void nntp_newgroups(const char *cmd) {
445 * HACK: this works because the 5XX series error codes from citadel
446 * protocol will also be considered error codes by an NNTP client
448 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
451 char stringy_date[16];
452 char stringy_time[16];
453 char stringy_gmt[16];
457 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
458 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
459 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
461 memset(&tm, 0, sizeof tm);
462 if (strlen(stringy_date) == 6) {
463 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
467 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
470 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
471 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
472 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
473 thetime = mktime(&tm);
474 if (!strcasecmp(stringy_gmt, "GMT")) {
480 cprintf("231 list of new newsgroups follows\r\n");
481 CtdlGetUser(&CC->user, CC->curr_user);
482 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
488 * Called once per room by nntp_list() to qualify and possibly output a single room
490 void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
494 struct nntp_list_data *nld = (struct nntp_list_data *)data;
496 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
498 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
504 * Implements the LIST commands
506 void nntp_list(const char *cmd) {
508 * HACK: this works because the 5XX series error codes from citadel
509 * protocol will also be considered error codes by an NNTP client
511 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
513 char list_format[64];
514 char wildmat_pattern[1024];
515 struct nntp_list_data nld;
517 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
518 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
520 if (strlen(wildmat_pattern) > 0) {
521 nld.wildmat_pattern = wildmat_pattern;
524 nld.wildmat_pattern = NULL;
527 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
528 nld.list_format = NNTP_LIST_ACTIVE;
530 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
531 nld.list_format = NNTP_LIST_NEWSGROUPS;
534 cprintf("501 syntax error , unsupported list format\r\n");
538 cprintf("215 list of newsgroups follows\r\n");
539 CtdlGetUser(&CC->user, CC->curr_user);
540 CtdlForEachRoom(nntp_list_backend, &nld);
546 * Implement HELP command.
548 void nntp_help(void) {
549 cprintf("100 This is the Citadel NNTP service.\r\n");
550 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
556 * back end for the LISTGROUP command , called for each message number
558 void nntp_listgroup_backend(long msgnum, void *userdata) {
560 struct listgroup_range *lr = (struct listgroup_range *)userdata;
562 // check range if supplied
563 if (msgnum < lr->lo) return;
564 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
566 cprintf("%ld\r\n", msgnum);
571 * Implements the GROUP and LISTGROUP commands
573 void nntp_group(const char *cmd) {
575 * HACK: this works because the 5XX series error codes from citadel
576 * protocol will also be considered error codes by an NNTP client
578 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
581 char requested_group[1024];
582 char message_range[256];
585 char requested_room[ROOMNAMELEN];
586 char augmented_roomname[ROOMNAMELEN];
590 struct ctdlroom QRscratch;
593 struct listgroup_range lr;
595 extract_token(verb, cmd, 0, ' ', sizeof verb);
596 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
597 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
598 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
599 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
600 lr.lo = atoi(range_lo);
601 lr.hi = atoi(range_hi);
603 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
604 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
605 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
608 /* First try a regular match */
609 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
610 c = CtdlGetRoom(&QRscratch, requested_room);
612 /* Then try a mailbox name match */
614 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
615 c = CtdlGetRoom(&QRscratch, augmented_roomname);
617 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
621 /* If the room exists, check security/access */
623 /* See if there is an existing user/room relationship */
624 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
626 /* normal clients have to pass through security */
632 /* Fail here if no such room */
634 cprintf("411 no such newsgroup\r\n");
640 * CtdlUserGoto() formally takes us to the desired room, happily returning
641 * the number of messages and number of new messages.
643 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
644 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
645 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
647 // If this is a GROUP command, we can stop here.
648 if (!strcasecmp(verb, "GROUP")) {
652 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
653 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
659 * Implements the MODE command
661 void nntp_mode(const char *cmd) {
665 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
667 if (!strcasecmp(which_mode, "reader")) {
668 cprintf("201 Reader mode FIXME implement posting and change to 200\r\n");
671 cprintf("501 unknown mode\r\n");
678 * Implements the ARTICLE, HEAD, BODY, and STAT commands.
679 * (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
681 void nntp_article(const char *cmd) {
690 char which_command[16];
692 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
694 if (!strcasecmp(which_command, "article")) {
697 else if (!strcasecmp(which_command, "head")) {
700 else if (!strcasecmp(which_command, "body")) {
703 else if (!strcasecmp(which_command, "stat")) {
707 cprintf("500 I'm afraid I can't do that.\r\n");
711 // now figure out what the client is asking for.
712 char requested_article[256];
713 long requested_msgnum = 0;
714 char *lb, *rb = NULL;
715 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
716 lb = strchr(requested_article, '<');
717 rb = strchr(requested_article, '>');
718 requested_msgnum = atol(requested_article);
720 // If no article number or message-id is specified, the client wants the "next" article.
721 // We don't know how to do that yet.
722 if (IsEmptyStr(requested_article)) {
723 cprintf("500 FIXME I don't know how to fetch next yet.\r\n");
727 // If the requested article is numeric, it maps directly to a message number. Good.
728 else if (requested_msgnum > 0) {
729 // good -- fall through and keep going
732 // If the requested article has angle brackets, the client wants a specific message-id.
733 // We don't know how to do that yet.
734 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
735 cprintf("500 FIXME I don't know how to fetch by message-id yet.\r\n");
739 // Anything else is noncompliant gobbledygook and should die in a car fire.
741 cprintf("500 syntax error\r\n");
748 cprintf("500 FIXME write the rest of cmd=%d msgnum=%ld\r\n", acmd, requested_msgnum);
753 * Main command loop for NNTP server sessions.
755 void nntp_command_loop(void)
757 StrBuf *Cmd = NewStrBuf();
761 if (CtdlClientGetLine(Cmd) < 1) {
762 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
763 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
767 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
768 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
771 * Rumpelstiltskin lookups are awesome
774 if (!strcasecmp(cmdname, "quit")) {
778 else if (!strcasecmp(cmdname, "help")) {
782 else if (!strcasecmp(cmdname, "capabilities")) {
786 else if (!strcasecmp(cmdname, "starttls")) {
790 else if (!strcasecmp(cmdname, "authinfo")) {
791 nntp_authinfo(ChrPtr(Cmd));
794 else if (!strcasecmp(cmdname, "newgroups")) {
795 nntp_newgroups(ChrPtr(Cmd));
798 else if (!strcasecmp(cmdname, "list")) {
799 nntp_list(ChrPtr(Cmd));
802 else if (!strcasecmp(cmdname, "group")) {
803 nntp_group(ChrPtr(Cmd));
806 else if (!strcasecmp(cmdname, "listgroup")) {
807 nntp_group(ChrPtr(Cmd));
810 else if (!strcasecmp(cmdname, "mode")) {
811 nntp_mode(ChrPtr(Cmd));
815 (!strcasecmp(cmdname, "article"))
816 || (!strcasecmp(cmdname, "head"))
817 || (!strcasecmp(cmdname, "body"))
818 || (!strcasecmp(cmdname, "stat"))
820 nntp_article(ChrPtr(Cmd));
824 cprintf("500 I'm afraid I can't do that.\r\n");
831 /*****************************************************************************/
832 /* MODULE INITIALIZATION STUFF */
833 /*****************************************************************************/
837 * This cleanup function blows away the temporary memory used by
840 void nntp_cleanup_function(void)
842 /* Don't do this stuff if this is not an NNTP session! */
843 if (CC->h_command_function != nntp_command_loop) return;
845 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
848 const char *CitadelServiceNNTP="NNTP";
850 CTDL_MODULE_INIT(nntp)
854 CtdlRegisterServiceHook(119, // FIXME config.c_nntp_port,
862 CtdlRegisterServiceHook(563, // FIXME config.c_nntps_port,
870 CtdlRegisterCleanupHook(nntp_cleanup);
871 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
874 /* return our module name for the log */