client: Fix AutoConnect configuration
[platform/upstream/connman.git] / client / commands.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *  it under the terms of the GNU General Public License version 2 as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25
26 #include <glib.h>
27 #include <gdbus.h>
28
29 #include "client/services.h"
30 #include "client/technology.h"
31 #include "client/data_manager.h"
32 #include "client/monitor.h"
33 #include "client/interactive.h"
34 #include "gdbus/gdbus.h"
35
36 #define MANDATORY_ARGS 3
37
38 static char *ipv4[] = {
39         "Method",
40         "Address",
41         "Netmask",
42         "Gateway",
43         NULL
44 };
45
46 static char *ipv6[] = {
47         "Method",
48         "Address",
49         "PrefixLength",
50         "Gateway",
51         "Privacy",
52         NULL
53 };
54
55 static char *proxy_simple[] = {
56         "Method",
57         "URL",
58         NULL
59 };
60
61 void show_help(void)
62 {
63         printf("Usage: connmanctl <command> [args]\n"
64         "  enable                             Enables given technology\n"
65         "        <technology>\n"
66         "        offlinemode                  Enables OfflineMode\n"
67         "  disable                            Disables given technology\n"
68         "        <technology>\n"
69         "        offlinemode                  Disables OfflineMode\n"
70         "  state                              Shows if the system is online or offline\n"
71         "  services                           Display list of all services\n"
72         "        --properties <service name>  Show properties of service\n"
73         "  technologies                       Current technology on the system\n"
74         "  scan <technology>                  Scans for new services on the given technology\n"
75         "  connect <service>                  Connect to a given service\n"
76         "  disconnect <service>               Disconnect from service\n"
77         "  config <service> [arg]             Set certain config options\n"
78         "        --autoconnect=y/n            Set autoconnect to service\n"
79         "        --nameservers <names>        Set manual name servers\n"
80         "        --timeservers <names>        Set manual time servers\n"
81         "        --domains <domains>          Set manual domains\n"
82         "        --ipv4                       Set ipv4 configuration\n"
83         "          [METHOD|DHCP|AUTO|MANUAL] [IP] [NETMASK] [GATEWAY]\n"
84         "        --ipv6                       Set ipv6 configuration\n"
85         "          [METHOD|AUTO|MANUAL|OFF] [IP] [PREFIXLENGTH] [GATEWAY]\n"
86         "          [PRIVACY|DISABLED|ENABLED|PREFERED]\n"
87         "        --proxy                      Set proxy configuration\n"
88         "          [METHOD|URL|SERVERS|EXCLUDES]\n"
89         "          if METHOD = manual, enter 'servers' then the list of servers\n"
90         "                         then enter 'excludes' then the list of excludes\n"
91         "  monitor                            Monitor signals from all Connman interfaces\n"
92         "        --services                   Monitor signals from the Service interface\n"
93         "        --tech                       Monitor signals from the Technology interface\n"
94         "        --manager                    Monitor signals from the Manager interface\n"
95         "  help, --help, (no arguments)       Show this dialogue\n"
96         "  exit, quit, q                      Quit interactive mode\n"
97         "\nNote: arguments and output are considered EXPERIMENTAL for now.\n\n");
98 }
99
100 int service_switch(int argc, char *argv[], int c, DBusConnection *conn,
101                                                 struct service_data *service)
102 {
103         const char *name;
104         DBusMessage *message;
105         int error;
106
107         message = get_message(conn, "GetServices");
108
109         switch (c) {
110         case 'p':
111                 name = find_service(conn, message, argv[2], service);
112                 if (name == NULL)
113                         return -ENXIO;
114                 error = list_properties(conn, "GetServices", (char *) name);
115                 if (error != 0)
116                         return error;
117                 break;
118         default:
119                 fprintf(stderr, "Command not recognized, please check help\n");
120                 return -EINVAL;
121                 break;
122         }
123         return 0;
124 }
125
126 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
127 {
128         DBusMessage *message;
129         int num_args = argc - MANDATORY_ARGS;
130         int error;
131         dbus_bool_t val;
132
133         message = get_message(conn, "GetServices");
134
135         switch (c) {
136         case 'a':
137                 switch (*optarg) {
138                 case 'y':
139                 case '1':
140                 case 't':
141                         val = TRUE;
142                         break;
143                 case 'n':
144                 case '0':
145                 case 'f':
146                         val = FALSE;
147                         break;
148                 default:
149                         return -EINVAL;
150                 }
151                 error = set_service_property(conn, message, argv[1],
152                                                 "AutoConnect", NULL,
153                                                 &val, 0);
154                 if (error != 0)
155                         return error;
156                 break;
157         case 'i':
158                 error = set_service_property(conn, message, argv[1],
159                                         "IPv4.Configuration", ipv4,
160                                         argv + MANDATORY_ARGS, num_args);
161                 if (error != 0)
162                         return error;
163                 break;
164         case 'v':
165                 error = set_service_property(conn, message, argv[1],
166                                         "IPv6.Configuration", ipv6,
167                                         argv + MANDATORY_ARGS, num_args);
168                 if (error != 0)
169                         return error;
170                 break;
171         case 'n':
172                 error = set_service_property(conn, message, argv[1],
173                                         "Nameservers.Configuration", NULL,
174                                         argv + MANDATORY_ARGS, num_args);
175                 if (error != 0)
176                         return error;
177                 break;
178         case 't':
179                 error = set_service_property(conn, message, argv[1],
180                                         "Timeservers.Configuration", NULL,
181                                         argv + MANDATORY_ARGS, num_args);
182                 if (error != 0)
183                         return error;
184                 break;
185         case 'd':
186                 error = set_service_property(conn, message, argv[1],
187                                         "Domains.Configuration", NULL,
188                                         argv + MANDATORY_ARGS, num_args);
189                 if (error != 0)
190                         return error;
191                 break;
192         case 'x':
193                 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
194                         (strcmp(argv[3], "auto") == 0 && argc < 6)) {
195                         error = set_service_property(conn, message, argv[1],
196                                         "Proxy.Configuration", proxy_simple,
197                                         argv + MANDATORY_ARGS, num_args);
198                         if (error != 0)
199                                 return error;
200                 } else if (strcmp(argv[3], "manual") == 0
201                                   && strcmp(argv[4], "servers") == 0
202                                   && argc > 5) {
203                         argc -= 5;
204                         error = store_proxy_input(conn, message, argv[1],
205                                                                 argc, &argv[5]);
206                         if (error != 0)
207                                 return error;
208                 } else {
209                         fprintf(stderr, "Incorrect arguments\n");
210                         return -EINVAL;
211                 }
212                 break;
213         default:
214                 fprintf(stderr, "Command not recognized, please check help\n");
215                 return -EINVAL;
216                 break;
217         }
218         return 0;
219 }
220
221 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
222 {
223         int error;
224
225         switch (c) {
226         case 's':
227                 error = monitor_connman(conn, "Service", "PropertyChanged");
228                 if (error != 0)
229                         return error;
230                 if (dbus_connection_add_filter(conn, service_property_changed,
231                                                         NULL, NULL) == FALSE)
232                         return -ENOMEM;
233                 printf("Now monitoring the service interface.\n");
234                 break;
235         case 'c':
236                 error = monitor_connman(conn, "Technology", "PropertyChanged");
237                 if (error != 0)
238                         return error;
239                 if (dbus_connection_add_filter(conn, tech_property_changed,
240                                                         NULL, NULL) == FALSE)
241                         return -ENOMEM;
242                 printf("Now monitoring the technology interface.\n");
243                 break;
244         case 'm':
245                 error = monitor_connman(conn, "Manager", "PropertyChanged");
246                 if (error != 0)
247                         return error;
248                 error = monitor_connman(conn, "Manager", "TechnologyAdded");
249                 if (error != 0)
250                         return error;
251                 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
252                 if (error != 0)
253                         return error;
254                 error = monitor_connman(conn, "Manager", "ServicesChanged");
255                 if (error != 0)
256                         return error;
257                 if (dbus_connection_add_filter(conn, manager_property_changed,
258                                                         NULL, NULL) == FALSE)
259                         return -ENOMEM;
260                 if (dbus_connection_add_filter(conn, tech_added_removed,
261                                                         NULL, NULL) == FALSE)
262                         return -ENOMEM;
263                 if (dbus_connection_add_filter(conn, manager_services_changed,
264                                                         NULL, NULL) == FALSE)
265                         return -ENOMEM;
266                 printf("Now monitoring the manager interface.\n");
267                 break;
268         default:
269                 fprintf(stderr, "Command not recognized, please check help\n");
270                 return -EINVAL;
271                 break;
272         }
273         return 0;
274 }
275
276 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
277 {
278         int error;
279         DBusMessage *message;
280
281
282         if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0  ||
283                                                 strcmp(argv[0], "h") == 0) {
284                 show_help();
285         } else if (strcmp(argv[0], "state") == 0) {
286                 if (argc != 1) {
287                         fprintf(stderr, "State cannot accept an argument, "
288                                                                 "see help\n");
289                         return -EINVAL;
290                 }
291                 error = list_properties(connection, "GetProperties", NULL);
292                 if (error != 0)
293                         return error;
294         } else if (strcmp(argv[0], "technologies") == 0) {
295                 if (argc != 1) {
296                         fprintf(stderr, "Tech cannot accept an argument, "
297                                                                 "see help\n");
298                         return -EINVAL;
299                 }
300                 error = list_properties(connection, "GetTechnologies", NULL);
301                 if (error != 0)
302                         return error;
303         } else if (strcmp(argv[0], "connect") == 0) {
304                 if (argc != 2) {
305                         fprintf(stderr, "Connect requires a service name or "
306                                                         "path, see help\n");
307                         return -EINVAL;
308                 }
309                 error = connect_service(connection, strip_service_path(argv[1]));
310                 if (error != 0)
311                         return error;
312                 printf("Connected to: %s\n", strip_service_path(argv[1]));
313         } else if (strcmp(argv[0], "disconnect") == 0) {
314                 if (argc != 2) {
315                         fprintf(stderr, "Disconnect requires a service name or "
316                                                         "path, see help\n");
317                         return -EINVAL;
318                 }
319                 error = disconnect_service(connection, strip_service_path(argv[1]));
320                 if (error != 0)
321                         return error;
322                 printf("Disconnected from: %s\n", strip_service_path(argv[1]));
323         } else if (strcmp(argv[0], "scan") == 0) {
324                 if (argc != 2) {
325                         fprintf(stderr, "Scan requires a service name or path, "
326                                                                 "see help\n");
327                         return -EINVAL;
328                 }
329                 message = get_message(connection, "GetTechnologies");
330                 error = scan_technology(connection, message, argv[1]);
331                 if (error != 0)
332                         return error;
333                 dbus_message_unref(message);
334         } else if (strcmp(argv[0], "enable") == 0) {
335                 if (argc != 2) {
336                         fprintf(stderr, "Enable requires a technology name or "
337                                 "the argument 'offlinemode', see help\n");
338                         return -EINVAL;
339                 }
340                 if (strcmp(argv[1], "offlinemode") == 0) {
341                         error = set_manager(connection, "OfflineMode", TRUE);
342                         if (error != 0)
343                                 return error;
344                         printf("OfflineMode is now enabled\n");
345                 } else {
346                         message = get_message(connection, "GetTechnologies");
347                         error = set_technology(connection, message, "Powered",
348                                                         argv[1], TRUE);
349                         if (error != 0)
350                                 return error;
351                         printf("Enabled %s technology\n", argv[1]);
352                         dbus_message_unref(message);
353                 }
354         } else if (strcmp(argv[0], "disable") == 0) {
355                 if (argc != 2) {
356                         fprintf(stderr, "Disable requires a technology name or "
357                                 "the argument 'offlinemode' see help\n");
358                         return -EINVAL;
359                 }
360                 if (strcmp(argv[1], "offlinemode") == 0) {
361                         error = set_manager(connection, "OfflineMode", FALSE);
362                         if (error != 0)
363                                 return error;
364                         printf("OfflineMode is now disabled\n");
365                 } else {
366                         message = get_message(connection, "GetTechnologies");
367                         error = set_technology(connection, message, "Powered",
368                                                         argv[1], FALSE);
369                         if (error != 0)
370                                 return error;
371                         printf("Disabled %s technology\n", argv[1]);
372                         dbus_message_unref(message);
373                 }
374         } else
375                 return -1;
376         return 0;
377 }
378
379 int commands_options(DBusConnection *connection, char *argv[], int argc)
380 {
381         int error, c;
382         int option_index = 0;
383         struct service_data service;
384
385         static struct option service_options[] = {
386                 {"properties", required_argument, 0, 'p'},
387                 {0, 0, 0, 0}
388         };
389
390         static struct option config_options[] = {
391                 {"nameservers", required_argument, 0, 'n'},
392                 {"timeservers", required_argument, 0, 't'},
393                 {"domains", required_argument, 0, 'd'},
394                 {"ipv6", required_argument, 0, 'v'},
395                 {"proxy", required_argument, 0, 'x'},
396                 {"autoconnect", required_argument, 0, 'a'},
397                 {"ipv4", required_argument, 0, 'i'},
398                 {0, 0, 0, 0}
399         };
400
401         static struct option monitor_options[] = {
402                 {"services", no_argument, 0, 's'},
403                 {"tech", no_argument, 0, 'c'},
404                 {"manager", no_argument, 0, 'm'},
405                 {0, 0, 0, 0}
406         };
407
408         if (strcmp(argv[0], "services") == 0) {
409                 if (argc > 3) {
410                         fprintf(stderr, "Too many arguments for services, "
411                                                                 "see help\n");
412                         return -EINVAL;
413                 }
414                 if (argc < 2) {
415                         printf("List of all services:\n");
416                         error = list_properties(connection, "GetServices", NULL);
417                         if (error != 0)
418                                 return error;
419                 } else {
420                         while ((c = getopt_long(argc, argv, "", service_options,
421                                                 &option_index))) {
422                                 if (c == -1) {
423                                         if (option_index == 0) {
424                                                 printf("Services takes an "
425                                                         "option, see help.\n");
426                                                 return -EINVAL;
427                                         }
428                                         break;
429                                 }
430                                 error = service_switch(argc, argv, c,
431                                                                 connection,
432                                                                 &service);
433                                 if (error != 0)
434                                         return error;
435                                 option_index++;
436                         }
437                 }
438         } else if (strcmp(argv[0], "config") == 0) {
439                 if (argc < 3) {
440                         fprintf(stderr, "Config requires an option, "
441                                                                 "see help\n");
442                         return -EINVAL;
443                 }
444                 while ((c = getopt_long(argc, argv, "", config_options,
445                                                         &option_index))) {
446                         if (c == -1) {
447                                 if (option_index == 0) {
448                                         printf("Config requires an option, "
449                                                         "see help\n");
450                                         return -EINVAL;
451                                 }
452                                 break;
453                         }
454                         error = config_switch(argc, argv, c, connection);
455                         if (error != 0)
456                                 return error;
457                         option_index++;
458                 }
459         } else if (strcmp(argv[0], "monitor") == 0) {
460                 if (argc > 2) {
461                         fprintf(stderr, "Too many arguments for monitor, "
462                                                                 "see help\n");
463                         return -EINVAL;
464                 }
465                 if (argc < 2) {
466                         error = monitor_connman(connection, "Service",
467                                                         "PropertyChanged");
468                         if (error != 0)
469                                 return error;
470                         error = monitor_connman(connection, "Technology",
471                                                         "PropertyChanged");
472                         if (error != 0)
473                                 return error;
474                         error = monitor_connman(connection, "Manager",
475                                                         "PropertyChanged");
476                         if (error != 0)
477                                 return error;
478                         error = monitor_connman(connection, "Manager",
479                                                         "TechnologyAdded");
480                         if (error != 0)
481                                 return error;
482                         error = monitor_connman(connection, "Manager",
483                                                         "TechnologyRemoved");
484                         if (error != 0)
485                                 return error;
486                         error = monitor_connman(connection, "Manager",
487                                                         "ServicesChanged");
488                         if (error != 0)
489                                 return error;
490                         if (dbus_connection_add_filter(connection,
491                                         service_property_changed, NULL, NULL)
492                                                                 == FALSE)
493                                 return -ENOMEM;
494                         if (dbus_connection_add_filter(connection,
495                                         tech_property_changed, NULL, NULL)
496                                                                 == FALSE)
497                                 return -ENOMEM;
498                         if (dbus_connection_add_filter(connection,
499                                         tech_added_removed, NULL, NULL)
500                                                                 == FALSE)
501                                 return -ENOMEM;
502                         if (dbus_connection_add_filter(connection,
503                                         manager_property_changed, NULL, NULL)
504                                                                 == FALSE)
505                                 return -ENOMEM;
506                         if (dbus_connection_add_filter(connection,
507                                         manager_services_changed, NULL, NULL)
508                                                                 == FALSE)
509                                 return -ENOMEM;
510                         printf("Now monitoring all interfaces.\n");
511                 } else
512                         while ((c = getopt_long(argc, argv, "", monitor_options,
513                                                         &option_index))) {
514                                 if (c == -1) {
515                                         if (option_index == 0) {
516                                                 printf("Monitor takes an "
517                                                         "option, see help\n");
518                                                 return -EINVAL;
519                                         }
520                                         break;
521                                 }
522                                 error = monitor_switch(argc, argv, c, connection);
523                                 if (error != 0)
524                                         return error;
525                                 option_index++;
526                         }
527         } else
528                 return -1;
529         return 0;
530 }