1 /* 2 ** This program checks to see if your version of setreuid works. 3 ** Compile it, make it setuid root, and run it as yourself (NOT as 4 ** root). If it won't compile or outputs any MAYDAY messages, don't 5 ** define HASSETREUID in conf.h. 6 ** 7 ** Compilation is trivial -- just "cc t_setreuid.c". Make it setuid, 8 ** root and then execute it as a non-root user. 9 */ 10 11 #include <sys/types.h> 12 #include <unistd.h> 13 #include <stdio.h> 14 15 #ifdef __hpux 16 #define setreuid(r, e) setresuid(r, e, -1) 17 #endif 18 19 main() 20 { 21 int fail = 0; 22 uid_t realuid = getuid(); 23 24 printuids("initial uids", realuid, 0); 25 26 if (geteuid() != 0) 27 { 28 printf("SETUP ERROR: re-run setuid root\n"); 29 exit(1); 30 } 31 32 if (getuid() == 0) 33 { 34 printf("SETUP ERROR: must be run by a non-root user\n"); 35 exit(1); 36 } 37 38 if (setreuid(0, 1) < 0) 39 { 40 fail++; 41 printf("setreuid(0, 1) failure\n"); 42 } 43 printuids("after setreuid(0, 1)", 0, 1); 44 45 if (geteuid() != 1) 46 { 47 fail++; 48 printf("MAYDAY! Wrong effective uid\n"); 49 } 50 51 /* do activity here */ 52 53 if (setreuid(-1, 0) < 0) 54 { 55 fail++; 56 printf("setreuid(-1, 0) failure\n"); 57 } 58 printuids("after setreuid(-1, 0)", 0, 0); 59 if (setreuid(realuid, 0) < 0) 60 { 61 fail++; 62 printf("setreuid(%d, 0) failure\n", realuid); 63 } 64 printuids("after setreuid(realuid, 0)", realuid, 0); 65 66 if (geteuid() != 0) 67 { 68 fail++; 69 printf("MAYDAY! Wrong effective uid\n"); 70 } 71 if (getuid() != realuid) 72 { 73 fail++; 74 printf("MAYDAY! Wrong real uid\n"); 75 } 76 printf("\n"); 77 78 if (setreuid(0, 2) < 0) 79 { 80 fail++; 81 printf("setreuid(0, 2) failure\n"); 82 } 83 printuids("after setreuid(0, 2)", 0, 2); 84 85 if (geteuid() != 2) 86 { 87 fail++; 88 printf("MAYDAY! Wrong effective uid\n"); 89 } 90 91 /* do activity here */ 92 93 if (setreuid(-1, 0) < 0) 94 { 95 fail++; 96 printf("setreuid(-1, 0) failure\n"); 97 } 98 printuids("after setreuid(-1, 0)", 0, 0); 99 if (setreuid(realuid, 0) < 0) 100 { 101 fail++; 102 printf("setreuid(%d, 0) failure\n", realuid); 103 } 104 printuids("after setreuid(realuid, 0)", realuid, 0); 105 106 if (geteuid() != 0) 107 { 108 fail++; 109 printf("MAYDAY! Wrong effective uid\n"); 110 } 111 if (getuid() != realuid) 112 { 113 fail++; 114 printf("MAYDAY! Wrong real uid\n"); 115 } 116 117 if (fail) 118 { 119 printf("\nThis system cannot use setreuid\n"); 120 exit(1); 121 } 122 123 printf("\nIt is safe to define HASSETREUID on this system\n"); 124 exit(0); 125 } 126 127 printuids(str, r, e) 128 char *str; 129 int r, e; 130 { 131 printf("%s (should be %d/%d): r/euid=%d/%d\n", str, r, e, 132 getuid(), geteuid()); 133 } 134