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 {
49 * A mapping of configuration file names to internal variables.
51 * Note that the string is dynamically allocated, so it MUST
52 * be freed. When the configuration file parse re-reads the string,
53 * it free's the old one, and strdup's the new one, placing the pointer
54 * to the strdup'd string into 'config.string'. This gets around
57 static CONF_PARSER module_config[] = {
58 { "wait", PW_TYPE_BOOLEAN, offsetof(rlm_exec_t,wait), NULL, "yes" },
59 { "program", PW_TYPE_STRING_PTR,
60 offsetof(rlm_exec_t,program), NULL, NULL },
61 { "input_pairs", PW_TYPE_STRING_PTR,
62 offsetof(rlm_exec_t,input), NULL, NULL },
63 { "output_pairs", PW_TYPE_STRING_PTR,
64 offsetof(rlm_exec_t,output), NULL, NULL },
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;
91 if (strcmp(string, "proxy-request") == 0) {
92 if (!request->proxy) return NULL;
94 return &request->proxy->vps;
97 if (strcmp(string, "proxy-reply") == 0) {
98 if (!request->proxy_reply) return NULL;
100 return &request->proxy_reply->vps;
103 if (strcmp(string, "config") == 0) {
104 return &request->config_items;
112 * Do xlat of strings.
114 static int exec_xlat(void *instance, REQUEST *request,
115 char *fmt, char *out, int outlen,
116 RADIUS_ESCAPE_STRING func)
119 rlm_exec_t *inst = instance;
120 VALUE_PAIR **input_pairs;
122 input_pairs = decode_string(request, inst->input);
124 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
131 * FIXME: Do xlat of program name?
133 DEBUG2("rlm_exec (%s): Executing %s", inst->xlat_name, fmt);
134 result = radius_exec_program(fmt, request, inst->wait,
135 out, outlen, *input_pairs, NULL);
136 DEBUG2("rlm_exec (%s): result %d", inst->xlat_name, result);
147 * Detach an instance and free it's data.
149 static int exec_detach(void *instance)
151 rlm_exec_t *inst = instance;
153 if (inst->xlat_name) {
154 xlat_unregister(inst->xlat_name, exec_xlat);
155 free(inst->xlat_name);
161 if (inst->program) free(inst->program);
162 if (inst->input) free(inst->input);
163 if (inst->output) free(inst->output);
171 * Do any per-module initialization that is separate to each
172 * configured instance of the module. e.g. set up connections
173 * to external databases, read configuration files, set up
174 * dictionary entries, etc.
176 * If configuration information is given in the config section
177 * that must be referenced in later calls, store a handle to it
178 * in *instance otherwise put a null pointer there.
180 static int exec_instantiate(CONF_SECTION *conf, void **instance)
186 * Set up a storage area for instance data
189 inst = rad_malloc(sizeof(rlm_exec_t));
190 memset(inst, 0, sizeof(rlm_exec_t));
193 * If the configuration parameters can't be parsed, then
196 if (cf_section_parse(conf, inst, module_config) < 0) {
197 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
203 * No input pairs defined. Why are we executing a program?
206 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
212 * Sanity check the config. If we're told to NOT wait,
213 * then the output pairs must not be defined.
216 (inst->output != NULL)) {
217 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
222 xlat_name = cf_section_name2(conf);
223 if (xlat_name == NULL)
224 xlat_name = cf_section_name1(conf);
226 inst->xlat_name = strdup(xlat_name);
227 xlat_register(xlat_name, exec_xlat, inst);
237 * Dispatch an exec method
239 static int exec_dispatch(void *instance, REQUEST *request)
242 VALUE_PAIR **input_pairs, **output_pairs;
244 rlm_exec_t *inst = (rlm_exec_t *) instance;
247 * We need a program to execute.
249 if (!inst->program) {
250 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
252 return RLM_MODULE_FAIL;
256 * Decide what input/output the program takes.
258 input_pairs = decode_string(request, inst->input);
259 output_pairs = decode_string(request, inst->output);
262 * We need a place to store the returned VP's
265 radlog(L_ERR, "rlm_exec (%s): Nowhere to place output",
267 return RLM_MODULE_FAIL;
271 * This function does it's own xlat of the input program
274 * FIXME: if inst->program starts with %{, then
275 * do an xlat ourselves. This will allow us to do
276 * program = %{Exec-Program}, which this module
277 * xlat's into it's string value, and then the
278 * exec program function xlat's it's string value
279 * into something else.
281 result = radius_exec_program(inst->program, request,
283 *input_pairs, &answer);
285 radlog(L_ERR, "rlm_exec (%s): External script failed",
287 return RLM_MODULE_FAIL;
291 * Move the answer over to the output pairs.
293 pairmove(output_pairs, &answer);
297 return RLM_MODULE_OK;
302 * The module name should be the only globally exported symbol.
303 * That is, everything else should be 'static'.
305 * If the module needs to temporarily modify it's instantiation
306 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
307 * The server will then take care of ensuring that the module
308 * is single-threaded.
310 module_t rlm_exec = {
312 RLM_TYPE_THREAD_SAFE, /* type */
313 NULL, /* initialization */
314 exec_instantiate, /* instantiation */
316 NULL, /* authentication */
317 exec_dispatch, /* authorization */
318 NULL, /* pre-accounting */
319 exec_dispatch, /* accounting */
320 NULL, /* check simul */
321 exec_dispatch, /* pre-proxy */
322 exec_dispatch, /* post-proxy */
323 exec_dispatch /* post-auth */
325 exec_detach, /* detach */