]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-passwd/search.c
Happy New Year
[openldap] / servers / slapd / back-passwd / search.c
index 1da4023edfdfe51fc77fc94954642da529544f06..c7239695dc0f2a640a7be3ba0f0abdd26bb1735d 100644 (file)
@@ -1,5 +1,36 @@
 /* search.c - /etc/passwd backend search function */
 /* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2013 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1995 Regents of the University of Michigan.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of Michigan at Ann Arbor. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).  Additional significant contributors
+ * include:
+ *     Hallvard B. Furuseth
+ *     Howard Chu
+ *     Kurt D. Zeilenga
+ */
 
 #include "portable.h"
 
 #include <pwd.h>
 
 #include "slap.h"
-#include "external.h"
-#include <ldap_pvt.h>
+#include "back-passwd.h"
 
-static Entry *pw2entry(
-       Backend *be,
-       struct passwd *pw,
-       char *rdn);
+static void pw_start( Backend *be );
+
+static int pw2entry(
+       Backend         *be,
+       struct passwd   *pw,
+       Entry           *ep );
 
 int
 passwd_back_search(
-    Backend    *be,
-    Connection *conn,
     Operation  *op,
-    char       *base,
-    char       *nbase,
-    int                scope,
-    int                deref,
-    int                slimit,
-    int                tlimit,
-    Filter     *filter,
-    char       *filterstr,
-    char       **attrs,
-    int                attrsonly
-)
+    SlapReply  *rs )
 {
        struct passwd   *pw;
-       Entry           *e;
-       char            *s;
-       time_t          stoptime;
-
-       int sent = 0;
-       int err = LDAP_SUCCESS;
+       time_t          stoptime = (time_t)-1;
 
-       char *rdn = NULL;
-       char *parent = NULL;
-       char *matched = NULL;
-       char *user = NULL;
+       LDAPRDN rdn = NULL;
+       struct berval parent = BER_BVNULL;
 
-       tlimit = (tlimit > be->be_timelimit || tlimit < 1) ? be->be_timelimit
-           : tlimit;
-       stoptime = op->o_time + tlimit;
-       slimit = (slimit > be->be_sizelimit || slimit < 1) ? be->be_sizelimit
-           : slimit;
+       AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
 
-       endpwent();
-
-#ifdef HAVE_SETPWFILE
-       if ( be->be_private != NULL ) {
-               (void) setpwfile( (char *) be->be_private );
+       if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
+               stoptime = op->o_time + op->ors_tlimit;
        }
-#endif /* HAVE_SETPWFILE */
 
        /* Handle a query for the base of this backend */
-       if ( be_issuffix( be,  nbase ) ) {
-               struct berval   val, *vals[2];
+       if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
+               struct berval   val;
 
-               vals[0] = &val;
-               vals[1] = NULL;
+               rs->sr_matched = op->o_req_dn.bv_val;
 
-               matched = ch_strdup( base );
+               if( op->ors_scope != LDAP_SCOPE_ONELEVEL ) {
+                       AttributeDescription    *desc = NULL;
+                       char                    *next;
+                       Entry                   e = { 0 };
 
-               if( scope != LDAP_SCOPE_ONELEVEL ) {
                        /* Create an entry corresponding to the base DN */
-                       e = (Entry *) ch_calloc(1, sizeof(Entry));
-                       e->e_attrs = NULL;
-                       e->e_dn = ch_strdup( base );
+                       e.e_name.bv_val = ch_strdup( op->o_req_dn.bv_val );
+                       e.e_name.bv_len = op->o_req_dn.bv_len;
+                       e.e_nname.bv_val =  ch_strdup( op->o_req_ndn.bv_val );
+                       e.e_nname.bv_len = op->o_req_ndn.bv_len;
 
                        /* Use the first attribute of the DN
                        * as an attribute within the entry itself.
                        */
-                       rdn = dn_rdn(NULL, base);
+                       if( ldap_bv2rdn( &op->o_req_dn, &rdn, &next, 
+                               LDAP_DN_FORMAT_LDAP ) )
+                       {
+                               rs->sr_err = LDAP_INVALID_DN_SYNTAX;
+                               goto done;
+                       }
 
-                       if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
-                               err = LDAP_INVALID_DN_SYNTAX;
+                       if( slap_bv2ad( &rdn[0]->la_attr, &desc, &rs->sr_text )) {
+                               rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                               ldap_rdnfree(rdn);
                                goto done;
                        }
 
-                       val.bv_val = rdn_attr_value(rdn);
-                       val.bv_len = strlen( val.bv_val );
-                       attr_merge( e, rdn_attr_type(rdn), vals );
+                       attr_merge_normalize_one( &e, desc, &rdn[0]->la_value, NULL );
 
-                       free(rdn);
+                       ldap_rdnfree(rdn);
                        rdn = NULL;
 
                        /* Every entry needs an objectclass. We don't really
@@ -104,151 +115,211 @@ passwd_back_search(
                         *
                         * should be a configuratable item
                         */
-                       val.bv_val = "organizationalUnit";
-                       val.bv_len = strlen( val.bv_val );
-                       attr_merge( e, "objectClass", vals );
+                       BER_BVSTR( &val, "organizationalUnit" );
+                       attr_merge_one( &e, ad_objectClass, &val, NULL );
        
-                       if ( test_filter( be, conn, op, e, filter ) == 0 ) {
-                               send_search_entry( be, conn, op,
-                                       e, attrs, attrsonly, NULL );
-                               sent++;
+                       if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
+                               rs->sr_entry = &e;
+                               rs->sr_attrs = op->ors_attrs;
+                               rs->sr_flags = REP_ENTRY_MODIFIABLE;
+                               send_search_entry( op, rs );
+                               rs->sr_flags = 0;
+                               rs->sr_attrs = NULL;
                        }
+
+                       entry_clean( &e );
                }
 
-               if ( scope != LDAP_SCOPE_BASE ) {
+               if ( op->ors_scope != LDAP_SCOPE_BASE ) {
                        /* check all our "children" */
 
+                       ldap_pvt_thread_mutex_lock( &passwd_mutex );
+                       pw_start( op->o_bd );
                        for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
+                               Entry           e = { 0 };
+
                                /* check for abandon */
-                               ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
                                if ( op->o_abandon ) {
-                                       ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
                                        endpwent();
-                                       return( -1 );
+                                       ldap_pvt_thread_mutex_unlock( &passwd_mutex );
+                                       return( SLAPD_ABANDON );
                                }
-                               ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
 
                                /* check time limit */
-                               if ( slap_get_time() > stoptime ) {
-                                       send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
-                                       NULL, NULL, NULL, NULL );
+                               if ( op->ors_tlimit != SLAP_NO_LIMIT
+                                               && slap_get_time() > stoptime )
+                               {
+                                       send_ldap_error( op, rs, LDAP_TIMELIMIT_EXCEEDED, NULL );
                                        endpwent();
+                                       ldap_pvt_thread_mutex_unlock( &passwd_mutex );
                                        return( 0 );
                                }
 
-                               e = pw2entry( be, pw, NULL );
+                               if ( pw2entry( op->o_bd, pw, &e ) ) {
+                                       rs->sr_err = LDAP_OTHER;
+                                       endpwent();
+                                       ldap_pvt_thread_mutex_unlock( &passwd_mutex );
+                                       goto done;
+                               }
 
-                               if ( test_filter( be, conn, op, e, filter ) == 0 ) {
+                               if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
                                        /* check size limit */
-                                       if ( --slimit == -1 ) {
-                                               send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
-                                               NULL, NULL, NULL, NULL );
+                                       if ( --op->ors_slimit == -1 ) {
+                                               send_ldap_error( op, rs, LDAP_SIZELIMIT_EXCEEDED, NULL );
                                                endpwent();
+                                               ldap_pvt_thread_mutex_unlock( &passwd_mutex );
                                                return( 0 );
                                        }
 
-                                       send_search_entry( be, conn, op,
-                                               e, attrs, attrsonly, NULL );
-                                       sent++;
+                                       rs->sr_entry = &e;
+                                       rs->sr_attrs = op->ors_attrs;
+                                       rs->sr_flags = REP_ENTRY_MODIFIABLE;
+                                       send_search_entry( op, rs );
+                                       rs->sr_flags = 0;
+                                       rs->sr_entry = NULL;
                                }
 
-                               entry_free( e );
+                               entry_clean( &e );
                        }
                        endpwent();
+                       ldap_pvt_thread_mutex_unlock( &passwd_mutex );
                }
 
        } else {
-               parent = dn_parent( be, base );
+               char    *next;
+               Entry   e = { 0 };
+               int     rc;
+
+               if (! be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
+                       dnParent( &op->o_req_ndn, &parent );
+               }
 
                /* This backend is only one layer deep. Don't answer requests for
                 * anything deeper than that.
                 */
-               if( !be_issuffix( be, parent ) ) {
+               if( !be_issuffix( op->o_bd, &parent ) ) {
                        int i;
-                       for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
-                               if( dn_issuffix( nbase, be->be_nsuffix[i] ) ) {
-                                       matched = ch_strdup( be->be_suffix[i] );
+                       for( i=0; op->o_bd->be_nsuffix[i].bv_val != NULL; i++ ) {
+                               if( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_nsuffix[i] ) ) {
+                                       rs->sr_matched = op->o_bd->be_suffix[i].bv_val;
                                        break;
                                }
                        }
-                       err = LDAP_NO_SUCH_OBJECT;
+                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
                        goto done;
                }
 
-               if( scope == LDAP_SCOPE_ONELEVEL ) {
+               if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
                        goto done;
                }
 
-               rdn = dn_rdn( NULL, base );
-
-               if ( (user = rdn_attr_value(rdn)) == NULL) {
-                       err = LDAP_OPERATIONS_ERROR;
+               if ( ldap_bv2rdn( &op->o_req_dn, &rdn, &next,
+                       LDAP_DN_FORMAT_LDAP ))
+               { 
+                       rs->sr_err = LDAP_OTHER;
                        goto done;
                }
 
-               if ( (pw = getpwnam( user )) == NULL ) {
-                       matched = parent;
-                       parent = NULL;
-                       err = LDAP_NO_SUCH_OBJECT;
+               ldap_pvt_thread_mutex_lock( &passwd_mutex );
+               pw_start( op->o_bd );
+               pw = getpwnam( rdn[0]->la_value.bv_val );
+               if ( pw == NULL ) {
+                       rs->sr_matched = parent.bv_val;
+                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
+                       ldap_pvt_thread_mutex_unlock( &passwd_mutex );
                        goto done;
                }
 
-               e = pw2entry( be, pw, rdn );
+               rc = pw2entry( op->o_bd, pw, &e );
+               ldap_pvt_thread_mutex_unlock( &passwd_mutex );
+               if ( rc ) {
+                       rs->sr_err = LDAP_OTHER;
+                       goto done;
+               }
 
-               if ( test_filter( be, conn, op, e, filter ) == 0 ) {
-                       send_search_entry( be, conn, op,
-                               e, attrs, attrsonly, NULL );
-                       sent++;
+               if ( test_filter( op, &e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
+                       rs->sr_entry = &e;
+                       rs->sr_attrs = op->ors_attrs;
+                       rs->sr_flags = REP_ENTRY_MODIFIABLE;
+                       send_search_entry( op, rs );
+                       rs->sr_flags = 0;
+                       rs->sr_entry = NULL;
+                       rs->sr_attrs = NULL;
                }
 
-               entry_free( e );
+               entry_clean( &e );
        }
 
 done:
-       send_ldap_result( conn, op,
-               err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
-               NULL, NULL );
+       if( rs->sr_err != LDAP_NO_SUCH_OBJECT ) rs->sr_matched = NULL;
+       send_ldap_result( op, rs );
 
-       if( matched != NULL ) free( matched );
-       if( parent != NULL ) free( parent );
-       if( rdn != NULL ) free( rdn );
-       if( user != NULL ) free( user );
+       if( rdn != NULL ) ldap_rdnfree( rdn );
 
        return( 0 );
 }
 
-static Entry *
-pw2entry( Backend *be, struct passwd *pw, char *rdn )
+static void
+pw_start(
+       Backend *be
+)
+{
+       endpwent();
+
+#ifdef HAVE_SETPWFILE
+       if ( be->be_private != NULL ) {
+               (void) setpwfile( (char *) be->be_private );
+       }
+#endif /* HAVE_SETPWFILE */
+}
+
+static int
+pw2entry( Backend *be, struct passwd *pw, Entry *e )
 {
-       Entry           *e;
-       char            buf[256];
+       size_t          pwlen;
        struct berval   val;
-       struct berval   *vals[2];
+       struct berval   bv;
 
-       vals[0] = &val;
-       vals[1] = NULL;
+       int             rc;
 
        /*
         * from pw we get pw_name and make it cn
         * give it an objectclass of person.
         */
 
-       e = (Entry *) ch_calloc( 1, sizeof(Entry) );
-       e->e_attrs = NULL;
+       pwlen = strlen( pw->pw_name );
+       val.bv_len = STRLENOF("uid=,") + ( pwlen + be->be_suffix[0].bv_len );
+       val.bv_val = ch_malloc( val.bv_len + 1 );
 
        /* rdn attribute type should be a configuratable item */
-       sprintf( buf, "uid=%s,%s", pw->pw_name, be->be_suffix[0] );
-       e->e_dn = ch_strdup( buf );
-       e->e_ndn = ch_strdup( buf );
-       (void) dn_normalize( e->e_ndn );
+       sprintf( val.bv_val, "uid=%s,%s",
+               pw->pw_name, be->be_suffix[0].bv_val );
+
+       rc = dnNormalize( 0, NULL, NULL, &val, &bv, NULL );
+       if( rc != LDAP_SUCCESS ) {
+               free( val.bv_val );
+               return( -1 );
+       }
+
+       e->e_name = val;
+       e->e_nname = bv;
+
+       e->e_attrs = NULL;
+
+       /* objectclasses should be configurable items */
+       BER_BVSTR( &val, "person" );
+       attr_merge_one( e, slap_schema.si_ad_objectClass, &val, NULL );
+
+       BER_BVSTR( &val, "uidObject" );
+       attr_merge_one( e, slap_schema.si_ad_objectClass, &val, NULL );
 
        val.bv_val = pw->pw_name;
-       val.bv_len = strlen( pw->pw_name );
-       attr_merge( e, "uid", vals );   /* required by uidObject */
-       attr_merge( e, "cn", vals );    /* required by person */
-       attr_merge( e, "sn", vals );    /* required by person */
+       val.bv_len = pwlen;
+       attr_merge_normalize_one( e, slap_schema.si_ad_uid, &val, NULL );       /* required by uidObject */
+       attr_merge_normalize_one( e, slap_schema.si_ad_cn, &val, NULL );        /* required by person */
+       attr_merge_normalize_one( e, ad_sn, &val, NULL );       /* required by person */
 
-#ifdef HAVE_PW_GECOS
+#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
        /*
         * if gecos is present, add it as a cn. first process it
         * according to standard BSD usage. If the processed cn has
@@ -257,41 +328,38 @@ pw2entry( Backend *be, struct passwd *pw, char *rdn )
        if (pw->pw_gecos[0]) {
                char *s;
 
-               val.bv_val = pw->pw_gecos;
-               val.bv_len = strlen(val.bv_val);
-               attr_merge(e, "description", vals);
-
-               s = strchr(val.bv_val, ',');
-               if (s)
-                       *s = '\0';
-               s = strchr(val.bv_val, '&');
-               if (s) {
-                       int i = s - val.bv_val;
-                       strncpy(buf, val.bv_val, i);
-                       s = buf+i;
-                       strcpy(s, pw->pw_name);
-                       *s = TOUPPER(*s);
-                       strcat(s, val.bv_val+i+1);
-                       val.bv_val = buf;
+               ber_str2bv( pw->pw_gecos, 0, 0, &val );
+               attr_merge_normalize_one( e, ad_desc, &val, NULL );
+
+               s = ber_bvchr( &val, ',' );
+               if ( s ) *s = '\0';
+
+               s = ber_bvchr( &val, '&' );
+               if ( s ) {
+                       char buf[1024];
+
+                       if( val.bv_len + pwlen < sizeof(buf) ) {
+                               int i = s - val.bv_val;
+                               strncpy( buf, val.bv_val, i );
+                               s = buf + i;
+                               strcpy( s, pw->pw_name );
+                               *s = TOUPPER((unsigned char)*s);
+                               strcat( s, val.bv_val + i + 1 );
+                               val.bv_val = buf;
+                       }
                }
-               val.bv_len = strlen(val.bv_val);
-               if ( strcmp( val.bv_val, pw->pw_name ))
-                       attr_merge( e, "cn", vals );
-               if ( (s=strrchr(val.bv_val, ' '))) {
-                       val.bv_val = s + 1;
-                       val.bv_len = strlen(val.bv_val);
-                       attr_merge(e, "sn", vals);
+               val.bv_len = strlen( val.bv_val );
+
+               if ( val.bv_len && strcasecmp( val.bv_val, pw->pw_name ) ) {
+                       attr_merge_normalize_one( e, slap_schema.si_ad_cn, &val, NULL );
                }
-       }
-#endif
 
-       /* objectclasses should be configuratable items */
-       val.bv_val = "person";
-       val.bv_len = strlen( val.bv_val );
-       attr_merge( e, "objectclass", vals );
+               if ( ( s = strrchr(val.bv_val, ' ' ) ) ) {
+                       ber_str2bv( s + 1, 0, 0, &val );
+                       attr_merge_normalize_one( e, ad_sn, &val, NULL );
+               }
+       }
+#endif /* HAVE_STRUCT_PASSWD_PW_GECOS */
 
-       val.bv_val = "uidObject";
-       val.bv_len = strlen( val.bv_val );
-       attr_merge( e, "objectclass", vals );
-       return( e );
+       return( 0 );
 }