Jump to content

Console Class Quest Addition


Recommended Posts

Hi everybody!

I've written a small class. It's meant for development and technical functions and direct communication with the core. For example you can write into syserr/syslog, create a file backup or trigger log rotation. If you have any ideas what I could add, feel free to tell me :) Maybe I'll add them.

Also feel free to pm me for any ideas I'd do. I'm a little bit bored :D

Quote
Disclaimer (kinda)

I hereby declare that the changes I made are all by myself. I did not steal from anyone. Therefore for this guide I am the author. If anyone wants to copy my guide and post it anywhere he's free to do as long as he mentions the original author. I do not provide or share the source code or anything else protected by copyright.
 
If something breaks I'm not the one to blame at. Always make sure to test changes. Never implement them on production releases, always use test distributions before! If you find any flaws, may it be regarding security or something else you're free to tell me so. I'm learning, as we all do, so I'm in no way too proud to admit I'm making mistakes. I'll correct them as soon as possible of course.

 

Here it is :) Just add it as any other quest extension to your source :)

Quote
#include "stdafx.h"
#include "questlua.h"
#include "questmanager.h"

namespace quest
{
  int console_sys_err(lua_State* L)
  {
    if (!lua_isstring(L, 1))
    {
      sys_err("quest::console::syserr: invalid argument");
      lua_pushnumber(L, 0);
      return 1;
    }
    
    const char* rslt = lua_tostring(L, 1);
    sys_err(rslt);
    return 0;
  }

  int console_sys_log(lua_State* L)
  {
    if (!lua_isstring(L, 1))
    {
      sys_err("quest::console::syslog: invalid argument");
      lua_pushnumber(L, 0);
      return 1;
    }
    
    const char* rslt = lua_tostring(L, 1);
    sys_log(0, rslt);
    return 0;
  }

  int console_log_rotate(lua_State* L)
  {
    log_rotate();
    return 0;
  }

  int console_file_backup(lua_State* L)
  {
    if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
    {
      sys_err("quest::console::syslog: usage: <serverfilesdir> <backupdir>");
      lua_pushboolean(L, false);
      return 1;
    }
    
    std::string command = "tar -u -a -f ";
    command.append(lua_tostring(L, 2)); //backup dir
    command.append(" ");
    command.append(lua_tostring(L, 1)); //serverfiles dir
    system(command.c_str());
    lua_pushboolean(L, true);
    return 0;
  }


  void RegisterConsoleFunctionTable()
  {
    luaL_Reg console_functions[] = 
    {
    { "sys_err", console_sys_err },
    { "log_rotate", console_log_rotate },
    { "sys_log", console_sys_log },
    { "file_backup", console_file_backup },
    { NULL, NULL }
    };

    CQuestManager::instance().AddLuaFunctionTable("console", console_functions);
  }
}

 

Sorry for the indentation again.. Quote tag! >.<

Oh and one thing you should note: If you're getting errors with luaL_Reg then just replace it with luaL_reg

Why? Because in newer implementations of lua luaL_reg is replaced by luaL_Reg. That's why I'm following up with that change. But if you're using the standard lua, then you're free to rollback ;) Thanks to flygun for pointing towards the confusing name change :)

  • Love 5
Link to comment
Share on other sites

You can keep the indentation, and it'll be prettier, if you use code tags:

#include "stdafx.h"
#include "questlua.h"
#include "questmanager.h"
namespace quest
{
    int console_sys_err(lua_State* L)
    {
        if (!lua_isstring(L, 1))
        {
            sys_err("quest::console::syserr: invalid argument");
            lua_pushnumber(L, 0);
            return 1;
        }
        const char* rslt = lua_tostring(L, 1);
        sys_err(rslt);
        return 0;
    }
    int console_sys_log(lua_State* L)
    {
        if (!lua_isstring(L, 1))
        {
            sys_err("quest::console::syslog: invalid argument");
            lua_pushnumber(L, 0);
            return 1;
        }
        const char* rslt = lua_tostring(L, 1);
        sys_log(0, rslt);
        return 0;
    }
    int console_log_rotate(lua_State* L)
    {
        log_rotate();
        return 0;
    }
    int console_file_backup(lua_State* L)
    {
        if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
        {
            sys_err("quest::console::syslog: usage: <serverfilesdir> <backupdir>");
            lua_pushboolean(L, false);
            return 1;
        }
        std::string command = "tar -u -a -f ";
        command.append(lua_tostring(L, 2)); //backup dir
        command.append(" ");
        command.append(lua_tostring(L, 1)); //serverfiles dir
        system(command.c_str());
        lua_pushboolean(L, true);
        return 0;
    }
    void RegisterConsoleFunctionTable()
    {
        luaL_Reg console_functions[] =
        {
            { "sys_err", console_sys_err },
            { "log_rotate", console_log_rotate },
            { "sys_log", console_sys_log },
            { "file_backup", console_file_backup },
            { NULL, NULL }
        };
        CQuestManager::instance().AddLuaFunctionTable("console", console_functions);
    }
}

 

And since you mentioned the security vulnerabilities... this is arguably a security problem:

system(command.c_str());

I mean, you could do console.file_backup("", "; rm -rf /") and poof the whole thing!

 

As for syslog & syserr, there are available two functions on the global quest scope:

            {    "sys_err",                    _syserr                    },
            {    "sys_log",                    _syslog                    },

Other than that, its ok!

 

Link to comment
Share on other sites

Are you sure about the two functions being available there? I didn't find them, maybe I missed them.. :/

 

To your vulnerability: Yep, that's true. But there's no reason in hindering admins from doing that.

You know, they'd also just use

os.execute("rm -rf /")

and have the same vulnerability. That's the reason behind me not paying attention on this since it's just a quest function and you can do a looot of stuff without exploiting that.

 

Thanks for the code that, in preview mode it was reeeeeaaaally ugly so I decided to not use it. I'll give it a try and edit my thread :)

Link to comment
Share on other sites

  • 1 month later...

I edited your some functions. Meanwhile congrulations ;)

int console_sys_err(lua_State * L)
{
	if (!lua_isstring(L, 1))
		return 1;
	
	const char * reason = lua_tostring(L, 1);
	sys_err(reason);
	return 0;
}

int console_sys_log(lua_State * L)
{
	if (!lua_isnumber(L, 1) || !lua_isstring(L, 2))
		return 1;
	
	BYTE bLevel = lua_tonumber(L, 1);
	if (bLevel >= 0 && bLevel <= 3)
	{
		const char * reason = lua_tostring(L, 2);
		sys_log(bLevel, reason);
		return 0;		
	}
	return 1;
}

int console_file_backup(lua_State * L)
{
	if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
		return 1;
	
	char szCommand[512];
	snprintf(szCommand, sizeof(szCommand), "tar -u -a -f %s %s", lua_tostring(L, 2), lua_tostring(L, 1));
	system(szCommand);
	return 0;
}

Kind Regards

Ken

  • Love 2

Do not be sorry, be better.

Link to comment
Share on other sites

  • 4 weeks later...

 

I don't remember seeing vanilla setting up standards ;) Actually it's the implementation of newer lua versions. Vanilla did right to change that, I'm just following up on that since you're free to do whatever you want with your source: This includes upgrading lua :)

 

But big thanks for noting that, I'm editing my thread right away! :)

Edited by Metin2 Dev
Core X - External 2 Internal
  • Love 2
Link to comment
Share on other sites

Why Alina doesnt wanna say that shehe is Vanilla?

Vanilla is like Ken that changes 10000 times his avatar

Vanilla changes 1000 times herhis name

 

Nope, sorry :)

I don't know where you get your "facts" but they're simply wrong. I'm still using the same avatar for months now. And leading from changing avatars to changing names is a bit too far fetched, don't you agree? If you still insist on that we can clear that up via pm but I think it's nothing you'd discuss here especially since it doesn't fit the topic :)

 

EDIT: 69 likes, shame on you guys! :D (yes I know, you're allowed to start a flame war about this^^)

  • Love 1
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.