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