index.html
Main Page
|
modules.html
Modules
|
namespaces.html
Namespace List
|
hierarchy.html
Class Hierarchy
|
annotated.html
Data Structures
|
dirs.html
Directories
|
files.html
File List
|
namespacemembers.html
Namespace Members
|
functions.html
Data Fields
|
pages.html
Related Pages
dir_000004.html
dbus
dbus-userdb.c
00001
/* -*- mode: C; c-file-style: "gnu" -*- */
00002
/* dbus-userdb.c User database abstraction
00003
*
00004
* Copyright (C) 2003, 2004  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
#define DBUS_USERDB_INCLUDES_PRIVATE 1
00024
#include "dbus-userdb.h"
00025
#include "dbus-hash.h"
00026
#include "dbus-test.h"
00027
#include "dbus-internals.h"
00028
#include "dbus-protocol.h"
00029
#include <string.h>
00030
00042
void
group__DBusInternalsUtils.html#ga113
00043
group__DBusInternalsUtils.html#ga113
_dbus_user_info_free_allocated
(
structDBusUserInfo.html
DBusUserInfo
*info)
00044 {
00045
if
(info ==
group__DBusMacros.html#ga4
NULL
)
/* hash table will pass NULL */
00046
return
;
00047
00048
group__DBusInternalsUtils.html#ga73
_dbus_user_info_free
(info);
00049
group__DBusMemory.html#ga3
dbus_free
(info);
00050 }
00051
00058
void
group__DBusInternalsUtils.html#ga114
00059
group__DBusInternalsUtils.html#ga114
_dbus_group_info_free_allocated
(
structDBusGroupInfo.html
DBusGroupInfo
*info)
00060 {
00061
if
(info ==
group__DBusMacros.html#ga4
NULL
)
/* hash table will pass NULL */
00062
return
;
00063
00064
group__DBusInternalsUtils.html#ga114
_dbus_group_info_free_allocated
(info);
00065
group__DBusMemory.html#ga3
dbus_free
(info);
00066 }
00067
00076
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga115
00077
group__DBusInternalsUtils.html#ga115
_dbus_is_a_number
(
const
structDBusString.html
DBusString
*str,
00078
unsigned
long
*num)
00079 {
00080
int
end;
00081
00082
if
(
group__DBusString.html#ga62
_dbus_string_parse_int
(str, 0, num, &end) &&
00083       end == _dbus_string_get_length (str))
00084
return
group__DBusMacros.html#ga2
TRUE
;
00085
else
00086
return
group__DBusMacros.html#ga3
FALSE
;
00087 }
00088
00101
structDBusUserInfo.html
DBusUserInfo
*
group__DBusInternalsUtils.html#ga116
00102
group__DBusInternalsUtils.html#ga116
_dbus_user_database_lookup
(DBusUserDatabase *db,
00103                             dbus_uid_t        uid,
00104
const
structDBusString.html
DBusString
*username,
00105
structDBusError.html
DBusError
*error)
00106 {
00107
structDBusUserInfo.html
DBusUserInfo
*info;
00108
00109   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00110
group__DBusInternalsUtils.html#ga130
_dbus_assert
(uid != DBUS_UID_UNSET || username !=
group__DBusMacros.html#ga4
NULL
);
00111
00112
/* See if the username is really a number */
00113
if
(uid == DBUS_UID_UNSET)
00114     {
00115
unsigned
long
n;
00116
00117
if
(
group__DBusInternalsUtils.html#ga115
_dbus_is_a_number
(username, &n))
00118         uid = n;
00119     }
00120
00121
if
(uid != DBUS_UID_UNSET)
00122     info =
group__DBusHashTable.html#ga18
_dbus_hash_table_lookup_ulong
(db->users, uid);
00123
else
00124     info =
group__DBusHashTable.html#ga16
_dbus_hash_table_lookup_string
(db->users_by_name, _dbus_string_get_const_data (username));
00125
00126
if
(info)
00127     {
00128       _dbus_verbose (
"Using cache for UID "
DBUS_UID_FORMAT
" information\n"
,
00129                      info->
structDBusUserInfo.html#o0
uid
);
00130
return
info;
00131     }
00132
else
00133     {
00134
if
(uid != DBUS_UID_UNSET)
00135         _dbus_verbose (
"No cache for UID "
DBUS_UID_FORMAT
"\n"
,
00136                        uid);
00137
else
00138         _dbus_verbose (
"No cache for user \"%s\"\n"
,
00139                        _dbus_string_get_const_data (username));
00140
00141       info =
group__DBusMemory.html#ga7
dbus_new0
(
structDBusUserInfo.html
DBusUserInfo
, 1);
00142
if
(info ==
group__DBusMacros.html#ga4
NULL
)
00143         {
00144
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00145
return
group__DBusMacros.html#ga4
NULL
;
00146         }
00147
00148
if
(uid != DBUS_UID_UNSET)
00149         {
00150
if
(!
group__DBusInternalsUtils.html#ga72
_dbus_user_info_fill_uid
(info, uid, error))
00151             {
00152               _DBUS_ASSERT_ERROR_IS_SET (error);
00153
group__DBusInternalsUtils.html#ga113
_dbus_user_info_free_allocated
(info);
00154
return
group__DBusMacros.html#ga4
NULL
;
00155             }
00156         }
00157
else
00158         {
00159
if
(!
group__DBusInternalsUtils.html#ga71
_dbus_user_info_fill
(info, username, error))
00160             {
00161               _DBUS_ASSERT_ERROR_IS_SET (error);
00162
group__DBusInternalsUtils.html#ga113
_dbus_user_info_free_allocated
(info);
00163
return
group__DBusMacros.html#ga4
NULL
;
00164             }
00165         }
00166
00167
/* be sure we don't use these after here */
00168       uid = DBUS_UID_UNSET;
00169       username =
group__DBusMacros.html#ga4
NULL
;
00170
00171
/* insert into hash */
00172
if
(!
group__DBusHashTable.html#ga24
_dbus_hash_table_insert_ulong
(db->users, info->
structDBusUserInfo.html#o0
uid
, info))
00173         {
00174
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00175
group__DBusInternalsUtils.html#ga113
_dbus_user_info_free_allocated
(info);
00176
return
group__DBusMacros.html#ga4
NULL
;
00177         }
00178
00179
if
(!
group__DBusHashTable.html#ga22
_dbus_hash_table_insert_string
(db->users_by_name,
00180                                            info->
structDBusUserInfo.html#o4
username
,
00181                                            info))
00182         {
00183
group__DBusHashTable.html#ga21
_dbus_hash_table_remove_ulong
(db->users, info->
structDBusUserInfo.html#o0
uid
);
00184
group__DBusErrors.html#ga6
dbus_set_error
(error, DBUS_ERROR_NO_MEMORY,
group__DBusMacros.html#ga4
NULL
);
00185
return
group__DBusMacros.html#ga4
NULL
;
00186         }
00187
00188
return
info;
00189     }
00190 }
00191
00192
group__DBusInternalsUtils.html#ga146
_DBUS_DEFINE_GLOBAL_LOCK
(system_users);
00193
static
group__DBusTypes.html#ga2
dbus_bool_t
database_locked =
group__DBusMacros.html#ga3
FALSE
;
00194
static
DBusUserDatabase *system_db =
group__DBusMacros.html#ga4
NULL
;
00195
static
structDBusString.html
DBusString
process_username;
00196
static
structDBusString.html
DBusString
process_homedir;
00197
00198
static
void
00199 shutdown_system_db (
void
*data)
00200 {
00201
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(system_db);
00202   system_db =
group__DBusMacros.html#ga4
NULL
;
00203
group__DBusString.html#ga9
_dbus_string_free
(&process_username);
00204
group__DBusString.html#ga9
_dbus_string_free
(&process_homedir);
00205 }
00206
00207
static
group__DBusTypes.html#ga2
dbus_bool_t
00208 init_system_db (
void
)
00209 {
00210
group__DBusInternalsUtils.html#ga130
_dbus_assert
(database_locked);
00211
00212
if
(system_db ==
group__DBusMacros.html#ga4
NULL
)
00213     {
00214
structDBusError.html
DBusError
error;
00215
const
structDBusUserInfo.html
DBusUserInfo
*info;
00216
00217       system_db =
group__DBusInternalsUtils.html#ga126
_dbus_user_database_new
();
00218
if
(system_db ==
group__DBusMacros.html#ga4
NULL
)
00219
return
group__DBusMacros.html#ga3
FALSE
;
00220
00221
group__DBusErrors.html#ga0
dbus_error_init
(&error);
00222
00223
if
(!
group__DBusInternalsUtils.html#ga128
_dbus_user_database_get_uid
(system_db,
00224
group__DBusInternalsUtils.html#ga78
_dbus_getuid
(),
00225                                         &info,
00226                                         &error))
00227         {
00228
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(system_db);
00229           system_db =
group__DBusMacros.html#ga4
NULL
;
00230
00231
if
(
group__DBusErrors.html#ga4
dbus_error_has_name
(&error, DBUS_ERROR_NO_MEMORY))
00232             {
00233
group__DBusErrors.html#ga1
dbus_error_free
(&error);
00234
return
group__DBusMacros.html#ga3
FALSE
;
00235             }
00236
else
00237             {
00238
/* This really should not happen. */
00239
group__DBusInternalsUtils.html#ga7
_dbus_warn
(
"Could not get password database information for UID of current process: %s\n"
,
00240                           error.message);
00241
group__DBusErrors.html#ga1
dbus_error_free
(&error);
00242
return
group__DBusMacros.html#ga3
FALSE
;
00243             }
00244         }
00245
00246
if
(!
group__DBusString.html#ga6
_dbus_string_init
(&process_username))
00247         {
00248
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(system_db);
00249           system_db =
group__DBusMacros.html#ga4
NULL
;
00250
return
group__DBusMacros.html#ga3
FALSE
;
00251         }
00252
00253
if
(!
group__DBusString.html#ga6
_dbus_string_init
(&process_homedir))
00254         {
00255
group__DBusString.html#ga9
_dbus_string_free
(&process_username);
00256
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(system_db);
00257           system_db =
group__DBusMacros.html#ga4
NULL
;
00258
return
group__DBusMacros.html#ga3
FALSE
;
00259         }
00260
00261
if
(!
group__DBusString.html#ga27
_dbus_string_append
(&process_username,
00262                                 info->
structDBusUserInfo.html#o4
username
) ||
00263           !
group__DBusString.html#ga27
_dbus_string_append
(&process_homedir,
00264                                 info->
structDBusUserInfo.html#o5
homedir
) ||
00265           !
group__DBusMemoryInternals.html#ga3
_dbus_register_shutdown_func
(shutdown_system_db,
group__DBusMacros.html#ga4
NULL
))
00266         {
00267
group__DBusString.html#ga9
_dbus_string_free
(&process_username);
00268
group__DBusString.html#ga9
_dbus_string_free
(&process_homedir);
00269
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(system_db);
00270           system_db =
group__DBusMacros.html#ga4
NULL
;
00271
return
group__DBusMacros.html#ga3
FALSE
;
00272         }
00273     }
00274
00275
return
group__DBusMacros.html#ga2
TRUE
;
00276 }
00277
00281
void
group__DBusInternalsUtils.html#ga119
00282
group__DBusInternalsUtils.html#ga119
_dbus_user_database_lock_system
(
void
)
00283 {
00284
group__DBusInternalsUtils.html#ga148
_DBUS_LOCK
(system_users);
00285   database_locked =
group__DBusMacros.html#ga2
TRUE
;
00286 }
00287
00291
void
group__DBusInternalsUtils.html#ga120
00292
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
(
void
)
00293 {
00294   database_locked =
group__DBusMacros.html#ga3
FALSE
;
00295
group__DBusInternalsUtils.html#ga149
_DBUS_UNLOCK
(system_users);
00296 }
00297
00304 DBusUserDatabase*
group__DBusInternalsUtils.html#ga121
00305
group__DBusInternalsUtils.html#ga121
_dbus_user_database_get_system
(
void
)
00306 {
00307
group__DBusInternalsUtils.html#ga130
_dbus_assert
(database_locked);
00308
00309   init_system_db ();
00310
00311
return
system_db;
00312 }
00313
00321
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga122
00322
group__DBusInternalsUtils.html#ga122
_dbus_username_from_current_process
(
const
structDBusString.html
DBusString
**username)
00323 {
00324
group__DBusInternalsUtils.html#ga119
_dbus_user_database_lock_system
();
00325
if
(!init_system_db ())
00326     {
00327
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00328
return
group__DBusMacros.html#ga3
FALSE
;
00329     }
00330   *username = &process_username;
00331
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00332
00333
return
group__DBusMacros.html#ga2
TRUE
;
00334 }
00335
00343
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga123
00344
group__DBusInternalsUtils.html#ga123
_dbus_homedir_from_current_process
(
const
structDBusString.html
DBusString
**homedir)
00345 {
00346
group__DBusInternalsUtils.html#ga119
_dbus_user_database_lock_system
();
00347
if
(!init_system_db ())
00348     {
00349
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00350
return
group__DBusMacros.html#ga3
FALSE
;
00351     }
00352   *homedir = &process_homedir;
00353
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00354
00355
return
group__DBusMacros.html#ga2
TRUE
;
00356 }
00357
00365
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga124
00366
group__DBusInternalsUtils.html#ga124
_dbus_homedir_from_username
(
const
structDBusString.html
DBusString
*username,
00367
structDBusString.html
DBusString
*homedir)
00368 {
00369   DBusUserDatabase *db;
00370
const
structDBusUserInfo.html
DBusUserInfo
*info;
00371
group__DBusInternalsUtils.html#ga119
_dbus_user_database_lock_system
();
00372
00373   db =
group__DBusInternalsUtils.html#ga121
_dbus_user_database_get_system
();
00374
if
(db ==
group__DBusMacros.html#ga4
NULL
)
00375     {
00376
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00377
return
group__DBusMacros.html#ga3
FALSE
;
00378     }
00379
00380
if
(!
group__DBusInternalsUtils.html#ga129
_dbus_user_database_get_username
(db, username,
00381                                          &info,
group__DBusMacros.html#ga4
NULL
))
00382     {
00383
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00384
return
group__DBusMacros.html#ga3
FALSE
;
00385     }
00386
00387
if
(!
group__DBusString.html#ga27
_dbus_string_append
(homedir, info->
structDBusUserInfo.html#o5
homedir
))
00388     {
00389
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00390
return
group__DBusMacros.html#ga3
FALSE
;
00391     }
00392
00393
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00394
return
group__DBusMacros.html#ga2
TRUE
;
00395 }
00396
00404
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga125
00405
group__DBusInternalsUtils.html#ga125
_dbus_credentials_from_username
(
const
structDBusString.html
DBusString
*username,
00406
structDBusCredentials.html
DBusCredentials
*
structDBusTransport.html#o5
credentials
)
00407 {
00408   DBusUserDatabase *db;
00409
const
structDBusUserInfo.html
DBusUserInfo
*info;
00410
group__DBusInternalsUtils.html#ga119
_dbus_user_database_lock_system
();
00411
00412   db =
group__DBusInternalsUtils.html#ga121
_dbus_user_database_get_system
();
00413
if
(db ==
group__DBusMacros.html#ga4
NULL
)
00414     {
00415
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00416
return
group__DBusMacros.html#ga3
FALSE
;
00417     }
00418
00419
if
(!
group__DBusInternalsUtils.html#ga129
_dbus_user_database_get_username
(db, username,
00420                                          &info,
group__DBusMacros.html#ga4
NULL
))
00421     {
00422
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00423
return
group__DBusMacros.html#ga3
FALSE
;
00424     }
00425
00426   credentials->
structDBusCredentials.html#o0
pid
= DBUS_PID_UNSET;
00427   credentials->
structDBusCredentials.html#o1
uid
= info->
structDBusUserInfo.html#o0
uid
;
00428   credentials->
structDBusCredentials.html#o2
gid
= info->
structDBusUserInfo.html#o1
primary_gid
;
00429
00430
group__DBusInternalsUtils.html#ga120
_dbus_user_database_unlock_system
();
00431
return
group__DBusMacros.html#ga2
TRUE
;
00432 }
00433
00439 DBusUserDatabase*
group__DBusInternalsUtils.html#ga126
00440
group__DBusInternalsUtils.html#ga126
_dbus_user_database_new
(
void
)
00441 {
00442   DBusUserDatabase *db;
00443
00444   db =
group__DBusMemory.html#ga7
dbus_new0
(DBusUserDatabase, 1);
00445
if
(db ==
group__DBusMacros.html#ga4
NULL
)
00446
return
group__DBusMacros.html#ga4
NULL
;
00447
00448   db->refcount = 1;
00449
00450   db->users =
group__DBusHashTable.html#ga1
_dbus_hash_table_new
(DBUS_HASH_ULONG,
00451
group__DBusMacros.html#ga4
NULL
, (
group__DBusMemory.html#ga8
DBusFreeFunction
)
group__DBusInternalsUtils.html#ga113
_dbus_user_info_free_allocated
);
00452
00453
if
(db->users ==
group__DBusMacros.html#ga4
NULL
)
00454
goto
failed;
00455
00456   db->groups =
group__DBusHashTable.html#ga1
_dbus_hash_table_new
(DBUS_HASH_ULONG,
00457
group__DBusMacros.html#ga4
NULL
, (
group__DBusMemory.html#ga8
DBusFreeFunction
)
group__DBusInternalsUtils.html#ga114
_dbus_group_info_free_allocated
);
00458
00459
if
(db->groups ==
group__DBusMacros.html#ga4
NULL
)
00460
goto
failed;
00461
00462   db->users_by_name =
group__DBusHashTable.html#ga1
_dbus_hash_table_new
(DBUS_HASH_STRING,
00463
group__DBusMacros.html#ga4
NULL
,
group__DBusMacros.html#ga4
NULL
);
00464
if
(db->users_by_name ==
group__DBusMacros.html#ga4
NULL
)
00465
goto
failed;
00466
00467   db->groups_by_name =
group__DBusHashTable.html#ga1
_dbus_hash_table_new
(DBUS_HASH_STRING,
00468
group__DBusMacros.html#ga4
NULL
,
group__DBusMacros.html#ga4
NULL
);
00469
if
(db->groups_by_name ==
group__DBusMacros.html#ga4
NULL
)
00470
goto
failed;
00471
00472
return
db;
00473
00474  failed:
00475
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(db);
00476
return
group__DBusMacros.html#ga4
NULL
;
00477 }
00478
00479
#ifdef DBUS_BUILD_TESTS
00480
00485 DBusUserDatabase *
00486 _dbus_user_database_ref (DBusUserDatabase  *db)
00487 {
00488
group__DBusInternalsUtils.html#ga130
_dbus_assert
(db->refcount > 0);
00489
00490   db->refcount += 1;
00491
00492
return
db;
00493 }
00494
#endif
/* DBUS_BUILD_TESTS */
00495
00500
void
group__DBusInternalsUtils.html#ga127
00501
group__DBusInternalsUtils.html#ga127
_dbus_user_database_unref
(DBusUserDatabase  *db)
00502 {
00503
group__DBusInternalsUtils.html#ga130
_dbus_assert
(db->refcount > 0);
00504
00505   db->refcount -= 1;
00506
if
(db->refcount == 0)
00507     {
00508
if
(db->users)
00509
group__DBusHashTable.html#ga3
_dbus_hash_table_unref
(db->users);
00510
00511
if
(db->groups)
00512
group__DBusHashTable.html#ga3
_dbus_hash_table_unref
(db->groups);
00513
00514
if
(db->users_by_name)
00515
group__DBusHashTable.html#ga3
_dbus_hash_table_unref
(db->users_by_name);
00516
00517
if
(db->groups_by_name)
00518
group__DBusHashTable.html#ga3
_dbus_hash_table_unref
(db->groups_by_name);
00519
00520
group__DBusMemory.html#ga3
dbus_free
(db);
00521     }
00522 }
00523
00534
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga128
00535
group__DBusInternalsUtils.html#ga128
_dbus_user_database_get_uid
(DBusUserDatabase    *db,
00536                              dbus_uid_t           uid,
00537
const
structDBusUserInfo.html
DBusUserInfo
**info,
00538
structDBusError.html
DBusError
*error)
00539 {
00540   *info =
group__DBusInternalsUtils.html#ga116
_dbus_user_database_lookup
(db, uid,
group__DBusMacros.html#ga4
NULL
, error);
00541
return
*info !=
group__DBusMacros.html#ga4
NULL
;
00542 }
00543
00553
group__DBusTypes.html#ga2
dbus_bool_t
group__DBusInternalsUtils.html#ga129
00554
group__DBusInternalsUtils.html#ga129
_dbus_user_database_get_username
(DBusUserDatabase     *db,
00555
const
structDBusString.html
DBusString
*username,
00556
const
structDBusUserInfo.html
DBusUserInfo
**info,
00557
structDBusError.html
DBusError
*error)
00558 {
00559   *info =
group__DBusInternalsUtils.html#ga116
_dbus_user_database_lookup
(db, DBUS_UID_UNSET, username, error);
00560
return
*info !=
group__DBusMacros.html#ga4
NULL
;
00561 }
00562
00565
/* Tests in dbus-userdb-util.c */
Generated on Tue Sep 13 01:28:08 2005 for D-BUS by
http://www.doxygen.org/index.html
doxygen
1.4.4
