timfel-krb5-auth 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/COPYING +510 -0
  2. data/README +21 -0
  3. data/Rakefile +115 -0
  4. data/TODO +3 -0
  5. data/examples/example.rb +44 -0
  6. data/ext/extconf.rb +7 -0
  7. data/ext/ruby_krb5_auth.c +755 -0
  8. metadata +63 -0
data/README ADDED
@@ -0,0 +1,21 @@
1
+ Ruby bindings for the Kerberos library
2
+
3
+ This is an implementation of Ruby bindings for the Kerberos library.
4
+
5
+ To build a gem, you'll want to:
6
+
7
+ $ rake gem
8
+ $ sudo gem install pkg/krb5-auth-{VERSION}.gem
9
+
10
+ To build an RPM, you'll want to:
11
+
12
+ $ rake rpm
13
+ $ sudo rpm -Uvh pkg/{i386,x86_64}/rubygem-krb5-auth-{VERSION}.{i386,x86_64}.rpm
14
+
15
+ and then install the resulting RPM.
16
+
17
+ To build it just for development, you'll want to:
18
+ $ rake build
19
+
20
+ To build the documentation, make sure you have rdoc installed, and then:
21
+ $ rake rdoc
data/Rakefile ADDED
@@ -0,0 +1,115 @@
1
+ # -*- ruby -*-
2
+ # Rakefile: build ruby kerberos bindings
3
+ #
4
+ # Copyright (C) 2008 Red Hat, Inc.
5
+ #
6
+ # Distributed under the GNU Lesser General Public License v2.1 or later.
7
+ # See COPYING for details
8
+ #
9
+ # Chris Lalancette <clalance@redhat.com>
10
+
11
+ # Rakefile for ruby-rpm -*- ruby -*-
12
+ require 'rake/clean'
13
+ require 'rake/rdoctask'
14
+ require 'rake/testtask'
15
+ require 'rake/gempackagetask'
16
+
17
+ PKG_NAME='krb5-auth'
18
+ PKG_VERSION='0.8'
19
+
20
+ EXT_CONF='ext/extconf.rb'
21
+ MAKEFILE='ext/Makefile'
22
+ KRB5AUTH_MODULE='ext/krb5_auth.so'
23
+ SPEC_FILE='rubygem-krb5-auth.spec'
24
+ KRB5AUTH_SRC='ext/ruby_krb5_auth.c'
25
+
26
+ CLEAN.include [ "ext/*.o", KRB5AUTH_MODULE ]
27
+ CLOBBER.include [ "ext/mkmf.log", MAKEFILE ]
28
+
29
+ #
30
+ # Build locally
31
+ #
32
+ file MAKEFILE => EXT_CONF do |t|
33
+ Dir::chdir(File::dirname(EXT_CONF)) do
34
+ unless sh "ruby #{File::basename(EXT_CONF)}"
35
+ $stderr.puts "Failed to run extconf"
36
+ break
37
+ end
38
+ end
39
+ end
40
+ file KRB5AUTH_MODULE => [ MAKEFILE, KRB5AUTH_SRC ] do |t|
41
+ Dir::chdir(File::dirname(EXT_CONF)) do
42
+ unless sh "make"
43
+ $stderr.puts "make failed"
44
+ break
45
+ end
46
+ end
47
+ end
48
+ desc "Build the native library"
49
+ task :build => KRB5AUTH_MODULE
50
+
51
+ Rake::RDocTask.new(:rdoc) do |rd|
52
+ rd.rdoc_dir = "doc"
53
+ rd.rdoc_files.include("ext/*.c")
54
+ end
55
+
56
+ #
57
+ # Package tasks
58
+ #
59
+
60
+ PKG_FILES = FileList[
61
+ "README",
62
+ "examples/example.rb",
63
+ "ext/extconf.rb",
64
+ "ext/ruby_krb5_auth.c",
65
+ "test/test_krb5.rb",
66
+ "Rakefile"
67
+ ]
68
+
69
+ SPEC = Gem::Specification.new do |s|
70
+ s.name = PKG_NAME
71
+ s.version = PKG_VERSION
72
+ s.email = "clalance@redhat.com"
73
+ s.homepage = "http://rubyforge.org/projects/krb5-auth/"
74
+ s.summary = "Kerberos binding for Ruby"
75
+ s.license = "LGPL"
76
+ s.files = PKG_FILES
77
+ s.autorequire = "Krb5Auth"
78
+ s.require_paths = [ "ext" ]
79
+ s.extensions = "ext/extconf.rb"
80
+ s.author = "Chris Lalancette"
81
+ s.platform = Gem::Platform::RUBY
82
+ s.has_rdoc = true
83
+ end
84
+
85
+ Rake::GemPackageTask.new(SPEC) do |pkg|
86
+ pkg.need_tar = true
87
+ pkg.need_zip = true
88
+ end
89
+
90
+ desc "Build (S)RPM for #{PKG_NAME}"
91
+ task :rpm => [ :package ] do |t|
92
+ system("sed -e 's/@VERSION@/#{PKG_VERSION}/' #{SPEC_FILE} > pkg/#{SPEC_FILE}")
93
+ Dir::chdir("pkg") do |dir|
94
+ dir = File::expand_path(".")
95
+ system("rpmbuild --define '_topdir #{dir}' --define '_sourcedir #{dir}' --define '_srcrpmdir #{dir}' --define '_rpmdir #{dir}' --define '_builddir #{dir}' -ba #{SPEC_FILE} > rpmbuild.log 2>&1")
96
+ if $? != 0
97
+ raise "rpmbuild failed"
98
+ end
99
+ end
100
+ end
101
+
102
+ #
103
+ # Default
104
+ #
105
+
106
+ desc "Default task: build all"
107
+ task :default => [ :build, :rdoc, :rpm ] do |t|
108
+ end
109
+
110
+ Rake::TestTask.new(:test) do |test|
111
+ task :test => [:build]
112
+ test.libs << 'ext'
113
+ test.warning = true
114
+ test.verbose = true
115
+ end
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ TODO items:
2
+ 1. Finer-grained error reporting
3
+ 2. Automated tests
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'krb5_auth'
3
+ include Krb5Auth
4
+
5
+ krb5 = Krb5.new
6
+
7
+ # get the default realm
8
+ default_realm = krb5.get_default_realm
9
+ puts "Default realm is: " + default_realm
10
+
11
+ # try to cache non-existant data (this should fail and throw an exception)
12
+ begin
13
+ krb5.cache
14
+ rescue Krb5Auth::Krb5::Exception
15
+ puts "Failed caching credentials before obtaining them. Continuing..."
16
+ end
17
+
18
+ # Get initial credentials for the default principal and default keytab
19
+ krb5.get_init_creds_keytab
20
+
21
+ # cache those credentials in the default cache location
22
+ krb5.cache
23
+
24
+ puts "Principal: " + krb5.get_default_principal
25
+
26
+ # List all of the credentials in the cache, and expiration times, etc.
27
+ krb5.list_cache.each do |cred|
28
+ starttime = DateTime.strptime(cred.starttime.to_s, "%s")
29
+ endtime = DateTime.strptime(cred.endtime.to_s, "%s")
30
+ puts "Client: " + cred.client + " Server: " + cred.server + " starttime: " + starttime.strftime("%D %T") + " endtime: " + endtime.strftime("%D %T")
31
+ end
32
+
33
+ # destroy those same credentials from the default cache location
34
+ krb5.destroy
35
+
36
+ # close the object (releases all memory)
37
+ krb5.close
38
+
39
+ # now try to use the object again; this should fail and throw an exception
40
+ begin
41
+ krb5.cache
42
+ rescue Krb5Auth::Krb5::Exception
43
+ puts "Tried to reuse closed object; continuing..."
44
+ end
data/ext/extconf.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ extension_name = 'krb5_auth'
4
+ dir_config(extension_name)
5
+ have_library("c", "main")
6
+ have_library("krb5","krb5_init_context")
7
+ create_makefile('krb5_auth')
@@ -0,0 +1,755 @@
1
+ /*
2
+ * ruby_krb5_auth.c: Ruby bindings for Kerberos authentication
3
+ *
4
+ * Copyright (C) 2008 Red Hat Inc.
5
+ *
6
+ * This library is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * This library is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with this library; if not, write to the Free Software
18
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ *
20
+ * Author: Chris Lalancette <clalance@redhat.com>
21
+ */
22
+
23
+ #include <ruby.h>
24
+ #include <krb5.h>
25
+ #include <stdio.h>
26
+ #include <strings.h>
27
+
28
+ static VALUE mKerberos;
29
+ static VALUE cKrb5;
30
+ static VALUE cCred;
31
+ static VALUE cKrb5_Exception;
32
+
33
+ struct ruby_krb5 {
34
+ krb5_context ctx;
35
+ krb5_creds creds;
36
+ krb5_principal princ;
37
+ };
38
+
39
+ #define OOM_EXCEPT() rb_raise(cKrb5_Exception, "%s", "Error mallocing memory");
40
+ #define NOSTRUCT_EXCEPT() rb_raise(cKrb5_Exception, "%s", "Class not initialized properly (try 'new')");
41
+
42
+ static char *get_string_or_nil(VALUE arg)
43
+ {
44
+ if (TYPE(arg) == T_NIL)
45
+ return NULL;
46
+ else if (TYPE(arg) == T_STRING)
47
+ return StringValueCStr(arg);
48
+ else
49
+ rb_raise(rb_eTypeError, "wrong argument type (expected String or nil)"); return NULL;
50
+ }
51
+
52
+ void Krb5_register_error(int error)
53
+ {
54
+ rb_raise(cKrb5_Exception, "%s", error_message(error));
55
+ }
56
+
57
+ static void kerb_free(void *p)
58
+ {
59
+ struct ruby_krb5 *kerb;
60
+
61
+ if (!p)
62
+ return;
63
+
64
+ kerb = (struct ruby_krb5 *)p;
65
+
66
+ // kerb->creds is not a pointer, so we can't check for NULL; however, the
67
+ // implementation of krb5_free_cred_contents does do NULL checking, so it is
68
+ // safe (at least in the MIT version) to call it unconditionally
69
+ krb5_free_cred_contents(kerb->ctx, &kerb->creds);
70
+ if (kerb->princ)
71
+ krb5_free_principal(kerb->ctx, kerb->princ);
72
+ if (kerb->ctx)
73
+ krb5_free_context(kerb->ctx);
74
+ memset(kerb, 0, sizeof(struct ruby_krb5));
75
+ free(kerb);
76
+ }
77
+
78
+ /*
79
+ * call-seq:
80
+ * new
81
+ *
82
+ * Create a new Krb5Auth::Krb5 object. This must be called before any other
83
+ * methods are called.
84
+ *
85
+ * Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
86
+ */
87
+ static VALUE Krb5_new(VALUE self)
88
+ {
89
+ struct ruby_krb5 *kerb;
90
+ krb5_error_code krbret;
91
+
92
+ kerb = (struct ruby_krb5 *)malloc(sizeof(struct ruby_krb5));
93
+ if (kerb == NULL) {
94
+ OOM_EXCEPT();
95
+ return Qnil;
96
+ }
97
+
98
+ memset(kerb, 0, sizeof(struct ruby_krb5));
99
+
100
+ krbret = krb5_init_context(&kerb->ctx);
101
+ if (krbret) {
102
+ Krb5_register_error(krbret);
103
+ return Qnil;
104
+ }
105
+
106
+ return Data_Wrap_Struct(cKrb5, NULL, kerb_free, kerb);
107
+ }
108
+
109
+ /*
110
+ * call-seq:
111
+ * get_default_realm -> string
112
+ *
113
+ * Call krb5_get_default_realm() to get the default realm. Returns the default realm on success, raises Krb5Auth::Krb5::Exception on failure.
114
+ */
115
+ static VALUE Krb5_get_default_realm(VALUE self)
116
+ {
117
+ struct ruby_krb5 *kerb;
118
+ char *realm;
119
+ VALUE result;
120
+ krb5_error_code krbret;
121
+
122
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
123
+
124
+ if (!kerb) {
125
+ NOSTRUCT_EXCEPT();
126
+ return Qfalse;
127
+ }
128
+
129
+ krbret = krb5_get_default_realm(kerb->ctx, &realm);
130
+
131
+ if (krbret) {
132
+ Krb5_register_error(krbret);
133
+ return Qnil;
134
+ }
135
+
136
+ result = rb_str_new2(realm);
137
+
138
+ free(realm);
139
+
140
+ return result;
141
+ }
142
+
143
+ /*
144
+ * call-seq:
145
+ * get_default_principal -> string
146
+ *
147
+ * Call krb5_cc_get_principal() to get the principal from the default
148
+ * cachefile. Returns the default principal on success, raises a
149
+ * Krb5Auth::Krb5::Exception on failure.
150
+ */
151
+ static VALUE Krb5_get_default_principal(VALUE self)
152
+ {
153
+ struct ruby_krb5 *kerb;
154
+ char *princ_name;
155
+ VALUE result;
156
+ krb5_error_code krbret;
157
+ krb5_ccache cc;
158
+
159
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
160
+ if (!kerb) {
161
+ NOSTRUCT_EXCEPT();
162
+ return Qfalse;
163
+ }
164
+
165
+ krbret = krb5_cc_default(kerb->ctx, &cc);
166
+ if (krbret) {
167
+ Krb5_register_error(krbret);
168
+ return Qfalse;
169
+ }
170
+
171
+ krbret = krb5_cc_get_principal(kerb->ctx, cc, &kerb->princ);
172
+ if (krbret) {
173
+ krb5_cc_close(kerb->ctx, cc);
174
+ Krb5_register_error(krbret);
175
+ return Qnil;
176
+ }
177
+
178
+ krb5_cc_close(kerb->ctx, cc);
179
+
180
+ krbret = krb5_unparse_name(kerb->ctx, kerb->princ, &princ_name);
181
+ if (krbret) {
182
+ Krb5_register_error(krbret);
183
+ return Qnil;
184
+ }
185
+
186
+ result = rb_str_new2(princ_name);
187
+
188
+ free(princ_name);
189
+
190
+ return result;
191
+ }
192
+
193
+ /*
194
+ * call-seq:
195
+ * get_init_creds_password(username, password)
196
+ *
197
+ * Call krb5_get_init_creds_password() to get credentials based on a username
198
+ * and password. Returns true on success, raises Krb5Auth::Krb5::Exception on
199
+ * failure.
200
+ */
201
+ static VALUE Krb5_get_init_creds_password(VALUE self, VALUE _user, VALUE _pass)
202
+ {
203
+ Check_Type(_user,T_STRING);
204
+ Check_Type(_pass,T_STRING);
205
+ char *user = StringValueCStr(_user);
206
+ char *pass = StringValueCStr(_pass);
207
+
208
+ struct ruby_krb5 *kerb;
209
+ krb5_error_code krbret;
210
+
211
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
212
+ if (!kerb) {
213
+ NOSTRUCT_EXCEPT();
214
+ return Qfalse;
215
+ }
216
+
217
+ krbret = krb5_parse_name(kerb->ctx, user, &kerb->princ);
218
+ if (krbret) {
219
+ goto failed_pass;
220
+ }
221
+
222
+ krbret = krb5_get_init_creds_password(kerb->ctx, &kerb->creds, kerb->princ,
223
+ pass, 0, NULL, 0,NULL, NULL);
224
+ if (krbret) {
225
+ goto failed_pass;
226
+ }
227
+
228
+ return Qtrue;
229
+
230
+ failed_pass:
231
+ Krb5_register_error(krbret);
232
+
233
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
234
+ // leave it to shut the compiler up
235
+ return Qfalse;
236
+ }
237
+
238
+ /*
239
+ * call-seq:
240
+ * get_init_creds_keytab([principal][,keytab])
241
+ *
242
+ * Call krb5_get_init_creds_keytab() to get credentials based on a keytab. With no parameters, gets the default principal (probably the username@DEFAULT_REALM) from the default keytab (as configured in /etc/krb5.conf). With one parameter, get the named principal from the default keytab (as configured in /etc/krb5.conf). With two parameters, get the named principal from the named keytab. Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
243
+ */
244
+ static VALUE Krb5_get_init_creds_keytab(int argc, VALUE *argv, VALUE self)
245
+ {
246
+ VALUE princ_val, keytab_val;
247
+ char *princ;
248
+ char *keytab_name;
249
+ struct ruby_krb5 *kerb;
250
+ krb5_error_code krbret;
251
+ krb5_keytab keytab;
252
+
253
+ keytab = NULL;
254
+
255
+ rb_scan_args(argc, argv, "02", &princ_val, &keytab_val);
256
+
257
+ princ = get_string_or_nil(princ_val);
258
+ keytab_name = get_string_or_nil(keytab_val);
259
+
260
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
261
+ if (!kerb) {
262
+ NOSTRUCT_EXCEPT();
263
+ return Qfalse;
264
+ }
265
+
266
+ if (keytab_name != NULL) {
267
+ krbret = krb5_kt_resolve(kerb->ctx, keytab_name, &keytab);
268
+ if (krbret) {
269
+ goto failed_keytab;
270
+ }
271
+ }
272
+ // implicit else: if we weren't passed a keytab name, just leave keytab as
273
+ // NULL to use the default
274
+
275
+ if (princ != NULL) {
276
+ krbret = krb5_parse_name(kerb->ctx, princ, &kerb->princ);
277
+ }
278
+ else {
279
+ // if we weren't passed a principal, we just get the default principal
280
+ // (which is generally the hostname)
281
+ krbret = krb5_sname_to_principal(kerb->ctx, NULL, NULL, KRB5_NT_SRV_HST,
282
+ &kerb->princ);
283
+ }
284
+ if (krbret) {
285
+ goto failed_keytab;
286
+ }
287
+
288
+ krbret = krb5_get_init_creds_keytab(kerb->ctx, &kerb->creds, kerb->princ,
289
+ keytab, 0, NULL, NULL);
290
+ if (krbret) {
291
+ goto failed_keytab;
292
+ }
293
+
294
+ if (keytab)
295
+ krb5_kt_close(kerb->ctx, keytab);
296
+
297
+ return Qtrue;
298
+
299
+ failed_keytab:
300
+ if (keytab)
301
+ krb5_kt_close(kerb->ctx, keytab);
302
+
303
+ Krb5_register_error(krbret);
304
+
305
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
306
+ // leave it to shut the compiler up
307
+ return Qfalse;
308
+ }
309
+
310
+ /*
311
+ * call-seq:
312
+ * change_password(old_password, new_password)
313
+ *
314
+ * Allow user to change their Kerberos password, providing the +old_password+
315
+ * and the +new_password+.
316
+ *
317
+ * Returns true on success, raises a Krb5Auth::Krb5::Exception on failure.
318
+ */
319
+ static VALUE Krb5_change_password(VALUE self, VALUE v_old, VALUE v_new)
320
+ {
321
+ char *oldpass;
322
+ char *newpass;
323
+ int pw_result;
324
+ struct ruby_krb5 *kerb;
325
+ krb5_error_code krbret;
326
+ krb5_data pw_res_string, res_string;
327
+
328
+ Check_Type(v_old, T_STRING);
329
+ Check_Type(v_new, T_STRING);
330
+
331
+ oldpass = StringValueCStr(v_old);
332
+ newpass = StringValueCStr(v_new);
333
+
334
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
335
+
336
+ if(!kerb){
337
+ NOSTRUCT_EXCEPT();
338
+ return Qfalse;
339
+ }
340
+
341
+ krbret = krb5_get_init_creds_password(
342
+ kerb->ctx,
343
+ &kerb->creds,
344
+ kerb->princ,
345
+ oldpass,
346
+ NULL,
347
+ NULL,
348
+ 0,
349
+ "kadmin/changepw",
350
+ NULL
351
+ );
352
+
353
+ if(krbret){
354
+ Krb5_register_error(krbret);
355
+ return Qfalse;
356
+ }
357
+
358
+ krbret = krb5_change_password(
359
+ kerb->ctx,
360
+ &kerb->creds,
361
+ newpass,
362
+ &pw_result,
363
+ &pw_res_string,
364
+ &res_string
365
+ );
366
+
367
+ // 0 is success. Anything else is failure.
368
+
369
+ if(krbret){
370
+ Krb5_register_error(krbret);
371
+ return Qfalse;
372
+ }
373
+
374
+ if(pw_result){
375
+ Krb5_register_error(pw_result);
376
+ return Qfalse;
377
+ }
378
+
379
+ return Qtrue;
380
+ }
381
+
382
+ /*
383
+ * call-seq:
384
+ * set_password(new_password)
385
+ *
386
+ * Call krb5_set_password() to set the password for this credential to
387
+ * new_password.
388
+ *
389
+ * This method requires that the credential for kadmin/changepw@REALM has
390
+ * already been fetched using Krb5#get_init_creds_password or
391
+ * Krb5#get_init_creds_keytab.
392
+ *
393
+ * Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
394
+ *--
395
+ * TODO: This method does not work at the moment.
396
+ */
397
+ static VALUE Krb5_set_password(VALUE self, VALUE _newpass)
398
+ {
399
+ Check_Type(_newpass,T_STRING);
400
+ char *newpass = StringValueCStr(_newpass);
401
+
402
+ struct ruby_krb5 *kerb;
403
+ krb5_error_code krbret;
404
+ int pw_result;
405
+ krb5_data pw_res_string, res_string;
406
+
407
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
408
+
409
+ if(!kerb){
410
+ NOSTRUCT_EXCEPT();
411
+ return Qfalse;
412
+ }
413
+
414
+ // TODO: get kadmin/changepw credentials here.
415
+
416
+ krbret = krb5_set_password(
417
+ kerb->ctx,
418
+ &kerb->creds,
419
+ newpass,
420
+ NULL,
421
+ &pw_result,
422
+ &pw_res_string,
423
+ &res_string
424
+ );
425
+
426
+ if(krbret){
427
+ Krb5_register_error(krbret);
428
+ return Qfalse;
429
+ }
430
+
431
+ if(pw_result){
432
+ Krb5_register_error(pw_result);
433
+ return Qfalse;
434
+ }
435
+
436
+ return Qtrue;
437
+ }
438
+
439
+ /*
440
+ * call-seq:
441
+ * cache([cache_name])
442
+ *
443
+ * Call krb5_cc_store_cred to store credentials in a cachefile. With no parameters, it stores the credentials in the default cachefile. With one parameter, it stores the credentials in the named cachefile. This requires that the credentials have already been fetched via Krb5.get_init_creds_password or Krb5.get_init_creds_keytab. Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
444
+ */
445
+ static VALUE Krb5_cache_creds(int argc, VALUE *argv, VALUE self)
446
+ {
447
+ VALUE cache_val;
448
+ struct ruby_krb5 *kerb;
449
+ krb5_error_code krbret;
450
+ char *cache_name;
451
+ krb5_ccache cc;
452
+
453
+ rb_scan_args(argc, argv, "01", &cache_val);
454
+
455
+ cache_name = get_string_or_nil(cache_val);
456
+
457
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
458
+ if (!kerb) {
459
+ NOSTRUCT_EXCEPT();
460
+ return Qfalse;
461
+ }
462
+
463
+ if (!kerb->princ) {
464
+ // OK, it looks like they are trying to cache credentials that they don't
465
+ // yet have; just throw an exception so we don't segfault later
466
+ rb_raise(cKrb5_Exception, "%s", "Attempting to cache before obtaining credentials");
467
+ return Qfalse;
468
+ }
469
+
470
+ if (cache_name == NULL) {
471
+ krbret = krb5_cc_default(kerb->ctx, &cc);
472
+ }
473
+ else {
474
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
475
+ }
476
+
477
+ if (krbret) {
478
+ goto fail_cache;
479
+ }
480
+
481
+ krbret = krb5_cc_initialize(kerb->ctx, cc, kerb->princ);
482
+ if (krbret) {
483
+ goto fail_free_cc;
484
+ }
485
+
486
+ krbret = krb5_cc_store_cred(kerb->ctx, cc, &kerb->creds);
487
+ if (krbret) {
488
+ goto fail_free_cc;
489
+ }
490
+
491
+ krb5_cc_close(kerb->ctx, cc);
492
+
493
+ return Qtrue;
494
+
495
+ fail_free_cc:
496
+ krb5_cc_close(kerb->ctx, cc);
497
+
498
+ fail_cache:
499
+ Krb5_register_error(krbret);
500
+
501
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
502
+ // leave it to shut the compiler up
503
+ return Qfalse;
504
+ }
505
+
506
+ /*
507
+ * call-seq:
508
+ * list_cache([cache_name]) -> array
509
+ *
510
+ * Call krb5_cc_next_cred to fetch credentials from a cachefile. With no parameters, it fetches the credentials in the default cachefile. With one parameter, it fetches the credentials in the named cachefile. Returns a list of Krb5Auth::Krb5::Cred objects on success, raises Krb5Auth::Krb5::Exception on failure.
511
+ */
512
+ static VALUE Krb5_list_cache_creds(int argc, VALUE *argv, VALUE self)
513
+ {
514
+ VALUE cache_val;
515
+ struct ruby_krb5 *kerb;
516
+ krb5_error_code krbret;
517
+ char *cache_name;
518
+ krb5_ccache cc;
519
+ krb5_cc_cursor cur;
520
+ krb5_creds creds;
521
+ char *name;
522
+ char *sname;
523
+ krb5_ticket *tkt;
524
+ VALUE result;
525
+ VALUE line;
526
+
527
+ rb_scan_args(argc, argv, "01", &cache_val);
528
+
529
+ cache_name = get_string_or_nil(cache_val);
530
+
531
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
532
+ if (!kerb) {
533
+ NOSTRUCT_EXCEPT();
534
+ return Qfalse;
535
+ }
536
+
537
+ if (cache_name == NULL) {
538
+ krbret = krb5_cc_default(kerb->ctx, &cc);
539
+ }
540
+ else {
541
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
542
+ }
543
+
544
+ if (krbret) {
545
+ goto cache_fail_raise;
546
+ }
547
+
548
+ krbret = krb5_cc_start_seq_get(kerb->ctx, cc, &cur);
549
+ if (krbret) {
550
+ goto cache_fail_close;
551
+ }
552
+
553
+ result = rb_ary_new();
554
+ while (!(krbret = krb5_cc_next_cred(kerb->ctx, cc, &cur, &creds))) {
555
+ krbret = krb5_unparse_name(kerb->ctx, creds.client, &name);
556
+ if (krbret) {
557
+ krb5_free_cred_contents(kerb->ctx, &creds);
558
+ break;
559
+ }
560
+ krbret = krb5_unparse_name(kerb->ctx, creds.server, &sname);
561
+ if (krbret) {
562
+ free(name);
563
+ krb5_free_cred_contents(kerb->ctx, &creds);
564
+ break;
565
+ }
566
+ krbret = krb5_decode_ticket(&creds.ticket, &tkt);
567
+ if (krbret) {
568
+ free(sname);
569
+ free(name);
570
+ krb5_free_cred_contents(kerb->ctx, &creds);
571
+ break;
572
+ }
573
+ line = rb_class_new_instance(0, NULL, cCred);
574
+ rb_iv_set(line, "@client", rb_str_new2(name));
575
+ rb_iv_set(line, "@server", rb_str_new2(sname));
576
+ rb_iv_set(line, "@starttime", INT2NUM(creds.times.starttime));
577
+ rb_iv_set(line, "@authtime", INT2NUM(creds.times.authtime));
578
+ rb_iv_set(line, "@endtime", INT2NUM(creds.times.endtime));
579
+ rb_iv_set(line, "@ticket_flags", INT2NUM(creds.ticket_flags));
580
+ rb_iv_set(line, "@cred_enctype", INT2NUM(creds.keyblock.enctype));
581
+ rb_iv_set(line, "@ticket_enctype", INT2NUM(tkt->enc_part.enctype));
582
+ rb_ary_push(result, line);
583
+ krb5_free_ticket(kerb->ctx, tkt);
584
+ free(sname);
585
+ free(name);
586
+ krb5_free_cred_contents(kerb->ctx, &creds);
587
+ }
588
+
589
+ if (krbret != KRB5_CC_END) {
590
+ // FIXME: do we need to free up "result" here? There will be no
591
+ // references to it, so I think the garbage collector will pick it up,
592
+ // but I'm not sure.
593
+
594
+ goto cache_fail_close;
595
+ }
596
+
597
+ krbret = krb5_cc_end_seq_get(kerb->ctx, cc, &cur);
598
+
599
+ krb5_cc_close(kerb->ctx, cc);
600
+
601
+ return result;
602
+
603
+ cache_fail_close:
604
+ krb5_cc_close(kerb->ctx, cc);
605
+
606
+ cache_fail_raise:
607
+ Krb5_register_error(krbret);
608
+
609
+ return Qfalse;
610
+ }
611
+
612
+ /*
613
+ * call-seq:
614
+ * destroy([cache_name])
615
+ *
616
+ * Call krb5_cc_destroy to destroy all credentials in a cachefile. With no parameters, it destroys the credentials in the default cachefile. With one parameter, it destroys the credentials in the named cachefile. Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
617
+ */
618
+ static VALUE Krb5_destroy_creds(int argc, VALUE *argv, VALUE self)
619
+ {
620
+ VALUE cache_val;
621
+ struct ruby_krb5 *kerb;
622
+ krb5_error_code krbret;
623
+ char *cache_name;
624
+ krb5_ccache cc;
625
+
626
+ rb_scan_args(argc, argv, "01", &cache_val);
627
+
628
+ cache_name = get_string_or_nil(cache_val);
629
+
630
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
631
+ if (!kerb) {
632
+ NOSTRUCT_EXCEPT();
633
+ return Qfalse;
634
+ }
635
+
636
+ if (cache_name == NULL) {
637
+ krbret = krb5_cc_default(kerb->ctx, &cc);
638
+ }
639
+ else {
640
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
641
+ }
642
+
643
+ if (krbret) {
644
+ Krb5_register_error(krbret);
645
+ return Qfalse;
646
+ }
647
+
648
+ krbret = krb5_cc_destroy(kerb->ctx, cc);
649
+ if (krbret) {
650
+ Krb5_register_error(krbret);
651
+ return Qfalse;
652
+ }
653
+
654
+ // NOTE: we don't need to call krb5_cc_close here since it is freed
655
+ // automatically by krb5_cc_destroy()
656
+
657
+ return Qtrue;
658
+ }
659
+
660
+ /*
661
+ * call-seq:
662
+ * close
663
+ *
664
+ * Free up all memory associated with this object. After this is called, no more methods may be called against this object.
665
+ */
666
+ static VALUE Krb5_close(VALUE self)
667
+ {
668
+ struct ruby_krb5 *kerb;
669
+
670
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
671
+ if (kerb) {
672
+ kerb_free(kerb);
673
+ DATA_PTR(self) = NULL;
674
+ }
675
+
676
+ return Qnil;
677
+ }
678
+
679
+ /*
680
+ * = Ruby bindings for kerberos
681
+ *
682
+ * The module Krb5Auth provides bindings to kerberos version 5 libraries
683
+ */
684
+ void Init_krb5_auth()
685
+ {
686
+ mKerberos = rb_define_module("Krb5Auth");
687
+
688
+ cKrb5 = rb_define_class_under(mKerberos,"Krb5", rb_cObject);
689
+
690
+ cKrb5_Exception = rb_define_class_under(cKrb5, "Exception", rb_eStandardError);
691
+
692
+ cCred = rb_define_class_under(cKrb5, "Cred", rb_cObject);
693
+ rb_define_attr(cCred, "client", 1, 0);
694
+ rb_define_attr(cCred, "server", 1, 0);
695
+ rb_define_attr(cCred, "starttime", 1, 0);
696
+ rb_define_attr(cCred, "authtime", 1, 0);
697
+ rb_define_attr(cCred, "endtime", 1, 0);
698
+ rb_define_attr(cCred, "ticket_flags", 1, 0);
699
+ rb_define_attr(cCred, "cred_enctype", 1, 0);
700
+ rb_define_attr(cCred, "ticket_enctype", 1, 0);
701
+
702
+ #define DEF_FLAG_CONST(name) \
703
+ rb_define_const(cCred, #name, INT2NUM(name))
704
+
705
+ DEF_FLAG_CONST(TKT_FLG_FORWARDABLE);
706
+ DEF_FLAG_CONST(TKT_FLG_FORWARDED);
707
+ DEF_FLAG_CONST(TKT_FLG_PROXIABLE);
708
+ DEF_FLAG_CONST(TKT_FLG_PROXY);
709
+ DEF_FLAG_CONST(TKT_FLG_MAY_POSTDATE);
710
+ DEF_FLAG_CONST(TKT_FLG_POSTDATED);
711
+ DEF_FLAG_CONST(TKT_FLG_INVALID);
712
+ DEF_FLAG_CONST(TKT_FLG_RENEWABLE);
713
+ DEF_FLAG_CONST(TKT_FLG_INITIAL);
714
+ DEF_FLAG_CONST(TKT_FLG_HW_AUTH);
715
+ DEF_FLAG_CONST(TKT_FLG_PRE_AUTH);
716
+ DEF_FLAG_CONST(TKT_FLG_TRANSIT_POLICY_CHECKED);
717
+ DEF_FLAG_CONST(TKT_FLG_OK_AS_DELEGATE);
718
+ DEF_FLAG_CONST(TKT_FLG_ANONYMOUS);
719
+
720
+ #undef DEF_FLAG_CONST
721
+
722
+ #define DEF_ENC_CONST(name) \
723
+ rb_define_const(cCred, #name, INT2NUM(name))
724
+
725
+ DEF_ENC_CONST(ENCTYPE_NULL);
726
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_CRC);
727
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_MD4);
728
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_MD5);
729
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_RAW);
730
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_SHA);
731
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_RAW);
732
+ DEF_ENC_CONST(ENCTYPE_DES_HMAC_SHA1);
733
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_SHA1);
734
+ DEF_ENC_CONST(ENCTYPE_AES128_CTS_HMAC_SHA1_96);
735
+ DEF_ENC_CONST(ENCTYPE_AES256_CTS_HMAC_SHA1_96);
736
+ DEF_ENC_CONST(ENCTYPE_ARCFOUR_HMAC);
737
+ DEF_ENC_CONST(ENCTYPE_ARCFOUR_HMAC_EXP);
738
+ DEF_ENC_CONST(ENCTYPE_UNKNOWN);
739
+ #undef DEF_ENC_CONST
740
+
741
+ rb_define_singleton_method(cKrb5, "new", Krb5_new, 0);
742
+ rb_define_method(cKrb5, "get_init_creds_password", Krb5_get_init_creds_password, 2);
743
+ rb_define_method(cKrb5, "get_init_creds_keytab", Krb5_get_init_creds_keytab, -1);
744
+ rb_define_method(cKrb5, "get_default_realm", Krb5_get_default_realm, 0);
745
+ rb_define_method(cKrb5, "get_default_principal", Krb5_get_default_principal, 0);
746
+ rb_define_method(cKrb5, "change_password", Krb5_change_password, 2);
747
+ rb_define_method(cKrb5, "set_password", Krb5_set_password, 1);
748
+ rb_define_method(cKrb5, "cache", Krb5_cache_creds, -1);
749
+ rb_define_method(cKrb5, "list_cache", Krb5_list_cache_creds, -1);
750
+ rb_define_method(cKrb5, "destroy", Krb5_destroy_creds, -1);
751
+ rb_define_method(cKrb5, "close", Krb5_close, 0);
752
+
753
+ /* 0.8.0: The version of the krb5-auth library */
754
+ rb_define_const(cKrb5, "VERSION", rb_str_new2("0.8.0"));
755
+ }