X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=cli%2Fcom_password.c;h=0303b2b3c902abd53f1f4c1e2fa161a1b79a99ec;hb=1b72c10a906e2060054e84943f241614d857e236;hp=4b1f184381050079d67f34bd4346d40a7ff25b77;hpb=192ea617675b35fe37460a0fbb0c1511c0074fe8;p=ngadmin diff --git a/cli/com_password.c b/cli/com_password.c index 4b1f184..0303b2b 100644 --- a/cli/com_password.c +++ b/cli/com_password.c @@ -1,60 +1,65 @@ -#include "common.h" - - - - -static bool do_password_change (const struct TreeNode *tn UNUSED, int nb, const char **com, struct ngadmin *nga) { - - int i; - const struct swi_attr *sa; - - - if ( nb!=1 ) { - printf("Usage: password set \n"); - return false; - } - - if ( (sa=ngadmin_getCurrentSwitch(nga))==NULL ) { - printf("must be logged\n"); - return false; - } - - i=ngadmin_changePassword(nga, com[0]); - printErrCode(i); - - - return true; - +#include "commands.h" + + +int do_password_change (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + const struct swi_attr *sa; + + + if (argc != 1) { + printf("usage: password change \n"); + return 1; + } + + sa = ngadmin_getCurrentSwitch(nga); + if (sa == NULL) { + printf("must be logged\n"); + return 1; + } + + i = ngadmin_changePassword(nga, argv[0]); + printErrCode(i); + + + return 0; } - -static bool do_password_set (const struct TreeNode *tn UNUSED, int nb, const char **com, struct ngadmin *nga) { - - int i; - - - if ( nb!=1 ) { - printf("Usage: password set \n"); - return false; - } - - i=ngadmin_setPassword(nga, com[0]); - printErrCode(i); - - - return true; - +int do_password_set (int argc, const char **argv, struct ngadmin *nga) +{ + int i; + char buf[64]; + const char *pass; + + + if (argc > 1) { + printf("usage: password set []\n"); + return 1; + } + + if (argc == 0) { + printf("Enter 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_setPassword(nga, pass); + printErrCode(i); + } + + + return 0; } - - -static const struct TreeNode com_password_change=COM("change", do_password_change, true, NULL); -static const struct TreeNode com_password_set=COM("set", do_password_set, true, NULL); - -const struct TreeNode com_password=COM("password", NULL, false, &com_password_change, &com_password_set, NULL); - - -