]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/radius.c
22aa461eaf094ccf298c3ec28a3212ad3e64f909
[openldap] / contrib / slapd-modules / passwd / radius.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2007 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
10  * A copy of this license is available in the file LICENSE in the
11  * top-level directory of the distribution or, alternatively, at
12  * <http://www.OpenLDAP.org/license.html>.
13  */
14
15 #include <stdio.h>
16
17 #include <lber.h>
18 #include <lber_pvt.h>   /* BER_BVC definition */
19 #include "lutil.h"
20 #include <ldap_pvt_thread.h>
21 #include <ac/string.h>
22 #include <ac/unistd.h>
23
24 #include <radlib.h>
25
26 static LUTIL_PASSWD_CHK_FUNC chk_radius;
27 static const struct berval scheme = BER_BVC("{RADIUS}");
28 static char *config_filename;
29 static ldap_pvt_thread_mutex_t libradius_mutex;
30
31 static int
32 chk_radius(
33         const struct berval     *sc,
34         const struct berval     *passwd,
35         const struct berval     *cred,
36         const char              **text )
37 {
38         unsigned int            i;
39         int                     rc = LUTIL_PASSWD_ERR;
40
41         struct rad_handle       *h = NULL;
42
43         for ( i = 0; i < cred->bv_len; i++ ) {
44                 if ( cred->bv_val[ i ] == '\0' ) {
45                         return LUTIL_PASSWD_ERR;        /* NUL character in cred */
46                 }
47         }
48
49         if ( cred->bv_val[ i ] != '\0' ) {
50                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
51         }
52
53         for ( i = 0; i < passwd->bv_len; i++ ) {
54                 if ( passwd->bv_val[ i ] == '\0' ) {
55                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
56                 }
57         }
58
59         if ( passwd->bv_val[ i ] != '\0' ) {
60                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
61         }
62
63         ldap_pvt_thread_mutex_lock( &libradius_mutex );
64
65         h = rad_auth_open();
66         if ( h == NULL ) {
67                 ldap_pvt_thread_mutex_unlock( &libradius_mutex );
68                 return LUTIL_PASSWD_ERR;
69         }
70
71         if ( rad_config( h, config_filename ) != 0 ) {
72                 goto done;
73         }
74
75         if ( rad_create_request( h, RAD_ACCESS_REQUEST ) ) {
76                 goto done;
77         }
78
79         if ( rad_put_string( h, RAD_USER_NAME, passwd->bv_val ) != 0 ) {
80                 goto done;
81         }
82
83         if ( rad_put_string( h, RAD_USER_PASSWORD, cred->bv_val ) != 0 ) {
84                 goto done;
85         }
86
87         switch ( rad_send_request( h ) ) {
88         case RAD_ACCESS_ACCEPT:
89                 rc = LUTIL_PASSWD_OK;
90                 break;
91
92         case RAD_ACCESS_REJECT:
93                 rc = LUTIL_PASSWD_ERR;
94                 break;
95
96         case RAD_ACCESS_CHALLENGE:
97                 rc = LUTIL_PASSWD_ERR;
98                 break;
99
100         case -1:
101                 /* no valid response is received */
102                 break;
103         }
104
105 done:;
106         rad_close( h );
107
108         ldap_pvt_thread_mutex_unlock( &libradius_mutex );
109         return rc;
110 }
111
112 int
113 term_module()
114 {
115         return ldap_pvt_thread_mutex_destroy( &libradius_mutex );
116 }
117
118 int
119 init_module( int argc, char *argv[] )
120 {
121         int     i;
122
123         for ( i = 0; i < argc; i++ ) {
124                 if ( strncasecmp( argv[ i ], "config=", STRLENOF( "config=" ) ) == 0 ) {
125                         /* FIXME: what if multiple loads of same module?
126                          * does it make sense (e.g. override an existing one)? */
127                         if ( config_filename == NULL ) {
128                                 config_filename = ber_strdup( &argv[ i ][ STRLENOF( "config=" ) ] );
129                         }
130
131                 } else {
132                         fprintf( stderr, "init_module(radius): unknown arg#%d=\"%s\".\n",
133                                 i, argv[ i ] );
134                         return 1;
135                 }
136         }
137
138         ldap_pvt_thread_mutex_init( &libradius_mutex );
139
140         return lutil_passwd_add( (struct berval *)&scheme, chk_radius, NULL );
141 }