1 /* $NetBSD: rdate.c,v 1.20 2017/05/04 16:26:09 sevan 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * rdate.c: Set the date from the specified host 30 * 31 * Uses the rfc868 time protocol at socket 37. 32 * Time is returned as the number of seconds since 33 * midnight January 1st 1900. 34 */ 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __RCSID("$NetBSD: rdate.c,v 1.20 2017/05/04 16:26:09 sevan Exp $"); 38 #endif /* lint */ 39 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <sys/time.h> 43 44 #include <netinet/in.h> 45 46 #include <err.h> 47 #include <netdb.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <util.h> 53 54 /* seconds from midnight Jan 1900 - 1970 */ 55 #define DIFFERENCE 2208988800ULL 56 57 static void usage(void); 58 59 static void 60 usage(void) 61 { 62 (void) fprintf(stderr, "usage: %s [-psa] host\n", getprogname()); 63 (void) fprintf(stderr, " -p: just print, don't set\n"); 64 (void) fprintf(stderr, " -s: just set, don't print\n"); 65 (void) fprintf(stderr, " -a: use adjtime instead of instant change\n"); 66 } 67 68 int 69 main(int argc, char *argv[]) 70 { 71 int pr = 0, silent = 0, s; 72 int slidetime = 0; 73 int adjustment; 74 uint32_t data; 75 time_t tim; 76 char *hname; 77 const char *emsg = NULL; 78 struct addrinfo hints, *res, *res0; 79 int c; 80 int error; 81 82 adjustment = 0; 83 while ((c = getopt(argc, argv, "psa")) != -1) 84 switch (c) { 85 case 'p': 86 pr++; 87 break; 88 89 case 's': 90 silent++; 91 break; 92 93 case 'a': 94 slidetime++; 95 break; 96 97 default: 98 usage(); 99 return 1; 100 } 101 102 if (argc - 1 != optind) { 103 usage(); 104 return 1; 105 } 106 hname = argv[optind]; 107 108 memset(&hints, 0, sizeof (hints)); 109 hints.ai_family = PF_UNSPEC; 110 hints.ai_socktype = SOCK_STREAM; 111 hints.ai_flags = AI_CANONNAME; 112 error = getaddrinfo(hname, "time", &hints, &res0); 113 if (error) 114 errx(1, "%s: %s", gai_strerror(error), hname); 115 116 for (res = res0, s = -1; res != NULL; res = res->ai_next) { 117 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 118 if (s < 0) { 119 emsg = "socket"; 120 continue; 121 } 122 123 if (connect(s, res->ai_addr, res->ai_addrlen)) { 124 close(s); 125 s = -1; 126 emsg = "connect"; 127 continue; 128 } 129 130 break; 131 } 132 if (s < 0) 133 err(1, "%s", emsg); 134 135 if (read(s, &data, sizeof(uint32_t)) != sizeof(uint32_t)) 136 err(1, "Could not read data"); 137 138 (void) close(s); 139 tim = ntohl(data) - DIFFERENCE; 140 141 if (!pr) { 142 struct timeval tv; 143 if (!slidetime) { 144 logwtmp("|", "date", ""); 145 tv.tv_sec = tim; 146 tv.tv_usec = 0; 147 if (settimeofday(&tv, NULL) == -1) 148 err(1, "Could not set time of day"); 149 logwtmp("{", "date", ""); 150 } else { 151 struct timeval tv_current; 152 if (gettimeofday(&tv_current, NULL) == -1) 153 err(1, "Could not get local time of day"); 154 adjustment = tv.tv_sec = tim - tv_current.tv_sec; 155 tv.tv_usec = 0; 156 if (adjtime(&tv, NULL) == -1) 157 err(1, "Could not adjust time of day"); 158 } 159 } 160 161 if (!silent) { 162 (void) fputs(ctime(&tim), stdout); 163 if (slidetime) 164 (void) fprintf(stdout, 165 "%s: adjust local clock by %d seconds\n", 166 getprogname(), adjustment); 167 } 168 return 0; 169 } 170