13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2 ■ CENTRALIZING YOUR NETWORK: KERBEROS, LDAP, AND NFS0708 die "Usage is adduser.pl [username] ["realname"]\n" if length(@ARGV) != 1;0910 my $username = $ARGV[0];11 my $realname = $ARGV[1];1213 # Plain LDAP version if you prefer this to the LDAPS version14 # my $ldap = Net::LDAP->new( 'ldapserver.example.com');15 my $ldap = Net::LDAPS->new( 'ldapserver.example.com',16 verify => 'optional',17 cafile => '/etc/ldap/cacert.pem' ) or die $@;18 my $mesg = $ldap->bind;1920 $mesg = $ldap->search( base => "ou=people,dc=example,dc=com",21 filter => "(uid=$username)",22 );23 $mesg->code && die $mesg->error;2425 my $searchResults = $mesg->count;26 die "Error! Username already exists!" unless $searchResults == 0;2728 $mesg = $ldap->search ( base => "ou=people,dc=example,dc=com",29 attrs => [ 'uidNumber' ],30 );31 my @entries = $mesg->sorted('uidNumber');32 my $entry = pop @entries;33 my $newuid = $entry->get_value( 'uidNumber' );34 $newuid++;3536 my $result = $ldap->add("uid=$username,ou=people,dc=example,dc=com",37 attr => [ 'cn' => $realname,38 'uid' => $userid,39 'uidNumber' => $newuid,40 'mail' => '$username@example.com',41 'homeDirectory' => '/home/$username',42 'objectclass' => [ 'person', 'inetOrgPerson',43 'posixAccount' ]44 ]45 );4647 $mesg = $ldap->unbind;If using ldaps:///, you will need the CA certificate for the CA that signed your server’s certificate.For a proper CA (as opposed to self-signed), you should be able to use the capath (rather than cafile)attribute to set the directory where CA certificates live rather than needing to give a specific file.Line 17 performs an anonymous bind. If you want to bind with a specific DN (for example, toauthenticate as your admin user, if you’re not using Kerberos), you can provide extra options:my $adminuser = "cn=root,dc=example,dc=com"my $password = "mypasswd";$mesg = $ldap->bind( $adminuser, password => $password );50Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!