client: Fix some memory leaks in commands part
[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 = 0;
106
107         message = get_message(conn, "GetServices");
108         if (message == NULL)
109                 return -ENOMEM;
110
111         switch (c) {
112         case 'p':
113                 name = find_service(conn, message, argv[2], service);
114                 if (name == NULL) {
115                         error = -ENXIO;
116                         break;
117                 }
118
119                 error = list_properties(conn, "GetServices", (char *) name);
120                 break;
121         default:
122                 fprintf(stderr, "Command not recognized, please check help\n");
123                 error = -EINVAL;
124                 break;
125         }
126
127         dbus_message_unref(message);
128
129         return error;
130 }
131
132 int config_switch(int argc, char *argv[], int c, DBusConnection *conn)
133 {
134         DBusMessage *message;
135         int num_args = argc - MANDATORY_ARGS;
136         int error = 0;
137         dbus_bool_t val;
138
139         message = get_message(conn, "GetServices");
140         if (message == NULL)
141                 return -ENOMEM;
142
143         switch (c) {
144         case 'a':
145                 switch (*optarg) {
146                 case 'y':
147                 case '1':
148                 case 't':
149                         val = TRUE;
150                         break;
151                 case 'n':
152                 case '0':
153                 case 'f':
154                         val = FALSE;
155                         break;
156                 default:
157                         return -EINVAL;
158                 }
159                 error = set_service_property(conn, message, argv[1],
160                                                 "AutoConnect", NULL,
161                                                 &val, 0);
162                 break;
163         case 'i':
164                 error = set_service_property(conn, message, argv[1],
165                                         "IPv4.Configuration", ipv4,
166                                         argv + MANDATORY_ARGS, num_args);
167                 break;
168         case 'v':
169                 error = set_service_property(conn, message, argv[1],
170                                         "IPv6.Configuration", ipv6,
171                                         argv + MANDATORY_ARGS, num_args);
172                 break;
173         case 'n':
174                 error = set_service_property(conn, message, argv[1],
175                                         "Nameservers.Configuration", NULL,
176                                         argv + MANDATORY_ARGS, num_args);
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                 break;
183         case 'd':
184                 error = set_service_property(conn, message, argv[1],
185                                         "Domains.Configuration", NULL,
186                                         argv + MANDATORY_ARGS, num_args);
187                 break;
188         case 'x':
189                 if ((strcmp(argv[3], "direct") == 0 && argc < 5) ||
190                         (strcmp(argv[3], "auto") == 0 && argc < 6)) {
191                         error = set_service_property(conn, message, argv[1],
192                                         "Proxy.Configuration", proxy_simple,
193                                         argv + MANDATORY_ARGS, num_args);
194                 } else if (strcmp(argv[3], "manual") == 0
195                                   && strcmp(argv[4], "servers") == 0
196                                   && argc > 5) {
197                         argc -= 5;
198                         error = store_proxy_input(conn, message, argv[1],
199                                                                 argc, &argv[5]);
200                 } else {
201                         fprintf(stderr, "Incorrect arguments\n");
202                         error = -EINVAL;
203                 }
204                 break;
205         default:
206                 fprintf(stderr, "Command not recognized, please check help\n");
207                 error = -EINVAL;
208                 break;
209         }
210
211         dbus_message_unref(message);
212
213         return error;
214 }
215
216 int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn)
217 {
218         int error;
219
220         switch (c) {
221         case 's':
222                 error = monitor_connman(conn, "Service", "PropertyChanged");
223                 if (error != 0)
224                         return error;
225                 if (dbus_connection_add_filter(conn, service_property_changed,
226                                                         NULL, NULL) == FALSE)
227                         return -ENOMEM;
228                 printf("Now monitoring the service interface.\n");
229                 break;
230         case 'c':
231                 error = monitor_connman(conn, "Technology", "PropertyChanged");
232                 if (error != 0)
233                         return error;
234                 if (dbus_connection_add_filter(conn, tech_property_changed,
235                                                         NULL, NULL) == FALSE)
236                         return -ENOMEM;
237                 printf("Now monitoring the technology interface.\n");
238                 break;
239         case 'm':
240                 error = monitor_connman(conn, "Manager", "PropertyChanged");
241                 if (error != 0)
242                         return error;
243                 error = monitor_connman(conn, "Manager", "TechnologyAdded");
244                 if (error != 0)
245                         return error;
246                 error = monitor_connman(conn, "Manager", "TechnologyRemoved");
247                 if (error != 0)
248                         return error;
249                 error = monitor_connman(conn, "Manager", "ServicesChanged");
250                 if (error != 0)
251                         return error;
252                 if (dbus_connection_add_filter(conn, manager_property_changed,
253                                                         NULL, NULL) == FALSE)
254                         return -ENOMEM;
255                 if (dbus_connection_add_filter(conn, tech_added_removed,
256                                                         NULL, NULL) == FALSE)
257                         return -ENOMEM;
258                 if (dbus_connection_add_filter(conn, manager_services_changed,
259                                                         NULL, NULL) == FALSE)
260                         return -ENOMEM;
261                 printf("Now monitoring the manager interface.\n");
262                 break;
263         default:
264                 fprintf(stderr, "Command not recognized, please check help\n");
265                 return -EINVAL;
266                 break;
267         }
268         return 0;
269 }
270
271 int commands_no_options(DBusConnection *connection, char *argv[], int argc)
272 {
273         DBusMessage *message = NULL;
274         int error = 0;
275
276
277         if (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "help") == 0  ||
278                                                 strcmp(argv[0], "h") == 0) {
279                 show_help();
280         } else if (strcmp(argv[0], "state") == 0) {
281                 if (argc != 1) {
282                         fprintf(stderr, "State cannot accept an argument, "
283                                                                 "see help\n");
284                         error = -EINVAL;
285                 } else
286                         error = list_properties(connection,
287                                                 "GetProperties", NULL);
288         } else if (strcmp(argv[0], "technologies") == 0) {
289                 if (argc != 1) {
290                         fprintf(stderr, "Tech cannot accept an argument, "
291                                                                 "see help\n");
292                         error = -EINVAL;
293                 } else
294                         error = list_properties(connection,
295                                                 "GetTechnologies", NULL);
296         } else if (strcmp(argv[0], "connect") == 0) {
297                 if (argc != 2) {
298                         fprintf(stderr, "Connect requires a service name or "
299                                                         "path, see help\n");
300                         error = -EINVAL;
301                 } else
302                         error = connect_service(connection,
303                                                 strip_service_path(argv[1]));
304                 if (error == 0)
305                         printf("Connected to: %s\n",
306                                                 strip_service_path(argv[1]));
307         } else if (strcmp(argv[0], "disconnect") == 0) {
308                 if (argc != 2) {
309                         fprintf(stderr, "Disconnect requires a service name or "
310                                                         "path, see help\n");
311                         error = -EINVAL;
312                 } else
313                         error = disconnect_service(connection,
314                                                 strip_service_path(argv[1]));
315                 if (error == 0)
316                         printf("Disconnected from: %s\n",
317                                                 strip_service_path(argv[1]));
318         } else if (strcmp(argv[0], "scan") == 0) {
319                 if (argc != 2) {
320                         fprintf(stderr, "Scan requires a service name or path, "
321                                                                 "see help\n");
322                         error = -EINVAL;
323                 }
324                 message = get_message(connection, "GetTechnologies");
325                 if (message == NULL)
326                         error = -ENOMEM;
327                 else
328                         error = scan_technology(connection, message, argv[1]);
329         } else if (strcmp(argv[0], "enable") == 0) {
330                 if (argc != 2) {
331                         fprintf(stderr, "Enable requires a technology name or "
332                                 "the argument 'offlinemode', see help\n");
333                         error = -EINVAL;
334                 } else if (strcmp(argv[1], "offlinemode") == 0) {
335                         error = set_manager(connection, "OfflineMode", TRUE);
336                         if (error == 0)
337                                 printf("OfflineMode is now enabled\n");
338                 } else {
339                         message = get_message(connection, "GetTechnologies");
340                         if (message == NULL)
341                                 error = -ENOMEM;
342                         else
343                                 error = set_technology(connection, message,
344                                                 "Powered", argv[1], TRUE);
345                         if (error == 0)
346                                 printf("Enabled %s technology\n", argv[1]);
347                 }
348         } else if (strcmp(argv[0], "disable") == 0) {
349                 if (argc != 2) {
350                         fprintf(stderr, "Disable requires a technology name or "
351                                 "the argument 'offlinemode' see help\n");
352                         error = -EINVAL;
353                 } else if (strcmp(argv[1], "offlinemode") == 0) {
354                         error = set_manager(connection, "OfflineMode", FALSE);
355                         if (error == 0)
356                                 printf("OfflineMode is now disabled\n");
357                 } else {
358                         message = get_message(connection, "GetTechnologies");
359                         if (message == NULL)
360                                 error = -ENOMEM;
361                         else
362                                 error = set_technology(connection, message,
363                                                 "Powered", argv[1], FALSE);
364                         if (error == 0)
365                                 printf("Disabled %s technology\n", argv[1]);
366                 }
367         } else
368                 return -1;
369
370         if (message != NULL)
371                 dbus_message_unref(message);
372
373         return error;
374 }
375
376 int commands_options(DBusConnection *connection, char *argv[], int argc)
377 {
378         int error, c;
379         int option_index = 0;
380         struct service_data service;
381
382         static struct option service_options[] = {
383                 {"properties", required_argument, 0, 'p'},
384                 {0, 0, 0, 0}
385         };
386
387         static struct option config_options[] = {
388                 {"nameservers", required_argument, 0, 'n'},
389                 {"timeservers", required_argument, 0, 't'},
390                 {"domains", required_argument, 0, 'd'},
391                 {"ipv6", required_argument, 0, 'v'},
392                 {"proxy", required_argument, 0, 'x'},
393                 {"autoconnect", required_argument, 0, 'a'},
394                 {"ipv4", required_argument, 0, 'i'},
395                 {0, 0, 0, 0}
396         };
397
398         static struct option monitor_options[] = {
399                 {"services", no_argument, 0, 's'},
400                 {"tech", no_argument, 0, 'c'},
401                 {"manager", no_argument, 0, 'm'},
402                 {0, 0, 0, 0}
403         };
404
405         if (strcmp(argv[0], "services") == 0) {
406                 if (argc > 3) {
407                         fprintf(stderr, "Too many arguments for services, "
408                                                                 "see help\n");
409                         return -EINVAL;
410                 }
411                 if (argc < 2) {
412                         printf("List of all services:\n");
413                         error = list_properties(connection, "GetServices", NULL);
414                         if (error != 0)
415                                 return error;
416                 } else {
417                         while ((c = getopt_long(argc, argv, "", service_options,
418                                                 &option_index))) {
419                                 if (c == -1) {
420                                         if (option_index == 0) {
421                                                 printf("Services takes an "
422                                                         "option, see help.\n");
423                                                 return -EINVAL;
424                                         }
425                                         break;
426                                 }
427                                 error = service_switch(argc, argv, c,
428                                                                 connection,
429                                                                 &service);
430                                 if (error != 0)
431                                         return error;
432                                 option_index++;
433                         }
434                 }
435         } else if (strcmp(argv[0], "config") == 0) {
436                 if (argc < 3) {
437                         fprintf(stderr, "Config requires an option, "
438                                                                 "see help\n");
439                         return -EINVAL;
440                 }
441                 while ((c = getopt_long(argc, argv, "", config_options,
442                                                         &option_index))) {
443                         if (c == -1) {
444                                 if (option_index == 0) {
445                                         printf("Config requires an option, "
446                                                         "see help\n");
447                                         return -EINVAL;
448                                 }
449                                 break;
450                         }
451                         error = config_switch(argc, argv, c, connection);
452                         if (error != 0)
453                                 return error;
454                         option_index++;
455                 }
456         } else if (strcmp(argv[0], "monitor") == 0) {
457                 if (argc > 2) {
458                         fprintf(stderr, "Too many arguments for monitor, "
459                                                                 "see help\n");
460                         return -EINVAL;
461                 }
462                 if (argc < 2) {
463                         error = monitor_connman(connection, "Service",
464                                                         "PropertyChanged");
465                         if (error != 0)
466                                 return error;
467                         error = monitor_connman(connection, "Technology",
468                                                         "PropertyChanged");
469                         if (error != 0)
470                                 return error;
471                         error = monitor_connman(connection, "Manager",
472                                                         "PropertyChanged");
473                         if (error != 0)
474                                 return error;
475                         error = monitor_connman(connection, "Manager",
476                                                         "TechnologyAdded");
477                         if (error != 0)
478                                 return error;
479                         error = monitor_connman(connection, "Manager",
480                                                         "TechnologyRemoved");
481                         if (error != 0)
482                                 return error;
483                         error = monitor_connman(connection, "Manager",
484                                                         "ServicesChanged");
485                         if (error != 0)
486                                 return error;
487                         if (dbus_connection_add_filter(connection,
488                                         service_property_changed, NULL, NULL)
489                                                                 == FALSE)
490                                 return -ENOMEM;
491                         if (dbus_connection_add_filter(connection,
492                                         tech_property_changed, NULL, NULL)
493                                                                 == FALSE)
494                                 return -ENOMEM;
495                         if (dbus_connection_add_filter(connection,
496                                         tech_added_removed, NULL, NULL)
497                                                                 == FALSE)
498                                 return -ENOMEM;
499                         if (dbus_connection_add_filter(connection,
500                                         manager_property_changed, NULL, NULL)
501                                                                 == FALSE)
502                                 return -ENOMEM;
503                         if (dbus_connection_add_filter(connection,
504                                         manager_services_changed, NULL, NULL)
505                                                                 == FALSE)
506                                 return -ENOMEM;
507                         printf("Now monitoring all interfaces.\n");
508                 } else
509                         while ((c = getopt_long(argc, argv, "", monitor_options,
510                                                         &option_index))) {
511                                 if (c == -1) {
512                                         if (option_index == 0) {
513                                                 printf("Monitor takes an "
514                                                         "option, see help\n");
515                                                 return -EINVAL;
516                                         }
517                                         break;
518                                 }
519                                 error = monitor_switch(argc, argv, c, connection);
520                                 if (error != 0)
521                                         return error;
522                                 option_index++;
523                         }
524         } else
525                 return -1;
526         return 0;
527 }