-
Notifications
You must be signed in to change notification settings - Fork 1
/
newsd.C
598 lines (533 loc) · 15.8 KB
/
newsd.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// newsd -- A simple news server - [email protected]
//
// Copyright 2003-2004 Michael Sweet
// Copyright 2002 Greg Ercolano
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public Licensse as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// This program was designed based on the RFCs:
//
// rfc1036.txt
// rfc2980.txt
// rfc977.txt
//
// TODO:
// Should probably reorganize both NNTP headers and mail headers
// so when browsing messages, headers don't jump around
//
// Cleanup the whole mail gateway posting/mail vs regular posting/mail;
// lots of redundant code.
//
// IPv6 support!
//
#define _BSD_SIGNALS 1 /* IRIX */
#define _USE_BSD /* LINUX */
#define _BSD /* OSF1 */
// Convenience macro...
#define ISHEAD(a) (strncasecmp(head[t].c_str(), (a), strlen(a))==0)
#include "Server.H"
// Global configuration data...
Configuration G_conf;
// Number of child processes...
static unsigned G_numclients = 0;
// Overview data headers...
static const char *overview[] =
{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
"Bytes:",
"Lines:",
"Xref:full",
"Reply-To:",
NULL
};
// HANDLE SIGCHLD
// Daemon reaps child processes to keep count.
//
void sigcld_handler(int)
{
// REAPER
// for() limits #children reaped at a time mainly to prevent
// possible infinite loop.
//
pid_t pid; // Process ID
int status; // Exit status
for (int t = 0; t < 100 && (pid = waitpid(-1, &status, WNOHANG)) > 0; t ++)
{
if ( pid > 0 && G_numclients > 0 )
G_numclients --;
}
}
void HelpAndExit()
{
fputs("newsd - a simple news daemon (V " VERSION ")\n"
" See LICENSE file packaged with newsd for license/copyright info.\n"
"\n"
"Usage:\n"
" newsd [-c configfile] [-d] [-f] -- start server\n"
" newsd -mailgateway <group> [-preserve-date] -- gateway an email (stdin) into specified <group>\n"
" newsd -newgroup -- used to create new groups\n"
" newsd -rotate -- force log rotation\n",
stderr);
exit(1);
}
// RUN AS A PARTICULAR USER
int RunAs()
{
int err = 0;
if (chdir("/")) { perror("newsd: chdir(\"/\")"); err = 1; }
if (G_conf.BadUser()) err = 1;
else if (!geteuid())
{
gid_t gid = G_conf.GID();
setgroups(1, &gid);
if ( setgid(gid) < 0 ) { perror("newsd: setgid()"); err = 1; }
if ( setuid(G_conf.UID()) < 0 ) { perror("newsd: setuid()"); err = 1; }
}
return(err);
}
// CREATE NEWSD SPOOL DIRECTORY (IF IT DOESNT ALREADY EXIST)
// Returns:
// 0 on success (dir exists or was created OK)
// -1 on error, prints reason on stderr
//
int CreateSpoolDir()
{
const char *spooldir = G_conf.SpoolDir();
struct stat buf;
if ( stat(spooldir, &buf) == -1 )
{
// Spool dir doesn't exist? Create it..
if ( mkdir(spooldir, 0755) == -1 ) // mkdir failed?
{
// Failed..
string emsg = "mkdir('";
emsg += spooldir;
emsg += "')";
perror(emsg.c_str());
return -1;
}
}
// Make sure spooldir owned by configured news user
if ( chown(spooldir, G_conf.UID(), G_conf.GID()) == -1 )
{
// Failed..
string emsg = "chown('";
emsg += spooldir;
emsg += "',";
emsg += G_conf.UID();
emsg += ",";
emsg += G_conf.GID();
emsg += ")";
perror(emsg.c_str());
return -1;
}
return 0;
}
/****
// CREATE DEAD LETTER FILE, CHOWN ACCORDINGLY
// Returns:
// 0 on success
// -1 on error, prints reason on stderr
//
int CreateDeadLetter()
{
// Make sure spool dir exists; create if not
if ( CreateSpoolDir() < 0 ) return -1;
string filename = G_conf.SpoolDir();
filename += "/.deadletters";
fprintf(stderr, "(Creating %s)\n", filename.c_str());
FILE *fp = fopen(filename.c_str(), "a");
if ( fp == NULL ) { perror(filename.c_str()); return(-1); }
fchmod(fileno(fp), 0600);
fchown(fileno(fp), G_conf.UID(), G_conf.GID());
fclose(fp);
return(0);
}
****/
// WRITE A MESSAGE (header+body) TO DEAD LETTER FILE
void DeadLetter(const char *errmsg, vector<string>&head, vector<string>&body)
{
// Make sure spool dir exists; create if not
if ( CreateSpoolDir() < 0 ) return;
string filename = G_conf.SpoolDir();
filename += "/.deadletters";
FILE *fp = fopen(filename.c_str(), "a");
fchmod(fileno(fp), 0600);
fchown(fileno(fp), G_conf.UID(), G_conf.GID());
if ( fp == NULL )
{ perror(filename.c_str()); return; }
for ( uint t=0; t<head.size(); t++ )
fprintf(fp, "%s\n", head[t].c_str());
fprintf(fp, "\n");
for ( uint t=0; t<body.size(); t++ )
fprintf(fp, "%s\n", body[t].c_str());
fprintf(fp, "\n");
fclose(fp);
}
// HANDLE GATEWAYING MAIL INTO THE NEWSGROUP
// Reads email message from stdin.
//
int MailGateway(const char *groupname,
bool preservedate=0)
{
Group group;
if ( group.LoadInfo(groupname) < 0 )
{
fprintf(stderr, "newsd: Unknown group \"%s\": %s\n", groupname,
group.Errmsg());
return(1);
}
// COLLECT EMAIL FROM STDIN
int linechars = 0,
linecount = 0,
toolong = 0;
char c;
string msg;
// CONVERT EMAIL HEADER -> NEWSGROUP HEADER
{
// Newsgroups:
msg = "Newsgroups: ";
msg += groupname;
msg += "\r\n";
// X-News-Gateway:
// I pulled this outta my ass; need some way to indicate
// msg passed through a mail -> news gateway.
//
msg += "X-Mail-To-News-Gateway: via newsd ";
msg += VERSION;
msg += "\r\n";
}
while (read(0, &c, 1) == 1 )
{
if ( c == '\r' ) continue; // ignore \r, we handle it ourself
// KEEP TRACK OF #LINES
// Lines longer than 80 chars count as multiple lines.
// If posting too long, stop accumulating message in ram,
// but keep reading until they've sent the terminating "."
//
++linechars;
if ( linechars > 80 || c == '\n' )
{ linechars = 0; linecount++; }
if ( group.PostLimit() > 0 && linecount > group.PostLimit() )
{ toolong = 1; continue; }
if ( c == '\n' ) msg += '\r';
msg += c;
}
// POSTING TOO LONG? FAIL
if ( toolong )
{
fprintf(stderr, "newsd: Article not posted to %s: longer than %d lines.\n",
groupname, group.PostLimit());
return(1);
}
// BLESS ARTICLE -- VERIFY HEADER INTEGRITY, ADD NEEDED HEADERS
vector<string> head;
vector<string> body;
if ( group.ParseArticle(msg, head, body) < 0 )
{
fprintf(stderr, "newsd: Article not posted to %s: %s.\n",
groupname, group.Errmsg());
return(1);
}
// UPDATE 'Path:'
group.UpdatePath(head);
// CHECK FOR LOOPS, MASSAGE HEADERS
for ( uint t=0; t<head.size(); t++ )
{
// CONVERT RFC822 "From .." -> "X-Original-From: .."
if ( ISHEAD("From ") )
{
string newfrom = head[t];
newfrom.replace(0, strlen("From "), "X-Original-From: ");
head[t] = newfrom;
}
// SHORT CIRCUIT LOOP DELIVERY
// Example: "X-Loop: outOfSpace"
//
else if ( ISHEAD("X-Newsd-Loop:") || ISHEAD("X-Loop:") )
{
string errmsg = "newsd: -mailgateway ";
errmsg += groupname;
errmsg += ": NOT POSTED: '";
errmsg += head[t];
errmsg += "' mail loop detected: message dropped to "
SPOOL_DIR "/.deadletters";
DeadLetter(errmsg.c_str(), head, body);
fprintf(stderr, "%s\n",
(const char*)errmsg.c_str());
return(1);
}
}
{for (uint t=0; t<head.size(); t++) { G_conf.LogMessage(L_DEBUG, "Gateway Post: --- head[%03d]: '%s'\n", t, head[t].c_str()); } }
{for (uint t=0; t<body.size(); t++) { G_conf.LogMessage(L_DEBUG, "Gateway Post: --- body[%03d]: '%s'\n", t, body[t].c_str()); } }
// POST ARTICLE
// Don't affect 'current group' or 'current article'.
//
if ( group.Post(overview, head, body, "localhost", true, preservedate) < 0 )
{
fprintf(stderr, "newsd: Article not posted to %s: %s.\n",
groupname, group.Errmsg());
return(1);
}
// CC MESSAGE TO MAIL ADDRESS?
if ( group.IsCCPost() )
{
string from = "Anonymous",
subject = "-";
// HANDLE PRESERVING FIELDS FROM NNTP POSTING -> SMTP
string preserve;
int pflag = 0;
for ( unsigned t=0; t<head.size(); t++ )
{
char c = head[t].c_str()[0];
// CONTINUATION OF HEADER LINE?
if ( c == ' ' || c == 9 )
{
// CONTINUATION OF PREVIOUS PRESERVED HEADER LINE?
if ( pflag )
{ preserve += head[t]; preserve += "\n"; }
continue;
}
// ZERO OUT PRESERVE -- NO MORE CONTINUATIONS
pflag = 0;
// CHECK FOR PRESERVE FIELDS
if ( ISHEAD("From: ") || // must
ISHEAD("Subject: ") || // must
ISHEAD("References: ") || // needed to preserve threading
ISHEAD("Xref: ") || // ?
ISHEAD("Path: ") || // RFC 1036 2.1.6 (STR #15)
ISHEAD("Content-Type: ") || // mime related
ISHEAD("MIME-Version: ") || // mime related
ISHEAD("Message-ID: ") ) // needed to preserve threading
{
pflag = 1;
preserve += head[t];
preserve += "\n";
}
}
G_conf.LogMessage(L_DEBUG, "popen(%s,\"w\")..",
(const char*)G_conf.SendMail());
FILE *fp = popen(G_conf.SendMail(), "w");
if ( ! fp )
{
G_conf.LogMessage(L_ERROR,
"mailgateway: ccpost popen() can't execute '%s': %s",
(const char*)G_conf.SendMail(),
(const char*)strerror(errno));
}
else
{
fprintf(fp, "To: %s\n", (const char*)group.VoidEmail());
fprintf(fp, "Bcc: %s\n", (const char*)group.CCPost());
fprintf(fp, "%s", preserve.c_str());
// Reply-To: Needed for mail gateway
if ( group.IsReplyTo() )
fprintf(fp, "Reply-To: %s\n", (const char*)group.ReplyTo());
// Errors-To: advised so admin hears about problems, in addition
// to the real person who sent the message.
//
fprintf(fp, "Errors-To: %s\n", (const char*)group.Creator());
fprintf(fp, "\n");
fprintf(fp, "[posted to %s]\n\n", (const char*)group.Name());
for (unsigned t = 0; t < body.size(); t ++ )
fprintf(fp, "%s\n", body[t].c_str());
if (pclose(fp) < 0)
G_conf.LogMessage(L_ERROR,
"mailgateway: ccpost pclose() failed for '%s': %s",
(const char*)G_conf.SendMail(),
(const char*)strerror(errno));
}
}
return(0);
}
int main(int argc, const char *argv[])
{
// HANDLE SIGCHLD, IGNORE SIGPIPE + SIGALRM
signal(SIGCHLD, sigcld_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGALRM, SIG_IGN);
// umask(022); // Let the boot script determine this
Server server;
const char *conffile = CONFIG_FILE;
const char *mailgateway = NULL;
int newgroup = 0;
int dodebug = 0,
dofork = 1,
dorotate = 0,
preservedate = 0; // default: server rewrites date
// Scan command-line...
for (int t = 1; t < argc; t ++)
{
if (!strcmp(argv[t], "-c"))
{
if (++t >= argc)
{
fputs("newsd: Expected filename after \"-c\"!\n", stderr);
HelpAndExit();
}
conffile = argv[t];
}
else if (!strcmp(argv[t], "-d"))
{ dodebug = 1; dofork = 0; }
else if (!strcmp(argv[t], "-f"))
{ dofork = 0; }
else if (!strncmp(argv[t], "-h", 2))
{ HelpAndExit(); }
else if (!strcmp(argv[t], "-mailgateway"))
{
if (++t >= argc)
{
fputs("newsd: Expected groupname after \"-mailgateway\"!\n", stderr);
HelpAndExit();
}
mailgateway = argv[t];
dofork = 0;
}
else if (!strcmp(argv[t], "-preserve-date"))
{ preservedate = 1; }
else if (!strcmp(argv[t], "-newgroup"))
{ newgroup = 1; dofork = 0; }
else if (!strcmp(argv[t], "-rotate"))
{ dorotate = 1; dofork = 0; }
else
{ fprintf(stderr, "newsd: Unknown argument '%s'\n", argv[t]); HelpAndExit(); }
}
// Load global config data...
G_conf.Load(conffile);
if (dodebug)
{
G_conf.LogLevel(L_DEBUG);
G_conf.ErrorLog("stderr");
}
// Do stuff...
if (dorotate)
{
G_conf.InitLog(); // open log (it isn't yet)
G_conf.LogLock(); // lock while rotating
G_conf.Rotate(true); // force rotation
G_conf.LogUnlock();
exit(0);
}
else if (mailgateway)
{
if (RunAs()) return(1);
return(MailGateway(mailgateway, preservedate));
}
else if (newgroup)
{
// Make sure spool dir exists first
// Do this as root, since /var/spool is root owned
//
if ( CreateSpoolDir() == -1 ) return 1;
// Now become the news user..
if (RunAs()) return(1);
Group tmp;
return(tmp.NewGroup());
}
// Start logging...
G_conf.InitLog();
// Log server information...
G_conf.LogMessage(L_INFO, "-- newsd started - V%s--", VERSION);
G_conf.LogMessage(L_INFO, "-- start config summary --");
G_conf.LogSelf(L_INFO);
G_conf.LogMessage(L_INFO, "-- end config summary --");
if (server.Listen() < 0)
{
G_conf.LogMessage(L_ERROR, "Unable to listen for connections: %s",
server.Errmsg());
return(1);
}
// RUN AS THE USER 'NEWS'
// Now that we've opened the reserved port,
// we no longer need to be root.
//
if (RunAs())
return(1);
// Fork into the background...
if (dofork)
{
pid_t pid = fork();
switch ( pid )
{
case -1: // ERROR
G_conf.LogMessage(L_ERROR, "daemonize fork(): %s (exiting)",
strerror(errno));
return(1);
case 0: // CHILD
// "Daemonize" our application so it is no longer connected to
// the current terminal session...
close(0);
open("/dev/null", O_RDONLY);
close(1);
open("/dev/null", O_WRONLY);
close(2);
open("/dev/null", O_WRONLY);
if ( setsid() < 0 )
G_conf.LogMessage(L_ERROR, "setsid() failed (ignored): %s",
strerror(errno));
break;
default: // PARENT
return(0);
}
}
// ACCEPT NEW CONNECTIONS LOOP
for (;;)
{
ostringstream remote_msg;
if (server.Accept(remote_msg) < 0)
{
G_conf.LogMessage(L_ERROR, "Unable to accept new connection: %s",
server.Errmsg());
sleep(10);
continue;
}
// TOO MANY CHILDREN?
if (G_conf.MaxClients() != 0 && G_numclients >= G_conf.MaxClients())
{
server.Send("400 Server has too many connections open -- try again later");
close(server.MsgSock());
continue;
}
// FORK A CHILD TO HANDLE CONNECTION
// News readers can keep a connection open for the entire
// duration of the news reading session.
//
pid_t pid;
while ((pid = fork()) == -1)
{
G_conf.LogMessage(L_ERROR, "%s", remote_msg.str().c_str()); // remote info first
G_conf.LogMessage(L_ERROR, "Unable to fork handler process: %s", // fork error after
strerror(errno));
sleep(10);
}
G_numclients ++;
switch (pid)
{
case 0 : // CHILD
//G_conf.ErrorLog(G_conf.ErrorLog()); // 07/13/22: commented out - fork() already makes a copy separate from parent
G_conf.LogMessage(L_ERROR, "%s", remote_msg.str().c_str()); // show remote info AFTER fork(), to ensure pid of "Connection from" matches subsequent msgs
server.CommandLoop(overview);
exit(0);
default : // PARENT
close(server.MsgSock());
break;
}
}
//NOTREACHED
}