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