client: Factor out disconnect command
[platform/upstream/connman.git] / client / commands.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <getopt.h>
32
33 #include <glib.h>
34 #include <gdbus.h>
35
36 #include "services.h"
37 #include "technology.h"
38 #include "data_manager.h"
39 #include "monitor.h"
40 #include "interactive.h"
41
42 #define MANDATORY_ARGS 3
43
44 static char *ipv4[] = {
45         "Method",
46         "Address",
47         "Netmask",
48         "Gateway",
49         NULL
50 };
51
52 static char *ipv6[] = {
53         "Method",
54         "Address",
55         "PrefixLength",
56         "Gateway",
57         "Privacy",
58         NULL
59 };
60
61 static char *proxy_simple[] = {
62         "Method",
63         "URL",
64         NULL
65 };
66
67 static int cmd_help(char *args[], int num, struct option *options);
68
69 static int parse_args(char *arg, struct option *options)
70 {
71         int i;
72
73         if (arg == NULL)
74                 return -1;
75
76         for (i = 0; options[i].name != NULL; i++) {
77                 if (strcmp(options[i].name, arg) == 0 ||
78                                 (strncmp(arg, "--", 2) == 0 &&
79                                         strcmp(&arg[2], options[i].name) == 0))
80                         return options[i].val;
81         }
82
83         return '?';
84 }
85
86 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
87 {
88         int error;
89
90         switch (c) {
91         case 's':
92                 error = monitor_connman(conn, "Service", "PropertyChanged");
93                 if (error != 0)
94                         return error;
95                 if (dbus_connection_add_filter(conn, service_property_changed,
96                                                         NULL, NULL) == FALSE)
97                         return -ENOMEM;
98                 printf("Now monitoring the service interface.\n");
99                 break;
100         case 'c':
101                 error = monitor_connman(conn, "Technology", "PropertyChanged");
102                 if (error != 0)
103                         return error;
104                 if (dbus_connection_add_filter(conn, tech_property_changed,
105                                                         NULL, NULL) == FALSE)
106                         return -ENOMEM;
107                 printf("Now monitoring the technology interface.\n");
108                 break;
109         case 'm':
110                 error = monitor_connman(conn, "Manager", "PropertyChanged");
111                 if (error != 0)
112                         return error;
113                 error = monitor_connman(conn, "Manager", "TechnologyAdded");
114                 if (error != 0)
115                         return error;
116                 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
117                 if (error != 0)
118                         return error;
119                 error = monitor_connman(conn, "Manager", "ServicesChanged");
120                 if (error != 0)
121                         return error;
122                 if (dbus_connection_add_filter(conn, manager_property_changed,
123                                                         NULL, NULL) == FALSE)
124                         return -ENOMEM;
125                 if (dbus_connection_add_filter(conn, tech_added_removed,
126                                                         NULL, NULL) == FALSE)
127                         return -ENOMEM;
128                 if (dbus_connection_add_filter(conn, manager_services_changed,
129                                                         NULL, NULL) == FALSE)
130                         return -ENOMEM;
131                 printf("Now monitoring the manager interface.\n");
132                 break;
133         default:
134                 fprintf(stderr, "Command not recognized, please check help\n");
135                 return -EINVAL;
136                 break;
137         }
138         return 0;
139 }
140
141 static int cmd_enable(char *args[], int num, struct option *options)
142 {
143         return -1;
144 }
145
146 static int cmd_disable(char *args[], int num, struct option *options)
147 {
148         return -1;
149 }
150
151 static int cmd_state(char *args[], int num, struct option *options)
152 {
153         if (num > 1)
154                 return -E2BIG;
155
156         return list_properties(connection, "GetProperties", NULL);
157 }
158
159 static int cmd_services(char *args[], int num, struct option *options)
160 {
161         char *service_name = NULL;
162         int err = 0;
163         int c;
164         DBusMessage *message;
165
166         if (num > 3)
167                 return -E2BIG;
168
169         c = parse_args(args[1], options);
170         switch (c) {
171         case -1:
172                 break;
173         case 'p':
174                 if (num < 3)
175                         return -EINVAL;
176                 service_name = args[2];
177                 break;
178         default:
179                 if (num > 2)
180                         return -E2BIG;
181                 service_name = args[1];
182                 break;
183         }
184
185         message = get_message(connection, "GetServices");
186         if (message == NULL)
187                 return -ENOMEM;
188
189         err = list_properties(connection, "GetServices", service_name);
190         dbus_message_unref(message);
191
192         return err;
193 }
194
195 static int cmd_technologies(char *args[], int num, struct option *options)
196 {
197         if (num > 1)
198                 return -E2BIG;
199
200         return list_properties(connection, "GetTechnologies", NULL);
201 }
202
203 static int cmd_scan(char *args[], int num, struct option *options)
204 {
205         return -1;
206 }
207
208 static int cmd_connect(char *args[], int num, struct option *options)
209 {
210         int err;
211
212         if (num > 2)
213                 return -E2BIG;
214
215         if (num < 2)
216                 return -EINVAL;
217
218         err = connect_service(connection, args[1]);
219         if (err == 0)
220                 printf("Connected\n");
221
222         return 0;
223 }
224
225 static int cmd_disconnect(char *args[], int num, struct option *options)
226 {
227         int err;
228
229         if (num > 2)
230                 return -E2BIG;
231
232         if (num < 2)
233                 return -EINVAL;
234
235         err = disconnect_service(connection, args[1]);
236         if (err == 0)
237                 printf("Disconnected\n");
238
239         return 0;
240 }
241
242 static int cmd_config(char *args[], int num, struct option *options)
243 {
244         int res = 0, index = 2, oldindex = 0;
245         int c;
246         char *service_name;
247         DBusMessage *message;
248         char **opt_start;
249         dbus_bool_t val;
250
251         service_name = args[1];
252         if (service_name == NULL)
253                 return -EINVAL;
254
255         while (index < num && args[index] != NULL) {
256                 c = parse_args(args[index], options);
257                 opt_start = &args[index + 1];
258                 res = 0;
259
260                 message = get_message(connection, "GetServices");
261                 if (message == NULL)
262                         return -ENOMEM;
263
264                 oldindex = index;
265
266                 switch (c) {
267                 case 'a':
268                         switch (parse_boolean(*opt_start)) {
269                         case 1:
270                                 val = TRUE;
271                                 break;
272                         case 0:
273                                 val = FALSE;
274                                 break;
275                         default:
276                                 res = -EINVAL;
277                                 break;
278                         }
279                         if (res == 0)
280                                 res = set_service_property(connection, message,
281                                                 service_name, "AutoConnect",
282                                                 NULL, &val, 0);
283                         break;
284                 case 'i':
285                         res = set_service_property(connection, message,
286                                         service_name, "IPv4.Configuration",
287                                         ipv4, opt_start, 0);
288                         if (res < 0)
289                                 index += 4;
290                         break;
291                 case 'v':
292                         res = set_service_property(connection, message,
293                                         service_name, "IPv6.Configuration",
294                                         ipv6, opt_start, 0);
295                         if (res < 0)
296                                 index += 5;
297                         break;
298                 case 'n':
299                         res = set_service_property(connection, message,
300                                         service_name,
301                                         "Nameservers.Configuration",
302                                         NULL, opt_start, 0);
303                         break;
304                 case 't':
305                         res = set_service_property(connection, message,
306                                         service_name,
307                                         "Timeservers.Configuration",
308                                         NULL, opt_start, 0);
309                         break;
310                 case 'd':
311                         res = set_service_property(connection, message,
312                                         service_name,
313                                         "Domains.Configuration",
314                                         NULL, opt_start, 0);
315                         break;
316                 case 'x':
317                         if (*opt_start == NULL) {
318                                 res = -EINVAL;
319                                 break;
320                         }
321
322                         if (strcmp(*opt_start, "direct") == 0) {
323                                 res = set_service_property(connection, message,
324                                                 service_name,
325                                                 "Proxy.Configuration",
326                                                 proxy_simple, opt_start, 1);
327                                 break;
328                         }
329
330                         if (strcmp(*opt_start, "auto") == 0) {
331                                 res = set_service_property(connection, message,
332                                                 service_name,
333                                                 "Proxy.Configuration",
334                                                 proxy_simple, opt_start, 1);
335                                 break;
336                         }
337
338                         if (strcmp(*opt_start, "manual") == 0) {
339                                         char **url_start = &args[index + 2];
340
341                                         if (*url_start != NULL &&
342                                                 strcmp(*url_start,
343                                                         "servers") == 0) {
344                                                 url_start = &args[index + 3];
345                                                 index++;
346                                         }
347                                         res = store_proxy_input(connection,
348                                                         message, service_name,
349                                                         0, url_start);
350                         }
351
352                         break;
353                 case 'r':
354                         res = remove_service(connection, message, service_name);
355                         break;
356                 default:
357                         res = -EINVAL;
358                         break;
359                 }
360
361                 dbus_message_unref(message);
362
363                 if (res < 0) {
364                         printf("Error '%s': %s\n", args[oldindex],
365                                         strerror(-res));
366                 } else
367                         index += res;
368
369                 index++;
370         }
371
372         return 0;
373 }
374
375 static int cmd_monitor(char *args[], int num, struct option *options)
376 {
377         return -1;
378 }
379
380 static int cmd_exit(char *args[], int num, struct option *options)
381 {
382         return 0;
383 }
384
385 static struct option service_options[] = {
386         {"properties", required_argument, 0, 'p'},
387         { NULL, }
388 };
389
390 static const char *service_desc[] = {
391         "[<service>]      (obsolete)",
392         NULL
393 };
394
395 static struct option config_options[] = {
396         {"nameservers", required_argument, 0, 'n'},
397         {"timeservers", required_argument, 0, 't'},
398         {"domains", required_argument, 0, 'd'},
399         {"ipv6", required_argument, 0, 'v'},
400         {"proxy", required_argument, 0, 'x'},
401         {"autoconnect", required_argument, 0, 'a'},
402         {"ipv4", required_argument, 0, 'i'},
403         {"remove", 0, 0, 'r'},
404         { NULL, }
405 };
406
407 static const char *config_desc[] = {
408         "<dns1> [<dns2>] [<dns3>]",
409         "<ntp1> [<ntp2>] [...]",
410         "<domain1> [<domain2>] [...]",
411         "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
412         "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
413         "                   [exclude <exclude1> [<exclude2>] [...]]",
414         "yes|no",
415         "off|dhcp|manual <address> <prefixlength> <gateway>",
416         "                 Remove service",
417         NULL
418 };
419
420 static struct option monitor_options[] = {
421         {"services", no_argument, 0, 's'},
422         {"tech", no_argument, 0, 'c'},
423         {"manager", no_argument, 0, 'm'},
424         { NULL, }
425 };
426
427 static const char *monitor_desc[] = {
428         "                 Monitor only services",
429         "                 Monitor only technologies",
430         "                 Monitor only manager interface",
431         NULL
432 };
433
434 static const struct {
435         const char *cmd;
436         const char *argument;
437         struct option *options;
438         const char **options_desc;
439         int (*func) (char *args[], int num, struct option *options);
440         const char *desc;
441 } cmd_table[] = {
442         { "enable",       "<technology>|offline", NULL,    NULL,
443           cmd_enable, "Enables given technology or offline mode" },
444         { "disable",      "<technology>|offline", NULL,    NULL,
445           cmd_disable, "Disables given technology or offline mode"},
446         { "state",        NULL,           NULL,            NULL,
447           cmd_state, "Shows if the system is online or offline" },
448         { "services",     "[<service>]",  service_options, &service_desc[0],
449           cmd_services, "Display services" },
450         { "technologies", NULL,           NULL,            NULL,
451           cmd_technologies, "Display technologies" },
452         { "scan",         "<technology>", NULL,            NULL,
453           cmd_scan, "Scans for new services for given technology" },
454         { "connect",      "<service>",    NULL,            NULL,
455           cmd_connect, "Connect a given service" },
456         { "disconnect",   "<service>",    NULL,            NULL,
457           cmd_disconnect, "Disconnect a given service" },
458         { "config",       "<service>",    config_options,  &config_desc[0],
459           cmd_config, "Set service configuration options" },
460         { "monitor",      NULL,           monitor_options, &monitor_desc[0],
461           cmd_monitor, "Monitor signals from interfaces" },
462         { "help",         NULL,           NULL,            NULL,
463           cmd_help, "Show help" },
464         { "exit",         NULL,           NULL,            NULL,
465           cmd_exit,       "Exit" },
466         { "quit",         NULL,           NULL,            NULL,
467           cmd_exit,       "Quit" },
468         {  NULL, },
469 };
470
471 static int cmd_help(char *args[], int num, struct option *options)
472 {
473         int i, j;
474
475         for (i = 0; cmd_table[i].cmd != NULL; i++) {
476                 const char *cmd = cmd_table[i].cmd;
477                 const char *argument = cmd_table[i].argument;
478                 const char *desc = cmd_table[i].desc;
479
480                 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
481                                 argument != NULL? argument: "",
482                                 desc != NULL? desc: "");
483
484                 if (cmd_table[i].options != NULL) {
485                         for (j = 0; cmd_table[i].options[j].name != NULL;
486                              j++) {
487                                 const char *options_desc =
488                                         cmd_table[i].options_desc != NULL ?
489                                         cmd_table[i].options_desc[j]: "";
490
491                                 printf("   --%-12s%s\n",
492                                                 cmd_table[i].options[j].name,
493                                                 options_desc);
494                         }
495                 }
496         }
497
498         return 0;
499 }
500
501 int commands(DBusConnection *connection, char *argv[], int argc)
502 {
503         int i, result;
504
505         for (i = 0; cmd_table[i].cmd != NULL; i++) {
506                 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
507                                 cmd_table[i].func != NULL) {
508                         result = cmd_table[i].func(argv, argc,
509                                         cmd_table[i].options);
510                         if (result < 0)
511                                 printf("Error '%s': %s\n", argv[0],
512                                                 strerror(-result));
513                         return 0;
514                 }
515         }
516
517         return -1;
518 }
519
520 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
521 {
522         DBusMessage *message = NULL;
523         int error = 0;
524
525         if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0  ||
526                                                 strcmp(argv[0], "h") == 0) {
527                 printf("Usage: connmanctl [[command] [args]]\n");
528                 cmd_help(NULL, 0, NULL);
529                 printf("\nNote: arguments and output are considered "
530                                 "EXPERIMENTAL for now.\n\n");
531         } else if (strcmp(argv[0], "scan") == 0) {
532                 if (argc != 2) {
533                         fprintf(stderr, "Scan requires a service name or path, "
534                                                                 "see help\n");
535                         error = -EINVAL;
536                 }
537                 message = get_message(connection, "GetTechnologies");
538                 if (message == NULL)
539                         error = -ENOMEM;
540                 else
541                         error = scan_technology(connection, message, argv[1]);
542         } else if (strcmp(argv[0], "enable") == 0) {
543                 if (argc != 2) {
544                         fprintf(stderr, "Enable requires a technology name or "
545                                 "the argument 'offlinemode', see help\n");
546                         error = -EINVAL;
547                 } else if (strcmp(argv[1], "offlinemode") == 0) {
548                         error = set_manager(connection, "OfflineMode", TRUE);
549                         if (error == 0)
550                                 printf("OfflineMode is now enabled\n");
551                 } else {
552                         message = get_message(connection, "GetTechnologies");
553                         if (message == NULL)
554                                 error = -ENOMEM;
555                         else
556                                 error = set_technology(connection, message,
557                                                 "Powered", argv[1], TRUE);
558                         if (error == 0)
559                                 printf("Enabled %s technology\n", argv[1]);
560                 }
561         } else if (strcmp(argv[0], "disable") == 0) {
562                 if (argc != 2) {
563                         fprintf(stderr, "Disable requires a technology name or "
564                                 "the argument 'offlinemode' see help\n");
565                         error = -EINVAL;
566                 } else if (strcmp(argv[1], "offlinemode") == 0) {
567                         error = set_manager(connection, "OfflineMode", FALSE);
568                         if (error == 0)
569                                 printf("OfflineMode is now disabled\n");
570                 } else {
571                         message = get_message(connection, "GetTechnologies");
572                         if (message == NULL)
573                                 error = -ENOMEM;
574                         else
575                                 error = set_technology(connection, message,
576                                                 "Powered", argv[1], FALSE);
577                         if (error == 0)
578                                 printf("Disabled %s technology\n", argv[1]);
579                 }
580         } else
581                 return -1;
582
583         if (message != NULL)
584                 dbus_message_unref(message);
585
586         return error;
587 }
588
589 int commands_options(DBusConnection *connection, char *argv[], int argc)
590 {
591         int error, c;
592         int option_index = 0;
593
594         if (strcmp(argv[0], "monitor") == 0) {
595                 if (argc > 2) {
596                         fprintf(stderr, "Too many arguments for monitor, "
597                                                                 "see help\n");
598                         return -EINVAL;
599                 }
600                 if (argc < 2) {
601                         error = monitor_connman(connection, "Service",
602                                                         "PropertyChanged");
603                         if (error != 0)
604                                 return error;
605                         error = monitor_connman(connection, "Technology",
606                                                         "PropertyChanged");
607                         if (error != 0)
608                                 return error;
609                         error = monitor_connman(connection, "Manager",
610                                                         "PropertyChanged");
611                         if (error != 0)
612                                 return error;
613                         error = monitor_connman(connection, "Manager",
614                                                         "TechnologyAdded");
615                         if (error != 0)
616                                 return error;
617                         error = monitor_connman(connection, "Manager",
618                                                         "TechnologyRemoved");
619                         if (error != 0)
620                                 return error;
621                         error = monitor_connman(connection, "Manager",
622                                                         "ServicesChanged");
623                         if (error != 0)
624                                 return error;
625                         if (dbus_connection_add_filter(connection,
626                                         service_property_changed, NULL, NULL)
627                                                                 == FALSE)
628                                 return -ENOMEM;
629                         if (dbus_connection_add_filter(connection,
630                                         tech_property_changed, NULL, NULL)
631                                                                 == FALSE)
632                                 return -ENOMEM;
633                         if (dbus_connection_add_filter(connection,
634                                         tech_added_removed, NULL, NULL)
635                                                                 == FALSE)
636                                 return -ENOMEM;
637                         if (dbus_connection_add_filter(connection,
638                                         manager_property_changed, NULL, NULL)
639                                                                 == FALSE)
640                                 return -ENOMEM;
641                         if (dbus_connection_add_filter(connection,
642                                         manager_services_changed, NULL, NULL)
643                                                                 == FALSE)
644                                 return -ENOMEM;
645                         printf("Now monitoring all interfaces.\n");
646                 } else
647                         while ((c = getopt_long(argc, argv, "", monitor_options,
648                                                         &option_index))) {
649                                 if (c == -1) {
650                                         if (option_index == 0) {
651                                                 printf("Monitor takes an "
652                                                         "option, see help\n");
653                                                 return -EINVAL;
654                                         }
655                                         break;
656                                 }
657                                 error = monitor_switch(argc, argv, c, connection);
658                                 if (error != 0)
659                                         return error;
660                                 option_index++;
661                         }
662         } else
663                 return -1;
664         return 0;
665 }