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>
32 static int num_picked = 0; /* used when user picks entry at More prompt */
38 LDAPMessage *results = NULL;
39 static char *attrs[2] = { "1.1", NULL };
44 printf("->vrfy(%s)\n", dn);
46 /* verify that this DN exists in the directory */
47 (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, attrs, TRUE, &results);
48 (void) ldap_msgfree(results);
50 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
52 if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
54 else if (ld_errno == LDAP_SUCCESS)
57 ldap_perror(ld, "ldap_search");
64 disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
66 int choice; /* entry that user chooses */
68 char *namelist[MAX_NUM_NAMES]; /* names found */
69 char response[SMALL_BUF_SIZE]; /* results from user */
70 char *name = NULL; /* DN to lookup */
71 LDAPMessage *mp = NULL;
76 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches,
80 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
83 * If we are here, it means that we got back multiple answers.
85 if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
86 || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
88 printf(" Your query was too general and a limit was exceeded. The results listed\n");
89 printf(" are not complete. You may want to try again with a more refined query.\n\n");
92 printf(" Time or size limit exceeded. Partial results follow.\n\n");
94 printf(" %1d names matched \"%s\".\n", matches, who);
96 printf(" Do you wish to see a list of names? ");
98 (void) memset(response, '\0', sizeof(response));
99 fetch_buffer(response, sizeof(response), stdin);
100 switch (response[0]) {
109 print_list(result, namelist, &matches);
111 choice = pick_one(matches);
116 name = strdup(namelist[choice]);
118 * Now free up all of the pointers allocated in
119 * namelist. The print_list() routine that filled
120 * in this collection of strings starts at 1, not 0.
122 for (i = 1; namelist[i] != NULL; i++)
130 if (debug & D_FIND) {
131 printf(" Calling ldap_search_s()\n");
132 printf(" ld = 0x%x\n", ld);
133 printf(" search base = %s\n", name);
134 printf(" scope = LDAP_SCOPE_BASE\n");
135 printf(" filter = (objectClass=*)\n");
136 for (i = 0; read_attrs[i] != NULL; i++)
137 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
138 printf(" read_attrs[%d] = NULL\n", i);
139 printf(" attrsonly = FALSE\n");
140 printf(" &mp = 0x%x\n", &mp);
143 if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
144 ldap_perror(ld, "ldap_search_s");
153 printf(" Please enter 'y', 'n', or RETURN.\n");
160 find( char *who, int quiet )
162 register int i, j, k; /* general ints */
163 int matches; /* from ldap_count_entries() */
164 int admonished = FALSE;
165 static int attrs_set = 0;
166 static char *read_attrs[MAX_ATTRS]; /* attrs to use in a read op */
167 static char *search_attrs[MAX_ATTRS]; /* attrs to use in a srch op */
168 static int rc; /* return from ldap_search */
169 LDAPMessage *ldtmp, *res; /* results returned from search */
170 char name[MED_BUF_SIZE];
171 char response[SMALL_BUF_SIZE];
172 char *cp, *dn, **rdns;
177 fprintf(stderr, "->find(%s)\n", who);
179 /* did not specify a 'who' */
181 printf(" Locate whose entry? ");
183 fetch_buffer(name, sizeof(name), stdin);
189 if (attrs_set == 0) {
192 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
193 if (attrlist[i].flags & ATTR_FLAG_READ)
194 read_attrs[j++] = attrlist[i].quipu_name;
195 if (attrlist[i].flags & ATTR_FLAG_SEARCH)
196 search_attrs[k++] = attrlist[i].quipu_name;
198 read_attrs[j] = NULL;
199 search_attrs[k] = NULL;
203 * Old users of the MTS *USERDIRECTORY will likely wrap the name
204 * in quotes. Not only is this unnecessary, but it also won't work.
206 for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
208 printf(" You do not need to enclose names in quotes.\n");
217 * It wasn't a UFN, so look it up in the usual method.
219 for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
220 fi = ldap_getnextfilter(lfdp)) {
223 printf("Searching, filter = %s\n", fi->lfi_filter);
226 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope,
227 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
228 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
229 ldap_perror(ld, "ldap_search_s");
233 if ((matches = ldap_count_entries(ld, res)) < 0) {
234 ldap_perror(ld, "ldap_count_entries");
238 else if (matches == 1) {
239 dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
242 printf(" Found one %s match for \"%s\"\n",
244 if (!fi->lfi_isexact) {
245 rdns = ldap_explode_dn(dn, TRUE);
246 printf(" Do you mean %s? ", *rdns);
247 (void) ldap_value_free(rdns);
249 fetch_buffer(response, sizeof(response), stdin);
250 if ((response[0] == 'n') || (response[0] == 'N'))
257 if (debug & D_FIND) {
258 printf(" Calling ldap_search_s()\n");
259 printf(" ld = 0x%x\n", ld);
260 printf(" dn = %s\n", dn);
261 printf(" scope = LDAP_SCOPE_BASE\n");
262 printf(" filter = %s\n", "(objectClass=*)");
263 for (i = 0; read_attrs[i] != NULL; i++)
264 printf(" read_attrs[%d] = %s\n", i, read_attrs[i]);
265 printf(" read_attrs[%d] = NULL\n", i);
266 printf(" attrsonly = FALSE\n");
267 printf(" &results = 0x%x\n", &res);
270 if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res) != LDAP_SUCCESS) {
271 ldap_perror(ld, "ldap_search_s");
278 else if (matches > 0) {
279 ldtmp = disambiguate(res, matches, read_attrs, who);
283 /* if we're here, there were zero matches */
293 char user_pick[SMALL_BUF_SIZE];
297 printf("->pick_one(%d)\n", i);
300 /* make the user pick an entry */
302 printf(" Enter the number of the name you want or Q to quit: ");
304 fetch_buffer(user_pick, sizeof(user_pick), stdin);
305 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
308 if ((n > 0) && (n <= i))
310 printf(" Invalid response\n");
316 print_list( LDAPMessage *list, char **names, int *matches )
319 char resp[SMALL_BUF_SIZE];
320 register LDAPMessage *ep;
322 register int rest = 4; /* 4, not 1 */
326 printf("->print_list(%x, %x, %x)\n", list, names, matches);
328 /* print a list of names from which the user will select */
329 for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
331 names[i] = ldap_get_dn(ld, ep);
332 rdns = ldap_explode_dn(names[i], TRUE);
333 cpp = ldap_get_values(ld, ep, "title");
335 printf(" %3d. %s\n", i, *rdns);
337 printf(" %3d. %s, %s\n", i, *rdns, *cpp);
338 ldap_value_free(rdns);
339 ldap_value_free(cpp);
341 if ((rest++ > (lpp - 1)) && (i < *matches)) {
345 fetch_buffer(resp, sizeof(resp), stdin);
346 if ((resp[0] == 'n') || (resp[0] == 'N'))
348 else if ((num_picked = atoi(resp)) != 0) {
363 find_all_subscribers( char **sub, char *group )
367 static char *attributes[] = { "cn", NULL };
368 char filter[MED_BUF_SIZE];
369 register LDAPMessage *ep;
374 printf("->find_all_subscribers(%x, %s)\n", sub, group);
377 sprintf(filter, "%s=%s", "memberOfGroup", group);
378 if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
380 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
381 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
383 ldap_perror(ld, "ldap_search_s");
386 count = ldap_count_entries(ld, result);
388 ldap_msgfree(result);
391 if ( count > MAX_VALUES ) {
392 printf( " Only retrieving the first %d subscribers....\n",
396 for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
397 sub[i++] = ldap_get_dn(ld, ep);
400 printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
404 ldap_msgfree(result);
409 fetch_boolean_value( char *who, struct attribute attr )
411 LDAPMessage *result; /* from the search below */
412 register LDAPMessage *ep; /* entry pointer */
413 register char **vp; /* for parsing the result */
414 static char *attributes[] = { NULL, NULL };
418 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
420 attributes[0] = attr.quipu_name;
421 if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, NULL, 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");
427 ldap_msgfree(result);
432 * We did a read on one name and only asked for one attribute.
433 * There's no reason to loop through any of these structures.
435 * If ldap_first_attribute() returns NULL, then this entry did
436 * not have this particular attribute.
438 ep = ldap_first_entry(ld, result);
439 if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
440 ldap_msgfree(result);
444 ldap_msgfree(result);
445 if (!strcasecmp(*vp, "TRUE")) {
449 else if (!strcasecmp(*vp, "FALSE")) {
454 fprintf(stderr, " Got garbage -> [%s]\n", *vp);