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 NFSOr you can use an SASL mechanism by giving an Authen::SASL object as an argument:$mesg = $ldap->bind( "cn=root,dc=example,dc=com", sasl => $sasl );Lines 19–25 search the directory for an existing user with the given UID. Net::LDAP::Search acts as acontainer for the results of an LDAP search, and there is a Net::LDAP::Entry for each individual result.In this example, you’re checking to make sure that the username doesn’t already exist, so all youneed do is to find the number of entries returned. If it’s 0, the username is free. Line 24 handles this: thecount method gives the number of entries returned. $mesg here is a Net::LDAP::Search object.■ Note Numerous other methods are available for interacting with a Net::LDAP::Search object. You can retrievean entry by number from the Net::LDAP::Search container, sort the entries, pop off an entry at a time, or returnan array of all the Net::LDAP::Entry objects found by the search.The search method will accept attribute specifications, just as ldapsearch will (see recipe 2-7):$mesg = $ldap->search( filter => "(uid=jkemp)",base =>"ou=people,dc=example,dc=com",attrs => ['uid', 'cn', 'homeDirectory'] );This will return the uid, cn, and homeDirectory of the user jkemp.To look at the entries returned, you use the Net::LDAP::Entries methods. Lines 27–33 look for thenext free user ID to allocate to our user. All the users are returned, as a Net::LDAP::Search object (acontainer of Net::LDAP::Entries objects), and then are sorted by uidNumber and dumped into an array ofNet::LDAP::Entry objects (line 30). Line 31 pops the highest one off the stack. Our next free uidNumber isthe value for that entry, plus one.■ Note There are also methods to return the nth entry from a search; but in this case we don’t use them becausethe order of our results matters (we want to get the highest existing userID and then add one), so we have to sortthe results first.Line 35–44 use the $ldap->add method (the most straightforward option) to add an entry of a userwith attributes set in that hash. Attributes with multiple values (as with objectclass) use a list.Alternatively, you could use the Net::LDAP::Entry object to write directly to the LDAP directory:my $entry = Net::LDAP::Entry->new();# set DN$entry->dn("uid=$userid,ou=people,dc=example,dc=com");# You can add attributes all at once, or in as many operations as you like51Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!