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