|
3 | 3 | require 'passgen'
|
4 | 4 |
|
5 | 5 | module Proxy::AdRealm
|
6 |
| - class Provider |
7 |
| - include Proxy::Log |
8 |
| - include Proxy::Util |
9 |
| - include Proxy::Kerberos |
10 |
| - |
11 |
| - def initialize(realm, keytab_path, principal, domain_controller, ou) |
12 |
| - @realm = realm |
13 |
| - @keytab_path = keytab_path |
14 |
| - @principal = principal |
15 |
| - @domain_controller = domain_controller |
16 |
| - @domain = realm.downcase |
17 |
| - @ou = ou |
18 |
| - logger.info "Proxy::AdRealm: initialize... #{@realm}, #{@keytab_path}, #{@principal}, #{@domain_controller}, #{@domain}, #{ou}" |
19 |
| - end |
20 |
| - |
21 |
| - def check_realm realm |
22 |
| - raise Exception.new "Unknown realm #{realm}" unless realm.casecmp(@realm).zero? |
23 |
| - end |
24 |
| - |
25 |
| - def find hostfqdn |
26 |
| - true |
27 |
| - end |
28 |
| - |
29 |
| - def create realm, hostfqdn, params |
30 |
| - logger.info "Proxy::AdRealm: create... #{realm}, #{hostfqdn}, #{params}" |
31 |
| - check_realm realm |
32 |
| - kinit_radcli_connect |
33 |
| - |
34 |
| - password = generate_password |
35 |
| - result = { :randompassword => password } |
36 |
| - |
37 |
| - if params[:rebuild] == "true" |
38 |
| - do_host_rebuild hostfqdn, password |
39 |
| - else |
40 |
| - do_host_create hostfqdn, password |
41 |
| - end |
42 |
| - |
43 |
| - JSON.pretty_generate(result) |
44 |
| - end |
45 |
| - |
46 |
| - def delete realm, hostfqdn |
47 |
| - logger.info "Proxy::AdRealm: delete... #{realm}, #{hostfqdn}" |
48 |
| - kinit_radcli_connect |
49 |
| - check_realm realm |
50 |
| - radcli_delete hostfqdn |
51 |
| - end |
52 |
| - |
53 |
| - private |
54 |
| - |
55 |
| - def hostfqdn_to_hostname host_fqdn |
56 |
| - begin |
57 |
| - host_fqdn_split = host_fqdn.split('.') |
58 |
| - host_fqdn_split[0] |
59 |
| - rescue => e |
60 |
| - logger.debug "hostfqdn_to_hostname error: #{e}" |
61 |
| - raise e |
62 |
| - end |
63 |
| - end |
64 |
| - |
65 |
| - def do_host_create hostfqdn, password |
66 |
| - hostname = hostfqdn_to_hostname hostfqdn |
67 |
| - radcli_join hostfqdn, hostname, password |
68 |
| - end |
69 |
| - |
70 |
| - def do_host_rebuild hostfqdn, password |
71 |
| - hostname = hostfqdn_to_hostname hostfqdn |
72 |
| - radcli_password hostname, password |
73 |
| - |
74 |
| - end |
75 |
| - |
76 |
| - def kinit_radcli_connect |
77 |
| - init_krb5_ccache @keytab_path, @principal |
78 |
| - @adconn = radcli_connect() |
79 |
| - end |
80 |
| - |
81 |
| - def radcli_connect |
82 |
| - # Connect to active directory |
83 |
| - conn = Adcli::AdConn.new(@domain) |
84 |
| - conn.set_domain_realm(@realm) |
85 |
| - conn.set_domain_controller(@domain_controller) |
86 |
| - conn.set_login_ccache_name("") |
87 |
| - conn.connect() |
88 |
| - return conn |
89 |
| - end |
90 |
| - |
91 |
| - def radcli_join hostfqdn, hostname, password |
92 |
| - # Join computer |
93 |
| - enroll = Adcli::AdEnroll.new(@adconn) |
94 |
| - enroll.set_computer_name(hostname) |
95 |
| - enroll.set_host_fqdn(hostfqdn) |
96 |
| - enroll.set_domain_ou(@ou) if @ou |
97 |
| - enroll.set_computer_password(password) |
98 |
| - enroll.join() |
99 |
| - end |
100 |
| - |
101 |
| - def generate_password |
102 |
| - Passgen::generate(:length => 20) |
103 |
| - end |
104 |
| - |
105 |
| - def radcli_password hostname, password |
106 |
| - # Reset a computer's password |
107 |
| - enroll = Adcli::AdEnroll.new(@adconn) |
108 |
| - enroll.set_computer_name(hostname) |
109 |
| - enroll.set_domain_ou(@ou) if @ou |
110 |
| - enroll.set_computer_password(password) |
111 |
| - enroll.password() |
112 |
| - end |
113 |
| - |
114 |
| - def radcli_delete hostname |
115 |
| - # Delete a computer's account |
116 |
| - enroll = Adcli::AdEnroll.new(@adconn) |
117 |
| - enroll.set_computer_name(hostname) |
118 |
| - enroll.set_domain_ou(@ou) if @ou |
119 |
| - enroll.delete() |
120 |
| - end |
| 6 | + class Provider |
| 7 | + include Proxy::Log |
| 8 | + include Proxy::Util |
| 9 | + include Proxy::Kerberos |
| 10 | + |
| 11 | + def initialize(realm, keytab_path, principal, domain_controller, ou) |
| 12 | + @realm = realm |
| 13 | + @keytab_path = keytab_path |
| 14 | + @principal = principal |
| 15 | + @domain_controller = domain_controller |
| 16 | + @domain = realm.downcase |
| 17 | + @ou = ou |
| 18 | + logger.info "Proxy::AdRealm: initialize... #{@realm}, #{@keytab_path}, #{@principal}, #{@domain_controller}, #{@domain}, #{@ou}" |
| 19 | + end |
| 20 | + |
| 21 | + def check_realm(realm) |
| 22 | + raise Exception, "Unknown realm #{realm}" unless realm.casecmp(@realm).zero? |
| 23 | + end |
| 24 | + |
| 25 | + def find(_hostfqdn) |
| 26 | + true |
| 27 | + end |
| 28 | + |
| 29 | + def create(realm, hostfqdn, params) |
| 30 | + logger.info "Proxy::AdRealm: create... #{realm}, #{hostfqdn}, #{params}" |
| 31 | + check_realm(realm) |
| 32 | + kinit_radcli_connect |
| 33 | + |
| 34 | + password = generate_password |
| 35 | + result = { randompassword: password } |
| 36 | + |
| 37 | + if params[:rebuild] == 'true' |
| 38 | + do_host_rebuild(hostfqdn, password) |
| 39 | + else |
| 40 | + do_host_create(hostfqdn, password) |
| 41 | + end |
| 42 | + |
| 43 | + JSON.pretty_generate(result) |
| 44 | + end |
| 45 | + |
| 46 | + def delete(realm, hostfqdn) |
| 47 | + logger.info "Proxy::AdRealm: delete... #{realm}, #{hostfqdn}" |
| 48 | + kinit_radcli_connect |
| 49 | + check_realm(realm) |
| 50 | + radcli_delete(hostfqdn) |
| 51 | + end |
| 52 | + |
| 53 | + private |
| 54 | + |
| 55 | + def hostfqdn_to_hostname(host_fqdn) |
| 56 | + host_fqdn_split = host_fqdn.split('.') |
| 57 | + host_fqdn_split.first |
| 58 | + end |
| 59 | + |
| 60 | + def do_host_create(hostfqdn, password) |
| 61 | + hostname = hostfqdn_to_hostname(hostfqdn) |
| 62 | + radcli_join(hostfqdn, hostname, password) |
| 63 | + end |
| 64 | + |
| 65 | + def do_host_rebuild(hostfqdn, password) |
| 66 | + hostname = hostfqdn_to_hostname hostfqdn |
| 67 | + radcli_password(hostname, password) |
| 68 | + end |
| 69 | + |
| 70 | + def kinit_radcli_connect |
| 71 | + init_krb5_ccache(@keytab_path, @principal) |
| 72 | + @adconn = radcli_connect |
| 73 | + end |
| 74 | + |
| 75 | + def radcli_connect |
| 76 | + # Connect to active directory |
| 77 | + conn = Adcli::AdConn.new(@domain) |
| 78 | + conn.set_domain_realm(@realm) |
| 79 | + conn.set_domain_controller(@domain_controller) |
| 80 | + conn.set_login_ccache_name('') |
| 81 | + conn.connect |
| 82 | + conn |
| 83 | + end |
| 84 | + |
| 85 | + def radcli_join(hostfqdn, hostname, password) |
| 86 | + # Join computer |
| 87 | + enroll = Adcli::AdEnroll.new(@adconn) |
| 88 | + enroll.set_computer_name(hostname) |
| 89 | + enroll.set_host_fqdn(hostfqdn) |
| 90 | + enroll.set_domain_ou(@ou) if @ou |
| 91 | + enroll.set_computer_password(password) |
| 92 | + enroll.join |
| 93 | + end |
| 94 | + |
| 95 | + def generate_password |
| 96 | + Passgen.generate(:length => 20) |
| 97 | + end |
| 98 | + |
| 99 | + def radcli_password(hostname, password) |
| 100 | + # Reset a computer's password |
| 101 | + enroll = Adcli::AdEnroll.new(@adconn) |
| 102 | + enroll.set_computer_name(hostname) |
| 103 | + enroll.set_domain_ou(@ou) if @ou |
| 104 | + enroll.set_computer_password(password) |
| 105 | + enroll.password |
| 106 | + end |
121 | 107 |
|
| 108 | + def radcli_delete(hostname) |
| 109 | + # Delete a computer's account |
| 110 | + enroll = Adcli::AdEnroll.new(@adconn) |
| 111 | + enroll.set_computer_name(hostname) |
| 112 | + enroll.set_domain_ou(@ou) if @ou |
| 113 | + enroll.delete |
122 | 114 | end
|
| 115 | + end |
123 | 116 | end
|
0 commit comments