Attachment 'ldapphone.cc'

Download

   1 #include <iostream>
   2 #include <sstream>
   3 #include <iomanip>
   4 #include <string>
   5 #include <map>
   6 #include <cstdlib>
   7 #include <ldap.h>
   8 
   9 void ldap_assert(const char *self, LDAP *ldap, int status)
  10 {
  11     if (status) {
  12         ldap_perror(ldap, self);
  13         std::exit(status);
  14     }
  15 }
  16 
  17 int main(int argc, char *argv[])
  18 {
  19     if (argc != 2) {
  20         std::cerr << "Usage: " << argv[0] << " PATTERN" << std::endl;
  21         std::exit(1);
  22     }
  23     const char *self = argv[0];
  24     const char *phrase = argv[1];
  25     const char *host = "ldap.desy.de";
  26     const char *bind = "ou=People,o=DESY,c=DE";
  27     char *attributes[] = { "uid", "cn", "roomNumber", "telephoneNumber", 0 }; // { LDAP_ALL_USER_ATTRIBUTES, 0 }
  28 
  29     std::ostringstream *filter = new std::ostringstream;
  30     (*filter) << "(&(objectClass=person)(|(uid=" << phrase << ")(cn=*" << phrase << "*)(telephoneNumber=" << phrase << ")))";
  31 
  32     LDAP *ldap = ldap_init(host, LDAP_PORT);
  33     if (!ldap) std::exit(1);
  34     ldap_assert(self, ldap, ldap_simple_bind_s(ldap, 0, 0));
  35     LDAPMessage *result;
  36     ldap_assert(self, ldap, ldap_search_s(ldap, bind, LDAP_SCOPE_ONE, filter->str().c_str(), attributes, 0, &result));
  37     delete filter;
  38 
  39     for (LDAPMessage *entry = ldap_first_entry(ldap, result); entry; entry = ldap_next_entry(ldap, entry)) {
  40 
  41         std::map<std::string, std::string> info; // we collect all attributes of an entry (and their values) in this map
  42 
  43         BerElement *berptr;
  44         for (char *attribute = ldap_first_attribute(ldap, entry, &berptr); attribute; attribute = ldap_next_attribute(ldap, entry, berptr)) {
  45             
  46             char **values = ldap_get_values(ldap, entry, attribute);
  47             if (ldap_count_values(values)) { // are there any values assigned to this attribute?
  48                 info[attribute] = values[0]; // an attribute can have multiple values, but we take only the first here
  49             }
  50             ldap_value_free(values);
  51             ldap_memfree(attribute);
  52         }
  53         ber_free(berptr, 0);
  54 
  55         if (info.find("telephoneNumber") != info.end()) { // show only entries that have a telephone number assigned
  56             std::cout << std::left
  57                 << std::setw(10) << info["uid"] << " "
  58                 << std::setw(32) << info["cn"] << " "
  59                 << std::setw(9) << info["roomNumber"] << " "
  60                 << info["telephoneNumber"] << std::endl;
  61         }
  62     }
  63 
  64     (void)ldap_msgfree(result);
  65     ldap_assert(self, ldap, ldap_unbind_s(ldap));
  66 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-10-29 21:58:46, 0.8 KB) [[attachment:asc.c]]
  • [get | view] (2008-10-29 15:52:36, 0.6 KB) [[attachment:bbox]]
  • [get | view] (2008-10-29 15:52:48, 0.8 KB) [[attachment:bib2bib]]
  • [get | view] (2008-10-29 21:58:53, 0.0 KB) [[attachment:bits.c]]
  • [get | view] (2008-10-29 21:58:59, 0.7 KB) [[attachment:claus.c]]
  • [get | view] (2008-10-29 15:53:02, 0.9 KB) [[attachment:cvstags]]
  • [get | view] (2008-10-29 15:53:20, 0.7 KB) [[attachment:german2iso]]
  • [get | view] (2008-10-29 15:53:26, 1.1 KB) [[attachment:germanuml]]
  • [get | view] (2008-10-29 15:53:34, 0.4 KB) [[attachment:gnuplot_diff]]
  • [get | view] (2008-10-29 15:53:42, 1.2 KB) [[attachment:httphead]]
  • [get | view] (2008-10-30 10:56:00, 2.4 KB) [[attachment:ldapphone.cc]]
  • [get | view] (2008-10-29 21:59:14, 0.0 KB) [[attachment:leak.cc]]
  • [get | view] (2008-10-29 22:00:45, 1.3 KB) [[attachment:lfc-lr.cc]]
  • [get | view] (2008-10-29 22:00:55, 1.9 KB) [[attachment:lfc-se-ls.cc]]
  • [get | view] (2008-10-29 22:01:03, 2.2 KB) [[attachment:lfc-se-user.cc]]
  • [get | view] (2008-10-29 22:01:09, 1.5 KB) [[attachment:lfc-sfn2lfn.cc]]
  • [get | view] (2008-10-29 21:59:35, 0.4 KB) [[attachment:liststat.cc]]
  • [get | view] (2008-10-29 21:59:41, 0.2 KB) [[attachment:listsum.cc]]
  • [get | view] (2008-10-29 21:59:47, 0.4 KB) [[attachment:listtab.cc]]
  • [get | view] (2008-10-29 15:53:55, 1.3 KB) [[attachment:lsf]]
  • [get | view] (2008-10-29 15:54:01, 0.3 KB) [[attachment:mangrep]]
  • [get | view] (2008-10-29 15:54:09, 0.4 KB) [[attachment:msec2time]]
  • [get | view] (2008-10-29 15:54:17, 0.3 KB) [[attachment:notouch]]
  • [get | view] (2008-10-29 15:54:27, 1.1 KB) [[attachment:pathgrep]]
  • [get | view] (2008-10-29 21:59:55, 0.7 KB) [[attachment:realtime.cc]]
  • [get | view] (2008-10-29 15:54:39, 0.5 KB) [[attachment:root2iso]]
  • [get | view] (2008-10-29 15:54:47, 0.4 KB) [[attachment:rpath]]
  • [get | view] (2008-10-29 15:54:58, 0.7 KB) [[attachment:sourcegrep]]
  • [get | view] (2008-10-29 15:55:06, 0.3 KB) [[attachment:system]]
  • [get | view] (2008-10-29 22:00:07, 1.6 KB) [[attachment:timeout.c]]
  • [get | view] (2008-11-18 12:03:33, 0.4 KB) [[attachment:unicode]]
  • [get | view] (2008-10-29 22:00:15, 0.4 KB) [[attachment:unixtime2time.c]]
  • [get | view] (2008-10-29 15:55:18, 0.1 KB) [[attachment:win]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.