Check that hostname and domainname is valid
[connman] / plugins / loopback.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <errno.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/inotify.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <net/if.h>
35
36 #include <glib.h>
37
38 #define CONNMAN_API_SUBJECT_TO_CHANGE
39 #include <connman/plugin.h>
40 #include <connman/log.h>
41
42 static GIOChannel *inotify_channel = NULL;
43
44 static int hostname_descriptor = -1;
45
46 static gboolean inotify_event(GIOChannel *channel,
47                                         GIOCondition condition, gpointer data)
48 {
49         unsigned char buf[129], *ptr = buf;
50         gsize len;
51         GIOError err;
52
53         if (condition & (G_IO_HUP | G_IO_ERR))
54                 return FALSE;
55
56         memset(buf, 0, sizeof(buf));
57
58         err = g_io_channel_read(channel, (gchar *) buf, sizeof(buf) - 1, &len);
59         if (err != G_IO_ERROR_NONE) {
60                 if (err == G_IO_ERROR_AGAIN)
61                         return TRUE;
62                 connman_error("Reading from inotify channel failed");
63                 return FALSE;
64         }
65
66         while (len >= sizeof(struct inotify_event)) {
67                 struct inotify_event *evt = (struct inotify_event *) ptr;
68
69                 if (evt->wd == hostname_descriptor) {
70                         if (evt->mask & (IN_CREATE | IN_MOVED_TO))
71                                 connman_info("create hostname file");
72
73                         if (evt->mask & (IN_DELETE | IN_MOVED_FROM))
74                                 connman_info("delete hostname file");
75
76                         if (evt->mask & (IN_MODIFY | IN_MOVE_SELF))
77                                 connman_info("modify hostname file");
78                 }
79
80                 len -= sizeof(struct inotify_event) + evt->len;
81                 ptr += sizeof(struct inotify_event) + evt->len;
82         }
83
84         return TRUE;
85 }
86
87 static int create_watch(void)
88 {
89         int fd;
90
91         fd = inotify_init();
92         if (fd < 0) {
93                 connman_error("Creation of inotify context failed");
94                 return -EIO;
95         }
96
97         inotify_channel = g_io_channel_unix_new(fd);
98         if (inotify_channel == NULL) {
99                 connman_error("Creation of inotify channel failed");
100                 close(fd);
101                 return -EIO;
102         }
103
104         g_io_add_watch(inotify_channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
105                                                         inotify_event, NULL);
106
107         hostname_descriptor = inotify_add_watch(fd, "/etc/hostname",
108                                 IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
109         if (hostname_descriptor < 0) {
110                 connman_error("Creation of hostname watch failed");
111                 g_io_channel_unref(inotify_channel);
112                 inotify_channel = NULL;
113                 close(fd);
114                 return -EIO;
115         }
116
117         return 0;
118 }
119
120 static void remove_watch(void)
121 {
122         int fd;
123
124         if (inotify_channel == NULL)
125                 return;
126
127         fd = g_io_channel_unix_get_fd(inotify_channel);
128
129         if (hostname_descriptor >= 0)
130                 inotify_rm_watch(fd, hostname_descriptor);
131
132         g_io_channel_unref(inotify_channel);
133
134         close(fd);
135 }
136
137 static int setup_hostname(void)
138 {
139         char name[HOST_NAME_MAX + 1];
140
141         memset(name, 0, sizeof(name));
142
143         if (gethostname(name, HOST_NAME_MAX) < 0) {
144                 connman_error("Failed to get current hostname");
145                 return -EIO;
146         }
147
148         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
149                 connman_info("System hostname is %s", name);
150
151         memset(name, 0, sizeof(name));
152
153         if (getdomainname(name, HOST_NAME_MAX) < 0) {
154                 connman_error("Failed to get current domainname");
155                 return -EIO;
156         }
157
158         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
159                 connman_info("System domainname is %s", name);
160
161         return 0;
162 }
163
164 static int setup_loopback(void)
165 {
166         struct ifreq ifr;
167         struct sockaddr_in *addr;
168         int sk, err;
169
170         sk = socket(PF_INET, SOCK_DGRAM, 0);
171         if (sk < 0)
172                 return -1;
173
174         memset(&ifr, 0, sizeof(ifr));
175         strcpy(ifr.ifr_name, "lo");
176
177         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
178                 err = -errno;
179                 goto done;
180         }
181
182         if (ifr.ifr_flags & IFF_UP) {
183                 err = -EALREADY;
184                 connman_info("The loopback interface is already up");
185                 goto done;
186         }
187
188         addr = (struct sockaddr_in *) &ifr.ifr_addr;
189         addr->sin_family = AF_INET;
190         addr->sin_addr.s_addr = inet_addr("127.0.0.1");
191
192         err = ioctl(sk, SIOCSIFADDR, &ifr);
193         if (err < 0) {
194                 err = -errno;
195                 connman_error("Setting address failed (%s)", strerror(-err));
196                 goto done;
197         }
198
199         addr = (struct sockaddr_in *) &ifr.ifr_netmask;
200         addr->sin_family = AF_INET;
201         addr->sin_addr.s_addr = inet_addr("255.0.0.0");
202
203         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
204         if (err < 0) {
205                 err = -errno;
206                 connman_error("Setting netmask failed (%s)", strerror(-err));
207                 goto done;
208         }
209
210         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
211                 err = -errno;
212                 goto done;
213         }
214
215         ifr.ifr_flags |= IFF_UP;
216
217         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
218                 err = -errno;
219                 connman_error("Activating loopback interface failed (%s)",
220                                                         strerror(-err));
221                 goto done;
222         }
223
224 done:
225         close(sk);
226
227         return err;
228 }
229
230 static int loopback_init(void)
231 {
232         setup_loopback();
233
234         setup_hostname();
235
236         create_watch();
237
238         return 0;
239 }
240
241 static void loopback_exit(void)
242 {
243         remove_watch();
244 }
245
246 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
247                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)