2 // NNTP server module (RFC 3977)
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) {
80 if (!strncasecmp(name, "ctdl.", 5)) return(0);
84 if (isalpha(ptr[0])) {
92 if ( (isalnum(ptr[0]))
103 return( (has_a_letter) && (num_dots >= 1) ) ;
108 // Convert a Citadel room name to a valid newsgroup name
110 void room_to_newsgroup(char *target, char *source, size_t target_size) {
115 if (is_valid_newsgroup_name(source)) {
116 strncpy(target, source, target_size);
120 strcpy(target, "ctdl.");
125 while (ch=*ptr++, ch!=0) {
126 if (len >= target_size) return;
131 target[len++] = tolower(ch);
135 target[len++] = '+' ;
136 sprintf(&target[len], "%02x", ch);
145 // Convert a newsgroup name to a Citadel room name.
146 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
148 void newsgroup_to_room(char *target, char *source, size_t target_size) {
153 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
154 strncpy(target, source, target_size);
160 char *ptr = &source[5];
163 while (ch=*ptr++, ch!=0) {
164 if (len >= target_size) return;
171 digit = strtol(hex, NULL, 16);
180 // ***************** END UTILITY FUNCTIONS THAT COULD BE MOVED ELSEWHERE LATER **************
185 // Here's where our NNTP session begins its happy day.
187 void nntp_greeting(void)
189 strcpy(CC->cs_clientname, "NNTP session");
190 CC->cs_flags |= CS_STEALTH;
192 CC->session_specific_data = malloc(sizeof(citnntp));
193 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
194 memset(nntpstate, 0, sizeof(citnntp));
196 if (CC->nologin==1) {
197 cprintf("451 Too many connections are already open; please try again later.\r\n");
198 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
202 // Display the standard greeting
203 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", config.c_fqdn);
208 // NNTPS is just like NNTP, except it goes crypto right away.
210 void nntps_greeting(void) {
211 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
213 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
220 // implements the STARTTLS command
222 void nntp_starttls(void)
224 char ok_response[SIZ];
225 char nosup_response[SIZ];
226 char error_response[SIZ];
228 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
229 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
230 sprintf(error_response, "580 Internal error\r\n");
231 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
236 // Implements the CAPABILITY command
238 void nntp_capabilities(void)
240 cprintf("101 Capability list:\r\n");
241 cprintf("IMPLEMENTATION Citadel v%d.%02d\r\n", (REV_LEVEL/100), (REV_LEVEL%100));
242 cprintf("VERSION 2\r\n");
243 cprintf("READER\r\n");
244 cprintf("MODE-READER\r\n");
245 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
248 cprintf("STARTTLS\r\n");
250 if (!CC->logged_in) {
251 cprintf("AUTHINFO USER\r\n");
258 // Implements the QUIT command
262 cprintf("221 Goodbye...\r\n");
263 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
268 // Cleanup hook for this module
270 void nntp_cleanup(void)
272 /* nothing here yet */
277 // Implements the AUTHINFO USER command (RFC 4643)
279 void nntp_authinfo_user(const char *username)
281 int a = CtdlLoginExistingUser(NULL, username);
283 case login_already_logged_in:
284 cprintf("482 Already logged in\r\n");
286 case login_too_many_users:
287 cprintf("481 Too many users are already online (maximum is %d)\r\n", config.c_maxsessions);
290 cprintf("381 Password required for %s\r\n", CC->curr_user);
292 case login_not_found:
293 cprintf("481 %s not found\r\n", username);
296 cprintf("502 Internal error\r\n");
302 // Implements the AUTHINFO PASS command (RFC 4643)
304 void nntp_authinfo_pass(const char *buf)
308 a = CtdlTryPassword(buf, strlen(buf));
311 case pass_already_logged_in:
312 cprintf("482 Already logged in\r\n");
315 cprintf("482 Authentication commands issued out of sequence\r\n");
317 case pass_wrong_password:
318 cprintf("481 Authentication failed\r\n");
321 cprintf("281 Authentication accepted\r\n");
328 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
330 void nntp_authinfo(const char *cmd) {
332 if (!strncasecmp(cmd, "authinfo user ", 14)) {
333 nntp_authinfo_user(&cmd[14]);
336 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
337 nntp_authinfo_pass(&cmd[14]);
341 cprintf("502 command unavailable\r\n");
347 // Utility function to fetch the current list of message numbers in a room
349 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
350 struct nntp_msglist nm;
351 struct cdbdata *cdbfr;
353 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
355 nm.msgnums = (long*)cdbfr->ptr;
357 nm.num_msgs = cdbfr->len / sizeof(long);
369 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
371 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
373 struct nntp_msglist nm;
374 long low_water_mark = 0;
375 long high_water_mark = 0;
377 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
379 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
380 if (!wildmat(n_name, wildmat_pattern)) {
385 nm = nntp_fetch_msglist(qrbuf);
386 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
387 low_water_mark = nm.msgnums[0];
388 high_water_mark = nm.msgnums[nm.num_msgs - 1];
391 // Only the mandatory formats are supported
392 switch(which_format) {
393 case NNTP_LIST_ACTIVE:
394 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
395 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
397 case NNTP_LIST_NEWSGROUPS:
398 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
402 if (nm.msgnums != NULL) {
409 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
411 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
415 time_t thetime = *(time_t *)data;
417 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
420 * The "created after <date/time>" heuristics depend on the happy coincidence
421 * that for a very long time we have used a unix timestamp as the room record's
422 * generation number (QRgen). When this module is merged into the master
423 * source tree we should rename QRgen to QR_create_time or something like that.
427 if (qrbuf->QRgen >= thetime) {
428 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
435 // Implements the NEWGROUPS command
437 void nntp_newgroups(const char *cmd) {
438 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
440 char stringy_date[16];
441 char stringy_time[16];
442 char stringy_gmt[16];
446 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
447 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
448 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
450 memset(&tm, 0, sizeof tm);
451 if (strlen(stringy_date) == 6) {
452 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
456 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
459 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
460 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
461 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
462 thetime = mktime(&tm);
463 if (!strcasecmp(stringy_gmt, "GMT")) {
469 cprintf("231 list of new newsgroups follows\r\n");
470 CtdlGetUser(&CC->user, CC->curr_user);
471 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
477 // Called once per room by nntp_list() to qualify and possibly output a single room
479 void nntp_list_backend(struct ctdlroom *qrbuf, void *data)
483 struct nntp_list_data *nld = (struct nntp_list_data *)data;
485 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
487 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
493 // Implements the LIST commands
495 void nntp_list(const char *cmd) {
496 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
498 char list_format[64];
499 char wildmat_pattern[1024];
500 struct nntp_list_data nld;
502 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
503 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
505 if (strlen(wildmat_pattern) > 0) {
506 nld.wildmat_pattern = wildmat_pattern;
509 nld.wildmat_pattern = NULL;
512 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
513 nld.list_format = NNTP_LIST_ACTIVE;
515 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
516 nld.list_format = NNTP_LIST_NEWSGROUPS;
518 else if (!strcasecmp(list_format, "OVERVIEW.FMT")) {
519 nld.list_format = NNTP_LIST_OVERVIEW_FMT;
522 cprintf("501 syntax error , unsupported list format\r\n");
526 // OVERVIEW.FMT delivers a completely different type of data than all of the
527 // other LIST commands. It's a stupid place to put it. But that's how it's
528 // written into RFC3977, so we have to handle it here.
529 if (nld.list_format == NNTP_LIST_OVERVIEW_FMT) {
530 cprintf("215 Order of fields in overview database.\r\n");
531 cprintf("Subject:\r\n");
532 cprintf("From:\r\n");
533 cprintf("Date:\r\n");
534 cprintf("Message-ID:\r\n");
535 cprintf("References:\r\n");
536 cprintf("Bytes:\r\n");
537 cprintf("Lines:\r\n");
542 cprintf("215 list of newsgroups follows\r\n");
543 CtdlGetUser(&CC->user, CC->curr_user);
544 CtdlForEachRoom(nntp_list_backend, &nld);
550 // Implement HELP command.
552 void nntp_help(void) {
553 cprintf("100 This is the Citadel NNTP service.\r\n");
554 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
560 // Implement DATE command.
562 void nntp_date(void) {
569 localtime_r(&now, &nowLocal);
570 gmtime_r(&now, &nowUtc);
572 strftime(tsFromUtc, sizeof(tsFromUtc), "%Y%m%d%H%M%S", &nowUtc);
574 cprintf("111 %s\r\n", tsFromUtc);
579 // back end for the LISTGROUP command , called for each message number
581 void nntp_listgroup_backend(long msgnum, void *userdata) {
583 struct listgroup_range *lr = (struct listgroup_range *)userdata;
585 // check range if supplied
586 if (msgnum < lr->lo) return;
587 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
589 cprintf("%ld\r\n", msgnum);
594 // Implements the GROUP and LISTGROUP commands
596 void nntp_group(const char *cmd) {
597 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
599 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
601 char requested_group[1024];
602 char message_range[256];
605 char requested_room[ROOMNAMELEN];
606 char augmented_roomname[ROOMNAMELEN];
610 struct ctdlroom QRscratch;
613 struct listgroup_range lr;
615 extract_token(verb, cmd, 0, ' ', sizeof verb);
616 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
617 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
618 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
619 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
620 lr.lo = atoi(range_lo);
621 lr.hi = atoi(range_hi);
623 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
624 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
625 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
628 /* First try a regular match */
629 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
630 c = CtdlGetRoom(&QRscratch, requested_room);
632 /* Then try a mailbox name match */
634 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
635 c = CtdlGetRoom(&QRscratch, augmented_roomname);
637 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
641 /* If the room exists, check security/access */
643 /* See if there is an existing user/room relationship */
644 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
646 /* normal clients have to pass through security */
652 /* Fail here if no such room */
654 cprintf("411 no such newsgroup\r\n");
660 * CtdlUserGoto() formally takes us to the desired room, happily returning
661 * the number of messages and number of new messages.
663 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
664 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
665 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
667 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
668 if (!strcasecmp(verb, "GROUP")) {
669 nntpstate->current_article_number = oldest;
673 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
674 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
680 // Implements the MODE command
682 void nntp_mode(const char *cmd) {
686 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
688 if (!strcasecmp(which_mode, "reader")) {
689 // FIXME implement posting and change to 200
690 cprintf("201 Reader mode activated\r\n");
693 cprintf("501 unknown mode\r\n");
699 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
700 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
702 void nntp_article(const char *cmd) {
703 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
705 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
706 char which_command[16];
708 char requested_article[256];
709 long requested_msgnum = 0;
710 char *lb, *rb = NULL;
711 int must_change_currently_selected_article = 0;
713 // We're going to store one of these values in the variable 'acmd' so that
714 // we can quickly check later which version of the output we want.
722 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
724 if (!strcasecmp(which_command, "article")) {
727 else if (!strcasecmp(which_command, "head")) {
730 else if (!strcasecmp(which_command, "body")) {
733 else if (!strcasecmp(which_command, "stat")) {
737 cprintf("500 I'm afraid I can't do that.\r\n");
741 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
742 int headers_only = HEADERS_ALL;
743 if (acmd == HEAD) headers_only = HEADERS_FAST;
744 else if (acmd == BODY) headers_only = HEADERS_NONE;
745 else if (acmd == STAT) headers_only = HEADERS_FAST;
747 // now figure out what the client is asking for.
748 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
749 lb = strchr(requested_article, '<');
750 rb = strchr(requested_article, '>');
751 requested_msgnum = atol(requested_article);
753 // If no article number or message-id is specified, the client wants the "currently selected article"
754 if (IsEmptyStr(requested_article)) {
755 if (nntpstate->current_article_number < 1) {
756 cprintf("420 No current article selected\r\n");
759 requested_msgnum = nntpstate->current_article_number;
760 must_change_currently_selected_article = 1;
761 // got it -- now fall through and keep going
764 // If the requested article is numeric, it maps directly to a message number. Good.
765 else if (requested_msgnum > 0) {
766 must_change_currently_selected_article = 1;
767 // good -- fall through and keep going
770 // If the requested article has angle brackets, the client wants a specific message-id.
771 // We don't know how to do that yet.
772 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
773 must_change_currently_selected_article = 0;
774 cprintf("500 I don't know how to fetch by message-id yet.\r\n"); // FIXME
778 // Anything else is noncompliant gobbledygook and should die in a car fire.
780 must_change_currently_selected_article = 0;
781 cprintf("500 syntax error\r\n");
785 // At this point we know the message number of the "article" being requested.
786 // We have an awesome API call that does all the heavy lifting for us.
787 char *fetched_message_id = NULL;
788 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
789 int fetch = CtdlOutputMsg(requested_msgnum,
790 MT_RFC822, // output in RFC822 format ... sort of
791 headers_only, // headers, body, or both?
792 0, // don't do Citadel protocol responses
794 NULL, // teh whole thing, not just a section
795 0, // no flags yet ... maybe new ones for Path: etc ?
798 &fetched_message_id // extract the message ID from the message as we go...
800 StrBuf *msgtext = CC->redirect_buffer;
801 CC->redirect_buffer = NULL;
803 if (fetch != om_ok) {
804 cprintf("423 no article with that number\r\n");
805 FreeStrBuf(&msgtext);
809 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
810 // MUST or MUST NOT be set to the message we just referenced.
811 if (must_change_currently_selected_article) {
812 nntpstate->current_article_number = requested_msgnum;
815 // Now give the client what it asked for.
816 if (acmd == ARTICLE) {
817 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
820 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
823 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
826 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
827 FreeStrBuf(&msgtext);
831 client_write(SKEY(msgtext));
832 cprintf(".\r\n"); // this protocol uses a dot terminator
833 FreeStrBuf(&msgtext);
834 if (fetched_message_id) free(fetched_message_id);
839 // Utility function for nntp_last_next() that turns a msgnum into a message ID.
840 // The memory for the returned string is pwnz0red by the caller.
842 char *message_id_from_msgnum(long msgnum) {
844 char *fetched_message_id = NULL;
845 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
846 CtdlOutputMsg(msgnum,
847 MT_RFC822, // output in RFC822 format ... sort of
848 HEADERS_FAST, // headers, body, or both?
849 0, // don't do Citadel protocol responses
851 NULL, // teh whole thing, not just a section
852 0, // no flags yet ... maybe new ones for Path: etc ?
855 &fetched_message_id // extract the message ID from the message as we go...
857 StrBuf *msgtext = CC->redirect_buffer;
858 CC->redirect_buffer = NULL;
860 FreeStrBuf(&msgtext);
861 return(fetched_message_id);
866 // The LAST and NEXT commands are so similar that they are handled by a single function.
868 void nntp_last_next(const char *cmd) {
869 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
871 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
872 char which_command[16];
875 // We're going to store one of these values in the variable 'acmd' so that
876 // we can quickly check later which version of the output we want.
882 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
884 if (!strcasecmp(which_command, "last")) {
887 else if (!strcasecmp(which_command, "next")) {
891 cprintf("500 I'm afraid I can't do that.\r\n");
895 // ok, here we go ... fetch the msglist so we can figure out our place in the universe
896 struct nntp_msglist nm;
898 long selected_msgnum = 0;
899 char *message_id = NULL;
901 nm = nntp_fetch_msglist(&CC->room);
902 if ((nm.num_msgs < 0) || (nm.msgnums == NULL)) {
903 cprintf("500 something bad happened\r\n");
907 if ( (acmd == NNTP_LAST) && (nm.num_msgs == 0) ) {
908 cprintf("422 no previous article in this group\r\n"); // nothing here
911 else if ( (acmd == NNTP_LAST) && (nntpstate->current_article_number <= nm.msgnums[0]) ) {
912 cprintf("422 no previous article in this group\r\n"); // already at the beginning
915 else if (acmd == NNTP_LAST) {
916 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
917 if ( (nm.msgnums[i] >= nntpstate->current_article_number) && (i > 0) ) {
918 selected_msgnum = nm.msgnums[i-1];
921 if (selected_msgnum > 0) {
922 nntpstate->current_article_number = selected_msgnum;
923 message_id = message_id_from_msgnum(nntpstate->current_article_number);
924 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
925 if (message_id) free(message_id);
928 cprintf("422 no previous article in this group\r\n");
932 else if ( (acmd == NNTP_NEXT) && (nm.num_msgs == 0) ) {
933 cprintf("421 no next article in this group\r\n"); // nothing here
936 else if ( (acmd == NNTP_NEXT) && (nntpstate->current_article_number >= nm.msgnums[nm.num_msgs-1]) ) {
937 cprintf("421 no next article in this group\r\n"); // already at the end
940 else if (acmd == NNTP_NEXT) {
941 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
942 if (nm.msgnums[i] > nntpstate->current_article_number) {
943 selected_msgnum = nm.msgnums[i];
946 if (selected_msgnum > 0) {
947 nntpstate->current_article_number = selected_msgnum;
948 message_id = message_id_from_msgnum(nntpstate->current_article_number);
949 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
950 if (message_id) free(message_id);
953 cprintf("421 no next article in this group\r\n");
957 // should never get here
959 cprintf("500 internal error\r\n");
962 if (nm.msgnums != NULL) {
970 // back end for the XOVER command , called for each message number
972 void nntp_xover_backend(long msgnum, void *userdata) {
974 struct listgroup_range *lr = (struct listgroup_range *)userdata;
976 // check range if supplied
977 if (msgnum < lr->lo) return;
978 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
980 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 0);
985 // Teh RFC says we need:
986 // -------------------------
987 // Subject header content
988 // From header content
989 // Date header content
990 // Message-ID header content
991 // References header content
992 // :bytes metadata item
993 // :lines metadata item
995 time_t msgtime = atol(msg->cm_fields[eTimestamp]);
997 ctime_r(&msgtime, strtimebuf);
999 // here we go -- print the line o'data
1000 cprintf("%ld\t%s\t%s <%s>\t%s\t%s\t%s\t100\t10\r\n",
1002 msg->cm_fields[eMsgSubject],
1003 msg->cm_fields[eAuthor],
1004 msg->cm_fields[erFc822Addr],
1006 msg->cm_fields[emessageId],
1007 msg->cm_fields[eWeferences]
1016 // XOVER is used by some clients, even if we don't offer it
1018 void nntp_xover(const char *cmd) {
1019 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
1021 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1023 struct listgroup_range lr;
1025 extract_token(range, cmd, 1, ' ', sizeof range);
1026 lr.lo = atol(range);
1028 lr.lo = nntpstate->current_article_number;
1029 lr.hi = nntpstate->current_article_number;
1032 char *dash = strchr(range, '-');
1039 if (lr.hi < lr.lo) {
1048 cprintf("224 Overview information follows\r\n");
1049 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_xover_backend, &lr);
1055 // Main command loop for NNTP server sessions.
1057 void nntp_command_loop(void)
1059 StrBuf *Cmd = NewStrBuf();
1063 if (CtdlClientGetLine(Cmd) < 1) {
1064 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
1065 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
1069 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
1070 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
1072 // Rumpelstiltskin lookups are *awesome*
1074 if (!strcasecmp(cmdname, "quit")) {
1078 else if (!strcasecmp(cmdname, "help")) {
1082 else if (!strcasecmp(cmdname, "date")) {
1086 else if (!strcasecmp(cmdname, "capabilities")) {
1087 nntp_capabilities();
1090 else if (!strcasecmp(cmdname, "starttls")) {
1094 else if (!strcasecmp(cmdname, "authinfo")) {
1095 nntp_authinfo(ChrPtr(Cmd));
1098 else if (!strcasecmp(cmdname, "newgroups")) {
1099 nntp_newgroups(ChrPtr(Cmd));
1102 else if (!strcasecmp(cmdname, "list")) {
1103 nntp_list(ChrPtr(Cmd));
1106 else if (!strcasecmp(cmdname, "group")) {
1107 nntp_group(ChrPtr(Cmd));
1110 else if (!strcasecmp(cmdname, "listgroup")) {
1111 nntp_group(ChrPtr(Cmd));
1114 else if (!strcasecmp(cmdname, "mode")) {
1115 nntp_mode(ChrPtr(Cmd));
1119 (!strcasecmp(cmdname, "article"))
1120 || (!strcasecmp(cmdname, "head"))
1121 || (!strcasecmp(cmdname, "body"))
1122 || (!strcasecmp(cmdname, "stat"))
1125 nntp_article(ChrPtr(Cmd));
1129 (!strcasecmp(cmdname, "last"))
1130 || (!strcasecmp(cmdname, "next"))
1133 nntp_last_next(ChrPtr(Cmd));
1137 (!strcasecmp(cmdname, "xover"))
1138 || (!strcasecmp(cmdname, "over"))
1141 nntp_xover(ChrPtr(Cmd));
1145 cprintf("500 I'm afraid I can't do that.\r\n");
1152 // ****************************************************************************
1153 // MODULE INITIALIZATION STUFF
1154 // ****************************************************************************
1158 // This cleanup function blows away the temporary memory used by
1161 void nntp_cleanup_function(void)
1163 /* Don't do this stuff if this is not an NNTP session! */
1164 if (CC->h_command_function != nntp_command_loop) return;
1166 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
1167 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1168 if (nntpstate != NULL) {
1174 const char *CitadelServiceNNTP="NNTP";
1175 const char *CitadelServiceNNTPS="NNTPS";
1177 CTDL_MODULE_INIT(nntp)
1181 CtdlRegisterServiceHook(config.c_nntp_port,
1186 CitadelServiceNNTP);
1189 CtdlRegisterServiceHook(config.c_nntps_port,
1194 CitadelServiceNNTPS);
1197 CtdlRegisterCleanupHook(nntp_cleanup);
1198 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1201 /* return our module name for the log */