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