Generated.
[shishi.git] / lib / password.c
blob795845c203c5dd095c841a00b5d86ea05d309a45
1 /* password.c get passwords from user
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* XXX? zeroize password */
24 #include "internal.h"
25 #ifdef WITH_STRINGPREP
26 #include <stringprep.h>
27 #include <stringprep_kerberos5.h>
28 #endif
30 #if defined (HAVE_TERMIOS_H)
32 #include <termios.h>
34 static int
35 tty_set_echo (int echo)
37 struct termios termios_p;
38 int fd = fileno (stdin);
40 if (tcgetattr (fd, &termios_p) != 0)
41 return SHISHI_TTY_ERROR;
43 if (echo)
44 termios_p.c_lflag |= ECHO;
45 else
46 termios_p.c_lflag &= ~ECHO;
48 if (tcsetattr (fd, TCSANOW, &termios_p) != 0)
49 return SHISHI_TTY_ERROR;
51 return SHISHI_OK;
54 #else
56 mail simon @ josefsson.org and tell what system this is
57 #endif
58 static RETSIGTYPE
59 tty_echo (int signum)
61 tty_set_echo (1);
64 static RETSIGTYPE
65 tty_noecho (int signum)
67 tty_set_echo (0);
70 int
71 shishi_read_password (FILE * fh, char *s, int size)
73 int rc;
75 rc = tty_set_echo (0);
76 if (rc != SHISHI_OK)
77 return rc;
79 #ifdef HAVE_SIGNAL
80 signal (SIGQUIT, tty_echo);
81 signal (SIGCONT, tty_noecho);
82 #endif
84 fgets (s, size, fh);
85 s[strlen (s) - 1] = '\0';
87 #ifdef HAVE_SIGNAL
88 signal (SIGQUIT, SIG_DFL);
89 signal (SIGCONT, SIG_DFL);
90 #endif
92 rc = tty_set_echo (1);
93 if (rc != SHISHI_OK)
94 return rc;
96 return SHISHI_OK;
99 int
100 shishi_prompt_password_raw (FILE * in, char *s, int size,
101 FILE * out, char *format, ...)
103 va_list ap;
104 int rc;
106 va_start (ap, format);
107 vfprintf (out, format, ap);
108 fflush (out);
109 va_end (ap);
111 rc = shishi_read_password (in, s, size);
113 fprintf (out, "\n");
115 return rc;
119 shishi_prompt_password (Shishi * handle,
120 FILE * in, char *s, int size,
121 FILE * out, char *format, ...)
123 char *p;
124 va_list ap;
125 int rc;
127 #ifdef WITH_STRINGPREP
128 if (VERBOSE (handle))
130 printf ("Libstringprep thinks your locale is `%s'.\n",
131 stringprep_locale_charset ());
133 #endif
135 va_start (ap, format);
136 vfprintf (out, format, ap);
137 fflush (out);
138 va_end (ap);
140 rc = shishi_read_password (in, s, size);
142 fprintf (out, "\n");
144 if (rc != SHISHI_OK)
145 return rc;
147 if (VERBOSE (handle))
149 size_t i;
150 printf ("Read password (length %d): ", strlen (s));
151 for (i = 0; i < strlen (s); i++)
152 printf ("%02x ", s[i] & 0xFF);
153 printf ("\n");
156 if (handle->stringprocess
157 && strcasecmp (handle->stringprocess, "none") != 0)
158 #ifdef WITH_STRINGPREP
160 if (strcasecmp (handle->stringprocess, "stringprep") == 0)
161 p = stringprep_locale_to_utf8 (s);
162 else
163 p = stringprep_convert (s, handle->stringprocess,
164 stringprep_locale_charset ());
166 if (p)
168 strncpy (s, p, size);
169 s[size - 1] = '\0';
170 free (p);
173 if (VERBOSE (handle))
175 size_t i;
176 printf ("Password converted to %s (length %d): ",
177 strcasecmp (handle->stringprocess, "stringprep") == 0 ?
178 "UTF-8" : handle->stringprocess, strlen (s));
179 for (i = 0; i < strlen (s); i++)
180 printf ("%02x ", s[i] & 0xFF);
181 printf ("\n");
184 if (strcasecmp (handle->stringprocess, "stringprep") == 0)
186 rc = stringprep_kerberos5 (s, size);
187 if (rc != SHISHI_OK)
188 return rc;
190 if (VERBOSE (handle))
192 size_t i;
193 printf ("Stringprep'ed password (length %d): ", strlen (s));
194 for (i = 0; i < strlen (s); i++)
195 printf ("%02x ", s[i] & 0xFF);
196 printf ("\n");
201 #else
202 shishi_warn (handle, "Password string processing (%s) disabled",
203 handle->stringprocess);
204 #endif
206 return SHISHI_OK;