3 * Copyright (c) 1991, 1992, 1993
4 * Regents of the University of Michigan. All rights reserved.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
21 #include <ac/string.h>
29 static int num_picked = 0; /* used when user picks entry at More prompt */
36 static char *attrs[2] = { "objectClass", NULL };
41 printf("->vrfy(%s)\n", dn);
43 /* verify that this DN exists in the directory */
44 (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", attrs, TRUE, &results);
45 (void) ldap_msgfree(results);
47 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
49 if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
51 else if (ld_errno == LDAP_SUCCESS)
54 ldap_perror(ld, "ldap_search");
61 disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
63 int choice; /* entry that user chooses */
65 char *namelist[MAX_NUM_NAMES]; /* names found */
66 char response[SMALL_BUF_SIZE]; /* results from user */
67 char *name = NULL; /* DN to lookup */
73 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches,
77 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
80 * If we are here, it means that we got back multiple answers.
82 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
83 || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
85 printf(" Your query was too general and a limit was exceeded. The results listed\n");
86 printf(" are not complete. You may want to try again with a more refined query.\n\n");
89 printf(" Time or size limit exceeded. Partial results follow.\n\n");
91 printf(" %1d names matched \"%s\".\n", matches, who);
93 printf(" Do you wish to see a list of names? ");
95 (void) memset(response, 0, sizeof(response));
96 fetch_buffer(response, sizeof(response), stdin);
97 switch (response[0]) {
106 print_list(result, namelist, &matches);
108 choice = pick_one(matches);
113 name = strdup(namelist[choice]);
115 * Now free up all of the pointers allocated in
116 * namelist. The print_list() routine that filled
117 * in this collection of strings starts at 1, not 0.
119 for (i = 1; namelist[i] != NULL; i++)
127 if (debug & D_FIND) {
128 printf(" Calling ldap_search_s()\n");
129 printf(" ld = 0x%x\n", ld);
130 printf(" search base = %s\n", name);
131 printf(" scope = LDAP_SCOPE_BASE\n");
132 printf(" filter = objectClass=*\n");
133 for (i = 0; read_attrs[i] != NULL; i++)
134 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
135 printf(" read_attrs[%d] = NULL\n", i);
136 printf(" attrsonly = FALSE\n");
137 printf(" &mp = 0x%x\n", &mp);
140 if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
141 ldap_perror(ld, "ldap_search_s");
150 printf(" Please enter 'y', 'n', or RETURN.\n");
157 find( char *who, int quiet )
159 register int i, j, k; /* general ints */
160 int matches; /* from ldap_count_entries() */
161 int admonished = FALSE;
162 static int attrs_set = 0;
163 static char *read_attrs[MAX_ATTRS]; /* attrs to use in a read op */
164 static char *search_attrs[MAX_ATTRS]; /* attrs to use in a srch op */
165 static int rc; /* return from ldap_search */
166 LDAPMessage *ldtmp, *res; /* results returned from search */
167 char name[MED_BUF_SIZE];
168 char response[SMALL_BUF_SIZE];
169 char *cp, *dn, **rdns;
174 fprintf(stderr, "->find(%s)\n", who);
176 /* did not specify a 'who' */
178 printf(" Locate whose entry? ");
180 fetch_buffer(name, sizeof(name), stdin);
186 if (attrs_set == 0) {
189 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
190 if (attrlist[i].flags & ATTR_FLAG_READ)
191 read_attrs[j++] = attrlist[i].quipu_name;
192 if (attrlist[i].flags & ATTR_FLAG_SEARCH)
193 search_attrs[k++] = attrlist[i].quipu_name;
195 read_attrs[j] = NULL;
196 search_attrs[k] = NULL;
200 * If the user-supplied name has any commas in it, we
201 * assume that it is a UFN, and do everything right
202 * here. If we don't find it, treat it as NOT a UFN.
204 if (strchr(who, ',') != NULL) {
205 int savederef, deref;
208 printf("\"%s\" appears to be a UFN\n", who);
210 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
211 deref = LDAP_DEREF_FINDING;
212 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
214 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
215 LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
216 rc != LDAP_TIMELIMIT_EXCEEDED) {
217 ldap_perror(ld, "ldap_ufn_search_s");
218 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
221 if ((matches = ldap_count_entries(ld, res)) < 0) {
222 ldap_perror(ld, "ldap_count_entries");
223 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
225 } else if (matches == 1) {
226 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
227 rc = ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res);
229 if (rc != LDAP_SUCCESS) {
231 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
232 if (ld_errno == LDAP_UNAVAILABLE)
233 printf(" Could not contact the LDAP server to find \"%s\".\n", who);
235 ldap_perror(ld, "ldap_search_s");
238 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
240 } else if (matches > 1 ) {
241 return disambiguate( res, matches, read_attrs, who );
243 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
247 * Old users of the MTS *USERDIRECTORY will likely wrap the name
248 * in quotes. Not only is this unnecessary, but it also won't work.
250 for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
252 printf(" You do not need to enclose names in quotes.\n");
261 * It wasn't a UFN, so look it up in the usual method.
263 for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
264 fi = ldap_getnextfilter(lfdp)) {
267 printf("Searching, filter = %s\n", fi->lfi_filter);
270 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope,
271 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
272 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
273 ldap_perror(ld, "ldap_search_s");
277 if ((matches = ldap_count_entries(ld, res)) < 0) {
278 ldap_perror(ld, "ldap_count_entries");
282 else if (matches == 1) {
283 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
286 printf(" Found one %s match for \"%s\"\n",
288 if (!fi->lfi_isexact) {
289 rdns = ldap_explode_dn(dn, TRUE);
290 printf(" Do you mean %s? ", *rdns);
291 (void) ldap_value_free(rdns);
293 fetch_buffer(response, sizeof(response), stdin);
294 if ((response[0] == 'n') || (response[0] == 'N'))
301 if (debug & D_FIND) {
302 printf(" Calling ldap_search_s()\n");
303 printf(" ld = 0x%x\n", ld);
304 printf(" dn = %s\n", dn);
305 printf(" scope = LDAP_SCOPE_BASE\n");
306 printf(" filter = %s\n", "objectClass=*");
307 for (i = 0; read_attrs[i] != NULL; i++)
308 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
309 printf(" read_attrs[%d] = NULL\n", i);
310 printf(" attrsonly = FALSE\n");
311 printf(" &results = 0x%x\n", &res);
314 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
315 ldap_perror(ld, "ldap_search_s");
322 else if (matches > 0) {
323 ldtmp = disambiguate(res, matches, read_attrs, who);
327 /* if we're here, there were zero matches */
337 char user_pick[SMALL_BUF_SIZE];
341 printf("->pick_one(%d)\n", i);
344 /* make the user pick an entry */
346 printf(" Enter the number of the name you want or Q to quit: ");
348 fetch_buffer(user_pick, sizeof(user_pick), stdin);
349 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
352 if ((n > 0) && (n <= i))
354 printf(" Invalid response\n");
360 print_list( LDAPMessage *list, char **names, int *matches )
363 char resp[SMALL_BUF_SIZE];
364 register LDAPMessage *ep;
366 register int rest = 4; /* 4, not 1 */
370 printf("->print_list(%x, %x, %x)\n", list, names, matches);
372 /* print a list of names from which the user will select */
373 for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
375 names[i] = ldap_get_dn(ld, ep);
376 rdns = ldap_explode_dn(names[i], TRUE);
377 cpp = ldap_get_values(ld, ep, "title");
379 printf(" %3d. %s\n", i, *rdns);
381 printf(" %3d. %s, %s\n", i, *rdns, *cpp);
382 ldap_value_free(rdns);
383 ldap_value_free(cpp);
385 if ((rest++ > (lpp - 1)) && (i < *matches)) {
389 fetch_buffer(resp, sizeof(resp), stdin);
390 if ((resp[0] == 'n') || (resp[0] == 'N'))
392 else if ((num_picked = atoi(resp)) != 0) {
407 find_all_subscribers( char **sub, char *group )
411 static char *attributes[] = { "cn", NULL };
412 char filter[MED_BUF_SIZE];
413 register LDAPMessage *ep;
418 printf("->find_all_subscribers(%x, %s)\n", sub, group);
421 sprintf(filter, "%s=%s", "memberOfGroup", group);
422 if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
424 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
425 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
427 ldap_perror(ld, "ldap_search_s");
430 count = ldap_count_entries(ld, result);
432 ldap_msgfree(result);
435 if ( count > MAX_VALUES ) {
436 printf( " Only retrieving the first %d subscribers....\n",
440 for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
441 sub[i++] = ldap_get_dn(ld, ep);
444 printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
448 ldap_msgfree(result);
453 fetch_boolean_value( char *who, struct attribute attr )
455 LDAPMessage *result; /* from the search below */
456 register LDAPMessage *ep; /* entry pointer */
457 register char **vp; /* for parsing the result */
458 static char *attributes[] = { NULL, NULL };
462 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
464 attributes[0] = attr.quipu_name;
465 if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
467 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
468 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
470 ldap_perror(ld, "ldap_search_s");
471 ldap_msgfree(result);
476 * We did a read on one name and only asked for one attribute.
477 * There's no reason to loop through any of these structures.
479 * If ldap_first_attribute() returns NULL, then this entry did
480 * not have this particular attribute.
482 ep = ldap_first_entry(ld, result);
483 if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
484 ldap_msgfree(result);
488 ldap_msgfree(result);
489 if (!strcasecmp(*vp, "TRUE")) {
493 else if (!strcasecmp(*vp, "FALSE")) {
498 fprintf(stderr, " Got garbage -> [%s]\n", *vp);