MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / sendmail-8.14 / test / t_seteuid.c
blob9fab8981c0a158e545a2890886bb76c669415929
1 /*
2 * Copyright (c) 1999-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 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.
17 ** NOTE: It is not sufficient to have seteuid in your library.
18 ** You must also have saved uids that function properly.
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.
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <stdio.h>
28 #ifndef lint
29 static char id[] = "@(#)$Id: t_seteuid.c,v 8.8 2001/09/23 03:35:41 ca Exp $";
30 #endif /* ! lint */
32 #ifdef __hpux
33 # define seteuid(e) setresuid(-1, e, -1)
34 #endif /* __hpux */
36 static void
37 printuids(str, r, e)
38 char *str;
39 uid_t r, e;
41 printf("%s (should be %d/%d): r/euid=%d/%d\n", str, (int) r, (int) e,
42 (int) getuid(), (int) geteuid());
45 int
46 main(argc, argv)
47 int argc;
48 char **argv;
50 int fail = 0;
51 uid_t realuid = getuid();
53 printuids("initial uids", realuid, 0);
55 if (geteuid() != 0)
57 printf("SETUP ERROR: re-run set-user-ID root\n");
58 exit(1);
61 if (getuid() == 0)
63 printf("SETUP ERROR: must be run by a non-root user\n");
64 exit(1);
67 if (seteuid(1) < 0)
68 printf("seteuid(1) failure\n");
69 printuids("after seteuid(1)", realuid, 1);
71 if (geteuid() != 1)
73 fail++;
74 printf("MAYDAY! Wrong effective uid\n");
77 /* do activity here */
79 if (seteuid(0) < 0)
81 fail++;
82 printf("seteuid(0) failure\n");
84 printuids("after seteuid(0)", realuid, 0);
86 if (geteuid() != 0)
88 fail++;
89 printf("MAYDAY! Wrong effective uid\n");
91 if (getuid() != realuid)
93 fail++;
94 printf("MAYDAY! Wrong real uid\n");
96 printf("\n");
98 if (seteuid(2) < 0)
100 fail++;
101 printf("seteuid(2) failure\n");
103 printuids("after seteuid(2)", realuid, 2);
105 if (geteuid() != 2)
107 fail++;
108 printf("MAYDAY! Wrong effective uid\n");
111 /* do activity here */
113 if (seteuid(0) < 0)
115 fail++;
116 printf("seteuid(0) failure\n");
118 printuids("after seteuid(0)", realuid, 0);
120 if (geteuid() != 0)
122 fail++;
123 printf("MAYDAY! Wrong effective uid\n");
125 if (getuid() != realuid)
127 fail++;
128 printf("MAYDAY! Wrong real uid\n");
131 if (fail)
133 printf("\nThis system cannot use seteuid\n");
134 exit(1);
137 printf("\nIt is safe to define USESETEUID on this system\n");
138 exit(0);