Add support for tags to remaining functions in lib/valuepair.c
[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 #ifdef WITH_PROXY
92         if (strcmp(string, "proxy-request") == 0) {
93                 if (!request->proxy) return NULL;
94
95                 return &request->proxy->vps;
96         }
97
98         if (strcmp(string, "proxy-reply") == 0) {
99                 if (!request->proxy_reply) return NULL;
100
101                 return &request->proxy_reply->vps;
102         }
103 #endif
104
105         if (strcmp(string, "config") == 0) {
106                 return &request->config_items;
107         }
108
109         if (strcmp(string, "none") == 0) {
110                 return NULL;
111         }
112
113         return NULL;
114 }
115
116
117 /*
118  *      Do xlat of strings.
119  */
120 static size_t exec_xlat(void *instance, REQUEST *request,
121                      const char *fmt, char *out, size_t outlen)
122 {
123         int             result;
124         rlm_exec_t      *inst = instance;
125         VALUE_PAIR      **input_pairs;
126         char *p;
127
128         
129         input_pairs = decode_string(request, inst->input);
130         if (!input_pairs) {
131                 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs"
132                        " for xlat", inst->xlat_name);
133                 out[0] = '\0';
134                 return 0;
135         }
136
137         /*
138          *      FIXME: Do xlat of program name?
139          */
140         RDEBUG2("Executing %s", fmt);
141         result = radius_exec_program(fmt, request, inst->wait,
142                                      out, outlen, *input_pairs, NULL, inst->shell_escape);
143         RDEBUG2("result %d", result);
144         if (result != 0) {
145                 out[0] = '\0';
146                 return 0;
147         }
148
149         for (p = out; *p != '\0'; p++) {
150                 if (*p < ' ') *p = ' ';
151         }
152
153         return strlen(out);
154 }
155
156
157 /*
158  *      Detach an instance and free it's data.
159  */
160 static int exec_detach(void *instance)
161 {
162         rlm_exec_t      *inst = instance;
163
164         if (inst->xlat_name) {
165                 xlat_unregister(inst->xlat_name, exec_xlat, instance);
166                 free(inst->xlat_name);
167         }
168
169         free(inst);
170         return 0;
171 }
172
173
174 /*
175  *      Do any per-module initialization that is separate to each
176  *      configured instance of the module.  e.g. set up connections
177  *      to external databases, read configuration files, set up
178  *      dictionary entries, etc.
179  *
180  *      If configuration information is given in the config section
181  *      that must be referenced in later calls, store a handle to it
182  *      in *instance otherwise put a null pointer there.
183  */
184 static int exec_instantiate(CONF_SECTION *conf, void **instance)
185 {
186         rlm_exec_t      *inst;
187         const char      *xlat_name;
188
189         /*
190          *      Set up a storage area for instance data
191          */
192
193         inst = rad_malloc(sizeof(rlm_exec_t));
194         if (!inst)
195                 return -1;
196         memset(inst, 0, sizeof(rlm_exec_t));
197         
198         xlat_name = cf_section_name2(conf);
199         if (xlat_name == NULL) {
200                 xlat_name = cf_section_name1(conf);
201                 inst->bare = 1;
202         }
203         if (xlat_name){
204                 inst->xlat_name = strdup(xlat_name);
205                 xlat_register(xlat_name, exec_xlat, inst);
206         }
207
208         /*
209          *      If the configuration parameters can't be parsed, then
210          *      fail.
211          */
212         if (cf_section_parse(conf, inst, module_config) < 0) {
213                 radlog(L_ERR, "rlm_exec (%s): Failed parsing the "
214                        "configuration", xlat_name);
215                 exec_detach(inst);
216                 return -1;
217         }
218
219         /*
220          *      No input pairs defined.  Why are we executing a program?
221          */
222         if (!inst->input) {
223                 radlog(L_ERR, "rlm_exec (%s): Must define input pairs for "
224                        "external program", xlat_name);
225                 exec_detach(inst);
226                 return -1;
227         }
228
229         /*
230          *      Sanity check the config.  If we're told to NOT wait,
231          *      then the output pairs must not be defined.
232          */
233         if (!inst->wait &&
234             (inst->output != NULL)) {
235                 radlog(L_ERR, "rlm_exec (%s): Cannot read output pairs if "
236                               "wait=no", xlat_name);
237                 exec_detach(inst);
238                 return -1;
239         }
240
241         /*
242          *      Get the packet type on which to execute
243          */
244         if (!inst->packet_type) {
245                 inst->packet_code = 0;
246         } else {
247                 DICT_VALUE      *dval;
248
249                 dval = dict_valbyname(PW_PACKET_TYPE, 0, inst->packet_type);
250                 if (!dval) {
251                         radlog(L_ERR, "rlm_exec (%s): Unknown packet type %s: "
252                                "See list of VALUEs for Packet-Type in "
253                                "share/dictionary", xlat_name,
254                                inst->packet_type);
255                         exec_detach(inst);
256                         return -1;
257                 }
258                 inst->packet_code = dval->value;
259         }
260
261         *instance = inst;
262
263         return 0;
264 }
265
266
267 /*
268  *  Dispatch an exec method
269  */
270 static int exec_dispatch(void *instance, REQUEST *request)
271 {
272         rlm_exec_t *inst = (rlm_exec_t *) instance;
273         int result;
274         VALUE_PAIR      **input_pairs, **output_pairs;
275         VALUE_PAIR      *answer = NULL;
276         char            msg[1024];
277         size_t          len;
278
279         /*
280          *      We need a program to execute.
281          */
282         if (!inst->program) {
283                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
284                        inst->xlat_name);
285                 return RLM_MODULE_FAIL;
286         }
287
288         /*
289          *      See if we're supposed to execute it now.
290          */
291         if (!((inst->packet_code == 0) ||
292               (request->packet->code == inst->packet_code) ||
293               (request->reply->code == inst->packet_code)
294 #ifdef WITH_PROXY
295               || (request->proxy &&
296                (request->proxy->code == inst->packet_code)) ||
297               (request->proxy_reply &&
298                (request->proxy_reply->code == inst->packet_code))
299 #endif
300                     )) {
301                 RDEBUG2("Packet type is not %s.  Not executing.",
302                        inst->packet_type);
303                 return RLM_MODULE_NOOP;
304         }
305
306         /*
307          *      Decide what input/output the program takes.
308          */
309         input_pairs = decode_string(request, inst->input);
310         output_pairs = decode_string(request, inst->output);
311
312         if (!input_pairs) {
313                 RDEBUG2("WARNING: Possible parse error in %s",
314                         inst->input);
315                 return RLM_MODULE_NOOP;
316         }
317
318         /*
319          *      It points to the attribute list, but the attribute
320          *      list is empty.
321          */
322         if (!*input_pairs) {
323                 RDEBUG2("WARNING! Input pairs are empty.  No attributes will be passed to the script");
324         }
325
326         /*
327          *      This function does it's own xlat of the input program
328          *      to execute.
329          *
330          *      FIXME: if inst->program starts with %{, then
331          *      do an xlat ourselves.  This will allow us to do
332          *      program = %{Exec-Program}, which this module
333          *      xlat's into it's string value, and then the
334          *      exec program function xlat's it's string value
335          *      into something else.
336          */
337         result = radius_exec_program(inst->program, request,
338                                      inst->wait, msg, sizeof(msg),
339                                      *input_pairs, &answer, inst->shell_escape);
340         if (result < 0) {
341                 radlog(L_ERR, "rlm_exec (%s): External script failed",
342                        inst->xlat_name);
343                 return RLM_MODULE_FAIL;
344         }
345
346         /*
347          *      Move the answer over to the output pairs.
348          *
349          *      If we're not waiting, then there are no output pairs.
350          */
351         if (output_pairs) pairmove(output_pairs, &answer);
352
353         pairfree(&answer);
354
355         if (result == 0) {
356                 return RLM_MODULE_OK;
357         }
358         if (result > RLM_MODULE_NUMCODES) {
359                 return RLM_MODULE_FAIL;
360         }
361         
362         /*
363          *      Write any exec output to module failure message
364          */
365         if (*msg) {
366                 /* Trim off returns and newlines */
367                 len = strlen(msg);
368                 if (msg[len - 1] == '\n' || msg[len - 1] == '\r') {
369                         msg[len - 1] = '\0';
370                 }
371                 
372                 module_failure_msg(request, "rlm_exec (%s): %s",
373                                    inst->xlat_name, msg);
374         }
375         
376         return result-1;
377 }
378
379
380 /*
381  *      First, look for Exec-Program && Exec-Program-Wait.
382  *
383  *      Then, call exec_dispatch.
384  */
385 static int exec_postauth(void *instance, REQUEST *request)
386 {
387         int result;
388         int exec_wait = 0;
389         VALUE_PAIR *vp, *tmp;
390         rlm_exec_t *inst = (rlm_exec_t *) instance;
391
392         vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0, TAG_ANY);
393         if (vp) {
394                 exec_wait = 0;
395
396         } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0, TAG_ANY)) != NULL) {
397                 exec_wait = 1;
398         }
399         if (!vp) {
400                 if (!inst->program) return RLM_MODULE_NOOP;
401                 
402                 return exec_dispatch(instance, request);
403         }
404
405         tmp = NULL;
406         result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
407                                      NULL, 0, request->packet->vps, &tmp,
408                                      inst->shell_escape);
409
410         /*
411          *      Always add the value-pairs to the reply.
412          */
413         pairmove(&request->reply->vps, &tmp);
414         pairfree(&tmp);
415
416         if (result < 0) {
417                 RDEBUG2("%s", module_failure_msg(request, "rlm_exec (%s): "
418                                                  "Login incorrect (external "
419                                                  "check failed)",
420                                                  inst->xlat_name));
421
422                 request->reply->code = PW_AUTHENTICATION_REJECT;
423                 return RLM_MODULE_REJECT;
424         }
425         if (result > 0) {
426                 /*
427                  *      Reject. radius_exec_program() returns >0
428                  *      if the exec'ed program had a non-zero
429                  *      exit status.
430                  */
431                 request->reply->code = PW_AUTHENTICATION_REJECT;
432                 
433                 RDEBUG2("%s", module_failure_msg(request, "rlm_exec (%s): "
434                                                  "Login incorrect (external "
435                                                  "check said so)",
436                                                  inst->xlat_name));
437                 return RLM_MODULE_REJECT;
438         }
439
440         return RLM_MODULE_OK;
441 }
442
443 /*
444  *      First, look for Exec-Program && Exec-Program-Wait.
445  *
446  *      Then, call exec_dispatch.
447  */
448 static int exec_accounting(void *instance, REQUEST *request)
449 {
450         int result;
451         int exec_wait = 0;
452         VALUE_PAIR *vp;
453         rlm_exec_t *inst = (rlm_exec_t *) instance;
454
455         /*
456          *      The "bare" exec module takes care of handling
457          *      Exec-Program and Exec-Program-Wait.
458          */
459         if (!inst->bare) return exec_dispatch(instance, request);
460
461         vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM, 0, TAG_ANY);
462         if (vp) {
463                 exec_wait = 0;
464
465         } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT, 0, TAG_ANY)) != NULL) {
466                 exec_wait = 1;
467         }
468         if (!vp) return RLM_MODULE_NOOP;
469
470         result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
471                                      NULL, 0, request->packet->vps, NULL,
472                                      inst->shell_escape);
473         if (result != 0) {
474                 return RLM_MODULE_REJECT;
475         }
476
477         return RLM_MODULE_OK;
478 }
479
480 /*
481  *      The module name should be the only globally exported symbol.
482  *      That is, everything else should be 'static'.
483  *
484  *      If the module needs to temporarily modify it's instantiation
485  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
486  *      The server will then take care of ensuring that the module
487  *      is single-threaded.
488  */
489 module_t rlm_exec = {
490         RLM_MODULE_INIT,
491         "exec",                         /* Name */
492         RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
493         exec_instantiate,               /* instantiation */
494         exec_detach,                    /* detach */
495         {
496                 exec_dispatch,          /* authentication */
497                 exec_dispatch,          /* authorization */
498                 exec_dispatch,          /* pre-accounting */
499                 exec_accounting,        /* accounting */
500                 NULL,                   /* check simul */
501                 exec_dispatch,          /* pre-proxy */
502                 exec_dispatch,          /* post-proxy */
503                 exec_postauth           /* post-auth */
504 #ifdef WITH_COA
505                 , exec_dispatch,
506                 exec_dispatch
507 #endif
508         },
509 };