File: nntpd.c

package info (click to toggle)
leafnode 1.11.2.rel-1.0sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,284 kB
  • ctags: 572
  • sloc: ansic: 10,540; sh: 4,154; xml: 636; makefile: 266; perl: 84; sed: 4
file content (1872 lines) | stat: -rw-r--r-- 47,148 bytes parent folder | download
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
/*
nntpd -- the NNTP server

Written by Arnt Gulbrandsen <agulbra@troll.no> and copyright 1995
Troll Tech AS, Postboks 6133 Etterstad, 0602 Oslo, Norway, fax +47
22646949.
Modified by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>
and Randolf Skerka <Randolf.Skerka@gmx.de>.
Copyright of the modifications 1997.
Modified by Kent Robotti <robotti@erols.com>. Copyright of the
modifications 1998.
Modified by Markus Enzenberger <enz@cip.physik.uni-muenchen.de>.
Copyright of the modifications 1998.
Modified by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>
and Kazushi (Jam) Marukawa <jam@pobox.com>.
Copyright of the modifications 1998, 1999.
Modified by Matthias Andree <matthias.andree@web.de>
Copyright of the modifications 2000 - 2002.
Modified by Ralf Wildenhues <ralf.wildenhues@gmx.de>
Copyright of the modifications 2002.
Modified by Jonathan Larmour <jifl@jifvik.org>
Copyright of the modifications 2002.

See file COPYING for restrictions on the use of this software.
*/

#include "leafnode.h"
#include "masock.h"
#include "mastring.h"
#include "validatefqdn.h"
#include "strlcpy.h"
#include "ln_log.h"
#include "nntpd.h"

#ifdef SOCKS
#include <socks.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#ifndef __LCLINT__
#include <arpa/inet.h>
#endif
#include <ctype.h>
#include "system.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <syslog.h>
#include <unistd.h>
#include <utime.h>

#define MAXLINELENGTH 1000

#ifdef HAVE_IPV6
/*
 *  * local union struct
 */
union sockaddr_union {
    struct sockaddr sa;
    struct sockaddr_in sin;
    struct sockaddr_in6 sin6;
};
#endif

/*@null@*/ static struct newsgroup *group;	/* current group, initially none */
/*@null@*/ static struct newsgroup *xovergroup = NULL;
static unsigned long artno;		/* current article number */
/*@dependent@*/ /*@null@*/ static char *cmd;	/* current command line */
static time_t activetime;

int debug = 0;
int verbose = 0;		/* verbose doesn't count here */

static void
fatal_write(void)
{
    /*@observer@*/ static const char *const e = "Write error on stdout.";
    syslog(LOG_CRIT, "%s", e);
    fprintf(stderr, "%s\n", e);
    exit(EXIT_FAILURE);
}

static void
rereadactive(void)
{
    struct stat st;
    char s[SIZE_s+1];

    (void)xsnprintf(s, SIZE_s, "%s/leaf.node/groupinfo", spooldir);

    if ((!stat(s, &st) && (st.st_mtime > activetime)) || (active == NULL)) {
	char *grouptmp = NULL;
	if (debugmode)
	    syslog(LOG_DEBUG, "rereading %s", s);
	if (group) {
	    grouptmp = critstrdup(group->name, "rereadactive");
	}
	readactive();
	if (activesize == 0) 
	    fakeactive();
	else
	    activetime = st.st_mtime;
	if (grouptmp) {
	    group = findgroup(grouptmp);
	    xovergroup = NULL;
	    free(grouptmp);
	}
    }
}

/*
 * pseudo article stuff
 */

/* build and return an open fd to pseudoart in group */
static FILE *
buildpseudoart(const char *grp)
{
    FILE *f;
    int fd;

    mastr *n = mastr_new(PATH_MAX);
    (void)mastr_vcat(n, spooldir, "/temp.files/pseudo.XXXXXXXXXX", NULL);
    fd = safe_mkstemp(mastr_modifyable_str(n));
    if (fd < 0) {
	syslog(LOG_ERR, "Could not create pseudoarticle: mkstemp failed: %m");
	mastr_delete(n);
	return NULL;
    }

    (void)unlink(mastr_str(n));

    f = fdopen(fd, "w+b");
    if (f == NULL) {
	syslog(LOG_ERR, "Could not create pseudoarticle: fdopen failed: %m");
	(void)close(fd);
	mastr_delete(n);
	return NULL;
    }

    fprintf(f, "Path: %s\n", fqdn);
    fprintf(f, "Newsgroups: %s\n", grp);
    fprintf(f, "From: Leafnode <%s>\n", newsadmin);
    fprintf(f, "Subject: Leafnode placeholder for group %s\n", grp);
    fprintf(f, "Date: %s\n", rfctime());
    fprintf(f, "Message-ID: <leafnode:placeholder:%s@%s>\n", grp, fqdn);
    fprintf(f, "\n");
    fprintf(f,
	    "This server is running leafnode, which is a dynamic NNTP proxy.\n"
	    "This means that it does not retrieve newsgroups unless someone is\n"
	    "actively reading them.\n"
	    "\n"
	    "If you do an operation on a group - such as reading an article,\n"
	    "looking at the group table of contents or similar, then leafnode\n"
	    "will go and fetch articles from that group when it next updates.\n\n");
    fprintf(f,
	    "Since you have read this dummy article, leafnode will retrieve\n"
	    "the newsgroup %s when fetchnews is run\n"
	    "the next time. If you'll look into this group a little later, you\n"
	    "will see real articles.\n\n", grp);
    fprintf(f,
	    "If you see articles in groups you do not read, that is almost\n"
	    "always because of cross-posting.  These articles do not occupy any\n"
	    "more space - they are hard-linked into each newsgroup directory.\n"
	    "\n"
	    "If you do not understand this, please talk to your newsmaster.\n"
	    "\n"
	    "Leafnode can be found at\n" "\thttp://leafnode.sourceforge.net/\n\n");

    if (ferror(f)) {
	(void)fclose(f);
	f = NULL;
    } else {
	rewind(f);
    }
    mastr_delete(n);
    return f;
}

static int
parserange(char *arg, unsigned long *a, unsigned long *b)
{
    char *l = arg;
    /* no argument */
    if (!*l) return 0;

    /* parse */
    if (*l != '-')
	*a = strtoul(arg, &l, 10);
    SKIPLWS(l);
    if (*l == '-') {
	++l;
	SKIPLWS(l);
	if (isdigit((unsigned char)*l))
	    *b = strtoul(l, &l, 10);
    } else {
	*b = *a;
    }
    SKIPLWS(l);

    /* trailing garbage */
    if (l && *l) {
	return 0;
    }

    return 1;
}

static int
is_pseudogroup(/*@null@*/ const struct newsgroup *g)
{
    if (!g) return 0;
    if (!chdirgroup(g->name, FALSE)) return 1;
/*    if (isinteresting(g->name)) return 0; */
    if (g->last < g->first) return 1;
    if (g->last <= 1) return 1;
    return 0;
}

