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