1993-10-30 03:36:16 +03:00
|
|
|
/*
|
|
|
|
** This program checks to see if your version of setreuid works.
|
|
|
|
** Compile it, make it setuid root, and run it as yourself (NOT as
|
|
|
|
** root). If it won't compile or outputs any MAYDAY messages, don't
|
|
|
|
** define HASSETREUID in conf.h.
|
|
|
|
**
|
1996-01-22 13:52:39 +03:00
|
|
|
** Compilation is trivial -- just "cc t_setreuid.c". Make it setuid,
|
|
|
|
** root and then execute it as a non-root user.
|
1993-10-30 03:36:16 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef __hpux
|
|
|
|
#define setreuid(r, e) setresuid(r, e, -1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
1996-01-22 13:52:39 +03:00
|
|
|
int fail = 0;
|
1993-10-30 03:36:16 +03:00
|
|
|
uid_t realuid = getuid();
|
|
|
|
|
|
|
|
printuids("initial uids", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
1996-01-22 13:52:39 +03:00
|
|
|
printf("SETUP ERROR: re-run setuid root\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
|
|
|
printf("SETUP ERROR: must be run by a non-root user\n");
|
1993-10-30 03:36:16 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setreuid(0, 1) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(0, 1) failure\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(0, 1)", 0, 1);
|
|
|
|
|
|
|
|
if (geteuid() != 1)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
|
|
|
|
/* do activity here */
|
|
|
|
|
|
|
|
if (setreuid(-1, 0) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(-1, 0) failure\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(-1, 0)", 0, 0);
|
|
|
|
if (setreuid(realuid, 0) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(%d, 0) failure\n", realuid);
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(realuid, 0)", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
if (getuid() != realuid)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (setreuid(0, 2) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(0, 2) failure\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(0, 2)", 0, 2);
|
|
|
|
|
|
|
|
if (geteuid() != 2)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
|
|
|
|
/* do activity here */
|
|
|
|
|
|
|
|
if (setreuid(-1, 0) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(-1, 0) failure\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(-1, 0)", 0, 0);
|
|
|
|
if (setreuid(realuid, 0) < 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("setreuid(%d, 0) failure\n", realuid);
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
printuids("after setreuid(realuid, 0)", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
if (getuid() != realuid)
|
1996-01-22 13:52:39 +03:00
|
|
|
{
|
|
|
|
fail++;
|
1993-10-30 03:36:16 +03:00
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
1996-01-22 13:52:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
|
|
|
printf("\nThis system cannot use setreuid\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1993-10-30 03:36:16 +03:00
|
|
|
|
1996-11-18 08:29:14 +03:00
|
|
|
printf("\nIt is safe to define HASSETREUID on this system\n");
|
1993-10-30 03:36:16 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printuids(str, r, e)
|
|
|
|
char *str;
|
|
|
|
int r, e;
|
|
|
|
{
|
|
|
|
printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e,
|
|
|
|
getuid(), geteuid());
|
|
|
|
}
|