SHARE
TWEET

Untitled

a guest Apr 26th, 2018 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ruby.h"
  2. #include <krb5.h>
  3. #include <stdio.h>
  4. #include <strings.h>
  5.  
  6. static VALUE mKerberos;
  7. static VALUE cTicket;
  8. static VALUE cException;
  9.  
  10. struct ruby_krb5 {
  11.   krb5_context ctx;
  12.   krb5_creds creds;
  13.   krb5_principal princ;
  14. };
  15.  
  16. #define OOM_EXCEPT() rb_raise(cException, "%s", "Error mallocing memory");
  17. #define NOSTRUCT_EXCEPT() rb_raise(cException, "%s", "Class not initialized properly (try 'new')");
  18.  
  19. void Krb5_register_error(int error) {
  20.   rb_raise(cException, "%s", error_message(error));
  21. }
  22.  
  23. static void kerb_free(void *p) {
  24.   struct ruby_krb5 *kerb;
  25.  
  26.   if (!p) return;
  27.  
  28.   kerb = (struct ruby_krb5 *)p;
  29.  
  30.   // kerb->creds is not a pointer, so we can't check for NULL; however, the
  31.   // implementation of krb5_free_cred_contents does do NULL checking, so it
  32.   // is safe (at least in the MIT version) to call it unconditionally
  33.   krb5_free_cred_contents(kerb->ctx, &kerb->creds);
  34.   if (kerb->princ)
  35.     krb5_free_principal(kerb->ctx, kerb->princ);
  36.   if (kerb->ctx)
  37.     krb5_free_context(kerb->ctx);
  38.   memset(kerb, 0, sizeof(struct ruby_krb5));
  39.   free(kerb);
  40. }
  41.  
  42. static VALUE Ticket_alloc(VALUE klass) {
  43.   struct ruby_krb5 *kerb;
  44.   krb5_error_code krbret;
  45.  
  46.   kerb = (struct ruby_krb5 *)malloc(sizeof(struct ruby_krb5));
  47.   if (kerb == NULL) {
  48.     OOM_EXCEPT();
  49.     return Qnil;
  50.   }
  51.  
  52.   memset(kerb, 0, sizeof(struct ruby_krb5));
  53.  
  54.   krbret = krb5_init_context(&kerb->ctx);
  55.   if (krbret) {
  56.     Krb5_register_error(krbret);
  57.     return Qnil;
  58.   }
  59.  
  60.   return Data_Wrap_Struct(klass, NULL, kerb_free, kerb);
  61. }
  62.  
  63. static VALUE Ticket_initialize(VALUE self, VALUE _user, VALUE _realm) {
  64.   VALUE user = StringValue(_user);
  65.   VALUE realm = StringValue(_realm);
  66.  
  67.   rb_iv_set(self, "@user", user);
  68.   rb_iv_set(self, "@realm", realm);
  69.  
  70.   return Qnil;
  71. }
  72.  
  73. static VALUE Ticket_kerberos_name(VALUE self) {
  74.   VALUE user = rb_iv_get(self, "@user");
  75.   VALUE realm = rb_iv_get(self, "@realm");
  76.  
  77.   return rb_str_concat(
  78.       rb_str_concat(
  79.         user, rb_str_new2("@")),
  80.       realm);
  81. }
  82.  
  83. static VALUE Ticket_authenticate(VALUE self, VALUE _pass) {
  84.   char *user = StringValueCStr(Ticket_kerberos_name(self));
  85.   char *pass = StringValueCStr(_pass);
  86.  
  87.   struct ruby_krb5 *kerb;
  88.   krb5_error_code krbret;
  89.  
  90.   Data_Get_Struct(self, struct ruby_krb5, kerb);
  91.   if (!kerb) {
  92.     NOSTRUCT_EXCEPT();
  93.     return Qfalse;
  94.   }
  95.  
  96.   krbret = krb5_parse_name(kerb->ctx, user, &kerb->princ);
  97.   if (krbret) {
  98.     goto failed_pass;
  99.   }
  100.  
  101.   krbret = krb5_get_init_creds_password(kerb->ctx, &kerb->creds, kerb->princ,
  102.       pass, 0, NULL, 0, NULL, NULL);
  103.  
  104.   if (krbret) {
  105.     goto failed_pass;
  106.   }
  107.  
  108.   return Qtrue;
  109.  
  110. failed_pass:
  111.   Krb5_register_error(krbret);
  112.   return Qfalse;
  113. }
  114.  
  115. void Init_krb5() {
  116.   mKerberos = rb_define_module("Kerberos");
  117.   cTicket = rb_define_class_under(mKerberos, "Ticket", rb_cObject);
  118.   cException = rb_define_class_under(mKerberos, "Exception", rb_eStandardError);
  119.  
  120.   rb_define_alloc_func(cTicket, Ticket_alloc);
  121.   rb_define_method(cTicket, "kerberos_name", Ticket_kerberos_name, 0);
  122.   rb_define_method(cTicket, "authenticate", Ticket_authenticate, 2);
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top