]> git.sur5r.net Git - openldap/blob - clients/ud/find.c
Free() -> ldap_memfree() changes. Remove use of the return value of free().
[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 #include <stdlib.h>
17
18 #include <ac/ctype.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include <lber.h>
23 #include <ldap.h>
24
25 #include "ud.h"
26
27 static int num_picked = 0;      /* used when user picks entry at More prompt */
28
29
30 int
31 vrfy( char *dn )
32 {
33         LDAPMessage *results;
34         static char *attrs[2] = { "objectClass", NULL };
35         int ld_errno = 0;
36
37 #ifdef DEBUG
38         if (debug & D_TRACE)
39                 printf("->vrfy(%s)\n", dn);
40 #endif
41         /* verify that this DN exists in the directory */
42         (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", attrs, TRUE, &results);
43         (void) ldap_msgfree(results);
44
45         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
46
47         if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
48                 return(0);
49         else if (ld_errno == LDAP_SUCCESS)
50                 return(1);
51         else {
52                 ldap_perror(ld, "ldap_search");
53                 return(0);
54         }
55 }
56         
57
58 static LDAPMessage *
59 disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
60 {
61         int choice;                     /* entry that user chooses */
62         int i;
63         char *namelist[MAX_NUM_NAMES];  /* names found */
64         char response[SMALL_BUF_SIZE];  /* results from user */
65         char *name = NULL;              /* DN to lookup */
66         LDAPMessage *mp;
67         int ld_errno = 0;
68
69 #ifdef DEBUG
70         if (debug & D_TRACE)
71                 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches, 
72                                                         read_attrs, who);
73 #endif
74
75         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
76
77         /*
78          *  If we are here, it means that we got back multiple answers.
79          */
80         if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
81             || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
82                 if (verbose) {
83                         printf("  Your query was too general and a limit was exceeded.  The results listed\n");
84                         printf("  are not complete.  You may want to try again with a more refined query.\n\n");
85                 }
86                 else
87                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
88         }
89         printf("  %1d names matched \"%s\".\n", matches, who);
90         for (;;) {
91                 printf("  Do you wish to see a list of names? ");
92                 fflush(stdout);
93                 (void) memset(response, 0, sizeof(response));
94                 fetch_buffer(response, sizeof(response), stdin);
95                 switch (response[0]) {
96                 case 'n' :
97                 case 'N' :
98                 case '\0' :
99                 case '\n' :
100                         return(NULL);
101                         /* NOTREACHED */
102                 case 'y' :
103                 case 'Y' :
104                         print_list(result, namelist, &matches);
105                         if (num_picked == 0)
106                                 choice = pick_one(matches);
107                         else
108                                 choice = num_picked;
109                         num_picked = 0;
110                         if (choice >= 0)
111                                 name = strdup(namelist[choice]);
112                         /*
113                          *  Now free up all of the pointers allocated in
114                          *  namelist.  The print_list() routine that filled
115                          *  in this collection of strings starts at 1, not 0.
116                          */
117                         for (i = 1; namelist[i] != NULL; i++)
118                                 Free(namelist[i]);
119                         if (choice < 0) {
120                                 if (name != NULL)
121                                         Free(name);
122                                 return(NULL);
123                         }
124 #ifdef DEBUG
125                         if (debug & D_FIND) {
126                                 printf("  Calling ldap_search_s()\n");
127                                 printf("     ld = 0x%x\n", ld);
128                                 printf("     search base = %s\n", name);
129                                 printf("     scope = LDAP_SCOPE_BASE\n");
130                                 printf("     filter = objectClass=*\n");
131                                 for (i = 0; read_attrs[i] != NULL; i++)
132                                         printf("     read_attrs[%d] = %s\n", i, read_attrs[i]);
133                                 printf("     read_attrs[%d] = NULL\n", i);
134                                 printf("     attrsonly = FALSE\n");
135                                 printf("     &mp = 0x%x\n", &mp);
136                         }
137 #endif
138                         if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
139                                 ldap_perror(ld, "ldap_search_s");
140                                 Free(name);
141                                 ldap_msgfree(mp);
142                                 return(NULL);
143                         }
144                         Free(name);
145                         return(mp);
146                         /* NOTREACHED */
147                 default :
148                         printf("  Please enter 'y', 'n', or RETURN.\n");
149                         break;
150                 }
151         }
152 }
153
154 LDAPMessage *
155 find( char *who, int quiet )
156 {
157         register int i, j, k;           /* general ints */
158         int matches;                    /* from ldap_count_entries() */
159         int admonished = FALSE;
160         static int attrs_set = 0;
161         static char *read_attrs[MAX_ATTRS];     /* attrs to use in a read op */
162         static char *search_attrs[MAX_ATTRS];   /* attrs to use in a srch op */
163         static int rc;                  /* return from ldap_search */
164         LDAPMessage *ldtmp, *res;       /* results returned from search */
165         char name[MED_BUF_SIZE];
166         char response[SMALL_BUF_SIZE];
167         char *cp, *dn, **rdns;
168         LDAPFiltInfo *fi;
169
170 #ifdef DEBUG
171         if (debug & D_TRACE)
172                 fprintf(stderr, "->find(%s)\n", who);
173 #endif
174         /* did not specify a 'who' */
175         if (who == NULL) {
176                 printf("  Locate whose entry? ");
177                 fflush(stdout);
178                 fetch_buffer(name, sizeof(name), stdin);
179                 if (name[0] != '\0')
180                         who = name;
181                 else
182                         return(NULL);
183         }
184         if (attrs_set == 0) {
185                 j = k = 0;
186                 attrs_set = 1;
187                 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
188                         if (attrlist[i].flags & ATTR_FLAG_READ)
189                                 read_attrs[j++] = attrlist[i].quipu_name;
190                         if (attrlist[i].flags & ATTR_FLAG_SEARCH)
191                                 search_attrs[k++] = attrlist[i].quipu_name;
192                 }
193                 read_attrs[j] = NULL;
194                 search_attrs[k] = NULL;
195         }
196
197         /*
198          *  If the user-supplied name has any commas in it, we
199          *  assume that it is a UFN, and do everything right
200          *  here.  If we don't find it, treat it as NOT a UFN.
201          */
202         if (strchr(who, ',') != NULL) {
203                 int     savederef, deref;
204 #ifdef DEBUG
205                 if (debug & D_FIND)
206                         printf("\"%s\" appears to be a UFN\n", who);
207 #endif
208                 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
209                 deref = LDAP_DEREF_FINDING;
210                 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
211
212                 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
213                     LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
214                     rc != LDAP_TIMELIMIT_EXCEEDED) {
215                         ldap_perror(ld, "ldap_ufn_search_s");
216                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
217                         return(NULL);
218                 }
219                 if ((matches = ldap_count_entries(ld, res)) < 0) {
220                         ldap_perror(ld, "ldap_count_entries");
221                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
222                         return(NULL);
223                 } else if (matches == 1) {
224                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
225                         rc = ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res);
226                         ldap_memfree(dn);
227                         if (rc != LDAP_SUCCESS) {
228                                 int ld_errno = 0;
229                                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
230                                 if (ld_errno == LDAP_UNAVAILABLE)
231                                         printf("  Could not contact the LDAP server to find \"%s\".\n", who);
232                                 else
233                                         ldap_perror(ld, "ldap_search_s");
234                                 return(NULL);
235                         }
236                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
237                         return(res);
238                 } else if (matches > 1 ) {
239                         return disambiguate( res, matches, read_attrs, who );
240                 }
241                 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
242         }
243
244         /*
245          *  Old users of the MTS *USERDIRECTORY will likely wrap the name
246          *  in quotes.  Not only is this unnecessary, but it also won't work.
247          */
248         for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
249                 if (!admonished) {
250                         printf("  You do not need to enclose names in quotes.\n");
251                         admonished = TRUE;
252                 }
253                 *cp++ = ' ';
254                 if (*cp == '\0')
255                         break;
256         }
257
258         /*
259          *  It wasn't a UFN, so look it up in the usual method.
260          */
261         for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
262              fi = ldap_getnextfilter(lfdp)) {
263 #ifdef DEBUG
264                 if (debug & D_FIND)
265                         printf("Searching, filter = %s\n", fi->lfi_filter);
266 #endif
267
268                 if ((rc = ldap_search_s(ld, search_base, fi->lfi_scope, 
269                 fi->lfi_filter, search_attrs, FALSE, &res)) != LDAP_SUCCESS &&
270                 rc != LDAP_SIZELIMIT_EXCEEDED && rc != LDAP_TIMELIMIT_EXCEEDED) {
271                         ldap_perror(ld, "ldap_search_s");
272                         ldap_msgfree(res);
273                         return(NULL);
274                 }
275                 if ((matches = ldap_count_entries(ld, res)) < 0) {
276                         ldap_perror(ld, "ldap_count_entries");
277                         ldap_msgfree(res);
278                         return(NULL);
279                 }
280                 else if (matches == 1) {
281                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
282                         ldap_msgfree(res);
283                         if (!quiet)
284                                 printf("  Found one %s match for \"%s\"\n", 
285                                                         fi->lfi_desc, who);
286                         if (!fi->lfi_isexact) {
287                                 rdns = ldap_explode_dn(dn, TRUE);
288                                 printf("  Do you mean %s? ", *rdns);
289                                 (void) ldap_value_free(rdns);
290                                 fflush(stdout);
291                                 fetch_buffer(response, sizeof(response), stdin);
292                                 if ((response[0] == 'n') || (response[0] == 'N'))
293                                 {
294                                         ldap_memfree(dn);
295                                         return(NULL);
296                                 }
297                         }
298 #ifdef DEBUG
299                         if (debug & D_FIND) {
300                                 printf("  Calling ldap_search_s()\n");
301                                 printf("     ld = 0x%x\n", ld);
302                                 printf("     dn = %s\n", dn);
303                                 printf("     scope = LDAP_SCOPE_BASE\n");
304                                 printf("     filter = %s\n", "objectClass=*");
305                                 for (i = 0; read_attrs[i] != NULL; i++)
306                                         printf("     read_attrs[%d] = %s\n", i, read_attrs[i]);
307                                 printf("     read_attrs[%d] = NULL\n", i);
308                                 printf("     attrsonly = FALSE\n");
309                                 printf("     &results = 0x%x\n", &res);
310                         }
311 #endif
312                         if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
313                                 ldap_perror(ld, "ldap_search_s");
314                                 ldap_msgfree(res);
315                                 res = NULL;
316                         }
317                         ldap_memfree(dn);
318                         return(res);
319                 }
320                 else if (matches > 0) {
321                         ldtmp = disambiguate(res, matches, read_attrs, who);
322                         ldap_msgfree(res);
323                         return(ldtmp);
324                 }
325                 /* if we're here, there were zero matches */
326                 ldap_msgfree(res);
327         }
328         return(NULL);
329 }
330
331 int
332 pick_one( int i )
333 {
334         int n;
335         char user_pick[SMALL_BUF_SIZE];
336
337 #ifdef DEBUG
338         if (debug & D_TRACE)
339                 printf("->pick_one(%d)\n", i);
340 #endif
341         
342         /* make the user pick an entry */
343         for (;;) {
344                 printf("  Enter the number of the name you want or Q to quit: ");
345                 fflush(stdout);
346                 fetch_buffer(user_pick, sizeof(user_pick), stdin);
347                 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
348                         return(-1);
349                 n = atoi(user_pick);
350                 if ((n > 0) && (n <= i))
351                         return(n);
352                 printf("  Invalid response\n");
353         }
354         /* NOTREACHED */
355 }
356
357 void
358 print_list( LDAPMessage *list, char **names, int *matches )
359 {
360         char **rdns, **cpp;
361         char resp[SMALL_BUF_SIZE];
362         register LDAPMessage *ep;
363         register int i = 1;
364         register int rest = 4;          /* 4, not 1 */
365
366 #ifdef DEBUG
367         if (debug & D_TRACE)
368                 printf("->print_list(%x, %x, %x)\n", list, names, matches);
369 #endif
370         /* print a list of names from which the user will select */
371         for (ep = ldap_first_entry(ld, list); ep != NULL; ep = ldap_next_entry(ld, ep)) {
372                 
373                 names[i] = ldap_get_dn(ld, ep);
374                 rdns = ldap_explode_dn(names[i], TRUE);
375                 cpp = ldap_get_values(ld, ep, "title");
376                 if (cpp == NULL)
377                         printf(" %3d. %s\n", i, *rdns);
378                 else
379                         printf(" %3d. %s, %s\n", i, *rdns, *cpp);
380                 ldap_value_free(rdns);
381                 ldap_value_free(cpp);
382                 i++;
383                 if ((rest++ > (lpp - 1)) && (i < *matches)) {
384 again:
385                         printf("  More? ");
386                         fflush(stdout);
387                         fetch_buffer(resp, sizeof(resp), stdin);
388                         if ((resp[0] == 'n') || (resp[0] == 'N'))
389                                 break;
390                         else if ((num_picked = atoi(resp)) != 0) {
391                                 if (num_picked < i)
392                                         break;
393                                 else
394                                         goto again;
395                         }
396                         rest = 1;
397                 }
398         }
399         *matches = i - 1;
400         names[i] = NULL;
401         return;
402 }
403
404 int
405 find_all_subscribers( char **sub, char *group )
406 {
407         int count;
408         LDAPMessage *result;
409         static char *attributes[] = { "cn", NULL };
410         char filter[MED_BUF_SIZE];
411         register LDAPMessage *ep;
412         register int i = 0;
413
414 #ifdef DEBUG
415         if (debug & D_TRACE)
416                 printf("->find_all_subscribers(%x, %s)\n", sub, group);
417 #endif
418
419         sprintf(filter, "%s=%s", "memberOfGroup", group);
420         if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
421                 int ld_errno = 0;
422                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
423                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
424                         return(0);
425                 ldap_perror(ld, "ldap_search_s");
426                 return(0);
427         }
428         count = ldap_count_entries(ld, result);
429         if (count < 1) {
430                 ldap_msgfree(result);
431                 return(0);
432         }
433         if ( count > MAX_VALUES ) {
434                 printf( "  Only retrieving the first %d subscribers....\n",
435                         MAX_VALUES );
436         }
437
438         for (ep = ldap_first_entry(ld, result); i < MAX_VALUES && ep != NULL; ep = ldap_next_entry(ld, ep)) {
439                 sub[i++] = ldap_get_dn(ld, ep);
440 #ifdef DEBUG
441                 if (debug & D_PARSE)
442                         printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
443 #endif
444         }
445         sub[i] = NULL;
446         ldap_msgfree(result);
447         return(count);
448 }
449
450 char *
451 fetch_boolean_value( char *who, struct attribute attr )
452 {
453         LDAPMessage *result;            /* from the search below */
454         register LDAPMessage *ep;       /* entry pointer */
455         register char **vp;             /* for parsing the result */
456         static char *attributes[] = { NULL, NULL };
457
458 #ifdef DEBUG
459         if (debug & D_TRACE)
460                 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
461 #endif
462         attributes[0] = attr.quipu_name;
463         if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
464                 int ld_errno = 0;
465                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
466                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
467                         return("FALSE");
468                 ldap_perror(ld, "ldap_search_s");
469                 ldap_msgfree(result);
470                 return(NULL);
471         }
472
473         /*
474          *  We did a read on one name and only asked for one attribute.
475          *  There's no reason to loop through any of these structures.
476          *
477          *  If ldap_first_attribute() returns NULL, then this entry did
478          *  not have this particular attribute.
479          */
480         ep = ldap_first_entry(ld, result);
481         if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
482                 ldap_msgfree(result);
483                 return("FALSE");
484         }
485         else {
486                 ldap_msgfree(result);
487                 if (!strcasecmp(*vp, "TRUE")) {
488                         ldap_value_free(vp);
489                         return("TRUE");
490                 }
491                 else if (!strcasecmp(*vp, "FALSE")) {
492                         ldap_value_free(vp);
493                         return("FALSE");
494                 }
495                 else {
496                         fprintf(stderr, "  Got garbage -> [%s]\n", *vp);
497                         ldap_value_free(vp);
498                         return(NULL);
499                 }
500         }
501 }