PHP socket SSL connect - java

I do have the following code to connect to a JAVA Websocket Server:
$service_port = 10;
$address = 'localhost';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP );
$result = socket_connect($socket, $address, $service_port);
$xml = '<?xml version="1.0" encoding="UTF-8"?><JAVADEMO><Question="Login-Anfrage"/></JAVADEMO>';
$msg_length = strlen( $xml );
$withLength = pack( "Na{$msg_length}", $msg_length, $xml );
$lngLen = strlen( $withLength );
$send_bytes = socket_write( $socket, $withLength, $lngLen );
while ($out = socket_read($socket, 2048)) {
echo $out."<br />";
}
socket_close($socket);
This works perfect.
Now i have to connect to an SSL server. I have tried to change 'localhost' to 'ssl://localhost' or 'ssl://127.0.0.1' or 'ssl://192.168.0.1', .....
Nothings works. Anyone any idea how to connect to an SSL secured server?
EDIT:
I was missing something: On the server side, there is no Apache running! It's a Java SSL server.

Related

Connection to oracle server for login.php

I am trying to connect to oracle server with is in the uni. I think it does connect however i cant select from table to check if the login credentials are same in order to login. I have tried other ways as well but this is the closest i got so far. Problem is in the oci_bin part thats where it is showing error but i dunno any other way to solve this.
<?php
session_start();
if(!isset($_POST['username']) || !isset($_POST['password'])) {
header("Location: ../session.php");
}
putenv("ORACLE_SID=teaching");
if ($Connection = oci_connect("w4e09", "melih312")) {
print "Connection OK \n";}
if(isset($_SESSION['loggedin'])) header("Location: ../secret.php");
$Statement = oci_parse($Connection, 'select *
from Company
where address = :un_bv
and email = :pw_bv' );
Oci_bind_by_name($s, ":un_bv", $_POST['username']);
Oci_bind_by_name($s, ":pw_bv", $_POST['password']);
oci_execute($s);
$r = oci_fetch_array($s, OCI_ASSOC);
}
if ($r) {
$_SESSION['loggedin']=TRUE; $_SESSION['username']="admin";
}
else {
// No rows matched so login failed
login_form('Login failed. Valid usernames/passwords ' .
'are "chris/tiger" and "alison/red"');
}
header("Location: secret.php");
?>
oci_bind_by_name, oci_execute and oci_fetch_array have to use the resource returned by oci_parse. In your case, that would be the $Statement variable:
$Statement = oci_parse(
$Connection,
'select * from Company where address = :un_bv and email = :pw_bv'
);
oci_bind_by_name($Statement, ":un_bv", $_POST['username']);
oci_bind_by_name($Statement, ":pw_bv", $_POST['password']);
oci_execute($Statement);
$r = oci_fetch_array($Statement, OCI_ASSOC);
Take a look at the documentation:
http://php.net/manual/en/function.oci-connect.php
http://php.net/manual/en/function.oci-parse.php
Add some error checking:
// During development only
error_reporting(E_ALL); // In PHP 5.3 use E_ALL|E_STRICT
ini_set('display_errors', 'On');
. . .
if ($Connection = oci_connect("w4e09", "melih312")) {
print "Connection OK \n";}
else {
$m = oci_error();
trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}
Similarly check errors after oci_execute().
See "Handling PHP OCI8 Errors" on p 161 of Oracle's free book http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html

JDBC connection between Docker containers (docker-compose)

