OpenNTPD daemon with OpenSSL implementation & flexible configurability
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

495 lines
16 KiB

  1. From: Pekka Helenius <fincer89@hotmail.com>
  2. Date: Tue, 04 Aug 2020 01:52:18 +0300
  3. Subject: Add user agent string support for HTTPS constraints, update ChangeLog
  4. --- a/src/parse.y 2020-08-03 23:22:43.401482642 +0300
  5. +++ b/src/parse.y 2020-08-04 01:03:44.458183310 +0300
  6. @@ -69,6 +69,7 @@ struct opts {
  7. int trusted;
  8. char *refstr;
  9. int port;
  10. + char *useragent;
  11. int pos_num;
  12. double pos_decimal;
  13. } opts;
  14. @@ -91,6 +92,7 @@ typedef struct {
  15. %token SERVER SERVERS SENSOR CORRECTION RTABLE REFID STRATUM WEIGHT
  16. %token ERROR
  17. %token PORT
  18. +%token USERAGENT
  19. %token _NTPD_USER
  20. %token _DRIFTFILE
  21. @@ -164,6 +166,7 @@ typedef struct {
  22. %type <v.opts> weight
  23. %type <v.opts> trusted
  24. %type <v.opts> port
  25. +%type <v.opts> useragent
  26. %type <v.opts> pos_num
  27. %type <v.opts> pos_decimal
  28. @@ -376,6 +379,8 @@ main : LISTEN ON address listen_opts {
  29. p->addr_head.pool = ++poolseqnum;
  30. p->addr_head.name = strdup($3->name);
  31. + if ($4.useragent)
  32. + p->useragent = strdup($4.useragent);
  33. p->addr_head.path = strdup($3->path);
  34. if (p->addr_head.name == NULL ||
  35. p->addr_head.path == NULL)
  36. @@ -425,6 +430,8 @@ main : LISTEN ON address listen_opts {
  37. p->addr_head.pool = 0;
  38. p->addr_head.name = strdup($3->name);
  39. + if ($4.useragent)
  40. + p->useragent = strdup($4.useragent);
  41. p->addr_head.path = strdup($3->path);
  42. if (p->addr_head.name == NULL ||
  43. p->addr_head.path == NULL)
  44. @@ -735,7 +742,7 @@ constraint_opts : { opts_default(); }
  45. constraint_opts_l : constraint_opts_l constraint_opt
  46. | constraint_opt
  47. ;
  48. -constraint_opt : port
  49. +constraint_opt : port | useragent
  50. ;
  51. sensor_opts : { opts_default(); }
  52. @@ -803,6 +810,19 @@ port : PORT NUMBER {
  53. }
  54. ;
  55. +useragent : USERAGENT STRING {
  56. + size_t l = strlen($2);
  57. +
  58. + if (l < 1 || l > USERAGENT_MAX_LENGTH) {
  59. + yyerror("user agent string length must be from 1 to %d characters",
  60. + USERAGENT_MAX_LENGTH);
  61. + free($2);
  62. + YYERROR;
  63. + }
  64. + opts.useragent = $2;
  65. + }
  66. + ;
  67. +
  68. rtable : RTABLE NUMBER {
  69. #ifdef RT_TABLEID_MAX
  70. if ($2 < 0 || $2 > RT_TABLEID_MAX) {
  71. @@ -924,6 +944,7 @@ lookup(char *s)
  72. { "trustlevel_badpeer", _TRUSTLEVEL_BADPEER, "single" },
  73. { "trustlevel_max", _TRUSTLEVEL_MAX, "single" },
  74. { "trustlevel_pathetic", _TRUSTLEVEL_PATHETIC, "single" },
  75. + { "useragent", USERAGENT, "multiple" },
  76. { "weight", WEIGHT, "multiple" },
  77. };
  78. struct keywords *p;
  79. --- a/src/ntpd.h 2020-08-03 23:25:02.978705101 +0300
  80. +++ b/src/ntpd.h 2020-08-03 23:41:03.644863921 +0300
  81. @@ -114,6 +114,8 @@
  82. #define CONSTRAINT_CA SYSCONFDIR "/ssl/cert.pem"
  83. #define CONSTRAINT_CA_VALIDATION 0x01;
  84. +#define USERAGENT_MAX_LENGTH 256
  85. +
  86. #define PARENT_SOCK_FILENO CONSTRAINT_PASSFD
  87. #define NTP_PROC_NAME "ntp_main"
  88. @@ -159,9 +161,11 @@ struct ntp_addr_wrap {
  89. struct ntp_addr_msg {
  90. struct ntp_addr a;
  91. int port;
  92. - size_t namelen;
  93. - size_t pathlen;
  94. + char useragent[USERAGENT_MAX_LENGTH];
  95. + size_t namelen;
  96. + size_t pathlen;
  97. size_t portlen;
  98. + size_t agentlen;
  99. };
  100. struct ntp_status {
  101. @@ -239,6 +243,7 @@ struct constraint {
  102. time_t last;
  103. time_t constraint;
  104. int dnstries;
  105. + char *useragent;
  106. };
  107. struct ntp_conf_sensor {
  108. @@ -495,6 +500,7 @@ struct httpsdate {
  109. char *tls_hostname;
  110. char *tls_path;
  111. char *tls_request;
  112. + char *tls_useragent;
  113. struct tls_config *tls_config;
  114. struct tls *tls_ctx;
  115. struct tm tls_tm;
  116. @@ -502,11 +508,11 @@ struct httpsdate {
  117. struct httpsdate *
  118. httpsdate_init(const char *, const int *, const char *,
  119. - const char *, const u_int8_t *, size_t);
  120. + const char *, const char *, const u_int8_t *, size_t);
  121. void httpsdate_free(void *);
  122. int httpsdate_request(struct httpsdate *, struct timeval *);
  123. void *httpsdate_query(const char *, const int *, const char *,
  124. - const char *, const u_int8_t *, size_t,
  125. + const char *, const char *, const u_int8_t *, size_t,
  126. struct timeval *, struct timeval *);
  127. char *tls_readline(struct tls *, size_t *, size_t *, struct timeval *);
  128. --- a/src/constraint.c 2020-08-02 01:58:28.366952848 +0300
  129. +++ b/src/constraint.c 2020-08-03 23:43:48.584926017 +0300
  130. @@ -132,7 +132,7 @@ constraint_query(struct constraint *cstr
  131. {
  132. time_t now;
  133. struct ntp_addr_msg am;
  134. - struct iovec iov[4];
  135. + struct iovec iov[5];
  136. int iov_cnt = 0;
  137. now = getmonotime();
  138. @@ -185,6 +185,18 @@ constraint_query(struct constraint *cstr
  139. memcpy(&am.a, cstr->addr, sizeof(am.a));
  140. memcpy(&am.port, &cstr->addr_head.port, sizeof(am.port));
  141. + if (cstr->useragent)
  142. + memcpy(&am.useragent, cstr->useragent, USERAGENT_MAX_LENGTH);
  143. +/*
  144. + * FIXME For some reason, dynamically allocating user agent string size does not seem to work
  145. + * Investigate, why usage of dynamic memory allocated data fails.
  146. + *
  147. + if (cstr->useragent) {
  148. + if ((am.useragent = calloc(1, strlen(cstr->useragent)) ) == NULL)
  149. + fatal("constraint id %d: can't allocate memory for user agent string", cstr->id);
  150. + memcpy(am.useragent, cstr->useragent, strlen(cstr->useragent));
  151. + }
  152. +*/
  153. iov[iov_cnt].iov_base = &am;
  154. iov[iov_cnt++].iov_len = sizeof(am);
  155. if (cstr->addr_head.name) {
  156. @@ -202,6 +214,11 @@ constraint_query(struct constraint *cstr
  157. iov[iov_cnt].iov_base = &cstr->addr_head.port;
  158. iov[iov_cnt++].iov_len = am.portlen;
  159. }
  160. + if (cstr->useragent) {
  161. + am.agentlen = strlen(cstr->useragent) + 1;
  162. + iov[iov_cnt].iov_base = cstr->useragent;
  163. + iov[iov_cnt++].iov_len = am.agentlen;
  164. + }
  165. imsg_composev(ibuf_main, IMSG_CONSTRAINT_QUERY,
  166. cstr->id, 0, -1, iov, iov_cnt);
  167. @@ -228,8 +245,9 @@ priv_constraint_msg(u_int32_t id, u_int8
  168. log_warnx("constraint id %d: longer query expected", id);
  169. return;
  170. }
  171. +
  172. memcpy(&am, data, sizeof(am));
  173. - if (len != (sizeof(am) + am.namelen + am.pathlen + am.portlen)) {
  174. + if (len != (sizeof(am) + am.namelen + am.pathlen + am.portlen + am.agentlen)) {
  175. log_warnx("constraint id %d: invalid query received", id);
  176. return;
  177. }
  178. @@ -238,6 +256,7 @@ priv_constraint_msg(u_int32_t id, u_int8
  179. if ((h = calloc(1, sizeof(*h))) == NULL)
  180. fatal("constraint id %d: can't allocate memory for network address", id);
  181. memcpy(h, &am.a, sizeof(*h));
  182. +
  183. h->next = NULL;
  184. cstr = new_constraint();
  185. @@ -308,7 +327,7 @@ priv_constraint_readquery(struct constra
  186. );
  187. memcpy(am, imsg.data, sizeof(*am));
  188. - if (mlen != (sizeof(*am) + am->namelen + am->pathlen + am->portlen))
  189. + if (mlen != (sizeof(*am) + am->namelen + am->pathlen + am->portlen + am->agentlen))
  190. fatalx("constraint: invalid message length received from parent process (%s)",
  191. __func__
  192. );
  193. @@ -327,6 +346,8 @@ priv_constraint_readquery(struct constra
  194. cstr->port = port;
  195. cstr->addr_head.port = port;
  196. + cstr->useragent = strdup(am->useragent);
  197. +
  198. dptr = imsg.data;
  199. memcpy(*data, dptr + sizeof(*am), mlen - sizeof(*am));
  200. imsg_free(&imsg);
  201. @@ -464,7 +485,7 @@ priv_constraint_child(const char *pw_dir
  202. log_debug("constraint %s: initializing HTTPS request", addr);
  203. if ((ctx = httpsdate_query(addr,
  204. &cstr.addr_head.port, cstr.addr_head.name, cstr.addr_head.path,
  205. - conf->ca, conf->ca_len, &rectv, &xmttv)) == NULL) {
  206. + cstr.useragent, conf->ca, conf->ca_len, &rectv, &xmttv)) == NULL) {
  207. log_debug("constraint %s: failed to get proper time results", addr);
  208. /* Abort with failure but without warning */
  209. exit(1);
  210. @@ -852,6 +873,9 @@ constraint_msg_dns(u_int32_t id, u_int8_
  211. ncstr->addr_head.name = strdup(cstr->addr_head.name);
  212. ncstr->addr_head.path = strdup(cstr->addr_head.path);
  213. ncstr->addr_head.port = intdup(cstr->addr_head.port);
  214. +
  215. + if (cstr->useragent)
  216. + ncstr->useragent = strdup(cstr->useragent);
  217. // Unless we do this, we have value 0 in ncstr->port
  218. ncstr->port = intdup(cstr->port);
  219. --- a/src/constraint-libressl.c 2020-08-01 19:50:24.130263065 +0300
  220. +++ b/src/constraint-libressl.c 2020-08-04 00:01:30.008179382 +0300
  221. @@ -26,7 +26,7 @@
  222. struct httpsdate *
  223. httpsdate_init(const char *addr, const int *port, const char *hostname,
  224. - const char *path, const u_int8_t *ca, size_t ca_len)
  225. + const char *path, const char *useragent, const u_int8_t *ca, size_t ca_len)
  226. {
  227. struct httpsdate *httpsdate = NULL;
  228. char port_s[sizeof(port)];
  229. @@ -45,10 +45,23 @@ httpsdate_init(const char *addr, const i
  230. (httpsdate->tls_path = strdup(path)) == NULL)
  231. goto fail;
  232. - if (asprintf(&httpsdate->tls_request,
  233. - "HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
  234. - httpsdate->tls_path, httpsdate->tls_hostname) == -1)
  235. - goto fail;
  236. + if (useragent)
  237. + httpsdate->tls_useragent = strdup(useragent);
  238. +
  239. + if (httpsdate->tls_useragent) {
  240. + if (asprintf(&httpsdate->tls_request,
  241. + "HEAD %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: close\r\n\r\n",
  242. + httpsdate->tls_path,
  243. + httpsdate->tls_hostname,
  244. + httpsdate->tls_useragent) == -1)
  245. + goto fail;
  246. + } else {
  247. + if (asprintf(&httpsdate->tls_request,
  248. + "HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
  249. + httpsdate->tls_path,
  250. + httpsdate->tls_hostname) == -1)
  251. + goto fail;
  252. + }
  253. if ((httpsdate->tls_config = tls_config_new()) == NULL)
  254. goto fail;
  255. @@ -110,6 +123,10 @@ httpsdate_request(struct httpsdate *http
  256. if (tls_configure(httpsdate->tls_ctx, httpsdate->tls_config) == -1)
  257. goto fail;
  258. + if (httpsdate->tls_useragent)
  259. + log_debug("constraint %s: user agent: %s", httpsdate->tls_addr,
  260. + httpsdate->tls_useragent);
  261. +
  262. /*
  263. * LibreSSL expects an address string, which can also be a DNS name,
  264. * but we pass a pre-resolved IP address string in tls_addr so it
  265. @@ -222,14 +239,14 @@ httpsdate_request(struct httpsdate *http
  266. void *
  267. httpsdate_query(const char *addr, const int *port, const char *hostname,
  268. - const char *path, const u_int8_t *ca, size_t ca_len,
  269. + const char *path, const char *useragent, const u_int8_t *ca, size_t ca_len,
  270. struct timeval *rectv, struct timeval *xmttv)
  271. {
  272. struct httpsdate *httpsdate;
  273. struct timeval when;
  274. time_t t;
  275. - if ((httpsdate = httpsdate_init(addr, port, hostname, path,
  276. + if ((httpsdate = httpsdate_init(addr, port, hostname, path, useragent,
  277. ca, ca_len)) == NULL)
  278. return (NULL);
  279. --- a/src/constraint-openssl.c 2020-08-03 19:23:54.377109002 +0300
  280. +++ b/src/constraint-openssl.c 2020-08-04 00:01:16.178179367 +0300
  281. @@ -37,11 +37,20 @@ o_httpsdate_init(struct constraint *cstr
  282. if ((httpsdate->cstr = cstr) == NULL)
  283. goto fail;
  284. - if (asprintf(&httpsdate->tls_request,
  285. - "HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
  286. - httpsdate->cstr->addr_head.path,
  287. - httpsdate->cstr->addr_head.name) == -1)
  288. - goto fail;
  289. + if (httpsdate->cstr->useragent) {
  290. + if (asprintf(&httpsdate->tls_request,
  291. + "HEAD %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: close\r\n\r\n",
  292. + httpsdate->cstr->addr_head.path,
  293. + httpsdate->cstr->addr_head.name,
  294. + httpsdate->cstr->useragent) == -1)
  295. + goto fail;
  296. + } else {
  297. + if (asprintf(&httpsdate->tls_request,
  298. + "HEAD %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
  299. + httpsdate->cstr->addr_head.path,
  300. + httpsdate->cstr->addr_head.name) == -1)
  301. + goto fail;
  302. + }
  303. if ((httpsdate->tls_method = TLS_method()) == NULL)
  304. goto fail;
  305. @@ -161,6 +170,10 @@ o_httpsdate_request(struct o_httpsdate *
  306. goto fail;
  307. }
  308. + if (httpsdate->cstr->useragent)
  309. + log_debug("constraint %s: user agent: %s", ia_str,
  310. + httpsdate->cstr->useragent);
  311. +
  312. log_debug("constraint %s: establishing connection", ia_str);
  313. ret = SSL_connect(httpsdate->tls_conn);
  314. if (ret < 1) {
  315. --- a/src/ntpd.conf.5 2020-08-03 23:21:11.124672226 +0300
  316. +++ b/src/ntpd.conf.5 2020-08-04 01:38:53.878185531 +0300
  317. @@ -14,7 +14,7 @@
  318. .\" AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  319. .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  320. .\"
  321. -.Dd $Mdocdate: August 01 2020 $
  322. +.Dd $Mdocdate: August 04 2020 $
  323. .Dt NTPD.CONF 5
  324. .Os
  325. .Sh NAME
  326. @@ -58,14 +58,12 @@ is given as an address,
  327. .Xr ntpd 8
  328. will listen on all local addresses using the specified routing table.
  329. .Xr ntpd 8
  330. -does not listen on any address by default.
  331. -The optional
  332. +does not listen on any address by default. The optional
  333. .Ic rtable
  334. keyword will specify which routing table to listen on, if the operating system supports rdomains.
  335. By default
  336. .Xr ntpd 8
  337. -will listen using the current routing table.
  338. -The optional
  339. +will listen using the current routing table. The optional
  340. .Ic port
  341. keyword will specify which local UDP port the NTP server process should use for inbound connections.
  342. By default
  343. @@ -240,18 +238,29 @@ Configuring a constraint without proper
  344. .Xr ntpd 8
  345. to log a warning message on startup.
  346. .Bl -tag -width Ds
  347. -.It Ic constraint from Ar url [ip...]
  348. +.It Xo Ic constraint from Ar url
  349. +.Op Ar ip...
  350. .Op Ic port Ar port-number
  351. +.Op Ic useragent Ar agent-string
  352. +.Xc
  353. Specify the URL, IP address or the hostname of an HTTPS server to
  354. provide a constraint. The optional
  355. .Ic port
  356. number is an HTTPS server port to connect to. By default
  357. .Xr ntpd 8
  358. -will connect to remote TCP port 443.
  359. -If the url is followed by one or more addresses the url and addresses will be
  360. +will connect to remote TCP port 443. The optional
  361. +.Ic useragent
  362. +string adds a defined user agent string into a constraint HTTP
  363. +request. This option helps retrieving a time constraint from HTTPS
  364. +servers which block user agentless HTTP
  365. +requests. If the
  366. +.Ic url
  367. +is followed by one or more
  368. +.Ic ip
  369. +addresses the URL and addresses will be
  370. tried until a working one is found.
  371. -The url path and expected certificate name is always taken from the
  372. -url specified.
  373. +The URL path and expected certificate name is always taken from the
  374. +URL specified.
  375. If
  376. .Ic constraint from
  377. is used more than once,
  378. @@ -259,29 +268,34 @@ is used more than once,
  379. will calculate a median constraint from all the servers specified.
  380. .Bd -literal -offset indent
  381. server ntp.example.org
  382. -constraint from www.example.com
  383. +constraint from "www.example.com"
  384. constraint from "https://9.9.9.9" "2620:fe::9"
  385. -constraint from www.google.com port 443
  386. +constraint from "www.google.com" port 443 useragent "OpenNTPD time query"
  387. .Ed
  388. -.It Ic constraints from Ar url
  389. +.It Xo Ic constraints from Ar url
  390. .Op Ic port Ar port-number
  391. +.Op Ic useragent Ar agent-string
  392. +.Xc
  393. As with
  394. .Ic constraint from ,
  395. specify the URL, IP address or the hostname of an HTTPS server to
  396. provide a constraint.
  397. Should the hostname resolve to multiple IP addresses,
  398. .Xr ntpd 8
  399. -will calculate a median constraint from all of them.
  400. -The optional
  401. +will calculate a median constraint from all of them. The optional
  402. .Ic port
  403. number is an HTTPS server port to connect to. By default
  404. .Xr ntpd 8
  405. -will connect to remote TCP port 443.
  406. +will connect to remote TCP port 443. The optional
  407. +.Ic useragent
  408. +string adds a defined user agent string into a constraint HTTP
  409. +request. This option helps retrieving a time constraint from HTTPS
  410. +servers which block user agentless HTTP requests.
  411. For example:
  412. .Bd -literal -offset indent
  413. servers pool.ntp.org
  414. constraints from "https://www.google.com/"
  415. -constraints from "https://duckduckgo.com/" port 443
  416. +constraints from "https://duckduckgo.com/" port 443 useragent "OpenNTPD time query"
  417. .Ed
  418. .El
  419. .Sh ADVANCED KEYWORDS
  420. @@ -676,6 +690,16 @@ Negligible drift time to not log in mill
  421. 32
  422. .El
  423. .Ed
  424. +.It Ic max_display_width Ar number
  425. +Maximum number of characters in a
  426. +.Xr ntpctl 8
  427. +report line (peers, status, sensors and all).
  428. +.Bd -literal -offset indent
  429. +.Bl -tag -width "Default:" -compact
  430. +.It Default:
  431. +80
  432. +.El
  433. +.Ed
  434. .It Ic max_frequency_adjust Ar decimal
  435. Maximum allowed frequency correction per iteration.
  436. .Bd -literal -offset indent
  437. @@ -780,6 +804,14 @@ Sensor default reference ID string.
  438. "HARD"
  439. .El
  440. .Ed
  441. +.It Ic sensor_offsets Ar seconds
  442. +Maximum allowed sensor time offset in seconds.
  443. +.Bd -literal -offset indent
  444. +.Bl -tag -width "Default:" -compact
  445. +.It Default:
  446. +6
  447. +.El
  448. +.Ed
  449. .It Ic sensor_query_interval Ar seconds
  450. Sensor query interval in seconds.
  451. .Bd -literal -offset indent
  452. --- a/ChangeLog 2020-08-02 14:01:47.093664999 +0300
  453. +++ b/ChangeLog 2020-08-04 01:52:06.054853035 +0300
  454. @@ -32,6 +32,10 @@ OpenNTPD Portable Release Notes:
  455. * Many previously hardcoded values are now configurable via conf file.
  456. + * Implement UDP & TCP port selection for multiple options.
  457. +
  458. + * Implement custom user agent string support for constraints.
  459. +
  460. * Implemented OpenSSL support. Either LibreSSL or OpenSSL can be used.
  461. * Improved log entries interpretation.