Class: Kerberos::Krb5::CredentialsCache

Inherits:
Object
  • Object
show all
Defined in:
ext/rkerberos/ccache.c

Instance Method Summary collapse

Constructor Details

#Kerberos::CredentialsCache.new(principal: nil, cache_name: nil, context: nil) ⇒ Object

Creates and returns a new Kerberos::CredentialsCache object. Accepts the following keyword arguments:

  • principal: A string principal name. If specified, the credentials cache is created or refreshed with this as the primary principal. If a cache already exists, its contents are destroyed. May also be an object that responds to the .principal method.

  • cache_name: The name of the credentials cache to use, which must be in "type:residual" format, where 'type' is a type known to Kerberos (typically 'FILE'). If omitted, the default cache is used.

  • context: A Kerberos::Krb5::Context object. If provided, that context is used instead of creating a new one via krb5_init_context.

Note that the principal's credentials are not set via the constructor. It merely creates the cache and sets the default principal.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/rkerberos/ccache.c', line 68

static VALUE rkrb5_ccache_initialize(int argc, VALUE* argv, VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  krb5_error_code kerror;
  VALUE v_opts, v_principal, v_name, v_context;
  ID kw_table[3] = { rb_intern("principal"), rb_intern("cache_name"), rb_intern("context") };
  VALUE kw_vals[3];

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  rb_scan_args(argc, argv, "0:", &v_opts);

  if(NIL_P(v_opts))
    v_opts = rb_hash_new();

  rb_get_kwargs(v_opts, kw_table, 0, 3, kw_vals);
  v_principal = kw_vals[0] == Qundef ? Qnil : kw_vals[0];
  v_name = kw_vals[1] == Qundef ? Qnil : kw_vals[1];
  v_context = kw_vals[2] == Qundef ? Qnil : kw_vals[2];

  if(!NIL_P(v_principal)){
    if(rb_respond_to(v_principal, rb_intern("principal")))
      v_principal = rb_funcall(v_principal, rb_intern("principal"), 0);

    Check_Type(v_principal, T_STRING);
  }

  // Initialize or borrow the context
  if(!NIL_P(v_context)){
    ptr->ctx = rkrb5_context_borrow(v_context);
    ptr->rb_context = v_context;
  }
  else{
    kerror = krb5_init_context(&ptr->ctx);

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_init_context: %s", error_message(kerror));

    ptr->rb_context = Qnil;
  }

  // Convert the principal name to a principal object
  if(!NIL_P(v_principal)){
    kerror = krb5_parse_name(
      ptr->ctx,
      StringValueCStr(v_principal),
      &ptr->principal
    );

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_parse_name: %s", error_message(kerror));
  }

  // Set the credentials cache using the default cache if no name is provided
  if(NIL_P(v_name)){
    kerror = krb5_cc_default(ptr->ctx, &ptr->ccache);

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_cc_default: %s", error_message(kerror));
  }
  else{
    Check_Type(v_name, T_STRING);
    kerror = krb5_cc_resolve(ptr->ctx, StringValueCStr(v_name), &ptr->ccache);

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_cc_resolve: %s", error_message(kerror));
  }

  // Initialize the credentials cache if a principal was provided
  if(RTEST(v_principal)){
    kerror = krb5_cc_initialize(ptr->ctx, ptr->ccache, ptr->principal);

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_cc_initialize: %s", error_message(kerror));
  }

  return self;
}

Instance Method Details

#cache_nameObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/rkerberos/ccache.c', line 203

static VALUE rkrb5_ccache_get_name(VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  const char *name;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  name = krb5_cc_get_name(ptr->ctx, ptr->ccache);
  if(!name)
    rb_raise(cKrb5Exception, "krb5_cc_get_name returned NULL");

  return rb_str_new2(name);
}

#cache_typeObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'ext/rkerberos/ccache.c', line 220

static VALUE rkrb5_ccache_get_type(VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  const char *type;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  type = krb5_cc_get_type(ptr->ctx, ptr->ccache);
  if(!type)
    rb_raise(cKrb5Exception, "krb5_cc_get_type returned NULL");

  return rb_str_new2(type);
}

#closeObject

Closes the ccache object. Once the ccache object is closed no more methods may be called on it, or an exception will be raised.

Note that unlike ccache.destroy, this does not delete the cache.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'ext/rkerberos/ccache.c', line 155

static VALUE rkrb5_ccache_close(VALUE self){
  RUBY_KRB5_CCACHE* ptr;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    return self;

  if(ptr->ccache)
    krb5_cc_close(ptr->ctx, ptr->ccache);

  if(ptr->principal)
    krb5_free_principal(ptr->ctx, ptr->principal);

  if(ptr->ctx && ptr->rb_context == Qnil)
    krb5_free_context(ptr->ctx);
  else if(ptr->rb_context != Qnil)
    rkrb5_context_release(ptr->rb_context);

  ptr->ccache = NULL;
  ptr->ctx = NULL;
  ptr->principal = NULL;
  ptr->rb_context = Qnil;

  return self;
}

