]> git.sur5r.net Git - openldap/blob - clients/ud/find.c
modify ldap_dn2ufn() to return completely typeless UFNs
[openldap] / clients / ud / find.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1991, 1992, 1993 
8  * Regents of the University of Michigan.  All rights reserved.
9  *
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.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/stdlib.h>
23
24 #include <ac/ctype.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include <ldap.h>
29
30 #include "ud.h"
31
32 static int num_picked = 0;      /* used when user picks entry at More prompt */
33
34
35 int
36 vrfy( char *dn )
37 {
38         LDAPMessage *results = NULL;
39         static char *attrs[2] = { "1.1", NULL };
40         int ld_errno = 0;
41
42 #ifdef DEBUG
43         if (debug & D_TRACE)
44                 printf("->vrfy(%s)\n", dn);
45 #endif
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);
49
50         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
51
52         if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
53                 return(0);
54         else if (ld_errno == LDAP_SUCCESS)
55                 return(1);
56         else {
57                 ldap_perror(ld, "ldap_search");
58                 return(0);
59         }
60 }
61         
62
63 static LDAPMessage *
64 disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
65 {
66         int choice;                     /* entry that user chooses */
67         int i;
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;
72         int ld_errno = 0;
73
74 #ifdef DEBUG
75         if (debug & D_TRACE)
76                 printf("->disambiguate(%x, %d, %x, %s)\n", result, matches, 
77                                                         read_attrs, who);
78 #endif
79
80         ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
81
82         /*
83          *  If we are here, it means that we got back multiple answers.
84          */
85         if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
86             || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
87                 if (verbose) {
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");
90                 }
91                 else
92                         printf("  Time or size limit exceeded.  Partial results follow.\n\n");
93         }
94         printf("  %1d names matched \"%s\".\n", matches, who);
95         for (;;) {
96                 printf("  Do you wish to see a list of names? ");
97                 fflush(stdout);
98                 (void) memset(response, '\0', sizeof(response));
99                 fetch_buffer(response, sizeof(response), stdin);
100                 switch (response[0]) {
101                 case 'n' :
102                 case 'N' :
103                 case '\0' :
104                 case '\n' :
105                         return(NULL);
106                         /* NOTREACHED */
107                 case 'y' :
108                 case 'Y' :
109                         print_list(result, namelist, &matches);
110                         if (num_picked == 0)
111                                 choice = pick_one(matches);
112                         else
113                                 choice = num_picked;
114                         num_picked = 0;
115                         if (choice >= 0)
116                                 name = strdup(namelist[choice]);
117                         /*
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.
121                          */
122                         for (i = 1; namelist[i] != NULL; i++)
123                                 Free(namelist[i]);
124                         if (choice < 0) {
125                                 if (name != NULL)
126                                         Free(name);
127                                 return(NULL);
128                         }
129 #ifdef DEBUG
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);
141                         }
142 #endif
143                         if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
144                                 ldap_perror(ld, "ldap_search_s");
145                                 Free(name);
146                                 ldap_msgfree(mp);
147                                 return(NULL);
148                         }
149                         Free(name);
150                         return(mp);
151                         /* NOTREACHED */
152                 default :
153                         printf("  Please enter 'y', 'n', or RETURN.\n");
154                         break;
155                 }
156         }
157 }
158
159 LDAPMessage *
160 find( char *who, int quiet )
161 {
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;
173         LDAPFiltInfo *fi;
174
175 #ifdef DEBUG
176         if (debug & D_TRACE)
177                 fprintf(stderr, "->find(%s)\n", who);
178 #endif
179         /* did not specify a 'who' */
180         if (who == NULL) {
181                 printf("  Locate whose entry? ");
182                 fflush(stdout);
183                 fetch_buffer(name, sizeof(name), stdin);
184                 if (name[0] != '\0')
185                         who = name;
186                 else
187                         return(NULL);
188         }
189         if (attrs_set == 0) {
190                 j = k = 0;
191                 attrs_set = 1;
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;
197                 }
198                 read_attrs[j] = NULL;
199                 search_attrs[k] = NULL;
200         }
201
202 #if LDAP_UFN
203         /*
204          *  If the user-supplied name has any commas in it, we
205          *  assume that it is a UFN, and do everything right
206          *  here.  If we don't find it, treat it as NOT a UFN.
207          */
208         if (strchr(who, ',') != NULL) {
209                 int     savederef, deref;
210 #ifdef DEBUG
211                 if (debug & D_FIND)
212                         printf("\"%s\" appears to be a UFN\n", who);
213 #endif
214                 ldap_get_option(ld, LDAP_OPT_DEREF, &savederef);
215                 deref = LDAP_DEREF_FINDING;
216                 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
217
218                 if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
219                     LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
220                     rc != LDAP_TIMELIMIT_EXCEEDED) {
221                         ldap_perror(ld, "ldap_ufn_search_s");
222                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
223                         return(NULL);
224                 }
225                 if ((matches = ldap_count_entries(ld, res)) < 0) {
226                         ldap_perror(ld, "ldap_count_entries");
227                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
228                         return(NULL);
229                 } else if (matches == 1) {
230                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
231                         rc = ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res);
232                         ldap_memfree(dn);
233                         if (rc != LDAP_SUCCESS) {
234                                 ldap_perror(ld, "ldap_search_s");
235                                 return(NULL);
236                         }
237                         ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
238                         return(res);
239                 } else if (matches > 1 ) {
240                         return disambiguate( res, matches, read_attrs, who );
241                 }
242                 ldap_set_option(ld, LDAP_OPT_DEREF, &savederef);
243         }
244 #endif
245
246         /*
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.
249          */
250         for (cp = strchr(who, '"'); cp != NULL; cp = strchr(cp, '"')) {
251                 if (!admonished) {
252                         printf("  You do not need to enclose names in quotes.\n");
253                         admonished = TRUE;
254                 }
255                 *cp++ = ' ';
256                 if (*cp == '\0')
257                         break;
258         }
259
260         /*
261          *  It wasn't a UFN, so look it up in the usual method.
262          */
263         for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
264              fi = ldap_getnextfilter(lfdp)) {
265 #ifdef DEBUG
266                 if (debug & D_FIND)
267                         printf("Searching, filter = %s\n", fi->lfi_filter);
268 #endif
269
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");
274                         ldap_msgfree(res);
275                         return(NULL);
276                 }
277                 if ((matches = ldap_count_entries(ld, res)) < 0) {
278                         ldap_perror(ld, "ldap_count_entries");
279                         ldap_msgfree(res);
280                         return(NULL);
281                 }
282                 else if (matches == 1) {
283                         dn = ldap_get_dn(ld, ldap_first_entry(ld, res));
284                         ldap_msgfree(res);
285                         if (!quiet)
286                                 printf("  Found one %s match for \"%s\"\n", 
287                                                         fi->lfi_desc, who);
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);
292                                 fflush(stdout);
293                                 fetch_buffer(response, sizeof(response), stdin);
294                                 if ((response[0] == 'n') || (response[0] == 'N'))
295                                 {
296                                         ldap_memfree(dn);
297                                         return(NULL);
298                                 }
299                         }
300 #ifdef DEBUG
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);
312                         }
313 #endif
314                         if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res) != LDAP_SUCCESS) {
315                                 ldap_perror(ld, "ldap_search_s");
316                                 ldap_msgfree(res);
317                                 res = NULL;
318                         }
319                         ldap_memfree(dn);
320                         return(res);
321                 }
322                 else if (matches > 0) {
323                         ldtmp = disambiguate(res, matches, read_attrs, who);
324                         ldap_msgfree(res);
325                         return(ldtmp);
326                 }
327                 /* if we're here, there were zero matches */
328                 ldap_msgfree(res);
329         }
330         return(NULL);
331 }
332
333 int
334 pick_one( int i )
335 {
336         int n;
337         char user_pick[SMALL_BUF_SIZE];
338
339 #ifdef DEBUG
340         if (debug & D_TRACE)
341                 printf("->pick_one(%d)\n", i);
342 #endif
343         
344         /* make the user pick an entry */
345         for (;;) {
346                 printf("  Enter the number of the name you want or Q to quit: ");
347                 fflush(stdout);
348                 fetch_buffer(user_pick, sizeof(user_pick), stdin);
349                 if (user_pick[0] == 'q' || user_pick[0] == 'Q')
350                         return(-1);
351                 n = atoi(user_pick);
352                 if ((n > 0) && (n <= i))
353                         return(n);
354                 printf("  Invalid response\n");
355         }
356         /* NOTREACHED */
357 }
358
359 void
360 print_list( LDAPMessage *list, char **names, int *matches )
361 {
362         char **rdns, **cpp;
363         char resp[SMALL_BUF_SIZE];
364         register LDAPMessage *ep;
365         register int i = 1;
366         register int rest = 4;          /* 4, not 1 */
367
368 #ifdef DEBUG
369         if (debug & D_TRACE)
370                 printf("->print_list(%x, %x, %x)\n", list, names, matches);
371 #endif
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)) {
374                 
375                 names[i] = ldap_get_dn(ld, ep);
376                 rdns = ldap_explode_dn(names[i], TRUE);
377                 cpp = ldap_get_values(ld, ep, "title");
378                 if (cpp == NULL)
379                         printf(" %3d. %s\n", i, *rdns);
380                 else
381                         printf(" %3d. %s, %s\n", i, *rdns, *cpp);
382                 ldap_value_free(rdns);
383                 ldap_value_free(cpp);
384                 i++;
385                 if ((rest++ > (lpp - 1)) && (i < *matches)) {
386 again:
387                         printf("  More? ");
388                         fflush(stdout);
389                         fetch_buffer(resp, sizeof(resp), stdin);
390                         if ((resp[0] == 'n') || (resp[0] == 'N'))
391                                 break;
392                         else if ((num_picked = atoi(resp)) != 0) {
393                                 if (num_picked < i)
394                                         break;
395                                 else
396                                         goto again;
397                         }
398                         rest = 1;
399                 }
400         }
401         *matches = i - 1;
402         names[i] = NULL;
403         return;
404 }
405
406 int
407 find_all_subscribers( char **sub, char *group )
408 {
409         int count;
410         LDAPMessage *result;
411         static char *attributes[] = { "cn", NULL };
412         char filter[MED_BUF_SIZE];
413         register LDAPMessage *ep;
414         register int i = 0;
415
416 #ifdef DEBUG
417         if (debug & D_TRACE)
418                 printf("->find_all_subscribers(%x, %s)\n", sub, group);
419 #endif
420
421         sprintf(filter, "%s=%s", "memberOfGroup", group);
422         if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
423                 int ld_errno = 0;
424                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
425                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
426                         return(0);
427                 ldap_perror(ld, "ldap_search_s");
428                 return(0);
429         }
430         count = ldap_count_entries(ld, result);
431         if (count < 1) {
432                 ldap_msgfree(result);
433                 return(0);
434         }
435         if ( count > MAX_VALUES ) {
436                 printf( "  Only retrieving the first %d subscribers....\n",
437                         MAX_VALUES );
438         }
439
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);
442 #ifdef DEBUG
443                 if (debug & D_PARSE)
444                         printf("sub[%d] = %s\n", i - 1, sub[i - 1]);
445 #endif
446         }
447         sub[i] = NULL;
448         ldap_msgfree(result);
449         return(count);
450 }
451
452 char *
453 fetch_boolean_value( char *who, struct attribute attr )
454 {
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 };
459
460 #ifdef DEBUG
461         if (debug & D_TRACE)
462                 printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
463 #endif
464         attributes[0] = attr.quipu_name;
465         if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, NULL, attributes, FALSE, &result) != LDAP_SUCCESS) {
466                 int ld_errno = 0;
467                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
468                 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
469                         return("FALSE");
470                 ldap_perror(ld, "ldap_search_s");
471                 ldap_msgfree(result);
472                 return(NULL);
473         }
474
475         /*
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.
478          *
479          *  If ldap_first_attribute() returns NULL, then this entry did
480          *  not have this particular attribute.
481          */
482         ep = ldap_first_entry(ld, result);
483         if ((vp = (char **) ldap_get_values(ld, ep, attr.quipu_name)) == NULL) {
484                 ldap_msgfree(result);
485                 return("FALSE");
486         }
487         else {
488                 ldap_msgfree(result);
489                 if (!strcasecmp(*vp, "TRUE")) {
490                         ldap_value_free(vp);
491                         return("TRUE");
492                 }
493                 else if (!strcasecmp(*vp, "FALSE")) {
494                         ldap_value_free(vp);
495                         return("FALSE");
496                 }
497                 else {
498                         fprintf(stderr, "  Got garbage -> [%s]\n", *vp);
499                         ldap_value_free(vp);
500                         return(NULL);
501                 }
502         }
503 }