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