ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSAdmin/adduser.c
Revision: 564
Committed: 2005-08-28T03:22:30-07:00 (19 years, 9 months ago) by douglas
Content type: text/x-c
File size: 3096 byte(s)
Log Message:
Security, eh!

File Contents

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