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