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