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