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