Pulled from branch_1_1
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2002  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/autoconf.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <freeradius-devel/radiusd.h>
31 #include <freeradius-devel/modules.h>
32 #include <freeradius-devel/conffile.h>
33
34 static const char rcsid[] = "$Id$";
35
36 /*
37  *      Define a structure for our module configuration.
38  */
39 typedef struct rlm_exec_t {
40         char    *xlat_name;
41         int     wait;
42         char    *program;
43         char    *input;
44         char    *output;
45         char    *packet_type;
46         unsigned int    packet_code;
47         int     shell_escape;
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 const 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         { "shell_escape", PW_TYPE_BOOLEAN,  offsetof(rlm_exec_t,shell_escape), NULL, "yes" },
70         { NULL, -1, 0, NULL, NULL }             /* end the list */
71 };
72
73
74 /*
75  *      Decode the configuration file string to a pointer to
76  *      a value-pair list in the REQUEST data structure.
77  */
78 static VALUE_PAIR **decode_string(REQUEST *request, const char *string)
79 {
80         if (!string) return NULL;
81
82         /*
83          *      Yuck.  We need a 'switch' over character strings
84          *      in C.
85          */
86         if (strcmp(string, "request") == 0) {
87                 return &request->packet->vps;
88         }
89
90         if (strcmp(string, "reply") == 0) {
91                 if (!request->reply) return NULL;
92
93                 return &request->reply->vps;
94         }
95
96         if (strcmp(string, "proxy-request") == 0) {
97                 if (!request->proxy) return NULL;
98
99                 return &request->proxy->vps;
100         }
101
102         if (strcmp(string, "proxy-reply") == 0) {
103                 if (!request->proxy_reply) return NULL;
104
105                 return &request->proxy_reply->vps;
106         }
107
108         if (strcmp(string, "config") == 0) {
109                 return &request->config_items;
110         }
111
112         if (strcmp(string, "none") == 0) {
113                 return NULL;
114         }
115
116         return NULL;
117 }
118
119
120 /*
121  *      Do xlat of strings.
122  */
123 static int exec_xlat(void *instance, REQUEST *request,
124                      char *fmt, char *out, int outlen,
125                      UNUSED RADIUS_ESCAPE_STRING func)
126 {
127         int             result;
128         rlm_exec_t      *inst = instance;
129         VALUE_PAIR      **input_pairs;
130
131         input_pairs = decode_string(request, inst->input);
132         if (!input_pairs) {
133                 radlog(L_ERR, "rlm_exec (%s): Failed to find input pairs for xlat",
134                        inst->xlat_name);
135                 out[0] = '\0';
136                 return 0;
137         }
138
139         /*
140          *      FIXME: Do xlat of program name?
141          */
142         DEBUG2("rlm_exec (%s): Executing %s", inst->xlat_name, fmt);
143         result = radius_exec_program(fmt, request, inst->wait,
144                                      out, outlen, *input_pairs, NULL, inst->shell_escape);
145         DEBUG2("rlm_exec (%s): result %d", inst->xlat_name, result);
146         if (result != 0) {
147                 out[0] = '\0';
148                 return 0;
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         /*
168          *  Free the strings.
169          */
170         if (inst->program) free(inst->program);
171         if (inst->input) free(inst->input);
172         if (inst->output) free(inst->output);
173         if (inst->packet_type) free(inst->packet_type);
174
175         free(inst);
176         return 0;
177 }
178
179
180 /*
181  *      Do any per-module initialization that is separate to each
182  *      configured instance of the module.  e.g. set up connections
183  *      to external databases, read configuration files, set up
184  *      dictionary entries, etc.
185  *
186  *      If configuration information is given in the config section
187  *      that must be referenced in later calls, store a handle to it
188  *      in *instance otherwise put a null pointer there.
189  */
190 static int exec_instantiate(CONF_SECTION *conf, void **instance)
191 {
192         rlm_exec_t      *inst;
193         const char      *xlat_name;
194
195         /*
196          *      Set up a storage area for instance data
197          */
198
199         inst = rad_malloc(sizeof(rlm_exec_t));
200         if (!inst)
201                 return -1;
202         memset(inst, 0, sizeof(rlm_exec_t));
203
204         /*
205          *      If the configuration parameters can't be parsed, then
206          *      fail.
207          */
208         if (cf_section_parse(conf, inst, module_config) < 0) {
209                 radlog(L_ERR, "rlm_exec: Failed parsing the configuration");
210                 exec_detach(inst);
211                 return -1;
212         }
213
214         /*
215          *      No input pairs defined.  Why are we executing a program?
216          */
217         if (!inst->input) {
218                 radlog(L_ERR, "rlm_exec: Must define input pairs for external program.");
219                 exec_detach(inst);
220                 return -1;
221         }
222
223         /*
224          *      Sanity check the config.  If we're told to NOT wait,
225          *      then the output pairs must not be defined.
226          */
227         if (!inst->wait &&
228             (inst->output != NULL)) {
229                 radlog(L_ERR, "rlm_exec: Cannot read output pairs if wait=no");
230                 exec_detach(inst);
231                 return -1;
232         }
233
234         /*
235          *      Sanity check the config.  If we're told to wait,
236          *      then the output pairs should be defined.
237          */
238         if (inst->wait &&
239             (inst->output == NULL)) {
240                 radlog(L_INFO, "rlm_exec: wait=yes but no output defined. Did you mean output=none?");
241         }
242
243         /*
244          *      Get the packet type on which to execute
245          */
246         if (!inst->packet_type) {
247                 inst->packet_code = 0;
248         } else {
249                 DICT_VALUE      *dval;
250
251                 dval = dict_valbyname(PW_PACKET_TYPE, inst->packet_type);
252                 if (!dval) {
253                         radlog(L_ERR, "rlm_exec: Unknown packet type %s: See list of VALUEs for Packet-Type in share/dictionary", inst->packet_type);
254                         exec_detach(inst);
255                         return -1;
256                 }
257                 inst->packet_code = dval->value;
258         }
259
260         xlat_name = cf_section_name2(conf);
261         if (xlat_name == NULL)
262                 xlat_name = cf_section_name1(conf);
263         if (xlat_name){
264                 inst->xlat_name = strdup(xlat_name);
265                 xlat_register(xlat_name, exec_xlat, inst);
266         }
267
268         *instance = inst;
269
270         return 0;
271 }
272
273
274 /*
275  *  Dispatch an exec method
276  */
277 static int exec_dispatch(void *instance, REQUEST *request)
278 {
279         int result;
280         VALUE_PAIR **input_pairs, **output_pairs;
281         VALUE_PAIR *answer;
282         rlm_exec_t *inst = (rlm_exec_t *) instance;
283
284         /*
285          *      We need a program to execute.
286          */
287         if (!inst->program) {
288                 radlog(L_ERR, "rlm_exec (%s): We require a program to execute",
289                        inst->xlat_name);
290                 return RLM_MODULE_FAIL;
291         }
292
293         /*
294          *      See if we're supposed to execute it now.
295          */
296         if (!((inst->packet_code == 0) ||
297               (request->packet->code == inst->packet_code) ||
298               (request->reply->code == inst->packet_code) ||
299               (request->proxy &&
300                (request->proxy->code == inst->packet_code)) ||
301               (request->proxy_reply &&
302                (request->proxy_reply->code == inst->packet_code)))) {
303                 DEBUG2("  rlm_exec (%s): Packet type is not %s.  Not executing.",
304                        inst->xlat_name, inst->packet_type);
305                 return RLM_MODULE_NOOP;
306         }
307
308         /*
309          *      Decide what input/output the program takes.
310          */
311         input_pairs = decode_string(request, inst->input);
312         output_pairs = decode_string(request, inst->output);
313
314         /*
315          *      It points to the attribute list, but the attribute
316          *      list is empty.
317          */
318         if (input_pairs && !*input_pairs) {
319                 DEBUG2("rlm_exec (%s): WARNING! Input pairs are empty.  No attributes will be passed to the script", inst->xlat_name);
320         }
321
322         /*
323          *      This function does it's own xlat of the input program
324          *      to execute.
325          *
326          *      FIXME: if inst->program starts with %{, then
327          *      do an xlat ourselves.  This will allow us to do
328          *      program = %{Exec-Program}, which this module
329          *      xlat's into it's string value, and then the
330          *      exec program function xlat's it's string value
331          *      into something else.
332          */
333         result = radius_exec_program(inst->program, request,
334                                      inst->wait, NULL, 0,
335                                      *input_pairs, &answer, inst->shell_escape);
336         if (result < 0) {
337                 radlog(L_ERR, "rlm_exec (%s): External script failed",
338                        inst->xlat_name);
339                 return RLM_MODULE_FAIL;
340         }
341
342         /*
343          *      Move the answer over to the output pairs.
344          *
345          *      If we're not waiting, then there are no output pairs.
346          */
347         if (output_pairs) pairmove(output_pairs, &answer);
348
349         pairfree(&answer);
350
351         if (result == 0) {
352                 return RLM_MODULE_OK;
353         }
354         if (result > RLM_MODULE_NUMCODES) {
355                 return RLM_MODULE_FAIL;
356         }
357         return result-1;
358 }
359
360
361 /*
362  *      First, look for Exec-Program && Exec-Program-Wait.
363  *
364  *      Then, call exec_dispatch.
365  */
366 static int exec_postauth(void *instance, REQUEST *request)
367 {
368         int result;
369         int exec_wait = 0;
370         VALUE_PAIR *vp, *tmp;
371         rlm_exec_t *inst = (rlm_exec_t *) instance;
372
373         vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM);
374         if (vp) {
375                 exec_wait = 0;
376
377         } else if ((vp = pairfind(request->reply->vps, PW_EXEC_PROGRAM_WAIT)) != NULL) {
378                 exec_wait = 1;
379         }
380         if (!vp) goto dispatch;
381
382         tmp = NULL;
383         result = radius_exec_program(vp->vp_strvalue, request, exec_wait,
384                                      NULL, 0, request->packet->vps, &tmp,
385                                      inst->shell_escape);
386
387         /*
388          *      Always add the value-pairs to the reply.
389          */
390         pairmove(&request->reply->vps, &tmp);
391         pairfree(&tmp);
392
393         if (result < 0) {
394                 /*
395                  *      Error. radius_exec_program() returns -1 on
396                  *      fork/exec errors.
397                  */
398                 tmp = pairmake("Reply-Message", "Access denied (external check failed)", T_OP_SET);
399                 pairadd(&request->reply->vps, tmp);
400                 
401                 DEBUG2("Login incorrect (external check failed)");
402
403                 request->reply->code = PW_AUTHENTICATION_REJECT;
404                 return RLM_MODULE_REJECT;
405         }
406         if (result > 0) {
407                 /*
408                  *      Reject. radius_exec_program() returns >0
409                  *      if the exec'ed program had a non-zero
410                  *      exit status.
411                  */
412                 request->reply->code = PW_AUTHENTICATION_REJECT;
413                 DEBUG2("Login incorrect (external check said so)");
414                 return RLM_MODULE_REJECT;
415         }
416
417  dispatch:
418         if (!inst->program) return RLM_MODULE_NOOP;
419
420         return exec_dispatch(instance, request);
421 }
422
423 /*
424  *      The module name should be the only globally exported symbol.
425  *      That is, everything else should be 'static'.
426  *
427  *      If the module needs to temporarily modify it's instantiation
428  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
429  *      The server will then take care of ensuring that the module
430  *      is single-threaded.
431  */
432 module_t rlm_exec = {
433         RLM_MODULE_INIT,
434         "exec",                         /* Name */
435         RLM_TYPE_THREAD_SAFE,           /* type */
436         exec_instantiate,               /* instantiation */
437         exec_detach,                    /* detach */
438         {
439                 exec_dispatch,          /* authentication */
440                 exec_dispatch,          /* authorization */
441                 exec_dispatch,          /* pre-accounting */
442                 exec_dispatch,          /* accounting */
443                 NULL,                   /* check simul */
444                 exec_dispatch,          /* pre-proxy */
445                 exec_dispatch,          /* post-proxy */
446                 exec_postauth           /* post-auth */
447         },
448 };