Merge "adns: remove dependency to lynx" into tizen
[scm/bb/meta-tizen.git] / meta-tizen-adaptation / meta / recipes-core / dbus / files / 0012-Enable-checking-of-smack-context-from-DBus-interface.patch
1 From: Brian McGillion <brian.mcgillion@intel.com>
2 Date: Mon, 6 Feb 2012 18:46:05 +0200
3 Subject: Enable checking of smack context from DBus interface
4
5 ---
6  bus/Makefile.am          |   4 ++
7  bus/driver.c             |   6 +++
8  bus/smack.c              | 132 +++++++++++++++++++++++++++++++++++++++++++++++
9  bus/smack.h              |  36 +++++++++++++
10  cmake/CMakeLists.txt     |   3 ++
11  cmake/bus/CMakeLists.txt |   4 +-
12  configure.ac             |  17 +++++-
13  7 files changed, 199 insertions(+), 3 deletions(-)
14  create mode 100644 bus/smack.c
15  create mode 100644 bus/smack.h
16
17 diff --git a/bus/Makefile.am b/bus/Makefile.am
18 index 6cbc09a..7f63d86 100644
19 --- a/bus/Makefile.am
20 +++ b/bus/Makefile.am
21 @@ -7,6 +7,7 @@ DBUS_BUS_LIBS = \
22         $(THREAD_LIBS) \
23         $(ADT_LIBS) \
24         $(NETWORK_libs) \
25 +       $(LIBSMACK_LIBS) \
26         $(NULL)
27  
28  DBUS_LAUNCHER_LIBS = \
29 @@ -21,6 +22,7 @@ AM_CPPFLAGS = \
30         -DDBUS_SYSTEM_CONFIG_FILE=\""$(configdir)/system.conf"\" \
31         -DDBUS_COMPILATION \
32         -DDBUS_STATIC_BUILD \
33 +       $(LIBSMACK_CFLAGS) \
34         $(NULL)
35  
36  # if assertions are enabled, improve backtraces
37 @@ -93,6 +95,8 @@ BUS_SOURCES=                                  \
38         services.h                              \
39         signals.c                               \
40         signals.h                               \
41 +       smack.c                                 \
42 +       smack.h                                 \
43         stats.c                                 \
44         stats.h                                 \
45         test.c                                  \
46 diff --git a/bus/driver.c b/bus/driver.c
47 index 574e0f3..c6298d7 100644
48 --- a/bus/driver.c
49 +++ b/bus/driver.c
50 @@ -30,6 +30,7 @@
51  #include "services.h"
52  #include "selinux.h"
53  #include "signals.h"
54 +#include "smack.h"
55  #include "stats.h"
56  #include "utils.h"
57  #include <dbus/dbus-string.h>
58 @@ -38,6 +39,7 @@
59  #include <dbus/dbus-marshal-recursive.h>
60  #include <string.h>
61  
62 +
63  static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
64                                                      DBusMessage    *hello_message,
65                                                      BusTransaction *transaction,
66 @@ -1736,6 +1738,10 @@ static const MessageHandler dbus_message_handlers[] = {
67      "",
68      DBUS_TYPE_STRING_AS_STRING,
69      bus_driver_handle_get_id },
70 +  { "GetConnectionSmackContext",
71 +    DBUS_TYPE_STRING_AS_STRING,
72 +    DBUS_TYPE_STRING_AS_STRING,
73 +    bus_smack_handle_get_connection_context },
74    { NULL, NULL, NULL, NULL }
75  };
76  
77 diff --git a/bus/smack.c b/bus/smack.c
78 new file mode 100644
79 index 0000000..b8542c2
80 --- /dev/null
81 +++ b/bus/smack.c
82 @@ -0,0 +1,132 @@
83 +/* smack.c - Provide interface to query smack context
84 + *
85 + * Author: Brian McGillion <brian.mcgillion@intel.com>
86 + * Copyright © 2011 Intel Corporation
87 + *
88 + * Licensed under the Academic Free License version 2.1
89 + *
90 + * This program is free software; you can redistribute it and/or modify
91 + * it under the terms of the GNU General Public License as published by
92 + * the Free Software Foundation; either version 2 of the License, or
93 + * (at your option) any later version.
94 + *
95 + * This program is distributed in the hope that it will be useful,
96 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
97 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
98 + * GNU General Public License for more details.
99 + *
100 + * You should have received a copy of the GNU General Public License
101 + * along with this program; if not, write to the Free Software
102 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
103 + * 02110-1301 USA
104 + */
105 +
106 +#include <config.h>
107 +#include "smack.h"
108 +
109 +#include <dbus/dbus-internals.h>
110 +
111 +#include "connection.h"
112 +#include "services.h"
113 +#include "utils.h"
114 +
115 +#ifdef DBUS_ENABLE_SMACK
116 +#include <sys/smack.h>
117 +#endif
118 +
119 +#ifdef DBUS_ENABLE_SMACK
120 +static char *
121 +bus_smack_get_label (DBusConnection *connection)
122 +{
123 +  char *label;
124 +  int sock_fd;
125 +
126 +  if (!dbus_connection_get_socket(connection, &sock_fd))
127 +    return NULL;
128 +
129 +  if (smack_new_label_from_socket(sock_fd, &label) < 0)
130 +    return NULL;
131 +  return label;
132 +}
133 +#endif
134 +
135 +dbus_bool_t
136 +bus_smack_handle_get_connection_context (DBusConnection *connection,
137 +                                         BusTransaction *transaction,
138 +                                         DBusMessage    *message,
139 +                                         DBusError      *error)
140 +{
141 +#ifdef DBUS_ENABLE_SMACK
142 +  const char *remote_end = NULL;
143 +  BusRegistry *registry;
144 +  DBusString remote_end_str;
145 +  BusService *service;
146 +  DBusConnection *remote_connection;
147 +  DBusMessage *reply = NULL;
148 +  char *label;
149 +
150 +  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
151 +
152 +  registry = bus_connection_get_registry (connection);
153 +
154 +  if (!dbus_message_get_args (message, error, DBUS_TYPE_STRING, &remote_end,
155 +                              DBUS_TYPE_INVALID))
156 +    return FALSE;
157 +
158 +  _dbus_verbose ("asked for label of connection %s\n", remote_end);
159 +
160 +  _dbus_string_init_const (&remote_end_str, remote_end);
161 +
162 +  service = bus_registry_lookup (registry, &remote_end_str);
163 +  if (service == NULL)
164 +    {
165 +      dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
166 +                      "Bus name '%s' has no owner", remote_end);
167 +      return FALSE;
168 +    }
169 +
170 +  remote_connection = bus_service_get_primary_owners_connection (service);
171 +  if (remote_connection == NULL)
172 +    goto oom;
173 +
174 +  reply = dbus_message_new_method_return (message);
175 +  if (reply == NULL)
176 +    goto oom;
177 +
178 +  label = bus_smack_get_label (remote_connection);
179 +  if (label == NULL)
180 +    {
181 +      dbus_set_error (error, DBUS_ERROR_FAILED,
182 +                      "Failed to get the socket fd of the connection",
183 +                      remote_end);
184 +      goto err;
185 +    }
186 +
187 +  if (!dbus_message_append_args (reply, DBUS_TYPE_STRING,
188 +                                 &label, DBUS_TYPE_INVALID))
189 +    goto oom;
190 +
191 +  if (!bus_transaction_send_from_driver (transaction, connection, reply))
192 +    goto oom;
193 +
194 +  dbus_message_unref (reply);
195 +  dbus_free(label);
196 +
197 +  return TRUE;
198 +
199 +oom:
200 +  BUS_SET_OOM (error);
201 +
202 +err:
203 +  if (reply != NULL)
204 +    dbus_message_unref (reply);
205 +
206 +  dbus_free(label);
207 +
208 +  return FALSE;
209 +#else
210 +  dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
211 +                  "SMACK support is not enabled");
212 +  return FALSE;
213 +#endif
214 +}
215 diff --git a/bus/smack.h b/bus/smack.h
216 new file mode 100644
217 index 0000000..04a4a2a
218 --- /dev/null
219 +++ b/bus/smack.h
220 @@ -0,0 +1,36 @@
221 +/* smack.h - Provide interface to query smack context
222 + *
223 + * Author: Brian McGillion <brian.mcgillion@intel.com>
224 + * Copyright © 2011 Intel Corporation
225 + *
226 + * Based on example from Stats interface
227 + *
228 + * Licensed under the Academic Free License version 2.1
229 + *
230 + * This program is free software; you can redistribute it and/or modify
231 + * it under the terms of the GNU General Public License as published by
232 + * the Free Software Foundation; either version 2 of the License, or
233 + * (at your option) any later version.
234 + *
235 + * This program is distributed in the hope that it will be useful,
236 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
237 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
238 + * GNU General Public License for more details.
239 + *
240 + * You should have received a copy of the GNU General Public License
241 + * along with this program; if not, write to the Free Software
242 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
243 + * 02110-1301 USA
244 + */
245 +
246 +#ifndef SMACK_H
247 +#define SMACK_H
248 +
249 +#include "bus.h"
250 +
251 +dbus_bool_t bus_smack_handle_get_connection_context (DBusConnection *connection,
252 +                                                     BusTransaction *transaction,
253 +                                                     DBusMessage    *message,
254 +                                                     DBusError      *error);
255 +
256 +#endif // SMACK_H
257 diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
258 index 000acda..68b7a9e 100644
259 --- a/cmake/CMakeLists.txt
260 +++ b/cmake/CMakeLists.txt
261 @@ -94,6 +94,8 @@ option (DBUS_ENABLE_STATS "enable bus daemon usage statistics" OFF)
262  
263  option (DBUS_ENABLE_STATS "enable bus daemon usage statistics" OFF)
264  
265 +option (DBUS_ENABLE_SMACK "enable smack checks in the daemon" OFF)
266 +
267  if (DBUS_USE_EXPAT)
268      find_package(LibExpat)
269  else ()
270 @@ -555,6 +557,7 @@ message("        Building bus stats API:   ${DBUS_ENABLE_STATS}                "
271  message("        installing system libs:   ${DBUS_INSTALL_SYSTEM_LIBS}         ")
272  #message("        Building SELinux support: ${have_selinux}                     ")
273  #message("        Building dnotify support: ${have_dnotify}                     ")
274 +message("        Building Smack support:   ${DBUS_ENABLE_SMACK}                ")
275  message("        Building Doxygen docs:    ${DBUS_ENABLE_DOXYGEN_DOCS}         ")
276  message("        Building XML docs:        ${DBUS_ENABLE_XML_DOCS}             ")
277  #message("        Gettext libs (empty OK):  ${INTLLIBS}                         ")
278 diff --git a/cmake/bus/CMakeLists.txt b/cmake/bus/CMakeLists.txt
279 index 2657605..13fb34c 100644
280 --- a/cmake/bus/CMakeLists.txt
281 +++ b/cmake/bus/CMakeLists.txt
282 @@ -72,7 +72,9 @@ set (BUS_SOURCES
283         ${BUS_DIR}/test.c                                       
284         ${BUS_DIR}/test.h                                       
285         ${BUS_DIR}/utils.c                                      
286 -       ${BUS_DIR}/utils.h                                      
287 +       ${BUS_DIR}/utils.h
288 +       ${BUS_DIR}/smack.c
289 +       ${BUS_DIR}/smack.h
290         ${XML_SOURCES}
291         ${DIR_WATCH_SOURCE}
292  )
293 diff --git a/configure.ac b/configure.ac
294 index a963d4d..95216c5 100644
295 --- a/configure.ac
296 +++ b/configure.ac
297 @@ -207,6 +207,9 @@ if test "x$enable_embedded_tests" = xyes; then
298        [Define to build test code into the library and binaries])
299  fi
300  
301 +# call early to ensure availability
302 +PKG_PROG_PKG_CONFIG
303 +
304  # DBUS_ENABLE_MODULAR_TESTS controls tests that work based on public API.
305  # These use GTest, from GLib, because life's too short. They're enabled by
306  # default (unless you don't have GLib), because they don't bloat the library
307 @@ -907,8 +910,6 @@ fi
308  # unix:path=/foo or unix:abstract=/foo
309  AC_SUBST(DBUS_PATH_OR_ABSTRACT)
310  
311 -PKG_PROG_PKG_CONFIG
312 -
313  #### Sort out XML library
314  
315  # see what we have
316 @@ -1703,6 +1704,17 @@ if test "x$enable_stats" = xyes; then
317      [Define to enable bus daemon usage statistics])
318  fi
319  
320 +#enable smack label support
321 +AC_ARG_ENABLE([smack], [AS_HELP_STRING([--enable-smack], [enable SMACK security checks])], [], [enable_smack=no])
322 +if test "x$enable_smack" = xyes; then
323 +  PKG_CHECK_MODULES([LIBSMACK], [libsmack >= 1.0],
324 +     [AC_DEFINE([DBUS_ENABLE_SMACK], [1], [Define to enable SMACK security features])],
325 +     [AC_MSG_ERROR([libsmack is required to enable smack support])])
326 +fi
327 +
328 +AC_SUBST([LIBSMACK_CFLAGS])
329 +AC_SUBST([LIBSMACK_LIBS])
330 +
331  AC_CONFIG_FILES([
332  Doxyfile
333  dbus/versioninfo.rc
334 @@ -1781,6 +1793,7 @@ echo "
335          Building checks:          ${enable_checks}
336          Building bus stats API:   ${enable_stats}
337          Building SELinux support: ${have_selinux}
338 +       Building SMACK support:   ${enable_smack}
339          Building inotify support: ${have_inotify}
340          Building dnotify support: ${have_dnotify}
341          Building kqueue support:  ${have_kqueue}