xref: /netbsd-src/crypto/external/bsd/openssh/dist/misc.h (revision c5555919c1ef14dca119ee5d4a4e93656523a56e)
1 /*	$NetBSD: misc.h,v 1.28 2024/06/25 16:36:54 christos Exp $	*/
2 /* $OpenBSD: misc.h,v 1.107 2024/03/04 02:16:11 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. */
26 struct 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 
37 int forward_equals(const struct Forward *, const struct Forward *);
38 int daemonized(void);
39 
40 /* Common server and client forwarding options. */
41 struct 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 
49 char	*chop(char *);
50 void	 rtrim(char *);
51 void	skip_space(char **);
52 char	*strdelim(char **);
53 char	*strdelimw(char **);
54 int	 set_nonblock(int);
55 int	 unset_nonblock(int);
56 void	 set_nodelay(int);
57 int	 set_reuseaddr(int);
58 char	*get_rdomain(int);
59 int	 set_rdomain(int, const char *);
60 int	 get_sock_af(int);
61 void	 set_sock_tos(int, int);
62 int	 waitrfd(int, int *, volatile sig_atomic_t *);
63 int	 timeout_connect(int, const struct sockaddr *, socklen_t, int *);
64 int	 a2port(const char *);
65 int	 a2tun(const char *, int *);
66 char	*put_host_port(const char *, u_short);
67 char	*hpdelim2(char **, char *);
68 char	*hpdelim(char **);
69 char	*cleanhostname(char *);
70 char	*colon(char *);
71 int	 parse_user_host_path(const char *, char **, char **, char **);
72 int	 parse_user_host_port(const char *, char **, char **, int *);
73 int	 parse_uri(const char *, const char *, char **, char **, int *, char **);
74 int	 convtime(const char *);
75 const char *fmt_timeframe(time_t t);
76 int	 tilde_expand(const char *, uid_t, char **);
77 char	*tilde_expand_filename(const char *, uid_t);
78 
79 char	*dollar_expand(int *, const char *string, ...);
80 char	*percent_expand(const char *, ...) __attribute__((__sentinel__));
81 char	*percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
82 char	*tohex(const void *, size_t);
83 void	 xextendf(char **s, const char *sep, const char *fmt, ...)
84     __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
85 void	 sanitise_stdfd(void);
86 struct timeval;
87 void	 ms_subtract_diff(struct timeval *, int *);
88 void	 ms_to_timespec(struct timespec *, int);
89 void	 monotime_ts(struct timespec *);
90 void	 monotime_tv(struct timeval *);
91 time_t	 monotime(void);
92 double	 monotime_double(void);
93 void	 lowercase(char *s);
94 int	 unix_listener(const char *, int, int);
95 int	 valid_domain(char *, int, const char **);
96 int	 valid_env_name(const char *);
97 const char *atoi_err(const char *, int *);
98 int	 parse_absolute_time(const char *, uint64_t *);
99 void	 format_absolute_time(uint64_t, char *, size_t);
100 int	 parse_pattern_interval(const char *, char **, int *);
101 int	 path_absolute(const char *);
102 int	 stdfd_devnull(int, int, int);
103 int	 lib_contains_symbol(const char *, const char *);
104 
105 int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
106     u_int8_t *, size_t, unsigned int);
107 
108 struct passwd *pwcopy(struct passwd *);
109 const char *ssh_gai_strerror(int);
110 
111 typedef void privdrop_fn(struct passwd *);
112 typedef 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 */
118 pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
119     struct passwd *, privdrop_fn *, privrestore_fn *);
120 
121 typedef struct arglist arglist;
122 struct arglist {
123 	char    **list;
124 	u_int   num;
125 	u_int   nalloc;
126 };
127 void	 addargs(arglist *, const char *, ...)
128 	     __attribute__((format(printf, 2, 3)));
129 void	 replacearg(arglist *, u_int, const char *, ...)
130 	     __attribute__((format(printf, 3, 4)));
131 void	 freeargs(arglist *);
132 
133 int	 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 */
150 u_int64_t	get_u64(const void *)
151     __attribute__((__bounded__( __minbytes__, 1, 8)));
152 u_int32_t	get_u32(const void *)
153     __attribute__((__bounded__( __minbytes__, 1, 4)));
154 u_int16_t	get_u16(const void *)
155     __attribute__((__bounded__( __minbytes__, 1, 2)));
156 void		put_u64(void *, u_int64_t)
157     __attribute__((__bounded__( __minbytes__, 1, 8)));
158 void		put_u32(void *, u_int32_t)
159     __attribute__((__bounded__( __minbytes__, 1, 4)));
160 void		put_u16(void *, u_int16_t)
161     __attribute__((__bounded__( __minbytes__, 1, 2)));
162 
163 /* Little-endian store/load, used by umac.c */
164 u_int32_t	get_u32_le(const void *)
165     __attribute__((__bounded__(__minbytes__, 1, 4)));
166 void		put_u32_le(void *, u_int32_t)
167     __attribute__((__bounded__(__minbytes__, 1, 4)));
168 
169 struct 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 
177 void bandwidth_limit_init(struct bwlimit *, u_int64_t, size_t);
178 void bandwidth_limit(struct bwlimit *, size_t);
179 
180 int parse_ipqos(const char *);
181 const char *iptos2str(int);
182 void mktemp_proto(char *, size_t);
183 
184 void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
185 	    const char *value);
186 const char *lookup_env_in_list(const char *env,
187 	    char * const *envs, size_t nenvs);
188 const char *lookup_setenv_in_list(const char *env,
189 	    char * const *envs, size_t nenvs);
190 
191 int	 argv_split(const char *, int *, char ***, int);
192 char	*argv_assemble(int, char **argv);
193 char	*argv_next(int *, char ***);
194 void	 argv_consume(int *);
195 void	 argv_free(char **, int);
196 
197 int	 exited_cleanly(pid_t, const char *, const char *, int);
198 
199 struct stat;
200 int	 safe_path(const char *, struct stat *, const char *, uid_t,
201 	    char *, size_t);
202 int	 safe_path_fd(int, const char *, struct passwd *,
203 	    char *err, size_t errlen);
204 
205 /* authorized_key-style options parsing helpers */
206 int	opt_flag(const char *opt, int allow_negate, const char **optsp);
207 char	*opt_dequote(const char **sp, const char **errstrp);
208 int	opt_match(const char **opts, const char *term);
209 
210 /* readconf/servconf option lists */
211 void	opt_array_append(const char *file, const int line,
212 	    const char *directive, char ***array, u_int *lp, const char *s);
213 void	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 void	opt_array_free2(char **array, int **iarray, u_int l);
217 
218 struct timespec;
219 void ptimeout_init(struct timespec *pt);
220 void ptimeout_deadline_sec(struct timespec *pt, long sec);
221 void ptimeout_deadline_ms(struct timespec *pt, long ms);
222 void ptimeout_deadline_monotime_tsp(struct timespec *pt, struct timespec *when);
223 void ptimeout_deadline_monotime(struct timespec *pt, time_t when);
224 int ptimeout_get_ms(struct timespec *pt);
225 struct timespec *ptimeout_get_tsp(struct timespec *pt);
226 int ptimeout_isset(struct timespec *pt);
227 
228 /* readpass.c */
229 
230 #define RP_ECHO			0x0001
231 #define RP_ALLOW_STDIN		0x0002
232 #define RP_ALLOW_EOF		0x0004
233 #define RP_USE_ASKPASS		0x0008
234 
235 struct notifier_ctx;
236 
237 char	*read_passphrase(const char *, int);
238 int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
239 struct notifier_ctx *notify_start(int, const char *, ...)
240 	__attribute__((format(printf, 2, 3)));
241 void	notify_complete(struct notifier_ctx *, const char *, ...)
242 	__attribute__((format(printf, 2, 3)));
243 
244 #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
245 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
246 #define ROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
247 
248 typedef void (*sshsig_t)(int);
249 sshsig_t ssh_signal(int, sshsig_t);
250 
251 /* On OpenBSD time_t is int64_t which is long long. */
252 #define SSH_TIME_T_MAX LLONG_MAX
253 
254 #endif /* _MISC_H */
255