compat.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compat.c
  7. * \brief Wrappers to make calls more portable. This code defines
  8. * functions such as tor_snprintf, get/set various data types,
  9. * renaming, setting socket options, switching user IDs. It is basically
  10. * where the non-portable items are conditionally included depending on
  11. * the platform.
  12. **/
  13. #define COMPAT_PRIVATE
  14. #include "common/compat.h"
  15. #ifdef _WIN32
  16. #include <winsock2.h>
  17. #include <windows.h>
  18. #include <sys/locking.h>
  19. #endif
  20. #ifdef HAVE_UNAME
  21. #include <sys/utsname.h>
  22. #endif
  23. #ifdef HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #ifdef HAVE_SYS_SYSCTL_H
  27. #include <sys/sysctl.h>
  28. #endif
  29. #ifdef HAVE_SYS_STAT_H
  30. #include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_UTIME_H
  33. #include <utime.h>
  34. #endif
  35. #ifdef HAVE_SYS_UTIME_H
  36. #include <sys/utime.h>
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #ifdef HAVE_SYS_FCNTL_H
  42. #include <sys/fcntl.h>
  43. #endif
  44. #ifdef HAVE_PWD_H
  45. #include <pwd.h>
  46. #endif
  47. #ifdef HAVE_GRP_H
  48. #include <grp.h>
  49. #endif
  50. #ifdef HAVE_FCNTL_H
  51. #include <fcntl.h>
  52. #endif
  53. #ifdef HAVE_ERRNO_H
  54. #include <errno.h>
  55. #endif
  56. #ifdef HAVE_ARPA_INET_H
  57. #include <arpa/inet.h>
  58. #endif
  59. #ifdef HAVE_SYS_STATVFS_H
  60. #include <sys/statvfs.h>
  61. #endif
  62. #ifdef HAVE_SYS_CAPABILITY_H
  63. #include <sys/capability.h>
  64. #endif
  65. #ifdef _WIN32
  66. #include <conio.h>
  67. #include <wchar.h>
  68. /* Some mingw headers lack these. :p */
  69. #if defined(HAVE_DECL__GETWCH) && !HAVE_DECL__GETWCH
  70. wint_t _getwch(void);
  71. #endif
  72. #ifndef WEOF
  73. #define WEOF (wchar_t)(0xFFFF)
  74. #endif
  75. #if defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY
  76. static inline void
  77. SecureZeroMemory(PVOID ptr, SIZE_T cnt)
  78. {
  79. volatile char *vcptr = (volatile char*)ptr;
  80. while (cnt--)
  81. *vcptr++ = 0;
  82. }
  83. #endif /* defined(HAVE_DECL_SECUREZEROMEMORY) && !HAVE_DECL_SECUREZEROMEMORY */
  84. #elif defined(HAVE_READPASSPHRASE_H)
  85. #include <readpassphrase.h>
  86. #else
  87. #include "tor_readpassphrase.h"
  88. #endif /* defined(_WIN32) || ... */
  89. /* Includes for the process attaching prevention */
  90. #if defined(HAVE_SYS_PRCTL_H) && defined(__linux__)
  91. /* Only use the linux prctl; the IRIX prctl is totally different */
  92. #include <sys/prctl.h>
  93. #elif defined(__APPLE__)
  94. #include <sys/ptrace.h>
  95. #endif /* defined(HAVE_SYS_PRCTL_H) && defined(__linux__) || ... */
  96. #ifdef HAVE_NETDB_H
  97. #include <netdb.h>
  98. #endif
  99. #ifdef HAVE_SYS_PARAM_H
  100. #include <sys/param.h> /* FreeBSD needs this to know what version it is */
  101. #endif
  102. #include <stdio.h>
  103. #include <stdlib.h>
  104. #ifdef HAVE_SIGNAL_H
  105. #include <signal.h>
  106. #endif
  107. #ifdef HAVE_MMAP
  108. #include <sys/mman.h>
  109. #endif
  110. #ifdef HAVE_SYS_SYSLIMITS_H
  111. #include <sys/syslimits.h>
  112. #endif
  113. #ifdef HAVE_SYS_FILE_H
  114. #include <sys/file.h>
  115. #endif
  116. #include "lib/log/torlog.h"
  117. #include "common/util.h"
  118. #include "lib/container/smartlist.h"
  119. #include "lib/wallclock/tm_cvt.h"
  120. #include "lib/net/address.h"
  121. #include "lib/sandbox/sandbox.h"
  122. /*
  123. * Process control
  124. */
  125. /** Emit the password prompt <b>prompt</b>, then read up to <b>buflen</b>
  126. * bytes of passphrase into <b>output</b>. Return the number of bytes in
  127. * the passphrase, excluding terminating NUL.
  128. */
  129. ssize_t
  130. tor_getpass(const char *prompt, char *output, size_t buflen)
  131. {
  132. tor_assert(buflen <= SSIZE_MAX);
  133. tor_assert(buflen >= 1);
  134. #if defined(HAVE_READPASSPHRASE)
  135. char *pwd = readpassphrase(prompt, output, buflen, RPP_ECHO_OFF);
  136. if (pwd == NULL)
  137. return -1;
  138. return strlen(pwd);
  139. #elif defined(_WIN32)
  140. int r = -1;
  141. while (*prompt) {
  142. _putch(*prompt++);
  143. }
  144. tor_assert(buflen <= INT_MAX);
  145. wchar_t *buf = tor_calloc(buflen, sizeof(wchar_t));
  146. wchar_t *ptr = buf, *lastch = buf + buflen - 1;
  147. while (ptr < lastch) {
  148. wint_t ch = _getwch();
  149. switch (ch) {
  150. case '\r':
  151. case '\n':
  152. case WEOF:
  153. goto done_reading;
  154. case 3:
  155. goto done; /* Can't actually read ctrl-c this way. */
  156. case '\b':
  157. if (ptr > buf)
  158. --ptr;
  159. continue;
  160. case 0:
  161. case 0xe0:
  162. ch = _getwch(); /* Ignore; this is a function or arrow key */
  163. break;
  164. default:
  165. *ptr++ = ch;
  166. break;
  167. }
  168. }
  169. done_reading:
  170. ;
  171. #ifndef WC_ERR_INVALID_CHARS
  172. #define WC_ERR_INVALID_CHARS 0x80
  173. #endif
  174. /* Now convert it to UTF-8 */
  175. r = WideCharToMultiByte(CP_UTF8,
  176. WC_NO_BEST_FIT_CHARS|WC_ERR_INVALID_CHARS,
  177. buf, (int)(ptr-buf),
  178. output, (int)(buflen-1),
  179. NULL, NULL);
  180. if (r <= 0) {
  181. r = -1;
  182. goto done;
  183. }
  184. tor_assert(r < (int)buflen);
  185. output[r] = 0;
  186. done:
  187. SecureZeroMemory(buf, sizeof(wchar_t)*buflen);
  188. tor_free(buf);
  189. return r;
  190. #else
  191. #error "No implementation for tor_getpass found!"
  192. #endif /* defined(HAVE_READPASSPHRASE) || ... */
  193. }