1 /* $OpenBSD: rdate.c,v 1.11 1997/07/25 20:12:18 mickey Exp $ */ 2 /* $NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $ */ 3 4 /* 5 * Copyright (c) 1994 Christos Zoulas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Christos Zoulas. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * rdate.c: Set the date from the specified host 36 * 37 * Uses the rfc868 time protocol at socket 37. 38 * Time is returned as the number of seconds since 39 * midnight January 1st 1900. 40 */ 41 #ifndef lint 42 #if 0 43 from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $"; 44 #else 45 static char rcsid[] = "$OpenBSD: rdate.c,v 1.11 1997/07/25 20:12:18 mickey Exp $"; 46 #endif 47 #endif /* lint */ 48 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <stdio.h> 52 #include <ctype.h> 53 #include <err.h> 54 #include <string.h> 55 #include <sys/time.h> 56 #include <sys/socket.h> 57 #include <netdb.h> 58 #include <netinet/in.h> 59 #include <unistd.h> 60 #include <util.h> 61 #include <time.h> 62 63 /* seconds from midnight Jan 1900 - 1970 */ 64 #ifdef __STDC__ 65 #define DIFFERENCE 2208988800UL 66 #else 67 #define DIFFERENCE 2208988800 68 #endif 69 70 extern char *__progname; 71 72 static void 73 usage() 74 { 75 (void) fprintf(stderr, "Usage: %s [-psa] host\n", __progname); 76 (void) fprintf(stderr, " -p: just print, don't set\n"); 77 (void) fprintf(stderr, " -s: just set, don't print\n"); 78 (void) fprintf(stderr, " -a: use adjtime instead of instant change\n"); 79 } 80 81 int 82 main(argc, argv) 83 int argc; 84 char *argv[]; 85 { 86 int pr = 0, silent = 0, s; 87 int slidetime = 0; 88 int adjustment; 89 time_t tim; 90 char *hname; 91 struct hostent *hp; 92 struct protoent *pp, ppp; 93 struct servent *sp, ssp; 94 struct sockaddr_in sa; 95 extern int optind; 96 int c; 97 98 while ((c = getopt(argc, argv, "psa")) != -1) 99 switch (c) { 100 case 'p': 101 pr++; 102 break; 103 104 case 's': 105 silent++; 106 break; 107 108 case 'a': 109 slidetime++; 110 break; 111 112 default: 113 usage(); 114 return 1; 115 } 116 117 if (argc - 1 != optind) { 118 usage(); 119 return 1; 120 } 121 hname = argv[optind]; 122 123 if ((hp = gethostbyname(hname)) == NULL) { 124 warnx("%s: %s", hname, hstrerror(h_errno)); 125 return 1; 126 } 127 128 if ((sp = getservbyname("time", "tcp")) == NULL) { 129 sp = &ssp; 130 sp->s_port = 37; 131 sp->s_proto = "tcp"; 132 } 133 if ((pp = getprotobyname(sp->s_proto)) == NULL) { 134 pp = &ppp; 135 pp->p_proto = 6; 136 } 137 if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1) 138 err(1, "Could not create socket"); 139 140 bzero(&sa, sizeof sa); 141 sa.sin_family = AF_INET; 142 sa.sin_port = sp->s_port; 143 144 (void) memcpy(&(sa.sin_addr.s_addr), hp->h_addr, hp->h_length); 145 146 if (connect(s, (struct sockaddr *) & sa, sizeof(sa)) == -1) 147 err(1, "Could not connect socket"); 148 149 if (read(s, &tim, sizeof(time_t)) != sizeof(time_t)) 150 err(1, "Could not read data"); 151 152 (void) close(s); 153 tim = ntohl(tim) - DIFFERENCE; 154 155 if (!pr) { 156 struct timeval tv; 157 if (!slidetime) { 158 logwtmp("|", "date", ""); 159 tv.tv_sec = tim; 160 tv.tv_usec = 0; 161 if (settimeofday(&tv, NULL) == -1) 162 err(1, "Could not set time of day"); 163 logwtmp("{", "date", ""); 164 } else { 165 struct timeval tv_current; 166 if (gettimeofday(&tv_current, NULL) == -1) 167 err(1, "Could not get local time of day"); 168 adjustment = tv.tv_sec = tim - tv_current.tv_sec; 169 tv.tv_usec = 0; 170 if (adjtime(&tv, NULL) == -1) 171 err(1, "Could not adjust time of day"); 172 } 173 } 174 175 if (!silent) { 176 struct tm *ltm; 177 char buf[80]; 178 179 ltm = localtime(&tim); 180 (void) strftime(buf, 80, "%a %b %e %H:%M:%S %Z %Y\n", ltm); 181 (void) fputs(buf, stdout); 182 183 if (slidetime) 184 (void) fprintf(stdout, 185 "%s: adjust local clock by %d seconds\n", 186 __progname, adjustment); 187 } 188 return 0; 189 } 190