1 |
douglas |
562 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// CCS Computer Science |
4 |
|
|
// |
5 |
|
|
// Common Functions |
6 |
|
|
|
7 |
|
|
#include "common.h" |
8 |
|
|
|
9 |
|
|
int check(int value, jmp_buf environment) |
10 |
|
|
{ |
11 |
|
|
if (value == -1) |
12 |
|
|
longjmp(environment, 1); |
13 |
|
|
|
14 |
|
|
return value; |
15 |
|
|
} |
16 |
|
|
|
17 |
|
|
int regcheck(int value, const regex_t *regex, jmp_buf environment) |
18 |
|
|
{ |
19 |
|
|
if (value && value != REG_NOMATCH) |
20 |
|
|
{ |
21 |
|
|
char exception[regerror(value, regex, NULL, 0)]; |
22 |
|
|
|
23 |
|
|
regerror(value, regex, exception, sizeof (exception)); |
24 |
|
|
longjmp(environment, (int)exception); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
return value; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
char *fcheck(char *value, FILE *stream, jmp_buf environment) |
31 |
|
|
{ |
32 |
|
|
if (!value) |
33 |
|
|
{ |
34 |
|
|
if (ferror(stream)) |
35 |
|
|
longjmp(environment, 1); |
36 |
|
|
else |
37 |
|
|
{ |
38 |
|
|
clearerr(stdin); |
39 |
|
|
printf("\n"); |
40 |
|
|
|
41 |
|
|
return ""; |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
return value; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
void authenticate(const char *program, jmp_buf environment) |
49 |
|
|
{ |
50 |
|
|
struct passwd *entry = getpwuid(getuid()); |
51 |
|
|
|
52 |
|
|
if (!entry) |
53 |
|
|
longjmp(environment, 1); |
54 |
|
|
|
55 |
|
|
char name[strlen(entry->pw_name) + 36]; |
56 |
|
|
|
57 |
|
|
sprintf(name, "uid=%s,ou=People,dc=ccs,dc=ucsb,dc=edu", entry->pw_name); |
58 |
|
|
umask(022); |
59 |
|
|
|
60 |
|
|
char *password = getpass("Password: "); |
61 |
|
|
|
62 |
|
|
if (!strlen(password)) |
63 |
|
|
longjmp(environment, (int)"Empty password"); |
64 |
|
|
|
65 |
|
|
char secret[] = "/tmp/secret.XXXXXX"; |
66 |
|
|
|
67 |
|
|
putpassword(password, secret, environment); |
68 |
|
|
|
69 |
|
|
int pipe_[2]; |
70 |
|
|
|
71 |
|
|
check(pipe(pipe_), environment); |
72 |
|
|
|
73 |
|
|
pid_t ldapwhoami; |
74 |
|
|
|
75 |
|
|
if (!(ldapwhoami = check(fork(), environment))) |
76 |
|
|
{ |
77 |
|
|
check(dup2(pipe_[1], 1), environment); |
78 |
|
|
check(close(pipe_[0]), environment); |
79 |
|
|
check(execl("/usr/local/bin/ldapwhoami", program, "-D", name, "-W", "-x", "-y", secret, NULL), environment); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
check(close(pipe_[1]), environment); |
83 |
|
|
|
84 |
|
|
FILE *ldapwhoami_ = fdopen(pipe_[0], "r"); |
85 |
|
|
size_t size; |
86 |
|
|
char *whoami_ = fcheck(fgetln(ldapwhoami_, &size), ldapwhoami_, environment); |
87 |
|
|
int status; |
88 |
|
|
|
89 |
|
|
check(waitpid(ldapwhoami, &status, 0), environment); |
90 |
|
|
check(unlink(secret), environment); |
91 |
|
|
|
92 |
|
|
char whoami[size]; |
93 |
|
|
|
94 |
|
|
strlcpy(whoami, whoami_, size); |
95 |
|
|
|
96 |
|
|
whoami_ = malloc(sizeof (name) + 3); |
97 |
|
|
|
98 |
|
|
sprintf(whoami_, "dn:%s", name); |
99 |
|
|
|
100 |
|
|
if (strcmp(whoami, whoami_)) |
101 |
|
|
longjmp(environment, (int)"Sorry"); |
102 |
|
|
|
103 |
|
|
free(whoami_); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
void getpassword(char password[_PASSWORD_LEN], jmp_buf environment) |
107 |
|
|
{ |
108 |
|
|
strcpy(password, getpass("New Password: ")); |
109 |
|
|
|
110 |
|
|
if (!strlen(password)) |
111 |
|
|
longjmp(environment, (int)"Empty password"); |
112 |
|
|
|
113 |
|
|
if (strcmp(password, getpass("Confirm Password: "))) |
114 |
|
|
longjmp(environment, (int)"Password and confirm password mismatched"); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
void putpassword(char password[_PASSWORD_LEN], char *name, jmp_buf environment) |
118 |
|
|
{ |
119 |
|
|
int file = check(mkstemp(name), environment); |
120 |
|
|
|
121 |
|
|
check(write(file, password, strlen(password)), environment); |
122 |
|
|
check(close(file), environment); |
123 |
|
|
} |
124 |
douglas |
563 |
|
125 |
|
|
void get(const char *prompt, regex_t *regex, char **string, jmp_buf environment) |
126 |
|
|
{ |
127 |
|
|
do |
128 |
|
|
{ |
129 |
|
|
printf("%s: ", prompt); |
130 |
|
|
|
131 |
|
|
size_t size; |
132 |
|
|
char *string_ = fcheck(fgetln(stdin, &size), stdin, environment); |
133 |
|
|
|
134 |
|
|
*string = *string ? realloc(*string, size) : malloc(size); |
135 |
|
|
|
136 |
|
|
strlcpy(*string, string_, size); |
137 |
|
|
} |
138 |
|
|
while (regcheck(regexec(regex, *string, 0, NULL, 0), regex, environment)); |
139 |
|
|
} |