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