All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions | Variables
util.c File Reference
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/rad_assert.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
+ Include dependency graph for util.c:

Go to the source code of this file.

Macros

#define USEC   1000000
 

Functions

char * rad_ajoin (TALLOC_CTX *ctx, char const **argv, int argc, char c)
 talloc a buffer to hold the concatenated value of all elements of argv More...
 
char * rad_asprint_gid (TALLOC_CTX *ctx, uid_t gid)
 Print gid to a string. More...
 
char * rad_asprint_uid (TALLOC_CTX *ctx, uid_t uid)
 Print uid to a string. More...
 
void NEVER_RETURNS rad_assert_fail (char const *file, unsigned int line, char const *expr)
 
void rad_const_free (void const *ptr)
 
int rad_copy_string (char *to, char const *from)
 
int rad_copy_string_bare (char *to, char const *from)
 
int rad_copy_variable (char *to, char const *from)
 
char const * rad_default_lib_dir (void)
 Return the default lib dir. More...
 
char const * rad_default_log_dir (void)
 Return the default log dir. More...
 
char const * rad_default_raddb_dir (void)
 Return the default raddb dir. More...
 
char const * rad_default_run_dir (void)
 Return the default run dir. More...
 
char const * rad_default_sbin_dir (void)
 Return the default sbin dir. More...
 
int rad_expand_xlat (REQUEST *request, char const *cmd, int max_argc, char const *argv[], bool can_fail, size_t argv_buflen, char *argv_buf)
 Split string into words and expand each one. More...
 
void rad_file_error (int num)
 Write a file access error to the fr_strerror buffer, including euid/egid. More...
 
