1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
/* -*- C -*-
* $Id: misc.c,v 1.11 2006/07/03 22:54:52 ianmacd Exp $
*/
#include "ruby.h"
#include "rbldap.h"
VALUE rb_sLDAP_APIInfo;
VALUE rb_cLDAP_Control;
#ifdef LDAP_OPT_API_INFO
VALUE
rb_ldap_apiinfo_new (LDAPAPIInfo * info)
{
VALUE info_version, api_version, protocol_version;
VALUE extensions, vendor_name, vendor_version;
int i;
info_version = INT2NUM (info->ldapai_info_version);
api_version = INT2NUM (info->ldapai_api_version);
protocol_version = INT2NUM (info->ldapai_protocol_version);
vendor_version = INT2NUM (info->ldapai_vendor_version);
vendor_name = rb_tainted_str_new2 (info->ldapai_vendor_name);
extensions = rb_ary_new ();
for (i = 0; info->ldapai_extensions[i]; i++)
{
rb_ary_push (extensions,
rb_tainted_str_new2 (info->ldapai_extensions[i]));
}
return rb_struct_new (rb_sLDAP_APIInfo,
info_version, api_version, protocol_version,
extensions, vendor_name, vendor_version, 0);
}
LDAPAPIInfo *
rb_ldap_get_apiinfo (VALUE data)
{
LDAPAPIInfo *info;
VALUE r_extensions;
int len, i;
char **c_extensions;
if (data == Qnil)
return NULL;
info = ALLOC_N (LDAPAPIInfo, 1);
info->ldapai_info_version =
FIX2INT (rb_struct_getmember (data, rb_intern ("info_version")));
info->ldapai_api_version =
FIX2INT (rb_struct_getmember (data, rb_intern ("api_version")));
info->ldapai_protocol_version =
FIX2INT (rb_struct_getmember (data, rb_intern ("protocol_version")));
r_extensions = rb_struct_getmember (data, rb_intern ("extensions"));
len = RARRAY_LEN (r_extensions);
c_extensions = ALLOCA_N (char *, len);
for (i = 0; i <= len - 1; i++)
{
VALUE str = RARRAY_PTR (r_extensions)[i];
RB_LDAP_SET_STR (c_extensions[i], str);
}
info->ldapai_extensions = c_extensions;
RB_LDAP_SET_STR (info->ldapai_vendor_name,
rb_struct_getmember (data, rb_intern ("vendor_name")));
info->ldapai_vendor_version =
FIX2INT (rb_struct_getmember (data, rb_intern ("vendor_version")));
return info;
}
#endif /* LDAP_OPT_API_INFO */
#ifdef HAVE_LDAPCONTROL
static void
rb_ldap_control_free (LDAPControl * ctl)
{
if (ctl)
{
if (ctl->ldctl_value.bv_val)
xfree (ctl->ldctl_value.bv_val);
if (ctl->ldctl_oid)
xfree (ctl->ldctl_oid);
xfree (ctl);
}
}
VALUE
rb_ldap_control_new (LDAPControl * ctl)
{
if (!ctl)
return Qnil;
else
return Data_Wrap_Struct (rb_cLDAP_Control, 0, rb_ldap_control_free, ctl);
}
/* Identical to rb_ldap_control_new, but does not define a routine with which
to free memory. This should be called only by rb_ldap_parse_result().
*/
VALUE
rb_ldap_control_new2 (LDAPControl * ctl)
{
if (!ctl)
return Qnil;
else
return Data_Wrap_Struct (rb_cLDAP_Control, 0, 0, ctl);
}
/* This is called by #initialize_copy and is using for duping/cloning. */
VALUE
rb_ldap_control_copy (VALUE copy, VALUE orig)
{
LDAPControl *orig_ctl, *copy_ctl;
Data_Get_Struct (orig, LDAPControl, orig_ctl);
Data_Get_Struct (copy, LDAPControl, copy_ctl);
memcpy (copy_ctl, orig_ctl, (size_t) sizeof (LDAPControl));
return copy;
}
static VALUE
rb_ldap_control_s_allocate (VALUE klass)
{
LDAPControl *ctl;
ctl = ALLOC_N (LDAPControl, 1);
ctl->ldctl_value.bv_val = NULL;
ctl->ldctl_value.bv_len = 0;
ctl->ldctl_oid = NULL;
ctl->ldctl_iscritical = 0;
return Data_Wrap_Struct (klass, 0, rb_ldap_control_free, ctl);
}
#if RUBY_VERSION_CODE < 170
static VALUE
rb_ldap_control_s_new (int argc, VALUE argv[], VALUE klass)
{
VALUE obj;
obj = rb_ldap_control_s_allocate (klass);
rb_obj_call_init (obj, argc, argv);
return obj;
}
#endif
static VALUE
rb_ldap_control_set_value (VALUE self, VALUE val)
{
LDAPControl *ctl;
Data_Get_Struct (self, LDAPControl, ctl);
if (ctl->ldctl_value.bv_val)
free (ctl->ldctl_value.bv_val);
if (val == Qnil)
{
ctl->ldctl_value.bv_val = NULL;
ctl->ldctl_value.bv_len = 0;
}
else
{
RB_LDAP_SET_STR (ctl->ldctl_value.bv_val, val);
ctl->ldctl_value.bv_len = RSTRING_LEN (val);
}
return val;
}
static VALUE
rb_ldap_control_get_value (VALUE self)
{
LDAPControl *ctl;
VALUE val;
Data_Get_Struct (self, LDAPControl, ctl);
if (ctl->ldctl_value.bv_len == 0 || ctl->ldctl_value.bv_val == NULL)
{
val = Qnil;
}
else
{
val =
rb_tainted_str_new (ctl->ldctl_value.bv_val, ctl->ldctl_value.bv_len);
}
return val;
}
/*
* Document-method: value
*
* call-seq:
* ctrl.value => String or nil
*
* Return the value of the control.
*/
/*
* Document-method: value=
*
* call-seq:
* ctrl.value=(val) => val
*
* Set the value of the control.
*/
static VALUE
rb_ldap_control_value (int argc, VALUE argv[], VALUE self)
{
VALUE val;
if (rb_scan_args (argc, argv, "01", &val) == 1)
val = rb_ldap_control_set_value (self, val);
else
val = rb_ldap_control_get_value (self);
return val;
}
static VALUE
rb_ldap_control_set_oid (VALUE self, VALUE val)
{
LDAPControl *ctl;
Data_Get_Struct (self, LDAPControl, ctl);
if (ctl->ldctl_oid)
free (ctl->ldctl_oid);
if (val == Qnil)
{
ctl->ldctl_oid = NULL;
}
else
{
RB_LDAP_SET_STR (ctl->ldctl_oid, val);
}
return val;
}
static VALUE
rb_ldap_control_get_oid (VALUE self)
{
LDAPControl *ctl;
VALUE val;
Data_Get_Struct (self, LDAPControl, ctl);
if (ctl->ldctl_oid == NULL)
{
val = Qnil;
}
else
{
val = rb_tainted_str_new2 (ctl->ldctl_oid);
}
return val;
}
/*
* Document-method: oid
*
* call-seq:
* ctrl.oid => String or nil
*
* Return the OID of the control.
*/
/*
* Document-method: oid=
*
* call-seq:
* ctrl.oid=(oid) => oid
*
* Set the OID of the control.
*/
static VALUE
rb_ldap_control_oid (int argc, VALUE argv[], VALUE self)
{
VALUE val;
LDAPControl *ctl;
Data_Get_Struct (self, LDAPControl, ctl);
if (rb_scan_args (argc, argv, "01", &val) == 1)
{
val = rb_ldap_control_set_oid (self, val);
}
else
{
val = rb_ldap_control_get_oid (self);
}
return val;
}
static VALUE
rb_ldap_control_set_critical (VALUE self, VALUE val)
{
LDAPControl *ctl;
Data_Get_Struct (self, LDAPControl, ctl);
ctl->ldctl_iscritical = (val == Qtrue) ? 1 : 0;
return val;
}
static VALUE
rb_ldap_control_get_critical (VALUE self)
{
LDAPControl *ctl;
VALUE val;
Data_Get_Struct (self, LDAPControl, ctl);
val = ctl->ldctl_iscritical ? Qtrue : Qfalse;
return val;
}
/*
* Document-method: critical
*
* call-seq:
* ctrl.critical => true or false
* ctrl.critical? => true or false
* ctrl.iscritical => true or false
*
* Return the criticality of the control.
*/
/*
* Document-method: critical=
*
* call-seq:
* ctrl.critical=(val) => val
* ctrl.iscritical=(val) => val
*
* Set the criticality of the control. +val+ should be *true* or *false*.
*/
static VALUE
rb_ldap_control_critical (int argc, VALUE argv[], VALUE self)
{
VALUE val;
LDAPControl *ctl;
Data_Get_Struct (self, LDAPControl, ctl);
if (rb_scan_args (argc, argv, "01", &val) == 1)
{
val = rb_ldap_control_set_critical (self, val);
}
else
{
val = rb_ldap_control_get_critical (self);
}
return val;
}
/*
* Document-method: new
*
* call-seq:
* LDAP::Control.new(oid, value, criticality) => LDAP::Control
*
* Create a new LDAP::Control. +oid+ is the OID of the control, +value+ is the
* value to be assigned to the control, and +criticality+ is the criticality
* of the control, which should be *true* or *false*.
*/
static VALUE
rb_ldap_control_initialize (int argc, VALUE argv[], VALUE self)
{
VALUE oid, value, critical;
switch (rb_scan_args (argc, argv, "03", &oid, &value, &critical))
{
case 3:
rb_ldap_control_set_critical (self, critical);
case 2:
rb_ldap_control_set_value (self, value);
case 1:
rb_ldap_control_set_oid (self, oid);
default:
break;
}
return Qnil;
}
/*
* call-seq:
* ctrl.inspect => String
*
* Produce a concise representation of the control.
*/
static VALUE
rb_ldap_control_inspect (VALUE self)
{
VALUE str;
str = rb_tainted_str_new2 ("#<");
rb_str_cat2 (str, rb_class2name (CLASS_OF (self)));
rb_str_cat2 (str, " oid=");
rb_str_concat (str, rb_inspect (rb_ldap_control_get_oid (self)));
rb_str_cat2 (str, " value=");
rb_str_concat (str, rb_inspect (rb_ldap_control_get_value (self)));
rb_str_cat2 (str, " iscritical=");
rb_str_concat (str, rb_inspect (rb_ldap_control_get_critical (self)));
rb_str_cat2 (str, ">");
return str;
}
VALUE
rb_ldap_controls_new (LDAPControl ** ctrls)
{
int i;
VALUE ary;
if (!ctrls)
return Qnil;
ary = rb_ary_new ();
for (i = 0; ctrls[i]; i++)
rb_ary_push (ary, rb_ldap_control_new (ctrls[i]));
return ary;
}
LDAPControl *
rb_ldap_get_control (VALUE obj)
{
LDAPControl *ctl;
if (obj == Qnil)
{
return NULL;
}
else
{
Data_Get_Struct (obj, LDAPControl, ctl);
return ctl;
}
}
LDAPControl **
rb_ldap_get_controls (VALUE data)
{
LDAPControl **ctls;
int len, i;
if (data == Qnil)
return NULL;
Check_Type (data, T_ARRAY);
len = RARRAY_LEN (data);
ctls = ALLOC_N (LDAPControl *, len + 1);
for (i = 0; i < len; i++)
{
ctls[i] = rb_ldap_get_control (rb_ary_entry (data, i));
}
ctls[len] = NULL;
return ctls;
}
#endif
/* Document-class: LDAP::Control
*
* Create, manipulate and inspect LDAP controls.
*/
void
Init_ldap_misc ()
{
rb_sLDAP_APIInfo = rb_struct_define ("APIInfo", "info_version", /* ldapai_xxxx */
"api_version",
"protocol_version",
"extensions",
"vendor_name", "vendor_version", NULL);
rb_define_const (rb_mLDAP, "APIInfo", rb_sLDAP_APIInfo);
#ifdef HAVE_LDAPCONTROL
rb_cLDAP_Control = rb_define_class_under (rb_mLDAP, "Control", rb_cObject);
#if RUBY_VERSION_CODE < 170
rb_define_singleton_method (rb_cLDAP_Control, "new",
rb_ldap_control_s_new, -1);
#endif
#if RUBY_VERSION_CODE >= 173
rb_define_alloc_func (rb_cLDAP_Control, rb_ldap_control_s_allocate);
#else
rb_define_singleton_method (rb_cLDAP_Control, "allocate",
rb_ldap_control_s_allocate, 0);
#endif
rb_define_method (rb_cLDAP_Control, "initialize",
rb_ldap_control_initialize, -1);
rb_define_method (rb_cLDAP_Control, "initialize_copy", rb_ldap_control_copy,
1);
rb_define_method (rb_cLDAP_Control, "inspect", rb_ldap_control_inspect, 0);
rb_define_method (rb_cLDAP_Control, "oid", rb_ldap_control_oid, -1);
rb_define_method (rb_cLDAP_Control, "oid=", rb_ldap_control_oid, -1);
rb_define_method (rb_cLDAP_Control, "value", rb_ldap_control_value, -1);
rb_define_method (rb_cLDAP_Control, "value=", rb_ldap_control_value, -1);
rb_define_method (rb_cLDAP_Control, "critical?", rb_ldap_control_critical,
-1);
rb_define_method (rb_cLDAP_Control, "critical", rb_ldap_control_critical,
-1);
rb_define_method (rb_cLDAP_Control, "critical=", rb_ldap_control_critical,
-1);
rb_define_method (rb_cLDAP_Control, "iscritical", rb_ldap_control_critical,
-1);
rb_define_method (rb_cLDAP_Control, "iscritical=", rb_ldap_control_critical,
-1);
#endif
}
|