]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
zero out sml_managing any time a Modifications is built (use calloc?)
[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, &qpw->rs_old,
77                 &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         ml = ch_malloc( sizeof(Modifications) );
204         if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
205
206         if ( default_passwd_hash ) {
207                 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
208                 hashes = default_passwd_hash;
209         } else {
210                 nhash = 1;
211                 hashes = (char **)defhash;
212         }
213         ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
214         for ( i=0; hashes[i]; i++ ) {
215                 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
216                 if ( hash.bv_len == 0 ) {
217                         if ( !rs->sr_text ) {
218                                 rs->sr_text = "password hash failed";
219                         }
220                         break;
221                 }
222                 ml->sml_values[i] = hash;
223         }
224         ml->sml_values[i].bv_val = NULL;
225         ml->sml_nvalues = NULL;
226         ml->sml_desc = slap_schema.si_ad_userPassword;
227         ml->sml_op = LDAP_MOD_REPLACE;
228         ml->sml_flags = 0;
229         ml->sml_managing = 0;
230         ml->sml_next = qpw->rs_mods;
231         qpw->rs_mods = ml;
232
233         if ( hashes[i] ) {
234                 rs->sr_err = LDAP_OTHER;
235
236         } else {
237                 slap_callback *sc = op->o_callback;
238
239                 op->o_tag = LDAP_REQ_MODIFY;
240                 op->o_callback = &cb2;
241                 op->orm_modlist = qpw->rs_mods;
242                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
243                                          * if it cares... */
244
245                 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
246                         NULL, 0, 1 );
247                 
248                 if ( rs->sr_err == LDAP_SUCCESS ) {
249                         rs->sr_err = op->o_bd->be_modify( op, rs );
250                 }
251                 if ( rs->sr_err == LDAP_SUCCESS ) {
252                         rs->sr_rspdata = rsp;
253                 } else if ( rsp ) {
254                         ber_bvfree( rsp );
255                 }
256                 op->o_tag = LDAP_REQ_EXTENDED;
257                 op->o_callback = sc;
258         }
259         slap_mods_free( qpw->rs_mods, 1 );
260         if ( rsp ) {
261                 free( qpw->rs_new.bv_val );
262         }
263
264         rc = rs->sr_err;
265         op->oq_extended = qext;
266
267 error_return:;
268         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
269                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
270         }
271         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
272                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
273         }
274
275         return rc;
276 }
277
278 int slap_passwd_parse( struct berval *reqdata,
279         struct berval *id,
280         struct berval *oldpass,
281         struct berval *newpass,
282         const char **text )
283 {
284         int rc = LDAP_SUCCESS;
285         ber_tag_t tag;
286         ber_len_t len = -1;
287         BerElementBuffer berbuf;
288         BerElement *ber = (BerElement *)&berbuf;
289
290         if( reqdata == NULL ) {
291                 return LDAP_SUCCESS;
292         }
293
294         if( reqdata->bv_len == 0 ) {
295                 *text = "empty request data field";
296                 return LDAP_PROTOCOL_ERROR;
297         }
298
299         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
300         ber_init2( ber, reqdata, 0 );
301
302         tag = ber_scanf( ber, "{" /*}*/ );
303
304         if( tag == LBER_ERROR ) {
305                 Debug( LDAP_DEBUG_TRACE,
306                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
307                 rc = LDAP_PROTOCOL_ERROR;
308                 goto done;
309         }
310
311         tag = ber_peek_tag( ber, &len );
312         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
313                 if( id == NULL ) {
314                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
315                                 0, 0, 0 );
316
317                         *text = "user must change own password";
318                         rc = LDAP_UNWILLING_TO_PERFORM;
319                         goto done;
320                 }
321
322                 tag = ber_scanf( ber, "m", id );
323
324                 if( tag == LBER_ERROR ) {
325                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
326                                 0, 0, 0 );
327
328                         goto decoding_error;
329                 }
330
331                 tag = ber_peek_tag( ber, &len );
332         }
333
334         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
335                 if( oldpass == NULL ) {
336                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
337                                 0, 0, 0 );
338
339                         *text = "use bind to verify old password";
340                         rc = LDAP_UNWILLING_TO_PERFORM;
341                         goto done;
342                 }
343
344                 tag = ber_scanf( ber, "m", oldpass );
345
346                 if( tag == LBER_ERROR ) {
347                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
348                                 0, 0, 0 );
349
350                         goto decoding_error;
351                 }
352
353                 tag = ber_peek_tag( ber, &len );
354         }
355
356         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
357                 if( newpass == NULL ) {
358                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
359                                 0, 0, 0 );
360
361                         *text = "user specified passwords disallowed";
362                         rc = LDAP_UNWILLING_TO_PERFORM;
363                         goto done;
364                 }
365
366                 tag = ber_scanf( ber, "m", newpass );
367
368                 if( tag == LBER_ERROR ) {
369                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
370                                 0, 0, 0 );
371
372                         goto decoding_error;
373                 }
374
375                 tag = ber_peek_tag( ber, &len );
376         }
377
378         if( len != 0 ) {
379 decoding_error:
380                 Debug( LDAP_DEBUG_TRACE,
381                         "slap_passwd_parse: decoding error, len=%ld\n",
382                         (long) len, 0, 0 );
383
384                 *text = "data decoding error";
385                 rc = LDAP_PROTOCOL_ERROR;
386         }
387
388 done:
389         return rc;
390 }
391
392 struct berval * slap_passwd_return(
393         struct berval           *cred )
394 {
395         int rc;
396         struct berval *bv = NULL;
397         BerElementBuffer berbuf;
398         /* opaque structure, size unknown but smaller than berbuf */
399         BerElement *ber = (BerElement *)&berbuf;
400
401         assert( cred != NULL );
402
403         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
404                 (long) cred->bv_len, 0, 0 );
405         
406         ber_init_w_nullc( ber, LBER_USE_DER );
407
408         rc = ber_printf( ber, "{tON}",
409                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
410
411         if( rc >= 0 ) {
412                 (void) ber_flatten( ber, &bv );
413         }
414
415         ber_free_buf( ber );
416
417         return bv;
418 }
419
420 /*
421  * if "e" is provided, access to each value of the password is checked first
422  */
423 int
424 slap_passwd_check(
425         Operation       *op,
426         Entry           *e,
427         Attribute       *a,
428         struct berval   *cred,
429         const char      **text )
430 {
431         int                     result = 1;
432         struct berval           *bv;
433         AccessControlState      acl_state = ACL_STATE_INIT;
434
435 #ifdef SLAPD_SPASSWD
436         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
437                 op->o_conn->c_sasl_authctx, NULL );
438 #endif
439
440         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
441                 /* if e is provided, check access */
442                 if ( e && access_allowed( op, e, a->a_desc, bv,
443                                         ACL_AUTH, &acl_state ) == 0 )
444                 {
445                         continue;
446                 }
447                 
448                 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
449                         result = 0;
450                         break;
451                 }
452         }
453
454 #ifdef SLAPD_SPASSWD
455         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
456                 NULL, NULL );
457 #endif
458
459         return result;
460 }
461
462 void
463 slap_passwd_generate( struct berval *pass )
464 {
465         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
466         pass->bv_val = NULL;
467         pass->bv_len = 0;
468
469         /*
470          * generate passwords of only 8 characters as some getpass(3)
471          * implementations truncate at 8 characters.
472          */
473         lutil_passwd_generate( pass, 8 );
474 }
475
476 void
477 slap_passwd_hash_type(
478         struct berval * cred,
479         struct berval * new,
480         char *hash,
481         const char **text )
482 {
483         new->bv_len = 0;
484         new->bv_val = NULL;
485
486         assert( hash != NULL );
487
488         lutil_passwd_hash( cred , hash, new, text );
489 }
490 void
491 slap_passwd_hash(
492         struct berval * cred,
493         struct berval * new,
494         const char **text )
495 {
496         char *hash = NULL;
497         if ( default_passwd_hash ) {
498                 hash = default_passwd_hash[0];
499         }
500         if ( !hash ) {
501                 hash = (char *)defhash[0];
502         }
503
504         slap_passwd_hash_type( cred, new, hash, text );
505 }
506
507 #ifdef SLAPD_CRYPT
508 static ldap_pvt_thread_mutex_t passwd_mutex;
509 static lutil_cryptfunc slapd_crypt;
510
511 static int slapd_crypt( const char *key, const char *salt, char **hash )
512 {
513         char *cr;
514         int rc;
515
516         ldap_pvt_thread_mutex_lock( &passwd_mutex );
517
518         cr = crypt( key, salt );
519         if ( cr == NULL || cr[0] == '\0' ) {
520                 /* salt must have been invalid */
521                 rc = LUTIL_PASSWD_ERR;
522         } else {
523                 if ( hash ) {
524                         *hash = ber_strdup( cr );
525                         rc = LUTIL_PASSWD_OK;
526
527                 } else {
528                         rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
529                 }
530         }
531
532         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
533         return rc;
534 }
535 #endif /* SLAPD_CRYPT */
536
537 void slap_passwd_init()
538 {
539 #ifdef SLAPD_CRYPT
540         ldap_pvt_thread_mutex_init( &passwd_mutex );
541         lutil_cryptptr = slapd_crypt;
542 #endif
543 }
544