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