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