leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fetchnews_check_date.c
Go to the documentation of this file.
1/* (C) Copyright 2003 - 2010 Matthias Andree. See COPYING for license. */
2
3#include "leafnode.h"
4
5#include <string.h>
6
7#include "fetchnews.h"
8#include "system.h"
9#include "ln_log.h"
10
11/* send DATE command to upstream server and complain if the clocks are
12 * more than 15 minutes apart.
13 */
14void
15check_date(const struct server *current_server)
16{
17 int reply;
18 struct tm tm;
19 time_t t, to;
20 const int tolerate = 10;
21 const char *lastline;
22
23 strcpy(lineout, "DATE\r\n");
24 putaline();
25 reply = nntpreply(current_server);
26 if (reply != 111) {
27 /* upstream does not support the DATE command, so ignore */
28 if (debugmode) {
29 syslog(LOG_DEBUG, "check_date: %s: does not support DATE, "
30 "reply %d, expected 111", current_server->name, reply);
31 }
32 return;
33 }
34
35 lastline = lastreply();
36 if (lastline == NULL) {
38 "%s: warning: server disconnect or timeout in response to DATE command",
39 current_server->name);
40 return;
41 }
42
43 /* upstream supports the DATE command */
44 if (sscanf(lastline, "%*d %4d%2d%2d%2d%2d%2d",
45 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
46 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) < 6) {
47 /* too few fields */
48 ln_log(LNLOG_SINFO, LNLOG_CSERVER, "%s: check_date: too few fields in successful DATE reply "
49 "\"%s\"", current_server->name, lastline);
50 return;
51 }
52
53 /* we can match 6 fields, parse date */
54 tm.tm_year -= 1900;
55 tm.tm_mon -= 1;
56 tm.tm_isdst = -1; /* let libc figure time zone offset */
57 t = timegm(&tm);
58 if (t == (time_t) -1) {
59 /* error, ignore */
60 ln_log(LNLOG_SINFO, LNLOG_CSERVER, "%s: check_date: upstream sent unparsable reply "
61 "to DATE, mktime failed. \"%s\"", current_server->name, lastline);
62 return;
63 }
64
65 if (labs((long)(t - time(&to))) > tolerate * 60
66#if SIZEOF_TIME_T >= SIZEOF_LONG
67 || t - to > LONG_MAX || to - t > LONG_MAX
68#endif
69 ) {
70 syslog(LOG_WARNING, "check_date: %s: clocks of upstream and this computer are more than %d minutes apart. Check your system clock.", current_server->name, tolerate);
71 printf("check_date: %s: clocks of upstream and this computer are more than\n%d minutes apart. Check your system clock.\n", current_server->name, tolerate);
72 } else {
73 if (debugmode) {
74 syslog(LOG_DEBUG, "check_date: %s: server time %ld, our time %ld",
75 current_server->name, (long)t, (long)to);
76 }
77 }
78}
int debugmode
Definition: configutil.c:67
void check_date(const struct server *current_server)
void putaline(void)
Definition: nntputil.c:75
time_t timegm(struct tm *tm)
Definition: timegm.c:50
char lineout[1024+1]
Definition: nntputil.c:54
int nntpreply(const struct server *)
Definition: nntputil.c:249
char * lastreply(void)
Definition: nntputil.c:162
void ln_log(int sev, int ctx, const char *format,...)
Definition: ln_log.c:103
#define LNLOG_CSERVER
Definition: ln_log.h:23
#define LNLOG_SWARNING
Definition: ln_log.h:14
#define LNLOG_SINFO
Definition: ln_log.h:16
#define LNLOG_CTOP
Definition: ln_log.h:22
char * name
Definition: leafnode.h:220