Reformat telnetd.c
This commit is contained in:
@ -38,7 +38,8 @@
|
|||||||
#include <log_manager.h>
|
#include <log_manager.h>
|
||||||
#include <modinfo.h>
|
#include <modinfo.h>
|
||||||
|
|
||||||
MODULE_INFO info = {
|
MODULE_INFO info =
|
||||||
|
{
|
||||||
MODULE_API_PROTOCOL,
|
MODULE_API_PROTOCOL,
|
||||||
MODULE_GA,
|
MODULE_GA,
|
||||||
GWPROTOCOL_VERSION,
|
GWPROTOCOL_VERSION,
|
||||||
@ -81,7 +82,8 @@ static int telnetd_listen(DCB *dcb, char *config);
|
|||||||
/**
|
/**
|
||||||
* The "module object" for the telnetd protocol module.
|
* The "module object" for the telnetd protocol module.
|
||||||
*/
|
*/
|
||||||
static GWPROTOCOL MyObject = {
|
static GWPROTOCOL MyObject =
|
||||||
|
{
|
||||||
telnetd_read_event, /**< Read - EPOLLIN handler */
|
telnetd_read_event, /**< Read - EPOLLIN handler */
|
||||||
telnetd_write, /**< Write - data from gateway */
|
telnetd_write, /**< Write - data from gateway */
|
||||||
telnetd_write_event, /**< WriteReady - EPOLLOUT handler */
|
telnetd_write_event, /**< WriteReady - EPOLLOUT handler */
|
||||||
@ -93,7 +95,7 @@ static GWPROTOCOL MyObject = {
|
|||||||
telnetd_listen, /**< Create a listener */
|
telnetd_listen, /**< Create a listener */
|
||||||
NULL, /**< Authentication */
|
NULL, /**< Authentication */
|
||||||
NULL /**< Session */
|
NULL /**< Session */
|
||||||
};
|
};
|
||||||
|
|
||||||
static void telnetd_command(DCB *, unsigned char *cmd);
|
static void telnetd_command(DCB *, unsigned char *cmd);
|
||||||
static void telnetd_echo(DCB *dcb, int enable);
|
static void telnetd_echo(DCB *dcb, int enable);
|
||||||
@ -103,8 +105,7 @@ static void telnetd_echo(DCB *dcb, int enable);
|
|||||||
*
|
*
|
||||||
* @return version string of the module
|
* @return version string of the module
|
||||||
*/
|
*/
|
||||||
char *
|
char* version()
|
||||||
version()
|
|
||||||
{
|
{
|
||||||
return version_str;
|
return version_str;
|
||||||
}
|
}
|
||||||
@ -113,8 +114,7 @@ version()
|
|||||||
* The module initialisation routine, called when the module
|
* The module initialisation routine, called when the module
|
||||||
* is first loaded.
|
* is first loaded.
|
||||||
*/
|
*/
|
||||||
void
|
void ModuleInit()
|
||||||
ModuleInit()
|
|
||||||
{
|
{
|
||||||
MXS_INFO("Initialise Telnetd Protocol module.");
|
MXS_INFO("Initialise Telnetd Protocol module.");
|
||||||
}
|
}
|
||||||
@ -127,8 +127,7 @@ ModuleInit()
|
|||||||
*
|
*
|
||||||
* @return The module object
|
* @return The module object
|
||||||
*/
|
*/
|
||||||
GWPROTOCOL *
|
GWPROTOCOL* GetModuleObject()
|
||||||
GetModuleObject()
|
|
||||||
{
|
{
|
||||||
return &MyObject;
|
return &MyObject;
|
||||||
}
|
}
|
||||||
@ -139,18 +138,16 @@ GetModuleObject()
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_read_event(DCB* dcb)
|
||||||
telnetd_read_event(DCB* dcb)
|
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
GWBUF *head = NULL;
|
GWBUF *head = NULL;
|
||||||
SESSION *session = dcb->session;
|
SESSION *session = dcb->session;
|
||||||
TELNETD *telnetd = (TELNETD *)dcb->protocol;
|
TELNETD *telnetd = (TELNETD *)dcb->protocol;
|
||||||
char *password, *t;
|
char *password, *t;
|
||||||
|
|
||||||
if ((n = dcb_read(dcb, &head, 0)) != -1)
|
if ((n = dcb_read(dcb, &head, 0)) != -1)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (head)
|
if (head)
|
||||||
{
|
{
|
||||||
unsigned char *ptr = GWBUF_DATA(head);
|
unsigned char *ptr = GWBUF_DATA(head);
|
||||||
@ -170,7 +167,9 @@ char *password, *t;
|
|||||||
/* Strip the cr/lf from the username */
|
/* Strip the cr/lf from the username */
|
||||||
t = strstr(telnetd->username, "\r\n");
|
t = strstr(telnetd->username, "\r\n");
|
||||||
if (t)
|
if (t)
|
||||||
|
{
|
||||||
*t = 0;
|
*t = 0;
|
||||||
|
}
|
||||||
telnetd->state = TELNETD_STATE_PASSWD;
|
telnetd->state = TELNETD_STATE_PASSWD;
|
||||||
dcb_printf(dcb, "Password: ");
|
dcb_printf(dcb, "Password: ");
|
||||||
telnetd_echo(dcb, 0);
|
telnetd_echo(dcb, 0);
|
||||||
@ -181,7 +180,9 @@ char *password, *t;
|
|||||||
/* Strip the cr/lf from the username */
|
/* Strip the cr/lf from the username */
|
||||||
t = strstr(password, "\r\n");
|
t = strstr(password, "\r\n");
|
||||||
if (t)
|
if (t)
|
||||||
|
{
|
||||||
*t = 0;
|
*t = 0;
|
||||||
|
}
|
||||||
if (admin_verify(telnetd->username, password))
|
if (admin_verify(telnetd->username, password))
|
||||||
{
|
{
|
||||||
telnetd_echo(dcb, 1);
|
telnetd_echo(dcb, 1);
|
||||||
@ -219,8 +220,7 @@ char *password, *t;
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_write_event(DCB *dcb)
|
||||||
telnetd_write_event(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
return dcb_drain_writeq(dcb);
|
return dcb_drain_writeq(dcb);
|
||||||
}
|
}
|
||||||
@ -234,8 +234,7 @@ telnetd_write_event(DCB *dcb)
|
|||||||
* @param dcb Descriptor Control Block for the socket
|
* @param dcb Descriptor Control Block for the socket
|
||||||
* @param queue Linked list of buffes to write
|
* @param queue Linked list of buffes to write
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_write(DCB *dcb, GWBUF *queue)
|
||||||
telnetd_write(DCB *dcb, GWBUF *queue)
|
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
rc = dcb_write(dcb, queue);
|
rc = dcb_write(dcb, queue);
|
||||||
@ -247,8 +246,7 @@ telnetd_write(DCB *dcb, GWBUF *queue)
|
|||||||
*
|
*
|
||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_error(DCB *dcb)
|
||||||
telnetd_error(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -258,8 +256,7 @@ telnetd_error(DCB *dcb)
|
|||||||
*
|
*
|
||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_hangup(DCB *dcb)
|
||||||
telnetd_hangup(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -271,10 +268,9 @@ telnetd_hangup(DCB *dcb)
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
* @return The number of new connections created
|
* @return The number of new connections created
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_accept(DCB *dcb)
|
||||||
telnetd_accept(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
int n_connect = 0;
|
int n_connect = 0;
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@ -287,14 +283,15 @@ int n_connect = 0;
|
|||||||
so = accept(dcb->fd, (struct sockaddr *)&addr, &addrlen);
|
so = accept(dcb->fd, (struct sockaddr *)&addr, &addrlen);
|
||||||
|
|
||||||
if (so == -1)
|
if (so == -1)
|
||||||
|
{
|
||||||
return n_connect;
|
return n_connect;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
atomic_add(&dcb->stats.n_accepts, 1);
|
atomic_add(&dcb->stats.n_accepts, 1);
|
||||||
client_dcb = dcb_alloc(DCB_ROLE_REQUEST_HANDLER);
|
client_dcb = dcb_alloc(DCB_ROLE_REQUEST_HANDLER);
|
||||||
|
|
||||||
if (client_dcb == NULL)
|
if (client_dcb == NULL)
|
||||||
|
|
||||||
{
|
{
|
||||||
close(so);
|
close(so);
|
||||||
return n_connect;
|
return n_connect;
|
||||||
@ -302,8 +299,8 @@ int n_connect = 0;
|
|||||||
client_dcb->fd = so;
|
client_dcb->fd = so;
|
||||||
client_dcb->remote = strdup(inet_ntoa(addr.sin_addr));
|
client_dcb->remote = strdup(inet_ntoa(addr.sin_addr));
|
||||||
memcpy(&client_dcb->func, &MyObject, sizeof(GWPROTOCOL));
|
memcpy(&client_dcb->func, &MyObject, sizeof(GWPROTOCOL));
|
||||||
client_dcb->session =
|
client_dcb->session = session_alloc(dcb->session->service, client_dcb);
|
||||||
session_alloc(dcb->session->service, client_dcb);
|
|
||||||
if (NULL == client_dcb->session)
|
if (NULL == client_dcb->session)
|
||||||
{
|
{
|
||||||
dcb_close(client_dcb);
|
dcb_close(client_dcb);
|
||||||
@ -339,13 +336,14 @@ int n_connect = 0;
|
|||||||
* @param dcb The descriptor control block
|
* @param dcb The descriptor control block
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int
|
static int telnetd_close(DCB *dcb)
|
||||||
telnetd_close(DCB *dcb)
|
|
||||||
{
|
{
|
||||||
TELNETD *telnetd = dcb->protocol;
|
TELNETD *telnetd = dcb->protocol;
|
||||||
|
|
||||||
if (telnetd && telnetd->username)
|
if (telnetd && telnetd->username)
|
||||||
|
{
|
||||||
free(telnetd->username);
|
free(telnetd->username);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -356,19 +354,19 @@ TELNETD *telnetd = dcb->protocol;
|
|||||||
* @param listener The Listener DCB
|
* @param listener The Listener DCB
|
||||||
* @param config Configuration (ip:port)
|
* @param config Configuration (ip:port)
|
||||||
*/
|
*/
|
||||||
static int
|
static int telnetd_listen(DCB *listener, char *config)
|
||||||
telnetd_listen(DCB *listener, char *config)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
int one = 1;
|
int one = 1;
|
||||||
int rc;
|
int rc;
|
||||||
int syseno = 0;
|
int syseno = 0;
|
||||||
|
|
||||||
memcpy(&listener->func, &MyObject, sizeof(GWPROTOCOL));
|
memcpy(&listener->func, &MyObject, sizeof(GWPROTOCOL));
|
||||||
|
|
||||||
if (!parse_bindconfig(config, 4442, &addr))
|
if (!parse_bindconfig(config, 4442, &addr))
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if ((listener->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
if ((listener->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||||
{
|
{
|
||||||
@ -378,7 +376,8 @@ int syseno = 0;
|
|||||||
// socket options
|
// socket options
|
||||||
syseno = setsockopt(listener->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
|
syseno = setsockopt(listener->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
|
||||||
|
|
||||||
if(syseno != 0){
|
if (syseno != 0)
|
||||||
|
{
|
||||||
char errbuf[STRERROR_BUFLEN];
|
char errbuf[STRERROR_BUFLEN];
|
||||||
MXS_ERROR("Failed to set socket options. Error %d: %s",
|
MXS_ERROR("Failed to set socket options. Error %d: %s",
|
||||||
errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
errno, strerror_r(errno, errbuf, sizeof(errbuf)));
|
||||||
@ -394,9 +393,12 @@ int syseno = 0;
|
|||||||
|
|
||||||
rc = listen(listener->fd, SOMAXCONN);
|
rc = listen(listener->fd, SOMAXCONN);
|
||||||
|
|
||||||
if (rc == 0) {
|
if (rc == 0)
|
||||||
|
{
|
||||||
MXS_NOTICE("Listening telnet connections at %s", config);
|
MXS_NOTICE("Listening telnet connections at %s", config);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
int eno = errno;
|
int eno = errno;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
char errbuf[STRERROR_BUFLEN];
|
char errbuf[STRERROR_BUFLEN];
|
||||||
@ -407,7 +409,6 @@ int syseno = 0;
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (poll_add_dcb(listener) == -1)
|
if (poll_add_dcb(listener) == -1)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@ -425,8 +426,7 @@ int syseno = 0;
|
|||||||
* @param dcb The client DCB
|
* @param dcb The client DCB
|
||||||
* @param cmd The command stream
|
* @param cmd The command stream
|
||||||
*/
|
*/
|
||||||
static void
|
static void telnetd_command(DCB *dcb, unsigned char *cmd)
|
||||||
telnetd_command(DCB *dcb, unsigned char *cmd)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -436,14 +436,15 @@ telnetd_command(DCB *dcb, unsigned char *cmd)
|
|||||||
* @param dcb DCB of the telnet connection
|
* @param dcb DCB of the telnet connection
|
||||||
* @param enable Enable or disable echo functionality
|
* @param enable Enable or disable echo functionality
|
||||||
*/
|
*/
|
||||||
static void
|
static void telnetd_echo(DCB *dcb, int enable)
|
||||||
telnetd_echo(DCB *dcb, int enable)
|
|
||||||
{
|
{
|
||||||
GWBUF *gwbuf;
|
GWBUF *gwbuf;
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
if ((gwbuf = gwbuf_alloc(3)) == NULL)
|
if ((gwbuf = gwbuf_alloc(3)) == NULL)
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
buf = GWBUF_DATA(gwbuf);
|
buf = GWBUF_DATA(gwbuf);
|
||||||
buf[0] = TELNET_IAC;
|
buf[0] = TELNET_IAC;
|
||||||
buf[1] = enable ? TELNET_WONT : TELNET_WILL;
|
buf[1] = enable ? TELNET_WONT : TELNET_WILL;
|
||||||
|
|||||||
Reference in New Issue
Block a user