File size: 39,404 Bytes
8ecd256 | 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 | .oO Phrack Magazine Oo.
Volume Seven, Issue Forty-Nine
File 09 of 16
by Dr.Dimitri Vulis (KOTM)
A Content-Blind Cancelbot for Usenet (CBCB)
Usenet News is a popular system for transmitting articles. Historically it
used to propagate over UUCP. However today most of the transmission is done
over the Internet TCP/IP connections using the NNTP protocol (RFC 977).
Each article consists of a series of headers of the form
Keyword: value
followed by a blank line, followed by the body of the message.
Some required headers are self-explanatory: From:, Date:, Subject:.
The Newsgroups: header identifies a series of keywords that can be used
to search for articles in the newsfeed. For example:
Newsgroups: news.admin.policy,comp.lang.c
identifies a Usenet article relevant to both Usenet administrative policy
and to the C computer language.
The Message-Id: header uniquely identifies each article. For example:
Message-Id: <12341223@whitehouse.gov>
The message-ids are not supposed to be recycled.
The cancelbot program is supposed to search the user-specified newsgroups for
articles whose headers match user-specified regular expressions and to issue
special 'cancel' control articles. It will copy some of the headers from the
original message and add a special header:
Control: cancel <message-id>
This program is an NNTP client. Much of the processing is offloaded to an
NNTP server, to which the cancelbot talks using the Internet sockets protocol.
This cancelbot does not look at article bodies and is therefore content-blind.
Inputs:
argv[1] (required) hosts file
A line that starts with # is a comment. Otherwise, each line contains the
following 5 fields:
1. hostname (some.domain.com) or ip address (a.b.c.d)
2. port (normally 119)
3. Y/N - do we ask this host for NEWNEWS/HEADER?
4. I/P/N - do we inject cancels to this host with IHAVE, POST, not at all
5. Timeout - the number of seconds to wait for a response from this server.
Example of a hosts file:
# ask the local server for new news and post back the cancels
127.0.0.1 119 Y P 60
# don't get message-ids from remote server, but give it cancels via IHAVE
news.xx.net 119 N I 300
argv[2] (required) target file
A line that starts with # is a comment. Otherwise, each line contains the
following 9 fields:
1. List of newsgroups to be scanned for new messages. This is not interpreted
by the cancelbot, but passed on to the NNTP server. Per RFC 997, multiple
groups can be separated by commas. Asterisk "*" may be used to match multiple
newsgroup names. The exclamation point "!" (as the first character) may be used
to negate a match. Warning: specifying a single * will generate a lot of data.
Example: news.groups,comp.*,sci.*,!sci.math.*
2. A watchword (case-sensitive) that needs to be contained in the article
headers for the cancel to be issued.
3. Format of the Subject: header in the cancel article.
C - Subject cancel <message-id> (same as Control:)
O - Subject: header copied from the original article
N - none.
If N is specified, then Subject: MUST be provided in the file appended to
the header, or the cancel won't propagate.
4. cancel message-id prefix
normally cancel. or cn.
Most cancellation articles follow the so-called $alz convention:
Control: cancel <message.id>
Message-id: <cancel.message.id>
However this is not a requirement.
5. path constant (string to put in path). May be 'none'.
6. path copy # (number of elements to copy from the right, may be 0)
Explanation of these two parameters:
each Usenet article contains the "Path:" header with a list of hosts separated
by explanation marks. For example:
Path: ohost1!ohost2!ohost3!ohost4
If you specify path constant of "nhosta!nhostb" and path copy of 2
then the path written by cbcb will be
Path: nhosta!nhostb!ohost3!ohost4
7. Name of the file appended to the header or 'none'
Examples:
# should be supplied as a courtesy
X-Cancelled-By: Cancelbot
# if and only if target file field 3 contains 'N':
Subject: Cancelling a Usenet article
# only if posting via IHAVE:
NNTP-Posting-Host: usenet.cabal.org
8. Name of the file that will become the body of the cancel or 'none'
If 'none' is specified, the default will be
"Please cancel this article."
9. The string to be prepended to the newsgroups. Normally 'none',
but may be set to something like misc.test (or misc.test,alt.test).
Example of a target file:
# delete all articles that mention C++ (but not c++)
comp.lang.c.* C++ C cancel. cyberspam 3 can.hdr none none
# no sex in the sci hierarchy, and add misc.test to the cancel
sci.* sex C cn. plutonium 2 can1.hdr can.txt misc.test
argv[3] (optional) datestamp, YYMMDD. If not specified, default is 900101. Only
articles after this date are examined. This parameter is not processed by the
cancelbot, but passed on to the NNTP server. It should normally be specified
so as not to look at old Usenet articles.
argv[4] (optional) timestamp, digits HHMMSS, where HH is hours on the 24-hour
clock, MM is minutes 00-59, and SS is seconds 00-59. If not specified, default
is 000000. Note that both datestamp and timestamp are in Greenwich mean time.
---------------8<-------cut me loose!-------------->8--------------------------
ed-note:
To compile, you must define an OS type (under gcc, this is accomplished using
the -Dmacro directive). Under Unix, for example:
gcc -DCBCB_UNIX -o cancelbot cbcb.c
---------------8<-------cut me loose!-------------->8--------------------------
cbcb.c:
/*
Context-blind CancelBot 0.9 04/01/96
Description of operations:
Open socket connections to the hosts listed in the hosts file
loop on targets
{
loop on servers
{
if (newnews_flag=='Y')
{
send NEWNEWS newsgroups datestamp timestamp GMT to this socket
receive a list of message-ids and save them in a LIFO linked list
loop on message-ids
{
send HEADER message-id to this server's socket
receieve a header
if the header contains the watchword
{
compose a cancel according to the target file specifications
loop on servers
{
if post_flag is P or I
send the cancel to this server's socket using posting method
}
}
delete this message-id from the linked list
}
}
}
}
*/
#ifndef CBCB_UNIX
#ifndef CBCB_VMS
#ifndef CBCB_NT
#ifndef CBCB_OS2
#error One of (CBCB_UNIX, CBCB_VMS, CBCB_NT, CBCB_OS2) must be defined
#endif
#endif
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
/* various flavors of Unix */
#ifdef CBCB_UNIX
/* gcc -DCBCB_UNIX cbcb.c -o cbcb */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/* perror to be called after failed socket calls */
#define perror_sock perror
/* how to close a socket */
#define close_sock close
#endif
/* Windows NT, /subsystem:console. The executable is supposed to work
under NT and Windows 95, but not under Win32s. */
#ifdef CBCB_NT
/* important note: when compiling on NT, say something like
cl /DCBCB_NT /Ogaityb1 /G5Fs /ML cbcb.c wsock32.lib */
#include <winsock.h>
/* regular perror doesn't work with WinSock under NT */
#define perror_sock(s) fprintf(stderr,"%s : WinSock error %d\n",s,WSAGetLastError())
/* regular close doesn't work with WinSock under NT */
#define close_sock closesocket
/* NT doesn't understand unix-style sleep in seconds */
#define sleep(n) Sleep(n*1000)
#endif
/* DEC VAX/VMS */
#ifdef CBCB_VMS
/* important note: when compiling on VAX/VMS, say something like
cc/define=CBCB_VMS cbcb/nodebug/optimize=(disjoint,inline)
link cbcb/nouserlib/notraceback,sys$library:ucx$ipc.olb/lib,-
sys$library:vaxcrtl.olb/lib
(to link in shared routines)
*/
#include <types.h>
#include <socket.h>
#include <netdb.h>
#include <in.h>
#include <inet.h>
#include <time.h>
#include <unixio.h>
#define perror_sock perror
#define close_sock close
#endif
/* IBM OS/2 - link with tcpip.lib */
#ifdef CBCB_OS2
#define OS2
/* we will use a BSD-like select, not Oleg's hack */
#define BSD_SELECT
#define INCL_DOSPROCESS
#include <bsedos.h> /* DosSleep */
#include <sys\types.h>
#include <sys\socket.h>
#include <sys\select.h>
#include <netinet\in.h>
/*#include <arpa\inet.h>*/
#include <netdb.h>
/* perror to be called after failed socket calls */
#define perror_sock fprintf(stderr,"%s : tcp error %d\n",s,tcperrno())
/* how to close a socket */
#define close_sock soclose
#define sleep(n) DosSleep(n/1000)
#endif
/*
Future Macintosh notes: Need Apple's MPW (Macintosh Programmer's Workshop).
Build CBCB as an MPW tool. Set the Macintosh file type to MPST and the
Macintosh creator to MPS, so we can use stdout and stderr.
Sockets are supposed to be available on the Mac.
*/
#ifndef FD_ZERO
/* macros for select() not defined on VAX or HPUX
However they are defined to be something completely different
under NT WinSock, so we must use macros */
#define fd_set int
#define FD_ZERO(p) {*(p)=0;}
#define FD_SET(s,p) {*(p)|=(1<<(s));}
#define FD_ISSET(s,p) ((*(p)&(1<<(s)))!=0)
#endif
/* file pointers */
FILE *sptr, /* hosts file */
*tptr; /* target file*/
/* there's a reason for making all these variables static. If I weren't lazy,
I would have put them in their respective functions with 'static' */
#define MAXHOSTS 100
struct {
int cfd; /* socket handle */
char newnews_flag;
char post_flag;
int timeout;
} hosts[MAXHOSTS];
int nhosts;
short int port;
#define ASCII_CR 13
#define ASCII_LF 10
#define BUFFERSIZE 2048
#define BUFFERBIGSIZE 20480
char buffer_big[BUFFERBIGSIZE];
struct _msgidq {
char *msgid;
struct _msgidq *next;
};
struct _msgidq *msg_queue,*msg_t;
int parse_state, /* for parsing server responses */
h_flag,d_flag; /* shortcut for states when parsing headers */
char hostname[BUFFERSIZE];
char buffer[BUFFERSIZE];
char extra_header[BUFFERSIZE];
char extra_body[BUFFERSIZE];
int file_rec;
char newsgroups[BUFFERSIZE]; /* target field 1 */
char watchword[BUFFERSIZE]; /* target field 2 */
char subject_flag; /* target field 3 */
char cmsg_id_prefix[BUFFERSIZE]; /* target field 4 */
char path_const[BUFFERSIZE]; /* target field 5 */
int path_num; /* target field 6 */
char hdr_fname[BUFFERSIZE]; /* target field 7 */
char txt_fname[BUFFERSIZE]; /* target field 8 */
char extra_ngrp[BUFFERSIZE]; /* target field 9 */
char *datestamp,*timestamp; /* for the NEWNEWS command */
char *sznone="none";
char *szcabal=" Usenet@Cabal";
char *szsubject="Subject:";
char *szsubjectc="Subject: cmsg";
char *szendl="\r\n";
char *szempty="";
int nretry; /* number of retries in various places */
int nbytes;
int host1,host2,i,j; /* loop indices */
#define NOLDHEADERS 8
/* We're interested in 8 original headers :
Path: 0 (requires special handling)
From: 1
Sender: 2
Approved: 3
Newsgroups: 4
Date: 5
Subject: 6
Organization: 7
*/
char *h_ptr[NOLDHEADERS];
char *t_ptr[3];
/* ANSI function prototypes */
int cbcb_parse_hosts(void);
int cbcb_parse_targets(void);
int cbcb_process_target(void);
int cbcb_parse_message_ids(void);
int cbcb_process_article(char *);
int cbcb_get_headers(void);
void cbcb_save_headers(void);
void cbcb_save_header(int);
int cbcb_flush_sock(int);
int cbcb_test_sock(int);
int cbcb_recv_resp(int,char);
int cbcb_copy_buffer(char *);
int main(int argc,char*argv[])
{
/* process the arguments */
if (argc<3 || argc>5)
{
fprintf(stderr,"Usage: cbcb hostfile targetfile [datestamp] [timestamp]\n");
return(1);
}
if (argc<4)
datestamp="900101";
else
datestamp=argv[3];
if (argc<5)
timestamp="000000";
else
timestamp=argv[4];
/* open the hosts file */
if (NULL==(sptr=fopen(argv[1],"r")))
{
perror("open()");
fprintf(stderr,"cbcb cannot open hosts file %s\n",argv[1]);
return(0);
}
/* open the target file */
if (NULL==(tptr=fopen(argv[2],"r")))
{
perror("open()");
fprintf(stderr,"cbcb cannot open target file %s\n",argv[2]);
return(0);
}
#ifdef SIGPIPE
signal(SIGPIPE,SIG_IGN); /* ignore broken pipes if this platform knows them */
#endif
/* establish the connections to the NNTP servers */
if (0==cbcb_parse_hosts())
{
fprintf(stderr,"cbcb unable to connect to any NNTP servers\n");
return(1);
}
fclose(sptr);
if (!cbcb_parse_targets())
{
fprintf(stderr,"cbcb encountered an error processing targets\n");
return(1);
}
fclose(tptr);
/* final cleanup */
for (i=0; i<nhosts; i++)
close_sock(hosts[i].cfd);
#ifdef CBCB_NT
WSACleanup();
#endif
return(0);
}
int cbcb_parse_hosts(void)
{
unsigned long host_ip;
struct hostent *host_struct;
struct in_addr *host_node;
/*
struct servent *sp;
*/
struct sockaddr_in serverUaddr;
#ifdef CBCB_NT
WSADATA wsaData; /* needed for WSAStartup */
#endif
#ifdef CBCB_NT
if (WSAStartup(MAKEWORD(1,1),&wsaData))
{
perror_sock("WSAStartup()");
fprintf(stderr,"couldn't start up WinSock\n");
return(0);
}
fprintf(stderr,"Found WinSock: %s\n",wsaData.szDescription);
#endif
#ifdef CBCB_OS2
if (0!=sock_init())
{
perror_sock("sock_init()");
fprintf(stderr,"couldn't start up sockets - is inet.sys running?\n");
return(0);
}
#endif
/*
if (NULL==(sp=getservbyname("nntp","tcp")))
{
fprintf(stderr,"Can't find the NNTP port\n");
return(0);
}
...
serverUaddr.sin_port=(sp->s_port);
*/
/* loop on the hosts file */
nhosts=0;
file_rec=0;
while(NULL!=fgets(buffer,sizeof(buffer),sptr))
{
file_rec++;
if (*buffer=='#')
continue;
if (nhosts>=MAXHOSTS)
{
fprintf(stderr,"Please increase MAXHOSTS\n");
break;
}
if (5!=sscanf(buffer,"%2048s %hd %c %c %d",
hostname,&port,&hosts[nhosts].newnews_flag,&hosts[nhosts].post_flag,
&hosts[nhosts].timeout))
{
fprintf(stderr,"Error parsing host file line %d \"%s\"\n",file_rec,buffer);
continue;
}
/* verify that the newnews flag is Y or N */
if (hosts[nhosts].newnews_flag=='n')
hosts[nhosts].newnews_flag='N';
else if (hosts[nhosts].newnews_flag=='y')
hosts[nhosts].newnews_flag='Y';
else if (hosts[nhosts].newnews_flag!='Y'&&hosts[nhosts].newnews_flag!='N')
{
fprintf(stderr,"Newnews flag %c, must be Y or N on line %d\n",
hosts[nhosts].newnews_flag,file_rec);
continue;
}
/* verify that the posting flag is P, or I, or N */
if (hosts[nhosts].post_flag=='i')
hosts[nhosts].post_flag='I';
else if (hosts[nhosts].post_flag=='p')
hosts[nhosts].post_flag='P';
else if (hosts[nhosts].post_flag=='n')
hosts[nhosts].post_flag='N';
else if (hosts[nhosts].post_flag!='I'&&hosts[nhosts].post_flag!='P'&&hosts[nhosts].post_flag!='N')
{
fprintf(stderr,"Posting flag %c, must be I, or P, or N on line %d\n",
hosts[nhosts].post_flag,file_rec);
continue;
}
/* translate the hostname into an ip address. If it starts with a digit,
try to interpret it as a A.B.C.D address */
if (!isdigit(*hostname)||(0xFFFFFFFF==(host_ip=inet_addr(hostname))))
{
if (NULL==(host_struct=gethostbyname(hostname)))
{
perror("gethostbyname");
fprintf(stderr,"Can't resolve host name %s to ip on line %d\n",
hostname,file_rec);
continue;
}
host_node=(struct in_addr*)host_struct->h_addr;
fprintf(stderr,"Note: Using NNTP server at %s\n",inet_ntoa(*host_node));
host_ip=host_node->s_addr;
}
/* fill in the address to connect to */
memset(&serverUaddr,0,sizeof(serverUaddr));
serverUaddr.sin_family=PF_INET;
serverUaddr.sin_addr.s_addr=/*htonl*/(host_ip); /* already in net order */
serverUaddr.sin_port=htons(port);
/* try to create a socket */
if ((hosts[nhosts].cfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror_sock("socket()");
continue;
}
conn1:
if (0>=connect(hosts[nhosts].cfd,(struct sockaddr*)&serverUaddr,sizeof(serverUaddr)))
goto conn2; /* we use goto so we can use continue */
if (nretry>10)
{
fprintf(stderr,"give up trying to connect to %s port %hd on line %d\n",
hostname,port,file_rec);
close_sock(hosts[nhosts].cfd);
hosts[nhosts].newnews_flag=hosts[nhosts].post_flag='N';
continue;
}
perror_sock("connect()");
nretry++;
sleep(1);
goto conn1;
conn2:
if (!cbcb_recv_resp(nhosts,'2'))
{
fprintf(stderr,"NNTP problem after connecting to %s port %hd on line %d\n",
hostname,port,file_rec);
close_sock(hosts[nhosts].cfd);
hosts[nhosts].newnews_flag=hosts[nhosts].post_flag='N';
continue;
}
nhosts++;
}
return(nhosts);
}
int cbcb_parse_targets(void)
{
file_rec=0;
while(fgets(buffer,sizeof(buffer),tptr)) /* read a target line */
{
file_rec++;
if (*buffer=='#') /* comment */
continue;
/* parse the buffer into the 8 fields */
if (9!=sscanf(buffer,"%2048s %2048s %c %2048s %2048s %d %2048s %2048s %2048s",
newsgroups, watchword, &subject_flag, cmsg_id_prefix, path_const,
&path_num, hdr_fname, txt_fname, extra_ngrp))
{
fprintf(stderr,"Error parsing 8 fields on line %d \"%s\"\n",
file_rec,buffer);
continue;
}
/* verify that the subject flag is C, O, or N */
if (subject_flag=='c')
subject_flag='C';
else if (subject_flag=='o')
subject_flag='O';
else if (subject_flag=='n')
subject_flag='N';
else if (subject_flag!='C'&&subject_flag!='O'&&subject_flag!='N')
{
fprintf(stderr,"Subject flag %c, must be C, O, or N on line %d\n",
subject_flag,file_rec);
continue;
}
if (0==strcmp(path_const,sznone)) /* if 'none' is specified */
{
if (path_num==0)
{
fprintf(stderr,"Can't have path_const none and path_num 0\n");
continue;
}
path_const[0]=0;
}
else /* if not none, append bang if needed */
{
i=strlen(path_const);
if (path_const[i-1]!='!')
{
path_const[i]='!';
path_const[i+1]=0;
}
}
if (0==strcmp(extra_ngrp,sznone)) /* if 'none' is specified */
extra_ngrp[0]=0;
else /* if not none, append comma if needed */
{
i=strlen(extra_ngrp);
if (extra_ngrp[i-1]!=',')
{
extra_ngrp[i]=',';
extra_ngrp[i+1]=0;
}
}
/* read the extra header lines */
if (0==strcmp(hdr_fname,sznone)) /* if 'none' is specified */
*extra_header=0;
else
{
/* try to open the specified file */
if (NULL==(sptr=fopen(hdr_fname,"r")))
{
perror("open()");
fprintf(stderr,"cbcb cannot open extra-header file %s\n",hdr_fname);
continue;
}
nbytes=fread(buffer,1,BUFFERSIZE,sptr);
fclose(sptr);
if (nbytes>=BUFFERSIZE)
fprintf(stderr,"extra-header file %s is too long\n",hdr_fname);
if (!cbcb_copy_buffer(extra_header))
{
fprintf(stderr,"error in header file\n");
continue;
}
}
/* read the body the same way */
if (0==strcmp(txt_fname,sznone)) /* if 'none' is specified */
strcpy(extra_body,"Please cancel this article\r\n");
else
{
/* try to open the specified file */
if (NULL==(sptr=fopen(txt_fname,"r")))
{
perror("open()");
fprintf(stderr,"cbcb cannot open body file %s\n",txt_fname);
continue;
}
nbytes=fread(buffer,1,BUFFERSIZE,sptr);
fclose(sptr);
if (nbytes>=BUFFERSIZE)
fprintf(stderr,"body file %s is too long\n",txt_fname);
if (!cbcb_copy_buffer(extra_body))
{
fprintf(stderr,"error in body file\n");
continue;
}
}
if (!cbcb_process_target()) /* process otherwise. warn and go on if error */
fprintf(stderr,"cbcb encountered a problem processing target, line %d\n",
file_rec);
}
return(1);
}
int cbcb_process_target(void)
{
/* loop on hosts */
for (host1=0; host1<nhosts; host1++)
if (hosts[host1].newnews_flag=='Y') /* if we want to get message-ids from it */
{
cbcb_flush_sock(hosts[host1].cfd);
/* compose the rfc 977 newnews command. Ansi C would let us write
nbytes=sprintf(..), but gcc has a non-compilant sprintf which return
buffer instead, so we must use strlen */
sprintf(buffer,"NEWNEWS %s %s %s GMT\r\n",
newsgroups,datestamp,timestamp);
nbytes=strlen(buffer);
/* send the command to the server */
if (nbytes!=send(hosts[host1].cfd,buffer,nbytes,0))
{
perror_sock("NEWNEWS send()");
continue;
}
/* the server is supposed to return a list of message-ids now */
if (!cbcb_parse_message_ids())
fprintf(stderr,"Problem parsing message-ids\n");
/* no 'continue': even if we return a partial queue, try to process it */
/* loop through headers, newest first */
while (msg_queue)
{
msg_t=msg_queue;
if (!cbcb_process_article(msg_queue->msgid))
fprintf(stderr,"Problem processing article <%s>\n",msg_queue->msgid);
msg_queue=msg_queue->next;
free(msg_t);
}
}
return(1);
}
int cbcb_parse_message_ids(void)
{
msg_queue=NULL;
parse_state=7;
nretry=0;
recv_msgids:
if (!cbcb_test_sock(hosts[host1].cfd)) /* nothing to read */
{
if (nretry>hosts[host1].timeout)
{
fprintf(stderr,"timeout waiting to recv message-ids\n");
return(0);
}
fprintf(stderr,".");
nretry++;
sleep(1);
goto recv_msgids;
}
nbytes=recv(hosts[host1].cfd,buffer,sizeof(buffer),0);
if (nbytes<0) /* an error shouldn't happen here */
{
perror_sock("NEWNEWS recv()");
return(0);
}
#ifdef DEBUG
fwrite(buffer,1,nbytes,stdout); /* for debugging only!! */
#endif
/* now see if what we received makes sense */
for (i=0; i<nbytes; i++)
{
switch(parse_state)
{
case 0:
if (buffer[i]=='.')
parse_state=4;
else if (buffer[i]!='<')
goto recv_bad_msg_id;
else
{
j=0;
parse_state=1;
}
break;
case 1:
if (buffer[i]=='>')
{
/* add to the queue */
msg_t=(struct _msgidq*)malloc(sizeof(struct _msgidq));
if (msg_t==NULL)
{
fprintf(stderr,"malloc failed\n");
return(0);
}
msg_t->msgid=(char*)malloc(j+1);
if (msg_t->msgid==NULL)
{
free(msg_t);
fprintf(stderr,"malloc failed\n");
return(0);
}
memcpy(msg_t->msgid,buffer_big,j);
*(msg_t->msgid+j)=0;
msg_t->next=msg_queue;
msg_queue=msg_t;
parse_state=2;
}
else
{
if (j>=BUFFERBIGSIZE)
{
fprintf(stderr,"Please increase BUFFERBIGSIZE\n");
return(0);
}
buffer_big[j]=buffer[i];
j++;
/* parse_state=1; */
}
break;
case 2:
if (buffer[i]==ASCII_CR)
parse_state=3;
else
goto recv_bad_msg_id;
break;
case 3:
if (buffer[i]==ASCII_LF)
parse_state=0;
else
goto recv_bad_msg_id;
break;
case 4:
if (buffer[i]==ASCII_CR)
parse_state=5;
else
goto recv_bad_msg_id;
break;
case 5:
if (buffer[i]==ASCII_LF)
parse_state=6;
else
goto recv_bad_msg_id;
break;
case 6: /* more data after final . */
goto recv_bad_msg_id;
case 7: /* initial, really */
if (buffer[i]=='2')
parse_state=8;
else
goto recv_bad_msg_id;
break;
case 8:
if (buffer[i]==ASCII_CR)
parse_state=3;
break;
}
}
if (parse_state!=6)
goto recv_msgids;
/* normal competion */
return(1);
recv_bad_msg_id:
fprintf(stderr,"Unexpected response (expected message-ids) ");
if (i)
{
fprintf(stderr,"after \"");
fwrite(buffer,1,i,stderr);
fprintf(stderr,"\" ");
}
if (i<nbytes)
{
fprintf(stderr,"before \"");
fwrite(buffer+i,1,nbytes-i,stderr);
fprintf(stderr,"\"");
}
fprintf(stderr,"\n");
return(0);
}
int cbcb_process_article(char *msgid)
{
/* if there is any leftover data in the socket, get it out */
cbcb_flush_sock(hosts[host1].cfd);
/* compose the rfc 977 head command */
sprintf(buffer,"HEAD <%s>\r\n",msgid);
/* send the command to the server */
nbytes=strlen(buffer);
if (nbytes!=send(hosts[host1].cfd,buffer,nbytes,0))
{
perror_sock("HEAD send()");
return(0);
}
/* the server is supposed to return the article headers now */
if (!cbcb_get_headers())
{
fprintf(stderr,"Problem retrieving headers\n");
return(0);
}
if (!strstr(buffer_big,watchword))
return(1); /* no match, nothing to do */
/* found the watchword: let's cancel */
cbcb_save_headers();
sprintf(buffer_big,"\
Path: %s%s\r\n\
From:%s\r\n\
Sender:%s\r\n\
Approved:%s\r\n\
Newsgroups: %s%s\r\n\
Date:%s\r\n\
%s%s%s\
Organization:%s\r\n\
Control:%s\r\n\
Message-ID: <%s%s>\r\n\
%s\
\r\n\
%s\
.\r\n",
path_const,
h_ptr[0],h_ptr[1],h_ptr[2],h_ptr[3],extra_ngrp,h_ptr[4],h_ptr[5],
t_ptr[0],h_ptr[6],t_ptr[1],h_ptr[7],t_ptr[2],
cmsg_id_prefix,msgid,extra_header,extra_body);
fputs(buffer_big,stderr); /* to see what we're posting */
for (host2=0; host2<nhosts; host2++)
if (hosts[host2].post_flag=='P'||hosts[host2].post_flag=='I')
{
cbcb_flush_sock(hosts[host2].cfd);
if (hosts[host2].post_flag=='P')
{
/* send the command to the server */
if (6!=send(hosts[host2].cfd,"POST\r\n",6,0))
{
perror_sock("POST send()");
continue;
}
}
else /*hosts[host2].post_flag=='I') */
{
sprintf(buffer,"IHAVE <%s%s>\r\n",cmsg_id_prefix,msgid);
nbytes=strlen(buffer);
/* send the command to the server */
if (nbytes!=send(hosts[host2].cfd,buffer,nbytes,0))
{
perror_sock("IHAVE send()");
continue;
}
}
if (!cbcb_recv_resp(host2,'3'))
{
fprintf(stderr,"NNTP problem while trying to post\n");
continue;
}
nbytes=strlen(buffer_big);
if (nbytes!=send(hosts[host2].cfd,buffer_big,nbytes,0))
{
perror_sock("article send()");
continue;
}
if (!cbcb_recv_resp(host2,'2'))
{
fprintf(stderr,"NNTP problem after posting\n");
continue;
}
}
return(1); /* all's well */
}
int cbcb_get_headers(void)
{
h_ptr[0]=h_ptr[1]=h_ptr[2]=h_ptr[3]=h_ptr[4]=h_ptr[5]=h_ptr[6]=h_ptr[7]=NULL;
h_flag=d_flag=parse_state=0;
nretry=0;
j=0;
/* recv */
recv_headers:
if (!cbcb_test_sock(hosts[host1].cfd)) /* nothing to read */
{
if (nretry>hosts[host1].timeout)
{
fprintf(stderr,"timeout waiting to recv article headers\n");
return(0);
}
fprintf(stderr,".");
nretry++;
sleep(1);
goto recv_headers;
}
nbytes=recv(hosts[host1].cfd,buffer,sizeof(buffer),0);
if (nbytes<0) /* an error shouldn't happen here */
{
perror_sock("headers recv()");
return(0);
}
#ifdef DEBUG
fwrite(buffer,1,nbytes,stdout); /* for debugging only!! */
#endif
/* see if what we received makes sense */
for (i=0; i<nbytes; i++)
{
switch(parse_state)
{
case 0:
if (buffer[i]=='2')
parse_state=1;
else
goto recv_bad_header;
break;
case 1:
if (buffer[i]=='2')
parse_state=2;
else
goto recv_bad_header;
break;
case 2:
if (buffer[i]==ASCII_CR)
parse_state=3;
/*
else
parse_state=2;
*/
break;
case 3:
if (buffer[i]==ASCII_LF)
{
if (d_flag)
parse_state=5;
else
{
h_flag=1;
parse_state=4;
goto recv_header_save;
}
}
else
goto recv_bad_header;
break;
case 4:
if (buffer[i]==ASCII_CR) /* don't save cr's */
parse_state=3;
else
{
if (h_flag)
{
d_flag=0;
if (buffer[i]=='.')
d_flag=1;
else if (buffer[i]=='p'||buffer[i]=='P')
parse_state=10;
else if (buffer[i]=='f'||buffer[i]=='F')
parse_state=20;
else if (buffer[i]=='s'||buffer[i]=='S')
parse_state=30;
else if (buffer[i]=='a'||buffer[i]=='A')
parse_state=40;
else if (buffer[i]=='n'||buffer[i]=='N')
parse_state=50;
else if (buffer[i]=='d'||buffer[i]=='D')
parse_state=60;
else if (buffer[i]=='o'||buffer[i]=='O')
parse_state=70;
else if (buffer[i]==' '||buffer[i]=='\t') /* space means continuation */
j--; /* backup over the lf */
h_flag=0;
}
else
d_flag=0;
goto recv_header_save;
}
break;
case 5: /* more data after the final . */
goto recv_bad_header;
/* we recognize these headers on the fly */
case 10:
if (buffer[i]=='a'||buffer[i]=='A')
parse_state=11;
else
parse_state=4;
goto recv_header_save;
case 11:
if (buffer[i]=='t'||buffer[i]=='t')
parse_state=12;
else
parse_state=4;
goto recv_header_save;
case 12:
if (buffer[i]=='h'||buffer[i]=='H')
parse_state=13;
else
parse_state=4;
goto recv_header_save;
case 13:
if (buffer[i]==':')
h_ptr[0]=buffer_big+j+1; /* Path: */
parse_state=4;
goto recv_header_save;
case 20:
if (buffer[i]=='r'||buffer[i]=='R')
parse_state=21;
else
parse_state=4;
goto recv_header_save;
case 21:
if (buffer[i]=='o'||buffer[i]=='O')
parse_state=22;
else
parse_state=4;
goto recv_header_save;
case 22:
if (buffer[i]=='m'||buffer[i]=='M')
parse_state=23;
else
parse_state=4;
goto recv_header_save;
case 23:
if (buffer[i]==':')
h_ptr[1]=buffer_big+j+1; /* From: */
parse_state=4;
goto recv_header_save;
case 30:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=31;
else if (buffer[i]=='u'||buffer[i]=='U')
parse_state=90;
else
parse_state=4;
goto recv_header_save;
case 31:
if (buffer[i]=='n'||buffer[i]=='N')
parse_state=32;
else
parse_state=4;
goto recv_header_save;
case 32:
if (buffer[i]=='d'||buffer[i]=='D')
parse_state=33;
else
parse_state=4;
goto recv_header_save;
case 33:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=34;
else
parse_state=4;
goto recv_header_save;
case 34:
if (buffer[i]=='r'||buffer[i]=='R')
parse_state=35;
else
parse_state=4;
goto recv_header_save;
case 35:
if (buffer[i]==':')
h_ptr[2]=buffer_big+j+1; /* Sender: */
parse_state=4;
goto recv_header_save;
case 40:
if (buffer[i]=='p'||buffer[i]=='P')
parse_state=41;
else
parse_state=4;
goto recv_header_save;
case 41:
if (buffer[i]=='p'||buffer[i]=='P')
parse_state=42;
else
parse_state=4;
goto recv_header_save;
case 42:
if (buffer[i]=='r'||buffer[i]=='R')
parse_state=43;
else
parse_state=4;
goto recv_header_save;
case 43:
if (buffer[i]=='o'||buffer[i]=='O')
parse_state=44;
else
parse_state=4;
goto recv_header_save;
case 44:
if (buffer[i]=='v'||buffer[i]=='V')
parse_state=45;
else
parse_state=4;
goto recv_header_save;
case 45:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=46;
else
parse_state=4;
goto recv_header_save;
case 46:
if (buffer[i]=='d'||buffer[i]=='D')
parse_state=47;
else
parse_state=4;
goto recv_header_save;
case 47:
if (buffer[i]==':')
h_ptr[3]=buffer_big+j+1; /* Approved: */
parse_state=4;
goto recv_header_save;
case 50:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=51;
else
parse_state=4;
goto recv_header_save;
case 51:
if (buffer[i]=='w'||buffer[i]=='W')
parse_state=52;
else
parse_state=4;
goto recv_header_save;
case 52:
if (buffer[i]=='s'||buffer[i]=='S')
parse_state=53;
else
parse_state=4;
goto recv_header_save;
case 53:
if (buffer[i]=='g'||buffer[i]=='G')
parse_state=54;
else
parse_state=4;
goto recv_header_save;
case 54:
if (buffer[i]=='r'||buffer[i]=='R')
parse_state=55;
else
parse_state=4;
goto recv_header_save;
case 55:
if (buffer[i]=='o'||buffer[i]=='O')
parse_state=56;
else
parse_state=4;
goto recv_header_save;
case 56:
if (buffer[i]=='u'||buffer[i]=='U')
parse_state=57;
else
parse_state=4;
goto recv_header_save;
case 57:
if (buffer[i]=='p'||buffer[i]=='P')
parse_state=58;
else
parse_state=4;
goto recv_header_save;
case 58:
if (buffer[i]=='s'||buffer[i]=='S')
parse_state=59;
else
parse_state=4;
goto recv_header_save;
case 59:
if (buffer[i]==':')
h_ptr[4]=buffer_big+j+2; /* Newsgroups:, skip space */
parse_state=4;
goto recv_header_save;
case 60:
if (buffer[i]=='a'||buffer[i]=='A')
parse_state=61;
else
parse_state=4;
goto recv_header_save;
case 61:
if (buffer[i]=='t'||buffer[i]=='T')
parse_state=62;
else
parse_state=4;
goto recv_header_save;
case 62:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=63;
else
parse_state=4;
goto recv_header_save;
case 63:
if (buffer[i]==':')
h_ptr[5]=buffer_big+j+1; /* Date: */
parse_state=4;
goto recv_header_save;
case 70:
if (buffer[i]=='r'||buffer[i]=='R')
parse_state=71;
else
parse_state=4;
goto recv_header_save;
case 71:
if (buffer[i]=='g'||buffer[i]=='G')
parse_state=72;
else
parse_state=4;
goto recv_header_save;
case 72:
if (buffer[i]=='a'||buffer[i]=='A')
parse_state=73;
else
parse_state=4;
goto recv_header_save;
case 73:
if (buffer[i]=='n'||buffer[i]=='N')
parse_state=74;
else
parse_state=4;
goto recv_header_save;
case 74:
if (buffer[i]=='i'||buffer[i]=='I')
parse_state=75;
else
parse_state=4;
goto recv_header_save;
case 75:
if (buffer[i]=='z'||buffer[i]=='Z')
parse_state=76;
else
parse_state=4;
goto recv_header_save;
case 76:
if (buffer[i]=='a'||buffer[i]=='A')
parse_state=77;
else
parse_state=4;
goto recv_header_save;
case 77:
if (buffer[i]=='t'||buffer[i]=='T')
parse_state=78;
else
parse_state=4;
goto recv_header_save;
case 78:
if (buffer[i]=='i'||buffer[i]=='I')
parse_state=79;
else
parse_state=4;
goto recv_header_save;
case 79:
if (buffer[i]=='o'||buffer[i]=='O')
parse_state=80;
else
parse_state=4;
goto recv_header_save;
case 80:
if (buffer[i]=='n'||buffer[i]=='N')
parse_state=81;
else
parse_state=4;
goto recv_header_save;
case 81:
if (buffer[i]==':')
h_ptr[7]=buffer_big+j+1; /* Organization: */
parse_state=4;
goto recv_header_save;
case 90:
if (buffer[i]=='b'||buffer[i]=='B')
parse_state=91;
else
parse_state=4;
goto recv_header_save;
case 91:
if (buffer[i]=='j'||buffer[i]=='J')
parse_state=92;
else
parse_state=4;
goto recv_header_save;
case 92:
if (buffer[i]=='e'||buffer[i]=='E')
parse_state=93;
else
parse_state=4;
goto recv_header_save;
case 93:
if (buffer[i]=='c'||buffer[i]=='C')
parse_state=94;
else
parse_state=4;
goto recv_header_save;
case 94:
if (buffer[i]=='t'||buffer[i]=='T')
parse_state=95;
else
parse_state=4;
goto recv_header_save;
case 95:
if (buffer[i]==':')
h_ptr[6]=buffer_big+j+1; /* Subject: */
parse_state=4;
goto recv_header_save;
default: /* how could we ever get here? */
goto recv_bad_header;
}
continue; /* ugly, branch around save */
recv_header_save:
if (j>=BUFFERBIGSIZE)
{
fprintf(stderr,"Please increase BUFFERBIGSIZE\n");
return(0);
}
buffer_big[j++]=buffer[i];
} /* next i */
if (parse_state!=5)
goto recv_headers;
return(1);
recv_bad_header:
fprintf(stderr,"Unexpected response (expected headers) ");
if (i)
{
fprintf(stderr,"after \"");
fwrite(buffer,1,i,stderr);
fprintf(stderr,"\" ");
}
if (i<nbytes)
{
fprintf(stderr,"before \"");
fwrite(buffer+i,1,nbytes-i,stderr);
fprintf(stderr,"\"");
}
fprintf(stderr,"\n");
return(0);
}
void cbcb_save_headers(void)
{
/* now copy old headers to buffer for safekeeping */
/* only if buffer_big matched the pattern */
/* only Path: is special: no initial space */
if (h_ptr[0]==NULL) /* no path */
{
j=0;
h_ptr[0]=" ";
}
else
{
i=h_ptr[0]-buffer_big;
j=path_num;
while (buffer_big[i]!=ASCII_LF)
i++;
i--;
/* now go back and look for the last n bang-separated components, or the
beginning of path */
while (buffer_big[i]>' ' && j)
{
i--;
if (buffer_big[i]=='!')
j--;
}
i++;
j=0;
h_ptr[0]=buffer;
while (buffer_big[i]!=ASCII_LF)
buffer[j++]=buffer_big[i++];
buffer[j++]=0;
}
t_ptr[2]=buffer+j;
sprintf(t_ptr[2]," cancel <%s>",msg_queue->msgid);
j+=strlen(t_ptr[2])+1;
if (h_ptr[1]==NULL) /* no from? Highly unlikely */
h_ptr[1]=szcabal;
else
cbcb_save_header(1);
if (h_ptr[2]==NULL) /* sender */
h_ptr[2]=h_ptr[1];
else
cbcb_save_header(2);
if (h_ptr[3]==NULL) /* approved */
h_ptr[3]=h_ptr[2];
else
cbcb_save_header(3);
if (h_ptr[4]==NULL) /* no newsgroups? */
h_ptr[4]="control";
else
cbcb_save_header(4);
if (h_ptr[5]==NULL) /* no date??? */
h_ptr[5]=" 1 Jan 1990 00:00 GMT";
else
cbcb_save_header(5);
/* subject is special - must use flag */
if (subject_flag=='O')
{
if (h_ptr[6]==NULL)
h_ptr[6]=szcabal; /* no subject??? */
else
cbcb_save_header(6);
t_ptr[0]=szsubject;
t_ptr[1]=szendl;
}
else if (subject_flag=='C')
{
h_ptr[6]=t_ptr[2]; /* same as the Control: */
t_ptr[0]=szsubjectc;
t_ptr[1]=szendl;
}
else /* if (subject_flag=='N') */
{
t_ptr[0]=t_ptr[1]=h_ptr[6]=szempty;
}
if (h_ptr[7]==NULL) /* organization */
h_ptr[7]=szcabal;
else
cbcb_save_header(7);
#ifdef DEBUG
for (i=0; i<8; i++)
if (h_ptr[i])
printf("%d:%s\n",i,h_ptr[i]);
#endif
}
void cbcb_save_header(int k)
{
i=h_ptr[k]-buffer_big;
h_ptr[k]=buffer+j;
while (buffer_big[i]!=ASCII_LF)
buffer[j++]=buffer_big[i++];
buffer[j++]=0;
}
int cbcb_flush_sock(int sock)
{
/* if there is any leftover data in the socket, get it out */
while (cbcb_test_sock(sock))
{
nbytes=recv(sock,buffer,sizeof(buffer),0);
if (nbytes<0)
perror_sock("flush recv()"); /* but don't abort */
else
fwrite(buffer,1,nbytes,stderr); /* display it, as it may be informative */
}
return(1);
}
/* use select to see if there's data here.
There don't seem to be any unixes left which understand poll and not select.*/
int cbcb_test_sock(int sock)
{
fd_set setm;
static struct timeval zerotime={0,0};
FD_ZERO(&setm);
FD_SET(sock,&setm);
if (select(sock+1,&setm,NULL,NULL,&zerotime)<0)
{
perror_sock("select()");
}
if (FD_ISSET(sock,&setm))
return(1);
else
return(0);
}
int cbcb_recv_resp(int host,char c)
{
parse_state=0;
nretry=0;
recv_resp:
if (!cbcb_test_sock(hosts[host].cfd)) /* nothing to read */
{
if (nretry>hosts[host].timeout)
{
fprintf(stderr,"timeout waiting to recv response\n");
return(0);
}
fprintf(stderr,".");
nretry++;
sleep(1);
goto recv_resp;
}
nbytes=recv(hosts[host].cfd,buffer,sizeof(buffer),0);
if (nbytes<0) /* an error shouldn't happen here */
{
perror_sock("response recv()");
return(0);
}
/* #ifdef DEBUG */
fwrite(buffer,1,nbytes,stdout); /* for debugging only!! */
/* #endif */
/* now see if what we received makes sense */
for (i=0; i<nbytes; i++)
{
switch(parse_state)
{
case 0:
if (buffer[i]==c)
parse_state=1;
else
goto recv_bad_resp;
break;
case 1:
if (buffer[i]==ASCII_CR)
parse_state=2;
break;
case 2:
if (buffer[i]==ASCII_LF)
parse_state=3;
else
goto recv_bad_resp;
break;
case 3: /* more data after final \n */
goto recv_bad_resp;
}
}
if (parse_state!=3)
goto recv_resp;
/* normal competion */
return(1);
recv_bad_resp:
fprintf(stderr,"Unexpected response (expected %cxx message) ",c);
if (i)
{
fprintf(stderr,"after \"");
fwrite(buffer,1,i,stderr);
fprintf(stderr,"\" ");
}
if (i<nbytes)
{
fprintf(stderr,"before \"");
fwrite(buffer+i,1,nbytes-i,stderr);
fprintf(stderr,"\"");
}
fprintf(stderr,"\n");
return(0);
}
int cbcb_copy_buffer(char *s)
{
i=j=0;
if (nbytes>0&&buffer[nbytes-1]!='\n')
buffer[nbytes++]='\n';
buffer[nbytes]=0;
while (buffer[i])
{
if (j>=BUFFERSIZE)
{
fprintf(stderr,"File too big\n");
return(0);
}
if (buffer[i]=='\n')
*(s+(j++))='\r';
*(s+(j++))=buffer[i++];
}
*(s+j)=0;
return(1);
}
---------------8<-------cut me loose!-------------->8--------------------------
|