Class: Krb5Auth::Krb5

Inherits:
Object
  • Object
show all
Defined in:
lib/krb5-auth.rb,
ext/ruby_krb5_auth.c

Overview

Krb5 contains the kerberos end user functionality, such as user authentication and password changes.

Defined Under Namespace

Classes: Exception

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/ruby_krb5_auth.c', line 67

static VALUE Krb5_new(VALUE self)
{
  struct ruby_krb5 *kerb;
  krb5_error_code krbret;

  kerb = (struct ruby_krb5 *)malloc(sizeof(struct ruby_krb5));
  if (kerb == NULL) {
    OOM_EXCEPT();
    return Qnil;
  }

  memset(kerb, 0, sizeof(struct ruby_krb5));

  krbret = krb5_init_context(&kerb->ctx);
  if (krbret) {
    Krb5_register_error(krbret);    
    return Qnil;
  }

  return Data_Wrap_Struct(cKrb5, NULL, kerb_free, kerb);
}

Instance Method Details

#cache(*args) ⇒ Object



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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'ext/ruby_krb5_auth.c', line 299

static VALUE Krb5_cache_creds(int argc, VALUE *argv, VALUE self)
{
  struct ruby_krb5 *kerb;
  krb5_error_code krbret;
  char *cache_name;
  krb5_ccache cc;

  if (argc == 0) {
    cache_name = NULL;
  }
  else if (argc == 1) {
    Check_Type(argv[0], T_STRING);
    cache_name = STR2CSTR(argv[0]);
  }
  else {
    rb_raise(rb_eRuntimeError, "Invalid arguments");
  }

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  if (!kerb->princ) {
    // OK, it looks like they are trying to cache credentials that they don't
    // yet have; just throw an exception so we don't segfault later
    rb_raise(cKrb5_Exception, "%s", "Attempting to cache before obtaining credentials");
    return Qfalse;
  }

  if (cache_name == NULL) {
    krbret = krb5_cc_default(kerb->ctx, &cc);
  }
  else {
    krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
  }

  if (krbret) {
    goto fail_cache;
  }

  krbret = krb5_cc_initialize(kerb->ctx, cc, kerb->princ);
  if (krbret) {
    goto fail_free_cc;
  }

  krbret = krb5_cc_store_cred(kerb->ctx, cc, &kerb->creds);
  if (krbret) {
    goto fail_free_cc;
  }

  krb5_cc_close(kerb->ctx, cc);

  return Qtrue;

 fail_free_cc:
  krb5_cc_close(kerb->ctx, cc);

 fail_cache:
  Krb5_register_error(krbret);

  // we will never reach here, since Krb5_register_error will rb_raise().  just
  // leave it to shut the compiler up
  return Qfalse;
}

#change_password(_newpass) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/ruby_krb5_auth.c', line 273

static VALUE Krb5_change_password(VALUE self, VALUE _newpass)
{
  Check_Type(_newpass,T_STRING);
  char *newpass = STR2CSTR(_newpass);

  struct ruby_krb5 *kerb;
  krb5_error_code krbret;
  int pw_result;
  krb5_data pw_res_string, res_string;

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  krbret = krb5_set_password(kerb->ctx, &kerb->creds, newpass, NULL,
			     &pw_result, &pw_res_string, &res_string );
  if (krbret) {
    Krb5_register_error(krbret);
    return Qfalse;
  }

  return Qtrue;
}

#closeObject



414
415
416
417
418
419
420
421
422
423
424
425
# File 'ext/ruby_krb5_auth.c', line 414

static VALUE Krb5_close(VALUE self)
{
  struct ruby_krb5 *kerb;

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (kerb) {
    kerb_free(kerb);
    DATA_PTR(self) = NULL;
  }

  return Qnil;
}

#destroy(*args) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'ext/ruby_krb5_auth.c', line 366

static VALUE Krb5_destroy_creds(int argc, VALUE *argv, VALUE self)
{
  struct ruby_krb5 *kerb;
  krb5_error_code krbret;
  char *cache_name;
  krb5_ccache cc;

  if (argc == 0) {
    cache_name = NULL;
  }
  else if (argc == 1) {
    Check_Type(argv[0], T_STRING);
    cache_name = STR2CSTR(argv[0]);
  }
  else {
    rb_raise(rb_eRuntimeError, "Invalid arguments");
  }

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  if (cache_name == NULL) {
    krbret = krb5_cc_default(kerb->ctx, &cc);
  }
  else {
    krbret = krb5_cc_resolve(kerb->ctx, cache_name, &cc);
  }

  if (krbret) {
    Krb5_register_error(krbret);
    return Qfalse;
  }

  krbret = krb5_cc_destroy(kerb->ctx, cc);
  if (krbret) {
    Krb5_register_error(krbret);
    return Qfalse;
  }

  // NOTE: we don't need to call krb5_cc_close here since it is freed
  // automatically by krb5_cc_destroy()

  return Qtrue;
}

