]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Referrals should not (except in special cases) be
[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 );
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 #ifdef NEW_LOGGING
273                 LDAP_LOG( OPERATION, ERR, 
274                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
275 #else
276                 Debug( LDAP_DEBUG_TRACE,
277                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
278 #endif
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 #ifdef NEW_LOGGING
287                         LDAP_LOG( OPERATION, ERR,
288                            "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
289 #else
290                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
291                                 0, 0, 0 );
292 #endif
293
294                         *text = "user must change own password";
295                         rc = LDAP_UNWILLING_TO_PERFORM;
296                         goto done;
297                 }
298
299                 tag = ber_scanf( ber, "m", id );
300
301                 if( tag == LBER_ERROR ) {
302 #ifdef NEW_LOGGING
303                         LDAP_LOG( OPERATION, ERR,
304                            "slap_passwd_parse:  ID parse failed.\n", 0, 0, 0 );
305 #else
306                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
307                                 0, 0, 0 );
308 #endif
309
310                         goto decoding_error;
311                 }
312
313                 tag = ber_peek_tag( ber, &len);
314         }
315
316         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
317                 if( oldpass == NULL ) {
318 #ifdef NEW_LOGGING
319                         LDAP_LOG( OPERATION, ERR,
320                            "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
321 #else
322                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
323                                 0, 0, 0 );
324 #endif
325
326                         *text = "use bind to verify old password";
327                         rc = LDAP_UNWILLING_TO_PERFORM;
328                         goto done;
329                 }
330
331                 tag = ber_scanf( ber, "m", oldpass );
332
333                 if( tag == LBER_ERROR ) {
334 #ifdef NEW_LOGGING
335                         LDAP_LOG( OPERATION, ERR,
336                            "slap_passwd_parse:  ID parse failed.\n" , 0, 0, 0 );
337 #else
338                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
339                                 0, 0, 0 );
340 #endif
341
342                         goto decoding_error;
343                 }
344
345                 tag = ber_peek_tag( ber, &len );
346         }
347
348         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
349                 if( newpass == NULL ) {
350 #ifdef NEW_LOGGING
351                         LDAP_LOG( OPERATION, ERR,
352                            "slap_passwd_parse:  NEW not allowed.\n", 0, 0, 0 );
353 #else
354                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
355                                 0, 0, 0 );
356 #endif
357
358                         *text = "user specified passwords disallowed";
359                         rc = LDAP_UNWILLING_TO_PERFORM;
360                         goto done;
361                 }
362
363                 tag = ber_scanf( ber, "m", newpass );
364
365                 if( tag == LBER_ERROR ) {
366 #ifdef NEW_LOGGING
367                         LDAP_LOG( OPERATION, ERR,
368                            "slap_passwd_parse:  OLD parse failed.\n", 0, 0, 0 );
369 #else
370                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
371                                 0, 0, 0 );
372 #endif
373
374                         goto decoding_error;
375                 }
376
377                 tag = ber_peek_tag( ber, &len );
378         }
379
380         if( len != 0 ) {
381 decoding_error:
382 #ifdef NEW_LOGGING
383                 LDAP_LOG( OPERATION, ERR, 
384                         "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
385 #else
386                 Debug( LDAP_DEBUG_TRACE,
387                         "slap_passwd_parse: decoding error, len=%ld\n",
388                         (long) len, 0, 0 );
389 #endif
390
391                 *text = "data decoding error";
392                 rc = LDAP_PROTOCOL_ERROR;
393         }
394
395 done:
396         return rc;
397 }
398
399 struct berval * slap_passwd_return(
400         struct berval           *cred )
401 {
402         int rc;
403         struct berval *bv = NULL;
404         BerElementBuffer berbuf;
405         /* opaque structure, size unknown but smaller than berbuf */
406         BerElement *ber = (BerElement *)&berbuf;
407
408         assert( cred != NULL );
409
410 #ifdef NEW_LOGGING
411         LDAP_LOG( OPERATION, ENTRY, 
412                 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
413 #else
414         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
415                 (long) cred->bv_len, 0, 0 );
416 #endif
417         
418         ber_init_w_nullc( ber, LBER_USE_DER );
419
420         rc = ber_printf( ber, "{tON}",
421                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
422
423         if( rc >= 0 ) {
424                 (void) ber_flatten( ber, &bv );
425         }
426
427         ber_free_buf( ber );
428
429         return bv;
430 }
431
432 int
433 slap_passwd_check(
434         Connection *conn,
435         Attribute *a,
436         struct berval *cred,
437         const char **text )
438 {
439         int result = 1;
440         struct berval *bv;
441
442 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
443         ldap_pvt_thread_mutex_lock( &passwd_mutex );
444 #ifdef SLAPD_SPASSWD
445         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
446 #endif
447 #endif
448
449         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
450                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
451                         result = 0;
452                         break;
453                 }
454         }
455
456 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
457 #ifdef SLAPD_SPASSWD
458         lutil_passwd_sasl_conn = NULL;
459 #endif
460         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
461 #endif
462
463         return result;
464 }
465
466 void
467 slap_passwd_generate( struct berval *pass )
468 {
469 #ifdef NEW_LOGGING
470         LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
471 #else
472         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
473 #endif
474         pass->bv_val = NULL;
475         pass->bv_len = 0;
476
477         /*
478          * generate passwords of only 8 characters as some getpass(3)
479          * implementations truncate at 8 characters.
480          */
481         lutil_passwd_generate( pass, 8 );
482 }
483
484 void
485 slap_passwd_hash_type(
486         struct berval * cred,
487         struct berval * new,
488         char *hash,
489         const char **text )
490 {
491         new->bv_len = 0;
492         new->bv_val = NULL;
493
494         assert( hash );
495
496 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
497         ldap_pvt_thread_mutex_lock( &passwd_mutex );
498 #endif
499
500         lutil_passwd_hash( cred , hash, new, text );
501         
502 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
503         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
504 #endif
505
506 }
507 void
508 slap_passwd_hash(
509         struct berval * cred,
510         struct berval * new,
511         const char **text )
512 {
513         char *hash = NULL;
514         if ( default_passwd_hash ) {
515                 hash = default_passwd_hash[0];
516         }
517         if ( !hash ) {
518                 hash = (char *)defhash[0];
519         }
520
521         slap_passwd_hash_type( cred, new, hash, text );
522 }