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