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