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