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.



122
123
124
125
126
127
128
129
130
131
132
133
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/rkerberos/context.c', line 122

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

#close(force: false) ⇒ Object

Closes the context object. Raises if wrappers are still borrowing the context. With force: true, the context is closed to new operations and borrowers immediately, but its native resources are retained until the existing borrowers have closed.



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
# File 'ext/rkerberos/context.c', line 76

static VALUE rkrb5_context_close(int argc, VALUE* argv, VALUE self){
  RUBY_KRB5_CONTEXT* ptr;
  VALUE v_opts, v_force;
  ID kw_table[1] = { rb_intern("force") };
  VALUE kw_vals[1];

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

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

  if(NIL_P(v_opts))
    v_opts = rb_hash_new();

  rb_get_kwargs(v_opts, kw_table, 0, 1, kw_vals);
  v_force = kw_vals[0] == Qundef ? Qfalse : kw_vals[0];

  if(ptr->closed || !ptr->ctx)
    return self;

  if(ptr->borrowers > 0 && !RTEST(v_force))
    rb_raise(cKrb5Exception, "context is in use by %lu dependent wrapper%s",
      (unsigned long)ptr->borrowers, ptr->borrowers == 1 ? "" : "s");

  ptr->closed = 1;

  if(ptr->borrowers == 0){
    krb5_free_context(ptr->ctx);
    ptr->ctx = NULL;
  }

  return self;
}

#default_realmObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/rkerberos/context.c', line 198

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 || ptr->closed)
    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



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
# File 'ext/rkerberos/context.c', line 226

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 || ptr->closed)
    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;
}