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