Fix "use of GNU empty initializer extension" warning.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2014 Guus Sliepen <guus@tinc-vpn.org>,
4                   2000-2005 Ivo Timmermans
5                   2006      Scott Lamb <slamb@slamb.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "cipher.h"
25 #include "connection.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "net.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 #ifndef MIN
34 #define MIN(x, y) (((x)<(y))?(x):(y))
35 #endif
36
37 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
38         connection_t *c = handle;
39
40         if(!c) {
41                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
42                 abort();
43         }
44
45         buffer_add(&c->outbuf, buffer, length);
46         io_set(&c->io, IO_READ | IO_WRITE);
47
48         return true;
49 }
50
51 bool send_meta(connection_t *c, const char *buffer, int length) {
52         if(!c) {
53                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
54                 abort();
55         }
56
57         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
58                c->name, c->hostname);
59
60         if(c->protocol_minor >= 2) {
61                 return sptps_send_record(&c->sptps, 0, buffer, length);
62         }
63
64         /* Add our data to buffer */
65         if(c->status.encryptout) {
66 #ifdef DISABLE_LEGACY
67                 return false;
68 #else
69
70                 if(length > c->outbudget) {
71                         logger(DEBUG_META, LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
72                         return false;
73                 } else {
74                         c->outbudget -= length;
75                 }
76
77                 size_t outlen = length;
78
79                 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
80                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
81                                c->name, c->hostname);
82                         return false;
83                 }
84
85 #endif
86         } else {
87                 buffer_add(&c->outbuf, buffer, length);
88         }
89
90         io_set(&c->io, IO_READ | IO_WRITE);
91
92         return true;
93 }
94
95 void send_meta_raw(connection_t *c, const char *buffer, int length) {
96         if(!c) {
97                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
98                 abort();
99         }
100
101         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of raw metadata to %s (%s)", length,
102                c->name, c->hostname);
103
104         buffer_add(&c->outbuf, buffer, length);
105
106         io_set(&c->io, IO_READ | IO_WRITE);
107 }
108
109 void broadcast_meta(connection_t *from, const char *buffer, int length) {
110         for list_each(connection_t, c, connection_list)
111                 if(c != from && c->edge) {
112                         send_meta(c, buffer, length);
113                 }
114 }
115
116 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
117         const char *data = vdata;
118         connection_t *c = handle;
119
120         if(!c) {
121                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
122                 abort();
123         }
124
125         if(type == SPTPS_HANDSHAKE) {
126                 if(c->allow_request == ACK) {
127                         return send_ack(c);
128                 } else {
129                         return true;
130                 }
131         }
132
133         if(!data) {
134                 return true;
135         }
136
137         /* Are we receiving a TCPpacket? */
138
139         if(c->tcplen) {
140                 if(length != c->tcplen) {
141                         return false;
142                 }
143
144                 receive_tcppacket(c, data, length);
145                 c->tcplen = 0;
146                 return true;
147         }
148
149         /* Change newline to null byte, just like non-SPTPS requests */
150
151         if(data[length - 1] == '\n') {
152                 ((char *)data)[length - 1] = 0;
153         }
154
155         /* Otherwise we are waiting for a request */
156
157         return receive_request(c, data);
158 }
159
160 bool receive_meta(connection_t *c) {
161         int inlen;
162         char inbuf[MAXBUFSIZE];
163         char *bufp = inbuf, *endp;
164
165         /* Strategy:
166            - Read as much as possible from the TCP socket in one go.
167            - Decrypt it.
168            - Check if a full request is in the input buffer.
169            - If yes, process request and remove it from the buffer,
170            then check again.
171            - If not, keep stuff in buffer and exit.
172          */
173
174         buffer_compact(&c->inbuf, MAXBUFSIZE);
175
176         if(sizeof(inbuf) <= c->inbuf.len) {
177                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
178                 return false;
179         }
180
181         inlen = recv(c->socket, inbuf, sizeof(inbuf) - c->inbuf.len, 0);
182
183         if(inlen <= 0) {
184                 if(!inlen || !sockerrno) {
185                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
186                                c->name, c->hostname);
187                 } else if(sockwouldblock(sockerrno)) {
188                         return true;
189                 } else
190                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
191                                c->name, c->hostname, sockstrerror(sockerrno));
192
193                 return false;
194         }
195
196         do {
197                 /* Are we receiving a SPTPS packet? */
198
199                 if(c->sptpslen) {
200                         int len = MIN(inlen, c->sptpslen - c->inbuf.len);
201                         buffer_add(&c->inbuf, bufp, len);
202
203                         char *sptpspacket = buffer_read(&c->inbuf, c->sptpslen);
204
205                         if(!sptpspacket) {
206                                 return true;
207                         }
208
209                         if(!receive_tcppacket_sptps(c, sptpspacket, c->sptpslen)) {
210                                 return false;
211                         }
212
213                         c->sptpslen = 0;
214
215                         bufp += len;
216                         inlen -= len;
217                         continue;
218                 }
219
220                 if(c->protocol_minor >= 2) {
221                         int len = sptps_receive_data(&c->sptps, bufp, inlen);
222
223                         if(!len) {
224                                 return false;
225                         }
226
227                         bufp += len;
228                         inlen -= len;
229                         continue;
230                 }
231
232                 if(!c->status.decryptin) {
233                         endp = memchr(bufp, '\n', inlen);
234
235                         if(endp) {
236                                 endp++;
237                         } else {
238                                 endp = bufp + inlen;
239                         }
240
241                         buffer_add(&c->inbuf, bufp, endp - bufp);
242
243                         inlen -= endp - bufp;
244                         bufp = endp;
245                 } else {
246 #ifdef DISABLE_LEGACY
247                         return false;
248 #else
249
250                         if(inlen > c->inbudget) {
251                                 logger(DEBUG_META, LOG_ERR, "yte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
252                                 return false;
253                         } else {
254                                 c->inbudget -= inlen;
255                         }
256
257                         size_t outlen = inlen;
258
259                         if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
260                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
261                                        c->name, c->hostname);
262                                 return false;
263                         }
264
265                         inlen = 0;
266 #endif
267                 }
268
269                 while(c->inbuf.len) {
270                         /* Are we receiving a TCPpacket? */
271
272                         if(c->tcplen) {
273                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
274
275                                 if(!tcpbuffer) {
276                                         break;
277                                 }
278
279                                 if(!c->node) {
280                                         if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
281                                                 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
282                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
283                                                 } else {
284                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
285                                                         return false;
286                                                 }
287                                         } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
288                                                 if(tcpbuffer[0] != 5) {
289                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
290                                                         return false;
291                                                 }
292
293                                                 if(tcpbuffer[1] == (char)0xff) {
294                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
295                                                         return false;
296                                                 }
297
298                                                 if(tcpbuffer[2] != 5) {
299                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
300                                                         return false;
301                                                 }
302
303                                                 if(tcpbuffer[3] == 0) {
304                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
305                                                 } else {
306                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
307                                                         return false;
308                                                 }
309                                         } else {
310                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
311                                                 abort();
312                                         }
313                                 } else {
314                                         if(c->allow_request == ALL) {
315                                                 receive_tcppacket(c, tcpbuffer, c->tcplen);
316                                         } else {
317                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
318                                                 return false;
319                                         }
320                                 }
321
322                                 c->tcplen = 0;
323                         }
324
325                         /* Otherwise we are waiting for a request */
326
327                         char *request = buffer_readline(&c->inbuf);
328
329                         if(request) {
330                                 bool result = receive_request(c, request);
331
332                                 if(!result) {
333                                         return false;
334                                 }
335
336                                 continue;
337                         } else {
338                                 break;
339                         }
340                 }
341         } while(inlen);
342
343         return true;
344 }