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