Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / test / t_setreuid.c
blobb307b0803924583ff7487872bbc1bb44211b95cb
1 /*
2 * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
9 */
12 ** This program checks to see if your version of setreuid 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 HASSETREUID in conf.h.
17 ** Compilation is trivial -- just "cc t_setreuid.c". Make it set-user-ID,
18 ** root and then execute it as a non-root user.
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <stdio.h>
25 #ifndef lint
26 static char id[] = "@(#)$Id: t_setreuid.c,v 8.9 2001/10/12 03:04:46 gshapiro Exp $";
27 #endif /* ! lint */
29 #ifdef __hpux
30 # define setreuid(r, e) setresuid(r, e, -1)
31 #endif /* __hpux */
33 static void
34 printuids(str, r, e)
35 char *str;
36 uid_t r, e;
38 printf("%s (should be %d/%d): r/euid=%d/%d\n", str, (int) r, (int) e,
39 (int) getuid(), (int) geteuid());
42 int
43 main(argc, argv)
44 int argc;
45 char **argv;
47 int fail = 0;
48 uid_t realuid = getuid();
50 printuids("initial uids", realuid, 0);
52 if (geteuid() != 0)
54 printf("SETUP ERROR: re-run set-user-ID root\n");
55 exit(1);
58 if (getuid() == 0)
60 printf("SETUP ERROR: must be run by a non-root user\n");
61 exit(1);
64 if (setreuid(0, 1) < 0)
66 fail++;
67 printf("setreuid(0, 1) failure\n");
69 printuids("after setreuid(0, 1)", 0, 1);
71 if (getuid() != 0)
73 fail++;
74 printf("MAYDAY! Wrong real uid\n");
77 if (geteuid() != 1)
79 fail++;
80 printf("MAYDAY! Wrong effective uid\n");
83 /* do activity here */
85 if (setreuid(-1, 0) < 0)
87 fail++;
88 printf("setreuid(-1, 0) failure\n");
90 printuids("after setreuid(-1, 0)", 0, 0);
91 if (setreuid(realuid, 0) < 0)
93 fail++;
94 printf("setreuid(%d, 0) failure\n", (int) realuid);
96 printuids("after setreuid(realuid, 0)", realuid, 0);
98 if (geteuid() != 0)
100 fail++;
101 printf("MAYDAY! Wrong effective uid\n");
103 if (getuid() != realuid)
105 fail++;
106 printf("MAYDAY! Wrong real uid\n");
108 printf("\n");
110 if (setreuid(0, 2) < 0)
112 fail++;
113 printf("setreuid(0, 2) failure\n");
115 printuids("after setreuid(0, 2)", 0, 2);
117 if (geteuid() != 2)
119 fail++;
120 printf("MAYDAY! Wrong effective uid\n");
123 if (getuid() != 0)
125 fail++;
126 printf("MAYDAY! Wrong real uid\n");
129 /* do activity here */
131 if (setreuid(-1, 0) < 0)
133 fail++;
134 printf("setreuid(-1, 0) failure\n");
136 printuids("after setreuid(-1, 0)", 0, 0);
137 if (setreuid(realuid, 0) < 0)
139 fail++;
140 printf("setreuid(%d, 0) failure\n", (int) realuid);
142 printuids("after setreuid(realuid, 0)", realuid, 0);
144 if (geteuid() != 0)
146 fail++;
147 printf("MAYDAY! Wrong effective uid\n");
149 if (getuid() != realuid)
151 fail++;
152 printf("MAYDAY! Wrong real uid\n");
155 if (fail)
157 printf("\nThis system cannot use setreuid\n");
158 exit(1);
161 printf("\nIt is safe to define HASSETREUID on this system\n");
162 exit(0);