Empress Logo



How to Execute Programs Stored in a Database


Introduction

Example

where module_executeBLOB is Empress PSM function which reads the content of the executable_object (e.g. "Hello World" executable) in table execRepositoryTable and executes it.

The executable hello could be created in the following way:

Let us create a database execRepositoryDB which will serve as a repository for different kind of executables/shell scripts.

Create table execRepositoryTable with the following attributes:

Insert "ls" executable from the system into the table execRepositoryTable

Insert "hello" executable in the table execRepositoryTable

Pseudo Code for an Empress PSM function is given (the full source code is given in the appendix):

APPENDIX

#!/bin/sh

cat > ./hello.c <<EOM
#include <stdio.h>
int main()
{
        printf("Hello World!\n");
        return (0);
}
EOM

cc -o hello hello.c

cat > ./module_executeBLOB.c <<EOM

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include  <usrfns.h>

char    *executeBLOB( char *name, gen_binary *blob )
{
        FILE    *fp;
        char    *rc;
        char    fname[32] = "./";
        int     i;

        rc = mspsm_malloc (128);
        strcat(fname, name);

        if ((fp = fopen ( fname, "w")) == NULL)
        {
                snprintf(rc, 128, "Failed to open file %s", fname);
                return (rc);
        }

        for (i = 0; i < blob->num_segments; i++)
        {
                if (fwrite (blob->segment[i].data, blob->segment[i].data_len, 1, fp) < 1)
                {
                        fclose (fp);
                        snprintf(rc, 128, "Failed to write data to file %s", fname);
                        return (rc);
                }
        }

        chmod(fname, S_IXUSR);
        fclose (fp);

        system(fname);
        remove(fname);
        strcpy(rc, "Success");
        return (rc);
}
EOM

emppsmcc -o module_executeBLOB.dll module_executeBLOB.c


empmkdb execRepositoryDB

empbatch execRepositoryDB <<EOM
CREATE MODULE module_executeBLOB
       FUNCTION executeBLOB( GENERIC CHAR, GENERIC BINARY)
       RETURNS GENERIC CHAR
       EXTERNAL NAME executeBLOB;
END MODULE

UPDATE MODULE module_executeBLOB
FROM './module_executeBLOB.dll';


CREATE TABLE execRepositoryTable (exec_id integer, exec_name char, exec_object bulk);
INSERT INTO TABLE execRepositoryTable VALUES (1, "ls", DIRECT_FROM '/bin/ls');
INSERT INTO TABLE execRepositoryTable VALUES (2, "hello", DIRECT_FROM './hello');
EOM

rm ./hello

empcmd execRepositoryDB "SELECT executeBLOB(exec_name, exec_object) FROM execRepositoryTable WHERE exec_id=2;"