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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 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 GNU |
| 16 | * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 24 | #include <sys/time.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <netdb.h> |
| 29 | #include <stdio.h> |
| 30 | #include <getopt.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <time.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <unistd.h> |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Glenn L McGrath | a55d72b | 2003-10-09 11:38:45 +0000 | [diff] [blame] | 36 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 38 | |
| 39 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 40 | static const int RFC_868_BIAS = 2208988800UL; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | a55d72b | 2003-10-09 11:38:45 +0000 | [diff] [blame] | 42 | static void socket_timeout(int sig) |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 43 | { |
Eric Andersen | 7f2935b | 2003-09-12 08:32:24 +0000 | [diff] [blame] | 44 | bb_error_msg_and_die("timeout connecting to time server"); |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 47 | static time_t askremotedate(const char *host) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 48 | { |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 49 | unsigned long int nett, localt; |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 50 | struct sockaddr_in s_in; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 51 | int fd; |
| 52 | |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 53 | bb_lookup_host(&s_in, host, "time"); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 54 | |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 55 | /* Add a timeout for dead or non accessable servers */ |
| 56 | alarm(10); |
| 57 | signal(SIGALRM, socket_timeout); |
| 58 | |
Eric Andersen | 04d055f | 2003-11-03 21:20:18 +0000 | [diff] [blame] | 59 | fd = xconnect(&s_in); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 725db19 | 2003-07-22 08:26:05 +0000 | [diff] [blame] | 61 | 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] | 62 | bb_error_msg_and_die("%s did not send the complete time", host); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 64 | close(fd); |
| 65 | |
| 66 | /* convert from network byte order to local byte order. |
| 67 | * RFC 868 time is the number of seconds |
| 68 | * since 00:00 (midnight) 1 January 1900 GMT |
| 69 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
| 70 | * Subtract the RFC 868 time to get Linux epoch |
| 71 | */ |
| 72 | localt= ntohl(nett) - RFC_868_BIAS; |
| 73 | |
| 74 | return(localt); |
| 75 | } |
| 76 | |
| 77 | int rdate_main(int argc, char **argv) |
| 78 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 79 | time_t remote_time; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 80 | int opt; |
Matt Kraai | 13cb842 | 2001-07-30 14:43:20 +0000 | [diff] [blame] | 81 | int setdate = 1; |
| 82 | int printdate = 1; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 83 | |
| 84 | /* Interpret command line args */ |
Matt Kraai | 13cb842 | 2001-07-30 14:43:20 +0000 | [diff] [blame] | 85 | while ((opt = getopt(argc, argv, "sp")) > 0) { |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 86 | switch (opt) { |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 87 | case 's': |
Matt Kraai | 13cb842 | 2001-07-30 14:43:20 +0000 | [diff] [blame] | 88 | printdate = 0; |
| 89 | setdate = 1; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 90 | break; |
| 91 | case 'p': |
Matt Kraai | 13cb842 | 2001-07-30 14:43:20 +0000 | [diff] [blame] | 92 | printdate = 1; |
| 93 | setdate = 0; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 94 | break; |
Matt Kraai | 13cb842 | 2001-07-30 14:43:20 +0000 | [diff] [blame] | 95 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | bb_show_usage(); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 100 | if (optind == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | bb_show_usage(); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 102 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 103 | remote_time = askremotedate(argv[optind]); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 104 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 105 | if (setdate) { |
Eric Andersen | 89f10bc | 2003-12-19 11:29:29 +0000 | [diff] [blame^] | 106 | time_t current_time; |
| 107 | |
| 108 | time(¤t_time); |
| 109 | if (current_time == remote_time) |
| 110 | bb_error_msg("Current time matches remote time."); |
| 111 | else |
| 112 | if (stime(&remote_time) < 0) |
| 113 | bb_perror_msg_and_die("Could not set time of day"); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 114 | } |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 115 | |
| 116 | if (printdate) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 117 | printf("%s", ctime(&remote_time)); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 118 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 119 | return EXIT_SUCCESS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 120 | } |