hidden password input

Sat, 03 Oct 2015 16:44:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 03 Oct 2015 16:44:50 +0200
changeset 146
e48048334602
parent 145
82475dc12dd4
child 147
458a8dc68048

hidden password input

dav/main.c file | annotate | diff | comparison | revisions
libidav/resource.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sat Oct 03 14:36:12 2015 +0200
+++ b/dav/main.c	Sat Oct 03 16:44:50 2015 +0200
@@ -36,6 +36,7 @@
 #include <ucx/string.h>
 #include <ucx/utils.h>
 #include <dirent.h>
+#include <termios.h>
 
 
 #include <libidav/utils.h>
@@ -174,6 +175,49 @@
     fprintf(stderr, "\n");
 }
 
+char* password_input(char *prompt) {
+    fprintf(stderr, "%s", prompt);
+    fflush(stderr);
+    
+    // hide terminal input
+#ifdef _WIN32
+    // TODO
+#else
+    struct termios oflags, nflags;
+    tcgetattr(fileno(stdin), &oflags);
+    nflags = oflags;
+    nflags.c_lflag &= ~ECHO;
+    nflags.c_lflag |= ECHONL;
+    if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
+        perror("tcsetattr");
+    }
+#endif
+    
+    // read password input
+    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
+    int c = 0;
+    while((c = getchar()) != EOF) {
+        if(c == '\n') {
+            break;
+        }
+        ucx_buffer_putc(buf, c);
+    }
+    ucx_buffer_putc(buf, 0);
+    
+    // restore terminal settings
+#ifdef _WIN32
+    // TODO
+#else
+    if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
+        perror("tcsetattr");
+    }
+#endif
+    
+    char *str = buf->space;
+    free(buf); // only free the UcxBuffer struct
+    return str;
+}
+
 int request_auth(Repository *repo, DavSession *sn) {
     static int login = 0;
     if(login) {
@@ -181,31 +225,24 @@
     }
     
     char *user = NULL;
-    char *password = NULL;
     char ubuf[256];
-    char pbuf[256];
     if(repo->user) {
        user = repo->user; 
     } else {
-        printf("user: ");
-        fflush(stdout);
+        fprintf(stderr, "user: ");
+        fflush(stderr);
         user = fgets(ubuf, 256, stdin);
     }
     
-    printf("password: ");
-    fflush(stdout);
-    password = fgets(pbuf, 256, stdin);
+    char *password = password_input("password: ");
     
     size_t ulen = strlen(user);
     if(user[ulen-1] == '\n') {
         user[ulen-1] = '\0';
     }
-    size_t plen = strlen(password);
-    if(password[plen-1] == '\n') {
-        password[plen-1] = '\0';
-    }
     
     dav_session_set_auth(sn, user, password);
+    free(password);
     login = 1;
     return 1;
 }
--- a/libidav/resource.c	Sat Oct 03 14:36:12 2015 +0200
+++ b/libidav/resource.c	Sat Oct 03 16:44:50 2015 +0200
@@ -509,7 +509,7 @@
         name->ns = key.data;
         for(int k=0;j<key.len;k++) {
             if(((char*)key.data)[k] == '\0') {
-                name->name = key.data + k + 1;
+                name->name = (char*)key.data + k + 1;
                 break;
             }
         }

mercurial