1 /*	$NetBSD: servconf.h,v 1.29 2023/07/26 17:58:15 christos Exp $	*/
2 /* $OpenBSD: servconf.h,v 1.159 2023/01/17 09:44:48 djm Exp $ */
3 
4 /*
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7  *                    All rights reserved
8  * Definitions for server configuration data and for the functions reading it.
9  *
10  * As far as I am concerned, the code I have written for this software
11  * can be used freely for any purpose.  Any derived versions of this
12  * software must be clearly marked as such, and if the derived work is
13  * incompatible with the protocol description in the RFC file, it must be
14  * called by a name other than "ssh" or "Secure Shell".
15  */
16 
17 #ifndef SERVCONF_H
18 #define SERVCONF_H
19 
20 #ifdef WITH_LDAP_PUBKEY
21 #include "ldapauth.h"
22 #endif
23 
24 #include <sys/queue.h>
25 
26 #define MAX_PORTS		256	/* Max # ports. */
27 
28 #define MAX_SUBSYSTEMS		256	/* Max # subsystems. */
29 
30 /* permit_root_login */
31 #define	PERMIT_NOT_SET		-1
32 #define	PERMIT_NO		0
33 #define	PERMIT_FORCED_ONLY	1
34 #define	PERMIT_NO_PASSWD	2
35 #define	PERMIT_YES		3
36 
37 /* use_privsep */
38 #define PRIVSEP_OFF		0
39 #define PRIVSEP_ON		1
40 #define PRIVSEP_NOSANDBOX	2
41 
42 /* PermitOpen */
43 #define PERMITOPEN_ANY		0
44 #define PERMITOPEN_NONE		-2
45 
46 /* IgnoreRhosts */
47 #define IGNORE_RHOSTS_NO	0
48 #define IGNORE_RHOSTS_YES	1
49 #define IGNORE_RHOSTS_SHOSTS	2
50 
51 #define DEFAULT_AUTH_FAIL_MAX	6	/* Default for MaxAuthTries */
52 #define DEFAULT_SESSIONS_MAX	10	/* Default for MaxSessions */
53 
54 /* Magic name for internal sftp-server */
55 #define INTERNAL_SFTP_NAME	"internal-sftp"
56 
57 /* PubkeyAuthOptions flags */
58 #define PUBKEYAUTH_TOUCH_REQUIRED	(1)
59 #define PUBKEYAUTH_VERIFY_REQUIRED	(1<<1)
60 
61 struct ssh;
62 struct fwd_perm_list;
63 
64 /*
65  * Used to store addresses from ListenAddr directives. These may be
66  * incomplete, as they may specify addresses that need to be merged
67  * with any ports requested by ListenPort.
68  */
69 struct queued_listenaddr {
70 	char *addr;
71 	int port; /* <=0 if unspecified */
72 	char *rdomain;
73 };
74 
75 /* Resolved listen addresses, grouped by optional routing domain */
76 struct listenaddr {
77 	char *rdomain;
78 	struct addrinfo *addrs;
79 };
80 
81 typedef struct {
82 	u_int	num_ports;
83 	u_int	ports_from_cmdline;
84 	int	ports[MAX_PORTS];	/* Port number to listen on. */
85 	struct queued_listenaddr *queued_listen_addrs;
86 	u_int	num_queued_listens;
87 	struct listenaddr *listen_addrs;
88 	u_int	num_listen_addrs;
89 	int	address_family;		/* Address family used by the server. */
90 
91 	char	*routing_domain;	/* Bind session to routing domain */
92 
93 	char   **host_key_files;	/* Files containing host keys. */
94 	int	*host_key_file_userprovided; /* Key was specified by user. */
95 	u_int	num_host_key_files;     /* Number of files for host keys. */
96 	char   **host_cert_files;	/* Files containing host certs. */
97 	u_int	num_host_cert_files;	/* Number of files for host certs. */
98 
99 	char   *host_key_agent;		/* ssh-agent socket for host keys. */
100 	char   *pid_file;		/* Where to put our pid */
101 	char   *moduli_file;		/* moduli file for DH-GEX */
102 	int     login_grace_time;	/* Disconnect if no auth in this time
103 					 * (sec). */
104 	int     permit_root_login;	/* PERMIT_*, see above */
105 	int     ignore_rhosts;	/* Ignore .rhosts and .shosts. */
106 	int     ignore_root_rhosts;	/* Ignore .rhosts and .shosts for root;
107 					   defaults to ignore_rhosts if not
108 					   given. */
109 	int     ignore_user_known_hosts;	/* Ignore ~/.ssh/known_hosts
110 						 * for RhostsRsaAuth */
111 	int     print_motd;	/* If true, print /etc/motd. */
112 	int	print_lastlog;	/* If true, print lastlog */
113 	int     x11_forwarding;	/* If true, permit inet (spoofing) X11 fwd. */
114 	int     x11_display_offset;	/* What DISPLAY number to start
115 					 * searching at */
116 	int     x11_use_localhost;	/* If true, use localhost for fake X11 server. */
117 	char   *xauth_location;	/* Location of xauth program */
118 	int	permit_tty;	/* If false, deny pty allocation */
119 	int	permit_user_rc;	/* If false, deny ~/.ssh/rc execution */
120 	int     strict_modes;	/* If true, require string home dir modes. */
121 	int     tcp_keep_alive;	/* If true, set SO_KEEPALIVE. */
122 	int	ip_qos_interactive;	/* IP ToS/DSCP/class for interactive */
123 	int	ip_qos_bulk;		/* IP ToS/DSCP/class for bulk traffic */
124 	char   *ciphers;	/* Supported SSH2 ciphers. */
125 	char   *macs;		/* Supported SSH2 macs. */
126 	char   *kex_algorithms;	/* SSH2 kex methods in order of preference. */
127 	struct ForwardOptions fwd_opts;	/* forwarding options */
128 	SyslogFacility log_facility;	/* Facility for system logging. */
129 	LogLevel log_level;	/* Level for system logging. */
130 	u_int	num_log_verbose;	/* Verbose log overrides */
131 	char	**log_verbose;
132 	int     hostbased_authentication;	/* If true, permit ssh2 hostbased auth */
133 	int     hostbased_uses_name_from_packet_only; /* experimental */
134 	char   *hostbased_accepted_algos; /* Algos allowed for hostbased */
135 	char   *hostkeyalgorithms;	/* SSH2 server key types */
136 	char   *ca_sign_algorithms;	/* Allowed CA signature algorithms */
137 	int     pubkey_authentication;	/* If true, permit ssh2 pubkey authentication. */
138 	char   *pubkey_accepted_algos;	/* Signature algos allowed for pubkey */
139 	int	pubkey_auth_options;	/* -1 or mask of PUBKEYAUTH_* flags */
140 	int     kerberos_authentication;	/* If true, permit Kerberos
141 						 * authentication. */
142 	int     kerberos_or_local_passwd;	/* If true, permit kerberos
143 						 * and any other password
144 						 * authentication mechanism,
145 						 * such as SecurID or
146 						 * /etc/passwd */
147 	int     kerberos_ticket_cleanup;	/* If true, destroy ticket
148 						 * file on logout. */
149 	int	kerberos_tgt_passing;		/* If true, permit Kerberos TGT
150 						 * passing. */
151 	int     kerberos_get_afs_token;		/* If true, try to get AFS token if
152 						 * authenticated with Kerberos. */
153 #ifdef AFS
154 	int     afs_token_passing;      /* If true, permit AFS token passing. */
155 #endif
156 	int     gss_authentication;	/* If true, permit GSSAPI authentication */
157 	int     gss_cleanup_creds;	/* If true, destroy cred cache on logout */
158 	int     gss_strict_acceptor;	/* If true, restrict the GSSAPI acceptor name */
159 	int     password_authentication;	/* If true, permit password
160 						 * authentication. */
161 	int     kbd_interactive_authentication;	/* If true, permit */
162 	int     permit_empty_passwd;	/* If false, do not permit empty
163 					 * passwords. */
164 	int     permit_user_env;	/* If true, read ~/.ssh/environment */
165 	char   *permit_user_env_allowlist; /* pattern-list of allowed env names */
166 	int     compression;	/* If true, compression is allowed */
167 	int	allow_tcp_forwarding; /* One of FORWARD_* */
168 	int	allow_streamlocal_forwarding; /* One of FORWARD_* */
169 	int	allow_agent_forwarding;
170 	int	disable_forwarding;
171 	u_int num_allow_users;
172 	char   **allow_users;
173 	u_int num_deny_users;
174 	char   **deny_users;
175 	u_int num_allow_groups;
176 	char   **allow_groups;
177 	u_int num_deny_groups;
178 	char   **deny_groups;
179 
180 	u_int num_subsystems;
181 	char   *subsystem_name[MAX_SUBSYSTEMS];
182 	char   *subsystem_command[MAX_SUBSYSTEMS];
183 	char   *subsystem_args[MAX_SUBSYSTEMS];
184 
185 	u_int num_accept_env;
186 	char   **accept_env;
187 	u_int num_setenv;
188 	char   **setenv;
189 
190 	int	max_startups_begin;
191 	int	max_startups_rate;
192 	int	max_startups;
193 	int	per_source_max_startups;
194 	int	per_source_masklen_ipv4;
195 	int	per_source_masklen_ipv6;
196 	int	max_authtries;
197 	int	max_sessions;
198 	char   *banner;			/* SSH-2 banner message */
199 	int	use_dns;
200 	int	client_alive_interval;	/*
201 					 * poke the client this often to
202 					 * see if it's still there
203 					 */
204 	int	client_alive_count_max;	/*
205 					 * If the client is unresponsive
206 					 * for this many intervals above,
207 					 * disconnect the session
208 					 */
209 
210 	u_int	num_authkeys_files;	/* Files containing public keys */
211 	char   **authorized_keys_files;
212 
213 	int	use_pam;		/* Enable auth via PAM */
214 	int     none_enabled;           /* enable NONE cipher switch */
215 	int     tcp_rcv_buf_poll;       /* poll tcp rcv window in autotuning kernels*/
216 	int	hpn_disabled;		/* disable hpn functionality. false by default */
217 	int	hpn_buffer_size;	/* set the hpn buffer size - default 3MB */
218 
219 	char   *adm_forced_command;
220 
221 	int	permit_tun;
222 #ifdef WITH_LDAP_PUBKEY
223         ldap_opt_t lpk;
224 #endif
225 
226 	char   **permitted_opens;	/* May also be one of PERMITOPEN_* */
227 	u_int   num_permitted_opens;
228 	char   **permitted_listens; /* May also be one of PERMITOPEN_* */
229 	u_int   num_permitted_listens;
230 
231 	char   *chroot_directory;
232 	char   *revoked_keys_file;
233 	char   *trusted_user_ca_keys;
234 	char   *authorized_keys_command;
235 	char   *authorized_keys_command_user;
236 	char   *authorized_principals_file;
237 	char   *authorized_principals_command;
238 	char   *authorized_principals_command_user;
239 
240 	int64_t rekey_limit;
241 	int	rekey_interval;
242 
243 	char   *version_addendum;	/* Appended to SSH banner */
244 
245 	u_int	num_auth_methods;
246 	char   **auth_methods;
247 
248 	int	fingerprint_hash;
249 	int	expose_userauth_info;
250 	u_int64_t timing_secret;
251 	char   *sk_provider;
252 	int	required_rsa_size;	/* minimum size of RSA keys */
253 
254 	char	**channel_timeouts;	/* inactivity timeout by channel type */
255 	u_int	num_channel_timeouts;
256 
257 	int	unused_connection_timeout;
258 }       ServerOptions;
259 
260 /* Information about the incoming connection as used by Match */
261 struct connection_info {
262 	const char *user;
263 	const char *host;	/* possibly resolved hostname */
264 	const char *address;	/* remote address */
265 	const char *laddress;	/* local address */
266 	int lport;		/* local port */
267 	const char *rdomain;	/* routing domain if available */
268 	int test;		/* test mode, allow some attributes to be
269 				 * unspecified */
270 };
271 
272 /* List of included files for re-exec from the parsed configuration */
273 struct include_item {
274 	char *selector;
275 	char *filename;
276 	struct sshbuf *contents;
277 	TAILQ_ENTRY(include_item) entry;
278 };
279 TAILQ_HEAD(include_list, include_item);
280 
281 
282 /*
283  * These are string config options that must be copied between the
284  * Match sub-config and the main config, and must be sent from the
285  * privsep child to the privsep master. We use a macro to ensure all
286  * the options are copied and the copies are done in the correct order.
287  *
288  * NB. an option must appear in servconf.c:copy_set_server_options() or
289  * COPY_MATCH_STRING_OPTS here but never both.
290  */
291 #define COPY_MATCH_STRING_OPTS() do { \
292 		M_CP_STROPT(banner); \
293 		M_CP_STROPT(trusted_user_ca_keys); \
294 		M_CP_STROPT(revoked_keys_file); \
295 		M_CP_STROPT(authorized_keys_command); \
296 		M_CP_STROPT(authorized_keys_command_user); \
297 		M_CP_STROPT(authorized_principals_file); \
298 		M_CP_STROPT(authorized_principals_command); \
299 		M_CP_STROPT(authorized_principals_command_user); \
300 		M_CP_STROPT(hostbased_accepted_algos); \
301 		M_CP_STROPT(pubkey_accepted_algos); \
302 		M_CP_STROPT(ca_sign_algorithms); \
303 		M_CP_STROPT(routing_domain); \
304 		M_CP_STROPT(permit_user_env_allowlist); \
305 		M_CP_STRARRAYOPT(authorized_keys_files, num_authkeys_files); \
306 		M_CP_STRARRAYOPT(allow_users, num_allow_users); \
307 		M_CP_STRARRAYOPT(deny_users, num_deny_users); \
308 		M_CP_STRARRAYOPT(allow_groups, num_allow_groups); \
309 		M_CP_STRARRAYOPT(deny_groups, num_deny_groups); \
310 		M_CP_STRARRAYOPT(accept_env, num_accept_env); \
311 		M_CP_STRARRAYOPT(setenv, num_setenv); \
312 		M_CP_STRARRAYOPT(auth_methods, num_auth_methods); \
313 		M_CP_STRARRAYOPT(permitted_opens, num_permitted_opens); \
314 		M_CP_STRARRAYOPT(permitted_listens, num_permitted_listens); \
315 		M_CP_STRARRAYOPT(channel_timeouts, num_channel_timeouts); \
316 		M_CP_STRARRAYOPT(log_verbose, num_log_verbose); \
317 	} while (0)
318 
319 struct connection_info *get_connection_info(struct ssh *, int, int);
320 void	 initialize_server_options(ServerOptions *);
321 void	 fill_default_server_options(ServerOptions *);
322 int	 process_server_config_line(ServerOptions *, char *, const char *, int,
323 	    int *, struct connection_info *, struct include_list *includes);
324 void	 process_permitopen(struct ssh *ssh, ServerOptions *options);
325 void	 process_channel_timeouts(struct ssh *ssh, ServerOptions *);
326 void	 load_server_config(const char *, struct sshbuf *);
327 void	 parse_server_config(ServerOptions *, const char *, struct sshbuf *,
328 	    struct include_list *includes, struct connection_info *, int);
329 void	 parse_server_match_config(ServerOptions *,
330 	    struct include_list *includes, struct connection_info *);
331 int	 parse_server_match_testspec(struct connection_info *, char *);
332 int	 server_match_spec_complete(struct connection_info *);
333 void	 copy_set_server_options(ServerOptions *, ServerOptions *, int);
334 void	 dump_config(ServerOptions *);
335 char	*derelativise_path(const char *);
336 void	 servconf_add_hostkey(const char *, const int,
337 	    ServerOptions *, const char *path, int);
338 void	 servconf_add_hostcert(const char *, const int,
339 	    ServerOptions *, const char *path);
340 
341 #endif				/* SERVCONF_H */
342