misc.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* $OpenBSD: misc.h,v 1.87 2020/05/29 11:17:56 dtucker Exp $ */
  2. /*
  3. * Author: Tatu Ylonen <ylo@cs.hut.fi>
  4. * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  5. * All rights reserved
  6. *
  7. * As far as I am concerned, the code I have written for this software
  8. * can be used freely for any purpose. Any derived versions of this
  9. * software must be clearly marked as such, and if the derived work is
  10. * incompatible with the protocol description in the RFC file, it must be
  11. * called by a name other than "ssh" or "Secure Shell".
  12. */
  13. #ifndef _MISC_H
  14. #define _MISC_H
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <stdio.h>
  19. /* Data structure for representing a forwarding request. */
  20. struct Forward {
  21. char *listen_host; /* Host (address) to listen on. */
  22. int listen_port; /* Port to forward. */
  23. char *listen_path; /* Path to bind domain socket. */
  24. char *connect_host; /* Host to connect. */
  25. int connect_port; /* Port to connect on connect_host. */
  26. char *connect_path; /* Path to connect domain socket. */
  27. int allocated_port; /* Dynamically allocated listen port */
  28. int handle; /* Handle for dynamic listen ports */
  29. };
  30. int forward_equals(const struct Forward *, const struct Forward *);
  31. int daemonized(void);
  32. /* Common server and client forwarding options. */
  33. struct ForwardOptions {
  34. int gateway_ports; /* Allow remote connects to forwarded ports. */
  35. mode_t streamlocal_bind_mask; /* umask for streamlocal binds */
  36. int streamlocal_bind_unlink; /* unlink socket before bind */
  37. };
  38. /* misc.c */
  39. char *chop(char *);
  40. void skip_space(char **);
  41. char *strdelim(char **);
  42. char *strdelimw(char **);
  43. int set_nonblock(int);
  44. int unset_nonblock(int);
  45. void set_nodelay(int);
  46. int set_reuseaddr(int);
  47. char *get_rdomain(int);
  48. int set_rdomain(int, const char *);
  49. int waitrfd(int, int *);
  50. int timeout_connect(int, const struct sockaddr *, socklen_t, int *);
  51. int a2port(const char *);
  52. int a2tun(const char *, int *);
  53. char *put_host_port(const char *, u_short);
  54. char *hpdelim2(char **, char *);
  55. char *hpdelim(char **);
  56. char *cleanhostname(char *);
  57. char *colon(char *);
  58. int parse_user_host_path(const char *, char **, char **, char **);
  59. int parse_user_host_port(const char *, char **, char **, int *);
  60. int parse_uri(const char *, const char *, char **, char **, int *, char **);
  61. int convtime(const char *);
  62. const char *fmt_timeframe(time_t t);
  63. char *tilde_expand_filename(const char *, uid_t);
  64. char *dollar_expand(int *, const char *string, ...);
  65. char *percent_expand(const char *, ...) __attribute__((__sentinel__));
  66. char *percent_dollar_expand(const char *, ...) __attribute__((__sentinel__));
  67. char *tohex(const void *, size_t);
  68. void xextendf(char **s, const char *sep, const char *fmt, ...)
  69. __attribute__((__format__ (printf, 3, 4))) __attribute__((__nonnull__ (3)));
  70. void sanitise_stdfd(void);
  71. void ms_subtract_diff(struct timeval *, int *);
  72. void ms_to_timeval(struct timeval *, int);
  73. void monotime_ts(struct timespec *);
  74. void monotime_tv(struct timeval *);
  75. time_t monotime(void);
  76. double monotime_double(void);
  77. void lowercase(char *s);
  78. int unix_listener(const char *, int, int);
  79. int valid_domain(char *, int, const char **);
  80. int valid_env_name(const char *);
  81. const char *atoi_err(const char *, int *);
  82. int parse_absolute_time(const char *, uint64_t *);
  83. void format_absolute_time(uint64_t, char *, size_t);
  84. int path_absolute(const char *);
  85. int stdfd_devnull(int, int, int);
  86. void sock_set_v6only(int);
  87. struct passwd *pwcopy(struct passwd *);
  88. const char *ssh_gai_strerror(int);
  89. typedef void privdrop_fn(struct passwd *);
  90. typedef void privrestore_fn(void);
  91. #define SSH_SUBPROCESS_STDOUT_DISCARD (1) /* Discard stdout */
  92. #define SSH_SUBPROCESS_STDOUT_CAPTURE (1<<1) /* Redirect stdout */
  93. #define SSH_SUBPROCESS_STDERR_DISCARD (1<<2) /* Discard stderr */
  94. #define SSH_SUBPROCESS_UNSAFE_PATH (1<<3) /* Don't check for safe cmd */
  95. #define SSH_SUBPROCESS_PRESERVE_ENV (1<<4) /* Keep parent environment */
  96. pid_t subprocess(const char *, const char *, int, char **, FILE **, u_int,
  97. struct passwd *, privdrop_fn *, privrestore_fn *);
  98. typedef struct arglist arglist;
  99. struct arglist {
  100. char **list;
  101. u_int num;
  102. u_int nalloc;
  103. };
  104. void addargs(arglist *, char *, ...)
  105. __attribute__((format(printf, 2, 3)));
  106. void replacearg(arglist *, u_int, char *, ...)
  107. __attribute__((format(printf, 3, 4)));
  108. void freeargs(arglist *);
  109. int tun_open(int, int, char **);
  110. /* Common definitions for ssh tunnel device forwarding */
  111. #define SSH_TUNMODE_NO 0x00
  112. #define SSH_TUNMODE_POINTOPOINT 0x01
  113. #define SSH_TUNMODE_ETHERNET 0x02
  114. #define SSH_TUNMODE_DEFAULT SSH_TUNMODE_POINTOPOINT
  115. #define SSH_TUNMODE_YES (SSH_TUNMODE_POINTOPOINT|SSH_TUNMODE_ETHERNET)
  116. #define SSH_TUNID_ANY 0x7fffffff
  117. #define SSH_TUNID_ERR (SSH_TUNID_ANY - 1)
  118. #define SSH_TUNID_MAX (SSH_TUNID_ANY - 2)
  119. /* Fake port to indicate that host field is really a path. */
  120. #define PORT_STREAMLOCAL -2
  121. /* Functions to extract or store big-endian words of various sizes */
  122. u_int64_t get_u64(const void *)
  123. __attribute__((__bounded__( __minbytes__, 1, 8)));
  124. u_int32_t get_u32(const void *)
  125. __attribute__((__bounded__( __minbytes__, 1, 4)));
  126. u_int16_t get_u16(const void *)
  127. __attribute__((__bounded__( __minbytes__, 1, 2)));
  128. void put_u64(void *, u_int64_t)
  129. __attribute__((__bounded__( __minbytes__, 1, 8)));
  130. void put_u32(void *, u_int32_t)
  131. __attribute__((__bounded__( __minbytes__, 1, 4)));
  132. void put_u16(void *, u_int16_t)
  133. __attribute__((__bounded__( __minbytes__, 1, 2)));
  134. /* Little-endian store/load, used by umac.c */
  135. u_int32_t get_u32_le(const void *)
  136. __attribute__((__bounded__(__minbytes__, 1, 4)));
  137. void put_u32_le(void *, u_int32_t)
  138. __attribute__((__bounded__(__minbytes__, 1, 4)));
  139. struct bwlimit {
  140. size_t buflen;
  141. u_int64_t rate; /* desired rate in kbit/s */
  142. u_int64_t thresh; /* threshold after which we'll check timers */
  143. u_int64_t lamt; /* amount written in last timer interval */
  144. struct timeval bwstart, bwend;
  145. };
  146. void bandwidth_limit_init(struct bwlimit *, u_int64_t, size_t);
  147. void bandwidth_limit(struct bwlimit *, size_t);
  148. int parse_ipqos(const char *);
  149. const char *iptos2str(int);
  150. void mktemp_proto(char *, size_t);
  151. void child_set_env(char ***envp, u_int *envsizep, const char *name,
  152. const char *value);
  153. int argv_split(const char *, int *, char ***);
  154. char *argv_assemble(int, char **argv);
  155. int exited_cleanly(pid_t, const char *, const char *, int);
  156. struct stat;
  157. int safe_path(const char *, struct stat *, const char *, uid_t,
  158. char *, size_t);
  159. int safe_path_fd(int, const char *, struct passwd *,
  160. char *err, size_t errlen);
  161. /* authorized_key-style options parsing helpers */
  162. int opt_flag(const char *opt, int allow_negate, const char **optsp);
  163. char *opt_dequote(const char **sp, const char **errstrp);
  164. int opt_match(const char **opts, const char *term);
  165. /* readconf/servconf option lists */
  166. void opt_array_append(const char *file, const int line,
  167. const char *directive, char ***array, u_int *lp, const char *s);
  168. void opt_array_append2(const char *file, const int line,
  169. const char *directive, char ***array, int **iarray, u_int *lp,
  170. const char *s, int i);
  171. /* readpass.c */
  172. #define RP_ECHO 0x0001
  173. #define RP_ALLOW_STDIN 0x0002
  174. #define RP_ALLOW_EOF 0x0004
  175. #define RP_USE_ASKPASS 0x0008
  176. struct notifier_ctx;
  177. char *read_passphrase(const char *, int);
  178. int ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
  179. struct notifier_ctx *notify_start(int, const char *, ...)
  180. __attribute__((format(printf, 2, 3)));
  181. void notify_complete(struct notifier_ctx *, const char *, ...)
  182. __attribute__((format(printf, 2, 3)));
  183. #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
  184. #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
  185. #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
  186. typedef void (*sshsig_t)(int);
  187. sshsig_t ssh_signal(int, sshsig_t);
  188. #endif /* _MISC_H */