Home | History | Annotate | Line # | Download | only in dist
misc.h revision 1.19
      1  1.17  christos /*	$NetBSD: misc.h,v 1.19 2020/12/04 18:42:50 christos Exp $	*/
      2  1.19  christos /* $OpenBSD: misc.h,v 1.87 2020/05/29 11:17:56 dtucker 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.11  christos 
     23   1.7  christos /* Data structure for representing a forwarding request. */
     24   1.7  christos struct Forward {
     25   1.7  christos 	char	 *listen_host;		/* Host (address) to listen on. */
     26   1.7  christos 	int	  listen_port;		/* Port to forward. */
     27   1.7  christos 	char	 *listen_path;		/* Path to bind domain socket. */
     28   1.7  christos 	char	 *connect_host;		/* Host to connect. */
     29   1.7  christos 	int	  connect_port;		/* Port to connect on connect_host. */
     30   1.7  christos 	char	 *connect_path;		/* Path to connect domain socket. */
     31   1.7  christos 	int	  allocated_port;	/* Dynamically allocated listen port */
     32   1.7  christos 	int	  handle;		/* Handle for dynamic listen ports */
     33   1.7  christos };
     34   1.7  christos 
     35  1.10  christos int forward_equals(const struct Forward *, const struct Forward *);
     36  1.11  christos int daemonized(void);
     37  1.10  christos 
     38   1.7  christos /* Common server and client forwarding options. */
     39   1.7  christos struct ForwardOptions {
     40   1.7  christos 	int	 gateway_ports; /* Allow remote connects to forwarded ports. */
     41   1.7  christos 	mode_t	 streamlocal_bind_mask; /* umask for streamlocal binds */
     42   1.7  christos 	int	 streamlocal_bind_unlink; /* unlink socket before bind */
     43   1.7  christos };
     44   1.7  christos 
     45   1.1  christos /* misc.c */
     46   1.1  christos 
     47   1.1  christos char	*chop(char *);
     48  1.17  christos void	skip_space(char **);
     49   1.1  christos char	*strdelim(char **);
     50  1.15  christos char	*strdelimw(char **);
     51   1.1  christos int	 set_nonblock(int);
     52   1.1  christos int	 unset_nonblock(int);
     53   1.1  christos void	 set_nodelay(int);
     54  1.14  christos int	 set_reuseaddr(int);
     55  1.14  christos char	*get_rdomain(int);
     56  1.14  christos int	 set_rdomain(int, const char *);
     57  1.16  christos int	 waitrfd(int, int *);
     58  1.16  christos int	 timeout_connect(int, const struct sockaddr *, socklen_t, int *);
     59   1.1  christos int	 a2port(const char *);
     60   1.1  christos int	 a2tun(const char *, int *);
     61   1.1  christos char	*put_host_port(const char *, u_short);
     62  1.16  christos char	*hpdelim2(char **, char *);
     63   1.1  christos char	*hpdelim(char **);
     64   1.1  christos char	*cleanhostname(char *);
     65   1.1  christos char	*colon(char *);
     66  1.14  christos int	 parse_user_host_path(const char *, char **, char **, const char **);
     67  1.10  christos int	 parse_user_host_port(const char *, char **, char **, int *);
     68  1.14  christos int	 parse_uri(const char *, const char *, char **, char **, int *, const char **);
     69   1.1  christos long	 convtime(const char *);
     70  1.19  christos const char *fmt_timeframe(time_t t);
     71   1.1  christos char	*tilde_expand_filename(const char *, uid_t);
     72  1.19  christos 
     73  1.19  christos char	*dollar_expand(int *, const char *string, ...);
     74  1.19  christos char	*percent_expand(const char *, ...) __attribute__((__sentinel__));
     75  1.19  christos char	*percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
     76   1.1  christos char	*tohex(const void *, size_t);
     77  1.18  christos void	 xextendf(char **s, const char *sep, const char *fmt, ...)
     78  1.18  christos     __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
     79   1.1  christos void	 sanitise_stdfd(void);
     80   1.7  christos struct timeval;
     81   1.1  christos void	 ms_subtract_diff(struct timeval *, int *);
     82   1.1  christos void	 ms_to_timeval(struct timeval *, int);
     83  1.14  christos void	 monotime_ts(struct timespec *);
     84  1.14  christos void	 monotime_tv(struct timeval *);
     85   1.6  christos time_t	 monotime(void);
     86  1.10  christos double	 monotime_double(void);
     87   1.7  christos void	 lowercase(char *s);
     88   1.7  christos int	 unix_listener(const char *, int, int);
     89  1.14  christos int	 valid_domain(char *, int, const char **);
     90  1.16  christos int	 valid_env_name(const char *);
     91  1.14  christos const char *atoi_err(const char *, int *);
     92  1.14  christos int	 parse_absolute_time(const char *, uint64_t *);
     93  1.14  christos void	 format_absolute_time(uint64_t, char *, size_t);
     94  1.16  christos int	 path_absolute(const char *);
     95   1.4  christos 
     96   1.7  christos int	bcrypt_pbkdf(const char *, size_t, const u_int8_t *, size_t,
     97   1.7  christos     u_int8_t *, size_t, unsigned int);
     98   1.2  christos 
     99   1.1  christos struct passwd *pwcopy(struct passwd *);
    100   1.1  christos const char *ssh_gai_strerror(int);
    101   1.1  christos 
    102   1.1  christos typedef struct arglist arglist;
    103   1.1  christos struct arglist {
    104   1.1  christos 	char    **list;
    105   1.1  christos 	u_int   num;
    106   1.1  christos 	u_int   nalloc;
    107   1.1  christos };
    108   1.4  christos void	 addargs(arglist *, const char *, ...)
    109   1.1  christos 	     __attribute__((format(printf, 2, 3)));
    110   1.4  christos void	 replacearg(arglist *, u_int, const char *, ...)
    111   1.1  christos 	     __attribute__((format(printf, 3, 4)));
    112   1.1  christos void	 freeargs(arglist *);
    113   1.1  christos 
    114  1.14  christos int	 tun_open(int, int, char **);
    115   1.1  christos 
    116   1.1  christos /* Common definitions for ssh tunnel device forwarding */
    117   1.1  christos #define SSH_TUNMODE_NO		0x00
    118   1.1  christos #define SSH_TUNMODE_POINTOPOINT	0x01
    119   1.1  christos #define SSH_TUNMODE_ETHERNET	0x02
    120   1.1  christos #define SSH_TUNMODE_DEFAULT	SSH_TUNMODE_POINTOPOINT
    121   1.1  christos #define SSH_TUNMODE_YES		(SSH_TUNMODE_POINTOPOINT|SSH_TUNMODE_ETHERNET)
    122   1.1  christos 
    123   1.1  christos #define SSH_TUNID_ANY		0x7fffffff
    124   1.1  christos #define SSH_TUNID_ERR		(SSH_TUNID_ANY - 1)
    125   1.1  christos #define SSH_TUNID_MAX		(SSH_TUNID_ANY - 2)
    126   1.1  christos 
    127   1.7  christos /* Fake port to indicate that host field is really a path. */
    128   1.7  christos #define PORT_STREAMLOCAL	-2
    129   1.7  christos 
    130   1.1  christos /* Functions to extract or store big-endian words of various sizes */
    131   1.1  christos u_int64_t	get_u64(const void *)
    132   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 8)));
    133   1.1  christos u_int32_t	get_u32(const void *)
    134   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 4)));
    135   1.1  christos u_int16_t	get_u16(const void *)
    136   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 2)));
    137   1.1  christos void		put_u64(void *, u_int64_t)
    138   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 8)));
    139   1.1  christos void		put_u32(void *, u_int32_t)
    140   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 4)));
    141   1.1  christos void		put_u16(void *, u_int16_t)
    142   1.7  christos     __attribute__((__bounded__( __minbytes__, 1, 2)));
    143   1.7  christos 
    144   1.7  christos /* Little-endian store/load, used by umac.c */
    145   1.7  christos u_int32_t	get_u32_le(const void *)
    146   1.7  christos     __attribute__((__bounded__(__minbytes__, 1, 4)));
    147   1.7  christos void		put_u32_le(void *, u_int32_t)
    148   1.7  christos     __attribute__((__bounded__(__minbytes__, 1, 4)));
    149   1.1  christos 
    150   1.4  christos struct bwlimit {
    151   1.4  christos 	size_t buflen;
    152  1.16  christos 	u_int64_t rate;		/* desired rate in kbit/s */
    153  1.16  christos 	u_int64_t thresh;	/* threshold after which we'll check timers */
    154  1.16  christos 	u_int64_t lamt;		/* amount written in last timer interval */
    155   1.4  christos 	struct timeval bwstart, bwend;
    156   1.4  christos };
    157   1.4  christos 
    158   1.4  christos void bandwidth_limit_init(struct bwlimit *, u_int64_t, size_t);
    159   1.4  christos void bandwidth_limit(struct bwlimit *, size_t);
    160   1.4  christos 
    161   1.4  christos int parse_ipqos(const char *);
    162   1.5  christos const char *iptos2str(int);
    163   1.4  christos void mktemp_proto(char *, size_t);
    164   1.1  christos 
    165  1.13  christos void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
    166  1.13  christos 	     const char *value);
    167  1.13  christos 
    168  1.13  christos int	 argv_split(const char *, int *, char ***);
    169  1.13  christos char	*argv_assemble(int, char **argv);
    170  1.13  christos int	 exited_cleanly(pid_t, const char *, const char *, int);
    171  1.13  christos 
    172  1.13  christos struct stat;
    173  1.13  christos int	 safe_path(const char *, struct stat *, const char *, uid_t,
    174  1.13  christos 	     char *, size_t);
    175  1.13  christos int	 safe_path_fd(int, const char *, struct passwd *,
    176  1.13  christos 	     char *err, size_t errlen);
    177  1.13  christos 
    178  1.17  christos /* authorized_key-style options parsing helpers */
    179  1.17  christos int	opt_flag(const char *opt, int allow_negate, const char **optsp);
    180  1.17  christos char	*opt_dequote(const char **sp, const char **errstrp);
    181  1.17  christos int	opt_match(const char **opts, const char *term);
    182  1.17  christos 
    183   1.1  christos /* readpass.c */
    184   1.1  christos 
    185   1.1  christos #define RP_ECHO			0x0001
    186   1.1  christos #define RP_ALLOW_STDIN		0x0002
    187   1.1  christos #define RP_ALLOW_EOF		0x0004
    188   1.1  christos #define RP_USE_ASKPASS		0x0008
    189   1.1  christos 
    190  1.18  christos struct notifier_ctx;
    191  1.18  christos 
    192   1.1  christos char	*read_passphrase(const char *, int);
    193   1.1  christos int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
    194  1.18  christos struct notifier_ctx *notify_start(int, const char *, ...)
    195  1.18  christos 	__attribute__((format(printf, 2, 3)));
    196  1.18  christos void	notify_complete(struct notifier_ctx *);
    197   1.1  christos 
    198  1.11  christos #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
    199  1.11  christos #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
    200  1.11  christos #define ROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
    201  1.11  christos 
    202  1.18  christos typedef void (*sshsig_t)(int);
    203  1.18  christos sshsig_t ssh_signal(int, sshsig_t);
    204   1.1  christos #endif /* _MISC_H */
    205