xref: /openbsd/usr.bin/ssh/auth.h (revision 09467b48)
1 /* $OpenBSD: auth.h,v 1.100 2019/09/06 05:23:55 djm Exp $ */
2 
3 /*
4  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef AUTH_H
29 #define AUTH_H
30 
31 #include <signal.h>
32 
33 #include <bsd_auth.h>
34 #ifdef KRB5
35 #include <krb5.h>
36 #endif
37 
38 struct passwd;
39 struct ssh;
40 struct sshbuf;
41 struct sshkey;
42 struct sshauthopt;
43 
44 typedef struct Authctxt Authctxt;
45 typedef struct Authmethod Authmethod;
46 typedef struct KbdintDevice KbdintDevice;
47 
48 struct Authctxt {
49 	sig_atomic_t	 success;
50 	int		 authenticated;	/* authenticated and alarms cancelled */
51 	int		 postponed;	/* authentication needs another step */
52 	int		 valid;		/* user exists and is allowed to login */
53 	int		 attempt;
54 	int		 failures;
55 	int		 server_caused_failure;
56 	int		 force_pwchange;
57 	char		*user;		/* username sent by the client */
58 	char		*service;
59 	struct passwd	*pw;		/* set if 'valid' */
60 	char		*style;
61 
62 	/* Method lists for multiple authentication */
63 	char		**auth_methods;	/* modified from server config */
64 	u_int		 num_auth_methods;
65 
66 	/* Authentication method-specific data */
67 	void		*methoddata;
68 	void		*kbdintctxt;
69 	auth_session_t	*as;
70 #ifdef KRB5
71 	krb5_context	 krb5_ctx;
72 	krb5_ccache	 krb5_fwd_ccache;
73 	krb5_principal	 krb5_user;
74 	char		*krb5_ticket_file;
75 #endif
76 
77 	/* Authentication keys already used; these will be refused henceforth */
78 	struct sshkey	**prev_keys;
79 	u_int		 nprev_keys;
80 
81 	/* Last used key and ancillary information from active auth method */
82 	struct sshkey	*auth_method_key;
83 	char		*auth_method_info;
84 
85 	/* Information exposed to session */
86 	struct sshbuf	*session_info;	/* Auth info for environment */
87 };
88 
89 /*
90  * Every authentication method has to handle authentication requests for
91  * non-existing users, or for users that are not allowed to login. In this
92  * case 'valid' is set to 0, but 'user' points to the username requested by
93  * the client.
94  */
95 
96 struct Authmethod {
97 	char	*name;
98 	int	(*userauth)(struct ssh *);
99 	int	*enabled;
100 };
101 
102 /*
103  * Keyboard interactive device:
104  * init_ctx	returns: non NULL upon success
105  * query	returns: 0 - success, otherwise failure
106  * respond	returns: 0 - success, 1 - need further interaction,
107  *		otherwise - failure
108  */
109 struct KbdintDevice
110 {
111 	const char *name;
112 	void*	(*init_ctx)(Authctxt*);
113 	int	(*query)(void *ctx, char **name, char **infotxt,
114 		    u_int *numprompts, char ***prompts, u_int **echo_on);
115 	int	(*respond)(void *ctx, u_int numresp, char **responses);
116 	void	(*free_ctx)(void *ctx);
117 };
118 
119 int
120 auth_rhosts2(struct passwd *, const char *, const char *, const char *);
121 
122 int      auth_password(struct ssh *, const char *);
123 
124 int	 hostbased_key_allowed(struct ssh *, struct passwd *,
125 	    const char *, char *, struct sshkey *);
126 int	 user_key_allowed(struct ssh *, struct passwd *, struct sshkey *, int,
127     struct sshauthopt **);
128 int	 auth2_key_already_used(Authctxt *, const struct sshkey *);
129 
130 /*
131  * Handling auth method-specific information for logging and prevention
132  * of key reuse during multiple authentication.
133  */
134 void	 auth2_authctxt_reset_info(Authctxt *);
135 void	 auth2_record_key(Authctxt *, int, const struct sshkey *);
136 void	 auth2_record_info(Authctxt *authctxt, const char *, ...)
137 	    __attribute__((__format__ (printf, 2, 3)))
138 	    __attribute__((__nonnull__ (2)));
139 void	 auth2_update_session_info(Authctxt *, const char *, const char *);
140 
141 #ifdef KRB5
142 int	auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client, krb5_data *);
143 int	auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt);
144 int	auth_krb5_password(Authctxt *authctxt, const char *password);
145 void	krb5_cleanup_proc(Authctxt *authctxt);
146 #endif /* KRB5 */
147 
148 void	do_authentication2(struct ssh *);
149 
150 void	auth_log(struct ssh *, int, int, const char *, const char *);
151 void	auth_maxtries_exceeded(struct ssh *) __attribute__((noreturn));
152 void	userauth_finish(struct ssh *, int, const char *, const char *);
153 int	auth_root_allowed(struct ssh *, const char *);
154 
155 char	*auth2_read_banner(void);
156 int	 auth2_methods_valid(const char *, int);
157 int	 auth2_update_methods_lists(Authctxt *, const char *, const char *);
158 int	 auth2_setup_methods_lists(Authctxt *);
159 int	 auth2_method_allowed(Authctxt *, const char *, const char *);
160 
161 void	privsep_challenge_enable(void);
162 
163 int	auth2_challenge(struct ssh *, char *);
164 void	auth2_challenge_stop(struct ssh *);
165 int	bsdauth_query(void *, char **, char **, u_int *, char ***, u_int **);
166 int	bsdauth_respond(void *, u_int, char **);
167 
168 int	allowed_user(struct ssh *, struct passwd *);
169 struct passwd * getpwnamallow(struct ssh *, const char *user);
170 
171 char	*expand_authorized_keys(const char *, struct passwd *pw);
172 char	*authorized_principals_file(struct passwd *);
173 
174 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
175 FILE	*auth_openprincipals(const char *, struct passwd *, int);
176 int	 auth_key_is_revoked(struct sshkey *);
177 
178 const char	*auth_get_canonical_hostname(struct ssh *, int);
179 
180 HostStatus
181 check_key_in_hostfiles(struct passwd *, struct sshkey *, const char *,
182     const char *, const char *);
183 
184 /* hostkey handling */
185 struct sshkey	*get_hostkey_by_index(int);
186 struct sshkey	*get_hostkey_public_by_index(int, struct ssh *);
187 struct sshkey	*get_hostkey_public_by_type(int, int, struct ssh *);
188 struct sshkey	*get_hostkey_private_by_type(int, int, struct ssh *);
189 int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
190 int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
191     u_char **, size_t *, const u_char *, size_t, const char *);
192 
193 /* Key / cert options linkage to auth layer */
194 const struct sshauthopt *auth_options(struct ssh *);
195 int	 auth_activate_options(struct ssh *, struct sshauthopt *);
196 void	 auth_restrict_session(struct ssh *);
197 int	 auth_authorise_keyopts(struct ssh *, struct passwd *pw,
198     struct sshauthopt *, int, const char *);
199 void	 auth_log_authopts(const char *, const struct sshauthopt *, int);
200 
201 /* debug messages during authentication */
202 void	 auth_debug_add(const char *fmt,...)
203     __attribute__((format(printf, 1, 2)));
204 void	 auth_debug_send(struct ssh *);
205 void	 auth_debug_reset(void);
206 
207 struct passwd *fakepw(void);
208 
209 #define	SSH_SUBPROCESS_STDOUT_DISCARD  (1)     /* Discard stdout */
210 #define	SSH_SUBPROCESS_STDOUT_CAPTURE  (1<<1)  /* Redirect stdout */
211 #define	SSH_SUBPROCESS_STDERR_DISCARD  (1<<2)  /* Discard stderr */
212 pid_t	subprocess(const char *, struct passwd *,
213     const char *, int, char **, FILE **, u_int flags);
214 
215 #endif
216