2004-08-03 11:37:55 +08:00
|
|
|
/*
|
|
|
|
This program 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 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2004-09-27 11:26:07 +08:00
|
|
|
xrdp: A Remote Desktop Protocol server.
|
2010-05-04 17:24:24 +08:00
|
|
|
Copyright (C) Jay Sorg 2004-2010
|
2004-08-03 11:37:55 +08:00
|
|
|
|
|
|
|
listen for incoming connection
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "xrdp.h"
|
|
|
|
|
2007-02-04 15:53:15 +08:00
|
|
|
/* 'g_process' is protected by the semaphore 'g_process_sem'. One thread sets
|
|
|
|
g_process and waits for the other to process it */
|
2008-03-08 18:42:35 +08:00
|
|
|
static tbus g_process_sem = 0;
|
2004-08-03 11:37:55 +08:00
|
|
|
static struct xrdp_process* g_process = 0;
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2012-05-27 04:58:31 +08:00
|
|
|
static int
|
|
|
|
xrdp_listen_create_pro_done(struct xrdp_listen* self)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2008-12-01 17:37:08 +08:00
|
|
|
int pid;
|
|
|
|
char text[256];
|
2004-08-03 11:37:55 +08:00
|
|
|
|
2008-12-01 17:37:08 +08:00
|
|
|
pid = g_getpid();
|
|
|
|
g_snprintf(text, 255, "xrdp_%8.8x_listen_pro_done_event", pid);
|
|
|
|
self->pro_done_event = g_create_wait_obj(text);
|
2012-05-27 04:58:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
struct xrdp_listen* APP_CC
|
|
|
|
xrdp_listen_create(void)
|
|
|
|
{
|
|
|
|
struct xrdp_listen* self;
|
|
|
|
|
|
|
|
self = (struct xrdp_listen*)g_malloc(sizeof(struct xrdp_listen), 1);
|
|
|
|
xrdp_listen_create_pro_done(self);
|
2008-03-31 19:28:18 +08:00
|
|
|
self->process_list = list_create();
|
2008-04-05 16:25:49 +08:00
|
|
|
if (g_process_sem == 0)
|
|
|
|
{
|
|
|
|
g_process_sem = tc_sem_create(0);
|
|
|
|
}
|
2010-05-04 17:24:24 +08:00
|
|
|
self->listen_trans = trans_create(TRANS_MODE_TCP, 16, 16);
|
|
|
|
if (self->listen_trans == 0)
|
|
|
|
{
|
2012-02-03 10:49:06 +08:00
|
|
|
g_writeln("xrdp_listen_create: trans_create failed");
|
2010-05-04 17:24:24 +08:00
|
|
|
}
|
2004-08-03 11:37:55 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-06-28 11:00:43 +08:00
|
|
|
void APP_CC
|
|
|
|
xrdp_listen_delete(struct xrdp_listen* self)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2010-05-04 17:24:24 +08:00
|
|
|
if (self->listen_trans != 0)
|
|
|
|
{
|
|
|
|
trans_delete(self->listen_trans);
|
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
if (g_process_sem != 0)
|
2005-01-30 12:34:19 +08:00
|
|
|
{
|
2008-04-05 16:25:49 +08:00
|
|
|
tc_sem_delete(g_process_sem);
|
|
|
|
g_process_sem = 0;
|
2005-01-30 12:34:19 +08:00
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
g_delete_wait_obj(self->pro_done_event);
|
|
|
|
list_delete(self->process_list);
|
|
|
|
g_free(self);
|
2004-08-03 11:37:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* returns error */
|
2006-10-24 12:05:38 +08:00
|
|
|
static int APP_CC
|
2008-03-08 18:42:35 +08:00
|
|
|
xrdp_listen_add_pro(struct xrdp_listen* self, struct xrdp_process* process)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2008-03-31 19:28:18 +08:00
|
|
|
list_add_item(self->process_list, (tbus)process);
|
|
|
|
return 0;
|
2004-08-03 11:37:55 +08:00
|
|
|
}
|
|
|
|
|
2004-09-12 05:27:39 +08:00
|
|
|
/*****************************************************************************/
|
2008-03-31 19:28:18 +08:00
|
|
|
static int APP_CC
|
|
|
|
xrdp_listen_delete_done_pro(struct xrdp_listen* self)
|
2004-09-12 05:27:39 +08:00
|
|
|
{
|
|
|
|
int i;
|
2008-03-31 19:28:18 +08:00
|
|
|
struct xrdp_process* pro;
|
2004-09-12 05:27:39 +08:00
|
|
|
|
2008-03-31 19:28:18 +08:00
|
|
|
for (i = self->process_list->count - 1; i >= 0; i--)
|
2004-09-12 05:27:39 +08:00
|
|
|
{
|
2008-03-31 19:28:18 +08:00
|
|
|
pro = (struct xrdp_process*)list_get_item(self->process_list, i);
|
|
|
|
if (pro != 0)
|
2004-09-12 05:27:39 +08:00
|
|
|
{
|
2008-03-31 19:28:18 +08:00
|
|
|
if (pro->status < 0)
|
|
|
|
{
|
|
|
|
xrdp_process_delete(pro);
|
|
|
|
list_remove_item(self->process_list, i);
|
|
|
|
}
|
2004-09-12 05:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-03 11:37:55 +08:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* i can't get stupid in_val to work, hum using global var for now */
|
2005-06-28 11:00:43 +08:00
|
|
|
THREAD_RV THREAD_CC
|
|
|
|
xrdp_process_run(void* in_val)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2007-02-04 15:53:15 +08:00
|
|
|
struct xrdp_process* process;
|
|
|
|
|
2006-04-29 09:47:06 +08:00
|
|
|
DEBUG(("process started"));
|
2007-02-04 15:53:15 +08:00
|
|
|
process = g_process;
|
|
|
|
g_process = 0;
|
|
|
|
tc_sem_inc(g_process_sem);
|
|
|
|
xrdp_process_main_loop(process);
|
2006-04-29 09:47:06 +08:00
|
|
|
DEBUG(("process done"));
|
2004-08-03 11:37:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2008-03-08 18:42:35 +08:00
|
|
|
static int
|
2010-10-20 12:23:13 +08:00
|
|
|
xrdp_listen_get_port_address(char* port, int port_bytes,
|
2012-02-03 10:49:06 +08:00
|
|
|
char* address, int address_bytes,
|
|
|
|
struct xrdp_startup_params* startup_param)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2005-11-30 09:27:08 +08:00
|
|
|
int fd;
|
2008-03-08 18:42:35 +08:00
|
|
|
int error;
|
2005-11-30 09:27:08 +08:00
|
|
|
int index;
|
|
|
|
char* val;
|
|
|
|
struct list* names;
|
|
|
|
struct list* values;
|
2009-05-19 12:23:49 +08:00
|
|
|
char cfg_file[256];
|
2005-11-30 09:27:08 +08:00
|
|
|
|
|
|
|
/* default to port 3389 */
|
2008-03-08 18:42:35 +08:00
|
|
|
g_strncpy(port, "3389", port_bytes - 1);
|
2010-10-20 12:23:13 +08:00
|
|
|
/* Default to all */
|
|
|
|
g_strncpy(address, "0.0.0.0", address_bytes - 1);
|
|
|
|
/* see if port or address is in xrdp.ini file */
|
2009-05-19 12:23:49 +08:00
|
|
|
g_snprintf(cfg_file, 255, "%s/xrdp.ini", XRDP_CFG_PATH);
|
|
|
|
fd = g_file_open(cfg_file);
|
2005-11-30 09:27:08 +08:00
|
|
|
if (fd > 0)
|
|
|
|
{
|
|
|
|
names = list_create();
|
|
|
|
names->auto_free = 1;
|
|
|
|
values = list_create();
|
|
|
|
values->auto_free = 1;
|
|
|
|
if (file_read_section(fd, "globals", names, values) == 0)
|
|
|
|
{
|
|
|
|
for (index = 0; index < names->count; index++)
|
|
|
|
{
|
|
|
|
val = (char*)list_get_item(names, index);
|
|
|
|
if (val != 0)
|
|
|
|
{
|
2008-03-08 18:42:35 +08:00
|
|
|
if (g_strcasecmp(val, "port") == 0)
|
2005-11-30 09:27:08 +08:00
|
|
|
{
|
|
|
|
val = (char*)list_get_item(values, index);
|
|
|
|
error = g_atoi(val);
|
2008-03-08 18:42:35 +08:00
|
|
|
if ((error > 0) && (error < 65000))
|
2005-11-30 09:27:08 +08:00
|
|
|
{
|
2008-03-08 18:42:35 +08:00
|
|
|
g_strncpy(port, val, port_bytes - 1);
|
2005-11-30 09:27:08 +08:00
|
|
|
}
|
2010-10-20 12:23:13 +08:00
|
|
|
}
|
|
|
|
if (g_strcasecmp(val, "address") == 0)
|
|
|
|
{
|
|
|
|
val = (char*)list_get_item(values, index);
|
|
|
|
g_strncpy(address, val, address_bytes - 1);
|
2005-11-30 09:27:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_delete(names);
|
|
|
|
list_delete(values);
|
|
|
|
g_file_close(fd);
|
|
|
|
}
|
2012-02-03 10:49:06 +08:00
|
|
|
/* startup_param overrides */
|
|
|
|
if (startup_param->port[0] != 0)
|
|
|
|
{
|
|
|
|
g_strncpy(port, startup_param->port, port_bytes - 1);
|
|
|
|
}
|
2008-03-08 18:42:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-27 04:23:02 +08:00
|
|
|
/*****************************************************************************/
|
|
|
|
static int APP_CC
|
|
|
|
xrdp_listen_fork(struct xrdp_listen* self, struct trans* server_trans)
|
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
struct xrdp_process* process;
|
|
|
|
|
|
|
|
pid = g_fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
/* child */
|
|
|
|
/* recreate some main globals */
|
|
|
|
xrdp_child_fork();
|
|
|
|
/* recreate the process done wait object, not used in fork mode */
|
2012-05-27 04:58:31 +08:00
|
|
|
/* close, don't delete this */
|
2012-05-27 08:24:04 +08:00
|
|
|
g_close_wait_obj(self->pro_done_event);
|
2012-05-27 04:58:31 +08:00
|
|
|
xrdp_listen_create_pro_done(self);
|
2012-05-27 04:23:02 +08:00
|
|
|
/* delete listener, child need not listen */
|
|
|
|
trans_delete(self->listen_trans);
|
|
|
|
self->listen_trans = 0;
|
|
|
|
/* new connect instance */
|
|
|
|
process = xrdp_process_create(self, 0);
|
|
|
|
process->server_trans = server_trans;
|
|
|
|
g_process = process;
|
|
|
|
xrdp_process_run(0);
|
|
|
|
xrdp_process_delete(process);
|
|
|
|
/* mark this process to exit */
|
|
|
|
g_set_term(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* parent */
|
|
|
|
trans_delete(server_trans);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-04 17:24:24 +08:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* a new connection is coming in */
|
|
|
|
int DEFAULT_CC
|
|
|
|
xrdp_listen_conn_in(struct trans* self, struct trans* new_self)
|
|
|
|
{
|
|
|
|
struct xrdp_process* process;
|
|
|
|
struct xrdp_listen* lis;
|
|
|
|
|
|
|
|
lis = (struct xrdp_listen*)(self->callback_data);
|
2012-05-27 04:23:02 +08:00
|
|
|
if (lis->startup_params->fork)
|
|
|
|
{
|
|
|
|
return xrdp_listen_fork(lis, new_self);
|
|
|
|
}
|
2010-05-04 17:24:24 +08:00
|
|
|
process = xrdp_process_create(lis, lis->pro_done_event);
|
|
|
|
if (xrdp_listen_add_pro(lis, process) == 0)
|
|
|
|
{
|
|
|
|
/* start thread */
|
|
|
|
process->server_trans = new_self;
|
|
|
|
g_process = process;
|
|
|
|
tc_thread_create(xrdp_process_run, 0);
|
|
|
|
tc_sem_dec(g_process_sem); /* this will wait */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xrdp_process_delete(process);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-08 18:42:35 +08:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* wait for incoming connections */
|
|
|
|
int APP_CC
|
2012-05-27 04:23:02 +08:00
|
|
|
xrdp_listen_main_loop(struct xrdp_listen* self)
|
2008-03-08 18:42:35 +08:00
|
|
|
{
|
|
|
|
int error;
|
2008-03-31 19:28:18 +08:00
|
|
|
int robjs_count;
|
|
|
|
int cont;
|
2010-11-03 23:59:26 +08:00
|
|
|
int timeout = 0;
|
2012-02-03 10:49:06 +08:00
|
|
|
char port[128];
|
2010-10-20 12:23:13 +08:00
|
|
|
char address[256];
|
2008-04-05 16:25:49 +08:00
|
|
|
tbus robjs[8];
|
|
|
|
tbus term_obj;
|
|
|
|
tbus sync_obj;
|
|
|
|
tbus sck_obj;
|
|
|
|
tbus done_obj;
|
2008-03-08 18:42:35 +08:00
|
|
|
|
|
|
|
self->status = 1;
|
2010-10-20 12:23:13 +08:00
|
|
|
if (xrdp_listen_get_port_address(port, sizeof(port),
|
2012-02-03 10:49:06 +08:00
|
|
|
address, sizeof(address),
|
2012-05-27 04:23:02 +08:00
|
|
|
self->startup_params) != 0)
|
2005-02-04 11:35:12 +08:00
|
|
|
{
|
2010-05-04 17:24:24 +08:00
|
|
|
g_writeln("xrdp_listen_main_loop: xrdp_listen_get_port failed");
|
2005-03-12 23:59:54 +08:00
|
|
|
self->status = -1;
|
2005-02-04 11:35:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-23 10:45:18 +08:00
|
|
|
error = trans_listen_address(self->listen_trans, port, address);
|
2004-08-03 11:37:55 +08:00
|
|
|
if (error == 0)
|
|
|
|
{
|
2010-05-04 17:24:24 +08:00
|
|
|
self->listen_trans->trans_conn_in = xrdp_listen_conn_in;
|
|
|
|
self->listen_trans->callback_data = self;
|
2008-04-05 16:25:49 +08:00
|
|
|
term_obj = g_get_term_event();
|
|
|
|
sync_obj = g_get_sync_event();
|
|
|
|
done_obj = self->pro_done_event;
|
2008-03-31 19:28:18 +08:00
|
|
|
cont = 1;
|
|
|
|
while (cont)
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2008-04-05 16:25:49 +08:00
|
|
|
/* build the wait obj list */
|
|
|
|
robjs_count = 0;
|
|
|
|
robjs[robjs_count++] = term_obj;
|
|
|
|
robjs[robjs_count++] = sync_obj;
|
|
|
|
robjs[robjs_count++] = done_obj;
|
2010-05-04 17:24:24 +08:00
|
|
|
timeout = -1;
|
|
|
|
if (trans_get_wait_objs(self->listen_trans, robjs, &robjs_count,
|
|
|
|
&timeout) != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
/* wait */
|
2010-05-04 17:24:24 +08:00
|
|
|
if (g_obj_wait(robjs, robjs_count, 0, 0, timeout) != 0)
|
2005-01-30 12:34:19 +08:00
|
|
|
{
|
2008-04-05 16:25:49 +08:00
|
|
|
/* error, should not get here */
|
2004-08-03 11:37:55 +08:00
|
|
|
g_sleep(100);
|
2005-01-30 12:34:19 +08:00
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
if (g_is_wait_obj_set(term_obj)) /* term */
|
2005-01-30 12:34:19 +08:00
|
|
|
{
|
2004-08-03 11:37:55 +08:00
|
|
|
break;
|
2005-01-30 12:34:19 +08:00
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
if (g_is_wait_obj_set(sync_obj)) /* sync */
|
2004-08-03 11:37:55 +08:00
|
|
|
{
|
2008-04-05 16:25:49 +08:00
|
|
|
g_reset_wait_obj(sync_obj);
|
2008-03-31 19:28:18 +08:00
|
|
|
g_loop();
|
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
if (g_is_wait_obj_set(done_obj)) /* pro_done_event */
|
|
|
|
{
|
|
|
|
g_reset_wait_obj(done_obj);
|
|
|
|
xrdp_listen_delete_done_pro(self);
|
|
|
|
}
|
2010-05-04 17:24:24 +08:00
|
|
|
if (trans_check_wait_objs(self->listen_trans) != 0)
|
|
|
|
{
|
2012-05-27 04:23:02 +08:00
|
|
|
break;
|
2010-05-04 17:24:24 +08:00
|
|
|
}
|
2008-04-05 16:25:49 +08:00
|
|
|
}
|
|
|
|
/* stop listening */
|
2010-05-04 17:24:24 +08:00
|
|
|
trans_delete(self->listen_trans);
|
|
|
|
self->listen_trans = 0;
|
2008-04-05 16:25:49 +08:00
|
|
|
/* second loop to wait for all process threads to close */
|
|
|
|
cont = 1;
|
|
|
|
while (cont)
|
|
|
|
{
|
|
|
|
if (self->process_list->count == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* build the wait obj list */
|
|
|
|
robjs_count = 0;
|
|
|
|
robjs[robjs_count++] = sync_obj;
|
|
|
|
robjs[robjs_count++] = done_obj;
|
|
|
|
/* wait */
|
|
|
|
if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0)
|
|
|
|
{
|
|
|
|
/* error, should not get here */
|
|
|
|
g_sleep(100);
|
|
|
|
}
|
|
|
|
if (g_is_wait_obj_set(sync_obj)) /* sync */
|
|
|
|
{
|
|
|
|
g_reset_wait_obj(sync_obj);
|
|
|
|
g_loop();
|
|
|
|
}
|
|
|
|
if (g_is_wait_obj_set(done_obj)) /* pro_done_event */
|
2008-03-31 19:28:18 +08:00
|
|
|
{
|
2008-04-05 16:25:49 +08:00
|
|
|
g_reset_wait_obj(done_obj);
|
2008-03-31 19:28:18 +08:00
|
|
|
xrdp_listen_delete_done_pro(self);
|
|
|
|
}
|
2004-08-03 11:37:55 +08:00
|
|
|
}
|
|
|
|
}
|
2004-09-12 05:27:39 +08:00
|
|
|
else
|
|
|
|
{
|
2011-07-07 10:54:10 +08:00
|
|
|
g_writeln("xrdp_listen_main_loop: listen error, possible port "
|
|
|
|
"already in use");
|
2004-09-12 05:27:39 +08:00
|
|
|
}
|
2004-08-03 11:37:55 +08:00
|
|
|
self->status = -1;
|
|
|
|
return 0;
|
|
|
|
}
|