]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Sync with HEAD
[openldap] / servers / slapd / passwd.c
1 /* passwd.c - password extended operation routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/krb.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #include "slap.h"
27
28 #include <lber_pvt.h>
29 #include <lutil.h>
30 #include <lutil_sha1.h>
31
32 static const char *defhash[] = {
33 #ifdef LUTIL_SHA1_BYTES
34         "{SSHA}",
35 #else
36         "{SMD5}",
37 #endif
38         NULL
39 };
40
41 int passwd_extop(
42         Operation *op,
43         SlapReply *rs )
44 {
45         struct berval id = {0, NULL}, hash, *rsp = NULL;
46         req_pwdexop_s *qpw = &op->oq_pwdexop;
47         req_extended_s qext = op->oq_extended;
48         Modifications *ml;
49         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
50         slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
51         int i, nhash;
52         char **hashes;
53         int     rc;
54         BackendDB *op_be;
55
56         cb2.sc_next = &cb;
57
58         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
59
60         if( op->o_dn.bv_len == 0 ) {
61                 rs->sr_text = "only authenticated users may change passwords";
62                 return LDAP_STRONG_AUTH_REQUIRED;
63         }
64
65         qpw->rs_old.bv_val = NULL;
66         qpw->rs_new.bv_val = NULL;
67         qpw->rs_mods = NULL;
68         qpw->rs_modtail = NULL;
69
70         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
71                 &qpw->rs_new, &rs->sr_text );
72
73         if ( rs->sr_err != LDAP_SUCCESS ) {
74                 return rs->sr_err;
75         }
76
77         if ( id.bv_len ) {
78                 ber_dupbv_x( &op->o_req_dn, &id, op->o_tmpmemctx );
79                 /* ndn is in tmpmem, so we don't need to free it */
80                 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
81                 if ( rs->sr_err != LDAP_SUCCESS ) {
82                         rs->sr_text = "Invalid DN";
83                         rc = rs->sr_err;
84                         goto error_return;
85                 }
86                 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
87
88         } else {
89                 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
90                 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
91                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
92                 op->o_bd = op->o_conn->c_authz_backend;
93                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
94         }
95
96         if( op->o_bd == NULL ) {
97 #ifdef HAVE_CYRUS_SASL
98                 rc = slap_sasl_setpass( op, rs );
99 #else
100                 rs->sr_text = "no authz backend";
101                 rc = LDAP_OTHER;
102 #endif
103                 goto error_return;
104         }
105
106         if ( op->o_req_ndn.bv_len == 0 ) {
107                 rs->sr_text = "no password is associated with the Root DSE";
108                 rc = LDAP_UNWILLING_TO_PERFORM;
109                 goto error_return;
110         }
111
112         /* If we've got a glued backend, check the real backend */
113         op_be = op->o_bd;
114         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
115                 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
116         }
117
118         if (backend_check_restrictions( op, rs,
119                         (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
120                 rc = rs->sr_err;
121                 goto error_return;
122         }
123
124         /* check for referrals */
125         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
126                 rc = rs->sr_err;
127                 goto error_return;
128         }
129
130 #ifndef SLAPD_MULTIMASTER
131         /* This does not apply to multi-master case */
132         if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
133                 /* we SHOULD return a referral in this case */
134                 BerVarray defref = op->o_bd->be_update_refs
135                         ? op->o_bd->be_update_refs : default_referral; 
136
137                 if( defref != NULL ) {
138                         rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
139                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
140                         if(rs->sr_ref) {
141                                 rs->sr_flags |= REP_REF_MUSTBEFREED;
142                         } else {
143                                 rs->sr_ref = defref;
144                         }
145                         rc = LDAP_REFERRAL;
146                         goto error_return;
147
148                 }
149
150                 rs->sr_text = "shadow context; no update referral";
151                 rc = LDAP_UNWILLING_TO_PERFORM;
152                 goto error_return;
153         }
154 #endif /* !SLAPD_MULTIMASTER */
155
156         /* generate a new password if none was provided */
157         if ( qpw->rs_new.bv_len == 0 ) {
158                 slap_passwd_generate( &qpw->rs_new );
159                 if ( qpw->rs_new.bv_len ) {
160                         rsp = slap_passwd_return( &qpw->rs_new );
161                 }
162         }
163         if ( qpw->rs_new.bv_len == 0 ) {
164                 rs->sr_text = "password generation failed";
165                 rc = LDAP_OTHER;
166                 goto error_return;
167         }
168
169         op->o_bd = op_be;
170
171         /* Give the backend a chance to handle this itself */
172         if ( op->o_bd->be_extended ) {
173                 rs->sr_err = op->o_bd->be_extended( op, rs );
174                 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
175                         rs->sr_err != SLAP_CB_CONTINUE ) {
176                         rc = rs->sr_err;
177                         goto error_return;
178                 }
179         }
180
181         /* The backend didn't handle it, so try it here */
182         if( op->o_bd && !op->o_bd->be_modify ) {
183                 rs->sr_text = "operation not supported for current user";
184                 rc = LDAP_UNWILLING_TO_PERFORM;
185                 goto error_return;
186         }
187
188         ml = ch_malloc( sizeof(Modifications) );
189         if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
190
191         if ( default_passwd_hash ) {
192                 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
193                 hashes = default_passwd_hash;
194         } else {
195                 nhash = 1;
196                 hashes = (char **)defhash;
197         }
198         ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
199         for ( i=0; hashes[i]; i++ ) {
200                 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
201                 if ( hash.bv_len == 0 ) {
202                         if ( !rs->sr_text ) {
203                                 rs->sr_text = "password hash failed";
204                         }
205                         break;
206                 }
207                 ml->sml_values[i] = hash;
208         }
209         ml->sml_values[i].bv_val = NULL;
210         ml->sml_nvalues = NULL;
211         ml->sml_desc = slap_schema.si_ad_userPassword;
212         ml->sml_op = LDAP_MOD_REPLACE;
213         ml->sml_next = qpw->rs_mods;
214         qpw->rs_mods = ml;
215
216         if ( hashes[i] ) {
217                 rs->sr_err = LDAP_OTHER;
218
219         } else {
220                 slap_callback *sc = op->o_callback;
221
222                 op->o_tag = LDAP_REQ_MODIFY;
223                 op->o_callback = &cb2;
224                 op->orm_modlist = qpw->rs_mods;
225                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
226                                          * if it cares... */
227
228                 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
229                         NULL, 0, 1 );
230                 
231                 if ( rs->sr_err == LDAP_SUCCESS ) {
232                         rs->sr_err = op->o_bd->be_modify( op, rs );
233                 }
234                 if ( rs->sr_err == LDAP_SUCCESS ) {
235                         rs->sr_rspdata = rsp;
236                 } else if ( rsp ) {
237                         ber_bvfree( rsp );
238                 }
239                 op->o_tag = LDAP_REQ_EXTENDED;
240                 op->o_callback = sc;
241         }
242         slap_mods_free( qpw->rs_mods );
243         if ( rsp ) {
244                 free( qpw->rs_new.bv_val );
245         }
246
247         rc = rs->sr_err;
248         op->oq_extended = qext;
249
250 error_return:;
251         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
252                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
253         }
254         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
255                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
256         }
257
258         return rc;
259 }
260
261 int slap_passwd_parse( struct berval *reqdata,
262         struct berval *id,
263         struct berval *oldpass,
264         struct berval *newpass,
265         const char **text )
266 {
267         int rc = LDAP_SUCCESS;
268         ber_tag_t tag;
269         ber_len_t len = -1;
270         BerElementBuffer berbuf;
271         BerElement *ber = (BerElement *)&berbuf;
272
273         if( reqdata == NULL ) {
274                 return LDAP_SUCCESS;
275         }
276
277         if( reqdata->bv_len == 0 ) {
278                 *text = "empty request data field";
279                 return LDAP_PROTOCOL_ERROR;
280         }
281
282         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
283         ber_init2( ber, reqdata, 0 );
284
285         tag = ber_scanf( ber, "{" /*}*/ );
286
287         if( tag == LBER_ERROR ) {
288                 Debug( LDAP_DEBUG_TRACE,
289                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
290                 rc = LDAP_PROTOCOL_ERROR;
291                 goto done;
292         }
293
294         tag = ber_peek_tag( ber, &len );
295         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
296                 if( id == NULL ) {
297                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
298                                 0, 0, 0 );
299
300                         *text = "user must change own password";
301                         rc = LDAP_UNWILLING_TO_PERFORM;
302                         goto done;
303                 }
304
305                 tag = ber_scanf( ber, "m", id );
306
307                 if( tag == LBER_ERROR ) {
308                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
309                                 0, 0, 0 );
310
311                         goto decoding_error;
312                 }
313
314                 tag = ber_peek_tag( ber, &len);
315         }
316
317         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
318                 if( oldpass == NULL ) {
319                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
320                                 0, 0, 0 );
321
322                         *text = "use bind to verify old password";
323                         rc = LDAP_UNWILLING_TO_PERFORM;
324                         goto done;
325                 }
326
327                 tag = ber_scanf( ber, "m", oldpass );
328
329                 if( tag == LBER_ERROR ) {
330                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
331                                 0, 0, 0 );
332
333                         goto decoding_error;
334                 }
335
336                 tag = ber_peek_tag( ber, &len );
337         }
338
339         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
340                 if( newpass == NULL ) {
341                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
342                                 0, 0, 0 );
343
344                         *text = "user specified passwords disallowed";
345                         rc = LDAP_UNWILLING_TO_PERFORM;
346                         goto done;
347                 }
348
349                 tag = ber_scanf( ber, "m", newpass );
350
351                 if( tag == LBER_ERROR ) {
352                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
353                                 0, 0, 0 );
354
355                         goto decoding_error;
356                 }
357
358                 tag = ber_peek_tag( ber, &len );
359         }
360
361         if( len != 0 ) {
362 decoding_error:
363                 Debug( LDAP_DEBUG_TRACE,
364                         "slap_passwd_parse: decoding error, len=%ld\n",
365                         (long) len, 0, 0 );
366
367                 *text = "data decoding error";
368                 rc = LDAP_PROTOCOL_ERROR;
369         }
370
371 done:
372         return rc;
373 }
374
375 struct berval * slap_passwd_return(
376         struct berval           *cred )
377 {
378         int rc;
379         struct berval *bv = NULL;
380         BerElementBuffer berbuf;
381         /* opaque structure, size unknown but smaller than berbuf */
382         BerElement *ber = (BerElement *)&berbuf;
383
384         assert( cred != NULL );
385
386         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
387                 (long) cred->bv_len, 0, 0 );
388         
389         ber_init_w_nullc( ber, LBER_USE_DER );
390
391         rc = ber_printf( ber, "{tON}",
392                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
393
394         if( rc >= 0 ) {
395                 (void) ber_flatten( ber, &bv );
396         }
397
398         ber_free_buf( ber );
399
400         return bv;
401 }
402
403 /*
404  * if "e" is provided, access to each value of the password is checked first
405  */
406 int
407 slap_passwd_check(
408         Operation       *op,
409         Entry           *e,
410         Attribute       *a,
411         struct berval   *cred,
412         const char      **text )
413 {
414         int                     result = 1;
415         struct berval           *bv;
416         AccessControlState      acl_state = ACL_STATE_INIT;
417
418         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
419                 int     rc;
420
421                 /* if e is provided, check access */
422                 if ( e && access_allowed( op, e, a->a_desc, bv,
423                                         ACL_AUTH, &acl_state ) == 0 )
424                 {
425                         continue;
426                 }
427
428 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
429                 ldap_pvt_thread_mutex_lock( &passwd_mutex );
430 #ifdef SLAPD_SPASSWD
431                 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
432 #endif
433 #endif
434         
435                 rc = lutil_passwd( bv, cred, NULL, text );
436
437 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
438 #ifdef SLAPD_SPASSWD
439                 lutil_passwd_sasl_conn = NULL;
440 #endif
441                 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
442 #endif
443
444                 if ( !rc ) {
445                         result = 0;
446                         break;
447                 }
448         }
449
450         return result;
451 }
452
453 void
454 slap_passwd_generate( struct berval *pass )
455 {
456         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
457         pass->bv_val = NULL;
458         pass->bv_len = 0;
459
460         /*
461          * generate passwords of only 8 characters as some getpass(3)
462          * implementations truncate at 8 characters.
463          */
464         lutil_passwd_generate( pass, 8 );
465 }
466
467 void
468 slap_passwd_hash_type(
469         struct berval * cred,
470         struct berval * new,
471         char *hash,
472         const char **text )
473 {
474         new->bv_len = 0;
475         new->bv_val = NULL;
476
477         assert( hash );
478
479 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
480         ldap_pvt_thread_mutex_lock( &passwd_mutex );
481 #endif
482
483         lutil_passwd_hash( cred , hash, new, text );
484         
485 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
486         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
487 #endif
488
489 }
490 void
491 slap_passwd_hash(
492         struct berval * cred,
493         struct berval * new,
494         const char **text )
495 {
496         char *hash = NULL;
497         if ( default_passwd_hash ) {
498                 hash = default_passwd_hash[0];
499         }
500         if ( !hash ) {
501                 hash = (char *)defhash[0];
502         }
503
504         slap_passwd_hash_type( cred, new, hash, text );
505 }