client: Factor out scan 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         DBusMessage *message;
206         int err;
207
208         if (num > 2)
209                 return -E2BIG;
210
211         if (num < 2)
212                 return -EINVAL;
213
214         message = get_message(connection, "GetTechnologies");
215         if (message == NULL)
216                 err = -ENOMEM;
217         else {
218                 err = scan_technology(connection, message, args[1]);
219                 if (err == 0)
220                         printf("Scan completed\n");
221         }
222
223         return 0;
224 }
225
226 static int cmd_connect(char *args[], int num, struct option *options)
227 {
228         int err;
229
230         if (num > 2)
231                 return -E2BIG;
232
233         if (num < 2)
234                 return -EINVAL;
235
236         err = connect_service(connection, args[1]);
237         if (err == 0)
238                 printf("Connected\n");
239
240         return 0;
241 }
242
243 static int cmd_disconnect(char *args[], int num, struct option *options)
244 {
245         int err;
246
247         if (num > 2)
248                 return -E2BIG;
249
250         if (num < 2)
251                 return -EINVAL;
252
253         err = disconnect_service(connection, args[1]);
254         if (err == 0)
255                 printf("Disconnected\n");
256
257         return 0;
258 }
259
260 static int cmd_config(char *args[], int num, struct option *options)
261 {
262         int res = 0, index = 2, oldindex = 0;
263         int c;
264         char *service_name;
265         DBusMessage *message;
266         char **opt_start;
267         dbus_bool_t val;
268
269         service_name = args[1];
270         if (service_name == NULL)
271                 return -EINVAL;
272
273         while (index < num && args[index] != NULL) {
274                 c = parse_args(args[index], options);
275                 opt_start = &args[index + 1];
276                 res = 0;
277
278                 message = get_message(connection, "GetServices");
279                 if (message == NULL)
280                         return -ENOMEM;
281
282                 oldindex = index;
283
284                 switch (c) {
285                 case 'a':
286                         switch (parse_boolean(*opt_start)) {
287                         case 1:
288                                 val = TRUE;
289                                 break;
290                         case 0:
291                                 val = FALSE;
292                                 break;
293                         default:
294                                 res = -EINVAL;
295                                 break;
296                         }
297                         if (res == 0)
298                                 res = set_service_property(connection, message,
299                                                 service_name, "AutoConnect",
300                                                 NULL, &val, 0);
301                         break;
302                 case 'i':
303                         res = set_service_property(connection, message,
304                                         service_name, "IPv4.Configuration",
305                                         ipv4, opt_start, 0);
306                         if (res < 0)
307                                 index += 4;
308                         break;
309                 case 'v':
310                         res = set_service_property(connection, message,
311                                         service_name, "IPv6.Configuration",
312                                         ipv6, opt_start, 0);
313                         if (res < 0)
314                                 index += 5;
315                         break;
316                 case 'n':
317                         res = set_service_property(connection, message,
318                                         service_name,
319                                         "Nameservers.Configuration",
320                                         NULL, opt_start, 0);
321                         break;
322                 case 't':
323                         res = set_service_property(connection, message,
324                                         service_name,
325                                         "Timeservers.Configuration",
326                                         NULL, opt_start, 0);
327                         break;
328                 case 'd':
329                         res = set_service_property(connection, message,
330                                         service_name,
331                                         "Domains.Configuration",
332                                         NULL, opt_start, 0);
333                         break;
334                 case 'x':
335                         if (*opt_start == NULL) {
336                                 res = -EINVAL;
337                                 break;
338                         }
339
340                         if (strcmp(*opt_start, "direct") == 0) {
341                                 res = set_service_property(connection, message,
342                                                 service_name,
343                                                 "Proxy.Configuration",
344                                                 proxy_simple, opt_start, 1);
345                                 break;
346                         }
347
348                         if (strcmp(*opt_start, "auto") == 0) {
349                                 res = set_service_property(connection, message,
350                                                 service_name,
351                                                 "Proxy.Configuration",
352                                                 proxy_simple, opt_start, 1);
353                                 break;
354                         }
355
356                         if (strcmp(*opt_start, "manual") == 0) {
357                                         char **url_start = &args[index + 2];
358
359                                         if (*url_start != NULL &&
360                                                 strcmp(*url_start,
361                                                         "servers") == 0) {
362                                                 url_start = &args[index + 3];
363                                                 index++;
364                                         }
365                                         res = store_proxy_input(connection,
366                                                         message, service_name,
367                                                         0, url_start);
368                         }
369
370                         break;
371                 case 'r':
372                         res = remove_service(connection, message, service_name);
373                         break;
374                 default:
375                         res = -EINVAL;
376                         break;
377                 }
378
379                 dbus_message_unref(message);
380
381                 if (res < 0) {
382                         printf("Error '%s': %s\n", args[oldindex],
383                                         strerror(-res));
384                 } else
385                         index += res;
386
387                 index++;
388         }
389
390         return 0;
391 }
392
393 static int cmd_monitor(char *args[], int num, struct option *options)
394 {
395         return -1;
396 }
397
398 static int cmd_exit(char *args[], int num, struct option *options)
399 {
400         return 0;
401 }
402
403 static struct option service_options[] = {
404         {"properties", required_argument, 0, 'p'},
405         { NULL, }
406 };
407
408 static const char *service_desc[] = {
409         "[<service>]      (obsolete)",
410         NULL
411 };
412
413 static struct option config_options[] = {
414         {"nameservers", required_argument, 0, 'n'},
415         {"timeservers", required_argument, 0, 't'},
416         {"domains", required_argument, 0, 'd'},
417         {"ipv6", required_argument, 0, 'v'},
418         {"proxy", required_argument, 0, 'x'},
419         {"autoconnect", required_argument, 0, 'a'},
420         {"ipv4", required_argument, 0, 'i'},
421         {"remove", 0, 0, 'r'},
422         { NULL, }
423 };
424
425 static const char *config_desc[] = {
426         "<dns1> [<dns2>] [<dns3>]",
427         "<ntp1> [<ntp2>] [...]",
428         "<domain1> [<domain2>] [...]",
429         "off|auto|manual <address> <prefixlength> <gateway> <privacy>",
430         "direct|auto <URL>|manual <URL1> [<URL2>] [...]\n"
431         "                   [exclude <exclude1> [<exclude2>] [...]]",
432         "yes|no",
433         "off|dhcp|manual <address> <prefixlength> <gateway>",
434         "                 Remove service",
435         NULL
436 };
437
438 static struct option monitor_options[] = {
439         {"services", no_argument, 0, 's'},
440         {"tech", no_argument, 0, 'c'},
441         {"manager", no_argument, 0, 'm'},
442         { NULL, }
443 };
444
445 static const char *monitor_desc[] = {
446         "                 Monitor only services",
447         "                 Monitor only technologies",
448         "                 Monitor only manager interface",
449         NULL
450 };
451
452 static const struct {
453         const char *cmd;
454         const char *argument;
455         struct option *options;
456         const char **options_desc;
457         int (*func) (char *args[], int num, struct option *options);
458         const char *desc;
459 } cmd_table[] = {
460         { "enable",       "<technology>|offline", NULL,    NULL,
461           cmd_enable, "Enables given technology or offline mode" },
462         { "disable",      "<technology>|offline", NULL,    NULL,
463           cmd_disable, "Disables given technology or offline mode"},
464         { "state",        NULL,           NULL,            NULL,
465           cmd_state, "Shows if the system is online or offline" },
466         { "services",     "[<service>]",  service_options, &service_desc[0],
467           cmd_services, "Display services" },
468         { "technologies", NULL,           NULL,            NULL,
469           cmd_technologies, "Display technologies" },
470         { "scan",         "<technology>", NULL,            NULL,
471           cmd_scan, "Scans for new services for given technology" },
472         { "connect",      "<service>",    NULL,            NULL,
473           cmd_connect, "Connect a given service" },
474         { "disconnect",   "<service>",    NULL,            NULL,
475           cmd_disconnect, "Disconnect a given service" },
476         { "config",       "<service>",    config_options,  &config_desc[0],
477           cmd_config, "Set service configuration options" },
478         { "monitor",      NULL,           monitor_options, &monitor_desc[0],
479           cmd_monitor, "Monitor signals from interfaces" },
480         { "help",         NULL,           NULL,            NULL,
481           cmd_help, "Show help" },
482         { "exit",         NULL,           NULL,            NULL,
483           cmd_exit,       "Exit" },
484         { "quit",         NULL,           NULL,            NULL,
485           cmd_exit,       "Quit" },
486         {  NULL, },
487 };
488
489 static int cmd_help(char *args[], int num, struct option *options)
490 {
491         int i, j;
492
493         for (i = 0; cmd_table[i].cmd != NULL; i++) {
494                 const char *cmd = cmd_table[i].cmd;
495                 const char *argument = cmd_table[i].argument;
496                 const char *desc = cmd_table[i].desc;
497
498                 printf("%-12s%-22s%s\n", cmd != NULL? cmd: "",
499                                 argument != NULL? argument: "",
500                                 desc != NULL? desc: "");
501
502                 if (cmd_table[i].options != NULL) {
503                         for (j = 0; cmd_table[i].options[j].name != NULL;
504                              j++) {
505                                 const char *options_desc =
506                                         cmd_table[i].options_desc != NULL ?
507                                         cmd_table[i].options_desc[j]: "";
508
509                                 printf("   --%-12s%s\n",
510                                                 cmd_table[i].options[j].name,
511                                                 options_desc);
512                         }
513                 }
514         }
515
516         return 0;
517 }
518
519 int commands(DBusConnection *connection, char *argv[], int argc)
520 {
521         int i, result;
522
523         for (i = 0; cmd_table[i].cmd != NULL; i++) {
524                 if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 &&
525                                 cmd_table[i].func != NULL) {
526                         result = cmd_table[i].func(argv, argc,
527                                         cmd_table[i].options);
528                         if (result < 0)
529                                 printf("Error '%s': %s\n", argv[0],
530                                                 strerror(-result));
531                         return 0;
532                 }
533         }
534
535         return -1;
536 }
537
538 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
539 {
540         DBusMessage *message = NULL;
541         int error = 0;
542
543         if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0  ||
544                                                 strcmp(argv[0], "h") == 0) {
545                 printf("Usage: connmanctl [[command] [args]]\n");
546                 cmd_help(NULL, 0, NULL);
547                 printf("\nNote: arguments and output are considered "
548                                 "EXPERIMENTAL for now.\n\n");
549         } else if (strcmp(argv[0], "enable") == 0) {
550                 if (argc != 2) {
551                         fprintf(stderr, "Enable requires a technology name or "
552                                 "the argument 'offlinemode', see help\n");
553                         error = -EINVAL;
554                 } else if (strcmp(argv[1], "offlinemode") == 0) {
555                         error = set_manager(connection, "OfflineMode", TRUE);
556                         if (error == 0)
557                                 printf("OfflineMode is now enabled\n");
558                 } else {
559                         message = get_message(connection, "GetTechnologies");
560                         if (message == NULL)
561                                 error = -ENOMEM;
562                         else
563                                 error = set_technology(connection, message,
564                                                 "Powered", argv[1], TRUE);
565                         if (error == 0)
566                                 printf("Enabled %s technology\n", argv[1]);
567                 }
568         } else if (strcmp(argv[0], "disable") == 0) {
569                 if (argc != 2) {
570                         fprintf(stderr, "Disable requires a technology name or "
571                                 "the argument 'offlinemode' see help\n");
572                         error = -EINVAL;
573                 } else if (strcmp(argv[1], "offlinemode") == 0) {
574                         error = set_manager(connection, "OfflineMode", FALSE);
575                         if (error == 0)
576                                 printf("OfflineMode is now disabled\n");
577                 } else {
578                         message = get_message(connection, "GetTechnologies");
579                         if (message == NULL)
580                                 error = -ENOMEM;
581                         else
582                                 error = set_technology(connection, message,
583                                                 "Powered", argv[1], FALSE);
584                         if (error == 0)
585                                 printf("Disabled %s technology\n", argv[1]);
586                 }
587         } else
588                 return -1;
589
590         if (message != NULL)
591                 dbus_message_unref(message);
592
593         return error;
594 }
595
596 int commands_options(DBusConnection *connection, char *argv[], int argc)
597 {
598         int error, c;
599         int option_index = 0;
600
601         if (strcmp(argv[0], "monitor") == 0) {
602                 if (argc > 2) {
603                         fprintf(stderr, "Too many arguments for monitor, "
604                                                                 "see help\n");
605                         return -EINVAL;
606                 }
607                 if (argc < 2) {
608                         error = monitor_connman(connection, "Service",
609                                                         "PropertyChanged");
610                         if (error != 0)
611                                 return error;
612                         error = monitor_connman(connection, "Technology",
613                                                         "PropertyChanged");
614                         if (error != 0)
615                                 return error;
616                         error = monitor_connman(connection, "Manager",
617                                                         "PropertyChanged");
618                         if (error != 0)
619                                 return error;
620                         error = monitor_connman(connection, "Manager",
621                                                         "TechnologyAdded");
622                         if (error != 0)
623                                 return error;
624                         error = monitor_connman(connection, "Manager",
625                                                         "TechnologyRemoved");
626                         if (error != 0)
627                                 return error;
628                         error = monitor_connman(connection, "Manager",
629                                                         "ServicesChanged");
630                         if (error != 0)
631                                 return error;
632                         if (dbus_connection_add_filter(connection,
633                                         service_property_changed, NULL, NULL)
634                                                                 == FALSE)
635                                 return -ENOMEM;
636                         if (dbus_connection_add_filter(connection,
637                                         tech_property_changed, NULL, NULL)
638                                                                 == FALSE)
639                                 return -ENOMEM;
640                         if (dbus_connection_add_filter(connection,
641                                         tech_added_removed, NULL, NULL)
642                                                                 == FALSE)
643                                 return -ENOMEM;
644                         if (dbus_connection_add_filter(connection,
645                                         manager_property_changed, NULL, NULL)
646                                                                 == FALSE)
647                                 return -ENOMEM;
648                         if (dbus_connection_add_filter(connection,
649                                         manager_services_changed, NULL, NULL)
650                                                                 == FALSE)
651                                 return -ENOMEM;
652                         printf("Now monitoring all interfaces.\n");
653                 } else
654                         while ((c = getopt_long(argc, argv, "", monitor_options,
655                                                         &option_index))) {
656                                 if (c == -1) {
657                                         if (option_index == 0) {
658                                                 printf("Monitor takes an "
659                                                         "option, see help\n");
660                                                 return -EINVAL;
661                                         }
662                                         break;
663                                 }
664                                 error = monitor_switch(argc, argv, c, connection);
665                                 if (error != 0)
666                                         return error;
667                                 option_index++;
668                         }
669         } else
670                 return -1;
671         return 0;
672 }