]> git.meshlink.io Git - meshlink/blob - src/libmeshlink.c
Working tinc_setup() function in the library
[meshlink] / src / libmeshlink.c
1 /*
2     libmeshlink.h -- Tincd Library
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org> Saverio Proto <zioproto@gmail.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "libmeshlink.h"
21 #include "crypto.h"
22 #include "ecdsagen.h"
23 char *hosts_dir = NULL;
24 static char *name = NULL;
25 char *tinc_conf = NULL;
26
27
28 /*
29   Generate a public/private ECDSA keypair, and ask for a file to store
30   them in.
31 */
32 bool ecdsa_keygen(bool ask) {
33         ecdsa_t *key;
34         FILE *f;
35         char *pubname, *privname;
36
37         fprintf(stderr, "Generating ECDSA keypair:\n");
38
39         if(!(key = ecdsa_generate())) {
40                 fprintf(stderr, "Error during key generation!\n");
41                 return false;
42         } else
43                 fprintf(stderr, "Done.\n");
44
45         xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
46         //f = ask_and_open(privname, "private ECDSA key", "a", ask, 0600); //this function is not ported to lib because makes no sense
47         free(privname);
48
49         if(!f)
50                 return false;
51
52         if(!ecdsa_write_pem_private_key(key, f)) {
53                 fprintf(stderr, "Error writing private key!\n");
54                 ecdsa_free(key);
55                 fclose(f);
56                 return false;
57         }
58
59         fclose(f);
60
61         if(name)
62                 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
63         else
64                 xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
65
66         //f = ask_and_open(pubname, "public ECDSA key", "a", ask, 0666);
67         free(pubname);
68
69         if(!f)
70                 return false;
71
72         char *pubkey = ecdsa_get_base64_public_key(key);
73         fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
74         free(pubkey);
75
76         fclose(f);
77         ecdsa_free(key);
78
79         return true;
80 }
81
82 /*
83   Generate a public/private RSA keypair, and ask for a file to store
84   them in.
85 */
86 bool rsa_keygen(int bits, bool ask) {
87         rsa_t *key;
88         FILE *f;
89         char *pubname, *privname;
90
91         fprintf(stderr, "Generating %d bits keys:\n", bits);
92
93         if(!(key = rsa_generate(bits, 0x10001))) {
94                 fprintf(stderr, "Error during key generation!\n");
95                 return false;
96         } else
97                 fprintf(stderr, "Done.\n");
98
99         xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
100         //f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
101         free(privname);
102
103         if(!f)
104                 return false;
105
106         if(!rsa_write_pem_private_key(key, f)) {
107                 fprintf(stderr, "Error writing private key!\n");
108                 fclose(f);
109                 rsa_free(key);
110                 return false;
111         }
112
113         fclose(f);
114
115         if(name)
116                 xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
117         else
118                 xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
119
120         //f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
121         free(pubname);
122
123         if(!f)
124                 return false;
125
126         if(!rsa_write_pem_public_key(key, f)) {
127                 fprintf(stderr, "Error writing public key!\n");
128                 fclose(f);
129                 rsa_free(key);
130                 return false;
131         }
132
133         fclose(f);
134         rsa_free(key);
135
136         return true;
137 }
138
139 static bool try_bind(int port) {
140         struct addrinfo *ai = NULL;
141         struct addrinfo hint = {
142                 .ai_flags = AI_PASSIVE,
143                 .ai_family = AF_UNSPEC,
144                 .ai_socktype = SOCK_STREAM,
145                 .ai_protocol = IPPROTO_TCP,
146         };
147
148         char portstr[16];
149         snprintf(portstr, sizeof portstr, "%d", port);
150
151         if(getaddrinfo(NULL, portstr, &hint, &ai) || !ai)
152                 return false;
153
154         while(ai) {
155                 int fd = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
156                 if(!fd)
157                         return false;
158                 int result = bind(fd, ai->ai_addr, ai->ai_addrlen);
159                 closesocket(fd);
160                 if(result)
161                         return false;
162                 ai = ai->ai_next;
163         }
164
165         return true;
166 }
167
168 int check_port(char *name) {
169         if(try_bind(655))
170                 return 655;
171
172         fprintf(stderr, "Warning: could not bind to port 655. ");
173
174         for(int i = 0; i < 100; i++) {
175                 int port = 0x1000 + (rand() & 0x7fff);
176                 if(try_bind(port)) {
177                         char *filename;
178                         xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
179                         FILE *f = fopen(filename, "a");
180                         free(filename);
181                         if(!f) {
182                                 fprintf(stderr, "Please change tinc's Port manually.\n");
183                                 return 0;
184                         }
185
186                         fprintf(f, "Port = %d\n", port);
187                         fclose(f);
188                         fprintf(stderr, "Tinc will instead listen on port %d.\n", port);
189                         return port;
190                 }
191         }
192
193         fprintf(stderr, "Please change tinc's Port manually.\n");
194         return 0;
195 }
196 //tinc_setup() should basically do what cmd_init() from src/tincctl.c does, except it doesn't have to generate a tinc-up script.
197 bool tinc_setup(const char* confbase, const char* name) {
198         make_names();
199         xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
200         xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
201         if(!access(tinc_conf, F_OK)) {
202                 fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
203                 return false;
204         }
205
206         if(!check_id(name)) {
207                 fprintf(stderr, "Invalid Name! Only a-z, A-Z, 0-9 and _ are allowed characters.\n");
208                 return false;
209         }
210
211         if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
212                 fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
213                 return false;
214         }
215
216         if(mkdir(confbase, 0777) && errno != EEXIST) {
217                 fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
218                 return false;
219         }
220
221         if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
222                 fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
223                 return false;
224         }
225
226         FILE *f = fopen(tinc_conf, "w");
227         if(!f) {
228                 fprintf(stderr, "Could not create file %s: %s\n", tinc_conf, strerror(errno));
229                 return 1;
230         }
231
232         fprintf(f, "Name = %s\n", name);
233         fclose(f);
234
235         if(!rsa_keygen(2048, false) || !ecdsa_keygen(false))
236                 return false;
237
238         check_port(name);
239
240         return true;
241
242 }
243
244
245 bool tinc_start(const char* path);
246
247 bool tinc_stop();
248
249 // can be called from any thread
250 bool tinc_send_packet(node_t *receiver, const char* buf, unsigned int len);
251
252 // handler runs in tinc thread and should return immediately
253 bool tinc_set_packet_receive_handler(void (*handler)(const char* sender, const char* buf, unsigned int len));
254
255
256 //It might also be a good idea to add the option of looking up hosts by public
257 //key (fingerprints) instead of names.
258
259 node_t *tinc_get_host(const char *name);
260
261 bool tinc_get_hosts(node_t** hosts);
262
263 bool tinc_sign(const char* payload, unsigned int len, const char** signature);
264
265 int tinc_verify(const char* sender, const char* payload, unsigned int plen, const char* signature, unsigned int slen);
266
267 /*
268 TODO: It would be good to add a void pointer here that will be passed on to the
269 handler function whenever it is called, or have a void pointer in node_t
270 that can be filled in by the application. That way, you can easily link an
271 application-specific data structure to a node_t.
272 */
273 void channel_set_packet_send_handler(int (*handler)(const char* receiver, const char* buf, unsigned int len));
274 void channel_packet_receive_handler(const char* sender, const char* buf, unsigned int len);
275
276 bool channel_open(const char* partner, void(*read)(int id, const char* buf, unsigned int len), void(*result)(int result, int id));
277 void channel_close(int id);
278 bool channel_write(int id, const char* buf, unsigned int len, void(*result)(int result, int id, unsigned int written));
279
280
281 //We do need some more functions. First of all, we need to be able to add nodes
282 //to a VPN. To do that, either an invitation protocol should be used:
283
284 bool tinc_join_network(const char *invitation);
285 const char *tinc_generate_invitation(const char *name);
286
287 /*
288 Or two nodes should exchange some information (their name, address, public
289 key). If the application provides a way to exchange some data with another
290 node, then:
291 */
292
293 bool tinc_export(char *buf, size_t *len);
294 node_t *tinc_import(const char *buf, size_t len);
295 /*
296 Last but not least, some way to get rid of unwanted nodes. Simplest is a
297 function that just blacklists a node.
298 Which should immediately cause the local tincd to ignore any data from that
299 host from that point on. Of course, a somewhat centrally managed,
300 automatically distributed blacklist or whitelist would be the next step.
301 */
302 bool tinc_blacklist(node_t *host);
303
304
305
306