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].dnsaddr[0] || ipcp_gotoptions[0].dnsaddr[1]) { |
| 232 if (ipcp_gotoptions[0].dnsaddr[0]) { |
| 233 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[0], |
| 234 buf, INET_ADDRSTRLEN); |
| 235 append(&dict, "DNS1", buf); |
| 236 } |
| 237 if (ipcp_gotoptions[0].dnsaddr[1]) { |
| 238 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[1], |
| 239 buf, INET_ADDRSTRLEN); |
| 240 append(&dict, "DNS2", buf); |
| 241 } |
| 242 } |
| 243 |
| 244 dbus_message_iter_close_container(&iter, &dict); |
| 245 |
| 246 dbus_connection_send(connection, msg, NULL); |
| 247 |
| 248 dbus_connection_flush(connection); |
| 249 |
| 250 dbus_message_unref(msg); |
| 251 } |
| 252 |
| 253 static void pptp_exit(void *data, int arg) |
| 254 { |
| 255 if (connection != NULL) { |
| 256 dbus_connection_unref(connection); |
| 257 connection = NULL; |
| 258 } |
| 259 |
| 260 if (busname != NULL) { |
| 261 free(busname); |
| 262 busname = NULL; |
| 263 } |
| 264 |
| 265 if (interface != NULL) { |
| 266 free(interface); |
| 267 interface = NULL; |
| 268 } |
| 269 |
| 270 if (path != NULL) { |
| 271 free(path); |
| 272 path = NULL; |
| 273 } |
| 274 } |
| 275 |
| 276 static void pptp_phase_change(void *data, int arg) |
| 277 { |
| 278 const char *reason = "disconnect"; |
| 279 DBusMessage *msg; |
| 280 |
| 281 syslog(LOG_INFO, "%s: %s: Change to %s (%d)", syslog_prefix, |
| 282 __func__, get_phase_name(arg), arg); |
| 283 |
| 284 if (connection == NULL) { |
| 285 syslog(LOG_ERR, "%s: %s: connection not set", |
| 286 syslog_prefix, __func__); |
| 287 return; |
| 288 } |
| 289 |
| 290 if (arg == PHASE_DEAD || arg == PHASE_DISCONNECT) { |
| 291 msg = dbus_message_new_method_call(busname, path, |
| 292 interface, "notify"); |
| 293 if (msg == NULL) { |
| 294 syslog(LOG_ERR, "%s: %s: unable to create dbus " |
| 295 "call", syslog_prefix, __func__); |
| 296 return; |
| 297 } |
| 298 |
| 299 dbus_message_set_no_reply(msg, TRUE); |
| 300 |
| 301 dbus_message_append_args(msg, |
| 302 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); |
| 303 |
| 304 dbus_connection_send(connection, msg, NULL); |
| 305 |
| 306 dbus_connection_flush(connection); |
| 307 |
| 308 dbus_message_unref(msg); |
| 309 } |
| 310 } |
| 311 |
| 312 int plugin_init(void) |
| 313 { |
| 314 DBusError error; |
| 315 static const char *bus, *inter, *p; |
| 316 |
| 317 dbus_error_init(&error); |
| 318 |
| 319 bus = getenv("CONNMAN_BUSNAME"); |
| 320 inter = getenv("CONNMAN_INTERFACE"); |
| 321 p = getenv("CONNMAN_PATH"); |
| 322 |
| 323 if (!bus || !inter || !p) { |
| 324 syslog(LOG_ERR, "%s: %s: bus, interface, or path not set", |
| 325 syslog_prefix, __func__); |
| 326 return -1; |
| 327 } |
| 328 |
| 329 busname = strdup(bus); |
| 330 interface = strdup(inter); |
| 331 path = strdup(p); |
| 332 |
| 333 if (!busname || !interface || !path) { |
| 334 syslog(LOG_ERR, "%s: %s: alloc of bus, interface, or path " |
| 335 "failed", syslog_prefix, __func__); |
| 336 pptp_exit(NULL, 0); |
| 337 return -1; |
| 338 } |
| 339 |
| 340 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); |
| 341 if (connection == NULL) { |
| 342 syslog(LOG_ERR, "%s: %s: connection not set", |
| 343 syslog_prefix, __func__); |
| 344 if (dbus_error_is_set(&error) == TRUE) |
| 345 dbus_error_free(&error); |
| 346 |
| 347 pptp_exit(NULL, 0); |
| 348 return -1; |
| 349 } |
| 350 |
| 351 pap_passwd_hook = pptp_get_secret; |
| 352 chap_passwd_hook = pptp_get_secret; |
| 353 |
| 354 chap_check_hook = pptp_have_secret; |
| 355 pap_check_hook = pptp_have_secret; |
| 356 |
| 357 add_notifier(&ip_up_notifier, ppptp_up, NULL); |
| 358 add_notifier(&phasechange, pptp_phase_change, NULL); |
| 359 add_notifier(&exitnotify, pptp_exit, connection); |
| 360 |
| 361 return 0; |
| 362 } |
OLD | NEW |