1*25ae3b00Smarkus /* $OpenBSD: auth-bsdauth.c,v 1.15 2018/07/09 21:35:50 markus Exp $ */
262256e4aSmarkus /*
362256e4aSmarkus * Copyright (c) 2001 Markus Friedl. All rights reserved.
462256e4aSmarkus *
562256e4aSmarkus * Redistribution and use in source and binary forms, with or without
662256e4aSmarkus * modification, are permitted provided that the following conditions
762256e4aSmarkus * are met:
862256e4aSmarkus * 1. Redistributions of source code must retain the above copyright
962256e4aSmarkus * notice, this list of conditions and the following disclaimer.
1062256e4aSmarkus * 2. Redistributions in binary form must reproduce the above copyright
1162256e4aSmarkus * notice, this list of conditions and the following disclaimer in the
1262256e4aSmarkus * documentation and/or other materials provided with the distribution.
1362256e4aSmarkus *
1462256e4aSmarkus * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1562256e4aSmarkus * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1662256e4aSmarkus * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1762256e4aSmarkus * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1862256e4aSmarkus * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1962256e4aSmarkus * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2062256e4aSmarkus * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2162256e4aSmarkus * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2262256e4aSmarkus * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2362256e4aSmarkus * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2462256e4aSmarkus */
25bd9502d5Sderaadt
26bd9502d5Sderaadt #include <sys/types.h>
27ea2d8289Sdjm #include <stdarg.h>
28ea2d8289Sdjm #include <stdio.h>
2962256e4aSmarkus
3062256e4aSmarkus #include "xmalloc.h"
31*25ae3b00Smarkus #include "sshkey.h"
32*25ae3b00Smarkus #include "sshbuf.h"
33bd9502d5Sderaadt #include "hostfile.h"
3462256e4aSmarkus #include "auth.h"
3562256e4aSmarkus #include "log.h"
36bd9502d5Sderaadt #ifdef GSSAPI
37bd9502d5Sderaadt #include "ssh-gss.h"
38bd9502d5Sderaadt #endif
3950c32c83Sprovos #include "monitor_wrap.h"
4062256e4aSmarkus
4162256e4aSmarkus static void *
bsdauth_init_ctx(Authctxt * authctxt)4262256e4aSmarkus bsdauth_init_ctx(Authctxt *authctxt)
4362256e4aSmarkus {
4462256e4aSmarkus return authctxt;
4562256e4aSmarkus }
4662256e4aSmarkus
4750c32c83Sprovos int
bsdauth_query(void * ctx,char ** name,char ** infotxt,u_int * numprompts,char *** prompts,u_int ** echo_on)4862256e4aSmarkus bsdauth_query(void *ctx, char **name, char **infotxt,
4962256e4aSmarkus u_int *numprompts, char ***prompts, u_int **echo_on)
5062256e4aSmarkus {
5162256e4aSmarkus Authctxt *authctxt = ctx;
5262256e4aSmarkus char *challenge = NULL;
5362256e4aSmarkus
54a470cceaSdjm *infotxt = NULL;
55a470cceaSdjm *numprompts = 0;
56a470cceaSdjm *prompts = NULL;
57a470cceaSdjm *echo_on = NULL;
58a470cceaSdjm
5962256e4aSmarkus if (authctxt->as != NULL) {
6062256e4aSmarkus debug2("bsdauth_query: try reuse session");
6162256e4aSmarkus challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
6262256e4aSmarkus if (challenge == NULL) {
6362256e4aSmarkus auth_close(authctxt->as);
6462256e4aSmarkus authctxt->as = NULL;
6562256e4aSmarkus }
6662256e4aSmarkus }
6762256e4aSmarkus
6862256e4aSmarkus if (challenge == NULL) {
6962256e4aSmarkus debug2("bsdauth_query: new bsd auth session");
7062256e4aSmarkus debug3("bsdauth_query: style %s",
7162256e4aSmarkus authctxt->style ? authctxt->style : "<default>");
7262256e4aSmarkus authctxt->as = auth_userchallenge(authctxt->user,
7362256e4aSmarkus authctxt->style, "auth-ssh", &challenge);
7462256e4aSmarkus if (authctxt->as == NULL)
7562256e4aSmarkus challenge = NULL;
7662256e4aSmarkus debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
7762256e4aSmarkus }
7862256e4aSmarkus
7962256e4aSmarkus if (challenge == NULL)
8062256e4aSmarkus return -1;
8162256e4aSmarkus
8262256e4aSmarkus *name = xstrdup("");
8362256e4aSmarkus *infotxt = xstrdup("");
8462256e4aSmarkus *numprompts = 1;
85f846f1e3Sdjm *prompts = xcalloc(*numprompts, sizeof(char *));
86f846f1e3Sdjm *echo_on = xcalloc(*numprompts, sizeof(u_int));
8762256e4aSmarkus (*prompts)[0] = xstrdup(challenge);
8862256e4aSmarkus
8962256e4aSmarkus return 0;
9062256e4aSmarkus }
9162256e4aSmarkus
9250c32c83Sprovos int
bsdauth_respond(void * ctx,u_int numresponses,char ** responses)9362256e4aSmarkus bsdauth_respond(void *ctx, u_int numresponses, char **responses)
9462256e4aSmarkus {
9562256e4aSmarkus Authctxt *authctxt = ctx;
9662256e4aSmarkus int authok;
9762256e4aSmarkus
985686a373Sdtucker if (!authctxt->valid)
995686a373Sdtucker return -1;
1005686a373Sdtucker
1012e96fb62Smmcc if (authctxt->as == NULL)
10262256e4aSmarkus error("bsdauth_respond: no bsd auth session");
10362256e4aSmarkus
10462256e4aSmarkus if (numresponses != 1)
10562256e4aSmarkus return -1;
10662256e4aSmarkus
10762256e4aSmarkus authok = auth_userresponse(authctxt->as, responses[0], 0);
10862256e4aSmarkus authctxt->as = NULL;
10962256e4aSmarkus debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
11062256e4aSmarkus
11162256e4aSmarkus return (authok == 0) ? -1 : 0;
11262256e4aSmarkus }
11362256e4aSmarkus
11462256e4aSmarkus static void
bsdauth_free_ctx(void * ctx)11562256e4aSmarkus bsdauth_free_ctx(void *ctx)
11662256e4aSmarkus {
11762256e4aSmarkus Authctxt *authctxt = ctx;
11862256e4aSmarkus
11962256e4aSmarkus if (authctxt && authctxt->as) {
12062256e4aSmarkus auth_close(authctxt->as);
12162256e4aSmarkus authctxt->as = NULL;
12262256e4aSmarkus }
12362256e4aSmarkus }
12462256e4aSmarkus
12562256e4aSmarkus KbdintDevice bsdauth_device = {
12662256e4aSmarkus "bsdauth",
12762256e4aSmarkus bsdauth_init_ctx,
12862256e4aSmarkus bsdauth_query,
12962256e4aSmarkus bsdauth_respond,
13062256e4aSmarkus bsdauth_free_ctx
13162256e4aSmarkus };
13250c32c83Sprovos
13350c32c83Sprovos KbdintDevice mm_bsdauth_device = {
13450c32c83Sprovos "bsdauth",
13550c32c83Sprovos bsdauth_init_ctx,
13650c32c83Sprovos mm_bsdauth_query,
13750c32c83Sprovos mm_bsdauth_respond,
13850c32c83Sprovos bsdauth_free_ctx
13950c32c83Sprovos };
140