CI: ignore package job failures
[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         free((char *)r->request);
84         free(r);
85 }
86
87 static splay_tree_t past_request_tree = {
88         .compare = (splay_compare_t) past_request_compare,
89         .delete = (splay_action_t) free_past_request,
90 };
91
92 /* Generic request routines - takes care of logging and error
93    detection as well */
94
95 bool send_request(connection_t *c, const char *format, ...) {
96         va_list args;
97         char request[MAXBUFSIZE];
98         int len;
99
100         /* Use vsnprintf instead of vxasprintf: faster, no memory
101            fragmentation, cleanup is automatic, and there is a limit on the
102            input buffer anyway */
103
104         va_start(args, format);
105         len = vsnprintf(request, sizeof(request), format, args);
106         request[sizeof(request) - 1] = 0;
107         va_end(args);
108
109         if(len < 0 || (size_t)len > sizeof(request) - 1) {
110                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
111                        c->name, c->hostname);
112                 return false;
113         }
114
115         int id = atoi(request);
116         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", get_request_entry(id)->name, c->name, c->hostname, request);
117
118         request[len++] = '\n';
119
120         if(c == everyone) {
121                 broadcast_meta(NULL, request, len);
122                 return true;
123         } else {
124                 if(id) {
125                         return send_meta(c, request, len);
126                 } else {
127                         send_meta_raw(c, request, len);
128                         return true;
129                 }
130         }
131 }
132
133 void forward_request(connection_t *from, const char *request) {
134         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", get_request_entry(atoi(request))->name, from->name, from->hostname, request);
135
136         // Create a temporary newline-terminated copy of the request
137         size_t len = strlen(request);
138         const size_t tmplen = len + 1;
139         char *tmp = alloca(tmplen);
140         memcpy(tmp, request, len);
141         tmp[len] = '\n';
142         broadcast_meta(from, tmp, tmplen);
143 }
144
145 bool receive_request(connection_t *c, const char *request) {
146         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
147                 if(!request[0] || request[0] == '\r') {
148                         return true;
149                 }
150
151                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
152                         if(!strncmp(request + 9, "200", 3)) {
153                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
154                                 return true;
155                         } else {
156                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
157                                 return false;
158                         }
159                 }
160         }
161
162         int reqno = atoi(request);
163
164         if(reqno || *request == '0') {
165                 if(!is_valid_request(reqno) || !get_request_entry(reqno)->handler) {
166                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
167                         return false;
168                 }
169
170                 const request_entry_t *entry = get_request_entry(reqno);
171                 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", entry->name, c->name, c->hostname, request);
172
173                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
174                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
175                         return false;
176                 }
177
178                 if(!entry->handler(c, request)) {
179                         /* Something went wrong. Probably scriptkiddies. Terminate. */
180
181                         if(reqno != TERMREQ) {
182                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", entry->name, c->name, c->hostname);
183                         }
184
185                         return false;
186                 }
187         } else {
188                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
189                 return false;
190         }
191
192         return true;
193 }
194
195 static timeout_t past_request_timeout;
196
197 static void age_past_requests(void *data) {
198         (void)data;
199         int left = 0, deleted = 0;
200
201         for splay_each(past_request_t, p, &past_request_tree) {
202                 if(p->firstseen + pinginterval <= now.tv_sec) {
203                         splay_delete_node(&past_request_tree, node), deleted++;
204                 } else {
205                         left++;
206                 }
207         }
208
209         if(left || deleted) {
210                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
211         }
212
213         if(left)
214                 timeout_set(&past_request_timeout, &(struct timeval) {
215                 10, jitter()
216         });
217 }
218
219 bool seen_request(const char *request) {
220         past_request_t *new, p = {0};
221
222         p.request = request;
223
224         if(splay_search(&past_request_tree, &p)) {
225                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
226                 return true;
227         } else {
228                 new = xmalloc(sizeof(*new));
229                 new->request = xstrdup(request);
230                 new->firstseen = now.tv_sec;
231                 splay_insert(&past_request_tree, new);
232                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
233                         10, jitter()
234                 });
235                 return false;
236         }
237 }
238
239 void exit_requests(void) {
240         splay_empty_tree(&past_request_tree);
241
242         timeout_del(&past_request_timeout);
243 }