Skip to content

Commit ef6997b

Browse files
committed
sudo interface for pony to generate CSRs
Legacy-ID: 2834
1 parent 40b933c commit ef6997b

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/python2
2+
3+
from __future__ import print_function
4+
5+
import ldap
6+
import ldap.filter
7+
from OpenSSL import crypto
8+
import sys
9+
10+
# Validate arguments
11+
if len(sys.argv) < 3:
12+
exit('usage: gencsr-pony LOCKER HOSTNAME [HOSTNAME...]')
13+
14+
[progname, locker], hostnames = sys.argv[:2], sys.argv[2:]
15+
16+
if any(hostname for hostname in hostnames if '.' not in hostname):
17+
exit('error: Hostnames must be fully qualified')
18+
19+
# Connect to LDAP
20+
ll = ldap.initialize('ldapi://%2fvar%2frun%2fslapd-scripts.socket/')
21+
with open('/etc/signup-ldap-pw') as pw_file:
22+
ll.simple_bind_s('cn=Directory Manager', pw_file.read())
23+
24+
# Verify hostname existence and ownership
25+
locker_dn = ldap.dn.dn2str([[('uid', locker, 1)], [('ou', 'People', 1)], [('dc', 'scripts', 1)], [('dc', 'mit', 1)], [('dc', 'edu', 1)]])
26+
search_hostnames = set(hostnames)
27+
while search_hostnames:
28+
res = ll.search_s(
29+
'ou=VirtualHosts,dc=scripts,dc=mit,dc=edu',
30+
ldap.SCOPE_SUBTREE,
31+
ldap.filter.filter_format(
32+
'(&(objectClass=scriptsVhost)(|' +
33+
'(scriptsVhostName=%s)' * len(search_hostnames) +
34+
'(scriptsVhostAlias=%s)' * len(search_hostnames) +
35+
'))',
36+
list(search_hostnames) * 2),
37+
['scriptsVhostName', 'scriptsVhostAlias', 'scriptsVhostAccount'])
38+
search_hostnames -= {h for cn, attrs in res if attrs['scriptsVhostAccount'] == [locker_dn] for h in attrs['scriptsVhostName'] + attrs.get('scriptsVhostAlias', [])}
39+
if '*' in search_hostnames or search_hostnames & {h for cn, attrs in res for h in attrs['scriptsVhostName'] + attrs.get('scriptsVhostAlias', [])}:
40+
exit('error: Hostnames must exist and be owned by the specified locker')
41+
42+
# Strip one hostname component and try again with wildcards (foo.bar.baz -> *.bar.baz -> *.baz -> *)
43+
search_hostnames = {'.'.join(['*'] + hostname.split('.')[1 + hostname.startswith('*.'):]) for hostname in search_hostnames}
44+
45+
# Create a CSR
46+
req = crypto.X509Req()
47+
48+
subject = req.get_subject()
49+
subject.countryName = 'US'
50+
subject.stateOrProvinceName = 'Massachusetts'
51+
subject.localityName = 'Cambridge'
52+
subject.organizationName = 'Massachusetts Institute of Technology'
53+
subject.organizationalUnitName = 'scripts.mit.edu web hosting service'
54+
subject.CN = hostnames[0]
55+
56+
req.add_extensions([
57+
crypto.X509Extension('basicConstraints', False, 'CA:FALSE'),
58+
crypto.X509Extension('keyUsage', False, 'nonRepudiation, digitalSignature, keyEncipherment'),
59+
crypto.X509Extension('subjectAltName', False, ', '.join('DNS:' + hostname for hostname in hostnames)),
60+
])
61+
62+
# Add the private key, and sign the CSR
63+
with open('/etc/pki/tls/private/scripts-2048.key') as key_file:
64+
private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, key_file.read())
65+
66+
req.set_pubkey(private_key)
67+
req.sign(private_key, 'sha256')
68+
69+
print(end=crypto.dump_certificate_request(crypto.FILETYPE_PEM, req))

server/fedora/config/etc/sudoers

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ scripts ALL=(root) NOPASSWD: /usr/local/sbin/ldap-backup ""
7373
scripts ALL=(root) NOPASSWD: /usr/local/sbin/get-homedirs ""
7474
scripts ALL=(root) NOPASSWD: /etc/httpd/export-scripts-certs ""
7575
nrpe ALL=(signup) NOPASSWD: /etc/nagios/check_ldap_mmr.real
76+
pony ALL=(root) NOPASSWD: /etc/pki/tls/gencsr-pony
7677

7778
Defaults:munin !syslog
7879

server/fedora/config/etc/syslog-ng/d_zroot.pl

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ ($)
132132
} elsif ($message =~ m|^Setting tty modes failed: Invalid argument$|) {
133133
} elsif ($message =~ m|^ *nrpe .* COMMAND=/etc/nagios/check_ldap_mmr.real$|) {
134134
} elsif ($message =~ m|^ *scripts : .*; USER=root ; COMMAND=/etc/httpd/export-scripts-certs$|) {
135+
} elsif ($message =~ m|^ *pony : .*; USER=root ; COMMAND=/etc/pki/tls/gencsr-pony |) {
135136
} elsif ($message =~ m|^ *root : TTY=|) {
136137
} elsif ($message =~ m|^Set /proc/self/oom_adj to |) {
137138
} elsif ($message =~ m|^Set /proc/self/oom_score_adj to |) {

0 commit comments

Comments
 (0)