Fix changes pulled from v2.1.x for new API
[freeradius.git] / src / modules / rlm_exec / rlm_exec.c
1 /*
2  * rlm_exec.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2002,2006  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 /*
31  *      Define a structure for our module configuration.
32  */
33 typedef struct rlm_exec_t {
34         char    *xlat_name;
35         int     bare;
36         int     wait;
37         char    *program;
38         char    *input;
39         char    *output;
40         char    *packet_type;
41         unsigned int    packet_code;
42         int     shell_escape;
43 } rlm_exec_t;
44
45 /*
46  *      A mapping of configuration file names to internal variables.
47  *
48  *      Note that the string is dynamically allocated, so it MUST
49  *      be freed.  When the configuration file parse re-reads the string,
50  *      it free's the old one, and strdup's the new one, placing the pointer
51  *      to the strdup'd string into 'config.string'.  This gets around
52  *      buffer over-flows.
53  */
54 static const CONF_PARSER module_config[] = {
55         { "wait", PW_TYPE_BOOLEAN,  offsetof(rlm_exec_t,wait), NULL, "yes" },
56         { "program",  PW_TYPE_STRING_PTR,
57           offsetof(rlm_exec_t,program), NULL, NULL },
58         { "input_pairs", PW_TYPE_STRING_PTR,
59           offsetof(rlm_exec_t,input), NULL, "request" },
60         { "output_pairs",  PW_TYPE_STRING_PTR,
61           offsetof(rlm_exec_t,output), NULL, NULL },
62         { "packet_type", PW_TYPE_STRING_PTR,
63           offsetof(rlm_exec_t,packet_type), NULL, NULL },
64         { "shell_escape", PW_TYPE_BOOLEAN,  offsetof(rlm_exec_t,shell_escape), NULL, "yes" },
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         if (strcmp(string, "none") == 0) {
108                 return NULL;
109         }
110
111         return NULL;
112 }
113
114
115 /*
116  *      Do xlat of strings.
117  */
118 static size_t exec_xlat(void *instance, REQUEST *request,
119                      char *fmt, char *out, size_t outlen,
120                      UNUSED RADIUS_ESCAPE_STRING func)
121 {
122         int             result;
123         rlm_exec_t      *inst = instance;
124         VALUE_PAIR      **input_pairs;
125         char *p;
126
127         input_pairs = decode_string(request, inst->input);
128         if (!input_pairs) {
129                 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
130                        inst->xlat_name);
131                 out[0] = '\0';
132                 return 0;
133         }
134
135         /*
136          *      FIXME: Do xlat of program name?
137          */
138         RDEBUG2("Executing %s", fmt);
139         result = radius_exec_program(fmt, request, inst->wait,
140                                      out, outlen, *input_pairs, NULL, inst->shell_escape);
141         RDEBUG2("result %d", result);
142         if (result != 0) {
143                 out[0] = '\0';
144                 return 0;
145         }
146
147         for (p = out; *p != '\0'; p++) {
148                 if (*p < ' ') *p = ' ';
149         }
150
151         return strlen(out);
152 }
153
154
155 /*
156  *      Detach an instance and free it's data.
157  */
158 static int exec_detach(void *instance)
159 {
160         rlm_exec_t      *inst = instance;
161
162         if (inst->xlat_name) {
163                 xlat_unregister(inst->xlat_name, exec_xlat);
164                 free(inst->xlat_name);
165         }
166
167         free(inst);
168         return 0;
169 }
170
171
172 /*
173  *      Do any per-module initialization that is separate to each
174  *      configured instance of the module.  e.g. set up connections
175  *      to external databases, read configuration files, set up
176  *      dictionary entries, etc.
177  *
178  *      If configuration information is given in the config section
179  *      that must be referenced in later calls, store a handle to it
180  *      in *instance otherwise put a null pointer there.
181  */
182 static int exec_instantiate(CONF_SECTION *conf, void **instance)
183 {
184         rlm_exec_t      *inst;
185         const char      *xlat_name;
186
187         /*
188          *      Set up a storage area for instance data
189          */
190
191         inst = rad_malloc(sizeof(rlm_exec_t));
192         if (!inst)
193                 return -1;
194         memset(inst, 0, sizeof(rlm_exec_t));
195
196         /*
197          *      If the configuration parameters can't be parsed, then
198          *      fail.
199          */
200         if (cf_section_parse(conf, inst, module_config) < 0) {
201                 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
202                 exec_detach(inst);
203                 return -1;
204         }
205
206         /*
207          *      No input pairs defined.  Why are we executing a program?
208          */
209         if (!inst->input) {
210                 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
211                 exec_detach(inst);
212                 return -1;
213         }
214
215         /*
216          *      Sanity check the config.  If we're told to NOT wait,
217          *      then the output pairs must not be defined.
218          */
219         if (!inst->wait &&
220             (inst->output != NULL)) {
221                 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
222                 exec_detach(inst);
223                 return -1;
224         }
225
226         /*
227          *      Get the packet type on which to execute
228          */
229         if (!inst->packet_type) {
230                 inst->packet_code = 0;
231         } else {
232                 DICT_VALUE      *dval;
233
234                 dval = dict_valbyname(PW_PACKET_TYPE, 0, inst->packet_type);
235                 if (!dval) {
236                         radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
237                         exec_detach(inst);
238                         return -1;
239                 }
240                 inst->packet_code = dval->value;
241         }
242
243         xlat_name = cf_section_name2(conf);
244         if (xlat_name == NULL) {
245                 xlat_name = cf_section_name1(conf);
246                 inst->bare = 1;
247         }
248         if (xlat_name){
249                 inst->xlat_name = strdup(xlat_name);
250                 xlat_register(xlat_name, exec_xlat, inst);
251         }
252
253         *instance = inst;
254
255         return 0;
256 }
257
258
259 /*
260  *  Dispatch an exec method
261  */
262 static int exec_dispatch(void *instance, REQUEST *request)
263 {
264         int result;
265         VALUE_PAIR **input_pairs, **output_pairs;
266         VALUE_PAIR *answer;
267         rlm_exec_t *inst = (rlm_exec_t *) instance;
268
269         /*
270          *      We need a program to execute.
271          */
272         if (!inst->program) {
273                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
274                        inst->xlat_name);
275                 return RLM_MODULE_FAIL;
276         }
277
278         /*
279          *      See if we're supposed to execute it now.
280          */
281         if (!((inst->packet_code == 0) ||
282               (request->packet->code == inst->packet_code) ||
283               (request->reply->code == inst->packet_code) ||
284               (request->proxy &&
285                (request->proxy->code == inst->packet_code)) ||
286               (request->proxy_reply &&
287                (request->proxy_reply->code == inst->packet_code)))) {
288                 RDEBUG2("Packet type is not %s.  Not executing.",
289                        inst->packet_type);
290                 return RLM_MODULE_NOOP;
291         }
292
293         /*
294          *      Decide what input/output the program takes.
295          */
296         input_pairs = decode_string(request, inst->input);
297         output_pairs = decode_string(request, inst->output);
298
299         if (!input_pairs) {
300                 RDEBUG2("WARNING: Possible parse error in %s",
301                         inst->input);
302                 return RLM_MODULE_NOOP;
303         }
304
305         /*
306          *      It points to the attribute list, but the attribute
307          *      list is empty.
308          */
309         if (!*input_pairs) {
310                 RDEBUG2("WARNING! Input pairs are empty.  No attributes will be passed to the script");
311         }
312
313         /*
314          *      This function does it's own xlat of the input program
315          *      to execute.
316          *
317          *      FIXME: if inst->program starts with %{, then
318          *      do an xlat ourselves.  This will allow us to do
319          *      program = %{Exec-Program}, which this module
320          *      xlat's into it's string value, and then the
321          *      exec program function xlat's it's string value
322          *      into something else.
323          */
324         result = radius_exec_program(inst->program, request,
325                                      inst->wait, NULL, 0,
326                                      *input_pairs, &answer, inst->shell_escape);
327         if (result < 0) {
328                 radlog(L_ERR, "rlm_exec (%s): External script failed",
329                        inst->xlat_name);
330                 return RLM_MODULE_FAIL;
331         }
332
333         /*
334          *      Move the answer over to the output pairs.
335          *
336          *      If we're not waiting, then there are no output pairs.
337          */
338         if (output_pairs) pairmove(output_pairs, &answer);
339
340         pairfree(&answer);
341
342         if (result == 0) {
343                 return RLM_MODULE_OK;
344         }
345         if (result > RLM_MODULE_NUMCODES) {
346                 return RLM_MODULE_FAIL;
347         }
348         return result-1;
349 }
350
351
352 /*
353  *      First, look for Exec-Program && Exec-Program-Wait.
354  *
355  *      Then, call exec_dispatch.
356  */
357 static int exec_postauth(void *instance, REQUEST *request)
358 {
359         int result;
360         int exec_wait = 0;
361         VALUE_PAIR *vp, *tmp;
362         rlm_exec_t *inst = (rlm_exec_t *) instance;
363
364         vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0);
365         if (vp) {
366                 exec_wait = 0;
367
368         } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0)) != NULL) {
369                 exec_wait = 1;
370         }
371         if (!vp) {
372                 if (!inst->program) return RLM_MODULE_NOOP;
373                 
374                 return exec_dispatch(instance, request);
375         }
376
377         tmp = NULL;
378         result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
379                                      NULL, 0, request->packet->vps, &tmp,
380                                      inst->shell_escape);
381
382         /*
383          *      Always add the value-pairs to the reply.
384          */
385         pairmove(&request->reply->vps, &tmp);
386         pairfree(&tmp);
387
388         if (result < 0) {
389                 /*
390                  *      Error. radius_exec_program() returns -1 on
391                  *      fork/exec errors.
392                  */
393                 tmp = pairmake("Reply-Message", "Access denied (external check failed)", T_OP_SET);
394                 pairadd(&request->reply->vps, tmp);
395
396                 RDEBUG2("Login incorrect (external check failed)");
397
398                 request->reply->code = PW_AUTHENTICATION_REJECT;
399                 return RLM_MODULE_REJECT;
400         }
401         if (result > 0) {
402                 /*
403                  *      Reject. radius_exec_program() returns >0
404                  *      if the exec'ed program had a non-zero
405                  *      exit status.
406                  */
407                 request->reply->code = PW_AUTHENTICATION_REJECT;
408                 RDEBUG2("Login incorrect (external check said so)");
409                 return RLM_MODULE_REJECT;
410         }
411
412         return RLM_MODULE_OK;
413 }
414
415 /*
416  *      First, look for Exec-Program && Exec-Program-Wait.
417  *
418  *      Then, call exec_dispatch.
419  */
420 static int exec_accounting(void *instance, REQUEST *request)
421 {
422         int result;
423         int exec_wait = 0;
424         VALUE_PAIR *vp;
425         rlm_exec_t *inst = (rlm_exec_t *) instance;
426
427         /*
428          *      The "bare" exec module takes care of handling
429          *      Exec-Program and Exec-Program-Wait.
430          */
431         if (!inst->bare) return exec_dispatch(instance, request);
432
433         vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0);
434         if (vp) {
435                 exec_wait = 0;
436
437         } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0)) != NULL) {
438                 exec_wait = 1;
439         }
440         if (!vp) return RLM_MODULE_NOOP;
441
442         result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
443                                      NULL, 0, request->packet->vps, NULL,
444                                      inst->shell_escape);
445         if (result != 0) {
446                 return RLM_MODULE_REJECT;
447         }
448
449         return RLM_MODULE_OK;
450 }
451
452 /*
453  *      The module name should be the only globally exported symbol.
454  *      That is, everything else should be 'static'.
455  *
456  *      If the module needs to temporarily modify it's instantiation
457  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
458  *      The server will then take care of ensuring that the module
459  *      is single-threaded.
460  */
461 module_t rlm_exec = {
462         RLM_MODULE_INIT,
463         "exec",                         /* Name */
464         RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
465         exec_instantiate,               /* instantiation */
466         exec_detach,                    /* detach */
467         {
468                 exec_dispatch,          /* authentication */
469                 exec_dispatch,          /* authorization */
470                 exec_dispatch,          /* pre-accounting */
471                 exec_accounting,        /* accounting */
472                 NULL,                   /* check simul */
473                 exec_dispatch,          /* pre-proxy */
474                 exec_dispatch,          /* post-proxy */
475                 exec_postauth           /* post-auth */
476 #ifdef WITH_COA
477                 , exec_dispatch,
478                 exec_dispatch
479 #endif
480         },
481 };