Last active
December 28, 2015 09:48
-
-
Save shutingrz/7481220 to your computer and use it in GitHub Desktop.
LDAP in ConvivialNet.
adduser.
v0.0.3
This file contains hidden or 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/env ruby | |
| # | |
| # | |
| #version 0.0.1 | |
| # author alvise | |
| # description | |
| # "first version." | |
| # | |
| #version 0.0.2 | |
| # author shu | |
| #****** change log ************************ | |
| # | |
| # +{SHA} => {SSHA} | |
| # ++un used "digest/sha1" and use "slappasswd back quote" | |
| # | |
| # +add userPassword validation. | |
| # | |
| # +add rootdn input. | |
| # ++add rootdn validation | |
| # | |
| #****************************************** | |
| # | |
| #version 0.0.3 | |
| # author shu | |
| #****** change log ************************ | |
| # | |
| # +add mail of index. | |
| # +"mail" => [ uid.chomp + "@convivial.ne.jp"], | |
| # | |
| #****************************************** | |
| require "rubygems" | |
| require "ldap" | |
| require "optparse" | |
| Version = "0.0.2" | |
| opt = OptionParser.new | |
| opt.banner = "Usage: ldap_deluser.rb [option] uid" | |
| opt.on_tail("-h", "--help", "Show this message") do | |
| puts opt | |
| exit | |
| end | |
| opt.on_tail("--version", "Show Version") do | |
| puts Version | |
| exit | |
| end | |
| puts "Please enter the following" | |
| #rootdn validation | |
| begin | |
| system "stty -echo" | |
| print "rootdn Password:" | |
| rootdnpassword = gets | |
| system "stty echo" | |
| puts | |
| @validrootdn= LDAP::Conn.new('localhost', 389) | |
| @validrootdn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION,3) | |
| @validrootdn.bind("cn=Manager,dc=convivial,dc=ne,dc=jp", rootdnpassword.chomp) | |
| rescue LDAP::ResultError | |
| print "rootdn Password is not match. Please again.\n\n" | |
| retry | |
| ensure | |
| @validrootdn.unbind() | |
| end | |
| print "New UserID:" | |
| uid = gets | |
| #Password with validation | |
| while 1 do | |
| system "stty -echo" | |
| print "Password:" | |
| userpassword = gets | |
| system "stty echo" | |
| puts | |
| system "stty -echo" | |
| print "Retype Password:" | |
| retypepassword = gets | |
| system "stty echo" | |
| puts | |
| if userpassword == retypepassword then | |
| break | |
| end | |
| print "Password is not match. Please again.\n\n" | |
| end | |
| print "FirstName:" | |
| givenname = gets | |
| print "FamilyName:" | |
| sn = gets | |
| gidnumber = 5000 | |
| #make ssha by slappasswd | |
| userpassword = `slappasswd -h '{SSHA}' -s #{userpassword}` | |
| homedirectory = "/home/#{uid}" | |
| #platform is freebsd. | |
| loginshell = "/bin/csh" | |
| dn = "uid=#{uid},ou=People,dc=convivial,dc=ne,dc=jp" | |
| @conn = LDAP::Conn.new('localhost', 389) | |
| @conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION,3) | |
| begin | |
| @conn.bind("cn=Manager,dc=convivial,dc=ne,dc=jp", rootdnpassword.chomp) | |
| @conn.search("ou=People,dc=convivial,dc=ne,dc=jp", | |
| LDAP::LDAP_SCOPE_SUBTREE, | |
| "(objectClass=*)") do |entry| | |
| uidtmp = entry["uidNumber"] | |
| @uidmax = 0 | |
| if uidtmp != nil and @uidmax <= uidtmp[0].to_i then | |
| @uidmax = uidtmp[0].to_i | |
| end | |
| end | |
| uidnumber = @uidmax + 1 | |
| entry ={ | |
| "objectClass" => ["posixAccount", "top", "inetOrgPerson", "shadowAccount"], | |
| "uid" => [uid.chomp], | |
| "userPassword" => [userpassword], | |
| "gidNumber" => [gidnumber.to_s.chomp], | |
| "uidNumber" => [uidnumber.to_s.chomp], | |
| "givenName"=> [givenname.chomp], | |
| "sn" => [sn.chomp], | |
| "cn" => [givenname.chomp + " " + sn.chomp], | |
| "mail" => [ uid.chomp + "@convivial.ne.jp"], | |
| "homeDirectory" => [homedirectory.chomp], | |
| "loginShell" => [loginshell.chomp], | |
| "shadowFlag" => ["0"], | |
| "shadowMin" => ["0"], | |
| "shadowMax" => ["99999"], | |
| "shadowWarning" => ["0"], | |
| "shadowInactive" => ["99999"], | |
| "shadowLastChange" => ["12011"], | |
| "shadowExpire" => ["99999"]} | |
| @conn.add(dn, entry) | |
| rescue => e | |
| p e | |
| ensure | |
| @conn.unbind() | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment