comparison cmd.c @ 18:1e39a251cbe9

Use libtecla for interactive input
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu, 24 Aug 2017 13:10:56 +0200
parents a08ef0674d8e
children 5c6155c8e9b6
comparison
equal deleted inserted replaced
17:a08ef0674d8e 18:1e39a251cbe9
27 #ifdef HAVE_ERR_H 27 #ifdef HAVE_ERR_H
28 #include <err.h> 28 #include <err.h>
29 #endif /* HAVE_ERR_H */ 29 #endif /* HAVE_ERR_H */
30 #include <errno.h> 30 #include <errno.h>
31 #include <limits.h> 31 #include <limits.h>
32 #ifdef HAVE_READPASSPHRASE_H
33 #include <readpassphrase.h>
34 #endif /* READPASSPHRASE_H */
35 #include <regex.h> 32 #include <regex.h>
36 #include <stdlib.h> 33 #include <stdlib.h>
37 #include <string.h> 34 #include <string.h>
38 #include <time.h> 35 #include <time.h>
39 #include <unistd.h> 36 #include <unistd.h>
858 } 855 }
859 856
860 static enum cmd_return 857 static enum cmd_return
861 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[]) 858 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[])
862 { 859 {
863 size_t password_len; 860 size_t len;
864 char password_buf[PWS3_MAX_PASSWORD_LEN + 1];
865 char confirm_buf[PWS3_MAX_PASSWORD_LEN + 1];
866 861
867 if (argc > 2) { 862 if (argc > 2) {
868 return (CMD_USAGE); 863 return (CMD_USAGE);
869 } else if (argc == 2) { 864 } else if (argc == 2) {
870 password_len = strlen(argv[1]); 865 len = strlen(argv[1]);
871 if (password_len == 0) { 866 if (len == 0) {
872 pwm_err(ctx, "password must not be empty"); 867 pwm_err(ctx, "password must not be empty");
873 return (CMD_ERR); 868 return (CMD_ERR);
874 } else if (password_len + 1 > sizeof (ctx->password)) { 869 } else if (len + 1 > sizeof (ctx->password)) {
875 pwm_err(ctx, "password too long"); 870 pwm_err(ctx, "password too long");
876 return (CMD_ERR); 871 return (CMD_ERR);
877 } 872 }
878 memcpy(ctx->password, argv[1], password_len + 1); 873 memcpy(ctx->password, argv[1], len + 1);
879 } else { 874 } else {
880 if (readpassphrase("Enter password: ", password_buf, 875 if (pwm_read_password(ctx, 1) != 0) {
881 sizeof (password_buf), RPP_ECHO_OFF | RPP_REQUIRE_TTY) ==
882 NULL) {
883 err(1, "readpassphrase");
884 }
885 password_len = strlen(password_buf);
886 if (password_len == 0) {
887 pwm_err(ctx, "password must not be empty");
888 return (CMD_ERR); 876 return (CMD_ERR);
889 } 877 }
890 if (readpassphrase("Confirm password: ", confirm_buf,
891 sizeof (confirm_buf),
892 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) {
893 err(1, "readpassphrase");
894 }
895 if (strcmp(password_buf, confirm_buf) != 0) {
896 pwm_err(ctx, "passwords do not match");
897 return (CMD_ERR);
898 }
899 memcpy(ctx->password, password_buf, password_len + 1);
900 } 878 }
901 879
902 return (CMD_OK); 880 return (CMD_OK);
903 } 881 }
904 882