2007-07-13 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include <config.h>
24 #include <string.h>
25 #include "dbus-credentials.h"
26 #include "dbus-internals.h"
27
28 /**
29  * @defgroup DBusCredentials Credentials provable through authentication
30  * @ingroup  DBusInternals
31  * @brief DBusCredentials object
32  *
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.
36  */
37
38 /**
39  * @defgroup DBusCredentialsInternals Credentials implementation details
40  * @ingroup  DBusInternals
41  * @brief DBusCredentials implementation details
42  *
43  * Private details of credentials code.
44  *
45  * @{
46  */
47
48 struct DBusCredentials {
49   int refcount;
50   dbus_uid_t unix_uid;
51   dbus_pid_t unix_pid;
52   char *windows_sid;
53 };
54
55 /** @} */
56
57 /**
58  * @addtogroup DBusCredentials
59  * @{
60  */
61
62 /**
63  * Creates a new credentials object.
64  *
65  * @returns the new object or #NULL if no memory
66  */
67 DBusCredentials*
68 _dbus_credentials_new (void)
69 {
70   DBusCredentials *creds;
71
72   creds = dbus_new (DBusCredentials, 1);
73   if (creds == NULL)
74     return NULL;
75   
76   creds->refcount = 1;
77   creds->unix_uid = DBUS_UID_UNSET;
78   creds->unix_pid = DBUS_PID_UNSET;
79   creds->windows_sid = NULL;
80
81   return creds;
82 }
83
84 /**
85  * Creates a new object with credentials (user ID and process ID) from the current process.
86  * @returns the new object or #NULL if no memory
87  */
88 DBusCredentials*
89 _dbus_credentials_new_from_current_process (void)
90 {
91   DBusCredentials *creds;
92
93   creds = _dbus_credentials_new ();
94   if (creds == NULL)
95     return NULL;
96
97   if (!_dbus_credentials_add_from_current_process (creds))
98     {
99       _dbus_credentials_unref (creds);
100       return NULL;
101     }
102   
103   return creds;
104 }
105
106 /**
107  * Increment refcount on credentials.
108  *
109  * @param credentials the object
110  */
111 void
112 _dbus_credentials_ref (DBusCredentials *credentials)
113 {
114   _dbus_assert (credentials->refcount > 0);
115   credentials->refcount += 1;
116 }
117
118 /**
119  * Decrement refcount on credentials.
120  *
121  * @param credentials the object
122  */
123 void
124 _dbus_credentials_unref (DBusCredentials    *credentials)
125 {
126   _dbus_assert (credentials->refcount > 0);
127
128   credentials->refcount -= 1;
129   if (credentials->refcount == 0)
130     {
131       dbus_free (credentials->windows_sid);
132       dbus_free (credentials);
133     }
134 }
135
136 /**
137  * Add a UNIX process ID to the credentials.
138  *
139  * @param credentials the object
140  * @param pid the process ID
141  * @returns #FALSE if no memory
142  */
143 dbus_bool_t
144 _dbus_credentials_add_unix_pid (DBusCredentials    *credentials,
145                                 dbus_pid_t          pid)
146 {
147   credentials->unix_pid = pid;
148   return TRUE;
149 }
150
151 /**
152  * Add a UNIX user ID to the credentials.
153  *
154  * @param credentials the object
155  * @param uid the user ID
156  * @returns #FALSE if no memory
157  */
158 dbus_bool_t
159 _dbus_credentials_add_unix_uid(DBusCredentials    *credentials,
160                                dbus_uid_t          uid)
161 {
162   credentials->unix_uid = uid;
163   return TRUE;
164
165 }
166
167 /**
168  * Add a Windows user SID to the credentials.
169  *
170  * @param credentials the object
171  * @param windows_sid the user SID
172  * @returns #FALSE if no memory
173  */
174 dbus_bool_t
175 _dbus_credentials_add_windows_sid (DBusCredentials    *credentials,
176                                    const char         *windows_sid)
177 {
178   char *copy;
179
180   copy = _dbus_strdup (windows_sid);
181   if (copy == NULL)
182     return FALSE;
183
184   dbus_free (credentials->windows_sid);
185   credentials->windows_sid = copy;
186
187   return TRUE;
188 }
189
190 /**
191  * Checks whether the given credential is present.
192  *
193  * @param credentials the object
194  * @param type the credential to check for
195  * @returns #TRUE if the credential is present
196  */
197 dbus_bool_t
198 _dbus_credentials_include (DBusCredentials    *credentials,
199                            DBusCredentialType  type)
200 {
201   switch (type)
202     {
203     case DBUS_CREDENTIAL_UNIX_PROCESS_ID:
204       return credentials->unix_pid != DBUS_PID_UNSET;
205     case DBUS_CREDENTIAL_UNIX_USER_ID:
206       return credentials->unix_uid != DBUS_UID_UNSET;
207     case DBUS_CREDENTIAL_WINDOWS_SID:
208       return credentials->windows_sid != NULL;
209     }
210
211   _dbus_assert_not_reached ("Unknown credential enum value");
212   return FALSE;
213 }
214
215 /**
216  * Gets the UNIX process ID in the credentials, or #DBUS_PID_UNSET if
217  * the credentials object doesn't contain a process ID.
218  *
219  * @param credentials the object
220  * @returns UNIX process ID
221  */
222 dbus_pid_t
223 _dbus_credentials_get_unix_pid (DBusCredentials    *credentials)
224 {
225   return credentials->unix_pid;
226 }
227
228 /**
229  * Gets the UNIX user ID in the credentials, or #DBUS_UID_UNSET if
230  * the credentials object doesn't contain a user ID.
231  *
232  * @param credentials the object
233  * @returns UNIX user ID
234  */
235 dbus_uid_t
236 _dbus_credentials_get_unix_uid (DBusCredentials    *credentials)
237 {
238   return credentials->unix_uid;
239 }
240
241 /**
242  * Gets the Windows user SID in the credentials, or #NULL if
243  * the credentials object doesn't contain a Windows user SID.
244  *
245  * @param credentials the object
246  * @returns Windows user SID
247  */
248 const char*
249 _dbus_credentials_get_windows_sid (DBusCredentials    *credentials)
250 {
251   return credentials->windows_sid;
252 }
253
254 /**
255  * Checks whether the first credentials object contains
256  * all the credentials found in the second credentials object.
257  *
258  * @param credentials the object
259  * @param possible_subset see if credentials in here are also in the first arg
260  * @returns #TRUE if second arg is contained in first
261  */
262 dbus_bool_t
263 _dbus_credentials_are_superset (DBusCredentials    *credentials,
264                                 DBusCredentials    *possible_subset)
265 {
266   return
267     (possible_subset->unix_pid == DBUS_PID_UNSET ||
268      possible_subset->unix_pid == credentials->unix_pid) &&
269     (possible_subset->unix_uid == DBUS_UID_UNSET ||
270      possible_subset->unix_uid == credentials->unix_uid) &&
271     (possible_subset->windows_sid == NULL ||
272      (credentials->windows_sid && strcmp (possible_subset->windows_sid,
273                                           credentials->windows_sid) == 0));
274 }
275
276 /**
277  * Checks whether a credentials object contains anything.
278  * 
279  * @param credentials the object
280  * @returns #TRUE if there are no credentials in the object
281  */
282 dbus_bool_t
283 _dbus_credentials_are_empty (DBusCredentials    *credentials)
284 {
285   return
286     credentials->unix_pid == DBUS_PID_UNSET &&
287     credentials->unix_uid == DBUS_UID_UNSET &&
288     credentials->windows_sid == NULL;
289 }
290
291 /**
292  * Checks whether a credentials object contains a user identity.
293  * 
294  * @param credentials the object
295  * @returns #TRUE if there are no user identities in the object
296  */
297 dbus_bool_t
298 _dbus_credentials_are_anonymous (DBusCredentials    *credentials)
299 {
300   return
301     credentials->unix_uid == DBUS_UID_UNSET &&
302     credentials->windows_sid == NULL;
303 }
304
305 /**
306  * Merge all credentials found in the second object into the first object,
307  * overwriting the first object if there are any overlaps.
308  * 
309  * @param credentials the object
310  * @param other_credentials credentials to merge
311  * @returns #FALSE if no memory
312  */
313 dbus_bool_t
314 _dbus_credentials_add_credentials (DBusCredentials    *credentials,
315                                    DBusCredentials    *other_credentials)
316 {
317   return
318     _dbus_credentials_add_credential (credentials,
319                                       DBUS_CREDENTIAL_UNIX_PROCESS_ID,
320                                       other_credentials) &&
321     _dbus_credentials_add_credential (credentials,
322                                       DBUS_CREDENTIAL_UNIX_USER_ID,
323                                       other_credentials) &&
324     _dbus_credentials_add_credential (credentials,
325                                       DBUS_CREDENTIAL_WINDOWS_SID,
326                                       other_credentials);
327 }
328
329 /**
330  * Merge the given credential found in the second object into the first object,
331  * overwriting the first object's value for that credential.
332  *
333  * Does nothing if the second object does not contain the specified credential.
334  * i.e., will never delete a credential from the first object.
335  * 
336  * @param credentials the object
337  * @param which the credential to overwrite
338  * @param other_credentials credentials to merge
339  * @returns #FALSE if no memory
340  */
341 dbus_bool_t
342 _dbus_credentials_add_credential (DBusCredentials    *credentials,
343                                   DBusCredentialType  which,
344                                   DBusCredentials    *other_credentials)
345 {
346   if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
347       other_credentials->unix_pid != DBUS_PID_UNSET)
348     {
349       if (!_dbus_credentials_add_unix_pid (credentials, other_credentials->unix_pid))
350         return FALSE;
351     }
352   else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
353            other_credentials->unix_uid != DBUS_UID_UNSET)
354     {
355       if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
356         return FALSE;
357     }
358   else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
359            other_credentials->windows_sid != NULL)
360     {
361       if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
362         return FALSE;
363     }
364
365   return TRUE;
366 }
367
368 /**
369  * Clear all credentials in the object.
370  * 
371  * @param credentials the object
372  */
373 void
374 _dbus_credentials_clear (DBusCredentials    *credentials)
375 {
376   credentials->unix_pid = DBUS_PID_UNSET;
377   credentials->unix_uid = DBUS_UID_UNSET;
378   dbus_free (credentials->windows_sid);
379   credentials->windows_sid = NULL;
380 }
381
382 /**
383  * Copy a credentials object.
384  * 
385  * @param credentials the object
386  * @returns the copy or #NULL
387  */
388 DBusCredentials*
389 _dbus_credentials_copy (DBusCredentials    *credentials)
390 {
391   DBusCredentials *copy;
392
393   copy = _dbus_credentials_new ();
394   if (copy == NULL)
395     return NULL;
396
397   if (!_dbus_credentials_add_credentials (copy, credentials))
398     {
399       _dbus_credentials_unref (copy);
400       return NULL;
401     }
402
403   return copy;
404 }
405
406 /**
407  * Check whether the user-identifying credentials in two credentials
408  * objects are identical. Credentials that are not related to the
409  * user are ignored, but any kind of user ID credentials must be the
410  * same (UNIX user ID, Windows user SID, etc.) and present in both
411  * objects for the function to return #TRUE.
412  * 
413  * @param credentials the object
414  * @param other_credentials credentials to compare
415  * @returns #TRUE if the two credentials refer to the same user
416  */
417 dbus_bool_t
418 _dbus_credentials_same_user (DBusCredentials    *credentials,
419                              DBusCredentials    *other_credentials)
420 {
421   /* both windows and unix user must be the same (though pretty much
422    * in all conceivable cases, one will be unset)
423    */
424   return credentials->unix_uid == other_credentials->unix_uid &&
425     ((!(credentials->windows_sid || other_credentials->windows_sid)) ||
426      (credentials->windows_sid && other_credentials->windows_sid &&
427       strcmp (credentials->windows_sid, other_credentials->windows_sid) == 0));
428 }
429
430 /** @} */
431
432 /* tests in dbus-credentials-util.c */