lots of changes to radsrv/reply and use of new radmsg stuff
[radsecproxy.git] / udp.c
1 /*
2  * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <signal.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netdb.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <limits.h>
16 #ifdef SYS_SOLARIS9
17 #include <fcntl.h>
18 #endif
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/select.h>
22 #include <ctype.h>
23 #include <sys/wait.h>
24 #include <arpa/inet.h>
25 #include <regex.h>
26 #include <pthread.h>
27 #include <openssl/ssl.h>
28 #include "debug.h"
29 #include "list.h"
30 #include "tlv11.h"
31 #include "radmsg.h"
32 #include "util.h"
33 #include "radsecproxy.h"
34 #include "tls.h"
35
36 static int client4_sock = -1;
37 static int client6_sock = -1;
38 static struct queue *server_replyq = NULL;
39
40 /* exactly one of client and server must be non-NULL */
41 /* return who we received from in *client or *server */
42 /* return from in sa if not NULL */
43 unsigned char *radudpget(int s, struct client **client, struct server **server, struct sockaddr_storage *sa) {
44     int cnt, len;
45     unsigned char buf[4], *rad = NULL;
46     struct sockaddr_storage from;
47     socklen_t fromlen = sizeof(from);
48     struct clsrvconf *p;
49     struct list_node *node;
50     fd_set readfds;
51     
52     for (;;) {
53         if (rad) {
54             free(rad);
55             rad = NULL;
56         }
57         FD_ZERO(&readfds);
58         FD_SET(s, &readfds);
59         if (select(s + 1, &readfds, NULL, NULL, NULL) < 1)
60             continue;
61         cnt = recvfrom(s, buf, 4, MSG_PEEK | MSG_TRUNC, (struct sockaddr *)&from, &fromlen);
62         if (cnt == -1) {
63             debug(DBG_WARN, "radudpget: recv failed");
64             continue;
65         }
66         if (cnt < 20) {
67             debug(DBG_WARN, "radudpget: length too small");
68             recv(s, buf, 4, 0);
69             continue;
70         }
71         
72         p = client
73             ? find_clconf(RAD_UDP, (struct sockaddr *)&from, NULL)
74             : find_srvconf(RAD_UDP, (struct sockaddr *)&from, NULL);
75         if (!p) {
76             debug(DBG_WARN, "radudpget: got packet from wrong or unknown UDP peer %s, ignoring", addr2string((struct sockaddr *)&from, fromlen));
77             recv(s, buf, 4, 0);
78             continue;
79         }
80         
81         len = RADLEN(buf);
82         if (len < 20) {
83             debug(DBG_WARN, "radudpget: length too small");
84             recv(s, buf, 4, 0);
85             continue;
86         }
87             
88         rad = malloc(len);
89         if (!rad) {
90             debug(DBG_ERR, "radudpget: malloc failed");
91             recv(s, buf, 4, 0);
92             continue;
93         }
94         
95         cnt = recv(s, rad, len, MSG_TRUNC);
96         debug(DBG_DBG, "radudpget: got %d bytes from %s", cnt, addr2string((struct sockaddr *)&from, fromlen));
97
98         if (cnt < len) {
99             debug(DBG_WARN, "radudpget: packet smaller than length field in radius header");
100             continue;
101         }
102         if (cnt > len)
103             debug(DBG_DBG, "radudpget: packet was padded with %d bytes", cnt - len);
104
105         if (client) {
106             node = list_first(p->clients);
107             *client = node ? (struct client *)node->data : addclient(p);
108             if (!*client)
109                 continue;
110         } else if (server)
111             *server = p->servers;
112         break;
113     }
114     if (sa)
115         *sa = from;
116     return rad;
117 }
118
119 int clientradputudp(struct server *server, unsigned char *rad) {
120     size_t len;
121     struct sockaddr_storage sa;
122     struct sockaddr *sap;
123     struct clsrvconf *conf = server->conf;
124     in_port_t *port = NULL;
125     
126     len = RADLEN(rad);
127     
128     if (*rad == RAD_Accounting_Request) {
129         sap = (struct sockaddr *)&sa;
130         memcpy(sap, conf->addrinfo->ai_addr, conf->addrinfo->ai_addrlen);
131     } else
132         sap = conf->addrinfo->ai_addr;
133     
134     switch (sap->sa_family) {
135     case AF_INET:
136         port = &((struct sockaddr_in *)sap)->sin_port;
137         break;
138     case AF_INET6:
139         port = &((struct sockaddr_in6 *)sap)->sin6_port;
140         break;
141     default:
142         return 0;
143     }
144
145     if (*rad == RAD_Accounting_Request)
146         *port = htons(ntohs(*port) + 1);
147     
148     if (sendto(server->sock, rad, len, 0, sap, conf->addrinfo->ai_addrlen) >= 0) {
149         debug(DBG_DBG, "clienradputudp: sent UDP of length %d to %s port %d", len, conf->host, ntohs(*port));
150         return 1;
151     }
152
153     debug(DBG_WARN, "clientradputudp: send failed");
154     return 0;
155 }
156
157 void *udpclientrd(void *arg) {
158     struct server *server;
159     unsigned char *buf;
160     int *s = (int *)arg;
161     
162     for (;;) {
163         server = NULL;
164         buf = radudpget(*s, NULL, &server, NULL);
165         if (!replyh(server, buf))
166             free(buf);
167     }
168 }
169
170 void *udpserverrd(void *arg) {
171     struct request rq;
172     int *sp = (int *)arg;
173     
174     for (;;) {
175         memset(&rq, 0, sizeof(struct request));
176         rq.buf = radudpget(*sp, &rq.from, NULL, &rq.fromsa);
177         rq.fromudpsock = *sp;
178         radsrv(&rq);
179     }
180     free(sp);
181 }
182
183 void *udpserverwr(void *arg) {
184     struct queue *replyq = (struct queue *)arg;
185     struct reply *reply;
186     
187     for (;;) {
188         pthread_mutex_lock(&replyq->mutex);
189         while (!(reply = (struct reply *)list_shift(replyq->entries))) {
190             debug(DBG_DBG, "udp server writer, waiting for signal");
191             pthread_cond_wait(&replyq->cond, &replyq->mutex);
192             debug(DBG_DBG, "udp server writer, got signal");
193         }
194         pthread_mutex_unlock(&replyq->mutex);
195
196         if (sendto(reply->toudpsock, reply->buf, RADLEN(reply->buf), 0,
197                    (struct sockaddr *)&reply->tosa, SOCKADDR_SIZE(reply->tosa)) < 0)
198             debug(DBG_WARN, "sendudp: send failed");
199         free(reply->buf);
200         free(reply);
201     }
202 }
203
204 void addclientudp(struct client *client) {
205     client->replyq = server_replyq;
206 }
207
208 void addserverextraudp(struct clsrvconf *conf) {
209     switch (conf->addrinfo->ai_family) {
210     case AF_INET:
211         if (client4_sock < 0) {
212             client4_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET, 0, 1);
213             if (client4_sock < 0)
214                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
215         }
216         conf->servers->sock = client4_sock;
217         break;
218     case AF_INET6:
219         if (client6_sock < 0) {
220             client6_sock = bindtoaddr(getsrcprotores(RAD_UDP), AF_INET6, 0, 1);
221             if (client6_sock < 0)
222                 debugx(1, DBG_ERR, "addserver: failed to create client socket for server %s", conf->host);
223         }
224         conf->servers->sock = client6_sock;
225         break;
226     default:
227         debugx(1, DBG_ERR, "addserver: unsupported address family");
228     }
229 }
230
231 void initextraudp() {
232     pthread_t cl4th, cl6th, srvth;
233     
234     if (client4_sock >= 0)
235         if (pthread_create(&cl4th, NULL, udpclientrd, (void *)&client4_sock))
236             debugx(1, DBG_ERR, "pthread_create failed");
237     if (client6_sock >= 0)
238         if (pthread_create(&cl6th, NULL, udpclientrd, (void *)&client6_sock))
239             debugx(1, DBG_ERR, "pthread_create failed");
240
241     if (find_clconf_type(RAD_UDP, NULL)) {
242         server_replyq = newqueue();
243         if (pthread_create(&srvth, NULL, udpserverwr, (void *)server_replyq))
244             debugx(1, DBG_ERR, "pthread_create failed");
245     }
246 }