2 // NNTP server module (RFC 3977)
4 // Copyright (c) 2014-2020 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>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <libcitadel.h>
37 #include "citserver.h"
45 #include "internet_addressing.h"
48 #include "clientsocket.h"
49 #include "locate_host.h"
50 #include "citadel_dirs.h"
51 #include "ctdl_module.h"
52 #include "serv_nntp.h"
57 // Tests whether the supplied string is a valid newsgroup name
58 // Returns true (nonzero) or false (0)
60 int is_valid_newsgroup_name(char *name) {
66 if (!strncasecmp(name, "ctdl.", 5)) return(0);
70 if (isalpha(ptr[0])) {
78 if ( (isalnum(ptr[0]))
89 return( (has_a_letter) && (num_dots >= 1) ) ;
94 // Convert a Citadel room name to a valid newsgroup name
96 void room_to_newsgroup(char *target, char *source, size_t target_size) {
101 if (is_valid_newsgroup_name(source)) {
102 strncpy(target, source, target_size);
106 strcpy(target, "ctdl.");
111 while (ch=*ptr++, ch!=0) {
112 if (len >= target_size) return;
117 target[len++] = tolower(ch);
121 target[len++] = '+' ;
122 sprintf(&target[len], "%02x", ch);
131 // Convert a newsgroup name to a Citadel room name.
132 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
134 void newsgroup_to_room(char *target, char *source, size_t target_size) {
139 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
140 strncpy(target, source, target_size);
146 char *ptr = &source[5];
149 while (ch=*ptr++, ch!=0) {
150 if (len >= target_size) return;
157 digit = strtol(hex, NULL, 16);
167 // Here's where our NNTP session begins its happy day.
169 void nntp_greeting(void) {
170 strcpy(CC->cs_clientname, "NNTP session");
171 CC->cs_flags |= CS_STEALTH;
173 CC->session_specific_data = malloc(sizeof(citnntp));
174 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
175 memset(nntpstate, 0, sizeof(citnntp));
177 if (CC->nologin==1) {
178 cprintf("451 Too many connections are already open; please try again later.\r\n");
179 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
183 // Display the standard greeting
184 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", CtdlGetConfigStr("c_fqdn"));
189 // NNTPS is just like NNTP, except it goes crypto right away.
191 void nntps_greeting(void) {
192 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
194 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; /* kill session if no crypto */
201 // implements the STARTTLS command
203 void nntp_starttls(void) {
204 char ok_response[SIZ];
205 char nosup_response[SIZ];
206 char error_response[SIZ];
208 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
209 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
210 sprintf(error_response, "580 Internal error\r\n");
211 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
216 // Implements the CAPABILITY command
218 void nntp_capabilities(void) {
219 cprintf("101 Capability list:\r\n");
220 cprintf("IMPLEMENTATION Citadel %d\r\n", REV_LEVEL);
221 cprintf("VERSION 2\r\n");
222 cprintf("READER\r\n");
223 cprintf("MODE-READER\r\n");
224 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
227 cprintf("STARTTLS\r\n");
229 if (!CC->logged_in) {
230 cprintf("AUTHINFO USER\r\n");
237 // Implements the QUIT command
239 void nntp_quit(void) {
240 cprintf("221 Goodbye...\r\n");
241 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
246 // Implements the AUTHINFO USER command (RFC 4643)
248 void nntp_authinfo_user(const char *username) {
249 int a = CtdlLoginExistingUser(username);
251 case login_already_logged_in:
252 cprintf("482 Already logged in\r\n");
254 case login_too_many_users:
255 cprintf("481 Too many users are already online (maximum is %d)\r\n", CtdlGetConfigInt("c_maxsessions"));
258 cprintf("381 Password required for %s\r\n", CC->curr_user);
260 case login_not_found:
261 cprintf("481 %s not found\r\n", username);
264 cprintf("502 Internal error\r\n");
270 // Implements the AUTHINFO PASS command (RFC 4643)
272 void nntp_authinfo_pass(const char *buf) {
275 a = CtdlTryPassword(buf, strlen(buf));
278 case pass_already_logged_in:
279 cprintf("482 Already logged in\r\n");
282 cprintf("482 Authentication commands issued out of sequence\r\n");
284 case pass_wrong_password:
285 cprintf("481 Authentication failed\r\n");
288 cprintf("281 Authentication accepted\r\n");
295 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
297 void nntp_authinfo(const char *cmd) {
299 if (!strncasecmp(cmd, "authinfo user ", 14)) {
300 nntp_authinfo_user(&cmd[14]);
303 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
304 nntp_authinfo_pass(&cmd[14]);
308 cprintf("502 command unavailable\r\n");
314 // Utility function to fetch the current list of message numbers in a room
316 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
317 struct nntp_msglist nm;
318 struct cdbdata *cdbfr;
320 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
322 nm.msgnums = (long*)cdbfr->ptr;
324 nm.num_msgs = cdbfr->len / sizeof(long);
336 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
338 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
340 struct nntp_msglist nm;
341 long low_water_mark = 0;
342 long high_water_mark = 0;
344 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
346 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
347 if (!wildmat(n_name, wildmat_pattern)) {
352 nm = nntp_fetch_msglist(qrbuf);
353 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
354 low_water_mark = nm.msgnums[0];
355 high_water_mark = nm.msgnums[nm.num_msgs - 1];
358 // Only the mandatory formats are supported
359 switch(which_format) {
360 case NNTP_LIST_ACTIVE:
361 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
362 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
364 case NNTP_LIST_NEWSGROUPS:
365 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
369 if (nm.msgnums != NULL) {
376 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
378 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data) {
381 time_t thetime = *(time_t *)data;
383 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
386 * The "created after <date/time>" heuristics depend on the happy coincidence
387 * that for a very long time we have used a unix timestamp as the room record's
388 * generation number (QRgen). When this module is merged into the master
389 * source tree we should rename QRgen to QR_create_time or something like that.
393 if (qrbuf->QRgen >= thetime) {
394 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
401 // Implements the NEWGROUPS command
403 void nntp_newgroups(const char *cmd) {
404 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
406 char stringy_date[16];
407 char stringy_time[16];
408 char stringy_gmt[16];
412 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
413 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
414 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
416 memset(&tm, 0, sizeof tm);
417 if (strlen(stringy_date) == 6) {
418 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
422 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
425 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
426 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
427 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
428 thetime = mktime(&tm);
429 if (!strcasecmp(stringy_gmt, "GMT")) {
435 cprintf("231 list of new newsgroups follows\r\n");
436 CtdlGetUser(&CC->user, CC->curr_user);
437 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
443 // Called once per room by nntp_list() to qualify and possibly output a single room
445 void nntp_list_backend(struct ctdlroom *qrbuf, void *data) {
448 struct nntp_list_data *nld = (struct nntp_list_data *)data;
450 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
452 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
458 // Implements the LIST commands
460 void nntp_list(const char *cmd) {
461 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
463 char list_format[64];
464 char wildmat_pattern[1024];
465 struct nntp_list_data nld;
467 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
468 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
470 if (strlen(wildmat_pattern) > 0) {
471 nld.wildmat_pattern = wildmat_pattern;
474 nld.wildmat_pattern = NULL;
477 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
478 nld.list_format = NNTP_LIST_ACTIVE;
480 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
481 nld.list_format = NNTP_LIST_NEWSGROUPS;
483 else if (!strcasecmp(list_format, "OVERVIEW.FMT")) {
484 nld.list_format = NNTP_LIST_OVERVIEW_FMT;
487 cprintf("501 syntax error , unsupported list format\r\n");
491 // OVERVIEW.FMT delivers a completely different type of data than all of the
492 // other LIST commands. It's a stupid place to put it. But that's how it's
493 // written into RFC3977, so we have to handle it here.
494 if (nld.list_format == NNTP_LIST_OVERVIEW_FMT) {
495 cprintf("215 Order of fields in overview database.\r\n");
496 cprintf("Subject:\r\n");
497 cprintf("From:\r\n");
498 cprintf("Date:\r\n");
499 cprintf("Message-ID:\r\n");
500 cprintf("References:\r\n");
501 cprintf("Bytes:\r\n");
502 cprintf("Lines:\r\n");
507 cprintf("215 list of newsgroups follows\r\n");
508 CtdlGetUser(&CC->user, CC->curr_user);
509 CtdlForEachRoom(nntp_list_backend, &nld);
515 // Implement HELP command.
517 void nntp_help(void) {
518 cprintf("100 This is the Citadel NNTP service.\r\n");
519 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
525 // Implement DATE command.
527 void nntp_date(void) {
534 localtime_r(&now, &nowLocal);
535 gmtime_r(&now, &nowUtc);
537 strftime(tsFromUtc, sizeof(tsFromUtc), "%Y%m%d%H%M%S", &nowUtc);
539 cprintf("111 %s\r\n", tsFromUtc);
544 // back end for the LISTGROUP command , called for each message number
546 void nntp_listgroup_backend(long msgnum, void *userdata) {
548 struct listgroup_range *lr = (struct listgroup_range *)userdata;
550 // check range if supplied
551 if (msgnum < lr->lo) return;
552 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
554 cprintf("%ld\r\n", msgnum);
559 // Implements the GROUP and LISTGROUP commands
561 void nntp_group(const char *cmd) {
562 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
564 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
566 char requested_group[1024];
567 char message_range[256];
570 char requested_room[ROOMNAMELEN];
571 char augmented_roomname[ROOMNAMELEN];
575 struct ctdlroom QRscratch;
578 struct listgroup_range lr;
580 extract_token(verb, cmd, 0, ' ', sizeof verb);
581 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
582 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
583 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
584 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
585 lr.lo = atoi(range_lo);
586 lr.hi = atoi(range_hi);
588 /* In LISTGROUP mode we can specify an empty name for 'currently selected' */
589 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
590 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
593 /* First try a regular match */
594 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
595 c = CtdlGetRoom(&QRscratch, requested_room);
597 /* Then try a mailbox name match */
599 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
600 c = CtdlGetRoom(&QRscratch, augmented_roomname);
602 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
606 /* If the room exists, check security/access */
608 /* See if there is an existing user/room relationship */
609 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
611 /* normal clients have to pass through security */
617 /* Fail here if no such room */
619 cprintf("411 no such newsgroup\r\n");
625 * CtdlUserGoto() formally takes us to the desired room, happily returning
626 * the number of messages and number of new messages.
628 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
629 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
630 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
632 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
633 if (!strcasecmp(verb, "GROUP")) {
634 nntpstate->current_article_number = oldest;
638 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
639 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
645 // Implements the MODE command
647 void nntp_mode(const char *cmd) {
651 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
653 if (!strcasecmp(which_mode, "reader")) {
654 // FIXME implement posting and change to 200
655 cprintf("201 Reader mode activated\r\n");
658 cprintf("501 unknown mode\r\n");
664 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
665 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
667 void nntp_article(const char *cmd) {
668 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
670 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
671 char which_command[16];
673 char requested_article[256];
674 long requested_msgnum = 0;
675 char *lb, *rb = NULL;
676 int must_change_currently_selected_article = 0;
678 // We're going to store one of these values in the variable 'acmd' so that
679 // we can quickly check later which version of the output we want.
687 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
689 if (!strcasecmp(which_command, "article")) {
692 else if (!strcasecmp(which_command, "head")) {
695 else if (!strcasecmp(which_command, "body")) {
698 else if (!strcasecmp(which_command, "stat")) {
702 cprintf("500 I'm afraid I can't do that.\r\n");
706 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
707 int headers_only = HEADERS_ALL;
708 if (acmd == HEAD) headers_only = HEADERS_FAST;
709 else if (acmd == BODY) headers_only = HEADERS_NONE;
710 else if (acmd == STAT) headers_only = HEADERS_FAST;
712 // now figure out what the client is asking for.
713 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
714 lb = strchr(requested_article, '<');
715 rb = strchr(requested_article, '>');
716 requested_msgnum = atol(requested_article);
718 // If no article number or message-id is specified, the client wants the "currently selected article"
719 if (IsEmptyStr(requested_article)) {
720 if (nntpstate->current_article_number < 1) {
721 cprintf("420 No current article selected\r\n");
724 requested_msgnum = nntpstate->current_article_number;
725 must_change_currently_selected_article = 1;
726 // got it -- now fall through and keep going
729 // If the requested article is numeric, it maps directly to a message number. Good.
730 else if (requested_msgnum > 0) {
731 must_change_currently_selected_article = 1;
732 // good -- fall through and keep going
735 // If the requested article has angle brackets, the client wants a specific message-id.
736 // We don't know how to do that yet.
737 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
738 must_change_currently_selected_article = 0;
739 cprintf("500 I don't know how to fetch by message-id yet.\r\n"); // FIXME
743 // Anything else is noncompliant gobbledygook and should die in a car fire.
745 must_change_currently_selected_article = 0;
746 cprintf("500 syntax error\r\n");
750 // At this point we know the message number of the "article" being requested.
751 // We have an awesome API call that does all the heavy lifting for us.
752 char *fetched_message_id = NULL;
753 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
754 int fetch = CtdlOutputMsg(requested_msgnum,
755 MT_RFC822, // output in RFC822 format ... sort of
756 headers_only, // headers, body, or both?
757 0, // don't do Citadel protocol responses
759 NULL, // teh whole thing, not just a section
760 0, // no flags yet ... maybe new ones for Path: etc ?
763 &fetched_message_id // extract the message ID from the message as we go...
765 StrBuf *msgtext = CC->redirect_buffer;
766 CC->redirect_buffer = NULL;
768 if (fetch != om_ok) {
769 cprintf("423 no article with that number\r\n");
770 FreeStrBuf(&msgtext);
774 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
775 // MUST or MUST NOT be set to the message we just referenced.
776 if (must_change_currently_selected_article) {
777 nntpstate->current_article_number = requested_msgnum;
780 // Now give the client what it asked for.
781 if (acmd == ARTICLE) {
782 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
785 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
788 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
791 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
792 FreeStrBuf(&msgtext);
796 client_write(SKEY(msgtext));
797 cprintf(".\r\n"); // this protocol uses a dot terminator
798 FreeStrBuf(&msgtext);
799 if (fetched_message_id) free(fetched_message_id);
804 // Utility function for nntp_last_next() that turns a msgnum into a message ID.
805 // The memory for the returned string is pwnz0red by the caller.
807 char *message_id_from_msgnum(long msgnum) {
808 char *fetched_message_id = NULL;
809 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
810 CtdlOutputMsg(msgnum,
811 MT_RFC822, // output in RFC822 format ... sort of
812 HEADERS_FAST, // headers, body, or both?
813 0, // don't do Citadel protocol responses
815 NULL, // teh whole thing, not just a section
816 0, // no flags yet ... maybe new ones for Path: etc ?
819 &fetched_message_id // extract the message ID from the message as we go...
821 StrBuf *msgtext = CC->redirect_buffer;
822 CC->redirect_buffer = NULL;
824 FreeStrBuf(&msgtext);
825 return(fetched_message_id);
830 // The LAST and NEXT commands are so similar that they are handled by a single function.
832 void nntp_last_next(const char *cmd) {
833 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
835 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
836 char which_command[16];
839 // We're going to store one of these values in the variable 'acmd' so that
840 // we can quickly check later which version of the output we want.
846 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
848 if (!strcasecmp(which_command, "last")) {
851 else if (!strcasecmp(which_command, "next")) {
855 cprintf("500 I'm afraid I can't do that.\r\n");
859 // ok, here we go ... fetch the msglist so we can figure out our place in the universe
860 struct nntp_msglist nm;
862 long selected_msgnum = 0;
863 char *message_id = NULL;
865 nm = nntp_fetch_msglist(&CC->room);
866 if ((nm.num_msgs < 0) || (nm.msgnums == NULL)) {
867 cprintf("500 something bad happened\r\n");
871 if ( (acmd == NNTP_LAST) && (nm.num_msgs == 0) ) {
872 cprintf("422 no previous article in this group\r\n"); // nothing here
875 else if ( (acmd == NNTP_LAST) && (nntpstate->current_article_number <= nm.msgnums[0]) ) {
876 cprintf("422 no previous article in this group\r\n"); // already at the beginning
879 else if (acmd == NNTP_LAST) {
880 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
881 if ( (nm.msgnums[i] >= nntpstate->current_article_number) && (i > 0) ) {
882 selected_msgnum = nm.msgnums[i-1];
885 if (selected_msgnum > 0) {
886 nntpstate->current_article_number = selected_msgnum;
887 message_id = message_id_from_msgnum(nntpstate->current_article_number);
888 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
889 if (message_id) free(message_id);
892 cprintf("422 no previous article in this group\r\n");
896 else if ( (acmd == NNTP_NEXT) && (nm.num_msgs == 0) ) {
897 cprintf("421 no next article in this group\r\n"); // nothing here
900 else if ( (acmd == NNTP_NEXT) && (nntpstate->current_article_number >= nm.msgnums[nm.num_msgs-1]) ) {
901 cprintf("421 no next article in this group\r\n"); // already at the end
904 else if (acmd == NNTP_NEXT) {
905 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
906 if (nm.msgnums[i] > nntpstate->current_article_number) {
907 selected_msgnum = nm.msgnums[i];
910 if (selected_msgnum > 0) {
911 nntpstate->current_article_number = selected_msgnum;
912 message_id = message_id_from_msgnum(nntpstate->current_article_number);
913 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
914 if (message_id) free(message_id);
917 cprintf("421 no next article in this group\r\n");
921 // should never get here
923 cprintf("500 internal error\r\n");
926 if (nm.msgnums != NULL) {
934 // back end for the XOVER command , called for each message number
936 void nntp_xover_backend(long msgnum, void *userdata) {
938 struct listgroup_range *lr = (struct listgroup_range *)userdata;
940 // check range if supplied
941 if (msgnum < lr->lo) return;
942 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
944 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 0);
949 // Teh RFC says we need:
950 // -------------------------
951 // Subject header content
952 // From header content
953 // Date header content
954 // Message-ID header content
955 // References header content
956 // :bytes metadata item
957 // :lines metadata item
959 time_t msgtime = atol(msg->cm_fields[eTimestamp]);
961 ctime_r(&msgtime, strtimebuf);
963 // here we go -- print the line o'data
964 cprintf("%ld\t%s\t%s <%s>\t%s\t%s\t%s\t100\t10\r\n",
966 msg->cm_fields[eMsgSubject],
967 msg->cm_fields[eAuthor],
968 msg->cm_fields[erFc822Addr],
970 msg->cm_fields[emessageId],
971 msg->cm_fields[eWeferences]
980 // XOVER is used by some clients, even if we don't offer it
982 void nntp_xover(const char *cmd) {
983 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
985 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
987 struct listgroup_range lr;
989 extract_token(range, cmd, 1, ' ', sizeof range);
992 lr.lo = nntpstate->current_article_number;
993 lr.hi = nntpstate->current_article_number;
996 char *dash = strchr(range, '-');
1003 if (lr.hi < lr.lo) {
1012 cprintf("224 Overview information follows\r\n");
1013 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_xover_backend, &lr);
1019 // Main command loop for NNTP server sessions.
1021 void nntp_command_loop(void) {
1022 StrBuf *Cmd = NewStrBuf();
1026 if (CtdlClientGetLine(Cmd) < 1) {
1027 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
1028 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
1032 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
1033 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
1035 // Rumpelstiltskin lookups are *awesome*
1037 if (!strcasecmp(cmdname, "quit")) {
1041 else if (!strcasecmp(cmdname, "help")) {
1045 else if (!strcasecmp(cmdname, "date")) {
1049 else if (!strcasecmp(cmdname, "capabilities")) {
1050 nntp_capabilities();
1053 else if (!strcasecmp(cmdname, "starttls")) {
1057 else if (!strcasecmp(cmdname, "authinfo")) {
1058 nntp_authinfo(ChrPtr(Cmd));
1061 else if (!strcasecmp(cmdname, "newgroups")) {
1062 nntp_newgroups(ChrPtr(Cmd));
1065 else if (!strcasecmp(cmdname, "list")) {
1066 nntp_list(ChrPtr(Cmd));
1069 else if (!strcasecmp(cmdname, "group")) {
1070 nntp_group(ChrPtr(Cmd));
1073 else if (!strcasecmp(cmdname, "listgroup")) {
1074 nntp_group(ChrPtr(Cmd));
1077 else if (!strcasecmp(cmdname, "mode")) {
1078 nntp_mode(ChrPtr(Cmd));
1082 (!strcasecmp(cmdname, "article"))
1083 || (!strcasecmp(cmdname, "head"))
1084 || (!strcasecmp(cmdname, "body"))
1085 || (!strcasecmp(cmdname, "stat"))
1088 nntp_article(ChrPtr(Cmd));
1092 (!strcasecmp(cmdname, "last"))
1093 || (!strcasecmp(cmdname, "next"))
1096 nntp_last_next(ChrPtr(Cmd));
1100 (!strcasecmp(cmdname, "xover"))
1101 || (!strcasecmp(cmdname, "over"))
1104 nntp_xover(ChrPtr(Cmd));
1108 cprintf("500 I'm afraid I can't do that.\r\n");
1115 // ****************************************************************************
1116 // MODULE INITIALIZATION STUFF
1117 // ****************************************************************************
1121 // This cleanup function blows away the temporary memory used by
1124 void nntp_cleanup_function(void) {
1125 /* Don't do this stuff if this is not an NNTP session! */
1126 if (CC->h_command_function != nntp_command_loop) return;
1128 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
1129 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1130 if (nntpstate != NULL) {
1136 const char *CitadelServiceNNTP="NNTP";
1137 const char *CitadelServiceNNTPS="NNTPS";
1139 CTDL_MODULE_INIT(nntp)
1143 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntp_port"),
1148 CitadelServiceNNTP);
1151 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntps_port"),
1156 CitadelServiceNNTPS);
1159 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1162 /* return our module name for the log */