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 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
37 if( op->o_dn.bv_len == 0 ) {
38 rs->sr_text = "only authenticated users may change passwords";
39 return LDAP_STRONG_AUTH_REQUIRED;
42 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
43 op->o_bd = op->o_conn->c_authz_backend;
44 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
46 if( op->o_bd && !op->o_bd->be_extended ) {
47 rs->sr_text = "operation not supported for current user";
48 return LDAP_UNWILLING_TO_PERFORM;
51 if (backend_check_restrictions( op, rs,
52 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
56 if( op->o_bd == NULL ) {
57 #ifdef HAVE_CYRUS_SASL
58 rs->sr_err = slap_sasl_setpass( op, rs );
60 rs->sr_text = "no authz backend";
61 rs->sr_err = LDAP_OTHER;
64 #ifndef SLAPD_MULTIMASTER
65 /* This does not apply to multi-master case */
66 } else if( op->o_bd->be_update_ndn.bv_len ) {
67 /* we SHOULD return a referral in this case */
68 BerVarray defref = NULL;
69 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
71 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
73 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
74 ber_bvarray_add( &defref, &tmpbv );
77 defref = referral_rewrite( op->o_bd->be_update_refs,
78 NULL, NULL, LDAP_SCOPE_DEFAULT );
81 rs->sr_err = LDAP_REFERRAL;
82 #endif /* !SLAPD_MULTIMASTER */
85 rs->sr_err = op->o_bd->be_extended( op, rs );
91 int slap_passwd_parse( struct berval *reqdata,
93 struct berval *oldpass,
94 struct berval *newpass,
97 int rc = LDAP_SUCCESS;
100 BerElementBuffer berbuf;
101 BerElement *ber = (BerElement *)&berbuf;
103 if( reqdata == NULL ) {
107 if( reqdata->bv_len == 0 ) {
108 *text = "empty request data field";
109 return LDAP_PROTOCOL_ERROR;
112 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
113 ber_init2( ber, reqdata, 0 );
115 tag = ber_scanf( ber, "{" /*}*/ );
117 if( tag != LBER_ERROR ) {
118 tag = ber_peek_tag( ber, &len );
121 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
124 LDAP_LOG( OPERATION, ERR,
125 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
127 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
131 *text = "user must change own password";
132 rc = LDAP_UNWILLING_TO_PERFORM;
136 tag = ber_scanf( ber, "m", id );
138 if( tag == LBER_ERROR ) {
140 LDAP_LOG( OPERATION, ERR,
141 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
143 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
150 tag = ber_peek_tag( ber, &len);
153 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
154 if( oldpass == NULL ) {
156 LDAP_LOG( OPERATION, ERR,
157 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
159 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
163 *text = "use bind to verify old password";
164 rc = LDAP_UNWILLING_TO_PERFORM;
168 tag = ber_scanf( ber, "m", oldpass );
170 if( tag == LBER_ERROR ) {
172 LDAP_LOG( OPERATION, ERR,
173 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
175 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
182 tag = ber_peek_tag( ber, &len );
185 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
186 if( newpass == NULL ) {
188 LDAP_LOG( OPERATION, ERR,
189 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
195 *text = "user specified passwords disallowed";
196 rc = LDAP_UNWILLING_TO_PERFORM;
200 tag = ber_scanf( ber, "m", newpass );
202 if( tag == LBER_ERROR ) {
204 LDAP_LOG( OPERATION, ERR,
205 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
207 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
214 tag = ber_peek_tag( ber, &len );
220 LDAP_LOG( OPERATION, ERR,
221 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
223 Debug( LDAP_DEBUG_TRACE,
224 "slap_passwd_parse: decoding error, len=%ld\n",
228 *text = "data decoding error";
229 rc = LDAP_PROTOCOL_ERROR;
236 struct berval * slap_passwd_return(
237 struct berval *cred )
240 struct berval *bv = NULL;
241 BerElementBuffer berbuf;
242 /* opaque structure, size unknown but smaller than berbuf */
243 BerElement *ber = (BerElement *)&berbuf;
245 assert( cred != NULL );
248 LDAP_LOG( OPERATION, ENTRY,
249 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
251 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
252 (long) cred->bv_len, 0, 0 );
255 ber_init_w_nullc( ber, LBER_USE_DER );
257 rc = ber_printf( ber, "{tON}",
258 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
261 (void) ber_flatten( ber, &bv );
279 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
280 ldap_pvt_thread_mutex_lock( &passwd_mutex );
282 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
286 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
287 if( !lutil_passwd( bv, cred, NULL, text ) ) {
293 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
295 lutil_passwd_sasl_conn = NULL;
297 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
304 slap_passwd_generate( struct berval *pass )
308 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
310 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
313 * generate passwords of only 8 characters as some getpass(3)
314 * implementations truncate at 8 characters.
316 tmp = lutil_passwd_generate( 8 );
328 struct berval * cred,
333 #ifdef LUTIL_SHA1_BYTES
334 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
336 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
340 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
341 ldap_pvt_thread_mutex_lock( &passwd_mutex );
344 tmp = lutil_passwd_hash( cred , hash, text );
346 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
347 ldap_pvt_thread_mutex_unlock( &passwd_mutex );