]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
b44408b4a85c4eb7f70aae1bbc147a324646b418
[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         Modifications *ml;
48         Operation op2;
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                 op2 = *op;
212                 op2.o_tag = LDAP_REQ_MODIFY;
213                 op2.o_callback = &cb2;
214                 op2.orm_modlist = qpw->rs_mods;
215                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
216                                          * if it cares... */
217
218                 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
219                         NULL, 0, 1 );
220                 
221                 if ( rs->sr_err == LDAP_SUCCESS ) {
222                         rs->sr_err = op2.o_bd->be_modify( &op2, rs );
223                         /* FIXME: in case it got rewritten... */
224                         op->o_req_dn = op2.o_req_dn;
225                         op->o_req_ndn = op2.o_req_ndn;
226                 }
227                 if ( rs->sr_err == LDAP_SUCCESS ) {
228                         rs->sr_rspdata = rsp;
229                 } else if ( rsp ) {
230                         ber_bvfree( rsp );
231                 }
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
240 error_return:;
241         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
242                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
243         }
244         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
245                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
246         }
247
248         return rc;
249 }
250
251 int slap_passwd_parse( struct berval *reqdata,
252         struct berval *id,
253         struct berval *oldpass,
254         struct berval *newpass,
255         const char **text )
256 {
257         int rc = LDAP_SUCCESS;
258         ber_tag_t tag;
259         ber_len_t len = -1;
260         BerElementBuffer berbuf;
261         BerElement *ber = (BerElement *)&berbuf;
262
263         if( reqdata == NULL ) {
264                 return LDAP_SUCCESS;
265         }
266
267         if( reqdata->bv_len == 0 ) {
268                 *text = "empty request data field";
269                 return LDAP_PROTOCOL_ERROR;
270         }
271
272         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
273         ber_init2( ber, reqdata, 0 );
274
275         tag = ber_scanf( ber, "{" /*}*/ );
276
277         if( tag == LBER_ERROR ) {
278                 Debug( LDAP_DEBUG_TRACE,
279                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
280                 rc = LDAP_PROTOCOL_ERROR;
281                 goto done;
282         }
283
284         tag = ber_peek_tag( ber, &len );
285         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
286                 if( id == NULL ) {
287                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
288                                 0, 0, 0 );
289
290                         *text = "user must change own password";
291                         rc = LDAP_UNWILLING_TO_PERFORM;
292                         goto done;
293                 }
294
295                 tag = ber_scanf( ber, "m", id );
296
297                 if( tag == LBER_ERROR ) {
298                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
299                                 0, 0, 0 );
300
301                         goto decoding_error;
302                 }
303
304                 tag = ber_peek_tag( ber, &len);
305         }
306
307         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
308                 if( oldpass == NULL ) {
309                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
310                                 0, 0, 0 );
311
312                         *text = "use bind to verify old password";
313                         rc = LDAP_UNWILLING_TO_PERFORM;
314                         goto done;
315                 }
316
317                 tag = ber_scanf( ber, "m", oldpass );
318
319                 if( tag == LBER_ERROR ) {
320                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
321                                 0, 0, 0 );
322
323                         goto decoding_error;
324                 }
325
326                 tag = ber_peek_tag( ber, &len );
327         }
328
329         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
330                 if( newpass == NULL ) {
331                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
332                                 0, 0, 0 );
333
334                         *text = "user specified passwords disallowed";
335                         rc = LDAP_UNWILLING_TO_PERFORM;
336                         goto done;
337                 }
338
339                 tag = ber_scanf( ber, "m", newpass );
340
341                 if( tag == LBER_ERROR ) {
342                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
343                                 0, 0, 0 );
344
345                         goto decoding_error;
346                 }
347
348                 tag = ber_peek_tag( ber, &len );
349         }
350
351         if( len != 0 ) {
352 decoding_error:
353                 Debug( LDAP_DEBUG_TRACE,
354                         "slap_passwd_parse: decoding error, len=%ld\n",
355                         (long) len, 0, 0 );
356
357                 *text = "data decoding error";
358                 rc = LDAP_PROTOCOL_ERROR;
359         }
360
361 done:
362         return rc;
363 }
364
365 struct berval * slap_passwd_return(
366         struct berval           *cred )
367 {
368         int rc;
369         struct berval *bv = NULL;
370         BerElementBuffer berbuf;
371         /* opaque structure, size unknown but smaller than berbuf */
372         BerElement *ber = (BerElement *)&berbuf;
373
374         assert( cred != NULL );
375
376         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
377                 (long) cred->bv_len, 0, 0 );
378         
379         ber_init_w_nullc( ber, LBER_USE_DER );
380
381         rc = ber_printf( ber, "{tON}",
382                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
383
384         if( rc >= 0 ) {
385                 (void) ber_flatten( ber, &bv );
386         }
387
388         ber_free_buf( ber );
389
390         return bv;
391 }
392
393 /*
394  * if "e" is provided, access to each value of the password is checked first
395  */
396 int
397 slap_passwd_check(
398         Operation       *op,
399         Entry           *e,
400         Attribute       *a,
401         struct berval   *cred,
402         const char      **text )
403 {
404         int                     result = 1;
405         struct berval           *bv;
406         AccessControlState      acl_state = ACL_STATE_INIT;
407
408 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
409         ldap_pvt_thread_mutex_lock( &passwd_mutex );
410 #ifdef SLAPD_SPASSWD
411         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
412 #endif
413 #endif
414
415         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
416                 /* if e is provided, check access */
417                 if ( e && access_allowed( op, e, a->a_desc, bv,
418                                         ACL_AUTH, &acl_state ) == 0 )
419                 {
420                         continue;
421                 }
422                 
423                 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
424                         result = 0;
425                         break;
426                 }
427         }
428
429 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
430 #ifdef SLAPD_SPASSWD
431         lutil_passwd_sasl_conn = NULL;
432 #endif
433         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
434 #endif
435
436         return result;
437 }
438
439 void
440 slap_passwd_generate( struct berval *pass )
441 {
442         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
443         pass->bv_val = NULL;
444         pass->bv_len = 0;
445
446         /*
447          * generate passwords of only 8 characters as some getpass(3)
448          * implementations truncate at 8 characters.
449          */
450         lutil_passwd_generate( pass, 8 );
451 }
452
453 void
454 slap_passwd_hash_type(
455         struct berval * cred,
456         struct berval * new,
457         char *hash,
458         const char **text )
459 {
460         new->bv_len = 0;
461         new->bv_val = NULL;
462
463         assert( hash );
464
465 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
466         ldap_pvt_thread_mutex_lock( &passwd_mutex );
467 #endif
468
469         lutil_passwd_hash( cred , hash, new, text );
470         
471 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
472         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
473 #endif
474
475 }
476 void
477 slap_passwd_hash(
478         struct berval * cred,
479         struct berval * new,
480         const char **text )
481 {
482         char *hash = NULL;
483         if ( default_passwd_hash ) {
484                 hash = default_passwd_hash[0];
485         }
486         if ( !hash ) {
487                 hash = (char *)defhash[0];
488         }
489
490         slap_passwd_hash_type( cred, new, hash, text );
491 }