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