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