1 |
// Douglas Thrift |
2 |
// |
3 |
// CCS Computer Science |
4 |
// |
5 |
// Add User |
6 |
|
7 |
#include <sys/resource.h> |
8 |
#include <sys/time.h> |
9 |
#include <sys/types.h> |
10 |
|
11 |
#include "common.h" |
12 |
|
13 |
int main(int argc, char *argv[]) |
14 |
{ |
15 |
int exception; |
16 |
jmp_buf environment; |
17 |
|
18 |
switch (exception = setjmp(environment)) |
19 |
{ |
20 |
case 0: |
21 |
break; |
22 |
case 1: |
23 |
perror(argv[0]); |
24 |
|
25 |
return 1; |
26 |
default: |
27 |
fprintf(stderr, "%s: %s\n", argv[0], (char *)exception); |
28 |
|
29 |
return 1; |
30 |
} |
31 |
|
32 |
#ifdef _Zweihander_ |
33 |
regex_t user_, name_; |
34 |
|
35 |
regcheck(regcomp(&user_, "^-user=([a-z0-9]{1,16})$", REG_EXTENDED), &user_, environment); |
36 |
regcheck(regcomp(&name_, "^-name=([^:]+)$", REG_EXTENDED), &name_, environment); |
37 |
|
38 |
char user[MAXLOGNAME] = "", *name = NULL; |
39 |
|
40 |
for (int index = 1; index != argc; ++index) |
41 |
{ |
42 |
regmatch_t match[2]; |
43 |
|
44 |
if (!regcheck(regexec(&user_, argv[index], 2, match, 0), &user_, environment)) |
45 |
strlcpy(user, argv[index] + match[1].rm_so, match[1].rm_eo - match[1].rm_so + 1); |
46 |
else if (!regcheck(regexec(&name_, argv[index], 2, match, 0), &name_, environment)) |
47 |
name = argv[index] + match[1].rm_so; |
48 |
else |
49 |
{ |
50 |
printf("Usage: %s [-user=user] [-name=name]\n", argv[0]); |
51 |
|
52 |
return 1; |
53 |
} |
54 |
} |
55 |
|
56 |
regfree(&user_); |
57 |
regfree(&name_); |
58 |
authenticate(argv[0], environment); |
59 |
|
60 |
if (!strlen(user)) |
61 |
{ |
62 |
regcheck(regcomp(&user_, "^[a-z0-9]{1,16}$", REG_EXTENDED), &user_, environment); |
63 |
|
64 |
do |
65 |
{ |
66 |
printf("User Name: "); |
67 |
|
68 |
size_t size; |
69 |
char *user_ = fcheck(fgetln(stdin, &size), stdin, environment); |
70 |
|
71 |
strlcpy(user, user_, size < sizeof (user) ? size : sizeof (user)); |
72 |
} |
73 |
while (regcheck(regexec(&user_, user, 0, NULL, 0), &user_, environment)); |
74 |
|
75 |
regfree(&user_); |
76 |
} |
77 |
|
78 |
if (!name) |
79 |
{ |
80 |
regcheck(regcomp(&name_, "^[^:]+$", REG_EXTENDED), &name_, environment); |
81 |
get("Full Name", &name_, &name, environment); |
82 |
regfree(&name_); |
83 |
} |
84 |
|
85 |
char password[_PASSWORD_LEN]; |
86 |
|
87 |
getpassword(password, environment); |
88 |
check(setuid(geteuid()), environment); |
89 |
|
90 |
char slappasswd[] = "/tmp/slappasswd.XXXXXX"; |
91 |
|
92 |
putpassword(password, slappasswd, environment); |
93 |
|
94 |
int pipe_[2]; |
95 |
|
96 |
check(pipe(pipe_), environment); |
97 |
|
98 |
pid_t bash; |
99 |
|
100 |
if (!(bash = check(fork(), environment))) |
101 |
{ |
102 |
check(dup2(pipe_[0], 0), environment); |
103 |
check(close(pipe_[1]), environment); |
104 |
check(setenv("USER", user, 1), environment); |
105 |
check(setenv("NAME", name, 1), environment); |
106 |
check(setenv("FILE", slappasswd, 1), environment); |
107 |
check(execl("/ccs/bin/adduser.sh", argv[0], NULL), environment); |
108 |
} |
109 |
|
110 |
check(close(pipe_[0]), environment); |
111 |
|
112 |
FILE *smbpasswd = fdopen(pipe_[1], "w"); |
113 |
|
114 |
if (fprintf(smbpasswd, "%s\n%s\n", password, password) < 0) |
115 |
longjmp(environment, 1); |
116 |
|
117 |
if (fclose(smbpasswd)) |
118 |
longjmp(environment, 1); |
119 |
|
120 |
int status; |
121 |
|
122 |
check(waitpid(bash, &status, 0), environment); |
123 |
check(unlink(slappasswd), environment); |
124 |
|
125 |
if (WEXITSTATUS(status)) |
126 |
return 1; |
127 |
|
128 |
struct passwd *entry = getpwnam(user); |
129 |
|
130 |
if (!entry) |
131 |
longjmp(environment, 1); |
132 |
|
133 |
check(mkdir(entry->pw_dir, 0755), environment); |
134 |
check(chown(entry->pw_dir, entry->pw_uid, entry->pw_gid), environment); |
135 |
#else |
136 |
longjmp(environment, (int)"Log in to zweihander.ccs.ucsb.edu to add users"); |
137 |
#endif |
138 |
|
139 |
return 0; |
140 |
} |