1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* apr_password_get.c: abstraction to provide for obtaining a password from the
18 * command line in whatever way the OS supports. In the best case, it's a
19 * wrapper for the system library's getpass() routine; otherwise, we
20 * use one we define ourselves.
22 #include "apr_private.h"
23 #include "apr_strings.h"
25 #include "apr_errno.h"
26 #if APR_HAVE_SYS_TYPES_H
27 #include <sys/types.h>
38 #pragma warning(disable: 4032)
40 #pragma warning(default: 4032)
51 #if APR_HAVE_STRINGS_H
55 /* Disable getpass() support when PASS_MAX is defined and is "small",
56 * for an arbitrary definition of "small".
57 * HP-UX truncates passwords (PR49496) so we disable getpass() for
60 #if defined(HAVE_GETPASS) && \
61 (defined(PASS_MAX) && PASS_MAX < 32) || defined(__hpux) || defined(__hpux__)
65 #if defined(HAVE_TERMIOS_H) && !defined(HAVE_GETPASS)
69 #if !APR_CHARSET_EBCDIC
72 #else /* APR_CHARSET_EBCDIC */
75 #endif /* APR_CHARSET_EBCDIC */
77 #define MAX_STRING_LEN 256
79 #define ERR_OVERFLOW 5
81 #if !defined(HAVE_GETPASS) && !defined(HAVE_GETPASSPHRASE) && !defined(HAVE_GETPASS_R)
83 /* MPE, Win32, NetWare and BeOS all lack a native getpass() */
85 #if !defined(HAVE_TERMIOS_H) && !defined(WIN32) && !defined(NETWARE)
87 * MPE lacks getpass() and a way to suppress stdin echo. So for now, just
88 * issue the prompt and read the results with echo. (Ugh).
91 static char *get_password(const char *prompt)
93 static char password[MAX_STRING_LEN];
95 fputs(prompt, stderr);
96 fgets((char *) &password, sizeof(password), stdin);
98 return (char *) &password;
101 #elif defined (HAVE_TERMIOS_H)
104 static char *get_password(const char *prompt)
107 static char password[MAX_STRING_LEN];
109 fputs(prompt, stderr);
112 if (tcgetattr(STDIN_FILENO, &attr) != 0)
114 attr.c_lflag &= ~(ECHO);
116 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) != 0)
118 while ((password[n] = getchar()) != '\n') {
119 if (n < sizeof(password) - 1 && password[n] >= ' ' && password[n] <= '~') {
122 fprintf(stderr,"\n");
123 fputs(prompt, stderr);
131 if (n > (MAX_STRING_LEN - 1)) {
132 password[MAX_STRING_LEN - 1] = '\0';
135 attr.c_lflag |= ECHO;
136 tcsetattr(STDIN_FILENO, TCSANOW, &attr);
137 return (char*) &password;
143 * Windows lacks getpass(). So we'll re-implement it here.
146 static char *get_password(const char *prompt)
148 /* WCE lacks console. So the getpass is unsuported
149 * The only way is to use the GUI so the getpass should be implemented
150 * on per-application basis.
155 static char password[128];
159 fputs(prompt, stderr);
161 while ((ch = _getch()) != '\r') {
162 if (ch == EOF) /* EOF */ {
163 fputs("[EOF]\n", stderr);
166 else if (ch == 0 || ch == 0xE0) {
167 /* FN Keys (0 or E0) are a sentinal for a FN code */
168 ch = (ch << 4) | _getch();
169 /* Catch {DELETE}, {<--}, Num{DEL} and Num{<--} */
170 if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) && n) {
171 password[--n] = '\0';
172 fputs("\b \b", stderr);
178 else if ((ch == '\b' || ch == 127) && n) /* BS/DEL */ {
179 password[--n] = '\0';
180 fputs("\b \b", stderr);
182 else if (ch == 3) /* CTRL+C */ {
183 /* _getch() bypasses Ctrl+C but not Ctrl+Break detection! */
184 fputs("^C\n", stderr);
187 else if (ch == 26) /* CTRL+Z */ {
188 fputs("^Z\n", stderr);
191 else if (ch == 27) /* ESC */ {
193 fputs(prompt, stderr);
196 else if ((n < sizeof(password) - 1) && !apr_iscntrl(ch)) {
211 #endif /* no getchar or _getch */
213 #endif /* no getpass or getpassphrase or getpass_r */
216 * Use the OS getpass() routine (or our own) to obtain a password from
221 * 5: Partial success; entered text truncated to the size of the
224 * Restrictions: Truncation also occurs according to the host system's
225 * getpass() semantics, or at position 255 if our own version is used,
226 * but the caller is *not* made aware of it unless their own buffer is
227 * smaller than our own.
230 APR_DECLARE(apr_status_t) apr_password_get(const char *prompt, char *pwbuf, apr_size_t *bufsiz)
232 apr_status_t rv = APR_SUCCESS;
233 #if defined(HAVE_GETPASS_R)
234 if (getpass_r(prompt, pwbuf, *bufsiz) == NULL)
237 #if defined(HAVE_GETPASSPHRASE)
238 char *pw_got = getpassphrase(prompt);
239 #elif defined(HAVE_GETPASS)
240 char *pw_got = getpass(prompt);
241 #else /* use the replacement implementation above */
242 char *pw_got = get_password(prompt);
247 if (strlen(pw_got) >= *bufsiz) {
248 rv = APR_ENAMETOOLONG;
250 apr_cpystrn(pwbuf, pw_got, *bufsiz);
251 memset(pw_got, 0, strlen(pw_got));
252 #endif /* HAVE_GETPASS_R */