]> git.sur5r.net Git - openldap/blobdiff - clients/ud/find.c
From jon@symas.com - patches for consistent use of directory separators
[openldap] / clients / ud / find.c
index 7d4930b5d61f788847763820e5edf2597d4cf650..d5b85e9c21757f9b239111db9ccb73db00640287 100644 (file)
@@ -1,3 +1,8 @@
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 /*
  * Copyright (c) 1991, 1992, 1993 
  * Regents of the University of Michigan.  All rights reserved.
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <time.h>
-#ifndef __STDC__
-#include <memory.h>
-#endif
 
-#include <lber.h>
+#include <ac/stdlib.h>
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include <ldap.h>
 
 #include "ud.h"
 
-extern char *search_base;      /* search base */
-extern int verbose;            /* verbose mode flag */
-extern LDAP *ld;               /* our ldap descriptor */
-       
 static int num_picked = 0;     /* used when user picks entry at More prompt */
 
-#ifdef DEBUG
-extern int debug;              /* debug flag */
-#endif
 
-vrfy(dn)
-char *dn;
+int
+vrfy( char *dn )
 {
-       LDAPMessage *results;
-       static char *attrs[2] = { "objectClass", NULL };
+       LDAPMessage *results = NULL;
+       static char *attrs[2] = { "1.1", NULL };
+       int ld_errno = 0;
 
 #ifdef DEBUG
        if (debug & D_TRACE)
                printf("->vrfy(%s)\n", dn);
 #endif
        /* verify that this DN exists in the directory */
-       (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", attrs, TRUE, &results);
+       (void) ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, attrs, TRUE, &results);
        (void) ldap_msgfree(results);
-       if ((ld->ld_errno == LDAP_NO_SUCH_OBJECT) || (ld->ld_errno == LDAP_INVALID_DN_SYNTAX))
+
+       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
+       if ((ld_errno == LDAP_NO_SUCH_OBJECT) || (ld_errno == LDAP_INVALID_DN_SYNTAX))
                return(0);
-       else if (ld->ld_errno == LDAP_SUCCESS)
+       else if (ld_errno == LDAP_SUCCESS)
                return(1);
        else {
                ldap_perror(ld, "ldap_search");
@@ -57,30 +60,30 @@ char *dn;
 }
        
 
-static LDAPMessage * disambiguate( result, matches, read_attrs, who )
-LDAPMessage *result;
-int matches;
-char **read_attrs;
-char *who;
+static LDAPMessage *
+disambiguate( LDAPMessage *result, int matches, char **read_attrs, char *who )
 {
        int choice;                     /* entry that user chooses */
        int i;
        char *namelist[MAX_NUM_NAMES];  /* names found */
        char response[SMALL_BUF_SIZE];  /* results from user */
        char *name = NULL;              /* DN to lookup */
-       LDAPMessage *mp;
-       extern void Free();
+       LDAPMessage *mp = NULL;
+       int ld_errno = 0;
 
 #ifdef DEBUG
        if (debug & D_TRACE)
                printf("->disambiguate(%x, %d, %x, %s)\n", result, matches, 
                                                        read_attrs, who);
 #endif
+
+       ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
+
        /*
         *  If we are here, it means that we got back multiple answers.
         */
-       if ((ld->ld_errno == LDAP_TIMELIMIT_EXCEEDED)
-           || (ld->ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
+       if ((ld_errno == LDAP_TIMELIMIT_EXCEEDED)
+           || (ld_errno == LDAP_SIZELIMIT_EXCEEDED)) {
                if (verbose) {
                        printf("  Your query was too general and a limit was exceeded.  The results listed\n");
                        printf("  are not complete.  You may want to try again with a more refined query.\n\n");
@@ -92,7 +95,7 @@ char *who;
        for (;;) {
                printf("  Do you wish to see a list of names? ");
                fflush(stdout);
-               (void) memset(response, 0, sizeof(response));
+               (void) memset(response, '\0', sizeof(response));
                fetch_buffer(response, sizeof(response), stdin);
                switch (response[0]) {
                case 'n' :
@@ -129,7 +132,7 @@ char *who;
                                printf("     ld = 0x%x\n", ld);
                                printf("     search base = %s\n", name);
                                printf("     scope = LDAP_SCOPE_BASE\n");
-                               printf("     filter = objectClass=*\n");
+                               printf("     filter = (objectClass=*)\n");
                                for (i = 0; read_attrs[i] != NULL; i++)
                                        printf("     read_attrs[%d] = %s\n", i, read_attrs[i]);
                                printf("     read_attrs[%d] = NULL\n", i);
@@ -137,7 +140,7 @@ char *who;
                                printf("     &mp = 0x%x\n", &mp);
                        }
 #endif
-                       if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
+                       if (ldap_search_s(ld, name, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &mp) != LDAP_SUCCESS) {
                                ldap_perror(ld, "ldap_search_s");
                                Free(name);
                                ldap_msgfree(mp);
@@ -153,9 +156,8 @@ char *who;
        }
 }
 
-LDAPMessage * find(who, quiet)
-char *who;
-int quiet;
+LDAPMessage *
+find( char *who, int quiet )
 {
        register int i, j, k;           /* general ints */
        int matches;                    /* from ldap_count_entries() */
@@ -169,9 +171,6 @@ int quiet;
        char response[SMALL_BUF_SIZE];
        char *cp, *dn, **rdns;
        LDAPFiltInfo *fi;
-       extern LDAPFiltDesc *lfdp;              /* LDAP filter descriptor */
-       extern struct attribute attrlist[];     /* complete list of attrs */
-       extern void Free();
 
 #ifdef DEBUG
        if (debug & D_TRACE)
@@ -200,47 +199,6 @@ int quiet;
                search_attrs[k] = NULL;
        }
 
-       /*
-        *  If the user-supplied name has any commas in it, we
-        *  assume that it is a UFN, and do everything right
-        *  here.  If we don't find it, treat it as NOT a UFN.
-        */
-       if (strchr(who, ',') != NULL) {
-               int     savederef;
-#ifdef DEBUG
-               if (debug & D_FIND)
-                       printf("\"%s\" appears to be a UFN\n", who);
-#endif
-               savederef = ld->ld_deref;
-               ld->ld_deref = LDAP_DEREF_FINDING;
-               if ((rc = ldap_ufn_search_s(ld, who, search_attrs, FALSE, &res)) !=
-                   LDAP_SUCCESS && rc != LDAP_SIZELIMIT_EXCEEDED &&
-                   rc != LDAP_TIMELIMIT_EXCEEDED) {
-                       ldap_perror(ld, "ldap_ufn_search_s");
-                       ld->ld_deref = savederef;
-                       return(NULL);
-               }
-               if ((matches = ldap_count_entries(ld, res)) < 0) {
-                       ldap_perror(ld, "ldap_count_entries");
-                       ld->ld_deref = savederef;
-                       return(NULL);
-               } else if (matches == 1) {
-                       if (ldap_search_s(ld, ldap_get_dn(ld, ldap_first_entry(ld, res)), LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
-                               if (ld->ld_errno == LDAP_UNAVAILABLE)
-                                       printf("  Could not contact the LDAP server to find \"%s\".\n", who);
-                               else
-                                       ldap_perror(ld, "ldap_search_s");
-                               return(NULL);
-                       }
-                       ld->ld_deref = savederef;
-                       return(res);
-               } else if (matches > 1 ) {
-                       return( disambiguate( ld, res, matches, read_attrs,
-                           who ) );
-               }
-               ld->ld_deref = savederef;
-       }
-
        /*
         *  Old users of the MTS *USERDIRECTORY will likely wrap the name
         *  in quotes.  Not only is this unnecessary, but it also won't work.
@@ -255,9 +213,6 @@ int quiet;
                        break;
        }
 
-       /*
-        *  It wasn't a UFN, so look it up in the usual method.
-        */
        for (fi = ldap_getfirstfilter(lfdp, "ud", who); fi != NULL;
             fi = ldap_getnextfilter(lfdp)) {
 #ifdef DEBUG
@@ -290,7 +245,10 @@ int quiet;
                                fflush(stdout);
                                fetch_buffer(response, sizeof(response), stdin);
                                if ((response[0] == 'n') || (response[0] == 'N'))
+                               {
+                                       ldap_memfree(dn);
                                        return(NULL);
+                               }
                        }
 #ifdef DEBUG
                        if (debug & D_FIND) {
@@ -298,7 +256,7 @@ int quiet;
                                printf("     ld = 0x%x\n", ld);
                                printf("     dn = %s\n", dn);
                                printf("     scope = LDAP_SCOPE_BASE\n");
-                               printf("     filter = %s\n", "objectClass=*");
+                               printf("     filter = %s\n", "(objectClass=*)");
                                for (i = 0; read_attrs[i] != NULL; i++)
                                        printf("     read_attrs[%d] = %s\n", i, read_attrs[i]);
                                printf("     read_attrs[%d] = NULL\n", i);
@@ -306,12 +264,12 @@ int quiet;
                                printf("     &results = 0x%x\n", &res);
                        }
 #endif
-                       if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, "objectClass=*", read_attrs, FALSE, &res) != LDAP_SUCCESS) {
+                       if (ldap_search_s(ld, dn, LDAP_SCOPE_BASE, NULL, read_attrs, FALSE, &res) != LDAP_SUCCESS) {
                                ldap_perror(ld, "ldap_search_s");
                                ldap_msgfree(res);
-                               return(NULL);
+                               res = NULL;
                        }
-                       Free(dn);
+                       ldap_memfree(dn);
                        return(res);
                }
                else if (matches > 0) {
@@ -325,8 +283,8 @@ int quiet;
        return(NULL);
 }
 
-pick_one(i)
-int i;
+int
+pick_one( int i )
 {
        int n;
        char user_pick[SMALL_BUF_SIZE];
@@ -351,13 +309,10 @@ int i;
        /* NOTREACHED */
 }
 
-print_list(list, names, matches)
-LDAPMessage *list;
-char *names[];
-int *matches;
+void
+print_list( LDAPMessage *list, char **names, int *matches )
 {
        char **rdns, **cpp;
-       extern int lpp;
        char resp[SMALL_BUF_SIZE];
        register LDAPMessage *ep;
        register int i = 1;
@@ -401,9 +356,8 @@ again:
        return;
 }
 
-find_all_subscribers(sub, group)
-char *sub[];
-char *group;
+int
+find_all_subscribers( char **sub, char *group )
 {
        int count;
        LDAPMessage *result;
@@ -419,7 +373,9 @@ char *group;
 
        sprintf(filter, "%s=%s", "memberOfGroup", group);
        if (ldap_search_s(ld, search_base, LDAP_SCOPE_SUBTREE, filter, attributes, FALSE, &result) != LDAP_SUCCESS) {
-               if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
+               int ld_errno = 0;
+               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
+               if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
                        return(0);
                ldap_perror(ld, "ldap_search_s");
                return(0);
@@ -446,9 +402,8 @@ char *group;
        return(count);
 }
 
-char * fetch_boolean_value(who, attr)
-char *who;
-struct attribute attr;
+char *
+fetch_boolean_value( char *who, struct attribute attr )
 {
        LDAPMessage *result;            /* from the search below */
        register LDAPMessage *ep;       /* entry pointer */
@@ -460,8 +415,10 @@ struct attribute attr;
                printf("->fetch_boolean_value(%s, %s)\n", who, attr.quipu_name);
 #endif
        attributes[0] = attr.quipu_name;
-       if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, "objectClass=*", attributes, FALSE, &result) != LDAP_SUCCESS) {
-               if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
+       if (ldap_search_s(ld, who, LDAP_SCOPE_BASE, NULL, attributes, FALSE, &result) != LDAP_SUCCESS) {
+               int ld_errno = 0;
+               ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
+               if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
                        return("FALSE");
                ldap_perror(ld, "ldap_search_s");
                ldap_msgfree(result);