t_setreuid.c revision 64562
11541Srgrimes/* 21541Srgrimes** This program checks to see if your version of setreuid works. 31541Srgrimes** Compile it, make it setuid root, and run it as yourself (NOT as 41541Srgrimes** root). If it won't compile or outputs any MAYDAY messages, don't 51541Srgrimes** define HASSETREUID in conf.h. 61541Srgrimes** 71541Srgrimes** Compilation is trivial -- just "cc t_setreuid.c". Make it setuid, 81541Srgrimes** root and then execute it as a non-root user. 91541Srgrimes*/ 101541Srgrimes 111541Srgrimes#include <sys/types.h> 121541Srgrimes#include <unistd.h> 131541Srgrimes#include <stdio.h> 141541Srgrimes 151541Srgrimes#ifndef lint 161541Srgrimesstatic char id[] = "@(#)$Id: t_setreuid.c,v 8.4 1999/08/28 00:25:28 gshapiro Exp $"; 171541Srgrimes#endif /* ! lint */ 181541Srgrimes 191541Srgrimes#ifdef __hpux 201541Srgrimes# define setreuid(r, e) setresuid(r, e, -1) 211541Srgrimes#endif /* __hpux */ 221541Srgrimes 231541Srgrimesstatic void 241541Srgrimesprintuids(str, r, e) 251541Srgrimes char *str; 261541Srgrimes int r, e; 271541Srgrimes{ 281541Srgrimes printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e, 291541Srgrimes getuid(), geteuid()); 301541Srgrimes} 311541Srgrimes 321541Srgrimesint 3322521Sdysonmain(argc, argv) 3450477Speter int argc; 351541Srgrimes char **argv; 361541Srgrimes{ 371541Srgrimes int fail = 0; 381541Srgrimes uid_t realuid = getuid(); 3960041Sphk 401541Srgrimes printuids("initial uids", realuid, 0); 4131561Sbde 4234266Sjulian if (geteuid() != 0) 431541Srgrimes { 441541Srgrimes printf("SETUP ERROR: re-run setuid root\n"); 451541Srgrimes exit(1); 461541Srgrimes } 471541Srgrimes 481541Srgrimes if (getuid() == 0) 491541Srgrimes { 501541Srgrimes printf("SETUP ERROR: must be run by a non-root user\n"); 511541Srgrimes exit(1); 521541Srgrimes } 5396755Strhodes 541541Srgrimes if (setreuid(0, 1) < 0) 551541Srgrimes { 561541Srgrimes fail++; 571549Srgrimes printf("setreuid(0, 1) failure\n"); 5876132Sphk } 5976132Sphk printuids("after setreuid(0, 1)", 0, 1); 6034266Sjulian 6155799Smckusick if (geteuid() != 1) 6255799Smckusick { 631541Srgrimes fail++; 641541Srgrimes printf("MAYDAY! Wrong effective uid\n"); 651541Srgrimes } 6634266Sjulian 6734266Sjulian /* do activity here */ 681541Srgrimes 6939099Sbde if (setreuid(-1, 0) < 0) 701541Srgrimes { 7122521Sdyson fail++; 7222521Sdyson printf("setreuid(-1, 0) failure\n"); 7322521Sdyson } 7457446Sdillon printuids("after setreuid(-1, 0)", 0, 0); 7583366Sjulian if (setreuid(realuid, 0) < 0) 761541Srgrimes { 7776132Sphk fail++; 7834266Sjulian printf("setreuid(%d, 0) failure\n", realuid); 7934266Sjulian } 8076132Sphk printuids("after setreuid(realuid, 0)", realuid, 0); 8176132Sphk 8234266Sjulian if (geteuid() != 0) 8334266Sjulian { 8476132Sphk fail++; 8522521Sdyson printf("MAYDAY! Wrong effective uid\n"); 861541Srgrimes } 8776132Sphk if (getuid() != realuid) 8876132Sphk { 891541Srgrimes fail++; 901541Srgrimes printf("MAYDAY! Wrong real uid\n"); 911541Srgrimes } 921541Srgrimes printf("\n"); 931541Srgrimes 941541Srgrimes if (setreuid(0, 2) < 0) 951541Srgrimes { 9622521Sdyson fail++; 971541Srgrimes printf("setreuid(0, 2) failure\n"); 981541Srgrimes } 991541Srgrimes printuids("after setreuid(0, 2)", 0, 2); 1001541Srgrimes 1011541Srgrimes if (geteuid() != 2) 1021541Srgrimes { 1031541Srgrimes fail++; 10434266Sjulian printf("MAYDAY! Wrong effective uid\n"); 10534266Sjulian } 10634266Sjulian 10734266Sjulian /* do activity here */ 10824775Sbde 1091541Srgrimes if (setreuid(-1, 0) < 0) 1101541Srgrimes { 1111541Srgrimes fail++; 1121541Srgrimes printf("setreuid(-1, 0) failure\n"); 1131541Srgrimes } 1141541Srgrimes printuids("after setreuid(-1, 0)", 0, 0); 1151541Srgrimes if (setreuid(realuid, 0) < 0) 1161541Srgrimes { 1171541Srgrimes fail++; 1181541Srgrimes printf("setreuid(%d, 0) failure\n", realuid); 1191541Srgrimes } 12022521Sdyson printuids("after setreuid(realuid, 0)", realuid, 0); 12162976Smckusick 12262976Smckusick if (geteuid() != 0) 12322521Sdyson { 12424775Sbde fail++; 12522521Sdyson printf("MAYDAY! Wrong effective uid\n"); 1261541Srgrimes } 1271541Srgrimes if (getuid() != realuid) 1281541Srgrimes { 1291541Srgrimes fail++; 1306864Sdg printf("MAYDAY! Wrong real uid\n"); 13176132Sphk } 1321541Srgrimes 1331541Srgrimes if (fail) 1341541Srgrimes { 1351541Srgrimes printf("\nThis system cannot use setreuid\n"); 1361541Srgrimes exit(1); 1371541Srgrimes } 1381541Srgrimes 1391541Srgrimes printf("\nIt is safe to define HASSETREUID on this system\n"); 1401541Srgrimes exit(0); 14122521Sdyson} 1421541Srgrimes