Fix spelling errors.
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2017 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 <openssl/err.h>
25 #include <openssl/evp.h>
26
27 #include "avl_tree.h"
28 #include "connection.h"
29 #include "logger.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "protocol.h"
33 #include "proxy.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool send_meta(connection_t *c, const char *buffer, int length) {
38         int outlen;
39         int result;
40
41         ifdebug(META) logger(LOG_DEBUG, "Sending %d bytes of metadata to %s (%s)", length,
42                              c->name, c->hostname);
43
44         if(!c->outbuflen) {
45                 c->last_flushed_time = now;
46         }
47
48         /* Find room in connection's buffer */
49         if(length + c->outbuflen > c->outbufsize) {
50                 c->outbufsize = length + c->outbuflen;
51                 c->outbuf = xrealloc(c->outbuf, c->outbufsize);
52         }
53
54         if(length + c->outbuflen + c->outbufstart > c->outbufsize) {
55                 memmove(c->outbuf, c->outbuf + c->outbufstart, c->outbuflen);
56                 c->outbufstart = 0;
57         }
58
59         /* Add our data to buffer */
60         if(c->status.encryptout) {
61                 /* Check encryption limits */
62                 if((uint64_t)length > c->outbudget) {
63                         ifdebug(META) logger(LOG_ERR, "Byte limit exceeded for encryption to %s (%s)", c->name, c->hostname);
64                         return false;
65                 } else {
66                         c->outbudget -= length;
67                 }
68
69                 result = EVP_EncryptUpdate(c->outctx, (unsigned char *)c->outbuf + c->outbufstart + c->outbuflen,
70                                            &outlen, (unsigned char *)buffer, length);
71
72                 if(!result || outlen < length) {
73                         logger(LOG_ERR, "Error while encrypting metadata to %s (%s): %s",
74                                c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
75                         return false;
76                 } else if(outlen > length) {
77                         logger(LOG_EMERG, "Encrypted data too long! Heap corrupted!");
78                         abort();
79                 }
80
81                 c->outbuflen += outlen;
82         } else {
83                 memcpy(c->outbuf + c->outbufstart + c->outbuflen, buffer, length);
84                 c->outbuflen += length;
85         }
86
87         return true;
88 }
89
90 bool flush_meta(connection_t *c) {
91         int result;
92
93         ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s)",
94                              c->outbuflen, c->name, c->hostname);
95
96         while(c->outbuflen) {
97                 result = send(c->socket, c->outbuf + c->outbufstart, c->outbuflen, 0);
98
99                 if(result <= 0) {
100                         if(!errno || errno == EPIPE) {
101                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
102                                                             c->name, c->hostname);
103                         } else if(errno == EINTR) {
104                                 continue;
105                         } else if(sockwouldblock(sockerrno)) {
106                                 ifdebug(META) logger(LOG_DEBUG, "Flushing %d bytes to %s (%s) would block",
107                                                      c->outbuflen, c->name, c->hostname);
108                                 return true;
109                         } else {
110                                 logger(LOG_ERR, "Flushing meta data to %s (%s) failed: %s", c->name,
111                                        c->hostname, sockstrerror(sockerrno));
112                         }
113
114                         return false;
115                 }
116
117                 c->outbufstart += result;
118                 c->outbuflen -= result;
119         }
120
121         c->outbufstart = 0; /* avoid unnecessary memmoves */
122         return true;
123 }
124
125 void broadcast_meta(connection_t *from, const char *buffer, int length) {
126         avl_node_t *node;
127         connection_t *c;
128
129         for(node = connection_tree->head; node; node = node->next) {
130                 c = node->data;
131
132                 if(c != from && c->status.active) {
133                         send_meta(c, buffer, length);
134                 }
135         }
136 }
137
138 bool receive_meta(connection_t *c) {
139         int oldlen, i, result;
140         int lenin, lenout, reqlen;
141         bool decrypted = false;
142         char inbuf[MAXBUFSIZE];
143
144         /* Strategy:
145            - Read as much as possible from the TCP socket in one go.
146            - Decrypt it.
147            - Check if a full request is in the input buffer.
148            - If yes, process request and remove it from the buffer,
149            then check again.
150            - If not, keep stuff in buffer and exit.
151          */
152
153         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
154
155         if(lenin <= 0) {
156                 if(!lenin || !errno) {
157                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection closed by %s (%s)",
158                                                     c->name, c->hostname);
159                 } else if(sockwouldblock(sockerrno)) {
160                         return true;
161                 } else
162                         logger(LOG_ERR, "Metadata socket read error for %s (%s): %s",
163                                c->name, c->hostname, sockstrerror(sockerrno));
164
165                 return false;
166         }
167
168         oldlen = c->buflen;
169         c->buflen += lenin;
170
171         while(lenin > 0) {
172                 reqlen = 0;
173
174                 /* Is it proxy metadata? */
175
176                 if(c->allow_request == PROXY) {
177                         reqlen = receive_proxy_meta(c);
178
179                         if(reqlen < 0) {
180                                 return false;
181                         }
182
183                         goto consume;
184                 }
185
186                 /* Decrypt */
187
188                 if(c->status.decryptin && !decrypted) {
189                         /* Check decryption limits */
190                         if((uint64_t)lenin > c->inbudget) {
191                                 ifdebug(META) logger(LOG_ERR, "Byte limit exceeded for decryption from %s (%s)", c->name, c->hostname);
192                                 return false;
193                         } else {
194                                 c->inbudget -= lenin;
195                         }
196
197                         result = EVP_DecryptUpdate(c->inctx, (unsigned char *)inbuf, &lenout, (unsigned char *)c->buffer + oldlen, lenin);
198
199                         if(!result || lenout != lenin) {
200                                 logger(LOG_ERR, "Error while decrypting metadata from %s (%s): %s",
201                                        c->name, c->hostname, ERR_error_string(ERR_get_error(), NULL));
202                                 return false;
203                         }
204
205                         memcpy(c->buffer + oldlen, inbuf, lenin);
206                         decrypted = true;
207                 }
208
209                 /* Are we receiving a TCPpacket? */
210
211                 if(c->tcplen) {
212                         if(c->tcplen <= c->buflen) {
213                                 if(c->allow_request != ALL) {
214                                         logger(LOG_ERR, "Got unauthorized TCP packet from %s (%s)", c->name, c->hostname);
215                                         return false;
216                                 }
217
218                                 receive_tcppacket(c, c->buffer, c->tcplen);
219                                 reqlen = c->tcplen;
220                                 c->tcplen = 0;
221                         }
222                 } else {
223                         /* Otherwise we are waiting for a request */
224
225                         for(i = oldlen; i < c->buflen; i++) {
226                                 if(c->buffer[i] == '\n') {
227                                         c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
228                                         c->reqlen = reqlen = i + 1;
229                                         break;
230                                 }
231                         }
232
233                         if(reqlen && !receive_request(c)) {
234                                 return false;
235                         }
236                 }
237
238 consume:
239
240                 if(reqlen) {
241                         c->buflen -= reqlen;
242                         lenin -= reqlen - oldlen;
243                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
244                         oldlen = 0;
245                         continue;
246                 } else {
247                         break;
248                 }
249         }
250
251         if(c->buflen >= MAXBUFSIZE) {
252                 logger(LOG_ERR, "Metadata read buffer overflow for %s (%s)",
253                        c->name, c->hostname);
254                 return false;
255         }
256
257         return true;
258 }