Skip to content

Commit 61878f1

Browse files
committed
Refactored session.c to support X server validation
1 parent 1fe5b8f commit 61878f1

File tree

3 files changed

+791
-454
lines changed

3 files changed

+791
-454
lines changed

sesman/scp_process.c

+43-64
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ authenticate_and_authorize_connection(struct sesman_con *sc,
157157
}
158158
else
159159
{
160-
LOG(LOG_LEVEL_INFO, "Access permitted for user: %s",
161-
username);
160+
LOG(LOG_LEVEL_INFO, "Access permitted for user=%s uid=%d",
161+
username, uid);
162162
sc->auth_info = auth_info;
163163
sc->uid = uid;
164164
sc->username = dup_username;
@@ -210,6 +210,7 @@ allocate_and_start_session(struct auth_info *auth_info,
210210
{
211211
int pid = 0;
212212
struct session_chain *temp = (struct session_chain *)NULL;
213+
enum scp_screate_status status;
213214

214215
/* check to limit concurrent sessions */
215216
if (session_get_count() >= (unsigned int)g_cfg->sess.max_sessions)
@@ -238,70 +239,39 @@ allocate_and_start_session(struct auth_info *auth_info,
238239
return E_SCP_SCREATE_NO_MEMORY;
239240
}
240241

241-
pid = g_fork();
242-
if (pid == -1)
242+
status = session_start(auth_info, params, &pid);
243+
if (status == E_SCP_SCREATE_OK)
243244
{
244-
LOG(LOG_LEVEL_ERROR,
245-
"[session start] (display %d): Failed to fork for scp with "
246-
"errno: %d, description: %s",
247-
params->display, g_get_errno(), g_get_strerror());
248-
g_free(temp->item);
249-
g_free(temp);
250-
return E_SCP_SCREATE_GENERAL_ERROR;
251-
}
252-
253-
if (pid == 0)
254-
{
255-
/**
256-
* We're now forked from the main sesman process, so we
257-
* can close file descriptors that we no longer need */
245+
if (ip_addr[0] != '\0')
246+
{
247+
LOG(LOG_LEVEL_INFO, "++ created session: username %s, ip %s",
248+
username, ip_addr);
249+
}
250+
else
251+
{
252+
LOG(LOG_LEVEL_INFO, "++ created session: username %s", username);
253+
}
258254

259-
sesman_close_all(0);
255+
temp->item->pid = pid;
256+
temp->item->display = params->display;
257+
temp->item->width = params->width;
258+
temp->item->height = params->height;
259+
temp->item->bpp = params->bpp;
260+
temp->item->auth_info = auth_info;
261+
g_strncpy(temp->item->start_ip_addr, ip_addr,
262+
sizeof(temp->item->start_ip_addr) - 1);
263+
temp->item->uid = params->uid;
264+
temp->item->guid = params->guid;
260265

261-
/* Wait objects created in a parent are not valid in a child */
262-
g_delete_wait_obj(g_reload_event);
263-
g_delete_wait_obj(g_sigchld_event);
264-
g_delete_wait_obj(g_term_event);
266+
temp->item->start_time = g_time1();
265267

266-
session_start(auth_info, params);
267-
g_exit(1); /* Should not get here */
268-
}
268+
temp->item->type = params->type;
269+
temp->item->status = SESMAN_SESSION_STATUS_ACTIVE;
269270

270-
if (ip_addr[0] != '\0')
271-
{
272-
LOG(LOG_LEVEL_INFO, "++ created session: username %s, ip %s",
273-
username, ip_addr);
271+
session_chain_add(temp);
274272
}
275-
else
276-
{
277-
LOG(LOG_LEVEL_INFO, "++ created session: username %s", username);
278-
}
279-
280-
LOG(LOG_LEVEL_INFO, "Starting session: session_pid %d, "
281-
"display :%d.0, width %d, height %d, bpp %d, client ip %s, "
282-
"UID %d",
283-
pid, params->display, params->width, params->height, params->bpp,
284-
ip_addr, params->uid);
285-
286-
temp->item->pid = pid;
287-
temp->item->display = params->display;
288-
temp->item->width = params->width;
289-
temp->item->height = params->height;
290-
temp->item->bpp = params->bpp;
291-
temp->item->auth_info = auth_info;
292-
g_strncpy(temp->item->start_ip_addr, ip_addr,
293-
sizeof(temp->item->start_ip_addr) - 1);
294-
temp->item->uid = params->uid;
295-
temp->item->guid = params->guid;
296-
297-
temp->item->start_time = g_time1();
298273

299-
temp->item->type = params->type;
300-
temp->item->status = SESMAN_SESSION_STATUS_ACTIVE;
301-
302-
session_chain_add(temp);
303-
304-
return E_SCP_SCREATE_OK;
274+
return status;
305275
}
306276

307277
/******************************************************************************/
@@ -489,6 +459,8 @@ process_create_session_request(struct sesman_con *sc)
489459
// Parameters for a new session (if required). Filled in as
490460
// we go along.
491461
struct session_parameters sp = {0};
462+
const char *shellptr;
463+
const char *dirptr;
492464
enum scp_screate_status status = E_SCP_SCREATE_OK;
493465

494466
int display = 0;
@@ -498,7 +470,7 @@ process_create_session_request(struct sesman_con *sc)
498470

499471
rv = scp_get_create_session_request(sc->t,
500472
&sp.type, &sp.width, &sp.height,
501-
&sp.bpp, &sp.shell, &sp.directory);
473+
&sp.bpp, &shellptr, &dirptr);
502474

503475
if (rv == 0)
504476
{
@@ -509,8 +481,12 @@ process_create_session_request(struct sesman_con *sc)
509481
else
510482
{
511483
LOG(LOG_LEVEL_INFO,
512-
"Received request from %s to create a session for user %s",
513-
sc->peername, sc->username);
484+
"Received request from %s to create a session for user %s"
485+
" type=%s"
486+
" geometry=%dx%d, bpp=%d, shell=\"%s\", dir=\"%s\"",
487+
sc->peername, sc->username,
488+
SCP_SESSION_TYPE_TO_STR(sp.type),
489+
sp.width, sp.height, sp.bpp, shellptr, dirptr);
514490

515491
struct session_item *s_item =
516492
session_get_bydata(sc->uid, sp.type, sp.width, sp.height,
@@ -546,9 +522,13 @@ process_create_session_request(struct sesman_con *sc)
546522
guid = guid_new();
547523
display = session_get_available_display();
548524

549-
sp.guid = guid;
550525
sp.display = display;
551526
sp.uid = sc->uid;
527+
sp.guid = guid;
528+
// These need to be copied so they are available
529+
// when the sub-process closes all the connections
530+
g_snprintf(sp.shell, sizeof(sp.shell), "%s", shellptr);
531+
g_snprintf(sp.directory, sizeof(sp.directory), "%s", dirptr);
552532

553533
if (display == 0)
554534
{
@@ -696,4 +676,3 @@ scp_process(struct sesman_con *sc)
696676
}
697677
return rv;
698678
}
699-

0 commit comments

Comments
 (0)