ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/CCSAdmin/adduser.c
(Generate patch)

Comparing CCSAdmin/adduser.c (file contents):
Revision 806 by douglas, 2006-06-28T19:53:21-07:00 vs.
Revision 807 by douglas, 2006-06-29T07:39:29-07:00

# Line 4 | Line 4
4   //
5   // Add User
6  
7 < #define _BSD_SOURCE
7 > #include "common.h"
8  
9   #include <errno.h>
10 + #include <stdbool.h>
11   #include <sys/resource.h>
12   #include <sys/time.h>
13   #include <sys/types.h>
# Line 15 | Line 16
16  
17   #ifdef _Master_
18   #include <fts.h>
18 #endif
19  
20 < #include "common.h"
20 > static int compare(const char **key, const char **item)
21 > {
22 >        return strcmp(*key, *item);
23 > }
24 >
25 > static bool exists(const char *program, const char *user, jmp_buf environment)
26 > {
27 >        int pipe_[2];
28 >
29 >        check(pipe(pipe_), environment);
30 >
31 >        pid_t ldapsearch;
32 >
33 >        if (!(ldapsearch = check(fork(), environment)))
34 >        {
35 >                check(dup2(pipe_[1], 1), environment);
36 >                check(close(pipe_[0]), environment);
37 >
38 >                char uid[strlen(user) + 5];
39 >
40 >                sprintf(uid, "uid=%s", user);
41 >                check(execl(LDAPSEARCH, program, "-b", "ou=People,dc=ccs,dc=ucsb,dc=edu", "-H", "ldaps://" MASTER, "-LLL", "-x", uid, "1.1", NULL), environment);
42 >        }
43 >
44 >        check(close(pipe_[1]), environment);
45 >
46 >        FILE *ldapsearch_ = fdopen(pipe_[0], "r");
47 >        size_t size;
48 >        char *uid = fcheck(fgetln(ldapsearch_, &size), ldapsearch_, environment);
49 >        int status;
50 >
51 >        check(waitpid(ldapsearch, &status, 0), environment);
52 >
53 >        char user_[strlen(user) + 41];
54 >
55 >        sprintf(user_, "dn: uid=%s,ou=People,dc=ccs,dc=ucsb,dc=edu", user);
56 >
57 >        if (size != 0 && !strncmp(user_, uid, size - 1))
58 >                return true;
59 >
60 >        static const char *users[] = {
61 >        #       include "users.h"
62 >        };
63 >
64 >        if (bsearch(&user, users, sizeof (users) / sizeof (*users), sizeof (*users), (int (*)(const void *, const void *))compare))
65 >                return true;
66 >
67 >        return false;
68 > }
69 > #endif
70  
71   int main(int argc, char *argv[])
72   {
# Line 84 | Line 133 | int main(int argc, char *argv[])
133  
134                          strlcpy(user, user_, size < sizeof (user) ? size : sizeof (user));
135                  }
136 <                while (regcheck(regexec(&user_, user, 0, NULL, 0), &user_, environment));
136 >                while (regcheck(regexec(&user_, user, 0, NULL, 0), &user_, environment) || (exists(argv[0], user, environment) && fprintf(stderr, "%s: User exists\n", argv[0])));
137  
138                  regfree(&user_);
139          }
140 +        else if (exists(argv[0], user, environment))
141 +                longjmp(environment, (int)"User exists");
142  
143          if (!name)
144          {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines