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