File: entry.c

package info (click to toggle)
libldap-ruby 0.6.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 484 kB
  • ctags: 343
  • sloc: ansic: 2,298; ruby: 613; sh: 125; makefile: 59
file content (146 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download
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
/*
 * message.c
 * $Id: entry.c,v 1.11 2001/12/31 17:34:29 ttate Exp $
 */

#include "ruby.h"
#include "rbldap.h"

VALUE rb_cLDAP_Entry;


void
rb_ldap_entry_free(RB_LDAPENTRY_DATA *edata)
{
  /* edata->msg is valid in a block given by each search operation */
  /* ldap_msgfree should be called after ldap_search */
};

VALUE
rb_ldap_entry_new(LDAP *ldap, LDAPMessage *msg)
{
  VALUE val;
  RB_LDAPENTRY_DATA *edata;
  val = Data_Make_Struct(rb_cLDAP_Entry, RB_LDAPENTRY_DATA,
			 0, 0 /* rb_ldap_entry_free */, edata);
  edata->ldap    = ldap;
  edata->msg = msg;
  return val;
};

VALUE
rb_ldap_entry_get_dn(VALUE self)
{
  RB_LDAPENTRY_DATA *edata;
  char *cdn;
  VALUE dn;

  GET_LDAPENTRY_DATA(self, edata);

  cdn = ldap_get_dn(edata->ldap, edata->msg);
  if( cdn ){
    dn = rb_tainted_str_new2(cdn);
    ldap_memfree(cdn);
  }
  else{
    dn = Qnil;
  };

  return dn;
};

VALUE
rb_ldap_entry_get_values(VALUE self, VALUE attr)
{
  RB_LDAPENTRY_DATA *edata;
  char *c_attr;
  struct berval **c_vals;
  int i;
  VALUE vals;

  GET_LDAPENTRY_DATA(self, edata);
  c_attr = STR2CSTR(attr);

  c_vals = ldap_get_values_len(edata->ldap, edata->msg, c_attr);
  if( c_vals ){
    vals = rb_ary_new();
    for( i=0; c_vals[i] != NULL; i++ ){
      VALUE str;
      str = rb_tainted_str_new(c_vals[i]->bv_val, c_vals[i]->bv_len);
      rb_ary_push(vals,str);
    };
    ldap_value_free_len(c_vals);
  }
  else{
    vals = Qnil;
  };

  return vals;
};

VALUE
rb_ldap_entry_get_attributes(VALUE self)
{
  RB_LDAPENTRY_DATA *edata;
  int i;
  VALUE vals;
  char *attr;
  BerElement *ber;

  GET_LDAPENTRY_DATA(self, edata);
  
  vals = rb_ary_new();
  for( attr = ldap_first_attribute(edata->ldap, edata->msg, &ber);
       attr != NULL;
       attr = ldap_next_attribute(edata->ldap, edata->msg, ber) ){
    rb_ary_push(vals, rb_tainted_str_new2(attr));
  };

  /* this code may cause segv
#if !defined(USE_OPENLDAP1)
  if( ber != NULL ){
    ber_free(ber, 0);
  };
#endif
  */

  return vals;
};

VALUE
rb_ldap_entry_to_hash(VALUE self)
{
  VALUE attrs = rb_ldap_entry_get_attributes(self);
  VALUE hash = rb_hash_new();
  VALUE attr, vals, lst;
  int i;

  Check_Type(attrs, T_ARRAY);
  rb_hash_aset(hash, rb_tainted_str_new2("dn"),
	       rb_ary_new3(1,rb_ldap_entry_get_dn(self)));
  for( i=0; i < RARRAY(attrs)->len; i++ ){
    attr = RARRAY(attrs)->ptr[i];
    vals = rb_ldap_entry_get_values(self, attr);
    rb_hash_aset(hash, attr, vals);
  };

  return hash;
};

void
Init_ldap_entry()
{
  rb_cLDAP_Entry = rb_define_class_under(rb_mLDAP, "Entry", rb_cObject);
  rb_define_const(rb_mLDAP, "Message", rb_cLDAP_Entry); /* for compatibility */
  rb_undef_method(CLASS_OF(rb_cLDAP_Entry), "new");
  rb_ldap_entry_define_method("get_dn",
			      rb_ldap_entry_get_dn, 0);
  rb_ldap_entry_define_method("get_values",
			      rb_ldap_entry_get_values, 1);
  rb_ldap_entry_define_method("get_attributes",
			      rb_ldap_entry_get_attributes, 0);
  rb_alias(rb_cLDAP_Entry, rb_intern("dn"), rb_intern("get_dn"));
  rb_alias(rb_cLDAP_Entry, rb_intern("vals"), rb_intern("get_values"));
  rb_alias(rb_cLDAP_Entry, rb_intern("attrs"), rb_intern("get_attributes"));
  rb_ldap_entry_define_method("to_hash", rb_ldap_entry_to_hash, 0);
};