Page 1 of 1

communicating via shared memory

Posted: Tue Sep 10, 2019 6:24 am
by Otto
Dear Antonio,
I found a C-program which I could use for communicating via shared memory: shm_server.c, shm_client.c
Can we use these programs in mod harbour. I would like to store the encryption key for "data at rest encryption" in server memory.
I have this functionality in my PHP programs.

Thank you in advance
Otto

shm_server.c
-- simply creates the string and shared memory portion.
shm_client.c
-- attaches itself to the created shared memory portion and uses the string (printf.

Code: Select all

shm_server.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ     27

main()
{
    char c;
    int shmid;
    key_t key;
    char *shm, *s;

    /*
     * We'll name our shared memory segment
     * "5678".
     */
    key = 5678;

    /*
     * Create the segment.
     */
    if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
        perror("shmget");
        exit(1);
    }

    /*
     * Now we attach the segment to our data space.
     */
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

    /*
     * Now put some things into the memory for the
     * other process to read.
     */
    s = shm;

    for (c = 'a'; c <= 'z'; c++)
        *s++ = c;
    *s = NULL;

    /*
     * Finally, we wait until the other process 
     * changes the first character of our memory
     * to '*', indicating that it has read what 
     * we put there.
     */
    while (*shm != '*')
        sleep(1);

    exit(0);
}

shm_client.c

/*
 * shm-client - client program to demonstrate shared memory.
 */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SHMSZ     27

main()
{
    int shmid;
    key_t key;
    char *shm, *s;

    /*
     * We need to get the segment named
     * "5678", created by the server.
     */
    key = 5678;

    /*
     * Locate the segment.
     */
    if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
        perror("shmget");
        exit(1);
    }

    /*
     * Now we attach the segment to our data space.
     */
    if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        exit(1);
    }

    /*
     * Now read what the server put in the memory.
     */
    for (s = shm; *s != NULL; s++)
        putchar(*s);
    putchar('\n');

    /*
     * Finally, change the first character of the 
     * segment to '*', indicating we have read 
     * the segment.
     */
    *shm = '*';

    exit(0);
}
My PHP code:

Code: Select all

<?php


// Erstelle einen 100 Byte grossen gemeinsam genutzten Speicherblock
// mit mit der System_ID if 0xff3
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if(!$shm_id) {
        echo "Konnte kein gemeinsames Speichersegment erstellen\n";
}

// Hole die Groesse des gemeinsamen Speicherblocks
$shm_size = shmop_size($shm_id);
echo "SHM Block mit: ".$shm_size. " Bytes wurde erstellt.\n";

// Teststring in den gemeinsamen Speicher schreiben
$shm_bytes_written = shmop_write($shm_id, "mysecretKey", 0);
if($shm_bytes_written != strlen("mein gemeinsamer Speicher")) {
        echo "Konnte nicht den gesamten String schreiben\n";
}

// Den Teststring wieder auslesen
$my_string = shmop_read($shm_id, 0, $shm_size);
if(!$my_string) {
        echo "Konnte nicht aus dem gemeinsamen Speicher lesen\n";
}
echo "Die Daten im gemeinsamen Speicher waren: ".$my_string."\n";

// Den Speicherblock loeschen und den gemeinsamen Speicher schliessen
if(!shmop_delete($shm_id)) {
        echo "Konnte den gemeinsamen Speicherblock nicht zum Loeschen markieren.";
}
shmop_close($shm_id);

?>


 

Re: communicating via shared memory

Posted: Tue Sep 10, 2019 9:08 am
by Antonio Linares
Dear Otto,

Yesterday I already reviewed that code in google.

They use a function emalloc() which we don't know yet how to implement it.

Have you tried my getList suggestion ?

Re: communicating via shared memory

Posted: Tue Sep 10, 2019 9:41 pm
by Otto
Otto, you may try this using mod_harbour:
AAdd( getList, {=>} )
ATail( getList )[ "mykey" ] = "myvalue"
to recover it:
ATail( getList )[ "mykey" ]

freom SLACK

I will test this now.

Re: communicating via shared memory

Posted: Tue Sep 10, 2019 10:23 pm
by Otto
Dear Antonio,
I tested but it is not working.
Best regards
Otto

Re: communicating via shared memory

Posted: Wed Sep 11, 2019 4:01 am
by Antonio Linares
Another choice is to create a file in the server

You may use MemoWrit() and MemoRead() for a test

Re: communicating via shared memory

Posted: Wed Sep 11, 2019 6:34 am
by Otto
Dear Antonio,
file is no option.
I need the key for encryption "data at rest encryption".
In my WORDPRESS DMS I secure the data inside MariaDB through encryption.
And I do not want that the key is on the same server. If someone gets access to the server or a backup copy can read the data.
Therefore when I start Apache afterwards I call a php file and insert the key manually.
I changed WORDPRESS function <function require_wp_db() > like this:
I would like to use my mod harbour docklands in future as DMS system.
But first I have to resolve "data at rest encryption".
Best regards
Otto

Code: Select all


/**
 * Load the database class file and instantiate the `$wpdb` global.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb The WordPress database class.
 */
function require_wp_db() {
    global $wpdb;


$shm_id = shmop_open(0xff3, "c", 0644, 100);

$shm_size = shmop_size($shm_id);

$my_string = shmop_read($shm_id, 0, $shm_size);

shmop_close($shm_id);

    require_once( ABSPATH . WPINC . '/wp-db.php' );
    if ( file_exists( WP_CONTENT_DIR . '/db.php' ) )
        require_once( WP_CONTENT_DIR . '/db.php' );

    if ( isset( $wpdb ) ) {
        return;
    
$my_string = trim($my_string);
    $wpdb = new wpdb( DB_USER, $my_string , DB_NAME, DB_HOST );
}


 

Re: communicating via shared memory

Posted: Wed Sep 11, 2019 2:49 pm
by Otto
Dear Antonio,
crypt is working fine.
Now the only problem left is how to hide cPW in case someone gets physically access to the server.
It would be great if you could provide a solution to store the password into server memory.
Best regards
Otto

I use:

Code: Select all

to read the documents:
local cPW   := "password"
cCryptText := MemoRead("c:\www\htdocs\modharbour_samples\docklands\data\" + aDir[I,1])
cCryptText := Crypt( cCryptText, cPW)
            

to save the documents:
cData  := Crypt( cData, cPW)
memowrit( cArgs, cData  )


Image

Re: communicating via shared memory

Posted: Wed Sep 11, 2019 4:07 pm
by Otto
Dear Antonio,
I found these functions in harbour manual.
But I get:

Error: Unknown or unregistered symbol

PEEKBYTE() Reads a byte from memory
PEEKSTR() Reads a byte sequence from memory
PEEKWORD() Reads a 16-bit word from memory


POKEBYTE() Writes a byte to memory
POKEWORD() Writes a 16-bit word to memory
PPCBUFTYP() Determines the communication buffer type


? PEEKBYTE(1000, 2000) // Segment 1000d, Offset 2000d
The AT BIOS copyright is found at the F000:0h address. Therefore, each
byte is repeated and CharOdd() is used to display it. It reads to the
first Chr(0):

? CharOdd(PEEKSTR("F000", 0)) // "19xx, 19xx Copyright...."

Maybe these could help.
Best regards
Otto