static void
markinterest(const char *ng)
{
    struct stat st;
    struct utimbuf buf;
    int err;
    time_t now;
    FILE *f;
    char s[SIZE_s+1];

    err = 0;
    (void)xsnprintf(s, SIZE_s, "%s/interesting.groups/%s", spooldir, ng);
    if (stat(s, &st) == 0) {
	now = time(NULL);
	buf.actime = (now < st.st_atime) ? st.st_atime : now;
	/* now < update may happen through HW failures */
	buf.modtime = st.st_mtime;
	if (utime(s, &buf)) {
	    err = 1;
	}
	if (debugmode)
	    syslog(LOG_DEBUG, "markinterest: %s touched", ng);
    } else {
	err = 1;
    }
    if (err) {
	f = fopen(s, "w");
	if (f == NULL || fclose(f) == EOF) {
	    syslog(LOG_ERR, "Could not create %s: %m", s);
	} else {
	    if (debugmode)
		syslog(LOG_DEBUG, "markinterest: %s new", ng);
	}
    } else {
	int fd = open(".", O_RDONLY);
	(void)chdirgroup(ng, FALSE);
	if (fd >= 0) {
	    (void)fchdir(fd);
	    (void)close(fd);
	}
    }
}

/* open a pseudo art */
/* WARNING: article_num MUST be 0 for selection based on Message-ID */
static FILE *
fopenpseudoart(const char *arg, const unsigned long article_num)
{
    FILE *f = NULL;
    char *c;
    struct newsgroup *g;

    if (group && (article_num && ((article_num == group->first &&
	    group->first >= group->last) || is_pseudogroup(group)))) {
	f = buildpseudoart(group->name);
    } else if (!article_num) {
	if (!strncmp(arg, "<leafnode:placeholder:", 22)) {
	    mastr *msgidbuf = mastr_new(1024);
	    (void)mastr_cpy(msgidbuf, arg + 22);
	    if ((c = strchr(mastr_modifyable_str(msgidbuf), '@')) != NULL) {
		*c++ = '\0';
		(void)strtok(c, ">");
		if (0 == strcasecmp(c, fqdn)) {
		    g = findgroup(mastr_str(msgidbuf));
		    if (g && (g->last <= 1 || g->first >= g->last)) {
			markinterest(g->name);
			f = buildpseudoart(g->name);
		    }
		}
	    }
	    mastr_delete(msgidbuf);
	}
    }
    return f;
}

/* open an article by number or message-id */
static FILE *
fopenart(const char *arg)
{
    unsigned long int a;
    FILE *f;
    char *t;
    struct stat st;
    /*@temp@*/ char buf[32];

    t = NULL;
    a = strtoul(arg, &t, 10);
    if (arg && *arg == '<') {
	/* message ID given */
	f = fopen(lookup(arg), "r");
	if (!f) f = fopenpseudoart(arg, 0);
    } else if (t && !*t && group != NULL) {
	const char *ptr = arg;
	/* number not given -> take current article pointer */
	if (!a) {
	    a = artno;
	    sprintf(buf, "%lu", artno);	/* RATS: ignore */
	    ptr = buf;
	}
	if (is_pseudogroup(group)) {
	    f = fopenpseudoart(ptr, a);
	} else {
	    f = fopen(ptr, "r");
	}
	if (f != NULL)
	    artno = a;
	markinterest(group->name);
    } else {
	f = NULL;
    }
    if (f != NULL && (fstat(fileno(f), &st) || st.st_size == 0)) {
	(void)fclose(f);
	f = NULL;
    }
    return f;
}


/*
 * Mark an article for download by appending its number to the
 * corresponding file in interesting.groups
 */
static int
markdownload(const char *ng, unsigned long id)
{
    int i, e = 0;
    unsigned long n;
    FILE *f;
    char *t;
    char s[SIZE_s+1];

    (void)xsnprintf(s, SIZE_s, "%s/interesting.groups/%s", spooldir, ng);
    if ((f = fopen(s, "r+"))) {
	i = 0;
	while ((t = getaline(f))) {
	    if (sscanf(t, "%lu", &n) == 1 && n == id) {
		(void)fclose(f);	/* we only read from the file */
		return 0;	/* already marked */
	    }
	    if (ferror(f))
		e = errno;
	    ++i;
	}
	if (i < BODY_DOWNLOAD_LIMIT) {
	    (void)fprintf(f, "%lu\n", id);
	    if (ferror(f))
		e = errno;
	    if (debugmode)
		syslog(LOG_DEBUG, "Marking %s %lu for download", ng, id);
	} else {
	    syslog(LOG_ERR, "Too many bodies marked in %s", ng);
	}
	if (fclose(f))
	    e = errno;
    }
    if (e) {
	syslog(LOG_ERR, "I/O error handling \"%s\": %s", s, strerror(e));
	return -1;
    }
    return 1;
}

static void
nogroup(void)
{
    printf("412 Use the GROUP command first\r\n");
    if (debugmode)
	syslog(LOG_DEBUG, ">412 Use the GROUP command first");
    return;
}

