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