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/. */
5 /* With the exception of GetPasswordString, this file was
6 copied from NSS's cmd/lib/secutil.c hg revision 8f011395145e */
8 #include "nss_secutil.h"
17 static char consoleName
[] = {
26 static char * quiet_fgets (char *buf
, int length
, FILE *input
)
31 memset (buf
, 0, length
);
33 if (!isatty(fileno(input
))) {
34 return fgets(buf
,length
,input
);
40 #if defined (_WIN32_WCE)
41 c
= getchar(); /* gets a character from stdin */
43 c
= getch(); /* getch gets a character from the console */
51 else if (--length
> 0)
54 if (!c
|| c
== '\n' || c
== '\r')
63 GetPasswordString(void *arg
, char *prompt
)
66 char phrase
[200] = {'\0'};
67 int isInputTerminal
= isatty(fileno(stdin
));
69 (void) arg
; (void) prompt
; // avoid warnings
72 if (isInputTerminal
) {
73 input
= fopen(consoleName
, "r");
75 fprintf(stderr
, "Error opening input terminal for read\n");
81 if (isInputTerminal
) {
82 fprintf(stdout
, "Please enter your password:\n");
86 QUIET_FGETS (phrase
, sizeof(phrase
), input
);
88 if (isInputTerminal
) {
89 fprintf(stdout
, "\n");
93 if (isInputTerminal
) {
98 /* Strip off the newlines if present */
99 if (phrase
[PORT_Strlen(phrase
)-1] == '\n' ||
100 phrase
[PORT_Strlen(phrase
)-1] == '\r') {
101 phrase
[PORT_Strlen(phrase
)-1] = 0;
103 return (char*) PORT_Strdup(phrase
);
107 SECU_FilePasswd(PK11SlotInfo
*slot
, PRBool retry
, void *arg
)
109 char* phrases
, *phrase
;
114 const long maxPwdFileSize
= 4096;
115 char* tokenName
= NULL
;
122 return 0; /* no good retrying - the files contents will be the same */
125 phrases
= PORT_ZAlloc(maxPwdFileSize
+ 1);
128 return 0; /* out of memory */
131 fd
= PR_Open(pwFile
, PR_RDONLY
, 0);
133 fprintf(stderr
, "No password file \"%s\" exists.\n", pwFile
);
138 nb
= PR_Read(fd
, phrases
, maxPwdFileSize
);
143 fprintf(stderr
,"password file contains no data\n");
149 tokenName
= PK11_GetTokenName(slot
);
151 tokenLen
= PORT_Strlen(tokenName
);
160 /* handle the Windows EOL case */
161 while (i
< nb
&& phrases
[i
] != '\r' && phrases
[i
] != '\n') i
++;
162 /* terminate passphrase */
164 /* clean up any EOL before the start of the next passphrase */
165 while ( (i
<nb
) && (phrases
[i
] == '\r' || phrases
[i
] == '\n')) {
168 /* now analyze the current passphrase */
169 phrase
= &phrases
[startphrase
];
172 if (PORT_Strncmp(phrase
, tokenName
, tokenLen
)) continue;
173 phraseLen
= PORT_Strlen(phrase
);
174 if (phraseLen
< (tokenLen
+1)) continue;
175 if (phrase
[tokenLen
] != ':') continue;
176 phrase
= &phrase
[tokenLen
+1];
181 phrase
= PORT_Strdup((char*)phrase
);
187 SECU_GetModulePassword(PK11SlotInfo
*slot
, PRBool retry
, void *arg
)
190 secuPWData
*pwdata
= (secuPWData
*)arg
;
191 secuPWData pwnull
= { PW_NONE
, 0 };
192 secuPWData pwxtrn
= { PW_EXTERNAL
, "external" };
198 if (PK11_ProtectedAuthenticationPath(slot
)) {
201 if (retry
&& pwdata
->source
!= PW_NONE
) {
202 PR_fprintf(PR_STDERR
, "Incorrect password/PIN entered.\n");
206 switch (pwdata
->source
) {
208 sprintf(prompt
, "Enter Password or Pin for \"%s\":",
209 PK11_GetTokenName(slot
));
210 return GetPasswordString(NULL
, prompt
);
212 /* Instead of opening and closing the file every time, get the pw
213 * once, then keep it in memory (duh).
215 pw
= SECU_FilePasswd(slot
, retry
, pwdata
->data
);
216 pwdata
->source
= PW_PLAINTEXT
;
217 pwdata
->data
= strdup(pw
);
218 /* it's already been dup'ed */
222 "Press Enter, then enter PIN for \"%s\" on external device.\n",
223 PK11_GetTokenName(slot
));
224 pw
= GetPasswordString(NULL
, prompt
);
226 memset(pw
, 0, PORT_Strlen(pw
));
231 return strdup(pwdata
->data
);
236 PR_fprintf(PR_STDERR
, "Password check failed: No password found.\n");