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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2002 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
25 #include "libradius.h"
35 static const char rcsid[] = "$Id$";
38 * Define a structure for our module configuration.
40 typedef struct rlm_exec_t {
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 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 { NULL, -1, 0, NULL, NULL } /* end the list */
74 * Decode the configuration file string to a pointer to
75 * a value-pair list in the REQUEST data structure.
77 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
79 if (!string) return NULL;
82 * Yuck. We need a 'switch' over character strings
85 if (strcmp(string, "request") == 0) {
86 return &request->packet->vps;
89 if (strcmp(string, "reply") == 0) {
90 if (!request->reply) return NULL;
92 return &request->reply->vps;
95 if (strcmp(string, "proxy-request") == 0) {
96 if (!request->proxy) return NULL;
98 return &request->proxy->vps;
101 if (strcmp(string, "proxy-reply") == 0) {
102 if (!request->proxy_reply) return NULL;
104 return &request->proxy_reply->vps;
107 if (strcmp(string, "config") == 0) {
108 return &request->config_items;
111 if (strcmp(string, "none") == 0) {
120 * Do xlat of strings.
122 static int exec_xlat(void *instance, REQUEST *request,
123 char *fmt, char *out, int outlen,
124 RADIUS_ESCAPE_STRING func)
127 rlm_exec_t *inst = instance;
128 VALUE_PAIR **input_pairs;
130 input_pairs = decode_string(request, inst->input);
132 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
139 * FIXME: Do xlat of program name?
141 DEBUG2("rlm_exec (%s): Executing %s", inst->xlat_name, fmt);
142 result = radius_exec_program(fmt, request, inst->wait,
143 out, outlen, *input_pairs, NULL);
144 DEBUG2("rlm_exec (%s): result %d", inst->xlat_name, result);
155 * Detach an instance and free it's data.
157 static int exec_detach(void *instance)
159 rlm_exec_t *inst = instance;
161 if (inst->xlat_name) {
162 xlat_unregister(inst->xlat_name, exec_xlat);
163 free(inst->xlat_name);
169 if (inst->program) free(inst->program);
170 if (inst->input) free(inst->input);
171 if (inst->output) free(inst->output);
172 if (inst->packet_type) free(inst->packet_type);
180 * Do any per-module initialization that is separate to each
181 * configured instance of the module. e.g. set up connections
182 * to external databases, read configuration files, set up
183 * dictionary entries, etc.
185 * If configuration information is given in the config section
186 * that must be referenced in later calls, store a handle to it
187 * in *instance otherwise put a null pointer there.
189 static int exec_instantiate(CONF_SECTION *conf, void **instance)
195 * Set up a storage area for instance data
198 inst = rad_malloc(sizeof(rlm_exec_t));
201 memset(inst, 0, sizeof(rlm_exec_t));
204 * If the configuration parameters can't be parsed, then
207 if (cf_section_parse(conf, inst, module_config) < 0) {
208 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
214 * No input pairs defined. Why are we executing a program?
217 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
223 * Sanity check the config. If we're told to NOT wait,
224 * then the output pairs must not be defined.
227 (inst->output != NULL)) {
228 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
234 * Sanity check the config. If we're told to wait,
235 * then the output pairs should be defined.
238 (inst->output == NULL)) {
239 radlog(L_INFO, "rlm_exec: Wait=yes but no output defined. Did you mean output=none?");
243 * Get the packet type on which to execute
245 if (!inst->packet_type) {
246 inst->packet_code = 0;
250 dval = dict_valbyname(PW_PACKET_TYPE, inst->packet_type);
252 radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
256 inst->packet_code = dval->value;
259 xlat_name = cf_section_name2(conf);
260 if (xlat_name == NULL)
261 xlat_name = cf_section_name1(conf);
263 inst->xlat_name = strdup(xlat_name);
264 xlat_register(xlat_name, exec_xlat, inst);
274 * Dispatch an exec method
276 static int exec_dispatch(void *instance, REQUEST *request)
279 VALUE_PAIR **input_pairs, **output_pairs;
281 rlm_exec_t *inst = (rlm_exec_t *) instance;
284 * We need a program to execute.
286 if (!inst->program) {
287 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
289 return RLM_MODULE_FAIL;
293 * See if we're supposed to execute it now.
295 if (!((inst->packet_code == 0) ||
296 (request->packet->code == inst->packet_code) ||
297 (request->reply->code == inst->packet_code) ||
299 (request->proxy->code == inst->packet_code)) ||
300 (request->proxy_reply &&
301 (request->proxy_reply->code == inst->packet_code)))) {
302 DEBUG2(" rlm_exec (%s): Packet type is not %s. Not executing.",
303 inst->xlat_name, inst->packet_type);
304 return RLM_MODULE_NOOP;
308 * Decide what input/output the program takes.
310 input_pairs = decode_string(request, inst->input);
311 output_pairs = decode_string(request, inst->output);
314 * It points to the attribute list, but the attribute
317 if (input_pairs && !*input_pairs) {
318 DEBUG2("rlm_exec (%s): WARNING! Input pairs are empty. No attributes will be passed to the script", inst->xlat_name);
322 * This function does it's own xlat of the input program
325 * FIXME: if inst->program starts with %{, then
326 * do an xlat ourselves. This will allow us to do
327 * program = %{Exec-Program}, which this module
328 * xlat's into it's string value, and then the
329 * exec program function xlat's it's string value
330 * into something else.
332 result = radius_exec_program(inst->program, request,
334 *input_pairs, &answer);
336 radlog(L_ERR, "rlm_exec (%s): External script failed",
338 return RLM_MODULE_FAIL;
342 * Move the answer over to the output pairs.
344 * If we're not waiting, then there are no output pairs.
346 if (output_pairs) pairmove(output_pairs, &answer);
350 return RLM_MODULE_OK;
355 * The module name should be the only globally exported symbol.
356 * That is, everything else should be 'static'.
358 * If the module needs to temporarily modify it's instantiation
359 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
360 * The server will then take care of ensuring that the module
361 * is single-threaded.
363 module_t rlm_exec = {
365 RLM_TYPE_THREAD_SAFE, /* type */
366 NULL, /* initialization */
367 exec_instantiate, /* instantiation */
369 exec_dispatch, /* authentication */
370 exec_dispatch, /* authorization */
371 exec_dispatch, /* pre-accounting */
372 exec_dispatch, /* accounting */
373 NULL, /* check simul */
374 exec_dispatch, /* pre-proxy */
375 exec_dispatch, /* post-proxy */
376 exec_dispatch /* post-auth */
378 exec_detach, /* detach */