#default_nameObject



191
192
193
194
195
196
197
198
199
200
# File 'ext/rkerberos/ccache.c', line 191

static VALUE rkrb5_ccache_default_name(VALUE self){
  RUBY_KRB5_CCACHE* ptr;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  return rb_str_new2(krb5_cc_default_name(ptr->ctx));
}

#destroyObject Also known as: delete



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rkerberos/ccache.c', line 283

static VALUE rkrb5_ccache_destroy(VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  krb5_error_code kerror;
  VALUE v_bool = Qtrue;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  kerror = krb5_cc_destroy(ptr->ctx, ptr->ccache);

  // Don't raise an error if there's no cache. Just return false.
  if(kerror){
    if((kerror == KRB5_CC_NOTFOUND) || (kerror == KRB5_FCC_NOFILE)){
      v_bool = Qfalse;
    }
    else{
      if(ptr->principal)
        krb5_free_principal(ptr->ctx, ptr->principal);

      if(ptr->ctx && ptr->rb_context == Qnil)
        krb5_free_context(ptr->ctx);
      else if(ptr->rb_context != Qnil)
        rkrb5_context_release(ptr->rb_context);

      ptr->ccache = NULL;
      ptr->ctx = NULL;
      ptr->principal = NULL;
      ptr->rb_context = Qnil;

      rb_raise(cKrb5Exception, "krb5_cc_destroy: %s", error_message(kerror));
    }
  }

  if(ptr->principal)
    krb5_free_principal(ptr->ctx, ptr->principal);

  if(ptr->ctx && ptr->rb_context == Qnil)
    krb5_free_context(ptr->ctx);
  else if(ptr->rb_context != Qnil)
    rkrb5_context_release(ptr->rb_context);

  ptr->ccache = NULL;
  ptr->ctx = NULL;
  ptr->principal = NULL;
  ptr->rb_context = Qnil;

  return v_bool;
}

#dupObject Also known as: clone



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/rkerberos/ccache.c', line 341

static VALUE rkrb5_ccache_dup(VALUE self){
  RUBY_KRB5_CCACHE *ptr, *newptr;
  krb5_error_code kerror;
  VALUE newobj;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  // allocate new ruby object and struct
  newobj = rkrb5_ccache_allocate(CLASS_OF(self));
  TypedData_Get_Struct(newobj, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, newptr);

  // initialize a fresh context for the duplicate
  kerror = krb5_init_context(&newptr->ctx);
  if(kerror){
    rb_raise(cKrb5Exception, "krb5_init_context: %s", error_message(kerror));
  }

  // perform ccache duplication using the new context
  kerror = krb5_cc_dup(newptr->ctx, ptr->ccache, &newptr->ccache);
  if(kerror){
    krb5_free_context(newptr->ctx);
    newptr->ctx = NULL;
    rb_raise(cKrb5Exception, "krb5_cc_dup: %s", error_message(kerror));
  }

  // principal is not copied; let callers query primary_principal on each
  newptr->principal = NULL;

  return newobj;
}

#full_nameObject



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'ext/rkerberos/ccache.c', line 382

static VALUE rkrb5_ccache_full_name(VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  char *full_name;
  krb5_error_code kerror;
  VALUE result;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  kerror = krb5_cc_get_full_name(ptr->ctx, ptr->ccache, &full_name);

  if(kerror)
    rb_raise(cKrb5Exception, "krb5_cc_get_full_name: %s", error_message(kerror));

  result = rb_str_new2(full_name);
  krb5_free_string(ptr->ctx, full_name);

  return result;
}

#primary_principalObject Also known as: principal



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'ext/rkerberos/ccache.c', line 242

static VALUE rkrb5_ccache_primary_principal(VALUE self){
  RUBY_KRB5_CCACHE* ptr;
  krb5_error_code kerror;
  char* name;

  TypedData_Get_Struct(self, RUBY_KRB5_CCACHE, &rkrb5_ccache_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  if(ptr->principal){
    krb5_free_principal(ptr->ctx, ptr->principal);
    ptr->principal = NULL;
  }

  kerror = krb5_cc_get_principal(ptr->ctx, ptr->ccache, &ptr->principal);

  if(kerror)
    rb_raise(cKrb5Exception, "krb5_cc_get_principal: %s", error_message(kerror));

  kerror = krb5_unparse_name(ptr->ctx, ptr->principal, &name);

  if(kerror)
    rb_raise(cKrb5Exception, "krb5_unparse_name: %s", error_message(kerror));

  VALUE v_name = rb_str_new2(name);
  krb5_free_unparsed_name(ptr->ctx, name);

  return v_name;
}