You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

267 lines
7.1 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "utility.hh"
  26. #include <cstring>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include "pdnsexception.hh"
  31. #include "logger.hh"
  32. #include "misc.hh"
  33. #include <pwd.h>
  34. #include <grp.h>
  35. #include <sys/types.h>
  36. #include <sys/select.h>
  37. #ifdef NEED_INET_NTOP_PROTO
  38. extern "C" {
  39. const char *inet_ntop(int af, const void *src, char *dst, size_t cnt);
  40. }
  41. #endif
  42. #include "namespaces.hh"
  43. // Connects to socket with timeout
  44. int Utility::timed_connect( Utility::sock_t sock,
  45. const sockaddr *addr,
  46. Utility::socklen_t sockaddr_size,
  47. int timeout_sec,
  48. int timeout_usec )
  49. {
  50. fd_set set;
  51. struct timeval timeout;
  52. int ret;
  53. timeout.tv_sec = timeout_sec;
  54. timeout.tv_usec = timeout_usec;
  55. FD_ZERO(&set);
  56. FD_SET(sock, &set);
  57. setNonBlocking(sock);
  58. if ((ret = connect (sock, addr, sockaddr_size)) < 0) {
  59. if (errno != EINPROGRESS)
  60. return ret;
  61. }
  62. ret = select(sock + 1, NULL, &set, NULL, &timeout);
  63. setBlocking(sock);
  64. return ret;
  65. }
  66. void Utility::setBindAny(int af, sock_t sock)
  67. {
  68. const int one = 1;
  69. (void) one; // avoids 'unused var' warning on systems that have none of the defines checked below
  70. #ifdef IP_FREEBIND
  71. if (setsockopt(sock, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one)) < 0)
  72. g_log<<Logger::Warning<<"Warning: IP_FREEBIND setsockopt failed: "<<stringerror()<<endl;
  73. #endif
  74. #ifdef IP_BINDANY
  75. if (af == AF_INET)
  76. if (setsockopt(sock, IPPROTO_IP, IP_BINDANY, &one, sizeof(one)) < 0)
  77. g_log<<Logger::Warning<<"Warning: IP_BINDANY setsockopt failed: "<<stringerror()<<endl;
  78. #endif
  79. #ifdef IPV6_BINDANY
  80. if (af == AF_INET6)
  81. if (setsockopt(sock, IPPROTO_IPV6, IPV6_BINDANY, &one, sizeof(one)) < 0)
  82. g_log<<Logger::Warning<<"Warning: IPV6_BINDANY setsockopt failed: "<<stringerror()<<endl;
  83. #endif
  84. #ifdef SO_BINDANY
  85. if (setsockopt(sock, SOL_SOCKET, SO_BINDANY, &one, sizeof(one)) < 0)
  86. g_log<<Logger::Warning<<"Warning: SO_BINDANY setsockopt failed: "<<stringerror()<<endl;
  87. #endif
  88. }
  89. const char *Utility::inet_ntop(int af, const char *src, char *dst, size_t size)
  90. {
  91. return ::inet_ntop(af,src,dst,size);
  92. }
  93. unsigned int Utility::sleep(unsigned int sec)
  94. {
  95. return ::sleep(sec);
  96. }
  97. void Utility::usleep(unsigned long usec)
  98. {
  99. struct timespec ts;
  100. ts.tv_sec = usec / 1000000;
  101. ts.tv_nsec = (usec % 1000000) * 1000;
  102. // POSIX.1 recommends using nanosleep instead of usleep
  103. ::nanosleep(&ts, NULL);
  104. }
  105. // Drops the program's group privileges.
  106. void Utility::dropGroupPrivs( uid_t uid, gid_t gid )
  107. {
  108. if(gid && gid != getegid()) {
  109. if(setgid(gid)<0) {
  110. g_log<<Logger::Critical<<"Unable to set effective group id to "<<gid<<": "<<stringerror()<<endl;
  111. exit(1);
  112. }
  113. else
  114. g_log<<Logger::Info<<"Set effective group id to "<<gid<<endl;
  115. struct passwd *pw=getpwuid(uid);
  116. if(!pw) {
  117. g_log<<Logger::Warning<<"Unable to determine user name for uid "<<uid<<endl;
  118. if (setgroups(0, NULL)<0) {
  119. g_log<<Logger::Critical<<"Unable to drop supplementary gids: "<<stringerror()<<endl;
  120. exit(1);
  121. }
  122. } else {
  123. if (initgroups(pw->pw_name, gid)<0) {
  124. g_log<<Logger::Critical<<"Unable to set supplementary groups: "<<stringerror()<<endl;
  125. exit(1);
  126. }
  127. }
  128. }
  129. }
  130. // Drops the program's user privileges.
  131. void Utility::dropUserPrivs( uid_t uid )
  132. {
  133. if(uid && uid != geteuid()) {
  134. if(setuid(uid)<0) {
  135. g_log<<Logger::Critical<<"Unable to set effective user id to "<<uid<<": "<<stringerror()<<endl;
  136. exit(1);
  137. }
  138. else
  139. g_log<<Logger::Info<<"Set effective user id to "<<uid<<endl;
  140. }
  141. }
  142. // Returns the current process id.
  143. Utility::pid_t Utility::getpid( void )
  144. {
  145. return ::getpid();
  146. }
  147. // Returns the current time.
  148. int Utility::gettimeofday( struct timeval *tv, void *tz )
  149. {
  150. return ::gettimeofday(tv,0);
  151. }
  152. // Sets the random seed.
  153. void Utility::srandom(void)
  154. {
  155. struct timeval tv;
  156. gettimeofday(&tv, 0);
  157. ::srandom(tv.tv_sec ^ tv.tv_usec ^ getpid());
  158. }
  159. // Writes a vector.
  160. int Utility::writev(int socket, const iovec *vector, size_t count )
  161. {
  162. return ::writev(socket,vector,count);
  163. }
  164. /* this is cut and pasted from dietlibc, gratefully copied! */
  165. static int isleap(int year) {
  166. /* every fourth year is a leap year except for century years that are
  167. * not divisible by 400. */
  168. return (!(year%4) && ((year%100) || !(year%400)));
  169. }
  170. time_t Utility::timegm(struct tm *const t)
  171. {
  172. const static short spm[13] = /* days per month -- nonleap! */
  173. { 0,
  174. (31),
  175. (31+28),
  176. (31+28+31),
  177. (31+28+31+30),
  178. (31+28+31+30+31),
  179. (31+28+31+30+31+30),
  180. (31+28+31+30+31+30+31),
  181. (31+28+31+30+31+30+31+31),
  182. (31+28+31+30+31+30+31+31+30),
  183. (31+28+31+30+31+30+31+31+30+31),
  184. (31+28+31+30+31+30+31+31+30+31+30),
  185. (31+28+31+30+31+30+31+31+30+31+30+31),
  186. };
  187. time_t day;
  188. time_t i;
  189. time_t years = t->tm_year - 70;
  190. if (t->tm_sec>60) { t->tm_min += t->tm_sec/60; t->tm_sec%=60; }
  191. if (t->tm_min>60) { t->tm_hour += t->tm_min/60; t->tm_min%=60; }
  192. if (t->tm_hour>60) { t->tm_mday += t->tm_hour/60; t->tm_hour%=60; }
  193. if (t->tm_mon>11) { t->tm_year += t->tm_mon/12; t->tm_mon%=12; }
  194. while (t->tm_mday>spm[1+t->tm_mon]) {
  195. if (t->tm_mon==1 && isleap(t->tm_year+1900)) {
  196. if (t->tm_mon==31+29) break;
  197. --t->tm_mday;
  198. }
  199. t->tm_mday-=spm[t->tm_mon];
  200. ++t->tm_mon;
  201. if (t->tm_mon>11) { t->tm_mon=0; ++t->tm_year; }
  202. }
  203. if (t->tm_year < 70)
  204. return (time_t) -1;
  205. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  206. day = years * 365 + (years + 1) / 4;
  207. /* After 2100 we have to subtract 3 leap years for every 400 years
  208. This is not intuitive. Most mktime implementations do not support
  209. dates after 2059, anyway, so we might leave this out for its
  210. bloat. */
  211. if ((years -= 131) >= 0) {
  212. years /= 100;
  213. day -= (years >> 2) * 3 + 1;
  214. if ((years &= 3) == 3) years--;
  215. day -= years;
  216. }
  217. day += t->tm_yday = spm [t->tm_mon] + t->tm_mday-1 + ( isleap (t->tm_year+1900) & (t->tm_mon > 1) );
  218. /* day is now the number of days since 'Jan 1 1970' */
  219. i = 7;
  220. t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
  221. i = 24;
  222. day *= i;
  223. i = 60;
  224. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  225. }