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 | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 36 | |
| 37 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 38 | static const int RFC_868_BIAS = 2208988800UL; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 39 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 40 | static time_t askremotedate(const char *host) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 41 | { |
| 42 | struct hostent *h; |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 43 | struct sockaddr_in s_in; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 44 | struct servent *tserv; |
| 45 | unsigned long int nett, localt; |
| 46 | int fd; |
| 47 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 48 | if (!(h = gethostbyname(host))) /* get the IP addr */ |
| 49 | perror_msg_and_die("%s", host); |
| 50 | |
| 51 | if ((tserv = getservbyname("time", "tcp")) == NULL) /* find port # */ |
| 52 | perror_msg_and_die("%s", "time"); |
| 53 | |
| 54 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get net connection */ |
| 55 | perror_msg_and_die("%s", "socket"); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 56 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 57 | memcpy(&s_in.sin_addr, h->h_addr, sizeof(s_in.sin_addr)); |
| 58 | s_in.sin_port= tserv->s_port; |
| 59 | s_in.sin_family = AF_INET; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 61 | if (connect(fd, (struct sockaddr *)&s_in, sizeof(s_in)) < 0) /* connect to time server */ |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 62 | perror_msg_and_die("%s", host); |
| 63 | |
| 64 | if (read(fd, (void *)&nett, 4) != 4) /* read time from server */ |
| 65 | error_msg_and_die("%s did not send the complete time", host); |
| 66 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 67 | close(fd); |
| 68 | |
| 69 | /* convert from network byte order to local byte order. |
| 70 | * RFC 868 time is the number of seconds |
| 71 | * since 00:00 (midnight) 1 January 1900 GMT |
| 72 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
| 73 | * Subtract the RFC 868 time to get Linux epoch |
| 74 | */ |
| 75 | localt= ntohl(nett) - RFC_868_BIAS; |
| 76 | |
| 77 | return(localt); |
| 78 | } |
| 79 | |
| 80 | int rdate_main(int argc, char **argv) |
| 81 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 82 | time_t remote_time; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 83 | int opt; |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 84 | int setdate = 0; |
| 85 | int printdate= 0; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 86 | |
| 87 | /* Interpret command line args */ |
| 88 | /* do special-case option parsing */ |
| 89 | if (argv[1] && (strcmp(argv[1], "--help") == 0)) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 90 | show_usage(); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 91 | |
| 92 | /* do normal option parsing */ |
| 93 | while ((opt = getopt(argc, argv, "Hsp")) > 0) { |
| 94 | switch (opt) { |
| 95 | default: |
| 96 | case 'H': |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 97 | show_usage(); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 98 | break; |
| 99 | case 's': |
| 100 | setdate++; |
| 101 | break; |
| 102 | case 'p': |
| 103 | printdate++; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* the default action is to set the date */ |
| 109 | if (printdate==0 && setdate==0) setdate++; |
| 110 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 111 | if (optind == argc) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 112 | show_usage(); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 113 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 114 | remote_time = askremotedate(argv[optind]); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 115 | |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 116 | if (setdate) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 117 | if (stime(&remote_time) < 0) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 118 | perror_msg_and_die("Could not set time of day"); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 119 | } |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 120 | |
| 121 | if (printdate) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 122 | printf("%s", ctime(&remote_time)); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 123 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 124 | return EXIT_SUCCESS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 125 | } |