3 # Originally by Simon Tatham
4 # Modified by Richard Kettlewell, Colin Watson, Ian Jackson
6 # Copyright -2011 Simon Tatham
7 # Copyright 2011 Richard Kettlewell
8 # Copyright 2011 Colin Watson
9 # Copyright 2011 Ian Jackson
11 # Permission is hereby granted, free of charge, to any person obtaining a
12 # copy of this software and associated documentation files (the "Software"),
13 # to deal in the Software without restriction, including without limitation
14 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 # and/or sell copies of the Software, and to permit persons to whom the
16 # Software is furnished to do so, subject to the following conditions:
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 # SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 # DEALINGS IN THE SOFTWARE.
39 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
41 # set the version for version checking
45 @EXPORT = qw(cnntp_connect);
46 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
52 sub cnntp_connect ($) {
55 my $ns=$ENV{'NNTPSERVER'};
56 if (!defined $ns or !length $ns) {
57 $ns = `cat /etc/nntpserver`;
60 my $port = (getservbyname("nntp", "tcp"))[2];
62 my $proto = getprotobyname("tcp");
63 my $paddr = sockaddr_in($port, $ns);
65 my $sock = new IO::Handle;
66 socket($sock,PF_INET,SOCK_STREAM,$proto) or die "socket: $!";
67 connect($sock,$paddr) or die "connect: $!";
71 return bless { S => $sock, V => $verbose };
74 sub banner_reader ($) {
76 my ($code,$l) = $c->getline();
77 $code =~ /^2\d\d/ or die "no initial greeting from server\n";
78 $c->docmd("MODE READER");
90 print $v ">>> $line\n" if $v;
104 my $l = $c->getline_raw();
106 my $code = substr($l,0,3);
107 print $v "<<< $l\n" if $v;
112 my ($c,$cmd,$nocheck) = @_;
116 ($code,$l) = $c->getline();
117 if ($code eq "480") { $c->auth(); } else { last; }
120 $code =~ /^2\d\d/ or die "failed on `$cmd':\n$l\n";
128 return if $c->{Authed}++;
129 my $auth = $ENV{"NNTPAUTH"};
131 $c->putline("AUTHINFO GENERIC $auth");
132 pipe AUTHSTDIN, TOAUTH or die "unable to create pipes";
133 pipe FROMAUTH, AUTHSTDOUT or die "unable to create pipes";
137 die "unable to fork for authentication helper";
138 } elsif ($pid == 0) {
140 $c->{V} = undef if $c->{V} eq 'STDOUT';
141 $ENV{"NNTP_AUTH_FDS"} = "0.1";
142 open STDIN, "<&AUTHSTDIN";
143 open STDOUT, ">&AUTHSTDOUT";
152 my ($code,$l) = $c->getline(); print TOAUTH "$l\n";
156 ($code,$l) = $c->getline();
159 die "failed authentication\n" unless $? == 0;