2002-02-17 21:56:45 +00:00
|
|
|
/*
|
2014-01-26 20:46:55 +00:00
|
|
|
* Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
|
2002-02-17 21:56:45 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* By using this file, you agree to the terms and conditions set
|
|
|
|
* forth in the LICENSE file which can be found at the top level of
|
|
|
|
* the sendmail distribution.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
/*
|
|
|
|
** This program checks to see if your version of setreuid works.
|
2002-02-17 21:56:45 +00:00
|
|
|
** Compile it, make it set-user-ID root, and run it as yourself (NOT as
|
1998-08-03 05:56:20 +00:00
|
|
|
** root). If it won't compile or outputs any MAYDAY messages, don't
|
|
|
|
** define HASSETREUID in conf.h.
|
|
|
|
**
|
2002-02-17 21:56:45 +00:00
|
|
|
** Compilation is trivial -- just "cc t_setreuid.c". Make it set-user-ID,
|
1998-08-03 05:56:20 +00:00
|
|
|
** root and then execute it as a non-root user.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2000-08-12 21:55:49 +00:00
|
|
|
#ifndef lint
|
2014-05-22 04:39:17 +00:00
|
|
|
static char id[] = "@(#)$Id: t_setreuid.c,v 8.10 2013-11-22 20:52:01 ca Exp $";
|
2000-08-12 21:55:49 +00:00
|
|
|
#endif /* ! lint */
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
#ifdef __hpux
|
2000-08-12 21:55:49 +00:00
|
|
|
# define setreuid(r, e) setresuid(r, e, -1)
|
|
|
|
#endif /* __hpux */
|
|
|
|
|
|
|
|
static void
|
|
|
|
printuids(str, r, e)
|
|
|
|
char *str;
|
2002-02-17 21:56:45 +00:00
|
|
|
uid_t r, e;
|
2000-08-12 21:55:49 +00:00
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
printf("%s (should be %d/%d): r/euid=%d/%d\n", str, (int) r, (int) e,
|
|
|
|
(int) getuid(), (int) geteuid());
|
2000-08-12 21:55:49 +00:00
|
|
|
}
|
1998-08-03 05:56:20 +00:00
|
|
|
|
2000-08-12 21:55:49 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
1998-08-03 05:56:20 +00:00
|
|
|
{
|
|
|
|
int fail = 0;
|
|
|
|
uid_t realuid = getuid();
|
|
|
|
|
|
|
|
printuids("initial uids", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
2002-02-17 21:56:45 +00:00
|
|
|
printf("SETUP ERROR: re-run set-user-ID root\n");
|
1998-08-03 05:56:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() == 0)
|
|
|
|
{
|
|
|
|
printf("SETUP ERROR: must be run by a non-root user\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setreuid(0, 1) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("setreuid(0, 1) failure\n");
|
|
|
|
}
|
|
|
|
printuids("after setreuid(0, 1)", 0, 1);
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
if (getuid() != 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
|
|
|
}
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
if (geteuid() != 1)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do activity here */
|
|
|
|
|
|
|
|
if (setreuid(-1, 0) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("setreuid(-1, 0) failure\n");
|
|
|
|
}
|
|
|
|
printuids("after setreuid(-1, 0)", 0, 0);
|
|
|
|
if (setreuid(realuid, 0) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
2002-02-17 21:56:45 +00:00
|
|
|
printf("setreuid(%d, 0) failure\n", (int) realuid);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
printuids("after setreuid(realuid, 0)", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
|
|
|
}
|
|
|
|
if (getuid() != realuid)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (setreuid(0, 2) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("setreuid(0, 2) failure\n");
|
|
|
|
}
|
|
|
|
printuids("after setreuid(0, 2)", 0, 2);
|
|
|
|
|
|
|
|
if (geteuid() != 2)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
|
|
|
}
|
|
|
|
|
2002-02-17 21:56:45 +00:00
|
|
|
if (getuid() != 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
|
|
|
}
|
|
|
|
|
1998-08-03 05:56:20 +00:00
|
|
|
/* do activity here */
|
|
|
|
|
|
|
|
if (setreuid(-1, 0) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("setreuid(-1, 0) failure\n");
|
|
|
|
}
|
|
|
|
printuids("after setreuid(-1, 0)", 0, 0);
|
|
|
|
if (setreuid(realuid, 0) < 0)
|
|
|
|
{
|
|
|
|
fail++;
|
2002-02-17 21:56:45 +00:00
|
|
|
printf("setreuid(%d, 0) failure\n", (int) realuid);
|
1998-08-03 05:56:20 +00:00
|
|
|
}
|
|
|
|
printuids("after setreuid(realuid, 0)", realuid, 0);
|
|
|
|
|
|
|
|
if (geteuid() != 0)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong effective uid\n");
|
|
|
|
}
|
|
|
|
if (getuid() != realuid)
|
|
|
|
{
|
|
|
|
fail++;
|
|
|
|
printf("MAYDAY! Wrong real uid\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
|
|
|
printf("\nThis system cannot use setreuid\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nIt is safe to define HASSETREUID on this system\n");
|
|
|
|
exit(0);
|
|
|
|
}
|