Update gnulib files.
[shishi.git] / lib / password.c
blobae57506ee96f67d47aaca5d87675401b5e517ed7
1 /* password.c --- Get passwords from user.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
23 /* XXX? zeroize password */
25 #include "internal.h"
27 #include "getpass.h"
29 #ifdef HAVE_LIBIDN
30 # include <stringprep.h>
31 #endif
33 /**
34 * shishi_prompt_password:
35 * @handle: shishi handle as allocated by shishi_init().
36 * @s: pointer to newly allocated output string with read password.
37 * @format: printf(3) style format string.
38 * @...: printf(3) style arguments.
40 * Format and print a prompt, and read a password from user. The
41 * password is possibly converted (e.g., converted from Latin-1 to
42 * UTF-8, or processed using Stringprep profile) following any
43 * 'stringprocess' keywords in configuration files.
45 * Return value: Returns SHISHI_OK iff successful.
46 **/
47 int
48 shishi_prompt_password (Shishi * handle, char **s, const char *format, ...)
50 char *p;
51 va_list ap;
53 #ifdef HAVE_LIBIDN
54 if (VERBOSE (handle))
56 printf ("Libstringprep thinks your locale is `%s'.\n",
57 stringprep_locale_charset ());
59 #endif
61 va_start (ap, format);
62 vprintf (format, ap);
63 fflush (stdout);
64 va_end (ap);
66 p = getpass ("");
68 *s = xstrdup (p);
70 printf ("\n");
72 if (VERBOSENOISE (handle))
74 size_t i;
75 printf ("Read password (length %d): ", strlen (*s));
76 for (i = 0; i < strlen (*s); i++)
77 printf ("%02x ", (*s)[i] & 0xFF);
78 printf ("\n");
81 if (handle->stringprocess
82 && strcasecmp (handle->stringprocess, "none") != 0)
83 #ifdef HAVE_LIBIDN
85 if (strcasecmp (handle->stringprocess, "stringprep") == 0)
86 p = stringprep_locale_to_utf8 (*s);
87 else
88 p = stringprep_convert (*s, handle->stringprocess,
89 stringprep_locale_charset ());
91 if (p)
93 free (*s);
94 *s = p;
96 else
97 shishi_warn (handle, "Charset conversion of password failed");
99 if (VERBOSENOISE (handle))
101 size_t i;
102 printf ("Password converted to %s (length %d): ",
103 strcasecmp (handle->stringprocess, "stringprep") == 0 ?
104 "UTF-8" : handle->stringprocess, strlen (*s));
105 for (i = 0; i < strlen (*s); i++)
106 printf ("%02x ", (*s)[i] & 0xFF);
107 printf ("\n");
110 if (strcasecmp (handle->stringprocess, "stringprep") == 0)
112 int rc;
114 rc = stringprep_profile (*s, &p, "SASLprep", 0);
115 if (rc == SHISHI_OK)
117 free (*s);
118 *s = p;
120 else
121 shishi_warn (handle, "Stringprep conversion of password failed");
123 if (VERBOSENOISE (handle))
125 size_t i;
126 printf ("Stringprep'ed password (length %d): ", strlen (*s));
127 for (i = 0; i < strlen (*s); i++)
128 printf ("%02x ", (*s)[i] & 0xFF);
129 printf ("\n");
134 #else
135 shishi_warn (handle, "Password string processing (%s) disabled",
136 handle->stringprocess);
137 #endif
139 return SHISHI_OK;