Debian Codegraph @

src/pam/auth-support/getpin-cb.c

raw symbols SCIP
1 /* getpin-cb.c - getpin Assuan Callback (Poldi)
2 Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH
3
4 This file is part of Poldi.
5
6 Poldi 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.
10
11 Poldi 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 GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <poldi.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <pwd.h>
32 #include <dirent.h>
33 #include <time.h>
34
35 #include <gcrypt.h>
36
37 #include "assuan.h"
38 #include "util/support.h"
39 #include <util/defs.h>
40 #include "util/util.h"
41 #include "util/simplelog.h"
42 #include "auth-support/conv.h"
43
44 #include "ctx.h"
45
46 #include "getpin-cb.h"
47
48
49 /* Query the user through PAM for his PIN. Display INFO to the user.
50 Store the retrieved pin in PIN, which is of size PIN_SIZE. If it
51 does not fit, return error. */
52 static int
53 query_user (poldi_ctx_t ctx, const char *info, char *pin, size_t pin_size)
54 {
55 char *buffer;
56 int rc;
57
58 buffer = NULL;
59 rc = 0;
60
61 while (1) /* Loop until well-formed PIN retrieved. */
62 {
63 /* Retrieve PIN through PAM. */
64 rc = conv_ask (ctx->conv, 1, &buffer, info);
65 if (rc)
66 goto out;
67
68 /* Do some basic checks on the entered PIN. FIXME: hard-coded
69 values! Is this really the correct place for these checks?
70 Shouldn't they be done in scdaemon itself? -mo */
71
72 if (strlen (buffer) < 6) /* FIXME? is it really minimum of 6 bytes? */
73 {
74 log_msg_error (ctx->loghandle, "PIN too short");
75 conv_tell (ctx->conv, "%s", _("PIN too short"));
76 }
77 else
78 break;
79 }
80
81 if (strlen (buffer) >= pin_size)
82 {
83 log_msg_error (ctx->loghandle, "PIN too long for buffer!");
84 rc = gpg_error (GPG_ERR_INV_DATA); /* ? */
85 goto out;
86 }
87
88 strncpy (pin, buffer, pin_size - 1);
89 pin[pin_size-1] = 0;
90
91 out:
92
93 return rc;
94 }
95
96 /* Pop up a message window similar to the confirm one but keep it open
97 until agent_popup_message_stop has been called. It is crucial for
98 the caller to make sure that the stop function gets called as soon
99 as the message is not anymore required because the message is
100 system modal and all other attempts to use the pinentry will fail
101 (after a timeout). */
102 static int
103 keypad_mode_enter (poldi_ctx_t ctx, const char *info)
104 {
105 int rc;
106
107 rc = conv_tell (ctx->conv, info);
108
109 return rc;
110 }
111
112 static int
113 keypad_mode_leave (poldi_ctx_t ctx)
114 {
115 return 0;
116 }
117
118 /* This function is taken from pinentry.c. */
119 /* Note, that it is sufficient to allocate the target string D as
120 long as the source string S, i.e.: strlen(s)+1; */
121 static void
122 strcpy_escaped (char *d, const char *s)
123 {
124 while (*s)
125 {
126 if (*s == '%' && s[1] && s[2])
127 {
128 s++;
129 *d++ = xtoi_2 ( s);
130 s += 2;
131 }
132 else
133 *d++ = *s++;
134 }
135 *d = 0;
136 }
137
138 /* Unescape special characters in INFO and write unescaped string into
139 newly allocated memory in *INFO_FROBBED. Returns proper error
140 code. */
141 static gpg_error_t
142 frob_info_msg (const char *info, char **info_frobbed)
143 {
144 gpg_error_t err = 0;
145
146 *info_frobbed = xtrymalloc (strlen (info) + 1);
147 if (!*info_frobbed)
148 {
149 err = gpg_error_from_errno (errno);
150 goto out;
151 }
152
153 strcpy_escaped (*info_frobbed, info);
154
155 out:
156
157 return err;
158 }
159
160 /* Callback used to ask for the PIN which shall be written into BUF.
161 The buf has been allocated by the caller and is of size MAXBUF
162 which includes the terminating null. The function should return an
163 UTF-8 string with the passphrase/PIN, the buffer may optionally be
164 padded with arbitrary characters.
165
166 INFO gets displayed as part of a generic string. However if the
167 first character of INFO is a vertical bar all up to the next
168 verical bar are considered flags and only everything after the
169 second vertical bar gets displayed as the full prompt.
170
171 We don't need/implement the N/A flags. When they occur, we signal
172 an error. */
173 int
174 getpin_cb (void *opaque, const char *info, char *buf, size_t maxbuf)
175 {
176 struct getpin_cb_data *cb_data = opaque;
177 poldi_ctx_t ctx = cb_data->poldi_ctx;
178 char *info_frobbed;
179 int err;
180
181 info_frobbed = NULL;
182 err = 0;
183
184 #if 0
185 /* FIXME: why "< 2"? -mo */
186 if (buf && maxbuf < 2)
187 return gpg_error (GPG_ERR_INV_VALUE);
188 #endif
189
190 /* Older SCDaemons simply send "PIN" as prompt. We do not process
191 this prompt here but use a special case later. */
192 if (info && (strcmp (info, "PIN") != 0))
193 {
194 if (info[0] == '|')
195 {
196 if (info[1] == '|')
197 /* Skip "||" at the beginning. */
198 info += 2;
199 else
200 {
201 /* Weird that we received flags - they are neither expected nor
202 implemented here. */
203 log_msg_error (ctx->loghandle,
204 "getpin_cb called with flags set in info string `%s'\n",
205 info);
206 err = gpg_error (GPG_ERR_INV_VALUE); /* FIXME? */
207 goto out;
208 }
209 }
210 err = frob_info_msg (info, &info_frobbed);
211 if (err)
212 {
213 log_msg_error (ctx->loghandle,
214 "frob_info_msg failed for info msg of size %u\n",
215 (unsigned int) strlen (info));
216 goto out;
217 }
218 }
219
220 if (buf)
221 {
222 /* BUF being non-zero means we are not using a keypad. */
223
224 if (info_frobbed)
225 err = query_user (ctx, info_frobbed, buf, maxbuf);
226 else
227 /* Use string which is more user friendly. */
228 err = query_user (ctx, _("Please enter the PIN:"), buf, maxbuf);
229 }
230 else
231 {
232 /* Special handling for keypad mode hack. */
233
234 /* If BUF has been passed as NULL, we are in keypad mode: the
235 callback notifies the user and immediately returns. */
236 if (maxbuf == 0)
237 {
238 /* Close the "pinentry". */
239 err = keypad_mode_leave (ctx);
240 }
241 else if (maxbuf == 1)
242 {
243 /* Open the "pinentry". */
244 if (info_frobbed)
245 err = keypad_mode_enter (ctx, info_frobbed);
246 else
247 err = keypad_mode_enter (ctx, _("Please enter the PIN:"));
248 }
249 else
250 err = gpg_error (GPG_ERR_INV_VALUE); /* FIXME: must signal
251 internal error(!)?
252 -mo */
253 }
254
255 out:
256
257 xfree (info_frobbed);
258
259 return err;
260 }
261
262 /* END */