Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

auth-chall.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *
00013  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00014  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00015  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00016  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00017  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00018  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00019  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00020  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00021  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00022  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00023  */
00024 
00025 #include "includes.h"
00026 RCSID("$OpenBSD: auth-chall.c,v 1.9 2003/11/03 09:03:37 djm Exp $");
00027 
00028 #include "auth.h"
00029 #include "log.h"
00030 #include "xmalloc.h"
00031 #include "servconf.h"
00032 
00033 /* limited protocol v1 interface to kbd-interactive authentication */
00034 
00035 extern KbdintDevice *devices[];
00036 static KbdintDevice *device;
00037 extern ServerOptions options;
00038 
00039 char *
00040 get_challenge(Authctxt *authctxt)
00041 {
00042         char *challenge, *name, *info, **prompts;
00043         u_int i, numprompts;
00044         u_int *echo_on;
00045 
00046 #ifdef USE_PAM
00047         if (!options.use_pam)
00048                 remove_kbdint_device("pam");
00049 #endif
00050 
00051         device = devices[0]; /* we always use the 1st device for protocol 1 */
00052         if (device == NULL)
00053                 return NULL;
00054         if ((authctxt->kbdintctxt = device->init_ctx(authctxt)) == NULL)
00055                 return NULL;
00056         if (device->query(authctxt->kbdintctxt, &name, &info,
00057             &numprompts, &prompts, &echo_on)) {
00058                 device->free_ctx(authctxt->kbdintctxt);
00059                 authctxt->kbdintctxt = NULL;
00060                 return NULL;
00061         }
00062         if (numprompts < 1)
00063                 fatal("get_challenge: numprompts < 1");
00064         challenge = xstrdup(prompts[0]);
00065         for (i = 0; i < numprompts; i++)
00066                 xfree(prompts[i]);
00067         xfree(prompts);
00068         xfree(name);
00069         xfree(echo_on);
00070         xfree(info);
00071 
00072         return (challenge);
00073 }
00074 int
00075 verify_response(Authctxt *authctxt, const char *response)
00076 {
00077         char *resp[1], *name, *info, **prompts;
00078         u_int i, numprompts, *echo_on;
00079         int authenticated = 0;
00080 
00081         if (device == NULL)
00082                 return 0;
00083         if (authctxt->kbdintctxt == NULL)
00084                 return 0;
00085         resp[0] = (char *)response;
00086         switch (device->respond(authctxt->kbdintctxt, 1, resp)) {
00087         case 0: /* Success */
00088                 authenticated = 1;
00089                 break;
00090         case 1: /* Postponed - retry with empty query for PAM */
00091                 if ((device->query(authctxt->kbdintctxt, &name, &info,
00092                     &numprompts, &prompts, &echo_on)) != 0)
00093                         break;
00094                 if (numprompts == 0 &&
00095                     device->respond(authctxt->kbdintctxt, 0, resp) == 0)
00096                         authenticated = 1;
00097 
00098                 for (i = 0; i < numprompts; i++)
00099                         xfree(prompts[i]);
00100                 xfree(prompts);
00101                 xfree(name);
00102                 xfree(echo_on);
00103                 xfree(info);
00104                 break;
00105         }
00106         device->free_ctx(authctxt->kbdintctxt);
00107         authctxt->kbdintctxt = NULL;
00108         return authenticated;
00109 }
00110 void
00111 abandon_challenge_response(Authctxt *authctxt)
00112 {
00113         if (authctxt->kbdintctxt != NULL) {
00114                 device->free_ctx(authctxt->kbdintctxt);
00115                 authctxt->kbdintctxt = NULL;
00116         }
00117 }

© sourcejam.com 2005-2008