#include #include #include #include #include #include void print_entries_with_values(LDAP *ld, LDAPMessage *result, char *attribute) { LDAPMessage *e; BerElement *ber; char *dn, *attr; char **vals; int i; for (e = ldap_first_entry(ld, result); e != NULL; e = ldap_next_entry(ld, e)) { if(( dn = ldap_get_dn(ld,e) ) != NULL) { #ifdef DEBUG printf( "dn: %s\n", dn); #endif //ldap_memfree(dn); } for (attr = ldap_first_attribute (ld,e, &ber); attr != NULL; attr = ldap_next_attribute (ld, e, ber)) { #ifdef DEBUG printf("got here1: attr is: %s attriibute: %s\n", attr, attribute); #endif if(strcmp(attr, attribute)>=0) { #ifdef DEBUG printf("found the attr were looking for... attr: %s attibute: %s\n", attr, attribute); #endif if ((vals = ldap_get_values(ld,e,attribute)) != NULL) { #ifdef DEBUG printf("got here 2: vals[0]: %s\n", vals[0]); #endif for (i = 0; vals[i] != NULL; i++) { #ifdef DEBUG printf("%s: %s\n", attribute, vals[i]); #endif printf("%s\n", vals[i]); } ldap_value_free(vals); } // printf("\n"); if(ber != NULL) { ber_free(ber, 0); } } } } } int main(int argc, char **argv) { /* pass in arguments for the server, basedn, and configName The shell script wrapper needs to be smart enough to set the host and basedn variables everytime. This app will query for the specific value first, and if not found it will search for the default value. Right now this just handle simple text entries. I'll add support for multiline and base64/binary stuff later (probabaly useful...) */ LDAP *ld; LDAPMessage *result; LDAPMessage *entry; int nentries; struct utsname unamebuf; char hostname[300]; int c; int this_option_optiond; int option_index; char *server; char *basedn; char *config; char filter[300]; char attr[100]; static struct option long_options[] = { {"server", 1, 0, 's'}, {"basedn", 1, 0, 'b'}, {"config", 1, 0, 'f'}, {0,0,0,0} }; #ifdef DEBUG printf("debug!\n"); #endif this_option_optiond = optind ? optind : 1; option_index = 0; for(;;) { if(-1 == (c = getopt_long(argc,argv, "s:b:f:", long_options, &option_index))) break; switch (c) { case 's': server = optarg; break; case 'b': basedn = optarg; break; case 'f': config = optarg; break; default: exit(0); } } if(optind < argc) { strcpy(attr,argv[optind]); #ifdef DEBUG printf("extra arg found: %s\n", argv[optind]); #endif } else { printf("I need a attribute arg to be useful!\n"); exit(0); } #ifdef DEBUG printf("server: %s\n", server); printf("basedn: %s\n", basedn); printf("config: %s\n", config); printf(" attr: %s\n", &attr); #endif /* get the hostname */ uname(&unamebuf); strcpy(hostname,unamebuf.nodename); #ifdef DEBUG printf("hostname: %s\n", hostname); #endif if(!server) server = NULL; if(!basedn) basedn = NULL; if(!config) config = hostname; #ifdef DEBUG printf("config: %s\n", config); #endif if ((ld = ldap_open( server, LDAP_PORT)) != NULL) { sprintf(filter, "(configName=%s)", config); #ifdef DEBUG printf("the server on %s is up!\n", server); printf("filter: %s\n", &filter); printf("basedn: %s\n", basedn); #endif ldap_search_s(ld, basedn, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &result); nentries = ldap_count_entries(ld, result); if(!nentries) { #ifdef DEBUG sprintf(filter, "(configName=%s)", "Default"); #endif ldap_search_s(ld, basedn, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &result); } #ifdef DEBUG printf("Found %d entries...\n", nentries); #endif print_entries_with_values(ld, result, attr); for ( entry = ldap_first_entry(ld, result); entry != NULL; entry = ldap_next_entry(ld, entry)) { ldap_msgfree(result); ldap_unbind(ld); exit(0); } } exit(0); }