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