summaryrefslogtreecommitdiff
path: root/plugins/dhclient.c
blob: 7e5e6d0ac86160eee97b7aae9821681a3f8a74c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <sys/wait.h>
#include <glib/gstdio.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/driver.h>
#include <connman/dbus.h>
#include <connman/log.h>

#include "inet.h"

#define DHCLIENT_INTF "org.isc.dhclient"
#define DHCLIENT_PATH "/org/isc/dhclient"

static const char *busname;

struct dhclient_task {
	GPid pid;
	int ifindex;
	gchar *ifname;
	struct connman_element *element;
};

static GSList *task_list = NULL;

static struct dhclient_task *find_task_by_pid(GPid pid)
{
	GSList *list;

	for (list = task_list; list; list = list->next) {
		struct dhclient_task *task = list->data;

		if (task->pid == pid)
			return task;
	}

	return NULL;
}

static struct dhclient_task *find_task_by_index(int index)
{
	GSList *list;

	for (list = task_list; list; list = list->next) {
		struct dhclient_task *task = list->data;

		if (task->ifindex == index)
			return task;
	}

	return NULL;
}

static void kill_task(struct dhclient_task *task)
{
	DBG("task %p name %s pid %d", task, task->ifname, task->pid);

	if (task->pid > 0)
		kill(task->pid, SIGTERM);
}

static void unlink_task(struct dhclient_task *task)
{
	gchar *pathname;

	DBG("task %p name %s pid %d", task, task->ifname, task->pid);

	pathname = g_strdup_printf("%s/dhclient.%s.pid",
						STATEDIR, task->ifname);
	g_unlink(pathname);
	g_free(pathname);

	pathname = g_strdup_printf("%s/dhclient.%s.leases",
						STATEDIR, task->ifname);
	g_unlink(pathname);
	g_free(pathname);
}

static void task_died(GPid pid, gint status, gpointer data)
{
	struct dhclient_task *task = data;

	if (WIFEXITED(status))
		DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
	else
		DBG("signal %d killed %s", WTERMSIG(status), task->ifname);

	g_spawn_close_pid(pid);
	task->pid = 0;

	task_list = g_slist_remove(task_list, task);

	unlink_task(task);

	g_free(task->ifname);
	g_free(task);
}

static void task_setup(gpointer data)
{
	struct dhclient_task *task = data;

	DBG("task %p name %s", task, task->ifname);
}

static int dhclient_probe(struct connman_element *element)
{
	struct dhclient_task *task;
	char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
	char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];

	DBG("element %p name %s", element, element->name);

	if (access(DHCLIENT, X_OK) < 0)
		return -errno;

	task = g_try_new0(struct dhclient_task, 1);
	if (task == NULL)
		return -ENOMEM;

	task->ifindex = element->index;
	task->ifname = inet_index2name(element->index);
	task->element = element;

	if (task->ifname == NULL) {
		g_free(task);
		return -ENOMEM;
	}

	DBG("request %s", task->ifname);

	snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
	snprintf(pidfile, sizeof(pidfile) - 1,
			"%s/dhclient.%s.pid", STATEDIR, task->ifname);
	snprintf(leases, sizeof(leases) - 1,
			"%s/dhclient.%s.leases", STATEDIR, task->ifname);
	snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
	snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);

	argv[0] = DHCLIENT;
	argv[1] = "-d";
	argv[2] = "-q";
	argv[3] = "-n";
	argv[4] = "-e";
	argv[5] = address;
	argv[6] = "-pf";
	argv[7] = pidfile;
	argv[8] = "-lf";
	argv[9] = leases;
	argv[10] = "-cf";
	argv[11] = config;
	argv[12] = "-sf";
	argv[13] = script;
	argv[14] = task->ifname;
	argv[15] = NULL;

	envp[0] = NULL;

	if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
				task_setup, task, &task->pid, NULL) == FALSE) {
		connman_error("Failed to spawn dhclient");
		return -1;
	}

	task_list = g_slist_append(task_list, task);

	g_child_watch_add(task->pid, task_died, task);

	DBG("executed %s with pid %d", DHCLIENT, task->pid);

	return 0;
}

