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