dbus-credentials.c

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-credentials.c Credentials provable through authentication
00003  *
00004  * Copyright (C) 2007 Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #include <config.h>
00024 #include <string.h>
00025 #include "dbus-credentials.h"
00026 #include "dbus-internals.h"
00027 
00048 struct DBusCredentials {
00049   int refcount;
00050   dbus_uid_t unix_uid;
00051   dbus_pid_t unix_pid;
00052   char *windows_sid;
00053 };
00054 
00067 DBusCredentials*
00068 _dbus_credentials_new (void)
00069 {
00070   DBusCredentials *creds;
00071 
00072   creds = dbus_new (DBusCredentials, 1);
00073   if (creds == NULL)
00074     return NULL;
00075   
00076   creds->refcount = 1;
00077   creds->unix_uid = DBUS_UID_UNSET;
00078   creds->unix_pid = DBUS_PID_UNSET;
00079   creds->windows_sid = NULL;
00080 
00081   return creds;
00082 }
00083 
00088 DBusCredentials*
00089 _dbus_credentials_new_from_current_process (void)
00090 {
00091   DBusCredentials *creds;
00092 
00093   creds = _dbus_credentials_new ();
00094   if (creds == NULL)
00095     return NULL;
00096 
00097   if (!_dbus_credentials_add_from_current_process (creds))
00098     {
00099       _dbus_credentials_unref (creds);
00100       return NULL;
00101     }
00102   
00103   return creds;
00104 }
00105 
00111 void
00112 _dbus_credentials_ref (DBusCredentials *credentials)
00113 {
00114   _dbus_assert (credentials->refcount > 0);
00115   credentials->refcount += 1;
00116 }
00117 
00123 void
00124 _dbus_credentials_unref (DBusCredentials    *credentials)
00125 {
00126   _dbus_assert (credentials->refcount > 0);
00127 
00128   credentials->refcount -= 1;
00129   if (credentials->refcount == 0)
00130     {
00131       dbus_free (credentials->windows_sid);
00132       dbus_free (credentials);
00133     }
00134 }
00135 
00143 dbus_bool_t
00144 _dbus_credentials_add_unix_pid (DBusCredentials    *credentials,
00145                                 dbus_pid_t          pid)
00146 {
00147   credentials->unix_pid = pid;
00148   return TRUE;
00149 }
00150 
00158 dbus_bool_t
00159 _dbus_credentials_add_unix_uid(DBusCredentials    *credentials,
00160                                dbus_uid_t          uid)
00161 {
00162   credentials->unix_uid = uid;
00163   return TRUE;
00164 
00165 }
00166 
00174 dbus_bool_t
00175 _dbus_credentials_add_windows_sid (DBusCredentials    *credentials,
00176                                    const char         *windows_sid)
00177 {
00178   char *copy;
00179 
00180   copy = _dbus_strdup (windows_sid);
00181   if (copy == NULL)
00182     return FALSE;
00183 
00184   dbus_free (credentials->windows_sid);
00185   credentials->windows_sid = copy;
00186 
00187   return TRUE;
00188 }
00189 
00197 dbus_bool_t
00198 _dbus_credentials_include (DBusCredentials    *credentials,
00199                            DBusCredentialType  type)
00200 {
00201   switch (type)
00202     {
00203     case DBUS_CREDENTIAL_UNIX_PROCESS_ID:
00204       return credentials->unix_pid != DBUS_PID_UNSET;
00205     case DBUS_CREDENTIAL_UNIX_USER_ID:
00206       return credentials->unix_uid != DBUS_UID_UNSET;
00207     case DBUS_CREDENTIAL_WINDOWS_SID:
00208       return credentials->windows_sid != NULL;
00209     }
00210 
00211   _dbus_assert_not_reached ("Unknown credential enum value");
00212   return FALSE;
00213 }
00214 
00222 dbus_pid_t
00223 _dbus_credentials_get_unix_pid (DBusCredentials    *credentials)
00224 {
00225   return credentials->unix_pid;
00226 }
00227 
00235 dbus_uid_t
00236 _dbus_credentials_get_unix_uid (DBusCredentials    *credentials)
00237 {
00238   return credentials->unix_uid;
00239 }
00240 
00248 const char*
00249 _dbus_credentials_get_windows_sid (DBusCredentials    *credentials)
00250 {
00251   return credentials->windows_sid;
00252 }
00253 
00262 dbus_bool_t
00263 _dbus_credentials_are_superset (DBusCredentials    *credentials,
00264                                 DBusCredentials    *possible_subset)
00265 {
00266   return
00267     (possible_subset->unix_pid == DBUS_PID_UNSET ||
00268      possible_subset->unix_pid == credentials->unix_pid) &&
00269     (possible_subset->unix_uid == DBUS_UID_UNSET ||
00270      possible_subset->unix_uid == credentials->unix_uid) &&
00271     (possible_subset->windows_sid == NULL ||
00272      (credentials->windows_sid && strcmp (possible_subset->windows_sid,
00273                                           credentials->windows_sid) == 0));
00274 }
00275 
00282 dbus_bool_t
00283 _dbus_credentials_are_empty (DBusCredentials    *credentials)
00284 {
00285   return
00286     credentials->unix_pid == DBUS_PID_UNSET &&
00287     credentials->unix_uid == DBUS_UID_UNSET &&
00288     credentials->windows_sid == NULL;
00289 }
00290 
00297 dbus_bool_t
00298 _dbus_credentials_are_anonymous (DBusCredentials    *credentials)
00299 {
00300   return
00301     credentials->unix_uid == DBUS_UID_UNSET &&
00302     credentials->windows_sid == NULL;
00303 }
00304 
00313 dbus_bool_t
00314 _dbus_credentials_add_credentials (DBusCredentials    *credentials,
00315                                    DBusCredentials    *other_credentials)
00316 {
00317   return
00318     _dbus_credentials_add_credential (credentials,
00319                                       DBUS_CREDENTIAL_UNIX_PROCESS_ID,
00320                                       other_credentials) &&
00321     _dbus_credentials_add_credential (credentials,
00322                                       DBUS_CREDENTIAL_UNIX_USER_ID,
00323                                       other_credentials) &&
00324     _dbus_credentials_add_credential (credentials,
00325                                       DBUS_CREDENTIAL_WINDOWS_SID,
00326                                       other_credentials);
00327 }
00328 
00341 dbus_bool_t
00342 _dbus_credentials_add_credential (DBusCredentials    *credentials,
00343                                   DBusCredentialType  which,
00344                                   DBusCredentials    *other_credentials)
00345 {
00346   if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
00347       other_credentials->unix_pid != DBUS_PID_UNSET)
00348     {
00349       if (!_dbus_credentials_add_unix_pid (credentials, other_credentials->unix_pid))
00350         return FALSE;
00351     }
00352   else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
00353            other_credentials->unix_uid != DBUS_UID_UNSET)
00354     {
00355       if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
00356         return FALSE;
00357     }
00358   else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
00359            other_credentials->windows_sid != NULL)
00360     {
00361       if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
00362         return FALSE;
00363     }
00364 
00365   return TRUE;
00366 }
00367 
00373 void
00374 _dbus_credentials_clear (DBusCredentials    *credentials)
00375 {
00376   credentials->unix_pid = DBUS_PID_UNSET;
00377   credentials->unix_uid = DBUS_UID_UNSET;
00378   dbus_free (credentials->windows_sid);
00379   credentials->windows_sid = NULL;
00380 }
00381 
00388 DBusCredentials*
00389 _dbus_credentials_copy (DBusCredentials    *credentials)
00390 {
00391   DBusCredentials *copy;
00392 
00393   copy = _dbus_credentials_new ();
00394   if (copy == NULL)
00395     return NULL;
00396 
00397   if (!_dbus_credentials_add_credentials (copy, credentials))
00398     {
00399       _dbus_credentials_unref (copy);
00400       return NULL;
00401     }
00402 
00403   return copy;
00404 }
00405 
00417 dbus_bool_t
00418 _dbus_credentials_same_user (DBusCredentials    *credentials,
00419                              DBusCredentials    *other_credentials)
00420 {
00421   /* both windows and unix user must be the same (though pretty much
00422    * in all conceivable cases, one will be unset)
00423    */
00424   return credentials->unix_uid == other_credentials->unix_uid &&
00425     ((!(credentials->windows_sid || other_credentials->windows_sid)) ||
00426      (credentials->windows_sid && other_credentials->windows_sid &&
00427       strcmp (credentials->windows_sid, other_credentials->windows_sid) == 0));
00428 }
00429 
00432 /* tests in dbus-credentials-util.c */

Generated on Mon Dec 14 22:26:11 2009 for D-Bus by  doxygen 1.4.7