Class: Kerberos::Krb5::Context

Inherits:
Object
  • Object
show all
Defined in:
ext/rkerberos/context.c

Instance Method Summary collapse

Constructor Details

#Kerberos::Krb5::Context.new(secure: false, profile: nil) ⇒ Object

Creates and returns a new Kerberos::Context object.

The following keyword arguments are supported:

:secure  => true|false           # Use config files only, ignore env variables
:profile => '/path/to/krb5.conf' # Use the specified profile file

Note that the profile option may not be supported on your platform.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/rkerberos/context.c', line 67

static VALUE rkrb5_context_initialize(int argc, VALUE *argv, VALUE self){
  RUBY_KRB5_CONTEXT* ptr;
  VALUE v_opts;
  VALUE v_secure, v_profile;
  ID kw_table[2] = { rb_intern("secure"), rb_intern("profile") };
  VALUE kw_vals[2];
  krb5_error_code kerror;

  TypedData_Get_Struct(self, RUBY_KRB5_CONTEXT, &rkrb5_context_data_type, ptr);

  rb_scan_args(argc, argv, "0:", &v_opts);

  // Default behavior is a normal context that may respect environment.
  if (NIL_P(v_opts)) {
    kerror = krb5_init_context(&ptr->ctx);
    if(kerror)
      rb_raise(cKrb5Exception, "krb5_init_context: %s", error_message(kerror));

    return self;
  }

  rb_get_kwargs(v_opts, kw_table, 0, 2, kw_vals);
  v_secure = kw_vals[0] == Qundef ? Qfalse : kw_vals[0];
  v_profile = kw_vals[1] == Qundef ? Qnil : kw_vals[1];

  /*
   * If a profile path is supplied, load it via profile_init_path() and
   * create a context from that profile. The KRB5_INIT_CONTEXT_SECURE flag
   * is used when the :secure option is truthy.
   */
  if (!NIL_P(v_profile)){
#ifndef HAVE_PROFILE_INIT_PATH
    rb_raise(rb_eArgError, "profile option not supported on this platform");
#else
    Check_Type(v_profile, T_STRING);

    const char *profile_path = StringValueCStr(v_profile);
    profile_t profile = NULL;
    long pres = profile_init_path(profile_path, &profile);

    if(pres != 0)
      rb_raise(cKrb5Exception, "profile_init_path: %ld", pres);

    krb5_flags flags = RTEST(v_secure) ? KRB5_INIT_CONTEXT_SECURE : 0;
    kerror = krb5_init_context_profile(profile, flags, &ptr->ctx);

    profile_release(profile);

    if(kerror)
      rb_raise(cKrb5Exception, "krb5_init_context_profile: %s", error_message(kerror));

    return self;
#endif
  }

  // No profile given, choose secure or normal init.
  if (RTEST(v_secure)){
    kerror = krb5_init_secure_context(&ptr->ctx);
    if(kerror)
      rb_raise(cKrb5Exception, "krb5_init_secure_context: %s", error_message(kerror));
  }
  else{
    kerror = krb5_init_context(&ptr->ctx);
    if(kerror)
      rb_raise(cKrb5Exception, "krb5_init_context: %s", error_message(kerror));
  }

  return self;
}

Instance Method Details

#closeObject

Closes the context object.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/rkerberos/context.c', line 41

static VALUE rkrb5_context_close(VALUE self){
  RUBY_KRB5_CONTEXT* ptr;

  TypedData_Get_Struct(self, RUBY_KRB5_CONTEXT, &rkrb5_context_data_type, ptr);

  if(ptr->ctx)
    krb5_free_context(ptr->ctx);

  ptr->ctx = NULL;

  return self;
}

#default_realmObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/rkerberos/context.c', line 143

static VALUE rkrb5_context_default_realm(VALUE self){
  RUBY_KRB5_CONTEXT* ptr;
  char* realm;
  krb5_error_code kerror;

  TypedData_Get_Struct(self, RUBY_KRB5_CONTEXT, &rkrb5_context_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  kerror = krb5_get_default_realm(ptr->ctx, &realm);

  if(kerror)
    rb_raise(cKrb5Exception, "krb5_get_default_realm: %s", error_message(kerror));

  VALUE v_realm = rb_str_new2(realm);
  krb5_free_default_realm(ptr->ctx, realm);

  return v_realm;
}

#default_realm=(v_realm) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'ext/rkerberos/context.c', line 171

static VALUE rkrb5_context_set_default_realm(VALUE self, VALUE v_realm){
  RUBY_KRB5_CONTEXT* ptr;
  char* realm;
  krb5_error_code kerror;

  TypedData_Get_Struct(self, RUBY_KRB5_CONTEXT, &rkrb5_context_data_type, ptr);

  if(!ptr->ctx)
    rb_raise(cKrb5Exception, "no context has been established");

  if(NIL_P(v_realm)){
    realm = NULL;
  }
  else{
    Check_Type(v_realm, T_STRING);
    realm = StringValueCStr(v_realm);
  }

  kerror = krb5_set_default_realm(ptr->ctx, realm);

  if(kerror)
    rb_raise(cKrb5Exception, "krb5_set_default_realm: %s", error_message(kerror));

  return v_realm;
}