Only read one record at a time in sptps_receive_data().
[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 bool send_meta_sptps(void *handle, uint8_t type, const void *buffer, size_t length) {
34         connection_t *c = handle;
35
36         if(!c) {
37                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta_sptps() called with NULL pointer!");
38                 abort();
39         }
40
41         buffer_add(&c->outbuf, buffer, length);
42         io_set(&c->io, IO_READ | IO_WRITE);
43
44         return true;
45 }
46
47 bool send_meta(connection_t *c, const char *buffer, int length) {
48         if(!c) {
49                 logger(DEBUG_ALWAYS, LOG_ERR, "send_meta() called with NULL pointer!");
50                 abort();
51         }
52
53         logger(DEBUG_META, LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
54                            c->name, c->hostname);
55
56         if(c->protocol_minor >= 2)
57                 return sptps_send_record(&c->sptps, 0, buffer, length);
58
59         /* Add our data to buffer */
60         if(c->status.encryptout) {
61 #ifdef DISABLE_LEGACY
62                 return false;
63 #else
64                 size_t outlen = length;
65
66                 if(!cipher_encrypt(c->outcipher, buffer, length, buffer_prepare(&c->outbuf, length), &outlen, false) || outlen != length) {
67                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while encrypting metadata to %s (%s)",
68                                         c->name, c->hostname);
69                         return false;
70                 }
71 #endif
72         } else {
73                 buffer_add(&c->outbuf, buffer, length);
74         }
75
76         io_set(&c->io, IO_READ | IO_WRITE);
77
78         return true;
79 }
80
81 void broadcast_meta(connection_t *from, const char *buffer, int length) {
82         for list_each(connection_t, c, connection_list)
83                 if(c != from && c->edge)
84                         send_meta(c, buffer, length);
85 }
86
87 bool receive_meta_sptps(void *handle, uint8_t type, const void *vdata, uint16_t length) {
88         const char *data = vdata;
89         connection_t *c = handle;
90
91         if(!c) {
92                 logger(DEBUG_ALWAYS, LOG_ERR, "receive_meta_sptps() called with NULL pointer!");
93                 abort();
94         }
95
96         if(type == SPTPS_HANDSHAKE) {
97                 if(c->allow_request == ACK)
98                         return send_ack(c);
99                 else
100                         return true;
101         }
102
103         if(!data)
104                 return true;
105
106         /* Are we receiving a TCPpacket? */
107
108         if(c->tcplen) {
109                 if(length != c->tcplen)
110                         return false;
111                 receive_tcppacket(c, data, length);
112                 c->tcplen = 0;
113                 return true;
114         }
115
116         /* Change newline to null byte, just like non-SPTPS requests */
117
118         if(data[length - 1] == '\n')
119                 ((char *)data)[length - 1] = 0;
120
121         /* Otherwise we are waiting for a request */
122
123         return receive_request(c, data);
124 }
125
126 bool receive_meta(connection_t *c) {
127         int inlen;
128         char inbuf[MAXBUFSIZE];
129         char *bufp = inbuf, *endp;
130
131         /* Strategy:
132            - Read as much as possible from the TCP socket in one go.
133            - Decrypt it.
134            - Check if a full request is in the input buffer.
135            - If yes, process request and remove it from the buffer,
136            then check again.
137            - If not, keep stuff in buffer and exit.
138          */
139
140         buffer_compact(&c->inbuf, MAXBUFSIZE);
141
142         if(sizeof inbuf <= c->inbuf.len) {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Input buffer full for %s (%s)", c->name, c->hostname);
144                 return false;
145         }
146
147         inlen = recv(c->socket, inbuf, sizeof inbuf - c->inbuf.len, 0);
148
149         if(inlen <= 0) {
150                 if(!inlen || !sockerrno) {
151                         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection closed by %s (%s)",
152                                            c->name, c->hostname);
153                 } else if(sockwouldblock(sockerrno))
154                         return true;
155                 else
156                         logger(DEBUG_ALWAYS, LOG_ERR, "Metadata socket read error for %s (%s): %s",
157                                    c->name, c->hostname, sockstrerror(sockerrno));
158                 return false;
159         }
160
161         do {
162                 if(c->protocol_minor >= 2) {
163                         int len = sptps_receive_data(&c->sptps, bufp, inlen);
164                         if(!len)
165                                 return false;
166                         bufp += len;
167                         inlen -= len;
168                         continue;
169                 }
170
171                 if(!c->status.decryptin) {
172                         endp = memchr(bufp, '\n', inlen);
173                         if(endp)
174                                 endp++;
175                         else
176                                 endp = bufp + inlen;
177
178                         buffer_add(&c->inbuf, bufp, endp - bufp);
179
180                         inlen -= endp - bufp;
181                         bufp = endp;
182                 } else {
183 #ifdef DISABLE_LEGACY
184                         return false;
185 #else
186                         size_t outlen = inlen;
187
188                         if(!cipher_decrypt(c->incipher, bufp, inlen, buffer_prepare(&c->inbuf, inlen), &outlen, false) || inlen != outlen) {
189                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while decrypting metadata from %s (%s)",
190                                            c->name, c->hostname);
191                                 return false;
192                         }
193
194                         inlen = 0;
195 #endif
196                 }
197
198                 while(c->inbuf.len) {
199                         /* Are we receiving a TCPpacket? */
200
201                         if(c->tcplen) {
202                                 char *tcpbuffer = buffer_read(&c->inbuf, c->tcplen);
203                                 if(!tcpbuffer)
204                                         break;
205
206                                 if(!c->node) {
207                                         if(c->outgoing && proxytype == PROXY_SOCKS4 && c->allow_request == ID) {
208                                                 if(tcpbuffer[0] == 0 && tcpbuffer[1] == 0x5a) {
209                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
210                                                 } else {
211                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected");
212                                                         return false;
213                                                 }
214                                         } else if(c->outgoing && proxytype == PROXY_SOCKS5 && c->allow_request == ID) {
215                                                 if(tcpbuffer[0] != 5) {
216                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
217                                                         return false;
218                                                 }
219                                                 if(tcpbuffer[1] == (char)0xff) {
220                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Proxy request rejected: unsuitable authentication method");
221                                                         return false;
222                                                 }
223                                                 if(tcpbuffer[2] != 5) {
224                                                         logger(DEBUG_CONNECTIONS, LOG_ERR, "Invalid response from proxy server");
225                                                         return false;
226                                                 }
227                                                 if(tcpbuffer[3] == 0) {
228                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
229                                                 } else {
230                                                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request rejected");
231                                                         return false;
232                                                 }
233                                         } else {
234                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "c->tcplen set but c->node is NULL!");
235                                                 abort();
236                                         }
237                                 } else {
238                                         if(c->allow_request == ALL) {
239                                                 receive_tcppacket(c, tcpbuffer, c->tcplen);
240                                         } else {
241                                                 logger(DEBUG_CONNECTIONS, LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
242                                                 return false;
243                                         }
244                                 }
245
246                                 c->tcplen = 0;
247                         }
248
249                         /* Otherwise we are waiting for a request */
250
251                         char *request = buffer_readline(&c->inbuf);
252                         if(request) {
253                                 bool result = receive_request(c, request);
254                                 if(!result)
255                                         return false;
256                                 continue;
257                         } else {
258                                 break;
259                         }
260                 }
261         } while(inlen);
262
263         return true;
264 }