/**************************************************************/ /* naughty.c 1.0.0 (1999-Sep-18-Sat) */ /* Adam M. Costello */ /* */ /* naughty [ -n ] []... */ /* */ /* Opposite of nice. This command is just like the nice */ /* command, except that it doesn't support the obsolete */ /* -increment option, and the default increment is negative */ /* instead of positive (meaning the command will get more */ /* CPU). */ /* */ /* This command is intended to be setuid root. After it sets */ /* the priority, it gives up its root priviledges, becoming */ /* the original user again. This command should probably not */ /* be executable by everyone, but only by a particular group, */ /* perhaps group "naughty". */ /**************************************************************/ /* This is ANSI C plus Single UNIX v2 code */ #include #include #include #include #include #include #include /* Single UNIX v2 says NZERO should be defined */ /* by sys/resource.h, but in case it's not... */ #ifndef NZERO #define NZERO 20 #endif static void usage(const char *progname) { fprintf(stderr, "usage:\n" "%s [ -n ] command []...\n", progname); exit(EXIT_FAILURE); } int main(int argc, char **argv) { int current, increment = -(NZERO / 2), r; char * const *execargs = argv + 1; const char *progname = argv[0]; uid_t uid; errno = 0; current = getpriority(PRIO_PROCESS,0); if (errno != 0) { perror("getpriority(PRIO_PROCESS,0)"); return EXIT_FAILURE; } if (argc < 2) usage(progname); if (argv[1][0] == '-') { if (strcmp(argv[1], "-n")) usage(progname); else { if (argc < 4) usage(progname); increment = atoi(argv[2]); execargs = argv + 3; } } r = setpriority(PRIO_PROCESS, 0, current + increment); if (r == -1) { fprintf(stderr, "setpriority(PRIO_PROCESS, 0, %d + %d): %s\n", current, increment, strerror(errno)); } uid = getuid(); r = setuid(uid); if (r == -1) { fprintf(stderr, "setuid(%d): %s\n", (int) uid, strerror(errno)); return EXIT_FAILURE; } execvp(execargs[0], execargs); fprintf(stderr, "execvp(%s, ...): %s\n", execargs[0], strerror(errno)); return EXIT_FAILURE; }