Import sendmail 8.13.7
[dragonfly.git] / contrib / sendmail-8.13.7 / test / t_setuid.c
blob6533339298ae66b0ab8771102109caec84583fc7
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 setuid works.
13 ** Compile it, make it set-user-ID root, and run it as yourself (NOT as
14 ** root).
16 ** NOTE: This should work everywhere, but Linux has the ability
17 ** to use the undocumented setcap() call to make this break.
19 ** Compilation is trivial -- just "cc t_setuid.c". Make it set-user-ID,
20 ** root and then execute it as a non-root user.
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <stdio.h>
27 #ifndef lint
28 static char id[] = "@(#)$Id: t_setuid.c,v 8.7 2001/09/23 03:35:41 ca Exp $";
29 #endif /* ! lint */
31 static void
32 printuids(str, r, e)
33 char *str;
34 uid_t r, e;
36 printf("%s (should be %d/%d): r/euid=%d/%d\n", str, (int) r, (int) e,
37 (int) getuid(), (int) geteuid());
40 int
41 main(argc, argv)
42 int argc;
43 char **argv;
45 int fail = 0;
46 uid_t realuid = getuid();
48 printuids("initial uids", realuid, 0);
50 if (geteuid() != 0)
52 printf("SETUP ERROR: re-run set-user-ID root\n");
53 exit(1);
56 if (getuid() == 0)
58 printf("SETUP ERROR: must be run by a non-root user\n");
59 exit(1);
62 if (setuid(1) < 0)
63 printf("setuid(1) failure\n");
64 printuids("after setuid(1)", 1, 1);
66 if (geteuid() != 1)
68 fail++;
69 printf("MAYDAY! Wrong effective uid\n");
72 if (getuid() != 1)
74 fail++;
75 printf("MAYDAY! Wrong real uid\n");
79 /* do activity here */
80 if (setuid(0) == 0)
82 fail++;
83 printf("MAYDAY! setuid(0) succeeded (should have failed)\n");
85 else
87 printf("setuid(0) failed (this is correct)\n");
89 printuids("after setuid(0)", 1, 1);
91 if (geteuid() != 1)
93 fail++;
94 printf("MAYDAY! Wrong effective uid\n");
96 if (getuid() != 1)
98 fail++;
99 printf("MAYDAY! Wrong real uid\n");
101 printf("\n");
103 if (fail)
105 printf("\nThis system cannot use setuid (maybe use setreuid)\n");
106 exit(1);
109 printf("\nIt is safe to use setuid on this system\n");
110 exit(0);