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