2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
21 #include <lber_pvt.h> /* BER_BVC definition */
23 #include <ldap_pvt_thread.h>
24 #include <ac/string.h>
25 #include <ac/unistd.h>
29 extern char *global_host; /* from slapd */
30 static LUTIL_PASSWD_CHK_FUNC chk_radius;
31 static const struct berval scheme = BER_BVC("{RADIUS}");
32 static char *config_filename;
33 static ldap_pvt_thread_mutex_t libradius_mutex;
37 const struct berval *sc,
38 const struct berval *passwd,
39 const struct berval *cred,
43 int rc = LUTIL_PASSWD_ERR;
45 struct rad_handle *h = NULL;
47 for ( i = 0; i < cred->bv_len; i++ ) {
48 if ( cred->bv_val[ i ] == '\0' ) {
49 return LUTIL_PASSWD_ERR; /* NUL character in cred */
53 if ( cred->bv_val[ i ] != '\0' ) {
54 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
57 for ( i = 0; i < passwd->bv_len; i++ ) {
58 if ( passwd->bv_val[ i ] == '\0' ) {
59 return LUTIL_PASSWD_ERR; /* NUL character in password */
63 if ( passwd->bv_val[ i ] != '\0' ) {
64 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
67 ldap_pvt_thread_mutex_lock( &libradius_mutex );
71 ldap_pvt_thread_mutex_unlock( &libradius_mutex );
72 return LUTIL_PASSWD_ERR;
75 if ( rad_config( h, config_filename ) != 0 ) {
79 if ( rad_create_request( h, RAD_ACCESS_REQUEST ) ) {
83 if ( rad_put_string( h, RAD_USER_NAME, passwd->bv_val ) != 0 ) {
87 if ( rad_put_string( h, RAD_USER_PASSWORD, cred->bv_val ) != 0 ) {
91 if ( rad_put_string( h, RAD_NAS_IDENTIFIER, global_host ) != 0 ) {
95 switch ( rad_send_request( h ) ) {
96 case RAD_ACCESS_ACCEPT:
100 case RAD_ACCESS_REJECT:
101 rc = LUTIL_PASSWD_ERR;
104 case RAD_ACCESS_CHALLENGE:
105 rc = LUTIL_PASSWD_ERR;
109 /* no valid response is received */
116 ldap_pvt_thread_mutex_unlock( &libradius_mutex );
123 return ldap_pvt_thread_mutex_destroy( &libradius_mutex );
127 init_module( int argc, char *argv[] )
131 for ( i = 0; i < argc; i++ ) {
132 if ( strncasecmp( argv[ i ], "config=", STRLENOF( "config=" ) ) == 0 ) {
133 /* FIXME: what if multiple loads of same module?
134 * does it make sense (e.g. override an existing one)? */
135 if ( config_filename == NULL ) {
136 config_filename = ber_strdup( &argv[ i ][ STRLENOF( "config=" ) ] );
140 fprintf( stderr, "init_module(radius): unknown arg#%d=\"%s\".\n",
146 ldap_pvt_thread_mutex_init( &libradius_mutex );
148 return lutil_passwd_add( (struct berval *)&scheme, chk_radius, NULL );