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