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