Freeradius-Perl script using POP3 against Gmail - perl

has anyone successfully authenticate against gmail using pop3 perl scripts in freeradiu 3? I'm following this tutorial(https://kerker.website/freeradiusgmail802-1x%e8%a8%ad%e5%ae%9apop3s/) but perl continues rejecting the user.
This is the script:
use Data::Dumper;
use Mail::POP3Client;
use IO::Socket::SSL;
sub authenticate {
my $pop = Mail::POP3Client->new(
USER => $RAD_REQUEST{'User-Name'},
PASSWORD => $RAD_REQUEST{'User-Password'},
HOST => "pop.gmail.com",
USESSL => 1,
DEBUG => 1,
);
if($pop->Connect()){
return RLM_MODULE_OK;
}else{
return RLM_MODULE_REJECT;
}
$pop->Close;
}
I can't post the output right now but it rejects the user and I haven't been able to debug the script. I just want to know if anyone has recently sucedded in authenticating gmail and how.

Related

Twitter LED Timeline

Hello I have put together a script for a twitter timeline it works apart from i dont know how to authorize my twitter api key my led sign is just saying "Bad Authentication data"
here is my code
#!/usr/bin/perl
require LWP::UserAgent;
use JSON;
my $lwpua = LWP::UserAgent->new;
my $uagent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my #header = ( 'Referer' => 'http://api.twitter.com/', 'User-Agent' => $uagent );
my $twuser = '<twitter_name>';
my $twurl = "http://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$twuser";
my $response = $lwpua->get( $twurl, #header );
my $return = $response->content;
my $json = JSON->new->allow_nonref;
my $json_text = $json->decode($return);
my #tweets = #{$json_text};
my $message;
foreach $tweet (#tweets) {
$message .= $tweet->{text} . "\n";
}
use Device::MiniLED;
my $sign = Device::MiniLED->new( devicetype => "sign" );
$sign->addMsg(
data => "$message",
effect => "scroll",
speed => 4
);
$sign->send( device => "/dev/ttyUSB0" );
1;
First: use strict; and use warnings;. Even if you're the awesomest programmer ever, this should be your first port of call if you're having problems. (And everyone makes typos).
Secondly: $json_text is a hash ref, not an array ref. You probably want to use values or similar.
Thirdly: Bad Authentication Data is a twitter api error, not a code error. You need to authorize it with oAuth, and you're doing no twitter auth at all. From: https://dev.twitter.com/overview/api/response-codes
215 - Typically sent with 1.1 responses with HTTP code 400. The method requires authentication but it was not presented or was wholly invalid.
E.g. you can't do what you're doing without authenticating. I think what you need is this:
https://dev.twitter.com/oauth/overview/application-owner-access-tokens
Specifically - the 'easy answer' is generate an account specific authentication token, and send that in your request.
Once you have done this this web page on Twitter:
https://dev.twitter.com/oauth/tools/signature-generator/
allows you to generate a (time limited) example command that you could use to fetch your timeline. But you'll most likely need to build authentication into your script in order to do what you're trying to do. (user_timeline.json is a restricted access API). (There's a 'test oAuth' button on the app webpage).
Reading through the docs on creating authentication tokens, makes me thing that installing Net::Twitter or Net::Twitter::Lite might be the way to go.
First follow the instructions here: https://dev.twitter.com/oauth/overview/application-owner-access-tokens
Specifically, on https://apps.twitter.com/ you need to:
create an application
generate a token (from the application page).
(Under 'keys and access tokens' on the app specific page).
This will give you the 4 things you need to speak to twitter:
a consumer key
a consumer secret
an access token
an access token secret
By default, your access token will be read only. This is fine for what you want to do.
The process for turning them into Twitter auth is a bit complicated and involves RSA-HMAC encryption. So just let Net::Twitter do it for you: (I've removed my keys, because I'm not quite daft enough to post the equivalent of my Twitter password)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Net::Twitter;
my $twitter = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => 'long_string_of_stuff',
consumer_secret =>
'long_string_of_stuff',
access_token => '12345-long_string_of_stuff',
access_token_secret =>
'long_string_of_stuff',
ssl => 1,
);
my $tweets = $twitter->user_timeline();
print Dumper \$result;
my $message;
foreach my $tweet ( #{$tweets} ) {
$message .= $tweet->{text} . "\n";
}
print $message;
Tested this with my account, and it prints a list of my recent tweets, which I think was what you wanted?

connect to localhost failed (Connection refused) no (more) retries

I want to send an email using perl ,but when i execute the command as follows:
#./sendmail.sh "par1" "par2" "par3"
i got the error msg "connect to localhost failed (Connection refused) no (more) retries"
sendmail.sh:
/usr/bin/perl /code/sendmail.pl "$1" "$2" "$3";
sendmail.pl:
#!/usr/bin/perl -w
use Mail::Sendmail;
my $event1 = shift(#ARGV);
my $event2 = shift(#ARGV);
my $time = shift(#ARGV);
#my $info = shift(#ARGV);
my $datetime = `/bin/date "+20%y-%m-%d %H:%M:%S"`;
chomp $datetime;
$msg = "This is Monitor System speak:\n
The system discovers the events at $datetime.
Something may be abnormal, please check it. The detail is below:\n";
$msg = $msg."$event1 and $event2 at $time\n";
$msg = $msg."\n";
$msg = $msg."Any problem, check it from http://map_test.php\n\n\n";
$mail_subject = "Abnormal";
sendmail(
From => 'localhost',
To => 'test#mail.com',
Subject => $mail_subject,
Message => $msg,
);
Any help appreciated.
smtp stands for simple mail transfer protocol.
When you need to send an email your mail client needs to talk to an smtp server which will accept the message. Normally your internet service provider will provide an smtp host. If you look at your mail client it will need to have an smtp server configured to be able to send mail.
Ok so when you install the Mail::Sendmail module, it doesn't know what your smtp server will be. It is up to you to tell it. It provides a default of localhost which would often be true if your server is running a sendmail daemon.
The configuration of Mail::Sendmail is stored in a variable called
%Mail::Sendmail::mailcfg
You can change the value of the sendmail server using this snippet of code:
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
You need to add this line of code to your script to set the smtp server.
It adds this server to an array which also includes localhost.
So if neither of the hosts work it will still print an error message about localhost which is slightly confusing.
If you use Data::Dumper to print the contents of the mailcfg variable it will look something like this:
#!/usr/bin/perl
use Mail::Sendmail;
use Data::Dumper;
unshift #{$Mail::Sendmail::mailcfg{'smtp'}} , 'my.smtp.server';
print Dumper(\%Mail::Sendmail::mailcfg);
Should return:
$VAR1 = {
'retries' => 1,
'smtp' => [
'my.smtp.server',
'localhost'
],
'delay' => 1,
'port' => 25,
'from' => '',
'debug' => 0,
'tz' => '',
'mime' => 1
};

Trying to send authenticated email using Net::SMTP::SSL

I'm attempting to send an authenticated email with the Net::SMTP::SSL module to a comcast email server. I'm using the following code.
#!/usr/bin/perl
use Net::SMTP::SSL;
use MIME::Base64;
$smtp = Net::SMTP::SSL->new
(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
);
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();
# Mailbox info
$smtp->datasend(encode_base64('username')); # username
$smtp->response();
$smtp->datasend(encode_base64('password')); # password
$smtp->response();
# Email from
$smtp->mail('user\#comcast.net');
# Email to
$smtp->to('user\#host.com');
$smtp->data();
$smtp->datasend("To: user\#host.com\n");
$smtp->datasend("From: user\#comcast.net\n");
$smtp->datasend("Subject: Test");
# Line break to separate headers from body
$smtp->datasend("\n");
$smtp->datasend("Blah\n");
$smtp->dataend();
$smtp->quit();
exit;
I'm basically following the code from here for comcast.
I've ran telnet and can connect to the smtp server on the port and I can issue the AUTH LOGIN and successfully login, but issuing the
$smtp->datasend("AUTH LOGIN\n");
always results in:
Can't call method "datasend" on an undefined value
I've also tried executing the auth method to login and that fails as well.
What am I missing here? I know it's something simple I'm overlooking.
Pretty much all of the Net::SMTP methods optionally set error codes, so I suspect that Net::SMTP::SSL does the same. Try the following for your constructor:
use Net::SMTP::SSL;
use MIME::Base64;
use strict;
use warnings;
my $smtp = Net::SMTP::SSL->new(
"smtp.comcast.net",
Hello => "host.comcast.net",
Port => 465,
Timeout => 30,
Debug => 1,
) or die "Failed to connect to mail server: $!";
Also, the fact that your smtp server is different than your Hello is a little suspect to me.
For email services that use STARTTLS, it's best to use the newer NET::SMTPS module. Try the following code:
my $msg = MIME::Lite ->new (
From => 'from#bellsouth.net',
To => 'to#bellsouth.net',
Subject => 'Test Message',
Data => 'This is a test',
Type => 'text/html'
);
my $USERNAME = 'from#bellsouth.net';
my $PASSWORD = 'abc123';
my $smtps = Net::SMTPS->new("smtp.mail.att.net", Port => 587, doSSL => 'starttls', SSL_version=>'TLSv1');
$smtps->auth ( $USERNAME, $PASSWORD ) or die("Could not authenticate with bellsouth.\n");
$smtps ->mail('from#bellsouth.net');
$smtps->to('to#bellsouth.net');
$smtps->data();
$smtps->datasend( $msg->as_string() );
$smtps->dataend();
$smtps->quit;
Originally from http://www.skipser.com/p/2/p/send-email-using-perl-via-live.com.html
Try this:
Email from
Change
$smtp->mail('user\#comcast.net'); to $smtp->mail('user#comcast.net'); without '\'
Email to
Change
$smtp->to('user\#host.com'); to $smtp->to('user#host.com'); without '\'

How can I connect to gmail from perl?

I am trying to read messages from a gmail account, and the examples I've seen aren't working.
I started with this:
use Mail::IMAPClient;
use IO::Socket::SSL;
my $user = 'user\#mydomain.com';
my $pwd = 'password';
my $socket = IO::Socket::SSL->new(
PeerAddr => 'imap.gmail.com',
PeerPort => 993,
SSL_verify_mode => SSL_VERIFY_PEER,
)
or die "socket(): $#";
my $client = Mail::IMAPClient->new(
Socket => $socket,
User => $user,
Password => $pwd,
)
or die "new(): $#";
if ( $client->IsAuthenticated() ) {
print "Auth OK\n";
} else {
print "No auth\n";
}
This appears to work, but never authenticates. According to the documentation, Mail::IMAPClient->new should call login if username and password are provided.
I have tried calling client->login with no difference.
There are a few questions with similar content, but the answers state to use a different package (Mail::Webmail::Gmail is one, but it seems obsolete and doesn't work either)
The account is a google apps account, not a regular gmail account. I have enabled imap access for the account.
I also tried using Net::IMAP::Client, both with a google apps account and using my gmail account with an app-specific password, and get nothing but "Invalid credentials".
use Net::IMAP::Client;
my $user = 'user\#gmail.com';
my $pwd = ' app password';
my $imap = Net::IMAP::Client->new(
server => 'imap.gmail.com',
user => $user,
pass => $pwd,
ssl => 1, # (use SSL? default no)
ssl_verify_peer => 0, # (use ca to verify server, default yes)
port => 993 # (but defaults are sane)
) or die "Could not connect to IMAP server: $_";
$imap->login or
die('Login failed: ' . $imap->last_error);
Is there something I'm missing when trying to connect to gmail imap?
In Perl, the string "\#" inside single quotes is literally "\#".
Lose the \.

How to receive a new email directly from email server in Perl

I have a an application sitting on a desktop and I want to run it directly through the trigger of an Email. Basically I want a script that would periodically fetch new mails from the server and trigger my script on receipt of a specific mail.
How should I proceed, or in other words which libraries in Perl can help?
I have gone through POP and IMAP manuals…I just don't know a library in Perl that could help me listen to a server.
Net::POP3 is a good place to start... this is an example from their manpage...
use Net::POP3;
$pop = Net::POP3->new('pop3host');
$pop = Net::POP3->new('pop3host', Timeout => 60);
if ($pop->login($username, $password) > 0) {
my $msgnums = $pop->list; # hashref of msgnum => size
foreach my $msgnum (keys %$msgnums) {
my $msg = $pop->get($msgnum);
print #$msg;
$pop->delete($msgnum);
}
}
$pop->quit;