-
Notifications
You must be signed in to change notification settings - Fork 1
/
rlm_two_factor.c
201 lines (183 loc) · 5.67 KB
/
rlm_two_factor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* rlm_two_factor.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2010 Wilson Felipe <[email protected]>
*/
#include <freeradius/ident.h>
#include <freeradius/radiusd.h>
#include <freeradius/modules.h>
#include "hotp.h"
/*
* Define a structure for our module configuration.
*
* These variables do not need to be in a structure, but it's
* a lot cleaner to do so, and a pointer to the structure can
* be used as the instance handle.
*/
typedef struct rlm_two_factor_t {
char *otpfile;
char *delim;
int challenge_length;
int offset;
} rlm_two_factor_t;
/*
* A mapping of configuration file names to internal variables.
*
* Note that the string is dynamically allocated, so it MUST
* be freed. When the configuration file parse re-reads the string,
* it free's the old one, and strdup's the new one, placing the pointer
* to the strdup'd string into 'config.string'. This gets around
* buffer over-flows.
*/
static const CONF_PARSER module_config[] = {
{"otpfile", PW_TYPE_STRING_PTR, offsetof(rlm_two_factor_t, otpfile),
NULL, "/etc/otpfile"},
{"delim", PW_TYPE_STRING_PTR, offsetof(rlm_two_factor_t, delim),
NULL, ":"},
{"challenge_length", PW_TYPE_INTEGER,
offsetof(rlm_two_factor_t, challenge_length), NULL, "6"},
{"offset", PW_TYPE_INTEGER, offsetof(rlm_two_factor_t, offset), NULL,
"3"},
{NULL, -1, 0, NULL, NULL} /* end the list */
};
/*
* Do any per-module initialization that is separate to each
* configured instance of the module. e.g. set up connections
* to external databases, read configuration files, set up
* dictionary entries, etc.
*
* If configuration information is given in the config section
* that must be referenced in later calls, store a handle to it
* in *instance otherwise put a null pointer there.
*/
static int two_factor_instantiate(CONF_SECTION * conf, void **instance)
{
rlm_two_factor_t *inst;
/*
* Set up a storage area for instance data
*/
inst = rad_malloc(sizeof(*inst));
if (!inst) {
return -1;
}
memset(inst, 0, sizeof(*inst));
/*
* If the configuration parameters can't be parsed, then
* fail.
*/
if (cf_section_parse(conf, inst, module_config) < 0) {
free(inst);
return -1;
}
*instance = inst;
return 0;
}
/*
* Authenticate the user with the given password.
*/
static int two_factor_authenticate(void *instance, REQUEST * request)
{
rlm_two_factor_t *inst = instance;
char passwd[MAX_STRING_LEN];
char challenge[MAX_STRING_LEN];
int clength = inst->challenge_length;
int retval;
if (!request->username || !request->password) {
radlog(L_AUTH,
"rlm_two_factor: Attribute User-Name or User-Password is missing and it's required for authentication.");
return RLM_MODULE_INVALID;
}
if (strlen(request->password->vp_strvalue) < clength) {
radlog(L_AUTH,
"rlm_two_factor: Attribute User-Password is lower than challenge length: %d.",
clength);
return RLM_MODULE_INVALID;
}
memcpy(challenge, request->password->vp_strvalue, clength);
memset(challenge + clength + 1, '\0', 1);
memcpy(passwd,
request->password->vp_strvalue + (sizeof(char) * clength),
strlen(request->password->vp_strvalue) - clength + 1);
strcpy(request->password->vp_strvalue, passwd);
/* check OTP */
retval = check_hotp
(inst->otpfile, inst->offset, clength,
request->username->vp_strvalue, atoi(challenge));
/*
* 0: wrong validation
* 1: validation was correct and return ok
* -1: could not read otpfile
* -2: user not found in otpfile
*/
switch (retval) {
case 1:
return RLM_MODULE_OK;
case 0:
radlog(L_AUTH,
"rlm_two_factor: validation of token for user %s was not correct.",
request->username->vp_strvalue);
return RLM_MODULE_REJECT;
case -1:
radlog(L_AUTH,
"rlm_two_factor: could not read otpfile %s.",
inst->otpfile);
return RLM_MODULE_REJECT;
case -2:
radlog(L_AUTH, "rlm_two_factor: user %s not found.",
request->username->vp_strvalue);
return RLM_MODULE_REJECT;
default:
radlog(L_AUTH, "rlm_two_factor: unknown error %d.", retval);
return RLM_MODULE_REJECT;
}
}
/*
* Only free memory we allocated. The strings allocated via
* cf_section_parse() do not need to be freed.
*/
static int two_factor_detach(void *instance)
{
free(instance);
return 0;
}
/*
* The module name should be the only globally exported symbol.
* That is, everything else should be 'static'.
*
* If the module needs to temporarily modify it's instantiation
* data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
* The server will then take care of ensuring that the module
* is single-threaded.
*/
module_t rlm_two_factor = {
RLM_MODULE_INIT,
"two_factor",
RLM_TYPE_THREAD_SAFE, /* type */
two_factor_instantiate, /* instantiation */
two_factor_detach, /* detach */
{
two_factor_authenticate, /* authentication */
NULL, /* authorization */
NULL, /* preaccounting */
NULL, /* accounting */
NULL, /* checksimul */
NULL, /* pre-proxy */
NULL, /* post-proxy */
NULL /* post-auth */
}
,
};