]> git.sur5r.net Git - openldap/blob - clients/ud/find.c
Undo last change (I commited the wrong version)
[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( ld, res, matches, read_attrs,
251                             who ) );
252                 }
253                 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
254         }
255
256         /*
257          *  Old users of the MTS *USERDIRECTORY will likely wrap the name
258          *  in quotes.  Not only is this unnecessary, but it also won't work.
259          */
260         for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
261                 if (!admonished) {
262                         printf("  You do not need to enclose names in quotes.\n");
263                         admonished = TRUE;
264                 }
265                 *cp++ = ' ';
266                 if (*cp == '\0')
267                         break;
268         }
269
270         /*
271          *  It wasn't a UFN, so look it up in the usual method.
272          */
273         for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
274              fi = ldap_getnextfilter(lfdp)) {
275 #ifdef DEBUG
276                 if (debug & D_FIND)
277                         printf("Searching, filter = %s\n", fi->lfi_filter);
278 #endif
279
280                 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope, 
281                 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
282                 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
283                         ldap_perror(ld, "ldap_search_s");
284                         ldap_msgfree(res);
285                         return(NULL);
286                 }
287                 if ((matches = ldap_count_entries(ld, res)) < 0) {
288                         ldap_perror(ld, "ldap_count_entries");
289                         ldap_msgfree(res);
290                         return(NULL);
291                 }
292                 else if (matches == 1) {
293                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
294                         ldap_msgfree(res);
295                         if (!quiet)
296                                 printf("  Found one %s match for \"%s\"\n", 
297                                                         fi->lfi_desc, who);
298                         if (!fi->lfi_isexact) {
299                                 rdns = ldap_explode_dn(dn, TRUE);
300                                 printf("  Do you mean %s? ", *rdns);
301                                 (void) ldap_value_free(rdns);
302                                 fflush(stdout);
303                                 fetch_buffer(response, sizeof(response), stdin);
304                                 if ((response[0] == 'n') || (response[0] == 'N'))
305                                         return(NULL);
306                         }
307 #ifdef DEBUG
308                         if (debug & D_FIND) {
309                                 printf("  Calling ldap_search_s()\n");
310                                 printf("     ld = 0x%x\n", ld);
311                                 printf("     dn = %s\n", dn);
312                                 printf("     scope = LDAP_SCOPE_BASE\n");
313                                 printf("     filter = %s\n", "objectClass=*");
314                                 for (i = 0; read_attrs[i] != NULL; i++)
315                                         printf("     read_attrs[%d] = %s\n", i, read_attrs[i]);
316                                 printf("     read_attrs[%d] = NULL\n", i);
317                                 printf("     attrsonly = FALSE\n");
318                                 printf("     &results = 0x%x\n", &res);
319                         }
320 #endif
321                         if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
322                                 ldap_perror(ld, "ldap_search_s");
323                                 ldap_msgfree(res);
324                                 return(NULL);
325                         }
326                         Free(dn);
327                         return(res);
328                 }
329                 else if (matches > 0) {
330                         ldtmp = disambiguate(res, matches, read_attrs, who);
331                         ldap_msgfree(res);
332                         return(ldtmp);
333                 }
334                 /* if we're here, there were zero matches */
335                 ldap_msgfree(res);
336         }
337         return(NULL);
338 }
339
340 pick_one(i)
341 int i;
342 {
343         int n;
344         char user_pick[SMALL_BUF_SIZE];
345
346 #ifdef DEBUG
347         if (debug & D_TRACE)
348                 printf("->pick_one(%d)\n", i);
349 #endif
350         
351         /* make the user pick an entry */
352         for (;;) {
353                 printf("  Enter the number of the name you want or Q to quit: ");
354                 fflush(stdout);
355                 fetch_buffer(user_pick, sizeof(user_pick), stdin);
356                 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
357                         return(-1);
358                 n = atoi(user_pick);
359                 if ((n > 0) && (n <= i))
360                         return(n);
361                 printf("  Invalid response\n");
362         }
363         /* NOTREACHED */
364 }
365
366 print_list(list, names, matches)
367 LDAPMessage *list;
368 char *names[];
369 int *matches;
370 {
371         char **rdns, **cpp;
372         extern int lpp;
373         char resp[SMALL_BUF_SIZE];
374         register LDAPMessage *ep;
375         register int i = 1;
376         register int rest = 4;          /* 4, not 1 */
377
378 #ifdef DEBUG
379         if (debug & D_TRACE)
380                 printf("->print_list(%x, %x, %x)\n", list, names, matches);
381 #endif
382         /* print a list of names from which the user will select */
383         for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
384                 
385                 names[i] = ldap_get_dn(ld, ep);
386                 rdns = ldap_explode_dn(names[i], TRUE);
387                 cpp = ldap_get_values(ld, ep, "title");
388                 if (cpp == NULL)
389                         printf(" %3d. %s\n", i, *rdns);
390                 else
391                         printf(" %3d. %s, %s\n", i, *rdns, *cpp);
392                 ldap_value_free(rdns);
393                 ldap_value_free(cpp);
394                 i++;
395                 if ((rest++ > (lpp - 1)) && (i < *matches)) {
396 again:
397                         printf("  More? ");
398                         fflush(stdout);
399                         fetch_buffer(resp, sizeof(resp), stdin);
400                         if ((resp[0] == 'n') || (resp[0] == 'N'))
401                                 break;
402                         else if ((num_picked = atoi(resp)) != 0) {
403                                 if (num_picked < i)
404                                         break;
405                                 else
406                                         goto again;
407                         }
408                         rest = 1;
409                 }
410         }
411         *matches = i - 1;
412         names[i] = NULL;
413         return;
414 }
415
416 find_all_subscribers(sub, group)
417 char *sub[];
418 char *group;
419 {
420         int count;
421         LDAPMessage *result;
422         static char *attributes[] = { "cn", NULL };
423         char filter[MED_BUF_SIZE];
424         register LDAPMessage *ep;
425         register int i = 0;
426
427 #ifdef DEBUG
428         if (debug & D_TRACE)
429                 printf("->find_all_subscribers(%x, %s)\n", sub, group);
430 #endif
431
432         sprintf(filter, "%s=%s", "memberOfGroup", group);
433         if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
434                 int ld_errno = 0;
435                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
436                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
437                         return(0);
438                 ldap_perror(ld, "ldap_search_s");
439                 return(0);
440         }
441         count = ldap_count_entries(ld, result);
442         if (count < 1) {
443                 ldap_msgfree(result);
444                 return(0);
445         }
446         if ( count > MAX_VALUES ) {
447                 printf( "  Only retrieving the first %d subscribers....\n",
448                         MAX_VALUES );
449         }
450
451         for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
452                 sub[i++] = ldap_get_dn(ld, ep);
453 #ifdef DEBUG
454                 if (debug & D_PARSE)
455                         printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
456 #endif
457         }
458         sub[i] = NULL;
459         ldap_msgfree(result);
460         return(count);
461 }
462
463 char * fetch_boolean_value(who, attr)
464 char *who;
465 struct attribute attr;
466 {
467         LDAPMessage *result;            /* from the search below */
468         register LDAPMessage *ep;       /* entry pointer */
469         register char **vp;             /* for parsing the result */
470         static char *attributes[] = { NULL, NULL };
471
472 #ifdef DEBUG
473         if (debug & D_TRACE)
474                 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
475 #endif
476         attributes[0] = attr.quipu_name;
477         if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
478                 int ld_errno = 0;
479                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
480                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
481                         return("FALSE");
482                 ldap_perror(ld, "ldap_search_s");
483                 ldap_msgfree(result);
484                 return(NULL);
485         }
486
487         /*
488          *  We did a read on one name and only asked for one attribute.
489          *  There's no reason to loop through any of these structures.
490          *
491          *  If ldap_first_attribute() returns NULL, then this entry did
492          *  not have this particular attribute.
493          */
494         ep = ldap_first_entry(ld, result);
495         if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
496                 ldap_msgfree(result);
497                 return("FALSE");
498         }
499         else {
500                 ldap_msgfree(result);
501                 if (!strcasecmp(*vp, "TRUE")) {
502                         ldap_value_free(vp);
503                         return("TRUE");
504                 }
505                 else if (!strcasecmp(*vp, "FALSE")) {
506                         ldap_value_free(vp);
507                         return("FALSE");
508                 }
509                 else {
510                         fprintf(stderr, "  Got garbage -> [%s]\n", *vp);
511                         ldap_value_free(vp);
512                         return(NULL);
513                 }
514         }
515 }