#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "setonlygid.h"

int main(int argc, char *const argv[]) {
    const char *username = argv[1];
    const struct passwd *pwd;
    char *const *new_argv = argv + 2;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s username command...\n", argv[0]);
        exit(100);
    }

    pwd = getpwnam(username);
    
    if (pwd == NULL) {
        fprintf(stderr, "User %s not found\n", argv[1]);
        exit(111);
    }
    if (setonlygid(pwd->pw_gid) != 0) {
        perror("setonlygid");
        exit(111);
    }
    if (setuid(pwd->pw_uid) != 0) {
        perror("setuid");
        exit(111);
    }
    execvp(*new_argv, new_argv);
    perror("execvp");
    exit(111);
}



syntax highlighted by Code2HTML, v. 0.9.1