Remove "out of memory" log messages.
[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(packet, *vps);
157                                 if (!packet->vps) {
158                                         rcode = RLM_MODULE_FAIL;
159                                         goto done;
160                                 }
161                         }
162                         
163
164
165                         /*
166                          *      For CHAP, create the CHAP-Challenge if
167                          *      it doesn't exist.
168                          */
169                         if ((code == PW_AUTHENTICATION_REQUEST) &&
170                             (pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY) != NULL) &&
171                             (pairfind(request->packet->vps, PW_CHAP_CHALLENGE, 0, TAG_ANY) == NULL)) {
172                                 vp = radius_paircreate(request, &packet->vps,
173                                                        PW_CHAP_CHALLENGE, 0);
174                                 vp->length = AUTH_VECTOR_LEN;
175                                 memcpy(vp->vp_strvalue, request->packet->vector,
176                                        AUTH_VECTOR_LEN);
177                         }
178                 } else {
179                         size_t i;
180
181                         for (i = 0; i < sizeof(packet->vector); i++) {
182                                 packet->vector[i] = fr_rand() & 0xff;
183                         }
184
185                         packet->id++;
186                         free(packet->data);
187                         packet->data = NULL;
188                         packet->data_len = 0;
189                 }
190
191                 /*
192                  *      (Re)-Write these.
193                  */
194                 packet->dst_ipaddr = home->ipaddr;
195                 packet->dst_port = home->port;
196                 memset(&packet->src_ipaddr, 0, sizeof(packet->src_ipaddr));
197                 packet->src_port = 0;
198                 
199                 /*
200                  *      Encode, sign and then send the packet.
201                  */
202                 RDEBUG("Replicating list '%s' to Realm '%s'",
203                        fr_int2str(pair_lists, list, "¿unknown?"),realm->name);
204                 if (rad_send(packet, NULL, home->secret) < 0) {
205                         RDEBUGE("Failed replicating packet: %s",
206                                fr_strerror());
207                         rcode = RLM_MODULE_FAIL;
208                         goto done;
209                 }
210
211                 /*
212                  *      We've sent it to at least one destination.
213                  */
214                 rcode = RLM_MODULE_OK;
215         }
216         
217         done:
218         
219         cleanup(packet);
220         return rcode;
221 }
222 #else
223 static rlm_rcode_t replicate_packet(void *instance, REQUEST *request,
224                             pair_lists_t list, unsigned int code)
225 {
226         RDEBUG("Replication is unsupported in this build.");
227         return RLM_MODULE_FAIL;
228 }
229 #endif
230
231 static rlm_rcode_t mod_authorize(void *instance, REQUEST *request)
232 {
233         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
234                                 request->packet->code);
235 }
236
237 static rlm_rcode_t mod_preaccounting(void *instance, REQUEST *request)
238 {
239         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
240                                 request->packet->code);
241 }
242
243 static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
244 {
245         return replicate_packet(instance, request, PAIR_LIST_REPLY,
246                                 request->reply->code);
247 }
248
249 static rlm_rcode_t mod_pre_proxy(void *instance, REQUEST *request)
250 {
251         return replicate_packet(instance, request, PAIR_LIST_PROXY_REQUEST,
252                                 request->proxy->code);
253 }
254
255 static rlm_rcode_t mod_post_proxy(void *instance, REQUEST *request)
256 {
257         return replicate_packet(instance, request, PAIR_LIST_PROXY_REPLY,
258                                 request->proxy_reply->code);
259 }
260
261 static rlm_rcode_t mod_post_auth(void *instance, REQUEST *request)
262 {
263         return replicate_packet(instance, request, PAIR_LIST_REPLY,
264                                 request->reply->code);
265 }
266
267 static rlm_rcode_t mod_recv_coa(void *instance, REQUEST *request)
268 {
269         return replicate_packet(instance, request, PAIR_LIST_REQUEST,
270                                 request->packet->code);
271 }
272
273 /*
274  *      The module name should be the only globally exported symbol.
275  *      That is, everything else should be 'static'.
276  *
277  *      If the module needs to temporarily modify it's instantiation
278  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
279  *      The server will then take care of ensuring that the module
280  *      is single-threaded.
281  */
282 module_t rlm_replicate = {
283         RLM_MODULE_INIT,
284         "replicate",
285         RLM_TYPE_THREAD_SAFE,           /* type */
286         NULL,                           /* instantiation */
287         NULL,                           /* detach */
288         {
289                 NULL,                   /* authentication */
290                 mod_authorize,  /* authorization */
291                 mod_preaccounting,/* preaccounting */
292                 mod_accounting, /* accounting */
293                 NULL,                   /* checksimul */
294                 mod_pre_proxy,  /* pre-proxy */
295                 mod_post_proxy, /* post-proxy */
296                 mod_post_auth   /* post-auth */
297 #ifdef WITH_COA
298                 , mod_recv_coa, /* coa-request */
299                 NULL
300 #endif
301         },
302 };