Annotation of src/usr.bin/ssh/auth2-chall.c, Revision 1.30
1.30 ! deraadt 1: /* $OpenBSD: auth2-chall.c,v 1.29 2006/08/01 23:22:47 stevesk Exp $ */
1.1 markus 2: /*
1.3 deraadt 3: * Copyright (c) 2001 Markus Friedl. All rights reserved.
1.5 markus 4: * Copyright (c) 2001 Per Allansson. All rights reserved.
1.1 markus 5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
1.30 ! deraadt 26:
! 27: #include <sys/types.h>
1.28 stevesk 28:
1.29 stevesk 29: #include <stdio.h>
1.28 stevesk 30: #include <string.h>
1.1 markus 31:
1.30 ! deraadt 32: #include "xmalloc.h"
1.1 markus 33: #include "ssh2.h"
1.30 ! deraadt 34: #include "key.h"
! 35: #include "hostfile.h"
1.1 markus 36: #include "auth.h"
1.16 markus 37: #include "buffer.h"
1.1 markus 38: #include "packet.h"
39: #include "dispatch.h"
1.2 markus 40: #include "log.h"
1.1 markus 41:
1.7 itojun 42: static int auth2_challenge_start(Authctxt *);
43: static int send_userauth_info_request(Authctxt *);
1.13 markus 44: static void input_userauth_info_response(int, u_int32_t, void *);
1.5 markus 45:
46: #ifdef BSD_AUTH
47: extern KbdintDevice bsdauth_device;
48: #else
49: #ifdef SKEY
50: extern KbdintDevice skey_device;
51: #endif
52: #endif
53:
54: KbdintDevice *devices[] = {
55: #ifdef BSD_AUTH
56: &bsdauth_device,
57: #else
58: #ifdef SKEY
59: &skey_device,
60: #endif
61: #endif
62: NULL
63: };
64:
65: typedef struct KbdintAuthctxt KbdintAuthctxt;
66: struct KbdintAuthctxt
67: {
68: char *devices;
69: void *ctxt;
70: KbdintDevice *device;
1.19 markus 71: u_int nreq;
1.5 markus 72: };
73:
1.7 itojun 74: static KbdintAuthctxt *
1.5 markus 75: kbdint_alloc(const char *devs)
76: {
77: KbdintAuthctxt *kbdintctxt;
1.16 markus 78: Buffer b;
1.5 markus 79: int i;
80:
81: kbdintctxt = xmalloc(sizeof(KbdintAuthctxt));
82: if (strcmp(devs, "") == 0) {
1.16 markus 83: buffer_init(&b);
1.5 markus 84: for (i = 0; devices[i]; i++) {
1.16 markus 85: if (buffer_len(&b) > 0)
86: buffer_append(&b, ",", 1);
87: buffer_append(&b, devices[i]->name,
88: strlen(devices[i]->name));
1.5 markus 89: }
1.16 markus 90: buffer_append(&b, "\0", 1);
91: kbdintctxt->devices = xstrdup(buffer_ptr(&b));
92: buffer_free(&b);
1.5 markus 93: } else {
94: kbdintctxt->devices = xstrdup(devs);
95: }
1.16 markus 96: debug("kbdint_alloc: devices '%s'", kbdintctxt->devices);
1.5 markus 97: kbdintctxt->ctxt = NULL;
98: kbdintctxt->device = NULL;
1.19 markus 99: kbdintctxt->nreq = 0;
1.5 markus 100:
101: return kbdintctxt;
102: }
1.7 itojun 103: static void
1.5 markus 104: kbdint_reset_device(KbdintAuthctxt *kbdintctxt)
105: {
106: if (kbdintctxt->ctxt) {
107: kbdintctxt->device->free_ctx(kbdintctxt->ctxt);
108: kbdintctxt->ctxt = NULL;
109: }
110: kbdintctxt->device = NULL;
111: }
1.7 itojun 112: static void
1.5 markus 113: kbdint_free(KbdintAuthctxt *kbdintctxt)
114: {
115: if (kbdintctxt->device)
116: kbdint_reset_device(kbdintctxt);
117: if (kbdintctxt->devices) {
118: xfree(kbdintctxt->devices);
119: kbdintctxt->devices = NULL;
120: }
121: xfree(kbdintctxt);
122: }
123: /* get next device */
1.7 itojun 124: static int
1.5 markus 125: kbdint_next_device(KbdintAuthctxt *kbdintctxt)
126: {
127: size_t len;
128: char *t;
129: int i;
130:
131: if (kbdintctxt->device)
132: kbdint_reset_device(kbdintctxt);
133: do {
134: len = kbdintctxt->devices ?
135: strcspn(kbdintctxt->devices, ",") : 0;
136:
137: if (len == 0)
138: break;
139: for (i = 0; devices[i]; i++)
140: if (strncmp(kbdintctxt->devices, devices[i]->name, len) == 0)
141: kbdintctxt->device = devices[i];
142: t = kbdintctxt->devices;
143: kbdintctxt->devices = t[len] ? xstrdup(t+len+1) : NULL;
144: xfree(t);
145: debug2("kbdint_next_device: devices %s", kbdintctxt->devices ?
1.24 djm 146: kbdintctxt->devices : "<empty>");
1.5 markus 147: } while (kbdintctxt->devices && !kbdintctxt->device);
148:
149: return kbdintctxt->device ? 1 : 0;
150: }
1.1 markus 151:
152: /*
1.8 markus 153: * try challenge-response, set authctxt->postponed if we have to
1.1 markus 154: * wait for the response.
155: */
156: int
157: auth2_challenge(Authctxt *authctxt, char *devs)
158: {
1.5 markus 159: debug("auth2_challenge: user=%s devs=%s",
160: authctxt->user ? authctxt->user : "<nouser>",
161: devs ? devs : "<no devs>");
162:
1.6 markus 163: if (authctxt->user == NULL || !devs)
1.5 markus 164: return 0;
1.10 deraadt 165: if (authctxt->kbdintctxt == NULL)
1.5 markus 166: authctxt->kbdintctxt = kbdint_alloc(devs);
167: return auth2_challenge_start(authctxt);
168: }
169:
1.9 markus 170: /* unregister kbd-int callbacks and context */
171: void
172: auth2_challenge_stop(Authctxt *authctxt)
173: {
174: /* unregister callback */
175: dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE, NULL);
176: if (authctxt->kbdintctxt != NULL) {
177: kbdint_free(authctxt->kbdintctxt);
178: authctxt->kbdintctxt = NULL;
179: }
180: }
181:
1.5 markus 182: /* side effect: sets authctxt->postponed if a reply was sent*/
183: static int
184: auth2_challenge_start(Authctxt *authctxt)
185: {
186: KbdintAuthctxt *kbdintctxt = authctxt->kbdintctxt;
187:
188: debug2("auth2_challenge_start: devices %s",
189: kbdintctxt->devices ? kbdintctxt->devices : "<empty>");
190:
191: if (kbdint_next_device(kbdintctxt) == 0) {
1.9 markus 192: auth2_challenge_stop(authctxt);
1.5 markus 193: return 0;
194: }
195: debug("auth2_challenge_start: trying authentication method '%s'",
196: kbdintctxt->device->name);
1.1 markus 197:
1.5 markus 198: if ((kbdintctxt->ctxt = kbdintctxt->device->init_ctx(authctxt)) == NULL) {
1.9 markus 199: auth2_challenge_stop(authctxt);
1.1 markus 200: return 0;
1.5 markus 201: }
202: if (send_userauth_info_request(authctxt) == 0) {
1.9 markus 203: auth2_challenge_stop(authctxt);
1.1 markus 204: return 0;
1.5 markus 205: }
1.1 markus 206: dispatch_set(SSH2_MSG_USERAUTH_INFO_RESPONSE,
207: &input_userauth_info_response);
1.5 markus 208:
1.1 markus 209: authctxt->postponed = 1;
210: return 0;
211: }
212:
1.5 markus 213: static int
214: send_userauth_info_request(Authctxt *authctxt)
1.1 markus 215: {
1.5 markus 216: KbdintAuthctxt *kbdintctxt;
217: char *name, *instr, **prompts;
1.23 djm 218: u_int i, *echo_on;
1.5 markus 219:
220: kbdintctxt = authctxt->kbdintctxt;
221: if (kbdintctxt->device->query(kbdintctxt->ctxt,
1.19 markus 222: &name, &instr, &kbdintctxt->nreq, &prompts, &echo_on))
1.5 markus 223: return 0;
1.1 markus 224:
225: packet_start(SSH2_MSG_USERAUTH_INFO_REQUEST);
1.5 markus 226: packet_put_cstring(name);
227: packet_put_cstring(instr);
1.18 deraadt 228: packet_put_cstring(""); /* language not used */
1.19 markus 229: packet_put_int(kbdintctxt->nreq);
230: for (i = 0; i < kbdintctxt->nreq; i++) {
1.5 markus 231: packet_put_cstring(prompts[i]);
232: packet_put_char(echo_on[i]);
233: }
1.1 markus 234: packet_send();
235: packet_write_wait();
1.5 markus 236:
1.19 markus 237: for (i = 0; i < kbdintctxt->nreq; i++)
1.5 markus 238: xfree(prompts[i]);
239: xfree(prompts);
240: xfree(echo_on);
241: xfree(name);
242: xfree(instr);
243: return 1;
1.1 markus 244: }
245:
1.5 markus 246: static void
1.13 markus 247: input_userauth_info_response(int type, u_int32_t seq, void *ctxt)
1.1 markus 248: {
249: Authctxt *authctxt = ctxt;
1.5 markus 250: KbdintAuthctxt *kbdintctxt;
1.23 djm 251: int authenticated = 0, res, len;
252: u_int i, nresp;
1.5 markus 253: char **response = NULL, *method;
1.1 markus 254:
255: if (authctxt == NULL)
256: fatal("input_userauth_info_response: no authctxt");
1.5 markus 257: kbdintctxt = authctxt->kbdintctxt;
258: if (kbdintctxt == NULL || kbdintctxt->ctxt == NULL)
259: fatal("input_userauth_info_response: no kbdintctxt");
260: if (kbdintctxt->device == NULL)
261: fatal("input_userauth_info_response: no device");
1.1 markus 262:
263: authctxt->postponed = 0; /* reset */
264: nresp = packet_get_int();
1.19 markus 265: if (nresp != kbdintctxt->nreq)
266: fatal("input_userauth_info_response: wrong number of replies");
267: if (nresp > 100)
268: fatal("input_userauth_info_response: too many replies");
1.5 markus 269: if (nresp > 0) {
1.26 djm 270: response = xcalloc(nresp, sizeof(char *));
1.5 markus 271: for (i = 0; i < nresp; i++)
272: response[i] = packet_get_string(NULL);
273: }
1.12 markus 274: packet_check_eom();
1.5 markus 275:
1.22 dtucker 276: res = kbdintctxt->device->respond(kbdintctxt->ctxt, nresp, response);
1.5 markus 277:
278: for (i = 0; i < nresp; i++) {
279: memset(response[i], 'r', strlen(response[i]));
280: xfree(response[i]);
281: }
282: if (response)
283: xfree(response);
284:
285: switch (res) {
286: case 0:
287: /* Success! */
1.22 dtucker 288: authenticated = authctxt->valid ? 1 : 0;
1.5 markus 289: break;
290: case 1:
291: /* Authentication needs further interaction */
1.9 markus 292: if (send_userauth_info_request(authctxt) == 1)
293: authctxt->postponed = 1;
1.5 markus 294: break;
295: default:
296: /* Failure! */
297: break;
1.1 markus 298: }
1.5 markus 299:
300: len = strlen("keyboard-interactive") + 2 +
301: strlen(kbdintctxt->device->name);
302: method = xmalloc(len);
1.15 markus 303: snprintf(method, len, "keyboard-interactive/%s",
304: kbdintctxt->device->name);
1.5 markus 305:
306: if (!authctxt->postponed) {
307: if (authenticated) {
1.9 markus 308: auth2_challenge_stop(authctxt);
1.5 markus 309: } else {
310: /* start next device */
311: /* may set authctxt->postponed */
312: auth2_challenge_start(authctxt);
313: }
314: }
1.4 markus 315: userauth_finish(authctxt, authenticated, method);
1.5 markus 316: xfree(method);
1.17 provos 317: }
318:
319: void
320: privsep_challenge_enable(void)
321: {
322: #ifdef BSD_AUTH
323: extern KbdintDevice mm_bsdauth_device;
324: #endif
325: #ifdef SKEY
326: extern KbdintDevice mm_skey_device;
327: #endif
328: /* As long as SSHv1 has devices[0] hard coded this is fine */
329: #ifdef BSD_AUTH
330: devices[0] = &mm_bsdauth_device;
331: #else
332: #ifdef SKEY
333: devices[0] = &mm_skey_device;
334: #endif
335: #endif
1.1 markus 336: }