xref: /openssh-portable/misc.h (revision 31d8d231)
1*31d8d231Sdjm@openbsd.org /* $OpenBSD: misc.h,v 1.95 2021/04/03 06:18:40 djm Exp $ */
236579d3dSBen Lindstrom 
3226cfa03SBen Lindstrom /*
4226cfa03SBen Lindstrom  * Author: Tatu Ylonen <ylo@cs.hut.fi>
5226cfa03SBen Lindstrom  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6226cfa03SBen Lindstrom  *                    All rights reserved
7226cfa03SBen Lindstrom  *
8226cfa03SBen Lindstrom  * As far as I am concerned, the code I have written for this software
9226cfa03SBen Lindstrom  * can be used freely for any purpose.  Any derived versions of this
10226cfa03SBen Lindstrom  * software must be clearly marked as such, and if the derived work is
11226cfa03SBen Lindstrom  * incompatible with the protocol description in the RFC file, it must be
12226cfa03SBen Lindstrom  * called by a name other than "ssh" or "Secure Shell".
13226cfa03SBen Lindstrom  */
144cc240daSBen Lindstrom 
153f941889SDamien Miller #ifndef _MISC_H
163f941889SDamien Miller #define _MISC_H
173f941889SDamien Miller 
185bcc1e27Sguenther@openbsd.org #include <sys/time.h>
19de4ae07fSdjm@openbsd.org #include <sys/types.h>
200a843d9aSdjm@openbsd.org #include <sys/socket.h>
217fe141b9Sdjm@openbsd.org #include <stdio.h>
237acefbbcSDamien Miller /* Data structure for representing a forwarding request. */
247acefbbcSDamien Miller struct Forward {
257acefbbcSDamien Miller 	char	 *listen_host;		/* Host (address) to listen on. */
267acefbbcSDamien Miller 	int	  listen_port;		/* Port to forward. */
277acefbbcSDamien Miller 	char	 *listen_path;		/* Path to bind domain socket. */
287acefbbcSDamien Miller 	char	 *connect_host;		/* Host to connect. */
297acefbbcSDamien Miller 	int	  connect_port;		/* Port to connect on connect_host. */
307acefbbcSDamien Miller 	char	 *connect_path;		/* Path to connect domain socket. */
317acefbbcSDamien Miller 	int	  allocated_port;	/* Dynamically allocated listen port */
327acefbbcSDamien Miller 	int	  handle;		/* Handle for dynamic listen ports */
337acefbbcSDamien Miller };
347acefbbcSDamien Miller 
350ccbd5ecSdjm@openbsd.org int forward_equals(const struct Forward *, const struct Forward *);
367fc4766aSdtucker@openbsd.org int daemonized(void);
387acefbbcSDamien Miller /* Common server and client forwarding options. */
397acefbbcSDamien Miller struct ForwardOptions {
407acefbbcSDamien Miller 	int	 gateway_ports; /* Allow remote connects to forwarded ports. */
417acefbbcSDamien Miller 	mode_t	 streamlocal_bind_mask; /* umask for streamlocal binds */
427acefbbcSDamien Miller 	int	 streamlocal_bind_unlink; /* unlink socket before bind */
437acefbbcSDamien Miller };
447acefbbcSDamien Miller 
45e608ca29SDarren Tucker /* misc.c */
46e608ca29SDarren Tucker 
4716ae3d0dSBen Lindstrom char	*chop(char *);
48c72d78ccSdjm@openbsd.org void	skip_space(char **);
4916ae3d0dSBen Lindstrom char	*strdelim(char **);
507082bb58Sdjm@openbsd.org char	*strdelimw(char **);
51232711f6SDamien Miller int	 set_nonblock(int);
52232711f6SDamien Miller int	 unset_nonblock(int);
53398e1cfaSDamien Miller void	 set_nodelay(int);
54acf559e1Sdjm@openbsd.org int	 set_reuseaddr(int);
5535eb33fbSdjm@openbsd.org char	*get_rdomain(int);
56acf559e1Sdjm@openbsd.org int	 set_rdomain(int, const char *);
5733313ebcSdjm@openbsd.org int	 get_sock_af(int);
5833313ebcSdjm@openbsd.org void	 set_sock_tos(int, int);
590a843d9aSdjm@openbsd.org int	 waitrfd(int, int *);
600a843d9aSdjm@openbsd.org int	 timeout_connect(int, const struct sockaddr *, socklen_t, int *);
6116ae3d0dSBen Lindstrom int	 a2port(const char *);
62d27b9471SDamien Miller int	 a2tun(const char *, int *);
63da345535SDarren Tucker char	*put_host_port(const char *, u_short);
64d05ea255Sdtucker@openbsd.org char	*hpdelim2(char **, char *);
65f91ee4c3SDamien Miller char	*hpdelim(char **);
6616ae3d0dSBen Lindstrom char	*cleanhostname(char *);
6716ae3d0dSBen Lindstrom char	*colon(char *);
68887669efSmillert@openbsd.org int	 parse_user_host_path(const char *, char **, char **, char **);
69ed877ef6Sdjm@openbsd.org int	 parse_user_host_port(const char *, char **, char **, int *);
70887669efSmillert@openbsd.org int	 parse_uri(const char *, const char *, char **, char **, int *, char **);
716d30673fSdtucker@openbsd.org int	 convtime(const char *);
72756c6f66Sdjm@openbsd.org const char *fmt_timeframe(time_t t);
735fd38c0eSDamien Miller char	*tilde_expand_filename(const char *, uid_t);
75712ac1efSdtucker@openbsd.org char	*dollar_expand(int *, const char *string, ...);
768e2eb308SDarren Tucker char	*percent_expand(const char *, ...) __attribute__((__sentinel__));
774a1b46e6Sdtucker@openbsd.org char	*percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
783f941889SDamien Miller char	*tohex(const void *, size_t);
798075fccbSdjm@openbsd.org void	 xextendf(char **s, const char *sep, const char *fmt, ...)
808075fccbSdjm@openbsd.org     __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
81ce321d8aSDarren Tucker void	 sanitise_stdfd(void);
823fc464efSDarren Tucker void	 ms_subtract_diff(struct timeval *, int *);
833fc464efSDarren Tucker void	 ms_to_timeval(struct timeval *, int);
845db6fbf1Sdtucker@openbsd.org@openbsd.org void	 monotime_ts(struct timespec *);
855db6fbf1Sdtucker@openbsd.org@openbsd.org void	 monotime_tv(struct timeval *);
86b759c9c2SDarren Tucker time_t	 monotime(void);
87b8d4eafeSdtucker@openbsd.org double	 monotime_double(void);
88e9fc72edSDamien Miller void	 lowercase(char *s);
897acefbbcSDamien Miller int	 unix_listener(const char *, int, int);
90887669efSmillert@openbsd.org int	 valid_domain(char *, int, const char **);
915eff5b85Sdjm@openbsd.org int	 valid_env_name(const char *);
92609d96b3Sdtucker@openbsd.org const char *atoi_err(const char *, int *);
93bf0fbf2bSdjm@openbsd.org int	 parse_absolute_time(const char *, uint64_t *);
94bf0fbf2bSdjm@openbsd.org void	 format_absolute_time(uint64_t, char *, size_t);
952a35862eSdjm@openbsd.org int	 path_absolute(const char *);
96396d32f3Sdjm@openbsd.org int	 stdfd_devnull(int, int, int);
97e9fc72edSDamien Miller 
9804ee0f8fSDamien Miller void	 sock_set_v6only(int);
991bda4c83SBen Lindstrom 
100d009ae3dSKevin Steves struct passwd *pwcopy(struct passwd *);
1014abde771SDarren Tucker const char *ssh_gai_strerror(int);
102d009ae3dSKevin Steves 
103a34e14a5Sdjm@openbsd.org typedef void privdrop_fn(struct passwd *);
104a34e14a5Sdjm@openbsd.org typedef void privrestore_fn(void);
105a34e14a5Sdjm@openbsd.org #define	SSH_SUBPROCESS_STDOUT_DISCARD	(1)     /* Discard stdout */
106a34e14a5Sdjm@openbsd.org #define	SSH_SUBPROCESS_STDOUT_CAPTURE	(1<<1)  /* Redirect stdout */
107a34e14a5Sdjm@openbsd.org #define	SSH_SUBPROCESS_STDERR_DISCARD	(1<<2)  /* Discard stderr */
108a34e14a5Sdjm@openbsd.org #define	SSH_SUBPROCESS_UNSAFE_PATH	(1<<3)	/* Don't check for safe cmd */
109a34e14a5Sdjm@openbsd.org #define	SSH_SUBPROCESS_PRESERVE_ENV	(1<<4)	/* Keep parent environment */
110a34e14a5Sdjm@openbsd.org pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
111a34e14a5Sdjm@openbsd.org     struct passwd *, privdrop_fn *, privrestore_fn *);
113387c4726SBen Lindstrom typedef struct arglist arglist;
114387c4726SBen Lindstrom struct arglist {
115387c4726SBen Lindstrom 	char    **list;
116c7a6fc41SDarren Tucker 	u_int   num;
117c7a6fc41SDarren Tucker 	u_int   nalloc;
118387c4726SBen Lindstrom };
1193eec6b73SDamien Miller void	 addargs(arglist *, char *, ...)
1203eec6b73SDamien Miller 	    __attribute__((format(printf, 2, 3)));
1213eec6b73SDamien Miller void	 replacearg(arglist *, u_int, char *, ...)
1223eec6b73SDamien Miller 	    __attribute__((format(printf, 3, 4)));
1233eec6b73SDamien Miller void	 freeargs(arglist *);
12406f2bd8bSDarren Tucker 
125b7548b12Sdjm@openbsd.org int	 tun_open(int, int, char **);
1267b58e800SDamien Miller 
1277b58e800SDamien Miller /* Common definitions for ssh tunnel device forwarding */
1287b58e800SDamien Miller #define SSH_TUNMODE_NO		0x00
1297b58e800SDamien Miller #define SSH_TUNMODE_POINTOPOINT	0x01
1307b58e800SDamien Miller #define SSH_TUNMODE_ETHERNET	0x02
1337b58e800SDamien Miller 
1347b58e800SDamien Miller #define SSH_TUNID_ANY		0x7fffffff
1357b58e800SDamien Miller #define SSH_TUNID_ERR		(SSH_TUNID_ANY - 1)
1367b58e800SDamien Miller #define SSH_TUNID_MAX		(SSH_TUNID_ANY - 2)
1373f941889SDamien Miller 
1387acefbbcSDamien Miller /* Fake port to indicate that host field is really a path. */
1397acefbbcSDamien Miller #define PORT_STREAMLOCAL	-2
1407acefbbcSDamien Miller 
1413f941889SDamien Miller /* Functions to extract or store big-endian words of various sizes */
1423f941889SDamien Miller u_int64_t	get_u64(const void *)
143686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 8)));
1443f941889SDamien Miller u_int32_t	get_u32(const void *)
145686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 4)));
1463f941889SDamien Miller u_int16_t	get_u16(const void *)
147686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 2)));
1483f941889SDamien Miller void		put_u64(void *, u_int64_t)
149686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 8)));
1503f941889SDamien Miller void		put_u32(void *, u_int32_t)
151686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 4)));
1523f941889SDamien Miller void		put_u16(void *, u_int16_t)
153686c7d9eSDamien Miller     __attribute__((__bounded__( __minbytes__, 1, 2)));
1543f941889SDamien Miller 
15588856691SDamien Miller /* Little-endian store/load, used by umac.c */
15688856691SDamien Miller u_int32_t	get_u32_le(const void *)
157686c7d9eSDamien Miller     __attribute__((__bounded__(__minbytes__, 1, 4)));
15888856691SDamien Miller void		put_u32_le(void *, u_int32_t)
159686c7d9eSDamien Miller     __attribute__((__bounded__(__minbytes__, 1, 4)));
16088856691SDamien Miller 
16165e42f87SDamien Miller struct bwlimit {
16265e42f87SDamien Miller 	size_t buflen;
163434b587aSdtucker@openbsd.org 	u_int64_t rate;		/* desired rate in kbit/s */
164434b587aSdtucker@openbsd.org 	u_int64_t thresh;	/* threshold after which we'll check timers */
165434b587aSdtucker@openbsd.org 	u_int64_t lamt;		/* amount written in last timer interval */
16665e42f87SDamien Miller 	struct timeval bwstart, bwend;
16765e42f87SDamien Miller };
16865e42f87SDamien Miller 
16965e42f87SDamien Miller void bandwidth_limit_init(struct bwlimit *, u_int64_t, size_t);
17065e42f87SDamien Miller void bandwidth_limit(struct bwlimit *, size_t);
17165e42f87SDamien Miller 
1720dac6fb6SDamien Miller int parse_ipqos(const char *);
17391475865SDamien Miller const char *iptos2str(int);
1742cd62934SDamien Miller void mktemp_proto(char *, size_t);
1753f941889SDamien Miller 
176de4ae07fSdjm@openbsd.org void	 child_set_env(char ***envp, u_int *envsizep, const char *name,
177de4ae07fSdjm@openbsd.org 	    const char *value);
179de4ae07fSdjm@openbsd.org int	 argv_split(const char *, int *, char ***);
180de4ae07fSdjm@openbsd.org char	*argv_assemble(int, char **argv);
181b074c3c3Sdjm@openbsd.org int	 exited_cleanly(pid_t, const char *, const char *, int);
183de4ae07fSdjm@openbsd.org struct stat;
184de4ae07fSdjm@openbsd.org int	 safe_path(const char *, struct stat *, const char *, uid_t,
185de4ae07fSdjm@openbsd.org 	    char *, size_t);
186de4ae07fSdjm@openbsd.org int	 safe_path_fd(int, const char *, struct passwd *,
187de4ae07fSdjm@openbsd.org 	    char *err, size_t errlen);
1895485f8d5Sdjm@openbsd.org /* authorized_key-style options parsing helpers */
1905485f8d5Sdjm@openbsd.org int	opt_flag(const char *opt, int allow_negate, const char **optsp);
1915485f8d5Sdjm@openbsd.org char	*opt_dequote(const char **sp, const char **errstrp);
1925485f8d5Sdjm@openbsd.org int	opt_match(const char **opts, const char *term);
194b696858aSmarkus@openbsd.org /* readconf/servconf option lists */
195b696858aSmarkus@openbsd.org void	opt_array_append(const char *file, const int line,
196b696858aSmarkus@openbsd.org 	    const char *directive, char ***array, u_int *lp, const char *s);
197b696858aSmarkus@openbsd.org void	opt_array_append2(const char *file, const int line,
198b696858aSmarkus@openbsd.org 	    const char *directive, char ***array, int **iarray, u_int *lp,
199b696858aSmarkus@openbsd.org 	    const char *s, int i);
201bdf00ca0SDamien Miller /* readpass.c */
202bdf00ca0SDamien Miller 
203bdf00ca0SDamien Miller #define RP_ECHO			0x0001
204bdf00ca0SDamien Miller #define RP_ALLOW_STDIN		0x0002
205bdf00ca0SDamien Miller #define RP_ALLOW_EOF		0x0004
206bdf00ca0SDamien Miller #define RP_USE_ASKPASS		0x0008
207bdf00ca0SDamien Miller 
2085d1c1590Sdjm@openbsd.org struct notifier_ctx;
210bdf00ca0SDamien Miller char	*read_passphrase(const char *, int);
211bdf00ca0SDamien Miller int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
2125d1c1590Sdjm@openbsd.org struct notifier_ctx *notify_start(int, const char *, ...)
2135d1c1590Sdjm@openbsd.org 	__attribute__((format(printf, 2, 3)));
214d5a0cd4fSdjm@openbsd.org void	notify_complete(struct notifier_ctx *, const char *, ...)
215d5a0cd4fSdjm@openbsd.org 	__attribute__((format(printf, 2, 3)));
216bdf00ca0SDamien Miller 
2179136ec13Sderaadt@openbsd.org #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
2189136ec13Sderaadt@openbsd.org #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
2199136ec13Sderaadt@openbsd.org #define ROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
2213bf2a6acSdtucker@openbsd.org typedef void (*sshsig_t)(int);
2223bf2a6acSdtucker@openbsd.org sshsig_t ssh_signal(int, sshsig_t);
224bdf00ca0SDamien Miller #endif /* _MISC_H */