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

auth-bsdauth.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 #include "includes.h"
00025 RCSID("$OpenBSD: auth-bsdauth.c,v 1.6 2005/01/19 13:11:47 dtucker Exp $");
00026 
00027 #ifdef BSD_AUTH
00028 #include "xmalloc.h"
00029 #include "auth.h"
00030 #include "log.h"
00031 #include "monitor_wrap.h"
00032 
00033 static void *
00034 bsdauth_init_ctx(Authctxt *authctxt)
00035 {
00036         return authctxt;
00037 }
00038 
00039 int
00040 bsdauth_query(void *ctx, char **name, char **infotxt,
00041    u_int *numprompts, char ***prompts, u_int **echo_on)
00042 {
00043         Authctxt *authctxt = ctx;
00044         char *challenge = NULL;
00045 
00046         if (authctxt->as != NULL) {
00047                 debug2("bsdauth_query: try reuse session");
00048                 challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
00049                 if (challenge == NULL) {
00050                         auth_close(authctxt->as);
00051                         authctxt->as = NULL;
00052                 }
00053         }
00054 
00055         if (challenge == NULL) {
00056                 debug2("bsdauth_query: new bsd auth session");
00057                 debug3("bsdauth_query: style %s",
00058                     authctxt->style ? authctxt->style : "<default>");
00059                 authctxt->as = auth_userchallenge(authctxt->user,
00060                     authctxt->style, "auth-ssh", &challenge);
00061                 if (authctxt->as == NULL)
00062                         challenge = NULL;
00063                 debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
00064         }
00065 
00066         if (challenge == NULL)
00067                 return -1;
00068 
00069         *name = xstrdup("");
00070         *infotxt = xstrdup("");
00071         *numprompts = 1;
00072         *prompts = xmalloc(*numprompts * sizeof(char *));
00073         *echo_on = xmalloc(*numprompts * sizeof(u_int));
00074         (*echo_on)[0] = 0;
00075         (*prompts)[0] = xstrdup(challenge);
00076 
00077         return 0;
00078 }
00079 
00080 int
00081 bsdauth_respond(void *ctx, u_int numresponses, char **responses)
00082 {
00083         Authctxt *authctxt = ctx;
00084         int authok;
00085 
00086         if (!authctxt->valid)
00087                 return -1;
00088 
00089         if (authctxt->as == 0)
00090                 error("bsdauth_respond: no bsd auth session");
00091 
00092         if (numresponses != 1)
00093                 return -1;
00094 
00095         authok = auth_userresponse(authctxt->as, responses[0], 0);
00096         authctxt->as = NULL;
00097         debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
00098 
00099         return (authok == 0) ? -1 : 0;
00100 }
00101 
00102 static void
00103 bsdauth_free_ctx(void *ctx)
00104 {
00105         Authctxt *authctxt = ctx;
00106 
00107         if (authctxt && authctxt->as) {
00108                 auth_close(authctxt->as);
00109                 authctxt->as = NULL;
00110         }
00111 }
00112 
00113 KbdintDevice bsdauth_device = {
00114         "bsdauth",
00115         bsdauth_init_ctx,
00116         bsdauth_query,
00117         bsdauth_respond,
00118         bsdauth_free_ctx
00119 };
00120 
00121 KbdintDevice mm_bsdauth_device = {
00122         "bsdauth",
00123         bsdauth_init_ctx,
00124         mm_bsdauth_query,
00125         mm_bsdauth_respond,
00126         bsdauth_free_ctx
00127 };
00128 #endif

© sourcejam.com 2005-2008