· 8 years ago · Feb 28, 2018, 04:38 PM
1#!/usr/bin/env ruby
2
3# author: Cies Breijs (cies on the kde.nl domain), 2009/jan, with ruby-1.8.7
4
5# a simple authentication script to be used with some like mod_authnz_external
6# auths a login/password pair against google's pop3 service
7
8# many organizations use google (gmail) apps lately...
9# why organize authentication for people if they can just use their
10# google account credentials?
11
12# # # authentication, done with an external script (authing against gmail)
13# # AddExternalAuth googlepop /srv/ssmtp_gmail_auth/ssmtp_gmail_auth.rb
14# # SetExternalAuthMethod googlepop pipe
15# # <Location />
16# # AuthType Basic
17# # AuthName "Welcome to the authenticated domain"
18# # AuthBasicProvider external
19# # AuthExternal googlepop
20# # Require valid-user
21# # </Location>
22
23# make sure you use SSL on the particular website you are securing
24# otherwise your login info if flying plain text over the net
25
26require 'rubygems'
27require 'tlsmail' # install this: sudo gem install tlsmail
28
29WHITELIST = [['cies', 'password']]
30ACCEPTED_DOMAINS = ["blabla.net", "someotherdomain.co.com"]
31
32
33# Get the login/password from the stdin
34@login = STDIN.readline.strip.downcase
35@pass = STDIN.readline.strip
36
37# proper dieing with a message, from can be :success or :failed
38def die(from, msg)
39 STDERR.puts "[#{Time.now.to_s}] #{$0} #{(from == :success ? 'SUCCESS':'FAILED')} (#{@login}), #{msg}"
40 exit 0 if from == :success # strange to have 0 for success, but ok
41 exit 1
42end
43
44die(:success, 'whitelisted') if WHITELIST.include? [@login, @pass]
45
46die(:failed, 'invalid domain') unless /(@#{ACCEPTED_DOMAINS.join('$|@')}$)/ =~ @login
47
48begin
49 Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
50 if s = Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @login, @pass, :login)
51 s.finish
52 die(:success, 'authenticated against gmail')
53 end
54rescue # login errors always throw an exception
55 die(:failed, "#{$!.class} -- #{$!.to_str[0..44]}")
56end
57
58die(:failed, 'UNDEFINED ERROR, should not happen...')