t_seteuid.c revision 64562
1/*
2**  This program checks to see if your version of seteuid 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 USESETEUID in conf.h.
6**
7**	NOTE:  It is not sufficient to have seteuid in your library.
8**	You must also have saved uids that function properly.
9**
10**  Compilation is trivial -- just "cc t_seteuid.c".  Make it setuid,
11**  root and then execute it as a non-root user.
12*/
13
14#include <sys/types.h>
15#include <unistd.h>
16#include <stdio.h>
17
18#ifndef lint
19static char id[] = "@(#)$Id: t_seteuid.c,v 8.4 1999/08/28 00:25:28 gshapiro Exp $";
20#endif /* ! lint */
21
22#ifdef __hpux
23# define seteuid(e)	setresuid(-1, e, -1)
24#endif /* __hpux */
25
26static void
27printuids(str, r, e)
28	char *str;
29	int r, e;
30{
31	printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e,
32		getuid(), geteuid());
33}
34
35int
36main(argc, argv)
37	int argc;
38	char **argv;
39{
40	int fail = 0;
41	uid_t realuid = getuid();
42
43	printuids("initial uids", realuid, 0);
44
45	if (geteuid() != 0)
46	{
47		printf("SETUP ERROR: re-run setuid root\n");
48		exit(1);
49	}
50
51	if (getuid() == 0)
52	{
53		printf("SETUP ERROR: must be run by a non-root user\n");
54		exit(1);
55	}
56
57	if (seteuid(1) < 0)
58		printf("seteuid(1) failure\n");
59	printuids("after seteuid(1)", realuid, 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 (seteuid(0) < 0)
70	{
71		fail++;
72		printf("seteuid(0) failure\n");
73	}
74	printuids("after seteuid(0)", realuid, 0);
75
76	if (geteuid() != 0)
77	{
78		fail++;
79		printf("MAYDAY!  Wrong effective uid\n");
80	}
81	if (getuid() != realuid)
82	{
83		fail++;
84		printf("MAYDAY!  Wrong real uid\n");
85	}
86	printf("\n");
87
88	if (seteuid(2) < 0)
89	{
90		fail++;
91		printf("seteuid(2) failure\n");
92	}
93	printuids("after seteuid(2)", realuid, 2);
94
95	if (geteuid() != 2)
96	{
97		fail++;
98		printf("MAYDAY!  Wrong effective uid\n");
99	}
100
101	/* do activity here */
102
103	if (seteuid(0) < 0)
104	{
105		fail++;
106		printf("seteuid(0) failure\n");
107	}
108	printuids("after seteuid(0)", realuid, 0);
109
110	if (geteuid() != 0)
111	{
112		fail++;
113		printf("MAYDAY!  Wrong effective uid\n");
114	}
115	if (getuid() != realuid)
116	{
117		fail++;
118		printf("MAYDAY!  Wrong real uid\n");
119	}
120
121	if (fail)
122	{
123		printf("\nThis system cannot use seteuid\n");
124		exit(1);
125	}
126
127	printf("\nIt is safe to define USESETEUID on this system\n");
128	exit(0);
129}
130