d6e42ebb9bfdae9bf273c7f994277851dafe99db
[platform/upstream/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 #include <syslog.h>
30 #include <libgen.h>
31
32 #include <dbus/dbus.h>
33
34 extern char **environ;
35
36 static void print(const char *format, ...)
37 {
38         va_list ap;
39
40         va_start(ap, format);
41         vsyslog(LOG_INFO, format, ap);
42         va_end(ap);
43 }
44
45 static void append(DBusMessageIter *dict, const char *pattern)
46 {
47         DBusMessageIter entry;
48         const char *key, *value;
49         char *delim;
50
51         delim = strchr(pattern, '=');
52         *delim = '\0';
53
54         key = pattern;
55         value = delim + 1;
56
57         /* We clean the environment before invoking openconnect, but
58            might as well still filter out the few things that get
59            added that we're not interested in */
60         if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
61             !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
62             !strcmp(key, "connman_network"))
63                 return;
64
65         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
66                                                         NULL, &entry);
67
68         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
69
70         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
71
72         dbus_message_iter_close_container(dict, &entry);
73 }
74
75 int main(int argc, char *argv[])
76 {
77         DBusConnection *conn;
78         DBusError error;
79         DBusMessage *msg;
80         DBusMessageIter iter, dict;
81         char **envp, *busname, *reason, *interface, *path;
82         int ret = 0;
83
84         openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);
85
86         busname = getenv("CONNMAN_BUSNAME");
87         interface = getenv("CONNMAN_INTERFACE");
88         path = getenv("CONNMAN_PATH");
89
90         reason = getenv("reason");
91
92         if (!busname || !interface || !path || !reason) {
93                 print("Required environment variables not set");
94                 ret = 1;
95                 goto out;
96         }
97
98         if (strcmp(reason, "pre-init") == 0)
99                 goto out;
100
101         dbus_error_init(&error);
102
103         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
104         if (conn == NULL) {
105                 if (dbus_error_is_set(&error) == TRUE) {
106                         print("%s", error.message);
107                         dbus_error_free(&error);
108                 } else
109                         print("Failed to get on system bus");
110
111                 goto out;
112         }
113
114         msg = dbus_message_new_method_call(busname, path,
115                                                 interface, "notify");
116         if (msg == NULL) {
117                 dbus_connection_unref(conn);
118                 print("Failed to allocate method call");
119                 goto out;
120         }
121
122         dbus_message_set_no_reply(msg, TRUE);
123
124         dbus_message_append_args(msg,
125                                  DBUS_TYPE_STRING, &reason,
126                                  DBUS_TYPE_INVALID);
127
128         dbus_message_iter_init_append(msg, &iter);
129
130         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
131                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
132                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
133                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
134
135         for (envp = environ; envp && *envp; envp++)
136                 append(&dict, *envp);
137
138         dbus_message_iter_close_container(&iter, &dict);
139
140         if (dbus_connection_send(conn, msg, NULL) == FALSE) {
141                 print("Failed to send message");
142                 goto out;
143         }
144
145         dbus_connection_flush(conn);
146
147         dbus_message_unref(msg);
148
149         dbus_connection_unref(conn);
150
151 out:
152         closelog();
153
154         return ret;
155 }