size_t rad_filename_escape (UNUSED REQUEST *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
 Escapes the raw string such that it should be safe to use as part of a file path. More...
 
size_t rad_filename_make_safe (UNUSED REQUEST *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
 Ensures that a filename cannot walk up the directory structure. More...
 
ssize_t rad_filename_unescape (char *out, size_t outlen, char const *in, size_t inlen)
 Converts data stored in a file name back to its original form. More...
 
int rad_getgid (TALLOC_CTX *ctx, gid_t *out, char const *name)
 Resolve a group name to a GID. More...
 
int rad_getgrgid (TALLOC_CTX *ctx, struct group **out, gid_t gid)
 Resolve a gid to a group database entry. More...
 
int rad_getgrnam (TALLOC_CTX *ctx, struct group **out, char const *name)
 Resolve a group name to a group database entry. More...
 
int rad_getpwnam (TALLOC_CTX *ctx, struct passwd **out, char const *name)
 Resolve a username to a passwd entry. More...
 
int rad_getpwuid (TALLOC_CTX *ctx, struct passwd **out, uid_t uid)
 Resolve a uid to a passwd entry. More...
 
void * rad_malloc (size_t size)
 
int rad_mkdir (char *dir, mode_t mode, uid_t uid, gid_t gid)
 Create possibly many directories. More...
 
void rad_mode_to_oct (char out[5], mode_t mode)
 
void rad_mode_to_str (char out[10], mode_t mode)
 Convert mode_t into humanly readable permissions flags. More...
 
uint32_t rad_pps (uint32_t *past, uint32_t *present, time_t *then, struct timeval *now)
 
char const * rad_radacct_dir (void)
 Return the default radacct dir. More...
 
int rad_segid (gid_t gid)
 Alter the effective user id. More...
 
int rad_seuid (uid_t uid)
 Alter the effective user id. More...
 
void rad_suid_down (void)
 
void rad_suid_down_permanent (void)
 
void rad_suid_set_down_uid (uid_t uid)
 
void rad_suid_up (void)
 
static void verify_packet (char const *file, int line, REQUEST *request, RADIUS_PACKET *packet, char const *type)
 
void verify_request (char const *file, int line, REQUEST *request)
 

Variables

void(*)(int) reset_signal (int signo, void(*func)(int))
 

Macro Definition Documentation

#define USEC   1000000

Definition at line 605 of file util.c.

Function Documentation

char* rad_ajoin ( TALLOC_CTX *  ctx,
char const **  argv,
int  argc,
char  c 
)

talloc a buffer to hold the concatenated value of all elements of argv

Parameters
ctxto allocate buffer in.
argvarray of substrings.
argclength of array.
cseparation character. Optional, may be '\0' for no separator.
Returns
the concatenation of the elements of argv, separated by c.

Definition at line 441 of file util.c.

+ Here is the call graph for this function:

char* rad_asprint_gid ( TALLOC_CTX *  ctx,
uid_t  gid 
)

Print gid to a string.

Parameters
ctxTALLOC_CTX for temporary allocations.
gidto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1289 of file util.c.

+ Here is the call graph for this function:

char* rad_asprint_uid ( TALLOC_CTX *  ctx,
uid_t  uid 
)

Print uid to a string.

Parameters
ctxTALLOC_CTX for temporary allocations.
uidto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1269 of file util.c.

+ Here is the call graph for this function:

void NEVER_RETURNS rad_assert_fail ( char const *  file,
unsigned int  line,
char const *  expr 
)

Definition at line 481 of file util.c.

+ Here is the call graph for this function:

void rad_const_free ( void const *  ptr)

Definition at line 424 of file util.c.

+ Here is the caller graph for this function:

int rad_copy_string ( char *  to,
char const *  from 
)

Definition at line 491 of file util.c.

+ Here is the caller graph for this function:

int rad_copy_string_bare ( char *  to,
char const *  from 
)

Definition at line 519 of file util.c.

+ Here is the caller graph for this function:

int rad_copy_variable ( char *  to,
char const *  from 
)

Definition at line 545 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char const* rad_default_lib_dir ( void  )

Return the default lib dir.

This is set at build time from –prefix

Returns
the value of LIBDIR

Definition at line 823 of file util.c.

char const* rad_default_log_dir ( void  )

Return the default log dir.

This is set at build time from –prefix

Returns
the value of LOGDIR

Definition at line 813 of file util.c.

char const* rad_default_raddb_dir ( void  )

Return the default raddb dir.

This is set at build time from –prefix

Returns
the value of RADDBDIR

Definition at line 833 of file util.c.

char const* rad_default_run_dir ( void  )

Return the default run dir.

This is set at build time from –prefix

Returns
the value of RUNDIR

Definition at line 843 of file util.c.

char const* rad_default_sbin_dir ( void  )

Return the default sbin dir.

This is set at build time from –prefix

Returns
the value of SBINDIR

Definition at line 853 of file util.c.

int rad_expand_xlat ( REQUEST request,
char const *  cmd,
int  max_argc,
char const *  argv[],
bool  can_fail,
size_t  argv_buflen,
char *  argv_buf 
)

Split string into words and expand each one.

Parameters
requestCurrent request.
cmdstring to split.
max_argcthe maximum number of arguments to split into.
argvWhere to write the pointers into argv_buf.
can_failIf false, stop processing if any of the xlat expansions fail.
argv_buflensize of argv_buf.
argv_buftemporary buffer we used to mangle/expand cmd. Pointers to offsets of this buffer will be written to argv.
Returns
argc or -1 on failure.

Definition at line 658 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void rad_file_error ( int  num)

Write a file access error to the fr_strerror buffer, including euid/egid.

Note
retrieve error with fr_strerror()
Parameters
numUsually num, unless the error is returned by the function.

Definition at line 1306 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t rad_filename_escape ( UNUSED REQUEST request,
char *  out,
size_t  outlen,
char const *  in,
UNUSED void *  arg 
)

Escapes the raw string such that it should be safe to use as part of a file path.

This function is designed to produce a string that's still readable but portable across the majority of file systems.

For security reasons it cannot remove characters from the name, and must not allow collisions to occur between different strings.

With that in mind '-' has been chosen as the escape character, and will be double escaped '-' -> '–' to avoid collisions.

Escaping should be reversible if the original string needs to be extracted.

Note
function takes additional arguments so that it may be used as an xlat escape function but it's fine to call it directly.
OSX/Unix/NTFS/VFAT have a max filename size of 255 bytes.
Parameters
requestCurrent request (may be NULL).
outOutput buffer.
outlenSize of the output buffer.
instring to escape.
argContext arguments (unused, should be NULL).

Definition at line 269 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

size_t rad_filename_make_safe ( UNUSED REQUEST request,
char *  out,
size_t  outlen,
char const *  in,
UNUSED void *  arg 
)

Ensures that a filename cannot walk up the directory structure.

Also sanitizes control chars.

Parameters
requestCurrent request (may be NULL).
outOutput buffer.
outlenSize of the output buffer.
instring to escape.
argContext arguments (unused, should be NULL).

Definition at line 175 of file util.c.

+ Here is the caller graph for this function:

ssize_t rad_filename_unescape ( char *  out,
size_t  outlen,
char const *  in,
size_t  inlen 
)

Converts data stored in a file name back to its original form.

Parameters
outWhere to write the unescaped string (may be the same as in).
outlenLength of the output buffer.
inInput filename.
inlenLength of input.
Returns
  • Number of bytes written to output buffer
  • offset where parse error occurred on failure.

Definition at line 354 of file util.c.

+ Here is the call graph for this function:

int rad_getgid ( TALLOC_CTX *  ctx,
gid_t *  out,
char const *  name 
)

Resolve a group name to a GID.

Parameters
ctxTALLOC_CTX for temporary allocations.
nameof group.
outwhere to write gid.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1248 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int rad_getgrgid ( TALLOC_CTX *  ctx,
struct group **  out,
gid_t  gid 
)

Resolve a gid to a group database entry.

Resolves a gid to a group database entry. The memory to hold the group entry is talloced under ctx, and must be freed when no longer required.

Parameters
ctxto allocate passwd entry in.
outWhere to write pointer to entry.
gidto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1118 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int rad_getgrnam ( TALLOC_CTX *  ctx,
struct group **  out,
char const *  name 
)

Resolve a group name to a group database entry.

Resolves a group name to a group database entry. The memory to hold the group entry is talloced under ctx, and must be freed when no longer required.

Parameters
ctxto allocate passwd entry in.
outWhere to write pointer to entry.
nameto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1185 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int rad_getpwnam ( TALLOC_CTX *  ctx,
struct passwd **  out,
char const *  name 
)

Resolve a username to a passwd entry.

Resolves a username to a passwd entry. The memory to hold the passwd entry is talloced under ctx, and must be freed when no longer required.

Parameters
ctxto allocate passwd entry in.
outWhere to write pointer to entry.
nameto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1051 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int rad_getpwuid ( TALLOC_CTX *  ctx,
struct passwd **  out,
uid_t  uid 
)

Resolve a uid to a passwd entry.

Resolves a uid to a passwd entry. The memory to hold the passwd entry is talloced under ctx, and must be freed when no longer required.

Parameters
ctxto allocate passwd entry in.
outWhere to write pointer to entry.
uidto resolve.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 984 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void* rad_malloc ( size_t  size)

Definition at line 411 of file util.c.

+ Here is the caller graph for this function:

int rad_mkdir ( char *  dir,
mode_t  mode,
uid_t  uid,
gid_t  gid 
)

Create possibly many directories.

Note
that the input directory name is NOT treated as a constant. This is so that if an error is returned, the 'directory' ptr points to the name of the file which caused the error.
Parameters
dirpath to directory to create.
modefor new directories.
uidto set on new directories, may be -1 to use effective uid.
gidto set on new directories, may be -1 to use effective gid.
Returns
  • 0 on success.
  • -1 on failure. Error available as errno.

Definition at line 90 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void rad_mode_to_oct ( char  out[5],
mode_t  mode 
)

Definition at line 962 of file util.c.

void rad_mode_to_str ( char  out[10],
mode_t  mode 
)

Convert mode_t into humanly readable permissions flags.

Author
Jonathan Leffler.
Parameters
modeto convert.
outWhere to write the string to, must be exactly 10 bytes long.

Definition at line 949 of file util.c.

uint32_t rad_pps ( uint32_t *  past,
uint32_t *  present,
time_t *  then,
struct timeval *  now 
)

Definition at line 608 of file util.c.

+ Here is the caller graph for this function:

char const* rad_radacct_dir ( void  )

Return the default radacct dir.

This is set at build time from –prefix

Returns
the value of RADIR

Definition at line 863 of file util.c.

int rad_segid ( gid_t  gid)

Alter the effective user id.

Parameters
gidto set
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1512 of file util.c.

+ Here is the call graph for this function:

int rad_seuid ( uid_t  uid)

Alter the effective user id.

Parameters
uidto set
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1491 of file util.c.

+ Here is the call graph for this function:

void rad_suid_down ( void  )

Definition at line 1474 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void rad_suid_down_permanent ( void  )

Definition at line 1478 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void rad_suid_set_down_uid ( uid_t  uid)

Definition at line 1468 of file util.c.

void rad_suid_up ( void  )

Definition at line 1471 of file util.c.

+ Here is the caller graph for this function:

static void verify_packet ( char const *  file,
int  line,
REQUEST request,
RADIUS_PACKET packet,
char const *  type 
)
static

Definition at line 872 of file util.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void verify_request ( char const *  file,
int  line,
REQUEST request 
)

Definition at line 905 of file util.c.

+ Here is the call graph for this function:

Variable Documentation

void(*)(int) reset_signal(int signo, void(*func)(int))

Definition at line 45 of file util.c.