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"
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);
236 cprintf("250 NOOP\r\n");
240 void nntp_capabilities(void)
242 cprintf("101 Capability list:\r\n");
244 cprintf("STARTTLS\r\n");
246 if (!CC->logged_in) {
247 cprintf("AUTHINFO USER\r\n");
255 cprintf("221 Goodbye...\r\n");
256 CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
260 void nntp_cleanup(void)
262 /* nothing here yet */
268 * Implements the AUTHINFO USER command (RFC 4643)
270 void nntp_authinfo_user(const char *username)
272 int a = CtdlLoginExistingUser(NULL, username);
274 case login_already_logged_in:
275 cprintf("482 Already logged in\r\n");
277 case login_too_many_users:
278 cprintf("481 Too many users are already online (maximum is %d)\r\n", config.c_maxsessions);
281 cprintf("381 Password required for %s\r\n", CC->curr_user);
283 case login_not_found:
284 cprintf("481 %s not found\r\n", username);
287 cprintf("502 Internal error\r\n");
293 * Implements the AUTHINFO PASS command (RFC 4643)
295 void nntp_authinfo_pass(const char *buf)
299 a = CtdlTryPassword(buf, strlen(buf));
302 case pass_already_logged_in:
303 cprintf("482 Already logged in\r\n");
306 cprintf("482 Authentication commands issued out of sequence\r\n");
308 case pass_wrong_password:
309 cprintf("481 Authentication failed\r\n");
312 cprintf("281 Authentication accepted\r\n");
320 * Implements the AUTHINFO extension (RFC 4643) in USER/PASS mode
322 void nntp_authinfo(const char *cmd) {
324 if (!strncasecmp(cmd, "authinfo user ", 14)) {
325 nntp_authinfo_user(&cmd[14]);
328 else if (!strncasecmp(cmd, "authinfo pass ", 14)) {
329 nntp_authinfo_pass(&cmd[14]);
333 cprintf("502 command unavailable\r\n");
339 /* FIXME not finished need to add water marks
341 void output_roomname_in_list_active_format(struct ctdlroom *qrbuf) {
343 room_to_newsgroup(n_name, qrbuf->QRname, sizeof n_name);
344 cprintf("%s\r\n", n_name);
351 void nntp_newgroups_backend(struct ctdlroom *qrbuf, void *data)
355 time_t thetime = *(time_t *)data;
357 CtdlRoomAccess(qrbuf, &CC->user, &ra, &view);
360 * The "created after <date/time>" heuristics depend on the happy coincidence
361 * that for a very long time we have used a unix timestamp as the room record's
362 * generation number (QRgen). When this module is merged into the master
363 * source tree we should rename QRgen to QR_create_time or something like that.
367 if (qrbuf->QRgen >= thetime) {
368 output_roomname_in_list_active_format(qrbuf);
374 * Implements the NEWGROUPS command
376 void nntp_newgroups(const char *cmd) {
378 * HACK: this works because the 5XX series error codes from citadel
379 * protocol will also be considered error codes by an NNTP client
381 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
384 char stringy_date[16];
385 char stringy_time[16];
386 char stringy_gmt[16];
390 extract_token(stringy_date, cmd, 1, ' ', sizeof stringy_date);
391 extract_token(stringy_time, cmd, 2, ' ', sizeof stringy_time);
392 extract_token(stringy_gmt, cmd, 3, ' ', sizeof stringy_gmt);
394 memset(&tm, 0, sizeof tm);
395 if (strlen(stringy_date) == 6) {
396 sscanf(stringy_date, "%2d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
400 sscanf(stringy_date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
403 tm.tm_mon -= 1; // tm_mon is zero based (0=January)
404 tm.tm_isdst = (-1); // let the C library figure out whether DST is in effect
405 sscanf(stringy_time, "%2d%2d%2d", &tm.tm_hour, &tm.tm_min ,&tm.tm_sec);
406 thetime = mktime(&tm);
407 if (!strcasecmp(stringy_gmt, "GMT")) {
413 cprintf("231 list of new newsgroups follows\r\n");
414 CtdlGetUser(&CC->user, CC->curr_user);
415 CtdlForEachRoom(nntp_newgroups_backend, &thetime);
421 * Main command loop for NNTP server sessions.
423 void nntp_command_loop(void)
425 StrBuf *Cmd = NewStrBuf();
429 if (CtdlClientGetLine(Cmd) < 1) {
430 syslog(LOG_CRIT, "NNTP: client disconnected: ending session.\n");
431 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
435 syslog(LOG_DEBUG, "NNTP server: %s\n", ChrPtr(Cmd));
436 extract_token(cmdname, ChrPtr(Cmd), 0, ' ', sizeof cmdname);
439 * Rumpelstiltskin lookups are awesome
442 if (!strcasecmp(cmdname, "quit")) {
446 else if (!strcasecmp(cmdname, "capabilities")) {
450 else if (!strcasecmp(cmdname, "starttls")) {
454 else if (!strcasecmp(cmdname, "noop")) {
458 else if (!strcasecmp(cmdname, "authinfo")) {
459 nntp_authinfo(ChrPtr(Cmd));
462 else if (!strcasecmp(cmdname, "newgroups")) {
463 nntp_newgroups(ChrPtr(Cmd));
467 cprintf("500 I'm afraid I can't do that.\r\n");
474 /*****************************************************************************/
475 /* MODULE INITIALIZATION STUFF */
476 /*****************************************************************************/
480 * This cleanup function blows away the temporary memory used by
483 void nntp_cleanup_function(void)
485 /* Don't do this stuff if this is not an NNTP session! */
486 if (CC->h_command_function != nntp_command_loop) return;
488 syslog(LOG_DEBUG, "Performing NNTP cleanup hook\n");
491 const char *CitadelServiceNNTP="NNTP";
493 CTDL_MODULE_INIT(nntp)
497 CtdlRegisterServiceHook(119, // FIXME config.c_nntp_port,
505 CtdlRegisterServiceHook(563, // FIXME config.c_nntps_port,
513 CtdlRegisterCleanupHook(nntp_cleanup);
514 CtdlRegisterSessionHook(nntp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
517 /* return our module name for the log */