Menu

[r853]: / branches / Release-4-0-8 / php-java-bridge / secure_protocol.c  Maximize  Restore  History

Download this file

267 lines (209 with data), 8.2 kB

  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
/*-*- mode: C; tab-width:4 -*-*/
/* secure_protocol.c -- implementation of the PHP/Java Bridge XML
protocol, uses SSL to communicate with the J2EE server.
Copyright (C) 2003-2007 Jost Boekemeier
This file is part of the PHP/Java Bridge.
The PHP/Java Bridge ("the library") is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2, or (at your option) any later version.
The library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with the PHP/Java Bridge; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this file statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
#include "php_java.h"
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "protocol.h"
#include "java_bridge.h"
EXT_EXTERN_MODULE_GLOBALS(EXT)
/* Instead of using a separate protocol stack, this implementation
changes f_send, f_recv and f_close in the constructor and when
checkSession ("override redirect") is called. It reverts to the
original values when redirect arrives. */
struct secure_proxyenv_ {
struct proxyenv_ env;
zval *secure_peer; /* the SSL socket */
char *host; int port;
short (*f_close)(proxyenv *env);
ssize_t (*f_send)(proxyenv*env, const void *buf, size_t len);
ssize_t (*f_recv)(proxyenv*env, void *buf, size_t len);
void (*destruct)(proxyenv*env);
};
typedef struct secure_proxyenv_ *secure_proxyenv;
static ssize_t send_socket(proxyenv*_env, const void*buf, size_t length) {
static char name[] = "fwrite";
ssize_t count;
secure_proxyenv *env = (secure_proxyenv*)_env;
zval array, *string, *retval, *resource = (*env)->secure_peer;
array_init(&array);
INIT_PZVAL(&array);
MAKE_STD_ZVAL(string);
ZVAL_STRINGL(string, (char*)buf, length, 1);
zval_add_ref(&resource);
zend_hash_next_index_insert(Z_ARRVAL(array),&resource,sizeof(zval *), NULL);
zend_hash_next_index_insert(Z_ARRVAL(array),&string,sizeof(zval *), NULL);
EXT_GLOBAL(call_php_function) (name, sizeof(name)-1, &array, &retval);
count = Z_LVAL_P(retval);
zval_dtor(&array);
zval_ptr_dtor(&retval);
return count;
}
static ssize_t recv_socket(proxyenv*_env, void*buf, size_t length) {
static char name[] = "fread";
ssize_t count;
secure_proxyenv *env = (secure_proxyenv*)_env;
zval array, *number, *retval, *resource = (*env)->secure_peer;
array_init(&array);
INIT_PZVAL(&array);
MAKE_STD_ZVAL(number);
ZVAL_LONG(number, length);
zval_add_ref(&resource);
zend_hash_next_index_insert(Z_ARRVAL(array),&resource,sizeof(zval *), NULL);
zend_hash_next_index_insert(Z_ARRVAL(array),&number,sizeof(zval *), NULL);
EXT_GLOBAL(call_php_function) (name, sizeof(name)-1, &array, &retval);
memcpy(buf, Z_STRVAL_P(retval), count = Z_STRLEN_P(retval));
zval_dtor(&array);
zval_ptr_dtor(&number);
zval_ptr_dtor(&retval);
return count;
}
static short open_secure_socket(struct secure_proxyenv_*env) {
static char name[] = "fsockopen";
zval array, *string, *number;
zval **sock = &env->secure_peer;
array_init(&array);
INIT_PZVAL(&array);
MAKE_STD_ZVAL(string);
ZVAL_STRING(string, env->host, 1);
MAKE_STD_ZVAL(number);
ZVAL_LONG(number, env->port);
zend_hash_next_index_insert(Z_ARRVAL(array),&string,sizeof(zval *), NULL);
zend_hash_next_index_insert(Z_ARRVAL(array),&number,sizeof(zval *), NULL);
EXT_GLOBAL(call_php_function) (name, sizeof(name)-1, &array, sock);
if(Z_TYPE_PP(sock)!=IS_RESOURCE) return 0;
zval_dtor(&array);
zval_ptr_dtor(&string);
zval_ptr_dtor(&number);
return 1;
}
static char *init(struct secure_proxyenv_ *env) {
struct proxyenv_ *_env = &env->env;
//static char prefix[] = "";
static char prefix[] = "ssl://";
char *name, *hosts, *host, *port, *ssl;
size_t len;
TSRMLS_FETCH();
hosts = strdup(JG(hosts)); if(!hosts) exit(9);
host = strtok(hosts, "; "); /* TODO: check other hosts from the list */
name = strdup(host); if(!name) exit(9);
port = strchr(host, ':');
assert(port);
if(port) *port++=0; else port = "8443";
len = strlen(host);
ssl = emalloc(len+sizeof(prefix)); if(!ssl) exit(9);
strcpy(ssl, prefix);
strcat(ssl, host);
env->host = ssl;
env->port = atoi(port);
free(hosts);
return name;
}
static short close_socket(proxyenv *_env) {
static char name[] = "fclose";
secure_proxyenv *env = (secure_proxyenv*)_env;
zval array, *retval, *resource = (*env)->secure_peer;
array_init(&array);
INIT_PZVAL(&array);
zval_add_ref(&resource);
zend_hash_next_index_insert(Z_ARRVAL(array),&resource,sizeof(zval *), NULL);
EXT_GLOBAL(call_php_function) (name, sizeof(name)-1, &array, &retval);
zval_dtor(&array);
zval_ptr_dtor(&retval);
zval_ptr_dtor(&resource);
return 1;
}
static void check_session (proxyenv *_env) {
secure_proxyenv *env = (secure_proxyenv*)_env;
TSRMLS_FETCH();
if(!(*_env)->is_local && IS_SERVLET_BACKEND(_env) && !(*_env)->backend_has_session_proxy) {
if((*_env)->peer_redirected) { /* override redirect */
if (open_secure_socket(*env)) {
(*_env)->peer0 = (*_env)->peer;
(*_env)->peer = 0;
(*_env)->f_recv = recv_socket;
(*_env)->f_send = send_socket;
(*_env)->f_close = close_socket;
} else {
EXT_GLOBAL(sys_error)("Could not connect to server",48);
}
}
(*_env)->finish=(*_env)->endSession;
}
}
static void redirect(proxyenv*_env) {
secure_proxyenv*env = (secure_proxyenv*)_env;
(*_env)->f_close = (*env)->f_close;
(*_env)->f_recv = (*env)->f_recv;
(*_env)->f_send =(*env)->f_send;
}
static void destruct(proxyenv*_env) {
secure_proxyenv*env = (secure_proxyenv*)_env;
if((*env)->host) { free((*env)->host); (*env)->host = 0; }
(*_env)->destruct(_env);
}
proxyenv *EXT_GLOBAL(createSecureEnvironment) (short (*handle_request)(proxyenv *env), short (*handle_cached)(proxyenv *env), short *is_local) {
char *server;
zval *peer = 0;
secure_proxyenv *env;
struct proxyenv_ *_env;
env=(secure_proxyenv*)malloc(sizeof *env);
if(!env) return 0;
*env=(secure_proxyenv)calloc(1, sizeof **env);
if(!*env) {free(env); return 0;}
server = init(*env);
if(!open_secure_socket(*env)) {free(server); free(*env); free(env); return 0;}
*is_local = 0;
if(!EXT_GLOBAL(init_environment)(&((*env)->env), handle_request, handle_cached, *is_local)) {
free(*env);
free(env);
return 0;
}
_env = &((*env)->env);
(*env)->f_recv = _env->f_recv;
(*env)->f_send = _env->f_send;
(*env)->f_close = _env->f_close;
(*env)->destruct = _env->destruct;
_env->server_name = server;
_env->peer=0; // != -1 so that end_connection is called
_env->f_recv0 = _env->f_recv = recv_socket;
_env->f_send0 = _env->f_send = send_socket;
_env->f_close = close_socket;
_env->checkSession = check_session;
_env->redirect = redirect;
return (proxyenv*)env;
}
#ifndef PHP_WRAPPER_H
#error must include php_wrapper.h
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.