]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
228b631df7cf5675a66d5c8726c1d401d4c0c529
[openldap] / servers / slapd / passwd.c
1 /* bind.c - ldbm backend bind and unbind routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/krb.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #include "slap.h"
18
19 #include <lber_pvt.h>
20 #include <lutil.h>
21
22 int passwd_extop(
23         Operation *op,
24         SlapReply *rs )
25 {
26         assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
27
28         if( op->o_dn.bv_len == 0 ) {
29                 rs->sr_text = "only authenticated users may change passwords";
30                 return LDAP_STRONG_AUTH_REQUIRED;
31         }
32
33         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
34         op->o_bd = op->o_conn->c_authz_backend;
35         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
36
37         if( op->o_bd && !op->o_bd->be_extended ) {
38                 rs->sr_text = "operation not supported for current user";
39                 return LDAP_UNWILLING_TO_PERFORM;
40         }
41
42         if (backend_check_restrictions( op, rs,
43                         (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
44                 return rs->sr_err;
45         }
46
47         if( op->o_bd == NULL ) {
48 #ifdef HAVE_CYRUS_SASL
49                 rs->sr_err = slap_sasl_setpass( op, rs );
50 #else
51                 rs->sr_text = "no authz backend";
52                 rs->sr_err = LDAP_OTHER;
53 #endif
54
55 #ifndef SLAPD_MULTIMASTER
56         /* This does not apply to multi-master case */
57         } else if( op->o_bd->be_update_ndn.bv_len ) {
58                 /* we SHOULD return a referral in this case */
59                 BerVarray defref = NULL;
60                 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
61                         syncinfo_t *si;
62                         LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
63                                 struct berval tmpbv;
64                                 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
65                                 ber_bvarray_add( &defref, &tmpbv );
66                         }
67                 } else {
68                         defref = referral_rewrite( op->o_bd->be_update_refs,
69                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
70                 }
71                 rs->sr_ref = defref;
72                 rs->sr_err = LDAP_REFERRAL;
73 #endif /* !SLAPD_MULTIMASTER */
74
75         } else {
76                 rs->sr_err = op->o_bd->be_extended( op, rs );
77         }
78
79         return rs->sr_err;
80 }
81
82 int slap_passwd_parse( struct berval *reqdata,
83         struct berval *id,
84         struct berval *oldpass,
85         struct berval *newpass,
86         const char **text )
87 {
88         int rc = LDAP_SUCCESS;
89         ber_tag_t tag;
90         ber_len_t len;
91         BerElementBuffer berbuf;
92         BerElement *ber = (BerElement *)&berbuf;
93
94         if( reqdata == NULL ) {
95                 return LDAP_SUCCESS;
96         }
97
98         if( reqdata->bv_len == 0 ) {
99                 *text = "empty request data field";
100                 return LDAP_PROTOCOL_ERROR;
101         }
102
103         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
104         ber_init2( ber, reqdata, 0 );
105
106         tag = ber_scanf( ber, "{" /*}*/ );
107
108         if( tag != LBER_ERROR ) {
109                 tag = ber_peek_tag( ber, &len );
110         }
111
112         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
113                 if( id == NULL ) {
114 #ifdef NEW_LOGGING
115                         LDAP_LOG( OPERATION, ERR,
116                            "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
117 #else
118                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
119                                 0, 0, 0 );
120 #endif
121
122                         *text = "user must change own password";
123                         rc = LDAP_UNWILLING_TO_PERFORM;
124                         goto done;
125                 }
126
127                 tag = ber_scanf( ber, "m", id );
128
129                 if( tag == LBER_ERROR ) {
130 #ifdef NEW_LOGGING
131                         LDAP_LOG( OPERATION, ERR,
132                            "slap_passwd_parse:  ID parse failed.\n", 0, 0, 0 );
133 #else
134                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
135                                 0, 0, 0 );
136 #endif
137
138                         goto decoding_error;
139                 }
140
141                 tag = ber_peek_tag( ber, &len);
142         }
143
144         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
145                 if( oldpass == NULL ) {
146 #ifdef NEW_LOGGING
147                         LDAP_LOG( OPERATION, ERR,
148                            "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
149 #else
150                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
151                                 0, 0, 0 );
152 #endif
153
154                         *text = "use bind to verify old password";
155                         rc = LDAP_UNWILLING_TO_PERFORM;
156                         goto done;
157                 }
158
159                 tag = ber_scanf( ber, "m", oldpass );
160
161                 if( tag == LBER_ERROR ) {
162 #ifdef NEW_LOGGING
163                         LDAP_LOG( OPERATION, ERR,
164                            "slap_passwd_parse:  ID parse failed.\n" , 0, 0, 0 );
165 #else
166                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
167                                 0, 0, 0 );
168 #endif
169
170                         goto decoding_error;
171                 }
172
173                 tag = ber_peek_tag( ber, &len );
174         }
175
176         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
177                 if( newpass == NULL ) {
178 #ifdef NEW_LOGGING
179                         LDAP_LOG( OPERATION, ERR,
180                            "slap_passwd_parse:  NEW not allowed.\n", 0, 0, 0 );
181 #else
182                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
183                                 0, 0, 0 );
184 #endif
185
186                         *text = "user specified passwords disallowed";
187                         rc = LDAP_UNWILLING_TO_PERFORM;
188                         goto done;
189                 }
190
191                 tag = ber_scanf( ber, "m", newpass );
192
193                 if( tag == LBER_ERROR ) {
194 #ifdef NEW_LOGGING
195                         LDAP_LOG( OPERATION, ERR,
196                            "slap_passwd_parse:  OLD parse failed.\n", 0, 0, 0 );
197 #else
198                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
199                                 0, 0, 0 );
200 #endif
201
202                         goto decoding_error;
203                 }
204
205                 tag = ber_peek_tag( ber, &len );
206         }
207
208         if( len != 0 ) {
209 decoding_error:
210 #ifdef NEW_LOGGING
211                 LDAP_LOG( OPERATION, ERR, 
212                         "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
213 #else
214                 Debug( LDAP_DEBUG_TRACE,
215                         "slap_passwd_parse: decoding error, len=%ld\n",
216                         (long) len, 0, 0 );
217 #endif
218
219                 *text = "data decoding error";
220                 rc = LDAP_PROTOCOL_ERROR;
221         }
222
223 done:
224         return rc;
225 }
226
227 struct berval * slap_passwd_return(
228         struct berval           *cred )
229 {
230         int rc;
231         struct berval *bv = NULL;
232         BerElementBuffer berbuf;
233         /* opaque structure, size unknown but smaller than berbuf */
234         BerElement *ber = (BerElement *)&berbuf;
235
236         assert( cred != NULL );
237
238 #ifdef NEW_LOGGING
239         LDAP_LOG( OPERATION, ENTRY, 
240                 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
241 #else
242         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
243                 (long) cred->bv_len, 0, 0 );
244 #endif
245         
246         ber_init_w_nullc( ber, LBER_USE_DER );
247
248         rc = ber_printf( ber, "{tON}",
249                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
250
251         if( rc >= 0 ) {
252                 (void) ber_flatten( ber, &bv );
253         }
254
255         ber_free_buf( ber );
256
257         return bv;
258 }
259
260 int
261 slap_passwd_check(
262         Connection *conn,
263         Attribute *a,
264         struct berval *cred,
265         const char **text )
266 {
267         int result = 1;
268         struct berval *bv;
269
270 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
271         ldap_pvt_thread_mutex_lock( &passwd_mutex );
272 #ifdef SLAPD_SPASSWD
273         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
274 #endif
275 #endif
276
277         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
278                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
279                         result = 0;
280                         break;
281                 }
282         }
283
284 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
285 #ifdef SLAPD_SPASSWD
286         lutil_passwd_sasl_conn = NULL;
287 #endif
288         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
289 #endif
290
291         return result;
292 }
293
294 void
295 slap_passwd_generate( struct berval *pass )
296 {
297         struct berval *tmp;
298 #ifdef NEW_LOGGING
299         LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
300 #else
301         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
302 #endif
303         /*
304          * generate passwords of only 8 characters as some getpass(3)
305          * implementations truncate at 8 characters.
306          */
307         tmp = lutil_passwd_generate( 8 );
308         if (tmp) {
309                 *pass = *tmp;
310                 free(tmp);
311         } else {
312                 pass->bv_val = NULL;
313                 pass->bv_len = 0;
314         }
315 }
316
317 void
318 slap_passwd_hash(
319         struct berval * cred,
320         struct berval * new,
321         const char **text )
322 {
323         struct berval *tmp;
324 #ifdef LUTIL_SHA1_BYTES
325         char* hash = default_passwd_hash ?  default_passwd_hash : "{SSHA}";
326 #else
327         char* hash = default_passwd_hash ?  default_passwd_hash : "{SMD5}";
328 #endif
329         
330
331 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
332         ldap_pvt_thread_mutex_lock( &passwd_mutex );
333 #endif
334
335         tmp = lutil_passwd_hash( cred , hash, text );
336         
337 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
338         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
339 #endif
340
341         if( tmp == NULL ) {
342                 new->bv_len = 0;
343                 new->bv_val = NULL;
344         }
345
346         *new = *tmp;
347         free( tmp );
348         return;
349 }