Created
September 27, 2010 13:11
-
-
Save jaspervdj/598989 to your computer and use it in GitHub Desktop.
Script to add users to the ZeusWPI LDAP
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
#!/usr/bin/ruby | |
# Script to add a new user in the Zeus LDAP | |
require 'fileutils' | |
# Find a new user id | |
# | |
def find_new_user_id | |
id = 1200 | |
while not `getent passwd #{id}`.empty? do id = id + 1 end | |
return id | |
end | |
# Create an ldif that can be imported intro the database | |
# | |
def create_ldif user_name, first_name, last_name, password | |
return <<-EOS.gsub(/^ {4}/, "") | |
version: 1 | |
dn: uid=#{user_name},ou=People,dc=zeus,dc=UGent,dc=be | |
cn: #{first_name} #{last_name} | |
gecos: #{first_name} #{last_name} | |
gidnumber: 103 | |
givenname: #{first_name} | |
homedirectory: /home/trial/#{user_name} | |
loginshell: /bin/bash | |
mail: #{user_name}@zeus.UGent.be | |
objectclass: person | |
objectclass: organizationalPerson | |
objectclass: inetOrgPerson | |
objectclass: posixAccount | |
objectclass: top | |
objectclass: shadowAccount | |
shadowlastchange: 14875 | |
shadowmax: 99999 | |
shadowwarning: 7 | |
sn: #{last_name} | |
uid: #{user_name} | |
uidnumber: #{find_new_user_id} | |
userpassword: #{password} | |
EOS | |
end | |
# Generate a random password | |
# | |
def random_password | |
chars = ("a".."z").to_a | |
newpass = "" | |
1.upto(6) do | |
|i| newpass << chars[rand(chars.size-1)] | |
end | |
return newpass | |
end | |
# Create a home directory and put a .forward file in it | |
# | |
def initialize_home user_name, email | |
home_dir = "/home/trial/#{user_name}" | |
FileUtils::mkdir_p home_dir | |
FileUtils::chown "#{user_name}", "users", home_dir | |
File.open "#{home_dir}/.forward", 'w' do |file| | |
file.write email | |
end | |
end | |
# Add a user to the mailing list | |
# | |
def add_to_mailing_list user_name | |
email = "#{user_name}@zeus.ugent.be" | |
`echo "#{email}" | /var/lib/mailman/bin/add_members -w n -r - leden` | |
end | |
# Mail a user about his password | |
# | |
def send_password_mail user_name, email, password | |
mail_body = <<-EOS.gsub(/^ {4}/, "") | |
Uw zeus account (#{user_name}) is aangemaakt en uw wachtwoord is ingesteld | |
op #{password}. Gelieve in te loggen via ssh op zeus.ugent.be, poort 2222 | |
en met het commando passwd uw wachtwoord te veranderen. In geval van | |
problemen of vragen, mail naar jasper@zeus.ugent.be. | |
EOS | |
`echo '#{mail_body}' | mail -s "Uw Zeus Account" #{email}` | |
end | |
# Simple prompt | |
# | |
def prompt title | |
print "#{title}: " | |
gets.chop | |
end | |
# Check if a given user exists | |
# | |
def user_exists user_name | |
not `id -u #{user_name} 2>/dev/null`.empty? | |
end | |
# Main function, sorta | |
# | |
def main | |
# Get a username | |
user_name = prompt "User name" | |
if user_exists user_name then | |
puts "User #{user_name} already exists." | |
exit 1 | |
end | |
# Read simple fields | |
first_name = prompt "First name" | |
last_name = prompt "Last name" | |
email = prompt "Email" | |
# Generate a password | |
password = random_password | |
# Create the ldif file | |
ldif = create_ldif(user_name, first_name, last_name, password) | |
File.open "temp.ldif", "w" do |file| | |
file.write(ldif) | |
end | |
# Import the ldif file, then remove it | |
`ldapadd -v -x -W -D cn=root,dc=zeus,dc=ugent,dc=be -f temp.ldif` | |
FileUtils::rm "temp.ldif" | |
# Wait a bit | |
puts "Waiting for user..." | |
until user_exists user_name do sleep 1 end | |
# Various administrative tasks | |
initialize_home user_name, email | |
add_to_mailing_list user_name | |
send_password_mail user_name, email, password | |
end | |
# Run | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment