]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
d1f7cdcd32ebb78a09c738242437615f5c46acd8
[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 ( op->o_bd->syncinfo ) {
61                         defref = op->o_bd->syncinfo->provideruri_bv;
62                 } else
63                 {
64                         defref = referral_rewrite( op->o_bd->be_update_refs,
65                                 NULL, NULL, LDAP_SCOPE_DEFAULT );
66                 }
67                 rs->sr_ref = defref;
68                 rs->sr_err = LDAP_REFERRAL;
69 #endif /* !SLAPD_MULTIMASTER */
70
71         } else {
72                 rs->sr_err = op->o_bd->be_extended( op, rs );
73         }
74
75         return rs->sr_err;
76 }
77
78 int slap_passwd_parse( struct berval *reqdata,
79         struct berval *id,
80         struct berval *oldpass,
81         struct berval *newpass,
82         const char **text )
83 {
84         int rc = LDAP_SUCCESS;
85         ber_tag_t tag;
86         ber_len_t len;
87         char berbuf[LBER_ELEMENT_SIZEOF];
88         BerElement *ber = (BerElement *)berbuf;
89
90         if( reqdata == NULL ) {
91                 return LDAP_SUCCESS;
92         }
93
94         if( reqdata->bv_len == 0 ) {
95                 *text = "empty request data field";
96                 return LDAP_PROTOCOL_ERROR;
97         }
98
99         /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
100         ber_init2( ber, reqdata, 0 );
101
102         tag = ber_scanf( ber, "{" /*}*/ );
103
104         if( tag != LBER_ERROR ) {
105                 tag = ber_peek_tag( ber, &len );
106         }
107
108         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
109                 if( id == NULL ) {
110 #ifdef NEW_LOGGING
111                         LDAP_LOG( OPERATION, ERR,
112                            "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
113 #else
114                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
115                                 0, 0, 0 );
116 #endif
117
118                         *text = "user must change own password";
119                         rc = LDAP_UNWILLING_TO_PERFORM;
120                         goto done;
121                 }
122
123                 tag = ber_scanf( ber, "m", id );
124
125                 if( tag == LBER_ERROR ) {
126 #ifdef NEW_LOGGING
127                         LDAP_LOG( OPERATION, ERR,
128                            "slap_passwd_parse:  ID parse failed.\n", 0, 0, 0 );
129 #else
130                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
131                                 0, 0, 0 );
132 #endif
133
134                         goto decoding_error;
135                 }
136
137                 tag = ber_peek_tag( ber, &len);
138         }
139
140         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
141                 if( oldpass == NULL ) {
142 #ifdef NEW_LOGGING
143                         LDAP_LOG( OPERATION, ERR,
144                            "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
145 #else
146                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
147                                 0, 0, 0 );
148 #endif
149
150                         *text = "use bind to verify old password";
151                         rc = LDAP_UNWILLING_TO_PERFORM;
152                         goto done;
153                 }
154
155                 tag = ber_scanf( ber, "m", oldpass );
156
157                 if( tag == LBER_ERROR ) {
158 #ifdef NEW_LOGGING
159                         LDAP_LOG( OPERATION, ERR,
160                            "slap_passwd_parse:  ID parse failed.\n" , 0, 0, 0 );
161 #else
162                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
163                                 0, 0, 0 );
164 #endif
165
166                         goto decoding_error;
167                 }
168
169                 tag = ber_peek_tag( ber, &len );
170         }
171
172         if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
173                 if( newpass == NULL ) {
174 #ifdef NEW_LOGGING
175                         LDAP_LOG( OPERATION, ERR,
176                            "slap_passwd_parse:  NEW not allowed.\n", 0, 0, 0 );
177 #else
178                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
179                                 0, 0, 0 );
180 #endif
181
182                         *text = "user specified passwords disallowed";
183                         rc = LDAP_UNWILLING_TO_PERFORM;
184                         goto done;
185                 }
186
187                 tag = ber_scanf( ber, "m", newpass );
188
189                 if( tag == LBER_ERROR ) {
190 #ifdef NEW_LOGGING
191                         LDAP_LOG( OPERATION, ERR,
192                            "slap_passwd_parse:  OLD parse failed.\n", 0, 0, 0 );
193 #else
194                         Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
195                                 0, 0, 0 );
196 #endif
197
198                         goto decoding_error;
199                 }
200
201                 tag = ber_peek_tag( ber, &len );
202         }
203
204         if( len != 0 ) {
205 decoding_error:
206 #ifdef NEW_LOGGING
207                 LDAP_LOG( OPERATION, ERR, 
208                         "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
209 #else
210                 Debug( LDAP_DEBUG_TRACE,
211                         "slap_passwd_parse: decoding error, len=%ld\n",
212                         (long) len, 0, 0 );
213 #endif
214
215                 *text = "data decoding error";
216                 rc = LDAP_PROTOCOL_ERROR;
217         }
218
219 done:
220         return rc;
221 }
222
223 struct berval * slap_passwd_return(
224         struct berval           *cred )
225 {
226         int rc;
227         struct berval *bv = NULL;
228         char berbuf[LBER_ELEMENT_SIZEOF];
229         /* opaque structure, size unknown but smaller than berbuf */
230         BerElement *ber = (BerElement *)berbuf;
231
232         assert( cred != NULL );
233
234 #ifdef NEW_LOGGING
235         LDAP_LOG( OPERATION, ENTRY, 
236                 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
237 #else
238         Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
239                 (long) cred->bv_len, 0, 0 );
240 #endif
241         
242         ber_init_w_nullc( ber, LBER_USE_DER );
243
244         rc = ber_printf( ber, "{tON}",
245                 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
246
247         if( rc >= 0 ) {
248                 (void) ber_flatten( ber, &bv );
249         }
250
251         ber_free_buf( ber );
252
253         return bv;
254 }
255
256 int
257 slap_passwd_check(
258         Connection *conn,
259         Attribute *a,
260         struct berval *cred,
261         const char **text )
262 {
263         int result = 1;
264         struct berval *bv;
265
266 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
267         ldap_pvt_thread_mutex_lock( &passwd_mutex );
268 #ifdef SLAPD_SPASSWD
269         lutil_passwd_sasl_conn = conn->c_sasl_authctx;
270 #endif
271 #endif
272
273         for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
274                 if( !lutil_passwd( bv, cred, NULL, text ) ) {
275                         result = 0;
276                         break;
277                 }
278         }
279
280 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
281 #ifdef SLAPD_SPASSWD
282         lutil_passwd_sasl_conn = NULL;
283 #endif
284         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
285 #endif
286
287         return result;
288 }
289
290 void
291 slap_passwd_generate( struct berval *pass )
292 {
293         struct berval *tmp;
294 #ifdef NEW_LOGGING
295         LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
296 #else
297         Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
298 #endif
299         /*
300          * generate passwords of only 8 characters as some getpass(3)
301          * implementations truncate at 8 characters.
302          */
303         tmp = lutil_passwd_generate( 8 );
304         if (tmp) {
305                 *pass = *tmp;
306                 free(tmp);
307         } else {
308                 pass->bv_val = NULL;
309                 pass->bv_len = 0;
310         }
311 }
312
313 void
314 slap_passwd_hash(
315         struct berval * cred,
316         struct berval * new,
317         const char **text )
318 {
319         struct berval *tmp;
320 #ifdef LUTIL_SHA1_BYTES
321         char* hash = default_passwd_hash ?  default_passwd_hash : "{SSHA}";
322 #else
323         char* hash = default_passwd_hash ?  default_passwd_hash : "{SMD5}";
324 #endif
325         
326
327 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
328         ldap_pvt_thread_mutex_lock( &passwd_mutex );
329 #endif
330
331         tmp = lutil_passwd_hash( cred , hash, text );
332         
333 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
334         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
335 #endif
336
337         if( tmp == NULL ) {
338                 new->bv_len = 0;
339                 new->bv_val = NULL;
340         }
341
342         *new = *tmp;
343         free( tmp );
344         return;
345 }