Check return value of EVP_* functions, and check if length before en/decryption
[tinc] / src / meta.c
1 /*
2     meta.c -- handle the meta communication
3     Copyright (C) 2000-2003 Guus Sliepen <guus@sliepen.eu.org>,
4                   2000-2003 Ivo Timmermans <ivo@o2w.nl>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: meta.c,v 1.1.2.45 2003/10/10 16:24:24 guus Exp $
21 */
22
23 #include "system.h"
24
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 "system.h"
34 #include "utils.h"
35
36 bool send_meta(connection_t *c, const char *buffer, int length)
37 {
38         const char *bufp;
39         int outlen;
40         char outbuf[MAXBUFSIZE];
41         int result;
42
43         cp();
44
45         ifdebug(META) logger(LOG_DEBUG, _("Sending %d bytes of metadata to %s (%s)"), length,
46                            c->name, c->hostname);
47
48         if(c->status.encryptout) {
49                 result = EVP_EncryptUpdate(c->outctx, outbuf, &outlen, buffer, length);
50                 if(!result || outlen != length) {
51                         logger(LOG_ERR, _("Error while encrypting metadata to %s (%s): %s"), ERR_error_string(ERR_get_error(), NULL));
52                         return false;
53                 }
54                 bufp = outbuf;
55                 length = outlen;
56         } else
57                 bufp = buffer;
58
59         while(length) {
60                 result = send(c->socket, bufp, length, 0);
61                 if(result <= 0) {
62                         if(!errno || errno == EPIPE) {
63                                 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
64                                                    c->name, c->hostname);
65                         } else if(errno == EINTR)
66                                 continue;
67                         else
68                                 logger(LOG_ERR, _("Sending meta data to %s (%s) failed: %s"), c->name,
69                                            c->hostname, strerror(errno));
70                         return false;
71                 }
72                 bufp += result;
73                 length -= result;
74         }
75         
76         return true;
77 }
78
79 void broadcast_meta(connection_t *from, const char *buffer, int length)
80 {
81         avl_node_t *node;
82         connection_t *c;
83
84         cp();
85
86         for(node = connection_tree->head; node; node = node->next) {
87                 c = node->data;
88
89                 if(c != from && c->status.active)
90                         send_meta(c, buffer, length);
91         }
92 }
93
94 bool receive_meta(connection_t *c)
95 {
96         int oldlen, i, result;
97         int lenin, lenout, reqlen;
98         bool decrypted = false;
99         char inbuf[MAXBUFSIZE];
100
101         cp();
102
103         /* Strategy:
104            - Read as much as possible from the TCP socket in one go.
105            - Decrypt it.
106            - Check if a full request is in the input buffer.
107            - If yes, process request and remove it from the buffer,
108            then check again.
109            - If not, keep stuff in buffer and exit.
110          */
111
112         lenin = recv(c->socket, c->buffer + c->buflen, MAXBUFSIZE - c->buflen, 0);
113
114         if(lenin <= 0) {
115                 if(!lenin || !errno) {
116                         ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection closed by %s (%s)"),
117                                            c->name, c->hostname);
118                 } else if(errno == EINTR)
119                         return true;
120                 else
121                         logger(LOG_ERR, _("Metadata socket read error for %s (%s): %s"),
122                                    c->name, c->hostname, strerror(errno));
123
124                 return false;
125         }
126
127         oldlen = c->buflen;
128         c->buflen += lenin;
129
130         while(lenin > 0) {
131                 /* Decrypt */
132
133                 if(c->status.decryptin && !decrypted) {
134                         result = EVP_DecryptUpdate(c->inctx, inbuf, &lenout, c->buffer + oldlen, lenin);
135                         if(!result || lenout != lenin) {
136                                 logger(LOG_ERR, _("Error while decrypting metadata from %s (%s): %s"), ERR_error_string(ERR_get_error(), NULL));
137                                 return false;
138                         }
139                         memcpy(c->buffer + oldlen, inbuf, lenin);
140                         decrypted = true;
141                 }
142
143                 /* Are we receiving a TCPpacket? */
144
145                 if(c->tcplen) {
146                         if(c->tcplen <= c->buflen) {
147                                 receive_tcppacket(c, c->buffer, c->tcplen);
148
149                                 c->buflen -= c->tcplen;
150                                 lenin -= c->tcplen;
151                                 memmove(c->buffer, c->buffer + c->tcplen, c->buflen);
152                                 oldlen = 0;
153                                 c->tcplen = 0;
154                                 continue;
155                         } else {
156                                 break;
157                         }
158                 }
159
160                 /* Otherwise we are waiting for a request */
161
162                 reqlen = 0;
163
164                 for(i = oldlen; i < c->buflen; i++) {
165                         if(c->buffer[i] == '\n') {
166                                 c->buffer[i] = '\0';    /* replace end-of-line by end-of-string so we can use sscanf */
167                                 reqlen = i + 1;
168                                 break;
169                         }
170                 }
171
172                 if(reqlen) {
173                         c->reqlen = reqlen;
174                         if(!receive_request(c))
175                                 return false;
176
177                         c->buflen -= reqlen;
178                         lenin -= reqlen;
179                         memmove(c->buffer, c->buffer + reqlen, c->buflen);
180                         oldlen = 0;
181                         continue;
182                 } else {
183                         break;
184                 }
185         }
186
187         if(c->buflen >= MAXBUFSIZE) {
188                 logger(LOG_ERR, _("Metadata read buffer overflow for %s (%s)"),
189                            c->name, c->hostname);
190                 return false;
191         }
192
193         c->last_ping_time = now;
194
195         return true;
196 }