[Fix] Dereference after free in sta_remove_callback()
[platform/upstream/connman.git] / plugins / loopback.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <stdio.h>
36
37 #include <glib.h>
38 #include <glib/gprintf.h>
39
40 #define CONNMAN_API_SUBJECT_TO_CHANGE
41 #include <connman/plugin.h>
42 #include <connman/utsname.h>
43 #include <connman/log.h>
44 #include <connman/inet.h>
45
46 static in_addr_t loopback_address;
47 static in_addr_t loopback_netmask;
48
49 static char system_hostname[HOST_NAME_MAX + 1];
50
51 static void create_hostname(void)
52 {
53         const char *name = "localhost";
54
55         if (sethostname(name, strlen(name)) < 0)
56                 connman_error("Failed to set hostname to %s", name);
57
58         strncpy(system_hostname, name, HOST_NAME_MAX);
59 }
60
61 static int setup_hostname(void)
62 {
63         char name[HOST_NAME_MAX + 1];
64
65         memset(system_hostname, 0, sizeof(system_hostname));
66
67 #if defined TIZEN_EXT
68         FILE *fp = NULL;
69 #define WIFI_MAC "/opt/etc/.mac.info"
70         {
71                 char* rv = 0;
72                 gchar* dev_id = "TIZEN";
73                 char wifi_mac[HOST_NAME_MAX + 1];
74
75                 fp = fopen(WIFI_MAC, "r");
76                 if(!fp){
77                         connman_error("Failed to get current hostname");
78                         strncpy(system_hostname, dev_id, strlen(dev_id));
79                         goto host_name_end;
80                 }
81
82                 rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
83                 if(!rv){
84                         connman_error("Failed to get current hostname");
85                         strncpy(system_hostname, dev_id, strlen(dev_id));
86                         fclose(fp);
87                         goto host_name_end;
88                 }
89
90                 dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
91                 g_sprintf(system_hostname, "TIZEN-%s", dev_id);
92                 g_free(dev_id);
93                 fclose(fp);
94         }
95
96 host_name_end:
97 #else
98         if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
99                 connman_error("Failed to get current hostname");
100                 return -EIO;
101         }
102 #endif
103
104         if (strlen(system_hostname) > 0 &&
105                                 strcmp(system_hostname, "(none)") != 0)
106                 connman_info("System hostname is %s", system_hostname);
107         else
108                 create_hostname();
109
110         memset(name, 0, sizeof(name));
111
112         if (getdomainname(name, HOST_NAME_MAX) < 0) {
113                 connman_error("Failed to get current domainname");
114                 return -EIO;
115         }
116
117         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
118                 connman_info("System domainname is %s", name);
119
120         return 0;
121 }
122
123 static bool valid_loopback(int sk, struct ifreq *ifr)
124 {
125         struct sockaddr_in *addr;
126         int err;
127         char buf[INET_ADDRSTRLEN];
128
129         /* It is possible to end up in situations in which the
130          * loopback interface is up but has no valid address. In that
131          * case, we expect EADDRNOTAVAIL and should return FALSE.
132          */
133
134         err = ioctl(sk, SIOCGIFADDR, ifr);
135         if (err < 0) {
136                 err = -errno;
137                 connman_error("Getting address failed (%s)", strerror(-err));
138                 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
139         }
140
141         addr = (struct sockaddr_in *) &ifr->ifr_addr;
142         if (addr->sin_addr.s_addr != loopback_address) {
143                 connman_warn("Invalid loopback address %s",
144                         inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
145                 return false;
146         }
147
148         err = ioctl(sk, SIOCGIFNETMASK, ifr);
149         if (err < 0) {
150                 err = -errno;
151                 connman_error("Getting netmask failed (%s)", strerror(-err));
152                 return true;
153         }
154
155         addr = (struct sockaddr_in *) &ifr->ifr_netmask;
156         if (addr->sin_addr.s_addr != loopback_netmask) {
157                 connman_warn("Invalid loopback netmask %s",
158                         inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
159                 return false;
160         }
161
162         return true;
163 }
164
165 static int setup_loopback(void)
166 {
167         struct ifreq ifr;
168         struct sockaddr_in addr;
169         int sk, err;
170
171         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
172         if (sk < 0)
173                 return -errno;
174
175         memset(&ifr, 0, sizeof(ifr));
176         strcpy(ifr.ifr_name, "lo");
177
178         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
179                 err = -errno;
180                 goto done;
181         }
182
183         if (ifr.ifr_flags & IFF_UP) {
184                 connman_info("Checking loopback interface settings");
185                 if (valid_loopback(sk, &ifr)) {
186                         err = -EALREADY;
187                         goto done;
188                 }
189
190                 connman_warn("Correcting wrong loopback settings");
191         }
192
193         memset(&addr, 0, sizeof(addr));
194         addr.sin_family = AF_INET;
195         addr.sin_addr.s_addr = loopback_address;
196         memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
197
198         err = ioctl(sk, SIOCSIFADDR, &ifr);
199         if (err < 0) {
200                 err = -errno;
201                 connman_error("Setting address failed (%s)", strerror(-err));
202                 goto done;
203         }
204
205         memset(&addr, 0, sizeof(addr));
206         addr.sin_family = AF_INET;
207         addr.sin_addr.s_addr = loopback_netmask;
208         memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
209
210         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
211         if (err < 0) {
212                 err = -errno;
213                 connman_error("Setting netmask failed (%s)", strerror(-err));
214                 goto done;
215         }
216
217         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
218                 err = -errno;
219                 goto done;
220         }
221
222         ifr.ifr_flags |= IFF_UP;
223
224         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
225                 err = -errno;
226                 connman_error("Activating loopback interface failed (%s)",
227                                                         strerror(-err));
228                 goto done;
229         }
230
231 done:
232         close(sk);
233
234         return err;
235 }
236
237 static const char *loopback_get_hostname(void)
238 {
239         return system_hostname;
240 }
241
242 static int loopback_set_hostname(const char *hostname)
243 {
244         const char *ptr;
245         int err, len;
246
247         if (g_strcmp0(hostname, "<hostname>") == 0)
248                 return 0;
249
250         len = strlen(hostname);
251
252         if (!connman_inet_check_hostname(hostname, len))
253                 return -EINVAL;
254
255         if ((ptr = strstr(hostname, ".")))
256                 len = ptr - hostname;
257
258         if (sethostname(hostname, len) < 0) {
259                 err = -errno;
260                 connman_error("Failed to set hostname to %s", hostname);
261                 return err;
262         }
263
264         connman_info("Setting hostname to %s", hostname);
265
266         return 0;
267 }
268
269 static int loopback_set_domainname(const char *domainname)
270 {
271         int err, len;
272
273         len = strlen(domainname);
274
275         if (!connman_inet_check_hostname(domainname, len))
276                 return -EINVAL;
277
278         if (setdomainname(domainname, len) < 0) {
279                 err = -errno;
280                 connman_error("Failed to set domainname to %s", domainname);
281                 return err;
282         }
283
284         connman_info("Setting domainname to %s", domainname);
285
286         return 0;
287 }
288
289 static struct connman_utsname_driver loopback_driver = {
290         .name           = "loopback",
291         .get_hostname   = loopback_get_hostname,
292         .set_hostname   = loopback_set_hostname,
293         .set_domainname = loopback_set_domainname,
294 };
295
296 static int loopback_init(void)
297 {
298         loopback_address = inet_addr("127.0.0.1");
299         loopback_netmask = inet_addr("255.0.0.0");
300
301         setup_loopback();
302
303         setup_hostname();
304
305         connman_utsname_driver_register(&loopback_driver);
306
307         return 0;
308 }
309
310 static void loopback_exit(void)
311 {
312         connman_utsname_driver_unregister(&loopback_driver);
313 }
314
315 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
316                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)