krb5 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,187 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ #### Start of system configuration section. ####
5
+
6
+ srcdir = ext
7
+ topdir = /Users/dvyjones/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1
8
+ hdrdir = /Users/dvyjones/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1
9
+ arch_hdrdir = /Users/dvyjones/.rvm/rubies/ruby-1.9.2-p0/include/ruby-1.9.1/$(arch)
10
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
11
+ prefix = $(DESTDIR)/Users/dvyjones/.rvm/rubies/ruby-1.9.2-p0
12
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
13
+ exec_prefix = $(prefix)
14
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
15
+ sitehdrdir = $(rubyhdrdir)/site_ruby
16
+ rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
17
+ vendordir = $(rubylibprefix)/vendor_ruby
18
+ sitedir = $(rubylibprefix)/site_ruby
19
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
20
+ mandir = $(datarootdir)/man
21
+ localedir = $(datarootdir)/locale
22
+ libdir = $(exec_prefix)/lib
23
+ psdir = $(docdir)
24
+ pdfdir = $(docdir)
25
+ dvidir = $(docdir)
26
+ htmldir = $(docdir)
27
+ infodir = $(datarootdir)/info
28
+ docdir = $(datarootdir)/doc/$(PACKAGE)
29
+ oldincludedir = $(DESTDIR)/usr/include
30
+ includedir = $(prefix)/include
31
+ localstatedir = $(prefix)/var
32
+ sharedstatedir = $(prefix)/com
33
+ sysconfdir = $(prefix)/etc
34
+ datadir = $(datarootdir)
35
+ datarootdir = $(prefix)/share
36
+ libexecdir = $(exec_prefix)/libexec
37
+ sbindir = $(exec_prefix)/sbin
38
+ bindir = $(exec_prefix)/bin
39
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
40
+ archdir = $(rubylibdir)/$(arch)
41
+ sitelibdir = $(sitedir)/$(ruby_version)
42
+ sitearchdir = $(sitelibdir)/$(sitearch)
43
+ vendorlibdir = $(vendordir)/$(ruby_version)
44
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
45
+
46
+ CC = gcc
47
+ CXX = g++
48
+ LIBRUBY = $(LIBRUBY_SO)
49
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
50
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
51
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
52
+ OUTFLAG = -o
53
+ COUTFLAG = -o
54
+
55
+ RUBY_EXTCONF_H =
56
+ cflags = $(optflags) $(debugflags) $(warnflags)
57
+ optflags = -O3
58
+ debugflags = -ggdb
59
+ warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wshorten-64-to-32 -Wno-long-long
60
+ CFLAGS = -fno-common $(cflags) -fno-common -pipe
61
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
62
+ DEFS =
63
+ CPPFLAGS = -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE $(DEFS) $(cppflags)
64
+ CXXFLAGS = $(CFLAGS) $(cxxflags)
65
+ ldflags = -L.
66
+ dldflags = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -Wl,-flat_namespace
67
+ ARCH_FLAG =
68
+ DLDFLAGS = $(ldflags) $(dldflags)
69
+ LDSHARED = $(CC) -dynamic -bundle
70
+ LDSHAREDXX = $(CXX) -dynamic -bundle
71
+ AR = ar
72
+ EXEEXT =
73
+
74
+ RUBY_BASE_NAME = ruby
75
+ RUBY_INSTALL_NAME = ruby
76
+ RUBY_SO_NAME = ruby.1.9.1
77
+ arch = x86_64-darwin10.4.0
78
+ sitearch = $(arch)
79
+ ruby_version = 1.9.1
80
+ ruby = /Users/dvyjones/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
81
+ RUBY = $(ruby)
82
+ RM = rm -f
83
+ RM_RF = $(RUBY) -run -e rm -- -rf
84
+ RMDIRS = $(RUBY) -run -e rmdir -- -p
85
+ MAKEDIRS = mkdir -p
86
+ INSTALL = /usr/bin/install -c
87
+ INSTALL_PROG = $(INSTALL) -m 0755
88
+ INSTALL_DATA = $(INSTALL) -m 644
89
+ COPY = cp
90
+
91
+ #### End of system configuration section. ####
92
+
93
+ preload =
94
+
95
+ libpath = . $(libdir)
96
+ LIBPATH = -L. -L$(libdir)
97
+ DEFFILE =
98
+
99
+ CLEANFILES = mkmf.log
100
+ DISTCLEANFILES =
101
+ DISTCLEANDIRS =
102
+
103
+ extout =
104
+ extout_prefix =
105
+ target_prefix =
106
+ LOCAL_LIBS =
107
+ LIBS = $(LIBRUBYARG_SHARED) -lkrb5 -lpthread -ldl -lobjc
108
+ SRCS = ruby_krb5.c
109
+ OBJS = ruby_krb5.o
110
+ TARGET = krb5
111
+ DLLIB = $(TARGET).bundle
112
+ EXTSTATIC =
113
+ STATIC_LIB =
114
+
115
+ BINDIR = $(bindir)
116
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
117
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
118
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
119
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
120
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
121
+
122
+ TARGET_SO = $(DLLIB)
123
+ CLEANLIBS = $(TARGET).bundle
124
+ CLEANOBJS = *.o *.bak
125
+
126
+ all: $(DLLIB)
127
+ static: $(STATIC_LIB)
128
+ .PHONY: all install static install-so install-rb
129
+ .PHONY: clean clean-so clean-rb
130
+
131
+ clean-rb-default::
132
+ clean-rb::
133
+ clean-so::
134
+ clean: clean-so clean-rb-default clean-rb
135
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
136
+
137
+ distclean-rb-default::
138
+ distclean-rb::
139
+ distclean-so::
140
+ distclean: clean distclean-so distclean-rb-default distclean-rb
141
+ @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
142
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
143
+ @-$(RMDIRS) $(DISTCLEANDIRS)
144
+
145
+ realclean: distclean
146
+ install: install-so install-rb
147
+
148
+ install-so: $(RUBYARCHDIR)
149
+ install-so: $(RUBYARCHDIR)/$(DLLIB)
150
+ $(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
151
+ @-$(MAKEDIRS) $(@D)
152
+ $(INSTALL_PROG) $(DLLIB) $(@D)
153
+ install-rb: pre-install-rb install-rb-default
154
+ install-rb-default: pre-install-rb-default
155
+ pre-install-rb: Makefile
156
+ pre-install-rb-default: Makefile
157
+ $(RUBYARCHDIR):
158
+ $(MAKEDIRS) $@
159
+
160
+ site-install: site-install-so site-install-rb
161
+ site-install-so: install-so
162
+ site-install-rb: install-rb
163
+
164
+ .SUFFIXES: .c .m .cc .cxx .cpp .C .o
165
+
166
+ .cc.o:
167
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
168
+
169
+ .cxx.o:
170
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
171
+
172
+ .cpp.o:
173
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
174
+
175
+ .C.o:
176
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
177
+
178
+ .c.o:
179
+ $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
180
+
181
+ $(DLLIB): $(OBJS) Makefile
182
+ @-$(RM) $(@)
183
+ $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
184
+
185
+
186
+
187
+ $(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
data/README ADDED
@@ -0,0 +1,5 @@
1
+ Ruby bindings for the Kerberos library
2
+
3
+ This is an implementation of Ruby bindings for the Kerberos library.
4
+
5
+ Forked of Chris Lalancette's krb5-auth.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ TODO items:
2
+ 1. Finer-grained error reporting
3
+ 2. Automated tests
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+
3
+ have_library("krb5","krb5_init_context")
4
+ create_makefile('krb5')
@@ -0,0 +1,645 @@
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 methods are called. Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
83
+ */
84
+ static VALUE Krb5_new(VALUE self)
85
+ {
86
+ struct ruby_krb5 *kerb;
87
+ krb5_error_code krbret;
88
+
89
+ kerb = (struct ruby_krb5 *)malloc(sizeof(struct ruby_krb5));
90
+ if (kerb == NULL) {
91
+ OOM_EXCEPT();
92
+ return Qnil;
93
+ }
94
+
95
+ memset(kerb, 0, sizeof(struct ruby_krb5));
96
+
97
+ krbret = krb5_init_context(&kerb->ctx);
98
+ if (krbret) {
99
+ Krb5_register_error(krbret);
100
+ return Qnil;
101
+ }
102
+
103
+ return Data_Wrap_Struct(cKrb5, NULL, kerb_free, kerb);
104
+ }
105
+
106
+ /*
107
+ * call-seq:
108
+ * get_default_realm -> string
109
+ *
110
+ * Call krb5_get_default_realm() to get the default realm. Returns the default realm on success, raises Krb5Auth::Krb5::Exception on failure.
111
+ */
112
+ static VALUE Krb5_get_default_realm(VALUE self)
113
+ {
114
+ struct ruby_krb5 *kerb;
115
+ char *realm;
116
+ VALUE result;
117
+ krb5_error_code krbret;
118
+
119
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
120
+ if (!kerb) {
121
+ NOSTRUCT_EXCEPT();
122
+ return Qfalse;
123
+ }
124
+
125
+ krbret = krb5_get_default_realm(kerb->ctx, &realm);
126
+ if (krbret) {
127
+ Krb5_register_error(krbret);
128
+ return Qnil;
129
+ }
130
+
131
+ result = rb_str_new2(realm);
132
+
133
+ free(realm);
134
+
135
+ return result;
136
+ }
137
+
138
+ /*
139
+ * call-seq:
140
+ * get_default_principal -> string
141
+ *
142
+ * Call krb5_cc_get_principal() to get the principal from the default cachefile. Returns the default principal on success, raises Krb5Auth::Krb5::Exception on failure.
143
+ */
144
+ static VALUE Krb5_get_default_principal(VALUE self)
145
+ {
146
+ struct ruby_krb5 *kerb;
147
+ char *princ_name;
148
+ VALUE result;
149
+ krb5_error_code krbret;
150
+ krb5_ccache cc;
151
+
152
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
153
+ if (!kerb) {
154
+ NOSTRUCT_EXCEPT();
155
+ return Qfalse;
156
+ }
157
+
158
+ krbret = krb5_cc_default(kerb->ctx, &cc);
159
+ if (krbret) {
160
+ Krb5_register_error(krbret);
161
+ return Qfalse;
162
+ }
163
+
164
+ krbret = krb5_cc_get_principal(kerb->ctx, cc, &kerb->princ);
165
+ if (krbret) {
166
+ krb5_cc_close(kerb->ctx, cc);
167
+ Krb5_register_error(krbret);
168
+ return Qnil;
169
+ }
170
+
171
+ krb5_cc_close(kerb->ctx, cc);
172
+
173
+ krbret = krb5_unparse_name(kerb->ctx, kerb->princ, &princ_name);
174
+ if (krbret) {
175
+ Krb5_register_error(krbret);
176
+ return Qnil;
177
+ }
178
+
179
+ result = rb_str_new2(princ_name);
180
+
181
+ free(princ_name);
182
+
183
+ return result;
184
+ }
185
+
186
+ /*
187
+ * call-seq:
188
+ * get_init_creds_password(username, password)
189
+ *
190
+ * Call krb5_get_init_creds_password() to get credentials based on a username and password. Returns true on success, raises Krb5Auth::Krb5::Exception on failure.
191
+ */
192
+ static VALUE Krb5_get_init_creds_password(VALUE self, VALUE _user, VALUE _pass)
193
+ {
194
+ Check_Type(_user,T_STRING);
195
+ Check_Type(_pass,T_STRING);
196
+ char *user = StringValueCStr(_user);
197
+ char *pass = StringValueCStr(_pass);
198
+
199
+ struct ruby_krb5 *kerb;
200
+ krb5_error_code krbret;
201
+
202
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
203
+ if (!kerb) {
204
+ NOSTRUCT_EXCEPT();
205
+ return Qfalse;
206
+ }
207
+
208
+ krbret = krb5_parse_name(kerb->ctx, user, &kerb->princ);
209
+ if (krbret) {
210
+ goto failed_pass;
211
+ }
212
+
213
+ krbret = krb5_get_init_creds_password(kerb->ctx, &kerb->creds, kerb->princ,
214
+ pass, 0, NULL, 0,NULL, NULL);
215
+ if (krbret) {
216
+ goto failed_pass;
217
+ }
218
+
219
+ return Qtrue;
220
+
221
+ failed_pass:
222
+ Krb5_register_error(krbret);
223
+
224
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
225
+ // leave it to shut the compiler up
226
+ return Qfalse;
227
+ }
228
+
229
+ /*
230
+ * call-seq:
231
+ * get_init_creds_keytab([principal][,keytab])
232
+ *
233
+ * 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.
234
+ */
235
+ static VALUE Krb5_get_init_creds_keytab(int argc, VALUE *argv, VALUE self)
236
+ {
237
+ VALUE princ_val, keytab_val;
238
+ char *princ;
239
+ char *keytab_name;
240
+ struct ruby_krb5 *kerb;
241
+ krb5_error_code krbret;
242
+ krb5_keytab keytab;
243
+
244
+ keytab = NULL;
245
+
246
+ rb_scan_args(argc, argv, "02", &princ_val, &keytab_val);
247
+
248
+ princ = get_string_or_nil(princ_val);
249
+ keytab_name = get_string_or_nil(keytab_val);
250
+
251
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
252
+ if (!kerb) {
253
+ NOSTRUCT_EXCEPT();
254
+ return Qfalse;
255
+ }
256
+
257
+ if (keytab_name != NULL) {
258
+ krbret = krb5_kt_resolve(kerb->ctx, keytab_name, &keytab);
259
+ if (krbret) {
260
+ goto failed_keytab;
261
+ }
262
+ }
263
+ // implicit else: if we weren't passed a keytab name, just leave keytab as
264
+ // NULL to use the default
265
+
266
+ if (princ != NULL) {
267
+ krbret = krb5_parse_name(kerb->ctx, princ, &kerb->princ);
268
+ }
269
+ else {
270
+ // if we weren't passed a principal, we just get the default principal
271
+ // (which is generally the hostname)
272
+ krbret = krb5_sname_to_principal(kerb->ctx, NULL, NULL, KRB5_NT_SRV_HST,
273
+ &kerb->princ);
274
+ }
275
+ if (krbret) {
276
+ goto failed_keytab;
277
+ }
278
+
279
+ krbret = krb5_get_init_creds_keytab(kerb->ctx, &kerb->creds, kerb->princ,
280
+ keytab, 0, NULL, NULL);
281
+ if (krbret) {
282
+ goto failed_keytab;
283
+ }
284
+
285
+ if (keytab)
286
+ krb5_kt_close(kerb->ctx, keytab);
287
+
288
+ return Qtrue;
289
+
290
+ failed_keytab:
291
+ if (keytab)
292
+ krb5_kt_close(kerb->ctx, keytab);
293
+
294
+ Krb5_register_error(krbret);
295
+
296
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
297
+ // leave it to shut the compiler up
298
+ return Qfalse;
299
+ }
300
+
301
+ /*
302
+ * call-seq:
303
+ * set_password(new_password)
304
+ *
305
+ * Call krb5_set_password to set the password for this credential to new_password. 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.
306
+ */
307
+ static VALUE Krb5_change_password(VALUE self, VALUE _newpass)
308
+ {
309
+ Check_Type(_newpass,T_STRING);
310
+ char *newpass = StringValueCStr(_newpass);
311
+
312
+ struct ruby_krb5 *kerb;
313
+ krb5_error_code krbret;
314
+ int pw_result;
315
+ krb5_data pw_res_string, res_string;
316
+
317
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
318
+ if (!kerb) {
319
+ NOSTRUCT_EXCEPT();
320
+ return Qfalse;
321
+ }
322
+
323
+ krbret = krb5_set_password(kerb->ctx, &kerb->creds, newpass, NULL,
324
+ &pw_result, &pw_res_string, &res_string );
325
+ if (krbret) {
326
+ Krb5_register_error(krbret);
327
+ return Qfalse;
328
+ }
329
+
330
+ return Qtrue;
331
+ }
332
+
333
+ /*
334
+ * call-seq:
335
+ * cache([cache_name])
336
+ *
337
+ * 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.
338
+ */
339
+ static VALUE Krb5_cache_creds(int argc, VALUE *argv, VALUE self)
340
+ {
341
+ VALUE cache_val;
342
+ struct ruby_krb5 *kerb;
343
+ krb5_error_code krbret;
344
+ char *cache_name;
345
+ krb5_ccache cc;
346
+
347
+ rb_scan_args(argc, argv, "01", &cache_val);
348
+
349
+ cache_name = get_string_or_nil(cache_val);
350
+
351
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
352
+ if (!kerb) {
353
+ NOSTRUCT_EXCEPT();
354
+ return Qfalse;
355
+ }
356
+
357
+ if (!kerb->princ) {
358
+ // OK, it looks like they are trying to cache credentials that they don't
359
+ // yet have; just throw an exception so we don't segfault later
360
+ rb_raise(cKrb5_Exception, "%s", "Attempting to cache before obtaining credentials");
361
+ return Qfalse;
362
+ }
363
+
364
+ if (cache_name == NULL) {
365
+ krbret = krb5_cc_default(kerb->ctx, &cc);
366
+ }
367
+ else {
368
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
369
+ }
370
+
371
+ if (krbret) {
372
+ goto fail_cache;
373
+ }
374
+
375
+ krbret = krb5_cc_initialize(kerb->ctx, cc, kerb->princ);
376
+ if (krbret) {
377
+ goto fail_free_cc;
378
+ }
379
+
380
+ krbret = krb5_cc_store_cred(kerb->ctx, cc, &kerb->creds);
381
+ if (krbret) {
382
+ goto fail_free_cc;
383
+ }
384
+
385
+ krb5_cc_close(kerb->ctx, cc);
386
+
387
+ return Qtrue;
388
+
389
+ fail_free_cc:
390
+ krb5_cc_close(kerb->ctx, cc);
391
+
392
+ fail_cache:
393
+ Krb5_register_error(krbret);
394
+
395
+ // we will never reach here, since Krb5_register_error will rb_raise(). just
396
+ // leave it to shut the compiler up
397
+ return Qfalse;
398
+ }
399
+
400
+ /*
401
+ * call-seq:
402
+ * list_cache([cache_name]) -> array
403
+ *
404
+ * 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.
405
+ */
406
+ static VALUE Krb5_list_cache_creds(int argc, VALUE *argv, VALUE self)
407
+ {
408
+ VALUE cache_val;
409
+ struct ruby_krb5 *kerb;
410
+ krb5_error_code krbret;
411
+ char *cache_name;
412
+ krb5_ccache cc;
413
+ krb5_cc_cursor cur;
414
+ krb5_creds creds;
415
+ char *name;
416
+ char *sname;
417
+ krb5_ticket *tkt;
418
+ VALUE result;
419
+ VALUE line;
420
+
421
+ rb_scan_args(argc, argv, "01", &cache_val);
422
+
423
+ cache_name = get_string_or_nil(cache_val);
424
+
425
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
426
+ if (!kerb) {
427
+ NOSTRUCT_EXCEPT();
428
+ return Qfalse;
429
+ }
430
+
431
+ if (cache_name == NULL) {
432
+ krbret = krb5_cc_default(kerb->ctx, &cc);
433
+ }
434
+ else {
435
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
436
+ }
437
+
438
+ if (krbret) {
439
+ goto cache_fail_raise;
440
+ }
441
+
442
+ krbret = krb5_cc_start_seq_get(kerb->ctx, cc, &cur);
443
+ if (krbret) {
444
+ goto cache_fail_close;
445
+ }
446
+
447
+ result = rb_ary_new();
448
+ while (!(krbret = krb5_cc_next_cred(kerb->ctx, cc, &cur, &creds))) {
449
+ krbret = krb5_unparse_name(kerb->ctx, creds.client, &name);
450
+ if (krbret) {
451
+ krb5_free_cred_contents(kerb->ctx, &creds);
452
+ break;
453
+ }
454
+ krbret = krb5_unparse_name(kerb->ctx, creds.server, &sname);
455
+ if (krbret) {
456
+ free(name);
457
+ krb5_free_cred_contents(kerb->ctx, &creds);
458
+ break;
459
+ }
460
+ krbret = krb5_decode_ticket(&creds.ticket, &tkt);
461
+ if (krbret) {
462
+ free(sname);
463
+ free(name);
464
+ krb5_free_cred_contents(kerb->ctx, &creds);
465
+ break;
466
+ }
467
+ line = rb_class_new_instance(0, NULL, cCred);
468
+ rb_iv_set(line, "@client", rb_str_new2(name));
469
+ rb_iv_set(line, "@server", rb_str_new2(sname));
470
+ rb_iv_set(line, "@starttime", INT2NUM(creds.times.starttime));
471
+ rb_iv_set(line, "@authtime", INT2NUM(creds.times.authtime));
472
+ rb_iv_set(line, "@endtime", INT2NUM(creds.times.endtime));
473
+ rb_iv_set(line, "@ticket_flags", INT2NUM(creds.ticket_flags));
474
+ rb_iv_set(line, "@cred_enctype", INT2NUM(creds.keyblock.enctype));
475
+ rb_iv_set(line, "@ticket_enctype", INT2NUM(tkt->enc_part.enctype));
476
+ rb_ary_push(result, line);
477
+ krb5_free_ticket(kerb->ctx, tkt);
478
+ free(sname);
479
+ free(name);
480
+ krb5_free_cred_contents(kerb->ctx, &creds);
481
+ }
482
+
483
+ if (krbret != KRB5_CC_END) {
484
+ // FIXME: do we need to free up "result" here? There will be no
485
+ // references to it, so I think the garbage collector will pick it up,
486
+ // but I'm not sure.
487
+
488
+ goto cache_fail_close;
489
+ }
490
+
491
+ krbret = krb5_cc_end_seq_get(kerb->ctx, cc, &cur);
492
+
493
+ krb5_cc_close(kerb->ctx, cc);
494
+
495
+ return result;
496
+
497
+ cache_fail_close:
498
+ krb5_cc_close(kerb->ctx, cc);
499
+
500
+ cache_fail_raise:
501
+ Krb5_register_error(krbret);
502
+
503
+ return Qfalse;
504
+ }
505
+
506
+ /*
507
+ * call-seq:
508
+ * destroy([cache_name])
509
+ *
510
+ * 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.
511
+ */
512
+ static VALUE Krb5_destroy_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
+
520
+ rb_scan_args(argc, argv, "01", &cache_val);
521
+
522
+ cache_name = get_string_or_nil(cache_val);
523
+
524
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
525
+ if (!kerb) {
526
+ NOSTRUCT_EXCEPT();
527
+ return Qfalse;
528
+ }
529
+
530
+ if (cache_name == NULL) {
531
+ krbret = krb5_cc_default(kerb->ctx, &cc);
532
+ }
533
+ else {
534
+ krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
535
+ }
536
+
537
+ if (krbret) {
538
+ Krb5_register_error(krbret);
539
+ return Qfalse;
540
+ }
541
+
542
+ krbret = krb5_cc_destroy(kerb->ctx, cc);
543
+ if (krbret) {
544
+ Krb5_register_error(krbret);
545
+ return Qfalse;
546
+ }
547
+
548
+ // NOTE: we don't need to call krb5_cc_close here since it is freed
549
+ // automatically by krb5_cc_destroy()
550
+
551
+ return Qtrue;
552
+ }
553
+
554
+ /*
555
+ * call-seq:
556
+ * close
557
+ *
558
+ * Free up all memory associated with this object. After this is called, no more methods may be called against this object.
559
+ */
560
+ static VALUE Krb5_close(VALUE self)
561
+ {
562
+ struct ruby_krb5 *kerb;
563
+
564
+ Data_Get_Struct(self, struct ruby_krb5, kerb);
565
+ if (kerb) {
566
+ kerb_free(kerb);
567
+ DATA_PTR(self) = NULL;
568
+ }
569
+
570
+ return Qnil;
571
+ }
572
+
573
+ /*
574
+ * = Ruby bindings for kerberos
575
+ *
576
+ * The module Krb5 provides bindings to kerberos version 5 libraries
577
+ */
578
+ void Init_krb5()
579
+ {
580
+ mKerberos = rb_define_module("Krb5");
581
+
582
+ cKrb5 = rb_define_class_under(mKerberos,"Krb5", rb_cObject);
583
+
584
+ cKrb5_Exception = rb_define_class_under(cKrb5, "Exception", rb_eStandardError);
585
+
586
+ cCred = rb_define_class_under(cKrb5, "Cred", rb_cObject);
587
+ rb_define_attr(cCred, "client", 1, 0);
588
+ rb_define_attr(cCred, "server", 1, 0);
589
+ rb_define_attr(cCred, "starttime", 1, 0);
590
+ rb_define_attr(cCred, "authtime", 1, 0);
591
+ rb_define_attr(cCred, "endtime", 1, 0);
592
+ rb_define_attr(cCred, "ticket_flags", 1, 0);
593
+ rb_define_attr(cCred, "cred_enctype", 1, 0);
594
+ rb_define_attr(cCred, "ticket_enctype", 1, 0);
595
+
596
+ #define DEF_FLAG_CONST(name) \
597
+ rb_define_const(cCred, #name, INT2NUM(name))
598
+
599
+ DEF_FLAG_CONST(TKT_FLG_FORWARDABLE);
600
+ DEF_FLAG_CONST(TKT_FLG_FORWARDED);
601
+ DEF_FLAG_CONST(TKT_FLG_PROXIABLE);
602
+ DEF_FLAG_CONST(TKT_FLG_PROXY);
603
+ DEF_FLAG_CONST(TKT_FLG_MAY_POSTDATE);
604
+ DEF_FLAG_CONST(TKT_FLG_POSTDATED);
605
+ DEF_FLAG_CONST(TKT_FLG_INVALID);
606
+ DEF_FLAG_CONST(TKT_FLG_RENEWABLE);
607
+ DEF_FLAG_CONST(TKT_FLG_INITIAL);
608
+ DEF_FLAG_CONST(TKT_FLG_HW_AUTH);
609
+ DEF_FLAG_CONST(TKT_FLG_PRE_AUTH);
610
+ DEF_FLAG_CONST(TKT_FLG_TRANSIT_POLICY_CHECKED);
611
+ DEF_FLAG_CONST(TKT_FLG_OK_AS_DELEGATE);
612
+ DEF_FLAG_CONST(TKT_FLG_ANONYMOUS);
613
+
614
+ #undef DEF_FLAG_CONST
615
+
616
+ #define DEF_ENC_CONST(name) \
617
+ rb_define_const(cCred, #name, INT2NUM(name))
618
+
619
+ DEF_ENC_CONST(ENCTYPE_NULL);
620
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_CRC);
621
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_MD4);
622
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_MD5);
623
+ DEF_ENC_CONST(ENCTYPE_DES_CBC_RAW);
624
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_SHA);
625
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_RAW);
626
+ DEF_ENC_CONST(ENCTYPE_DES_HMAC_SHA1);
627
+ DEF_ENC_CONST(ENCTYPE_DES3_CBC_SHA1);
628
+ DEF_ENC_CONST(ENCTYPE_AES128_CTS_HMAC_SHA1_96);
629
+ DEF_ENC_CONST(ENCTYPE_AES256_CTS_HMAC_SHA1_96);
630
+ DEF_ENC_CONST(ENCTYPE_ARCFOUR_HMAC);
631
+ DEF_ENC_CONST(ENCTYPE_ARCFOUR_HMAC_EXP);
632
+ DEF_ENC_CONST(ENCTYPE_UNKNOWN);
633
+ #undef DEF_ENC_CONST
634
+
635
+ rb_define_singleton_method(cKrb5, "new", Krb5_new, 0);
636
+ rb_define_method(cKrb5, "get_init_creds_password", Krb5_get_init_creds_password, 2);
637
+ rb_define_method(cKrb5, "get_init_creds_keytab", Krb5_get_init_creds_keytab, -1);
638
+ rb_define_method(cKrb5, "get_default_realm", Krb5_get_default_realm, 0);
639
+ rb_define_method(cKrb5, "get_default_principal", Krb5_get_default_principal, 0);
640
+ rb_define_method(cKrb5, "change_password", Krb5_change_password, 1);
641
+ rb_define_method(cKrb5, "cache", Krb5_cache_creds, -1);
642
+ rb_define_method(cKrb5, "list_cache", Krb5_list_cache_creds, -1);
643
+ rb_define_method(cKrb5, "destroy", Krb5_destroy_creds, -1);
644
+ rb_define_method(cKrb5, "close", Krb5_close, 0);
645
+ }