]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
ITS#5489
[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/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
286                 /* be_modify() might have shuffled modifications */
287                 qpw->rs_mods = op->orm_modlist;
288
289                 if ( rs->sr_err == LDAP_SUCCESS ) {
290                         rs->sr_rspdata = rsp;
291
292                 } else if ( rsp ) {
293                         ber_bvfree( rsp );
294                         rsp = NULL;
295                 }
296                 op->o_tag = LDAP_REQ_EXTENDED;
297                 op->o_callback = sc;
298         }
299
300         rc = rs->sr_err;
301         op->oq_extended = qext;
302
303 error_return:;
304         if ( qpw->rs_mods ) {
305                 slap_mods_free( qpw->rs_mods, 1 );
306         }
307         if ( freenewpw ) {
308                 free( qpw->rs_new.bv_val );
309         }
310         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
311                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
312                 BER_BVZERO( &op->o_req_dn );
313         }
314         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
315                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
316                 BER_BVZERO( &op->o_req_ndn );
317         }
318
319         return rc;
320 }
321
322 int slap_passwd_parse( struct berval *reqdata,
323         struct berval *id,
324         struct berval *oldpass,
325         struct berval *newpass,
326         const char **text )
327 {
328         int rc = LDAP_SUCCESS;
329         ber_tag_t tag;
330         ber_len_t len = -1;
331         BerElementBuffer berbuf;
332         BerElement *ber = (BerElement *)&berbuf;
333
334         if( reqdata == NULL ) {
335                 return LDAP_SUCCESS;
336         }
337
338         if( reqdata->bv_len == 0 ) {
339                 *text = "empty request data field";
340                 return LDAP_PROTOCOL_ERROR;
341         }
342
343         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
344         ber_init2( ber, reqdata, 0 );
345
346         tag = ber_scanf( ber, "{" /*}*/ );
347
348         if( tag == LBER_ERROR ) {
349                 Debug( LDAP_DEBUG_TRACE,
350                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
351                 rc = LDAP_PROTOCOL_ERROR;
352                 goto done;
353         }
354
355         tag = ber_peek_tag( ber, &len );
356         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
357                 if( id == NULL ) {
358                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
359                                 0, 0, 0 );
360
361                         *text = "user must change own password";
362                         rc = LDAP_UNWILLING_TO_PERFORM;
363                         goto done;
364                 }
365
366                 tag = ber_scanf( ber, "m", id );
367
368                 if( tag == LBER_ERROR ) {
369                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
370                                 0, 0, 0 );
371
372                         goto decoding_error;
373                 }
374
375                 tag = ber_peek_tag( ber, &len );
376         }
377
378         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
379                 if( oldpass == NULL ) {
380                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
381                                 0, 0, 0 );
382
383                         *text = "use bind to verify old password";
384                         rc = LDAP_UNWILLING_TO_PERFORM;
385                         goto done;
386                 }
387
388                 tag = ber_scanf( ber, "m", oldpass );
389
390                 if( tag == LBER_ERROR ) {
391                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
392                                 0, 0, 0 );
393
394                         goto decoding_error;
395                 }
396
397                 if( oldpass->bv_len == 0 ) {
398                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
399                                 0, 0, 0 );
400
401                         *text = "old password value is empty";
402                         rc = LDAP_UNWILLING_TO_PERFORM;
403                         goto done;
404                 }
405
406                 tag = ber_peek_tag( ber, &len );
407         }
408
409         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
410                 if( newpass == NULL ) {
411                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
412                                 0, 0, 0 );
413
414                         *text = "user specified passwords disallowed";
415                         rc = LDAP_UNWILLING_TO_PERFORM;
416                         goto done;
417                 }
418
419                 tag = ber_scanf( ber, "m", newpass );
420
421                 if( tag == LBER_ERROR ) {
422                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
423                                 0, 0, 0 );
424
425                         goto decoding_error;
426                 }
427
428                 if( newpass->bv_len == 0 ) {
429                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
430                                 0, 0, 0 );
431
432                         *text = "new password value is empty";
433                         rc = LDAP_UNWILLING_TO_PERFORM;
434                         goto done;
435                 }
436
437                 tag = ber_peek_tag( ber, &len );
438         }
439
440         if( len != 0 ) {
441 decoding_error:
442                 Debug( LDAP_DEBUG_TRACE,
443                         "slap_passwd_parse: decoding error, len=%ld\n",
444                         (long) len, 0, 0 );
445
446                 *text = "data decoding error";
447                 rc = LDAP_PROTOCOL_ERROR;
448         }
449
450 done:
451         return rc;
452 }
453
454 struct berval * slap_passwd_return(
455         struct berval           *cred )
456 {
457         int rc;
458         struct berval *bv = NULL;
459         BerElementBuffer berbuf;
460         /* opaque structure, size unknown but smaller than berbuf */
461         BerElement *ber = (BerElement *)&berbuf;
462
463         assert( cred != NULL );
464
465         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
466                 (long) cred->bv_len, 0, 0 );
467         
468         ber_init_w_nullc( ber, LBER_USE_DER );
469
470         rc = ber_printf( ber, "{tON}",
471                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
472
473         if( rc >= 0 ) {
474                 (void) ber_flatten( ber, &bv );
475         }
476
477         ber_free_buf( ber );
478
479         return bv;
480 }
481
482 /*
483  * if "e" is provided, access to each value of the password is checked first
484  */
485 int
486 slap_passwd_check(
487         Operation       *op,
488         Entry           *e,
489         Attribute       *a,
490         struct berval   *cred,
491         const char      **text )
492 {
493         int                     result = 1;
494         struct berval           *bv;
495         AccessControlState      acl_state = ACL_STATE_INIT;
496
497 #ifdef SLAPD_SPASSWD
498         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
499                 op->o_conn->c_sasl_authctx, NULL );
500 #endif
501
502         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
503                 /* if e is provided, check access */
504                 if ( e && access_allowed( op, e, a->a_desc, bv,
505                                         ACL_AUTH, &acl_state ) == 0 )
506                 {
507                         continue;
508                 }
509                 
510                 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
511                         result = 0;
512                         break;
513                 }
514         }
515
516 #ifdef SLAPD_SPASSWD
517         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
518                 NULL, NULL );
519 #endif
520
521         return result;
522 }
523
524 void
525 slap_passwd_generate( struct berval *pass )
526 {
527         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
528         BER_BVZERO( pass );
529
530         /*
531          * generate passwords of only 8 characters as some getpass(3)
532          * implementations truncate at 8 characters.
533          */
534         lutil_passwd_generate( pass, 8 );
535 }
536
537 void
538 slap_passwd_hash_type(
539         struct berval * cred,
540         struct berval * new,
541         char *hash,
542         const char **text )
543 {
544         new->bv_len = 0;
545         new->bv_val = NULL;
546
547         assert( hash != NULL );
548
549         lutil_passwd_hash( cred , hash, new, text );
550 }
551 void
552 slap_passwd_hash(
553         struct berval * cred,
554         struct berval * new,
555         const char **text )
556 {
557         char *hash = NULL;
558         if ( default_passwd_hash ) {
559                 hash = default_passwd_hash[0];
560         }
561         if ( !hash ) {
562                 hash = (char *)defhash[0];
563         }
564
565         slap_passwd_hash_type( cred, new, hash, text );
566 }
567
568 #ifdef SLAPD_CRYPT
569 static ldap_pvt_thread_mutex_t passwd_mutex;
570 static lutil_cryptfunc slapd_crypt;
571
572 static int slapd_crypt( const char *key, const char *salt, char **hash )
573 {
574         char *cr;
575         int rc;
576
577         ldap_pvt_thread_mutex_lock( &passwd_mutex );
578
579         cr = crypt( key, salt );
580         if ( cr == NULL || cr[0] == '\0' ) {
581                 /* salt must have been invalid */
582                 rc = LUTIL_PASSWD_ERR;
583         } else {
584                 if ( hash ) {
585                         *hash = ber_strdup( cr );
586                         rc = LUTIL_PASSWD_OK;
587
588                 } else {
589                         rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
590                 }
591         }
592
593         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
594         return rc;
595 }
596 #endif /* SLAPD_CRYPT */
597
598 void slap_passwd_init()
599 {
600 #ifdef SLAPD_CRYPT
601         ldap_pvt_thread_mutex_init( &passwd_mutex );
602         lutil_cryptptr = slapd_crypt;
603 #endif
604 }
605