Use the exported D-Bus service name instead of hardcoded string
[connman] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <net/if.h>
37
38 #include <glib.h>
39 #include <gdbus.h>
40
41 #include <connman/plugin.h>
42 #include <connman/dhcp.h>
43 #include <connman/dbus.h>
44
45 #define DHCLIENT_INTF "org.isc.dhclient"
46 #define DHCLIENT_PATH "/org/isc/dhclient"
47
48 static const char *busname;
49
50 struct dhclient_task {
51         GPid pid;
52         int ifindex;
53         char *ifname;
54         struct connman_iface *iface;
55 };
56
57 static GSList *tasks = NULL;
58
59 static struct dhclient_task *find_task_by_pid(GPid pid)
60 {
61         GSList *list;
62
63         for (list = tasks; list; list = list->next) {
64                 struct dhclient_task *task = list->data;
65
66                 if (task->pid == pid)
67                         return task;
68         }
69
70         return NULL;
71 }
72
73 static struct dhclient_task *find_task_by_index(int index)
74 {
75         GSList *list;
76
77         for (list = tasks; list; list = list->next) {
78                 struct dhclient_task *task = list->data;
79
80                 if (task->ifindex == index)
81                         return task;
82         }
83
84         return NULL;
85 }
86
87 static void kill_task(struct dhclient_task *task)
88 {
89         if (task->pid > 0)
90                 kill(task->pid, SIGTERM);
91 }
92
93 static void task_died(GPid pid, gint status, gpointer data)
94 {
95         struct dhclient_task *task = data;
96         char pathname[PATH_MAX];
97
98         if (WIFEXITED(status))
99                 printf("[DHCP] exit status %d for %s\n",
100                                         WEXITSTATUS(status), task->ifname);
101         else
102                 printf("[DHCP] signal %d killed %s\n",
103                                         WTERMSIG(status), task->ifname);
104
105         g_spawn_close_pid(pid);
106         task->pid = 0;
107
108         tasks = g_slist_remove(tasks, task);
109
110         snprintf(pathname, sizeof(pathname) - 1,
111                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
112         unlink(pathname);
113
114         snprintf(pathname, sizeof(pathname) - 1,
115                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
116         unlink(pathname);
117
118         free(task->ifname);
119
120         g_free(task);
121 }
122
123 static void task_setup(gpointer data)
124 {
125         struct dhclient_task *task = data;
126
127         printf("[DHCP] setup %s\n", task->ifname);
128 }
129
130 static int dhclient_request(struct connman_iface *iface)
131 {
132         struct ifreq ifr;
133         struct dhclient_task *task;
134         char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
135         char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
136         int sk, err;
137
138         sk = socket(PF_INET, SOCK_DGRAM, 0);
139         if (sk < 0)
140                 return -EIO;
141
142         memset(&ifr, 0, sizeof(ifr));
143         ifr.ifr_ifindex = iface->index;
144
145         err = ioctl(sk, SIOCGIFNAME, &ifr);
146
147         close(sk);
148
149         if (err < 0)
150                 return -EIO;
151
152         task = g_try_new0(struct dhclient_task, 1);
153         if (task == NULL)
154                 return -ENOMEM;
155
156         task->ifindex = iface->index;
157         task->ifname = strdup(ifr.ifr_name);
158         task->iface = iface;
159
160         if (task->ifname == NULL) {
161                 g_free(task);
162                 return -ENOMEM;
163         }
164
165         printf("[DHCP] request %s\n", task->ifname);
166
167         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
168         snprintf(pidfile, sizeof(pidfile) - 1,
169                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
170         snprintf(leases, sizeof(leases) - 1,
171                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
172         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
173         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
174
175         argv[0] = DHCLIENT;
176         argv[1] = "-d";
177         argv[2] = "-q";
178         argv[3] = "-n";
179         argv[4] = "-e";
180         argv[5] = address;
181         argv[6] = "-pf";
182         argv[7] = pidfile;
183         argv[8] = "-lf";
184         argv[9] = leases;
185         argv[10] = "-cf";
186         argv[11] = config;
187         argv[12] = "-sf";
188         argv[13] = script;
189         argv[14] = task->ifname;
190         argv[15] = NULL;
191
192         envp[0] = NULL;
193
194         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
195                                 task_setup, task, &task->pid, NULL) == FALSE) {
196                 printf("Failed to spawn dhclient\n");
197                 return -1;
198         }
199
200         tasks = g_slist_append(tasks, task);
201
202         g_child_watch_add(task->pid, task_died, task);
203
204         printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
205
206         return 0;
207 }
208
209 static int dhclient_release(struct connman_iface *iface)
210 {
211         struct dhclient_task *task;
212
213         task = find_task_by_index(iface->index);
214         if (task == NULL)
215                 return -ENODEV;
216
217         printf("[DHCP] release %s\n", task->ifname);
218
219         tasks = g_slist_remove(tasks, task);
220
221         kill_task(task);
222
223         return 0;
224 }
225
226 static struct connman_dhcp_driver dhclient_driver = {
227         .name           = "dhclient",
228         .request        = dhclient_request,
229         .release        = dhclient_release,
230 };
231
232 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
233                                                 DBusMessage *msg, void *data)
234 {
235         DBusMessageIter iter, dict;
236         dbus_uint32_t pid;
237         struct dhclient_task *task;
238         struct connman_ipv4 ipv4;
239         const char *text, *key, *value;
240
241         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
242                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
243
244         memset(&ipv4, 0, sizeof(ipv4));
245
246         dbus_message_iter_init(msg, &iter);
247
248         dbus_message_iter_get_basic(&iter, &pid);
249         dbus_message_iter_next(&iter);
250
251         dbus_message_iter_get_basic(&iter, &text);
252         dbus_message_iter_next(&iter);
253
254         printf("[DHCP] change %d to %s\n", pid, text);
255
256         task = find_task_by_pid(pid);
257         if (task == NULL)
258                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
259
260         dbus_message_iter_recurse(&iter, &dict);
261
262         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
263                 DBusMessageIter entry;
264
265                 dbus_message_iter_recurse(&dict, &entry);
266                 dbus_message_iter_get_basic(&entry, &key);
267                 dbus_message_iter_next(&entry);
268                 dbus_message_iter_get_basic(&entry, &value);
269
270                 printf("[DHCP] %s = %s\n", key, value);
271
272                 if (strcmp(key, "new_ip_address") == 0)
273                         inet_aton(value, &ipv4.address);
274
275                 if (strcmp(key, "new_subnet_mask") == 0)
276                         inet_aton(value, &ipv4.netmask);
277
278                 if (strcmp(key, "new_routers") == 0)
279                         inet_aton(value, &ipv4.gateway);
280
281                 if (strcmp(key, "new_network_number") == 0)
282                         inet_aton(value, &ipv4.network);
283
284                 if (strcmp(key, "new_broadcast_address") == 0)
285                         inet_aton(value, &ipv4.broadcast);
286
287                 if (strcmp(key, "new_domain_name_servers") == 0)
288                         inet_aton(value, &ipv4.nameserver);
289
290                 dbus_message_iter_next(&dict);
291         }
292
293         if (strcmp(text, "PREINIT") == 0)
294                 connman_dhcp_update(task->iface,
295                                         CONNMAN_DHCP_STATE_INIT, &ipv4);
296         else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
297                 connman_dhcp_update(task->iface,
298                                         CONNMAN_DHCP_STATE_BOUND, &ipv4);
299         else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
300                 connman_dhcp_update(task->iface,
301                                         CONNMAN_DHCP_STATE_RENEW, &ipv4);
302         else
303                 connman_dhcp_update(task->iface,
304                                         CONNMAN_DHCP_STATE_FAILED, NULL);
305
306         return DBUS_HANDLER_RESULT_HANDLED;
307 }
308
309 static DBusConnection *connection;
310
311 static int plugin_init(void)
312 {
313         gchar *filter;
314
315         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
316
317         busname = dbus_bus_get_unique_name(connection);
318         busname = CONNMAN_SERVICE;
319
320         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
321
322         filter = g_strdup_printf("interface=%s,path=%s",
323                                                 DHCLIENT_INTF, DHCLIENT_PATH);
324
325         dbus_bus_add_match(connection, filter, NULL);
326
327         g_free(filter);
328
329         connman_dhcp_register(&dhclient_driver);
330
331         return 0;
332 }
333
334 static void plugin_exit(void)
335 {
336         GSList *list;
337
338         for (list = tasks; list; list = list->next) {
339                 struct dhclient_task *task = list->data;
340
341                 printf("[DHCP] killing process %d\n", task->pid);
342
343                 kill_task(task);
344         }
345
346         g_slist_free(tasks);
347
348         connman_dhcp_unregister(&dhclient_driver);
349
350         dbus_connection_unref(connection);
351 }
352
353 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
354                                                 plugin_init, plugin_exit)