Add preliminary stage of 'exec' module, which currently only
[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 } 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 CONF_PARSER module_config[] = {
55         { "wait", PW_TYPE_BOOLEAN,    offsetof(rlm_exec_t,wait), NULL, "yes" },
56         { NULL, -1, 0, NULL, NULL }             /* end the list */
57 };
58
59 /*
60  *  Do xlat of strings!
61  */ 
62 static int exec_xlat(void *instance, REQUEST *request,
63                      char *fmt, char *out, int outlen,
64                      RADIUS_ESCAPE_STRING func)
65 {
66         int             result;
67         rlm_exec_t      *inst = instance;
68         char            buffer[256];
69
70         inst = inst;            /* -Wunused */
71
72         /*
73          * Do an xlat on the provided string (nice recursive operation).
74          */
75         if (!radius_xlat(buffer, sizeof(buffer), fmt, request, NULL)) {
76                 radlog(L_ERR, "rlm_exec: xlat failed.");
77                 return 0;
78         }
79
80         DEBUG2("rlm_exec: %d %s", inst->wait, buffer);
81         result = radius_exec_program(buffer, request, inst->wait,
82                                      out, outlen, FALSE);
83         DEBUG2("rlm_exec: result %d", result);
84         if (result != 0) {
85                 out[0] = '\0';
86                 return 0;
87         }
88
89         return strlen(out);
90 }
91
92 /*
93  *      Do any per-module initialization that is separate to each
94  *      configured instance of the module.  e.g. set up connections
95  *      to external databases, read configuration files, set up
96  *      dictionary entries, etc.
97  *
98  *      If configuration information is given in the config section
99  *      that must be referenced in later calls, store a handle to it
100  *      in *instance otherwise put a null pointer there.
101  */
102 static int exec_instantiate(CONF_SECTION *conf, void **instance)
103 {
104         rlm_exec_t      *inst;
105         char            *xlat_name;
106         
107         /*
108          *      Set up a storage area for instance data
109          */
110         
111         inst = rad_malloc(sizeof(rlm_exec_t));
112         memset(inst, 0, sizeof(rlm_exec_t));
113                 
114         /*
115          *      If the configuration parameters can't be parsed, then
116          *      fail.
117          */
118         if (cf_section_parse(conf, inst, module_config) < 0) {
119                 free(inst);
120                 return -1;
121         }
122         
123         xlat_name = cf_section_name2(conf);
124         if (xlat_name == NULL) 
125                 xlat_name = cf_section_name1(conf);
126         if (xlat_name){ 
127                 inst->xlat_name = strdup(xlat_name);
128                 xlat_register(xlat_name, exec_xlat, inst); 
129         } 
130
131         *instance = inst;
132         
133         return 0;
134 }
135
136 /*
137  * Detach a instance free all ..
138  */
139 static int exec_detach(void *instance)
140 {
141         rlm_exec_t      *inst = instance;
142
143         xlat_unregister(inst->xlat_name, exec_xlat);
144         free(inst->xlat_name);
145
146         free(inst);
147         return 0;
148 }
149
150 /*
151  *      The module name should be the only globally exported symbol.
152  *      That is, everything else should be 'static'.
153  *
154  *      If the module needs to temporarily modify it's instantiation
155  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
156  *      The server will then take care of ensuring that the module
157  *      is single-threaded.
158  */
159 module_t rlm_exec = {
160         "exec",                         /* Name */
161         RLM_TYPE_THREAD_SAFE,           /* type */
162         NULL,                           /* initialization */
163         exec_instantiate,               /* instantiation */
164         {
165                 NULL,                   /* authentication */
166                 NULL,                   /* authorization */
167                 NULL,                   /* pre-accounting */
168                 NULL,                   /* accounting */
169                 NULL,                   /* check simul */
170                 NULL,                   /* pre-proxy */
171                 NULL,                   /* post-proxy */
172                 NULL                    /* post-auth */
173         },
174         exec_detach,                    /* detach */
175         NULL,                           /* destroy */
176 };