Commit 0e6c2803 authored by Renato Figueiro Maia's avatar Renato Figueiro Maia

[OPENBUS-2742] Fazer com que o busadmin ao pedir a senha interativamente não...

[OPENBUS-2742] Fazer com que o busadmin ao pedir a senha interativamente não leia apenas um máximo de 8 characteres

git-svn-id: https://subversion.tecgraf.puc-rio.br/engdist/openbus/sdk/lua/trunk@161213 ae0415b3-e90b-0410-900d-d0be9363c56b
parent 23e183ac
......@@ -38,25 +38,30 @@ return function (...)
local _, port = assert(sock:getsockname())
assert(sock:listen())
local extra = ""
if preload.lpw ~= nil then
extra = [[
["*?"] = require("lpw").getpass,
]]
end
spawn([[
local io = require "io"
local readline = io.read
local writetext = io.write
local cothread = require "cothread"
cothread.plugin(require "cothread.plugin.socket")
local socket = require "cothread.socket"
local newsocket = socket.tcp
local echo = require "openbus.util.echo"
local getecho = echo.getecho
local setecho = echo.setecho
local Message = "%d\n%s"
local handlers = {
]]..extra..[[
["*?"] = function()
local active = getecho()
if active then setecho(false) end
result = readline()
if active then setecho(true) end
writetext("\n")
return result
end,
}
local sock = newsocket()
......@@ -65,13 +70,8 @@ return function (...)
while true do
local format = sock:receive()
if format ~= nil then
local handler = handlers[format]
if handler ~= nil then
result = handler()
else
result = readline(format)
end
assert(sock:send(#result.."\n"..result))
local ok, result = pcall(handlers[format] or readline, format)
assert(sock:send((ok and "." or "!")..#result.."\n"..result))
else
assert(sock:close())
break
......@@ -95,9 +95,15 @@ return function (...)
if #results == 0 then results[1] = "*l" end
if mutex:try() then
for index, format in ipairs(results) do
conn:send(tostring(format).."\n")
local size = tonumber(conn:receive())
results[index] = size == 0 and "" or conn:receive(size)
assert(conn:send(tostring(format).."\n"))
local ok = (assert(conn:receive(1)) == ".")
local size = assert(tonumber(assert(conn:receive())))
local result = (size == 0) and "" or assert(conn:receive(size))
if not ok then
mutex:free()
error(result)
end
results[index] = result
end
mutex:free()
return unpack(results)
......
......@@ -65,7 +65,7 @@ static int pmain (lua_State *L) {
}
}
openbuslua_report(L, status);
lua_pushinteger(L, EXIT_FAILURE);
lua_pushinteger(L, status == LUA_OK ? 0 : EXIT_FAILURE);
return 1;
}
......
#include <lua.h>
#include <lauxlib.h>
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#include "compat-5.2.h"
#endif
#if defined(_WIN32)
#include <windows.h>
static int getecho(int *active)
{
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
if (hstdin != INVALID_HANDLE_VALUE) {
DWORD mode;
if (GetConsoleMode(hstdin, &mode)) {
*active = (mode & ENABLE_ECHO_INPUT);
return 0;
}
}
return GetLastError();
}
static int setecho(int active)
{
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
if (hstdin != INVALID_HANDLE_VALUE) {
DWORD mode;
if (GetConsoleMode(hstdin, &mode)) {
mode = active ? (mode | ENABLE_ECHO_INPUT) : (mode & ~ENABLE_ECHO_INPUT);
if (SetConsoleMode(hstdin, mode)) {
return 0;
}
}
}
return GetLastError();
}
static int raiseerror(lua_State *L, int error)
{
char buffer[256];
DWORD res = FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|
FORMAT_MESSAGE_FROM_SYSTEM, /* formatting opts. */
NULL, /* ignored */
error, /* message id. */
0, /* language id. */
buffer, /* output buffer */
sizeof(buffer)/sizeof(char), /* buffer size */
NULL); /* ignored */
if (res != 0) {
DWORD msgerr = GetLastError();
luaL_error(L, "failed to message for error %d (got error %d)", error, msgerr);
}
lua_pushlstring(L, buffer, res);
lua_error(L);
}
#else
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
static int getecho(int *active)
{
int fildes = fileno(stdin);
struct termios opts;
if (tcgetattr(fildes, &opts) == 0) {
*active = opts.c_lflag & ECHO;
return 0;
}
return errno;
}
static int setecho(int active)
{
int fildes = fileno(stdin);
struct termios opts;
if (tcgetattr(fildes, &opts) == 0) {
opts.c_lflag = active ? (opts.c_lflag | ECHO) : (opts.c_lflag & ~ECHO);
if (tcsetattr(fildes, TCSANOW, &opts) == 0) {
return 0;
}
}
return errno;
}
static void raiseerror(lua_State *L, int code)
{
lua_pushstring(L, strerror(code));
lua_error(L);
}
#endif
static int l_getmode (lua_State *L)
{
int active;
int error = getecho(&active);
if (error) raiseerror(L, error);
lua_pushboolean(L, active);
return 1;
}
static int l_setmode (lua_State *L)
{
int error = setecho(lua_toboolean(L, 1));
if (error) raiseerror(L, error);
return 0;
}
static const luaL_Reg funcs[] = {
{"getecho", l_getmode},
{"setecho", l_setmode},
{NULL, NULL}
};
LUAMOD_API int luaopen_openbus_util_echo (lua_State *L) {
luaL_newlib(L, funcs);
return 1;
}
#include <lua.h>
#include <lauxlib.h>
#include <openbuslua.h>
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#include "compat-5.2.h"
#endif
#include "openbuslua.h"
#if defined(_WIN32)
#include <errno.h>
#include <process.h>
int prompt_newthread(void (__cdecl *func) (void *), void *data) {
static int newthread(void (__cdecl *func) (void *), void *data) {
uintptr_t res = _beginthread(func, 0, data);
if (res == -1L) return errno;
return 0;
}
const char *prompt_errormessage(int code) {
static const char *geterrmsg(int code) {
switch (code) {
case EAGAIN: return "too many threads";
case EINVAL: return "stack size is incorrect";
......@@ -23,7 +28,7 @@ const char *prompt_errormessage(int code) {
return "unexpected error";
}
void OpenBusLuaThread (void *data)
static void luathread (void *data)
{
lua_State *L = (lua_State *)data;
int status = openbuslua_call(L, 1, 0);
......@@ -36,18 +41,18 @@ void OpenBusLuaThread (void *data)
#include <errno.h>
#include <string.h>
int prompt_newthread(void *(*func) (void *), void *data) {
static int newthread(void *(*func) (void *), void *data) {
pthread_t thread;
int res = pthread_create(&thread, NULL, func, data);
if (res) return errno;
return 0;
}
const char *prompt_errormessage(int code) {
static const char *geterrmsg(int code) {
return strerror(code);
}
static void *OpenBusLuaThread (void *data)
static void *luathread (void *data)
{
lua_State *L = (lua_State *)data;
int status = openbuslua_call(L, 1, 0);
......@@ -57,10 +62,8 @@ static void *OpenBusLuaThread (void *data)
#endif
#include "openbuslua.h"
static void copyPreload (lua_State *from, lua_State *to)
static void copypreload (lua_State *from, lua_State *to)
{
/* table is in the stack at index 't' */
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM > 501
......@@ -96,12 +99,12 @@ static int l_spawn (lua_State *L)
lua_getfield(L, LUA_REGISTRYINDEX, "OPENBUS_DEBUG");
status = openbuslua_init(newL, 0, !lua_isnil(L, -1));
if (status == LUA_OK) {
copyPreload(L, newL);
copypreload(L, newL);
status = luaL_loadbuffer(newL, code, codelen, code);
if (status == LUA_OK) {
int res = prompt_newthread(OpenBusLuaThread, newL);
int res = newthread(luathread, newL);
if (res) {
const char *errmsg = prompt_errormessage(errno);
const char *errmsg = geterrmsg(errno);
lua_close(newL);
luaL_error(L, "unable to start thread (error=%s)", errmsg);
}
......
......@@ -3,7 +3,8 @@ LIBNAME= $(PROJNAME)
SRC= \
openbuslua.c \
threadlib.c \
lecholib.c \
lthreadlib.c \
$(PRELOAD_DIR)/$(LIBNAME).c
OPENBUSSCSIDL= ${SCS_IDL1_2_HOME}/src
......@@ -36,7 +37,8 @@ LUASRC= \
$(LUADIR)/openbus/util/sysex.lua \
$(LUADIR)/openbus/util/tickets.lua \
$(LUADIR)/openbus.lua \
threadlib.c \
lecholib.c \
lthreadlib.c \
$(LUADIR)/openbus/console/costdin.lua \
$(LUADIR)/openbus/console/utils.lua
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment