Home | History | Annotate | Line # | Download | only in dist
auth-bsdauth.c revision 1.2.26.1
      1  1.2.26.1       riz /*	$NetBSD: auth-bsdauth.c,v 1.2.26.1 2015/04/30 06:07:30 riz Exp $	*/
      2  1.2.26.1       riz /* $OpenBSD: auth-bsdauth.c,v 1.13 2014/06/24 01:13:21 djm Exp $ */
      3       1.1  christos /*
      4       1.1  christos  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
      5       1.1  christos  *
      6       1.1  christos  * Redistribution and use in source and binary forms, with or without
      7       1.1  christos  * modification, are permitted provided that the following conditions
      8       1.1  christos  * are met:
      9       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  christos  *    documentation and/or other materials provided with the distribution.
     14       1.1  christos  *
     15       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16       1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17       1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18       1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19       1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20       1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21       1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22       1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23       1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24       1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25       1.1  christos  */
     26       1.1  christos 
     27       1.2  christos #include "includes.h"
     28  1.2.26.1       riz __RCSID("$NetBSD: auth-bsdauth.c,v 1.2.26.1 2015/04/30 06:07:30 riz Exp $");
     29       1.1  christos #include <sys/types.h>
     30  1.2.26.1       riz #include <stdarg.h>
     31  1.2.26.1       riz #include <stdio.h>
     32       1.1  christos 
     33       1.2  christos #ifdef BSD_AUTH
     34       1.1  christos #include "xmalloc.h"
     35       1.1  christos #include "key.h"
     36       1.1  christos #include "hostfile.h"
     37       1.1  christos #include "auth.h"
     38       1.1  christos #include "log.h"
     39       1.1  christos #include "buffer.h"
     40       1.1  christos #ifdef GSSAPI
     41       1.1  christos #include "ssh-gss.h"
     42       1.1  christos #endif
     43       1.1  christos #include "monitor_wrap.h"
     44       1.1  christos 
     45       1.1  christos static void *
     46       1.1  christos bsdauth_init_ctx(Authctxt *authctxt)
     47       1.1  christos {
     48       1.1  christos 	return authctxt;
     49       1.1  christos }
     50       1.1  christos 
     51       1.1  christos int
     52       1.1  christos bsdauth_query(void *ctx, char **name, char **infotxt,
     53       1.1  christos    u_int *numprompts, char ***prompts, u_int **echo_on)
     54       1.1  christos {
     55       1.1  christos 	Authctxt *authctxt = ctx;
     56       1.1  christos 	char *challenge = NULL;
     57       1.1  christos 
     58  1.2.26.1       riz 	*infotxt = NULL;
     59  1.2.26.1       riz 	*numprompts = 0;
     60  1.2.26.1       riz 	*prompts = NULL;
     61  1.2.26.1       riz 	*echo_on = NULL;
     62  1.2.26.1       riz 
     63       1.1  christos 	if (authctxt->as != NULL) {
     64       1.1  christos 		debug2("bsdauth_query: try reuse session");
     65       1.1  christos 		challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
     66       1.1  christos 		if (challenge == NULL) {
     67       1.1  christos 			auth_close(authctxt->as);
     68       1.1  christos 			authctxt->as = NULL;
     69       1.1  christos 		}
     70       1.1  christos 	}
     71       1.1  christos 
     72       1.1  christos 	if (challenge == NULL) {
     73       1.1  christos 		debug2("bsdauth_query: new bsd auth session");
     74       1.1  christos 		debug3("bsdauth_query: style %s",
     75       1.1  christos 		    authctxt->style ? authctxt->style : "<default>");
     76       1.1  christos 		authctxt->as = auth_userchallenge(authctxt->user,
     77       1.1  christos 		    authctxt->style, "auth-ssh", &challenge);
     78       1.1  christos 		if (authctxt->as == NULL)
     79       1.1  christos 			challenge = NULL;
     80       1.1  christos 		debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
     81       1.1  christos 	}
     82       1.1  christos 
     83       1.1  christos 	if (challenge == NULL)
     84       1.1  christos 		return -1;
     85       1.1  christos 
     86       1.1  christos 	*name = xstrdup("");
     87       1.1  christos 	*infotxt = xstrdup("");
     88       1.1  christos 	*numprompts = 1;
     89       1.1  christos 	*prompts = xcalloc(*numprompts, sizeof(char *));
     90       1.1  christos 	*echo_on = xcalloc(*numprompts, sizeof(u_int));
     91       1.1  christos 	(*prompts)[0] = xstrdup(challenge);
     92       1.1  christos 
     93       1.1  christos 	return 0;
     94       1.1  christos }
     95       1.1  christos 
     96       1.1  christos int
     97       1.1  christos bsdauth_respond(void *ctx, u_int numresponses, char **responses)
     98       1.1  christos {
     99       1.1  christos 	Authctxt *authctxt = ctx;
    100       1.1  christos 	int authok;
    101       1.1  christos 
    102       1.1  christos 	if (!authctxt->valid)
    103       1.1  christos 		return -1;
    104       1.1  christos 
    105       1.1  christos 	if (authctxt->as == 0)
    106       1.1  christos 		error("bsdauth_respond: no bsd auth session");
    107       1.1  christos 
    108       1.1  christos 	if (numresponses != 1)
    109       1.1  christos 		return -1;
    110       1.1  christos 
    111       1.1  christos 	authok = auth_userresponse(authctxt->as, responses[0], 0);
    112       1.1  christos 	authctxt->as = NULL;
    113       1.1  christos 	debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
    114       1.1  christos 
    115       1.1  christos 	return (authok == 0) ? -1 : 0;
    116       1.1  christos }
    117       1.1  christos 
    118       1.1  christos static void
    119       1.1  christos bsdauth_free_ctx(void *ctx)
    120       1.1  christos {
    121       1.1  christos 	Authctxt *authctxt = ctx;
    122       1.1  christos 
    123       1.1  christos 	if (authctxt && authctxt->as) {
    124       1.1  christos 		auth_close(authctxt->as);
    125       1.1  christos 		authctxt->as = NULL;
    126       1.1  christos 	}
    127       1.1  christos }
    128       1.1  christos 
    129       1.1  christos KbdintDevice bsdauth_device = {
    130       1.1  christos 	"bsdauth",
    131       1.1  christos 	bsdauth_init_ctx,
    132       1.1  christos 	bsdauth_query,
    133       1.1  christos 	bsdauth_respond,
    134       1.1  christos 	bsdauth_free_ctx
    135       1.1  christos };
    136       1.1  christos 
    137       1.1  christos KbdintDevice mm_bsdauth_device = {
    138       1.1  christos 	"bsdauth",
    139       1.1  christos 	bsdauth_init_ctx,
    140       1.1  christos 	mm_bsdauth_query,
    141       1.1  christos 	mm_bsdauth_respond,
    142       1.1  christos 	bsdauth_free_ctx
    143       1.1  christos };
    144       1.2  christos #endif
    145