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