]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Fix prev commit
[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
53         cb2.sc_next = &cb;
54
55         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
56
57         if( op->o_dn.bv_len == 0 ) {
58                 rs->sr_text = "only authenticated users may change passwords";
59                 return LDAP_STRONG_AUTH_REQUIRED;
60         }
61
62         qpw->rs_old.bv_val = NULL;
63         qpw->rs_new.bv_val = NULL;
64         qpw->rs_mods = NULL;
65         qpw->rs_modtail = NULL;
66
67         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
68                 &qpw->rs_new, &rs->sr_text );
69
70         if ( rs->sr_err != LDAP_SUCCESS ) {
71                 return rs->sr_err;
72         }
73
74         if ( id.bv_len ) {
75                 op->o_req_dn = id;
76                 /* ndn is in tmpmem, so we don't need to free it */
77                 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
78                 if ( rs->sr_err != LDAP_SUCCESS ) {
79                         rs->sr_text = "Invalid DN";
80                         return rs->sr_err;
81                 }
82                 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
83         } else {
84                 op->o_req_dn = op->o_dn;
85                 op->o_req_ndn = op->o_ndn;
86                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
87                 op->o_bd = op->o_conn->c_authz_backend;
88                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
89         }
90
91         if( op->o_bd == NULL ) {
92 #ifdef HAVE_CYRUS_SASL
93                 return slap_sasl_setpass( op, rs );
94 #else
95                 rs->sr_text = "no authz backend";
96                 return LDAP_OTHER;
97 #endif
98         }
99
100         if ( op->o_req_ndn.bv_len == 0 ) {
101                 rs->sr_text = "no password is associated with the Root DSE";
102                 return LDAP_UNWILLING_TO_PERFORM;
103         }
104
105         if (backend_check_restrictions( op, rs,
106                         (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
107                 return rs->sr_err;
108         }
109
110
111 #ifndef SLAPD_MULTIMASTER
112         /* This does not apply to multi-master case */
113         if( op->o_bd->be_update_ndn.bv_len ) {
114                 /* we SHOULD return a referral in this case */
115                 BerVarray defref = NULL;
116                 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
117                         syncinfo_t *si;
118                         LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
119                                 struct berval tmpbv;
120                                 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
121                                 ber_bvarray_add( &defref, &tmpbv );
122                         }
123                 } else {
124                         defref = referral_rewrite( op->o_bd->be_update_refs,
125                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
126                 }
127                 rs->sr_ref = defref;
128                 return LDAP_REFERRAL;
129         }
130 #endif /* !SLAPD_MULTIMASTER */
131
132         /* generate a new password if none was provided */
133         if ( qpw->rs_new.bv_len == 0 ) {
134                 slap_passwd_generate( &qpw->rs_new );
135                 if ( qpw->rs_new.bv_len ) {
136                         rsp = slap_passwd_return( &qpw->rs_new );
137                 }
138         }
139         if ( qpw->rs_new.bv_len == 0 ) {
140                 rs->sr_text = "password generation failed";
141                 return LDAP_OTHER;
142         }
143
144         /* Give the backend a chance to handle this itself */
145         if ( op->o_bd->be_extended ) {
146                 rs->sr_err = op->o_bd->be_extended( op, rs );
147                 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
148                         rs->sr_err != SLAP_CB_CONTINUE ) {
149                         return rs->sr_err;
150                 }
151         }
152
153         /* The backend didn't handle it, so try it here */
154         if( op->o_bd && !op->o_bd->be_modify ) {
155                 rs->sr_text = "operation not supported for current user";
156                 return LDAP_UNWILLING_TO_PERFORM;
157         }
158
159         ml = ch_malloc( sizeof(Modifications) );
160         if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
161
162         if ( default_passwd_hash ) {
163                 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
164                 hashes = default_passwd_hash;
165         } else {
166                 nhash = 1;
167                 hashes = (char **)defhash;
168         }
169         ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
170         for ( i=0; hashes[i]; i++ ) {
171                 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
172                 if ( hash.bv_len == 0 ) {
173                         if ( !rs->sr_text ) {
174                                 rs->sr_text = "password hash failed";
175                         }
176                         break;
177                 }
178                 ml->sml_values[i] = hash;
179         }
180         ml->sml_values[i].bv_val = NULL;
181         ml->sml_nvalues = NULL;
182         ml->sml_desc = slap_schema.si_ad_userPassword;
183         ml->sml_op = qpw->rs_old.bv_val ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
184         ml->sml_next = qpw->rs_mods;
185         qpw->rs_mods = ml;
186         if ( qpw->rs_old.bv_val ) {
187                 ml = ch_malloc( sizeof(Modifications) );
188                 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
189                 for ( i=0; hashes[i]; i++ ) {
190                         slap_passwd_hash_type( &qpw->rs_old, &hash, hashes[i], &rs->sr_text );
191                         if ( hash.bv_len == 0 ) {
192                                 if ( !rs->sr_text ) {
193                                         rs->sr_text = "password hash failed";
194                                 }
195                                 break;
196                         }
197                         ml->sml_values[i] = hash;
198                 }
199                 ml->sml_values[i].bv_val = NULL;
200                 ml->sml_desc = slap_schema.si_ad_userPassword;
201                 ml->sml_op = LDAP_MOD_DELETE;
202                 ml->sml_next = qpw->rs_mods;
203                 qpw->rs_mods = ml;
204         }
205         if ( hashes[i] ) {
206                 rs->sr_err = LDAP_OTHER;
207         } else {
208
209                 op2 = *op;
210                 op2.o_tag = LDAP_REQ_MODIFY;
211                 op2.o_callback = &cb2;
212                 op2.orm_modlist = qpw->rs_mods;
213                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
214                                          * if it cares... */
215
216                 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
217                         NULL, 0 );
218                 
219                 if ( rs->sr_err == LDAP_SUCCESS ) {
220                         rs->sr_err = op2.o_bd->be_modify( &op2, rs );
221                 }
222                 if ( rs->sr_err == LDAP_SUCCESS ) {
223                         rs->sr_rspdata = rsp;
224                 } else if ( rsp ) {
225                         ber_bvfree( rsp );
226                 }
227         }
228         slap_mods_free( qpw->rs_mods );
229         if ( rsp ) {
230                 free( qpw->rs_new.bv_val );
231         }
232
233         return rs->sr_err;
234 }
235
236 int slap_passwd_parse( struct berval *reqdata,
237         struct berval *id,
238         struct berval *oldpass,
239         struct berval *newpass,
240         const char **text )
241 {
242         int rc = LDAP_SUCCESS;
243         ber_tag_t tag;
244         ber_len_t len = -1;
245         BerElementBuffer berbuf;
246         BerElement *ber = (BerElement *)&berbuf;
247
248         if( reqdata == NULL ) {
249                 return LDAP_SUCCESS;
250         }
251
252         if( reqdata->bv_len == 0 ) {
253                 *text = "empty request data field";
254                 return LDAP_PROTOCOL_ERROR;
255         }
256
257         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
258         ber_init2( ber, reqdata, 0 );
259
260         tag = ber_scanf( ber, "{" /*}*/ );
261
262         if( tag == LBER_ERROR ) {
263 #ifdef NEW_LOGGING
264                 LDAP_LOG( OPERATION, ERR, 
265                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
266 #else
267                 Debug( LDAP_DEBUG_TRACE,
268                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
269 #endif
270                 rc = LDAP_PROTOCOL_ERROR;
271                 goto done;
272         }
273
274         tag = ber_peek_tag( ber, &len );
275         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
276                 if( id == NULL ) {
277 #ifdef NEW_LOGGING
278                         LDAP_LOG( OPERATION, ERR,
279                            "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
280 #else
281                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
282                                 0, 0, 0 );
283 #endif
284
285                         *text = "user must change own password";
286                         rc = LDAP_UNWILLING_TO_PERFORM;
287                         goto done;
288                 }
289
290                 tag = ber_scanf( ber, "m", id );
291
292                 if( tag == LBER_ERROR ) {
293 #ifdef NEW_LOGGING
294                         LDAP_LOG( OPERATION, ERR,
295                            "slap_passwd_parse:  ID parse failed.\n", 0, 0, 0 );
296 #else
297                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
298                                 0, 0, 0 );
299 #endif
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 #ifdef NEW_LOGGING
310                         LDAP_LOG( OPERATION, ERR,
311                            "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
312 #else
313                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
314                                 0, 0, 0 );
315 #endif
316
317                         *text = "use bind to verify old password";
318                         rc = LDAP_UNWILLING_TO_PERFORM;
319                         goto done;
320                 }
321
322                 tag = ber_scanf( ber, "m", oldpass );
323
324                 if( tag == LBER_ERROR ) {
325 #ifdef NEW_LOGGING
326                         LDAP_LOG( OPERATION, ERR,
327                            "slap_passwd_parse:  ID parse failed.\n" , 0, 0, 0 );
328 #else
329                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
330                                 0, 0, 0 );
331 #endif
332
333                         goto decoding_error;
334                 }
335
336                 tag = ber_peek_tag( ber, &len );
337         }
338
339         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
340                 if( newpass == NULL ) {
341 #ifdef NEW_LOGGING
342                         LDAP_LOG( OPERATION, ERR,
343                            "slap_passwd_parse:  NEW not allowed.\n", 0, 0, 0 );
344 #else
345                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
346                                 0, 0, 0 );
347 #endif
348
349                         *text = "user specified passwords disallowed";
350                         rc = LDAP_UNWILLING_TO_PERFORM;
351                         goto done;
352                 }
353
354                 tag = ber_scanf( ber, "m", newpass );
355
356                 if( tag == LBER_ERROR ) {
357 #ifdef NEW_LOGGING
358                         LDAP_LOG( OPERATION, ERR,
359                            "slap_passwd_parse:  OLD parse failed.\n", 0, 0, 0 );
360 #else
361                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
362                                 0, 0, 0 );
363 #endif
364
365                         goto decoding_error;
366                 }
367
368                 tag = ber_peek_tag( ber, &len );
369         }
370
371         if( len != 0 ) {
372 decoding_error:
373 #ifdef NEW_LOGGING
374                 LDAP_LOG( OPERATION, ERR, 
375                         "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
376 #else
377                 Debug( LDAP_DEBUG_TRACE,
378                         "slap_passwd_parse: decoding error, len=%ld\n",
379                         (long) len, 0, 0 );
380 #endif
381
382                 *text = "data decoding error";
383                 rc = LDAP_PROTOCOL_ERROR;
384         }
385
386 done:
387         return rc;
388 }
389
390 struct berval * slap_passwd_return(
391         struct berval           *cred )
392 {
393         int rc;
394         struct berval *bv = NULL;
395         BerElementBuffer berbuf;
396         /* opaque structure, size unknown but smaller than berbuf */
397         BerElement *ber = (BerElement *)&berbuf;
398
399         assert( cred != NULL );
400
401 #ifdef NEW_LOGGING
402         LDAP_LOG( OPERATION, ENTRY, 
403                 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
404 #else
405         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
406                 (long) cred->bv_len, 0, 0 );
407 #endif
408         
409         ber_init_w_nullc( ber, LBER_USE_DER );
410
411         rc = ber_printf( ber, "{tON}",
412                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
413
414         if( rc >= 0 ) {
415                 (void) ber_flatten( ber, &bv );
416         }
417
418         ber_free_buf( ber );
419
420         return bv;
421 }
422
423 int
424 slap_passwd_check(
425         Connection *conn,
426         Attribute *a,
427         struct berval *cred,
428         const char **text )
429 {
430         int result = 1;
431         struct berval *bv;
432
433 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
434         ldap_pvt_thread_mutex_lock( &passwd_mutex );
435 #ifdef SLAPD_SPASSWD
436         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
437 #endif
438 #endif
439
440         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
441                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
442                         result = 0;
443                         break;
444                 }
445         }
446
447 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
448 #ifdef SLAPD_SPASSWD
449         lutil_passwd_sasl_conn = NULL;
450 #endif
451         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
452 #endif
453
454         return result;
455 }
456
457 void
458 slap_passwd_generate( struct berval *pass )
459 {
460 #ifdef NEW_LOGGING
461         LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
462 #else
463         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
464 #endif
465         pass->bv_val = NULL;
466         pass->bv_len = 0;
467
468         /*
469          * generate passwords of only 8 characters as some getpass(3)
470          * implementations truncate at 8 characters.
471          */
472         lutil_passwd_generate( pass, 8 );
473 }
474
475 void
476 slap_passwd_hash_type(
477         struct berval * cred,
478         struct berval * new,
479         char *hash,
480         const char **text )
481 {
482         new->bv_len = 0;
483         new->bv_val = NULL;
484
485         assert( hash );
486
487 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
488         ldap_pvt_thread_mutex_lock( &passwd_mutex );
489 #endif
490
491         lutil_passwd_hash( cred , hash, new, text );
492         
493 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
494         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
495 #endif
496
497 }
498 void
499 slap_passwd_hash(
500         struct berval * cred,
501         struct berval * new,
502         const char **text )
503 {
504         char *hash = NULL;
505         if ( default_passwd_hash ) {
506                 hash = default_passwd_hash[0];
507         }
508         if ( !hash ) {
509                 hash = (char *)defhash[0];
510         }
511
512         slap_passwd_hash_type( cred, new, hash, text );
513 }