[examples] added misc helpers for client side examples;

v0.5.xx
Alexander Vdolainen 9 years ago
parent 710bae75e1
commit 291b52d508

@ -41,7 +41,7 @@ smpfd_SOURCES = helpers.c smpfd.c
smpfd_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \ smpfd_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \
$(LIBUUID_LIBS) $(libsxmp) -lpthread $(LIBUUID_LIBS) $(libsxmp) -lpthread
smpfc_SOURCES = smpfc.c smpfc_SOURCES = misc.c smpfc.c
smpfc_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \ smpfc_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \
$(LIBUUID_LIBS) $(libsxmp) -lpthread -lreadline $(LIBUUID_LIBS) $(libsxmp) -lpthread -lreadline

@ -0,0 +1,91 @@
/*
* Secure X Message Passing Library v2 examples.
*
* (c) Alexander Vdolainen 2013-2015 <avdolainen@gmail.com>
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsxmp 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int exmps_getaddrs(const char *cline, char **host, char **login, int *port)
{
int p = 0;
char *tb = NULL, *h, *ttb;
tb = strchr(cline, '@');
if(!tb) return 0;
h = strndup(cline, strlen(cline) - strlen(tb));
if(!h) return 0;
else p++;
*host = h;
tb += sizeof(char);
if(!(ttb = strchr(tb, ':'))) h = strdup(tb);
else h = strndup(tb, strlen(tb) - strlen(ttb));
if(!h) return 0;
p++; *login = h;
if(ttb) {
ttb += sizeof(char);
*port = atoi(ttb);
p++;
}
return p;
}
/*
* simple function to read a password.
*/
char *exmps_getpass(const char *promt)
{
char *pass = malloc(64);
int fd = fileno(stdin), in = 0, c;
struct termios tio;
fprintf(stdout, "%s", promt);
if(!pass) return NULL;
else memset(pass, 0, 64);
/* set the flag to disable echoing */
tcgetattr(fd, &tio);
tio.c_lflag &= ~ECHO;
tcsetattr(fd, 0, &tio);
/* read stupid line */
while(in <= 64) {
c = getchar();
if(c == '\n') break;
else {
pass[in] = c;
in++;
}
}
/* enable echo again */
tio.c_lflag |= ECHO;
tcsetattr(fd, 0, &tio);
return pass;
}

@ -0,0 +1,29 @@
/*
* Secure X Message Passing Library v2 examples.
*
* (c) Alexander Vdolainen 2013-2015 <avdolainen@gmail.com>
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsxmp 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*
*/
#ifndef __SXMP_EXAMPLES_MISC_H__
#define __SXMP_EXAMPLES_MISC_H__
int exmps_getaddrs(const char *, char **, char **, int *);
char *exmps_getpass(const char *);
#endif /* __SXMP_EXAMPLES_MISC_H__ */

@ -2,6 +2,7 @@
* Secure X Message Passing Library v2 examples. * Secure X Message Passing Library v2 examples.
* *
* (c) Alexander Vdolainen 2013-2015 <avdolainen@gmail.com> * (c) Alexander Vdolainen 2013-2015 <avdolainen@gmail.com>
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
* *
* libsxmp is free software: you can redistribute it and/or modify it * libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published * under the terms of the GNU Lesser General Public License as published

@ -34,6 +34,10 @@
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <execinfo.h> #include <execinfo.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <pthread.h>
#include <stdint.h>
#include <getopt.h>
#include <errno.h>
#include <tdata/usrtc.h> #include <tdata/usrtc.h>
#include <tdata/list.h> #include <tdata/list.h>
@ -41,12 +45,11 @@
#include <sxmp/limits.h> #include <sxmp/limits.h>
#include <sxmp/sxmp.h> #include <sxmp/sxmp.h>
#include <pthread.h> #include <readline/readline.h>
#include <stdint.h> #include <readline/history.h>
#include <getopt.h>
#include <errno.h>
#include "smpf.h" #include "smpf.h"
#include "misc.h"
#include "../config.h" #include "../config.h"
#define FULL_PROGRAM_NAME "SMPF (secure message passing file) client" #define FULL_PROGRAM_NAME "SMPF (secure message passing file) client"
@ -66,9 +69,25 @@ struct _remote_host {
int port; int port;
sxhub_t *hub; sxhub_t *hub;
sxlink_t *link; sxlink_t *link;
struct _session *session;
}; };
/* session processing itself */
static void __shell(sxlink_t *link)
{
struct _session *s = (struct _session *)sxlink_getpriv(link);
char *line;
for(;;) {
line = readline("$>");
if(line && *line) add_history(line);
else break;
if(!strcmp(line, "exit")) break;
}
return;
}
/* sxmp stuff below */ /* sxmp stuff below */
static void __session_free(sxlink_t *l) static void __session_free(sxlink_t *l)
{ {
@ -181,14 +200,23 @@ int main(int argc, char **argv)
return EINVAL; return EINVAL;
} }
/* TODO: better check */
exmps_getaddrs(cline, &login, &host, &port);
if(!host) {
fprintf(stderr, "Host not pointed.\n");
__help_print(stderr, argv[0]);
return EINVAL;
}
/* ok validate all before doing any stuff */ /* ok validate all before doing any stuff */
if(!rootca || !cert) { if(!rootca || !cert) {
fprintf(stderr, "One or more x.509 certificates files not pointed.\n"); fprintf(stderr, "One or more x.509 certificates files not pointed.\n");
__help_print(stderr, argv[0]); __help_print(stderr, argv[0]);
return EINVAL; return EINVAL;
} }
if(!login || !password) { if(!login) {
fprintf(stderr, "Login or password not pointed.\n"); fprintf(stderr, "Login not pointed.\n");
__help_print(stderr, argv[0]); __help_print(stderr, argv[0]);
return EINVAL; return EINVAL;
} }
@ -198,6 +226,10 @@ int main(int argc, char **argv)
return EINVAL; return EINVAL;
} }
/* read the password */
password = exmps_getpass("Password: ");
fprintf(stdout, "\n");
/* initialize sxmp first */ /* initialize sxmp first */
sxmp_init(); /* init library */ sxmp_init(); /* init library */
lhub = sxhub_create(); /* create sxhub for link creation */ lhub = sxhub_create(); /* create sxhub for link creation */
@ -220,6 +252,8 @@ int main(int argc, char **argv)
fprintf(stderr, "Failed to create link to %s@%s:%d with %d.\n", rh->login, rh->hostname, fprintf(stderr, "Failed to create link to %s@%s:%d with %d.\n", rh->login, rh->hostname,
rh->port, i); rh->port, i);
} else { } else {
/* hooray now we ready to do something */
__shell(rh->link);
sxlink_close(rh->link); /* don't forget to do this */ sxlink_close(rh->link); /* don't forget to do this */
} }

Loading…
Cancel
Save