D-Bus: dbus-credentials.c Source File

D-Bus 1.12.4

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 char *linux_security_label;
54 void *adt_audit_data;
55 dbus_int32_t adt_audit_data_size;
56 };
57
70 DBusCredentials*
71 _dbus_credentials_new (void)
72 {
73 DBusCredentials *creds;
74
75 creds = dbus_new (DBusCredentials, 1);
76 if (creds == NULL)
77 return NULL;
78
79 creds->refcount = 1;
80 creds->unix_uid = DBUS_UID_UNSET;
81 creds->pid = DBUS_PID_UNSET;
82 creds->windows_sid = NULL;
83 creds->linux_security_label = NULL;
84 creds->adt_audit_data = NULL;
85 creds->adt_audit_data_size = 0;
86
87 return creds;
88 }
89
94 DBusCredentials*
95 _dbus_credentials_new_from_current_process (void)
96 {
97 DBusCredentials *creds;
98
99 creds = _dbus_credentials_new ();
100 if (creds == NULL)
101 return NULL;
102
103 if (!_dbus_credentials_add_from_current_process (creds))
104 {
105 _dbus_credentials_unref (creds);
106 return NULL;
107 }
108
109 return creds;
110 }
111
117 void
118 _dbus_credentials_ref (DBusCredentials *credentials)
119 {
120 _dbus_assert (credentials->refcount > 0);
121 credentials->refcount += 1;
122 }
123
129 void
130 _dbus_credentials_unref (DBusCredentials *credentials)
131 {
132 _dbus_assert (credentials->refcount > 0);
133
134 credentials->refcount -= 1;
135 if (credentials->refcount == 0)
136 {
137 dbus_free (credentials->windows_sid);
138 dbus_free (credentials->linux_security_label);
139 dbus_free (credentials->adt_audit_data);
140 dbus_free (credentials);
141 }
142 }
143
151 dbus_bool_t
152 _dbus_credentials_add_pid (DBusCredentials *credentials,
153 dbus_pid_t pid)
154 {
155 credentials->pid = pid;
156 return TRUE;
157 }
158
166 dbus_bool_t
167 _dbus_credentials_add_unix_uid(DBusCredentials *credentials,
168 dbus_uid_t uid)
169 {
170 credentials->unix_uid = uid;
171 return TRUE;
172
173 }
174
182 dbus_bool_t
183 _dbus_credentials_add_windows_sid (DBusCredentials *credentials,
184 const char *windows_sid)
185 {
186 char *copy;
187
188 copy = _dbus_strdup (windows_sid);
189 if (copy == NULL)
190 return FALSE;
191
192 dbus_free (credentials->windows_sid);
193 credentials->windows_sid = copy;
194
195 return TRUE;
196 }
197
206 dbus_bool_t
207 _dbus_credentials_add_linux_security_label (DBusCredentials *credentials,
208 const char *label)
209 {
210 char *copy;
211
212 copy = _dbus_strdup (label);
213 if (copy == NULL)
214 return FALSE;
215
216 dbus_free (credentials->linux_security_label);
217 credentials->linux_security_label = copy;
218
219 return TRUE;
220 }
221
230 dbus_bool_t
231 _dbus_credentials_add_adt_audit_data (DBusCredentials *credentials,
232 void *audit_data,
233 dbus_int32_t size)
234 {
235 void *copy;
236 copy = _dbus_memdup (audit_data, size);
237 if (copy == NULL)
238 return FALSE;
239
240 dbus_free (credentials->adt_audit_data);
241 credentials->adt_audit_data = copy;
242 credentials->adt_audit_data_size = size;
243
244 return TRUE;
245 }
246
254 dbus_bool_t
255 _dbus_credentials_include (DBusCredentials *credentials,
256 DBusCredentialType type)
257 {
258 switch (type)
259 {
260 case DBUS_CREDENTIAL_UNIX_PROCESS_ID:
261 return credentials->pid != DBUS_PID_UNSET;
262 case DBUS_CREDENTIAL_UNIX_USER_ID:
263 return credentials->unix_uid != DBUS_UID_UNSET;
264 case DBUS_CREDENTIAL_WINDOWS_SID:
265 return credentials->windows_sid != NULL;
266 case DBUS_CREDENTIAL_LINUX_SECURITY_LABEL:
267 return credentials->linux_security_label != NULL;
268 case DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID:
269 return credentials->adt_audit_data != NULL;
270 default:
271 _dbus_assert_not_reached ("Unknown credential enum value");
272 return FALSE;
273 }
274 }
275
283 dbus_pid_t
284 _dbus_credentials_get_pid (DBusCredentials *credentials)
285 {
286 return credentials->pid;
287 }
288
296 dbus_uid_t
297 _dbus_credentials_get_unix_uid (DBusCredentials *credentials)
298 {
299 return credentials->unix_uid;
300 }
301
309 const char*
310 _dbus_credentials_get_windows_sid (DBusCredentials *credentials)
311 {
312 return credentials->windows_sid;
313 }
314
322 const char *
323 _dbus_credentials_get_linux_security_label (DBusCredentials *credentials)
324 {
325 return credentials->linux_security_label;
326 }
327
335 void *
336 _dbus_credentials_get_adt_audit_data (DBusCredentials *credentials)
337 {
338 return credentials->adt_audit_data;
339 }
340
348 dbus_int32_t
349 _dbus_credentials_get_adt_audit_data_size (DBusCredentials *credentials)
350 {
351 return credentials->adt_audit_data_size;
352 }
353
362 dbus_bool_t
363 _dbus_credentials_are_superset (DBusCredentials *credentials,
364 DBusCredentials *possible_subset)
365 {
366 return
367 (possible_subset->pid == DBUS_PID_UNSET ||
368 possible_subset->pid == credentials->pid) &&
369 (possible_subset->unix_uid == DBUS_UID_UNSET ||
370 possible_subset->unix_uid == credentials->unix_uid) &&
371 (possible_subset->windows_sid == NULL ||
372 (credentials->windows_sid && strcmp (possible_subset->windows_sid,
373 credentials->windows_sid) == 0)) &&
374 (possible_subset->linux_security_label == NULL ||
375 (credentials->linux_security_label != NULL &&
376 strcmp (possible_subset->linux_security_label,
377 credentials->linux_security_label) == 0)) &&
378 (possible_subset->adt_audit_data == NULL ||
379 (credentials->adt_audit_data && memcmp (possible_subset->adt_audit_data,
380 credentials->adt_audit_data,
381 credentials->adt_audit_data_size) == 0));
382 }
383
390 dbus_bool_t
391 _dbus_credentials_are_empty (DBusCredentials *credentials)
392 {
393 return
394 credentials->pid == DBUS_PID_UNSET &&
395 credentials->unix_uid == DBUS_UID_UNSET &&
396 credentials->windows_sid == NULL &&
397 credentials->linux_security_label == NULL &&
398 credentials->adt_audit_data == NULL;
399 }
400
407 dbus_bool_t
408 _dbus_credentials_are_anonymous (DBusCredentials *credentials)
409 {
410 return
411 credentials->unix_uid == DBUS_UID_UNSET &&
412 credentials->windows_sid == NULL;
413 }
414
423 dbus_bool_t
424 _dbus_credentials_add_credentials (DBusCredentials *credentials,
425 DBusCredentials *other_credentials)
426 {
427 return
428 _dbus_credentials_add_credential (credentials,
429 DBUS_CREDENTIAL_UNIX_PROCESS_ID,
430 other_credentials) &&
431 _dbus_credentials_add_credential (credentials,
432 DBUS_CREDENTIAL_UNIX_USER_ID,
433 other_credentials) &&
434 _dbus_credentials_add_credential (credentials,
435 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID,
436 other_credentials) &&
437 _dbus_credentials_add_credential (credentials,
438 DBUS_CREDENTIAL_LINUX_SECURITY_LABEL,
439 other_credentials) &&
440 _dbus_credentials_add_credential (credentials,
441 DBUS_CREDENTIAL_WINDOWS_SID,
442 other_credentials);
443 }
444
457 dbus_bool_t
458 _dbus_credentials_add_credential (DBusCredentials *credentials,
459 DBusCredentialType which,
460 DBusCredentials *other_credentials)
461 {
462 if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
463 other_credentials->pid != DBUS_PID_UNSET)
464 {
465 if (!_dbus_credentials_add_pid (credentials, other_credentials->pid))
466 return FALSE;
467 }
468 else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
469 other_credentials->unix_uid != DBUS_UID_UNSET)
470 {
471 if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
472 return FALSE;
473 }
474 else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
475 other_credentials->windows_sid != NULL)
476 {
477 if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
478 return FALSE;
479 }
480 else if (which == DBUS_CREDENTIAL_LINUX_SECURITY_LABEL &&
481 other_credentials->linux_security_label != NULL)
482 {
483 if (!_dbus_credentials_add_linux_security_label (credentials,
484 other_credentials->linux_security_label))
485 return FALSE;
486 }
487 else if (which == DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID &&
488 other_credentials->adt_audit_data != NULL)
489 {
490 if (!_dbus_credentials_add_adt_audit_data (credentials, other_credentials->adt_audit_data, other_credentials->adt_audit_data_size))
491 return FALSE;
492 }
493
494 return TRUE;
495 }
496
502 void
503 _dbus_credentials_clear (DBusCredentials *credentials)
504 {
505 credentials->pid = DBUS_PID_UNSET;
506 credentials->unix_uid = DBUS_UID_UNSET;
507 dbus_free (credentials->windows_sid);
508 credentials->windows_sid = NULL;
509 dbus_free (credentials->linux_security_label);
510 credentials->linux_security_label = NULL;
511 dbus_free (credentials->adt_audit_data);
512 credentials->adt_audit_data = NULL;
513 credentials->adt_audit_data_size = 0;
514 }
515
522 DBusCredentials*
523 _dbus_credentials_copy (DBusCredentials *credentials)
524 {
525 DBusCredentials *copy;
526
527 copy = _dbus_credentials_new ();
528 if (copy == NULL)
529 return NULL;
530
531 if (!_dbus_credentials_add_credentials (copy, credentials))
532 {
533 _dbus_credentials_unref (copy);
534 return NULL;
535 }
536
537 return copy;
538 }
539
551 dbus_bool_t
552 _dbus_credentials_same_user (DBusCredentials *credentials,
553 DBusCredentials *other_credentials)
554 {
555 /* both windows and unix user must be the same (though pretty much
556 * in all conceivable cases, one will be unset)
557 */
558 return credentials->unix_uid == other_credentials->unix_uid &&
559 ((!(credentials->windows_sid || other_credentials->windows_sid)) ||
560 (credentials->windows_sid && other_credentials->windows_sid &&
561 strcmp (credentials->windows_sid, other_credentials->windows_sid) == 0));
562 }
563
572 dbus_bool_t
573 _dbus_credentials_to_string_append (DBusCredentials *credentials,
574 DBusString *string)
575 {
576 dbus_bool_t join;
577
578 join = FALSE;
579 if (credentials->unix_uid != DBUS_UID_UNSET)
580 {
581 if (!_dbus_string_append_printf (string, "uid=" DBUS_UID_FORMAT, credentials->unix_uid))
582 goto oom;
583 join = TRUE;
584 }
585 if (credentials->pid != DBUS_PID_UNSET)
586 {
587 if (!_dbus_string_append_printf (string, "%spid=" DBUS_PID_FORMAT, join ? " " : "", credentials->pid))
588 goto oom;
589 join = TRUE;
590 }
591 else
592 join = FALSE;
593 if (credentials->windows_sid != NULL)
594 {
595 if (!_dbus_string_append_printf (string, "%ssid=%s", join ? " " : "", credentials->windows_sid))
596 goto oom;
597 join = TRUE;
598 }
599 else
600 join = FALSE;
601
602 if (credentials->linux_security_label != NULL)
603 {
604 if (!_dbus_string_append_printf (string, "%slsm='%s'",
605 join ? " " : "",
606 credentials->linux_security_label))
607 goto oom;
608 join = TRUE;
609 }
610
611 return TRUE;
612 oom:
613 return FALSE;
614 }
615
618 /* 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&#39;t contain...
Definition: dbus-credentials.c:297
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:255
_dbus_credentials_copy
DBusCredentials * _dbus_credentials_copy(DBusCredentials *credentials)
Copy a credentials object.
Definition: dbus-credentials.c:523
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:702
_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&#39;t contain ADT a...
Definition: dbus-credentials.c:349
_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:458
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:363
DBUS_PID_FORMAT
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
Definition: dbus-sysdeps.h:120
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
_dbus_credentials_add_linux_security_label
dbus_bool_t _dbus_credentials_add_linux_security_label(DBusCredentials *credentials, const char *label)
Add a Linux security label, as used by LSMs such as SELinux, Smack and AppArmor, to the credentials...
Definition: dbus-credentials.c:207
_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:183
DBUS_PID_UNSET
#define DBUS_PID_UNSET
an invalid PID used to represent an uninitialized dbus_pid_t field
Definition: dbus-sysdeps.h:113
_dbus_credentials_clear
void _dbus_credentials_clear(DBusCredentials *credentials)
Clear all credentials in the object.
Definition: dbus-credentials.c:503
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:115
_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:424
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:106
_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:408
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:95
_dbus_memdup
void * _dbus_memdup(const void *mem, size_t n_bytes)
Duplicates a block of memory.
Definition: dbus-internals.c:564
_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:1114
_dbus_credentials_ref
void _dbus_credentials_ref(DBusCredentials *credentials)
Increment refcount on credentials.
Definition: dbus-credentials.c:118
_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:231
_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:2622
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:163
_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:152
DBUS_UID_FORMAT
#define DBUS_UID_FORMAT
an appropriate printf format for dbus_uid_t
Definition: dbus-sysdeps.h:122
_dbus_credentials_new
DBusCredentials * _dbus_credentials_new(void)
Creates a new credentials object.
Definition: dbus-credentials.c:71
_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&#39;t cont...
Definition: dbus-credentials.c:284
_dbus_credentials_unref
void _dbus_credentials_unref(DBusCredentials *credentials)
Decrement refcount on credentials.
Definition: dbus-credentials.c:130
FALSE
#define FALSE
Expands to "0".
_dbus_credentials_get_linux_security_label
const char * _dbus_credentials_get_linux_security_label(DBusCredentials *credentials)
Gets the Linux security label (as used by LSMs) from the credentials, or NULL if the credentials obje...
Definition: dbus-credentials.c:323
_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:552
_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:573
_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&#39;t contain a Win...
Definition: dbus-credentials.c:310
dbus_int32_t
int dbus_int32_t
A 32-bit signed integer on all platforms.
Definition: dbus-arch-deps.h:42
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:536
_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:167
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:108
_dbus_credentials_are_empty
dbus_bool_t _dbus_credentials_are_empty(DBusCredentials *credentials)
Checks whether a credentials object contains anything.
Definition: dbus-credentials.c:391
_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&#39;t contain ADT aud...
Definition: dbus-credentials.c:336