Class: LDAP

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap/version.rb,
ext/ldap.c

Defined Under Namespace

Classes: Error, Message

Constant Summary collapse

Version =
VERSION = '0.0.4'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/ldap.c', line 68

static VALUE rldap_initialize(int argc, VALUE *argv, VALUE obj)
{
	VALUE rhost, rport;
	char *host;
	int port;
	RLDAP_WRAP *wrapper;

	rb_scan_args(argc, argv, "11", &rhost, &rport);

	if (NIL_P(rport))
		rport = INT2FIX(LDAP_PORT);

	wrapper = get_wrapper(obj);
	host = StringValuePtr(rhost);
	port = FIX2INT(rport);

	wrapper->ld = (LDAP *)ldap_init(host, port);

	return obj;
}

Class Method Details

.err2string(rerrno) ⇒ Object

class LDAP



51
52
53
54
55
56
# File 'ext/ldap.c', line 51

static VALUE rldap_err2string(VALUE klass, VALUE rerrno)
{
	int errno;
	errno = FIX2INT(rerrno);
	return rb_str_new2(ldap_err2string(errno));
}

Instance Method Details

#bind(*args) ⇒ Object



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
# File 'ext/ldap.c', line 236

static VALUE rldap_bind(int argc, VALUE *argv, VALUE obj)
{
	RLDAP_WRAP *wrapper;
	char *bind_dn = NULL, *bind_password = NULL;
	int retval;
	VALUE rdn, rpassword;
	
	rb_scan_args(argc, argv, "02", &rdn, &rpassword);
	
	if (NIL_P(rdn))
		bind_dn = NULL;
	else
		bind_dn = StringValuePtr(rdn);
	if (NIL_P(rpassword))
		bind_password = NULL;
	else
		bind_password = StringValuePtr(rpassword);
	
	wrapper = get_wrapper(obj);

	retval = ldap_bind_s(wrapper->ld, bind_dn, bind_password, LDAP_AUTH_SIMPLE);
	
	if (retval != LDAP_SUCCESS)
		rldap_raise(retval);
	else
		return Qtrue;
}

#errnoObject



207
208
209
210
# File 'ext/ldap.c', line 207

static VALUE rldap_errno(VALUE obj)
{
	return INT2NUM(rldap_errno_c(obj));
}

#inspectObject



224
225
226
227
228
229
230
231
232
233
234
# File 'ext/ldap.c', line 224

static VALUE rldap_inspect(VALUE obj)
{
	VALUE ruri, ret;
	
	ruri = rb_funcall(rldap_uri(obj), rb_intern("dump"), 0);
	ret = rb_str_new2("#<LDAP @uri=");
	rb_str_cat2(ret, StringValuePtr(ruri));
	rb_str_cat2(ret, ">");
	
	return ret;
}

#sasl_bind(*args) ⇒ Object



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
# File 'ext/ldap.c', line 354

static VALUE rldap_sasl_bind(int argc, VALUE *argv, VALUE obj)
{
	RLDAP_WRAP *wrapper;
	char *bind_dn = NULL, *passwd = NULL, *sasl_mech = NULL,
		*sasl_realm = NULL, *sasl_authz_id = NULL, *sasl_authc_id = NULL;
	VALUE rbind_dn, rpasswd, rsasl_mech, rsasl_realm,
		rsasl_authz_id, rsasl_authc_id, rprops;
	int retval;
	RLDAP_BICTX *ctx;
	
	wrapper = get_wrapper(obj);
	rb_scan_args(argc, argv, "07", &rbind_dn, &rpasswd, &rsasl_mech, &rsasl_realm, &rsasl_authz_id, &rsasl_authc_id, &rprops);
	
	if (!NIL_P(rprops))
		ldap_set_option(wrapper->ld, LDAP_OPT_X_SASL_SECPROPS, StringValuePtr(rprops));
	
	if (!NIL_P(rbind_dn))
		bind_dn = StringValuePtr(rbind_dn);
	if (!NIL_P(rpasswd))
		passwd = StringValuePtr(rpasswd);
	if (!NIL_P(rsasl_mech))
		sasl_mech = StringValuePtr(rsasl_mech);
	if (!NIL_P(rsasl_realm))
		sasl_realm = StringValuePtr(rsasl_realm);
	if (!NIL_P(rsasl_authz_id))
		sasl_authz_id = StringValuePtr(rsasl_authz_id);
	if (!NIL_P(rsasl_authc_id))
		sasl_authc_id = StringValuePtr(rsasl_authc_id);
	
	ctx = _rldap_sasl_setdefs(wrapper->ld, sasl_mech, sasl_realm, sasl_authc_id, passwd, sasl_authz_id);
	
	retval = ldap_sasl_interactive_bind_s(wrapper->ld, bind_dn, ctx->mech, NULL, NULL, LDAP_SASL_AUTOMATIC, _rldap_sasl_interact, ctx);
	
	_rldap_sasl_freedefs(ctx);
	
	if (retval != LDAP_SUCCESS)
		rldap_raise(retval);
	else
		return Qtrue;
}

