perl -p -i -e 's/\ +$//' $(find . -name "*.[ch]" -print)
[freeradius.git] / src / modules / rlm_replicate / rlm_replicate.c
1 /*
2  *   This program is is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License, version 2 if the
4  *   License as published by the Free Software Foundation.
5  *
6  *   This program is distributed in the hope that it will be useful,
7  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *   GNU General Public License for more details.
10  *
11  *   You should have received a copy of the GNU General Public License
12  *   along with this program; if not, write to the Free Software
13  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
14  */
15
16 /**
17  * $Id$
18  * @file rlm_replicate.c
19  * @brief Duplicate RADIUS requests.
20  *
21  * @copyright 2011-2013  The FreeRADIUS server project
22  */
23 #include <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28
29 #ifdef WITH_PROXY
30 static void cleanup(RADIUS_PACKET *packet)
31 {
32         if (!packet) return;
33         if (packet->sockfd >= 0) close(packet->sockfd);
34         rad_free(&packet);
35 }
36
37 /** Copy packet to multiple servers
38  *
39  * Create a duplicate of the packet and send it to a list of realms
40  * defined by the presence of the Replicate-To-Realm VP in the control
41  * list of the current request.
42  *
43  * This is pretty hacky and is 100% fire and forget. If you're looking
44  * to forward authentication requests to multiple realms and process
45  * the responses, this function will not allow you to do that.
46  *
47  * @param[in] instance  of this module.
48  * @param[in] request   The current request.
49  * @param[in] list      of attributes to copy to the duplicate packet.
50  * @param[in] code      to write into the code field of the duplicate packet.
51  * @return RCODE fail on error, invalid if list does not exist, noop if no
52  *         replications succeeded, else ok.
53  */
54 static int replicate_packet(void *instance, REQUEST *request,
55                             pair_lists_t list, unsigned int code)
56 {
57         int rcode = RLM_MODULE_NOOP;
58         VALUE_PAIR *vp, **vps, *last;
59         home_server *home;
60         REALM *realm;
61         home_pool_t *pool;
62         RADIUS_PACKET *packet = NULL;
63
64         instance = instance;    /* -Wunused */
65         last = request->config_items;
66
67         /*
68          *      Send as many packets as necessary to different
69          *      destinations.
70          */
71         while (1) {
72                 vp = pairfind(last, PW_REPLICATE_TO_REALM, 0, TAG_ANY);
73                 if (!vp) break;
74
75                 last = vp->next;
76
77                 realm = realm_find2(vp->vp_strvalue);
78                 if (!realm) {
79                         RDEBUG2E("Cannot Replicate to unknown realm %s", realm);
80                         continue;
81                 }
82                 
83                 /*
84                  *      We shouldn't really do this on every loop.
85                  */
86                 switch (request->packet->code) {
87                 default:
88                         RDEBUG2E("Cannot replicate unknown packet code %d",
89                                 request->packet->code);
90                         cleanup(packet);
91                         return RLM_MODULE_FAIL;
92                 
93                 case PW_AUTHENTICATION_REQUEST:
94                         pool = realm->auth_pool;
95                         break;
96                         
97 #ifdef WITH_ACCOUNTING
98                         
99                 case PW_ACCOUNTING_REQUEST:
100                         pool = realm->acct_pool;
101                         break;
102 #endif
103                         
104 #ifdef WITH_COA
105                 case PW_COA_REQUEST:
106                 case PW_DISCONNECT_REQUEST:
107                         pool = realm->acct_pool;
108                         break;
109 #endif
110                 }
111                 
112                 if (!pool) {
113                         RDEBUG2W("Cancelling replication to Realm %s, as the realm is local.", realm->name);
114                         continue;
115                 }
116                 
117                 home = home_server_ldb(realm->name, pool, request);
118                 if (!home) {
119                         RDEBUG2E("Failed to find live home server for realm %s",
120                                 realm->name);
121                         continue;
122                 }
123                 
124                 /*
125                  *      For replication to multiple servers we re-use the packet
126                  *      we built here.
127                  */
128                 if (!packet) {
129                         packet = rad_alloc(NULL, 1);
130                         if (!packet) return RLM_MODULE_FAIL;
131                         packet->sockfd = -1;
132                         packet->code = code;
133                         packet->id = fr_rand() & 0xff;
134
135                         packet->sockfd = fr_socket(&home->src_ipaddr, 0);
136                         if (packet->sockfd < 0) {
137                                 RDEBUGE("Failed opening socket: %s", fr_strerror());
138                                 rcode = RLM_MODULE_FAIL;
139                                 goto done;
140                         }
141                         
142                         vps = radius_list(request, list);
143                         if (!vps) {
144                                 RDEBUGW("List '%s' doesn't exist for "
145                                        "this packet", fr_int2str(pair_lists,
146                                        list, "¿unknown?"));
147                                 rcode = RLM_MODULE_INVALID;
148                                 goto done;
149                         }
150                         
151                         /*
152                          *      Don't assume the list actually contains any
153                          *      attributes.
154                          */
155                         if (*vps) {
156                                 packet->vps = paircopy(*vps);
157                                 if (!packet->vps) {
158                                         RDEBUGE("Out of memory!");
159                                         rcode = RLM_MODULE_FAIL;
160                                         goto done;
161                                 }
162                         }
163                         
164
165
166                         /*
167                          *      For CHAP, create the CHAP-Challenge if
168                          *      it doesn't exist.
169                          */
170                         if ((code == PW_AUTHENTICATION_REQUEST) &&
171                             (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) != NULL) &&
172                             (pairfind(request->packet->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL)) {
173                                 vp = radius_paircreate(request, &packet->vps,
174                                                        PW_CHAP_CHALLENGE, 0);
175                                 vp->length = AUTH_VECTOR_LEN;
176                                 memcpy(vp->vp_strvalue, request->packet->vector,
177                                        AUTH_VECTOR_LEN);
178                         }
179                 } else {
180                         size_t i;
181
182                         for (i = 0; i < sizeof(packet->vector); i++) {
183                                 packet->vector[i] = fr_rand() & 0xff;
184                         }
185
186                         packet->id++;
187                         free(packet->data);
188                         packet->data = NULL;
189                         packet->data_len = 0;
190                 }
191
192                 /*
193                  *      (Re)-Write these.
194                  */
195                 packet->dst_ipaddr = home->ipaddr;
196                 packet->dst_port = home->port;
197                 memset(&packet->src_ipaddr, 0, sizeof(packet->src_ipaddr));
198                 packet->src_port = 0;
199                 
200                 /*
201                  *      Encode, sign and then send the packet.
202                  */
203                 RDEBUG("Replicating list '%s' to Realm '%s'",
204                        fr_int2str(pair_lists, list, "¿unknown?"),realm->name);
205                 if (rad_send(packet, NULL, home->secret) < 0) {
206                         RDEBUGE("Failed replicating packet: %s",
207                                fr_strerror());
208                         rcode = RLM_MODULE_FAIL;
209                         goto done;
210                 }
211
212                 /*
213                  *      We've sent it to at least one destination.
214                  */
215                 rcode = RLM_MODULE_OK;
216         }
217         
218         done:
219         
220         cleanup(packet);
221         return rcode;
222 }
223 #else
224 static rlm_rcode_t replicate_packet(void *instance, REQUEST *request,
225                             pair_lists_t list, unsigned int code)
226 {
227         RDEBUG("Replication is unsupported in this build.");
228         return RLM_MODULE_FAIL;
229 }
230 #endif
231
232 static rlm_rcode_t replicate_authorize(void *instance, REQUEST *request)
233 {
234         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
235                                 request->packet->code);
236 }
237
238 static rlm_rcode_t replicate_preaccounting(void *instance, REQUEST *request)
239 {
240         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
241                                 request->packet->code);
242 }
243
244 static rlm_rcode_t replicate_accounting(void *instance, REQUEST *request)
245 {
246         return replicate_packet(instance, request, PAIR_LIST_REPLY,
247                                 request->reply->code);
248 }
249
250 static rlm_rcode_t replicate_preproxy(void *instance, REQUEST *request)
251 {
252         return replicate_packet(instance, request, PAIR_LIST_PROXY_REQUEST,
253                                 request->proxy->code);
254 }
255
256 static rlm_rcode_t replicate_postproxy(void *instance, REQUEST *request)
257 {
258         return replicate_packet(instance, request, PAIR_LIST_PROXY_REPLY,
259                                 request->proxy_reply->code);
260 }
261
262 static rlm_rcode_t replicate_postauth(void *instance, REQUEST *request)
263 {
264         return replicate_packet(instance, request, PAIR_LIST_REPLY,
265                                 request->reply->code);
266 }
267
268 static rlm_rcode_t replicate_coarequest(void *instance, REQUEST *request)
269 {
270         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
271                                 request->packet->code);
272 }
273
274 /*
275  *      The module name should be the only globally exported symbol.
276  *      That is, everything else should be 'static'.
277  *
278  *      If the module needs to temporarily modify it's instantiation
279  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
280  *      The server will then take care of ensuring that the module
281  *      is single-threaded.
282  */
283 module_t rlm_replicate = {
284         RLM_MODULE_INIT,
285         "replicate",
286         RLM_TYPE_THREAD_SAFE,           /* type */
287         NULL,                           /* instantiation */
288         NULL,                           /* detach */
289         {
290                 NULL,                   /* authentication */
291                 replicate_authorize,    /* authorization */
292                 replicate_preaccounting,/* preaccounting */
293                 replicate_accounting,   /* accounting */
294                 NULL,                   /* checksimul */
295                 replicate_preproxy,     /* pre-proxy */
296                 replicate_postproxy,    /* post-proxy */
297                 replicate_postauth      /* post-auth */
298 #ifdef WITH_COA
299                 , replicate_coarequest, /* coa-request */
300                 NULL
301 #endif
302         },
303 };