]> git.sur5r.net Git - openldap/blob - servers/slapd/passwd.c
b06cd71e056fdad8cbe4c8a7dfcec2c68e5372f7
[openldap] / servers / slapd / passwd.c
1 /* bind.c - ldbm backend bind and unbind routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 <lutil.h>
20
21 static int passwd_main(
22         SLAP_EXTOP_CALLBACK_FN ext_callback,
23         Connection *conn, Operation *op, char *oid,
24         struct berval *reqdata, struct berval **rspdata, char **text )
25 {
26         int rc;
27         BerElement *ber;
28         struct berval *cred = NULL;
29         ber_int_t type;
30
31         assert( oid != NULL );
32         assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, oid ) == 0 );
33
34         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
35                 *text = ch_strdup("only authenicated users may change passwords");
36                 return LDAP_STRONG_AUTH_REQUIRED;
37         }
38
39         if( reqdata == NULL || reqdata->bv_len == 0 ) {
40                 *text = ch_strdup("data missing");
41                 return LDAP_PROTOCOL_ERROR;
42         }
43
44         ber = ber_init( reqdata );
45
46         if( ber == NULL ) {
47                 *text = ch_strdup("password decoding error");
48                 return LDAP_PROTOCOL_ERROR;
49         }
50
51         rc = ber_scanf(ber, "{iO}", &type, &cred );
52         ber_free( ber, 1 );
53
54         if( rc == LBER_ERROR ) {
55                 *text = ch_strdup("data decoding error");
56                 return LDAP_PROTOCOL_ERROR;
57         }
58
59         if( cred == NULL || cred->bv_len == 0 ) {
60                 *text = ch_strdup("password missing");
61                 return LDAP_PROTOCOL_ERROR;
62         }
63
64         if( type != 0 ) {
65                 ber_bvfree( cred );
66                 *text = ch_strdup("password type unknown");
67                 return LDAP_PROTOCOL_ERROR;
68         }
69
70         if( conn->c_authz_backend != NULL &&
71                 conn->c_authz_backend->be_extended )
72         {
73                 rc = conn->c_authz_backend->be_extended(
74                         conn->c_authz_backend,
75                         conn, op,
76                         oid, cred, rspdata, text );
77
78         } else {
79                 *text = ch_strdup("operation not supported for current user");
80                 rc = LDAP_UNWILLING_TO_PERFORM;
81         }
82
83         ber_bvfree( cred );
84         return rc;
85 }
86
87 int
88 slap_passwd_init( void )
89 {
90         return load_extop( LDAP_EXOP_X_MODIFY_PASSWD, passwd_main );
91 }
92
93 int
94 slap_passwd_check(
95         Attribute *a,
96         struct berval *cred )
97 {
98         int     i;
99         for ( i = 0; a->a_vals[i] != NULL; i++ ) {
100                 int result;
101
102 #ifdef SLAPD_CRYPT
103                 ldap_pvt_thread_mutex_lock( &crypt_mutex );
104 #endif
105
106                 result = lutil_passwd(
107                         a->a_vals[i]->bv_val,
108                         cred->bv_val,
109                         NULL );
110
111 #ifdef SLAPD_CRYPT
112                 ldap_pvt_thread_mutex_unlock( &crypt_mutex );
113 #endif
114
115                 return result;
116         }
117
118         return( 1 );
119 }
120
121 struct berval * slap_passwd_generate(
122         struct berval * cred )
123 {
124         char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
125
126         struct berval *new = ber_memalloc( sizeof(struct berval) );
127
128         if( new == NULL ) return NULL;
129
130 #ifdef SLAPD_CRYPT
131         ldap_pvt_thread_mutex_lock( &crypt_mutex );
132 #endif
133
134         new->bv_val = lutil_passwd_generate( cred->bv_val , hash );
135         
136 #ifdef SLAPD_CRYPT
137         ldap_pvt_thread_mutex_unlock( &crypt_mutex );
138 #endif
139
140         if( new->bv_val == NULL ) {
141                 ber_bvfree( new );
142                 return NULL;
143         }
144
145         new->bv_len = strlen( new->bv_val );
146
147         return new;
148 }