OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Connection Manager |
| 4 * |
| 5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved. |
| 6 *t |
| 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 /* This file is built into a shared object which is loaded dynamically |
| 23 * as a plugin into pppd. |
| 24 */ |
| 25 |
| 26 #ifdef HAVE_CONFIG_H |
| 27 #include <config.h> |
| 28 #endif |
| 29 |
| 30 #include <stdio.h> |
| 31 #include <stdlib.h> |
| 32 #include <string.h> |
| 33 #include <sys/types.h> |
| 34 #include <sys/stat.h> |
| 35 #include <syslog.h> |
| 36 #include <fcntl.h> |
| 37 #include <pppd/pppd.h> |
| 38 #include <pppd/fsm.h> |
| 39 #include <pppd/ipcp.h> |
| 40 #include <netinet/in.h> |
| 41 #include <arpa/inet.h> |
| 42 |
| 43 #include <dbus/dbus.h> |
| 44 |
| 45 #define INET_ADDRES_LEN (INET_ADDRSTRLEN + 5) |
| 46 #define INET_DNS_LEN (2*INET_ADDRSTRLEN + 9) |
| 47 |
| 48 static char *busname = NULL; |
| 49 static char *interface = NULL; |
| 50 static char *path = NULL; |
| 51 static const char syslog_prefix[] = "libppp-plugin.so"; |
| 52 |
| 53 static DBusConnection *connection = NULL; |
| 54 |
| 55 char pppd_version[] = VERSION; |
| 56 |
| 57 int plugin_init(void); |
| 58 |
| 59 static void append(DBusMessageIter *dict, const char *key, const char *value) |
| 60 { |
| 61 DBusMessageIter entry; |
| 62 /* We clean the environment before invoking openconnect, but |
| 63 might as well still filter out the few things that get |
| 64 added that we're not interested in */ |
| 65 if (!strcmp(key, "PWD") || !strcmp(key, "_") || |
| 66 !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") || |
| 67 !strcmp(key, "connman_network")) |
| 68 return; |
| 69 |
| 70 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, |
| 71 NULL, &entry); |
| 72 |
| 73 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key); |
| 74 |
| 75 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value); |
| 76 |
| 77 dbus_message_iter_close_container(dict, &entry); |
| 78 } |
| 79 |
| 80 static const char *get_phase_name(int phase) { |
| 81 static const char *names[] = { |
| 82 "DEAD", |
| 83 "INITIALIZE", |
| 84 "SERIALCONN", |
| 85 "DORMANT", |
| 86 "ESTABLISH", |
| 87 "AUTHENTICATE", |
| 88 "CALLBACK", |
| 89 "NETWORK", |
| 90 "RUNNING", |
| 91 "TERMINATE", |
| 92 "DISCONNECT", |
| 93 "HOLDOFF", |
| 94 "MASTER" |
| 95 }; |
| 96 if (phase < 0 || phase >= sizeof(names)/sizeof(names[0])) { |
| 97 return "PHASE_UNKNOWN"; |
| 98 } |
| 99 return names[phase]; |
| 100 } |
| 101 |
| 102 static int pptp_have_secret() |
| 103 { |
| 104 return 1; |
| 105 } |
| 106 |
| 107 static int pptp_get_secret(char *username, char *password) |
| 108 { |
| 109 DBusMessage *msg, *reply; |
| 110 const char *user, *pass; |
| 111 DBusError err; |
| 112 |
| 113 if (username == NULL && password == NULL) { |
| 114 syslog(LOG_ERR, "%s: %s: username/password set to NULL", |
| 115 syslog_prefix, __func__); |
| 116 return -1; |
| 117 } |
| 118 |
| 119 if (password == NULL) { |
| 120 syslog(LOG_ERR, "%s: %s: password set to NULL", |
| 121 syslog_prefix, __func__); |
| 122 return 1; |
| 123 } |
| 124 if (connection == NULL) { |
| 125 syslog(LOG_ERR, "%s: %s: connection not set", |
| 126 syslog_prefix, __func__); |
| 127 return -1; |
| 128 } |
| 129 |
| 130 dbus_error_init(&err); |
| 131 |
| 132 msg = dbus_message_new_method_call(busname, path, |
| 133 interface, "getsec"); |
| 134 if (msg == NULL) { |
| 135 syslog(LOG_ERR, "%s: %s: unable to create dbus call", |
| 136 syslog_prefix, __func__); |
| 137 return -1; |
| 138 } |
| 139 |
| 140 dbus_message_append_args(msg, DBUS_TYPE_INVALID, DBUS_TYPE_INVALID); |
| 141 |
| 142 reply = dbus_connection_send_with_reply_and_block(connection, |
| 143 msg, -1, &err); |
| 144 |
| 145 if (reply == NULL) { |
| 146 syslog(LOG_ERR, "%s: %s: unable to get dbus reply", |
| 147 syslog_prefix, __func__); |
| 148 if (dbus_error_is_set(&err) == TRUE) |
| 149 dbus_error_free(&err); |
| 150 |
| 151 dbus_message_unref(msg); |
| 152 return -1; |
| 153 } |
| 154 |
| 155 dbus_message_unref(msg); |
| 156 |
| 157 dbus_error_init(&err); |
| 158 |
| 159 if (dbus_message_get_args(reply, &err, DBUS_TYPE_STRING, &user, |
| 160 DBUS_TYPE_STRING, &pass, |
| 161 DBUS_TYPE_INVALID) == FALSE) { |
| 162 syslog(LOG_ERR, "%s: %s: unable to get args", |
| 163 syslog_prefix, __func__); |
| 164 if (dbus_error_is_set(&err) == TRUE) |
| 165 dbus_error_free(&err); |
| 166 |
| 167 dbus_message_unref(reply); |
| 168 return -1; |
| 169 } |
| 170 |
| 171 if (username != NULL) |
| 172 strcpy(username, user); |
| 173 |
| 174 strcpy(password, pass); |
| 175 |
| 176 dbus_message_unref(reply); |
| 177 |
| 178 return 1; |
| 179 } |
| 180 |
| 181 static void ppptp_up(void *data, int arg) |
| 182 { |
| 183 char buf[INET_ADDRES_LEN]; |
| 184 const char *reason = "connect"; |
| 185 DBusMessageIter iter, dict; |
| 186 DBusMessage *msg; |
| 187 |
| 188 syslog(LOG_INFO, "%s: %s: interface up %s", syslog_prefix, |
| 189 __func__, ifname); |
| 190 |
| 191 if (connection == NULL) { |
| 192 syslog(LOG_ERR, "%s: %s: connection not set", |
| 193 syslog_prefix, __func__); |
| 194 return; |
| 195 } |
| 196 |
| 197 if (ipcp_gotoptions[0].ouraddr == 0) { |
| 198 syslog(LOG_ERR, "%s: %s: our address not set", |
| 199 syslog_prefix, __func__); |
| 200 return; |
| 201 } |
| 202 |
| 203 msg = dbus_message_new_method_call(busname, path, |
| 204 interface, "notify"); |
| 205 if (msg == NULL) { |
| 206 syslog(LOG_ERR, "%s: %s: unable to create dbus message", |
| 207 syslog_prefix, __func__); |
| 208 return; |
| 209 } |
| 210 |
| 211 dbus_message_set_no_reply(msg, TRUE); |
| 212 |
| 213 dbus_message_append_args(msg, |
| 214 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); |
| 215 |
| 216 dbus_message_iter_init_append(msg, &iter); |
| 217 |
| 218 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, |
| 219 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING |
| 220 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING |
| 221 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); |
| 222 |
| 223 append(&dict, "INTERNAL_IFNAME", ifname); |
| 224 |
| 225 inet_ntop(AF_INET, &ipcp_gotoptions[0].ouraddr, buf, INET_ADDRSTRLEN); |
| 226 append(&dict, "INTERNAL_IP4_ADDRESS", buf); |
| 227 |
| 228 inet_ntop(AF_INET, &ipcp_hisoptions[0].hisaddr, buf, INET_ADDRSTRLEN); |
| 229 append(&dict, "EXTERNAL_IP4_ADDRESS", buf); |
| 230 |
| 231 if (ipcp_gotoptions[0].default_route) { |
| 232 inet_ntop(AF_INET, &ipcp_hisoptions[0].hisaddr, buf, |
| 233 INET_ADDRSTRLEN); |
| 234 append(&dict, "GATEWAY_ADDRESS", buf); |
| 235 } |
| 236 |
| 237 if (ipcp_gotoptions[0].dnsaddr[0] || ipcp_gotoptions[0].dnsaddr[1]) { |
| 238 if (ipcp_gotoptions[0].dnsaddr[0]) { |
| 239 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[0], |
| 240 buf, INET_ADDRSTRLEN); |
| 241 append(&dict, "DNS1", buf); |
| 242 } |
| 243 if (ipcp_gotoptions[0].dnsaddr[1]) { |
| 244 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[1], |
| 245 buf, INET_ADDRSTRLEN); |
| 246 append(&dict, "DNS2", buf); |
| 247 } |
| 248 } |
| 249 |
| 250 dbus_message_iter_close_container(&iter, &dict); |
| 251 |
| 252 dbus_connection_send(connection, msg, NULL); |
| 253 |
| 254 dbus_connection_flush(connection); |
| 255 |
| 256 dbus_message_unref(msg); |
| 257 } |
| 258 |
| 259 static void pptp_exit(void *data, int arg) |
| 260 { |
| 261 if (connection != NULL) { |
| 262 dbus_connection_unref(connection); |
| 263 connection = NULL; |
| 264 } |
| 265 |
| 266 if (busname != NULL) { |
| 267 free(busname); |
| 268 busname = NULL; |
| 269 } |
| 270 |
| 271 if (interface != NULL) { |
| 272 free(interface); |
| 273 interface = NULL; |
| 274 } |
| 275 |
| 276 if (path != NULL) { |
| 277 free(path); |
| 278 path = NULL; |
| 279 } |
| 280 } |
| 281 |
| 282 static void pptp_phase_change(void *data, int arg) |
| 283 { |
| 284 const char *reason = "disconnect"; |
| 285 DBusMessage *msg; |
| 286 |
| 287 syslog(LOG_INFO, "%s: %s: Change to %s (%d)", syslog_prefix, |
| 288 __func__, get_phase_name(arg), arg); |
| 289 |
| 290 if (connection == NULL) { |
| 291 syslog(LOG_ERR, "%s: %s: connection not set", |
| 292 syslog_prefix, __func__); |
| 293 return; |
| 294 } |
| 295 |
| 296 if (arg == PHASE_DEAD || arg == PHASE_DISCONNECT) { |
| 297 msg = dbus_message_new_method_call(busname, path, |
| 298 interface, "notify"); |
| 299 if (msg == NULL) { |
| 300 syslog(LOG_ERR, "%s: %s: unable to create dbus " |
| 301 "call", syslog_prefix, __func__); |
| 302 return; |
| 303 } |
| 304 |
| 305 dbus_message_set_no_reply(msg, TRUE); |
| 306 |
| 307 dbus_message_append_args(msg, |
| 308 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); |
| 309 |
| 310 dbus_connection_send(connection, msg, NULL); |
| 311 |
| 312 dbus_connection_flush(connection); |
| 313 |
| 314 dbus_message_unref(msg); |
| 315 } |
| 316 } |
| 317 |
| 318 int plugin_init(void) |
| 319 { |
| 320 DBusError error; |
| 321 static const char *bus, *inter, *p; |
| 322 |
| 323 dbus_error_init(&error); |
| 324 |
| 325 bus = getenv("CONNMAN_BUSNAME"); |
| 326 inter = getenv("CONNMAN_INTERFACE"); |
| 327 p = getenv("CONNMAN_PATH"); |
| 328 |
| 329 if (!bus || !inter || !p) { |
| 330 syslog(LOG_ERR, "%s: %s: bus, interface, or path not set", |
| 331 syslog_prefix, __func__); |
| 332 return -1; |
| 333 } |
| 334 |
| 335 busname = strdup(bus); |
| 336 interface = strdup(inter); |
| 337 path = strdup(p); |
| 338 |
| 339 if (!busname || !interface || !path) { |
| 340 syslog(LOG_ERR, "%s: %s: alloc of bus, interface, or path " |
| 341 "failed", syslog_prefix, __func__); |
| 342 pptp_exit(NULL, 0); |
| 343 return -1; |
| 344 } |
| 345 |
| 346 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
| 347 if (connection == NULL) { |
| 348 syslog(LOG_ERR, "%s: %s: connection not set", |
| 349 syslog_prefix, __func__); |
| 350 if (dbus_error_is_set(&error) == TRUE) |
| 351 dbus_error_free(&error); |
| 352 |
| 353 pptp_exit(NULL, 0); |
| 354 return -1; |
| 355 } |
| 356 |
| 357 pap_passwd_hook = pptp_get_secret; |
| 358 chap_passwd_hook = pptp_get_secret; |
| 359 |
| 360 chap_check_hook = pptp_have_secret; |
| 361 pap_check_hook = pptp_have_secret; |
| 362 |
| 363 add_notifier(&ip_up_notifier, ppptp_up, NULL); |
| 364 add_notifier(&phasechange, pptp_phase_change, NULL); |
| 365 add_notifier(&exitnotify, pptp_exit, connection); |
| 366 |
| 367 return 0; |
| 368 } |
OLD | NEW |