Bump dnsmasq version to v2.78 to fix several CVEs Fixes CVE-2017-14491, CVE-2017...
[librecmc/librecmc.git] / package / network / services / dnsmasq / patches / 240-ubus.patch
1 --- a/src/dnsmasq.c
2 +++ b/src/dnsmasq.c
3 @@ -19,6 +19,8 @@
4  
5  #include "dnsmasq.h"
6  
7 +#include <libubus.h>
8 +
9  struct daemon *daemon;
10  
11  static volatile pid_t pid = 0;
12 @@ -32,6 +34,62 @@ static void fatal_event(struct event_des
13  static int read_event(int fd, struct event_desc *evp, char **msg);
14  static void poll_resolv(int force, int do_reload, time_t now);
15  
16 +static struct ubus_context *ubus;
17 +static struct blob_buf b;
18 +
19 +static struct ubus_object_type ubus_object_type = {
20 +       .name = "dnsmasq",
21 +};
22 +
23 +static struct ubus_object ubus_object = {
24 +       .name = "dnsmasq",
25 +       .type = &ubus_object_type,
26 +};
27 +
28 +void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name)
29 +{
30 +       if (!ubus || !ubus_object.has_subscribers)
31 +               return;
32 +
33 +       blob_buf_init(&b, 0);
34 +       if (mac)
35 +               blobmsg_add_string(&b, "mac", mac);
36 +       if (ip)
37 +               blobmsg_add_string(&b, "ip", ip);
38 +       if (name)
39 +               blobmsg_add_string(&b, "name", name);
40 +       ubus_notify(ubus, &ubus_object, type, b.head, -1);
41 +}
42 +
43 +static void set_ubus_listeners(void)
44 +{
45 +       if (!ubus)
46 +               return;
47 +
48 +       poll_listen(ubus->sock.fd, POLLIN);
49 +       poll_listen(ubus->sock.fd, POLLERR);
50 +       poll_listen(ubus->sock.fd, POLLHUP);
51 +}
52 +
53 +static void check_ubus_listeners()
54 +{
55 +       if (!ubus) {
56 +               ubus = ubus_connect(NULL);
57 +               if (ubus)
58 +                       ubus_add_object(ubus, &ubus_object);
59 +               else
60 +                       return;
61 +       }
62 +
63 +       if (poll_check(ubus->sock.fd, POLLIN))
64 +               ubus_handle_event(ubus);
65 +
66 +       if (poll_check(ubus->sock.fd, POLLHUP)) {
67 +               ubus_free(ubus);
68 +               ubus = NULL;
69 +       }
70 +}
71 +
72  int main (int argc, char **argv)
73  {
74    int bind_fallback = 0;
75 @@ -911,6 +969,7 @@ int main (int argc, char **argv)
76        set_dbus_listeners();
77  #endif 
78    
79 +      set_ubus_listeners();
80  #ifdef HAVE_DHCP
81        if (daemon->dhcp || daemon->relay4)
82         {
83 @@ -1041,6 +1100,8 @@ int main (int argc, char **argv)
84        check_dbus_listeners();
85  #endif
86        
87 +      check_ubus_listeners();
88 +
89        check_dns_listeners(now);
90  
91  #ifdef HAVE_TFTP
92 --- a/Makefile
93 +++ b/Makefile
94 @@ -85,7 +85,7 @@ all : $(BUILDDIR)
95         @cd $(BUILDDIR) && $(MAKE) \
96   top="$(top)" \
97   build_cflags="$(version) $(dbus_cflags) $(idn2_cflags) $(idn_cflags) $(ct_cflags) $(lua_cflags) $(nettle_cflags)" \
98 - build_libs="$(dbus_libs) $(idn2_libs) $(idn_libs) $(ct_libs) $(lua_libs) $(sunos_libs) $(nettle_libs) $(gmp_libs)" \
99 + build_libs="$(dbus_libs) $(idn2_libs) $(idn_libs) $(ct_libs) $(lua_libs) $(sunos_libs) $(nettle_libs) $(gmp_libs) -lubox -lubus" \
100   -f $(top)/Makefile dnsmasq 
101  
102  mostly_clean :
103 --- a/src/dnsmasq.h
104 +++ b/src/dnsmasq.h
105 @@ -1397,6 +1397,8 @@ void emit_dbus_signal(int action, struct
106  #  endif
107  #endif
108  
109 +void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name);
110 +
111  /* ipset.c */
112  #ifdef HAVE_IPSET
113  void ipset_init(void);
114 --- a/src/rfc2131.c
115 +++ b/src/rfc2131.c
116 @@ -1621,6 +1621,10 @@ static void log_packet(char *type, void
117               daemon->namebuff,
118               string ? string : "",
119               err ? err : "");
120 +  if (!strcmp(type, "DHCPACK"))
121 +         ubus_event_bcast("dhcp.ack", addr ? inet_ntoa(a) : NULL, daemon->namebuff, string ? string : NULL);
122 +  else if (!strcmp(type, "DHCPRELEASE"))
123 +         ubus_event_bcast("dhcp.release", addr ? inet_ntoa(a) : NULL, daemon->namebuff, string ? string : NULL);
124  }
125  
126  static void log_options(unsigned char *start, u32 xid)