23RCSID(
"$Id: 1a431f74bce697b00b0ce7c263da395624d09d30 $")
25#include <freeradius-devel/util/perm.h>
26#include <freeradius-devel/util/strerror.h>
27#include <freeradius-devel/util/syserror.h>
38 static char const *rwx[] = {
"---",
"--x",
"-w-",
"-wx",
"r--",
"r-x",
"rw-",
"rwx"};
43 if (mode & S_ISUID)
out[2] = (mode & 0100) ?
's' :
'S';
44 if (mode & S_ISGID)
out[5] = (mode & 0010) ?
's' :
'l';
45 if (mode & S_ISVTX)
out[8] = (mode & 0100) ?
't' :
'T';
53 out[0] =
'0' + ((mode >> 9) & 0x07);
54 out[1] =
'0' + ((mode >> 6) & 0x07);
55 out[2] =
'0' + ((mode >> 3) & 0x07);
56 out[3] =
'0' + (mode & 0x07);
78 if (strlen(str) != 4)
goto fail;
80 value = strtoul(str, &end, 8);
81 if (*end || (
value == ULONG_MAX)) {
111 }
else if (*p ==
'u') {
112 if (p[1] !=
'=')
goto expected_set;
117 }
else if (*p ==
'o') {
118 if (p[1] !=
'=')
goto expected_set;
128 for ( ; *p !=
'\0'; p++) {
155 if (isalpha((
uint8_t) *p))
goto redo;
169 if ((mode &
mask) != 0) {
181 if ((mode & S_IWOTH) != 0) {
219#ifdef _SC_GETPW_R_SIZE_MAX
222 sc_len = sysconf(_SC_GETPW_R_SIZE_MAX);
223 if (sc_len <= 0) sc_len = 1024;
230 buff = talloc_array(ctx,
uint8_t,
sizeof(
struct passwd) + len);
231 if (!
buff)
return -1;
237 while ((ret = getpwuid_r(uid, (
struct passwd *)
buff, (
char *)(
buff +
sizeof(
struct passwd)),
238 talloc_array_length(
buff) -
sizeof(
struct passwd),
out)) == ERANGE) {
239 buff = talloc_realloc_size(ctx,
buff, talloc_array_length(
buff) * 2);
242 if ((ret != 0) || !*
out) {
249 talloc_set_type(
buff,
struct passwd);
282#ifdef _SC_GETPW_R_SIZE_MAX
285 sc_len = sysconf(_SC_GETPW_R_SIZE_MAX);
286 if (sc_len <= 0) sc_len = 1024;
293 buff = talloc_array(ctx,
uint8_t,
sizeof(
struct passwd) + len);
294 if (!
buff)
return -1;
300 while ((ret = getpwnam_r(
name, (
struct passwd *)
buff, (
char *)(
buff +
sizeof(
struct passwd)),
301 talloc_array_length(
buff) -
sizeof(
struct passwd),
out)) == ERANGE) {
302 buff = talloc_realloc_size(ctx,
buff, talloc_array_length(
buff) * 2);
305 if ((ret != 0) || !*
out) {
312 talloc_set_type(
buff,
struct passwd);
345#ifdef _SC_GETGR_R_SIZE_MAX
348 sc_len = sysconf(_SC_GETGR_R_SIZE_MAX);
349 if (sc_len <= 0) sc_len = 1024;
356 buff = talloc_array(ctx,
uint8_t,
sizeof(
struct group) + len);
357 if (!
buff)
return -1;
363 while ((ret = getgrgid_r(gid, (
struct group *)
buff, (
char *)(
buff +
sizeof(
struct group)),
364 talloc_array_length(
buff) -
sizeof(
struct group),
out)) == ERANGE) {
365 buff = talloc_realloc_size(ctx,
buff, talloc_array_length(
buff) * 2);
368 if ((ret != 0) || !*
out) {
375 talloc_set_type(
buff,
struct group);
408#ifdef _SC_GETGR_R_SIZE_MAX
411 sc_len = sysconf(_SC_GETGR_R_SIZE_MAX);
412 if (sc_len <= 0) sc_len = 1024;
419 buff = talloc_array(ctx,
uint8_t,
sizeof(
struct group) + len);
420 if (!
buff)
return -1;
426 while ((ret = getgrnam_r(
name, (
struct group *)
buff, (
char *)(
buff +
sizeof(
struct group)),
427 talloc_array_length(
buff) -
sizeof(
struct group),
out)) == ERANGE) {
428 buff = talloc_realloc_size(ctx,
buff, talloc_array_length(
buff) * 2);
431 if ((ret != 0) || !*
out) {
438 talloc_set_type(
buff,
struct group);
456 struct passwd *result;
459 if (ret < 0)
return -1;
461 *
out = result->pw_uid;
478 struct group *result;
481 if (ret < 0)
return -1;
483 *
out = result->gr_gid;
498 struct passwd *result;
517 struct group *result;
536 struct passwd *user = NULL;
537 struct group *group = NULL;
544 fr_strerror_printf(
"Effective user/group - %s:%s: %s", user->pw_name, group->gr_name, error);
strcpy(log_entry->msg, buffer)
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
int fr_perm_getgrnam(TALLOC_CTX *ctx, struct group **out, char const *name)
Resolve a group name to a group database entry.
char * fr_perm_uid_to_str(TALLOC_CTX *ctx, uid_t uid)
Print uid to a string.
char const * fr_perm_mode_to_str(char out[static 10], mode_t mode)
Convert mode_t into humanly readable permissions flags.
char const * fr_perm_mode_to_oct(char out[static 5], mode_t mode)
int fr_perm_mode_from_str(mode_t *out, char const *str)
char * fr_perm_gid_to_str(TALLOC_CTX *ctx, uid_t gid)
Print gid to a string.
int fr_perm_getgrgid(TALLOC_CTX *ctx, struct group **out, gid_t gid)
Resolve a gid to a group database entry.
int fr_perm_getpwnam(TALLOC_CTX *ctx, struct passwd **out, char const *name)
Resolve a username to a passwd entry.
int fr_perm_uid_from_str(TALLOC_CTX *ctx, uid_t *out, char const *name)
Resolve a user name to a GID.
int fr_perm_getpwuid(TALLOC_CTX *ctx, struct passwd **out, uid_t uid)
Resolve a uid to a passwd entry.
int fr_perm_gid_from_str(TALLOC_CTX *ctx, gid_t *out, char const *name)
Resolve a group name to a GID.
void fr_perm_file_error(int num)
Write a file access error to the fr_strerror buffer, including euid/egid.
static char buff[sizeof("18446744073709551615")+3]
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define talloc_strdup(_ctx, _str)
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
static size_t char ** out