Class: Krb5Auth::Krb5
- Inherits:
-
Object
- Object
- Krb5Auth::Krb5
- Defined in:
- ext/ruby_krb5_auth.c
Defined Under Namespace
Class Method Summary collapse
-
.new ⇒ Object
Create a new Krb5Auth::Krb5 object.
Instance Method Summary collapse
-
#cache([cache_name]) ⇒ Object
Call krb5_cc_store_cred to store credentials in a cachefile.
-
#set_password(new_password) ⇒ Object
Call krb5_set_password to set the password for this credential to new_password.
-
#close ⇒ Object
Free up all memory associated with this object.
-
#destroy([cache_name]) ⇒ Object
Call krb5_cc_destroy to destroy all credentials in a cachefile.
-
#get_default_principal ⇒ String
Call krb5_cc_get_principal() to get the principal from the default cachefile.
-
#get_default_realm ⇒ String
Call krb5_get_default_realm() to get the default realm.
-
#get_init_creds_keytab([principal][,keytab]) ⇒ Object
Call krb5_get_init_creds_keytab() to get credentials based on a keytab.
-
#get_init_creds_password(username, password) ⇒ Object
Call krb5_get_init_creds_password() to get credentials based on a username and password.
-
#list_cache([cache_name]) ⇒ Array
Call krb5_cc_next_cred to fetch credentials from a cachefile.
Class Method Details
.new ⇒ Object
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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'ext/ruby_krb5_auth.c', line 74
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([cache_name]) ⇒ Object
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.
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 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 |
# File 'ext/ruby_krb5_auth.c', line 342
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;
}
|
#set_password(new_password) ⇒ Object
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.
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 |
# File 'ext/ruby_krb5_auth.c', line 310
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;
}
|
#close ⇒ Object
Free up all memory associated with this object. After this is called, no more methods may be called against this object.
581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'ext/ruby_krb5_auth.c', line 581
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([cache_name]) ⇒ Object
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.
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'ext/ruby_krb5_auth.c', line 527
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_principal ⇒ String
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.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'ext/ruby_krb5_auth.c', line 134
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_realm ⇒ String
Call krb5_get_default_realm() to get the default realm. Returns the default realm on success, raises Krb5Auth::Krb5::Exception on failure.
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 |
# File 'ext/ruby_krb5_auth.c', line 102
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([principal][,keytab]) ⇒ Object
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.
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 272 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 298 299 300 301 302 |
# File 'ext/ruby_krb5_auth.c', line 225
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(username, password) ⇒ Object
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.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'ext/ruby_krb5_auth.c', line 182
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;
}
|
#list_cache([cache_name]) ⇒ Array
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.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'ext/ruby_krb5_auth.c', line 415
static VALUE Krb5_list_cache_creds(int argc, VALUE *argv, VALUE self)
{
struct ruby_krb5 *kerb;
krb5_error_code krbret;
char *cache_name;
krb5_ccache cc;
krb5_cc_cursor cur;
krb5_creds creds;
char *name;
char *sname;
krb5_ticket *tkt;
VALUE result;
VALUE line;
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) {
goto cache_fail_raise;
}
krbret = krb5_cc_start_seq_get(kerb->ctx, cc, &cur);
if (krbret) {
goto cache_fail_close;
}
result = rb_ary_new();
while (!(krbret = krb5_cc_next_cred(kerb->ctx, cc, &cur, &creds))) {
krbret = krb5_unparse_name(kerb->ctx, creds.client, &name);
if (krbret) {
krb5_free_cred_contents(kerb->ctx, &creds);
break;
}
krbret = krb5_unparse_name(kerb->ctx, creds.server, &sname);
if (krbret) {
free(name);
krb5_free_cred_contents(kerb->ctx, &creds);
break;
}
krbret = krb5_decode_ticket(&creds.ticket, &tkt);
if (krbret) {
free(sname);
free(name);
krb5_free_cred_contents(kerb->ctx, &creds);
break;
}
line = rb_class_new_instance(0, NULL, cCred);
rb_iv_set(line, "@client", rb_str_new2(name));
rb_iv_set(line, "@server", rb_str_new2(sname));
rb_iv_set(line, "@starttime", INT2NUM(creds.times.starttime));
rb_iv_set(line, "@authtime", INT2NUM(creds.times.authtime));
rb_iv_set(line, "@endtime", INT2NUM(creds.times.endtime));
rb_iv_set(line, "@ticket_flags", INT2NUM(creds.ticket_flags));
rb_iv_set(line, "@cred_enctype", INT2NUM(creds.keyblock.enctype));
rb_iv_set(line, "@ticket_enctype", INT2NUM(tkt->enc_part.enctype));
rb_ary_push(result, line);
krb5_free_ticket(kerb->ctx, tkt);
free(sname);
free(name);
krb5_free_cred_contents(kerb->ctx, &creds);
}
if (krbret != KRB5_CC_END) {
// FIXME: do we need to free up "result" here? There will be no
// references to it, so I think the garbage collector will pick it up,
// but I'm not sure.
goto cache_fail_close;
}
krbret = krb5_cc_end_seq_get(kerb->ctx, cc, &cur);
krb5_cc_close(kerb->ctx, cc);
return result;
cache_fail_close:
krb5_cc_close(kerb->ctx, cc);
cache_fail_raise:
Krb5_register_error(krbret);
return Qfalse;
}
|