xref: /freebsd/contrib/sendmail/test/t_seteuid.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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 #ifdef __hpux
19 #define seteuid(e)	setresuid(-1, e, -1)
20 #endif
21 
22 main()
23 {
24 	int fail = 0;
25 	uid_t realuid = getuid();
26 
27 	printuids("initial uids", realuid, 0);
28 
29 	if (geteuid() != 0)
30 	{
31 		printf("SETUP ERROR: re-run setuid root\n");
32 		exit(1);
33 	}
34 
35 	if (getuid() == 0)
36 	{
37 		printf("SETUP ERROR: must be run by a non-root user\n");
38 		exit(1);
39 	}
40 
41 	if (seteuid(1) < 0)
42 		printf("seteuid(1) failure\n");
43 	printuids("after seteuid(1)", realuid, 1);
44 
45 	if (geteuid() != 1)
46 	{
47 		fail++;
48 		printf("MAYDAY!  Wrong effective uid\n");
49 	}
50 
51 	/* do activity here */
52 
53 	if (seteuid(0) < 0)
54 	{
55 		fail++;
56 		printf("seteuid(0) failure\n");
57 	}
58 	printuids("after seteuid(0)", realuid, 0);
59 
60 	if (geteuid() != 0)
61 	{
62 		fail++;
63 		printf("MAYDAY!  Wrong effective uid\n");
64 	}
65 	if (getuid() != realuid)
66 	{
67 		fail++;
68 		printf("MAYDAY!  Wrong real uid\n");
69 	}
70 	printf("\n");
71 
72 	if (seteuid(2) < 0)
73 	{
74 		fail++;
75 		printf("seteuid(2) failure\n");
76 	}
77 	printuids("after seteuid(2)", realuid, 2);
78 
79 	if (geteuid() != 2)
80 	{
81 		fail++;
82 		printf("MAYDAY!  Wrong effective uid\n");
83 	}
84 
85 	/* do activity here */
86 
87 	if (seteuid(0) < 0)
88 	{
89 		fail++;
90 		printf("seteuid(0) failure\n");
91 	}
92 	printuids("after seteuid(0)", realuid, 0);
93 
94 	if (geteuid() != 0)
95 	{
96 		fail++;
97 		printf("MAYDAY!  Wrong effective uid\n");
98 	}
99 	if (getuid() != realuid)
100 	{
101 		fail++;
102 		printf("MAYDAY!  Wrong real uid\n");
103 	}
104 
105 	if (fail)
106 	{
107 		printf("\nThis system cannot use seteuid\n");
108 		exit(1);
109 	}
110 
111 	printf("\nIt is safe to define USESETEUID on this system\n");
112 	exit(0);
113 }
114 
115 printuids(str, r, e)
116 	char *str;
117 	int r, e;
118 {
119 	printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e,
120 		getuid(), geteuid());
121 }
122