4 |
|
// |
5 |
|
// Add User |
6 |
|
|
7 |
+ |
#include <errno.h> |
8 |
|
#include <sys/resource.h> |
9 |
|
#include <sys/time.h> |
10 |
|
#include <sys/types.h> |
11 |
+ |
#include <sys/stat.h> |
12 |
+ |
#include <fcntl.h> |
13 |
+ |
#include <fts.h> |
14 |
|
|
15 |
|
#include "common.h" |
16 |
|
|
17 |
|
int main(int argc, char *argv[]) |
18 |
|
{ |
19 |
+ |
if (argc < 1) |
20 |
+ |
return 1; |
21 |
+ |
|
22 |
+ |
umask(S_IWGRP | S_IWOTH); |
23 |
+ |
|
24 |
|
int exception; |
25 |
|
jmp_buf environment; |
26 |
|
|
45 |
|
regcheck(regcomp(&name_, "^-name=([^:]+)$", REG_EXTENDED), &name_, environment); |
46 |
|
|
47 |
|
char user[MAXLOGNAME] = "", *name = NULL; |
48 |
< |
|
48 |
> |
|
49 |
|
for (int index = 1; index != argc; ++index) |
50 |
|
{ |
51 |
|
regmatch_t match[2]; |
87 |
|
if (!name) |
88 |
|
{ |
89 |
|
regcheck(regcomp(&name_, "^[^:]+$", REG_EXTENDED), &name_, environment); |
90 |
< |
|
82 |
< |
do |
83 |
< |
{ |
84 |
< |
printf("Full Name: "); |
85 |
< |
|
86 |
< |
size_t size; |
87 |
< |
char *name_ = fcheck(fgetln(stdin, &size), stdin, environment); |
88 |
< |
|
89 |
< |
name = name ? realloc(name, size) : malloc(size); |
90 |
< |
|
91 |
< |
strlcpy(name, name_, size); |
92 |
< |
} |
93 |
< |
while (regcheck(regexec(&name_, name, 0, NULL, 0), &name_, environment)); |
94 |
< |
|
90 |
> |
get("Full Name", &name_, &name, environment); |
91 |
|
regfree(&name_); |
92 |
|
} |
93 |
|
|
139 |
|
if (!entry) |
140 |
|
longjmp(environment, 1); |
141 |
|
|
142 |
< |
check(mkdir(entry->pw_dir, 0755), environment); |
142 |
> |
check(mkdir(entry->pw_dir, S_IRWXU | S_IRWXG | S_IRWXO), environment); |
143 |
|
check(chown(entry->pw_dir, entry->pw_uid, entry->pw_gid), environment); |
144 |
+ |
check(chdir(entry->pw_dir), environment); |
145 |
+ |
|
146 |
+ |
FTS *traversal = fts_open((char *[]){ "/usr/share/skel", NULL }, FTS_LOGICAL | FTS_NOCHDIR, NULL); |
147 |
+ |
|
148 |
+ |
if (!traversal) |
149 |
+ |
longjmp(environment, 1); |
150 |
+ |
|
151 |
+ |
FTSENT *entity; |
152 |
+ |
|
153 |
+ |
while ((entity = fts_read(traversal))) |
154 |
+ |
switch (entity->fts_info) |
155 |
+ |
{ |
156 |
+ |
case FTS_D: |
157 |
+ |
if (entity->fts_level != FTS_ROOTLEVEL) |
158 |
+ |
{ |
159 |
+ |
check(mkdir(entity->fts_name, entity->fts_statp->st_mode), environment); |
160 |
+ |
check(chown(entity->fts_name, entry->pw_uid, entry->pw_gid), environment); |
161 |
+ |
check(chdir(entity->fts_name), environment); |
162 |
+ |
} |
163 |
+ |
|
164 |
+ |
break; |
165 |
+ |
case FTS_DP: |
166 |
+ |
if (entity->fts_level != FTS_ROOTLEVEL) |
167 |
+ |
check(chdir(".."), environment); |
168 |
+ |
|
169 |
+ |
break; |
170 |
+ |
case FTS_F: |
171 |
+ |
{ |
172 |
+ |
int output = check(open(strncmp(entity->fts_name, "dot", 3) ? entity->fts_name : entity->fts_name + 3, O_WRONLY | O_CREAT | O_EXCL, entity->fts_statp->st_mode), environment), input = check(open(entity->fts_accpath, O_RDONLY), environment); |
173 |
+ |
char buffer[entity->fts_statp->st_blksize]; |
174 |
+ |
size_t size; |
175 |
+ |
|
176 |
+ |
while ((size = check(read(input, buffer, sizeof (buffer)), environment))) |
177 |
+ |
check(write(output, buffer, size), environment); |
178 |
+ |
|
179 |
+ |
check(fchown(output, entry->pw_uid, entry->pw_gid), environment); |
180 |
+ |
check(close(output), environment); |
181 |
+ |
check(close(input), environment); |
182 |
+ |
} |
183 |
+ |
|
184 |
+ |
break; |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
if (errno) |
188 |
+ |
longjmp(environment, 1); |
189 |
+ |
|
190 |
+ |
if (fts_close(traversal)) |
191 |
+ |
longjmp(environment, 1); |
192 |
|
#else |
193 |
|
longjmp(environment, (int)"Log in to zweihander.ccs.ucsb.edu to add users"); |
194 |
|
#endif |