6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2002,2006 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/autoconf.h>
33 #include <freeradius-devel/radiusd.h>
34 #include <freeradius-devel/modules.h>
37 * Define a structure for our module configuration.
39 typedef struct rlm_exec_t {
46 unsigned int packet_code;
51 * A mapping of configuration file names to internal variables.
53 * Note that the string is dynamically allocated, so it MUST
54 * be freed. When the configuration file parse re-reads the string,
55 * it free's the old one, and strdup's the new one, placing the pointer
56 * to the strdup'd string into 'config.string'. This gets around
59 static const CONF_PARSER module_config[] = {
60 { "wait", PW_TYPE_BOOLEAN, offsetof(rlm_exec_t,wait), NULL, "yes" },
61 { "program", PW_TYPE_STRING_PTR,
62 offsetof(rlm_exec_t,program), NULL, NULL },
63 { "input_pairs", PW_TYPE_STRING_PTR,
64 offsetof(rlm_exec_t,input), NULL, "request" },
65 { "output_pairs", PW_TYPE_STRING_PTR,
66 offsetof(rlm_exec_t,output), NULL, NULL },
67 { "packet_type", PW_TYPE_STRING_PTR,
68 offsetof(rlm_exec_t,packet_type), NULL, NULL },
69 { "shell_escape", PW_TYPE_BOOLEAN, offsetof(rlm_exec_t,shell_escape), NULL, "yes" },
70 { NULL, -1, 0, NULL, NULL } /* end the list */
75 * Decode the configuration file string to a pointer to
76 * a value-pair list in the REQUEST data structure.
78 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
80 if (!string) return NULL;
83 * Yuck. We need a 'switch' over character strings
86 if (strcmp(string, "request") == 0) {
87 return &request->packet->vps;
90 if (strcmp(string, "reply") == 0) {
91 if (!request->reply) return NULL;
93 return &request->reply->vps;
96 if (strcmp(string, "proxy-request") == 0) {
97 if (!request->proxy) return NULL;
99 return &request->proxy->vps;
102 if (strcmp(string, "proxy-reply") == 0) {
103 if (!request->proxy_reply) return NULL;
105 return &request->proxy_reply->vps;
108 if (strcmp(string, "config") == 0) {
109 return &request->config_items;
112 if (strcmp(string, "none") == 0) {
121 * Do xlat of strings.
123 static int exec_xlat(void *instance, REQUEST *request,
124 char *fmt, char *out, size_t outlen,
125 UNUSED RADIUS_ESCAPE_STRING func)
128 rlm_exec_t *inst = instance;
129 VALUE_PAIR **input_pairs;
131 input_pairs = decode_string(request, inst->input);
133 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
140 * FIXME: Do xlat of program name?
142 DEBUG2("rlm_exec (%s): Executing %s", inst->xlat_name, fmt);
143 result = radius_exec_program(fmt, request, inst->wait,
144 out, outlen, *input_pairs, NULL, inst->shell_escape);
145 DEBUG2("rlm_exec (%s): result %d", inst->xlat_name, result);
156 * Detach an instance and free it's data.
158 static int exec_detach(void *instance)
160 rlm_exec_t *inst = instance;
162 if (inst->xlat_name) {
163 xlat_unregister(inst->xlat_name, exec_xlat);
164 free(inst->xlat_name);
173 * Do any per-module initialization that is separate to each
174 * configured instance of the module. e.g. set up connections
175 * to external databases, read configuration files, set up
176 * dictionary entries, etc.
178 * If configuration information is given in the config section
179 * that must be referenced in later calls, store a handle to it
180 * in *instance otherwise put a null pointer there.
182 static int exec_instantiate(CONF_SECTION *conf, void **instance)
185 const char *xlat_name;
188 * Set up a storage area for instance data
191 inst = rad_malloc(sizeof(rlm_exec_t));
194 memset(inst, 0, sizeof(rlm_exec_t));
197 * If the configuration parameters can't be parsed, then
200 if (cf_section_parse(conf, inst, module_config) < 0) {
201 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
207 * No input pairs defined. Why are we executing a program?
210 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
216 * Sanity check the config. If we're told to NOT wait,
217 * then the output pairs must not be defined.
220 (inst->output != NULL)) {
221 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
227 * Sanity check the config. If we're told to wait,
228 * then the output pairs should be defined.
231 (inst->output == NULL)) {
232 radlog(L_INFO, "rlm_exec: wait=yes but no output defined. Did you mean output=none?");
236 * Get the packet type on which to execute
238 if (!inst->packet_type) {
239 inst->packet_code = 0;
243 dval = dict_valbyname(PW_PACKET_TYPE, inst->packet_type);
245 radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
249 inst->packet_code = dval->value;
252 xlat_name = cf_section_name2(conf);
253 if (xlat_name == NULL)
254 xlat_name = cf_section_name1(conf);
256 inst->xlat_name = strdup(xlat_name);
257 xlat_register(xlat_name, exec_xlat, inst);
267 * Dispatch an exec method
269 static int exec_dispatch(void *instance, REQUEST *request)
272 VALUE_PAIR **input_pairs, **output_pairs;
274 rlm_exec_t *inst = (rlm_exec_t *) instance;
277 * We need a program to execute.
279 if (!inst->program) {
280 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
282 return RLM_MODULE_FAIL;
286 * See if we're supposed to execute it now.
288 if (!((inst->packet_code == 0) ||
289 (request->packet->code == inst->packet_code) ||
290 (request->reply->code == inst->packet_code) ||
292 (request->proxy->code == inst->packet_code)) ||
293 (request->proxy_reply &&
294 (request->proxy_reply->code == inst->packet_code)))) {
295 DEBUG2(" rlm_exec (%s): Packet type is not %s. Not executing.",
296 inst->xlat_name, inst->packet_type);
297 return RLM_MODULE_NOOP;
301 * Decide what input/output the program takes.
303 input_pairs = decode_string(request, inst->input);
304 output_pairs = decode_string(request, inst->output);
307 * It points to the attribute list, but the attribute
310 if (input_pairs && !*input_pairs) {
311 DEBUG2("rlm_exec (%s): WARNING! Input pairs are empty. No attributes will be passed to the script", inst->xlat_name);
315 * This function does it's own xlat of the input program
318 * FIXME: if inst->program starts with %{, then
319 * do an xlat ourselves. This will allow us to do
320 * program = %{Exec-Program}, which this module
321 * xlat's into it's string value, and then the
322 * exec program function xlat's it's string value
323 * into something else.
325 result = radius_exec_program(inst->program, request,
327 *input_pairs, &answer, inst->shell_escape);
329 radlog(L_ERR, "rlm_exec (%s): External script failed",
331 return RLM_MODULE_FAIL;
335 * Move the answer over to the output pairs.
337 * If we're not waiting, then there are no output pairs.
339 if (output_pairs) pairmove(output_pairs, &answer);
344 return RLM_MODULE_OK;
346 if (result > RLM_MODULE_NUMCODES) {
347 return RLM_MODULE_FAIL;
354 * First, look for Exec-Program && Exec-Program-Wait.
356 * Then, call exec_dispatch.
358 static int exec_postauth(void *instance, REQUEST *request)
362 VALUE_PAIR *vp, *tmp;
363 rlm_exec_t *inst = (rlm_exec_t *) instance;
365 vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM);
369 } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT)) != NULL) {
372 if (!vp) goto dispatch;
375 result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
376 NULL, 0, request->packet->vps, &tmp,
380 * Always add the value-pairs to the reply.
382 pairmove(&request->reply->vps, &tmp);
387 * Error. radius_exec_program() returns -1 on
390 tmp = pairmake("Reply-Message", "Access denied (external check failed)", T_OP_SET);
391 pairadd(&request->reply->vps, tmp);
393 DEBUG2("Login incorrect (external check failed)");
395 request->reply->code = PW_AUTHENTICATION_REJECT;
396 return RLM_MODULE_REJECT;
400 * Reject. radius_exec_program() returns >0
401 * if the exec'ed program had a non-zero
404 request->reply->code = PW_AUTHENTICATION_REJECT;
405 DEBUG2("Login incorrect (external check said so)");
406 return RLM_MODULE_REJECT;
410 if (!inst->program) return RLM_MODULE_NOOP;
412 return exec_dispatch(instance, request);
416 * The module name should be the only globally exported symbol.
417 * That is, everything else should be 'static'.
419 * If the module needs to temporarily modify it's instantiation
420 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
421 * The server will then take care of ensuring that the module
422 * is single-threaded.
424 module_t rlm_exec = {
427 RLM_TYPE_THREAD_SAFE, /* type */
428 exec_instantiate, /* instantiation */
429 exec_detach, /* detach */
431 exec_dispatch, /* authentication */
432 exec_dispatch, /* authorization */
433 exec_dispatch, /* pre-accounting */
434 exec_dispatch, /* accounting */
435 NULL, /* check simul */
436 exec_dispatch, /* pre-proxy */
437 exec_dispatch, /* post-proxy */
438 exec_postauth /* post-auth */