1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/* Copyright (c) 2005 MySQL AB
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "myx_grt_fcgi.h"
static void fcgi_process_output(const char *text, void *user_data)
{
char *output= str_g_replace(g_strdup(text), _br, "<br>");
FCGX_FPrintF(((FCGX_Request *)user_data)->out, output);
g_free(output);
}
static void fcgi_print_404(FCGX_Request *request)
{
FCGX_FPrintF(request->out,
"Content-type: text/html\r\n"
"\r\n"
"<html><head><title>MySQL GRT Shell</title></head>\r\n"
"<body><h2>Error 404 - File not found</h2>\r\n"
"%s\r\n", FCGX_GetParam("SCRIPT_NAME", request->envp));
}
static void fcgi_print_status(FCGX_Request *request)
{
char *content_length;
int len = 0;
char **envp= request->envp;
FCGX_FPrintF(request->out,
"Content-type: text/html\r\n"
"\r\n"
"<html><head><title>MySQL GRT Shell</title></head>"
"<body><h2>MySQL GRT Shell FastCGI</h2>");
content_length = FCGX_GetParam("CONTENT_LENGTH", request->envp);
if (content_length != NULL)
len = strtol(content_length, NULL, 10);
if (len <= 0)
{
FCGX_FPrintF(request->out, "No data from standard input.<p>\n");
}
else
{
int i, ch;
FCGX_FPrintF(request->out, "Standard input:<br>\n<pre>\n");
for (i = 0; i < len; i++) {
if ((ch = FCGX_GetChar(request->in)) < 0) {
FCGX_FPrintF(request->out,
"Error: Not enough bytes received on standard input<p>\n");
break;
}
FCGX_PutChar(ch, request->out);
}
FCGX_FPrintF(request->out, "\n</pre><p>\n");
}
FCGX_FPrintF(request->out, "FCGI Environment Variables:<br>\n<pre>\n");
for( ; *envp != NULL; envp++) {
FCGX_FPrintF(request->out, "%s\n", *envp);
}
FCGX_FPrintF(request->out, "</pre><p>\n");
}
void myx_grt_start_fcgi(MYX_GRT *grt, int port, char **allowed_modules,
unsigned int allowed_modules_num)
{
FCGX_Request request;
int sock;
char port_string[7];
if (FCGX_Init())
{
g_message("Init failed.\n");
return;
}
g_snprintf(port_string, 7, ":%i", port);
sock= FCGX_OpenSocket(port_string, 50);
if (sock < 0)
{
g_message("Error trying to open socket %i. (%d).\n", port, sock);
return;
}
g_message("Start listening at port %i.\n", port);
FCGX_InitRequest(&request, sock, 0);
for (;;)
{
int rc = FCGX_Accept_r(&request);
if (rc < 0)
break;
myx_grt_set_output_callback(grt, &request, &fcgi_process_output);
if (strcmp2(FCGX_GetParam("SCRIPT_NAME", request.envp), "/info.lsp") == 0)
{
fcgi_print_status(&request);
}
else if (strcmp2(FCGX_GetParam("SCRIPT_NAME", request.envp), "/shell.lsp") == 0)
{
char cmd_buffer[256];
int len;
FCGX_FPrintF(request.out,
"Content-type: text/html\r\n"
"\r\n"
"<html>\r\n"
"<head>\r\n"
" <title>MySQL GRT Shell</title>\r\n"
" <style type=\"text/css\">\r\n"
" <!--\r\n"
" * { background-color: #000000; color:#888888; font-size: small;\r\n"
" font-family: \"Bitstream Vera Sans Mono\",Courier New,Courier; font-weight: bold} \r\n"
" input { border-width: 0; border-style: none; } \r\n"
" .output { margin-left: 16px } \r\n"
" -->\r\n"
" </style>\r\n"
"</head>\r\n"
"<body onLoad=\"javascript: document.f.cmd.focus();\" >\r\n"
"<div>");
len= FCGX_GetStr(cmd_buffer, 255, request.in);
if (len > 0)
{
char cmd[256];
cmd_buffer[len]= 0;
value_of_str(cmd, cmd_buffer);
FCGX_FPrintF(request.out,
"> %s</div>"
"<div class=\"output\">",
cmd);
myx_grt_shell_execute(grt, cmd);
}
FCGX_FPrintF(request.out,
"</div>\r\n"
"<form name=\"f\" action=\"/shell.lsp\" method=\"post\" enctype=\"text/plain\">\r\n"
"> <input name=\"cmd\" type=\"text\" size=\"80\">\r\n"
"</form>\r\n");
}
else
{
fcgi_print_404(&request);
}
myx_grt_set_output_callback(grt, NULL, NULL);
FCGX_FPrintF(request.out, "</body></html>");
FCGX_Finish_r(&request);
}
g_message("Exiting.\n");
return;
}
|