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"
|
|
|
|
#include "tcp.h"
|
|
|
|
#include "libscp.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "libscp.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
char user[257];
|
|
|
|
char pass[257];
|
|
|
|
char cmnd[257];
|
|
|
|
char serv[257];
|
|
|
|
char port[257];
|
|
|
|
|
|
|
|
struct log_config logging;
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
void cmndList(struct SCP_CONNECTION *c);
|
|
|
|
void cmndKill(struct SCP_CONNECTION *c, struct SCP_SESSION *s);
|
2016-12-23 20:52:22 +03:00
|
|
|
void cmndHelp(void);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
int inputSession(struct SCP_SESSION *s);
|
2008-09-12 00:23:15 +04:00
|
|
|
unsigned int menuSelect(unsigned int choices);
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
int main(int argc, char **argv)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
struct SCP_SESSION *s;
|
|
|
|
struct SCP_CONNECTION *c;
|
|
|
|
enum SCP_CLIENT_STATES_E e;
|
|
|
|
//int end;
|
|
|
|
int idx;
|
|
|
|
//int sel;
|
|
|
|
int sock;
|
|
|
|
char *pwd;
|
|
|
|
|
|
|
|
user[0] = '\0';
|
|
|
|
pass[0] = '\0';
|
|
|
|
cmnd[0] = '\0';
|
|
|
|
serv[0] = '\0';
|
|
|
|
port[0] = '\0';
|
|
|
|
|
2016-06-22 02:30:17 +03:00
|
|
|
logging.program_name = "sesadmin";
|
2012-09-20 07:51:34 +04:00
|
|
|
logging.log_file = g_strdup("xrdp-sesadmin.log");
|
|
|
|
logging.log_level = LOG_LEVEL_DEBUG;
|
|
|
|
logging.enable_syslog = 0;
|
|
|
|
log_start_from_param(&logging);
|
|
|
|
|
|
|
|
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], "-s=", 3))
|
|
|
|
{
|
|
|
|
g_strncpy(serv, (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(serv, "", 1))
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
g_strncpy(serv, "localhost", 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);
|
|
|
|
|
|
|
|
/* zeroing the password */
|
|
|
|
while ((*pwd) != '\0')
|
|
|
|
{
|
|
|
|
(*pwd) = 0x00;
|
|
|
|
pwd++;
|
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2016-06-22 02:30:16 +03:00
|
|
|
scp_init();
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
sock = g_tcp_socket();
|
2014-07-27 00:33:23 +04:00
|
|
|
if (sock < 0)
|
|
|
|
{
|
2016-10-17 08:21:07 +03:00
|
|
|
LOG_DBG("Socket open error, g_tcp_socket() failed");
|
2014-07-27 00:33:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
s = scp_session_create();
|
|
|
|
c = scp_connection_create(sock);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2016-10-17 08:21:07 +03:00
|
|
|
LOG_DBG("Connecting to %s:%s with user %s (%s)", serv, port, user, pass);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (0 != g_tcp_connect(sock, serv, port))
|
|
|
|
{
|
2016-10-17 08:21:07 +03:00
|
|
|
LOG_DBG("g_tcp_connect() error");
|
2012-09-20 07:51:34 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
scp_session_set_type(s, SCP_SESSION_TYPE_MANAGE);
|
|
|
|
scp_session_set_version(s, 1);
|
|
|
|
scp_session_set_username(s, user);
|
|
|
|
scp_session_set_password(s, pass);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
e = scp_v1c_mng_connect(c, s);
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (SCP_CLIENT_STATE_OK != e)
|
|
|
|
{
|
2016-10-17 08:21:07 +03:00
|
|
|
LOG_DBG("libscp error connecting: %s %d", s->errstr, (int)e);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (0 == g_strncmp(cmnd, "list", 5))
|
|
|
|
{
|
|
|
|
cmndList(c);
|
|
|
|
}
|
|
|
|
else if (0 == g_strncmp(cmnd, "kill:", 5))
|
|
|
|
{
|
|
|
|
cmndKill(c, s);
|
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
g_tcp_close(sock);
|
|
|
|
scp_session_destroy(s);
|
|
|
|
scp_connection_destroy(c);
|
|
|
|
log_end();
|
2008-09-12 00:23:15 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
return 0;
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:52:22 +03:00
|
|
|
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
|
|
|
|
print_session(const struct SCP_DISCONNECTED_SESSION *s)
|
|
|
|
{
|
|
|
|
printf("Session ID: %d\n", s->SID);
|
|
|
|
printf("\tSession type: %d\n", s->type);
|
|
|
|
printf("\tScreen size: %dx%d, color depth %d\n",
|
|
|
|
s->width, s->height, s->bpp);
|
|
|
|
printf("\tIdle time: %d day(s) %d hour(s) %d minute(s)\n",
|
|
|
|
s->idle_days, s->idle_hours, s->idle_minutes);
|
|
|
|
printf("\tConnected: %04d/%02d/%02d %02d:%02d\n",
|
|
|
|
s->conn_year, s->conn_month, s->conn_day, s->conn_hour,
|
|
|
|
s->conn_minute);
|
|
|
|
}
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
void cmndList(struct SCP_CONNECTION *c)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
struct SCP_DISCONNECTED_SESSION *dsl;
|
|
|
|
enum SCP_CLIENT_STATES_E e;
|
|
|
|
int scnt;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
e = scp_v1c_mng_get_session_list(c, &scnt, &dsl);
|
|
|
|
|
2017-01-26 02:32:57 +03:00
|
|
|
if (e != SCP_CLIENT_STATE_LIST_OK)
|
|
|
|
{
|
|
|
|
printf("Error getting session list.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scnt > 0)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
for (idx = 0; idx < scnt; idx++)
|
|
|
|
{
|
2017-01-26 03:24:34 +03:00
|
|
|
print_session(&dsl[idx]);
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
else
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
printf("No sessions.\n");
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
2014-07-19 22:56:00 +04:00
|
|
|
|
2017-01-26 02:32:57 +03:00
|
|
|
g_free(dsl);
|
2008-09-12 00:23:15 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
void cmndKill(struct SCP_CONNECTION *c, struct SCP_SESSION *s)
|
2008-09-12 00:23:15 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|