From e45068f68f416238be6b204d22781fe9d4c3b06f Mon Sep 17 00:00:00 2001 From: darkcoven Date: Fri, 18 Oct 2013 23:31:40 +0200 Subject: [PATCH] Cli: change password can ask new password with terminal echo disabled --- cli/man/ngcli.1 | 5 +++-- cli/src/com_password.c | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cli/man/ngcli.1 b/cli/man/ngcli.1 index 6c3d863..66101c2 100644 --- a/cli/man/ngcli.1 +++ b/cli/man/ngcli.1 @@ -198,8 +198,9 @@ mask : set the switch netmask to \fImask\fR gw : set the switch gateway to \fIgw\fR . .TP -\fBpassword change\fI password -Change the switch password to \fIpassword\fR. +\fBpassword change\fR [ \fIpassword\fR ] +Change the switch password to \fIpassword\fR. If not specified, it is asked +from user (with terminal echo disabled). . .TP \fBpassword set\fR [ \fIpassword\fR ] diff --git a/cli/src/com_password.c b/cli/src/com_password.c index 0303b2b..720c5d2 100644 --- a/cli/src/com_password.c +++ b/cli/src/com_password.c @@ -5,11 +5,13 @@ int do_password_change (int argc, const char **argv, struct ngadmin *nga) { int i; + char buf[64]; const struct swi_attr *sa; + const char *pass; - if (argc != 1) { - printf("usage: password change \n"); + if (argc > 1) { + printf("usage: password change []\n"); return 1; } @@ -19,8 +21,24 @@ int do_password_change (int argc, const char **argv, struct ngadmin *nga) return 1; } - i = ngadmin_changePassword(nga, argv[0]); - printErrCode(i); + if (argc == 0) { + printf("Enter new password: "); + fflush(stdout); + current_term.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + pass = fgets(buf, sizeof(buf), stdin); + trim(buf, strlen(buf)); + current_term.c_lflag |= ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + putchar('\n'); + } else { + pass = argv[0]; + } + + if (pass != NULL) { + i = ngadmin_changePassword(nga, pass); + printErrCode(i); + } return 0; -- 2.39.2