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