1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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 = { slap_null_cb, NULL };
41 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
43 if( op->o_dn.bv_len == 0 ) {
44 rs->sr_text = "only authenticated users may change passwords";
45 return LDAP_STRONG_AUTH_REQUIRED;
48 ber_dupbv_x( &tmpbv, op->oq_extended.rs_reqdata, op->o_tmpmemctx );
49 rs->sr_err = slap_passwd_parse( &tmpbv, &id, &old, &new, &rs->sr_text );
50 if ( rs->sr_err != LDAP_SUCCESS ) {
56 /* ndn is in tmpmem, so we don't need to free it */
57 rs->sr_err = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
58 if ( rs->sr_err != LDAP_SUCCESS ) {
59 rs->sr_text = "Invalid DN";
62 op->o_bd = select_backend( &ndn, 0, 0 );
66 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
67 op->o_bd = op->o_conn->c_authz_backend;
68 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
71 if( op->o_bd == NULL ) {
72 #ifdef HAVE_CYRUS_SASL
73 return slap_sasl_setpass( op, rs );
75 rs->sr_text = "no authz backend";
80 if ( ndn.bv_len == 0 ) {
81 rs->sr_text = "no password is associated with the Root DSE";
82 return LDAP_UNWILLING_TO_PERFORM;
85 if (backend_check_restrictions( op, rs,
86 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
91 #ifndef SLAPD_MULTIMASTER
92 /* This does not apply to multi-master case */
93 if( op->o_bd->be_update_ndn.bv_len ) {
94 /* we SHOULD return a referral in this case */
95 BerVarray defref = NULL;
96 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
98 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
100 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
101 ber_bvarray_add( &defref, &tmpbv );
104 defref = referral_rewrite( op->o_bd->be_update_refs,
105 NULL, NULL, LDAP_SCOPE_DEFAULT );
108 return LDAP_REFERRAL;
110 #endif /* !SLAPD_MULTIMASTER */
112 /* Give the backend a chance to handle this itself */
113 if ( op->o_bd->be_extended ) {
114 rs->sr_err = op->o_bd->be_extended( op, rs );
115 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM ) {
120 /* The backend didn't handle it, so try it here */
121 if( op->o_bd && !op->o_bd->be_modify ) {
122 rs->sr_text = "operation not supported for current user";
123 return LDAP_UNWILLING_TO_PERFORM;
126 if ( new.bv_len == 0 ) {
127 slap_passwd_generate( &new );
128 rsp = slap_passwd_return( &new );
130 if ( new.bv_len == 0 ) {
131 rs->sr_text = "password generation failed";
134 slap_passwd_hash( &new, &hash, &rs->sr_text );
138 if ( hash.bv_len == 0 ) {
139 if ( !rs->sr_text ) {
140 rs->sr_text = "password hash failed";
145 vals[1].bv_val = NULL;
146 ml.sml_desc = slap_schema.si_ad_userPassword;
147 ml.sml_values = vals;
148 ml.sml_nvalues = NULL;
149 ml.sml_op = LDAP_MOD_REPLACE;
153 op2.o_tag = LDAP_REQ_MODIFY;
154 op2.o_callback = &cb;
157 op2.orm_modlist = &ml;
159 modtail = &ml.sml_next;
160 rs->sr_err = slap_mods_opattrs( &op2, &ml, modtail, &rs->sr_text,
163 if ( rs->sr_err == LDAP_SUCCESS ) {
164 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
166 if ( rs->sr_err == LDAP_SUCCESS ) {
168 rs->sr_rspdata = rsp;
172 slap_mods_free( ml.sml_next );
178 int slap_passwd_parse( struct berval *reqdata,
180 struct berval *oldpass,
181 struct berval *newpass,
184 int rc = LDAP_SUCCESS;
187 BerElementBuffer berbuf;
188 BerElement *ber = (BerElement *)&berbuf;
190 if( reqdata == NULL ) {
194 if( reqdata->bv_len == 0 ) {
195 *text = "empty request data field";
196 return LDAP_PROTOCOL_ERROR;
199 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
200 ber_init2( ber, reqdata, 0 );
202 tag = ber_scanf( ber, "{" /*}*/ );
204 if( tag != LBER_ERROR ) {
205 tag = ber_peek_tag( ber, &len );
208 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
211 LDAP_LOG( OPERATION, ERR,
212 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
214 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
218 *text = "user must change own password";
219 rc = LDAP_UNWILLING_TO_PERFORM;
223 tag = ber_scanf( ber, "m", id );
225 if( tag == LBER_ERROR ) {
227 LDAP_LOG( OPERATION, ERR,
228 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
230 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
237 tag = ber_peek_tag( ber, &len);
240 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
241 if( oldpass == NULL ) {
243 LDAP_LOG( OPERATION, ERR,
244 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
246 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
250 *text = "use bind to verify old password";
251 rc = LDAP_UNWILLING_TO_PERFORM;
255 tag = ber_scanf( ber, "m", oldpass );
257 if( tag == LBER_ERROR ) {
259 LDAP_LOG( OPERATION, ERR,
260 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
262 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
269 tag = ber_peek_tag( ber, &len );
272 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
273 if( newpass == NULL ) {
275 LDAP_LOG( OPERATION, ERR,
276 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
278 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
282 *text = "user specified passwords disallowed";
283 rc = LDAP_UNWILLING_TO_PERFORM;
287 tag = ber_scanf( ber, "m", newpass );
289 if( tag == LBER_ERROR ) {
291 LDAP_LOG( OPERATION, ERR,
292 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
294 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
301 tag = ber_peek_tag( ber, &len );
307 LDAP_LOG( OPERATION, ERR,
308 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
310 Debug( LDAP_DEBUG_TRACE,
311 "slap_passwd_parse: decoding error, len=%ld\n",
315 *text = "data decoding error";
316 rc = LDAP_PROTOCOL_ERROR;
323 struct berval * slap_passwd_return(
324 struct berval *cred )
327 struct berval *bv = NULL;
328 BerElementBuffer berbuf;
329 /* opaque structure, size unknown but smaller than berbuf */
330 BerElement *ber = (BerElement *)&berbuf;
332 assert( cred != NULL );
335 LDAP_LOG( OPERATION, ENTRY,
336 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
338 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
339 (long) cred->bv_len, 0, 0 );
342 ber_init_w_nullc( ber, LBER_USE_DER );
344 rc = ber_printf( ber, "{tON}",
345 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
348 (void) ber_flatten( ber, &bv );
366 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
367 ldap_pvt_thread_mutex_lock( &passwd_mutex );
369 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
373 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
374 if( !lutil_passwd( bv, cred, NULL, text ) ) {
380 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
382 lutil_passwd_sasl_conn = NULL;
384 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
391 slap_passwd_generate( struct berval *pass )
395 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
400 * generate passwords of only 8 characters as some getpass(3)
401 * implementations truncate at 8 characters.
403 tmp = lutil_passwd_generate( 8 );
415 struct berval * cred,
420 #ifdef LUTIL_SHA1_BYTES
421 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
423 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
427 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
428 ldap_pvt_thread_mutex_lock( &passwd_mutex );
431 tmp = lutil_passwd_hash( cred , hash, text );
433 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
434 ldap_pvt_thread_mutex_unlock( &passwd_mutex );