OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * | |
3 * Connection Manager | |
4 * | |
5 * Copyright (C) 2007-2010 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 */ | |
Sam Leffler
2011/03/04 17:47:45
need a comment explaining this is built into a .so
kmixter1
2011/03/05 09:30:51
Done.
| |
21 | |
22 #ifdef HAVE_CONFIG_H | |
23 #include <config.h> | |
24 #endif | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <sys/types.h> | |
30 #include <sys/stat.h> | |
31 #include <syslog.h> | |
32 #include <fcntl.h> | |
33 #include <pppd/pppd.h> | |
34 #include <pppd/fsm.h> | |
35 #include <pppd/ipcp.h> | |
36 #include <netinet/in.h> | |
37 #include <arpa/inet.h> | |
38 | |
39 #include <dbus/dbus.h> | |
40 | |
41 #define INET_ADDRES_LEN (INET_ADDRSTRLEN + 5) | |
42 #define INET_DNS_LEN (2*INET_ADDRSTRLEN + 9) | |
43 | |
44 static char *busname = NULL; | |
45 static char *interface = NULL; | |
46 static char *path = NULL; | |
47 | |
48 static DBusConnection *connection = NULL; | |
49 | |
50 char pppd_version[] = VERSION; | |
51 | |
52 int plugin_init(void); | |
53 | |
54 static void append(DBusMessageIter *dict, const char *key, const char *value) | |
55 { | |
56 DBusMessageIter entry; | |
57 /* We clean the environment before invoking openconnect, but | |
58 might as well still filter out the few things that get | |
59 added that we're not interested in */ | |
60 if (!strcmp(key, "PWD") || !strcmp(key, "_") || | |
61 !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") || | |
62 !strcmp(key, "connman_network")) | |
63 return; | |
64 | |
65 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, | |
66 NULL, &entry); | |
67 | |
68 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key); | |
69 | |
70 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value); | |
71 | |
72 dbus_message_iter_close_container(dict, &entry); | |
73 } | |
74 | |
75 | |
76 static int pptp_have_secret() | |
77 { | |
78 return 1; | |
79 } | |
80 | |
81 static int pptp_get_secret (char *username, char *password) | |
Sam Leffler
2011/03/04 17:47:45
s/secret (/secret(/ (seems inconsistent--but perha
kmixter1
2011/03/05 09:30:51
Done.
| |
82 { | |
83 DBusMessage *msg, *reply; | |
84 const char *user, *pass; | |
85 DBusError err; | |
86 | |
87 syslog(LOG_INFO, "libppp-plugin.so: requesting secret"); | |
88 if (username == NULL && password == NULL) | |
Sam Leffler
2011/03/04 17:47:45
seems like you want some error msgs here; maybe se
kmixter1
2011/03/05 09:30:51
Done. Now I'm logging like error messages are fre
| |
89 return -1; | |
90 | |
91 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
92 if (password == NULL) | |
93 return 1; | |
94 | |
95 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
96 if (connection == NULL) | |
97 return -1; | |
98 | |
99 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
100 dbus_error_init(&err); | |
101 | |
102 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
103 msg = dbus_message_new_method_call(busname, path, | |
104 interface, "getsec"); | |
105 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
106 if (msg == NULL) | |
107 return -1; | |
108 | |
109 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
110 dbus_message_append_args(msg, DBUS_TYPE_INVALID, DBUS_TYPE_INVALID); | |
111 | |
112 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
113 reply = dbus_connection_send_with_reply_and_block(connection, | |
114 msg, -1, &err); | |
115 | |
116 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
117 if (reply == NULL) { | |
118 if (dbus_error_is_set(&err) == TRUE) | |
119 dbus_error_free(&err); | |
120 | |
121 dbus_message_unref(msg); | |
122 return -1; | |
123 } | |
124 | |
125 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
126 dbus_message_unref(msg); | |
127 | |
128 dbus_error_init(&err); | |
129 | |
130 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
131 if (dbus_message_get_args(reply, &err, DBUS_TYPE_STRING, &user, | |
132 DBUS_TYPE_STRING, &pass, | |
133 DBUS_TYPE_INVALID) == FALSE) { | |
134 if (dbus_error_is_set(&err) == TRUE) | |
135 dbus_error_free(&err); | |
136 | |
137 dbus_message_unref(reply); | |
138 return -1; | |
139 } | |
140 | |
141 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
142 if (username != NULL) | |
143 strcpy(username, user); | |
144 | |
145 strcpy(password, pass); | |
146 | |
147 dbus_message_unref(reply); | |
148 | |
149 return 1; | |
150 } | |
151 | |
152 static void ppptp_up (void *data, int arg) | |
153 { | |
154 char buf[INET_ADDRES_LEN]; | |
155 const char *reason = "connect"; | |
156 DBusMessageIter iter, dict; | |
157 DBusMessage *msg; | |
158 | |
159 syslog(LOG_INFO, "libppp-plugin.so: interface up %s", ifname); | |
160 if (connection == NULL) | |
161 return; | |
162 | |
163 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
164 if (ipcp_gotoptions[0].ouraddr == 0) | |
165 return; | |
166 | |
167 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
168 msg = dbus_message_new_method_call(busname, path, | |
169 interface, "notify"); | |
170 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
171 if (msg == NULL) | |
172 return; | |
173 | |
174 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
175 dbus_message_set_no_reply(msg, TRUE); | |
176 | |
177 dbus_message_append_args(msg, | |
178 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); | |
179 | |
180 dbus_message_iter_init_append(msg, &iter); | |
181 | |
182 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, | |
183 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING | |
184 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING | |
185 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict); | |
186 | |
187 append(&dict, "INTERNAL_IFNAME", ifname); | |
188 | |
189 inet_ntop(AF_INET, &ipcp_gotoptions[0].ouraddr, buf, INET_ADDRSTRLEN); | |
190 append(&dict, "INTERNAL_IP4_ADDRESS", buf); | |
191 | |
192 inet_ntop(AF_INET, &ipcp_gotoptions[0].hisaddr, buf, INET_ADDRSTRLEN); | |
193 append(&dict, "EXTERNAL_IP4_ADDRESS", buf); | |
194 | |
195 inet_ntop(AF_INET, &ipcp_hisoptions[0].ouraddr, buf, INET_ADDRSTRLEN); | |
196 append(&dict, "EXTERNAL_IP4_ADDRESS2", buf); | |
197 | |
198 strcpy(buf, "255.255.255.255"); | |
199 append(&dict, "INTERNAL_IP4_NETMASK", buf); | |
200 | |
201 if (ipcp_gotoptions[0].dnsaddr[0] || ipcp_gotoptions[0].dnsaddr[1]) { | |
202 if (ipcp_gotoptions[0].dnsaddr[0]) { | |
203 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[0], | |
204 buf, INET_ADDRSTRLEN); | |
205 append(&dict, "DNS1", buf); | |
206 } | |
207 if (ipcp_gotoptions[0].dnsaddr[1]) { | |
208 inet_ntop(AF_INET, &ipcp_gotoptions[0].dnsaddr[1], | |
209 buf, INET_ADDRSTRLEN); | |
210 append(&dict, "DNS2", buf); | |
211 } | |
212 } | |
213 | |
214 append(&dict, "MTU", "1400"); | |
Sam Leffler
2011/03/04 17:47:45
say what?
kmixter1
2011/03/05 09:30:51
yeah. upstream. I'll remove it since it's obvious
| |
215 | |
216 dbus_message_iter_close_container(&iter, &dict); | |
217 | |
218 dbus_connection_send(connection, msg, NULL); | |
219 | |
220 dbus_connection_flush(connection); | |
221 | |
222 dbus_message_unref(msg); | |
223 syslog(LOG_INFO, "libppp-plugin.so: CP%d", __LINE__); | |
224 } | |
225 | |
226 static void pptp_exit (void *data, int arg) | |
Sam Leffler
2011/03/04 17:47:45
s/ (/(/ or something to make style consistent
kmixter1
2011/03/05 09:30:51
Done.
| |
227 { | |
228 if (connection != NULL) { | |
229 dbus_connection_unref(connection); | |
230 connection = NULL; | |
231 } | |
232 | |
233 if (busname != NULL) { | |
234 free(busname); | |
235 busname = NULL; | |
236 } | |
237 | |
238 if (interface != NULL) { | |
239 free(interface); | |
240 interface = NULL; | |
241 } | |
242 | |
243 if (path != NULL) { | |
244 free(path); | |
245 path = NULL; | |
246 } | |
247 } | |
248 | |
249 static void pptp_phase_change(void *data, int arg) | |
250 { | |
251 const char *reason = "disconnect"; | |
252 DBusMessage *msg; | |
253 | |
254 syslog(LOG_INFO, "libppp-plugin.so: phase change"); | |
255 if (connection == NULL) | |
256 return; | |
257 | |
258 if (arg == PHASE_DEAD || arg == PHASE_DISCONNECT) { | |
259 msg = dbus_message_new_method_call(busname, path, | |
260 interface, "notify"); | |
261 if (msg == NULL) | |
262 return; | |
263 | |
264 dbus_message_set_no_reply(msg, TRUE); | |
265 | |
266 dbus_message_append_args(msg, | |
267 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); | |
268 | |
269 dbus_connection_send(connection, msg, NULL); | |
270 | |
271 dbus_connection_flush(connection); | |
272 | |
273 dbus_message_unref(msg); | |
274 } | |
275 } | |
276 | |
277 int plugin_init(void) | |
278 { | |
279 DBusError error; | |
280 static const char *bus, *inter, *p; | |
281 | |
282 dbus_error_init(&error); | |
283 | |
284 bus = getenv("CONNMAN_BUSNAME"); | |
285 inter = getenv("CONNMAN_INTERFACE"); | |
286 p = getenv("CONNMAN_PATH"); | |
287 | |
288 syslog(LOG_INFO, "libppp-plugin.so: initialized %s, %s, %s", bus, inter, p); | |
289 | |
290 if (!bus || !inter || !p) | |
291 return -1; | |
292 | |
293 busname = strdup(bus); | |
294 interface = strdup(inter); | |
295 path = strdup(p); | |
296 | |
297 if (!busname || !interface || !path) { | |
298 pptp_exit(NULL, 0); | |
299 return -1; | |
300 } | |
301 | |
302 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); | |
303 if (connection == NULL) { | |
304 if (dbus_error_is_set(&error) == TRUE) | |
305 dbus_error_free(&error); | |
306 | |
307 pptp_exit(NULL, 0); | |
308 return -1; | |
309 } | |
310 | |
311 pap_passwd_hook = pptp_get_secret; | |
312 chap_passwd_hook = pptp_get_secret; | |
313 | |
314 chap_check_hook = pptp_have_secret; | |
315 pap_check_hook = pptp_have_secret; | |
316 | |
317 add_notifier (&ip_up_notifier, ppptp_up, NULL); | |
318 add_notifier (&phasechange, pptp_phase_change, NULL); | |
319 add_notifier (&exitnotify, pptp_exit, connection); | |
320 | |
321 return 0; | |
322 } | |
OLD | NEW |