#search(*args) ⇒ Object



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
# File 'ext/ldap.c', line 102

static VALUE rldap_search(int argc, VALUE *argv, VALUE obj)
{
	RLDAP_WRAP *wrapper;
	char *base, *filter;
	int retval, count, i, scope;
	LDAPMessage *res, *msg;
	VALUE ary, rbase, rfilter, rscope;
	ID iscope;
	
	rb_scan_args(argc, argv, "21", &rbase, &rfilter, &rscope);

	switch(TYPE(rscope)) {
		case T_NIL:
			scope = LDAP_SCOPE_SUBTREE;
			break;
		case T_FIXNUM:
			scope = FIX2INT(rscope);
			break;
		case T_SYMBOL:
		case T_STRING:
			iscope = rb_to_id(rscope);
			if (iscope == rb_intern("subtree"))
				scope = LDAP_SCOPE_SUBTREE;
			if (iscope == rb_intern("base"))
				scope = LDAP_SCOPE_BASE;
			if (iscope == rb_intern("one"))
				scope = LDAP_SCOPE_ONE;
			break;
		default:
			rb_raise(rb_eTypeError, "not a valid scope");
			break;
	}
	
	wrapper = get_wrapper(obj);
	base = StringValuePtr(rbase);
	filter = StringValuePtr(rfilter);

	retval = ldap_search_ext_s(wrapper->ld, base, scope, filter, NULL, 0, NULL, NULL, NULL, 0, &res);

	if (retval != LDAP_SUCCESS)
		rldap_raise(retval);

	count = ldap_count_entries(wrapper->ld, res);
	
	if (count == -1) {
		int errno;
		ldap_get_option(wrapper->ld, LDAP_OPT_RESULT_CODE, &errno);
		rldap_raise(errno);
	}
	
	ary = rb_ary_new2((long)count);
	
	msg = ldap_first_entry(wrapper->ld, res);
	
	for (i=0; i<count; i++) {
		rb_ary_store(ary, (long)i, ldapmessage2obj(wrapper->ld, msg));
		msg = ldap_next_entry(wrapper->ld, msg);
	}
	
	return ary;
}

#set_option(roption, rvalue) ⇒ Object



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/ldap.c', line 164

static VALUE rldap_set_option(VALUE obj, VALUE roption, VALUE rvalue)
{
	RLDAP_WRAP *wrapper;
	int retval;
	int option;
	int ival;
	char *sval;
	void *val;
	
	wrapper = get_wrapper(obj);
	option = FIX2INT(roption);
	
	if (TYPE(rvalue) == T_STRING) {
		sval = StringValuePtr(rvalue);
		val = &sval;
	} else {
		ival = FIX2INT(rvalue);
		val = &ival;
	}
	
	retval = ldap_set_option(wrapper->ld, option, val);
	
	if (retval == LDAP_OPT_SUCCESS)
		return Qtrue;
	else
		return Qfalse;
}

#start_tlsObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/ldap.c', line 89

static VALUE rldap_start_tls(VALUE obj)
{
	RLDAP_WRAP *wrapper;
	int retval;

	wrapper = get_wrapper(obj);
	retval = ldap_start_tls_s(wrapper->ld, NULL, NULL);
	if (retval == LDAP_SUCCESS)
		return Qtrue;
	else
		rldap_raise(retval);
}

#unbindObject



264
265
266
267
268
269
270
271
272
273
274
275
# File 'ext/ldap.c', line 264

static VALUE rldap_unbind(VALUE obj)
{
	RLDAP_WRAP *wrapper;
	int retval;
	
	wrapper = get_wrapper(obj);
	retval = ldap_unbind_s(wrapper->ld);
	if (retval != LDAP_SUCCESS)
		rldap_raise(retval);
	else
		return Qtrue;
}

#uriObject



212
213
214
215
216
217
218
219
220
221
222
# File 'ext/ldap.c', line 212

static VALUE rldap_uri(VALUE obj)
{
	RLDAP_WRAP *wrapper;
	char *uri;
	VALUE ruri;
	
	wrapper = get_wrapper(obj);
	ldap_get_option(wrapper->ld, LDAP_OPT_URI, &uri);
	
	return rb_str_new2(uri);
}

#version=(version) ⇒ Object



192
193
194
195
# File 'ext/ldap.c', line 192

static VALUE rldap_set_version(VALUE obj, VALUE version)
{
	return rldap_set_option(obj, INT2FIX(LDAP_OPT_PROTOCOL_VERSION), version);
}