Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
progandy committed Apr 5, 2013
1 parent 07a81fa commit 42be98a
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# binary
fakeadd

# Object files
*.o

Expand Down
172 changes: 172 additions & 0 deletions fakeadd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#define _GNU_SOURCE 1
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "util.h"

static char *name;

void usage_fd(FILE * f) {
fprintf(f,"Usage: %s -G|-U -n name [-g gid] [-u uid] [-p password] [-m memberlist] [-s shell] [-c gecos] [-d dir] [-h] \n"
"(C) 2013 ProgAndy\n",
basename(name));
}

void usage(void) {
usage_fd(stdout);
}

void help() {
puts("fakeadd is a tool to add users groups to a fakeuser environment.\n"
"fakeuser works best in conjunction with fakeroot. Use it with LD_PRELOAD.\n");
usage();
puts("\nPARAMETER:\n"
"\n -U : ADD USER\n"
" -n name - Username\n"
" -u uid - User ID (optional, default 0)\n"
" -g gid - Group ID (optional, default 0)\n"
" -p password - Password (optional, default empty)\n"
" -s shell - Shell (optional, default empty) Shell\n"
" -c gecos - Real name (optional, default empty)\n"
" -d dir - Home directory (optional, default empty)\n"
"\n -G : ADD GROUP\n"
" -n name - Groupname\n"
" -g gid - Group ID (optional, default 0)\n"
" -p password - Password (optional, default empty) \n"
" -m memberlist - Memberlist (optional, default empty)\n"
" [Example: username,exampleuser,nobody]"
);
}


char **string_to_array(const char *s, const char *delims, char **strings)
{
char ** array = malloc( (strlen(s)/2 + 2) * sizeof(char*) );
char *str = strdup(s);
if (strings) *strings = str;
int len = 0;

char *tok = strtok(str, delims);
while (tok) {
array[len++] = tok;
tok = strtok(NULL, delims);
}
array[len++] = NULL;
array = (char**) realloc(array, len * sizeof(char*));
return array;
}




int main(int argc, char **argv) {
char * tmpdir = getenv("_FAKEUSER_DIR_");
char *passwd_file, *group_file;

name = argv[0];
int opt, ret = 0;
int action = 0, uid = 0, gid = 0;
char *name = NULL, *passwd = NULL, *members = NULL, *shell = NULL, *gecos = NULL, *dir = NULL;
extern char *optarg;

while ((opt = getopt(argc, argv, "UGu:g:n:p:m:s:c:d:h")) != -1) {
switch (opt) {
case 'U':
action = 'U';
break;
case 'G':
action = 'G';
break;
case 'u':
uid = atoi(optarg);
break;
case 'g':
gid = atoi(optarg);
break;
case 'n':
name = optarg;
break;
case 'p':
passwd = optarg;
break;
case 'm':
members = optarg;
break;
case 's':
shell = optarg;
break;
case 'c':
gecos = optarg;
break;
case 'd':
dir = optarg;
break;
case 'h':
help();
exit(EXIT_SUCCESS);
default: /* '?' */
usage_fd(stderr);
exit(EXIT_FAILURE);
}
}
if (action == 0 || name == NULL) {
usage();
exit(EXIT_FAILURE);
}

if (!tmpdir) {
fputs("Error! Not in fakeuser environment\n", stderr);
exit(EXIT_FAILURE);
}
passwd_file = (char*)malloc(strlen(tmpdir)+10);
group_file = (char*)malloc(strlen(tmpdir)+10);
strcpy(passwd_file, tmpdir);
strcpy(group_file, tmpdir);
strcat(passwd_file, "/passwd");
strcat(group_file, "/group");

// Create directory structure
mkdir_r(tmpdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

if (action == 'U') {
struct passwd pw;
pw.pw_name = name;
pw.pw_passwd = passwd ? passwd : "";
pw.pw_gecos = gecos ? gecos : "";
pw.pw_dir = dir ? dir : "";
pw.pw_shell = shell ? shell : "";
pw.pw_uid = uid;
pw.pw_gid = gid;
FILE * pwf = fopen(passwd_file, "a");
if (pwf) {
if(putpwent(&pw, pwf))
ret = EIO;
if (fclose(pwf))
ret = EIO;
} else
ret = EIO;
} else if (action == 'G') {
struct group gr;
gr.gr_name = name;
gr.gr_passwd = passwd ? passwd : "";
gr.gr_gid = gid;
char *strings;
gr.gr_mem = members ? string_to_array(members, " ,;", &strings) : (char *[]){NULL};
FILE * pwf = fopen(group_file, "a");
if (pwf) {
if(putgrent(&gr, pwf))
ret = EIO;
if (fclose(pwf))
ret = EIO;
} else
ret = EIO;
//return fakeaddgroup(&gr);
}
return ret;
}
Loading

0 comments on commit 42be98a

Please sign in to comment.