1 |
douglas |
562 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// CCS Computer Science |
4 |
|
|
// |
5 |
|
|
// Common Functions |
6 |
|
|
|
7 |
|
|
#include "common.h" |
8 |
|
|
|
9 |
douglas |
583 |
Shells shells[] = { |
10 |
|
|
{ "sh", "/bin/sh", "/bin/sh", "/bin/sh", "/usr/bin/sh" }, |
11 |
|
|
{ "csh", "/bin/csh", "/bin/csh", "/bin/csh", "/usr/bin/csh" }, |
12 |
|
|
{ "tcsh", "/bin/tcsh", "/bin/tcsh", "/bin/tcsh", "/usr/bin/tcsh" }, |
13 |
|
|
{ "bash", "/usr/local/bin/bash", "/bin/bash", "/bin/bash", "/usr/local/bin/bash" }, |
14 |
|
|
{ "ksh", "/usr/local/bin/ksh", "/bin/ksh", "/bin/ksh", "/usr/bin/ksh" }, |
15 |
|
|
{ "zsh", "/usr/local/bin/zsh", "/bin/zsh", "/bin/zsh", "/usr/bin/zsh" }, |
16 |
|
|
{ "3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh", "/ccs/bin/3sh" }, |
17 |
|
|
{ "custom", NULL, NULL, NULL, NULL } |
18 |
|
|
}; |
19 |
|
|
|
20 |
douglas |
562 |
int check(int value, jmp_buf environment) |
21 |
|
|
{ |
22 |
|
|
if (value == -1) |
23 |
|
|
longjmp(environment, 1); |
24 |
|
|
|
25 |
|
|
return value; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
int regcheck(int value, const regex_t *regex, jmp_buf environment) |
29 |
|
|
{ |
30 |
|
|
if (value && value != REG_NOMATCH) |
31 |
|
|
{ |
32 |
|
|
char exception[regerror(value, regex, NULL, 0)]; |
33 |
|
|
|
34 |
|
|
regerror(value, regex, exception, sizeof (exception)); |
35 |
|
|
longjmp(environment, (int)exception); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
return value; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
char *fcheck(char *value, FILE *stream, jmp_buf environment) |
42 |
|
|
{ |
43 |
|
|
if (!value) |
44 |
|
|
{ |
45 |
|
|
if (ferror(stream)) |
46 |
|
|
longjmp(environment, 1); |
47 |
|
|
else |
48 |
|
|
{ |
49 |
|
|
clearerr(stdin); |
50 |
|
|
printf("\n"); |
51 |
|
|
|
52 |
|
|
return ""; |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
return value; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void authenticate(const char *program, jmp_buf environment) |
60 |
|
|
{ |
61 |
|
|
struct passwd *entry = getpwuid(getuid()); |
62 |
|
|
|
63 |
|
|
if (!entry) |
64 |
|
|
longjmp(environment, 1); |
65 |
|
|
|
66 |
|
|
char name[strlen(entry->pw_name) + 36]; |
67 |
|
|
|
68 |
|
|
sprintf(name, "uid=%s,ou=People,dc=ccs,dc=ucsb,dc=edu", entry->pw_name); |
69 |
|
|
umask(022); |
70 |
|
|
|
71 |
|
|
char *password = getpass("Password: "); |
72 |
|
|
|
73 |
|
|
if (!strlen(password)) |
74 |
|
|
longjmp(environment, (int)"Empty password"); |
75 |
|
|
|
76 |
|
|
char secret[] = "/tmp/secret.XXXXXX"; |
77 |
|
|
|
78 |
|
|
putpassword(password, secret, environment); |
79 |
|
|
|
80 |
|
|
int pipe_[2]; |
81 |
|
|
|
82 |
|
|
check(pipe(pipe_), environment); |
83 |
|
|
|
84 |
|
|
pid_t ldapwhoami; |
85 |
|
|
|
86 |
|
|
if (!(ldapwhoami = check(fork(), environment))) |
87 |
|
|
{ |
88 |
|
|
check(dup2(pipe_[1], 1), environment); |
89 |
|
|
check(close(pipe_[0]), environment); |
90 |
douglas |
572 |
check(execl(LDAPWHOAMI, program, "-D", name, "-W", "-x", "-y", secret, NULL), environment); |
91 |
douglas |
562 |
} |
92 |
|
|
|
93 |
|
|
check(close(pipe_[1]), environment); |
94 |
|
|
|
95 |
|
|
FILE *ldapwhoami_ = fdopen(pipe_[0], "r"); |
96 |
|
|
size_t size; |
97 |
|
|
char *whoami_ = fcheck(fgetln(ldapwhoami_, &size), ldapwhoami_, environment); |
98 |
|
|
int status; |
99 |
|
|
|
100 |
|
|
check(waitpid(ldapwhoami, &status, 0), environment); |
101 |
|
|
check(unlink(secret), environment); |
102 |
|
|
|
103 |
|
|
char whoami[size]; |
104 |
|
|
|
105 |
|
|
strlcpy(whoami, whoami_, size); |
106 |
|
|
|
107 |
|
|
whoami_ = malloc(sizeof (name) + 3); |
108 |
|
|
|
109 |
|
|
sprintf(whoami_, "dn:%s", name); |
110 |
|
|
|
111 |
|
|
if (strcmp(whoami, whoami_)) |
112 |
|
|
longjmp(environment, (int)"Sorry"); |
113 |
|
|
|
114 |
|
|
free(whoami_); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
void getpassword(char password[_PASSWORD_LEN], jmp_buf environment) |
118 |
|
|
{ |
119 |
|
|
strcpy(password, getpass("New Password: ")); |
120 |
|
|
|
121 |
|
|
if (!strlen(password)) |
122 |
|
|
longjmp(environment, (int)"Empty password"); |
123 |
|
|
|
124 |
|
|
if (strcmp(password, getpass("Confirm Password: "))) |
125 |
|
|
longjmp(environment, (int)"Password and confirm password mismatched"); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
void putpassword(char password[_PASSWORD_LEN], char *name, jmp_buf environment) |
129 |
|
|
{ |
130 |
|
|
int file = check(mkstemp(name), environment); |
131 |
|
|
|
132 |
|
|
check(write(file, password, strlen(password)), environment); |
133 |
|
|
check(close(file), environment); |
134 |
|
|
} |
135 |
douglas |
563 |
|
136 |
|
|
void get(const char *prompt, regex_t *regex, char **string, jmp_buf environment) |
137 |
|
|
{ |
138 |
|
|
do |
139 |
|
|
{ |
140 |
|
|
printf("%s: ", prompt); |
141 |
|
|
|
142 |
|
|
size_t size; |
143 |
|
|
char *string_ = fcheck(fgetln(stdin, &size), stdin, environment); |
144 |
|
|
|
145 |
|
|
*string = *string ? realloc(*string, size) : malloc(size); |
146 |
|
|
|
147 |
|
|
strlcpy(*string, string_, size); |
148 |
|
|
} |
149 |
|
|
while (regcheck(regexec(regex, *string, 0, NULL, 0), regex, environment)); |
150 |
|
|
} |
151 |
douglas |
572 |
|
152 |
douglas |
583 |
void setshells(Shells *shells, jmp_buf environment) |
153 |
|
|
{ |
154 |
|
|
check(setenv("FREEBSD", shells->freebsd, 1), environment); |
155 |
|
|
check(setenv("LINUX", shells->linux, 1), environment); |
156 |
|
|
check(setenv("DARWIN", shells->darwin, 1), environment); |
157 |
|
|
check(setenv("SOLARIS", shells->solaris, 1), environment); |
158 |
|
|
} |
159 |
|
|
|
160 |
douglas |
572 |
#ifndef __FreeBSD__ |
161 |
|
|
size_t strlcpy(char *dst, const char *src, size_t size) |
162 |
|
|
{ |
163 |
|
|
dst[size - 1] = '\0'; |
164 |
|
|
|
165 |
|
|
return strlen(strncpy(dst, src, size - 1)); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
size_t strlcat(char *dst, const char *src, size_t size) |
169 |
|
|
{ |
170 |
|
|
dst[size - 1] = '\0'; |
171 |
|
|
|
172 |
|
|
size_t size_ = strlen(dst); |
173 |
|
|
|
174 |
|
|
return strlen(strncpy(dst + size_, src, size - size_ - 1)); |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
char *fgetln(FILE * restrict stream, size_t * restrict len) |
178 |
|
|
{ |
179 |
|
|
static char *line = NULL; |
180 |
|
|
static size_t size; |
181 |
|
|
|
182 |
|
|
if ((*len = getline(&line, &size, stream)) == -1) |
183 |
|
|
return NULL; |
184 |
|
|
|
185 |
|
|
return line; |
186 |
|
|
} |
187 |
|
|
#endif |