3
0

rdate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * The Rdate command will ask a time server for the RFC 868 time
  4. * and optionally set the system time.
  5. *
  6. * by Sterling Huxley <sterling@europa.com>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config RDATE
  11. //config: bool "rdate"
  12. //config: default y
  13. //config: help
  14. //config: The rdate utility allows you to synchronize the date and time of your
  15. //config: system clock with the date and time of a remote networked system using
  16. //config: the RFC868 protocol, which is built into the inetd daemon on most
  17. //config: systems.
  18. //applet:IF_RDATE(APPLET(rdate, BB_DIR_USR_SBIN, BB_SUID_DROP))
  19. //kbuild:lib-$(CONFIG_RDATE) += rdate.o
  20. //usage:#define rdate_trivial_usage
  21. //usage: "[-s/-p] HOST"
  22. //usage:#define rdate_full_usage "\n\n"
  23. //usage: "Set and print time from HOST using RFC 868\n"
  24. //usage: "\n -s Only set system time"
  25. //usage: "\n -p Only print time"
  26. #include "libbb.h"
  27. enum { RFC_868_BIAS = 2208988800UL };
  28. static void socket_timeout(int sig UNUSED_PARAM)
  29. {
  30. bb_error_msg_and_die("timeout connecting to time server");
  31. }
  32. static time_t askremotedate(const char *host)
  33. {
  34. uint32_t nett;
  35. int fd;
  36. /* Add a timeout for dead or inaccessible servers */
  37. alarm(10);
  38. signal(SIGALRM, socket_timeout);
  39. fd = create_and_connect_stream_or_die(host, bb_lookup_port("time", "tcp", 37));
  40. if (safe_read(fd, &nett, 4) != 4) /* read time from server */
  41. bb_error_msg_and_die("%s: %s", host, "short read");
  42. if (ENABLE_FEATURE_CLEAN_UP)
  43. close(fd);
  44. /* Convert from network byte order to local byte order.
  45. * RFC 868 time is the number of seconds
  46. * since 00:00 (midnight) 1 January 1900 GMT
  47. * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
  48. * Subtract the RFC 868 time to get Linux epoch.
  49. */
  50. nett = ntohl(nett) - RFC_868_BIAS;
  51. if (sizeof(time_t) > 4) {
  52. /* Now we have 32-bit lsb of a wider time_t
  53. * Imagine that nett = 0x00000001,
  54. * current time cur = 0x123ffffffff.
  55. * Assuming our time is not some 40 years off,
  56. * remote time must be 0x12400000001.
  57. * Need to adjust out time by (int32_t)(nett - cur).
  58. */
  59. time_t cur = time(NULL);
  60. int32_t adjust = (int32_t)(nett - (uint32_t)cur);
  61. return cur + adjust;
  62. }
  63. /* This is not going to work, but what can we do */
  64. return (time_t)nett;
  65. }
  66. int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  67. int rdate_main(int argc UNUSED_PARAM, char **argv)
  68. {
  69. time_t remote_time;
  70. unsigned flags;
  71. opt_complementary = "-1";
  72. flags = getopt32(argv, "sp");
  73. remote_time = askremotedate(argv[optind]);
  74. /* Manpages of various Unixes are confusing. What happens is:
  75. * (no opts) set and print time
  76. * -s: set time ("do not print the time")
  77. * -p: print time ("do not set, just print the remote time")
  78. * -sp: print time (that's what we do, not sure this is right)
  79. */
  80. if (!(flags & 2)) { /* no -p (-s may be present) */
  81. if (time(NULL) == remote_time)
  82. bb_error_msg("current time matches remote time");
  83. else
  84. if (stime(&remote_time) < 0)
  85. bb_perror_msg_and_die("can't set time of day");
  86. }
  87. if (flags != 1) /* not lone -s */
  88. printf("%s", ctime(&remote_time));
  89. return EXIT_SUCCESS;
  90. }