Commit 891a9ca2 authored by Jondy Zhao's avatar Jondy Zhao

use DuplicateHandle to fix writing pipe problem

parent c52ee536
#include <windows.h>
#include <stdio.h> #include <stdio.h>
HANDLE open_pipe(const char *pname);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int pd; HANDLE pd;
DWORD m;
char buf[512]; char buf[512];
int n; int n;
char *s, *s1, *s2; char *s, *s1, *s2;
...@@ -21,12 +25,6 @@ int main(int argc, char *argv[]) ...@@ -21,12 +25,6 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
pd = open(argv[1], 1);
if (pd == -1) {
fprintf(stderr, "invalid pipe %s\n", argv[1]);
return 1;
}
/* %(script_type)s %(common_name)s %(OPENVPN_external_ip)s */ /* %(script_type)s %(common_name)s %(OPENVPN_external_ip)s */
s1 = (char*)getenv("common_name"); s1 = (char*)getenv("common_name");
s2 = (char*)getenv("OPENVPN_external_ip"); s2 = (char*)getenv("OPENVPN_external_ip");
...@@ -42,10 +40,18 @@ int main(int argc, char *argv[]) ...@@ -42,10 +40,18 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (write(pd, buf, n) == -1) { pd = open_pipe(argv[1]);
if (pd == NULL) {
fprintf(stderr, "invalid pipe %s\n", argv[1]);
return 1;
}
if (!WriteFile(pd, buf, n, &m, NULL)) {
fprintf(stderr, "write pipe failed\n"); fprintf(stderr, "write pipe failed\n");
CloseHandle(pd);
return 1; return 1;
} }
CloseHandle(pd);
return 0; return 0;
} }
#include <windows.h>
extern sscanf (const char *, const char *, ...);
HANDLE open_pipe (const char *pname)
{
HANDLE proc, pipe_hdl, nio_hdl = NULL;
int pid, rwflags = 1; /* O_WRONLY */
sscanf (pname, "/proc/%d/fd/pipe:[%d]", &pid, (int *) &pipe_hdl);
if (!(proc = OpenProcess (PROCESS_DUP_HANDLE, FALSE, pid))) {
return NULL;
}
if (!DuplicateHandle (proc, pipe_hdl, GetCurrentProcess (), &nio_hdl,
0, FALSE, DUPLICATE_SAME_ACCESS)) {
return NULL;
}
CloseHandle (proc);
return nio_hdl;
}
#include <windows.h>
#include <stdio.h> #include <stdio.h>
/*
if os.environ['script_type'] == 'client-connect':
# Send client its external ip address
with open(sys.argv[2], 'w') as f:
f.write('push "setenv-safe external_ip %s"\n'
% os.environ['trusted_ip'])
# Write into pipe connect/disconnect events HANDLE open_pipe(const char *pname);
arg1 = sys.argv[1]
if arg1 != 'None':
os.write(int(arg1), '%(script_type)s %(common_name)s %(trusted_ip)s\n'
% os.environ)
*/
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int pd; HANDLE pd;
DWORD m;
char buf[512]; char buf[512];
int n; int n;
char *s, *s1, *s2; char *s, *s1, *s2;
...@@ -66,12 +58,6 @@ int main(int argc, char *argv[]) ...@@ -66,12 +58,6 @@ int main(int argc, char *argv[])
if (strcmp(argv[1], "None") == 0) if (strcmp(argv[1], "None") == 0)
return 0; return 0;
pd = open(argv[1], 1);
if (pd == -1) {
fprintf(stderr, "invalid pipe %s\n", argv[1]);
return 1;
}
/* %(script_type)s %(common_name)s %(OPENVPN_external_ip)s */ /* %(script_type)s %(common_name)s %(OPENVPN_external_ip)s */
s1 = (char*)getenv("common_name"); s1 = (char*)getenv("common_name");
if (s1 == NULL) { if (s1 == NULL) {
...@@ -85,10 +71,18 @@ int main(int argc, char *argv[]) ...@@ -85,10 +71,18 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (write(pd, buf, n) == -1) { pd = open_pipe(argv[1]);
if (pd == NULL) {
fprintf(stderr, "invalid pipe %s\n", argv[1]);
return 1;
}
if (!WriteFile(pd, buf, n, &m, NULL)) {
fprintf(stderr, "write pipe failed\n"); fprintf(stderr, "write pipe failed\n");
CloseHandle(pd);
return 1; return 1;
} }
CloseHandle(pd);
return 0; return 0;
} }
...@@ -31,7 +31,7 @@ def openvpn(iface, encrypt, *args, **kw): ...@@ -31,7 +31,7 @@ def openvpn(iface, encrypt, *args, **kw):
def server(iface, max_clients, dh_path, pipe_fd, port, proto, encrypt, *args, **kw): def server(iface, max_clients, dh_path, pipe_fd, port, proto, encrypt, *args, **kw):
client_script = '%s /proc/%u/fd/%s' % (ovpn_server, os.getpid(), pipe_fd) client_script = '%s %s' % (ovpn_server, utils.get_pipename(pipe_fd))
if pipe_fd is not None: if pipe_fd is not None:
args = ('--client-disconnect', client_script) + args args = ('--client-disconnect', client_script) + args
return openvpn(iface, encrypt, return openvpn(iface, encrypt,
......
...@@ -65,7 +65,7 @@ class Connection(object): ...@@ -65,7 +65,7 @@ class Connection(object):
'--resolv-retry', '0', '--resolv-retry', '0',
'--connect-retry-max', '3', '--tls-exit', '--connect-retry-max', '3', '--tls-exit',
'--ping-exit', str(timeout), '--ping-exit', str(timeout),
'--route-up', '%s /proc/%u/fd/%u' % (plib.ovpn_client, os.getpid(), write_pipe), '--route-up', '%s %s' % (plib.ovpn_client, utils.get_pipename(write_pipe)),
*ovpn_args) *ovpn_args)
_retry += 1 _retry += 1
self._retry = _retry < len(self.address_list) and ( self._retry = _retry < len(self.address_list) and (
......
...@@ -178,3 +178,12 @@ def encrypt(cert, data): ...@@ -178,3 +178,12 @@ def encrypt(cert, data):
if p.returncode: if p.returncode:
raise subprocess.CalledProcessError(p.returncode, 'openssl', err) raise subprocess.CalledProcessError(p.returncode, 'openssl', err)
return out return out
def get_pipename(pipe_id):
if pipe_id is not None:
path = '/proc/%u' % os.getpid()
with open(os.path.join(path, 'winpid'), 'r') as f:
winpid = f.readline()
r = os.path.realpath('%s/fd/%s' % (path, pipe_id)).split('/')
r[1] = winpid
return '/'.join(r)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment