ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSAdmin/turnin.c
Revision: 562
Committed: 2005-08-27T21:46:39-07:00 (19 years, 10 months ago) by douglas
Content type: text/x-c
Original Path: CCSAdmin/adduser.c
File size: 3307 byte(s)
Log Message:
Moo!

File Contents

# Content
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
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
95 regfree(&name_);
96 }
97
98 char password[_PASSWORD_LEN];
99
100 getpassword(password, environment);
101 check(setuid(geteuid()), environment);
102
103 char slappasswd[] = "/tmp/slappasswd.XXXXXX";
104
105 putpassword(password, slappasswd, environment);
106
107 int pipe_[2];
108
109 check(pipe(pipe_), environment);
110
111 pid_t bash;
112
113 if (!(bash = check(fork(), environment)))
114 {
115 check(dup2(pipe_[0], 0), environment);
116 check(close(pipe_[1]), environment);
117 check(setenv("USER", user, 1), environment);
118 check(setenv("NAME", name, 1), environment);
119 check(setenv("FILE", slappasswd, 1), environment);
120 check(execl("/ccs/bin/adduser.sh", argv[0], NULL), environment);
121 }
122
123 check(close(pipe_[0]), environment);
124
125 FILE *smbpasswd = fdopen(pipe_[1], "w");
126
127 if (fprintf(smbpasswd, "%s\n%s\n", password, password) < 0)
128 longjmp(environment, 1);
129
130 if (fclose(smbpasswd))
131 longjmp(environment, 1);
132
133 int status;
134
135 check(waitpid(bash, &status, 0), environment);
136 check(unlink(slappasswd), environment);
137
138 if (WEXITSTATUS(status))
139 return 1;
140
141 struct passwd *entry = getpwnam(user);
142
143 if (!entry)
144 longjmp(environment, 1);
145
146 check(mkdir(entry->pw_dir, 0755), environment);
147 check(chown(entry->pw_dir, entry->pw_uid, entry->pw_gid), environment);
148 #else
149 longjmp(environment, (int)"Log in to zweihander.ccs.ucsb.edu to add users");
150 #endif
151
152 return 0;
153 }