forked from bearded/ruby-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.c
1884 lines (1690 loc) · 46.3 KB
/
conn.c
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* conn.c
* $Id: conn.c,v 1.51 2006/08/01 00:07:53 ianmacd Exp $
*/
#include "ruby.h"
#include "rbldap.h"
#if defined(HAVE_SYS_TIME_H)
# include <sys/time.h>
#endif
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
/* RDoc needs the following bogus code to find the parent module:
*
* rb_mLDAP = rb_define_module ("LDAP");
*/
static VALUE rb_ldap_sort_obj = Qnil;
extern VALUE rb_ldap_control_new2 (LDAPControl * ctl);
extern VALUE rb_ldap_sslconn_initialize (int argc, VALUE argv[], VALUE self);
extern VALUE rb_ldap_conn_rebind (VALUE self);
VALUE rb_cLDAP_Conn;
static void
rb_ldap_conn_free (RB_LDAP_DATA * ldapdata)
{
if (ldapdata->ldap && ldapdata->bind)
{
ldap_unbind (ldapdata->ldap);
};
xfree(ldapdata);
};
static void
rb_ldap_conn_mark (RB_LDAP_DATA * ldapdata)
{
/* empty */
};
VALUE
rb_ldap_conn_new (VALUE klass, LDAP * cldap)
{
VALUE conn;
RB_LDAP_DATA *ldapdata;
conn = Data_Make_Struct (klass, RB_LDAP_DATA,
rb_ldap_conn_mark, rb_ldap_conn_free, ldapdata);
ldapdata->ldap = cldap;
ldapdata->err = 0;
ldapdata->bind = 0;
return conn;
};
VALUE
rb_ldap_conn_s_allocate (VALUE klass)
{
return rb_ldap_conn_new (klass, (LDAP *) 0);
}
/*
* call-seq:
* LDAP::Conn.new(host='localhost', port=LDAP_PORT) => LDAP::Conn
*
* Return a new LDAP::Conn connection to the server, +host+, on port +port+.
*/
VALUE
rb_ldap_conn_initialize (int argc, VALUE argv[], VALUE self)
{
LDAP *cldap;
char *chost;
int cport;
int was_verbose = Qfalse;
RB_LDAP_DATA *ldapdata;
VALUE host, port;
Data_Get_Struct (self, RB_LDAP_DATA, ldapdata);
if (ldapdata->ldap)
{
return Qnil;
}
switch (rb_scan_args (argc, argv, "02", &host, &port))
{
case 0:
chost = ALLOCA_N (char, strlen ("localhost") + 1);
strcpy (chost, "localhost");
cport = LDAP_PORT;
break;
case 1:
chost = StringValueCStr (host);
cport = LDAP_PORT;
break;
case 2:
chost = StringValueCStr (host);
cport = NUM2INT (port);
break;
default:
rb_bug ("rb_ldap_conn_new");
};
cldap = ldap_init (chost, cport);
if (!cldap)
rb_raise (rb_eLDAP_ResultError, "can't initialise an LDAP session");
ldapdata->ldap = cldap;
rb_iv_set (self, "@args", rb_ary_new4 (argc, argv));
/* Silence warning that next rb_iv_get produces. */
if (ruby_verbose == Qtrue)
{
was_verbose = Qtrue;
ruby_verbose = Qfalse;
}
if (rb_iv_get (self, "@sasl_quiet") != Qtrue)
rb_iv_set (self, "@sasl_quiet", Qfalse);
if (was_verbose == Qtrue)
ruby_verbose = Qtrue;
return Qnil;
};
/*
* call-seq:
* LDAP::Conn.open(host='localhost', port=LDAP_PORT) => LDAP::Conn
*
* Return a new LDAP::Conn connection to the server, +host+, on port +port+.
*/
VALUE
rb_ldap_conn_s_open (int argc, VALUE argv[], VALUE klass)
{
LDAP *cldap;
char *chost;
int cport;
VALUE host, port;
VALUE conn;
switch (rb_scan_args (argc, argv, "02", &host, &port))
{
case 0:
chost = ALLOCA_N (char, strlen ("localhost") + 1);
strcpy (chost, "localhost");
cport = LDAP_PORT;
break;
case 1:
chost = StringValueCStr (host);
cport = LDAP_PORT;
break;
case 2:
chost = StringValueCStr (host);
cport = NUM2INT (port);
break;
default:
rb_bug ("rb_ldap_conn_new");
};
cldap = ldap_open (chost, cport);
if (!cldap)
rb_raise (rb_eLDAP_ResultError, "can't open an LDAP session");
conn = rb_ldap_conn_new (klass, cldap);
return conn;
};
/*
* call-seq:
* LDAP::Conn.open_uri(uri) => LDAP::Conn
*
* Return a new LDAP::Conn connection to the server described with +uri+.
*/
VALUE
rb_ldap_conn_s_open_uri (VALUE klass, VALUE uri)
{
LDAP *cldap = NULL;
int rc;
rc = ldap_initialize (&cldap, StringValueCStr (uri));
if (rc || cldap == NULL)
rb_raise (rb_eLDAP_ResultError, "can't open an LDAP session");
return rb_ldap_conn_new (klass, cldap);
};
/*
* call-seq:
* conn.start_tls => nil
*
* Initiate START_TLS for the connection, +conn+.
*/
VALUE
rb_ldap_conn_start_tls_s (int argc, VALUE argv[], VALUE self)
{
#ifdef HAVE_LDAP_START_TLS_S
VALUE arg1, arg2;
RB_LDAP_DATA *ldapdata;
LDAPControl **serverctrls;
LDAPControl **clientctrls;
switch (rb_scan_args (argc, argv, "02", &arg1, &arg2))
{
case 0:
serverctrls = NULL;
clientctrls = NULL;
break;
case 1:
case 2:
rb_notimplement ();
default:
rb_bug ("rb_ldap_conn_start_tls_s");
};
GET_LDAP_DATA (self, ldapdata);
ldapdata->err = ldap_start_tls_s (ldapdata->ldap, serverctrls, clientctrls);
Check_LDAP_Result (ldapdata->err);
#else
rb_notimplement ();
#endif
return Qnil;
};
VALUE
rb_ldap_conn_rebind (VALUE self)
{
VALUE ary = rb_iv_get (self, "@args");
if (rb_obj_is_kind_of (self, rb_cLDAP_SSLConn) == Qtrue)
return rb_ldap_sslconn_initialize (RARRAY_LEN (ary), RARRAY_PTR (ary),
self);
else
return rb_ldap_conn_initialize (RARRAY_LEN (ary), RARRAY_PTR (ary),
self);
}
/*
* call-seq:
* conn.simple_bind(dn=nil, password=nil) => self
* conn.simple_bind(dn=nil, password=nil) { |conn| } => nil
*
* Bind an LDAP connection, using the DN, +dn+, and the credential, +password+.
* If a block is given, +self+ is yielded to the block.
*/
VALUE
rb_ldap_conn_simple_bind_s (int argc, VALUE argv[], VALUE self)
{
RB_LDAP_DATA *ldapdata;
VALUE arg1, arg2;
char *dn = NULL;
char *passwd = NULL;
Data_Get_Struct (self, RB_LDAP_DATA, ldapdata);
if (!ldapdata->ldap)
{
if (rb_iv_get (self, "@args") != Qnil)
{
rb_ldap_conn_rebind (self);
GET_LDAP_DATA (self, ldapdata);
}
else
{
rb_raise (rb_eLDAP_InvalidDataError,
"The LDAP handler has already unbound.");
}
}
if (ldapdata->bind)
{
rb_raise (rb_eLDAP_Error, "already bound.");
};
switch (rb_scan_args (argc, argv, "02", &arg1, &arg2))
{
case 0:
break;
case 1:
if (arg1 == Qnil)
{
dn = NULL;
}
else
{
dn = StringValueCStr (arg1);
}
break;
case 2:
if (arg1 == Qnil)
{
dn = NULL;
}
else
{
dn = StringValueCStr (arg1);
}
if (arg2 == Qnil)
{
passwd = NULL;
}
else
{
passwd = StringValueCStr (arg2);
}
break;
default:
rb_bug ("rb_ldap_conn_simple_bind_s");
}
ldapdata->err = ldap_simple_bind_s (ldapdata->ldap, dn, passwd);
Check_LDAP_Result (ldapdata->err);
ldapdata->bind = 1;
if (rb_block_given_p ())
{
rb_ensure (rb_yield, self, rb_ldap_conn_unbind, self);
return Qnil;
}
else
{
return self;
};
};
/*
* call-seq:
* conn.bind(dn=nil, password=nil, method=LDAP::LDAP_AUTH_SIMPLE)
* => self
* conn.bind(dn=nil, password=nil, method=LDAP::LDAP_AUTH_SIMPLE)
* { |conn| } => nil
*
* Bind an LDAP connection, using the DN, +dn+, the credential, +password+,
* and the bind method, +method+. If a block is given, +self+ is yielded to
* the block.
*/
VALUE
rb_ldap_conn_bind_s (int argc, VALUE argv[], VALUE self)
{
RB_LDAP_DATA *ldapdata;
VALUE arg1, arg2, arg3;
char *dn = NULL;
char *passwd = NULL;
int method = LDAP_AUTH_SIMPLE;
Data_Get_Struct (self, RB_LDAP_DATA, ldapdata);
if (!ldapdata->ldap)
{
if (rb_iv_get (self, "@args") != Qnil)
{
rb_ldap_conn_rebind (self);
GET_LDAP_DATA (self, ldapdata);
}
else
{
rb_raise (rb_eLDAP_InvalidDataError,
"The LDAP handler has already unbound.");
}
}
if (ldapdata->bind)
{
rb_raise (rb_eLDAP_Error, "already bound.");
};
switch (rb_scan_args (argc, argv, "03", &arg1, &arg2, &arg3))
{
case 0:
break;
case 1:
dn = StringValueCStr (arg1);
break;
case 2:
dn = StringValueCStr (arg1);
passwd = StringValueCStr (arg2);
break;
case 3:
dn = StringValueCStr (arg1);
passwd = StringValueCStr (arg2);
method = NUM2INT (arg3);
break;
default:
rb_bug ("rb_ldap_conn_bind_s");
}
ldapdata->err = ldap_bind_s (ldapdata->ldap, dn, passwd, method);
Check_LDAP_Result (ldapdata->err);
ldapdata->bind = 1;
if (rb_block_given_p ())
{
rb_ensure (rb_yield, self, rb_ldap_conn_unbind, self);
return Qnil;
}
else
{
return self;
};
};
/*
* call-seq:
* conn.unbind => nil
*
* Unbind the LDAP connection from the server.
*/
VALUE
rb_ldap_conn_unbind (VALUE self)
{
RB_LDAP_DATA *ldapdata;
GET_LDAP_DATA (self, ldapdata);
ldapdata->err = ldap_unbind (ldapdata->ldap);
ldapdata->bind = 0;
ldapdata->ldap = NULL;
Check_LDAP_Result (ldapdata->err);
return Qnil;
};
/*
* call-seq:
* conn.bound? => true or false
*
* Return *true* if the LDAP connection is still bound.
*/
VALUE
rb_ldap_conn_bound (VALUE self)
{
RB_LDAP_DATA *ldapdata;
Data_Get_Struct (self, RB_LDAP_DATA, ldapdata);
return ldapdata->bind == 0 ? Qfalse : Qtrue;
};
/*
* call-seq:
* conn.set_option(option, data) => self
*
* Set a session-wide option for this LDAP connection.
*
* For example:
*
* <code>conn.set_option( LDAP::LDAP_OPT_PROTOCOL_VERSION, 3 )</code>
*
* would set the protocol of this connection to LDAPv3.
*/
VALUE
rb_ldap_conn_set_option (VALUE self, VALUE opt, VALUE data)
{
/* ldap_set_option() is defined in IETF draft */
#ifdef HAVE_LDAP_SET_OPTION
RB_LDAP_DATA *ldapdata;
RB_LDAP_DATA dummy;
int idata;
void *optdata;
int copt;
if (NIL_P (self))
{
dummy.ldap = NULL;
dummy.err = dummy.bind = 0;
ldapdata = &dummy;
}
else
GET_LDAP_DATA (self, ldapdata);
copt = NUM2INT (opt);
switch (copt)
{
case LDAP_OPT_REFERRALS:
optdata = (void *) NUM2INT (data);
break;
case LDAP_OPT_DEREF:
case LDAP_OPT_SIZELIMIT:
case LDAP_OPT_TIMELIMIT:
case LDAP_OPT_RESTART:
case LDAP_OPT_PROTOCOL_VERSION:
if (ldapdata->bind != 0)
rb_raise (rb_eLDAP_ResultError,
"can't set LDAP protocol version after bind");
case LDAP_OPT_ERROR_NUMBER:
#ifdef LDAP_OPT_SSL
case LDAP_OPT_SSL:
#endif
#ifdef USE_OPENLDAP2
#ifdef LDAP_OPT_X_TLS
case LDAP_OPT_X_TLS:
#endif
#ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
case LDAP_OPT_X_TLS_REQUIRE_CERT:
#endif
#endif
idata = NUM2INT (data);
optdata = &idata;
break;
case LDAP_OPT_HOST_NAME:
case LDAP_OPT_ERROR_STRING:
#ifdef LDAP_OPT_MATCHED_DN
case LDAP_OPT_MATCHED_DN:
#endif
#ifdef USE_OPENLDAP2
#ifdef LDAP_OPT_X_TLS_CACERTFILE
case LDAP_OPT_X_TLS_CACERTFILE:
#endif
#ifdef LDAP_OPT_X_TLS_CACERTDIR
case LDAP_OPT_X_TLS_CACERTDIR:
#endif
#ifdef LDAP_OPT_X_TLS_CERT
case LDAP_OPT_X_TLS_CERT:
#endif
#ifdef LDAP_OPT_X_TLS_CERTFILE
case LDAP_OPT_X_TLS_CERTFILE:
#endif
#ifdef LDAP_OPT_X_TLS_KEYFILE
case LDAP_OPT_X_TLS_KEYFILE:
#endif
#ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
case LDAP_OPT_X_TLS_PROTOCOL_MIN:
#endif
#ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
case LDAP_OPT_X_TLS_CIPHER_SUITE:
#endif
#ifdef LDAP_OPT_X_TLS_RANDOM_FILE
case LDAP_OPT_X_TLS_RANDOM_FILE:
#endif
#endif
optdata = NIL_P (data) ? NULL : StringValueCStr (data);
break;
#ifdef LDAP_OPT_API_INFO
case LDAP_OPT_API_INFO:
rb_raise (rb_eLDAP_Error, "option is read-only");
/* optdata = (void*)rb_ldap_get_apiinfo(data); */
break;
#endif
#ifdef LDAP_OPT_SERVER_CONTROLS
case LDAP_OPT_SERVER_CONTROLS:
optdata = rb_ldap_get_controls (data);
break;
#endif
default:
rb_notimplement ();
}
ldapdata->err = ldap_set_option (ldapdata->ldap, copt, optdata);
Check_LDAP_OPT_Result (ldapdata->err);
return self;
#else
rb_notimplement ();
#endif
};
static VALUE
rb_ldap_conn_s_set_option (VALUE klass, VALUE opt, VALUE data)
{
return rb_ldap_conn_set_option (Qnil, opt, data);
}
/* call-seq:
* conn.get_option(opt) => String
*
* Return the value associated with the option, +opt+.
*/
VALUE
rb_ldap_conn_get_option (VALUE self, VALUE opt)
{
#ifdef HAVE_LDAP_GET_OPTION
RB_LDAP_DATA *ldapdata;
static RB_LDAP_DATA dummy = { NULL, 0, 0 };
long *data;
int copt;
VALUE val;
if (NIL_P (self))
{
if (dummy.ldap == NULL)
dummy.ldap = ldap_init ("", 0);
ldapdata = &dummy;
}
else
GET_LDAP_DATA (self, ldapdata);
copt = NUM2INT (opt);
#if defined(LDAP_OPT_API_INFO) && defined(LDAP_API_INFO_VERSION)
if (copt == LDAP_OPT_API_INFO)
{
LDAPAPIInfo *info;
info = ALLOCA_N (LDAPAPIInfo, 1);
/* This is from the Netscape SDK docs for 4.1* */
info->ldapai_info_version = LDAP_API_INFO_VERSION;
ldapdata->err = ldap_get_option (NULL, copt, (void *) info);
data = (long *) info;
}
else
{
data = (void *) ALLOCA_N (char, LDAP_GET_OPT_MAX_BUFFER_SIZE);
ldapdata->err = ldap_get_option (ldapdata->ldap, copt, (void *) data);
}
#else
data = (void *) ALLOCA_N (char, LDAP_GET_OPT_MAX_BUFFER_SIZE);
ldapdata->err = ldap_get_option (ldapdata->ldap, copt, (void *) data);
#endif
if (ldapdata->err == LDAP_OPT_SUCCESS)
{
switch (copt)
{
case LDAP_OPT_DEREF:
case LDAP_OPT_SIZELIMIT:
case LDAP_OPT_TIMELIMIT:
case LDAP_OPT_REFERRALS:
case LDAP_OPT_RESTART:
case LDAP_OPT_PROTOCOL_VERSION:
case LDAP_OPT_ERROR_NUMBER:
#ifdef USE_OPENLDAP2
#ifdef LDAP_OPT_X_TLS
case LDAP_OPT_X_TLS:
#endif
#ifdef LDAP_OPT_X_TLS_REQUIRE_CERT
case LDAP_OPT_X_TLS_REQUIRE_CERT:
#endif
#endif
val = INT2NUM ((int) (*data));
break;
case LDAP_OPT_HOST_NAME:
case LDAP_OPT_ERROR_STRING:
#ifdef LDAP_OPT_MATCHED_DN
case LDAP_OPT_MATCHED_DN:
#endif
#ifdef USE_OPENLDAP2
#ifdef LDAP_OPT_X_TLS_CACERTFILE
case LDAP_OPT_X_TLS_CACERTFILE:
#endif
#ifdef LDAP_OPT_X_TLS_CACERTDIR
case LDAP_OPT_X_TLS_CACERTDIR:
#endif
#ifdef LDAP_OPT_X_TLS_CERT
case LDAP_OPT_X_TLS_CERT:
#endif
#ifdef LDAP_OPT_X_TLS_CERTFILE
case LDAP_OPT_X_TLS_CERTFILE:
#endif
#ifdef LDAP_OPT_X_TLS_KEYFILE
case LDAP_OPT_X_TLS_KEYFILE:
#endif
#ifdef LDAP_OPT_X_TLS_PROTOCOL_MIN
case LDAP_OPT_X_TLS_PROTOCOL_MIN:
#endif
#ifdef LDAP_OPT_X_TLS_CIPHER_SUITE
case LDAP_OPT_X_TLS_CIPHER_SUITE:
#endif
#ifdef LDAP_OPT_X_TLS_RANDOM_FILE
case LDAP_OPT_X_TLS_RANDOM_FILE:
#endif
#endif
val = (data
&& *data) ? rb_tainted_str_new2 ((char *) (*data)) : Qnil;
break;
#ifdef LDAP_OPT_API_INFO
case LDAP_OPT_API_INFO:
val = rb_ldap_apiinfo_new ((LDAPAPIInfo *) data);
break;
#endif
default:
rb_notimplement ();
};
return val;
}
else
{
rb_raise (rb_eLDAP_Error, ldap_err2string (ldapdata->err));
};
#else
rb_notimplement ();
#endif
};
static VALUE
rb_ldap_conn_s_get_option (VALUE klass, VALUE opt)
{
return rb_ldap_conn_get_option (Qnil, opt);
}
/*
* call-seq:
* conn.perror(msg) => nil
*
* Print the text string associated with the error code of the last LDAP
* operation. +msg+ is used to prefix the error.
*/
VALUE
rb_ldap_conn_perror (VALUE self, VALUE msg)
{
RB_LDAP_DATA *ldapdata;
char *cmsg;
#if (! defined(HAVE_LDAP_PERROR)) || defined(USE_NETSCAPE_SDK)
char *str;
#endif
GET_LDAP_DATA (self, ldapdata);
cmsg = StringValueCStr (msg);
#if defined(HAVE_LDAP_PERROR) && (! defined(USE_NETSCAPE_SDK))
ldap_perror (ldapdata->ldap, cmsg);
#else
str = ldap_err2string (ldapdata->err);
fprintf (stderr, "%s: %s\n", cmsg, str);
#endif
return Qnil;
};
/*
* call-seq:
* conn.result2error(msg) => Fixnum
*
* Return the error code associated with the LDAP message, +msg+.
*/
VALUE
rb_ldap_conn_result2error (VALUE self, VALUE msg)
{
RB_LDAP_DATA *ldapdata;
RB_LDAPENTRY_DATA *edata;
int cdofree = 0;
GET_LDAP_DATA (self, ldapdata);
Check_Kind (msg, rb_cLDAP_Entry);
GET_LDAPENTRY_DATA (msg, edata);
ldapdata->err = ldap_result2error (ldapdata->ldap, edata->msg, cdofree);
return INT2NUM (ldapdata->err);
};
/*
* call-seq:
* conn.err2string(err) => String
*
* Return the text string associated with the LDAP error, +err+.
*/
VALUE
rb_ldap_conn_err2string (VALUE self, VALUE err)
{
RB_LDAP_DATA *ldapdata;
int c_err = NUM2INT (err);
char *str;
GET_LDAP_DATA (self, ldapdata);
str = ldap_err2string (c_err);
return (str ? rb_tainted_str_new2 (str) : Qnil);
};
VALUE
rb_ldap_conn_get_errno (VALUE self)
{
RB_LDAP_DATA *ldapdata;
VALUE err;
GET_LDAP_DATA (self, ldapdata);
#ifdef USE_NETSCAPE_SDK
int cerr = ldap_get_lderrno (ldapdata->ldap, NULL, NULL);
err = INT2NUM (cerr);
#else
# ifdef USE_OPENLDAP1
cerr = NUM2INT (ldapdata->ldap->ld_errno);
err = INT2NUM (cerr);
# else
rb_notimplement ();
# endif
#endif
return err;
};
static VALUE
rb_ldap_conn_invalidate_entry (VALUE msg)
{
RB_LDAPENTRY_DATA *edata;
GET_LDAPENTRY_DATA (msg, edata);
edata->ldap = NULL;
edata->msg = NULL;
return Qnil;
};
static int
rb_ldap_internal_strcmp (const char *left, const char *right)
{
VALUE res;
if (rb_ldap_sort_obj == Qtrue)
{
res = rb_funcall (rb_tainted_str_new2 (left), rb_intern ("<=>"), 1,
rb_tainted_str_new2 (right));
}
else if (rb_ldap_sort_obj != Qnil)
{
res = rb_funcall (rb_ldap_sort_obj, rb_intern ("call"), 2,
rb_tainted_str_new2 (left),
rb_tainted_str_new2 (right));
}
else
{
res = 0;
};
return INT2NUM (res);
};
static int
rb_ldap_conn_search_i (int argc, VALUE argv[], VALUE self,
RB_LDAP_DATA ** ldapdata, LDAPMessage ** cmsg)
{
VALUE base, scope, filter, attrs, attrsonly, sec, usec, s_attr, s_proc;
LDAP *cldap;
char *cbase;
int cscope;
char *cfilter;
char **cattrs;
char *sort_attr;
int cattrsonly;
int i;
struct timeval tv;
GET_LDAP_DATA (self, (*ldapdata));
cldap = (*ldapdata)->ldap;
cattrs = NULL;
cattrsonly = 0;
tv.tv_sec = 0;
tv.tv_usec = 0;
sort_attr = NULL;
rb_ldap_sort_obj = Qnil;
switch (rb_scan_args (argc, argv, "36",
&base, &scope, &filter, &attrs, &attrsonly, &sec,
&usec, &s_attr, &s_proc))
{
case 9:
rb_ldap_sort_obj = s_proc; /* Ruby's GC never starts in a C function */
case 8:
if (rb_ldap_sort_obj == Qnil)
{
rb_ldap_sort_obj = Qtrue;
}
sort_attr = StringValueCStr (s_attr);
case 7:
tv.tv_usec = NUM2INT (usec);
case 6:
tv.tv_sec = NUM2INT (sec);
case 5:
cattrsonly = (attrsonly == Qtrue) ? 1 : 0;
case 4:
if (TYPE (attrs) == T_NIL)
{
cattrs = NULL;
}
else
{
if (TYPE (attrs) == T_STRING)
attrs = rb_ary_to_ary (attrs);
else
Check_Type (attrs, T_ARRAY);
if (RARRAY_LEN (attrs) == 0)
{
cattrs = NULL;
}
else
{
cattrs = ALLOCA_N (char *, (RARRAY_LEN (attrs) + 1));
for (i = 0; i < RARRAY_LEN (attrs); i++)
{
cattrs[i] = StringValueCStr (RARRAY_PTR (attrs)[i]);
};
cattrs[RARRAY_LEN (attrs)] = NULL;
}
}
case 3:
cbase = StringValueCStr (base);
cscope = NUM2INT (scope);
cfilter = StringValueCStr (filter);
break;
default:
rb_bug ("rb_ldap_conn_search_s");
};
(*cmsg) = NULL;
if (tv.tv_sec == 0 && tv.tv_usec == 0)
{
(*ldapdata)->err = ldap_search_s (cldap, cbase, cscope, cfilter,
cattrs, cattrsonly, cmsg);
}
else
{
(*ldapdata)->err = ldap_search_st (cldap, cbase, cscope, cfilter,
cattrs, cattrsonly, &tv, cmsg);
}
if (!(cmsg && (*cmsg)))
{
rb_raise (rb_eRuntimeError, "no result returned by search");
}
Check_LDAP_Result ((*ldapdata)->err);
#ifdef HAVE_LDAP_SORT_ENTRIES
if (rb_ldap_sort_obj != Qnil)
{
ldap_sort_entries ((*ldapdata)->ldap, cmsg,
sort_attr, rb_ldap_internal_strcmp);
};
#endif
rb_ldap_sort_obj = Qnil;
return (*ldapdata)->err;
}
static VALUE
rb_ldap_conn_search_b (VALUE rdata)
{
void **data = (void **) rdata;
LDAP *cldap = (LDAP *) data[0];
LDAPMessage *cmsg = (LDAPMessage *) data[1];
LDAPMessage *e;
VALUE m;
for (e = ldap_first_entry (cldap, cmsg); e != NULL;
e = ldap_next_entry (cldap, e))
{
m = rb_ldap_entry_new (cldap, e);
rb_ensure (rb_yield, m, rb_ldap_conn_invalidate_entry, m);
}
return Qnil;
}
static VALUE
rb_ldap_conn_search2_b (VALUE rdata)
{
void **data = (void *) rdata;
LDAP *cldap = (LDAP *) data[0];
LDAPMessage *cmsg = (LDAPMessage *) data[1];
VALUE ary = (VALUE) data[2];
LDAPMessage *e;
VALUE m;
VALUE hash;
for (e = ldap_first_entry (cldap, cmsg); e != NULL;
e = ldap_next_entry (cldap, e))
{
m = rb_ldap_entry_new (cldap, e);
hash = rb_ldap_entry_to_hash (m);
rb_ary_push (ary, hash);
if (rb_block_given_p ())
{
rb_ensure (rb_yield, hash, rb_ldap_conn_invalidate_entry, m);
}
}
return Qnil;
}
static VALUE
rb_ldap_msgfree (VALUE data)
{
LDAPMessage *cmsg = (LDAPMessage *) data;
ldap_msgfree (cmsg);
return Qnil;
}
VALUE
rb_ldap_parse_result (LDAP * cldap, LDAPMessage * cmsg)
{
int rc, err, i;
char **referrals;
LDAPControl **serverctrls;
VALUE refs, ctls, ary;
refs = rb_ary_new ();
ctls = rb_ary_new ();
ary = rb_ary_new ();
rc = ldap_parse_result (cldap, cmsg, &err, NULL, NULL,
&referrals, &serverctrls, 0);
Check_LDAP_Result (rc);
Check_LDAP_Result (err);
if (referrals)
{
for (i = 0; referrals[i]; i++)
{
rb_ary_push (refs, rb_str_new2 (referrals[i]));
}
}
if (serverctrls)
{