xref: /freebsd/contrib/sendmail/test/t_seteuid.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 
28 #ifndef lint
29 static char id[] = "@(#)$Id: t_seteuid.c,v 8.9 2013-11-22 20:52:01 ca Exp $";
30 #endif /* ! lint */
31 
32 #ifdef __hpux
33 # define seteuid(e)	setresuid(-1, e, -1)
34 #endif /* __hpux */
35 
36 static void
37 printuids(str, r, e)
38 	char *str;
39 	uid_t r, e;
40 {
41 	printf("%s (should be %d/%d): r/euid=%d/%d\n", str, (int) r, (int) e,
42 	       (int) getuid(), (int) geteuid());
43 }
44 
45 int
46 main(argc, argv)
47 	int argc;
48 	char **argv;
49 {
50 	int fail = 0;
51 	uid_t realuid = getuid();
52 
53 	printuids("initial uids", realuid, 0);
54 
55 	if (geteuid() != 0)
56 	{
57 		printf("SETUP ERROR: re-run set-user-ID root\n");
58 		exit(1);
59 	}
60 
61 	if (getuid() == 0)
62 	{
63 		printf("SETUP ERROR: must be run by a non-root user\n");
64 		exit(1);
65 	}
66 
67 	if (seteuid(1) < 0)
68 		printf("seteuid(1) failure\n");
69 	printuids("after seteuid(1)", realuid, 1);
70 
71 	if (geteuid() != 1)
72 	{
73 		fail++;
74 		printf("MAYDAY!  Wrong effective uid\n");
75 	}
76 
77 	/* do activity here */
78 
79 	if (seteuid(0) < 0)
80 	{
81 		fail++;
82 		printf("seteuid(0) failure\n");
83 	}
84 	printuids("after seteuid(0)", realuid, 0);
85 
86 	if (geteuid() != 0)
87 	{
88 		fail++;
89 		printf("MAYDAY!  Wrong effective uid\n");
90 	}
91 	if (getuid() != realuid)
92 	{
93 		fail++;
94 		printf("MAYDAY!  Wrong real uid\n");
95 	}
96 	printf("\n");
97 
98 	if (seteuid(2) < 0)
99 	{
100 		fail++;
101 		printf("seteuid(2) failure\n");
102 	}
103 	printuids("after seteuid(2)", realuid, 2);
104 
105 	if (geteuid() != 2)
106 	{
107 		fail++;
108 		printf("MAYDAY!  Wrong effective uid\n");
109 	}
110 
111 	/* do activity here */
112 
113 	if (seteuid(0) < 0)
114 	{
115 		fail++;
116 		printf("seteuid(0) failure\n");
117 	}
118 	printuids("after seteuid(0)", realuid, 0);
119 
120 	if (geteuid() != 0)
121 	{
122 		fail++;
123 		printf("MAYDAY!  Wrong effective uid\n");
124 	}
125 	if (getuid() != realuid)
126 	{
127 		fail++;
128 		printf("MAYDAY!  Wrong real uid\n");
129 	}
130 
131 	if (fail)
132 	{
133 		printf("\nThis system cannot use seteuid\n");
134 		exit(1);
135 	}
136 
137 	printf("\nIt is safe to define USESETEUID on this system\n");
138 	exit(0);
139 }
140