1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
35 struct berval id = {0, NULL}, old = {0, NULL}, new = {0, NULL},
36 dn, ndn, hash, vals[2], tmpbv, *rsp = NULL;
37 Modifications ml, **modtail;
39 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
40 slap_callback cb2 = { &cb, slap_replog_cb, NULL, NULL };
42 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
44 if( op->o_dn.bv_len == 0 ) {
45 rs->sr_text = "only authenticated users may change passwords";
46 return LDAP_STRONG_AUTH_REQUIRED;
49 if( op->oq_extended.rs_reqdata ) {
50 ber_dupbv_x( &tmpbv, op->oq_extended.rs_reqdata, op->o_tmpmemctx );
52 rs->sr_err = slap_passwd_parse(
53 op->oq_extended.rs_reqdata ? &tmpbv : NULL,
54 &id, &old, &new, &rs->sr_text );
56 if ( rs->sr_err != LDAP_SUCCESS ) {
62 /* ndn is in tmpmem, so we don't need to free it */
63 rs->sr_err = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
64 if ( rs->sr_err != LDAP_SUCCESS ) {
65 rs->sr_text = "Invalid DN";
68 op->o_bd = select_backend( &ndn, 0, 0 );
72 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
73 op->o_bd = op->o_conn->c_authz_backend;
74 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
77 if( op->o_bd == NULL ) {
78 #ifdef HAVE_CYRUS_SASL
79 return slap_sasl_setpass( op, rs );
81 rs->sr_text = "no authz backend";
86 if ( ndn.bv_len == 0 ) {
87 rs->sr_text = "no password is associated with the Root DSE";
88 return LDAP_UNWILLING_TO_PERFORM;
91 if (backend_check_restrictions( op, rs,
92 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
97 #ifndef SLAPD_MULTIMASTER
98 /* This does not apply to multi-master case */
99 if( op->o_bd->be_update_ndn.bv_len ) {
100 /* we SHOULD return a referral in this case */
101 BerVarray defref = NULL;
102 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
104 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
106 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
107 ber_bvarray_add( &defref, &tmpbv );
110 defref = referral_rewrite( op->o_bd->be_update_refs,
111 NULL, NULL, LDAP_SCOPE_DEFAULT );
114 return LDAP_REFERRAL;
116 #endif /* !SLAPD_MULTIMASTER */
118 /* Give the backend a chance to handle this itself */
119 if ( op->o_bd->be_extended ) {
120 rs->sr_err = op->o_bd->be_extended( op, rs );
121 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM ) {
126 /* The backend didn't handle it, so try it here */
127 if( op->o_bd && !op->o_bd->be_modify ) {
128 rs->sr_text = "operation not supported for current user";
129 return LDAP_UNWILLING_TO_PERFORM;
132 if ( new.bv_len == 0 ) {
133 slap_passwd_generate( &new );
134 rsp = slap_passwd_return( &new );
136 if ( new.bv_len == 0 ) {
137 rs->sr_text = "password generation failed";
140 slap_passwd_hash( &new, &hash, &rs->sr_text );
144 if ( hash.bv_len == 0 ) {
145 if ( !rs->sr_text ) {
146 rs->sr_text = "password hash failed";
151 vals[1].bv_val = NULL;
152 ml.sml_desc = slap_schema.si_ad_userPassword;
153 ml.sml_values = vals;
154 ml.sml_nvalues = NULL;
155 ml.sml_op = LDAP_MOD_REPLACE;
159 op2.o_tag = LDAP_REQ_MODIFY;
160 op2.o_callback = &cb2;
163 op2.orm_modlist = &ml;
165 modtail = &ml.sml_next;
166 rs->sr_err = slap_mods_opattrs( &op2, &ml, modtail, &rs->sr_text,
169 if ( rs->sr_err == LDAP_SUCCESS ) {
170 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
172 if ( rs->sr_err == LDAP_SUCCESS ) {
173 rs->sr_rspdata = rsp;
177 slap_mods_free( ml.sml_next );
183 int slap_passwd_parse( struct berval *reqdata,
185 struct berval *oldpass,
186 struct berval *newpass,
189 int rc = LDAP_SUCCESS;
192 BerElementBuffer berbuf;
193 BerElement *ber = (BerElement *)&berbuf;
195 if( reqdata == NULL ) {
199 if( reqdata->bv_len == 0 ) {
200 *text = "empty request data field";
201 return LDAP_PROTOCOL_ERROR;
204 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
205 ber_init2( ber, reqdata, 0 );
207 tag = ber_scanf( ber, "{" /*}*/ );
209 if( tag == LBER_ERROR ) {
211 LDAP_LOG( OPERATION, ERR,
212 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
214 Debug( LDAP_DEBUG_TRACE,
215 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
217 rc = LDAP_PROTOCOL_ERROR;
221 tag = ber_peek_tag( ber, &len );
222 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
225 LDAP_LOG( OPERATION, ERR,
226 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
228 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
232 *text = "user must change own password";
233 rc = LDAP_UNWILLING_TO_PERFORM;
237 tag = ber_scanf( ber, "m", id );
239 if( tag == LBER_ERROR ) {
241 LDAP_LOG( OPERATION, ERR,
242 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
244 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
251 tag = ber_peek_tag( ber, &len);
254 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
255 if( oldpass == NULL ) {
257 LDAP_LOG( OPERATION, ERR,
258 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
260 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
264 *text = "use bind to verify old password";
265 rc = LDAP_UNWILLING_TO_PERFORM;
269 tag = ber_scanf( ber, "m", oldpass );
271 if( tag == LBER_ERROR ) {
273 LDAP_LOG( OPERATION, ERR,
274 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
276 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
283 tag = ber_peek_tag( ber, &len );
286 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
287 if( newpass == NULL ) {
289 LDAP_LOG( OPERATION, ERR,
290 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
292 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
296 *text = "user specified passwords disallowed";
297 rc = LDAP_UNWILLING_TO_PERFORM;
301 tag = ber_scanf( ber, "m", newpass );
303 if( tag == LBER_ERROR ) {
305 LDAP_LOG( OPERATION, ERR,
306 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
308 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
315 tag = ber_peek_tag( ber, &len );
321 LDAP_LOG( OPERATION, ERR,
322 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
324 Debug( LDAP_DEBUG_TRACE,
325 "slap_passwd_parse: decoding error, len=%ld\n",
329 *text = "data decoding error";
330 rc = LDAP_PROTOCOL_ERROR;
337 struct berval * slap_passwd_return(
338 struct berval *cred )
341 struct berval *bv = NULL;
342 BerElementBuffer berbuf;
343 /* opaque structure, size unknown but smaller than berbuf */
344 BerElement *ber = (BerElement *)&berbuf;
346 assert( cred != NULL );
349 LDAP_LOG( OPERATION, ENTRY,
350 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
352 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
353 (long) cred->bv_len, 0, 0 );
356 ber_init_w_nullc( ber, LBER_USE_DER );
358 rc = ber_printf( ber, "{tON}",
359 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
362 (void) ber_flatten( ber, &bv );
380 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
381 ldap_pvt_thread_mutex_lock( &passwd_mutex );
383 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
387 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
388 if( !lutil_passwd( bv, cred, NULL, text ) ) {
394 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
396 lutil_passwd_sasl_conn = NULL;
398 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
405 slap_passwd_generate( struct berval *pass )
409 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
411 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
414 * generate passwords of only 8 characters as some getpass(3)
415 * implementations truncate at 8 characters.
417 tmp = lutil_passwd_generate( 8 );
429 struct berval * cred,
434 #ifdef LUTIL_SHA1_BYTES
435 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
437 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
441 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
442 ldap_pvt_thread_mutex_lock( &passwd_mutex );
445 tmp = lutil_passwd_hash( cred , hash, text );
447 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
448 ldap_pvt_thread_mutex_unlock( &passwd_mutex );