D-Bus: dbus-credentials.c Source File

D-Bus 1.8.16

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List

dbus-credentials.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-credentials.c Credentials provable through authentication
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 #include <config.h>
24 #include <string.h>
25 #include "dbus-credentials.h"
26 #include "dbus-internals.h"
27
48 struct DBusCredentials {
49 int refcount;
50 dbus_uid_t unix_uid;
51 dbus_pid_t pid;
52 char *windows_sid;
53 void *adt_audit_data;
54 dbus_int32_t adt_audit_data_size;
55 };
56
69 DBusCredentials*
70 _dbus_credentials_new (void)
71 {
72 DBusCredentials *creds;
73
74 creds = dbus_new (DBusCredentials, 1);
75 if (creds == NULL)
76 return NULL;
77
78 creds->refcount = 1;
79 creds->unix_uid = DBUS_UID_UNSET;
80 creds->pid = DBUS_PID_UNSET;
81 creds->windows_sid = NULL;
82 creds->adt_audit_data = NULL;
83 creds->adt_audit_data_size = 0;
84
85 return creds;
86 }
87
92 DBusCredentials*
93 _dbus_credentials_new_from_current_process (void)
94 {
95 DBusCredentials *creds;
96
97 creds = _dbus_credentials_new ();
98 if (creds == NULL)
99 return NULL;
100
101 if (!_dbus_credentials_add_from_current_process (creds))
102 {
103 _dbus_credentials_unref (creds);
104 return NULL;
105 }
106
107 return creds;
108 }
109
115 void
116 _dbus_credentials_ref (DBusCredentials *credentials)
117 {
118 _dbus_assert (credentials->refcount > 0);
119 credentials->refcount += 1;
120 }
121
127 void
128 _dbus_credentials_unref (DBusCredentials *credentials)
129 {
130 _dbus_assert (credentials->refcount > 0);
131
132 credentials->refcount -= 1;
133 if (credentials->refcount == 0)
134 {
135 dbus_free (credentials->windows_sid);
136 dbus_free (credentials->adt_audit_data);
137 dbus_free (credentials);
138 }
139 }
140
148 dbus_bool_t
149 _dbus_credentials_add_pid (DBusCredentials *credentials,
150 dbus_pid_t pid)
151 {
152 credentials->pid = pid;
153 return TRUE;
154 }
155
163 dbus_bool_t
164 _dbus_credentials_add_unix_uid(DBusCredentials *credentials,
165 dbus_uid_t uid)
166 {
167 credentials->unix_uid = uid;
168 return TRUE;
169
170 }
171
179 dbus_bool_t
180 _dbus_credentials_add_windows_sid (DBusCredentials *credentials,
181 const char *windows_sid)
182 {
183 char *copy;
184
185 copy = _dbus_strdup (windows_sid);
186 if (copy == NULL)
187 return FALSE;
188
189 dbus_free (credentials->windows_sid);
190 credentials->windows_sid = copy;
191
192 return TRUE;
193 }
194
203 dbus_bool_t
204 _dbus_credentials_add_adt_audit_data (DBusCredentials *credentials,
205 void *audit_data,
206 dbus_int32_t size)
207 {
208 void *copy;
209 copy = _dbus_memdup (audit_data, size);
210 if (copy == NULL)
211 return FALSE;
212
213 dbus_free (credentials->adt_audit_data);
214 credentials->adt_audit_data = copy;
215 credentials->adt_audit_data_size = size;
216
217 return TRUE;
218 }
219
227 dbus_bool_t
228 _dbus_credentials_include (DBusCredentials *credentials,
229 DBusCredentialType type)
230 {
231 switch (type)
232 {
233 case DBUS_CREDENTIAL_UNIX_PROCESS_ID:
234 return credentials->pid != DBUS_PID_UNSET;
235 case DBUS_CREDENTIAL_UNIX_USER_ID:
236 return credentials->unix_uid != DBUS_UID_UNSET;
237 case DBUS_CREDENTIAL_WINDOWS_SID:
238 return credentials->windows_sid != NULL;
239 case DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID:
240 return credentials->adt_audit_data != NULL;
241 }
242
243 _dbus_assert_not_reached ("Unknown credential enum value");
244 return FALSE;
245 }
246
254 dbus_pid_t
255 _dbus_credentials_get_pid (DBusCredentials *credentials)
256 {
257 return credentials->pid;
258 }
259
267 dbus_uid_t
268 _dbus_credentials_get_unix_uid (DBusCredentials *credentials)
269 {
270 return credentials->unix_uid;
271 }
272
280 const char*
281 _dbus_credentials_get_windows_sid (DBusCredentials *credentials)
282 {
283 return credentials->windows_sid;
284 }
285
293 void *
294 _dbus_credentials_get_adt_audit_data (DBusCredentials *credentials)
295 {
296 return credentials->adt_audit_data;
297 }
298
306 dbus_int32_t
307 _dbus_credentials_get_adt_audit_data_size (DBusCredentials *credentials)
308 {
309 return credentials->adt_audit_data_size;
310 }
311
320 dbus_bool_t
321 _dbus_credentials_are_superset (DBusCredentials *credentials,
322 DBusCredentials *possible_subset)
323 {
324 return
325 (possible_subset->pid == DBUS_PID_UNSET ||
326 possible_subset->pid == credentials->pid) &&
327 (possible_subset->unix_uid == DBUS_UID_UNSET ||
328 possible_subset->unix_uid == credentials->unix_uid) &&
329 (possible_subset->windows_sid == NULL ||
330 (credentials->windows_sid && strcmp (possible_subset->windows_sid,
331 credentials->windows_sid) == 0)) &&
332 (possible_subset->adt_audit_data == NULL ||
333 (credentials->adt_audit_data && memcmp (possible_subset->adt_audit_data,
334 credentials->adt_audit_data,
335 credentials->adt_audit_data_size) == 0));
336 }
337
344 dbus_bool_t
345 _dbus_credentials_are_empty (DBusCredentials *credentials)
346 {
347 return
348 credentials->pid == DBUS_PID_UNSET &&
349 credentials->unix_uid == DBUS_UID_UNSET &&
350 credentials->windows_sid == NULL &&
351 credentials->adt_audit_data == NULL;
352 }
353
360 dbus_bool_t
361 _dbus_credentials_are_anonymous (DBusCredentials *credentials)
362 {
363 return
364 credentials->unix_uid == DBUS_UID_UNSET &&
365 credentials->windows_sid == NULL;
366 }
367
376 dbus_bool_t
377 _dbus_credentials_add_credentials (DBusCredentials *credentials,
378 DBusCredentials *other_credentials)
379 {
380 return
381 _dbus_credentials_add_credential (credentials,
382 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
383 other_credentials) &&
384 _dbus_credentials_add_credential (credentials,
385 DBUS_CREDENTIAL_UNIX_USER_ID,
386 other_credentials) &&
387 _dbus_credentials_add_credential (credentials,
388 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
389 other_credentials) &&
390 _dbus_credentials_add_credential (credentials,
391 DBUS_CREDENTIAL_WINDOWS_SID,
392 other_credentials);
393 }
394
407 dbus_bool_t
408 _dbus_credentials_add_credential (DBusCredentials *credentials,
409 DBusCredentialType which,
410 DBusCredentials *other_credentials)
411 {
412 if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
413 other_credentials->pid != DBUS_PID_UNSET)
414 {
415 if (!_dbus_credentials_add_pid (credentials, other_credentials->pid))
416 return FALSE;
417 }
418 else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
419 other_credentials->unix_uid != DBUS_UID_UNSET)
420 {
421 if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
422 return FALSE;
423 }
424 else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
425 other_credentials->windows_sid != NULL)
426 {
427 if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
428 return FALSE;
429 }
430 else if (which == DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID &&
431 other_credentials->adt_audit_data != NULL)
432 {
433 if (!_dbus_credentials_add_adt_audit_data (credentials, other_credentials->adt_audit_data, other_credentials->adt_audit_data_size))
434 return FALSE;
435 }
436
437 return TRUE;
438 }
439
445 void
446 _dbus_credentials_clear (DBusCredentials *credentials)
447 {
448 credentials->pid = DBUS_PID_UNSET;
449 credentials->unix_uid = DBUS_UID_UNSET;
450 dbus_free (credentials->windows_sid);
451 credentials->windows_sid = NULL;
452 dbus_free (credentials->adt_audit_data);
453 credentials->adt_audit_data = NULL;
454 credentials->adt_audit_data_size = 0;
455 }
456
463 DBusCredentials*
464 _dbus_credentials_copy (DBusCredentials *credentials)
465 {
466 DBusCredentials *copy;
467
468 copy = _dbus_credentials_new ();
469 if (copy == NULL)
470 return NULL;
471
472 if (!_dbus_credentials_add_credentials (copy, credentials))
473 {
474 _dbus_credentials_unref (copy);
475 return NULL;
476 }
477
478 return copy;
479 }
480
492 dbus_bool_t
493 _dbus_credentials_same_user (DBusCredentials *credentials,
494 DBusCredentials *other_credentials)
495 {
496 /* both windows and unix user must be the same (though pretty much
497 * in all conceivable cases, one will be unset)
498 */
499 return credentials->unix_uid == other_credentials->unix_uid &&
500 ((!(credentials->windows_sid || other_credentials->windows_sid)) ||
501 (credentials->windows_sid && other_credentials->windows_sid &&
502 strcmp (credentials->windows_sid, other_credentials->windows_sid) == 0));
503 }
504
513 dbus_bool_t
514 _dbus_credentials_to_string_append (DBusCredentials *credentials,
515 DBusString *string)
516 {
517 dbus_bool_t join;
518
519 join = FALSE;
520 if (credentials->unix_uid != DBUS_UID_UNSET)
521 {
522 if (!_dbus_string_append_printf (string, "uid=" DBUS_UID_FORMAT, credentials->unix_uid))
523 goto oom;
524 join = TRUE;
525 }
526 if (credentials->pid != DBUS_PID_UNSET)
527 {
528 if (!_dbus_string_append_printf (string, "%spid=" DBUS_PID_FORMAT, join ? " " : "", credentials->pid))
529 goto oom;
530 join = TRUE;
531 }
532 else
533 join = FALSE;
534 if (credentials->windows_sid != NULL)
535 {
536 if (!_dbus_string_append_printf (string, "%ssid=%s", join ? " " : "", credentials->windows_sid))
537 goto oom;
538 join = TRUE;
539 }
540 else
541 join = FALSE;
542
543 return TRUE;
544 oom:
545 return FALSE;
546 }
547
550 /* tests in dbus-credentials-util.c */
_dbus_credentials_get_unix_uid
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't contain...
Definition: dbus-credentials.c:268
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
DBusCredentials
Definition: dbus-credentials.c:48
_dbus_credentials_include
dbus_bool_t _dbus_credentials_include(DBusCredentials *credentials, DBusCredentialType type)
Checks whether the given credential is present.
Definition: dbus-credentials.c:228
_dbus_credentials_copy
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
Definition: dbus-credentials.c:464
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
_dbus_credentials_get_adt_audit_data_size
dbus_int32_t _dbus_credentials_get_adt_audit_data_size(DBusCredentials *credentials)
Gets the ADT audit data size in the credentials, or 0 if the credentials object doesn't contain ADT a...
Definition: dbus-credentials.c:307
_dbus_credentials_add_credential
dbus_bool_t _dbus_credentials_add_credential(DBusCredentials *credentials, DBusCredentialType which, DBusCredentials *other_credentials)
Merge the given credential found in the second object into the first object, overwriting the first ob...
Definition: dbus-credentials.c:408
dbus_new
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
_dbus_credentials_are_superset
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...
Definition: dbus-credentials.c:321
DBUS_PID_FORMAT
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
Definition: dbus-sysdeps.h:110
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:135
_dbus_credentials_add_windows_sid
dbus_bool_t _dbus_credentials_add_windows_sid(DBusCredentials *credentials, const char *windows_sid)
Add a Windows user SID to the credentials.
Definition: dbus-credentials.c:180
DBUS_PID_UNSET
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:103
_dbus_credentials_clear
void _dbus_credentials_clear(DBusCredentials *credentials)
Clear all credentials in the object.
Definition: dbus-credentials.c:446
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:105
_dbus_credentials_add_credentials
dbus_bool_t _dbus_credentials_add_credentials(DBusCredentials *credentials, DBusCredentials *other_credentials)
Merge all credentials found in the second object into the first object, overwriting the first object ...
Definition: dbus-credentials.c:377
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:96
_dbus_credentials_are_anonymous
dbus_bool_t _dbus_credentials_are_anonymous(DBusCredentials *credentials)
Checks whether a credentials object contains a user identity.
Definition: dbus-credentials.c:361
DBusString
Definition: dbus-string.h:42
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
_dbus_credentials_new_from_current_process
DBusCredentials * _dbus_credentials_new_from_current_process(void)
Creates a new object with credentials (user ID and process ID) from the current process.
Definition: dbus-credentials.c:93
_dbus_memdup
void * _dbus_memdup(const void *mem, size_t n_bytes)
Duplicates a block of memory.
Definition: dbus-internals.c:555
_dbus_string_append_printf
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1096
_dbus_credentials_ref
void _dbus_credentials_ref(DBusCredentials *credentials)
Increment refcount on credentials.
Definition: dbus-credentials.c:116
_dbus_credentials_add_adt_audit_data
dbus_bool_t _dbus_credentials_add_adt_audit_data(DBusCredentials *credentials, void *audit_data, dbus_int32_t size)
Add ADT audit data to the credentials.
Definition: dbus-credentials.c:204
_dbus_credentials_add_from_current_process
dbus_bool_t _dbus_credentials_add_from_current_process(DBusCredentials *credentials)
Adds the credentials of the current process to the passed-in credentials object.
Definition: dbus-sysdeps-unix.c:2368
TRUE
#define TRUE
Expands to "1".
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition: dbus-internals.h:145
_dbus_credentials_add_pid
dbus_bool_t _dbus_credentials_add_pid(DBusCredentials *credentials, dbus_pid_t pid)
Add a UNIX process ID to the credentials.
Definition: dbus-credentials.c:149
DBUS_UID_FORMAT
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:112
_dbus_credentials_new
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
Definition: dbus-credentials.c:70
_dbus_credentials_get_pid
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't cont...
Definition: dbus-credentials.c:255
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition: dbus-credentials.c:128
FALSE
#define FALSE
Expands to "0".
_dbus_credentials_same_user
dbus_bool_t _dbus_credentials_same_user(DBusCredentials *credentials, DBusCredentials *other_credentials)
Check whether the user-identifying credentials in two credentials objects are identical.
Definition: dbus-credentials.c:493
_dbus_credentials_to_string_append
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...
Definition: dbus-credentials.c:514
_dbus_credentials_get_windows_sid
const char * _dbus_credentials_get_windows_sid(DBusCredentials *credentials)
Gets the Windows user SID in the credentials, or NULL if the credentials object doesn't contain a Win...
Definition: dbus-credentials.c:281
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:527
_dbus_credentials_add_unix_uid
dbus_bool_t _dbus_credentials_add_unix_uid(DBusCredentials *credentials, dbus_uid_t uid)
Add a UNIX user ID to the credentials.
Definition: dbus-credentials.c:164
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:98
_dbus_credentials_are_empty
dbus_bool_t _dbus_credentials_are_empty(DBusCredentials *credentials)
Checks whether a credentials object contains anything.
Definition: dbus-credentials.c:345
_dbus_credentials_get_adt_audit_data
void * _dbus_credentials_get_adt_audit_data(DBusCredentials *credentials)
Gets the ADT audit data in the credentials, or NULL if the credentials object doesn't contain ADT aud...
Definition: dbus-credentials.c:294