D-Bus  1.13.7
dbus-credentials-util.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-credentials-util.c Would be in dbus-credentials.c, but only used for tests/bus
3  *
4  * Copyright (C) 2007 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-test.h"
27 #include "dbus-credentials.h"
28 #include <dbus/dbus-test-tap.h>
29 
37 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
38 #include <stdio.h>
39 #include <string.h>
40 
41 static DBusCredentials*
42 make_credentials(dbus_uid_t unix_uid,
43  dbus_pid_t pid,
44  int group_vector,
45  const char *windows_sid)
46 {
47  DBusCredentials *credentials;
48  static const struct
49  {
50  size_t n;
51  const dbus_gid_t gids[4];
52  }
53  group_vectors[] =
54  {
55  { 4, { 1000, 42, 123, 5678 } },
56  { 2, { 23, 1001 } },
57  { 4, { 5678, 123, 42, 1000 } }
58  };
59 
60  /*
61  * group_vector is 0 to not add any groups, or n > 0 to add groups from
62  * group_vectors[n-1].
63  */
64  _dbus_assert (group_vector >= 0);
65  _dbus_assert (group_vector <= _DBUS_N_ELEMENTS (group_vectors));
66 
67  credentials = _dbus_credentials_new ();
68 
69  if (unix_uid != DBUS_UID_UNSET)
70  {
71  if (!_dbus_credentials_add_unix_uid (credentials, unix_uid))
72  {
73  _dbus_credentials_unref (credentials);
74  return NULL;
75  }
76  }
77 
78  if (pid != DBUS_PID_UNSET)
79  {
80  if (!_dbus_credentials_add_pid (credentials, pid))
81  {
82  _dbus_credentials_unref (credentials);
83  return NULL;
84  }
85  }
86 
87  if (group_vector)
88  {
89  dbus_gid_t *copy;
90 
91  copy = dbus_new0 (dbus_gid_t, group_vectors[group_vector - 1].n);
92 
93  if (copy == NULL)
94  {
95  _dbus_credentials_unref (credentials);
96  return NULL;
97  }
98 
99  memcpy (copy, group_vectors[group_vector - 1].gids,
100  sizeof (dbus_gid_t) * group_vectors[group_vector - 1].n);
101 
102  _dbus_credentials_take_unix_gids (credentials, copy,
103  group_vectors[group_vector - 1].n);
104  }
105 
106  if (windows_sid != NULL)
107  {
108  if (!_dbus_credentials_add_windows_sid (credentials, windows_sid))
109  {
110  _dbus_credentials_unref (credentials);
111  return NULL;
112  }
113  }
114 
115  return credentials;
116 }
117 
118 #define SAMPLE_SID "whatever a windows sid looks like"
119 #define OTHER_SAMPLE_SID "whatever else"
120 
122 _dbus_credentials_test (const char *test_data_dir)
123 {
124  DBusCredentials *creds;
125  DBusCredentials *creds2;
126  DBusString str;
127  const dbus_gid_t *gids;
128  size_t n;
129 
130  if (test_data_dir == NULL)
131  return TRUE;
132 
133  creds = make_credentials (12, 511, 1, SAMPLE_SID);
134  if (creds == NULL)
135  _dbus_test_fatal ("oom");
136 
137  /* test refcounting */
138  _dbus_credentials_ref (creds);
139  _dbus_credentials_unref (creds);
140 
141  _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
142  _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
143  _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_GROUP_IDS));
144  _dbus_assert (_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
145 
147  _dbus_assert (_dbus_credentials_get_pid (creds) == 511);
148  _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds), SAMPLE_SID) == 0);
149  _dbus_assert (_dbus_credentials_get_unix_gids (creds, &gids, &n));
150  _dbus_assert (n == 4);
151  _dbus_assert (gids[0] == 42);
152  _dbus_assert (gids[1] == 123);
153  _dbus_assert (gids[2] == 1000);
154  _dbus_assert (gids[3] == 5678);
155 
158 
159  /* Test copy */
160  creds2 = _dbus_credentials_copy (creds);
161  if (creds2 == NULL)
162  _dbus_test_fatal ("oom");
163 
164  _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_USER_ID));
165  _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
166  _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_UNIX_GROUP_IDS));
167  _dbus_assert (_dbus_credentials_include (creds2, DBUS_CREDENTIAL_WINDOWS_SID));
168 
170  _dbus_assert (_dbus_credentials_get_pid (creds2) == 511);
171  _dbus_assert (strcmp (_dbus_credentials_get_windows_sid (creds2), SAMPLE_SID) == 0);
172  _dbus_assert (_dbus_credentials_get_unix_gids (creds2, &gids, &n));
173  _dbus_assert (n == 4);
174  _dbus_assert (gids[0] == 42);
175  _dbus_assert (gids[1] == 123);
176  _dbus_assert (gids[2] == 1000);
177  _dbus_assert (gids[3] == 5678);
178 
180 
181  _dbus_credentials_unref (creds2);
182 
183  /* Same user if both unix and windows are the same */
184  creds2 = make_credentials (12, DBUS_PID_UNSET, 0, SAMPLE_SID);
185  if (creds2 == NULL)
186  _dbus_test_fatal ("oom");
187 
188  _dbus_assert (_dbus_credentials_same_user (creds, creds2));
189 
190  _dbus_credentials_unref (creds2);
191 
192  /* Not the same user if Windows is missing */
193  creds2 = make_credentials (12, DBUS_PID_UNSET, 0, NULL);
194  if (creds2 == NULL)
195  _dbus_test_fatal ("oom");
196 
197  _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
199 
200  _dbus_credentials_unref (creds2);
201 
202  /* Not the same user if Windows is different */
203  creds2 = make_credentials (12, DBUS_PID_UNSET, 0, OTHER_SAMPLE_SID);
204  if (creds2 == NULL)
205  _dbus_test_fatal ("oom");
206 
207  _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
208  _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
209 
210  _dbus_credentials_unref (creds2);
211 
212  /* Not the same user if Unix is missing */
213  creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, 0, SAMPLE_SID);
214  if (creds2 == NULL)
215  _dbus_test_fatal ("oom");
216 
217  _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
219 
220  _dbus_credentials_unref (creds2);
221 
222  /* Not the same user if Unix is different */
223  creds2 = make_credentials (15, DBUS_PID_UNSET, 0, SAMPLE_SID);
224  if (creds2 == NULL)
225  _dbus_test_fatal ("oom");
226 
227  _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
228  _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
229 
230  _dbus_credentials_unref (creds2);
231 
232  /* Not the same user if both are missing */
233  creds2 = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, 0, NULL);
234  if (creds2 == NULL)
235  _dbus_test_fatal ("oom");
236 
237  _dbus_assert (!_dbus_credentials_same_user (creds, creds2));
239 
240  _dbus_credentials_unref (creds2);
241 
242  /* Same user, but not a superset, if groups are different */
243  creds2 = make_credentials (12, 511, 2, SAMPLE_SID);
244  if (creds2 == NULL)
245  _dbus_test_fatal ("oom");
246 
247  _dbus_assert (_dbus_credentials_same_user (creds, creds2));
248  _dbus_assert (!_dbus_credentials_are_superset (creds, creds2));
249 
250  _dbus_credentials_unref (creds2);
251 
252  /* Groups being in the same order make no difference */
253  creds2 = make_credentials (12, 511, 3, SAMPLE_SID);
254  if (creds2 == NULL)
255  _dbus_test_fatal ("oom");
256 
257  _dbus_assert (_dbus_credentials_same_user (creds, creds2));
260 
261  _dbus_credentials_unref (creds2);
262 
263  /* Clearing credentials works */
264  _dbus_credentials_clear (creds);
265 
266  _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_USER_ID));
267  _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_UNIX_PROCESS_ID));
268  _dbus_assert (!_dbus_credentials_include (creds, DBUS_CREDENTIAL_WINDOWS_SID));
269 
273 
276 
277  _dbus_credentials_unref (creds);
278 
279  /* Make some more realistic credentials blobs to test stringification */
280  if (!_dbus_string_init (&str))
281  _dbus_test_fatal ("oom");
282 
283  creds = make_credentials (12, DBUS_PID_UNSET, 0, NULL);
284  if (creds == NULL)
285  _dbus_test_fatal ("oom");
286 
287  if (!_dbus_credentials_to_string_append (creds, &str))
288  _dbus_test_fatal ("oom");
289 
290  _dbus_test_diag ("Unix uid only: %s", _dbus_string_get_const_data (&str));
291  _dbus_assert (strcmp (_dbus_string_get_const_data (&str),
292  "uid=12") == 0);
293 
294  _dbus_credentials_unref (creds);
295 
296  creds = make_credentials (12, 511, 1, NULL);
297  if (creds == NULL)
298  _dbus_test_fatal ("oom");
299 
300  if (!_dbus_string_set_length (&str, 0))
301  _dbus_test_fatal ("oom");
302 
303  if (!_dbus_credentials_to_string_append (creds, &str))
304  _dbus_test_fatal ("oom");
305 
306  _dbus_test_diag ("Unix complete set: %s", _dbus_string_get_const_data (&str));
307  _dbus_assert (strcmp (_dbus_string_get_const_data (&str),
308  "uid=12 pid=511 gid=42 gid=123 gid=1000 gid=5678") == 0);
309 
310  _dbus_credentials_unref (creds);
311 
312  creds = make_credentials (DBUS_UID_UNSET, DBUS_PID_UNSET, 0, SAMPLE_SID);
313  if (creds == NULL)
314  _dbus_test_fatal ("oom");
315 
316  if (!_dbus_string_set_length (&str, 0))
317  _dbus_test_fatal ("oom");
318 
319  if (!_dbus_credentials_to_string_append (creds, &str))
320  _dbus_test_fatal ("oom");
321 
322  _dbus_test_diag ("Windows sid only: %s", _dbus_string_get_const_data (&str));
323  _dbus_assert (strcmp (_dbus_string_get_const_data (&str),
324  "sid=" SAMPLE_SID) == 0);
325 
326  _dbus_credentials_unref (creds);
327 
328  creds = make_credentials (DBUS_UID_UNSET, 511, 0, SAMPLE_SID);
329  if (creds == NULL)
330  _dbus_test_fatal ("oom");
331 
332  if (!_dbus_string_set_length (&str, 0))
333  _dbus_test_fatal ("oom");
334 
335  if (!_dbus_credentials_to_string_append (creds, &str))
336  _dbus_test_fatal ("oom");
337 
338  _dbus_test_diag ("Windows complete set: %s", _dbus_string_get_const_data (&str));
339  _dbus_assert (strcmp (_dbus_string_get_const_data (&str),
340  "pid=511 sid=" SAMPLE_SID) == 0);
341 
342  _dbus_credentials_unref (creds);
343 
344  _dbus_string_free (&str);
345 
346  return TRUE;
347 }
348 
349 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
dbus_uid_t _dbus_credentials_get_unix_uid(DBusCredentials *credentials)
Gets the UNIX user ID in the credentials, or DBUS_UID_UNSET if the credentials object doesn&#39;t contain...
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_credentials_include(DBusCredentials *credentials, DBusCredentialType type)
Checks whether the given credential is present.
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
dbus_bool_t _dbus_credentials_are_superset(DBusCredentials *credentials, DBusCredentials *possible_subset)
Checks whether the first credentials object contains all the credentials found in the second credenti...
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_bool_t _dbus_credentials_add_windows_sid(DBusCredentials *credentials, const char *windows_sid)
Add a Windows user SID to the credentials.
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:139
void _dbus_credentials_clear(DBusCredentials *credentials)
Clear all credentials in the object.
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:141
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:132
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_credentials_take_unix_gids(DBusCredentials *credentials, dbus_gid_t *gids, size_t n_gids)
Add UNIX group IDs to the credentials, replacing any group IDs that might already have been present...
void _dbus_credentials_ref(DBusCredentials *credentials)
Increment refcount on credentials.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:264
#define TRUE
Expands to "1".
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
dbus_pid_t _dbus_credentials_get_pid(DBusCredentials *credentials)
Gets the UNIX process ID in the credentials, or DBUS_PID_UNSET if the credentials object doesn&#39;t cont...
dbus_bool_t _dbus_credentials_get_unix_gids(DBusCredentials *credentials, const dbus_gid_t **gids, size_t *n_gids)
Get the Unix group IDs.
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:819
dbus_bool_t _dbus_credentials_to_string_append(DBusCredentials *credentials, DBusString *string)
Convert the credentials in this object to a human-readable string format, and append to the given str...
const char * _dbus_credentials_get_windows_sid(DBusCredentials *credentials)
Gets the Windows user SID in the credentials, or NULL if the credentials object doesn&#39;t contain a Win...
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:136
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:134
dbus_bool_t _dbus_credentials_are_empty(DBusCredentials *credentials)
Checks whether a credentials object contains anything.