]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Fix prev commit, overlay config was broken
[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 #include "slap.h"
27
28 #include <lber_pvt.h>
29 #include <lutil.h>
30 #include <lutil_sha1.h>
31
32 static const char *defhash[] = {
33 #ifdef LUTIL_SHA1_BYTES
34         "{SSHA}",
35 #else
36         "{SMD5}",
37 #endif
38         NULL
39 };
40
41 int passwd_extop(
42         Operation *op,
43         SlapReply *rs )
44 {
45         struct berval id = {0, NULL}, hash, *rsp = NULL;
46         req_pwdexop_s *qpw = &op->oq_pwdexop;
47         req_extended_s qext = op->oq_extended;
48         Modifications *ml;
49         slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
50         slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
51         int i, nhash;
52         char **hashes;
53         int     rc;
54
55         cb2.sc_next = &cb;
56
57         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
58
59         if( op->o_dn.bv_len == 0 ) {
60                 rs->sr_text = "only authenticated users may change passwords";
61                 return LDAP_STRONG_AUTH_REQUIRED;
62         }
63
64         qpw->rs_old.bv_val = NULL;
65         qpw->rs_new.bv_val = NULL;
66         qpw->rs_mods = NULL;
67         qpw->rs_modtail = NULL;
68
69         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
70                 &qpw->rs_new, &rs->sr_text );
71
72         if ( rs->sr_err != LDAP_SUCCESS ) {
73                 return rs->sr_err;
74         }
75
76         if ( id.bv_len ) {
77                 ber_dupbv_x( &op->o_req_dn, &id, op->o_tmpmemctx );
78                 /* ndn is in tmpmem, so we don't need to free it */
79                 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
80                 if ( rs->sr_err != LDAP_SUCCESS ) {
81                         rs->sr_text = "Invalid DN";
82                         rc = rs->sr_err;
83                         goto error_return;
84                 }
85                 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
86
87         } else {
88                 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
89                 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
90                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
91                 op->o_bd = op->o_conn->c_authz_backend;
92                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
93         }
94
95         if( op->o_bd == NULL ) {
96 #ifdef HAVE_CYRUS_SASL
97                 rc = slap_sasl_setpass( op, rs );
98 #else
99                 rs->sr_text = "no authz backend";
100                 rc = LDAP_OTHER;
101 #endif
102                 goto error_return;
103         }
104
105         if ( op->o_req_ndn.bv_len == 0 ) {
106                 rs->sr_text = "no password is associated with the Root DSE";
107                 rc = LDAP_UNWILLING_TO_PERFORM;
108                 goto error_return;
109         }
110
111         if (backend_check_restrictions( op, rs,
112                         (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
113                 rc = rs->sr_err;
114                 goto error_return;
115         }
116
117         /* check for referrals */
118         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
119                 rc = rs->sr_err;
120                 goto error_return;
121         }
122
123 #ifndef SLAPD_MULTIMASTER
124         /* This does not apply to multi-master case */
125         if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
126                 /* we SHOULD return a referral in this case */
127                 BerVarray defref = op->o_bd->be_update_refs
128                         ? op->o_bd->be_update_refs : default_referral; 
129
130                 if( defref != NULL ) {
131                         rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
132                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
133                         if(rs->sr_ref) {
134                                 rs->sr_flags |= REP_REF_MUSTBEFREED;
135                         } else {
136                                 rs->sr_ref = defref;
137                         }
138                         rc = LDAP_REFERRAL;
139                         goto error_return;
140
141                 }
142
143                 rs->sr_text = "shadow context; no update referral";
144                 rc = LDAP_UNWILLING_TO_PERFORM;
145                 goto error_return;
146         }
147 #endif /* !SLAPD_MULTIMASTER */
148
149         /* generate a new password if none was provided */
150         if ( qpw->rs_new.bv_len == 0 ) {
151                 slap_passwd_generate( &qpw->rs_new );
152                 if ( qpw->rs_new.bv_len ) {
153                         rsp = slap_passwd_return( &qpw->rs_new );
154                 }
155         }
156         if ( qpw->rs_new.bv_len == 0 ) {
157                 rs->sr_text = "password generation failed";
158                 rc = LDAP_OTHER;
159                 goto error_return;
160         }
161
162         /* Give the backend a chance to handle this itself */
163         if ( op->o_bd->be_extended ) {
164                 rs->sr_err = op->o_bd->be_extended( op, rs );
165                 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
166                         rs->sr_err != SLAP_CB_CONTINUE ) {
167                         rc = rs->sr_err;
168                         goto error_return;
169                 }
170         }
171
172         /* The backend didn't handle it, so try it here */
173         if( op->o_bd && !op->o_bd->be_modify ) {
174                 rs->sr_text = "operation not supported for current user";
175                 rc = LDAP_UNWILLING_TO_PERFORM;
176                 goto error_return;
177         }
178
179         ml = ch_malloc( sizeof(Modifications) );
180         if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
181
182         if ( default_passwd_hash ) {
183                 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
184                 hashes = default_passwd_hash;
185         } else {
186                 nhash = 1;
187                 hashes = (char **)defhash;
188         }
189         ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
190         for ( i=0; hashes[i]; i++ ) {
191                 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
192                 if ( hash.bv_len == 0 ) {
193                         if ( !rs->sr_text ) {
194                                 rs->sr_text = "password hash failed";
195                         }
196                         break;
197                 }
198                 ml->sml_values[i] = hash;
199         }
200         ml->sml_values[i].bv_val = NULL;
201         ml->sml_nvalues = NULL;
202         ml->sml_desc = slap_schema.si_ad_userPassword;
203         ml->sml_op = LDAP_MOD_REPLACE;
204         ml->sml_next = qpw->rs_mods;
205         qpw->rs_mods = ml;
206
207         if ( hashes[i] ) {
208                 rs->sr_err = LDAP_OTHER;
209
210         } else {
211                 slap_callback *sc = op->o_callback;
212
213                 op->o_tag = LDAP_REQ_MODIFY;
214                 op->o_callback = &cb2;
215                 op->orm_modlist = qpw->rs_mods;
216                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
217                                          * if it cares... */
218
219                 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
220                         NULL, 0, 1 );
221                 
222                 if ( rs->sr_err == LDAP_SUCCESS ) {
223                         rs->sr_err = op->o_bd->be_modify( op, rs );
224                 }
225                 if ( rs->sr_err == LDAP_SUCCESS ) {
226                         rs->sr_rspdata = rsp;
227                 } else if ( rsp ) {
228                         ber_bvfree( rsp );
229                 }
230                 op->o_tag = LDAP_REQ_EXTENDED;
231                 op->o_callback = sc;
232         }
233         slap_mods_free( qpw->rs_mods );
234         if ( rsp ) {
235                 free( qpw->rs_new.bv_val );
236         }
237
238         rc = rs->sr_err;
239         op->oq_extended = qext;
240
241 error_return:;
242         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
243                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
244         }
245         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
246                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
247         }
248
249         return rc;
250 }
251
252 int slap_passwd_parse( struct berval *reqdata,
253         struct berval *id,
254         struct berval *oldpass,
255         struct berval *newpass,
256         const char **text )
257 {
258         int rc = LDAP_SUCCESS;
259         ber_tag_t tag;
260         ber_len_t len = -1;
261         BerElementBuffer berbuf;
262         BerElement *ber = (BerElement *)&berbuf;
263
264         if( reqdata == NULL ) {
265                 return LDAP_SUCCESS;
266         }
267
268         if( reqdata->bv_len == 0 ) {
269                 *text = "empty request data field";
270                 return LDAP_PROTOCOL_ERROR;
271         }
272
273         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
274         ber_init2( ber, reqdata, 0 );
275
276         tag = ber_scanf( ber, "{" /*}*/ );
277
278         if( tag == LBER_ERROR ) {
279                 Debug( LDAP_DEBUG_TRACE,
280                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
281                 rc = LDAP_PROTOCOL_ERROR;
282                 goto done;
283         }
284
285         tag = ber_peek_tag( ber, &len );
286         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
287                 if( id == NULL ) {
288                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
289                                 0, 0, 0 );
290
291                         *text = "user must change own password";
292                         rc = LDAP_UNWILLING_TO_PERFORM;
293                         goto done;
294                 }
295
296                 tag = ber_scanf( ber, "m", id );
297
298                 if( tag == LBER_ERROR ) {
299                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
300                                 0, 0, 0 );
301
302                         goto decoding_error;
303                 }
304
305                 tag = ber_peek_tag( ber, &len);
306         }
307
308         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
309                 if( oldpass == NULL ) {
310                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
311                                 0, 0, 0 );
312
313                         *text = "use bind to verify old password";
314                         rc = LDAP_UNWILLING_TO_PERFORM;
315                         goto done;
316                 }
317
318                 tag = ber_scanf( ber, "m", oldpass );
319
320                 if( tag == LBER_ERROR ) {
321                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
322                                 0, 0, 0 );
323
324                         goto decoding_error;
325                 }
326
327                 tag = ber_peek_tag( ber, &len );
328         }
329
330         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
331                 if( newpass == NULL ) {
332                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
333                                 0, 0, 0 );
334
335                         *text = "user specified passwords disallowed";
336                         rc = LDAP_UNWILLING_TO_PERFORM;
337                         goto done;
338                 }
339
340                 tag = ber_scanf( ber, "m", newpass );
341
342                 if( tag == LBER_ERROR ) {
343                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
344                                 0, 0, 0 );
345
346                         goto decoding_error;
347                 }
348
349                 tag = ber_peek_tag( ber, &len );
350         }
351
352         if( len != 0 ) {
353 decoding_error:
354                 Debug( LDAP_DEBUG_TRACE,
355                         "slap_passwd_parse: decoding error, len=%ld\n",
356                         (long) len, 0, 0 );
357
358                 *text = "data decoding error";
359                 rc = LDAP_PROTOCOL_ERROR;
360         }
361
362 done:
363         return rc;
364 }
365
366 struct berval * slap_passwd_return(
367         struct berval           *cred )
368 {
369         int rc;
370         struct berval *bv = NULL;
371         BerElementBuffer berbuf;
372         /* opaque structure, size unknown but smaller than berbuf */
373         BerElement *ber = (BerElement *)&berbuf;
374
375         assert( cred != NULL );
376
377         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
378                 (long) cred->bv_len, 0, 0 );
379         
380         ber_init_w_nullc( ber, LBER_USE_DER );
381
382         rc = ber_printf( ber, "{tON}",
383                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
384
385         if( rc >= 0 ) {
386                 (void) ber_flatten( ber, &bv );
387         }
388
389         ber_free_buf( ber );
390
391         return bv;
392 }
393
394 /*
395  * if "e" is provided, access to each value of the password is checked first
396  */
397 int
398 slap_passwd_check(
399         Operation       *op,
400         Entry           *e,
401         Attribute       *a,
402         struct berval   *cred,
403         const char      **text )
404 {
405         int                     result = 1;
406         struct berval           *bv;
407         AccessControlState      acl_state = ACL_STATE_INIT;
408
409 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
410         ldap_pvt_thread_mutex_lock( &passwd_mutex );
411 #ifdef SLAPD_SPASSWD
412         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
413 #endif
414 #endif
415
416         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
417                 /* if e is provided, check access */
418                 if ( e && access_allowed( op, e, a->a_desc, bv,
419                                         ACL_AUTH, &acl_state ) == 0 )
420                 {
421                         continue;
422                 }
423                 
424                 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
425                         result = 0;
426                         break;
427                 }
428         }
429
430 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
431 #ifdef SLAPD_SPASSWD
432         lutil_passwd_sasl_conn = NULL;
433 #endif
434         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
435 #endif
436
437         return result;
438 }
439
440 void
441 slap_passwd_generate( struct berval *pass )
442 {
443         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
444         pass->bv_val = NULL;
445         pass->bv_len = 0;
446
447         /*
448          * generate passwords of only 8 characters as some getpass(3)
449          * implementations truncate at 8 characters.
450          */
451         lutil_passwd_generate( pass, 8 );
452 }
453
454 void
455 slap_passwd_hash_type(
456         struct berval * cred,
457         struct berval * new,
458         char *hash,
459         const char **text )
460 {
461         new->bv_len = 0;
462         new->bv_val = NULL;
463
464         assert( hash );
465
466 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
467         ldap_pvt_thread_mutex_lock( &passwd_mutex );
468 #endif
469
470         lutil_passwd_hash( cred , hash, new, text );
471         
472 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
473         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
474 #endif
475
476 }
477 void
478 slap_passwd_hash(
479         struct berval * cred,
480         struct berval * new,
481         const char **text )
482 {
483         char *hash = NULL;
484         if ( default_passwd_hash ) {
485                 hash = default_passwd_hash[0];
486         }
487         if ( !hash ) {
488                 hash = (char *)defhash[0];
489         }
490
491         slap_passwd_hash_type( cred, new, hash, text );
492 }