• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2013, Jacob Appelbaum
2  * Copyright (c) 2012, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
4 
5 /**
6   * \file tlsdate.h
7   * \brief The main header for our clock helper.
8   **/
9 
10 #ifndef TLSDATE_H
11 #define TLSDATE_H
12 
13 #include "src/configmake.h"
14 #include <limits.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <getopt.h>
20 #include <limits.h>
21 #include <signal.h>
22 #include <time.h>
23 #include <unistd.h>
24 
25 #include "src/rtc.h"
26 
27 #define DEFAULT_HOST "google.com"
28 #define DEFAULT_PORT "443"
29 #define DEFAULT_PROXY "none"
30 #define DEFAULT_PROTOCOL "tlsv1"
31 #define DEFAULT_CERTDIR "/etc/ssl/certs"
32 #define DEFAULT_CERTFILE TLSDATE_CERTFILE
33 #define DEFAULT_DAEMON_CACHEDIR "/var/cache/tlsdated"
34 #define DEFAULT_DAEMON_TMPSUFFIX ".new"
35 #define DEFAULT_TLSDATE TLSDATE
36 #define DEFAULT_RTC_DEVICE "/dev/rtc"
37 #define DEFAULT_CONF_FILE TLSDATE_CONF_DIR "tlsdated.conf"
38 
39 /* tlsdated magic numbers */
40 #define MAX_TRIES 10
41 #define WAIT_BETWEEN_TRIES 10
42 #define SUBPROCESS_TRIES 10
43 #define SUBPROCESS_WAIT_BETWEEN_TRIES 10
44 #define RESOLVER_TIMEOUT 30
45 /* Invalidate the network sync once per day. */
46 #define STEADY_STATE_INTERVAL (60*60*24)
47 /* Check if the clock has jumped every four hours. */
48 #define CONTINUITY_INTERVAL (60*60*4)
49 #define DEFAULT_SYNC_HWCLOCK 1
50 #define DEFAULT_LOAD_FROM_DISK 1
51 #define DEFAULT_SAVE_TO_DISK 1
52 #define DEFAULT_USE_NETLINK 1
53 #define DEFAULT_DRY_RUN 0
54 #define MAX_SANE_BACKOFF (10*60) /* exponential backoff should only go this far */
55 
56 #ifndef TLSDATED_MAX_DATE
57 #define TLSDATED_MAX_DATE 1999991337L /* this'll be a great bug some day */
58 #endif
59 
60 #define MAX_EVENT_PRIORITIES 2
61 #define PRI_SAVE 0
62 #define PRI_NET 1
63 #define PRI_WAKE 1
64 #define PRI_ANY 1
65 
66 /* Sync sources in order of "reliability" */
67 #define SYNC_TYPE_NONE  (0)
68 #define SYNC_TYPE_BUILD  (1 << 0)
69 #define SYNC_TYPE_DISK  (1 << 1)
70 #define SYNC_TYPE_RTC  (1 << 2)
71 #define SYNC_TYPE_PLATFORM  (1 << 3)
72 #define SYNC_TYPE_NET  (1 << 4)
73 
74 /* Simple time setter<>tlsdated protocol */
75 #define SETTER_EXIT 0
76 #define SETTER_BAD_TIME 1
77 #define SETTER_NO_SAVE 2
78 #define SETTER_READ_ERR 3
79 #define SETTER_TIME_SET 4
80 #define SETTER_SET_ERR 5
81 #define SETTER_NO_SBOX 6
82 #define SETTER_NO_RTC 7
83 
84 #define TEST_HOST 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', \
85                   'c', 'o', 'm'
86 #define TEST_HOST_SIZE 14
87 static const char kTestHost[] = { TEST_HOST, 0 };
88 #define TEST_PORT 80
89 
90 /** The current version of tlsdate. */
91 #define tlsdate_version VERSION
92 
93 /** GNU/Hurd support requires that we declare this ourselves: */
94 #ifndef PATH_MAX
95 #define PATH_MAX 1024
96 #endif
97 #ifndef MAXPATHLEN
98 #define MAXPATHLEN PATH_MAX
99 #endif
100 
101 struct source
102 {
103 	struct source *next;
104 	char *host;
105 	char *port;
106 	char *proxy;
107 	int id;
108 };
109 
110 struct opts
111 {
112   const char *user;
113   const char *group;
114   char *supp_groups;
115   int max_tries;
116   int min_steady_state_interval;
117   int wait_between_tries;
118   int subprocess_tries;
119   int subprocess_wait_between_tries;
120   int steady_state_interval;
121   int continuity_interval;
122   const char *base_path;
123   char **base_argv;
124   char **argv;
125   int should_sync_hwclock;
126   int should_load_disk;
127   int should_save_disk;
128   int should_netlink;
129   int dry_run;
130   int jitter;
131   char *conf_file;
132   struct source *sources;
133   struct source *cur_source;
134   char *proxy;
135   int leap;
136   int should_dbus;
137 };
138 
139 #define MAX_FQDN_LEN 255
140 #define MAX_SCHEME_LEN 9
141 #define MAX_PORT_LEN 6  /* incl. : */
142 #define MAX_PROXY_URL (MAX_FQDN_LEN + MAX_SCHEME_LEN + MAX_PORT_LEN + 1)
143 
144 enum event_id_t
145 {
146   E_RESOLVER = 0,
147   E_TLSDATE,
148   E_TLSDATE_STATUS,
149   E_TLSDATE_TIMEOUT,
150   E_SAVE,
151   E_SIGCHLD,
152   E_SIGTERM,
153   E_STEADYSTATE,
154   E_ROUTEUP,
155   E_MAX
156 };
157 
158 struct event_base;
159 
160 /* This struct is used for passing tlsdated runtime state between
161  * events/ in its event loop.
162  */
163 struct state
164 {
165   struct opts opts;
166   struct event_base *base;
167   void *dbus;
168   char **envp;
169 
170   time_t clock_delta;
171   int last_sync_type;
172   time_t last_time;
173 
174   char timestamp_path[PATH_MAX];
175   struct rtc_handle hwclock;
176   char dynamic_proxy[MAX_PROXY_URL];
177   /* Event triggered events */
178 
179   struct event *events[E_MAX];
180   int tlsdate_monitor_fd;
181   pid_t tlsdate_pid;
182   pid_t setter_pid;
183   int setter_save_fd;
184   int setter_notify_fd;
185   uint32_t backoff;
186   int tries;
187   int resolving;
188   int running;  /* tlsdate itself */
189   int exitting;
190 };
191 
192 char timestamp_path[PATH_MAX];
193 
194 int is_sane_time (time_t ts);
195 int load_disk_timestamp (const char *path, time_t * t);
196 void save_disk_timestamp (const char *path, time_t t);
197 int add_jitter (int base, int jitter);
198 void time_setter_coprocess (int time_fd, int notify_fd, struct state *state);
199 int tlsdate (struct state *state);
200 
201 int save_timestamp_to_fd (int fd, time_t t);
202 void set_conf_defaults (struct opts *opts);
203 int new_tlsdate_monitor_pipe (int fds[2]);
204 int read_tlsdate_response (int fd, time_t *t);
205 
206 void invalidate_time (struct state *state);
207 int check_continuity (time_t *delta);
208 
209 void action_check_continuity (int fd, short what, void *arg);
210 void action_kickoff_time_sync (int fd, short what, void *arg);
211 void action_invalidate_time (int fd, short what, void *arg);
212 void action_stdin_wakeup (int fd, short what, void *arg);
213 void action_netlink_ready (int fd, short what, void *arg);
214 void action_run_tlsdate (int fd, short what, void *arg);
215 void action_sigterm (int fd, short what, void *arg);
216 void action_sync_and_save (int fd, short what, void *arg);
217 void action_time_set (int fd, short what, void *arg);
218 void action_tlsdate_status (int fd, short what, void *arg);
219 
220 int setup_event_timer_continuity (struct state *state);
221 int setup_event_timer_sync (struct state *state);
222 int setup_event_route_up (struct state *state);
223 int setup_time_setter (struct state *state);
224 int setup_tlsdate_status (struct state *state);
225 int setup_sigchld_event (struct state *state, int persist);
226 
227 void report_setter_error (siginfo_t *info);
228 
229 void sync_and_save (void *hwclock_handle, int should_save);
230 
231 /** This is where we store parsed commandline options. */
232 typedef struct
233 {
234   int verbose;
235   int verbose_debug;
236   int ca_racket;
237   int help;
238   int showtime;
239   int setclock;
240   time_t manual_time;
241   char *host;
242   char *port;
243   char *protocol;
244 } tlsdate_options_t;
245 
246 #endif /* TLSDATE_H */
247