I try to connect a web application which runs on a tomcat 8 to an oracle database. Both of them run as Docker containers:
docker-compose.yml:
version: "3"
services:
appweb:
build: ./app
image: "servlet-search-app:0.1"
ports:
- "8888:8080"
links:
- appdb
environment:
- DATA_SOURCE_NAME="jdbc:oracle:thin:#appdb:1521/XE"
appdb:
build: ./db
image: "servlet-search-db:0.1"
ports:
- "49160:22"
- "1521:1521"
- "8889:8080"
Dockerfile of my oracle DB image (build: ./db):
FROM wnameless/oracle-xe-11g
ADD createUser.sql /docker-entrypoint-initdb.d/
ENV ORACLE_ALLOW_REMOTE=true
Dockerfile of the tomcat image (build: ./app)
FROM tomcat:8.0.20-jre8
COPY servlet.war /usr/local/tomcat/webapps/
COPY ojdbc14-1.0.jar /usr/local/tomcat/lib/
So the app starts up as expected but throws an exception when trying to connect to the database:
java.lang.IllegalStateException: java.sql.SQLException: Io exception: Invalid connection string format, a valid format is: "host:port:sid"
org.se.lab.ui.ControllerServlet.createConnection(ControllerServlet.java:115)
org.se.lab.ui.ControllerServlet.handleSearch(ControllerServlet.java:78)
org.se.lab.ui.ControllerServlet.doPost(ControllerServlet.java:53)
org.se.lab.ui.ControllerServlet.doGet(ControllerServlet.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Now the issue seems obvious, however when I fix the DATA_SOURCE_NAME string to:
DATA_SOURCE_NAME="jdbc:oracle:thin:#appdb:1521:XE"
I get the following exception:
java.lang.IllegalStateException: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
appdb:1521:XE"
org.se.lab.ui.ControllerServlet.createConnection(ControllerServlet.java:115)
org.se.lab.ui.ControllerServlet.handleSearch(ControllerServlet.java:78)
org.se.lab.ui.ControllerServlet.doPost(ControllerServlet.java:53)
org.se.lab.ui.ControllerServlet.doGet(ControllerServlet.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Now I tried to find out which one of them should actually work. Thus, I started only the DB container:
docker build -t dbtest .
docker run -it -d --rm -p 1521:1521 --name dbtest dbtest
docker inspect dbtest | grep IPAddress
>> "IPAddress": "172.17.0.4"
Next, I try to connect with sqlplus:
sqlplus system/oracle#172.17.0.4:1521/XE # works
sqlplus system/oracle#172.17.0.4:1521:XE #ERROR: ORA-12545: Connect failed because target host or object does not exist
So what's the problem? Due to the link in the docker-compose file, the tomcat container can resolve "appdb" to the container's IP.
Here's the code which should establish the connection:
protected Connection createConnection() {
String datasource = System.getenv("DATA_SOURCE_NAME");
try {
// debug
InetAddress address = null;
try {
address = InetAddress.getByName("appdb");
System.out.println(address); // resolves in appdb/10.0.0.2
System.out.println(address.getHostAddress()); // resolves in 10.0.0.2
} catch (UnknownHostException e) {
e.printStackTrace();
}
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection(datasource, "system", "oracle");
} catch (SQLException | ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
Lastly here's the tnsnames.ora file:
cat $ORACLE_HOME/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File:
XE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = fcffb044d69d)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = XE)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
Thanks!
The oracle default listener resolved the configured host to the wrong IP address:
vim $ORACLE_HOME/network/admin/listener.ora:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/xe)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))
(ADDRESS = (PROTOCOL = TCP)(HOST = f4c4a3638c11)(PORT = 1521))
)
)
DEFAULT_SERVICE_LISTENER = (XE)
The HOST value is the Docker container id. If we look at /etc/hosts it is set up correctly for the service link in the docker-compose link:
10.0.0.5 f4c4a3638c11
It gets also resolved correctly from the tomcat container
ping f4c4a3638c11
PING f4c4a3638c11 (10.0.0.5): 56 data bytes
...
If I try to connect with an IP address of the other interface, which is the docker interface from the host system, the connection from the web application to the database works
String datasource = "jdbc:oracle:thin:#172.17.0.4:1521:XE";
So the solution is to configure the listener to listen to the correct IP address
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.5)(PORT = 1521))
Now this connection string works:
jdbc:oracle:thin:#appdb:1521:XE
I will report this behavior to wnameless/oracle-xe-11g as a bug
Sorry, this is not a definitive answer. Let's treat it as long comment :)
Your setup is quite complex for me to recreate however your error message is intriguing:
The Connection descriptor used by the client was:
appdb:1521:XE"
...
It looks like the environment value was chopped to appdb:1521:XE. How about if you try hard-coding:
String datasource = "jdbc:oracle:thin:#appdb:1521/XE";
If that works, then probably need to somehow escape your docker DATA_SOURCE_NAME environment variable.
I could be completely wrong but I think it is worth a try.

"names.default_domain = world" not being set when looking up tnsnames.ora

