ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/ccs/admin/adduser.c
Revision: 1
Committed: 2006-07-13T16:11:11-07:00 (18 years, 11 months ago) by douglas
Content type: text/x-c
File size: 6108 byte(s)
Log Message:
Moved this stuff from http://svn.douglasthrift.net/svn/repos/ to here.

File Contents

# User Rev Content
1 douglas 1 // Douglas Thrift
2     //
3     // CCS Computer Science
4     // Add User
5     //
6     // $Id$
7    
8     #include "common.h"
9    
10     #include <errno.h>
11     #include <stdbool.h>
12     #include <sys/resource.h>
13     #include <sys/time.h>
14     #include <sys/types.h>
15     #include <sys/stat.h>
16     #include <fcntl.h>
17    
18     #ifdef _Master_
19     #include <fts.h>
20    
21     static int compare(const char **key, const char **item)
22     {
23     return strcmp(*key, *item);
24     }
25    
26     static bool exists(const char *program, const char *user, jmp_buf environment)
27     {
28     int pipe_[2];
29    
30     check(pipe(pipe_), environment);
31    
32     pid_t ldapsearch;
33    
34     if (!(ldapsearch = check(fork(), environment)))
35     {
36     check(dup2(pipe_[1], 1), environment);
37     check(close(pipe_[0]), environment);
38    
39     char uid[strlen(user) + 5];
40    
41     sprintf(uid, "uid=%s", user);
42     check(execl(LDAPSEARCH, program, "-b", "ou=People,dc=ccs,dc=ucsb,dc=edu", "-H", "ldaps://" MASTER, "-LLL", "-x", uid, "1.1", NULL), environment);
43     }
44    
45     check(close(pipe_[1]), environment);
46    
47     FILE *ldapsearch_ = fdopen(pipe_[0], "r");
48     size_t size;
49     char *uid = fcheck(fgetln(ldapsearch_, &size), ldapsearch_, environment);
50     int status;
51    
52     check(waitpid(ldapsearch, &status, 0), environment);
53    
54     char user_[strlen(user) + 41];
55    
56     sprintf(user_, "dn: uid=%s,ou=People,dc=ccs,dc=ucsb,dc=edu", user);
57    
58     if (size != 0 && !strncmp(user_, uid, size - 1))
59     return true;
60    
61     static const char *users[] = {
62     # include "users.h"
63     };
64    
65     if (bsearch(&user, users, sizeof (users) / sizeof (*users), sizeof (*users), (int (*)(const void *, const void *))compare))
66     return true;
67    
68     return false;
69     }
70     #endif
71    
72     int main(int argc, char *argv[])
73     {
74     if (argc < 1)
75     return 1;
76    
77     umask(S_IWGRP | S_IWOTH);
78    
79     int exception;
80     jmp_buf environment;
81    
82     switch (exception = setjmp(environment))
83     {
84     case 0:
85     break;
86     case 1:
87     perror(argv[0]);
88    
89     return 1;
90     default:
91     fprintf(stderr, "%s: %s\n", argv[0], (char *)exception);
92    
93     return 1;
94     }
95    
96     #ifdef _Master_
97     regex_t user_, name_;
98    
99     regcheck(regcomp(&user_, "^-user=([a-z0-9]{1,16})$", REG_EXTENDED), &user_, environment);
100     regcheck(regcomp(&name_, "^-name=([^:]+)$", REG_EXTENDED), &name_, environment);
101    
102     char user[MAXLOGNAME] = "", *name = NULL;
103    
104     for (int index = 1; index != argc; ++index)
105     {
106     regmatch_t match[2];
107    
108     if (!regcheck(regexec(&user_, argv[index], 2, match, 0), &user_, environment))
109     strlcpy(user, argv[index] + match[1].rm_so, match[1].rm_eo - match[1].rm_so + 1);
110     else if (!regcheck(regexec(&name_, argv[index], 2, match, 0), &name_, environment))
111     name = argv[index] + match[1].rm_so;
112     else
113     {
114     printf("Usage: %s [-user=user] [-name=name]\n", argv[0]);
115    
116     return 1;
117     }
118     }
119    
120     regfree(&user_);
121     regfree(&name_);
122     authenticate(argv[0], environment);
123    
124     if (!strlen(user))
125     {
126     regcheck(regcomp(&user_, "^[a-z0-9]{1,16}$", REG_EXTENDED), &user_, environment);
127    
128     do
129     {
130     printf("User Name: ");
131    
132     size_t size;
133     char *user_ = fcheck(fgetln(stdin, &size), stdin, environment);
134    
135     strlcpy(user, user_, size < sizeof (user) ? size : sizeof (user));
136     }
137     while (regcheck(regexec(&user_, user, 0, NULL, 0), &user_, environment) || (exists(argv[0], user, environment) && fprintf(stderr, "%s: User exists\n", argv[0])));
138    
139     regfree(&user_);
140     }
141     else if (exists(argv[0], user, environment))
142     longjmp(environment, (int)"User exists");
143    
144     if (!name)
145     {
146     regcheck(regcomp(&name_, "^[^:]+$", REG_EXTENDED), &name_, environment);
147     get("Full Name", &name_, &name, environment);
148     regfree(&name_);
149     }
150    
151     char password[_PASSWORD_LEN];
152    
153     getpassword(password, environment);
154     check(setuid(geteuid()), environment);
155    
156     char slappasswd[] = "/tmp/slappasswd.XXXXXX";
157    
158     putpassword(password, slappasswd, environment);
159    
160     int pipe_[2];
161    
162     check(pipe(pipe_), environment);
163    
164     pid_t bash_;
165    
166     if (!(bash_ = check(fork(), environment)))
167     {
168     check(dup2(pipe_[0], 0), environment);
169     check(close(pipe_[1]), environment);
170     check(setenv("USER", user, 1), environment);
171     check(setenv("NAME", name, 1), environment);
172     check(setenv("FILE", slappasswd, 1), environment);
173     setshells(shells + bash, environment);
174     check(execl("/ccs/bin/adduser.sh", argv[0], NULL), environment);
175     }
176    
177     check(close(pipe_[0]), environment);
178    
179     FILE *smbpasswd = fdopen(pipe_[1], "w");
180    
181     if (fprintf(smbpasswd, "%s\n%s\n", password, password) < 0)
182     longjmp(environment, 1);
183    
184     if (fclose(smbpasswd))
185     longjmp(environment, 1);
186    
187     int status;
188    
189     check(waitpid(bash_, &status, 0), environment);
190     check(unlink(slappasswd), environment);
191    
192     if (WEXITSTATUS(status))
193     return 1;
194    
195     struct passwd *entry = getpwnam(user);
196    
197     if (!entry)
198     longjmp(environment, 1);
199    
200     check(mkdir(entry->pw_dir, S_IRWXU | S_IRWXG | S_IRWXO), environment);
201     check(chown(entry->pw_dir, entry->pw_uid, entry->pw_gid), environment);
202     check(chdir(entry->pw_dir), environment);
203    
204     FTS *traversal = fts_open((char *[]){ "/usr/share/skel", "/ccs/skel", NULL }, FTS_LOGICAL | FTS_NOCHDIR, NULL);
205    
206     if (!traversal)
207     longjmp(environment, 1);
208    
209     FTSENT *entity;
210    
211     while ((entity = fts_read(traversal)))
212     switch (entity->fts_info)
213     {
214     case FTS_D:
215     if (entity->fts_level != FTS_ROOTLEVEL)
216     {
217     check(mkdir(entity->fts_name, entity->fts_statp->st_mode), environment);
218     check(chown(entity->fts_name, entry->pw_uid, entry->pw_gid), environment);
219     check(chdir(entity->fts_name), environment);
220     }
221    
222     break;
223     case FTS_DP:
224     if (entity->fts_level != FTS_ROOTLEVEL)
225     check(chdir(".."), environment);
226    
227     break;
228     case FTS_F:
229     {
230     int output = check(open(strncmp(entity->fts_name, "dot", 3) ? entity->fts_name : entity->fts_name + 3, O_WRONLY | O_CREAT | O_EXCL, entity->fts_statp->st_mode), environment), input = check(open(entity->fts_accpath, O_RDONLY), environment);
231     char buffer[entity->fts_statp->st_blksize];
232     size_t size;
233    
234     while ((size = check(read(input, buffer, sizeof (buffer)), environment)))
235     check(write(output, buffer, size), environment);
236    
237     check(fchown(output, entry->pw_uid, entry->pw_gid), environment);
238     check(close(output), environment);
239     check(close(input), environment);
240     }
241    
242     break;
243     }
244    
245     if (errno)
246     longjmp(environment, 1);
247    
248     if (fts_close(traversal))
249     longjmp(environment, 1);
250     #else
251     longjmp(environment, (int)"Log in to " MASTER " to add users");
252     #endif
253    
254     return 0;
255     }

Properties

Name Value
svn:keywords Id