ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/ccs/admin/admin.c
Revision: 600
Committed: 2009-10-15T23:42:17-07:00 (15 years, 8 months ago) by douglas
Content type: text/x-c
File size: 3560 byte(s)
Log Message:
Improving the build system...

File Contents

# User Rev Content
1 douglas 597 // College of Creative Studies
2     // Admin
3     //
4     // Douglas Thrift
5     //
6     // $Id$
7    
8     #if !defined(__FreeBSD__) && !defined(__sun__) && !defined(__APPLE__)
9     # ifndef _BSD_SOURCE
10     # define _BSD_SOURCE
11     # endif
12     #endif
13    
14     #include <assert.h>
15     #include <libgen.h>
16     #include <setjmp.h>
17     #include <stdio.h>
18     #include <stdlib.h>
19     #include <unistd.h>
20     #include <sys/types.h>
21     #include <pwd.h>
22    
23     #define LDAP_DEPRECATED 1
24    
25     #include <ldap.h>
26    
27     #if !defined(__FreeBSD__) && !defined(__sun__) && !defined(__APPLE__)
28     static inline size_t strlcpy(char *dst, const char *src, size_t size)
29     {
30     dst[--size] = '\0';
31    
32     return strlen(strncpy(dst, src, size));
33     }
34     #endif
35    
36     #define EXCEPTION 1
37     #define POSIX_EXCEPTION 2
38    
39     static char *exception;
40    
41     static inline int posix_check(int value, jmp_buf environment)
42     {
43     if (value == -1)
44     longjmp(environment, POSIX_EXCEPTION);
45    
46     return value;
47     }
48    
49     static inline int ldap_check(int value, jmp_buf environment)
50     {
51     if (value != LDAP_SUCCESS)
52     longjmp(environment, (exception = ldap_err2string(value), EXCEPTION));
53    
54     return value;
55     }
56    
57     int main(int argc, char *argv[])
58     {
59     if (argc < 1)
60     return 1;
61    
62     jmp_buf environment;
63    
64     switch (setjmp(environment))
65     {
66     case 0:
67     break;
68     case EXCEPTION:
69     fprintf(stderr, "%s: %s\n", argv[0], exception);
70    
71     return 1;
72     case POSIX_EXCEPTION:
73     perror(argv[0]);
74    
75     return 1;
76     }
77    
78     char _program[strlen(argv[0])];
79    
80     strcpy(_program, argv[0]);
81    
82     char *program = basename(_program), *programs[] = { "adduser", "chfn", "chsh", "passwd" };
83     LDAP *ldap;
84    
85     if (!program)
86     longjmp(environment, POSIX_EXCEPTION);
87    
88     for (int index = 0; index != sizeof (programs) / sizeof (*programs); ++index)
89     if (!strcmp(program, programs[index]))
90     goto go;
91    
92     fprintf(stderr, "%s: Unknown command: %s.\n", argv[0], program);
93    
94     return 1;
95    
96 douglas 600 go: if (ldap_initialize(&ldap, MASTER_URI " " SLAVE_URI) != LDAP_SUCCESS)
97 douglas 597 longjmp(environment, POSIX_EXCEPTION);
98    
99     int version = LDAP_VERSION3, demand = LDAP_OPT_X_TLS_DEMAND;
100    
101     assert(ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &version) == LDAP_OPT_SUCCESS);
102     assert(ldap_set_option(ldap, LDAP_OPT_X_TLS_CACERTFILE, "/ccs/ssl/ccscert.pem") == LDAP_OPT_SUCCESS);
103     assert(ldap_set_option(ldap, LDAP_OPT_X_TLS_REQUIRE_CERT, &demand) == LDAP_OPT_SUCCESS);
104    
105     uid_t uid = getuid();
106     struct passwd *entry = getpwuid(uid);
107    
108     if (!entry)
109     longjmp(environment, POSIX_EXCEPTION);
110    
111     size_t length = strlen(entry->pw_name);
112 douglas 600 char user[length + 1], dn[length + 5 + sizeof (PEOPLE)], prompt[length + 16];
113 douglas 597
114     strlcpy(user, entry->pw_name, sizeof (user));
115 douglas 600 snprintf(dn, sizeof (dn), "uid=%s," PEOPLE, user);
116 douglas 597 snprintf(prompt, sizeof (prompt), "Password for %s: ", user);
117    
118     char *password = getpass(prompt);
119    
120     ldap_check(ldap_simple_bind_s(ldap, dn, password), environment);
121 douglas 598 ldap_check(ldap_unbind_s(ldap), environment);
122 douglas 597
123     if (!strcmp(program, "passwd") && uid != 0)
124     {
125     char old_password[] = "/tmp/old_password.XXXXXX";
126     int file = posix_check(mkstemp(old_password), environment);
127    
128     posix_check(write(file, password, strlen(password)), environment);
129     posix_check(close(file), environment);
130     posix_check(setenv("OLD_PASSWORD", old_password, 1), environment);
131     }
132     else
133     posix_check(unsetenv("OLD_PASSWORD"), environment);
134    
135     bzero(password, strlen(password));
136    
137     char python[strlen(program) + 13];
138    
139     snprintf(python, sizeof (python), "/ccs/lib/%s.py", program);
140     posix_check(setenv("USER", user, 1), environment);
141     posix_check(unsetenv("PYTHONPATH"), environment);
142     posix_check(unsetenv("LD_LIBRARY_PATH"), environment);
143     posix_check(setuid(geteuid()), environment);
144     posix_check(execv(python, argv), environment);
145    
146     return 0;
147     }

Properties

Name Value
svn:keywords Id