static void dhclient_remove(struct connman_element *element)
{
	struct dhclient_task *task;

	DBG("element %p name %s", element, element->name);

	task = find_task_by_index(element->index);
	if (task != NULL)
		task_list = g_slist_remove(task_list, task);

	if (task == NULL)
		return;

	DBG("release %s", task->ifname);

	kill_task(task);
}

static struct connman_driver dhclient_driver = {
	.name		= "dhclient",
	.type		= CONNMAN_ELEMENT_TYPE_DHCP,
	.probe		= dhclient_probe,
	.remove		= dhclient_remove,
};

static DBusHandlerResult dhclient_filter(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	DBusMessageIter iter, dict;
	dbus_uint32_t pid;
	struct dhclient_task *task;
	const char *text, *key, *value;

	if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	dbus_message_iter_init(msg, &iter);

	dbus_message_iter_get_basic(&iter, &pid);
	dbus_message_iter_next(&iter);

	dbus_message_iter_get_basic(&iter, &text);
	dbus_message_iter_next(&iter);

	DBG("change %d to %s", pid, text);

	task = find_task_by_pid(pid);

	if (task == NULL)
		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;

	dbus_message_iter_recurse(&iter, &dict);

	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry;

		dbus_message_iter_recurse(&dict, &entry);
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
		dbus_message_iter_get_basic(&entry, &value);

		DBG("%s = %s", key, value);

		if (g_ascii_strcasecmp(key, "new_ip_address") == 0) {
			g_free(task->element->ipv4.address);
			task->element->ipv4.address = g_strdup(value);
		}

		if (g_ascii_strcasecmp(key, "new_subnet_mask") == 0) {
			g_free(task->element->ipv4.netmask);
			task->element->ipv4.netmask = g_strdup(value);
		}

		if (g_ascii_strcasecmp(key, "new_routers") == 0) {
			g_free(task->element->ipv4.gateway);
			task->element->ipv4.gateway = g_strdup(value);
		}

		if (g_ascii_strcasecmp(key, "new_network_number") == 0) {
			g_free(task->element->ipv4.network);
			task->element->ipv4.network = g_strdup(value);
		}

		if (g_ascii_strcasecmp(key, "new_broadcast_address") == 0) {
			g_free(task->element->ipv4.broadcast);
			task->element->ipv4.broadcast = g_strdup(value);
		}

		if (g_ascii_strcasecmp(key, "new_domain_name_servers") == 0) {
			g_free(task->element->ipv4.nameserver);
			task->element->ipv4.nameserver = g_strdup(value);
		}

		dbus_message_iter_next(&dict);
	}

	if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
	} else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
				g_ascii_strcasecmp(text, "REBOOT") == 0) {
		struct connman_element *element;
		element = connman_element_create(NULL);
		element->type = CONNMAN_ELEMENT_TYPE_IPV4;
		element->index = task->ifindex;
		connman_element_update(task->element);
		if (connman_element_register(element, task->element) < 0)
			connman_element_unref(element);
	} else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
				g_ascii_strcasecmp(text, "REBIND") == 0) {
		connman_element_update(task->element);
	} else {
	}

	return DBUS_HANDLER_RESULT_HANDLED;
}

static DBusConnection *connection;

static const char *dhclient_rule = "path=" DHCLIENT_PATH
						",interface=" DHCLIENT_INTF;

static int dhclient_init(void)
{
	int err;

	connection = connman_dbus_get_connection();

	busname = dbus_bus_get_unique_name(connection);
	busname = CONNMAN_SERVICE;

	dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);

	dbus_bus_add_match(connection, dhclient_rule, NULL);

	err = connman_driver_register(&dhclient_driver);
	if (err < 0) {
		dbus_connection_unref(connection);
		return err;
	}

	return 0;
}

static void dhclient_exit(void)
{
	GSList *list;

	for (list = task_list; list; list = list->next) {
		struct dhclient_task *task = list->data;

		DBG("killing process %d", task->pid);

		kill_task(task);
		unlink_task(task);
	}

	g_slist_free(task_list);

	connman_driver_unregister(&dhclient_driver);

	dbus_bus_remove_match(connection, dhclient_rule, NULL);

	dbus_connection_remove_filter(connection, dhclient_filter, NULL);

	dbus_connection_unref(connection);
}

CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
		CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)