-
Notifications
You must be signed in to change notification settings - Fork 9
/
fakeadd.c
200 lines (180 loc) · 5.28 KB
/
fakeadd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
(C) 2013-2016 ProgAndy
This file is part of fakeuser.
fakeuser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
fakeuser is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with fakeuser. If not, see <http://www.gnu.org/licenses/>.
*/
#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;
// print usage to given stream
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-2016 ProgAndy\n",
basename(name));
}
// print usage to stdout
void usage(void) {
usage_fd(stdout);
}
// print help to 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]"
);
}
// convert string to array with specified delimiters
// if strings is not null, receives pointer to buffer for elements
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);
}
// only continue when environment variable with directory found.
if (!tmpdir) {
fputs("Error! Not in fakeuser environment\n", stderr);
exit(EXIT_FAILURE);
}
// init file paths
passwd_file = (char*)malloc(strlen(tmpdir)+10);
strcpy(passwd_file, tmpdir);
strcat(passwd_file, "/passwd");
group_file = (char*)malloc(strlen(tmpdir)+10);
strcpy(group_file, tmpdir);
strcat(group_file, "/group");
// Create directory structure
mkdir_r(tmpdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (action == 'U') {
// create and append passwd entry
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;
// append to file with error handling.
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') {
// create and append group entry
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};
// append to file with error handling.
FILE * pwf = fopen(group_file, "a");
if (pwf) {
if(putgrent(&gr, pwf))
ret = EIO;
if (fclose(pwf))
ret = EIO;
} else
ret = EIO;
}
// return 0 on success or the error value
return ret;
}