vpnd: Quit if no VPN configurations are found
[platform/upstream/connman.git] / vpn / vpn-manager.c
1 /*
2  *
3  *  ConnMan VPN daemon
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 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 <errno.h>
27
28 #include <gdbus.h>
29 #include <connman/log.h>
30 #include <connman/agent.h>
31
32 #include "../src/connman.h"
33
34 #include "vpn.h"
35 #include "connman/vpn-dbus.h"
36
37 static int vpn_connect_count;
38 static DBusConnection *connection;
39
40 static DBusMessage *create(DBusConnection *conn, DBusMessage *msg, void *data)
41 {
42         int err;
43
44         DBG("conn %p", conn);
45
46         err = __vpn_provider_create(msg);
47         if (err < 0) {
48                 if (err == -EINPROGRESS) {
49                         connman_error("Invalid return code (%d) "
50                                         "from connect", err);
51                         err = -EINVAL;
52                 }
53
54                 return __connman_error_failed(msg, -err);
55         }
56
57         return NULL;
58 }
59
60 static DBusMessage *remove(DBusConnection *conn, DBusMessage *msg, void *data)
61 {
62         const char *path;
63         int err;
64
65         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
66                                                         DBUS_TYPE_INVALID);
67
68         DBG("conn %p path %s", conn, path);
69
70         err = __vpn_provider_remove(path);
71         if (err < 0)
72                 return __connman_error_failed(msg, -err);
73
74         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
75 }
76
77 static DBusMessage *get_connections(DBusConnection *conn, DBusMessage *msg,
78                                                                 void *data)
79 {
80         DBusMessage *reply;
81
82         DBG("conn %p", conn);
83
84         reply = __vpn_provider_get_connections(msg);
85         if (reply == NULL)
86                 return __connman_error_failed(msg, -EINVAL);
87
88         __vpn_provider_check_connections();
89
90         return reply;
91 }
92
93 static DBusMessage *register_agent(DBusConnection *conn,
94                                         DBusMessage *msg, void *data)
95 {
96         const char *sender, *path;
97         int err;
98
99         DBG("conn %p", conn);
100
101         sender = dbus_message_get_sender(msg);
102
103         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
104                                                         DBUS_TYPE_INVALID);
105
106         err = connman_agent_register(sender, path);
107         if (err < 0)
108                 return __connman_error_failed(msg, -err);
109
110         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
111 }
112
113 static DBusMessage *unregister_agent(DBusConnection *conn,
114                                         DBusMessage *msg, void *data)
115 {
116         const char *sender, *path;
117         int err;
118
119         DBG("conn %p", conn);
120
121         sender = dbus_message_get_sender(msg);
122
123         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
124                                                         DBUS_TYPE_INVALID);
125
126         err = connman_agent_unregister(sender, path);
127         if (err < 0)
128                 return __connman_error_failed(msg, -err);
129
130         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
131 }
132
133 static const GDBusMethodTable manager_methods[] = {
134         { GDBUS_ASYNC_METHOD("Create",
135                         GDBUS_ARGS({ "properties", "a{sv}" }),
136                         GDBUS_ARGS({ "path", "o" }),
137                         create) },
138         { GDBUS_ASYNC_METHOD("Remove",
139                         GDBUS_ARGS({ "identifier", "o" }), NULL,
140                         remove) },
141         { GDBUS_METHOD("GetConnections", NULL,
142                         GDBUS_ARGS({ "connections", "a(oa{sv})" }),
143                         get_connections) },
144         { GDBUS_METHOD("RegisterAgent",
145                         GDBUS_ARGS({ "path", "o" }), NULL,
146                         register_agent) },
147         { GDBUS_METHOD("UnregisterAgent",
148                         GDBUS_ARGS({ "path", "o" }), NULL,
149                         unregister_agent) },
150         { },
151 };
152
153 static const GDBusSignalTable manager_signals[] = {
154         { GDBUS_SIGNAL("ConnectionAdded",
155                         GDBUS_ARGS({ "identifier", "o" },
156                                 { "properties", "a{sv}" })) },
157         { GDBUS_SIGNAL("ConnectionRemoved",
158                         GDBUS_ARGS({ "identifier", "o" })) },
159         { },
160 };
161
162 int __vpn_manager_init(void)
163 {
164         DBG("");
165
166         connection = connman_dbus_get_connection();
167         if (connection == NULL)
168                 return -1;
169
170         g_dbus_register_interface(connection, VPN_MANAGER_PATH,
171                                         VPN_MANAGER_INTERFACE,
172                                         manager_methods,
173                                         manager_signals, NULL, NULL, NULL);
174
175         vpn_connect_count = 0;
176
177         return 0;
178 }
179
180 void __vpn_manager_cleanup(void)
181 {
182         DBG("");
183
184         if (connection == NULL)
185                 return;
186
187         g_dbus_unregister_interface(connection, VPN_MANAGER_PATH,
188                                                 VPN_MANAGER_INTERFACE);
189
190         dbus_connection_unref(connection);
191 }