From b50b0ed59c59b6c09f03b7a299281c46f0ee57ad Mon Sep 17 00:00:00 2001 From: toni Date: Sat, 20 Dec 2014 16:04:07 +0100 Subject: - finished ui - added copyright stuff - makefile improvements --- .gitignore | 2 +- Makefile | 8 ++++---- main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------- ui.c | 23 +++++++++++++++-------- ui.h | 6 +++++- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 67402d3..b4c01e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/nask +/naskpass *.swp diff --git a/Makefile b/Makefile index 18c759f..60e27a6 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,13 @@ CFLAGS ?= $(shell ncurses5-config --cflags) -Wall -D_GNU_SOURCE=1 -g LDFLAGS ?= $(shell ncurses5-config --libs) -pthread CC := gcc INSTALL ?= install -VERSION = 1.0 -BIN = nask +VERSION = $(shell if [ -d ./.git ]; then echo -n "git-"; git rev-parse --short HEAD; else echo "1.1a"; fi) +BIN = naskpass SOURCES = status.c ui_ani.c ui_input.c ui_statusbar.c ui.c main.c -all: bin +all: $(BIN) -bin: +$(BIN): $(SOURCES) $(CC) $(SOURCES) -D_VERSION=\"$(VERSION)\" $(CFLAGS) $(LDFLAGS) -o $(BIN) install: diff --git a/main.c b/main.c index 36f396f..7d3995c 100644 --- a/main.c +++ b/main.c @@ -1,44 +1,87 @@ #include #include +#include #include +#include #include +#include +#include #include +#include #include "ui_ani.h" #include "ui_input.h" #include "ui_statusbar.h" #include "ui.h" -#define PKGNAME "nask" +#define AUTHOR "Toni Uhlig" +#define AUTHOR_EMAIL "matzeton@googlemail.com" +#define PKGNAME "naskpass" +#define PKGDESC "A NCurses replacement for cryptsetup's askpass." #ifdef _VERSION #define VERSION _VERSION #else #define VERSION "unknown" #endif + #define SHTDWN_CMD "echo 'o' >/proc/sysrq-trigger" -static void usage(char *arg0) +static void +usage(char *arg0) { - fprintf(stderr, "%s (%s)\n", PKGNAME, VERSION); - fprintf(stderr, " CMD: %s: [passfifo]\n", arg0); + fprintf(stderr, "%s (%s)\n %s\n", PKGNAME, VERSION, PKGDESC); + fprintf(stderr, " Written by %s (%s).\n", AUTHOR, AUTHOR_EMAIL); + fprintf(stderr, " License GPLv3+: GNU GPL version 3 or later .\n\n"); + fprintf(stderr, " Command: %s: [passfifo]\n", arg0); +} + +static bool +check_fifo(char *fifo_path) +{ + struct stat st; + + if (mkfifo(fifo_path, S_IRUSR | S_IWUSR) == 0) { + return (true); + } else { + if (errno == EEXIST) { + if (stat(fifo_path, &st) == 0) { + if (S_ISFIFO(st.st_mode) == 1) { + return (true); + } else { + fprintf(stderr, "stat: %s is not a FIFO\n", fifo_path); + return (false); + } + } + } + } + perror("check_fifo"); + return (false); } int main(int argc, char **argv) { pid_t child; - int status; + int status, ffd; if (argc != 2) { usage(argv[0]); - exit(-1); + exit(EXIT_FAILURE); + } + + if (check_fifo(argv[1]) == false) { + exit(EXIT_FAILURE); + } + if ((ffd = open(argv[1], O_NONBLOCK)) < 0) { + perror("open"); + exit(EXIT_FAILURE); } if ((child = vfork()) == 0) { if (setsid() == (pid_t)-1) { perror("setsid"); - return (EXIT_FAILURE); + exit (EXIT_FAILURE); } do_ui(); } if (child > 0) { diff --git a/ui.c b/ui.c index 91d551c..288e633 100644 --- a/ui.c +++ b/ui.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -15,6 +16,8 @@ #include "status.h" +#define APP_TIMEOUT 10 + #define PASSWD_WIDTH 35 #define PASSWD_HEIGHT 5 #define PASSWD_XRELPOS (unsigned int)(PASSWD_WIDTH / 2) - (PASSWD_WIDTH / 6) @@ -26,6 +29,7 @@ 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; static pthread_mutex_t mtx_cb = PTHREAD_MUTEX_INITIALIZER; @@ -106,6 +110,7 @@ do_ui_update(bool timed_out) static void * ui_thrd(void *arg) { + int cnd_ret; struct timeval now; struct timespec wait; @@ -117,11 +122,13 @@ ui_thrd(void *arg) sem_post(&sem_rdy); while (active == true) { pthread_mutex_unlock(&mtx_busy); - pthread_cond_timedwait(&cnd_update, &mtx_update, &wait); - wait.tv_sec += UILOOP_TIMEOUT; + 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 (active == false) break; - do_ui_update(true); + do_ui_update( (cnd_ret == ETIMEDOUT ? true : false) ); } pthread_mutex_unlock(&mtx_busy); pthread_mutex_unlock(&mtx_update); @@ -214,7 +221,7 @@ lower_statusbar_update(WINDOW *win, struct statusbar *bar) return (0); } -int +void do_ui(void) { struct input *pw_input; @@ -224,7 +231,7 @@ do_ui(void) if (sem_init(&sem_rdy, 0, 0) == -1) { perror("init semaphore"); - exit(1); + exit(DOUI_ERR); } init_ui(); pw_input = init_input((unsigned int)(max_x / 2)-PASSWD_XRELPOS, (unsigned int)(max_y / 2)-PASSWD_YRELPOS, PASSWD_WIDTH, "PASSWORD: ", 128, COLOR_PAIR(3), COLOR_PAIR(2)); @@ -236,9 +243,9 @@ do_ui(void) register_statusbar(lower); register_anic(heartbeat); activate_input(wnd_main, pw_input); - set_statusbar_text(higher, "* NASKPASS *"); + set_statusbar_text(higher, "/* NASKPASS */"); if (run_ui_thrd() != 0) { - exit(2); + exit(DOUI_ERR); } sem_wait(&sem_rdy); while ((key = wgetch(wnd_main)) != '\0' && process_key(key, pw_input, wnd_main) == true) { @@ -259,5 +266,5 @@ do_ui(void) free_statusbar(higher); free_statusbar(lower); free_ui(); - exit(0); + exit(DOUI_OK); } diff --git a/ui.h b/ui.h index 9d6312e..7566605 100644 --- a/ui.h +++ b/ui.h @@ -9,6 +9,10 @@ #define UICB_ERR_CB 2 #define UICB_ERR_BUF 3 +#define DOUI_OK 0 +#define DOUI_ERR 1 +#define DOUI_TMOUT 2 + #define UILOOP_TIMEOUT 1 #define UIKEY_ENTER 10 @@ -50,7 +54,7 @@ run_ui_thrd(void); int stop_ui_thrd(void); -int +void do_ui(void); #endif -- cgit v1.2.3