import from 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, 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 */
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          *      Sanity check the config.  If we're told to wait,
235          *      then the output pairs should be defined.
236          */
237         if (inst->wait &&
238             (inst->output == NULL)) {
239                 radlog(L_INFO, "rlm_exec: Wait=yes but no output defined. Did you mean output=none?");
240         }
241
242         /*
243          *      Get the packet type on which to execute
244          */
245         if (!inst->packet_type) {
246                 inst->packet_code = 0;
247         } else {
248                 DICT_VALUE      *dval;
249
250                 dval = dict_valbyname(PW_PACKET_TYPE, inst->packet_type);
251                 if (!dval) {
252                         radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
253                         exec_detach(inst);
254                         return -1;
255                 }
256                 inst->packet_code = dval->value;
257         }
258
259         xlat_name = cf_section_name2(conf);
260         if (xlat_name == NULL)
261                 xlat_name = cf_section_name1(conf);
262         if (xlat_name){
263                 inst->xlat_name = strdup(xlat_name);
264                 xlat_register(xlat_name, exec_xlat, inst);
265         }
266
267         *instance = inst;
268
269         return 0;
270 }
271
272
273 /*
274  *  Dispatch an exec method
275  */
276 static int exec_dispatch(void *instance, REQUEST *request)
277 {
278         int result;
279         VALUE_PAIR **input_pairs, **output_pairs;
280         VALUE_PAIR *answer;
281         rlm_exec_t *inst = (rlm_exec_t *) instance;
282
283         /*
284          *      We need a program to execute.
285          */
286         if (!inst->program) {
287                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
288                        inst->xlat_name);
289                 return RLM_MODULE_FAIL;
290         }
291
292         /*
293          *      See if we're supposed to execute it now.
294          */
295         if (!((inst->packet_code == 0) ||
296               (request->packet->code == inst->packet_code) ||
297               (request->reply->code == inst->packet_code) ||
298               (request->proxy &&
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;
305         }
306
307         /*
308          *      Decide what input/output the program takes.
309          */
310         input_pairs = decode_string(request, inst->input);
311         output_pairs = decode_string(request, inst->output);
312
313         /*
314          *      It points to the attribute list, but the attribute
315          *      list is empty.
316          */
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);
319         }
320
321         /*
322          *      This function does it's own xlat of the input program
323          *      to execute.
324          *
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.
331          */
332         result = radius_exec_program(inst->program, request,
333                                      inst->wait, NULL, 0,
334                                      *input_pairs, &answer);
335         if (result != 0) {
336                 radlog(L_ERR, "rlm_exec (%s): External script failed",
337                        inst->xlat_name);
338                 return RLM_MODULE_FAIL;
339         }
340
341         /*
342          *      Move the answer over to the output pairs.
343          *
344          *      If we're not waiting, then there are no output pairs.
345          */
346         if (output_pairs) pairmove(output_pairs, &answer);
347
348         pairfree(&answer);
349
350         return RLM_MODULE_OK;
351 }
352
353
354 /*
355  *      The module name should be the only globally exported symbol.
356  *      That is, everything else should be 'static'.
357  *
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.
362  */
363 module_t rlm_exec = {
364         "exec",                         /* Name */
365         RLM_TYPE_THREAD_SAFE,           /* type */
366         NULL,                           /* initialization */
367         exec_instantiate,               /* instantiation */
368         {
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 */
377         },
378         exec_detach,                    /* detach */
379         NULL,                           /* destroy */
380 };