pull the patch from the head
[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         char    *packet_type;
47         int     packet_code;
48 } rlm_exec_t;
49
50 /*
51  *      A mapping of configuration file names to internal variables.
52  *
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
57  *      buffer over-flows.
58  */
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, "reply" },
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 */
70 };
71
72
73 /*
74  *      Decode the configuration file string to a pointer to
75  *      a value-pair list in the REQUEST data structure.
76  */
77 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
78 {
79         if (!string) return NULL;
80
81         /*
82          *      Yuck.  We need a 'switch' over character strings
83          *      in C.
84          */
85         if (strcmp(string, "request") == 0) {
86                 return &request->packet->vps;
87         }
88
89         if (strcmp(string, "reply") == 0) {
90                 if (!request->reply) return NULL;
91
92                 return &request->reply->vps;
93         }
94
95         if (strcmp(string, "proxy-request") == 0) {
96                 if (!request->proxy) return NULL;
97
98                 return &request->proxy->vps;
99         }
100
101         if (strcmp(string, "proxy-reply") == 0) {
102                 if (!request->proxy_reply) return NULL;
103
104                 return &request->proxy_reply->vps;
105         }
106
107         if (strcmp(string, "config") == 0) {
108                 return &request->config_items;
109         }
110
111         if (strcmp(string, "none") == 0) {
112                 return NULL;
113         }
114
115         return NULL;
116 }
117
118
119 /*
120  *      Do xlat of strings.
121  */ 
122 static int exec_xlat(void *instance, REQUEST *request,
123                      char *fmt, char *out, int outlen,
124                      RADIUS_ESCAPE_STRING func)
125 {
126         int             result;
127         rlm_exec_t      *inst = instance;
128         VALUE_PAIR      **input_pairs;
129
130         input_pairs = decode_string(request, inst->input);
131         if (!input_pairs) {
132                 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
133                        inst->xlat_name);
134                 out[0] = '\0';
135                 return 0;
136         }
137
138         /*
139          *      FIXME: Do xlat of program name?
140          */
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);
145         if (result != 0) {
146                 out[0] = '\0';
147                 return 0;
148         }
149
150         return strlen(out);
151 }
152
153
154 /*
155  *      Detach an instance and free it's data.
156  */
157 static int exec_detach(void *instance)
158 {
159         rlm_exec_t      *inst = instance;
160
161         if (inst->xlat_name) {
162                 xlat_unregister(inst->xlat_name, exec_xlat);
163                 free(inst->xlat_name);
164         }
165
166         /*
167          *  Free the strings.
168          */
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);
173
174         free(inst);
175         return 0;
176 }
177
178
179 /*
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.
184  *
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.
188  */
189 static int exec_instantiate(CONF_SECTION *conf, void **instance)
190 {
191         rlm_exec_t      *inst;
192         char            *xlat_name;
193         
194         /*
195          *      Set up a storage area for instance data
196          */
197         
198         inst = rad_malloc(sizeof(rlm_exec_t));
199         if (!inst)
200                 return -1;
201         memset(inst, 0, sizeof(rlm_exec_t));
202                 
203         /*
204          *      If the configuration parameters can't be parsed, then
205          *      fail.
206          */
207         if (cf_section_parse(conf, inst, module_config) < 0) {
208                 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
209                 exec_detach(inst);
210                 return -1;
211         }
212         
213         /*
214          *      No input pairs defined.  Why are we executing a program?
215          */
216         if (!inst->input) {
217                 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
218                 exec_detach(inst);
219                 return -1;
220         }
221
222         /*
223          *      Sanity check the config.  If we're told to NOT wait,
224          *      then the output pairs must not be defined.
225          */
226         if (!inst->wait &&
227             (inst->output != NULL)) {
228                 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
229                 exec_detach(inst);
230                 return -1;
231         }
232
233         /*
234          *      Get the packet type on which to execute
235          */
236         if (!inst->packet_type) {
237                 inst->packet_code = 0;
238         } else {
239                 DICT_VALUE      *dval;
240                 
241                 dval = dict_valbyname(PW_PACKET_TYPE, inst->packet_type);
242                 if (!dval) {
243                         radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
244                         exec_detach(inst);
245                         return -1;
246                 }
247                 inst->packet_code = dval->value;
248         }
249
250         xlat_name = cf_section_name2(conf);
251         if (xlat_name == NULL) 
252                 xlat_name = cf_section_name1(conf);
253         if (xlat_name){ 
254                 inst->xlat_name = strdup(xlat_name);
255                 xlat_register(xlat_name, exec_xlat, inst); 
256         } 
257
258         *instance = inst;
259         
260         return 0;
261 }
262
263
264 /*
265  *  Dispatch an exec method
266  */
267 static int exec_dispatch(void *instance, REQUEST *request)
268 {
269         int result;
270         VALUE_PAIR **input_pairs, **output_pairs;
271         VALUE_PAIR *answer;
272         rlm_exec_t *inst = (rlm_exec_t *) instance;
273
274         /*
275          *      We need a program to execute.
276          */
277         if (!inst->program) {
278                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
279                        inst->xlat_name);
280                 return RLM_MODULE_FAIL;
281         }
282
283         /*
284          *      See if we're supposed to execute it now.
285          */
286         if (!((inst->packet_code == 0) ||
287               (request->packet->code == inst->packet_code) ||
288               (request->reply->code == inst->packet_code) ||
289               (request->proxy &&
290                (request->proxy->code == inst->packet_code)) ||
291               (request->proxy_reply &&
292                (request->proxy_reply->code == inst->packet_code)))) {
293                 DEBUG2("  rlm_exec (%s): Packet type is not %s.  Not executing.",
294                        inst->xlat_name, inst->packet_type);
295                 return RLM_MODULE_NOOP;
296         }
297
298         /*
299          *      Decide what input/output the program takes.
300          */
301         input_pairs = decode_string(request, inst->input);
302         output_pairs = decode_string(request, inst->output);
303
304         /*
305          *      It points to the attribute list, but the attribute
306          *      list is empty.
307          */
308         if (input_pairs && !*input_pairs) {
309                 DEBUG2("rlm_exec (%s): WARNING! Input pairs are empty.  No attributes will be passed to the script", inst->xlat_name);
310         }
311
312         /*
313          *      This function does it's own xlat of the input program
314          *      to execute.
315          *
316          *      FIXME: if inst->program starts with %{, then
317          *      do an xlat ourselves.  This will allow us to do
318          *      program = %{Exec-Program}, which this module
319          *      xlat's into it's string value, and then the
320          *      exec program function xlat's it's string value
321          *      into something else.
322          */
323         result = radius_exec_program(inst->program, request,
324                                      inst->wait, NULL, 0,
325                                      *input_pairs, &answer);
326         if (result != 0) {
327                 radlog(L_ERR, "rlm_exec (%s): External script failed",
328                        inst->xlat_name);
329                 return RLM_MODULE_FAIL;
330         }
331
332         /*
333          *      Move the answer over to the output pairs.
334          *
335          *      If we're not waiting, then there are no output pairs.
336          */
337         if (output_pairs) pairmove(output_pairs, &answer);
338
339         pairfree(&answer);
340
341         return RLM_MODULE_OK;
342 }
343
344
345 /*
346  *      The module name should be the only globally exported symbol.
347  *      That is, everything else should be 'static'.
348  *
349  *      If the module needs to temporarily modify it's instantiation
350  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
351  *      The server will then take care of ensuring that the module
352  *      is single-threaded.
353  */
354 module_t rlm_exec = {
355         "exec",                         /* Name */
356         RLM_TYPE_THREAD_SAFE,           /* type */
357         NULL,                           /* initialization */
358         exec_instantiate,               /* instantiation */
359         {
360                 exec_dispatch,          /* authentication */
361                 exec_dispatch,          /* authorization */
362                 exec_dispatch,          /* pre-accounting */
363                 exec_dispatch,          /* accounting */
364                 NULL,                   /* check simul */
365                 exec_dispatch,          /* pre-proxy */
366                 exec_dispatch,          /* post-proxy */
367                 exec_dispatch           /* post-auth */
368         },
369         exec_detach,                    /* detach */
370         NULL,                           /* destroy */
371 };