#get_default_principalObject



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
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/ruby_krb5_auth.c', line 115

static VALUE Krb5_get_default_principal(VALUE self)
{
  struct ruby_krb5 *kerb;
  char *princ_name;
  VALUE result;
  krb5_error_code krbret;
  krb5_ccache cc;

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  krbret = krb5_cc_default(kerb->ctx, &cc);
  if (krbret) {
    Krb5_register_error(krbret);
    return Qfalse;
  }

  krbret = krb5_cc_get_principal(kerb->ctx, cc, &kerb->princ);
  if (krbret) {
    krb5_cc_close(kerb->ctx, cc);
    Krb5_register_error(krbret);    
    return Qnil;
  }

  krb5_cc_close(kerb->ctx, cc);

  krbret = krb5_unparse_name(kerb->ctx, kerb->princ, &princ_name);
  if (krbret) {
    Krb5_register_error(krbret);    
    return Qnil;
  }

  result = rb_str_new2(princ_name);

  free(princ_name);

  return result;
}

#get_default_realmObject



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
# File 'ext/ruby_krb5_auth.c', line 89

static VALUE Krb5_get_default_realm(VALUE self)
{
  struct ruby_krb5 *kerb;
  char *realm;
  VALUE result;
  krb5_error_code krbret;

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  krbret = krb5_get_default_realm(kerb->ctx, &realm);
  if (krbret) {
    Krb5_register_error(krbret);    
    return Qnil;
  }

  result = rb_str_new2(realm);

  free(realm);

  return result;
}

#get_init_creds_keytab(*args) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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/ruby_krb5_auth.c', line 194

static VALUE Krb5_get_init_creds_keytab(int argc, VALUE *argv, VALUE self)
{
  char *princ;
  char *keytab_name;
  struct ruby_krb5 *kerb;
  krb5_error_code krbret;
  krb5_keytab keytab;

  keytab = NULL;

  if (argc == 0) {
    keytab_name = NULL;
    princ = NULL;
  }
  else if (argc == 1) {
    Check_Type(argv[0], T_STRING);
    princ = STR2CSTR(argv[0]);
    keytab_name = NULL;
  }
  else if (argc == 2) {
    Check_Type(argv[0], T_STRING);
    Check_Type(argv[1], T_STRING);
    princ = STR2CSTR(argv[0]);
    keytab_name = STR2CSTR(argv[1]);
  }
  else {
    rb_raise(rb_eRuntimeError, "Invalid arguments");
  }

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  if (keytab_name != NULL) {
    krbret = krb5_kt_resolve(kerb->ctx, keytab_name, &keytab);
    if (krbret) {
      goto failed_keytab;
    }
  }
  // implicit else: if we weren't passed a keytab name, just leave keytab as
  // NULL to use the default

  if (princ != NULL) {
    krbret = krb5_parse_name(kerb->ctx, princ, &kerb->princ);
  }
  else {
    // if we weren't passed a principal, we just get the default principal
    // (which is generally the hostname)
    krbret = krb5_sname_to_principal(kerb->ctx, NULL, NULL, KRB5_NT_SRV_HST,
				     &kerb->princ);
  }
  if (krbret) {
    goto failed_keytab;
  }

  krbret = krb5_get_init_creds_keytab(kerb->ctx, &kerb->creds, kerb->princ,
				      keytab, 0, NULL, NULL);
  if (krbret) {
    goto failed_keytab;
  }

  if (keytab)
    krb5_kt_close(kerb->ctx, keytab);

  return Qtrue;

 failed_keytab:
  if (keytab)
    krb5_kt_close(kerb->ctx, keytab);

  Krb5_register_error(krbret);

  // we will never reach here, since Krb5_register_error will rb_raise().  just
  // leave it to shut the compiler up
  return Qfalse;
}

#get_init_creds_password(_user, _pass) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/ruby_krb5_auth.c', line 157

static VALUE Krb5_get_init_creds_password(VALUE self, VALUE _user, VALUE _pass)
{
  Check_Type(_user,T_STRING);
  Check_Type(_pass,T_STRING);
  char *user = STR2CSTR(_user);
  char *pass = STR2CSTR(_pass);

  struct ruby_krb5 *kerb;
  krb5_error_code krbret;

  Data_Get_Struct(self, struct ruby_krb5, kerb);
  if (!kerb) {
    NOSTRUCT_EXCEPT();
    return Qfalse;
  }

  krbret = krb5_parse_name(kerb->ctx, user, &kerb->princ);
  if (krbret) {
    goto failed_pass;
  }

  krbret = krb5_get_init_creds_password(kerb->ctx, &kerb->creds, kerb->princ,
					pass, 0, NULL, 0,NULL, NULL);
  if (krbret) {
    goto failed_pass;
  }

  return Qtrue;

 failed_pass:
  Krb5_register_error(krbret);

  // we will never reach here, since Krb5_register_error will rb_raise().  just
  // leave it to shut the compiler up
  return Qfalse;
}