1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "includes.h"
RCSID("$Id: auth2-pam.c,v 1.14 2002/06/28 16:48:12 mouring Exp $");
#pragma ident "%Z%%M% %I% %E% SMI"
#ifdef USE_PAM
#include <security/pam_appl.h>
#include "ssh.h"
#include "ssh2.h"
#include "auth.h"
#include "auth-pam.h"
#include "auth-options.h"
#include "packet.h"
#include "xmalloc.h"
#include "dispatch.h"
#include "canohost.h"
#include "log.h"
#include "servconf.h"
#include "monitor_wrap.h"
#include "misc.h"
#ifdef HAVE_BSM
#include "bsmaudit.h"
#endif /* HAVE_BSM */
extern u_int utmp_len;
extern ServerOptions options;
extern Authmethod method_kbdint;
extern Authmethod method_passwd;
#define SSHD_PAM_KBDINT_SVC "sshd-kbdint"
static int do_pam_conv_kbd_int(int num_msg,
struct pam_message **msg, struct pam_response **resp,
void *appdata_ptr);
static void input_userauth_info_response_pam(int type,
u_int32_t seqnr,
void *ctxt);
static struct pam_conv conv2 = {
do_pam_conv_kbd_int,
NULL,
};
static void do_pam_kbdint_cleanup(pam_handle_t *pamh);
static void do_pam_kbdint(Authctxt *authctxt);
void
auth2_pam(Authctxt *authctxt)
{
if (authctxt->user == NULL)
fatal("auth2_pam: internal error: no user");
if (authctxt->method == NULL)
fatal("auth2_pam: internal error: no method");
conv2.appdata_ptr = authctxt;
new_start_pam(authctxt, &conv2);
authctxt->method->method_data = NULL; /* freed in the conv func */
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
&input_userauth_info_response_pam);
/*
* Since password userauth and keyboard-interactive userauth
* both use PAM, and since keyboard-interactive is so much
* better than password userauth, we should not allow the user
* to try password userauth after trying keyboard-interactive.
*/
if (method_passwd.enabled)
*method_passwd.enabled = 0;
do_pam_kbdint(authctxt);
dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
}
static void
do_pam_kbdint(Authctxt *authctxt)
{
int retval, retval2;
pam_handle_t *pamh = authctxt->pam->h;
const char *where = "authenticating";
debug2("Calling pam_authenticate()");
if ((retval = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
goto cleanup;
debug2("kbd-int: pam_authenticate() succeeded");
where = "authorizing";
retval = pam_acct_mgmt(pamh, 0);
if (retval == PAM_NEW_AUTHTOK_REQD) {
if (authctxt->valid && authctxt->pw != NULL) {
/*
* Can't use temporarily_use_uid() and restore_uid()
* here because we need (euid == 0 && ruid == pw_uid)
* whereas temporarily_use_uid() arranges for
* (suid = 0 && euid == pw_uid && ruid == pw_uid).
*/
(void) setreuid(authctxt->pw->pw_uid, -1);
debug2("kbd-int: changing expired password");
where = "changing authentication tokens (password)";
retval = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
audit_sshd_chauthtok(retval, authctxt->pw->pw_uid,
authctxt->pw->pw_gid);
(void) setreuid(0, -1);
} else {
retval = PAM_PERM_DENIED;
}
}
if (retval != PAM_SUCCESS)
goto cleanup;
authctxt->pam->state |= PAM_S_DONE_ACCT_MGMT;
retval = finish_userauth_do_pam(authctxt);
if (retval != PAM_SUCCESS)
goto cleanup;
/*
* PAM handle stays around so we can call pam_close_session()
* on it later.
*/
authctxt->method->authenticated = 1;
debug2("kbd-int: success (pam->state == %x)", authctxt->pam->state);
return;
cleanup:
/*
* Check for abandonment and cleanup. When kbdint is abandoned
* authctxt->pam->h is NULLed and by this point a new handle may
* be allocated.
*/
if (authctxt->pam->h != pamh) {
log("Keyboard-interactive (PAM) userauth abandoned "
"while %s", where);
if ((retval2 = pam_end(pamh, retval)) != PAM_SUCCESS) {
log("Cannot close PAM handle after "
"kbd-int userauth abandonment[%d]: %.200s",
retval2, PAM_STRERROR(pamh, retval2));
}
authctxt->method->abandoned = 1;
/*
* Avoid double counting; these are incremented in
* kbdint_pam_abandon() so that they reflect the correct
* count when userauth_finish() is called before
* unwinding the dispatch_run() loop, but they are
* incremented again in input_userauth_request() when
* the loop is unwound, right here.
*/
if (authctxt->method->abandons)
authctxt->method->abandons--;
if (authctxt->method->attempts)
authctxt->method->attempts--;
}
else {
/* Save error value for pam_end() */
authctxt->pam->last_pam_retval = retval;
log("Keyboard-interactive (PAM) userauth failed[%d] "
"while %s: %.200s", retval, where,
PAM_STRERROR(pamh, retval));
/* pam handle can be reused elsewhere, so no pam_end() here */
}
return;
}
static int
do_pam_conv_kbd_int(int num_msg, struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr)
{
int i, j;
char *text;
Convctxt *conv_ctxt;
Authctxt *authctxt = (Authctxt *)appdata_ptr;
if (!authctxt || !authctxt->method) {
debug("Missing state during PAM conversation");
return PAM_CONV_ERR;
}
conv_ctxt = xmalloc(sizeof(Convctxt));
(void) memset(conv_ctxt, 0, sizeof(Convctxt));
conv_ctxt->finished = 0;
conv_ctxt->num_received = 0;
conv_ctxt->num_expected = 0;
conv_ctxt->prompts = xmalloc(sizeof(int) * num_msg);
conv_ctxt->responses = xmalloc(sizeof(struct pam_response) * num_msg);
(void) memset(conv_ctxt->responses, 0, sizeof(struct pam_response) * num_msg);
text = NULL;
for (i = 0, conv_ctxt->num_expected = 0; i < num_msg; i++) {
int style = PAM_MSG_MEMBER(msg, i, msg_style);
switch (style) {
case PAM_PROMPT_ECHO_ON:
debug2("PAM echo on prompt: %s",
PAM_MSG_MEMBER(msg, i, msg));
conv_ctxt->num_expected++;
break;
case PAM_PROMPT_ECHO_OFF:
debug2("PAM echo off prompt: %s",
PAM_MSG_MEMBER(msg, i, msg));
conv_ctxt->num_expected++;
break;
case PAM_TEXT_INFO:
debug2("PAM text info prompt: %s",
PAM_MSG_MEMBER(msg, i, msg));
message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
break;
case PAM_ERROR_MSG:
debug2("PAM error prompt: %s",
PAM_MSG_MEMBER(msg, i, msg));
message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
break;
default:
/* Capture all these messages to be sent at once */
message_cat(&text, PAM_MSG_MEMBER(msg, i, msg));
break;
}
}
if (conv_ctxt->num_expected == 0 && text == NULL) {
xfree(conv_ctxt);
return PAM_SUCCESS;
}
authctxt->method->method_data = (void *) conv_ctxt;
packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
packet_put_cstring(""); /* Name */
packet_put_cstring(text ? text : ""); /* Instructions */
packet_put_cstring(""); /* Language */
packet_put_int(conv_ctxt->num_expected);
if (text)
xfree(text);
for (i = 0, j = 0; i < num_msg; i++) {
int style = PAM_MSG_MEMBER(msg, i, msg_style);
/* Skip messages which don't need a reply */
if (style != PAM_PROMPT_ECHO_ON && style != PAM_PROMPT_ECHO_OFF)
continue;
conv_ctxt->prompts[j++] = i;
packet_put_cstring(PAM_MSG_MEMBER(msg, i, msg));
packet_put_char(style == PAM_PROMPT_ECHO_ON);
}
packet_send();
packet_write_wait();
/*
* Here the dispatch_run() loop is nested. It should be unwound
* if keyboard-interactive userauth is abandoned (or restarted;
* same thing).
*
* The condition for breaking out of the nested dispatch_run() loop is
* ((got kbd-int info reponse) || (kbd-int abandoned))
*
* conv_ctxt->finished is set in either of those cases.
*
* When abandonment is detected the conv_ctxt->finished is set as
* is conv_ctxt->abandoned, causing this function to signal
* userauth nested dispatch_run() loop unwinding and to return
* PAM_CONV_ERR;
*/
debug2("Nesting dispatch_run loop");
dispatch_run(DISPATCH_BLOCK, &conv_ctxt->finished, appdata_ptr);
debug2("Nested dispatch_run loop exited");
if (conv_ctxt->abandoned) {
authctxt->unwind_dispatch_loop = 1;
xfree(conv_ctxt);
debug("PAM conv function returns PAM_CONV_ERR");
return PAM_CONV_ERR;
}
if (conv_ctxt->num_received == conv_ctxt->num_expected) {
*resp = conv_ctxt->responses;
xfree(conv_ctxt);
debug("PAM conv function returns PAM_SUCCESS");
return PAM_SUCCESS;
}
debug("PAM conv function returns PAM_CONV_ERR");
xfree(conv_ctxt);
return PAM_CONV_ERR;
}
static void
input_userauth_info_response_pam(int type, u_int32_t seqnr, void *ctxt)
{
Authctxt *authctxt = ctxt;
Convctxt *conv_ctxt;
unsigned int nresp = 0, rlen = 0, i = 0;
char *resp;
if (authctxt == NULL)
fatal("input_userauth_info_response_pam: no authentication context");
/* Check for spurious/unexpected info response */
if (method_kbdint.method_data == NULL) {
debug("input_userauth_info_response_pam: no method context");
return;
}
conv_ctxt = (Convctxt *) method_kbdint.method_data;
nresp = packet_get_int(); /* Number of responses. */
debug("got %d responses", nresp);
#if 0
if (nresp != conv_ctxt->num_expected)
fatal("%s: Received incorrect number of responses "
"(expected %d, received %u)", __func__,
conv_ctxt->num_expected, nresp);
#endif
if (nresp > 100)
fatal("%s: too many replies", __func__);
for (i = 0; i < nresp && i < conv_ctxt->num_expected ; i++) {
int j = conv_ctxt->prompts[i];
resp = packet_get_string(&rlen);
if (i < conv_ctxt->num_expected) {
conv_ctxt->responses[j].resp_retcode = PAM_SUCCESS;
conv_ctxt->responses[j].resp = xstrdup(resp);
conv_ctxt->num_received++;
}
xfree(resp);
}
if (nresp < conv_ctxt->num_expected)
fatal("%s: too few replies", __func__);
/* XXX - This could make a covert channel... */
if (nresp > conv_ctxt->num_expected)
debug("Ignoring additional PAM replies");
conv_ctxt->finished = 1;
packet_check_eom();
}
#if 0
int
kbdint_pam_abandon_chk(Authctxt *authctxt, Authmethod *method)
{
if (!method)
return 0; /* fatal(), really; it'll happen somewhere else */
if (!method->method_data)
return 0;
return 1;
}
#endif
void
kbdint_pam_abandon(Authctxt *authctxt, Authmethod *method)
{
Convctxt *conv_ctxt;
/*
* But, if it ever becomes desirable and possible to support
* kbd-int userauth abandonment, here's what must be done.
*/
if (!method)
return;
if (!method->method_data)
return;
conv_ctxt = (Convctxt *) method->method_data;
/* dispatch_run() loop will exit */
conv_ctxt->abandoned = 1;
conv_ctxt->finished = 1;
/*
* The method_data will be free in the corresponding, active
* conversation function
*/
method->method_data = NULL;
/* update counts that can't be updated elsewhere */
method->abandons++;
method->attempts++;
/* Finally, we cannot re-use the current current PAM handle */
authctxt->pam->h = NULL; /* Let the conv function cleanup */
}
#endif
|