Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Shafer committed Oct 10, 2019
2 parents b340d10 + 8150eac commit 531d4a6
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 38 deletions.
9 changes: 7 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ AC_PATH_PROG(SED, sed, /bin/sed)

AC_PATH_PROG(PATH_SSH, ssh, /usr/bin/ssh)
AC_PATH_PROG(PATH_GZIP, gzip, /usr/bin/gzip)
AC_DEFINE_UNQUOTED(PATH_SSH, ["$PATH_SSH"], [Path to SSH binary])
AC_DEFINE_UNQUOTED(PATH_GZIP, ["$PATH_GZIP"], [Path to GZIP binary])
AC_DEFINE_UNQUOTED(PATH_SSH, ["$PATH_SSH"], [Path to binary])
AC_DEFINE_UNQUOTED(PATH_GZIP, ["$PATH_GZIP"], [Path to binary])

AC_PATH_PROG(PATH_PERL, perl, /usr/local/bin/perl)
AC_PATH_PROG(PATH_PYTHON, python, /usr/local/bin/python)
AC_DEFINE_UNQUOTED(JUISE_PATH_PERL, ["$PATH_PERL"], [Path to binary])
AC_DEFINE_UNQUOTED(JUISE_PATH_PYTHON, ["$PATH_PYTHON"], [Path to binary])

AC_PROG_LN_S

Expand Down
184 changes: 148 additions & 36 deletions juise/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <sys/socket.h>
#include <sys/queue.h>
#include <string.h>
#include <signal.h>

#include <libxml/tree.h>
#include <libxml/dict.h>
Expand All @@ -47,6 +48,7 @@
#include <libjuise/xml/jsio.h>
#include <libjuise/xml/extensions.h>
#include <libjuise/xml/juisenames.h>
#include <libjuise/common/allocadup.h>

#include "juise.h"

Expand Down Expand Up @@ -111,52 +113,29 @@ srv_build_input_doc (lx_document_t *input)
return input;
}

static FILE *
open_script (const char *scriptname, char *full_name, int full_size)
{
char const *extentions[] = { "slax", "xsl", "xslt", "sh", "pl", "", NULL };
char const **cpp;
FILE *fp;
slax_data_node_t *dnp;
typedef struct script_info_s script_info_t;

SLAXDATALIST_FOREACH(dnp, &srv_includes) {
char *dir = dnp->dn_data;
#define SCRIPT_INFO_ARGS \
js_session_t *jsp UNUSED, script_info_t *sip UNUSED, \
FILE *scriptfile UNUSED, \
const char *scriptname UNUSED, const char *full_name UNUSED, \
lx_document_t *input UNUSED

for (cpp = extentions; *cpp; cpp++) {
snprintf(full_name, full_size, "%s/%s%s%s",
dir, scriptname, **cpp ? "." : "", *cpp);
fp = fopen(full_name, "r+");
if (fp)
return fp;
}
}
typedef int (*script_info_func_t)(SCRIPT_INFO_ARGS);

return NULL;
}
struct script_info_s {
const char *si_extension; /* file name extension */
const char *si_exec; /* Path to run the script */
script_info_func_t si_func; /* Function to run the script */
};

static int
srv_run_script (js_session_t *jsp, const char *scriptname,
lx_document_t *input)
run_slax_script (SCRIPT_INFO_ARGS)
{
lx_document_t *scriptdoc;
FILE *scriptfile;
lx_document_t *indoc;
lx_stylesheet_t *script;
lx_document_t *res = NULL;
char full_name[MAXPATHLEN];

script_count += 1;

if (scriptname == NULL)
errx(1, "missing script name");

scriptfile = open_script(scriptname, full_name, sizeof(full_name));
if (scriptfile == NULL) {
trace(trace_file, TRACE_ALL,
"file open failed for script '%s' (%s)",
scriptname, juise_dir ?: JUISE_SCRIPT_DIR);
return TRUE;
}

scriptdoc = slaxLoadFile(scriptname, scriptfile, NULL, 0);
if (scriptdoc == NULL)
Expand Down Expand Up @@ -198,6 +177,139 @@ srv_run_script (js_session_t *jsp, const char *scriptname,
return FALSE;
}

static int
run_exec_script (SCRIPT_INFO_ARGS)
{
int sv[2];
char *argv[3] = { ALLOCADUP(sip->si_exec), ALLOCADUP(full_name), NULL };
sigset_t sigblocked, sigblocked_old;
pid_t pid;

if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
trace(trace_file, TRACE_ALL, "socketpair failed: %m");
return TRUE;
}

sigemptyset(&sigblocked);
sigaddset(&sigblocked, SIGCHLD);
sigprocmask(SIG_BLOCK, &sigblocked, &sigblocked_old);

if ((pid = fork()) == 0) { /* Child process */
sigprocmask(SIG_SETMASK, &sigblocked_old, NULL);

close(sv[1]);
dup2(sv[0], 0);
dup2(sv[0], 1);

int i;
for (i = 3; i < 64; ++i) {
close(i);
}

execv(argv[0], argv);
_exit(1);
}

close(sv[0]); /* Close our side of the other side's socket */

/* Write our output to the child process */
slaxDumpToFd(sv[1], input, FALSE);

/* Close the "write" side of the socket so they see EOF */
shutdown(sv[1], SHUT_WR);

/* Read the results */
xmlDocPtr res = xmlReadFd(sv[1], "script.output",
"UTF-8", XSLT_PARSE_OPTIONS);
if (res) {
trace(trace_file, TRACE_ALL, "no script output");
slaxDumpToFd(fileno(jsp->js_fpout), res, FALSE);
xmlFreeDoc(res);
}

close(sv[1]);
close(sv[0]);

return FALSE;
}

