1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions
3*1b191cb5SApple OSS Distributions #include <TargetConditionals.h>
4*1b191cb5SApple OSS Distributions #include <limits.h>
5*1b191cb5SApple OSS Distributions #include <stdio.h>
6*1b191cb5SApple OSS Distributions #include <stdlib.h>
7*1b191cb5SApple OSS Distributions #include <string.h>
8*1b191cb5SApple OSS Distributions #include <sys/errno.h>
9*1b191cb5SApple OSS Distributions #include <unistd.h>
10*1b191cb5SApple OSS Distributions
11*1b191cb5SApple OSS Distributions #if !TARGET_OS_OSX
12*1b191cb5SApple OSS Distributions #include <pwd.h>
13*1b191cb5SApple OSS Distributions #include <sys/types.h>
14*1b191cb5SApple OSS Distributions #include <uuid/uuid.h>
15*1b191cb5SApple OSS Distributions #endif
16*1b191cb5SApple OSS Distributions
17*1b191cb5SApple OSS Distributions #include "drop_priv.h"
18*1b191cb5SApple OSS Distributions
19*1b191cb5SApple OSS Distributions #if TARGET_OS_OSX
20*1b191cb5SApple OSS Distributions #define INVOKER_UID "SUDO_UID"
21*1b191cb5SApple OSS Distributions #define INVOKER_GID "SUDO_GID"
22*1b191cb5SApple OSS Distributions #define ID_MAX (unsigned long)UINT_MAX
23*1b191cb5SApple OSS Distributions static unsigned
_get_sudo_invoker(const char * var)24*1b191cb5SApple OSS Distributions _get_sudo_invoker(const char *var)
25*1b191cb5SApple OSS Distributions {
26*1b191cb5SApple OSS Distributions char *value_str = getenv(var);
27*1b191cb5SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(value_str,
28*1b191cb5SApple OSS Distributions "Not running under sudo, getenv(\"%s\") failed", var);
29*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_NE_CHAR(*value_str, '\0',
30*1b191cb5SApple OSS Distributions "getenv(\"%s\") returned an empty string", var);
31*1b191cb5SApple OSS Distributions
32*1b191cb5SApple OSS Distributions char *endp;
33*1b191cb5SApple OSS Distributions unsigned long value = strtoul(value_str, &endp, 10);
34*1b191cb5SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_EQ_CHAR(*endp, '\0',
35*1b191cb5SApple OSS Distributions "strtoul(\"%s\") not called on a valid number", value_str);
36*1b191cb5SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NE_ULONG(value, ULONG_MAX,
37*1b191cb5SApple OSS Distributions "strtoul(\"%s\") overflow", value_str);
38*1b191cb5SApple OSS Distributions
39*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_NE_ULONG(value, 0ul, "%s invalid", var);
40*1b191cb5SApple OSS Distributions T_QUIET; T_ASSERT_LT_ULONG(value, ID_MAX, "%s invalid", var);
41*1b191cb5SApple OSS Distributions return (unsigned)value;
42*1b191cb5SApple OSS Distributions }
43*1b191cb5SApple OSS Distributions #endif /* TARGET_OS_OSX */
44*1b191cb5SApple OSS Distributions
45*1b191cb5SApple OSS Distributions void
drop_priv(void)46*1b191cb5SApple OSS Distributions drop_priv(void)
47*1b191cb5SApple OSS Distributions {
48*1b191cb5SApple OSS Distributions #if TARGET_OS_OSX
49*1b191cb5SApple OSS Distributions uid_t lower_uid = _get_sudo_invoker(INVOKER_UID);
50*1b191cb5SApple OSS Distributions gid_t lower_gid = _get_sudo_invoker(INVOKER_GID);
51*1b191cb5SApple OSS Distributions #else
52*1b191cb5SApple OSS Distributions struct passwd *pw = getpwnam("mobile");
53*1b191cb5SApple OSS Distributions T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(pw, "getpwnam(\"mobile\")");
54*1b191cb5SApple OSS Distributions uid_t lower_uid = pw->pw_uid;
55*1b191cb5SApple OSS Distributions gid_t lower_gid = pw->pw_gid;
56*1b191cb5SApple OSS Distributions #endif
57*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setgid(lower_gid), "Change group to %u", lower_gid);
58*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setuid(lower_uid), "Change user to %u", lower_uid);
59*1b191cb5SApple OSS Distributions }
60*1b191cb5SApple OSS Distributions
61*1b191cb5SApple OSS Distributions bool
running_as_root(void)62*1b191cb5SApple OSS Distributions running_as_root(void)
63*1b191cb5SApple OSS Distributions {
64*1b191cb5SApple OSS Distributions return geteuid() == 0;
65*1b191cb5SApple OSS Distributions }
66