2009-10-21 05:07:44 +04:00
|
|
|
/* $NetBSD: rdate.c,v 1.19 2009/10/21 01:07:47 snj Exp $ */
|
1996-02-22 09:59:15 +03:00
|
|
|
|
1994-06-03 02:54:59 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Christos Zoulas
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rdate.c: Set the date from the specified host
|
|
|
|
*
|
|
|
|
* Uses the rfc868 time protocol at socket 37.
|
|
|
|
* Time is returned as the number of seconds since
|
|
|
|
* midnight January 1st 1900.
|
|
|
|
*/
|
1997-10-18 07:50:58 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-06-03 02:54:59 +04:00
|
|
|
#ifndef lint
|
2009-10-21 05:07:44 +04:00
|
|
|
__RCSID("$NetBSD: rdate.c,v 1.19 2009/10/21 01:07:47 snj Exp $");
|
2002-05-16 23:57:22 +04:00
|
|
|
#endif /* lint */
|
1994-06-03 02:54:59 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
1997-10-18 07:50:58 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
1994-06-03 02:54:59 +04:00
|
|
|
#include <netinet/in.h>
|
1997-10-18 07:50:58 +04:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
2001-02-20 02:22:40 +03:00
|
|
|
#include <stdlib.h>
|
1997-10-18 07:50:58 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-03-27 00:12:58 +03:00
|
|
|
#include <util.h>
|
1994-06-03 02:54:59 +04:00
|
|
|
|
|
|
|
/* seconds from midnight Jan 1900 - 1970 */
|
2009-03-10 03:26:20 +03:00
|
|
|
#define DIFFERENCE 2208988800ULL
|
1994-06-03 02:54:59 +04:00
|
|
|
|
2002-07-14 05:01:10 +04:00
|
|
|
int main(int, char **);
|
|
|
|
static void usage(void);
|
1997-10-18 07:50:58 +04:00
|
|
|
|
1996-02-10 21:44:54 +03:00
|
|
|
static void
|
2002-07-14 05:01:10 +04:00
|
|
|
usage(void)
|
1996-02-10 21:44:54 +03:00
|
|
|
{
|
2004-01-06 02:23:32 +03:00
|
|
|
(void) fprintf(stderr, "usage: %s [-psa] host\n", getprogname());
|
1996-02-10 21:44:54 +03:00
|
|
|
(void) fprintf(stderr, " -p: just print, don't set\n");
|
|
|
|
(void) fprintf(stderr, " -s: just set, don't print\n");
|
|
|
|
(void) fprintf(stderr, " -a: use adjtime instead of instant change\n");
|
|
|
|
}
|
|
|
|
|
1994-06-03 02:54:59 +04:00
|
|
|
int
|
2002-07-14 05:01:10 +04:00
|
|
|
main(int argc, char *argv[])
|
1994-06-03 02:54:59 +04:00
|
|
|
{
|
|
|
|
int pr = 0, silent = 0, s;
|
1996-02-10 21:44:54 +03:00
|
|
|
int slidetime = 0;
|
|
|
|
int adjustment;
|
2009-03-10 03:26:20 +03:00
|
|
|
uint32_t data;
|
1994-06-03 02:54:59 +04:00
|
|
|
time_t tim;
|
2005-06-02 13:20:37 +04:00
|
|
|
char *hname;
|
|
|
|
const char *emsg = NULL;
|
2000-02-06 01:14:20 +03:00
|
|
|
struct addrinfo hints, *res, *res0;
|
1994-06-03 02:54:59 +04:00
|
|
|
int c;
|
2000-02-06 01:14:20 +03:00
|
|
|
int error;
|
1994-06-03 02:54:59 +04:00
|
|
|
|
1997-10-18 07:50:58 +04:00
|
|
|
adjustment = 0;
|
1996-02-10 21:44:54 +03:00
|
|
|
while ((c = getopt(argc, argv, "psa")) != -1)
|
1994-06-03 02:54:59 +04:00
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
pr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
silent++;
|
|
|
|
break;
|
|
|
|
|
1996-02-10 21:44:54 +03:00
|
|
|
case 'a':
|
|
|
|
slidetime++;
|
|
|
|
break;
|
|
|
|
|
1994-06-03 02:54:59 +04:00
|
|
|
default:
|
1996-02-10 21:44:54 +03:00
|
|
|
usage();
|
|
|
|
return 1;
|
1994-06-03 02:54:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - 1 != optind) {
|
1996-02-10 21:44:54 +03:00
|
|
|
usage();
|
|
|
|
return 1;
|
1994-06-03 02:54:59 +04:00
|
|
|
}
|
|
|
|
hname = argv[optind];
|
|
|
|
|
2000-02-06 01:14:20 +03:00
|
|
|
memset(&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
error = getaddrinfo(hname, "time", &hints, &res0);
|
|
|
|
if (error)
|
|
|
|
errx(1, "%s: %s", gai_strerror(error), hname);
|
|
|
|
|
|
|
|
for (res = res0, s = -1; res != NULL; res = res->ai_next) {
|
|
|
|
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (s < 0) {
|
|
|
|
emsg = "socket";
|
|
|
|
continue;
|
|
|
|
}
|
1994-06-03 02:54:59 +04:00
|
|
|
|
2000-02-06 01:14:20 +03:00
|
|
|
if (connect(s, res->ai_addr, res->ai_addrlen)) {
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
emsg = "connect";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
1994-06-03 02:54:59 +04:00
|
|
|
}
|
2000-02-06 01:14:20 +03:00
|
|
|
if (s < 0)
|
2000-07-07 19:10:32 +04:00
|
|
|
err(1, "%s", emsg);
|
1994-06-03 02:54:59 +04:00
|
|
|
|
2009-03-10 03:26:20 +03:00
|
|
|
if (read(s, &data, sizeof(uint32_t)) != sizeof(uint32_t))
|
1994-06-03 02:54:59 +04:00
|
|
|
err(1, "Could not read data");
|
|
|
|
|
|
|
|
(void) close(s);
|
2009-03-10 03:26:20 +03:00
|
|
|
tim = ntohl(data) - DIFFERENCE;
|
1994-06-03 02:54:59 +04:00
|
|
|
|
|
|
|
if (!pr) {
|
1996-02-10 21:44:54 +03:00
|
|
|
struct timeval tv;
|
|
|
|
if (!slidetime) {
|
1997-03-27 00:12:58 +03:00
|
|
|
logwtmp("|", "date", "");
|
1996-02-10 21:44:54 +03:00
|
|
|
tv.tv_sec = tim;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (settimeofday(&tv, NULL) == -1)
|
|
|
|
err(1, "Could not set time of day");
|
1997-03-27 00:12:58 +03:00
|
|
|
logwtmp("{", "date", "");
|
1996-02-10 21:44:54 +03:00
|
|
|
} else {
|
|
|
|
struct timeval tv_current;
|
|
|
|
if (gettimeofday(&tv_current, NULL) == -1)
|
|
|
|
err(1, "Could not get local time of day");
|
|
|
|
adjustment = tv.tv_sec = tim - tv_current.tv_sec;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (adjtime(&tv, NULL) == -1)
|
|
|
|
err(1, "Could not adjust time of day");
|
|
|
|
}
|
1994-06-03 02:54:59 +04:00
|
|
|
}
|
1996-02-10 21:44:54 +03:00
|
|
|
|
|
|
|
if (!silent) {
|
1994-06-03 02:54:59 +04:00
|
|
|
(void) fputs(ctime(&tim), stdout);
|
1996-02-10 21:44:54 +03:00
|
|
|
if (slidetime)
|
|
|
|
(void) fprintf(stdout,
|
|
|
|
"%s: adjust local clock by %d seconds\n",
|
2001-02-20 02:22:40 +03:00
|
|
|
getprogname(), adjustment);
|
1996-02-10 21:44:54 +03:00
|
|
|
}
|
1994-06-03 02:54:59 +04:00
|
|
|
return 0;
|
|
|
|
}
|