Modified cmd.c to prepare for more file access functions and changed

the bind system to keep a restricted flag on binds to prevent the
server from executing restricted commands by binding them to keys.
This commit is contained in:
Brian Koropoff 2002-05-31 05:38:06 +00:00
parent ccf2a0b34a
commit d85a238af2
4 changed files with 69 additions and 38 deletions

View file

@ -378,7 +378,10 @@ typedef struct {
extern keydest_t key_dest;
extern imt_t game_target;
extern char *keybindings[IMT_LAST][QFK_LAST];
extern struct keybind_s {
char *str;
qboolean restricted;
} keybindings[IMT_LAST][QFK_LAST];
extern int keydown[QFK_LAST];
void Key_Event (knum_t key, short unicode, qboolean down);
@ -387,7 +390,7 @@ void Key_Init_Cvars (void);
void Key_WriteBindings (VFile *f);
void Key_ClearStates (void);
const char *Key_GetBinding (imt_t imt, knum_t key);
void Key_SetBinding (imt_t target, knum_t keynum, const char *binding);
void Key_SetBinding (imt_t target, knum_t keynum, const char *binding, qboolean restricted);
const char *Key_KeynumToString (knum_t keynum);

View file

@ -58,7 +58,7 @@ bi_Key_SetBinding (progs_t *pr)
binding = NULL; /* unbind a binding */
}
Key_SetBinding (target, keynum, binding);
Key_SetBinding (target, keynum, binding, false);
}
/*
@ -77,7 +77,7 @@ bi_Key_LookupBinding (progs_t *pr)
const char *keybind = NULL;
for (i = 0; i < QFK_LAST; i++) {
keybind = keybindings[target][i];
keybind = keybindings[target][i].str;
if(keybind == NULL) { continue; }
if(strcmp(keybind, binding) == 0) {
bindnum--;
@ -105,7 +105,7 @@ bi_Key_CountBinding (progs_t *pr)
const char *keybind = NULL;
for (i = 0; i < QFK_LAST; i++) {
keybind = keybindings[target][i];
keybind = keybindings[target][i].str;
if(keybind == NULL) { continue; }
if(strcmp(keybind, binding) == 0) {
res++;

View file

@ -2054,7 +2054,7 @@ Cmd_CmdList_f (void)
cmd_function_t *cmd;
int i;
int show_description = 0;
if (Cmd_Argc () > 1)
show_description = 1;
for (cmd = cmd_functions, i = 0; cmd; cmd = cmd->next, i++) {
@ -2348,21 +2348,23 @@ Cmd_Strlen_f (void)
}
void
Cmd_Writefile_f (void)
{
VFile *file;
char *d, *p, *path;
/* File access */
if (Cmd_Restricted ()) {
Cmd_Error ("writefile: access to restricted command denied.\n");
return;
}
if (Cmd_Argc() != 3) {
Cmd_Error ("writefile: invalid number of arguments.\n");
return;
}
p = path = cmd_activebuffer->argv[1]->processed->str;
/*
Cmd_CollapsePath
Collapses .. and . in a path
and returns 1 if permission
to access it is granted.
Thanks to taniwha for this
mystic routine.
*/
int
Cmd_CollapsePath (char *str)
{
char *d, *p, *path;
p = path = str;
while (*p) {
if (p[0] == '.') {
if (p[1] == '.') {
@ -2396,10 +2398,33 @@ Cmd_Writefile_f (void)
if ( (!path[0])
|| (path[0] == '.' && path[1] == '.' && (path[2] == '/' || path [2] == 0))
|| (path[strlen (path) - 1] =='/') ) {
Cmd_Error ("writefile: invalid path or filename\n");
return 0;
}
return 1;
}
void
Cmd_Writefile_f (void)
{
VFile *file;
char *path;
if (Cmd_Restricted ()) {
Cmd_Error ("writefile: access to restricted command denied.\n");
return;
}
Sys_DPrintf ("writefile: opening %s/%s\n", com_gamedir, Cmd_Argv(1));
if (Cmd_Argc() != 3) {
Cmd_Error ("writefile: invalid number of arguments.\n");
return;
}
path = strdup (Cmd_Argv(1));
SYS_CHECKMEM (path);
if (!Cmd_CollapsePath (path)) {
free (path);
Cmd_Error ("writefile: access to restricted directory/file denied.\n");
return;
}
free(path);
Sys_DPrintf ("writefile: opening %s/%s\n", com_gamedir, path);
if (!(file = Qopen (va("%s/%s", com_gamedir, Cmd_Argv(1)), "w"))) {
Cmd_Error (va ("writefile: could not open file for writing: %s\n", strerror (errno)));
return;

View file

@ -59,7 +59,7 @@ cvar_t *in_bind_imt;
keydest_t key_dest = key_console;
imt_t game_target = IMT_CONSOLE;
char *keybindings[IMT_LAST][QFK_LAST];
struct keybind_s keybindings[IMT_LAST][QFK_LAST];
int keydown[QFK_LAST];
static int keyhelp;
@ -385,8 +385,10 @@ Key_Game (knum_t key, short unicode)
{
const char *kb;
char cmd[1024];
cmd_buffer_t *tbuffer;
kb = Key_GetBinding(game_target, key);
tbuffer = keybindings[game_target][key].restricted ? cmd_legacybuffer : cmd_keybindbuffer;
if (!kb && (game_target > IMT_0))
kb = Key_GetBinding(IMT_0, key);
@ -400,15 +402,15 @@ Key_Game (knum_t key, short unicode)
if (!keydown[key]) {
if (kb[0] == '+') {
snprintf (cmd, sizeof (cmd), "-%s %d\n", kb + 1, key);
Cbuf_AddTextTo (cmd_keybindbuffer, cmd);
Cbuf_AddTextTo (tbuffer, cmd);
}
} else if (keydown[key] == 1) {
if (kb[0] == '+') {
snprintf (cmd, sizeof (cmd), "%s %d\n", kb, key);
Cbuf_AddTextTo (cmd_keybindbuffer, cmd);
Cbuf_AddTextTo (tbuffer, cmd);
} else {
snprintf (cmd, sizeof (cmd), "%s\n", kb);
Cbuf_AddTextTo (cmd_keybindbuffer, cmd);
Cbuf_AddTextTo (tbuffer, cmd);
}
}
return true;
@ -533,7 +535,7 @@ Key_In_Unbind (const char *imt, const char *key)
return;
}
Key_SetBinding (t, b, NULL);
Key_SetBinding (t, b, NULL, false);
}
void
@ -553,11 +555,11 @@ Key_Unbindall_f (void)
for (j = 0; j < IMT_LAST; j++)
for (i = 0; i < QFK_LAST; i++)
Key_SetBinding (j, i, NULL);
Key_SetBinding (j, i, NULL, false);
}
void
Key_In_Bind (const char *imt, const char *key, const char *cmd)
Key_In_Bind (const char *imt, const char *key, const char *cmd, qboolean restricted)
{
int t, b;
@ -581,7 +583,7 @@ Key_In_Bind (const char *imt, const char *key, const char *cmd)
Con_Printf ("%s %s is not bound\n", imt, key);
return;
}
Key_SetBinding (t, b, cmd);
Key_SetBinding (t, b, cmd, restricted);
}
void
@ -614,7 +616,7 @@ Key_In_Bind_f (void)
}
}
Key_In_Bind (imt, key, cmd);
Key_In_Bind (imt, key, cmd, Cmd_Restricted ());
}
void
@ -659,7 +661,7 @@ Key_Bind_f (void)
}
}
Key_In_Bind (imt, key, cmd);
Key_In_Bind (imt, key, cmd, Cmd_Restricted ());
}
void
@ -827,22 +829,23 @@ Key_Init_Cvars (void)
const char *
Key_GetBinding (imt_t imt, knum_t key)
{
return keybindings[imt][key];
return keybindings[imt][key].str;
}
void
Key_SetBinding (imt_t target, knum_t keynum, const char *binding)
Key_SetBinding (imt_t target, knum_t keynum, const char *binding, qboolean restricted)
{
if (keynum == -1)
return;
// free old bindings
if (keybindings[target][keynum]) {
free (keybindings[target][keynum]);
keybindings[target][keynum] = NULL;
if (keybindings[target][keynum].str) {
free (keybindings[target][keynum].str);
keybindings[target][keynum].str = NULL;
}
// allocate memory for new binding
if (binding) {
keybindings[target][keynum] = strdup(binding);
keybindings[target][keynum].str = strdup(binding);
}
keybindings[target][keynum].restricted = restricted;
}