Make HAL requirement optional and check for PolicyKit
[connman] / plugins / resolvfile.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 <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/stat.h>
31
32 #include <connman/plugin.h>
33 #include <connman/driver.h>
34 #include <connman/log.h>
35
36 static int resolvfile_probe(struct connman_element *element)
37 {
38         const char *nameserver = NULL;
39         struct connman_element *internet;
40         gchar *cmd;
41         int fd, len, err;
42
43         DBG("element %p name %s", element, element->name);
44
45         connman_element_get_value(element,
46                         CONNMAN_PROPERTY_TYPE_IPV4_NAMESERVER, &nameserver);
47
48         if (nameserver == NULL)
49                 return -EINVAL;
50
51         fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
52                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
53         if (fd < 0)
54                 return errno;
55
56         err = ftruncate(fd, 0);
57
58         cmd = g_strdup_printf("nameserver %s\n", nameserver);
59
60         len = write(fd, cmd, strlen(cmd));
61
62         g_free(cmd);
63
64         close(fd);
65
66         internet = connman_element_create();
67
68         internet->type = CONNMAN_ELEMENT_TYPE_INTERNET;
69
70         connman_element_register(internet, element);
71
72         return 0;
73 }
74
75 static void resolvfile_remove(struct connman_element *element)
76 {
77         DBG("element %p name %s", element, element->name);
78 }
79
80 static struct connman_driver resolvfile_driver = {
81         .name           = "resolvconf",
82         .type           = CONNMAN_ELEMENT_TYPE_RESOLVER,
83         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
84         .probe          = resolvfile_probe,
85         .remove         = resolvfile_remove,
86 };
87
88 static int resolvfile_init(void)
89 {
90         return connman_driver_register(&resolvfile_driver);
91 }
92
93 static void resolvfile_exit(void)
94 {
95         connman_driver_unregister(&resolvfile_driver);
96 }
97
98 CONNMAN_PLUGIN_DEFINE("resolvfile", "Name resolver plugin", VERSION,
99                                         resolvfile_init, resolvfile_exit)