]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Ignore attempts to Add cn=schema,cn=config
[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-2009 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         struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
61
62         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
63
64         if( op->o_dn.bv_len == 0 ) {
65                 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD\n",
66                         op->o_log_prefix, 0, 0, 0, 0 );
67                 rs->sr_text = "only authenticated users may change passwords";
68                 return LDAP_STRONG_AUTH_REQUIRED;
69         }
70
71         qpw->rs_old.bv_len = 0;
72         qpw->rs_old.bv_val = NULL;
73         qpw->rs_new.bv_len = 0;
74         qpw->rs_new.bv_val = NULL;
75         qpw->rs_mods = NULL;
76         qpw->rs_modtail = NULL;
77
78         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
79                 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
80
81         if ( !BER_BVISNULL( &id )) {
82                 idNul = id.bv_val[id.bv_len];
83                 id.bv_val[id.bv_len] = '\0';
84         }
85         if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
86                 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
87                         op->o_log_prefix, id.bv_val,
88                         qpw->rs_old.bv_val ? " old" : "",
89                         qpw->rs_new.bv_val ? " new" : "", 0 );
90         } else {
91                 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD%s%s\n",
92                         op->o_log_prefix,
93                         qpw->rs_old.bv_val ? " old" : "",
94                         qpw->rs_new.bv_val ? " new" : "", 0, 0 );
95         }
96
97         if ( rs->sr_err != LDAP_SUCCESS ) {
98                 if ( !BER_BVISNULL( &id ))
99                         id.bv_val[id.bv_len] = idNul;
100                 return rs->sr_err;
101         }
102
103         if ( !BER_BVISEMPTY( &id ) ) {
104                 rs->sr_err = dnPrettyNormal( NULL, &id, &dn, &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_req_dn = dn;
112                 op->o_req_ndn = ndn;
113                 op->o_bd = select_backend( &op->o_req_ndn, 1 );
114
115         } else {
116                 ber_dupbv_x( &dn, &op->o_dn, op->o_tmpmemctx );
117                 ber_dupbv_x( &ndn, &op->o_ndn, op->o_tmpmemctx );
118                 op->o_req_dn = dn;
119                 op->o_req_ndn = ndn;
120                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
121                 op->o_bd = op->o_conn->c_authz_backend;
122                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
123         }
124
125         if( op->o_bd == NULL ) {
126                 if ( qpw->rs_old.bv_val != NULL ) {
127                         rs->sr_text = "unwilling to verify old password";
128                         rc = LDAP_UNWILLING_TO_PERFORM;
129                         goto error_return;
130                 }
131
132 #ifdef HAVE_CYRUS_SASL
133                 rc = slap_sasl_setpass( op, rs );
134 #else
135                 rs->sr_text = "no authz backend";
136                 rc = LDAP_OTHER;
137 #endif
138                 goto error_return;
139         }
140
141         if ( op->o_req_ndn.bv_len == 0 ) {
142                 rs->sr_text = "no password is associated with the Root DSE";
143                 rc = LDAP_UNWILLING_TO_PERFORM;
144                 goto error_return;
145         }
146
147         /* If we've got a glued backend, check the real backend */
148         op_be = op->o_bd;
149         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
150                 op->o_bd = select_backend( &op->o_req_ndn, 0 );
151         }
152
153         if (backend_check_restrictions( op, rs,
154                         (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
155                 rc = rs->sr_err;
156                 goto error_return;
157         }
158
159         /* check for referrals */
160         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
161                 rc = rs->sr_err;
162                 goto error_return;
163         }
164
165         /* This does not apply to multi-master case */
166         if(!( !SLAP_SINGLE_SHADOW( op->o_bd ) || be_isupdate( op ))) {
167                 /* we SHOULD return a referral in this case */
168                 BerVarray defref = op->o_bd->be_update_refs
169                         ? op->o_bd->be_update_refs : default_referral; 
170
171                 if( defref != NULL ) {
172                         rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
173                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
174                         if(rs->sr_ref) {
175                                 rs->sr_flags |= REP_REF_MUSTBEFREED;
176                         } else {
177                                 rs->sr_ref = defref;
178                         }
179                         rc = LDAP_REFERRAL;
180                         goto error_return;
181
182                 }
183
184                 rs->sr_text = "shadow context; no update referral";
185                 rc = LDAP_UNWILLING_TO_PERFORM;
186                 goto error_return;
187         }
188
189         /* generate a new password if none was provided */
190         if ( qpw->rs_new.bv_len == 0 ) {
191                 slap_passwd_generate( &qpw->rs_new );
192                 if ( qpw->rs_new.bv_len ) {
193                         rsp = slap_passwd_return( &qpw->rs_new );
194                         freenewpw = 1;
195                 }
196         }
197         if ( qpw->rs_new.bv_len == 0 ) {
198                 rs->sr_text = "password generation failed";
199                 rc = LDAP_OTHER;
200                 goto error_return;
201         }
202
203         op->o_bd = op_be;
204
205         /* Give the backend a chance to handle this itself */
206         if ( op->o_bd->be_extended ) {
207                 rs->sr_err = op->o_bd->be_extended( op, rs );
208                 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
209                         rs->sr_err != SLAP_CB_CONTINUE )
210                 {
211                         rc = rs->sr_err;
212                         if ( rsp ) {
213                                 rs->sr_rspdata = rsp;
214                                 rsp = NULL;
215                         }
216                         goto error_return;
217                 }
218         }
219
220         /* The backend didn't handle it, so try it here */
221         if( op->o_bd && !op->o_bd->be_modify ) {
222                 rs->sr_text = "operation not supported for current user";
223                 rc = LDAP_UNWILLING_TO_PERFORM;
224                 goto error_return;
225         }
226
227         if ( qpw->rs_old.bv_val != NULL ) {
228                 Entry *e = NULL;
229
230                 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
231                         slap_schema.si_ad_userPassword, 0, &e );
232                 if ( rc == LDAP_SUCCESS && e ) {
233                         Attribute *a = attr_find( e->e_attrs,
234                                 slap_schema.si_ad_userPassword );
235                         if ( a )
236                                 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
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( &dn ) ) {
321                 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
322                 BER_BVZERO( &op->o_req_dn );
323         }
324         if ( !BER_BVISNULL( &ndn ) ) {
325                 op->o_tmpfree( 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         char            credNul = cred->bv_val[cred->bv_len];
511
512 #ifdef SLAPD_SPASSWD
513         void            *old_authctx = NULL;
514
515         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
516                 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
517 #endif
518
519         if ( credNul ) cred->bv_val[cred->bv_len] = 0;
520
521         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
522                 /* if e is provided, check access */
523                 if ( e && access_allowed( op, e, a->a_desc, bv,
524                                         ACL_AUTH, &acl_state ) == 0 )
525                 {
526                         continue;
527                 }
528                 
529                 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
530                         result = 0;
531                         break;
532                 }
533         }
534
535         if ( credNul ) cred->bv_val[cred->bv_len] = credNul;
536
537 #ifdef SLAPD_SPASSWD
538         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
539                 old_authctx, 0, NULL, NULL );
540 #endif
541
542         return result;
543 }
544
545 void
546 slap_passwd_generate( struct berval *pass )
547 {
548         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
549         BER_BVZERO( pass );
550
551         /*
552          * generate passwords of only 8 characters as some getpass(3)
553          * implementations truncate at 8 characters.
554          */
555         lutil_passwd_generate( pass, 8 );
556 }
557
558 void
559 slap_passwd_hash_type(
560         struct berval * cred,
561         struct berval * new,
562         char *hash,
563         const char **text )
564 {
565         new->bv_len = 0;
566         new->bv_val = NULL;
567
568         assert( hash != NULL );
569
570         lutil_passwd_hash( cred , hash, new, text );
571 }
572 void
573 slap_passwd_hash(
574         struct berval * cred,
575         struct berval * new,
576         const char **text )
577 {
578         char *hash = NULL;
579         if ( default_passwd_hash ) {
580                 hash = default_passwd_hash[0];
581         }
582         if ( !hash ) {
583                 hash = (char *)defhash[0];
584         }
585
586         slap_passwd_hash_type( cred, new, hash, text );
587 }
588
589 #ifdef SLAPD_CRYPT
590 static ldap_pvt_thread_mutex_t passwd_mutex;
591 static lutil_cryptfunc slapd_crypt;
592
593 static int slapd_crypt( const char *key, const char *salt, char **hash )
594 {
595         char *cr;
596         int rc;
597
598         ldap_pvt_thread_mutex_lock( &passwd_mutex );
599
600         cr = crypt( key, salt );
601         if ( cr == NULL || cr[0] == '\0' ) {
602                 /* salt must have been invalid */
603                 rc = LUTIL_PASSWD_ERR;
604         } else {
605                 if ( hash ) {
606                         *hash = ber_strdup( cr );
607                         rc = LUTIL_PASSWD_OK;
608
609                 } else {
610                         rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
611                 }
612         }
613
614         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
615         return rc;
616 }
617 #endif /* SLAPD_CRYPT */
618
619 void slap_passwd_init()
620 {
621 #ifdef SLAPD_CRYPT
622         ldap_pvt_thread_mutex_init( &passwd_mutex );
623         lutil_cryptptr = slapd_crypt;
624 #endif
625 }
626