radcli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,278 @@
1
+ #include <radcli.h>
2
+
3
+ VALUE c_adenroll;
4
+
5
+ // Free function for the Adcli::AdEnroll class.
6
+ static void radenroll_free (RUBY_ADENROLL *ptr) {
7
+ if(!ptr)
8
+ return;
9
+
10
+ adcli_enroll_unref (ptr->enroll);
11
+
12
+ free (ptr);
13
+ }
14
+
15
+ // Allocation function for the Adcli:AdEnroll class.
16
+ static VALUE radenroll_allocate (VALUE klass) {
17
+ RUBY_ADENROLL *ptr = malloc (sizeof(RUBY_ADENROLL));
18
+
19
+ memset (ptr, 0, sizeof(RUBY_ADENROLL));
20
+
21
+ return Data_Wrap_Struct (klass, 0, radenroll_free, ptr);
22
+ }
23
+
24
+ /*
25
+ * call-seq:
26
+ * conn_object = Adcli::AdConn.connect('YOUR.REALM.COM')
27
+ * Adcli::AdEnroll.new(conn_object)
28
+ *
29
+ * Creates and returns a new Adcli::AdEnroll object.
30
+ *
31
+ */
32
+ static VALUE radenroll_initialize (VALUE self, VALUE ad_conn) {
33
+ RUBY_ADCONN *ptr_conn;
34
+ RUBY_ADENROLL *ptr_enroll;
35
+
36
+ Data_Get_Struct (ad_conn, RUBY_ADCONN, ptr_conn);
37
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
38
+
39
+ adcli_enroll *enroll = adcli_enroll_new (ptr_conn->conn);
40
+ ptr_enroll->enroll = enroll;
41
+
42
+ return self;
43
+ }
44
+
45
+ static VALUE radenroll_get_host_fqdn (VALUE self) {
46
+ RUBY_ADENROLL *ptr_enroll;
47
+ const char *c_host_fqdn = NULL;
48
+
49
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
50
+
51
+ c_host_fqdn = adcli_enroll_get_host_fqdn(ptr_enroll->enroll);
52
+
53
+ return rb_str_new_cstr (c_host_fqdn);
54
+ }
55
+
56
+ static VALUE radenroll_set_host_fqdn (VALUE self, VALUE value) {
57
+ RUBY_ADENROLL *ptr_enroll;
58
+ adcli_enroll *enroll;
59
+ Check_Type(value, T_STRING);
60
+ const char *c_fqdn = StringValuePtr (value);
61
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
62
+
63
+ enroll = ptr_enroll->enroll;
64
+ adcli_enroll_set_host_fqdn (enroll, c_fqdn);
65
+
66
+ return self;
67
+ }
68
+
69
+
70
+ /*
71
+ * call-seq:
72
+ *
73
+ * adenroll.get_computer_name # => hostname.your.realm.com
74
+ *
75
+ * Creates and returns the computer name.
76
+ *
77
+ */
78
+ static VALUE radenroll_get_computer_name (VALUE self) {
79
+ RUBY_ADENROLL *ptr_enroll;
80
+ const char *c_computer_name = NULL;
81
+
82
+ Data_Get_Struct (self ,RUBY_ADENROLL, ptr_enroll);
83
+
84
+ c_computer_name = adcli_enroll_get_computer_name (ptr_enroll->enroll);
85
+
86
+ return rb_str_new_cstr (c_computer_name);
87
+ }
88
+
89
+ /*
90
+ * call-seq:
91
+ *
92
+ * adenroll.set_computer_name('hostname')
93
+ *
94
+ * Set the computer name to do the enroll operation on (join or delete).
95
+ *
96
+ */
97
+ static VALUE radenroll_set_computer_name (VALUE self, VALUE value) {
98
+ RUBY_ADENROLL *ptr_enroll;
99
+ adcli_enroll *enroll;
100
+
101
+
102
+ Check_Type(value, T_STRING);
103
+
104
+ const char *c_value = StringValuePtr (value);
105
+
106
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
107
+
108
+ enroll = ptr_enroll->enroll;
109
+ adcli_enroll_set_computer_name (enroll, c_value);
110
+
111
+ return self;
112
+ }
113
+
114
+ /*
115
+ * call-seq:
116
+ *
117
+ * adenroll.set_domain_ou('OU=Testing,DC=domain,DC=example,DC=com')
118
+ *
119
+ * Set the domain organizational unit.
120
+ *
121
+ */
122
+ static VALUE radenroll_set_domain_ou (VALUE self, VALUE value) {
123
+ RUBY_ADENROLL *ptr_enroll;
124
+ adcli_enroll *enroll;
125
+
126
+ Check_Type(value, T_STRING);
127
+
128
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
129
+
130
+ const char *c_value = StringValuePtr(value);
131
+ enroll = ptr_enroll->enroll;
132
+
133
+ adcli_enroll_set_domain_ou(enroll, c_value);
134
+
135
+ return self;
136
+ }
137
+
138
+ /*
139
+ * call-seq:
140
+ *
141
+ * adenroll.get_computer_password
142
+ *
143
+ * Gets the computer password
144
+ *
145
+ */
146
+ static VALUE radenroll_get_computer_password (VALUE self) {
147
+ RUBY_ADENROLL *ptr_enroll;
148
+ const char *c_computer_password = NULL;
149
+
150
+ Data_Get_Struct (self ,RUBY_ADENROLL, ptr_enroll);
151
+
152
+ c_computer_password = adcli_enroll_get_computer_password (ptr_enroll->enroll);
153
+
154
+ return rb_str_new_cstr (c_computer_password);
155
+ }
156
+
157
+ /*
158
+ * call-seq:
159
+ *
160
+ * adenroll.set_computer_password('computer-password')
161
+ *
162
+ * Sets the computer password
163
+ *
164
+ */
165
+ static VALUE radenroll_set_computer_password (VALUE self, VALUE value) {
166
+ RUBY_ADENROLL *ptr_enroll;
167
+ Check_Type(value, T_STRING);
168
+
169
+ adcli_enroll *enroll;
170
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
171
+
172
+ enroll = ptr_enroll->enroll;
173
+ const char *c_value = StringValuePtr (value);
174
+
175
+ adcli_enroll_set_computer_password (enroll, c_value);
176
+
177
+ return self;
178
+ }
179
+
180
+ /*
181
+ * call-seq:
182
+ *
183
+ * adenroll.join
184
+ *
185
+ * Joins a new computer to the Active Directory domain. Creates a machine account and sets a password for it.
186
+ *
187
+ */
188
+ static VALUE radenroll_join (VALUE self) {
189
+ RUBY_ADENROLL *ptr_enroll;
190
+ adcli_result result;
191
+
192
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
193
+
194
+ result = adcli_enroll_join (ptr_enroll->enroll, ADCLI_ENROLL_NO_KEYTAB);
195
+
196
+ if(result != ADCLI_SUCCESS) {
197
+ rb_raise(rb_eRuntimeError, "%s", adcli_get_last_error());
198
+ }
199
+
200
+ return self;
201
+ }
202
+
203
+ /*
204
+ * call-seq
205
+ *
206
+ * adenroll.password
207
+ *
208
+ * Updates a computer password
209
+ *
210
+ */
211
+ static VALUE radenroll_password (VALUE self) {
212
+ RUBY_ADENROLL *ptr_enroll;
213
+ adcli_result result;
214
+
215
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
216
+
217
+ result = adcli_enroll_password (ptr_enroll->enroll, ADCLI_ENROLL_PASSWORD_VALID);
218
+
219
+ if (result != ADCLI_SUCCESS) {
220
+ rb_raise(rb_eRuntimeError, "%s", adcli_get_last_error());
221
+ }
222
+
223
+ return self;
224
+ }
225
+
226
+
227
+ /*
228
+ * call-seq:
229
+ *
230
+ * adenroll.delete
231
+ *
232
+ * Deletes a existing computer from the Active Directory domain
233
+ *
234
+ */
235
+ static VALUE radenroll_delete (VALUE self) {
236
+ RUBY_ADENROLL *ptr_enroll;
237
+ adcli_result result;;
238
+
239
+ Data_Get_Struct (self, RUBY_ADENROLL, ptr_enroll);
240
+
241
+ result = adcli_enroll_delete (ptr_enroll->enroll, 0);
242
+
243
+ if (result != ADCLI_SUCCESS) {
244
+ rb_raise(rb_eRuntimeError, "%s", adcli_get_last_error());
245
+ }
246
+
247
+ return self;
248
+ }
249
+
250
+ void Init_AdEnroll()
251
+ {
252
+ c_adenroll = rb_define_class_under (m_adcli, "AdEnroll", rb_cObject);
253
+ c_adenroll_exception = rb_define_class_under (m_adcli, "Exception", rb_eStandardError);
254
+
255
+ // Allocate functions
256
+ rb_define_alloc_func (c_adenroll, radenroll_allocate);
257
+
258
+ // AdEnroll methods
259
+ rb_define_method (c_adenroll, "initialize", radenroll_initialize, 1);
260
+
261
+ rb_define_method (c_adenroll, "get_host_fqdn", radenroll_get_host_fqdn, 0);
262
+ rb_define_method (c_adenroll, "set_host_fqdn", radenroll_set_host_fqdn, 1);
263
+
264
+ rb_define_method (c_adenroll, "get_computer_name", radenroll_get_computer_name, 0);
265
+ rb_define_method (c_adenroll, "set_computer_name", radenroll_set_computer_name, 1);
266
+
267
+ rb_define_method (c_adenroll, "set_domain_ou", radenroll_set_domain_ou, 1);
268
+
269
+ rb_define_method (c_adenroll, "get_computer_password", radenroll_get_computer_password, 0);
270
+ rb_define_method (c_adenroll, "set_computer_password", radenroll_set_computer_password, 1);
271
+
272
+ rb_define_method (c_adenroll, "join", radenroll_join, 0);
273
+ rb_define_method (c_adenroll, "password", radenroll_password, 0);
274
+ rb_define_method (c_adenroll, "delete", radenroll_delete, 0);
275
+
276
+
277
+
278
+ }
data/radcli.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'radcli'
5
+ spec.version = '0.1.0'
6
+ spec.authors = ['Mårten Cassel']
7
+ spec.license = 'Artistic-2.0'
8
+ spec.email = ['marten.cassel@gmail.com']
9
+ spec.homepage = 'http://github.com/martencassel/radcli'
10
+ spec.summary = 'A Ruby interface for the adcli library'
11
+ spec.test_files = Dir['test/test*']
12
+ spec.extensions = ['ext/radcli/extconf.rb']
13
+ spec.files = `git ls-files`.split("\n").reject { |f| f.include?('git') }
14
+
15
+ spec.extra_rdoc_files = ['README.md', 'CHANGES', 'MANIFEST'] + Dir['ext/radcli/*.c']
16
+
17
+ spec.add_dependency('rake-compiler')
18
+
19
+ spec.add_development_dependency('test-unit', '>= 2.1.0')
20
+
21
+ spec.description = <<-EOF
22
+ The radcli library provides a Ruby interface for performing actions on
23
+ a Active Directory domain using the realmd/adcli tool library.
24
+ EOF
25
+ end
26
+
data/scripts/delete.rb ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'radcli'
4
+
5
+ adconn = Adcli::AdConn.new("example.com")
6
+ adconn.set_login_user("Administrator")
7
+ adconn.set_user_password("password")
8
+ adconn.set_domain_realm("EXAMPLE.COM")
9
+ adconn.set_domain_controller("dc.example.com")
10
+ res = adconn.connect
11
+
12
+ enroll = Adcli::AdEnroll.new(adconn)
13
+ enroll.set_computer_name("server")
14
+
15
+ begin
16
+ enroll.delete()
17
+ rescue => error
18
+ puts error
19
+ end
20
+
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'radcli'
4
+
5
+ # Couldn't resolve host name: foo.example.com: Name or service not known
6
+
7
+ adconn = Adcli::AdConn.new("example.com")
8
+ adconn.set_login_user("Administrator")
9
+ adconn.set_user_password("password")
10
+ adconn.set_domain_realm("EXAMPLE.COM")
11
+ adconn.set_domain_controller("foo.example.com")
12
+
13
+ begin
14
+ res = adconn.connect
15
+ rescue => error
16
+ puts error
17
+ end
18
+
19
+ # Couldn't get kerberos ticket for: Administrator@EXAMPLE.INCORRECT: Cannot find KDC for realm "EXAMPLE.INCORRECT"
20
+
21
+ adconn = Adcli::AdConn.new("example.com")
22
+ adconn.set_login_user("Administrator")
23
+ adconn.set_user_password("password")
24
+ adconn.set_domain_realm("EXAMPLE.INCORRECT")
25
+ adconn.set_domain_controller("dc.example.com")
26
+
27
+ begin
28
+ res = adconn.connect
29
+ rescue => error
30
+ puts error
31
+ end
32
+
33
+ # Couldn't authenticate as: Administrator@EXAMPLE.COM: Preauthentication failed
34
+
35
+ adconn = Adcli::AdConn.new("example.com")
36
+ adconn.set_login_user("Administrator")
37
+ adconn.set_user_password("incorrect")
38
+ adconn.set_domain_realm("EXAMPLE.COM")
39
+ adconn.set_domain_controller("dc.example.com")
40
+
41
+ begin
42
+ res = adconn.connect
43
+ rescue => error
44
+ puts error
45
+ end
46
+
data/scripts/join.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'radcli'
4
+
5
+ adconn = Adcli::AdConn.new("example.com")
6
+ adconn.set_login_user("Administrator")
7
+ adconn.set_user_password("password")
8
+ adconn.set_domain_realm("EXAMPLE.COM")
9
+ adconn.set_domain_controller("dc.example.com")
10
+ res = adconn.connect
11
+
12
+ enroll = Adcli::AdEnroll.new(adconn)
13
+ enroll.set_host_fqdn("server.example.com")
14
+ enroll.set_computer_name("server")
15
+ enroll.set_computer_password("password")
16
+
17
+ begin
18
+ enroll.join()
19
+ rescue => error
20
+ puts error
21
+ end
22
+
data/scripts/reset.rb ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'radcli'
4
+
5
+ adconn = Adcli::AdConn.new("example.com")
6
+ adconn.set_login_user("Administrator")
7
+ adconn.set_user_password("password")
8
+ adconn.set_domain_realm("EXAMPLE.COM")
9
+ adconn.set_domain_controller("dc.example.com")
10
+ res = adconn.connect
11
+
12
+ enroll = Adcli::AdEnroll.new(adconn)
13
+ enroll.set_computer_name("serverixx")
14
+ enroll.set_computer_password("newpass")
15
+
16
+ begin
17
+ enroll.password()
18
+ rescue => error
19
+ puts error
20
+ end
21
+
22
+
23
+ # kinit server@EXAMPLE.COM
24
+ # >newpass
@@ -0,0 +1,110 @@
1
+ ########################################################################
2
+ # test_radconn.rb
3
+ #
4
+ # Test suite for the Adcli::Adconn class.
5
+ #
6
+ require 'rubygems'
7
+ gem 'test-unit'
8
+
9
+ require 'test/unit'
10
+ require 'radcli'
11
+
12
+ class TC_Adconn < Test::Unit::TestCase
13
+ def setup
14
+ @domain = "TEST.DOMAIN"
15
+ @realm = "TEST.REALM"
16
+ @domain_controller = "TEST-DC"
17
+ @ccache_name = "testccache"
18
+ @login_user = "testuser"
19
+ @user_password = "testpassword"
20
+
21
+ @conn = Adcli::AdConn.new(@domain)
22
+ end
23
+
24
+ test "an error is raised on connect if an invalid domain controller is used" do
25
+ @conn.set_login_user("Administrator")
26
+ @conn.set_user_password("password")
27
+ @conn.set_domain_realm("EXAMPLE.COM")
28
+ @conn.set_domain_controller("foo.example.com")
29
+
30
+ exception = assert_raises(RuntimeError) { @conn.connect }
31
+ assert_equal("Couldn't resolve host name: foo.example.com: Name or service not known", exception.message)
32
+ end
33
+
34
+ test "an error is raised on connect if an invalid realm is used" do
35
+ @conn.set_login_user("Administrator")
36
+ @conn.set_user_password("password")
37
+ @conn.set_domain_realm("EXAMPLE.INCORRECT")
38
+ @conn.set_domain_controller("dc.example.com")
39
+
40
+ exception = assert_raises(RuntimeError) { @conn.connect }
41
+ end
42
+
43
+ test "argument to constructor must be a string" do
44
+ assert_raise(TypeError){ Adcli::AdConn.new(1) }
45
+ assert_raise(TypeError){ Adcli::AdConn.new(true) }
46
+ end
47
+
48
+ test "constructor accepts a domain name" do
49
+ assert_nothing_raised{ Adcli::AdConn.new('domain') }
50
+ end
51
+
52
+ test "argument to domain realm setter must be a string" do
53
+ assert_raise(TypeError){ @conn.set_domain_realm(1) }
54
+ assert_raise(TypeError){ @conn.set_domain_realm(true) }
55
+ end
56
+
57
+ test "domain realm setter/getter sets/returns value" do
58
+ @conn.set_domain_realm(@realm)
59
+ realm = @conn.get_domain_realm()
60
+ assert_equal(@realm, realm)
61
+ end
62
+
63
+ test "argument to domain controller setter must be a string" do
64
+ assert_raise(TypeError){ @conn.set_domain_controller(1) }
65
+ assert_raise(TypeError){ @conn.set_domain_controller(true) }
66
+ end
67
+
68
+ test "domain controller setter/getter sets/returns value" do
69
+ @conn.set_domain_controller(@domain_controller)
70
+ domain_controller = @conn.get_domain_controller()
71
+ assert_equal(@domain_controller, domain_controller)
72
+ end
73
+
74
+ test "argument to ccache-name setter must be a string" do
75
+ assert_raise(TypeError){ @conn.set_login_ccache_name(1) }
76
+ assert_raise(TypeError){ @conn.set_login_ccache_name(true) }
77
+ end
78
+
79
+ test "login ccache name setter/getter sets/returns value" do
80
+ @conn.set_login_ccache_name(@ccache_name)
81
+ login_ccache = @conn.get_login_ccache_name()
82
+ assert_equal(@ccache_name, login_ccache)
83
+ end
84
+
85
+ test "argument to login user setter must be a string" do
86
+ assert_raise(TypeError){ @conn.set_login_user(1) }
87
+ assert_raise(TypeError){ @conn.set_login_user(true) }
88
+ end
89
+
90
+ test "login user setter/getter sets/returns value" do
91
+ @conn.set_login_user(@login_user)
92
+ login_user = @conn.get_login_user()
93
+ assert_equal(@login_user, login_user)
94
+ end
95
+
96
+ test "argument to login password setter must be a string" do
97
+ assert_raise(TypeError){ @conn.set_user_password(1) }
98
+ assert_raise(TypeError){ @conn.set_user_password(true) }
99
+ end
100
+
101
+ test "login password setter/getter sets/returns value" do
102
+ @conn.set_user_password(@user_password)
103
+ password = @conn.get_user_password()
104
+ assert_equal(@user_password, password)
105
+ end
106
+
107
+ def teardown
108
+ end
109
+
110
+ end