Wipe (some) secrets from memory after use
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2022 Guus Sliepen <guus@tinc-vpn.org>
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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "crypto.h"
26 #include "logger.h"
27 #include "meta.h"
28 #include "protocol.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 bool tunnelserver = false;
33 bool strictsubnets = false;
34 bool experimental = true;
35
36 static inline bool is_valid_request(request_t req) {
37         return req > ALL && req < LAST;
38 }
39
40 /* Request handlers */
41 const request_entry_t *get_request_entry(request_t req) {
42         if(!is_valid_request(req)) {
43                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid request %d", req);
44                 return NULL;
45         }
46
47         // Prevent user from accessing the table directly to always have bound checks
48         static const request_entry_t request_entries[] = {
49                 [ID] = {id_h, "ID"},
50                 [METAKEY] = {metakey_h, "METAKEY"},
51                 [CHALLENGE] = {challenge_h, "CHALLENGE"},
52                 [CHAL_REPLY] = {chal_reply_h, "CHAL_REPLY"},
53                 [ACK] = {ack_h, "ACK"},
54                 [STATUS] = {NULL, "STATUS"},
55                 [ERROR] = {NULL, "ERROR"},
56                 [TERMREQ] = {termreq_h, "TERMREQ"},
57                 [PING] = {ping_h, "PING"},
58                 [PONG] = {pong_h, "PONG"},
59                 [ADD_SUBNET] = {add_subnet_h, "ADD_SUBNET"},
60                 [DEL_SUBNET] = {del_subnet_h, "DEL_SUBNET"},
61                 [ADD_EDGE] = {add_edge_h, "ADD_EDGE"},
62                 [DEL_EDGE] = {del_edge_h, "DEL_EDGE"},
63                 [KEY_CHANGED] = {key_changed_h, "KEY_CHANGED"},
64                 [REQ_KEY] = {req_key_h, "REQ_KEY"},
65                 [ANS_KEY] = {ans_key_h, "ANS_KEY"},
66                 [PACKET] = {tcppacket_h, "PACKET"},
67                 [CONTROL] = {control_h, "CONTROL"},
68                 /* Not "real" requests yet */
69                 [REQ_PUBKEY] = {NULL, "REQ_PUBKEY"},
70                 [ANS_PUBKEY] = {NULL, "ANS_PUBKEY"},
71                 [SPTPS_PACKET] = {sptps_tcppacket_h, "SPTPS_PACKET"},
72                 [UDP_INFO] = {udp_info_h, "UDP_INFO"},
73                 [MTU_INFO] = {mtu_info_h, "MTU_INFO"},
74         };
75         return &request_entries[req];
76 }
77
78 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
79         return strcmp(a->request, b->request);
80 }
81
82 static void free_past_request(past_request_t *r) {
83         if(r) {
84                 free((char *)r->request);
85                 free(r);
86         }
87 }
88
89 static splay_tree_t past_request_tree = {
90         .compare = (splay_compare_t) past_request_compare,
91         .delete = (splay_action_t) free_past_request,
92 };
93
94 /* Generic request routines - takes care of logging and error
95    detection as well */
96
97 bool send_request(connection_t *c, const char *format, ...) {
98         va_list args;
99         char request[MAXBUFSIZE];
100         int len;
101
102         /* Use vsnprintf instead of vxasprintf: faster, no memory
103            fragmentation, cleanup is automatic, and there is a limit on the
104            input buffer anyway */
105
106         va_start(args, format);
107         len = vsnprintf(request, sizeof(request), format, args);
108         request[sizeof(request) - 1] = 0;
109         va_end(args);
110
111         if(len < 0 || (size_t)len > sizeof(request) - 1) {
112                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
113                        c->name, c->hostname);
114                 return false;
115         }
116
117         int id = atoi(request);
118         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", get_request_entry(id)->name, c->name, c->hostname, request);
119
120         request[len++] = '\n';
121
122         if(c == everyone) {
123                 broadcast_meta(NULL, request, len);
124                 return true;
125         } else {
126                 if(id) {
127                         return send_meta(c, request, len);
128                 } else {
129                         send_meta_raw(c, request, len);
130                         return true;
131                 }
132         }
133 }
134
135 void forward_request(connection_t *from, const char *request) {
136         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", get_request_entry(atoi(request))->name, from->name, from->hostname, request);
137
138         // Create a temporary newline-terminated copy of the request
139         size_t len = strlen(request);
140         const size_t tmplen = len + 1;
141         char *tmp = alloca(tmplen);
142         memcpy(tmp, request, len);
143         tmp[len] = '\n';
144         broadcast_meta(from, tmp, tmplen);
145 }
146
147 bool receive_request(connection_t *c, const char *request) {
148         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
149                 if(!request[0] || request[0] == '\r') {
150                         return true;
151                 }
152
153                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
154                         if(!strncmp(request + 9, "200", 3)) {
155                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
156                                 return true;
157                         } else {
158                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
159                                 return false;
160                         }
161                 }
162         }
163
164         int reqno = atoi(request);
165
166         if(reqno || *request == '0') {
167                 if(!is_valid_request(reqno) || !get_request_entry(reqno)->handler) {
168                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
169                         return false;
170                 }
171
172                 const request_entry_t *entry = get_request_entry(reqno);
173                 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", entry->name, c->name, c->hostname, request);
174
175                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
176                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
177                         return false;
178                 }
179
180                 if(!entry->handler(c, request)) {
181                         /* Something went wrong. Probably scriptkiddies. Terminate. */
182
183                         if(reqno != TERMREQ) {
184                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", entry->name, c->name, c->hostname);
185                         }
186
187                         return false;
188                 }
189         } else {
190                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
191                 return false;
192         }
193
194         return true;
195 }
196
197 static timeout_t past_request_timeout;
198
199 static void age_past_requests(void *data) {
200         (void)data;
201         int left = 0, deleted = 0;
202
203         for splay_each(past_request_t, p, &past_request_tree) {
204                 if(p->firstseen + pinginterval <= now.tv_sec) {
205                         splay_delete_node(&past_request_tree, node), deleted++;
206                 } else {
207                         left++;
208                 }
209         }
210
211         if(left || deleted) {
212                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
213         }
214
215         if(left)
216                 timeout_set(&past_request_timeout, &(struct timeval) {
217                 10, jitter()
218         });
219 }
220
221 bool seen_request(const char *request) {
222         past_request_t *new, p = {0};
223
224         p.request = request;
225
226         if(splay_search(&past_request_tree, &p)) {
227                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
228                 return true;
229         } else {
230                 new = xmalloc(sizeof(*new));
231                 new->request = xstrdup(request);
232                 new->firstseen = now.tv_sec;
233                 splay_insert(&past_request_tree, new);
234                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
235                         10, jitter()
236                 });
237                 return false;
238         }
239 }
240
241 void exit_requests(void) {
242         splay_empty_tree(&past_request_tree);
243
244         timeout_del(&past_request_timeout);
245 }