1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-credentials.c Credentials provable through authentication
4 * Copyright (C) 2007 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
25 #include "dbus-credentials.h"
26 #include "dbus-internals.h"
29 * @defgroup DBusCredentials Credentials provable through authentication
30 * @ingroup DBusInternals
31 * @brief DBusCredentials object
33 * Credentials are what you have to prove you have in order to
34 * authenticate. The main credentials right now are a unix user
35 * account, a Windows user account, or a UNIX process ID.
39 * @defgroup DBusCredentialsInternals Credentials implementation details
40 * @ingroup DBusInternals
41 * @brief DBusCredentials implementation details
43 * Private details of credentials code.
48 struct DBusCredentials {
53 char *linux_security_label;
55 dbus_int32_t adt_audit_data_size;
61 * @addtogroup DBusCredentials
66 * Creates a new credentials object.
68 * @returns the new object or #NULL if no memory
71 _dbus_credentials_new (void)
73 DBusCredentials *creds;
75 creds = dbus_new (DBusCredentials, 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;
91 * Creates a new object with credentials (user ID and process ID) from the current process.
92 * @returns the new object or #NULL if no memory
95 _dbus_credentials_new_from_current_process (void)
97 DBusCredentials *creds;
99 creds = _dbus_credentials_new ();
103 if (!_dbus_credentials_add_from_current_process (creds))
105 _dbus_credentials_unref (creds);
113 * Increment refcount on credentials.
115 * @param credentials the object
118 _dbus_credentials_ref (DBusCredentials *credentials)
120 _dbus_assert (credentials->refcount > 0);
121 credentials->refcount += 1;
125 * Decrement refcount on credentials.
127 * @param credentials the object
130 _dbus_credentials_unref (DBusCredentials *credentials)
132 _dbus_assert (credentials->refcount > 0);
134 credentials->refcount -= 1;
135 if (credentials->refcount == 0)
137 dbus_free (credentials->windows_sid);
138 dbus_free (credentials->linux_security_label);
139 dbus_free (credentials->adt_audit_data);
140 dbus_free (credentials);
145 * Add a UNIX process ID to the credentials.
147 * @param credentials the object
148 * @param pid the process ID
149 * @returns #FALSE if no memory
152 _dbus_credentials_add_pid (DBusCredentials *credentials,
155 credentials->pid = pid;
160 * Add a UNIX user ID to the credentials.
162 * @param credentials the object
163 * @param uid the user ID
164 * @returns #FALSE if no memory
167 _dbus_credentials_add_unix_uid(DBusCredentials *credentials,
170 credentials->unix_uid = uid;
176 * Add a Windows user SID to the credentials.
178 * @param credentials the object
179 * @param windows_sid the user SID
180 * @returns #FALSE if no memory
183 _dbus_credentials_add_windows_sid (DBusCredentials *credentials,
184 const char *windows_sid)
188 copy = _dbus_strdup (windows_sid);
192 dbus_free (credentials->windows_sid);
193 credentials->windows_sid = copy;
199 * Add a Linux security label, as used by LSMs such as SELinux, Smack and
200 * AppArmor, to the credentials.
202 * @param credentials the object
203 * @param label the label
204 * @returns #FALSE if no memory
207 _dbus_credentials_add_linux_security_label (DBusCredentials *credentials,
212 copy = _dbus_strdup (label);
216 dbus_free (credentials->linux_security_label);
217 credentials->linux_security_label = copy;
223 * Add ADT audit data to the credentials.
225 * @param credentials the object
226 * @param audit_data the audit data
227 * @param size the length of audit data
228 * @returns #FALSE if no memory
231 _dbus_credentials_add_adt_audit_data (DBusCredentials *credentials,
236 copy = _dbus_memdup (audit_data, size);
240 dbus_free (credentials->adt_audit_data);
241 credentials->adt_audit_data = copy;
242 credentials->adt_audit_data_size = size;
248 * Checks whether the given credential is present.
250 * @param credentials the object
251 * @param type the credential to check for
252 * @returns #TRUE if the credential is present
255 _dbus_credentials_include (DBusCredentials *credentials,
256 DBusCredentialType type)
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;
272 _dbus_assert_not_reached ("Unknown credential enum value");
277 * Gets the UNIX process ID in the credentials, or #DBUS_PID_UNSET if
278 * the credentials object doesn't contain a process ID.
280 * @param credentials the object
281 * @returns UNIX process ID
284 _dbus_credentials_get_pid (DBusCredentials *credentials)
286 return credentials->pid;
290 * Gets the UNIX user ID in the credentials, or #DBUS_UID_UNSET if
291 * the credentials object doesn't contain a user ID.
293 * @param credentials the object
294 * @returns UNIX user ID
297 _dbus_credentials_get_unix_uid (DBusCredentials *credentials)
299 return credentials->unix_uid;
303 * Gets the Windows user SID in the credentials, or #NULL if
304 * the credentials object doesn't contain a Windows user SID.
306 * @param credentials the object
307 * @returns Windows user SID
310 _dbus_credentials_get_windows_sid (DBusCredentials *credentials)
312 return credentials->windows_sid;
316 * Gets the Linux security label (as used by LSMs) from the credentials,
317 * or #NULL if the credentials object doesn't contain a security label.
319 * @param credentials the object
320 * @returns the security label
323 _dbus_credentials_get_linux_security_label (DBusCredentials *credentials)
325 return credentials->linux_security_label;
329 * Gets the ADT audit data in the credentials, or #NULL if
330 * the credentials object doesn't contain ADT audit data.
332 * @param credentials the object
333 * @returns Solaris ADT audit data
336 _dbus_credentials_get_adt_audit_data (DBusCredentials *credentials)
338 return credentials->adt_audit_data;
342 * Gets the ADT audit data size in the credentials, or 0 if
343 * the credentials object doesn't contain ADT audit data.
345 * @param credentials the object
346 * @returns Solaris ADT audit data size
349 _dbus_credentials_get_adt_audit_data_size (DBusCredentials *credentials)
351 return credentials->adt_audit_data_size;
355 * Checks whether the first credentials object contains
356 * all the credentials found in the second credentials object.
358 * @param credentials the object
359 * @param possible_subset see if credentials in here are also in the first arg
360 * @returns #TRUE if second arg is contained in first
363 _dbus_credentials_are_superset (DBusCredentials *credentials,
364 DBusCredentials *possible_subset)
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));
385 * Checks whether a credentials object contains anything.
387 * @param credentials the object
388 * @returns #TRUE if there are no credentials in the object
391 _dbus_credentials_are_empty (DBusCredentials *credentials)
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;
402 * Checks whether a credentials object contains a user identity.
404 * @param credentials the object
405 * @returns #TRUE if there are no user identities in the object
408 _dbus_credentials_are_anonymous (DBusCredentials *credentials)
411 credentials->unix_uid == DBUS_UID_UNSET &&
412 credentials->windows_sid == NULL;
416 * Merge all credentials found in the second object into the first object,
417 * overwriting the first object if there are any overlaps.
419 * @param credentials the object
420 * @param other_credentials credentials to merge
421 * @returns #FALSE if no memory
424 _dbus_credentials_add_credentials (DBusCredentials *credentials,
425 DBusCredentials *other_credentials)
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,
446 * Merge the given credential found in the second object into the first object,
447 * overwriting the first object's value for that credential.
449 * Does nothing if the second object does not contain the specified credential.
450 * i.e., will never delete a credential from the first object.
452 * @param credentials the object
453 * @param which the credential to overwrite
454 * @param other_credentials credentials to merge
455 * @returns #FALSE if no memory
458 _dbus_credentials_add_credential (DBusCredentials *credentials,
459 DBusCredentialType which,
460 DBusCredentials *other_credentials)
462 if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
463 other_credentials->pid != DBUS_PID_UNSET)
465 if (!_dbus_credentials_add_pid (credentials, other_credentials->pid))
468 else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
469 other_credentials->unix_uid != DBUS_UID_UNSET)
471 if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
474 else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
475 other_credentials->windows_sid != NULL)
477 if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
480 else if (which == DBUS_CREDENTIAL_LINUX_SECURITY_LABEL &&
481 other_credentials->linux_security_label != NULL)
483 if (!_dbus_credentials_add_linux_security_label (credentials,
484 other_credentials->linux_security_label))
487 else if (which == DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID &&
488 other_credentials->adt_audit_data != NULL)
490 if (!_dbus_credentials_add_adt_audit_data (credentials, other_credentials->adt_audit_data, other_credentials->adt_audit_data_size))
498 * Clear all credentials in the object.
500 * @param credentials the object
503 _dbus_credentials_clear (DBusCredentials *credentials)
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;
517 * Copy a credentials object.
519 * @param credentials the object
520 * @returns the copy or #NULL
523 _dbus_credentials_copy (DBusCredentials *credentials)
525 DBusCredentials *copy;
527 copy = _dbus_credentials_new ();
531 if (!_dbus_credentials_add_credentials (copy, credentials))
533 _dbus_credentials_unref (copy);
541 * Check whether the user-identifying credentials in two credentials
542 * objects are identical. Credentials that are not related to the
543 * user are ignored, but any kind of user ID credentials must be the
544 * same (UNIX user ID, Windows user SID, etc.) and present in both
545 * objects for the function to return #TRUE.
547 * @param credentials the object
548 * @param other_credentials credentials to compare
549 * @returns #TRUE if the two credentials refer to the same user
552 _dbus_credentials_same_user (DBusCredentials *credentials,
553 DBusCredentials *other_credentials)
555 /* both windows and unix user must be the same (though pretty much
556 * in all conceivable cases, one will be unset)
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));
565 * Convert the credentials in this object to a human-readable
566 * string format, and append to the given string.
568 * @param credentials the object
569 * @param string append to this string
570 * @returns #FALSE if no memory
573 _dbus_credentials_to_string_append (DBusCredentials *credentials,
579 if (credentials->unix_uid != DBUS_UID_UNSET)
581 if (!_dbus_string_append_printf (string, "uid=" DBUS_UID_FORMAT, credentials->unix_uid))
585 if (credentials->pid != DBUS_PID_UNSET)
587 if (!_dbus_string_append_printf (string, "%spid=" DBUS_PID_FORMAT, join ? " " : "", credentials->pid))
593 if (credentials->windows_sid != NULL)
595 if (!_dbus_string_append_printf (string, "%ssid=%s", join ? " " : "", credentials->windows_sid))
602 if (credentials->linux_security_label != NULL)
604 if (!_dbus_string_append_printf (string, "%slsm='%s'",
606 credentials->linux_security_label))
618 /* tests in dbus-credentials-util.c */