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