Home | History | Annotate | Line # | Download | only in dist
      1 /*	$NetBSD: misc.h,v 1.33 2026/04/08 18:58:40 christos Exp $	*/
      2 /* $OpenBSD: misc.h,v 1.116 2026/03/11 09:10:59 dtucker Exp $ */
      3 
      4 /*
      5  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      6  * Copyright (c) 1995 Tatu Ylonen <ylo (at) 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 const char *strprefix(const char *, const char *, int);
     61 char	*strdelim(char **);
     62 char	*strdelimw(char **);
     63 void	 stringlist_append(char ***listp, const char *s);
     64 void	 stringlist_free(char **list);
     65 int	 set_nonblock(int);
     66 int	 unset_nonblock(int);
     67 void	 set_nodelay(int);
     68 int	 set_reuseaddr(int);
     69 char	*get_rdomain(int);
     70 int	 set_rdomain(int, const char *);
     71 int	 get_sock_af(int);
     72 void	 set_sock_tos(int, int);
     73 int	 waitrfd(int, int *, volatile sig_atomic_t *);
     74 int	 timeout_connect(int, const struct sockaddr *, socklen_t, int *);
     75 int	 a2port(const char *);
     76 int	 a2tun(const char *, int *);
     77 char	*put_host_port(const char *, u_short);
     78 char	*hpdelim2(char **, char *);
     79 char	*hpdelim(char **);
     80 char	*cleanhostname(char *);
     81 char	*colon(char *);
     82 int	 parse_user_host_path(const char *, char **, char **, char **);
     83 int	 parse_user_host_port(const char *, char **, char **, int *);
     84 int	 parse_uri(const char *, const char *, char **, char **, int *, char **);
     85 double	 convtime_double(const char *);
     86 int	 convtime(const char *);
     87 double	 convtime_double(const char *);
     88 const char *fmt_timeframe(time_t t);
     89 int	 tilde_expand(const char *, uid_t, char **);
     90 char	*tilde_expand_filename(const char *, uid_t);
     91 
     92 char	*dollar_expand(int *, const char *string, ...);
     93 char	*percent_expand(const char *, ...) __attribute__((__sentinel__));
     94 char	*percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
     95 char	*tohex(const void *, size_t);
     96 void	 xextendf(char **s, const char *sep, const char *fmt, ...)
     97     __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
     98 void	 sanitise_stdfd(void);
     99 struct timeval;
    100 void	 ms_subtract_diff(struct timeval *, int *);
    101 void	 ms_to_timespec(struct timespec *, int);
    102 void	 monotime_ts(struct timespec *);
    103 void	 monotime_tv(struct timeval *);
    104 time_t	 monotime(void);
    105 double	 monotime_double(void);
    106 void	 lowercase(char *s);
    107 int	 unix_listener(const char *, int, int);
    108 int	 valid_domain(char *, int, const char **);
    109 int	 valid_env_name(const char *);
    110 const char *atoi_err(const char *, int *);
    111 int	 parse_absolute_time(const char *, uint64_t *);
    112 void	 format_absolute_time(uint64_t, char *, size_t);
    113 int	 parse_pattern_interval(const char *, char **, int *);
    114 int	 path_absolute(const char *);
    115 int	 stdfd_devnull(int, int, int);
    116 int	 lib_contains_symbol(const char *, const char *);
    117 char	*get_homedir(void);
    118 
    119 int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
    120     u_int8_t *, size_t, unsigned int);
    121 
    122 struct passwd *pwcopy(struct passwd *);
    123 void	 pwfree(struct passwd *); /* NB. only use with pwcopy */
    124 
    125 const char *ssh_gai_strerror(int);
    126 
    127 typedef void privdrop_fn(struct passwd *);
    128 typedef void privrestore_fn(void);
    129 #define	SSH_SUBPROCESS_STDOUT_DISCARD	(1)     /* Discard stdout */
    130 #define	SSH_SUBPROCESS_STDOUT_CAPTURE	(1<<1)  /* Redirect stdout */
    131 #define	SSH_SUBPROCESS_STDERR_DISCARD	(1<<2)  /* Discard stderr */
    132 #define	SSH_SUBPROCESS_UNSAFE_PATH	(1<<3)	/* Don't check for safe cmd */
    133 #define	SSH_SUBPROCESS_PRESERVE_ENV	(1<<4)	/* Keep parent environment */
    134 pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
    135     struct passwd *, privdrop_fn *, privrestore_fn *);
    136 
    137 typedef struct arglist arglist;
    138 struct arglist {
    139 	char    **list;
    140 	u_int   num;
    141 	u_int   nalloc;
    142 };
    143 void	 addargs(arglist *, const char *, ...)
    144 	     __attribute__((format(printf, 2, 3)));
    145 void	 replacearg(arglist *, u_int, const char *, ...)
    146 	     __attribute__((format(printf, 3, 4)));
    147 void	 freeargs(arglist *);
    148 
    149 int	 tun_open(int, int, char **);
    150 
    151 /* Common definitions for ssh tunnel device forwarding */
    152 #define SSH_TUNMODE_NO		0x00
    153 #define SSH_TUNMODE_POINTOPOINT	0x01
    154 #define SSH_TUNMODE_ETHERNET	0x02
    155 #define SSH_TUNMODE_DEFAULT	SSH_TUNMODE_POINTOPOINT
    156 #define SSH_TUNMODE_YES		(SSH_TUNMODE_POINTOPOINT|SSH_TUNMODE_ETHERNET)
    157 
    158 #define SSH_TUNID_ANY		0x7fffffff
    159 #define SSH_TUNID_ERR		(SSH_TUNID_ANY - 1)
    160 #define SSH_TUNID_MAX		(SSH_TUNID_ANY - 2)
    161 
    162 /* Fake port to indicate that host field is really a path. */
    163 #define PORT_STREAMLOCAL	-2
    164 
    165 /* Functions to extract or store big-endian words of various sizes */
    166 uint64_t	get_u64(const void *)
    167     __attribute__((__bounded__( __minbytes__, 1, 8)));
    168 uint32_t	get_u32(const void *)
    169     __attribute__((__bounded__( __minbytes__, 1, 4)));
    170 uint16_t	get_u16(const void *)
    171     __attribute__((__bounded__( __minbytes__, 1, 2)));
    172 void		put_u64(void *, uint64_t)
    173     __attribute__((__bounded__( __minbytes__, 1, 8)));
    174 void		put_u32(void *, uint32_t)
    175     __attribute__((__bounded__( __minbytes__, 1, 4)));
    176 void		put_u16(void *, uint16_t)
    177     __attribute__((__bounded__( __minbytes__, 1, 2)));
    178 
    179 /* Little-endian store/load, used by umac.c */
    180 uint32_t	get_u32_le(const void *)
    181     __attribute__((__bounded__(__minbytes__, 1, 4)));
    182 void		put_u32_le(void *, uint32_t)
    183     __attribute__((__bounded__(__minbytes__, 1, 4)));
    184 
    185 struct bwlimit {
    186 	size_t buflen;
    187 	uint64_t rate;		/* desired rate in kbit/s */
    188 	uint64_t thresh;	/* threshold after which we'll check timers */
    189 	uint64_t lamt;		/* amount written in last timer interval */
    190 	struct timeval bwstart, bwend;
    191 };
    192 
    193 void bandwidth_limit_init(struct bwlimit *, uint64_t, size_t);
    194 void bandwidth_limit(struct bwlimit *, size_t);
    195 
    196 int parse_ipqos(const char *);
    197 const char *iptos2str(int);
    198 void mktemp_proto(char *, size_t);
    199 
    200 void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
    201 	    const char *value);
    202 const char *lookup_env_in_list(const char *env,
    203 	    char * const *envs, size_t nenvs);
    204 const char *lookup_setenv_in_list(const char *env,
    205 	    char * const *envs, size_t nenvs);
    206 
    207 int	 argv_split(const char *, int *, char ***, int);
    208 char	*argv_assemble(int, char **argv);
    209 char	*argv_next(int *, char ***);
    210 void	 argv_consume(int *);
    211 void	 argv_free(char **, int);
    212 
    213 int	 exited_cleanly(pid_t, const char *, const char *, int);
    214 
    215 struct stat;
    216 int	 safe_path(const char *, struct stat *, const char *, uid_t,
    217 	    char *, size_t);
    218 int	 safe_path_fd(int, const char *, struct passwd *,
    219 	    char *err, size_t errlen);
    220 
    221 /* authorized_key-style options parsing helpers */
    222 int	opt_flag(const char *opt, int allow_negate, const char **optsp);
    223 char	*opt_dequote(const char **sp, const char **errstrp);
    224 int	opt_match(const char **opts, const char *term);
    225 
    226 /* readconf/servconf option lists */
    227 void	opt_array_append(const char *file, const int line,
    228 	    const char *directive, char ***array, u_int *lp, const char *s);
    229 void	opt_array_append2(const char *file, const int line,
    230 	    const char *directive, char ***array, int **iarray, u_int *lp,
    231 	    const char *s, int i);
    232 void	opt_array_free2(char **array, int **iarray, u_int l);
    233 
    234 struct timespec;
    235 void ptimeout_init(struct timespec *pt);
    236 void ptimeout_deadline_sec(struct timespec *pt, long sec);
    237 void ptimeout_deadline_ms(struct timespec *pt, long ms);
    238 void ptimeout_deadline_monotime_tsp(struct timespec *pt, struct timespec *when);
    239 void ptimeout_deadline_monotime(struct timespec *pt, time_t when);
    240 int ptimeout_get_ms(struct timespec *pt);
    241 struct timespec *ptimeout_get_tsp(struct timespec *pt);
    242 int ptimeout_isset(struct timespec *pt);
    243 
    244 /* misc-agent.c */
    245 char	*agent_hostname_hash(void);
    246 int	 agent_listener(const char *, const char *, int *, char **);
    247 void	 agent_cleanup_stale(const char *, int);
    248 
    249 /* readpass.c */
    250 
    251 #define RP_ECHO			0x0001
    252 #define RP_ALLOW_STDIN		0x0002
    253 #define RP_ALLOW_EOF		0x0004
    254 #define RP_USE_ASKPASS		0x0008
    255 
    256 struct notifier_ctx;
    257 
    258 char	*read_passphrase(const char *, int);
    259 int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
    260 struct notifier_ctx *notify_start(int, const char *, ...)
    261 	__attribute__((format(printf, 2, 3)));
    262 void	notify_complete(struct notifier_ctx *, const char *, ...)
    263 	__attribute__((format(printf, 2, 3)));
    264 
    265 #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
    266 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
    267 #define ROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
    268 
    269 typedef void (*sshsig_t)(int);
    270 sshsig_t ssh_signal(int, sshsig_t);
    271 int signal_is_crash(int);
    272 
    273 /* On OpenBSD time_t is int64_t which is long long. */
    274 #define SSH_TIME_T_MAX LLONG_MAX
    275 
    276 #define FD_CLOSEONEXEC(x) do { \
    277 	if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
    278 		fatal_f("fcntl(%d, F_SETFD, FD_CLOEXEC): %s", x, \
    279 		    strerror(errno)); \
    280 } while (0)
    281 
    282 
    283 #endif /* _MISC_H */
    284