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