1 /* $OpenBSD: rdate.c,v 1.24 2009/10/27 23:59:54 deraadt 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 42 #include <sys/param.h> 43 #include <sys/socket.h> 44 #include <sys/time.h> 45 46 #include <stdio.h> 47 #include <ctype.h> 48 #include <err.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <time.h> 52 53 /* there are systems without libutil; for portability */ 54 #ifndef NO_UTIL 55 #include <util.h> 56 #else 57 #define logwtmp(a,b,c) 58 #endif 59 60 void rfc868time_client (const char *, int, struct timeval *, struct timeval *, int); 61 void ntp_client (const char *, int, struct timeval *, struct timeval *, int); 62 63 extern char *__progname; 64 65 void 66 usage(void) 67 { 68 (void) fprintf(stderr, "usage: %s [-46acnpsv] host\n", __progname); 69 (void) fprintf(stderr, 70 " -4: use IPv4 only\n" 71 " -6: use IPv6 only\n" 72 " -a: use adjtime instead of instant change\n" 73 " -c: correct leap second count\n" 74 " -n: use SNTP instead of RFC868 time protocol\n" 75 " -p: just print, don't set\n" 76 " -s: just set, don't print\n" 77 " -v: verbose output\n"); 78 } 79 80 int 81 main(int argc, char **argv) 82 { 83 int pr = 0, silent = 0, ntp = 0, verbose = 0; 84 int slidetime = 0, corrleaps = 0; 85 char *hname; 86 extern int optind; 87 int c; 88 int family = PF_UNSPEC; 89 90 struct timeval new, adjust; 91 92 while ((c = getopt(argc, argv, "46psancv")) != -1) 93 switch (c) { 94 case '4': 95 family = PF_INET; 96 break; 97 98 case '6': 99 family = PF_INET6; 100 break; 101 102 case 'p': 103 pr++; 104 break; 105 106 case 's': 107 silent++; 108 break; 109 110 case 'a': 111 slidetime++; 112 break; 113 114 case 'n': 115 ntp++; 116 break; 117 118 case 'c': 119 corrleaps = 1; 120 break; 121 122 case 'v': 123 verbose++; 124 break; 125 126 default: 127 usage(); 128 return 1; 129 } 130 131 if (argc - 1 != optind) { 132 usage(); 133 return 1; 134 } 135 hname = argv[optind]; 136 137 if (ntp) 138 ntp_client(hname, family, &new, &adjust, corrleaps); 139 else 140 rfc868time_client(hname, family, &new, &adjust, corrleaps); 141 142 if (!pr) { 143 if (!slidetime) { 144 logwtmp("|", "date", ""); 145 if (settimeofday(&new, NULL) == -1) 146 err(1, "Could not set time of day"); 147 logwtmp("{", "date", ""); 148 } else { 149 if (adjtime(&adjust, NULL) == -1) 150 err(1, "Could not adjust time of day"); 151 } 152 } 153 154 if (!silent) { 155 struct tm *ltm; 156 char buf[80]; 157 time_t tim = new.tv_sec; 158 double adjsec; 159 160 ltm = localtime(&tim); 161 (void) strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y\n", ltm); 162 (void) fputs(buf, stdout); 163 164 adjsec = adjust.tv_sec + adjust.tv_usec / 1.0e6; 165 166 if (slidetime || verbose) { 167 if (ntp) 168 (void) fprintf(stdout, 169 "%s: adjust local clock by %.6f seconds\n", 170 __progname, adjsec); 171 else 172 (void) fprintf(stdout, 173 "%s: adjust local clock by %ld seconds\n", 174 __progname, adjust.tv_sec); 175 } 176 } 177 178 return 0; 179 } 180