static script_info_t script_info[] = {
{ "slax", NULL, run_slax_script },
{ "xsl", NULL, run_slax_script },
{ "xslt", NULL, run_slax_script },
{ "sh", "/bin/sh", run_exec_script },
#ifdef JUISE_PATH_PERL
{ "perl", JUISE_PATH_PERL, run_exec_script },
{ "pl", JUISE_PATH_PERL, run_exec_script },
#endif /* JUISE_PATH_PERL */
#ifdef JUISE_PATH_PYTHON
{ "python", JUISE_PATH_PYTHON, run_exec_script },
{ "py", JUISE_PATH_PYTHON, run_exec_script },
{ "pyc", JUISE_PATH_PYTHON, run_exec_script },
#endif /* JUISE_PATH_PYTHON */
{ NULL, NULL, NULL }
};

static FILE *
open_script (const char *scriptname, char *full_name, int full_size,
script_info_t **sipp)
{
script_info_t *sip;
FILE *fp;
slax_data_node_t *dnp;

SLAXDATALIST_FOREACH(dnp, &srv_includes) {
char *dir = dnp->dn_data;

for (sip = script_info; sip->si_extension; sip++) {
snprintf(full_name, full_size, "%s/%s%s%s",
dir, scriptname, sip->si_extension[0] ? "." : "",
sip->si_extension);
fp = fopen(full_name, "r+");
if (fp) {
trace(trace_file, TRACE_ALL,
"server: found script file '%s'", full_name);
*sipp = sip;
return fp;
}
}
}

*sipp = NULL;
return NULL;
}

static int
srv_run_script (js_session_t *jsp, const char *scriptname,
lx_document_t *input)
{
int rc;
FILE *scriptfile;
char full_name[MAXPATHLEN];
script_info_t *sip = NULL;

script_count += 1;

if (scriptname == NULL)
errx(1, "missing script name");

trace(trace_file, TRACE_ALL, "server: running script '%s'", scriptname);

scriptfile = open_script(scriptname, full_name, sizeof(full_name), &sip);
if (scriptfile == NULL) {
trace(trace_file, TRACE_ALL,
"file open failed for script '%s' (%s)",
scriptname, juise_dir ?: JUISE_SCRIPT_DIR);
return TRUE;
}

rc = sip->si_func(jsp, sip, scriptfile, scriptname, full_name, input);

fclose(scriptfile);

return rc;
}

void
run_server (int fdin, int fdout, session_type_t stype)
{
Expand Down
4 changes: 4 additions & 0 deletions libjuise/io/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ vlogging_event (int severity, const char *tag, const char *lsname,
case LOG_DEBUG:
if (logging_level < LOG_DEBUG)
return;
break;

case LOG_INFO:
if (logging_level < LOG_INFO)
return;
break;

default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions libjuise/xml/jsio.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ js_mixer_send_simple (js_session_t *jsp, const char *opname, const char *attrs,
jsio_trace("send mixer op '%s' with attrs: '%s', data: '%s'",
opname ?: "<null>", attrs ?: "<null>", data ?: "<null>");

/* Sanity check our arguments */
if (opname == NULL || attrs == NULL || data == NULL)
return -1;

mx_header_t *mhp = (mx_header_t *) buf;
js_mixer_header_build(mhp, len, opname, js_auth_muxer_id);

Expand Down
3 changes: 3 additions & 0 deletions libjuise/xml/xml_escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ xml_escape (char *buf, size_t size, const char *str, boolean attribute,
elen = sizeof(XML_ESCAPE_QUOT) - 1;
break;
}
goto common;

case '\'':
if (attribute) {
entity = XML_ESCAPE_APOS;
elen = sizeof(XML_ESCAPE_APOS) - 1;
break;
}
goto common;

default:
common:
if (XML_IS_BINARY(*cp)) {

/*
Expand Down
12 changes: 12 additions & 0 deletions tests/server/rpc-01.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<hello>
</hello>
]]>]]>
<rpc>
<test-01>
<one>1</one>
<two>2</two>
</test-01>
</rpc>
]]>]]>

12 changes: 12 additions & 0 deletions tests/server/rpc-02.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<hello>
</hello>
]]>]]>
<rpc>
<test-02>
<one>1</one>
<two>2</two>
</test-02>
</rpc>
]]>]]>

12 changes: 12 additions & 0 deletions tests/server/rpc-03.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<hello>
</hello>
]]>]]>
<rpc>
<test-03>
<one>1</one>
<two>2</two>
</test-03>
</rpc>
]]>]]>

12 changes: 12 additions & 0 deletions tests/server/test-01.slax
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version 1.1;

match / {
<chassis-hardware> {
<mumble> {
<dink>;
}
<grumble> {
<splat>;
}
}
}
12 changes: 12 additions & 0 deletions tests/server/test-02.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

cat - > test-02.out
cat <<EOF
<?xml version="1.0"?>
<top>
<grumble/>
</top>
EOF

exit 0

19 changes: 19 additions & 0 deletions tests/server/test-03.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/perl
#

@in = <STDIN>;

open(OUT, ">test-03.out");
print OUT @in;

$out = <<EOF;
<?xml version="1.0"?>
<some-type-of-data>
<details/>
<more-details/>
</some-type-of-data>
EOF

print $out;

exit(0);

0 comments on commit 531d4a6

Please sign in to comment.