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