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