2 * Copyright (c) 1991, 1992, 1993
3 * Regents of the University of Michigan. All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
18 #include <ac/string.h>
26 extern char *search_base; /* search base */
27 extern int verbose; /* verbose mode flag */
28 extern LDAP *ld; /* our ldap descriptor */
30 static int num_picked = 0; /* used when user picks entry at More prompt */
33 extern int debug; /* debug flag */
40 static char *attrs[2] = { "objectClass", NULL };
45 printf("->vrfy(%s)\n", dn);
47 /* verify that this DN exists in the directory */
48 (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", attrs, TRUE, &results);
49 (void) ldap_msgfree(results);
51 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
53 if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
55 else if (ld_errno == LDAP_SUCCESS)
58 ldap_perror(ld, "ldap_search");
64 static LDAPMessage * disambiguate( result, matches, read_attrs, who )
70 int choice; /* entry that user chooses */
72 char *namelist[MAX_NUM_NAMES]; /* names found */
73 char response[SMALL_BUF_SIZE]; /* results from user */
74 char *name = NULL; /* DN to lookup */
81 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches,
85 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
88 * If we are here, it means that we got back multiple answers.
90 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
91 || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
93 printf(" Your query was too general and a limit was exceeded. The results listed\n");
94 printf(" are not complete. You may want to try again with a more refined query.\n\n");
97 printf(" Time or size limit exceeded. Partial results follow.\n\n");
99 printf(" %1d names matched \"%s\".\n", matches, who);
101 printf(" Do you wish to see a list of names? ");
103 (void) memset(response, 0, sizeof(response));
104 fetch_buffer(response, sizeof(response), stdin);
105 switch (response[0]) {
114 print_list(result, namelist, &matches);
116 choice = pick_one(matches);
121 name = strdup(namelist[choice]);
123 * Now free up all of the pointers allocated in
124 * namelist. The print_list() routine that filled
125 * in this collection of strings starts at 1, not 0.
127 for (i = 1; namelist[i] != NULL; i++)
135 if (debug & D_FIND) {
136 printf(" Calling ldap_search_s()\n");
137 printf(" ld = 0x%x\n", ld);
138 printf(" search base = %s\n", name);
139 printf(" scope = LDAP_SCOPE_BASE\n");
140 printf(" filter = objectClass=*\n");
141 for (i = 0; read_attrs[i] != NULL; i++)
142 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
143 printf(" read_attrs[%d] = NULL\n", i);
144 printf(" attrsonly = FALSE\n");
145 printf(" &mp = 0x%x\n", &mp);
148 if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
149 ldap_perror(ld, "ldap_search_s");
158 printf(" Please enter 'y', 'n', or RETURN.\n");
164 LDAPMessage * find(who, quiet)
168 register int i, j, k; /* general ints */
169 int matches; /* from ldap_count_entries() */
170 int admonished = FALSE;
171 static int attrs_set = 0;
172 static char *read_attrs[MAX_ATTRS]; /* attrs to use in a read op */
173 static char *search_attrs[MAX_ATTRS]; /* attrs to use in a srch op */
174 static int rc; /* return from ldap_search */
175 LDAPMessage *ldtmp, *res; /* results returned from search */
176 char name[MED_BUF_SIZE];
177 char response[SMALL_BUF_SIZE];
178 char *cp, *dn, **rdns;
180 extern LDAPFiltDesc *lfdp; /* LDAP filter descriptor */
181 extern struct attribute attrlist[]; /* complete list of attrs */
186 fprintf(stderr, "->find(%s)\n", who);
188 /* did not specify a 'who' */
190 printf(" Locate whose entry? ");
192 fetch_buffer(name, sizeof(name), stdin);
198 if (attrs_set == 0) {
201 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
202 if (attrlist[i].flags & ATTR_FLAG_READ)
203 read_attrs[j++] = attrlist[i].quipu_name;
204 if (attrlist[i].flags & ATTR_FLAG_SEARCH)
205 search_attrs[k++] = attrlist[i].quipu_name;
207 read_attrs[j] = NULL;
208 search_attrs[k] = NULL;
212 * If the user-supplied name has any commas in it, we
213 * assume that it is a UFN, and do everything right
214 * here. If we don't find it, treat it as NOT a UFN.
216 if (strchr(who, ',') != NULL) {
217 int savederef, deref;
220 printf("\"%s\" appears to be a UFN\n", who);
222 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
223 deref = LDAP_DEREF_FINDING;
224 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
226 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
227 LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
228 rc != LDAP_TIMELIMIT_EXCEEDED) {
229 ldap_perror(ld, "ldap_ufn_search_s");
230 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
233 if ((matches = ldap_count_entries(ld, res)) < 0) {
234 ldap_perror(ld, "ldap_count_entries");
235 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
237 } else if (matches == 1) {
238 if (ldap_search_s(ld, ldap_get_dn(ld, ldap_first_entry(ld, res)), LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
240 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
241 if (ld_errno == LDAP_UNAVAILABLE)
242 printf(" Could not contact the LDAP server to find \"%s\".\n", who);
244 ldap_perror(ld, "ldap_search_s");
247 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
249 } else if (matches > 1 ) {
250 return disambiguate( res, matches, read_attrs, who );
252 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
256 * Old users of the MTS *USERDIRECTORY will likely wrap the name
257 * in quotes. Not only is this unnecessary, but it also won't work.
259 for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
261 printf(" You do not need to enclose names in quotes.\n");
270 * It wasn't a UFN, so look it up in the usual method.
272 for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
273 fi = ldap_getnextfilter(lfdp)) {
276 printf("Searching, filter = %s\n", fi->lfi_filter);
279 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope,
280 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
281 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
282 ldap_perror(ld, "ldap_search_s");
286 if ((matches = ldap_count_entries(ld, res)) < 0) {
287 ldap_perror(ld, "ldap_count_entries");
291 else if (matches == 1) {
292 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
295 printf(" Found one %s match for \"%s\"\n",
297 if (!fi->lfi_isexact) {
298 rdns = ldap_explode_dn(dn, TRUE);
299 printf(" Do you mean %s? ", *rdns);
300 (void) ldap_value_free(rdns);
302 fetch_buffer(response, sizeof(response), stdin);
303 if ((response[0] == 'n') || (response[0] == 'N'))
307 if (debug & D_FIND) {
308 printf(" Calling ldap_search_s()\n");
309 printf(" ld = 0x%x\n", ld);
310 printf(" dn = %s\n", dn);
311 printf(" scope = LDAP_SCOPE_BASE\n");
312 printf(" filter = %s\n", "objectClass=*");
313 for (i = 0; read_attrs[i] != NULL; i++)
314 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
315 printf(" read_attrs[%d] = NULL\n", i);
316 printf(" attrsonly = FALSE\n");
317 printf(" &results = 0x%x\n", &res);
320 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
321 ldap_perror(ld, "ldap_search_s");
328 else if (matches > 0) {
329 ldtmp = disambiguate(res, matches, read_attrs, who);
333 /* if we're here, there were zero matches */
343 char user_pick[SMALL_BUF_SIZE];
347 printf("->pick_one(%d)\n", i);
350 /* make the user pick an entry */
352 printf(" Enter the number of the name you want or Q to quit: ");
354 fetch_buffer(user_pick, sizeof(user_pick), stdin);
355 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
358 if ((n > 0) && (n <= i))
360 printf(" Invalid response\n");
365 print_list(list, names, matches)
372 char resp[SMALL_BUF_SIZE];
373 register LDAPMessage *ep;
375 register int rest = 4; /* 4, not 1 */
379 printf("->print_list(%x, %x, %x)\n", list, names, matches);
381 /* print a list of names from which the user will select */
382 for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
384 names[i] = ldap_get_dn(ld, ep);
385 rdns = ldap_explode_dn(names[i], TRUE);
386 cpp = ldap_get_values(ld, ep, "title");
388 printf(" %3d. %s\n", i, *rdns);
390 printf(" %3d. %s, %s\n", i, *rdns, *cpp);
391 ldap_value_free(rdns);
392 ldap_value_free(cpp);
394 if ((rest++ > (lpp - 1)) && (i < *matches)) {
398 fetch_buffer(resp, sizeof(resp), stdin);
399 if ((resp[0] == 'n') || (resp[0] == 'N'))
401 else if ((num_picked = atoi(resp)) != 0) {
415 find_all_subscribers(sub, group)
421 static char *attributes[] = { "cn", NULL };
422 char filter[MED_BUF_SIZE];
423 register LDAPMessage *ep;
428 printf("->find_all_subscribers(%x, %s)\n", sub, group);
431 sprintf(filter, "%s=%s", "memberOfGroup", group);
432 if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
434 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
435 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
437 ldap_perror(ld, "ldap_search_s");
440 count = ldap_count_entries(ld, result);
442 ldap_msgfree(result);
445 if ( count > MAX_VALUES ) {
446 printf( " Only retrieving the first %d subscribers....\n",
450 for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
451 sub[i++] = ldap_get_dn(ld, ep);
454 printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
458 ldap_msgfree(result);
462 char * fetch_boolean_value(who, attr)
464 struct attribute attr;
466 LDAPMessage *result; /* from the search below */
467 register LDAPMessage *ep; /* entry pointer */
468 register char **vp; /* for parsing the result */
469 static char *attributes[] = { NULL, NULL };
473 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
475 attributes[0] = attr.quipu_name;
476 if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
478 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
479 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
481 ldap_perror(ld, "ldap_search_s");
482 ldap_msgfree(result);
487 * We did a read on one name and only asked for one attribute.
488 * There's no reason to loop through any of these structures.
490 * If ldap_first_attribute() returns NULL, then this entry did
491 * not have this particular attribute.
493 ep = ldap_first_entry(ld, result);
494 if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
495 ldap_msgfree(result);
499 ldap_msgfree(result);
500 if (!strcasecmp(*vp, "TRUE")) {
504 else if (!strcasecmp(*vp, "FALSE")) {
509 fprintf(stderr, " Got garbage -> [%s]\n", *vp);