If we fail to open the privileged socket, open the unprivileged one.
[dhcpcd-ui] / src / dhcpcd-gtk / main.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009-2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <errno.h>
28 #include <locale.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef NOTIFY
33 #  include <libnotify/notify.h>
34 #ifndef NOTIFY_CHECK_VERSION
35 #  define NOTIFY_CHECK_VERSION(a,b,c) 0
36 #endif
37 static NotifyNotification *nn;
38 #endif
39
40 #include "config.h"
41 #include "dhcpcd.h"
42 #include "dhcpcd-gtk.h"
43
44 static GtkStatusIcon *status_icon;
45 static guint ani_timer;
46 static int ani_counter;
47 static bool online;
48 static bool carrier;
49
50 struct watch {
51         gpointer ref;
52         int fd;
53         guint eventid;
54         GIOChannel *gio;
55         struct watch *next;
56 };
57 static struct watch *watches;
58
59 WI_SCAN *wi_scans;
60
61 static gboolean dhcpcd_try_open(gpointer data);
62 static gboolean dhcpcd_wpa_try_open(gpointer data);
63
64 WI_SCAN *
65 wi_scan_find(DHCPCD_WI_SCAN *scan)
66 {
67         WI_SCAN *w;
68         DHCPCD_WI_SCAN *dw;
69
70         for (w = wi_scans; w; w = w->next) {
71                 for (dw = w->scans; dw; dw = dw->next)
72                         if (dw == scan)
73                                 break;
74                 if (dw)
75                         return w;
76         }
77         return NULL;
78 }
79
80 static gboolean
81 animate_carrier(_unused gpointer data)
82 {
83         const char *icon;
84
85         if (ani_timer == 0)
86                 return false;
87
88         switch(ani_counter++) {
89         case 0:
90                 icon = "network-transmit";
91                 break;
92         case 1:
93                 icon = "network-receive";
94                 break;
95         default:
96                 icon = "network-idle";
97                 ani_counter = 0;
98                 break;
99         }
100         gtk_status_icon_set_from_icon_name(status_icon, icon);
101         return true;
102 }
103
104 static gboolean
105 animate_online(_unused gpointer data)
106 {
107         const char *icon;
108
109         if (ani_timer == 0)
110                 return false;
111
112         if (ani_counter++ > 6) {
113                 ani_timer = 0;
114                 ani_counter = 0;
115                 return false;
116         }
117
118         if (ani_counter % 2 == 0)
119                 icon = "network-idle";
120         else
121                 icon = "network-transmit-receive";
122         gtk_status_icon_set_from_icon_name(status_icon, icon);
123         return true;
124 }
125
126 static void
127 update_online(DHCPCD_CONNECTION *con, bool showif)
128 {
129         bool ison, iscarrier;
130         char *msg, *msgs, *tmp;
131         DHCPCD_IF *ifs, *i;
132
133         ison = iscarrier = false;
134         msgs = NULL;
135         ifs = dhcpcd_interfaces(con);
136         for (i = ifs; i; i = i->next) {
137                 if (g_strcmp0(i->type, "link") == 0) {
138                         if (i->up)
139                                 iscarrier = true;
140                 } else {
141                         if (i->up)
142                                 ison = true;
143                 }
144                 msg = dhcpcd_if_message(i, NULL);
145                 if (msg) {
146                         if (showif)
147                                 g_message("%s", msg);
148                         if (msgs) {
149                                 tmp = g_strconcat(msgs, "\n", msg, NULL);
150                                 g_free(msgs);
151                                 g_free(msg);
152                                 msgs = tmp;
153                         } else
154                                 msgs = msg;
155                 } else if (showif)
156                         g_message("%s: %s", i->ifname, i->reason);
157         }
158
159         if (online != ison || carrier != iscarrier) {
160                 online = ison;
161                 carrier = iscarrier;
162                 if (ani_timer != 0) {
163                         g_source_remove(ani_timer);
164                         ani_timer = 0;
165                         ani_counter = 0;
166                 }
167                 if (ison) {
168                         animate_online(NULL);
169                         ani_timer = g_timeout_add(300, animate_online, NULL);
170                 } else if (iscarrier) {
171                         animate_carrier(NULL);
172                         ani_timer = g_timeout_add(500, animate_carrier, NULL);
173                 } else {
174                         gtk_status_icon_set_from_icon_name(status_icon,
175                             "network-offline");
176                 }
177         }
178         gtk_status_icon_set_tooltip_text(status_icon, msgs);
179         g_free(msgs);
180 }
181
182 void
183 notify_close(void)
184 {
185 #ifdef NOTIFY
186         if (nn != NULL)
187                 notify_notification_close(nn, NULL);
188 #endif
189 }
190
191 #ifdef NOTIFY
192 static char *notify_last_msg;
193
194 static void
195 notify_closed(void)
196 {
197         nn = NULL;
198 }
199
200 static void
201 notify(const char *title, const char *msg, const char *icon)
202 {
203
204         if (msg == NULL)
205                 return;
206         /* Don't spam the same message */
207         if (notify_last_msg) {
208                 if (notify_last_msg && strcmp(msg, notify_last_msg) == 0)
209                         return;
210                 g_free(notify_last_msg);
211         }
212         notify_last_msg = g_strdup(msg);
213
214         if (nn != NULL)
215                 notify_notification_close(nn, NULL);
216
217 #if NOTIFY_CHECK_VERSION(0,7,0)
218         nn = notify_notification_new(title, msg, icon);
219         notify_notification_set_hint(nn, "transient",
220             g_variant_new_boolean(TRUE));
221 #else
222         if (gtk_status_icon_get_visible(status_icon))
223                 nn = notify_notification_new_with_status_icon(title,
224                     msg, icon, status_icon);
225         else
226                 nn = notify_notification_new(title, msg, icon, NULL);
227 #endif
228
229         notify_notification_set_timeout(nn, 5000);
230         g_signal_connect(nn, "closed", G_CALLBACK(notify_closed), NULL);
231         notify_notification_show(nn, NULL);
232 }
233 #else
234 #  define notify(a, b, c)
235 #endif
236
237 static struct watch *
238 dhcpcd_findwatch(int fd, gpointer data, struct watch **last)
239 {
240         struct watch *w;
241
242         if (last)
243                 *last = NULL;
244         for (w = watches; w; w = w->next) {
245                 if (w->fd == fd || w->ref == data)
246                         return w;
247                 if (last)
248                         *last = w;
249         }
250         return NULL;
251 }
252
253 static void
254 dhcpcd_unwatch(int fd, gpointer data)
255 {
256         struct watch *w, *l;
257
258         if ((w = dhcpcd_findwatch(fd, data, &l))) {
259                 if (l)
260                         l->next = w->next;
261                 else
262                         watches = w->next;
263                 g_source_remove(w->eventid);
264                 g_io_channel_unref(w->gio);
265                 g_free(w);
266         }
267 }
268
269 static gboolean
270 dhcpcd_watch(int fd,
271     gboolean (*cb)(GIOChannel *, GIOCondition, gpointer),
272     gpointer data)
273 {
274         struct watch *w, *l;
275         GIOChannel *gio;
276         GIOCondition flags;
277         guint eventid;
278
279         /* Sanity */
280         if ((w = dhcpcd_findwatch(fd, data, &l))) {
281                 if (w->fd == fd)
282                         return TRUE;
283                 if (l)
284                         l->next = w->next;
285                 else
286                         watches = w->next;
287                 g_source_remove(w->eventid);
288                 g_io_channel_unref(w->gio);
289                 g_free(w);
290         }
291
292         gio = g_io_channel_unix_new(fd);
293         if (gio == NULL) {
294                 g_warning(_("Error creating new GIO Channel\n"));
295                 return FALSE;
296         }
297         flags = G_IO_IN | G_IO_ERR | G_IO_HUP;
298         if ((eventid = g_io_add_watch(gio, flags, cb, data)) == 0) {
299                 g_warning(_("Error creating watch\n"));
300                 g_io_channel_unref(gio);
301                 return FALSE;
302         }
303
304         w = g_try_malloc(sizeof(*w));
305         if (w == NULL) {
306                 g_warning(_("g_try_malloc\n"));
307                 g_source_remove(eventid);
308                 g_io_channel_unref(gio);
309                 return FALSE;
310         }
311
312         w->ref = data;
313         w->fd = fd;
314         w->eventid = eventid;
315         w->gio = gio;
316         w->next = watches;
317         watches = w;
318
319         return TRUE;
320 }
321
322 static void
323 dhcpcd_status_cb(DHCPCD_CONNECTION *con, const char *status,
324     _unused void *data)
325 {
326         static char *last = NULL;
327         const char *msg;
328         bool refresh;
329         WI_SCAN *w;
330
331         g_message("Status changed to %s", status);
332         if (g_strcmp0(status, "down") == 0) {
333                 msg = N_(last ?
334                     "Connection to dhcpcd lost" : "dhcpcd not running");
335                 if (ani_timer != 0) {
336                         g_source_remove(ani_timer);
337                         ani_timer = 0;
338                         ani_counter = 0;
339                 }
340                 online = carrier = false;
341                 gtk_status_icon_set_from_icon_name(status_icon,
342                     "network-offline");
343                 gtk_status_icon_set_tooltip_text(status_icon, msg);
344                 dhcpcd_prefs_abort();
345                 while (wi_scans) {
346                         w = wi_scans->next;
347                         dhcpcd_wi_scans_free(wi_scans->scans);
348                         g_free(wi_scans);
349                         wi_scans = w;
350                 }
351                 dhcpcd_unwatch(-1, con);
352                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
353         } else {
354                 if ((last == NULL || g_strcmp0(last, "down") == 0)) {
355                         g_message(_("Connected to %s-%s"), "dhcpcd",
356                             dhcpcd_version(con));
357                         refresh = true;
358                 } else
359                         refresh = g_strcmp0(last, "opened") ? false : true;
360                 update_online(con, refresh);
361         }
362
363         g_free(last);
364         last = g_strdup(status);
365 }
366
367 static gboolean
368 dhcpcd_cb(_unused GIOChannel *gio, _unused GIOCondition c, gpointer data)
369 {
370         DHCPCD_CONNECTION *con;
371
372         con = (DHCPCD_CONNECTION *)data;
373         if (dhcpcd_get_fd(con) == -1) {
374                 g_warning(_("dhcpcd connection lost"));
375                 dhcpcd_unwatch(-1, con);
376                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
377                 return FALSE;
378         }
379
380         dhcpcd_dispatch(con);
381         return TRUE;
382 }
383
384 static gboolean
385 dhcpcd_try_open(gpointer data)
386 {
387         DHCPCD_CONNECTION *con;
388         int fd;
389         static int last_error;
390
391         con = (DHCPCD_CONNECTION *)data;
392         fd = dhcpcd_open(con, true);
393         if (fd == -1) {
394                 if (errno == EACCES || errno == EPERM) {
395                         if ((fd = dhcpcd_open(con, false)) != -1)
396                                 goto unprived;
397                 }
398                 if (errno != last_error) {
399                         g_critical("dhcpcd_open: %s", strerror(errno));
400                         last_error = errno;
401                 }
402                 return TRUE;
403         }
404
405 unprived:
406         if (!dhcpcd_watch(fd, dhcpcd_cb, con)) {
407                 dhcpcd_close(con);
408                 return TRUE;
409         }
410
411         /* Start listening to WPA events */
412         dhcpcd_wpa_start(con);
413
414         return FALSE;
415 }
416
417 static void
418 dhcpcd_if_cb(DHCPCD_IF *i, _unused void *data)
419 {
420         DHCPCD_CONNECTION *con;
421         char *msg;
422         const char *icon;
423         bool new_msg;
424
425         /* We should ignore renew and stop so we don't annoy the user */
426         if (g_strcmp0(i->reason, "RENEW") &&
427             g_strcmp0(i->reason, "STOP") &&
428             g_strcmp0(i->reason, "STOPPED"))
429         {
430                 msg = dhcpcd_if_message(i, &new_msg);
431                 if (msg) {
432                         g_message("%s", msg);
433                         if (new_msg) {
434                                 if (i->up)
435                                         icon = "network-transmit-receive";
436                                 //else
437                                 //      icon = "network-transmit";
438                                 if (!i->up)
439                                         icon = "network-offline";
440                                 notify(_("Network event"), msg, icon);
441                         }
442                         g_free(msg);
443                 }
444         }
445
446         /* Update the tooltip with connection information */
447         con = dhcpcd_if_connection(i);
448         update_online(con, false);
449 }
450
451 static gboolean
452 dhcpcd_wpa_cb(_unused GIOChannel *gio, _unused GIOCondition c,
453     gpointer data)
454 {
455         DHCPCD_WPA *wpa;
456         DHCPCD_IF *i;
457
458         wpa = (DHCPCD_WPA *)data;
459         if (dhcpcd_wpa_get_fd(wpa) == -1) {
460                 dhcpcd_unwatch(-1, wpa);
461
462                 /* If the interface hasn't left, try re-opening */
463                 i = dhcpcd_wpa_if(wpa);
464                 if (i == NULL ||
465                     g_strcmp0(i->reason, "DEPARTED") == 0 ||
466                     g_strcmp0(i->reason, "STOPPED") == 0)
467                         return TRUE;
468                 g_warning(_("dhcpcd WPA connection lost: %s"), i->ifname);
469                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_wpa_try_open, wpa);
470                 return FALSE;
471         }
472
473         dhcpcd_wpa_dispatch(wpa);
474         return TRUE;
475 }
476
477 static gboolean
478 dhcpcd_wpa_try_open(gpointer data)
479 {
480         DHCPCD_WPA *wpa;
481         int fd;
482         static int last_error;
483
484         wpa = (DHCPCD_WPA *)data;
485         fd = dhcpcd_wpa_open(wpa);
486         if (fd == -1) {
487                 if (errno != last_error)
488                         g_critical("dhcpcd_wpa_open: %s", strerror(errno));
489                 last_error = errno;
490                 return TRUE;
491         }
492
493         if (!dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa)) {
494                 dhcpcd_wpa_close(wpa);
495                 return TRUE;
496         }
497
498         return FALSE;
499 }
500
501 static void
502 dhcpcd_wpa_scan_cb(DHCPCD_WPA *wpa, _unused void *data)
503 {
504         DHCPCD_IF *i;
505         WI_SCAN *w;
506         DHCPCD_WI_SCAN *scans, *s1, *s2;
507         char *txt, *t;
508         int lerrno, fd;
509         const char *msg;
510
511         /* This could be a new WPA so watch it */
512         fd = dhcpcd_wpa_get_fd(wpa);
513         if (fd == -1) {
514                 g_critical("No fd for WPA %p", wpa);
515                 dhcpcd_unwatch(-1, wpa);
516                 return;
517         }
518         dhcpcd_watch(fd, dhcpcd_wpa_cb, wpa);
519
520         i = dhcpcd_wpa_if(wpa);
521         if (i == NULL) {
522                 g_critical("No interface for WPA %p", wpa);
523                 return;
524         }
525         g_message(_("%s: Received scan results"), i->ifname);
526         lerrno = errno;
527         errno = 0;
528         scans = dhcpcd_wi_scans(i);
529         if (scans == NULL && errno)
530                 g_warning("%s: %s", i->ifname, strerror(errno));
531         errno = lerrno;
532         for (w = wi_scans; w; w = w->next)
533                 if (w->interface == i)
534                         break;
535         if (w == NULL) {
536                 w = g_malloc(sizeof(*w));
537                 w->interface = i;
538                 w->next = wi_scans;
539                 wi_scans = w;
540         } else {
541                 txt = NULL;
542                 msg = N_("New Access Point");
543                 for (s1 = scans; s1; s1 = s1->next) {
544                         for (s2 = w->scans; s2; s2 = s2->next)
545                                 if (g_strcmp0(s1->ssid, s2->ssid) == 0)
546                                         break;
547                         if (s2 == NULL) {
548                                 if (txt == NULL)
549                                         txt = g_strdup(s1->ssid);
550                                 else {
551                                         msg = N_("New Access Points");
552                                         t = g_strconcat(txt, "\n",
553                                             s1->ssid, NULL);
554                                         g_free(txt);
555                                         txt = t;
556                                 }
557                         }
558                 }
559                 if (txt) {
560                         notify(msg, txt, "network-wireless");
561                         g_free(txt);
562                 }
563                 menu_update_scans(w->interface, scans);
564                 dhcpcd_wi_scans_free(w->scans);
565         }
566         w->scans = scans;
567 }
568
569 int
570 main(int argc, char *argv[])
571 {
572         DHCPCD_CONNECTION *con;
573
574         setlocale(LC_ALL, "");
575         bindtextdomain(PACKAGE, NULL);
576         bind_textdomain_codeset(PACKAGE, "UTF-8");
577         textdomain(PACKAGE);
578
579         gtk_init(&argc, &argv);
580         g_set_application_name("Network Configurator");
581         gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(),
582             ICONDIR);
583         status_icon = gtk_status_icon_new_from_icon_name("network-offline");
584
585         gtk_status_icon_set_tooltip_text(status_icon,
586             _("Connecting to dhcpcd ..."));
587         gtk_status_icon_set_visible(status_icon, true);
588         online = false;
589 #ifdef NOTIFY
590         notify_init(PACKAGE);
591 #endif
592
593         g_message(_("Connecting ..."));
594         con = dhcpcd_new();
595         if (con ==  NULL) {
596                 g_critical("libdhcpcd: %s", strerror(errno));
597                 exit(EXIT_FAILURE);
598         }
599         dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
600         dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
601         dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
602         //dhcpcd_wpa_set_status_callback(con, dhcpcd_wpa_status_cb, NULL);
603         if (dhcpcd_try_open(con))
604                 g_timeout_add(DHCPCD_RETRYOPEN, dhcpcd_try_open, con);
605
606         menu_init(status_icon, con);
607
608         gtk_main();
609         dhcpcd_close(con);
610         dhcpcd_free(con);
611         return 0;
612 }