3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1991, 1992, 1993
8 * Regents of the University of Michigan. All rights reserved.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
25 #include <ac/string.h>
33 static int num_picked = 0; /* used when user picks entry at More prompt */
39 LDAPMessage *results = NULL;
40 static char *attrs[2] = { "1.1", 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, NULL, 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");
65 disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
67 int choice; /* entry that user chooses */
69 char *namelist[MAX_NUM_NAMES]; /* names found */
70 char response[SMALL_BUF_SIZE]; /* results from user */
71 char *name = NULL; /* DN to lookup */
72 LDAPMessage *mp = NULL;
77 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches,
81 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
84 * If we are here, it means that we got back multiple answers.
86 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
87 || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
89 printf(" Your query was too general and a limit was exceeded. The results listed\n");
90 printf(" are not complete. You may want to try again with a more refined query.\n\n");
93 printf(" Time or size limit exceeded. Partial results follow.\n\n");
95 printf(" %1d names matched \"%s\".\n", matches, who);
97 printf(" Do you wish to see a list of names? ");
99 (void) memset(response, '\0', sizeof(response));
100 fetch_buffer(response, sizeof(response), stdin);
101 switch (response[0]) {
110 print_list(result, namelist, &matches);
112 choice = pick_one(matches);
117 name = strdup(namelist[choice]);
119 * Now free up all of the pointers allocated in
120 * namelist. The print_list() routine that filled
121 * in this collection of strings starts at 1, not 0.
123 for (i = 1; namelist[i] != NULL; i++)
131 if (debug & D_FIND) {
132 printf(" Calling ldap_search_s()\n");
133 printf(" ld = 0x%x\n", ld);
134 printf(" search base = %s\n", name);
135 printf(" scope = LDAP_SCOPE_BASE\n");
136 printf(" filter = (objectClass=*)\n");
137 for (i = 0; read_attrs[i] != NULL; i++)
138 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
139 printf(" read_attrs[%d] = NULL\n", i);
140 printf(" attrsonly = FALSE\n");
141 printf(" &mp = 0x%x\n", &mp);
144 if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
145 ldap_perror(ld, "ldap_search_s");
154 printf(" Please enter 'y', 'n', or RETURN.\n");
161 find( char *who, int quiet )
163 register int i, j, k; /* general ints */
164 int matches; /* from ldap_count_entries() */
165 int admonished = FALSE;
166 static int attrs_set = 0;
167 static char *read_attrs[MAX_ATTRS]; /* attrs to use in a read op */
168 static char *search_attrs[MAX_ATTRS]; /* attrs to use in a srch op */
169 static int rc; /* return from ldap_search */
170 LDAPMessage *ldtmp, *res; /* results returned from search */
171 char name[MED_BUF_SIZE];
172 char response[SMALL_BUF_SIZE];
173 char *cp, *dn, **rdns;
178 fprintf(stderr, "->find(%s)\n", who);
180 /* did not specify a 'who' */
182 printf(" Locate whose entry? ");
184 fetch_buffer(name, sizeof(name), stdin);
190 if (attrs_set == 0) {
193 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
194 if (attrlist[i].flags & ATTR_FLAG_READ)
195 read_attrs[j++] = attrlist[i].quipu_name;
196 if (attrlist[i].flags & ATTR_FLAG_SEARCH)
197 search_attrs[k++] = attrlist[i].quipu_name;
199 read_attrs[j] = NULL;
200 search_attrs[k] = NULL;
204 * If the user-supplied name has any commas in it, we
205 * assume that it is a UFN, and do everything right
206 * here. If we don't find it, treat it as NOT a UFN.
208 if (strchr(who, ',') != NULL) {
209 int savederef, deref;
212 printf("\"%s\" appears to be a UFN\n", who);
214 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
215 deref = LDAP_DEREF_FINDING;
216 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
218 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
219 LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
220 rc != LDAP_TIMELIMIT_EXCEEDED) {
221 ldap_perror(ld, "ldap_ufn_search_s");
222 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
225 if ((matches = ldap_count_entries(ld, res)) < 0) {
226 ldap_perror(ld, "ldap_count_entries");
227 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
229 } else if (matches == 1) {
230 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
231 rc = ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res);
233 if (rc != LDAP_SUCCESS) {
234 ldap_perror(ld, "ldap_search_s");
237 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
239 } else if (matches > 1 ) {
240 return disambiguate( res, matches, read_attrs, who );
242 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
246 * Old users of the MTS *USERDIRECTORY will likely wrap the name
247 * in quotes. Not only is this unnecessary, but it also won't work.
249 for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
251 printf(" You do not need to enclose names in quotes.\n");
260 * It wasn't a UFN, so look it up in the usual method.
262 for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
263 fi = ldap_getnextfilter(lfdp)) {
266 printf("Searching, filter = %s\n", fi->lfi_filter);
269 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope,
270 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
271 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
272 ldap_perror(ld, "ldap_search_s");
276 if ((matches = ldap_count_entries(ld, res)) < 0) {
277 ldap_perror(ld, "ldap_count_entries");
281 else if (matches == 1) {
282 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
285 printf(" Found one %s match for \"%s\"\n",
287 if (!fi->lfi_isexact) {
288 rdns = ldap_explode_dn(dn, TRUE);
289 printf(" Do you mean %s? ", *rdns);
290 (void) ldap_value_free(rdns);
292 fetch_buffer(response, sizeof(response), stdin);
293 if ((response[0] == 'n') || (response[0] == 'N'))
300 if (debug & D_FIND) {
301 printf(" Calling ldap_search_s()\n");
302 printf(" ld = 0x%x\n", ld);
303 printf(" dn = %s\n", dn);
304 printf(" scope = LDAP_SCOPE_BASE\n");
305 printf(" filter = %s\n", "(objectClass=*)");
306 for (i = 0; read_attrs[i] != NULL; i++)
307 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
308 printf(" read_attrs[%d] = NULL\n", i);
309 printf(" attrsonly = FALSE\n");
310 printf(" &results = 0x%x\n", &res);
313 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res) != LDAP_SUCCESS) {
314 ldap_perror(ld, "ldap_search_s");
321 else if (matches > 0) {
322 ldtmp = disambiguate(res, matches, read_attrs, who);
326 /* if we're here, there were zero matches */
336 char user_pick[SMALL_BUF_SIZE];
340 printf("->pick_one(%d)\n", i);
343 /* make the user pick an entry */
345 printf(" Enter the number of the name you want or Q to quit: ");
347 fetch_buffer(user_pick, sizeof(user_pick), stdin);
348 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
351 if ((n > 0) && (n <= i))
353 printf(" Invalid response\n");
359 print_list( LDAPMessage *list, char **names, int *matches )
362 char resp[SMALL_BUF_SIZE];
363 register LDAPMessage *ep;
365 register int rest = 4; /* 4, not 1 */
369 printf("->print_list(%x, %x, %x)\n", list, names, matches);
371 /* print a list of names from which the user will select */
372 for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
374 names[i] = ldap_get_dn(ld, ep);
375 rdns = ldap_explode_dn(names[i], TRUE);
376 cpp = ldap_get_values(ld, ep, "title");
378 printf(" %3d. %s\n", i, *rdns);
380 printf(" %3d. %s, %s\n", i, *rdns, *cpp);
381 ldap_value_free(rdns);
382 ldap_value_free(cpp);
384 if ((rest++ > (lpp - 1)) && (i < *matches)) {
388 fetch_buffer(resp, sizeof(resp), stdin);
389 if ((resp[0] == 'n') || (resp[0] == 'N'))
391 else if ((num_picked = atoi(resp)) != 0) {
406 find_all_subscribers( char **sub, char *group )
410 static char *attributes[] = { "cn", NULL };
411 char filter[MED_BUF_SIZE];
412 register LDAPMessage *ep;
417 printf("->find_all_subscribers(%x, %s)\n", sub, group);
420 sprintf(filter, "%s=%s", "memberOfGroup", group);
421 if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
423 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
424 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
426 ldap_perror(ld, "ldap_search_s");
429 count = ldap_count_entries(ld, result);
431 ldap_msgfree(result);
434 if ( count > MAX_VALUES ) {
435 printf( " Only retrieving the first %d subscribers....\n",
439 for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
440 sub[i++] = ldap_get_dn(ld, ep);
443 printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
447 ldap_msgfree(result);
452 fetch_boolean_value( char *who, struct attribute attr )
454 LDAPMessage *result; /* from the search below */
455 register LDAPMessage *ep; /* entry pointer */
456 register char **vp; /* for parsing the result */
457 static char *attributes[] = { NULL, NULL };
461 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
463 attributes[0] = attr.quipu_name;
464 if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, NULL, attributes, FALSE, &result) != LDAP_SUCCESS) {
466 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
467 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
469 ldap_perror(ld, "ldap_search_s");
470 ldap_msgfree(result);
475 * We did a read on one name and only asked for one attribute.
476 * There's no reason to loop through any of these structures.
478 * If ldap_first_attribute() returns NULL, then this entry did
479 * not have this particular attribute.
481 ep = ldap_first_entry(ld, result);
482 if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
483 ldap_msgfree(result);
487 ldap_msgfree(result);
488 if (!strcasecmp(*vp, "TRUE")) {
492 else if (!strcasecmp(*vp, "FALSE")) {
497 fprintf(stderr, " Got garbage -> [%s]\n", *vp);