From 291b52d50803df0d6ddcb9258cafee01c836eec6 Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Thu, 10 Mar 2016 03:54:12 +0200 Subject: [PATCH] [examples] added misc helpers for client side examples; --- examples/Makefile.am | 2 +- examples/misc.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ examples/misc.h | 29 ++++++++++++++ examples/smpf.h | 1 + examples/smpfc.c | 48 +++++++++++++++++++---- 5 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 examples/misc.c create mode 100644 examples/misc.h diff --git a/examples/Makefile.am b/examples/Makefile.am index 7a20aa9..d9ce209 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -41,7 +41,7 @@ smpfd_SOURCES = helpers.c smpfd.c smpfd_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \ $(LIBUUID_LIBS) $(libsxmp) -lpthread -smpfc_SOURCES = smpfc.c +smpfc_SOURCES = misc.c smpfc.c smpfc_LDADD = $(LIBTDATA_LIBS) $(LIBSEXPR_LIBS) $(OPENSSL_LIBS) \ $(LIBUUID_LIBS) $(libsxmp) -lpthread -lreadline diff --git a/examples/misc.c b/examples/misc.c new file mode 100644 index 0000000..df193fd --- /dev/null +++ b/examples/misc.c @@ -0,0 +1,91 @@ +/* + * Secure X Message Passing Library v2 examples. + * + * (c) Alexander Vdolainen 2013-2015 + * (c) Alexander Vdolainen 2016 + * + * 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 ."; + * + */ + +#include +#include +#include +#include +#include + +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; +} diff --git a/examples/misc.h b/examples/misc.h new file mode 100644 index 0000000..773fb5b --- /dev/null +++ b/examples/misc.h @@ -0,0 +1,29 @@ +/* + * Secure X Message Passing Library v2 examples. + * + * (c) Alexander Vdolainen 2013-2015 + * (c) Alexander Vdolainen 2016 + * + * 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 ."; + * + */ + +#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__ */ diff --git a/examples/smpf.h b/examples/smpf.h index c5be2b8..e698bca 100644 --- a/examples/smpf.h +++ b/examples/smpf.h @@ -2,6 +2,7 @@ * Secure X Message Passing Library v2 examples. * * (c) Alexander Vdolainen 2013-2015 + * (c) Alexander Vdolainen 2016 * * libsxmp is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published diff --git a/examples/smpfc.c b/examples/smpfc.c index 87feeb0..31fa6d9 100644 --- a/examples/smpfc.c +++ b/examples/smpfc.c @@ -34,6 +34,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -41,12 +45,11 @@ #include #include -#include -#include -#include -#include +#include +#include #include "smpf.h" +#include "misc.h" #include "../config.h" #define FULL_PROGRAM_NAME "SMPF (secure message passing file) client" @@ -66,9 +69,25 @@ struct _remote_host { int port; sxhub_t *hub; 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 */ static void __session_free(sxlink_t *l) { @@ -181,14 +200,23 @@ int main(int argc, char **argv) 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 */ if(!rootca || !cert) { fprintf(stderr, "One or more x.509 certificates files not pointed.\n"); __help_print(stderr, argv[0]); return EINVAL; } - if(!login || !password) { - fprintf(stderr, "Login or password not pointed.\n"); + if(!login) { + fprintf(stderr, "Login not pointed.\n"); __help_print(stderr, argv[0]); return EINVAL; } @@ -198,6 +226,10 @@ int main(int argc, char **argv) return EINVAL; } + /* read the password */ + password = exmps_getpass("Password: "); + fprintf(stdout, "\n"); + /* initialize sxmp first */ sxmp_init(); /* init library */ 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, rh->port, i); } else { + /* hooray now we ready to do something */ + __shell(rh->link); sxlink_close(rh->link); /* don't forget to do this */ }