I have a requirement for my java application to connect to an Oracle database (10 and 11g) using the Oracle wallet for the DB credentials.
My issue is when connecting trying to connect to the database i get:
java.sql.SQLException: could not resolve the connect identifier "mydb".
sqlnet.ora looks like the following:
AUTOMATIC_IPC = OFF
TRACE_LEVEL_CLIENT = OFF
names.directory_path = (TNSNAMES)
names.default_domain = world
names.default_zone = world
WALLET_LOCATION =
(SOURCE =
(METHOD = FILE)
(METHOD_DATA =
(DIRECTORY = <wallet location C:....>)
)
)
SQLNET.WALLET_OVERRIDE = TRUE
SSL_CLIENT_AUTHENTICATION = FALSESSL_VERSION = 0
And my tnsnames.ora entry looks like:
mydb.WORLD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (Host = <HOST>)
(Port = <PORT>))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = <DB_SERVICES>)
(INSTANCE_NAME = mydb)
)
)
and my wallet entry is setup as follows:
mkstore -wrl C:\oracle\wallet -createCredential mydb username "password"
Java application connection string as follows:
jdbc:oracle:thin:/#mydb
Now the connection works if the .world is removed from the entry in tnsnames.ora, but from my understanding that is what the names.default_domain = world should pass to the tnsnames.ora lookup, is that correct? if so then why isn't this being set?
I can connect to sqlplus in dos fine by using sqlplus /#mydb
Thanks

why am i getting java.net.MalformedURLException: no protocol?

the website that im hosting off of is 000webhost.com
this is in the console that is the error,
java.net.MalformedURLException: no protocol: OxidePKz.net63.net/checkvote.php? username=oxide
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
and this is my check vote page code
<?php
$con = mysql_connect("mysql1.000webhost.com", "a5999082_oxidepk", "(put in right passcode)");
if (!$con) {
die("Could not connect to database: " . mysql_error());
}
mysql_select_db("DATABASE_NAME", $con);
$username = mysql_escape_string($_GET['username']);
if (isset($_GET['username'])) {
$result = mysql_query("SELECT * FROM `votes` where username = '$username'") or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['username'] == $username) {
mysql_query("DELETE FROM `votes` where username = '$username'");
echo "true";
} else {
echo "false";
}
}
i made a my admin php thing with the table name votes
and this is the call back page
$rspscoding = gethostbyname("http://www.oxidepkz.net63.net");
if($_SERVER['REMOTE_ADDR'] == $rspscoding) {
$con = mysql_connect("mysql1.000webhost.com", "a5999082_oxidepk", "(put in right passcode)");
if (!$con) {
die("Could not connect to database: " . mysql_error());
}
mysql_select_db("DATABASE_NAME", $con);
$username = mysql_escape_string($_GET['username']);
if (isset($_GET['username'])) {
mysql_query("INSERT INTO `votes` (username) VALUES ('$username')") or die(mysql_error());
}
mysql_close($con);
}
A few things;
I'm wonderinhg why you are getting back a java exception from a php application
Next gethostbyname("http://www.oxidepkz.net63.vote.html") does NOT take a URL, it takes a hostname like "www.oxidepkz.net63.com" for example according to: http://php.net/manual/en/function.gethostbyname.php
Finally, your URL is malformed since there is no ending slash after the domain name.
"http://www.oxidepkz.net63.vote.html" will not work in any browser
Did it get truncated, perhaps?

PHP - fsockopen() Connection timed out [duplicate]

I am working on a php file that connects to my game server and executes a command. The users enter their username on a HTML form that sends them and the username to a php file that connects to the server. The port is forwarded and the server is ready to receive the info, but I keep getting this error:
Warning: socket_connect() [function.socket-connect]: unable to connect [110]: Connection timed out in /home/moocraft/public_html/test/vote.php on line 8
error: could not connect to host
Here is the HTML file:
<html>
<body>
<form action="vote.php" method="post">
Minecraft Username: <input type="text" name="fname" />
<input type="submit" />
</form>
</body>
</html>
Here is the PHP file:
<?php
$HOST = "207.210.254.141"; //the ip of the bukkit server
$password = "examplepassword1";
$player = $_POST["fname"];
//Can't touch this:
$sock = socket_create(AF_INET, SOCK_STREAM, 0)
or die("error: could not create socket\n");
$succ = socket_connect($sock, $HOST, 4445)
or die("error: could not connect to host\n");
//Authentification
socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1)
or die("error: failed to write to socket\n");
//Begin custom code here.
socket_write($sock, $command = "/Command/ExecuteConsoleCommand:give $player 264 5;", strlen($command) + 1) //Writing text/command we want to send to the server
or die("error: failed to write to socket\n");
socket_write($sock, $command = "$player voted for MOOcraft and earned 5 diamonds. Vote at moocraft.org;", strlen($command) + 1)
or die("error: failed to write to socket\n");
?>
I can't figure out why I keep getting this. Any help is greatly appreciated.
Try something simple like:
$fp = fsockopen("207.210.254.141", 4445, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: 207.210.254.141\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
And see what do you get, it might be due to the reason that your server is taking too long to respond

Categories