xref: /freebsd/contrib/sendmail/test/t_setreuid.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2 **  This program checks to see if your version of setreuid works.
3 **  Compile it, make it setuid root, and run it as yourself (NOT as
4 **  root).  If it won't compile or outputs any MAYDAY messages, don't
5 **  define HASSETREUID in conf.h.
6 **
7 **  Compilation is trivial -- just "cc t_setreuid.c".  Make it setuid,
8 **  root and then execute it as a non-root user.
9 */
10 
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 
15 #ifndef lint
16 static char id[] = "@(#)$Id: t_setreuid.c,v 8.4 1999/08/28 00:25:28 gshapiro Exp $";
17 #endif /* ! lint */
18 
19 #ifdef __hpux
20 # define setreuid(r, e)	setresuid(r, e, -1)
21 #endif /* __hpux */
22 
23 static void
24 printuids(str, r, e)
25 	char *str;
26 	int r, e;
27 {
28 	printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e,
29 		getuid(), geteuid());
30 }
31 
32 int
33 main(argc, argv)
34 	int argc;
35 	char **argv;
36 {
37 	int fail = 0;
38 	uid_t realuid = getuid();
39 
40 	printuids("initial uids", realuid, 0);
41 
42 	if (geteuid() != 0)
43 	{
44 		printf("SETUP ERROR: re-run setuid root\n");
45 		exit(1);
46 	}
47 
48 	if (getuid() == 0)
49 	{
50 		printf("SETUP ERROR: must be run by a non-root user\n");
51 		exit(1);
52 	}
53 
54 	if (setreuid(0, 1) < 0)
55 	{
56 		fail++;
57 		printf("setreuid(0, 1) failure\n");
58 	}
59 	printuids("after setreuid(0, 1)", 0, 1);
60 
61 	if (geteuid() != 1)
62 	{
63 		fail++;
64 		printf("MAYDAY!  Wrong effective uid\n");
65 	}
66 
67 	/* do activity here */
68 
69 	if (setreuid(-1, 0) < 0)
70 	{
71 		fail++;
72 		printf("setreuid(-1, 0) failure\n");
73 	}
74 	printuids("after setreuid(-1, 0)", 0, 0);
75 	if (setreuid(realuid, 0) < 0)
76 	{
77 		fail++;
78 		printf("setreuid(%d, 0) failure\n", realuid);
79 	}
80 	printuids("after setreuid(realuid, 0)", realuid, 0);
81 
82 	if (geteuid() != 0)
83 	{
84 		fail++;
85 		printf("MAYDAY!  Wrong effective uid\n");
86 	}
87 	if (getuid() != realuid)
88 	{
89 		fail++;
90 		printf("MAYDAY!  Wrong real uid\n");
91 	}
92 	printf("\n");
93 
94 	if (setreuid(0, 2) < 0)
95 	{
96 		fail++;
97 		printf("setreuid(0, 2) failure\n");
98 	}
99 	printuids("after setreuid(0, 2)", 0, 2);
100 
101 	if (geteuid() != 2)
102 	{
103 		fail++;
104 		printf("MAYDAY!  Wrong effective uid\n");
105 	}
106 
107 	/* do activity here */
108 
109 	if (setreuid(-1, 0) < 0)
110 	{
111 		fail++;
112 		printf("setreuid(-1, 0) failure\n");
113 	}
114 	printuids("after setreuid(-1, 0)", 0, 0);
115 	if (setreuid(realuid, 0) < 0)
116 	{
117 		fail++;
118 		printf("setreuid(%d, 0) failure\n", realuid);
119 	}
120 	printuids("after setreuid(realuid, 0)", realuid, 0);
121 
122 	if (geteuid() != 0)
123 	{
124 		fail++;
125 		printf("MAYDAY!  Wrong effective uid\n");
126 	}
127 	if (getuid() != realuid)
128 	{
129 		fail++;
130 		printf("MAYDAY!  Wrong real uid\n");
131 	}
132 
133 	if (fail)
134 	{
135 		printf("\nThis system cannot use setreuid\n");
136 		exit(1);
137 	}
138 
139 	printf("\nIt is safe to define HASSETREUID on this system\n");
140 	exit(0);
141 }
142