openldap/servers/slapd/back-passwd/search.c

356 lines
7.8 KiB
C
Raw Normal View History

1998-08-09 08:43:13 +08:00
/* search.c - /etc/passwd backend search function */
/* $OpenLDAP$ */
1998-08-09 08:43:13 +08:00
1998-10-25 09:41:42 +08:00
#include "portable.h"
1998-08-09 08:43:13 +08:00
#include <stdio.h>
1998-10-25 09:41:42 +08:00
#include <ac/ctype.h>
1998-10-25 09:41:42 +08:00
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
1998-08-09 08:43:13 +08:00
#include <pwd.h>
1998-10-25 09:41:42 +08:00
1998-08-09 08:43:13 +08:00
#include "slap.h"
#include "external.h"
#include <ldap_pvt.h>
1998-08-09 08:43:13 +08:00
static Entry *pw2entry(
Backend *be,
struct passwd *pw,
char *rdn);
1998-08-09 08:43:13 +08:00
int
passwd_back_search(
Backend *be,
Connection *conn,
Operation *op,
2001-12-27 06:59:28 +08:00
struct berval *base,
struct berval *nbase,
1998-08-09 08:43:13 +08:00
int scope,
int deref,
int slimit,
int tlimit,
Filter *filter,
const char *filterstr,
struct berval **attrs,
1998-08-09 08:43:13 +08:00
int attrsonly
)
{
struct passwd *pw;
Entry *e;
char *s;
time_t stoptime;
int sent = 0;
int err = LDAP_SUCCESS;
char *rdn = NULL;
char *parent = NULL;
char *matched = NULL;
char *user = NULL;
1998-08-09 08:43:13 +08:00
AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
1998-08-09 08:43:13 +08:00
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;
endpwent();
1998-08-09 08:43:13 +08:00
#ifdef HAVE_SETPWFILE
if ( be->be_private != NULL ) {
(void) setpwfile( (char *) be->be_private );
}
#endif /* HAVE_SETPWFILE */
/* Handle a query for the base of this backend */
2001-12-27 06:59:28 +08:00
if ( be_issuffix( be, nbase->bv_val ) ) {
struct berval val, *vals[2];
vals[0] = &val;
vals[1] = NULL;
1998-08-09 08:43:13 +08:00
2001-12-25 11:47:35 +08:00
matched = (char *) base;
if( scope != LDAP_SCOPE_ONELEVEL ) {
char *type;
AttributeDescription *desc = NULL;
/* Create an entry corresponding to the base DN */
e = (Entry *) ch_calloc(1, sizeof(Entry));
2001-12-27 06:59:28 +08:00
e->e_name.bv_val = ch_strdup( base->bv_val );
e->e_name.bv_len = base->bv_len;
e->e_nname.bv_val = ch_strdup( nbase->bv_val );
e->e_nname.bv_len = nbase->bv_len;
e->e_attrs = NULL;
2001-12-27 06:59:28 +08:00
e->e_private = NULL;
/* Use the first attribute of the DN
* as an attribute within the entry itself.
*/
rdn = dn_rdn(NULL, base);
if( rdn == NULL || (s = strchr(rdn, '=')) == NULL ) {
err = LDAP_INVALID_DN_SYNTAX;
free(rdn);
goto done;
}
val.bv_val = rdn_attr_value(rdn);
val.bv_len = strlen( val.bv_val );
type = rdn_attr_type(rdn);
{
int rc;
const char *text;
rc = slap_str2ad( type, &desc, &text );
if( rc != LDAP_SUCCESS ) {
err = LDAP_NO_SUCH_OBJECT;
free(rdn);
goto done;
}
}
attr_merge( e, desc, vals );
free(rdn);
rdn = NULL;
/* Every entry needs an objectclass. We don't really
* know if our hardcoded choice here agrees with the
* DN that was configured for this backend, but it's
* better than nothing.
*
* should be a configuratable item
*/
val.bv_val = "organizationalUnit";
val.bv_len = sizeof("organizationalUnit")-1;
attr_merge( e, ad_objectClass, vals );
if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
send_search_entry( be, conn, op,
e, attrs, attrsonly, NULL );
sent++;
}
1998-08-09 08:43:13 +08:00
}
if ( scope != LDAP_SCOPE_BASE ) {
/* check all our "children" */
1998-08-09 08:43:13 +08:00
for ( pw = getpwent(); pw != NULL; pw = getpwent() ) {
/* 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( &op->o_abandonmutex );
/* check time limit */
if ( slap_get_time() > stoptime ) {
send_ldap_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
NULL, NULL, NULL, NULL );
endpwent();
return( 0 );
}
e = pw2entry( be, pw, NULL );
1998-08-09 08:43:13 +08:00
if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
/* check size limit */
if ( --slimit == -1 ) {
send_ldap_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
NULL, NULL, NULL, NULL );
endpwent();
return( 0 );
}
send_search_entry( be, conn, op,
e, attrs, attrsonly, NULL );
sent++;
}
entry_free( e );
}
1998-08-09 08:43:13 +08:00
endpwent();
}
} else {
2001-12-27 06:59:28 +08:00
parent = dn_parent( be, nbase->bv_val );
/* This backend is only one layer deep. Don't answer requests for
* anything deeper than that.
*/
if( !be_issuffix( be, parent ) ) {
int i;
for( i=0; be->be_nsuffix[i] != NULL; i++ ) {
2001-12-27 06:59:28 +08:00
if( dnIsSuffix( nbase, be->be_nsuffix[i] ) ) {
2001-12-26 03:48:26 +08:00
matched = be->be_suffix[i]->bv_val;
break;
}
}
err = LDAP_NO_SUCH_OBJECT;
goto done;
}
if( scope == LDAP_SCOPE_ONELEVEL ) {
goto done;
1998-08-09 08:43:13 +08:00
}
rdn = dn_rdn( NULL, base );
1998-08-09 08:43:13 +08:00
if ( (user = rdn_attr_value(rdn)) == NULL) {
err = LDAP_OPERATIONS_ERROR;
goto done;
}
if ( (pw = getpwnam( user )) == NULL ) {
matched = parent;
err = LDAP_NO_SUCH_OBJECT;
goto done;
}
e = pw2entry( be, pw, rdn );
if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
send_search_entry( be, conn, op,
e, attrs, attrsonly, NULL );
sent++;
1998-08-09 08:43:13 +08:00
}
entry_free( e );
}
done:
send_ldap_result( conn, op,
err, err == LDAP_NO_SUCH_OBJECT ? matched : NULL, NULL,
NULL, NULL );
if( rdn != NULL ) free( rdn );
if( user != NULL ) free( user );
1998-08-09 08:43:13 +08:00
return( 0 );
}
static Entry *
pw2entry( Backend *be, struct passwd *pw, char *rdn )
1998-08-09 08:43:13 +08:00
{
2001-12-28 15:09:12 +08:00
size_t pwlen;
1998-08-09 08:43:13 +08:00
Entry *e;
struct berval val;
struct berval *vals[2];
struct berval bv;
1998-08-09 08:43:13 +08:00
int rc;
const char *text;
2001-12-28 15:09:12 +08:00
AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
AttributeDescription *ad_cn = NULL;
AttributeDescription *ad_sn = NULL;
AttributeDescription *ad_uid = NULL;
AttributeDescription *ad_description = NULL;
rc = slap_str2ad( "cn", &ad_cn, &text );
if(rc != LDAP_SUCCESS) return NULL;
rc = slap_str2ad( "sn", &ad_sn, &text );
if(rc != LDAP_SUCCESS) return NULL;
rc = slap_str2ad( "uid", &ad_uid, &text );
if(rc != LDAP_SUCCESS) return NULL;
rc = slap_str2ad( "description", &ad_description, &text );
if(rc != LDAP_SUCCESS) return NULL;
1998-08-09 08:43:13 +08:00
/*
* from pw we get pw_name and make it cn
* give it an objectclass of person.
1998-08-09 08:43:13 +08:00
*/
2001-12-28 15:09:12 +08:00
pwlen = strlen( pw->pw_name );
val.bv_len = (sizeof("uid=,")-1) + ( 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( val.bv_val, "uid=%s,%s",
pw->pw_name, be->be_suffix[0]->bv_val );
rc = dnNormalize2( NULL, &val, &bv );
2001-12-28 15:09:12 +08:00
if( rc != LDAP_SUCCESS ) {
free( val.bv_val );
return NULL;
}
1998-08-09 08:43:13 +08:00
e = (Entry *) ch_calloc( 1, sizeof(Entry) );
2001-12-28 15:09:12 +08:00
e->e_name = val;
e->e_nname = bv;
2001-12-28 15:09:12 +08:00
1998-08-09 08:43:13 +08:00
e->e_attrs = NULL;
2001-12-28 15:09:12 +08:00
vals[0] = &val;
vals[1] = NULL;
/* objectclasses should be configurable items */
val.bv_val = "top";
val.bv_len = sizeof("top")-1;
attr_merge( e, ad_objectClass, vals );
val.bv_val = "person";
val.bv_len = sizeof("person")-1;
attr_merge( e, ad_objectClass, vals );
val.bv_val = "uidObject";
val.bv_len = sizeof("uidObject")-1;
attr_merge( e, ad_objectClass, vals );
1998-08-09 08:43:13 +08:00
val.bv_val = pw->pw_name;
2001-12-28 15:09:12 +08:00
val.bv_len = pwlen;
attr_merge( e, ad_uid, vals ); /* required by uidObject */
attr_merge( e, ad_cn, vals ); /* required by person */
attr_merge( e, ad_sn, vals ); /* required by person */
1999-03-18 06:27:46 +08:00
#ifdef HAVE_PW_GECOS
/*
* if gecos is present, add it as a cn. first process it
* according to standard BSD usage. If the processed cn has
* a space, use the tail as the surname.
*/
if (pw->pw_gecos[0]) {
char *s;
val.bv_val = pw->pw_gecos;
val.bv_len = strlen(val.bv_val);
attr_merge(e, ad_description, vals);
s = strchr(val.bv_val, ',');
if (s) *s = '\0';
s = strchr(val.bv_val, '&');
if (s) {
char buf[256];
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;
}
val.bv_len = strlen(val.bv_val);
if ( strcmp( val.bv_val, pw->pw_name ))
attr_merge( e, ad_cn, vals );
if ( (s=strrchr(val.bv_val, ' '))) {
val.bv_val = s + 1;
val.bv_len = strlen(val.bv_val);
attr_merge(e, ad_sn, vals);
}
}
1999-03-18 06:27:46 +08:00
#endif
1998-08-09 08:43:13 +08:00
return( e );
}