]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/passwd/radius.c
happy new year
[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 <ac/string.h>
21 #include <ac/unistd.h>
22
23 #include <radlib.h>
24
25 static LUTIL_PASSWD_CHK_FUNC chk_radius;
26 static const struct berval scheme = BER_BVC("{RADIUS}");
27 static char *config_filename;
28
29 static int
30 chk_radius(
31         const struct berval     *sc,
32         const struct berval     *passwd,
33         const struct berval     *cred,
34         const char              **text )
35 {
36         unsigned int            i;
37         int                     rc = LUTIL_PASSWD_ERR;
38
39         struct rad_handle       *h = NULL;
40
41         for ( i = 0; i < cred->bv_len; i++ ) {
42                 if ( cred->bv_val[ i ] == '\0' ) {
43                         return LUTIL_PASSWD_ERR;        /* NUL character in cred */
44                 }
45         }
46
47         if ( cred->bv_val[ i ] != '\0' ) {
48                 return LUTIL_PASSWD_ERR;        /* cred must behave like a string */
49         }
50
51         for ( i = 0; i < passwd->bv_len; i++ ) {
52                 if ( passwd->bv_val[ i ] == '\0' ) {
53                         return LUTIL_PASSWD_ERR;        /* NUL character in password */
54                 }
55         }
56
57         if ( passwd->bv_val[ i ] != '\0' ) {
58                 return LUTIL_PASSWD_ERR;        /* passwd must behave like a string */
59         }
60
61         h = rad_auth_open();
62         if ( h == NULL ) {
63                 return LUTIL_PASSWD_ERR;
64         }
65
66         if ( rad_config( h, config_filename ) != 0 ) {
67                 goto done;
68         }
69
70         if ( rad_create_request( h, RAD_ACCESS_REQUEST ) ) {
71                 goto done;
72         }
73
74         if ( rad_put_string( h, RAD_USER_NAME, passwd->bv_val ) != 0 ) {
75                 goto done;
76         }
77
78         if ( rad_put_string( h, RAD_USER_PASSWORD, cred->bv_val ) != 0 ) {
79                 goto done;
80         }
81
82         if ( rad_send_request( h ) == RAD_ACCESS_ACCEPT ) {
83                 rc = LUTIL_PASSWD_OK;
84         }
85
86 done:;
87         rad_close( h );
88
89         return rc;
90 }
91
92 int
93 init_module( int argc, char *argv[] )
94 {
95         int     i;
96
97         for ( i = 0; i < argc; i++ ) {
98                 if ( strncasecmp( argv[ i ], "config=", STRLENOF( "config=" ) ) == 0 ) {
99                         /* FIXME: what if multiple loads of same module?
100                          * does it make sense (e.g. override an existing one)? */
101                         if ( config_filename == NULL ) {
102                                 config_filename = ber_strdup( &argv[ i ][ STRLENOF( "config=" ) ] );
103                         }
104
105                 } else {
106                         fprintf( stderr, "init_module(radius): unknown arg#%d=\"%s\".\n",
107                                 i, argv[ i ] );
108                         return 1;
109                 }
110         }
111
112         return lutil_passwd_add( (struct berval *)&scheme, chk_radius, NULL );
113 }