5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
6 * Copyright (C) 2011 Intel Corporation. All rights reserved.
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.
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.
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
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/provider.h>
38 #include <connman/log.h>
39 #include <connman/task.h>
40 #include <connman/dbus.h>
41 #include <connman/inet.h>
45 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
55 const char *vpnc_default;
58 { "PPTP.User", "user", NULL, OPT_STRING },
59 { "PPTP.Password", "password", NULL, OPT_STRING },
60 { "PPTP.EchoFailure", "lcp-echo-failure", "0", OPT_STRING },
61 { "PPTP.EchoInterval", "lcp-echo-interval", "0", OPT_STRING },
62 { "PPTP.Debug", "debug", NULL, OPT_STRING },
63 { "PPTP.RefuseEAP", "refuse-eap", NULL, OPT_BOOL },
64 { "PPTP.RefusePAP", "refuse-pap", NULL, OPT_BOOL },
65 { "PPTP.RefuseCHAP", "refuse-chap", NULL, OPT_BOOL },
66 { "PPTP.RefuseMSCHAP", "refuse-mschap", NULL, OPT_BOOL },
67 { "PPTP.RefuseMSCHAP2", "refuse-mschapv2", NULL, OPT_BOOL },
68 { "PPTP.NoBSDComp", "nobsdcomp", NULL, OPT_BOOL },
69 { "PPTP.NoDeflate", "nodeflatey", NULL, OPT_BOOL },
70 { "PPTP.RequirMPPE", "require-mppe", NULL, OPT_BOOL },
71 { "PPTP.RequirMPPE40", "require-mppe-40", NULL, OPT_BOOL },
72 { "PPTP.RequirMPPE128", "require-mppe-128", NULL, OPT_BOOL },
73 { "PPTP.RequirMPPEStateful", "mppe-stateful", NULL, OPT_BOOL },
74 { "PPTP.NoVJ", "no-vj-comp", NULL, OPT_BOOL },
77 static DBusConnection *connection;
79 static DBusMessage *pptp_get_sec(struct connman_task *task,
80 DBusMessage *msg, void *user_data)
82 const char *user, *passwd;
83 struct connman_provider *provider = user_data;
86 if (dbus_message_get_no_reply(msg) == TRUE)
89 user = connman_provider_get_string(provider, "PPTP.User");
90 passwd = connman_provider_get_string(provider, "PPTP.Password");
91 if (user == NULL || strlen(user) == 0 ||
92 passwd == NULL || strlen(passwd) == 0)
95 reply = dbus_message_new_method_return(msg);
99 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
100 DBUS_TYPE_STRING, &passwd,
105 static int pptp_notify(DBusMessage *msg, struct connman_provider *provider)
107 DBusMessageIter iter, dict;
108 const char *reason, *key, *value;
109 char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
110 char *ifname = NULL, *nameservers = NULL;
111 struct connman_ipaddress *ipaddress = NULL;
113 dbus_message_iter_init(msg, &iter);
115 dbus_message_iter_get_basic(&iter, &reason);
116 dbus_message_iter_next(&iter);
118 if (provider == NULL) {
119 connman_error("No provider found");
120 return VPN_STATE_FAILURE;
123 if (strcmp(reason, "connect"))
124 return VPN_STATE_DISCONNECT;
126 dbus_message_iter_recurse(&iter, &dict);
128 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
129 DBusMessageIter entry;
131 dbus_message_iter_recurse(&dict, &entry);
132 dbus_message_iter_get_basic(&entry, &key);
133 dbus_message_iter_next(&entry);
134 dbus_message_iter_get_basic(&entry, &value);
136 DBG("%s = %s", key, value);
138 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
139 connman_provider_set_string(provider, "Address", value);
140 addressv4 = g_strdup(value);
143 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
144 connman_provider_set_string(provider, "Netmask", value);
145 netmask = g_strdup(value);
148 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
149 connman_provider_set_string(provider, "DNS", value);
150 nameservers = g_strdup(value);
153 if (!strcmp(key, "INTERNAL_IFNAME"))
154 ifname = g_strdup(value);
156 dbus_message_iter_next(&dict);
159 if (vpn_set_ifname(provider, ifname) < 0) {
164 return VPN_STATE_FAILURE;
167 if (addressv4 != NULL)
168 ipaddress = connman_ipaddress_alloc(AF_INET);
172 if (ipaddress == NULL) {
173 connman_error("No IP address for provider");
177 return VPN_STATE_FAILURE;
180 value = connman_provider_get_string(provider, "Host");
182 connman_provider_set_string(provider, "Gateway", value);
183 gateway = g_strdup(value);
186 if (addressv4 != NULL)
187 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
190 connman_provider_set_ipaddress(provider, ipaddress);
191 connman_provider_set_nameservers(provider, nameservers);
197 connman_ipaddress_free(ipaddress);
199 return VPN_STATE_CONNECT;
202 static void pptp_write_bool_option(struct connman_task *task,
203 const char *key, const char *value)
205 if (key != NULL && value != NULL) {
206 if (strcmp(value, "yes") == 0)
207 connman_task_add_argument(task, key, NULL);
211 static int pptp_connect(struct connman_provider *provider,
212 struct connman_task *task, const char *if_name)
214 const char *opt_s, *host;
218 if (connman_task_set_notify(task, "getsec",
219 pptp_get_sec, provider))
222 host = connman_provider_get_string(provider, "Host");
224 connman_error("Host not set; cannot enable VPN");
228 str = g_strdup_printf("%s %s --nolaunchpppd --loglevel 2",
231 connman_error("can not allocate memory");
235 connman_task_add_argument(task, "pty", str);
238 connman_task_add_argument(task, "nodetach", NULL);
239 connman_task_add_argument(task, "lock", NULL);
240 connman_task_add_argument(task, "usepeerdns", NULL);
241 connman_task_add_argument(task, "noipdefault", NULL);
242 connman_task_add_argument(task, "noauth", NULL);
243 connman_task_add_argument(task, "nodefaultroute", NULL);
244 connman_task_add_argument(task, "ipparam", "pptp_plugin");
246 for (i = 0; i < (int)ARRAY_SIZE(pptp_options); i++) {
247 opt_s = connman_provider_get_string(provider,
248 pptp_options[i].cm_opt);
250 opt_s = pptp_options[i].vpnc_default;
255 if (pptp_options[i].type == OPT_STRING)
256 connman_task_add_argument(task,
257 pptp_options[i].pptp_opt, opt_s);
258 else if (pptp_options[i].type == OPT_BOOL)
259 pptp_write_bool_option(task,
260 pptp_options[i].pptp_opt, opt_s);
263 connman_task_add_argument(task, "plugin",
264 SCRIPTDIR "/libppp-plugin.so");
266 err = connman_task_run(task, vpn_died, provider,
269 connman_error("pptp failed to start");
276 static int pptp_error_code(int exit_code)
281 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
283 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
285 return CONNMAN_PROVIDER_ERROR_AUTH_FAILED;
287 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
291 static struct vpn_driver vpn_driver = {
292 .flags = VPN_FLAG_NO_TUN,
293 .notify = pptp_notify,
294 .connect = pptp_connect,
295 .error_code = pptp_error_code,
298 static int pptp_init(void)
300 connection = connman_dbus_get_connection();
302 return vpn_register("pptp", &vpn_driver, PPPD);
305 static void pptp_exit(void)
307 vpn_unregister("pptp");
309 dbus_connection_unref(connection);
312 CONNMAN_PLUGIN_DEFINE(pptp, "pptp plugin", VERSION,
313 CONNMAN_PLUGIN_PRIORITY_DEFAULT, pptp_init, pptp_exit)