/* display an article or somesuch */
/* DOARTICLE */
static void
doarticle(const char *arg, int what)
{
    FILE *f;
    char *p = NULL;
    char *q = NULL;
    char *t;
    unsigned long localartno;
    unsigned long replyartno;
    char *localmsgid, *xref = NULL;
    char *markgroup = NULL;
    char s[SIZE_s+1];

    f = fopenart(arg);
    if (!f) {
	if (arg && *arg != '<' && !group) {
	    nogroup();
	} else if (strlen(arg)) {
	    printf("430 No such article: %s\r\n", arg);
	    if (debugmode)
		syslog(LOG_DEBUG, ">430 No such article: %s", arg);
	} else {
	    printf("423 No such article: %lu\r\n", artno);
	    if (debugmode)
		syslog(LOG_DEBUG, ">423 No such article: %lu", artno);
	}
	return;
    }

    if (!*arg) {
	    /* no. implicit */
	localartno = artno;
	localmsgid = fgetheader(f, "Message-ID:");
    } else if (*arg == '<') {
	    /* message-id -- do not modify artno */
	localartno = 0;
	localmsgid = critstrdup(arg, "doarticle");
    } else {
	    /* no. explicit */
	localartno = strtoul(arg, NULL, 10);
	localmsgid = fgetheader(f, "Message-ID:");
    }

    replyartno = localartno;
    if (!localartno) {
	/* we have to extract the article number and the newsgroup from
	 * the Xref: header */
	xref = fgetheader(f, "Xref:");
	p = xref;
	if (p) {
	    /* skip host name */
	    while (*p && !isspace((unsigned char)*p)) {
		p++;
	    }
	    while (isspace((unsigned char)*p)) {
		p++;
	    }
	}
	if (p) {
	    /* search article number of current group in Xref: */
	    if (group) {
		while ((q = strstr(p, group->name)) != NULL) {
		    q += strlen(group->name);
		    if (*q++ == ':') {
			localartno = strtoul(q, NULL, 10);
			markgroup = group->name;
			break;
		    }
		    p = q + strcspn(q, " \t");
		}
	    }
	    /* if we don't have a localartno, then we need to mark this
	     * article in a different news group */
	    if (!localartno) {
		char *r = p;
		while ((q = strchr(r, ':'))) {
		    *q++ = '\0';
		    if (isinteresting(p)) {
			/* got one we can mark */
			markgroup = r;
			localartno = strtoul(q, NULL, 10);
			break;
		    }
		    while (isdigit((unsigned char)*q)) {
			q++;
		    }
		    while (isspace((unsigned char)*q)) {
			q++;
		    }
		    r = q;
		}
	    }
	}
    } else if (group) {
	markgroup = group->name;
    }

    if (!localmsgid) {
	const char *tmp = "423 Corrupt article.";
	printf("%s\r\n", tmp);
	syslog(LOG_WARNING, ">%s", tmp);
	if (replyartno) {
	    (void)xsnprintf(s, SIZE_s, "%lu", replyartno);
	    (void)log_unlink(s, 0);
	}
	(void)fclose(f);
	if (xref)
	    free(xref);
	return;
    }
    (void)xsnprintf(s, SIZE_s - 24, "%3d %lu %s article retrieved - ",
		    223 - what, replyartno, localmsgid);
    free(localmsgid);

    if (what == 0)
	strcat(s, "request text separately");
    else if (what == 1)
	strcat(s, "body follows");
    else if (what == 2)
	strcat(s, "head follows");
    else
	strcat(s, "text follows");
    printf("%s\r\n", s);
    if (debugmode)
	syslog(LOG_DEBUG, ">%s", s);

    while ((t = getaline(f)) && *t) {
	if (what & 2) {
	    if (*t == '.')
		(void)fputc('.', stdout);
	    (void)fputs(t, stdout);
	    (void)fputs("\r\n", stdout);
	    if (ferror(stdout))
		fatal_write();
	}
    }
    /* Matthias Andree, 2002-03-05:
     * t == NULL or *t == '\0' makes a difference here.
     * -  t == NULL means we ran into EOF and don't have a body in delaybody mode.
     * -  t != NULL but *t == '\0' means we just read the empty separator line between header and body.
     */

    if (what == 3)
	printf("\r\n");		/* empty separator line */

    if (what & 1) {
	/* delaybody:
	   t == NULL: body is missing, mark for download
	   t != NULL: body is present */
	if (t == NULL) {
	    if (localartno && markgroup != NULL) {
		switch (markdownload(markgroup, localartno)) {
		case 0:
		    printf("\r\n\r\n"
			   "\t[ Leafnode: ]\r\n"
			   "\t[ This message has already been "
			   "marked for download. ]\r\n");
		    break;
		case 1:
		    printf("\r\n\r\n"
			   "\t[ Leafnode: ]\r\n"
			   "\t[ Message %lu of %s ]\r\n"
			   "\t[ has been marked for download. ]\r\n",
			   localartno, markgroup);
		    break;
		default:
		    printf("\r\n\r\n"
			   "\t[ Leafnode: ]\r\n"
			   "\t[ Message %lu of %s ]\r\n"
			   "\t[ cannot be marked for download. ]\r\n"
			   "\t[ (Check the server's syslog "
			   "for information). ]\r\n", localartno, markgroup);
		    break;
		}
	    } else {
		/* did not figure a group for which to mark this article */
		syslog(LOG_ERR,
		       "cannot mark for body download: arg=\"%s\" "
		       "localartno=%lu markgroup=\"%s\" group=\"%s\"",
		       arg, localartno, markgroup ? markgroup : "(null)",
		       group ? group->name : "(null)");
		printf("\r\n\r\n"
		       "\t[ Leafnode: ]\r\n"
		       "\t[ I cannot figure out a newsgroup for which to download ]\r\n"
		       "\t[ this article. Please report this condition to the ]\r\n"
		       "\t[ leafnode mailing list, with leafnode version, the name ]\r\n"
		       "\t[ and the version of your news reader and a log excerpt. ]\r\n");
	    }
	} else {		/* immediate body */
	    while ((t = getaline(f))) {
		if (*t == '.')
		    (void)fputc('.', stdout);
		(void)fputs(t, stdout);
		(void)fputs("\r\n", stdout);
		if (ferror(stdout))
		    fatal_write();
	    }
	}
    }
    if (what)
	printf(".\r\n");
    (void)fclose(f);

    if (xref)
	free(xref);

    return;			/* OF COURSE there were no errors */
}


/* change to group - note no checks on group name */
static int
dogroup(const char *arg)
{
    struct newsgroup *g;

    g = findgroup(arg);
    if (g) {
	group = g; /* global */
	if (isinteresting(arg)) {
	    if (debugmode)
		syslog(LOG_DEBUG, "marked group %s interesting", arg);
	    markinterest(arg);
	}
	if (!is_pseudogroup(g)) {
	    /* regular news group */
	    if (debugmode)
		syslog(LOG_DEBUG, ">211 %lu %lu %lu %s group selected",
		       g->last >= g->first ? g->last - g->first + 1 : 0,
		       g->first, g->last, g->name);
	    printf("211 %lu %lu %lu %s group selected\r\n",
		   g->last >= g->first ? g->last - g->first + 1 : 0,
		   g->first, g->last, g->name);
	} else {
	    /* pseudo news group */
	    if (debugmode)
		syslog(LOG_DEBUG,
		       ">211 %lu %lu %lu %s group selected (pseudo article)",
		       1lu, g->first, g->first, g->name);
	    printf("211 %lu %lu %lu %s group selected (pseudo article)\r\n",
		       1lu, g->first, g->first, g->name);
	}
	artno = g->first;
    } else {
	if (debugmode)
	    syslog(LOG_DEBUG, ">411 No such group");
	printf("411 No such group\r\n");
    }
    if (fflush(stdout)) return -1;
    return 0;
}

static void
dohelp(void)
{
    fputs("100 Legal commands on THIS server:\r\n"
	    " ARTICLE [<Message-ID>|<Number>]\r\n"
	    " BODY [<Message-ID>|<Number>]\r\n"
	    " DATE\r\n"
	    " GROUP <Newsgroup>\r\n"
	    " HDR <Header> <Message-ID>|<Range>\r\n"
	    " HEAD [<Message-ID>|<Number>]\r\n"
	    " HELP\r\n"
	    " LAST\r\n"
	    " LIST [ACTIVE|NEWSGROUPS] [<Wildmat>]]\r\n"
	    " LIST [ACTIVE.TIMES|EXTENSIONS|OVERVIEW.FMT]\r\n"
	    " LISTGROUP <Newsgroup>\r\n"
	    " MODE READER\r\n"
	    " NEWGROUPS <yymmdd> <hhmmss> [GMT]\r\n"
	    " NEXT\r\n"
	    " POST\r\n"
	    " OVER <Range>\r\n"
	    " SLAVE\r\n"
	    " STAT [<Message-ID>|<Number>]\r\n"
	    " XHDR <Header> <Message-ID>|<Range>\r\n"
	    " XOVER <Range>\r\n"
	    ".\r\n", stdout);
}

