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/radiusd.h>
28 #include <freeradius-devel/modules.h>
31 * Define a structure for our module configuration.
33 typedef struct rlm_exec_t {
41 unsigned int packet_code;
46 * A mapping of configuration file names to internal variables.
48 * Note that the string is dynamically allocated, so it MUST
49 * be freed. When the configuration file parse re-reads the string,
50 * it free's the old one, and strdup's the new one, placing the pointer
51 * to the strdup'd string into 'config.string'. This gets around
54 static const CONF_PARSER module_config[] = {
55 { "wait", PW_TYPE_BOOLEAN, offsetof(rlm_exec_t,wait), NULL, "yes" },
56 { "program", PW_TYPE_STRING_PTR,
57 offsetof(rlm_exec_t,program), NULL, NULL },
58 { "input_pairs", PW_TYPE_STRING_PTR,
59 offsetof(rlm_exec_t,input), NULL, "request" },
60 { "output_pairs", PW_TYPE_STRING_PTR,
61 offsetof(rlm_exec_t,output), NULL, NULL },
62 { "packet_type", PW_TYPE_STRING_PTR,
63 offsetof(rlm_exec_t,packet_type), NULL, NULL },
64 { "shell_escape", PW_TYPE_BOOLEAN, offsetof(rlm_exec_t,shell_escape), NULL, "yes" },
65 { NULL, -1, 0, NULL, NULL } /* end the list */
70 * Decode the configuration file string to a pointer to
71 * a value-pair list in the REQUEST data structure.
73 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
75 if (!string) return NULL;
78 * Yuck. We need a 'switch' over character strings
81 if (strcmp(string, "request") == 0) {
82 return &request->packet->vps;
85 if (strcmp(string, "reply") == 0) {
86 if (!request->reply) return NULL;
88 return &request->reply->vps;
92 if (strcmp(string, "proxy-request") == 0) {
93 if (!request->proxy) return NULL;
95 return &request->proxy->vps;
98 if (strcmp(string, "proxy-reply") == 0) {
99 if (!request->proxy_reply) return NULL;
101 return &request->proxy_reply->vps;
105 if (strcmp(string, "config") == 0) {
106 return &request->config_items;
109 if (strcmp(string, "none") == 0) {
118 * Do xlat of strings.
120 static size_t exec_xlat(void *instance, REQUEST *request,
121 char *fmt, char *out, size_t outlen,
122 UNUSED RADIUS_ESCAPE_STRING func)
125 rlm_exec_t *inst = instance;
126 VALUE_PAIR **input_pairs;
129 input_pairs = decode_string(request, inst->input);
131 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
138 * FIXME: Do xlat of program name?
140 RDEBUG2("Executing %s", fmt);
141 result = radius_exec_program(fmt, request, inst->wait,
142 out, outlen, *input_pairs, NULL, inst->shell_escape);
143 RDEBUG2("result %d", result);
149 for (p = out; *p != '\0'; p++) {
150 if (*p < ' ') *p = ' ';
158 * Detach an instance and free it's data.
160 static int exec_detach(void *instance)
162 rlm_exec_t *inst = instance;
164 if (inst->xlat_name) {
165 xlat_unregister(inst->xlat_name, exec_xlat);
166 free(inst->xlat_name);
175 * Do any per-module initialization that is separate to each
176 * configured instance of the module. e.g. set up connections
177 * to external databases, read configuration files, set up
178 * dictionary entries, etc.
180 * If configuration information is given in the config section
181 * that must be referenced in later calls, store a handle to it
182 * in *instance otherwise put a null pointer there.
184 static int exec_instantiate(CONF_SECTION *conf, void **instance)
187 const char *xlat_name;
190 * Set up a storage area for instance data
193 inst = rad_malloc(sizeof(rlm_exec_t));
196 memset(inst, 0, sizeof(rlm_exec_t));
199 * If the configuration parameters can't be parsed, then
202 if (cf_section_parse(conf, inst, module_config) < 0) {
203 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
209 * No input pairs defined. Why are we executing a program?
212 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
218 * Sanity check the config. If we're told to NOT wait,
219 * then the output pairs must not be defined.
222 (inst->output != NULL)) {
223 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
229 * Get the packet type on which to execute
231 if (!inst->packet_type) {
232 inst->packet_code = 0;
236 dval = dict_valbyname(PW_PACKET_TYPE, 0, inst->packet_type);
238 radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
242 inst->packet_code = dval->value;
245 xlat_name = cf_section_name2(conf);
246 if (xlat_name == NULL) {
247 xlat_name = cf_section_name1(conf);
251 inst->xlat_name = strdup(xlat_name);
252 xlat_register(xlat_name, exec_xlat, inst);
262 * Dispatch an exec method
264 static int exec_dispatch(void *instance, REQUEST *request)
267 VALUE_PAIR **input_pairs, **output_pairs;
269 rlm_exec_t *inst = (rlm_exec_t *) instance;
272 * We need a program to execute.
274 if (!inst->program) {
275 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
277 return RLM_MODULE_FAIL;
281 * See if we're supposed to execute it now.
283 if (!((inst->packet_code == 0) ||
284 (request->packet->code == inst->packet_code) ||
285 (request->reply->code == inst->packet_code)
287 || (request->proxy &&
288 (request->proxy->code == inst->packet_code)) ||
289 (request->proxy_reply &&
290 (request->proxy_reply->code == inst->packet_code))
293 RDEBUG2("Packet type is not %s. Not executing.",
295 return RLM_MODULE_NOOP;
299 * Decide what input/output the program takes.
301 input_pairs = decode_string(request, inst->input);
302 output_pairs = decode_string(request, inst->output);
305 RDEBUG2("WARNING: Possible parse error in %s",
307 return RLM_MODULE_NOOP;
311 * It points to the attribute list, but the attribute
315 RDEBUG2("WARNING! Input pairs are empty. No attributes will be passed to the script");
319 * This function does it's own xlat of the input program
322 * FIXME: if inst->program starts with %{, then
323 * do an xlat ourselves. This will allow us to do
324 * program = %{Exec-Program}, which this module
325 * xlat's into it's string value, and then the
326 * exec program function xlat's it's string value
327 * into something else.
329 result = radius_exec_program(inst->program, request,
331 *input_pairs, &answer, inst->shell_escape);
333 radlog(L_ERR, "rlm_exec (%s): External script failed",
335 return RLM_MODULE_FAIL;
339 * Move the answer over to the output pairs.
341 * If we're not waiting, then there are no output pairs.
343 if (output_pairs) pairmove(output_pairs, &answer);
348 return RLM_MODULE_OK;
350 if (result > RLM_MODULE_NUMCODES) {
351 return RLM_MODULE_FAIL;
358 * First, look for Exec-Program && Exec-Program-Wait.
360 * Then, call exec_dispatch.
362 static int exec_postauth(void *instance, REQUEST *request)
366 VALUE_PAIR *vp, *tmp;
367 rlm_exec_t *inst = (rlm_exec_t *) instance;
369 vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0);
373 } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0)) != NULL) {
377 if (!inst->program) return RLM_MODULE_NOOP;
379 return exec_dispatch(instance, request);
383 result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
384 NULL, 0, request->packet->vps, &tmp,
388 * Always add the value-pairs to the reply.
390 pairmove(&request->reply->vps, &tmp);
395 * Error. radius_exec_program() returns -1 on
398 tmp = pairmake("Reply-Message", "Access denied (external check failed)", T_OP_SET);
399 pairadd(&request->reply->vps, tmp);
401 RDEBUG2("Login incorrect (external check failed)");
403 request->reply->code = PW_AUTHENTICATION_REJECT;
404 return RLM_MODULE_REJECT;
408 * Reject. radius_exec_program() returns >0
409 * if the exec'ed program had a non-zero
412 request->reply->code = PW_AUTHENTICATION_REJECT;
413 RDEBUG2("Login incorrect (external check said so)");
414 return RLM_MODULE_REJECT;
417 return RLM_MODULE_OK;
421 * First, look for Exec-Program && Exec-Program-Wait.
423 * Then, call exec_dispatch.
425 static int exec_accounting(void *instance, REQUEST *request)
430 rlm_exec_t *inst = (rlm_exec_t *) instance;
433 * The "bare" exec module takes care of handling
434 * Exec-Program and Exec-Program-Wait.
436 if (!inst->bare) return exec_dispatch(instance, request);
438 vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0);
442 } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0)) != NULL) {
445 if (!vp) return RLM_MODULE_NOOP;
447 result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
448 NULL, 0, request->packet->vps, NULL,
451 return RLM_MODULE_REJECT;
454 return RLM_MODULE_OK;
458 * The module name should be the only globally exported symbol.
459 * That is, everything else should be 'static'.
461 * If the module needs to temporarily modify it's instantiation
462 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
463 * The server will then take care of ensuring that the module
464 * is single-threaded.
466 module_t rlm_exec = {
469 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
470 exec_instantiate, /* instantiation */
471 exec_detach, /* detach */
473 exec_dispatch, /* authentication */
474 exec_dispatch, /* authorization */
475 exec_dispatch, /* pre-accounting */
476 exec_accounting, /* accounting */
477 NULL, /* check simul */
478 exec_dispatch, /* pre-proxy */
479 exec_dispatch, /* post-proxy */
480 exec_postauth /* post-auth */