Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ruby.h"
- #include <krb5.h>
- #include <stdio.h>
- #include <strings.h>
- static VALUE mKerberos;
- static VALUE cTicket;
- static VALUE cException;
- struct ruby_krb5 {
- krb5_context ctx;
- krb5_creds creds;
- krb5_principal princ;
- };
- #define OOM_EXCEPT() rb_raise(cException, "%s", "Error mallocing memory");
- #define NOSTRUCT_EXCEPT() rb_raise(cException, "%s", "Class not initialized properly (try 'new')");
- void Krb5_register_error(int error) {
- rb_raise(cException, "%s", error_message(error));
- }
- static void kerb_free(void *p) {
- struct ruby_krb5 *kerb;
- if (!p) return;
- kerb = (struct ruby_krb5 *)p;
- // kerb->creds is not a pointer, so we can't check for NULL; however, the
- // implementation of krb5_free_cred_contents does do NULL checking, so it
- // is safe (at least in the MIT version) to call it unconditionally
- krb5_free_cred_contents(kerb->ctx, &kerb->creds);
- if (kerb->princ)
- krb5_free_principal(kerb->ctx, kerb->princ);
- if (kerb->ctx)
- krb5_free_context(kerb->ctx);
- memset(kerb, 0, sizeof(struct ruby_krb5));
- free(kerb);
- }
- static VALUE Ticket_alloc(VALUE klass) {
- 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(klass, NULL, kerb_free, kerb);
- }
- static VALUE Ticket_initialize(VALUE self, VALUE _user, VALUE _realm) {
- VALUE user = StringValue(_user);
- VALUE realm = StringValue(_realm);
- rb_iv_set(self, "@user", user);
- rb_iv_set(self, "@realm", realm);
- return Qnil;
- }
- static VALUE Ticket_kerberos_name(VALUE self) {
- VALUE user = rb_iv_get(self, "@user");
- VALUE realm = rb_iv_get(self, "@realm");
- return rb_str_concat(
- rb_str_concat(
- user, rb_str_new2("@")),
- realm);
- }
- static VALUE Ticket_authenticate(VALUE self, VALUE _pass) {
- char *user = StringValueCStr(Ticket_kerberos_name(self));
- 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);
- return Qfalse;
- }
- void Init_krb5() {
- mKerberos = rb_define_module("Kerberos");
- cTicket = rb_define_class_under(mKerberos, "Ticket", rb_cObject);
- cException = rb_define_class_under(mKerberos, "Exception", rb_eStandardError);
- rb_define_alloc_func(cTicket, Ticket_alloc);
- rb_define_method(cTicket, "kerberos_name", Ticket_kerberos_name, 0);
- rb_define_method(cTicket, "authenticate", Ticket_authenticate, 2);
- }
Add Comment
Please, Sign In to add comment