]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
component cleanup
[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-2004 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, 0 );
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
117 #ifndef SLAPD_MULTIMASTER
118         /* This does not apply to multi-master case */
119         if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
120                 /* we SHOULD return a referral in this case */
121                 BerVarray defref = op->o_bd->be_update_refs
122                         ? op->o_bd->be_update_refs : default_referral; 
123
124                 if( defref != NULL ) {
125                         rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
126                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
127                         if(rs->sr_ref) {
128                                 rs->sr_flags |= REP_REF_MUSTBEFREED;
129                         } else {
130                                 rs->sr_ref = defref;
131                         }
132                         rc = LDAP_REFERRAL;
133                         goto error_return;
134
135                 }
136
137                 rs->sr_text = "shadow context; no update referral";
138                 rc = LDAP_UNWILLING_TO_PERFORM;
139                 goto error_return;
140         }
141 #endif /* !SLAPD_MULTIMASTER */
142
143         /* generate a new password if none was provided */
144         if ( qpw->rs_new.bv_len == 0 ) {
145                 slap_passwd_generate( &qpw->rs_new );
146                 if ( qpw->rs_new.bv_len ) {
147                         rsp = slap_passwd_return( &qpw->rs_new );
148                 }
149         }
150         if ( qpw->rs_new.bv_len == 0 ) {
151                 rs->sr_text = "password generation failed";
152                 rc = LDAP_OTHER;
153                 goto error_return;
154         }
155
156         /* Give the backend a chance to handle this itself */
157         if ( op->o_bd->be_extended ) {
158                 rs->sr_err = op->o_bd->be_extended( op, rs );
159                 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
160                         rs->sr_err != SLAP_CB_CONTINUE ) {
161                         rc = rs->sr_err;
162                         goto error_return;
163                 }
164         }
165
166         /* The backend didn't handle it, so try it here */
167         if( op->o_bd && !op->o_bd->be_modify ) {
168                 rs->sr_text = "operation not supported for current user";
169                 rc = LDAP_UNWILLING_TO_PERFORM;
170                 goto error_return;
171         }
172
173         ml = ch_malloc( sizeof(Modifications) );
174         if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
175
176         if ( default_passwd_hash ) {
177                 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
178                 hashes = default_passwd_hash;
179         } else {
180                 nhash = 1;
181                 hashes = (char **)defhash;
182         }
183         ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
184         for ( i=0; hashes[i]; i++ ) {
185                 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
186                 if ( hash.bv_len == 0 ) {
187                         if ( !rs->sr_text ) {
188                                 rs->sr_text = "password hash failed";
189                         }
190                         break;
191                 }
192                 ml->sml_values[i] = hash;
193         }
194         ml->sml_values[i].bv_val = NULL;
195         ml->sml_nvalues = NULL;
196         ml->sml_desc = slap_schema.si_ad_userPassword;
197         ml->sml_op = LDAP_MOD_REPLACE;
198         ml->sml_next = qpw->rs_mods;
199         qpw->rs_mods = ml;
200
201         if ( hashes[i] ) {
202                 rs->sr_err = LDAP_OTHER;
203
204         } else {
205                 op2 = *op;
206                 op2.o_tag = LDAP_REQ_MODIFY;
207                 op2.o_callback = &cb2;
208                 op2.orm_modlist = qpw->rs_mods;
209                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
210                                          * if it cares... */
211
212                 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
213                         NULL, 0, 1 );
214                 
215                 if ( rs->sr_err == LDAP_SUCCESS ) {
216                         rs->sr_err = op2.o_bd->be_modify( &op2, rs );
217                         /* FIXME: in case it got rewritten... */
218                         op->o_req_dn = op2.o_req_dn;
219                         op->o_req_ndn = op2.o_req_ndn;
220                 }
221                 if ( rs->sr_err == LDAP_SUCCESS ) {
222                         rs->sr_rspdata = rsp;
223                 } else if ( rsp ) {
224                         ber_bvfree( rsp );
225                 }
226         }
227         slap_mods_free( qpw->rs_mods );
228         if ( rsp ) {
229                 free( qpw->rs_new.bv_val );
230         }
231
232         rc = rs->sr_err;
233
234 error_return:;
235         if ( !BER_BVISNULL( &op->o_req_dn ) ) {
236                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
237         }
238         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
239                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
240         }
241
242         return rc;
243 }
244
245 int slap_passwd_parse( struct berval *reqdata,
246         struct berval *id,
247         struct berval *oldpass,
248         struct berval *newpass,
249         const char **text )
250 {
251         int rc = LDAP_SUCCESS;
252         ber_tag_t tag;
253         ber_len_t len = -1;
254         BerElementBuffer berbuf;
255         BerElement *ber = (BerElement *)&berbuf;
256
257         if( reqdata == NULL ) {
258                 return LDAP_SUCCESS;
259         }
260
261         if( reqdata->bv_len == 0 ) {
262                 *text = "empty request data field";
263                 return LDAP_PROTOCOL_ERROR;
264         }
265
266         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
267         ber_init2( ber, reqdata, 0 );
268
269         tag = ber_scanf( ber, "{" /*}*/ );
270
271         if( tag == LBER_ERROR ) {
272                 Debug( LDAP_DEBUG_TRACE,
273                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
274                 rc = LDAP_PROTOCOL_ERROR;
275                 goto done;
276         }
277
278         tag = ber_peek_tag( ber, &len );
279         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
280                 if( id == NULL ) {
281                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
282                                 0, 0, 0 );
283
284                         *text = "user must change own password";
285                         rc = LDAP_UNWILLING_TO_PERFORM;
286                         goto done;
287                 }
288
289                 tag = ber_scanf( ber, "m", id );
290
291                 if( tag == LBER_ERROR ) {
292                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
293                                 0, 0, 0 );
294
295                         goto decoding_error;
296                 }
297
298                 tag = ber_peek_tag( ber, &len);
299         }
300
301         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
302                 if( oldpass == NULL ) {
303                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
304                                 0, 0, 0 );
305
306                         *text = "use bind to verify old password";
307                         rc = LDAP_UNWILLING_TO_PERFORM;
308                         goto done;
309                 }
310
311                 tag = ber_scanf( ber, "m", oldpass );
312
313                 if( tag == LBER_ERROR ) {
314                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
315                                 0, 0, 0 );
316
317                         goto decoding_error;
318                 }
319
320                 tag = ber_peek_tag( ber, &len );
321         }
322
323         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
324                 if( newpass == NULL ) {
325                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
326                                 0, 0, 0 );
327
328                         *text = "user specified passwords disallowed";
329                         rc = LDAP_UNWILLING_TO_PERFORM;
330                         goto done;
331                 }
332
333                 tag = ber_scanf( ber, "m", newpass );
334
335                 if( tag == LBER_ERROR ) {
336                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD 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( len != 0 ) {
346 decoding_error:
347                 Debug( LDAP_DEBUG_TRACE,
348                         "slap_passwd_parse: decoding error, len=%ld\n",
349                         (long) len, 0, 0 );
350
351                 *text = "data decoding error";
352                 rc = LDAP_PROTOCOL_ERROR;
353         }
354
355 done:
356         return rc;
357 }
358
359 struct berval * slap_passwd_return(
360         struct berval           *cred )
361 {
362         int rc;
363         struct berval *bv = NULL;
364         BerElementBuffer berbuf;
365         /* opaque structure, size unknown but smaller than berbuf */
366         BerElement *ber = (BerElement *)&berbuf;
367
368         assert( cred != NULL );
369
370         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
371                 (long) cred->bv_len, 0, 0 );
372         
373         ber_init_w_nullc( ber, LBER_USE_DER );
374
375         rc = ber_printf( ber, "{tON}",
376                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
377
378         if( rc >= 0 ) {
379                 (void) ber_flatten( ber, &bv );
380         }
381
382         ber_free_buf( ber );
383
384         return bv;
385 }
386
387 int
388 slap_passwd_check(
389         Connection *conn,
390         Attribute *a,
391         struct berval *cred,
392         const char **text )
393 {
394         int result = 1;
395         struct berval *bv;
396
397 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
398         ldap_pvt_thread_mutex_lock( &passwd_mutex );
399 #ifdef SLAPD_SPASSWD
400         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
401 #endif
402 #endif
403
404         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
405                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
406                         result = 0;
407                         break;
408                 }
409         }
410
411 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
412 #ifdef SLAPD_SPASSWD
413         lutil_passwd_sasl_conn = NULL;
414 #endif
415         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
416 #endif
417
418         return result;
419 }
420
421 void
422 slap_passwd_generate( struct berval *pass )
423 {
424         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
425         pass->bv_val = NULL;
426         pass->bv_len = 0;
427
428         /*
429          * generate passwords of only 8 characters as some getpass(3)
430          * implementations truncate at 8 characters.
431          */
432         lutil_passwd_generate( pass, 8 );
433 }
434
435 void
436 slap_passwd_hash_type(
437         struct berval * cred,
438         struct berval * new,
439         char *hash,
440         const char **text )
441 {
442         new->bv_len = 0;
443         new->bv_val = NULL;
444
445         assert( hash );
446
447 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
448         ldap_pvt_thread_mutex_lock( &passwd_mutex );
449 #endif
450
451         lutil_passwd_hash( cred , hash, new, text );
452         
453 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
454         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
455 #endif
456
457 }
458 void
459 slap_passwd_hash(
460         struct berval * cred,
461         struct berval * new,
462         const char **text )
463 {
464         char *hash = NULL;
465         if ( default_passwd_hash ) {
466                 hash = default_passwd_hash[0];
467         }
468         if ( !hash ) {
469                 hash = (char *)defhash[0];
470         }
471
472         slap_passwd_hash_type( cred, new, hash, text );
473 }