]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
Added ppolicy_hide_lockout keyword
[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 = LDAP_MOD_REPLACE;
184         ml->sml_next = qpw->rs_mods;
185         qpw->rs_mods = ml;
186
187         if ( hashes[i] ) {
188                 rs->sr_err = LDAP_OTHER;
189         } else {
190
191                 op2 = *op;
192                 op2.o_tag = LDAP_REQ_MODIFY;
193                 op2.o_callback = &cb2;
194                 op2.orm_modlist = qpw->rs_mods;
195                 cb2.sc_private = qpw;   /* let Modify know this was pwdMod,
196                                          * if it cares... */
197
198                 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
199                         NULL, 0 );
200                 
201                 if ( rs->sr_err == LDAP_SUCCESS ) {
202                         rs->sr_err = op2.o_bd->be_modify( &op2, rs );
203                 }
204                 if ( rs->sr_err == LDAP_SUCCESS ) {
205                         rs->sr_rspdata = rsp;
206                 } else if ( rsp ) {
207                         ber_bvfree( rsp );
208                 }
209         }
210         slap_mods_free( qpw->rs_mods );
211         if ( rsp ) {
212                 free( qpw->rs_new.bv_val );
213         }
214
215         return rs->sr_err;
216 }
217
218 int slap_passwd_parse( struct berval *reqdata,
219         struct berval *id,
220         struct berval *oldpass,
221         struct berval *newpass,
222         const char **text )
223 {
224         int rc = LDAP_SUCCESS;
225         ber_tag_t tag;
226         ber_len_t len = -1;
227         BerElementBuffer berbuf;
228         BerElement *ber = (BerElement *)&berbuf;
229
230         if( reqdata == NULL ) {
231                 return LDAP_SUCCESS;
232         }
233
234         if( reqdata->bv_len == 0 ) {
235                 *text = "empty request data field";
236                 return LDAP_PROTOCOL_ERROR;
237         }
238
239         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
240         ber_init2( ber, reqdata, 0 );
241
242         tag = ber_scanf( ber, "{" /*}*/ );
243
244         if( tag == LBER_ERROR ) {
245 #ifdef NEW_LOGGING
246                 LDAP_LOG( OPERATION, ERR, 
247                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
248 #else
249                 Debug( LDAP_DEBUG_TRACE,
250                         "slap_passwd_parse: decoding error\n", 0, 0, 0 );
251 #endif
252                 rc = LDAP_PROTOCOL_ERROR;
253                 goto done;
254         }
255
256         tag = ber_peek_tag( ber, &len );
257         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
258                 if( id == NULL ) {
259 #ifdef NEW_LOGGING
260                         LDAP_LOG( OPERATION, ERR,
261                            "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
262 #else
263                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
264                                 0, 0, 0 );
265 #endif
266
267                         *text = "user must change own password";
268                         rc = LDAP_UNWILLING_TO_PERFORM;
269                         goto done;
270                 }
271
272                 tag = ber_scanf( ber, "m", id );
273
274                 if( tag == LBER_ERROR ) {
275 #ifdef NEW_LOGGING
276                         LDAP_LOG( OPERATION, ERR,
277                            "slap_passwd_parse:  ID parse failed.\n", 0, 0, 0 );
278 #else
279                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
280                                 0, 0, 0 );
281 #endif
282
283                         goto decoding_error;
284                 }
285
286                 tag = ber_peek_tag( ber, &len);
287         }
288
289         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
290                 if( oldpass == NULL ) {
291 #ifdef NEW_LOGGING
292                         LDAP_LOG( OPERATION, ERR,
293                            "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
294 #else
295                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
296                                 0, 0, 0 );
297 #endif
298
299                         *text = "use bind to verify old password";
300                         rc = LDAP_UNWILLING_TO_PERFORM;
301                         goto done;
302                 }
303
304                 tag = ber_scanf( ber, "m", oldpass );
305
306                 if( tag == LBER_ERROR ) {
307 #ifdef NEW_LOGGING
308                         LDAP_LOG( OPERATION, ERR,
309                            "slap_passwd_parse:  ID parse failed.\n" , 0, 0, 0 );
310 #else
311                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
312                                 0, 0, 0 );
313 #endif
314
315                         goto decoding_error;
316                 }
317
318                 tag = ber_peek_tag( ber, &len );
319         }
320
321         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
322                 if( newpass == NULL ) {
323 #ifdef NEW_LOGGING
324                         LDAP_LOG( OPERATION, ERR,
325                            "slap_passwd_parse:  NEW not allowed.\n", 0, 0, 0 );
326 #else
327                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
328                                 0, 0, 0 );
329 #endif
330
331                         *text = "user specified passwords disallowed";
332                         rc = LDAP_UNWILLING_TO_PERFORM;
333                         goto done;
334                 }
335
336                 tag = ber_scanf( ber, "m", newpass );
337
338                 if( tag == LBER_ERROR ) {
339 #ifdef NEW_LOGGING
340                         LDAP_LOG( OPERATION, ERR,
341                            "slap_passwd_parse:  OLD parse failed.\n", 0, 0, 0 );
342 #else
343                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
344                                 0, 0, 0 );
345 #endif
346
347                         goto decoding_error;
348                 }
349
350                 tag = ber_peek_tag( ber, &len );
351         }
352
353         if( len != 0 ) {
354 decoding_error:
355 #ifdef NEW_LOGGING
356                 LDAP_LOG( OPERATION, ERR, 
357                         "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
358 #else
359                 Debug( LDAP_DEBUG_TRACE,
360                         "slap_passwd_parse: decoding error, len=%ld\n",
361                         (long) len, 0, 0 );
362 #endif
363
364                 *text = "data decoding error";
365                 rc = LDAP_PROTOCOL_ERROR;
366         }
367
368 done:
369         return rc;
370 }
371
372 struct berval * slap_passwd_return(
373         struct berval           *cred )
374 {
375         int rc;
376         struct berval *bv = NULL;
377         BerElementBuffer berbuf;
378         /* opaque structure, size unknown but smaller than berbuf */
379         BerElement *ber = (BerElement *)&berbuf;
380
381         assert( cred != NULL );
382
383 #ifdef NEW_LOGGING
384         LDAP_LOG( OPERATION, ENTRY, 
385                 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
386 #else
387         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
388                 (long) cred->bv_len, 0, 0 );
389 #endif
390         
391         ber_init_w_nullc( ber, LBER_USE_DER );
392
393         rc = ber_printf( ber, "{tON}",
394                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
395
396         if( rc >= 0 ) {
397                 (void) ber_flatten( ber, &bv );
398         }
399
400         ber_free_buf( ber );
401
402         return bv;
403 }
404
405 int
406 slap_passwd_check(
407         Connection *conn,
408         Attribute *a,
409         struct berval *cred,
410         const char **text )
411 {
412         int result = 1;
413         struct berval *bv;
414
415 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
416         ldap_pvt_thread_mutex_lock( &passwd_mutex );
417 #ifdef SLAPD_SPASSWD
418         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
419 #endif
420 #endif
421
422         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
423                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
424                         result = 0;
425                         break;
426                 }
427         }
428
429 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
430 #ifdef SLAPD_SPASSWD
431         lutil_passwd_sasl_conn = NULL;
432 #endif
433         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
434 #endif
435
436         return result;
437 }
438
439 void
440 slap_passwd_generate( struct berval *pass )
441 {
442 #ifdef NEW_LOGGING
443         LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
444 #else
445         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
446 #endif
447         pass->bv_val = NULL;
448         pass->bv_len = 0;
449
450         /*
451          * generate passwords of only 8 characters as some getpass(3)
452          * implementations truncate at 8 characters.
453          */
454         lutil_passwd_generate( pass, 8 );
455 }
456
457 void
458 slap_passwd_hash_type(
459         struct berval * cred,
460         struct berval * new,
461         char *hash,
462         const char **text )
463 {
464         new->bv_len = 0;
465         new->bv_val = NULL;
466
467         assert( hash );
468
469 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
470         ldap_pvt_thread_mutex_lock( &passwd_mutex );
471 #endif
472
473         lutil_passwd_hash( cred , hash, new, text );
474         
475 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
476         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
477 #endif
478
479 }
480 void
481 slap_passwd_hash(
482         struct berval * cred,
483         struct berval * new,
484         const char **text )
485 {
486         char *hash = NULL;
487         if ( default_passwd_hash ) {
488                 hash = default_passwd_hash[0];
489         }
490         if ( !hash ) {
491                 hash = (char *)defhash[0];
492         }
493
494         slap_passwd_hash_type( cred, new, hash, text );
495 }