1/*	$NetBSD: misc.h,v 1.27 2023/12/20 17:15:20 christos Exp $	*/
2/* $OpenBSD: misc.h,v 1.106 2023/10/11 22:42:26 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 *
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose.  Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 */
15
16#ifndef _MISC_H
17#define _MISC_H
18
19#include <sys/time.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <stdio.h>
23#include <signal.h>
24
25/* Data structure for representing a forwarding request. */
26struct Forward {
27	char	 *listen_host;		/* Host (address) to listen on. */
28	int	  listen_port;		/* Port to forward. */
29	char	 *listen_path;		/* Path to bind domain socket. */
30	char	 *connect_host;		/* Host to connect. */
31	int	  connect_port;		/* Port to connect on connect_host. */
32	char	 *connect_path;		/* Path to connect domain socket. */
33	int	  allocated_port;	/* Dynamically allocated listen port */
34	int	  handle;		/* Handle for dynamic listen ports */
35};
36
37int forward_equals(const struct Forward *, const struct Forward *);
38int daemonized(void);
39
40/* Common server and client forwarding options. */
41struct ForwardOptions {
42	int	 gateway_ports; /* Allow remote connects to forwarded ports. */
43	mode_t	 streamlocal_bind_mask; /* umask for streamlocal binds */
44	int	 streamlocal_bind_unlink; /* unlink socket before bind */
45};
46
47/* misc.c */
48
49char	*chop(char *);
50void	 rtrim(char *);
51void	skip_space(char **);
52char	*strdelim(char **);
53char	*strdelimw(char **);
54int	 set_nonblock(int);
55int	 unset_nonblock(int);
56void	 set_nodelay(int);
57int	 set_reuseaddr(int);
58char	*get_rdomain(int);
59int	 set_rdomain(int, const char *);
60int	 get_sock_af(int);
61void	 set_sock_tos(int, int);
62int	 waitrfd(int, int *, volatile sig_atomic_t *);
63int	 timeout_connect(int, const struct sockaddr *, socklen_t, int *);
64int	 a2port(const char *);
65int	 a2tun(const char *, int *);
66char	*put_host_port(const char *, u_short);
67char	*hpdelim2(char **, char *);
68char	*hpdelim(char **);
69char	*cleanhostname(char *);
70char	*colon(char *);
71int	 parse_user_host_path(const char *, char **, char **, char **);
72int	 parse_user_host_port(const char *, char **, char **, int *);
73int	 parse_uri(const char *, const char *, char **, char **, int *, char **);
74int	 convtime(const char *);
75const char *fmt_timeframe(time_t t);
76int	 tilde_expand(const char *, uid_t, char **);
77char	*tilde_expand_filename(const char *, uid_t);
78
79char	*dollar_expand(int *, const char *string, ...);
80char	*percent_expand(const char *, ...) __attribute__((__sentinel__));
81char	*percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
82char	*tohex(const void *, size_t);
83void	 xextendf(char **s, const char *sep, const char *fmt, ...)
84    __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
85void	 sanitise_stdfd(void);
86struct timeval;
87void	 ms_subtract_diff(struct timeval *, int *);
88void	 ms_to_timespec(struct timespec *, int);
89void	 monotime_ts(struct timespec *);
90void	 monotime_tv(struct timeval *);
91time_t	 monotime(void);
92double	 monotime_double(void);
93void	 lowercase(char *s);
94int	 unix_listener(const char *, int, int);
95int	 valid_domain(char *, int, const char **);
96int	 valid_env_name(const char *);
97const char *atoi_err(const char *, int *);
98int	 parse_absolute_time(const char *, uint64_t *);
99void	 format_absolute_time(uint64_t, char *, size_t);
100int	 parse_pattern_interval(const char *, char **, int *);
101int	 path_absolute(const char *);
102int	 stdfd_devnull(int, int, int);
103int	 lib_contains_symbol(const char *, const char *);
104
105int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
106    u_int8_t *, size_t, unsigned int);
107
108struct passwd *pwcopy(struct passwd *);
109const char *ssh_gai_strerror(int);
110
111typedef void privdrop_fn(struct passwd *);
112typedef void privrestore_fn(void);
113#define	SSH_SUBPROCESS_STDOUT_DISCARD	(1)     /* Discard stdout */
114#define	SSH_SUBPROCESS_STDOUT_CAPTURE	(1<<1)  /* Redirect stdout */
115#define	SSH_SUBPROCESS_STDERR_DISCARD	(1<<2)  /* Discard stderr */
116#define	SSH_SUBPROCESS_UNSAFE_PATH	(1<<3)	/* Don't check for safe cmd */
117#define	SSH_SUBPROCESS_PRESERVE_ENV	(1<<4)	/* Keep parent environment */
118pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
119    struct passwd *, privdrop_fn *, privrestore_fn *);
120
121typedef struct arglist arglist;
122struct arglist {
123	char    **list;
124	u_int   num;
125	u_int   nalloc;
126};
127void	 addargs(arglist *, const char *, ...)
128	     __attribute__((format(printf, 2, 3)));
129void	 replacearg(arglist *, u_int, const char *, ...)
130	     __attribute__((format(printf, 3, 4)));
131void	 freeargs(arglist *);
132
133int	 tun_open(int, int, char **);
134
135/* Common definitions for ssh tunnel device forwarding */
136#define SSH_TUNMODE_NO		0x00
137#define SSH_TUNMODE_POINTOPOINT	0x01
138#define SSH_TUNMODE_ETHERNET	0x02
139#define SSH_TUNMODE_DEFAULT	SSH_TUNMODE_POINTOPOINT
140#define SSH_TUNMODE_YES		(SSH_TUNMODE_POINTOPOINT|SSH_TUNMODE_ETHERNET)
141
142#define SSH_TUNID_ANY		0x7fffffff
143#define SSH_TUNID_ERR		(SSH_TUNID_ANY - 1)
144#define SSH_TUNID_MAX		(SSH_TUNID_ANY - 2)
145
146/* Fake port to indicate that host field is really a path. */
147#define PORT_STREAMLOCAL	-2
148
149/* Functions to extract or store big-endian words of various sizes */
150u_int64_t	get_u64(const void *)
151    __attribute__((__bounded__( __minbytes__, 1, 8)));
152u_int32_t	get_u32(const void *)
153    __attribute__((__bounded__( __minbytes__, 1, 4)));
154u_int16_t	get_u16(const void *)
155    __attribute__((__bounded__( __minbytes__, 1, 2)));
156void		put_u64(void *, u_int64_t)
157    __attribute__((__bounded__( __minbytes__, 1, 8)));
158void		put_u32(void *, u_int32_t)
159    __attribute__((__bounded__( __minbytes__, 1, 4)));
160void		put_u16(void *, u_int16_t)
161    __attribute__((__bounded__( __minbytes__, 1, 2)));
162
163/* Little-endian store/load, used by umac.c */
164u_int32_t	get_u32_le(const void *)
165    __attribute__((__bounded__(__minbytes__, 1, 4)));
166void		put_u32_le(void *, u_int32_t)
167    __attribute__((__bounded__(__minbytes__, 1, 4)));
168
169struct bwlimit {
170	size_t buflen;
171	u_int64_t rate;		/* desired rate in kbit/s */
172	u_int64_t thresh;	/* threshold after which we'll check timers */
173	u_int64_t lamt;		/* amount written in last timer interval */
174	struct timeval bwstart, bwend;
175};
176
177void bandwidth_limit_init(struct bwlimit *, u_int64_t, size_t);
178void bandwidth_limit(struct bwlimit *, size_t);
179
180int parse_ipqos(const char *);
181const char *iptos2str(int);
182void mktemp_proto(char *, size_t);
183
184void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
185	    const char *value);
186const char *lookup_env_in_list(const char *env,
187	    char * const *envs, size_t nenvs);
188const char *lookup_setenv_in_list(const char *env,
189	    char * const *envs, size_t nenvs);
190
191int	 argv_split(const char *, int *, char ***, int);
192char	*argv_assemble(int, char **argv);
193char	*argv_next(int *, char ***);
194void	 argv_consume(int *);
195void	 argv_free(char **, int);
196
197int	 exited_cleanly(pid_t, const char *, const char *, int);
198
199struct stat;
200int	 safe_path(const char *, struct stat *, const char *, uid_t,
201	    char *, size_t);
202int	 safe_path_fd(int, const char *, struct passwd *,
203	    char *err, size_t errlen);
204
205/* authorized_key-style options parsing helpers */
206int	opt_flag(const char *opt, int allow_negate, const char **optsp);
207char	*opt_dequote(const char **sp, const char **errstrp);
208int	opt_match(const char **opts, const char *term);
209
210/* readconf/servconf option lists */
211void	opt_array_append(const char *file, const int line,
212	    const char *directive, char ***array, u_int *lp, const char *s);
213void	opt_array_append2(const char *file, const int line,
214	    const char *directive, char ***array, int **iarray, u_int *lp,
215	    const char *s, int i);
216
217struct timespec;
218void ptimeout_init(struct timespec *pt);
219void ptimeout_deadline_sec(struct timespec *pt, long sec);
220void ptimeout_deadline_ms(struct timespec *pt, long ms);
221void ptimeout_deadline_monotime_tsp(struct timespec *pt, struct timespec *when);
222void ptimeout_deadline_monotime(struct timespec *pt, time_t when);
223int ptimeout_get_ms(struct timespec *pt);
224struct timespec *ptimeout_get_tsp(struct timespec *pt);
225int ptimeout_isset(struct timespec *pt);
226
227/* readpass.c */
228
229#define RP_ECHO			0x0001
230#define RP_ALLOW_STDIN		0x0002
231#define RP_ALLOW_EOF		0x0004
232#define RP_USE_ASKPASS		0x0008
233
234struct notifier_ctx;
235
236char	*read_passphrase(const char *, int);
237int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
238struct notifier_ctx *notify_start(int, const char *, ...)
239	__attribute__((format(printf, 2, 3)));
240void	notify_complete(struct notifier_ctx *, const char *, ...)
241	__attribute__((format(printf, 2, 3)));
242
243#define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
244#define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
245#define ROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
246
247typedef void (*sshsig_t)(int);
248sshsig_t ssh_signal(int, sshsig_t);
249
250/* On OpenBSD time_t is int64_t which is long long. */
251#define SSH_TIME_T_MAX LLONG_MAX
252
253#endif /* _MISC_H */
254