2007-06-09 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-credentials.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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  * Merge all credentials found in the second object into the first object,
293  * overwriting the first object if there are any overlaps.
294  * 
295  * @param credentials the object
296  * @param other_credentials credentials to merge
297  * @returns #FALSE if no memory
298  */
299 dbus_bool_t
300 _dbus_credentials_add_credentials (DBusCredentials    *credentials,
301                                    DBusCredentials    *other_credentials)
302 {
303   return
304     _dbus_credentials_add_credential (credentials,
305                                       DBUS_CREDENTIAL_UNIX_PROCESS_ID,
306                                       other_credentials) &&
307     _dbus_credentials_add_credential (credentials,
308                                       DBUS_CREDENTIAL_UNIX_USER_ID,
309                                       other_credentials) &&
310     _dbus_credentials_add_credential (credentials,
311                                       DBUS_CREDENTIAL_WINDOWS_SID,
312                                       other_credentials);
313 }
314
315 /**
316  * Merge the given credential found in the second object into the first object,
317  * overwriting the first object's value for that credential.
318  *
319  * Does nothing if the second object does not contain the specified credential.
320  * i.e., will never delete a credential from the first object.
321  * 
322  * @param credentials the object
323  * @param which the credential to overwrite
324  * @param other_credentials credentials to merge
325  * @returns #FALSE if no memory
326  */
327 dbus_bool_t
328 _dbus_credentials_add_credential (DBusCredentials    *credentials,
329                                   DBusCredentialType  which,
330                                   DBusCredentials    *other_credentials)
331 {
332   if (which == DBUS_CREDENTIAL_UNIX_PROCESS_ID &&
333       other_credentials->unix_pid != DBUS_PID_UNSET)
334     {
335       if (!_dbus_credentials_add_unix_pid (credentials, other_credentials->unix_pid))
336         return FALSE;
337     }
338   else if (which == DBUS_CREDENTIAL_UNIX_USER_ID &&
339            other_credentials->unix_uid != DBUS_UID_UNSET)
340     {
341       if (!_dbus_credentials_add_unix_uid (credentials, other_credentials->unix_uid))
342         return FALSE;
343     }
344   else if (which == DBUS_CREDENTIAL_WINDOWS_SID &&
345            other_credentials->windows_sid != NULL)
346     {
347       if (!_dbus_credentials_add_windows_sid (credentials, other_credentials->windows_sid))
348         return FALSE;
349     }
350
351   return TRUE;
352 }
353
354 /**
355  * Clear all credentials in the object.
356  * 
357  * @param credentials the object
358  */
359 void
360 _dbus_credentials_clear (DBusCredentials    *credentials)
361 {
362   credentials->unix_pid = DBUS_PID_UNSET;
363   credentials->unix_uid = DBUS_UID_UNSET;
364   dbus_free (credentials->windows_sid);
365   credentials->windows_sid = NULL;
366 }
367
368 /**
369  * Copy a credentials object.
370  * 
371  * @param credentials the object
372  * @returns the copy or #NULL
373  */
374 DBusCredentials*
375 _dbus_credentials_copy (DBusCredentials    *credentials)
376 {
377   DBusCredentials *copy;
378
379   copy = _dbus_credentials_new ();
380   if (copy == NULL)
381     return NULL;
382
383   if (!_dbus_credentials_add_credentials (copy, credentials))
384     {
385       _dbus_credentials_unref (copy);
386       return NULL;
387     }
388
389   return copy;
390 }
391
392 /**
393  * Check whether the user-identifying credentials in two credentials
394  * objects are identical. Credentials that are not related to the
395  * user are ignored, but any kind of user ID credentials must be the
396  * same (UNIX user ID, Windows user SID, etc.) and present in both
397  * objects for the function to return #TRUE.
398  * 
399  * @param credentials the object
400  * @param other_credentials credentials to compare
401  * @returns #TRUE if the two credentials refer to the same user
402  */
403 dbus_bool_t
404 _dbus_credentials_same_user (DBusCredentials    *credentials,
405                              DBusCredentials    *other_credentials)
406 {
407   /* both windows and unix user must be the same (though pretty much
408    * in all conceivable cases, one will be unset)
409    */
410   return credentials->unix_uid == other_credentials->unix_uid &&
411     ((!(credentials->windows_sid || other_credentials->windows_sid)) ||
412      (credentials->windows_sid && other_credentials->windows_sid &&
413       strcmp (credentials->windows_sid, other_credentials->windows_sid) == 0));
414 }
415
416 /** @} */
417
418 /* tests in dbus-credentials-util.c */