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
|
#if !defined(lint) && !defined(SABER) && !defined(GCC_WALL)
static char XRNrcsid[] = "$Id: clientlib.c,v 1.19 1998/07/10 13:38:43 jik Exp $";
#endif
/* #define DEBUG */
/*
* nntp client interface
*
* Copyright (c) 1988-1993, The Regents of the University of California.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the University of California not
* be used in advertising or publicity pertaining to distribution of
* the software without specific, written prior permission. The University
* of California makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* THE UNIVERSITY OF CALIFORNIA DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include "utils.h"
#include <X11/Xos.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef notdef
#if defined(AF_DECnet) && defined(ultrix)
# ifndef DECNET
# define DECNET
# endif
#endif
#endif /* notdef */
#ifdef DECNET
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#endif
#include "codes.h"
#include "news.h"
#include "server.h"
#include "internals.h"
#include "clientlib.h"
FILE *ser_rd_fp = NULL;
FILE *ser_wr_fp = NULL;
char *server_init_msg = NULL;
char *nntp_port = NULL;
#ifdef DECNET
static int get_dnet_socket _ARGUMENTS((char *));
#endif
static int get_tcp_socket _ARGUMENTS((char *));
int get_server_init_msg _ARGUMENTS((char **));
/*
* getserverbyfile Get the name of a server from a named file.
* Handle white space and comments.
* Use NNTPSERVER environment variable if set.
*
* Parameters: "file" is the name of the file to read.
*
* Returns: Pointer to static data area containing the
* first non-ws/comment line in the file.
* NULL on error (or lack of entry in file).
*
* Side effects: None.
*/
char * getserverbyfile(file)
char *file;
{
char *cp;
static char buf[256];
if ((cp = getenv("NNTPSERVER")) != 0) {
(void) strcpy(buf, cp);
} else {
cp = getinfofromfile(file);
if (cp == NULL) {
return(NULL);
} else {
(void) strcpy(buf, cp);
}
}
return (buf);
}
/*
* server_init Get a connection to the remote news server.
*
* Parameters: "machine" is the machine to connect to.
*
* Returns: -1 on error
* server's initial response code on success.
*
* Side effects: Connects to server.
* "ser_rd_fp" and "ser_wr_fp" are fp's
* for reading and writing to server.
*/
int server_init(machine)
char *machine;
{
#ifdef NNTPVIATIP
char line[256];
#else
int sockt_rd, sockt_wr;
#endif
int retval, retval2;
char *mode_init_msg;
#ifdef DECNET
char *cp;
#endif /* DECNET */
#ifdef NNTPVIATIP
ser_rd_fp = fdopen(3, "r"); /* 3 is dictated by tip(1) ~C command */
ser_wr_fp = fdopen(4, "r"); /* 4 is dictated by tip(1) ~C command */
(void) sprintf(line, "telnet %s %d", machine, 119);
put_server(line);
get_server(line, sizeof(line)); /* Trying ... */
get_server(line, sizeof(line)); /* Connected to ... */
get_server(line, sizeof(line)); /* Escape character is ... */
#else /* NNTPVIATIP */
#ifdef DECNET
cp = index(machine, ':');
if (cp && cp[1] == ':') {
*cp = '\0';
sockt_rd = get_dnet_socket(machine);
} else
sockt_rd = get_tcp_socket(machine);
#else /* DECNET */
sockt_rd = get_tcp_socket(machine);
#endif /* DECNET */
if (sockt_rd < 0)
return (-1);
/*
* Now we'll make file pointers (i.e., buffered I/O) out of
* the socket file descriptor. Note that we can't just
* open a fp for reading and writing -- we have to open
* up two separate fp's, one for reading, one for writing.
*/
if ((ser_rd_fp = fdopen(sockt_rd, "r")) == NULL) {
perror("server_init: fdopen #1");
return (-1);
}
sockt_wr = dup(sockt_rd);
if ((ser_wr_fp = fdopen(sockt_wr, "w")) == NULL) {
perror("server_init: fdopen #2");
(void) fclose(ser_rd_fp);
ser_rd_fp = NULL; /* from above */
return (-1);
}
#endif /* NNTPVIATIP */
if (server_init_msg) {
XtFree(server_init_msg);
server_init_msg = 0;
}
if ((retval = get_server_init_msg(&server_init_msg)) < 0) {
return (retval);
}
/*
Make sure we're in READER mode. Now, you may be saying to
yourself, "Why is this necessary? The only news server that
supports the MODE command is INN, and if the person
compiling this program is using INN, he's going to link
against the INN client libraries anyway, so this function
won't ever be used." Well, first of all, other news servers
are probably going to support the MODE command at some point
in the future. Second, someone may want to compile XRN
without access to the INN client libraries and still talk to
an INN server.
In any case, it doesn't hurt to send the MODE command and
see what happens.
*/
put_server("MODE READER");
if ((retval2 = get_server_init_msg(&mode_init_msg)) < 0) {
XtFree(server_init_msg);
server_init_msg = 0;
return (retval2);
}
if ((*mode_init_msg == CHAR_OK) || (retval2 == ERR_ACCESS)) {
XtFree(server_init_msg);
server_init_msg = mode_init_msg;
return (retval2);
}
else {
XtFree(mode_init_msg);
return (retval);
}
}
/*
* get_server_init_msg - get a banner message from the server.
*
* Parameters: "msg" a pointer into which the message will be
* placed. It should be freed when it is no
* longer needed.
*
* Returns: -1 on error or the server response code.
*
* Side effects: Reads data from server, closes server
* connection on error.
*
* Errors: Printed to stderr.
*/
int get_server_init_msg(in_msg)
char **in_msg;
{
char line[256];
int length;
char *msg;
msg = XtMalloc(1024);
*msg = '\0';
length = 1024;
do {
if (get_server(line, sizeof(line)) == -1) {
if (feof(ser_rd_fp)) {
fprintf(stderr, "Unexpected EOF on NNTP server socket -- server is probably malfunctioning.\n");
}
else {
perror("reading from server");
}
XtFree(msg);
(void) fclose(ser_rd_fp);
(void) fclose(ser_wr_fp);
ser_rd_fp = ser_wr_fp = NULL;
return (-1);
}
while (strlen(line)+strlen(msg)+1 >= (unsigned) length) {
length += 1024;
msg = XtRealloc(msg, length);
}
strcat(msg, line);
strcat(msg, "\n");
} while (line[3] == '-');
*in_msg = msg;
return (atoi(line));
}
/*
* get_tcp_socket -- get us a socket connected to the news server.
*
* Parameters: "machine" is the machine the server is running on.
*
* Returns: Socket connected to the news server if
* all is ok, else -1 on error.
*
* Side effects: Connects to server.
*
* Errors: Printed via perror.
*
* This is a rewrite of the original get_tcp_socket. This rewrite was
* supplied by Casey Leedom <casey@gauss.llnl.gov>
*/
static int get_tcp_socket1 _ARGUMENTS((struct sockaddr_in *));
static int get_tcp_socket(machine)
char *machine;
{
int s;
#ifdef h_addr
register char **cp;
#endif /* h_addr */
static int port = 0;
struct sockaddr_in sin;
struct servent *getservbyname _ARGUMENTS((CONST char *, CONST char *));
struct servent *sp;
struct hostent *gethostbyname _ARGUMENTS((CONST char *)), *hp;
(void) memset((char *) &sin, 0, sizeof(sin));
if (! port)
if (nntp_port)
port = htons(atoi(nntp_port));
else {
if ((sp = getservbyname("nntp", "tcp")) == NULL) {
(void) fprintf(stderr, "nntp/tcp: Unknown service.\n");
return (-1);
}
port = sp->s_port;
}
sin.sin_port = port;
if ((sin.sin_addr.s_addr = inet_addr(machine)) != -1) {
sin.sin_family = AF_INET;
return(get_tcp_socket1(&sin));
} else {
if ((hp = gethostbyname(machine)) == NULL) {
(void) fprintf(stderr, "%s: Unknown host.\n", machine);
return (-1);
}
sin.sin_family = hp->h_addrtype;
}
/*
* The following is kinda gross. The 4.3BSD hostent structure
* contains a list of addresses, each of which should be tried in
* turn if the previous one fails. However, 4.2 hostent structure
* doesn't have this list of addresses. Under 4.3, h_addr is a
* #define to h_addr_list[0]. We use this to figure out whether to
* include the NS specific code ...
*/
#ifdef h_addr
/* attempt multiple addresses */
for (s = 0, cp = hp->h_addr_list; *cp; cp++) {
if (s < 0)
(void) fprintf(stderr,
"trying alternate address for %s\n",
machine);
(void) memcpy((char *) &sin.sin_addr, *cp, hp->h_length);
s = get_tcp_socket1(&sin);
if (s >= 0)
return(s);
}
(void) fprintf(stderr, "giving up ...\n");
return(-1);
#else /* 4.2BSD hostent structure */
(void) memcpy((char *) &sin.sin_addr, hp->h_addr, hp->h_length);
return(get_tcp_socket1(&sin));
#endif
}
static int get_tcp_socket1(sp)
struct sockaddr_in *sp;
{
int s;
s = socket(sp->sin_family, SOCK_STREAM, 0);
if (s < 0) {
(void) perror("socket");
return (-1);
}
if (connect(s, (struct sockaddr *)sp, sizeof (*sp)) < 0) {
(void) perror("connect");
(void) close(s);
return(-1);
}
return(s);
}
#ifdef DECNET
/*
* get_dnet_socket -- get us a socket connected to the news server.
*
* Parameters: "machine" is the machine the server is running on.
*
* Returns: Socket connected to the news server if
* all is ok, else -1 on error.
*
* Side effects: Connects to server.
*
* Errors: Printed via nerror.
*/
static int get_dnet_socket(machine)
char *machine;
{
int s, area, node;
struct sockaddr_dn sdn;
struct nodeent *getnodebyname(), *np;
(void) memset((char *) &sdn, 0, sizeof(sdn));
switch (s = sscanf( machine, "%d%*[.]%d", &area, &node )) {
case 1:
node = area;
area = 0;
case 2:
node += area*1024;
sdn.sdn_add.a_len = 2;
sdn.sdn_family = AF_DECnet;
sdn.sdn_add.a_addr[0] = node % 256;
sdn.sdn_add.a_addr[1] = node / 256;
break;
default:
if ((np = getnodebyname(machine)) == NULL) {
(void) fprintf(stderr,
"%s: Unknown host.\n", machine);
return (-1);
} else {
(void) memcpy((char *) sdn.sdn_add.a_addr,
np->n_addr,
np->n_length);
sdn.sdn_add.a_len = np->n_length;
sdn.sdn_family = np->n_addrtype;
}
break;
}
sdn.sdn_objnum = 0;
sdn.sdn_flags = 0;
sdn.sdn_objnamel = strlen("NNTP");
(void) memcpy(&sdn.sdn_objname[0], "NNTP", sdn.sdn_objnamel);
if ((s = socket(AF_DECnet, SOCK_SEQPACKET, 0)) < 0) {
nerror("socket");
return (-1);
}
/* And then connect */
if (connect(s, (struct sockaddr *) &sdn, sizeof(sdn)) < 0) {
nerror("connect");
close(s);
return (-1);
}
return (s);
}
#endif /* DECNET */
/*
* handle_server_response
*
* Print some informative messages based on the server's initial
* response code. This is here so inews, rn, etc. can share
* the code.
*
* Parameters: "response" is the response code which the
* server sent us, presumably from "server_init",
* above.
* "server" is the news server we got the
* response code from.
*
* Returns: -1 if the error is fatal (and we should exit).
* 0 otherwise.
*
* Side effects: None.
*/
int handle_server_response(response, server)
int response;
char *server;
{
switch (response) {
case OK_NOPOST: /* fall through */
printf(
"NOTE: This machine does not have permission to post articles.\n");
printf(
" Please don't waste your time trying.\n\n");
case OK_CANPOST:
return (0);
case ERR_ACCESS:
printf(
"This machine does not have permission to use the %s news server.\n",
server);
return (-1);
case ERR_GOODBYE:
printf(
"The news service is currently not available from %s:\n\n",
server);
fputs(server_init_msg, stdout);
return(-1);
default:
printf("Unexpected response code from the %s news server.\n",
server);
if (server_init_msg)
printf("\n%s", server_init_msg);
return (-1);
}
/*NOTREACHED*/
}
/*
* put_server -- send a line of text to the server, terminating it
* with CR and LF, as per ARPA standard.
*
* Parameters: "string" is the string to be sent to the
* server.
*
* Returns: Nothing.
*
* Side effects: Talks to the server.
*
* Note: This routine flushes the buffer each time
* it is called. For large transmissions
* (i.e., posting news) don't use it. Instead,
* do the fprintf's yourself, and then a final
* fflush.
*/
void put_server(string)
char *string;
{
#ifdef DEBUG
(void) fprintf(stderr, ">>> %s\n", string);
#endif
(void) fprintf(ser_wr_fp, "%s\r\n", string);
(void) fflush(ser_wr_fp);
#ifdef NNTPVIATIP
{
char line[256];
get_server(line, sizeof(line));
}
#endif
}
/*
* get_server -- get a line of text from the server. Strips
* CR's and LF's.
*
* Parameters: "string" has the buffer space for the
* line received.
* "size" is the size of the buffer.
*
* Returns: -1 on error, 0 otherwise.
*
* Side effects: Talks to server, changes contents of "string".
*/
int get_server(string, size)
char *string;
int size;
{
register char *cp;
if (fgets(string, size, ser_rd_fp) == NULL) {
return (-1);
}
#ifdef NNTPVIATIP
for (cp = string; cp < &string[size]; cp++) {
*cp = *cp & 0x7F;
if (*cp == '\0') {
break;
}
}
#endif
cp = &string[strlen(string)];
if ((cp >= &string[2]) && (cp[-2] == '\r') && (cp[-1] == '\n'))
cp[-2] = '\0';
#ifdef DEBUG
(void) fprintf(stderr, "<<< %s\n", string);
#endif
return (0);
}
/*
* close_server -- close the connection to the server, after sending
* the "quit" command.
*
* Parameters: None.
*
* Returns: Nothing.
*
* Side effects: Closes the connection with the server.
* You can't use "put_server" or "get_server"
* after this routine is called.
*/
void close_server()
{
char ser_line[256];
if (ser_wr_fp == NULL || ser_rd_fp == NULL)
return;
put_server("QUIT");
(void) get_server(ser_line, sizeof(ser_line));
(void) fclose(ser_wr_fp);
(void) fclose(ser_rd_fp);
ser_wr_fp = ser_rd_fp = NULL;
}
|