Created
March 18, 2013 22:42
-
-
Save arnobroekhof/5191526 to your computer and use it in GitHub Desktop.
Generate and email a new password with puppet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# generatepassword.rb | |
# | |
module Puppet::Parser::Functions | |
newfunction(:generatepassword, :type => :rvalue, :doc => <<-EOS | |
This converts a string to a salted-SHA512 password hash for linux and unix systems | |
given any simple string, you will get a sha512 hash that can be insert directly into | |
the shadow file. | |
EOS | |
) do |arguments| | |
require 'digest/sha2' | |
require 'net/smtp' | |
def send_email(to,opts={}) | |
opts[:server] ||= 'localhost' | |
opts[:from] ||= 'email@example.com' | |
opts[:from_alias] ||= 'Example Emailer' | |
opts[:subject] ||= "Password change" | |
opts[:body] ||= "Important stuff!" | |
msg = <<END_OF_MESSAGE | |
From: #{opts[:from_alias]} <#{opts[:from]}> | |
To: <#{to}> | |
Subject: #{opts[:subject]} | |
#{opts[:body]} | |
END_OF_MESSAGE | |
Net::SMTP.start(opts[:server]) do |smtp| | |
smtp.send_message msg, opts[:from], to | |
end | |
end | |
raise(Puppet::ParseError, "generatepassword(): Wrong number of arguments " + | |
"passed (#{arguments.size} but we require 1)") if arguments.size != 1 | |
password = rand(36**15).to_s(36) | |
hostname = lookupvar('fqdn') | |
unless password.is_a?(String) | |
raise(Puppet::ParseError, 'generatepassword(): Requires a ' + | |
"String argument, you passed: #{password.class}") | |
end | |
salt = rand(36**8).to_s(36) | |
shadow_hash = password.crypt("$6$" + salt) | |
send_email "root@localhost", :body => "The new password is #{password}", :subject => "Password change #{hostname}" | |
return shadow_hash | |
end | |
end | |
# vim: set ts=2 sw=2 et : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment