DA: Skip initializing failed_bssids list when eapol failure case
[platform/upstream/connman.git] / scripts / openvpn-script.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010,2012-2014  BMW Car IT GmbH.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <libgen.h>
32
33 #include <dbus/dbus.h>
34
35 extern char **environ;
36
37 static void print(const char *format, ...)
38 {
39         va_list ap;
40
41         va_start(ap, format);
42         vsyslog(LOG_INFO, format, ap);
43         va_end(ap);
44 }
45
46 static void append(DBusMessageIter *dict, const char *pattern)
47 {
48         DBusMessageIter entry;
49         const char *key, *value;
50         char *delim;
51
52         delim = strchr(pattern, '=');
53         *delim = '\0';
54
55         key = pattern;
56         value = delim + 1;
57
58         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
59                                                         NULL, &entry);
60
61         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
62
63         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
64
65         dbus_message_iter_close_container(dict, &entry);
66 }
67
68 int main(int argc, char *argv[])
69 {
70         DBusConnection *conn;
71         DBusError error;
72         DBusMessage *msg;
73         DBusMessageIter iter, dict;
74         char **envp, *busname, *interface, *path, *reason;
75         int ret = 0;
76
77         openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);
78
79         busname = getenv("CONNMAN_BUSNAME");
80         interface = getenv("CONNMAN_INTERFACE");
81         path = getenv("CONNMAN_PATH");
82
83         reason = getenv("script_type");
84
85         if (!busname || !interface || !path || !reason) {
86                 print("Required environment variables not set; "
87                         "bus=%s iface=%s path=%s reason=%s",
88                         busname, interface, path, reason);
89                 ret = 1;
90                 goto out;
91         }
92         dbus_error_init(&error);
93
94         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
95         if (!conn) {
96                 if (dbus_error_is_set(&error)) {
97                         print("%s", error.message);
98                         dbus_error_free(&error);
99                 } else
100                         print("Failed to get on system bus");
101
102                 goto out;
103         }
104
105         msg = dbus_message_new_method_call(busname, path,
106                                                 interface, "notify");
107         if (!msg) {
108                 dbus_connection_unref(conn);
109                 print("Failed to allocate method call");
110                 goto out;
111         }
112
113         dbus_message_set_no_reply(msg, TRUE);
114
115         dbus_message_append_args(msg,
116                                  DBUS_TYPE_STRING, &reason,
117                                  DBUS_TYPE_INVALID);
118
119         dbus_message_iter_init_append(msg, &iter);
120
121         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
122                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
123                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
124                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
125
126         for (envp = environ; envp && *envp; envp++)
127                 append(&dict, *envp);
128
129         dbus_message_iter_close_container(&iter, &dict);
130
131         if (!dbus_connection_send(conn, msg, NULL)) {
132                 print("Failed to send message");
133                 goto out;
134         }
135
136         dbus_connection_flush(conn);
137
138         dbus_message_unref(msg);
139
140         dbus_connection_unref(conn);
141
142 out:
143         closelog();
144
145         return ret;
146 }