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