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