Start of control socket implementation.
[tinc] / src / tincctl.c
1 /*
2     tincctl.c -- Controlling a running tincd
3     Copyright (C) 2007 Guus Sliepen <guus@tinc-vpn.org>
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include "system.h"
23
24 #include <sys/un.h>
25 #include <openssl/rand.h>
26 #include <openssl/rsa.h>
27 #include <openssl/pem.h>
28 #include <openssl/evp.h>
29 #include <openssl/engine.h>
30
31 #include <getopt.h>
32
33 #include "conf.h"
34 #include "protocol.h"
35 #include "xalloc.h"
36
37 /* The name this program was run with. */
38 char *program_name = NULL;
39
40 /* If nonzero, display usage information and exit. */
41 bool show_help = false;
42
43 /* If nonzero, print the version on standard output and exit.  */
44 bool show_version = false;
45
46 /* If nonzero, it will attempt to kill a running tincd and exit. */
47 int kill_tincd = 0;
48
49 /* If nonzero, generate public/private keypair for this host/net. */
50 int generate_keys = 0;
51
52 char *identname = NULL;                         /* program name for syslog */
53 char *pidfilename = NULL;                       /* pid file location */
54 char *controlfilename = NULL;                   /* pid file location */
55 char *confbase = NULL;
56 char *netname = NULL;
57
58 static int status;
59
60 static struct option const long_options[] = {
61         {"config", required_argument, NULL, 'c'},
62         {"net", required_argument, NULL, 'n'},
63         {"help", no_argument, NULL, 1},
64         {"version", no_argument, NULL, 2},
65         {"pidfile", required_argument, NULL, 5},
66         {NULL, 0, NULL, 0}
67 };
68
69 static void usage(bool status) {
70         if(status)
71                 fprintf(stderr, _("Try `%s --help\' for more information.\n"),
72                                 program_name);
73         else {
74                 printf(_("Usage: %s [options] command\n\n"), program_name);
75                 printf(_("Valid options are:\n"
76                                 "  -c, --config=DIR           Read configuration options from DIR.\n"
77                                 "  -n, --net=NETNAME          Connect to net NETNAME.\n"
78                                 "      --pidfile=FILENAME     Write PID to FILENAME.\n"
79                                 "      --help                 Display this help and exit.\n"
80                                 "      --version              Output version information and exit.\n"
81                                 "Valid commands are:\n"
82                                 "  start                      Start tincd.\n"
83                                 "  stop                       Stop tincd.\n"
84                                 "  restart                    Restart tincd.\n"
85                                 "  reload                     Reload configuration of running tincd.\n"
86                                 "  genkey [bits]              Generate a new public/private keypair.\n"
87                                 "  dump                       Dump a list of one of the following things:\n"
88                                 "    nodes                    - all known nodes in the VPN\n"
89                                 "    edges                    - all known connections in the VPN\n"
90                                 "    subnets                  - all known subnets in the VPN\n"
91                                 "    connections              - all meta connections with ourself\n"
92                                 "    graph                    - graph of the VPN in dotty format\n"
93                                 "\n"));
94                 printf(_("Report bugs to tinc@tinc-vpn.org.\n"));
95         }
96 }
97
98 static bool parse_options(int argc, char **argv) {
99         int r;
100         int option_index = 0;
101
102         while((r = getopt_long(argc, argv, "c:n:", long_options, &option_index)) != EOF) {
103                 switch (r) {
104                         case 0:                         /* long option */
105                                 break;
106
107                         case 'c':                               /* config file */
108                                 confbase = xstrdup(optarg);
109                                 break;
110
111                         case 'n':                               /* net name given */
112                                 netname = xstrdup(optarg);
113                                 break;
114
115                         case 1:                                 /* show help */
116                                 show_help = true;
117                                 break;
118
119                         case 2:                                 /* show version */
120                                 show_version = true;
121                                 break;
122
123                         case 5:                                 /* write PID to a file */
124                                 pidfilename = xstrdup(optarg);
125                                 break;
126
127                         case '?':
128                                 usage(true);
129                                 return false;
130
131                         default:
132                                 break;
133                 }
134         }
135
136         return true;
137 }
138
139 /* This function prettyprints the key generation process */
140
141 static void indicator(int a, int b, void *p) {
142         switch (a) {
143                 case 0:
144                         fprintf(stderr, ".");
145                         break;
146
147                 case 1:
148                         fprintf(stderr, "+");
149                         break;
150
151                 case 2:
152                         fprintf(stderr, "-");
153                         break;
154
155                 case 3:
156                         switch (b) {
157                                 case 0:
158                                         fprintf(stderr, " p\n");
159                                         break;
160
161                                 case 1:
162                                         fprintf(stderr, " q\n");
163                                         break;
164
165                                 default:
166                                         fprintf(stderr, "?");
167                         }
168                         break;
169
170                 default:
171                         fprintf(stderr, "?");
172         }
173 }
174
175 /*
176   Generate a public/private RSA keypair, and ask for a file to store
177   them in.
178 */
179 static bool keygen(int bits) {
180         RSA *rsa_key;
181         FILE *f;
182         char *name = NULL;
183         char *filename;
184
185         fprintf(stderr, _("Generating %d bits keys:\n"), bits);
186         rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
187
188         if(!rsa_key) {
189                 fprintf(stderr, _("Error during key generation!\n"));
190                 return false;
191         } else
192                 fprintf(stderr, _("Done.\n"));
193
194         asprintf(&filename, "%s/rsa_key.priv", confbase);
195         f = ask_and_open(filename, _("private RSA key"), "a");
196
197         if(!f)
198                 return false;
199   
200 #ifdef HAVE_FCHMOD
201         /* Make it unreadable for others. */
202         fchmod(fileno(f), 0600);
203 #endif
204                 
205         if(ftell(f))
206                 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
207
208         PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
209         fclose(f);
210         free(filename);
211
212         if(name)
213                 asprintf(&filename, "%s/hosts/%s", confbase, name);
214         else
215                 asprintf(&filename, "%s/rsa_key.pub", confbase);
216
217         f = ask_and_open(filename, _("public RSA key"), "a");
218
219         if(!f)
220                 return false;
221
222         if(ftell(f))
223                 fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
224
225         PEM_write_RSAPublicKey(f, rsa_key);
226         fclose(f);
227         free(filename);
228
229         return true;
230 }
231
232 /*
233   Set all files and paths according to netname
234 */
235 static void make_names(void) {
236 #ifdef HAVE_MINGW
237         HKEY key;
238         char installdir[1024] = "";
239         long len = sizeof(installdir);
240 #endif
241
242         if(netname)
243                 asprintf(&identname, "tinc.%s", netname);
244         else
245                 identname = xstrdup("tinc");
246
247 #ifdef HAVE_MINGW
248         if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
249                 if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
250                         if(!logfilename)
251                                 asprintf(&logfilename, "%s/log/%s.log", identname);
252                         if(!confbase) {
253                                 if(netname)
254                                         asprintf(&confbase, "%s/%s", installdir, netname);
255                                 else
256                                         asprintf(&confbase, "%s", installdir);
257                         }
258                 }
259                 RegCloseKey(key);
260                 if(*installdir)
261                         return;
262         }
263 #endif
264
265         if(!pidfilename)
266                 asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
267
268         asprintf(&controlfilename, LOCALSTATEDIR "/run/%s.control", identname);
269
270         if(netname) {
271                 if(!confbase)
272                         asprintf(&confbase, CONFDIR "/tinc/%s", netname);
273                 else
274                         fprintf(stderr, _("Both netname and configuration directory given, using the latter...\n"));
275         } else {
276                 if(!confbase)
277                         asprintf(&confbase, CONFDIR "/tinc");
278         }
279 }
280
281 int main(int argc, char **argv) {
282         int fd;
283         struct sockaddr_un addr;
284         program_name = argv[0];
285
286         setlocale(LC_ALL, "");
287         bindtextdomain(PACKAGE, LOCALEDIR);
288         textdomain(PACKAGE);
289
290         if(!parse_options(argc, argv))
291                 return 1;
292         
293         make_names();
294
295         if(show_version) {
296                 printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
297                            VERSION, __DATE__, __TIME__, PROT_CURRENT);
298                 printf(_("Copyright (C) 1998-2007 Ivo Timmermans, Guus Sliepen and others.\n"
299                                 "See the AUTHORS file for a complete list.\n\n"
300                                 "tinc comes with ABSOLUTELY NO WARRANTY.  This is free software,\n"
301                                 "and you are welcome to redistribute it under certain conditions;\n"
302                                 "see the file COPYING for details.\n"));
303
304                 return 0;
305         }
306
307         if(show_help) {
308                 usage(false);
309                 return 0;
310         }
311
312         if(strlen(controlfilename) >= sizeof addr.sun_path) {
313                 fprintf(stderr, _("Control socket filename too long!\n"));
314                 return 1;
315         }
316
317         fd = socket(PF_UNIX, SOCK_STREAM, 0);
318         if(fd < 0) {
319                 fprintf(stderr, _("Cannot create UNIX socket: %s\n"), strerror(errno));
320                 return 1;
321         }
322
323         memset(&addr, 0, sizeof addr);
324         addr.sun_family = AF_UNIX;
325         strncpy(addr.sun_path, controlfilename, sizeof addr.sun_path - 1);
326
327         if(connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
328                 fprintf(stderr, _("Cannot connect to %s: %s\n"), controlfilename, strerror(errno));
329                 return 1;
330         }
331
332         printf("Connected to %s.\n", controlfilename);
333
334         close(fd);
335
336         return 0;
337 }