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