1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/lib/secpwd.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +#include "secutil.h" 1.8 + 1.9 +/* 1.10 + * NOTE: The contents of this file are NOT used by the client. 1.11 + * (They are part of the security library as a whole, but they are 1.12 + * NOT USED BY THE CLIENT.) Do not change things on behalf of the 1.13 + * client (like localizing strings), or add things that are only 1.14 + * for the client (put them elsewhere). 1.15 + */ 1.16 + 1.17 + 1.18 +#ifdef XP_UNIX 1.19 +#include <termios.h> 1.20 +#endif 1.21 + 1.22 +#if defined(XP_UNIX) || defined(XP_BEOS) 1.23 +#include <unistd.h> /* for isatty() */ 1.24 +#endif 1.25 + 1.26 +#if defined(_WINDOWS) 1.27 +#include <conio.h> 1.28 +#include <io.h> 1.29 +#define QUIET_FGETS quiet_fgets 1.30 +static char * quiet_fgets (char *buf, int length, FILE *input); 1.31 +#else 1.32 +#define QUIET_FGETS fgets 1.33 +#endif 1.34 + 1.35 +static void echoOff(int fd) 1.36 +{ 1.37 +#if defined(XP_UNIX) 1.38 + if (isatty(fd)) { 1.39 + struct termios tio; 1.40 + tcgetattr(fd, &tio); 1.41 + tio.c_lflag &= ~ECHO; 1.42 + tcsetattr(fd, TCSAFLUSH, &tio); 1.43 + } 1.44 +#endif 1.45 +} 1.46 + 1.47 +static void echoOn(int fd) 1.48 +{ 1.49 +#if defined(XP_UNIX) 1.50 + if (isatty(fd)) { 1.51 + struct termios tio; 1.52 + tcgetattr(fd, &tio); 1.53 + tio.c_lflag |= ECHO; 1.54 + tcsetattr(fd, TCSAFLUSH, &tio); 1.55 + } 1.56 +#endif 1.57 +} 1.58 + 1.59 +char *SEC_GetPassword(FILE *input, FILE *output, char *prompt, 1.60 + PRBool (*ok)(char *)) 1.61 +{ 1.62 +#if defined(_WINDOWS) 1.63 + int isTTY = (input == stdin); 1.64 +#define echoOn(x) 1.65 +#define echoOff(x) 1.66 +#else 1.67 + int infd = fileno(input); 1.68 + int isTTY = isatty(infd); 1.69 +#endif 1.70 + char phrase[200] = {'\0'}; /* ensure EOF doesn't return junk */ 1.71 + 1.72 + for (;;) { 1.73 + /* Prompt for password */ 1.74 + if (isTTY) { 1.75 + fprintf(output, "%s", prompt); 1.76 + fflush (output); 1.77 + echoOff(infd); 1.78 + } 1.79 + 1.80 + QUIET_FGETS ( phrase, sizeof(phrase), input); 1.81 + 1.82 + if (isTTY) { 1.83 + fprintf(output, "\n"); 1.84 + echoOn(infd); 1.85 + } 1.86 + 1.87 + /* stomp on newline */ 1.88 + phrase[PORT_Strlen(phrase)-1] = 0; 1.89 + 1.90 + /* Validate password */ 1.91 + if (!(*ok)(phrase)) { 1.92 + /* Not weird enough */ 1.93 + if (!isTTY) return 0; 1.94 + fprintf(output, "Password must be at least 8 characters long with one or more\n"); 1.95 + fprintf(output, "non-alphabetic characters\n"); 1.96 + continue; 1.97 + } 1.98 + return (char*) PORT_Strdup(phrase); 1.99 + } 1.100 +} 1.101 + 1.102 + 1.103 + 1.104 +PRBool SEC_CheckPassword(char *cp) 1.105 +{ 1.106 + int len; 1.107 + char *end; 1.108 + 1.109 + len = PORT_Strlen(cp); 1.110 + if (len < 8) { 1.111 + return PR_FALSE; 1.112 + } 1.113 + end = cp + len; 1.114 + while (cp < end) { 1.115 + unsigned char ch = *cp++; 1.116 + if (!((ch >= 'A') && (ch <= 'Z')) && 1.117 + !((ch >= 'a') && (ch <= 'z'))) { 1.118 + /* pass phrase has at least one non alphabetic in it */ 1.119 + return PR_TRUE; 1.120 + } 1.121 + } 1.122 + return PR_FALSE; 1.123 +} 1.124 + 1.125 +PRBool SEC_BlindCheckPassword(char *cp) 1.126 +{ 1.127 + if (cp != NULL) { 1.128 + return PR_TRUE; 1.129 + } 1.130 + return PR_FALSE; 1.131 +} 1.132 + 1.133 +/* Get a password from the input terminal, without echoing */ 1.134 + 1.135 +#if defined(_WINDOWS) 1.136 +static char * quiet_fgets (char *buf, int length, FILE *input) 1.137 + { 1.138 + int c; 1.139 + char *end = buf; 1.140 + 1.141 + /* fflush (input); */ 1.142 + memset (buf, 0, length); 1.143 + 1.144 + if (!isatty(fileno(input))) { 1.145 + return fgets(buf,length,input); 1.146 + } 1.147 + 1.148 + while (1) 1.149 + { 1.150 + c = getch(); /* getch gets a character from the console */ 1.151 + 1.152 + if (c == '\b') 1.153 + { 1.154 + if (end > buf) 1.155 + end--; 1.156 + } 1.157 + 1.158 + else if (--length > 0) 1.159 + *end++ = c; 1.160 + 1.161 + if (!c || c == '\n' || c == '\r') 1.162 + break; 1.163 + } 1.164 + 1.165 + return buf; 1.166 + } 1.167 +#endif