xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-perl/init.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: init.c,v 1.2 2020/08/11 13:15:41 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1999-2020 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 "../config.h"
22 
23 #ifdef PERL_SYS_INIT3
24 #include <ac/unistd.h>		/* maybe get environ */
25 extern char **environ;
26 #endif
27 
28 static void perl_back_xs_init LDAP_P((PERL_BACK_XS_INIT_PARAMS));
29 EXT void boot_DynaLoader LDAP_P((PERL_BACK_BOOT_DYNALOADER_PARAMS));
30 
31 PerlInterpreter *PERL_INTERPRETER = NULL;
32 ldap_pvt_thread_mutex_t	perl_interpreter_mutex;
33 
34 
35 /**********************************************************
36  *
37  * Init
38  *
39  **********************************************************/
40 
41 int
42 perl_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 fonction reference  (bi->bi_open) is not functional */
83 	Debug( LDAP_DEBUG_TRACE, "perl backend open\n", 0, 0, 0 );
84 
85 	if( PERL_INTERPRETER != NULL ) {
86 		Debug( LDAP_DEBUG_ANY, "perl backend open: already opened\n",
87 			0, 0, 0 );
88 		return 1;
89 	}
90 
91 	ldap_pvt_thread_mutex_init( &perl_interpreter_mutex );
92 
93 #ifdef PERL_SYS_INIT3
94 	PERL_SYS_INIT3(&argc, &argv, &env);
95 #endif
96 	PERL_INTERPRETER = perl_alloc();
97 	perl_construct(PERL_INTERPRETER);
98 #ifdef PERL_EXIT_DESTRUCT_END
99 	PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
100 #endif
101 	perl_parse(PERL_INTERPRETER, perl_back_xs_init, argc, argv, env);
102 	perl_run(PERL_INTERPRETER);
103 	return perl_back_init_cf( bi );
104 }
105 
106 int
107 perl_back_db_init(
108 	BackendDB	*be,
109 	ConfigReply	*cr
110 )
111 {
112 	be->be_private = (PerlBackend *) ch_malloc( sizeof(PerlBackend) );
113 	memset( be->be_private, '\0', sizeof(PerlBackend));
114 
115 	((PerlBackend *)be->be_private)->pb_filter_search_results = 0;
116 
117 	Debug( LDAP_DEBUG_TRACE, "perl backend db init\n", 0, 0, 0 );
118 
119 	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
120 
121 	return 0;
122 }
123 
124 int
125 perl_back_db_open(
126 	BackendDB	*be,
127 	ConfigReply	*cr
128 )
129 {
130 	int count;
131 	int return_code;
132 
133 	PerlBackend *perl_back = (PerlBackend *) be->be_private;
134 
135 	ldap_pvt_thread_mutex_lock( &perl_interpreter_mutex );
136 
137 	{
138 		dSP; ENTER; SAVETMPS;
139 
140 		PUSHMARK(sp);
141 		XPUSHs( perl_back->pb_obj_ref );
142 
143 		PUTBACK;
144 
145 		count = call_method("init", G_SCALAR);
146 
147 		SPAGAIN;
148 
149 		if (count != 1) {
150 			croak("Big trouble in perl_back_db_open\n");
151 		}
152 
153 		return_code = POPi;
154 
155 		PUTBACK; FREETMPS; LEAVE;
156 	}
157 
158 	ldap_pvt_thread_mutex_unlock( &perl_interpreter_mutex );
159 
160 	return return_code;
161 }
162 
163 
164 static void
165 perl_back_xs_init(PERL_BACK_XS_INIT_PARAMS)
166 {
167 	char *file = __FILE__;
168 	dXSUB_SYS;
169 	newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
170 }
171 
172 #if SLAPD_PERL == SLAPD_MOD_DYNAMIC
173 
174 /* conditionally define the init_module() function */
175 SLAP_BACKEND_INIT_MODULE( perl )
176 
177 #endif /* SLAPD_PERL == SLAPD_MOD_DYNAMIC */
178 
179 
180