|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 #include "secutil.h" |
|
5 |
|
6 /* |
|
7 * NOTE: The contents of this file are NOT used by the client. |
|
8 * (They are part of the security library as a whole, but they are |
|
9 * NOT USED BY THE CLIENT.) Do not change things on behalf of the |
|
10 * client (like localizing strings), or add things that are only |
|
11 * for the client (put them elsewhere). |
|
12 */ |
|
13 |
|
14 |
|
15 #ifdef XP_UNIX |
|
16 #include <termios.h> |
|
17 #endif |
|
18 |
|
19 #if defined(XP_UNIX) || defined(XP_BEOS) |
|
20 #include <unistd.h> /* for isatty() */ |
|
21 #endif |
|
22 |
|
23 #if defined(_WINDOWS) |
|
24 #include <conio.h> |
|
25 #include <io.h> |
|
26 #define QUIET_FGETS quiet_fgets |
|
27 static char * quiet_fgets (char *buf, int length, FILE *input); |
|
28 #else |
|
29 #define QUIET_FGETS fgets |
|
30 #endif |
|
31 |
|
32 static void echoOff(int fd) |
|
33 { |
|
34 #if defined(XP_UNIX) |
|
35 if (isatty(fd)) { |
|
36 struct termios tio; |
|
37 tcgetattr(fd, &tio); |
|
38 tio.c_lflag &= ~ECHO; |
|
39 tcsetattr(fd, TCSAFLUSH, &tio); |
|
40 } |
|
41 #endif |
|
42 } |
|
43 |
|
44 static void echoOn(int fd) |
|
45 { |
|
46 #if defined(XP_UNIX) |
|
47 if (isatty(fd)) { |
|
48 struct termios tio; |
|
49 tcgetattr(fd, &tio); |
|
50 tio.c_lflag |= ECHO; |
|
51 tcsetattr(fd, TCSAFLUSH, &tio); |
|
52 } |
|
53 #endif |
|
54 } |
|
55 |
|
56 char *SEC_GetPassword(FILE *input, FILE *output, char *prompt, |
|
57 PRBool (*ok)(char *)) |
|
58 { |
|
59 #if defined(_WINDOWS) |
|
60 int isTTY = (input == stdin); |
|
61 #define echoOn(x) |
|
62 #define echoOff(x) |
|
63 #else |
|
64 int infd = fileno(input); |
|
65 int isTTY = isatty(infd); |
|
66 #endif |
|
67 char phrase[200] = {'\0'}; /* ensure EOF doesn't return junk */ |
|
68 |
|
69 for (;;) { |
|
70 /* Prompt for password */ |
|
71 if (isTTY) { |
|
72 fprintf(output, "%s", prompt); |
|
73 fflush (output); |
|
74 echoOff(infd); |
|
75 } |
|
76 |
|
77 QUIET_FGETS ( phrase, sizeof(phrase), input); |
|
78 |
|
79 if (isTTY) { |
|
80 fprintf(output, "\n"); |
|
81 echoOn(infd); |
|
82 } |
|
83 |
|
84 /* stomp on newline */ |
|
85 phrase[PORT_Strlen(phrase)-1] = 0; |
|
86 |
|
87 /* Validate password */ |
|
88 if (!(*ok)(phrase)) { |
|
89 /* Not weird enough */ |
|
90 if (!isTTY) return 0; |
|
91 fprintf(output, "Password must be at least 8 characters long with one or more\n"); |
|
92 fprintf(output, "non-alphabetic characters\n"); |
|
93 continue; |
|
94 } |
|
95 return (char*) PORT_Strdup(phrase); |
|
96 } |
|
97 } |
|
98 |
|
99 |
|
100 |
|
101 PRBool SEC_CheckPassword(char *cp) |
|
102 { |
|
103 int len; |
|
104 char *end; |
|
105 |
|
106 len = PORT_Strlen(cp); |
|
107 if (len < 8) { |
|
108 return PR_FALSE; |
|
109 } |
|
110 end = cp + len; |
|
111 while (cp < end) { |
|
112 unsigned char ch = *cp++; |
|
113 if (!((ch >= 'A') && (ch <= 'Z')) && |
|
114 !((ch >= 'a') && (ch <= 'z'))) { |
|
115 /* pass phrase has at least one non alphabetic in it */ |
|
116 return PR_TRUE; |
|
117 } |
|
118 } |
|
119 return PR_FALSE; |
|
120 } |
|
121 |
|
122 PRBool SEC_BlindCheckPassword(char *cp) |
|
123 { |
|
124 if (cp != NULL) { |
|
125 return PR_TRUE; |
|
126 } |
|
127 return PR_FALSE; |
|
128 } |
|
129 |
|
130 /* Get a password from the input terminal, without echoing */ |
|
131 |
|
132 #if defined(_WINDOWS) |
|
133 static char * quiet_fgets (char *buf, int length, FILE *input) |
|
134 { |
|
135 int c; |
|
136 char *end = buf; |
|
137 |
|
138 /* fflush (input); */ |
|
139 memset (buf, 0, length); |
|
140 |
|
141 if (!isatty(fileno(input))) { |
|
142 return fgets(buf,length,input); |
|
143 } |
|
144 |
|
145 while (1) |
|
146 { |
|
147 c = getch(); /* getch gets a character from the console */ |
|
148 |
|
149 if (c == '\b') |
|
150 { |
|
151 if (end > buf) |
|
152 end--; |
|
153 } |
|
154 |
|
155 else if (--length > 0) |
|
156 *end++ = c; |
|
157 |
|
158 if (!c || c == '\n' || c == '\r') |
|
159 break; |
|
160 } |
|
161 |
|
162 return buf; |
|
163 } |
|
164 #endif |