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