# HG changeset patch # User Olaf Wintermann # Date 1443883490 -7200 # Node ID e480483346029a8e6d91dc4eda2b86b371e7fc81 # Parent 82475dc12dd43991d52eb131f5b9f43e21d986a3 hidden password input diff -r 82475dc12dd4 -r e48048334602 dav/main.c --- 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 #include #include +#include #include @@ -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; } diff -r 82475dc12dd4 -r e48048334602 libidav/resource.c --- 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;jname = key.data + k + 1; + name->name = (char*)key.data + k + 1; break; } }