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