static void
domove(int by)
{
    char *msgid;
    char s[SIZE_s+1];

    by = (by < 0) ? -1 : 1;
    if (group) {
	if (artno) {
	    artno += by;
	    do {
		sprintf(s, "%lu", artno);
		msgid = getheader(s, "Message-ID:");
		if (!msgid)
		    artno += by;
	    } while (msgid == NULL && artno >= group->first && artno <= group->last);
	    if (msgid && (artno > group->last || artno < group->first)) {
		free(msgid);
		msgid = NULL;
	    }
	    if (msgid == NULL) {
		if (by > 0) {
		    artno = group->last;
		    printf("421 There is no next article\r\n");
		    if (debugmode)
			syslog(LOG_DEBUG, ">421 There is no next article");
		} else {
		    artno = group->first;
		    printf("422 There is no previous article\r\n");
		    if (debugmode)
			syslog(LOG_DEBUG, ">422 There is no previous article");
		}
	    } else {
		printf("223 %lu %s article retrieved\r\n", artno, msgid);
		if (debugmode)
		    syslog(LOG_DEBUG, ">223 %lu %s article retrieved",
			   artno, msgid);
		free(msgid);
	    }
	} else {
	    printf("420 There is no current article\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG, ">420 There is no current article");
	}
    } else {
	nogroup();
    }
}

static int is_pattern(const char *s) {
    return s ? strcspn(s, "?*[") < strlen(s) : 1;
}

/* LIST ACTIVE if what==0,
 * LIST NEWSGROUPS if what==1
 * LIST ACTIVE.TIMES if what==2
 */
static void
printlist(const struct newsgroup *g, const int what)
{
    switch(what) {
	case 0:
	    if (is_pseudogroup(g))
		printf("%s %lu %lu y\r\n", g->name, g->first, g->first);
	    else
		printf("%s %lu %lu y\r\n", g->name, g->last, g->first);
	    break;
	case 1:
	    printf("%s\t%s\r\n", g->name, g->desc ? g->desc : "-x-");
	    break;
	case 2:
	    printf("%s %lu %s\r\n", g->name, (unsigned long)g->age, newsadmin);
	    break;
	default:
	    abort();
	    break;
    }
}

/* LIST ACTIVE if what==0,
 * LIST NEWSGROUPS if what==1
 * LIST ACTIVE.TIMES if what==2
 */
static void
list(struct newsgroup *g, const int what, const char *pattern)
{
    if (is_pattern(pattern)) {
	while(g->name) {
	    if (!pattern || !ngmatch(pattern, g->name)) {
		printlist(g, what);
	    }
	    g++;
	}
    } else {
	/* single group */
	g = findgroup(pattern);
	if (g) {
	    printlist(g, what);
	    if (what == 0 && isinteresting(pattern))
		markinterest(pattern);
	}
    }
}

static void
dolist(char *arg)
{
    if (!strcasecmp(arg, "extensions")) {
	printf("202 extensions supported follow\r\n"
		"HDR\r\n"
		"LISTGROUP\r\n"
		"OVER\r\n"
		"XHDR\r\n"
		"XOVER\r\n"
		".\r\n");
	if (debugmode)
	    syslog(LOG_DEBUG, ">202 extensions supported follow");
    } else if (!strcasecmp(arg, "overview.fmt")) {
	printf("215 information follows\r\n"
		"Subject:\r\n"
		"From:\r\n"
		"Date:\r\n"
		"Message-ID:\r\n"
		"References:\r\n"
		"Bytes:\r\n"
		"Lines:\r\n"
		"Xref:full\r\n"
		".\r\n");
	if (debugmode)
	    syslog(LOG_DEBUG, ">215 information follows");
    } else if (!strncasecmp(arg, "active.times", 12)) {
	if (active) {
	    const char m[] = "215 Note that leafnode will fetch groups on demand.";
		printf("%s\r\n", m);
	    if (debugmode)
		syslog(LOG_DEBUG, ">%s", m);
	    list(active, 2, NULL);
	    printf(".\r\n");
	} else {
	    const char e[] = "503 Active file has not been read.";
	    printf("%s\r\n", e);
	    if (debugmode)
		syslog(LOG_DEBUG, ">%s", e);
	}
    } else {
	if (!active) {
	    printf("503 Group information file does not exist!\r\n");
	    syslog(LOG_ERR, ">503 Group information file does not exist!");
	} else if (!*arg || !strncasecmp(arg, "active", 6)) {
	    printf("215 Newsgroups in form \"group high low flags\".\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG,
			">215 Newsgroups in form \"group high low flags\".");
	    if (active) {
		if (!*arg || strlen(arg) == 6)
		    list(active, 0, NULL);
		else {
		    while (*arg && (!isspace((unsigned char)*arg)))
			arg++;
		    while (*arg && isspace((unsigned char)*arg))
			arg++;
		    list(active, 0, arg);
		}
	    }
	    printf(".\r\n");
	} else if (!strncasecmp(arg, "newsgroups", 10)) {
	    printf("215 Descriptions in form \"group description\".\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG,
			">215 Descriptions in form \"group description\".");
	    if (active) {
		if (strlen(arg) == 10)
		    list(active, 1, NULL);
		else {
		    while (*arg && (!isspace((unsigned char)*arg)))
			arg++;
		    while (*arg && isspace((unsigned char)*arg))
			arg++;
		    list(active, 1, arg);
		}
	    }
	    printf(".\r\n");
	} else {
	    printf("503 Syntax error\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG, ">503 Syntax error");
	}
    }
}

static void
donewgroups(const char *arg)
{
    struct tm timearray;
    struct tm *ltime;
    time_t age;
    time_t now;
    int year, century;
    char *l;
    long a;
    long b;
    struct newsgroup *ng;

    now = time(NULL);
    ltime = localtime(&now);
    if (ltime == NULL) {
	syslog(LOG_CRIT, "fatal: localtime returned NULL. abort.");
	abort();
    }
    year = ltime->tm_year % 100;
    century = ltime->tm_year / 100;	/* 0 for 1900-1999, 1 for 2000-2099 etc */

    memset(&timearray, 0, sizeof(timearray));
    l = NULL;
    a = (int)strtol(arg, &l, 10);
    /* NEWGROUPS may have the form YYMMDD or YYYYMMDD.
       Distinguish between the two */
    b = a / 10000;
    if (b < 100) {
	/* YYMMDD */
	if (b <= year)
	    timearray.tm_year = (int)(b + (century * 100));
	else
	    timearray.tm_year = (int)(b + (century - 1) * 100);
    } else if (b < 1000) {
	/* YYYMMDD - happens with buggy newsreaders */
	/* In these readers, YYY=100 is equivalent to YY=00 or YYYY=2000 */
	syslog(LOG_NOTICE,
	       "NEWGROUPS year is %ld: please update your newsreader", b);
	timearray.tm_year = (int)b;
    } else {
	/* YYYYMMDD */
	timearray.tm_year = (int)b - 1900;
    }
    timearray.tm_mon = (int)(a % 10000 / 100) - 1;
    timearray.tm_mday = (int)(a % 100);
    while (*l && isspace((unsigned char)*l))
	l++;
    a = strtol(l, &l, 10);	/* we don't care about the rest of the line */
    while (*l && isspace((unsigned char)*l))
	l++;
    timearray.tm_hour = (int)(a / 10000);
    timearray.tm_min = (int)(a % 10000 / 100);
    timearray.tm_sec = (int)(a % 100);
    /* mktime() shall guess correct value of tm_isdst (0 or 1) */
    timearray.tm_isdst = -1;
    if (0 == strncasecmp(l, "gmt", 3))
	age = timegm(&timearray);
    else
	age = mktime(&timearray);

    printf("231 List of new newsgroups since %ld follows\r\n", (long)age);
    if (debugmode)
	syslog(LOG_DEBUG, "231 List of new newsgroups since %ld follows",
	       (long)age);

    ng = active;
    if (ng != NULL) 
	while (ng->name) {
	    if (ng->age >= age)
		printf("%s %lu %lu y\r\n", ng->name, ng->last, ng->first);
	    ng++;
	}
    printf(".\r\n");
}

/* next bit is copied from INN 1.4 and modified ("broken") by agulbra

   mail to Rich $alz <rsalz@uunet.uu.net> bounced */

/* Scale time back a bit, for shorter Message-ID's. */
#define OFFSET	(time_t)1026380000L

/*@observer@*/ static char *
generateMessageID(void)
{
    static char ALPHABET[] = "0123456789abcdefghijklmnopqrstuv";

    static char buff[1000];
    static time_t then;
    static unsigned int fudge;
    time_t now;
    char *p;
    unsigned long n;

    now = time(NULL);		/* might be 0, in which case fudge
				   will almost fix it */
    if (now < OFFSET) {
	ln_log(LNLOG_SCRIT, LNLOG_CTOP,
		"your system clock cannot be right. abort.");
	abort();
    }
    if (now != then)
	fudge = 0;
    else
	fudge++;
    then = now;

    p = buff;
    *p++ = '<';
    n = (unsigned long)now - OFFSET;
    while (n) {
	*p++ = ALPHABET[(int)(n & 31)];
	n >>= 5;
    }
    *p++ = '-';
    n = fudge * 32768 + (int)getpid();
    while (n) {
	*p++ = ALPHABET[(int)(n & 31)];
	n >>= 5;
    }
    sprintf(p, ".ln1@%-.256s>", fqdn);
    return buff;
}
/* the end of what I stole from rsalz and then mangled */


static int
dopost(void)
{
    char *line;
    int havefrom = 0;
    int havepath = 0;
    int havedate = 0;
    int havenewsgroups = 0;
    int havemessageid = 0;
    int havesubject = 0;
    int err = 0, ferr = 0;
    /*@observer@*/ const char *ferrstr = NULL;
    int o;
    size_t len;
    FILE *out;
    char outname[1000];
    static int postingno;	/* starts as 0 */
    char *suggmid;
    /*@observer@*/ const char *appendheader = NULL;

    if (getenv("LN_REJECT_POST_PRE")) {
	printf("400 Posting rejected - debug variable LN_REJECT_POST_PRE exists\r\n");
	return 0;
    }

    do {
	(void)xsnprintf(outname, sizeof(outname), "%s/out.going/%d-%d-%d",
		spooldir, (int)getpid(), (int)time(NULL), ++postingno);

	o = open(outname, O_WRONLY | O_EXCL | O_CREAT, 0244);
	if (o < 0 && errno != EEXIST) {
	    char *errmsg = strerror(errno);
	    printf("441 Unable to open spool file %s: %s\r\n", outname, errmsg);
	    syslog(LOG_ERR, ">441 Unable to open spool file %s: %s", outname,
		    errmsg);
	    return 0;
	}
    } while (o < 0);

    out = fdopen(o, "w");
    if (out == NULL) {
	char *errmsg = strerror(errno);
	printf("441 Unable to fdopen(%d): %s\r\n", o, errmsg);
	syslog(LOG_ERR, ">441 Unable to fdopen(%d): %s", o, errmsg);
	return 0;
    }

    suggmid = generateMessageID();
    printf("340 Ok, recommended ID %s\r\n", suggmid);
    if (debugmode)
	syslog(LOG_DEBUG, ">340 Go ahead.");
    if (fflush(stdout)) return -1;

    /* get headers */
    do {
	debug = 0;
	line = getaline(stdin);
	if (!line) { /* timeout */
            unlink(outname);
	    exit(0);
	}

	if (0 == strcmp(line, ".")) {
	    ferr = TRUE;
	    ferrstr = "No body found.";
	    break;
	}
	debug = debugmode;

	if (!strncasecmp(line, "From: ", 6)) {
	    if (havefrom)
		ferr = TRUE, ferrstr = "Duplicate From: header";
	    else
		havefrom = TRUE;
	}
	if (!strncasecmp(line, "Path: ", 6)) {
	    if (havepath)
		ferr = TRUE, ferrstr = "Duplicate Path: header";
	    else
		havepath = TRUE;
	}
	if (!strncasecmp(line, "Message-ID: ", 12)) {
	    if (havemessageid)
		ferr = TRUE, ferrstr = "Duplicate Message-ID: header";
	    else {
		char *vec[2]; /* RATS: ignore */
		int rc;
		
		havemessageid = TRUE;
		if (debugmode)
		    syslog(LOG_DEBUG, "debug header: %s", line);
		if (2 != (rc = pcre_extract(line, "Message-ID:\\s+<(?:[^>]+)@([^@>]+)>\\s*$", vec, 2))
			|| vec[1] == NULL) {
		    ferr = TRUE, ferrstr = "Malformatted Message-ID: header.";
		} else if (!strchr(vec[1], '.')) {
		    ferr = TRUE, ferrstr = "Message-ID: header does not have domain name part.";
		} else if (!is_validfqdn(vec[1])) {
		    ferr = TRUE, ferrstr = "Message-ID: header contains invalid domain name part.";
		}
		pcre_extract_free(vec, rc);
	    }
	}
	if (!strncasecmp(line, "Subject: ", 9)) {
	    if (havesubject)
		ferr = TRUE, ferrstr = "Duplicate Subject: header";
	    else
		havesubject = TRUE;
	}
	if (!strncasecmp(line, "Newsgroups: ", 12)) {
	    if (havenewsgroups)
		ferr = TRUE, ferrstr = "Duplicate Newsgroups: header";
	    else
		havenewsgroups = TRUE;
	}
	if (!strncasecmp(line, "Date: ", 6)) {
	    if (havedate)
		ferr = TRUE, ferrstr = "Duplicate Date: header";
	    else
		havedate = TRUE;
	}

	len = strlen(line);

	/* check for illegal 8bit/control stuff in header */
	{
	    char *t;
	    for (t = line; *t; t++) {
		if (*t & 0x80) {
		    if (allow_8bit_headers) {
			appendheader = "X-Leafnode-Warning: administrator "
			    "allowed illegal use of 8-bit data in header.\r\n";
		    } else {
			ferr = TRUE;
			ferrstr = "Illegal use of 8-bit data in header.";
			break;
		    }
		}
		if ((unsigned char)*t < (unsigned char)0x20u && *t != '\t') {
		    ferr = TRUE;
		    ferrstr = "Illegal use of control data in header.";
		    break;
		}
	    }
	}

	/* checks for non-folded lines */
	if (*line && *line != ' ' && *line != '\t') {
	    if (strchr(line, ':') == NULL) {
		/* must have a colon */
		ferr = TRUE;
		ferrstr = "Header tag not found.";
	    } else if (strcspn(line, " \t") < strcspn(line, ":")) {
		/* must not have space before colon */
		ferr = TRUE;
		ferrstr = "Whitespace in header tag is not allowed.";
	    }
	}

	if (len) {
	    if (fwrite(line, 1, len, out) != (size_t) len)
		err = 1;
	} else {
	    if (!havepath) {
		if (fputs("Path: ", out) == EOF)
		    err = 1;
		if (fputs(fqdn, out) == EOF)
		    err = 1;
		if (fprintf(out, "!%s\r\n", NEWS_USER) < 0)
		    err = 1;
	    }
	    if (!havedate) {
		const char *l = rfctime();
		if (fputs("Date: ", out) == EOF)
		    err = 1;
		if (fputs(l, out) == EOF)
		    err = 1;
		if (fputs("\r\n", out) == EOF)
		    err = 1;
	    }
	    if (!havemessageid) {
		if (fputs("Message-ID: ", out) == EOF)
		    err = 1;
		if (fputs(suggmid, out) == EOF)
		    err = 1;
		if (fputs("\r\n", out) == EOF)
		    err = 1;
	    }
	    if (appendheader) {
		if (fputs(appendheader, out) == EOF)
		    err = 1;
	    }
	}
	if (fputs("\r\n", out) == EOF)
	    err = 1;
    } while (*line);

    /* get bodies */
    if (strcmp(line, "."))
	do {
	    debug = 0;
	    line = getaline(stdin);
	    debug = debugmode;
	    if (!line) {
		(void)unlink(outname);
		exit(1);
	    }

	    len = strlen(line);
	    if (line[0] == '.') {
		if (len > 1) {
		    if (fputs(line + 1, out) == EOF)
			err = 1;
		    if (fputs("\r\n", out) == EOF)
			err = 1;
		}
	    } else {
		if (fputs(line, out) == EOF)
		    err = 1;
		if (fputs("\r\n", out) == EOF)
		    err = 1;
	    }
	} while (line[0] != '.' || line[1] != '\0');

    if (fflush(out))
	err = 1;
    
    if (fsync(fileno(out)))
	err = 1;

    if (fclose(out))
	err = 1;

    if (!havenewsgroups)
	ferrstr = "Missing Newsgroups: header";
    if (!havesubject)
	ferrstr = "Missing Subject: header";
    if (!havefrom)
	ferrstr = "Missing From: header";

    if (getenv("LN_REJECT_POST_POST"))
	ferr = 1;

    if (havefrom && havesubject && havenewsgroups && !ferr) {
	if (!err && 0 == chmod(outname, 0644)) {
	    printf("240 Article posted, now be patient\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG, ">240 Article posted, now be patient");
	    return 0;
	} else {
	    (void)unlink(outname);
	    printf("441 I/O error, article not posted\r\n");
	    syslog(LOG_INFO, ">441 I/O error, article not posted");
	    return 0;
	}
    }

    (void)unlink(outname);

    if (getenv("LN_REJECT_POST_POST")) {
	printf("400 Posting rejected - debug variable LN_REJECT_POST_POST exists\r\n");
	syslog(LOG_INFO, ">400 Posting rejected - debug variable LN_REJECT_POST_POST exists\r\n");
	return 0;
    }

    if (ferrstr) {
	printf("441 Post rejected, formatting error: %s\r\n", ferrstr);
	syslog(LOG_INFO, ">441 Post rejected, formatting error: %s", ferrstr);
    } else {
	printf("441 Post rejected, formatting error\r\n");
	syslog(LOG_INFO, ">441 Post rejected, formatting error");
    }

    return 0;
}

static void invalidrange(void)
{
    printf("420 No articles in specified range.\r\n");
    if (debugmode)
	syslog(LOG_DEBUG, ">420 No articles in specified range.");
}

/* check if a - b is a valid range for the current group.
 * If it's not, print a 420 error and return 0.
 * If it is, do not print anything and return 1.
 * group must not be NULL!
 */
static int checkrange(const struct newsgroup *g,
	unsigned long a, unsigned long b)
{
    if ((a > b) || (g->first <= g->last
		? (a > g->last) || (b < g->first)
		: (a > g->first) || (b < g->first))) {
	invalidrange();
	return 0;
    }
    return 1;
}



static void
doxhdr(char *arg)
{
    static const char *h[] = { "Subject", "From", "Date", "Message-ID",
	"References", "Bytes", "Lines"
    };

    int n = 7;
    size_t i;
    char *l;
    char *buf;
    unsigned long a, b = 0, c;
    char s[SIZE_s+1];

    if (!arg || !*arg) {
	if (debugmode)
	    syslog(LOG_DEBUG,
		   ">502 Usage: HDR header first[-last] or "
		   "HDR header message-id");
	printf("502 Usage: HDR header first[-last] or "
	       "HDR header message-id\r\n");
	return;
    }

    /* go figure header */
    l = arg;
    while (l && *l && !isspace((unsigned char)*l))
	l++;
    if (l && *l)
	*l++ = '\0';
    SKIPLWS(l);

    buf = critmalloc((i = strlen(arg)) + 2, "doxhdr");
    strcpy(buf, arg); /* RATS: ignore */
    if (buf[i - 1] != ':')
	strcpy(buf + i, ":");

    if (l && *l == '<') {	/* handle message-id form (well) */
	FILE *f;
	char *m = critstrdup(l, "doxhdr");
	f = fopenart(l);
	if (!f) {
	    printf("430 No such article\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG, ">430 No such article");
	    free(buf);
	    free(m);
	    return;
	}
	l = fgetheader(f, buf);
	if (debugmode) {
	    syslog(LOG_DEBUG, ">221 %s header of %s follows:", buf, m);
	    if (l) syslog(LOG_DEBUG, ">%s %s", m, l);
	    syslog(LOG_DEBUG, ">.");
	}
	printf("221 %s header of %s follows:\r\n", buf, m);
	if (l) printf("%s %s\r\n", m, l);
	printf(".\r\n");
	free(m);
	(void)fclose(f);
	free(buf);
	if (l) free(l);
	return;
    }

    if (!group) {
	nogroup();
	free(buf);
	return;
    }

    markinterest(group->name);

    a = group->first;
    b = group->last;
    if (b < a) b = a;
    if (!parserange(l, &a, &b)) {
	if (debugmode)
	    syslog(LOG_DEBUG, ">502 Usage: XHDR header first[-last] "
		    "or XHDR header message-id");
	printf("502 Usage: XHDR header first[-last] "
		"or XHDR header message-id\r\n");
	free(buf);
	return;
    }

    if (!checkrange(group, a, b)) {
	free(buf);
	return;
    }

    if (!is_pseudogroup(group)) {
	    if (xovergroup != group && chdirgroup(group->name, FALSE))
		    if (getxover())
			    xovergroup = group;
    }

    if (is_pseudogroup(group)) {
	do {
	    n--;
	} while (n >= 0 && strncasecmp(h[n], buf, strlen(h[n])) != 0);
	if ((n < 0) && strncasecmp("Newsgroups", buf, 10)) {
	    printf("430 No such header: %s\r\n", buf);
	    if (debugmode)
		syslog(LOG_DEBUG, ">430 No such header: %s", buf);
	    free(buf);
	    return;
	}
	if (debugmode)
	    syslog(LOG_DEBUG,
		    ">221 First line of %s pseudo-header follows:", buf);
	printf("221 First line of %s pseudo-header follows:\r\n", buf);
	if (a <= b && a <= group->first && b >= group->last) {
	    printf("%lu ", group->first);
	    if (n == 0)		/* Subject: */
		printf("Leafnode placeholder for group %s\r\n", group->name);
	    else if (n == 1)	/* From: */
		printf("Leafnode <%s>\r\n", newsadmin);
	    else if (n == 2)	/* Date: */
		printf("%s\r\n", rfctime());
	    else if (n == 3)	/* Message-ID: */
		printf("<leafnode:placeholder:%s@%s>\r\n", group->name, fqdn);
	    else if (n == 4)	/* References */
		printf("(none)\r\n");
	    else if (n == 5)	/* Bytes */
		printf("%d\r\n", 1024);	/* FIXME: just a guess */
	    else if (n == 6)	/* Lines */
		printf("%d\r\n", 22);	/* FIXME: from buildpseudoart() */
	    else			/* Newsgroups */
		printf("%s\r\n", group->name);
	}
	printf(".\r\n");
	free(buf);
	return;
    }

    do {
	n--;
    } while (n > -1 && strncasecmp(buf, h[n], strlen(h[n])));

    if (a < group->first)
	a = group->first;

    if (b > group->last)
	b = group->last;

    if (n >= 0) {
	if (debugmode)
	    syslog(LOG_DEBUG, "221 %s header (from overview) "
		   "for postings %lu-%lu:", h[n], a, b);
	printf("221 %s header (from overview) for postings %lu-%lu:\r\n",
	       h[n], a, b);

	s[sizeof(s)-1] = '\0';
	for (c = a; c <= b; c++) {
	    if (xoverinfo &&
		c >= xfirst && c <= xlast && xoverinfo[c - xfirst].text) {
		char *l2 = xoverinfo[c - xfirst].text;
		int d;
		for (d = 0; l2 && d <= n; d++)
		    l2 = strchr(l2 + 1, '\t');
		if (l2) {
		    char *p;
		    (void)strlcpy(s, ++l2, sizeof(s));
		    p = strchr(s, '\t');
		    if (p)
			*p = '\0';
		}
		if (l2 && *l2) printf("%lu %s\r\n", c, s);
	    }
	}
    } else {
	if (debugmode)
	    syslog(LOG_DEBUG, ">221 %s header (from article files) "
		   "for postings %lu-%lu:", buf, a, b);
	printf("221 %s header (from article files) for postings %lu-%lu:\r\n",
	       buf, a, b);
	for (c = a; c <= b; c++) {
	    sprintf(s, "%lu", c);
	    l = getheader(s, buf);
	    if (l) {
		printf("%lu %s\r\n", c, l);	/* (l && *l) ? l : "(none)" ); */
		free(l);
	    }
	}
    }

    free(buf);
    printf(".\r\n");
    return;
}

static void
doxover(char *arg)
{
    unsigned long a, b, art;

    if (!group) {
	nogroup();
	return;
    }

    markinterest(group->name);
    a = group->first;
    b = group->last;
    if (b < a) b = a;

    if (!arg || !*arg)
	a = b = artno;
    else if (!parserange(arg, &a, &b)) {
	printf("502 Usage: OVER first[-[last]]\r\n");
	if (debugmode)
	    syslog(LOG_DEBUG, ">502 Usage: OVER first[-[last]]");
	return;
    }

    if (!checkrange(group, a, b))
	return;

    if (!is_pseudogroup(group)) {
	if (xovergroup != group && chdirgroup(group->name, FALSE))
	    if (getxover()) xovergroup = group;

	if (NULL == xoverinfo) {
	    invalidrange();
	    return;
	}
	if (b > xlast)
	    b = xlast;
	if (a < xfirst)
	    a = xfirst;

	printf("224 Overview information for postings %lu-%lu:\r\n", a, b);
	if (debugmode)
	    syslog(LOG_DEBUG, ">224 Overview information for postings %lu-%lu:",
		   a, b);
	for (art = a; art <= b; art++) {
	    if (xoverinfo[art - xfirst].text)
		printf("%s\r\n", xoverinfo[art - xfirst].text);
	}
	printf(".\r\n");
    } else {
	if ((a > b) || (group->first <= group->last
		? (a > group->last) || (b < group->first)
		: (a > group->first) || (b < group->first))) {
	    printf("420 No articles in specified range.\r\n");
	    if (debugmode)
		syslog(LOG_DEBUG, ">420 No articles in specified range.");
	    return;
	}

	printf("224 Overview information (pseudo) for postings %lu-%lu:\r\n", 
		group->first, group->first);
	if (debugmode)
	    syslog(LOG_DEBUG, ">224 Overview information (pseudo) for "
		   "postings %lu-%lu:", group->first, group->first);
	printf("%lu\t"
	       "Leafnode placeholder for group %s\t"
	       "%s (Leafnode)\t%s\t"
	       "<leafnode:placeholder:%s@%s>\t\t1000\t40\r\n", group->first,
	       group->name, newsadmin, rfctime(), group->name, fqdn);
	printf(".\r\n");
	if (debugmode)
	    syslog(LOG_DEBUG, ">%lu\tLeafnode placeholder for group %s\t"
		   "%s (Leafnode)\t%s\t<leafnode:placeholder:%s@%s>\t\t1000\t40",
		   group->first, group->name, newsadmin, rfctime(), group->name, fqdn);
    }
}

static void
dolistgroup(const char *arg)
{
    unsigned long art;

    if (arg && *(arg)) {
	struct newsgroup *g;
	g = findgroup(arg);
	if (g) {
	    group = g;
	    artno = g->first;
	} else  {
	    printf("411 No such group: %s\r\n", arg);
	    if (debugmode)
		syslog(LOG_DEBUG, ">411 No such group: %s", arg);
	    return;
	}
    }

    if (!group) {
	nogroup();
	return;
    }

    /* group = g; */
    markinterest(group->name);
    if ((NULL == xovergroup || xovergroup != group)
	    && chdirgroup(group->name, FALSE))
	    if (getxover()) xovergroup = group;

    if (is_pseudogroup(group)) {
	printf("211 Article list for %s follows (pseudo)\r\n", group->name);
	if (debugmode)
	    syslog(LOG_DEBUG,
		   ">211 Article list for %s follows (pseudo)", group->name);
	printf("%lu\r\n", group->first ? group->first : 1);
    } else {
	printf("211 Article list for %s follows\r\n", group->name);
	if (debugmode)
	    syslog(LOG_DEBUG, ">211 Article list for %s follows", group->name);
	if (xoverinfo)
	    for (art = xfirst; art <= xlast; art++) {
		if (xoverinfo[art - xfirst].text)
		    printf("%lu\r\n", art);
	    }
    }
    printf(".\r\n");
}

static void
parser(void)
{
    char *arg;
    int n;
    size_t size;

    mgetaline_settimeout(timeout_client);

    while ((cmd = mgetaline(stdin))) {
	if (debug == 1)
	    syslog(LOG_DEBUG, "<%s", cmd);

	size = strlen(cmd);
	if (size == 0)
	    continue;		/* ignore */
	if (size > MAXLINELENGTH || (long)size > (long)INT_MAX) {
	    /* ignore attempts at buffer overflow */
	    if (debugmode)
		syslog(LOG_DEBUG, ">500 Dazed and confused");
	    printf("500 Dazed and confused\r\n");
	    continue;
	}

	/* parse command line */
	n = 0;
	while (isalpha((unsigned char)cmd[n]))
	    n++;
	while (isspace((unsigned char)cmd[n]))
	    cmd[n++] = '\0';

	arg = cmd + n;

	while (cmd[n])
	    n++;
	n--;
	while (n >= 0 && isspace((unsigned char)cmd[n]))
	    cmd[n--] = '\0';

	if (!strcasecmp(cmd, "quit")) {
	    if (debugmode)
		syslog(LOG_DEBUG, ">205 Always happy to serve!");
	    printf("205 Always happy to serve!\r\n");
	    return;
	}
	rereadactive();
	if (!strcasecmp(cmd, "article")) {
	    doarticle(arg, 3);
	} else if (!strcasecmp(cmd, "head")) {
	    doarticle(arg, 2);
	} else if (!strcasecmp(cmd, "body")) {
	    doarticle(arg, 1);
	} else if (!strcasecmp(cmd, "stat")) {
	    doarticle(arg, 0);
	} else if (!strcasecmp(cmd, "help")) {
	    dohelp();
	} else if (!strcasecmp(cmd, "last")) {
	    domove(-1);
	} else if (!strcasecmp(cmd, "next")) {
	    domove(1);
	} else if (!strcasecmp(cmd, "list")) {
	    dolist(arg);
	} else if (!strcasecmp(cmd, "date")) {
	    dodate();
	} else if (!strcasecmp(cmd, "mode")) {
	    if (debugmode)
		syslog(LOG_DEBUG, ">200 Leafnode %s, pleased to meet you!",
		       version);
	    printf("200 Leafnode %s, pleased to meet you!\r\n", version);
	} else if (!strcasecmp(cmd, "newgroups")) {
	    donewgroups(arg);
	} else if (!strcasecmp(cmd, "newnews")) {
	    if (debugmode)
		syslog(LOG_DEBUG,
		       ">500 NEWNEWS is meaningless for this server");
	    printf("500 NEWNEWS is meaningless for this server\r\n");
	} else if (!strcasecmp(cmd, "post")) {
	    if (dopost()) break;
	} else if (!strcasecmp(cmd, "slave")) {
	    if (debugmode)
		syslog(LOG_DEBUG, ">202 Cool - I always wanted a slave");
	    printf("202 Cool - I always wanted a slave\r\n");
	} else if (!strcasecmp(cmd, "xhdr")) {
	    doxhdr(arg);
	} else if (!strcasecmp(cmd, "hdr")) {
	    doxhdr(arg);
	} else if (!strcasecmp(cmd, "xover")) {
	    doxover(arg);
	} else if (!strcasecmp(cmd, "over")) {
	    doxover(arg);
	} else if (!strcasecmp(cmd, "listgroup")) {
	    dolistgroup(arg);
	} else if (!strcasecmp(cmd, "group")) {
	    if (dogroup(arg)) break;
	} else {
	    if (debugmode)
		syslog(LOG_DEBUG, ">500 Unknown command");
	    printf("500 Unknown command\r\n");
	}
	if (ferror(stdout) || fflush(stdout)) {
	    syslog(LOG_ERR, "Cannot write to client.");
	    break;
	}
    }
    if (debugmode)
	syslog(LOG_DEBUG, "Client timeout, disconnecting.");

    /* There was once a 400 error message here. It confused broken
     * clients, most notably, tin.
     * Future NNTP drafts command that we don't send stuff back on
     * timeout, so we anticipate these. */
}

int
main(int argc, char **argv)
{
    socklen_t fodder;
    char peername[256]; /* RATS: ignore */
#ifdef HAVE_IPV6
    char *st;
    int h_err;
#define ADDRLEN INET6_ADDRSTRLEN
    union sockaddr_union su;
#else
    struct hostent *he;
#ifdef INET_ADDRSTRLEN
#define ADDRLEN INET_ADDRSTRLEN
#else
#define ADDRLEN 16
#endif
    struct sockaddr_in sa;
#endif
    char peerip[ADDRLEN]; /* RATS: ignore */
    char ownip[ADDRLEN]; /* RATS: ignore */
    char origfqdn[FQDNLEN + 1]; /* RATS: ignore */

    ln_log_use_console(0); /* disable console logging */
    (void)argc;	/* quiet compiler warning */
    myopenlog("leafnode");

    /* this gets the actual hostname */
    if (!initvars(argv[0]))
	exit(1);

    artno = 0;
    verbose = 0;
    (void)umask(2);

    /* this reads the host name from the config file */
    if (!readconfig(1)) {
	const char *m = "503 Unable to read configuration file, exiting; the server's syslog should have more information.";
	printf("%s\r\n", m);
	syslog(LOG_ERR, "%s", m);
	exit(1);
    }
    freeservers();

    strcpy(origfqdn, fqdn); /* same size buffer */ /* RATS: ignore */

    /* get own name */
#ifdef HAVE_IPV6
    fodder = sizeof(union sockaddr_union);
    if (0 == getsockname(0, (struct sockaddr *)&su, &fodder)) {
	if (su.sin.sin_family == AF_INET6)
	    inet_ntop(AF_INET6, &su.sin6.sin6_addr, ownip, sizeof(ownip));
	else
	    inet_ntop(AF_INET, &su.sin.sin_addr, ownip, sizeof(ownip));

	if ((st = masock_sa2name((struct sockaddr *)&su, &h_err))) {
	    xstrlcpy(fqdn, st, sizeof(fqdn));
	    free(st);
	}
    }
#else
    fodder = sizeof(struct sockaddr_in);
    if (0 == getsockname(0, (struct sockaddr *)&sa, &fodder)) {
	he = gethostbyaddr((char *)&sa.sin_addr.s_addr,
			   sizeof(sa.sin_addr.s_addr), AF_INET);
	*fqdn = '\0';
	(void)xstrlcpy(fqdn,
		he && he->h_name ? he->h_name : inet_ntoa(sa.sin_addr),
		sizeof(fqdn));
	strcpy(ownip, inet_ntoa(sa.sin_addr));
    }
#endif
    else {
	strcpy(ownip, "no IP");
    }

    /* get remote name */
#ifdef HAVE_IPV6
    fodder = sizeof(union sockaddr_union);
    if (0 == getpeername(0, (struct sockaddr *)&su, &fodder)) {
	if (su.sa.sa_family == AF_INET6)
	    inet_ntop(AF_INET6, &su.sin6.sin6_addr, peername, sizeof(peername));
	else
	    inet_ntop(AF_INET, &su.sin.sin_addr, peername, sizeof(peername));

	strcpy(peerip, peername);

	if ((st = masock_sa2name((struct sockaddr *)&su, &h_err))) {
	    xstrlcpy(peername, st, sizeof(peername));
	    free(st);
	}

    }
#else
    fodder = sizeof(struct sockaddr_in);
    if (0 == getpeername(0, (struct sockaddr *)&sa, &fodder)) {
	he = gethostbyaddr((char *)&sa.sin_addr.s_addr,
			   sizeof(sa.sin_addr.s_addr), AF_INET);
	(void)xstrlcpy(peername,
		he && he->h_name ? he->h_name : inet_ntoa(sa.sin_addr),
		sizeof(peername));
	strcpy(peerip, inet_ntoa(sa.sin_addr));
    }
#endif
    else {
	if (errno == ENOTSOCK) {
	    strcpy(peername, "(local file)");
	    strcpy(peerip, "no IP");
	} else {
	    strcpy(peerip, "unknown");
	    strcpy(peername, "(unknown)");
	}
    }

    syslog(LOG_INFO, "connect from %s (%s) to %s (%s) (my fqdn: %s)",
	    peername, peerip, fqdn, ownip, origfqdn);

    if (allowstrangers == 0 && checkpeerlocal(0) != 1) {
	unsigned int i = 5;
	syslog(LOG_NOTICE, "Denying access from address outside the local networks. (Check config.example.)");
	while (i) i = sleep(i);
	printf("502 Remote access denied.\n");
	exit(0);
    }

    printf("200 Leafnode NNTP Daemon, version %s "
	    "running at %s (my fqdn: %s)\r\n",
	    version, fqdn, origfqdn);
    if (fflush(stdout)) exit(0);

    strcpy(fqdn, origfqdn);

    rereadactive();

    parser();

    (void)fflush(stdout);
    freeactive(active);
    freexover();
    freeconfig();
    sleep(1); /* protect against process ID induced file name collisions */
    exit(0);
}