xref: /freebsd/crypto/openssh/auth.h (revision 0fdf8fae8b569bf9fff3b5171e669dcd7cf9c79e)
1*0fdf8faeSEd Maste /* $OpenBSD: auth.h,v 1.108 2024/05/17 06:42:04 jsg Exp $ */
2af12a3e7SDag-Erling Smørgrav 
3b66f2d16SKris Kennaway /*
4b66f2d16SKris Kennaway  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5b66f2d16SKris Kennaway  *
6b66f2d16SKris Kennaway  * Redistribution and use in source and binary forms, with or without
7b66f2d16SKris Kennaway  * modification, are permitted provided that the following conditions
8b66f2d16SKris Kennaway  * are met:
9b66f2d16SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
10b66f2d16SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
11b66f2d16SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
12b66f2d16SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
13b66f2d16SKris Kennaway  *    documentation and/or other materials provided with the distribution.
14b66f2d16SKris Kennaway  *
15b66f2d16SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b66f2d16SKris Kennaway  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b66f2d16SKris Kennaway  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b66f2d16SKris Kennaway  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b66f2d16SKris Kennaway  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20b66f2d16SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21b66f2d16SKris Kennaway  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22b66f2d16SKris Kennaway  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23b66f2d16SKris Kennaway  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24b66f2d16SKris Kennaway  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255b9b2fafSBrian Feldman  *
26b66f2d16SKris Kennaway  */
27af12a3e7SDag-Erling Smørgrav 
28a04a10f8SKris Kennaway #ifndef AUTH_H
29a04a10f8SKris Kennaway #define AUTH_H
30a04a10f8SKris Kennaway 
31333ee039SDag-Erling Smørgrav #include <signal.h>
3238a52bd3SEd Maste #include <stdio.h>
33333ee039SDag-Erling Smørgrav 
341e8db6e2SBrian Feldman #ifdef HAVE_LOGIN_CAP
351e8db6e2SBrian Feldman #include <login_cap.h>
361e8db6e2SBrian Feldman #endif
371e8db6e2SBrian Feldman #ifdef BSD_AUTH
381e8db6e2SBrian Feldman #include <bsd_auth.h>
391e8db6e2SBrian Feldman #endif
40af12a3e7SDag-Erling Smørgrav #ifdef KRB5
41af12a3e7SDag-Erling Smørgrav #include <krb5.h>
42af12a3e7SDag-Erling Smørgrav #endif
431e8db6e2SBrian Feldman 
4447dd1d1bSDag-Erling Smørgrav struct passwd;
45bc5531deSDag-Erling Smørgrav struct ssh;
464f52dfbbSDag-Erling Smørgrav struct sshbuf;
4747dd1d1bSDag-Erling Smørgrav struct sshkey;
4838a52bd3SEd Maste struct sshkey_cert;
4947dd1d1bSDag-Erling Smørgrav struct sshauthopt;
50bc5531deSDag-Erling Smørgrav 
515b9b2fafSBrian Feldman typedef struct Authctxt Authctxt;
5280628bacSDag-Erling Smørgrav typedef struct Authmethod Authmethod;
53af12a3e7SDag-Erling Smørgrav typedef struct KbdintDevice KbdintDevice;
54af12a3e7SDag-Erling Smørgrav 
555b9b2fafSBrian Feldman struct Authctxt {
56333ee039SDag-Erling Smørgrav 	sig_atomic_t	 success;
57333ee039SDag-Erling Smørgrav 	int		 authenticated;	/* authenticated and alarms cancelled */
58cf2b5f3bSDag-Erling Smørgrav 	int		 postponed;	/* authentication needs another step */
59cf2b5f3bSDag-Erling Smørgrav 	int		 valid;		/* user exists and is allowed to login */
605b9b2fafSBrian Feldman 	int		 attempt;
611e8db6e2SBrian Feldman 	int		 failures;
62e146993eSDag-Erling Smørgrav 	int		 server_caused_failure;
631ec0d754SDag-Erling Smørgrav 	int		 force_pwchange;
64cf2b5f3bSDag-Erling Smørgrav 	char		*user;		/* username sent by the client */
655b9b2fafSBrian Feldman 	char		*service;
66cf2b5f3bSDag-Erling Smørgrav 	struct passwd	*pw;		/* set if 'valid' */
671e8db6e2SBrian Feldman 	char		*style;
684f52dfbbSDag-Erling Smørgrav 
694f52dfbbSDag-Erling Smørgrav 	/* Method lists for multiple authentication */
704f52dfbbSDag-Erling Smørgrav 	char		**auth_methods;	/* modified from server config */
714f52dfbbSDag-Erling Smørgrav 	u_int		 num_auth_methods;
724f52dfbbSDag-Erling Smørgrav 
734f52dfbbSDag-Erling Smørgrav 	/* Authentication method-specific data */
744f52dfbbSDag-Erling Smørgrav 	void		*methoddata;
75af12a3e7SDag-Erling Smørgrav 	void		*kbdintctxt;
761e8db6e2SBrian Feldman #ifdef BSD_AUTH
771e8db6e2SBrian Feldman 	auth_session_t	*as;
781e8db6e2SBrian Feldman #endif
79af12a3e7SDag-Erling Smørgrav #ifdef KRB5
80af12a3e7SDag-Erling Smørgrav 	krb5_context	 krb5_ctx;
81af12a3e7SDag-Erling Smørgrav 	krb5_ccache	 krb5_fwd_ccache;
82af12a3e7SDag-Erling Smørgrav 	krb5_principal	 krb5_user;
83af12a3e7SDag-Erling Smørgrav 	char		*krb5_ticket_file;
845962c0e9SDag-Erling Smørgrav 	char		*krb5_ccname;
85af12a3e7SDag-Erling Smørgrav #endif
864f52dfbbSDag-Erling Smørgrav 	struct sshbuf	*loginmsg;
87bc5531deSDag-Erling Smørgrav 
884f52dfbbSDag-Erling Smørgrav 	/* Authentication keys already used; these will be refused henceforth */
894f52dfbbSDag-Erling Smørgrav 	struct sshkey	**prev_keys;
904f52dfbbSDag-Erling Smørgrav 	u_int		 nprev_keys;
914f52dfbbSDag-Erling Smørgrav 
92190cef3dSDag-Erling Smørgrav 	/* Last used key and ancillary information from active auth method */
934f52dfbbSDag-Erling Smørgrav 	struct sshkey	*auth_method_key;
944f52dfbbSDag-Erling Smørgrav 	char		*auth_method_info;
954f52dfbbSDag-Erling Smørgrav 
964f52dfbbSDag-Erling Smørgrav 	/* Information exposed to session */
974f52dfbbSDag-Erling Smørgrav 	struct sshbuf	*session_info;	/* Auth info for environment */
985b9b2fafSBrian Feldman };
994f52dfbbSDag-Erling Smørgrav 
100cf2b5f3bSDag-Erling Smørgrav /*
101cf2b5f3bSDag-Erling Smørgrav  * Every authentication method has to handle authentication requests for
102cf2b5f3bSDag-Erling Smørgrav  * non-existing users, or for users that are not allowed to login. In this
103cf2b5f3bSDag-Erling Smørgrav  * case 'valid' is set to 0, but 'user' points to the username requested by
104cf2b5f3bSDag-Erling Smørgrav  * the client.
105cf2b5f3bSDag-Erling Smørgrav  */
1065b9b2fafSBrian Feldman 
107*0fdf8faeSEd Maste struct authmethod_cfg {
108*0fdf8faeSEd Maste 	const char *name;
109*0fdf8faeSEd Maste 	const char *synonym;
11080628bacSDag-Erling Smørgrav 	int *enabled;
11180628bacSDag-Erling Smørgrav };
11280628bacSDag-Erling Smørgrav 
113*0fdf8faeSEd Maste struct Authmethod {
114*0fdf8faeSEd Maste 	struct authmethod_cfg *cfg;
115*0fdf8faeSEd Maste 	int	(*userauth)(struct ssh *, const char *);
116*0fdf8faeSEd Maste };
117*0fdf8faeSEd Maste 
1181e8db6e2SBrian Feldman /*
119af12a3e7SDag-Erling Smørgrav  * Keyboard interactive device:
120af12a3e7SDag-Erling Smørgrav  * init_ctx	returns: non NULL upon success
121af12a3e7SDag-Erling Smørgrav  * query	returns: 0 - success, otherwise failure
122af12a3e7SDag-Erling Smørgrav  * respond	returns: 0 - success, 1 - need further interaction,
123af12a3e7SDag-Erling Smørgrav  *		otherwise - failure
1241e8db6e2SBrian Feldman  */
125af12a3e7SDag-Erling Smørgrav struct KbdintDevice
126af12a3e7SDag-Erling Smørgrav {
127af12a3e7SDag-Erling Smørgrav 	const char *name;
128af12a3e7SDag-Erling Smørgrav 	void*	(*init_ctx)(Authctxt*);
129af12a3e7SDag-Erling Smørgrav 	int	(*query)(void *ctx, char **name, char **infotxt,
130af12a3e7SDag-Erling Smørgrav 		    u_int *numprompts, char ***prompts, u_int **echo_on);
131af12a3e7SDag-Erling Smørgrav 	int	(*respond)(void *ctx, u_int numresp, char **responses);
132af12a3e7SDag-Erling Smørgrav 	void	(*free_ctx)(void *ctx);
133af12a3e7SDag-Erling Smørgrav };
1341e8db6e2SBrian Feldman 
1351e8db6e2SBrian Feldman int
136af12a3e7SDag-Erling Smørgrav auth_rhosts2(struct passwd *, const char *, const char *, const char *);
1371e8db6e2SBrian Feldman 
13847dd1d1bSDag-Erling Smørgrav int      auth_password(struct ssh *, const char *);
13980628bacSDag-Erling Smørgrav 
14019261079SEd Maste int	 hostbased_key_allowed(struct ssh *, struct passwd *,
14119261079SEd Maste 	    const char *, char *, struct sshkey *);
14238a52bd3SEd Maste int	 user_key_allowed(struct ssh *ssh, struct passwd *, struct sshkey *,
14338a52bd3SEd Maste     int, struct sshauthopt **);
1444f52dfbbSDag-Erling Smørgrav int	 auth2_key_already_used(Authctxt *, const struct sshkey *);
1451e8db6e2SBrian Feldman 
1464f52dfbbSDag-Erling Smørgrav /*
1474f52dfbbSDag-Erling Smørgrav  * Handling auth method-specific information for logging and prevention
1484f52dfbbSDag-Erling Smørgrav  * of key reuse during multiple authentication.
1494f52dfbbSDag-Erling Smørgrav  */
1504f52dfbbSDag-Erling Smørgrav void	 auth2_authctxt_reset_info(Authctxt *);
1514f52dfbbSDag-Erling Smørgrav void	 auth2_record_key(Authctxt *, int, const struct sshkey *);
1524f52dfbbSDag-Erling Smørgrav void	 auth2_record_info(Authctxt *authctxt, const char *, ...)
1534f52dfbbSDag-Erling Smørgrav 	    __attribute__((__format__ (printf, 2, 3)))
1544f52dfbbSDag-Erling Smørgrav 	    __attribute__((__nonnull__ (2)));
1554f52dfbbSDag-Erling Smørgrav void	 auth2_update_session_info(Authctxt *, const char *, const char *);
1566888a9beSDag-Erling Smørgrav 
157af12a3e7SDag-Erling Smørgrav #ifdef KRB5
158af12a3e7SDag-Erling Smørgrav int	auth_krb5_password(Authctxt *authctxt, const char *password);
1591ec0d754SDag-Erling Smørgrav void	krb5_cleanup_proc(Authctxt *authctxt);
160af12a3e7SDag-Erling Smørgrav #endif /* KRB5 */
161af12a3e7SDag-Erling Smørgrav 
1621ec0d754SDag-Erling Smørgrav #if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
1631ec0d754SDag-Erling Smørgrav #include <shadow.h>
1641ec0d754SDag-Erling Smørgrav int auth_shadow_acctexpired(struct spwd *);
1651ec0d754SDag-Erling Smørgrav int auth_shadow_pwexpired(Authctxt *);
1661ec0d754SDag-Erling Smørgrav #endif
1671ec0d754SDag-Erling Smørgrav 
168989dd127SDag-Erling Smørgrav #include "auth-pam.h"
169aa49c926SDag-Erling Smørgrav #include "audit.h"
170aa49c926SDag-Erling Smørgrav void remove_kbdint_device(const char *);
171aa49c926SDag-Erling Smørgrav 
17219261079SEd Maste void	do_authentication2(struct ssh *);
173a04a10f8SKris Kennaway 
17419261079SEd Maste void	auth_log(struct ssh *, int, int, const char *, const char *);
17519261079SEd Maste void	auth_maxtries_exceeded(struct ssh *) __attribute__((noreturn));
1764f52dfbbSDag-Erling Smørgrav void	userauth_finish(struct ssh *, int, const char *, const char *);
17747dd1d1bSDag-Erling Smørgrav int	auth_root_allowed(struct ssh *, const char *);
1786888a9beSDag-Erling Smørgrav 
17980628bacSDag-Erling Smørgrav char	*auth2_read_banner(void);
1806888a9beSDag-Erling Smørgrav int	 auth2_methods_valid(const char *, int);
181e4a9863fSDag-Erling Smørgrav int	 auth2_update_methods_lists(Authctxt *, const char *, const char *);
1826888a9beSDag-Erling Smørgrav int	 auth2_setup_methods_lists(Authctxt *);
183e4a9863fSDag-Erling Smørgrav int	 auth2_method_allowed(Authctxt *, const char *, const char *);
18480628bacSDag-Erling Smørgrav 
18580628bacSDag-Erling Smørgrav void	privsep_challenge_enable(void);
18680628bacSDag-Erling Smørgrav 
1874f52dfbbSDag-Erling Smørgrav int	auth2_challenge(struct ssh *, char *);
1884f52dfbbSDag-Erling Smørgrav void	auth2_challenge_stop(struct ssh *);
18980628bacSDag-Erling Smørgrav int	bsdauth_query(void *, char **, char **, u_int *, char ***, u_int **);
19080628bacSDag-Erling Smørgrav int	bsdauth_respond(void *, u_int, char **);
191a04a10f8SKris Kennaway 
19219261079SEd Maste int	allowed_user(struct ssh *, struct passwd *);
19319261079SEd Maste struct passwd * getpwnamallow(struct ssh *, const char *user);
1941e8db6e2SBrian Feldman 
195e146993eSDag-Erling Smørgrav char	*expand_authorized_keys(const char *, struct passwd *pw);
196e2f6069cSDag-Erling Smørgrav char	*authorized_principals_file(struct passwd *);
197af12a3e7SDag-Erling Smørgrav 
1984f52dfbbSDag-Erling Smørgrav int	 auth_key_is_revoked(struct sshkey *);
199af12a3e7SDag-Erling Smørgrav 
200076ad2f8SDag-Erling Smørgrav const char	*auth_get_canonical_hostname(struct ssh *, int);
201076ad2f8SDag-Erling Smørgrav 
202af12a3e7SDag-Erling Smørgrav HostStatus
2034f52dfbbSDag-Erling Smørgrav check_key_in_hostfiles(struct passwd *, struct sshkey *, const char *,
204af12a3e7SDag-Erling Smørgrav     const char *, const char *);
205af12a3e7SDag-Erling Smørgrav 
20680628bacSDag-Erling Smørgrav /* hostkey handling */
2074f52dfbbSDag-Erling Smørgrav struct sshkey	*get_hostkey_by_index(int);
2084f52dfbbSDag-Erling Smørgrav struct sshkey	*get_hostkey_public_by_index(int, struct ssh *);
2094f52dfbbSDag-Erling Smørgrav struct sshkey	*get_hostkey_public_by_type(int, int, struct ssh *);
2104f52dfbbSDag-Erling Smørgrav struct sshkey	*get_hostkey_private_by_type(int, int, struct ssh *);
2114f52dfbbSDag-Erling Smørgrav int	 get_hostkey_index(struct sshkey *, int, struct ssh *);
21219261079SEd Maste int	 sshd_hostkey_sign(struct ssh *, struct sshkey *, struct sshkey *,
21319261079SEd Maste     u_char **, size_t *, const u_char *, size_t, const char *);
21480628bacSDag-Erling Smørgrav 
21547dd1d1bSDag-Erling Smørgrav /* Key / cert options linkage to auth layer */
21647dd1d1bSDag-Erling Smørgrav int	 auth_activate_options(struct ssh *, struct sshauthopt *);
21747dd1d1bSDag-Erling Smørgrav void	 auth_restrict_session(struct ssh *);
21847dd1d1bSDag-Erling Smørgrav void	 auth_log_authopts(const char *, const struct sshauthopt *, int);
21947dd1d1bSDag-Erling Smørgrav 
22080628bacSDag-Erling Smørgrav /* debug messages during authentication */
22147dd1d1bSDag-Erling Smørgrav void	 auth_debug_add(const char *fmt,...)
22247dd1d1bSDag-Erling Smørgrav     __attribute__((format(printf, 1, 2)));
22319261079SEd Maste void	 auth_debug_send(struct ssh *);
22480628bacSDag-Erling Smørgrav void	 auth_debug_reset(void);
22580628bacSDag-Erling Smørgrav 
226cf2b5f3bSDag-Erling Smørgrav struct passwd *fakepw(void);
227cf2b5f3bSDag-Erling Smørgrav 
22838a52bd3SEd Maste /* auth2-pubkeyfile.c */
22938a52bd3SEd Maste int	 auth_authorise_keyopts(struct passwd *, struct sshauthopt *, int,
23038a52bd3SEd Maste     const char *, const char *, const char *);
23138a52bd3SEd Maste int	 auth_check_principals_line(char *, const struct sshkey_cert *,
23238a52bd3SEd Maste     const char *, struct sshauthopt **);
23338a52bd3SEd Maste int	 auth_process_principals(FILE *, const char *,
23438a52bd3SEd Maste     const struct sshkey_cert *, struct sshauthopt **);
23538a52bd3SEd Maste int	 auth_check_authkey_line(struct passwd *, struct sshkey *,
23638a52bd3SEd Maste     char *, const char *, const char *, const char *, struct sshauthopt **);
23738a52bd3SEd Maste int	 auth_check_authkeys_file(struct passwd *, FILE *, char *,
23838a52bd3SEd Maste     struct sshkey *, const char *, const char *, struct sshauthopt **);
23938a52bd3SEd Maste FILE	*auth_openkeyfile(const char *, struct passwd *, int);
24038a52bd3SEd Maste FILE	*auth_openprincipals(const char *, struct passwd *, int);
24138a52bd3SEd Maste 
24247dd1d1bSDag-Erling Smørgrav int	 sys_auth_passwd(struct ssh *, const char *);
243aa49c926SDag-Erling Smørgrav 
244d4ecd108SDag-Erling Smørgrav #if defined(KRB5) && !defined(HEIMDAL)
245d4ecd108SDag-Erling Smørgrav krb5_error_code ssh_krb5_cc_gen(krb5_context, krb5_ccache *);
246d4ecd108SDag-Erling Smørgrav #endif
24719261079SEd Maste 
24819261079SEd Maste #endif /* AUTH_H */
249