]> git.sur5r.net Git - openldap/blob - clients/ud/find.c
Removed bad LDAP* argument to disambiguate()
[openldap] / clients / ud / find.c
1 /*
2  * Copyright (c) 1991, 1992, 1993 
3  * Regents of the University of Michigan.  All rights reserved.
4  *
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.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/ctype.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #include <lber.h>
22 #include <ldap.h>
23
24 #include "ud.h"
25
26 extern char *search_base;       /* search base */
27 extern int verbose;             /* verbose mode flag */
28 extern LDAP *ld;                /* our ldap descriptor */
29         
30 static int num_picked = 0;      /* used when user picks entry at More prompt */
31
32 #ifdef DEBUG
33 extern int debug;               /* debug flag */
34 #endif
35
36 vrfy(dn)
37 char *dn;
38 {
39         LDAPMessage *results;
40         static char *attrs[2] = { "objectClass", NULL };
41         int ld_errno = 0;
42
43 #ifdef DEBUG
44         if (debug & D_TRACE)
45                 printf("->vrfy(%s)\n", dn);
46 #endif
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);
50
51         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
52
53         if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
54                 return(0);
55         else if (ld_errno == LDAP_SUCCESS)
56                 return(1);
57         else {
58                 ldap_perror(ld, "ldap_search");
59                 return(0);
60         }
61 }
62         
63
64 static LDAPMessage * disambiguate( result, matches, read_attrs, who )
65 LDAPMessage *result;
66 int matches;
67 char **read_attrs;
68 char *who;
69 {
70         int choice;                     /* entry that user chooses */
71         int i;
72         char *namelist[MAX_NUM_NAMES];  /* names found */
73         char response[SMALL_BUF_SIZE];  /* results from user */
74         char *name = NULL;              /* DN to lookup */
75         LDAPMessage *mp;
76         int ld_errno = 0;
77         extern void Free();
78
79 #ifdef DEBUG
80         if (debug & D_TRACE)
81                 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches, 
82                                                         read_attrs, who);
83 #endif
84
85         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
86
87         /*
88          *  If we are here, it means that we got back multiple answers.
89          */
90         if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
91             || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
92                 if (verbose) {
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");
95                 }
96                 else
97                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
98         }
99         printf("  %1d names matched \"%s\".\n", matches, who);
100         for (;;) {
101                 printf("  Do you wish to see a list of names? ");
102                 fflush(stdout);
103                 (void) memset(response, 0, sizeof(response));
104                 fetch_buffer(response, sizeof(response), stdin);
105                 switch (response[0]) {
106                 case 'n' :
107                 case 'N' :
108                 case '\0' :
109                 case '\n' :
110                         return(NULL);
111                         /* NOTREACHED */
112                 case 'y' :
113                 case 'Y' :
114                         print_list(result, namelist, &matches);
115                         if (num_picked == 0)
116                                 choice = pick_one(matches);
117                         else
118                                 choice = num_picked;
119                         num_picked = 0;
120                         if (choice >= 0)
121                                 name = strdup(namelist[choice]);
122                         /*
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.
126                          */
127                         for (i = 1; namelist[i] != NULL; i++)
128                                 Free(namelist[i]);
129                         if (choice < 0) {
130                                 if (name != NULL)
131                                         Free(name);
132                                 return(NULL);
133                         }
134 #ifdef DEBUG
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);
146                         }
147 #endif
148                         if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
149                                 ldap_perror(ld, "ldap_search_s");
150                                 Free(name);
151                                 ldap_msgfree(mp);
152                                 return(NULL);
153                         }
154                         Free(name);
155                         return(mp);
156                         /* NOTREACHED */
157                 default :
158                         printf("  Please enter 'y', 'n', or RETURN.\n");
159                         break;
160                 }
161         }
162 }
163
164 LDAPMessage * find(who, quiet)
165 char *who;
166 int quiet;
167 {
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;
179         LDAPFiltInfo *fi;
180         extern LDAPFiltDesc *lfdp;              /* LDAP filter descriptor */
181         extern struct attribute attrlist[];     /* complete list of attrs */
182         extern void Free();
183
184 #ifdef DEBUG
185         if (debug & D_TRACE)
186                 fprintf(stderr, "->find(%s)\n", who);
187 #endif
188         /* did not specify a 'who' */
189         if (who == NULL) {
190                 printf("  Locate whose entry? ");
191                 fflush(stdout);
192                 fetch_buffer(name, sizeof(name), stdin);
193                 if (name[0] != '\0')
194                         who = name;
195                 else
196                         return(NULL);
197         }
198         if (attrs_set == 0) {
199                 j = k = 0;
200                 attrs_set = 1;
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;
206                 }
207                 read_attrs[j] = NULL;
208                 search_attrs[k] = NULL;
209         }
210
211         /*
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.
215          */
216         if (strchr(who, ',') != NULL) {
217                 int     savederef, deref;
218 #ifdef DEBUG
219                 if (debug & D_FIND)
220                         printf("\"%s\" appears to be a UFN\n", who);
221 #endif
222                 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
223                 deref = LDAP_DEREF_FINDING;
224                 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
225
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);
231                         return(NULL);
232                 }
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);
236                         return(NULL);
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) {
239                                 int ld_errno = 0;
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);
243                                 else
244                                         ldap_perror(ld, "ldap_search_s");
245                                 return(NULL);
246                         }
247                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
248                         return(res);
249                 } else if (matches > 1 ) {
250                         return disambiguate( res, matches, read_attrs, who );
251                 }
252                 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
253         }
254
255         /*
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.
258          */
259         for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
260                 if (!admonished) {
261                         printf("  You do not need to enclose names in quotes.\n");
262                         admonished = TRUE;
263                 }
264                 *cp++ = ' ';
265                 if (*cp == '\0')
266                         break;
267         }
268
269         /*
270          *  It wasn't a UFN, so look it up in the usual method.
271          */
272         for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
273              fi = ldap_getnextfilter(lfdp)) {
274 #ifdef DEBUG
275                 if (debug & D_FIND)
276                         printf("Searching, filter = %s\n", fi->lfi_filter);
277 #endif
278
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");
283                         ldap_msgfree(res);
284                         return(NULL);
285                 }
286                 if ((matches = ldap_count_entries(ld, res)) < 0) {
287                         ldap_perror(ld, "ldap_count_entries");
288                         ldap_msgfree(res);
289                         return(NULL);
290                 }
291                 else if (matches == 1) {
292                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
293                         ldap_msgfree(res);
294                         if (!quiet)
295                                 printf("  Found one %s match for \"%s\"\n", 
296                                                         fi->lfi_desc, who);
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);
301                                 fflush(stdout);
302                                 fetch_buffer(response, sizeof(response), stdin);
303                                 if ((response[0] == 'n') || (response[0] == 'N'))
304                                         return(NULL);
305                         }
306 #ifdef DEBUG
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);
318                         }
319 #endif
320                         if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
321                                 ldap_perror(ld, "ldap_search_s");
322                                 ldap_msgfree(res);
323                                 return(NULL);
324                         }
325                         Free(dn);
326                         return(res);
327                 }
328                 else if (matches > 0) {
329                         ldtmp = disambiguate(res, matches, read_attrs, who);
330                         ldap_msgfree(res);
331                         return(ldtmp);
332                 }
333                 /* if we're here, there were zero matches */
334                 ldap_msgfree(res);
335         }
336         return(NULL);
337 }
338
339 pick_one(i)
340 int i;
341 {
342         int n;
343         char user_pick[SMALL_BUF_SIZE];
344
345 #ifdef DEBUG
346         if (debug & D_TRACE)
347                 printf("->pick_one(%d)\n", i);
348 #endif
349         
350         /* make the user pick an entry */
351         for (;;) {
352                 printf("  Enter the number of the name you want or Q to quit: ");
353                 fflush(stdout);
354                 fetch_buffer(user_pick, sizeof(user_pick), stdin);
355                 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
356                         return(-1);
357                 n = atoi(user_pick);
358                 if ((n > 0) && (n <= i))
359                         return(n);
360                 printf("  Invalid response\n");
361         }
362         /* NOTREACHED */
363 }
364
365 print_list(list, names, matches)
366 LDAPMessage *list;
367 char *names[];
368 int *matches;
369 {
370         char **rdns, **cpp;
371         extern int lpp;
372         char resp[SMALL_BUF_SIZE];
373         register LDAPMessage *ep;
374         register int i = 1;
375         register int rest = 4;          /* 4, not 1 */
376
377 #ifdef DEBUG
378         if (debug & D_TRACE)
379                 printf("->print_list(%x, %x, %x)\n", list, names, matches);
380 #endif
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)) {
383                 
384                 names[i] = ldap_get_dn(ld, ep);
385                 rdns = ldap_explode_dn(names[i], TRUE);
386                 cpp = ldap_get_values(ld, ep, "title");
387                 if (cpp == NULL)
388                         printf(" %3d. %s\n", i, *rdns);
389                 else
390                         printf(" %3d. %s, %s\n", i, *rdns, *cpp);
391                 ldap_value_free(rdns);
392                 ldap_value_free(cpp);
393                 i++;
394                 if ((rest++ > (lpp - 1)) && (i < *matches)) {
395 again:
396                         printf("  More? ");
397                         fflush(stdout);
398                         fetch_buffer(resp, sizeof(resp), stdin);
399                         if ((resp[0] == 'n') || (resp[0] == 'N'))
400                                 break;
401                         else if ((num_picked = atoi(resp)) != 0) {
402                                 if (num_picked < i)
403                                         break;
404                                 else
405                                         goto again;
406                         }
407                         rest = 1;
408                 }
409         }
410         *matches = i - 1;
411         names[i] = NULL;
412         return;
413 }
414
415 find_all_subscribers(sub, group)
416 char *sub[];
417 char *group;
418 {
419         int count;
420         LDAPMessage *result;
421         static char *attributes[] = { "cn", NULL };
422         char filter[MED_BUF_SIZE];
423         register LDAPMessage *ep;
424         register int i = 0;
425
426 #ifdef DEBUG
427         if (debug & D_TRACE)
428                 printf("->find_all_subscribers(%x, %s)\n", sub, group);
429 #endif
430
431         sprintf(filter, "%s=%s", "memberOfGroup", group);
432         if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
433                 int ld_errno = 0;
434                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
435                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
436                         return(0);
437                 ldap_perror(ld, "ldap_search_s");
438                 return(0);
439         }
440         count = ldap_count_entries(ld, result);
441         if (count < 1) {
442                 ldap_msgfree(result);
443                 return(0);
444         }
445         if ( count > MAX_VALUES ) {
446                 printf( "  Only retrieving the first %d subscribers....\n",
447                         MAX_VALUES );
448         }
449
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);
452 #ifdef DEBUG
453                 if (debug & D_PARSE)
454                         printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
455 #endif
456         }
457         sub[i] = NULL;
458         ldap_msgfree(result);
459         return(count);
460 }
461
462 char * fetch_boolean_value(who, attr)
463 char *who;
464 struct attribute attr;
465 {
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 };
470
471 #ifdef DEBUG
472         if (debug & D_TRACE)
473                 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
474 #endif
475         attributes[0] = attr.quipu_name;
476         if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
477                 int ld_errno = 0;
478                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
479                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
480                         return("FALSE");
481                 ldap_perror(ld, "ldap_search_s");
482                 ldap_msgfree(result);
483                 return(NULL);
484         }
485
486         /*
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.
489          *
490          *  If ldap_first_attribute() returns NULL, then this entry did
491          *  not have this particular attribute.
492          */
493         ep = ldap_first_entry(ld, result);
494         if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
495                 ldap_msgfree(result);
496                 return("FALSE");
497         }
498         else {
499                 ldap_msgfree(result);
500                 if (!strcasecmp(*vp, "TRUE")) {
501                         ldap_value_free(vp);
502                         return("TRUE");
503                 }
504                 else if (!strcasecmp(*vp, "FALSE")) {
505                         ldap_value_free(vp);
506                         return("FALSE");
507                 }
508                 else {
509                         fprintf(stderr, "  Got garbage -> [%s]\n", *vp);
510                         ldap_value_free(vp);
511                         return(NULL);
512                 }
513         }
514 }