2008-09-12 00:23:15 +04:00
|
|
|
/*
|
|
|
|
* sesadmin.c - an sesman administration tool
|
|
|
|
* (c) 2008 Simone Fedele
|
|
|
|
*
|
2015-08-27 00:33:00 +03: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.
|
2008-09-12 00:23:15 +04:00
|
|
|
*/
|
|
|
|
|
2017-03-03 07:33:23 +03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config_ac.h>
|
|
|
|
#endif
|
|
|
|
|
2008-09-12 00:23:15 +04:00
|
|
|
#include "arch.h"
|
2022-02-14 23:13:03 +03:00
|
|
|
#include "trans.h"
|
2008-09-12 00:23:15 +04:00
|
|
|
#include "log.h"
|
2022-02-14 23:13:03 +03:00
|
|
|
#include "os_calls.h"
|
2020-12-21 15:36:00 +03:00
|
|
|
#include "string_calls.h"
|
2022-02-14 23:13:03 +03:00
|
|
|
#include "tools_common.h"
|
2008-09-12 00:23:15 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
char user[257];
|
|
|
|
char pass[257];
|
|
|
|
char cmnd[257];
|
|
|
|
char port[257];
|
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
static int cmndList(struct trans *t);
|
|
|
|
static int cmndKill(struct trans *t);
|
|
|
|
static void cmndHelp(void);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
int main(int argc, char **argv)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2021-06-18 13:48:05 +03:00
|
|
|
struct trans *t;
|
2012-09-20 07:51:34 +04:00
|
|
|
//int end;
|
|
|
|
int idx;
|
|
|
|
//int sel;
|
|
|
|
char *pwd;
|
2020-10-30 15:13:31 +03:00
|
|
|
struct log_config *logging;
|
2022-02-14 23:13:03 +03:00
|
|
|
int rv = 1;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
user[0] = '\0';
|
|
|
|
pass[0] = '\0';
|
|
|
|
cmnd[0] = '\0';
|
|
|
|
port[0] = '\0';
|
|
|
|
|
2020-12-03 14:54:22 +03:00
|
|
|
logging = log_config_init_for_console(LOG_LEVEL_INFO, NULL);
|
2020-10-30 15:13:31 +03:00
|
|
|
log_start_from_param(logging);
|
|
|
|
log_config_free(logging);
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
for (idx = 0; idx < argc; idx++)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (0 == g_strncmp(argv[idx], "-u=", 3))
|
|
|
|
{
|
|
|
|
g_strncpy(user, (argv[idx]) + 3, 256);
|
|
|
|
}
|
|
|
|
else if (0 == g_strncmp(argv[idx], "-p=", 3))
|
|
|
|
{
|
|
|
|
g_strncpy(pass, (argv[idx]) + 3, 256);
|
|
|
|
}
|
|
|
|
else if (0 == g_strncmp(argv[idx], "-i=", 3))
|
|
|
|
{
|
|
|
|
g_strncpy(port, (argv[idx]) + 3, 256);
|
|
|
|
}
|
|
|
|
else if (0 == g_strncmp(argv[idx], "-c=", 3))
|
|
|
|
{
|
|
|
|
g_strncpy(cmnd, (argv[idx]) + 3, 256);
|
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (0 == g_strncmp(port, "", 1))
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
g_strncpy(port, "3350", 256);
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (0 == g_strncmp(user, "", 1))
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2016-02-14 21:27:17 +03:00
|
|
|
cmndHelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == g_strncmp(cmnd, "", 1))
|
|
|
|
{
|
|
|
|
cmndHelp();
|
|
|
|
return 0;
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
if (0 == g_strncmp(pass, "", 1))
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
pwd = getpass("password:");
|
|
|
|
g_strncpy(pass, pwd, 256);
|
|
|
|
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2022-03-19 14:29:28 +03:00
|
|
|
t = scp_connect(port, NULL);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
|
|
|
|
2021-09-17 13:29:02 +03:00
|
|
|
if (t == NULL)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2021-09-17 13:29:02 +03:00
|
|
|
LOG(LOG_LEVEL_ERROR, "scp_connect() error");
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2022-02-14 23:13:03 +03:00
|
|
|
else if (0 == g_strncmp(cmnd, "list", 5))
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
rv = cmndList(t);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
else if (0 == g_strncmp(cmnd, "kill:", 5))
|
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
rv = cmndKill(t);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
g_memset(pass, '\0', sizeof(pass));
|
|
|
|
|
2021-06-18 13:48:05 +03:00
|
|
|
trans_delete(t);
|
2012-09-20 07:51:34 +04:00
|
|
|
log_end();
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
return rv;
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
static void
|
|
|
|
cmndHelp(void)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2016-02-14 07:41:07 +03:00
|
|
|
fprintf(stderr, "sesadmin - a console sesman administration tool\n");
|
|
|
|
fprintf(stderr, "syntax: sesadmin [] COMMAND [OPTIONS]\n\n");
|
2012-09-20 07:51:34 +04:00
|
|
|
fprintf(stderr, "-u=<username>: username to connect to sesman [MANDATORY]\n");
|
2016-02-14 21:27:17 +03:00
|
|
|
fprintf(stderr, "-p=<password>: password to connect to sesman (asked if not given)\n");
|
2012-09-20 07:51:34 +04:00
|
|
|
fprintf(stderr, "-s=<hostname>: sesman host (default is localhost)\n");
|
|
|
|
fprintf(stderr, "-i=<port> : sesman port (default 3350)\n");
|
|
|
|
fprintf(stderr, "-c=<command> : command to execute on the server [MANDATORY]\n");
|
|
|
|
fprintf(stderr, " it can be one of those:\n");
|
2016-12-20 09:54:33 +03:00
|
|
|
fprintf(stderr, " list\n");
|
|
|
|
fprintf(stderr, " kill:<sid>\n");
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:24:34 +03:00
|
|
|
static void
|
2022-02-14 23:13:03 +03:00
|
|
|
print_session(const struct scp_session_info *s)
|
2017-01-26 03:24:34 +03:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
printf("Session ID: %d\n", s->sid);
|
|
|
|
printf("\tDisplay: :%u\n", s->display);
|
|
|
|
printf("\tUser: %s\n", s->username);
|
|
|
|
printf("\tSession type: %s\n", SCP_SESSION_TYPE_TO_STR(s->type));
|
2017-01-26 03:24:34 +03:00
|
|
|
printf("\tScreen size: %dx%d, color depth %d\n",
|
|
|
|
s->width, s->height, s->bpp);
|
2022-02-14 23:13:03 +03:00
|
|
|
printf("\tStarted: %s", ctime(&s->start_time));
|
|
|
|
if (s->connection_description[0] != '\0')
|
|
|
|
{
|
|
|
|
printf("\tConnection Description: %s\n", s->connection_description);
|
|
|
|
}
|
2017-01-26 03:24:34 +03:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
static int
|
|
|
|
cmndList(struct trans *t)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
struct list *sessions = list_create();
|
|
|
|
int end_of_list = 0;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
enum scp_list_sessions_status status;
|
|
|
|
struct scp_session_info *p;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
int rv = scp_send_list_sessions_request(t, user, pass);
|
|
|
|
|
|
|
|
sessions->auto_free = 1;
|
2017-01-26 02:32:57 +03:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
while (rv == 0 && !end_of_list)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
rv = wait_for_sesman_reply(t, E_SCP_LIST_SESSIONS_RESPONSE);
|
|
|
|
if (rv != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = scp_get_list_sessions_response(t, &status, &p);
|
|
|
|
if (rv != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status)
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
case E_SCP_LS_AUTHENTICATION_FAIL:
|
|
|
|
printf("Connection denied (authentication error)\n");
|
|
|
|
rv = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_SCP_LS_SESSION_INFO:
|
|
|
|
list_add_item(sessions, (tintptr)p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_SCP_LS_END_OF_LIST:
|
|
|
|
end_of_list = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Unexpected return code %d\n", status);
|
|
|
|
rv = 1;
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2022-02-14 23:13:03 +03:00
|
|
|
scp_msg_in_reset(t);
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2022-02-14 23:13:03 +03:00
|
|
|
|
|
|
|
if (rv == 0)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
if (sessions->count == 0)
|
|
|
|
{
|
|
|
|
printf("No sessions.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0 ; i < sessions->count; ++i)
|
|
|
|
{
|
|
|
|
print_session((struct scp_session_info *)sessions->items[i]);
|
|
|
|
}
|
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2014-07-19 22:56:00 +04:00
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
list_delete(sessions);
|
|
|
|
return rv;
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:13:03 +03:00
|
|
|
static int
|
|
|
|
cmndKill(struct trans *t)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2022-02-14 23:13:03 +03:00
|
|
|
fprintf(stderr, "not yet implemented\n");
|
|
|
|
return 1;
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|