/* * call-seq: * cache([cache_name]) * * 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. */ static VALUE Krb5_cache_creds(int argc, VALUE *argv, VALUE self) { VALUE cache_val; struct ruby_krb5 *kerb; krb5_error_code krbret; char *cache_name; krb5_ccache cc; rb_scan_args(argc, argv, "01", &cache_val); cache_name = get_string_or_nil(cache_val); 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; }
/* * call-seq: * 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. */ 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; }
/* * call-seq: * destroy([cache_name]) * * 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. */ static VALUE Krb5_destroy_creds(int argc, VALUE *argv, VALUE self) { VALUE cache_val; struct ruby_krb5 *kerb; krb5_error_code krbret; char *cache_name; krb5_ccache cc; rb_scan_args(argc, argv, "01", &cache_val); cache_name = get_string_or_nil(cache_val); 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; }
/* * call-seq: * set_password(new_password) * * Call krb5_set_password() to set the password for this credential to * new_password. * * This method requires that the credential for kadmin/changepw@REALM has * already been fetched using Krb5#get_init_creds_password or * Krb5#get_init_creds_keytab. * * Returns true on success, raises Krb5Auth::Krb5::Exception on failure. *-- * TODO: This method does not work at the moment. */ static VALUE Krb5_set_password(VALUE self, VALUE _newpass) { Check_Type(_newpass,T_STRING); char *newpass = StringValueCStr(_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; } // TODO: get kadmin/changepw credentials here. 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; } if(pw_result){ Krb5_register_error(pw_result); return Qfalse; } return Qtrue; }
/* * call-seq: * get_default_principal -> string * * Call krb5_cc_get_principal() to get the principal from the default * cachefile. Returns the default principal on success, raises a * Krb5Auth::Krb5::Exception on failure. */ 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; }
/* * call-seq: * get_init_creds_password(username, password) * * 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. */ 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 = StringValueCStr(_user); char *pass = StringValueCStr(_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; }
/* * call-seq: * 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. */ static VALUE Krb5_list_cache_creds(int argc, VALUE *argv, VALUE self) { VALUE cache_val; 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; rb_scan_args(argc, argv, "01", &cache_val); cache_name = get_string_or_nil(cache_val); 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; }
/* * call-seq: * change_password(old_password, new_password) * * Allow user to change their Kerberos password, providing the +old_password+ * and the +new_password+. * * Returns true on success, raises a Krb5Auth::Krb5::Exception on failure. */ static VALUE Krb5_change_password(VALUE self, VALUE v_old, VALUE v_new) { char *oldpass; char *newpass; int pw_result; struct ruby_krb5 *kerb; krb5_error_code krbret; krb5_data pw_res_string, res_string; Check_Type(v_old, T_STRING); Check_Type(v_new, T_STRING); oldpass = StringValueCStr(v_old); newpass = StringValueCStr(v_new); Data_Get_Struct(self, struct ruby_krb5, kerb); if(!kerb){ NOSTRUCT_EXCEPT(); return Qfalse; } krbret = krb5_get_init_creds_password( kerb->ctx, &kerb->creds, kerb->princ, oldpass, NULL, NULL, 0, "kadmin/changepw", NULL ); if(krbret){ Krb5_register_error(krbret); return Qfalse; } krbret = krb5_change_password( kerb->ctx, &kerb->creds, newpass, &pw_result, &pw_res_string, &res_string ); // 0 is success. Anything else is failure. if(krbret){ Krb5_register_error(krbret); return Qfalse; } if(pw_result){ Krb5_register_error(pw_result); return Qfalse; } return Qtrue; }
/* * call-seq: * get_init_creds_keytab([principal][,keytab]) * * 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. */ static VALUE Krb5_get_init_creds_keytab(int argc, VALUE *argv, VALUE self) { VALUE princ_val, keytab_val; char *princ; char *keytab_name; struct ruby_krb5 *kerb; krb5_error_code krbret; krb5_keytab keytab; keytab = NULL; rb_scan_args(argc, argv, "02", &princ_val, &keytab_val); princ = get_string_or_nil(princ_val); keytab_name = get_string_or_nil(keytab_val); 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; }
/* * call-seq: * change_password(old_password, new_password) * * Allow user to change their Kerberos password, providing the +old_password+ * and the +new_password+. * * Returns true on success, raises a Krb5Auth::Krb5::Exception on failure. */ static VALUE Krb5_change_password(VALUE self, VALUE v_old, VALUE v_new) { char *oldpass; char *newpass; int pw_result; struct ruby_krb5 *kerb; krb5_error_code krbret; krb5_data pw_res_string, res_string; Check_Type(v_old, T_STRING); Check_Type(v_new, T_STRING); oldpass = StringValueCStr(v_old); newpass = StringValueCStr(v_new); Data_Get_Struct(self, struct ruby_krb5, kerb); if(!kerb){ NOSTRUCT_EXCEPT(); return Qfalse; } krbret = krb5_get_init_creds_password( kerb->ctx, &kerb->creds, kerb->princ, oldpass, NULL, NULL, 0, "kadmin/changepw", NULL ); if(krbret){ Krb5_register_error(krbret); return Qfalse; } krbret = krb5_change_password( kerb->ctx, &kerb->creds, newpass, &pw_result, &pw_res_string, &res_string ); // 0 is success. Anything else is failure. if(krbret){ Krb5_register_error(krbret); return Qfalse; } if(pw_result){ switch(pw_result) { case KRB5_KPASSWD_MALFORMED: rb_raise(cKrb5_Exception, "Request Packet Incorrect"); break; case KRB5_KPASSWD_HARDERROR: rb_raise(cKrb5_Exception, "Password Server Error"); break; case KRB5_KPASSWD_AUTHERROR: rb_raise(cKrb5_Exception, "Authentication Error"); break; case KRB5_KPASSWD_SOFTERROR: rb_raise(cKrb5_Exception, "Password change rejected"); break; default: rb_raise(cKrb5_Exception, "Unknown error, code: %d", pw_result); break; } return Qfalse; } return Qtrue; }