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