Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
userdb.c File Reference

Access the POSIX user database. More...

#include "lib/fs/userdb.h"
#include "lib/malloc/malloc.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include <pwd.h>
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Macros

#define tor_passwd_free(pw)    FREE_AND_NULL(struct passwd, tor_passwd_free_, (pw))
 

Functions

static struct passwd * tor_passwd_dup (const struct passwd *pw)
 
static void tor_passwd_free_ (struct passwd *pw)
 
const struct passwd * tor_getpwnam (const char *username)
 
const struct passwd * tor_getpwuid (uid_t uid)
 
char * get_user_homedir (const char *username)
 

Variables

static struct passwd * passwd_cached = NULL
 

Detailed Description

Access the POSIX user database.

Definition in file userdb.c.

Function Documentation

◆ get_user_homedir()

char* get_user_homedir ( const char *  username)

Allocate and return a string containing the home directory for the user username. Only works on posix-like systems.

Definition at line 127 of file userdb.c.

◆ tor_getpwnam()

const struct passwd* tor_getpwnam ( const char *  username)

Wrapper around getpwnam() that caches result. Used so that we don't need to give the sandbox access to /etc/passwd.

The following fields alone will definitely be copied in the output: pw_uid, pw_gid, pw_name, pw_dir. Other fields are not present in cached values.

When called with a NULL argument, this function clears storage associated with static variables it uses.

Definition at line 70 of file userdb.c.

Referenced by get_user_homedir().

◆ tor_getpwuid()

const struct passwd* tor_getpwuid ( uid_t  uid)

Wrapper around getpwnam() that can use cached result from tor_getpwnam(). Used so that we don't need to give the sandbox access to /etc/passwd.

The following fields alone will definitely be copied in the output: pw_uid, pw_gid, pw_name, pw_dir. Other fields are not present in cached values.

Definition at line 106 of file userdb.c.

◆ tor_passwd_dup()

static struct passwd* tor_passwd_dup ( const struct passwd *  pw)
static

Helper: copy a struct passwd object.

We only copy the fields pw_uid, pw_gid, pw_name, pw_dir. Tor doesn't use any others, and I don't want to run into incompatibilities.

Definition at line 32 of file userdb.c.

◆ tor_passwd_free_()

static void tor_passwd_free_ ( struct passwd *  pw)
static

Helper: free one of our cached 'struct passwd' values.

Definition at line 50 of file userdb.c.

Variable Documentation

◆ passwd_cached

struct passwd* passwd_cached = NULL
static

Cached struct from the last getpwname() call we did successfully.

Definition at line 24 of file userdb.c.

Referenced by tor_getpwuid().