aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2015-10-17 18:11:00 +0200
committertoni <toni@devlap.local>2015-10-20 11:16:20 +0200
commit60f9330c35085d067a1c491714d7de49d2120b76 (patch)
tree03380b684d801699616e14f4d8e396ffc6e10918
parenta108c1aec43d1f2a5dac0984a5fe9aa4b1c74b91 (diff)
parentd7071577be3f49b964c4d234024bf62328d0209d (diff)
Merge branch 'master' of raspberrypi.local:/home/git/naskpass
-rw-r--r--.gitignore1
-rw-r--r--main.c29
-rw-r--r--tests/Makefile2
-rw-r--r--tests/mqtest.c49
-rw-r--r--ui.c43
-rw-r--r--ui.h3
-rw-r--r--ui_nwindow.c87
-rw-r--r--ui_nwindow.h8
8 files changed, 167 insertions, 55 deletions
diff --git a/.gitignore b/.gitignore
index 4b8fd3a..3e2ffcd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
/tests/producer
/tests/consumer
/tests/semtest
+/tests/mqtest
*.swp
diff --git a/main.c b/main.c
index c40265d..b4a4fdb 100644
--- a/main.c
+++ b/main.c
@@ -65,6 +65,7 @@ run_cryptcreate(char *pass, char *crypt_cmd)
if (crypt_cmd == NULL || pass == NULL) return (-1);
asprintf(&cmd, "echo '%s' | %s", pass, crypt_cmd);
retval = system(cmd);
+ free(cmd);
return (retval);
}
@@ -95,17 +96,22 @@ main(int argc, char **argv)
signal(SIGTERM, sigfunc);
signal(SIGKILL, sigfunc);
+ mq_attr.mq_flags = 0;
+ mq_attr.mq_msgsize = MAX_PASSWD_LEN;
+ mq_attr.mq_maxmsg = 3;
+ mq_attr.mq_curmsgs = 0;
+
if ( clock_gettime(CLOCK_REALTIME, &ts_sem_input) == -1 ) {
fprintf(stderr, "%s: clock get time error: %d (%s)\n", argv[0], errno, strerror(errno));
goto error;
}
if ( (sp_ui = sem_open(SEM_GUI, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED ||
(sp_input = sem_open(SEM_INP, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0)) == SEM_FAILED ||
-(mq_passwd = mq_open(MSQ_PWD, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, NULL)) == (mqd_t) -1 ) {
+(mq_passwd = mq_open(MSQ_PWD, O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &mq_attr)) == (mqd_t) -1 ) {
if ( errno == EEXIST ) {
fprintf(stderr, "%s: already started?\n", argv[0]);
} else {
- fprintf(stderr, "%s: can not create semaphore: %d (%s)\n", argv[0], errno, strerror(errno));
+ fprintf(stderr, "%s: can not create semaphore/message queue: %d (%s)\n", argv[0], errno, strerror(errno));
}
goto error;
}
@@ -154,26 +160,27 @@ main(int argc, char **argv)
goto error;
}
+ sem_post(sp_ui);
if ((child = fork()) == 0) {
/* child */
fclose(stderr);
/* Slave process: TUI */
- sem_post(sp_ui);
do_ui();
} else if (child > 0) {
/* parent */
fclose(stdin);
fclose(stdout);
/* Master process: mainloop (read passwd from message queue or fifo and exec cryptcreate */
- while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) {
- if ( sem_getvalue(sp_input, &i_sval) == 0 && i_sval > 0 ) {
- if (read(ffd, pbuf, MAX_PASSWD_LEN) > 0) {
- if (run_cryptcreate(pbuf, crypt_cmd) != 0) {
- fprintf(stderr, "cryptcreate error\n");
- }
+ while ( (sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0) || (sem_getvalue(sp_input, &i_sval) == 0 && i_sval > 0) ) {
+ if (read(ffd, pbuf, MAX_PASSWD_LEN) >= 0) {
+ if (run_cryptcreate(pbuf, crypt_cmd) != 0) {
+ fprintf(stderr, "cryptcreate error\n");
+ }
+ } else if ( mq_receive(mq_passwd, pbuf, MAX_PASSWD_LEN, NULL) >= 0 ) {
+ if (run_cryptcreate(pbuf, crypt_cmd) != 0) {
+ fprintf(stderr, "cryptcreate error\n");
}
- } else if ( mq_receive(mq_passwd, pbuf, MAX_PASSWD_LEN, NULL) > 0 ) {
-exit(77);
+ sem_wait(sp_input);
}
usleep(100000);
}
diff --git a/tests/Makefile b/tests/Makefile
index 22a5469..5a041e6 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,5 +1,5 @@
CFLAGS = -Wall
-LDFLAGS = -lpthread
+LDFLAGS = -lpthread -lrt
CC = gcc
SOURCES = $(wildcard *.c)
BINARIES = $(patsubst %.c,%,$(SOURCES))
diff --git a/tests/mqtest.c b/tests/mqtest.c
new file mode 100644
index 0000000..e9bc8e7
--- /dev/null
+++ b/tests/mqtest.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <mqueue.h>
+
+#include <assert.h>
+
+
+static mqd_t mq_test;
+static const size_t bufsiz = 256;
+
+int main(int argc, char **argv)
+{
+ struct mq_attr m_attr;
+ char buf[bufsiz], recv[bufsiz];
+ unsigned int prio;
+ ssize_t sz_recv;
+
+ memset(buf, '\0', bufsiz);
+ memset(recv, '\0', bufsiz);
+ if (argc > 1)
+ strncpy(buf, argv[1], bufsiz-1);
+
+ m_attr.mq_flags = 0;
+ m_attr.mq_msgsize = bufsiz;
+ m_attr.mq_maxmsg = 10;
+ m_attr.mq_curmsgs = 0;
+
+ mq_unlink("/testmq");
+ assert( (mq_test = mq_open( "/testmq", O_NONBLOCK | O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG, &m_attr )) != (mqd_t)-1 );
+ assert( mq_getattr(mq_test, &m_attr) == 0 );
+ printf("flags.........: %ld\n"
+ "maxmsg........: %ld\n"
+ "msgsize.......: %ld\n"
+ "curmsg........: %ld\n",
+ m_attr.mq_flags, m_attr.mq_maxmsg, m_attr.mq_msgsize, m_attr.mq_curmsgs);
+
+ strcpy(buf, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ");
+ assert ( mq_send(mq_test, buf, bufsiz, 0) == 0 );
+ assert ( (sz_recv = mq_receive(mq_test, recv, bufsiz, &prio)) > 0 );
+
+ printf("SENT(%03lu bytes): %s\n", (long unsigned int) strlen(buf), buf);
+ printf("RECV(%03lu bytes): %s\n", (long unsigned int) sz_recv, recv);
+
+ return 0;
+}
+
diff --git a/ui.c b/ui.c
index 9d0abae..99167a5 100644
--- a/ui.c
+++ b/ui.c
@@ -29,6 +29,10 @@
#define PASSWD_HEIGHT 5
#define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6)
#define PASSWD_YRELPOS (unsigned int)(PASSWD_HEIGHT / 2) + 1
+#define INFOWND_WIDTH 25
+#define INFOWND_HEIGHT 3
+#define INFOWND_XRELPOS (unsigned int)(INFOWND_WIDTH / 2) - (INFOWND_WIDTH / 6)
+#define INFOWND_YRELPOS (unsigned int)(INFOWND_HEIGHT / 2) + 1
#define STRLEN(s) (sizeof(s)/sizeof(s[0]))
@@ -37,7 +41,6 @@ static unsigned int max_x, max_y;
static WINDOW *wnd_main;
static struct nask_ui *nui = NULL;
static pthread_t thrd;
-static bool active;
static unsigned int atmout = APP_TIMEOUT;
static pthread_cond_t cnd_update = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mtx_update = PTHREAD_MUTEX_INITIALIZER;
@@ -123,7 +126,7 @@ do_ui_update(bool timed_out)
static void *
ui_thrd(void *arg)
{
- int cnd_ret;
+ int cnd_ret, i_sval;
struct timeval now;
struct timespec wait;
@@ -133,17 +136,14 @@ ui_thrd(void *arg)
wait.tv_nsec = now.tv_usec * 1000;
do_ui_update(true);
sem_post(&sem_rdy);
- while (active == true) {
+ while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) {
pthread_mutex_unlock(&mtx_busy);
cnd_ret = pthread_cond_timedwait(&cnd_update, &mtx_update, &wait);
if (cnd_ret == ETIMEDOUT) {
wait.tv_sec += UILOOP_TIMEOUT;
}
pthread_mutex_lock(&mtx_busy);
- if (--atmout == 0) active = false;
- if (active == false) {
- break;
- }
+ if (--atmout == 0) sem_trywait(sp_ui);
do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) );
}
pthread_mutex_unlock(&mtx_busy);
@@ -167,6 +167,7 @@ init_ui(void)
init_pair(1, COLOR_RED, COLOR_WHITE);
init_pair(2, COLOR_WHITE, COLOR_BLACK);
init_pair(3, COLOR_BLACK, COLOR_WHITE);
+ init_pair(4, COLOR_YELLOW, COLOR_RED);
raw();
keypad(stdscr, TRUE);
noecho();
@@ -185,25 +186,12 @@ free_ui(void)
static int
run_ui_thrd(void) {
- pthread_mutex_lock(&mtx_busy);
- active = true;
- pthread_cond_signal(&cnd_update);
- pthread_mutex_unlock(&mtx_busy);
return (pthread_create(&thrd, NULL, &ui_thrd, NULL));
}
-void
-stop_ui(void)
-{
- pthread_mutex_lock(&mtx_busy);
- active = false;
- pthread_mutex_unlock(&mtx_busy);
-}
-
static int
stop_ui_thrd(void)
{
- stop_ui();
return (pthread_join(thrd, NULL));
}
@@ -212,7 +200,8 @@ mq_passwd_send(char *passwd, size_t len)
{
struct mq_attr m_attr;
- if (mq_send(mq_passwd, "hellomq", 7, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) {
+ sem_post(sp_input);
+ if (mq_send(mq_passwd, passwd, len, 0) == 0 && mq_getattr(mq_passwd, &m_attr) == 0) {
return m_attr.mq_curmsgs;
}
memset(passwd, '\0', len);
@@ -278,7 +267,7 @@ do_ui(void)
asprintf(&title, "/* %s-%s */", PKGNAME, VERSION);
sp_ui = sem_open(SEM_GUI, 0, 0, 0);
sp_input = sem_open(SEM_INP, 0, 0, 0);
- mq_passwd = mq_open(MSQ_PWD, O_WRONLY, S_IWUSR, NULL);
+ mq_passwd = mq_open(MSQ_PWD, O_WRONLY, 0, NULL);
if ( sem_init(&sem_rdy, 0, 0) == -1 || !sp_ui || !sp_input || mq_passwd == (mqd_t)-1 ) {
perror("init semaphore");
goto error;
@@ -288,12 +277,15 @@ do_ui(void)
heartbeat = init_anic(0, 0, A_BOLD | COLOR_PAIR(1), "[%c]");
higher = init_statusbar(0, max_x, A_BOLD | COLOR_PAIR(3), NULL);
lower = init_statusbar(max_y - 1, max_x, COLOR_PAIR(3), lower_statusbar_update);
- infownd = init_txtwindow(10, 10, 25, 8, COLOR_PAIR(3), infownd_update);
+ infownd = init_txtwindow((unsigned int)(max_x / 2)-INFOWND_XRELPOS, (unsigned int)(max_y / 2)-INFOWND_YRELPOS, INFOWND_WIDTH, INFOWND_HEIGHT, COLOR_PAIR(4), COLOR_PAIR(4) | A_BOLD, infownd_update);
register_input(NULL, pw_input);
register_statusbar(higher);
register_statusbar(lower);
register_anic(heartbeat);
+ register_txtwindow(infownd);
+ set_txtwindow_title(infownd, "WARNUNG");
+ set_txtwindow_text(infownd, "String0...............\nString1..................\nString2.....");
activate_input(wnd_main, pw_input);
set_statusbar_text(higher, title);
if (run_ui_thrd() != 0) {
@@ -301,7 +293,7 @@ do_ui(void)
}
sem_wait(&sem_rdy);
wtimeout(wnd_main, 1000);
- while ( active && sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) {
+ while ( sem_getvalue(sp_ui, &i_sval) == 0 && i_sval > 0 ) {
if ((key = wgetch(wnd_main)) == '\0') {
break;
}
@@ -309,7 +301,7 @@ do_ui(void)
continue;
}
pthread_mutex_lock(&mtx_busy);
- active = process_key(key, pw_input, wnd_main);
+ if ( process_key(key, pw_input, wnd_main) == false ) sem_trywait(sp_ui);
activate_input(wnd_main, pw_input);
do_ui_update(false);
pthread_mutex_unlock(&mtx_busy);
@@ -327,7 +319,6 @@ do_ui(void)
free_txtwindow(infownd);
free_ui();
ret = DOUI_OK;
- sem_trywait(sp_ui);
mq_close(mq_passwd);
error:
if (title) free(title);
diff --git a/ui.h b/ui.h
index a34ca10..2174812 100644
--- a/ui.h
+++ b/ui.h
@@ -54,7 +54,4 @@ free_ui(void);
int
do_ui(void);
-void
-stop_ui(void);
-
#endif
diff --git a/ui_nwindow.c b/ui_nwindow.c
index 7098097..0b6e13b 100644
--- a/ui_nwindow.c
+++ b/ui_nwindow.c
@@ -6,7 +6,7 @@
struct txtwindow *
-init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, window_func cb_update)
+init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update)
{
struct txtwindow *a = calloc(1, sizeof(struct txtwindow));
@@ -17,9 +17,10 @@ init_txtwindow(unsigned int x, unsigned int y, unsigned int width, unsigned int
a->scrollable = false;
a->title_len = INITIAL_TITLE_LEN;
a->text_len = INITIAL_TEXT_LEN;
- a->title = calloc(a->title_len, sizeof(char));
- a->text = calloc(a->text_len, sizeof(char));
+ a->title = calloc(a->title_len+1, sizeof(char));
+ a->text = calloc(a->text_len+1, sizeof(char));
a->attrs = attrs;
+ a->text_attrs = text_attrs;
a->window_func = cb_update;
return (a);
}
@@ -36,10 +37,42 @@ free_txtwindow(struct txtwindow *a)
free(a);
}
+static void
+print_wnd(struct txtwindow *a)
+{
+ int i, x = a->x, y = a->y, w = a->width, h = a->height;
+ char tmp[a->width+1];
+
+ attron(a->attrs);
+ /* print window surface */
+ memset(tmp, ' ', a->width);
+ tmp[a->width] = '\0';
+ for (i = y-1; i < y+h+2; i++)
+ mvprintw(i, x, tmp);
+ /* print window border */
+ mvhline(y-2, x, 0, w);
+ mvhline(y+h+2, x, 0, w);
+ mvvline(y-1, x-1, 0, h+3);
+ mvvline(y-1, x+w, 0, h+3);
+ /* print window border edges */
+ mvaddch(y-2, x-1, ACS_ULCORNER);
+ mvaddch(y+2+h, x-1, ACS_LLCORNER);
+ mvaddch(y-2, x+w, ACS_URCORNER);
+ mvaddch(y+2+h, x+w, ACS_LRCORNER);
+ /* print window title */
+ attroff(a->attrs);
+ attron(a->text_attrs);
+ mvprintw(y-2, (float)x+(float)(w/2)-(float)(a->title_len*2/3), "[ %s ]", a->title);
+ /* print windows text */
+ attroff(a->text_attrs);
+}
+
int
txtwindow_cb(WINDOW *win, void *data, bool timedout)
{
struct txtwindow *a = (struct txtwindow *) data;
+
+ print_wnd(a);
return (UICB_OK);
}
@@ -49,22 +82,52 @@ register_txtwindow(struct txtwindow *a)
register_ui_elt(txtwindow_cb, (void *) a, NULL);
}
-static inline size_t
+static size_t
__do_textcpy(char **p_dest, size_t sz_dest, const char *p_src, size_t sz_src)
{
+ size_t cursiz = sz_dest;
+
if (sz_src > sz_dest) {
- *p_dest = (char *) realloc(*p_dest, sz_dest * sizeof(char));
+ *p_dest = (char *) realloc(*p_dest, (sz_src+1) * sizeof(char));
+ cursiz = sz_src;
}
- memset(*p_dest, '\0', sz_dest);
- return sz_dest;
+ memset(*p_dest, '\0', (cursiz+1) * sizeof(char));
+ memcpy(*p_dest, p_src, (cursiz+1) * sizeof(char));
+ return sz_src;
}
-void
-set_txtwindow_text(struct txtwindow *a, const char *text)
+static char **
+__do_textadjust(struct txtwindow *a, char *text)
{
- size_t len = strlen(text);
+ int i = 0, rows = (int)(strlen(text) / a->width)+1;
+ char **adj_text = calloc(rows+1, sizeof(char *));
+ char *p_strtok = strdupa(text), *tok;
+ const char sep[] = "\n";
- if (len > a->text_len) {
- a->text_len = __do_textcpy(&a->text, a->text_len, text, len);
+ printf("___[%d],[%d],[%lu],[%s]\n", rows, a->width, (long unsigned int) strlen(text), text);
+ if (rows > a->height) goto error;
+ adj_text[0] = text;
+ while ( (tok = strsep(&p_strtok, sep)) && rows-- >= 0 ) {
+ i++;
+ fprintf(stdout, "___[%p , %p],[%s],[%d]\n", p_strtok, tok, tok, rows);
+// adj_text[i] =
}
+ return adj_text;
+error:
+ free(adj_text);
+ return NULL;
+}
+
+void
+set_txtwindow_text(struct txtwindow *a, char *text)
+{
+ char **fmt_text = __do_textadjust(a, text), **curline = fmt_text;
+
+ //a->text_len = __do_textcpy(&a->text, a->text_len, text, strlen(text));
+}
+
+void
+set_txtwindow_title(struct txtwindow *a, const char *title)
+{
+ a->title_len = __do_textcpy(&a->title, a->title_len, title, strlen(title));
}
diff --git a/ui_nwindow.h b/ui_nwindow.h
index 5a983f7..614644c 100644
--- a/ui_nwindow.h
+++ b/ui_nwindow.h
@@ -19,12 +19,13 @@ struct txtwindow {
size_t text_len;
int (*window_func)(WINDOW *, struct txtwindow *);
chtype attrs;
+ chtype text_attrs;
};
typedef int (*window_func)(WINDOW *, struct txtwindow *);
struct txtwindow *
-init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, window_func cb_update);
+init_txtwindow(unsigned int, unsigned int y, unsigned int width, unsigned int height, chtype attrs, chtype text_attrs, window_func cb_update);
void
free_txtwindow(struct txtwindow *a);
@@ -36,9 +37,12 @@ void
register_txtwindow(struct txtwindow *a);
void
-set_txtwindow_text(struct txtwindow *a, const char *text);
+set_txtwindow_text(struct txtwindow *a, char *text);
void
set_txtwindow_scrollable(struct txtwindow *a, bool scrollable);
+void
+set_txtwindow_title(struct txtwindow *a, const char *title);
+
#endif