1 // NNTP server module (RFC 3977)
3 // Copyright (c) 2014-2023 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // are subject to the terms of the GNU General Public License version 3.
8 #include "../../sysdep.h"
17 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <libcitadel.h>
28 #include "../../citadel_defs.h"
29 #include "../../server.h"
30 #include "../../citserver.h"
31 #include "../../support.h"
32 #include "../../config.h"
33 #include "../../control.h"
34 #include "../../user_ops.h"
35 #include "../../room_ops.h"
36 #include "../../database.h"
37 #include "../../msgbase.h"
38 #include "../../internet_addressing.h"
39 #include "../../genstamp.h"
40 #include "../../domain.h"
41 #include "../../clientsocket.h"
42 #include "../../locate_host.h"
43 #include "../../citadel_dirs.h"
44 #include "../../ctdl_module.h"
45 #include "serv_nntp.h"
51 // Tests whether the supplied string is a valid newsgroup name
52 // Returns true (nonzero) or false (0)
53 int is_valid_newsgroup_name(char *name) {
59 if (!strncasecmp(name, "ctdl.", 5)) return(0);
63 if (isalpha(ptr[0])) {
71 if ( (isalnum(ptr[0]))
82 return( (has_a_letter) && (num_dots >= 1) ) ;
86 // Convert a Citadel room name to a valid newsgroup name
87 void room_to_newsgroup(char *target, char *source, size_t target_size) {
92 if (is_valid_newsgroup_name(source)) {
93 strncpy(target, source, target_size);
97 strcpy(target, "ctdl.");
102 while (ch=*ptr++, ch!=0) {
103 if (len >= target_size) return;
108 target[len++] = tolower(ch);
112 target[len++] = '+' ;
113 sprintf(&target[len], "%02x", ch);
121 // Convert a newsgroup name to a Citadel room name.
122 // This function recognizes names converted with room_to_newsgroup() and restores them with full fidelity.
123 void newsgroup_to_room(char *target, char *source, size_t target_size) {
128 if (strncasecmp(source, "ctdl.", 5)) { // not a converted room name; pass through as-is
129 strncpy(target, source, target_size);
135 char *ptr = &source[5];
138 while (ch=*ptr++, ch!=0) {
139 if (len >= target_size) return;
146 digit = strtol(hex, NULL, 16);
155 // Here's where our NNTP session begins its happy day.
156 void nntp_greeting(void) {
157 strcpy(CC->cs_clientname, "NNTP session");
158 CC->cs_flags |= CS_STEALTH;
160 CC->session_specific_data = malloc(sizeof(citnntp));
161 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
162 memset(nntpstate, 0, sizeof(citnntp));
164 if (CC->nologin==1) {
165 cprintf("451 Too many connections are already open; please try again later.\r\n");
166 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
170 // Display the standard greeting
171 cprintf("200 %s NNTP Citadel server is not finished yet\r\n", CtdlGetConfigStr("c_fqdn"));
175 // NNTPS is just like NNTP, except it goes crypto right away.
176 void nntps_greeting(void) {
177 CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
179 if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO; // kill session if no crypto
185 // implements the STARTTLS command
186 void nntp_starttls(void) {
187 char ok_response[SIZ];
188 char nosup_response[SIZ];
189 char error_response[SIZ];
191 sprintf(ok_response, "382 Begin TLS negotiation now\r\n");
192 sprintf(nosup_response, "502 Can not initiate TLS negotiation\r\n");
193 sprintf(error_response, "580 Internal error\r\n");
194 CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
198 // Implements the CAPABILITY command
199 void nntp_capabilities(void) {
200 cprintf("101 Capability list:\r\n");
201 cprintf("IMPLEMENTATION Citadel %d\r\n", REV_LEVEL);
202 cprintf("VERSION 2\r\n");
203 cprintf("READER\r\n");
204 cprintf("MODE-READER\r\n");
205 cprintf("LIST ACTIVE NEWSGROUPS\r\n");
208 cprintf("STARTTLS\r\n");
210 if (!CC->logged_in) {
211 cprintf("AUTHINFO USER\r\n");
217 // Implements the QUIT command
218 void nntp_quit(void) {
219 cprintf("221 Goodbye...\r\n");
220 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
224 // Implements the AUTHINFO USER command (RFC 4643)
225 void nntp_authinfo_user(const char *username) {
226 int a = CtdlLoginExistingUser(username);
228 case login_already_logged_in:
229 cprintf("482 Already logged in\r\n");
231 case login_too_many_users:
232 cprintf("481 Too many users are already online (maximum is %d)\r\n", CtdlGetConfigInt("c_maxsessions"));
235 cprintf("381 Password required for %s\r\n", CC->curr_user);
237 case login_not_found:
238 cprintf("481 %s not found\r\n", username);
241 cprintf("502 Internal error\r\n");
246 // Implements the AUTHINFO PASS command (RFC 4643)
247 void nntp_authinfo_pass(const char *buf) {
250 a = CtdlTryPassword(buf, strlen(buf));
253 case pass_already_logged_in:
254 cprintf("482 Already logged in\r\n");
257 cprintf("482 Authentication commands issued out of sequence\r\n");
259 case pass_wrong_password:
260 cprintf("481 Authentication failed\r\n");
263 cprintf("281 Authentication accepted\r\n");
269 // Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
270 void nntp_authinfo(const char *cmd) {
272 if (!strncasecmp(cmd, "authinfo user ", 14)) {
273 nntp_authinfo_user(&cmd[14]);
276 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
277 nntp_authinfo_pass(&cmd[14]);
281 cprintf("502 command unavailable\r\n");
286 // Utility function to fetch the current list of message numbers in a room
287 struct nntp_msglist nntp_fetch_msglist(struct ctdlroom *qrbuf) {
288 struct nntp_msglist nm;
289 struct cdbdata *cdbfr;
291 nm.num_msgs = CtdlFetchMsgList(qrbuf->QRnumber, &nm.msgnums);
296 // Output a room name (newsgroup name) in formats required for LIST and NEWGROUPS command
297 void output_roomname_in_list_format(struct ctdlroom *qrbuf, int which_format, char *wildmat_pattern) {
299 struct nntp_msglist nm;
300 long low_water_mark = 0;
301 long high_water_mark = 0;
303 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
305 if ((wildmat_pattern != NULL) && (!IsEmptyStr(wildmat_pattern))) {
306 if (!wildmat(n_name, wildmat_pattern)) {
311 nm = nntp_fetch_msglist(qrbuf);
312 if ((nm.num_msgs > 0) && (nm.msgnums != NULL)) {
313 low_water_mark = nm.msgnums[0];
314 high_water_mark = nm.msgnums[nm.num_msgs - 1];
317 // Only the mandatory formats are supported
318 switch(which_format) {
319 case NNTP_LIST_ACTIVE:
320 // FIXME we have hardcoded "n" for "no posting allowed" -- fix when we add posting
321 cprintf("%s %ld %ld n\r\n", n_name, high_water_mark, low_water_mark);
323 case NNTP_LIST_NEWSGROUPS:
324 cprintf("%s %s\r\n", n_name, qrbuf->QRname);
328 if (nm.msgnums != NULL) {
334 // Called once per room by nntp_newgroups() to qualify and possibly output a single room
335 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data) {
338 time_t thetime = *(time_t *)data;
340 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
342 // The "created after <date/time>" heuristics depend on the happy coincidence
343 // that for a very long time we have used a unix timestamp as the room record's
344 // generation number (QRgen). When this module is merged into the master
345 // source tree we should rename QRgen to QR_create_time or something like that.
347 if (qrbuf->QRgen >= thetime) {
348 output_roomname_in_list_format(qrbuf, NNTP_LIST_ACTIVE, NULL);
354 // Implements the NEWGROUPS command
355 void nntp_newgroups(const char *cmd) {
356 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
358 char stringy_date[16];
359 char stringy_time[16];
360 char stringy_gmt[16];
364 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
365 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
366 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
368 memset(&tm, 0, sizeof tm);
369 if (strlen(stringy_date) == 6) {
370 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
374 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
377 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
378 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
379 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
380 thetime = mktime(&tm);
381 if (!strcasecmp(stringy_gmt, "GMT")) {
384 thetime += (time_t) &tm.tm_gmtoff;
386 thetime += (time_t) timezone;
390 cprintf("231 list of new newsgroups follows\r\n");
391 CtdlGetUser(&CC->user, CC->curr_user);
392 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
397 // Called once per room by nntp_list() to qualify and possibly output a single room
398 void nntp_list_backend(struct ctdlroom *qrbuf, void *data) {
401 struct nntp_list_data *nld = (struct nntp_list_data *)data;
403 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
405 output_roomname_in_list_format(qrbuf, nld->list_format, nld->wildmat_pattern);
410 // Implements the LIST commands
411 void nntp_list(const char *cmd) {
412 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
414 char list_format[64];
415 char wildmat_pattern[1024];
416 struct nntp_list_data nld;
418 extract_token(list_format, cmd, 1, ' ', sizeof list_format);
419 extract_token(wildmat_pattern, cmd, 2, ' ', sizeof wildmat_pattern);
421 if (strlen(wildmat_pattern) > 0) {
422 nld.wildmat_pattern = wildmat_pattern;
425 nld.wildmat_pattern = NULL;
428 if ( (strlen(cmd) < 6) || (!strcasecmp(list_format, "ACTIVE")) ) {
429 nld.list_format = NNTP_LIST_ACTIVE;
431 else if (!strcasecmp(list_format, "NEWSGROUPS")) {
432 nld.list_format = NNTP_LIST_NEWSGROUPS;
434 else if (!strcasecmp(list_format, "OVERVIEW.FMT")) {
435 nld.list_format = NNTP_LIST_OVERVIEW_FMT;
438 cprintf("501 syntax error , unsupported list format\r\n");
442 // OVERVIEW.FMT delivers a completely different type of data than all of the
443 // other LIST commands. It's a stupid place to put it. But that's how it's
444 // written into RFC3977, so we have to handle it here.
445 if (nld.list_format == NNTP_LIST_OVERVIEW_FMT) {
446 cprintf("215 Order of fields in overview database.\r\n");
447 cprintf("Subject:\r\n");
448 cprintf("From:\r\n");
449 cprintf("Date:\r\n");
450 cprintf("Message-ID:\r\n");
451 cprintf("References:\r\n");
452 cprintf("Bytes:\r\n");
453 cprintf("Lines:\r\n");
458 cprintf("215 list of newsgroups follows\r\n");
459 CtdlGetUser(&CC->user, CC->curr_user);
460 CtdlForEachRoom(nntp_list_backend, &nld);
465 // Implement HELP command.
466 void nntp_help(void) {
467 cprintf("100 This is the Citadel NNTP service.\r\n");
468 cprintf("RTFM http://www.ietf.org/rfc/rfc3977.txt\r\n");
473 // Implement DATE command.
474 void nntp_date(void) {
481 localtime_r(&now, &nowLocal);
482 gmtime_r(&now, &nowUtc);
484 strftime(tsFromUtc, sizeof(tsFromUtc), "%Y%m%d%H%M%S", &nowUtc);
486 cprintf("111 %s\r\n", tsFromUtc);
490 // back end for the LISTGROUP command , called for each message number
491 void nntp_listgroup_backend(long msgnum, void *userdata) {
493 struct listgroup_range *lr = (struct listgroup_range *)userdata;
495 // check range if supplied
496 if (msgnum < lr->lo) return;
497 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
499 cprintf("%ld\r\n", msgnum);
503 // Implements the GROUP and LISTGROUP commands
504 void nntp_group(const char *cmd) {
505 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
507 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
509 char requested_group[1024];
510 char message_range[256];
513 char requested_room[ROOMNAMELEN];
514 char augmented_roomname[ROOMNAMELEN];
518 struct ctdlroom QRscratch;
521 struct listgroup_range lr;
523 extract_token(verb, cmd, 0, ' ', sizeof verb);
524 extract_token(requested_group, cmd, 1, ' ', sizeof requested_group);
525 extract_token(message_range, cmd, 2, ' ', sizeof message_range);
526 extract_token(range_lo, message_range, 0, '-', sizeof range_lo);
527 extract_token(range_hi, message_range, 1, '-', sizeof range_hi);
528 lr.lo = atoi(range_lo);
529 lr.hi = atoi(range_hi);
531 // In LISTGROUP mode we can specify an empty name for 'currently selected'
532 if ((!strcasecmp(verb, "LISTGROUP")) && (IsEmptyStr(requested_group))) {
533 room_to_newsgroup(requested_group, CC->room.QRname, sizeof requested_group);
536 // First try a regular match
537 newsgroup_to_room(requested_room, requested_group, sizeof requested_room);
538 c = CtdlGetRoom(&QRscratch, requested_room);
540 // Then try a mailbox name match
542 CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, requested_room);
543 c = CtdlGetRoom(&QRscratch, augmented_roomname);
545 safestrncpy(requested_room, augmented_roomname, sizeof(requested_room));
549 // If the room exists, check security/access
551 // See if there is an existing user/room relationship
552 CtdlRoomAccess(&QRscratch, &CC->user, &ra, NULL);
554 // normal clients have to pass through security
560 // Fail here if no such room
562 cprintf("411 no such newsgroup\r\n");
567 // CtdlUserGoto() formally takes us to the desired room, happily returning
568 // the number of messages and number of new messages.
569 memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
570 CtdlUserGoto(NULL, 0, 0, &msgs, &new, &oldest, &newest);
571 cprintf("211 %d %ld %ld %s\r\n", msgs, oldest, newest, requested_group);
573 // If this is a GROUP command, set the "current article number" to zero, and then stop here.
574 if (!strcasecmp(verb, "GROUP")) {
575 nntpstate->current_article_number = oldest;
579 // If we get to this point we are running a LISTGROUP command. Fetch those message numbers.
580 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_listgroup_backend, &lr);
585 // Implements the MODE command
586 void nntp_mode(const char *cmd) {
590 extract_token(which_mode, cmd, 1, ' ', sizeof which_mode);
592 if (!strcasecmp(which_mode, "reader")) {
593 // FIXME implement posting and change to 200
594 cprintf("201 Reader mode activated\r\n");
597 cprintf("501 unknown mode\r\n");
602 // Implements the ARTICLE, HEAD, BODY, and STAT commands.
603 // (These commands all accept the same parameters; they differ only in how they output the retrieved message.)
604 void nntp_article(const char *cmd) {
605 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
607 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
608 char which_command[16];
610 char requested_article[256];
611 long requested_msgnum = 0;
612 char *lb, *rb = NULL;
613 int must_change_currently_selected_article = 0;
615 // We're going to store one of these values in the variable 'acmd' so that
616 // we can quickly check later which version of the output we want.
624 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
626 if (!strcasecmp(which_command, "article")) {
629 else if (!strcasecmp(which_command, "head")) {
632 else if (!strcasecmp(which_command, "body")) {
635 else if (!strcasecmp(which_command, "stat")) {
639 cprintf("500 I'm afraid I can't do that.\r\n");
643 // Which NNTP command was issued, determines whether we will fetch headers, body, or both.
644 int headers_only = HEADERS_ALL;
645 if (acmd == HEAD) headers_only = HEADERS_FAST;
646 else if (acmd == BODY) headers_only = HEADERS_NONE;
647 else if (acmd == STAT) headers_only = HEADERS_FAST;
649 // now figure out what the client is asking for.
650 extract_token(requested_article, cmd, 1, ' ', sizeof requested_article);
651 lb = strchr(requested_article, '<');
652 rb = strchr(requested_article, '>');
653 requested_msgnum = atol(requested_article);
655 // If no article number or message-id is specified, the client wants the "currently selected article"
656 if (IsEmptyStr(requested_article)) {
657 if (nntpstate->current_article_number < 1) {
658 cprintf("420 No current article selected\r\n");
661 requested_msgnum = nntpstate->current_article_number;
662 must_change_currently_selected_article = 1;
663 // got it -- now fall through and keep going
666 // If the requested article is numeric, it maps directly to a message number. Good.
667 else if (requested_msgnum > 0) {
668 must_change_currently_selected_article = 1;
669 // good -- fall through and keep going
672 // If the requested article has angle brackets, the client wants a specific message-id.
673 // We don't know how to do that yet.
674 else if ( (lb != NULL) && (rb != NULL) && (lb < rb) ) {
675 must_change_currently_selected_article = 0;
676 cprintf("500 I don't know how to fetch by message-id yet.\r\n"); // FIXME
680 // Anything else is noncompliant gobbledygook and should die in a car fire.
682 must_change_currently_selected_article = 0;
683 cprintf("500 syntax error\r\n");
687 // At this point we know the message number of the "article" being requested.
688 // We have an awesome API call that does all the heavy lifting for us.
689 char *fetched_message_id = NULL;
690 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
691 int fetch = CtdlOutputMsg(requested_msgnum,
692 MT_RFC822, // output in RFC822 format ... sort of
693 headers_only, // headers, body, or both?
694 0, // don't do Citadel protocol responses
696 NULL, // teh whole thing, not just a section
697 0, // no flags yet ... maybe new ones for Path: etc ?
700 &fetched_message_id // extract the message ID from the message as we go...
702 StrBuf *msgtext = CC->redirect_buffer;
703 CC->redirect_buffer = NULL;
705 if (fetch != om_ok) {
706 cprintf("423 no article with that number\r\n");
707 FreeStrBuf(&msgtext);
711 // RFC3977 6.2.1.2 specifes conditions under which the "currently selected article"
712 // MUST or MUST NOT be set to the message we just referenced.
713 if (must_change_currently_selected_article) {
714 nntpstate->current_article_number = requested_msgnum;
717 // Now give the client what it asked for.
718 if (acmd == ARTICLE) {
719 cprintf("220 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
722 cprintf("221 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
725 cprintf("222 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
728 cprintf("223 %ld <%s>\r\n", requested_msgnum, fetched_message_id);
729 FreeStrBuf(&msgtext);
733 client_write(SKEY(msgtext));
734 cprintf(".\r\n"); // this protocol uses a dot terminator
735 FreeStrBuf(&msgtext);
736 if (fetched_message_id) free(fetched_message_id);
740 // Utility function for nntp_last_next() that turns a msgnum into a message ID.
741 // The memory for the returned string is pwnz0red by the caller.
742 char *message_id_from_msgnum(long msgnum) {
743 char *fetched_message_id = NULL;
744 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
745 CtdlOutputMsg(msgnum,
746 MT_RFC822, // output in RFC822 format ... sort of
747 HEADERS_FAST, // headers, body, or both?
748 0, // don't do Citadel protocol responses
750 NULL, // teh whole thing, not just a section
751 0, // no flags yet ... maybe new ones for Path: etc ?
754 &fetched_message_id // extract the message ID from the message as we go...
756 StrBuf *msgtext = CC->redirect_buffer;
757 CC->redirect_buffer = NULL;
759 FreeStrBuf(&msgtext);
760 return(fetched_message_id);
764 // The LAST and NEXT commands are so similar that they are handled by a single function.
765 void nntp_last_next(const char *cmd) {
766 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
768 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
769 char which_command[16];
772 // We're going to store one of these values in the variable 'acmd' so that
773 // we can quickly check later which version of the output we want.
779 extract_token(which_command, cmd, 0, ' ', sizeof which_command);
781 if (!strcasecmp(which_command, "last")) {
784 else if (!strcasecmp(which_command, "next")) {
788 cprintf("500 I'm afraid I can't do that.\r\n");
792 // ok, here we go ... fetch the msglist so we can figure out our place in the universe
793 struct nntp_msglist nm;
795 long selected_msgnum = 0;
796 char *message_id = NULL;
798 nm = nntp_fetch_msglist(&CC->room);
799 if ((nm.num_msgs < 0) || (nm.msgnums == NULL)) {
800 cprintf("500 something bad happened\r\n");
804 if ( (acmd == NNTP_LAST) && (nm.num_msgs == 0) ) {
805 cprintf("422 no previous article in this group\r\n"); // nothing here
808 else if ( (acmd == NNTP_LAST) && (nntpstate->current_article_number <= nm.msgnums[0]) ) {
809 cprintf("422 no previous article in this group\r\n"); // already at the beginning
812 else if (acmd == NNTP_LAST) {
813 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
814 if ( (nm.msgnums[i] >= nntpstate->current_article_number) && (i > 0) ) {
815 selected_msgnum = nm.msgnums[i-1];
818 if (selected_msgnum > 0) {
819 nntpstate->current_article_number = selected_msgnum;
820 message_id = message_id_from_msgnum(nntpstate->current_article_number);
821 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
822 if (message_id) free(message_id);
825 cprintf("422 no previous article in this group\r\n");
829 else if ( (acmd == NNTP_NEXT) && (nm.num_msgs == 0) ) {
830 cprintf("421 no next article in this group\r\n"); // nothing here
833 else if ( (acmd == NNTP_NEXT) && (nntpstate->current_article_number >= nm.msgnums[nm.num_msgs-1]) ) {
834 cprintf("421 no next article in this group\r\n"); // already at the end
837 else if (acmd == NNTP_NEXT) {
838 for (i=0; ((i<nm.num_msgs)&&(selected_msgnum<=0)); ++i) {
839 if (nm.msgnums[i] > nntpstate->current_article_number) {
840 selected_msgnum = nm.msgnums[i];
843 if (selected_msgnum > 0) {
844 nntpstate->current_article_number = selected_msgnum;
845 message_id = message_id_from_msgnum(nntpstate->current_article_number);
846 cprintf("223 %ld <%s>\r\n", nntpstate->current_article_number, message_id);
847 if (message_id) free(message_id);
850 cprintf("421 no next article in this group\r\n");
854 // should never get here
856 cprintf("500 internal error\r\n");
859 if (nm.msgnums != NULL) {
866 // back end for the XOVER command , called for each message number
867 void nntp_xover_backend(long msgnum, void *userdata) {
869 struct listgroup_range *lr = (struct listgroup_range *)userdata;
871 // check range if supplied
872 if (msgnum < lr->lo) return;
873 if ((lr->hi != 0) && (msgnum > lr->hi)) return;
875 struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 0);
880 // Teh RFC says we need:
881 // -------------------------
882 // Subject header content
883 // From header content
884 // Date header content
885 // Message-ID header content
886 // References header content
887 // :bytes metadata item
888 // :lines metadata item
890 time_t msgtime = atol(msg->cm_fields[eTimestamp]);
892 ctime_r(&msgtime, strtimebuf);
894 // here we go -- print the line o'data
895 cprintf("%ld\t%s\t%s <%s>\t%s\t%s\t%s\t100\t10\r\n",
897 msg->cm_fields[eMsgSubject],
898 msg->cm_fields[eAuthor],
899 msg->cm_fields[erFc822Addr],
901 msg->cm_fields[emessageId],
902 msg->cm_fields[eWeferences]
909 // XOVER is used by some clients, even if we don't offer it
910 void nntp_xover(const char *cmd) {
911 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
913 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
915 struct listgroup_range lr;
917 extract_token(range, cmd, 1, ' ', sizeof range);
920 lr.lo = nntpstate->current_article_number;
921 lr.hi = nntpstate->current_article_number;
924 char *dash = strchr(range, '-');
940 cprintf("224 Overview information follows\r\n");
941 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, nntp_xover_backend, &lr);
946 // Main command loop for NNTP server sessions.
947 void nntp_command_loop(void) {
948 StrBuf *Cmd = NewStrBuf();
952 if (CtdlClientGetLine(Cmd) < 1) {
953 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
954 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
958 syslog(LOG_DEBUG, "NNTP: %s\n", ((!strncasecmp(ChrPtr(Cmd), "AUTHINFO", 8)) ? "AUTHINFO ..." : ChrPtr(Cmd)));
959 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
961 // Rumpelstiltskin lookups are *awesome*
963 if (!strcasecmp(cmdname, "quit")) {
967 else if (!strcasecmp(cmdname, "help")) {
971 else if (!strcasecmp(cmdname, "date")) {
975 else if (!strcasecmp(cmdname, "capabilities")) {
979 else if (!strcasecmp(cmdname, "starttls")) {
983 else if (!strcasecmp(cmdname, "authinfo")) {
984 nntp_authinfo(ChrPtr(Cmd));
987 else if (!strcasecmp(cmdname, "newgroups")) {
988 nntp_newgroups(ChrPtr(Cmd));
991 else if (!strcasecmp(cmdname, "list")) {
992 nntp_list(ChrPtr(Cmd));
995 else if (!strcasecmp(cmdname, "group")) {
996 nntp_group(ChrPtr(Cmd));
999 else if (!strcasecmp(cmdname, "listgroup")) {
1000 nntp_group(ChrPtr(Cmd));
1003 else if (!strcasecmp(cmdname, "mode")) {
1004 nntp_mode(ChrPtr(Cmd));
1008 (!strcasecmp(cmdname, "article"))
1009 || (!strcasecmp(cmdname, "head"))
1010 || (!strcasecmp(cmdname, "body"))
1011 || (!strcasecmp(cmdname, "stat"))
1014 nntp_article(ChrPtr(Cmd));
1018 (!strcasecmp(cmdname, "last"))
1019 || (!strcasecmp(cmdname, "next"))
1022 nntp_last_next(ChrPtr(Cmd));
1026 (!strcasecmp(cmdname, "xover"))
1027 || (!strcasecmp(cmdname, "over"))
1030 nntp_xover(ChrPtr(Cmd));
1034 cprintf("500 I'm afraid I can't do that.\r\n");
1041 // ****************************************************************************
1042 // MODULE INITIALIZATION STUFF
1043 // ****************************************************************************
1046 // This cleanup function blows away the temporary memory used by the NNTP server.
1047 void nntp_cleanup_function(void) {
1048 // Don't do this stuff if this is not an NNTP session!
1049 if (CC->h_command_function != nntp_command_loop) return;
1051 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
1052 citnntp *nntpstate = (citnntp *) CC->session_specific_data;
1053 if (nntpstate != NULL) {
1059 const char *CitadelServiceNNTP="NNTP";
1060 const char *CitadelServiceNNTPS="NNTPS";
1063 // Initialization function, called from modules_init.c
1064 char *ctdl_module_init_nntp(void) {
1066 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntp_port"),
1071 CitadelServiceNNTP);
1074 CtdlRegisterServiceHook(CtdlGetConfigInt("c_nntps_port"),
1079 CitadelServiceNNTPS);
1082 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
1085 // return our module name for the log