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