Last stage of getting rlm_exec to work in multiple sections.
[freeradius.git] / src / modules / rlm_exec / rlm_exec.c
1 /*
2  * rlm_exe.c
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2002  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include "autoconf.h"
25 #include "libradius.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "radiusd.h"
32 #include "modules.h"
33 #include "conffile.h"
34
35 static const char rcsid[] = "$Id$";
36
37 /*
38  *      Define a structure for our module configuration.
39  */
40 typedef struct rlm_exec_t {
41         char    *xlat_name;
42         int     wait;
43         char    *program;
44         char    *input;
45         char    *output;
46 } rlm_exec_t;
47
48 /*
49  *      A mapping of configuration file names to internal variables.
50  *
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
55  *      buffer over-flows.
56  */
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 */
66 };
67
68
69 /*
70  *      Decode the configuration file string to a pointer to
71  *      a value-pair list in the REQUEST data structure.
72  */
73 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
74 {
75         if (!string) return NULL;
76
77         /*
78          *      Yuck.  We need a 'switch' over character strings
79          *      in C.
80          */
81         if (strcmp(string, "request") == 0) {
82                 return &request->packet->vps;
83         }
84
85         if (strcmp(string, "reply") == 0) {
86                 if (!request->reply) return NULL;
87
88                 return &request->reply->vps;
89         }
90
91         if (strcmp(string, "proxy-request") == 0) {
92                 if (!request->proxy) return NULL;
93
94                 return &request->proxy->vps;
95         }
96
97         if (strcmp(string, "proxy-reply") == 0) {
98                 if (!request->proxy_reply) return NULL;
99
100                 return &request->proxy_reply->vps;
101         }
102
103         if (strcmp(string, "config") == 0) {
104                 return &request->config_items;
105         }
106
107         return NULL;
108 }
109
110
111 /*
112  *      Do xlat of strings.
113  */ 
114 static int exec_xlat(void *instance, REQUEST *request,
115                      char *fmt, char *out, int outlen,
116                      RADIUS_ESCAPE_STRING func)
117 {
118         int             result;
119         rlm_exec_t      *inst = instance;
120         VALUE_PAIR      **input_pairs;
121
122         input_pairs = decode_string(request, inst->input);
123         if (!input_pairs) {
124                 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
125                        inst->xlat_name);
126                 out[0] = '\0';
127                 return 0;
128         }
129
130         /*
131          *      FIXME: Do xlat of program name?
132          */
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);
137         if (result != 0) {
138                 out[0] = '\0';
139                 return 0;
140         }
141
142         return strlen(out);
143 }
144
145
146 /*
147  *      Detach an instance and free it's data.
148  */
149 static int exec_detach(void *instance)
150 {
151         rlm_exec_t      *inst = instance;
152
153         if (inst->xlat_name) {
154                 xlat_unregister(inst->xlat_name, exec_xlat);
155                 free(inst->xlat_name);
156         }
157
158         /*
159          *  Free the strings.
160          */
161         if (inst->program) free(inst->program);
162         if (inst->input) free(inst->input);
163         if (inst->output) free(inst->output);
164
165         free(inst);
166         return 0;
167 }
168
169
170 /*
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.
175  *
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.
179  */
180 static int exec_instantiate(CONF_SECTION *conf, void **instance)
181 {
182         rlm_exec_t      *inst;
183         char            *xlat_name;
184         
185         /*
186          *      Set up a storage area for instance data
187          */
188         
189         inst = rad_malloc(sizeof(rlm_exec_t));
190         memset(inst, 0, sizeof(rlm_exec_t));
191                 
192         /*
193          *      If the configuration parameters can't be parsed, then
194          *      fail.
195          */
196         if (cf_section_parse(conf, inst, module_config) < 0) {
197                 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
198                 exec_detach(inst);
199                 return -1;
200         }
201         
202         /*
203          *      No input pairs defined.  Why are we executing a program?
204          */
205         if (!inst->input) {
206                 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
207                 exec_detach(inst);
208                 return -1;
209         }
210
211         /*
212          *      Sanity check the config.  If we're told to NOT wait,
213          *      then the output pairs must not be defined.
214          */
215         if (!inst->wait &&
216             (inst->output != NULL)) {
217                 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
218                 exec_detach(inst);
219                 return -1;
220         }
221
222         xlat_name = cf_section_name2(conf);
223         if (xlat_name == NULL) 
224                 xlat_name = cf_section_name1(conf);
225         if (xlat_name){ 
226                 inst->xlat_name = strdup(xlat_name);
227                 xlat_register(xlat_name, exec_xlat, inst); 
228         } 
229
230         *instance = inst;
231         
232         return 0;
233 }
234
235
236 /*
237  *  Dispatch an exec method
238  */
239 static int exec_dispatch(void *instance, REQUEST *request)
240 {
241         int result;
242         VALUE_PAIR **input_pairs, **output_pairs;
243         VALUE_PAIR *answer;
244         rlm_exec_t *inst = (rlm_exec_t *) instance;
245
246         /*
247          *      We need a program to execute.
248          */
249         if (!inst->program) {
250                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
251                        inst->xlat_name);
252                 return RLM_MODULE_FAIL;
253         }
254
255         /*
256          *      Decide what input/output the program takes.
257          */
258         input_pairs = decode_string(request, inst->input);
259         output_pairs = decode_string(request, inst->output);
260
261         /*
262          *      We need a place to store the returned VP's
263          */
264         if (!output_pairs) {
265                 radlog(L_ERR, "rlm_exec (%s): Nowhere to place output",
266                        inst->xlat_name);
267                 return RLM_MODULE_FAIL;
268         }
269         
270         /*
271          *      This function does it's own xlat of the input program
272          *      to execute.
273          *
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.
280          */
281         result = radius_exec_program(inst->program, request,
282                                      inst->wait, NULL, 0,
283                                      *input_pairs, &answer);
284         if (result != 0) {
285                 radlog(L_ERR, "rlm_exec (%s): External script failed",
286                        inst->xlat_name);
287                 return RLM_MODULE_FAIL;
288         }
289
290         /*
291          *      Move the answer over to the output pairs.
292          */
293         pairmove(output_pairs, &answer);
294
295         pairfree(&answer);
296
297         return RLM_MODULE_OK;
298 }
299
300
301 /*
302  *      The module name should be the only globally exported symbol.
303  *      That is, everything else should be 'static'.
304  *
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.
309  */
310 module_t rlm_exec = {
311         "exec",                         /* Name */
312         RLM_TYPE_THREAD_SAFE,           /* type */
313         NULL,                           /* initialization */
314         exec_instantiate,               /* instantiation */
315         {
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 */
324         },
325         exec_detach,                    /* detach */
326         NULL,                           /* destroy */
327 };