wifi: Add support for autoscan request
[framework/connectivity/connman.git] / scripts / openconnect-script.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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 version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 extern char **environ;
33
34 static void append(DBusMessageIter *dict, const char *pattern)
35 {
36         DBusMessageIter entry;
37         const char *key, *value;
38         char *delim;
39
40         delim = strchr(pattern, '=');
41         *delim = '\0';
42
43         key = pattern;
44         value = delim + 1;
45
46         /* We clean the environment before invoking openconnect, but
47            might as well still filter out the few things that get
48            added that we're not interested in */
49         if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
50             !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
51             !strcmp(key, "connman_network"))
52                 return;
53
54         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
55                                                         NULL, &entry);
56
57         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
58
59         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
60
61         dbus_message_iter_close_container(dict, &entry);
62 }
63
64 int main(int argc, char *argv[])
65 {
66         DBusConnection *conn;
67         DBusError error;
68         DBusMessage *msg;
69         DBusMessageIter iter, dict;
70         char **envp, *busname, *reason, *interface, *path;
71
72         busname = getenv("CONNMAN_BUSNAME");
73         interface = getenv("CONNMAN_INTERFACE");
74         path = getenv("CONNMAN_PATH");
75
76         reason = getenv("reason");
77
78         if (!busname || !interface || !path || !reason) {
79                 fprintf(stderr, "Required environment variables not set\n");
80                 return 1;
81         }
82
83         if (strcmp(reason, "pre-init") == 0)
84                 return 0;
85
86         dbus_error_init(&error);
87
88         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
89         if (conn == NULL) {
90                 if (dbus_error_is_set(&error) == TRUE) {
91                         fprintf(stderr, "%s\n", error.message);
92                         dbus_error_free(&error);
93                 } else
94                         fprintf(stderr, "Failed to get on system bus\n");
95                 return 0;
96         }
97
98         msg = dbus_message_new_method_call(busname, path,
99                                                 interface, "notify");
100         if (msg == NULL) {
101                 dbus_connection_unref(conn);
102                 fprintf(stderr, "Failed to allocate method call\n");
103                 return 0;
104         }
105
106         dbus_message_set_no_reply(msg, TRUE);
107
108         dbus_message_append_args(msg,
109                                  DBUS_TYPE_STRING, &reason,
110                                  DBUS_TYPE_INVALID);
111
112         dbus_message_iter_init_append(msg, &iter);
113
114         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
115                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
116                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
117                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
118
119         for (envp = environ; envp && *envp; envp++)
120                 append(&dict, *envp);
121
122         dbus_message_iter_close_container(&iter, &dict);
123
124         if (dbus_connection_send(conn, msg, NULL) == FALSE)
125                 fprintf(stderr, "Failed to send message\n");
126
127         dbus_connection_flush(conn);
128
129         dbus_message_unref(msg);
130
131         dbus_connection_unref(conn);
132
133         return 0;
134 }