Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 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 | * |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 8 | * Licensed under GPL v2 or later, see file License for details. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 11 | #include <sys/time.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <netinet/in.h> |
| 15 | #include <netdb.h> |
| 16 | #include <stdio.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <time.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 21 | #include <signal.h> |
Glenn L McGrath | a55d72b | 2003-10-09 11:38:45 +0000 | [diff] [blame] | 22 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 23 | #include "busybox.h" |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 24 | |
| 25 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 26 | static const int RFC_868_BIAS = 2208988800UL; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 27 | |
Glenn L McGrath | a55d72b | 2003-10-09 11:38:45 +0000 | [diff] [blame] | 28 | static void socket_timeout(int sig) |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 29 | { |
Eric Andersen | 7f2935b | 2003-09-12 08:32:24 +0000 | [diff] [blame] | 30 | bb_error_msg_and_die("timeout connecting to time server"); |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 33 | static time_t askremotedate(const char *host) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 34 | { |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 35 | unsigned long nett; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 36 | struct sockaddr_in s_in; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 37 | int fd; |
| 38 | |
Glenn L McGrath | ffccf6e | 2003-12-20 01:47:18 +0000 | [diff] [blame] | 39 | bb_lookup_host(&s_in, host); |
Eric Andersen | 15eb39c | 2004-01-18 18:18:33 +0000 | [diff] [blame] | 40 | s_in.sin_port = bb_lookup_port("time", "tcp", 37); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 41 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 42 | /* Add a timeout for dead or inaccessible servers */ |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 43 | alarm(10); |
| 44 | signal(SIGALRM, socket_timeout); |
| 45 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 46 | fd = xconnect(&s_in); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 725db19 | 2003-07-22 08:26:05 +0000 | [diff] [blame] | 48 | if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_error_msg_and_die("%s did not send the complete time", host); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 50 | close(fd); |
| 51 | |
| 52 | /* convert from network byte order to local byte order. |
| 53 | * RFC 868 time is the number of seconds |
| 54 | * since 00:00 (midnight) 1 January 1900 GMT |
| 55 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
| 56 | * Subtract the RFC 868 time to get Linux epoch |
| 57 | */ |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 58 | |
| 59 | return(ntohl(nett) - RFC_868_BIAS); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int rdate_main(int argc, char **argv) |
| 63 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 64 | time_t remote_time; |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned long flags = bb_getopt_ulflags(argc, argv, "sp"); |
| 67 | |
| 68 | if (!flags || argc == optind) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | bb_show_usage(); |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 71 | remote_time = askremotedate(argv[optind]); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 72 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 73 | if (flags & 1) { |
Eric Andersen | 89f10bc | 2003-12-19 11:29:29 +0000 | [diff] [blame] | 74 | time_t current_time; |
| 75 | |
| 76 | time(¤t_time); |
| 77 | if (current_time == remote_time) |
| 78 | bb_error_msg("Current time matches remote time."); |
| 79 | else |
| 80 | if (stime(&remote_time) < 0) |
| 81 | bb_perror_msg_and_die("Could not set time of day"); |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 82 | |
| 83 | /* No need to check for the -p flag as it's the only option left */ |
| 84 | |
| 85 | } else printf("%s", ctime(&remote_time)); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 86 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 87 | return EXIT_SUCCESS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 88 | } |