dnsproxy: safely access server_list_sec for removing a list node
[platform/upstream/connman.git] / scripts / vpn-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         /*
58          * We clean the environment before invoking openconnect/vpnc,
59          * but might as well still filter out the few things that get
60          * added that we're not interested in
61          */
62         if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
63             !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
64             !strcmp(key, "connman_network"))
65                 return;
66
67         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
68                                                         NULL, &entry);
69
70         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
71
72         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
73
74         dbus_message_iter_close_container(dict, &entry);
75 }
76
77 int main(int argc, char *argv[])
78 {
79         DBusConnection *conn;
80         DBusError error;
81         DBusMessage *msg;
82         DBusMessageIter iter, dict;
83         char **envp, *busname, *reason, *interface, *path;
84         int ret = 0;
85
86         openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);
87
88         busname = getenv("CONNMAN_BUSNAME");
89         interface = getenv("CONNMAN_INTERFACE");
90         path = getenv("CONNMAN_PATH");
91
92         reason = getenv("reason");
93
94         if (!busname || !interface || !path || !reason) {
95                 print("Required environment variables not set");
96                 ret = 1;
97                 goto out;
98         }
99
100         if (strcmp(reason, "pre-init") == 0)
101                 goto out;
102
103         dbus_error_init(&error);
104
105         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
106         if (!conn) {
107                 if (dbus_error_is_set(&error)) {
108                         print("%s", error.message);
109                         dbus_error_free(&error);
110                 } else
111                         print("Failed to get on system bus");
112
113                 goto out;
114         }
115
116         msg = dbus_message_new_method_call(busname, path,
117                                                 interface, "notify");
118         if (!msg) {
119                 dbus_connection_unref(conn);
120                 print("Failed to allocate method call");
121                 goto out;
122         }
123
124         dbus_message_set_no_reply(msg, TRUE);
125
126         dbus_message_append_args(msg,
127                                  DBUS_TYPE_STRING, &reason,
128                                  DBUS_TYPE_INVALID);
129
130         dbus_message_iter_init_append(msg, &iter);
131
132         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
133                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
134                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
135                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
136
137         for (envp = environ; envp && *envp; envp++)
138                 append(&dict, *envp);
139
140         dbus_message_iter_close_container(&iter, &dict);
141
142         if (!dbus_connection_send(conn, msg, NULL)) {
143                 print("Failed to send message");
144                 goto out;
145         }
146
147         dbus_connection_flush(conn);
148
149         dbus_message_unref(msg);
150
151         dbus_connection_unref(conn);
152
153 out:
154         closelog();
155
156         return ret;
157 }