1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2011 The OpenLDAP Foundation.
5 * Portions Copyright 1999 John C. Quillan.
6 * Portions Copyright 2002 myinternet Limited.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17
18#include "perl_back.h"
19#include "../config.h"
20
21static void perl_back_xs_init LDAP_P((PERL_BACK_XS_INIT_PARAMS));
22EXT void boot_DynaLoader LDAP_P((PERL_BACK_BOOT_DYNALOADER_PARAMS));
23
24PerlInterpreter *PERL_INTERPRETER = NULL;
25ldap_pvt_thread_mutex_t	perl_interpreter_mutex;
26
27
28/**********************************************************
29 *
30 * Init
31 *
32 **********************************************************/
33
34int
35perl_back_initialize(
36	BackendInfo	*bi
37)
38{
39	char *embedding[] = { "", "-e", "0" };
40	int argc = 3;
41
42	bi->bi_open = NULL;
43	bi->bi_config = 0;
44	bi->bi_close = perl_back_close;
45	bi->bi_destroy = 0;
46
47	bi->bi_db_init = perl_back_db_init;
48	bi->bi_db_config = perl_back_db_config;
49	bi->bi_db_open = perl_back_db_open;
50	bi->bi_db_close = 0;
51	bi->bi_db_destroy = perl_back_db_destroy;
52
53	bi->bi_op_bind = perl_back_bind;
54	bi->bi_op_unbind = 0;
55	bi->bi_op_search = perl_back_search;
56	bi->bi_op_compare = perl_back_compare;
57	bi->bi_op_modify = perl_back_modify;
58	bi->bi_op_modrdn = perl_back_modrdn;
59	bi->bi_op_add = perl_back_add;
60	bi->bi_op_delete = perl_back_delete;
61	bi->bi_op_abandon = 0;
62
63	bi->bi_extended = 0;
64
65	bi->bi_chk_referrals = 0;
66
67	bi->bi_connection_init = 0;
68	bi->bi_connection_destroy = 0;
69
70	/* injecting code from perl_back_open, because using fonction reference  (bi->bi_open) is not functional */
71	Debug( LDAP_DEBUG_TRACE, "perl backend open\n", 0, 0, 0 );
72
73	if( PERL_INTERPRETER != NULL ) {
74		Debug( LDAP_DEBUG_ANY, "perl backend open: already opened\n",
75			0, 0, 0 );
76		return 1;
77	}
78
79	ldap_pvt_thread_mutex_init( &perl_interpreter_mutex );
80
81#ifdef PERL_SYS_INIT3
82	PERL_SYS_INIT3(&argc, &embedding, (char ***)NULL);
83#endif
84	PERL_INTERPRETER = perl_alloc();
85	perl_construct(PERL_INTERPRETER);
86#ifdef PERL_EXIT_DESTRUCT_END
87	PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
88#endif
89	perl_parse(PERL_INTERPRETER, perl_back_xs_init, argc, embedding, (char **)NULL);
90	perl_run(PERL_INTERPRETER);
91	return perl_back_init_cf( bi );
92}
93
94int
95perl_back_db_init(
96	BackendDB	*be,
97	ConfigReply	*cr
98)
99{
100	be->be_private = (PerlBackend *) ch_malloc( sizeof(PerlBackend) );
101	memset( be->be_private, '\0', sizeof(PerlBackend));
102
103	((PerlBackend *)be->be_private)->pb_filter_search_results = 0;
104
105	Debug( LDAP_DEBUG_TRACE, "perl backend db init\n", 0, 0, 0 );
106
107	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
108
109	return 0;
110}
111
112int
113perl_back_db_open(
114	BackendDB	*be,
115	ConfigReply	*cr
116)
117{
118	int count;
119	int return_code;
120
121	PerlBackend *perl_back = (PerlBackend *) be->be_private;
122
123	ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );
124
125	{
126		dSP; ENTER; SAVETMPS;
127
128		PUSHMARK(sp);
129		XPUSHs( perl_back->pb_obj_ref );
130
131		PUTBACK;
132
133		count = call_method("init", G_SCALAR);
134
135		SPAGAIN;
136
137		if (count != 1) {
138			croak("Big trouble in perl_back_db_open\n");
139		}
140
141		return_code = POPi;
142
143		PUTBACK; FREETMPS; LEAVE;
144	}
145
146	ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
147
148	return return_code;
149}
150
151
152static void
153perl_back_xs_init(PERL_BACK_XS_INIT_PARAMS)
154{
155	char *file = __FILE__;
156	dXSUB_SYS;
157	newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
158}
159
160#if SLAPD_PERL == SLAPD_MOD_DYNAMIC
161
162/* conditionally define the init_module() function */
163SLAP_BACKEND_INIT_MODULE( perl )
164
165#endif /* SLAPD_PERL == SLAPD_MOD_DYNAMIC */
166
167
168