First Commit - Source Code from Reply

This commit is contained in:
vincenzofariello
2024-05-13 12:54:14 +02:00
parent 73e32a5020
commit a15aee1f08
11184 changed files with 1065913 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>
#define SHMSZ 27
#define KEY 5625
#define PERM 0600
void scrivi(void) {
int indirizzo = 0;
char c;
int shmid;
key_t key;
char * shm, * s, * res;
char str[80];
int i;
printf("Inserisci indirizzo.\n");
scanf("%d",&indirizzo);
printf("Indirizzo: %d \n",indirizzo);
//key = KEY;
key = (key_t)indirizzo;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | PERM)) < 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);
}
res = getpass("password:");
s = shm;
int j = strlen(res);
for (i = 0; i <= j; i++)
* s++ = * res++;
* s = NULL;
printf("Scrittura terminata.\n");
exit(0);
}
void cancella(void) {
int indirizzo = 0;
int shmid;
key_t key;
char *shm, *s;
struct shmid_ds * shmid_ds;
int rtrn;
printf("Inserisci indirizzo (x per usare il default).\n");
rtrn = scanf("%d",&indirizzo);
if(rtrn!=0) {
printf("Indirizzo: %d \n",indirizzo);
key = (key_t)indirizzo;
}
else {
printf("Indirizzo: %d \n",KEY);
key = KEY;
}
/*
* Locate the segment.
*/
if ((shmid = shmget(key, SHMSZ, PERM)) < 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);
}
if ((rtrn = shmctl(shmid, IPC_RMID, shmid_ds)) == -1) {
perror("shmctl");
exit(1);
}
printf("Dati cancellati.\n");
exit(0);
}
main(int argc, char * argv[]) {
// int i;
//
// printf("argc = %d\n", argc);
//
// for (i = 0; i < argc; i++)
// printf("argv[%d] = \"%s\"\n", i, argv[i]);
//
// if ((argc!=2) ||
// ((argc==2) && (argv[1][0]!='s') && (argv[1][0]!='c'))) {
// printf("Usage: shm_server [s|c]\n");
// }
// else {
// printf("1\n");
// if (argv[1][0] == 's')
// scrivi();
// else {
// printf("2\n");
// cancella();
// }
// }
cancella();
}

View File

@@ -0,0 +1,4 @@
gcc -fPIC -o libSecUtility.so -shared -I/usr/java14/include -static-libgcc it_valueteam_securityutility_KeyJNIBridge.c

View File

@@ -0,0 +1,4 @@
gcc -o pswd.so pswd.c

View File

@@ -0,0 +1,87 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <jni.h>
#include "it_valueteam_securityutility_KeyJNIBridge.h"
#define SHMSZ 27
//#define KEY 5678
#define KEY 5650
#define PERM 0600
JNIEXPORT jbyteArray JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_readKey
(JNIEnv * env, jobject obj, jint indirizzo){
int shmid;
int i = 0;
key_t key;
char *shm, *s;
jbyteArray result;
char buffer[24] = "12345123451234512345123";
//key = KEY;
key = (key_t)indirizzo;
result = (*env)->NewByteArray(env,24);
if(result==NULL) {
printf("Allocazione fallita.\n");
}
if ((shmid = shmget(key, SHMSZ, PERM)) < 0) {
perror("shmget");
return NULL;
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
return NULL;
}
for(s = shm; i<24; s++) {
buffer[i++] = *s;
}
(*env)->SetByteArrayRegion(env,result,0,24,buffer);
return result;
}
JNIEXPORT void JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_writeKey
(JNIEnv * env, jobject obj, jint indirizzo, jbyteArray chiave) {
int shmid;
key_t key;
char * shm, *s;
int i;
jbyte *start;
//key = KEY;
key = (key_t)indirizzo;
printf("Inizio scrittura.\n");
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | PERM)) < 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);
}
start = (*env)->GetByteArrayElements(env,chiave,NULL);
s = shm;
for(i=0;i<24;i++) {
* s++ = start[i];
}
(*env)->ReleaseByteArrayElements(env, chiave, start, 0);
}

View File

@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class itm_valueteam_securityutility_KeyJNIBridge */
#ifndef _Included_it_valueteam_securityutility_KeyJNIBridge
#define _Included_it_valueteam_securityutility_KeyJNIBridge
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: it_valueteam_securityutility_KeyJNIBridge
* Method: readKey
* Signature: (I)[B
*/
JNIEXPORT jbyteArray JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_readKey
(JNIEnv *, jobject, jint);
/*
* Class: it_valueteam_securityutility_KeyJNIBridge
* Method: writeKey
* Signature: (I[B)V
*/
JNIEXPORT void JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_writeKey
(JNIEnv *, jobject, jint, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <string.h>
main(int argc, char * argv[]) {
char * res;
res = getpass("Inserire valore: \n");
printf("%s",res);
}