1 /* $OpenBSD: control.c,v 1.18 2020/02/12 19:14:56 otto Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 5 * Copyright (c) 2012 Mike Miller <mmiller@mgm51.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/stat.h> 22 #include <sys/socket.h> 23 #include <sys/un.h> 24 #include <errno.h> 25 #include <math.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <unistd.h> 30 #include <fcntl.h> 31 #include <err.h> 32 33 #include "ntpd.h" 34 35 #define CONTROL_BACKLOG 5 36 37 #define square(x) ((x) * (x)) 38 39 int 40 control_check(char *path) 41 { 42 struct sockaddr_un sun; 43 int fd; 44 45 bzero(&sun, sizeof(sun)); 46 sun.sun_family = AF_UNIX; 47 strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 48 49 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { 50 log_debug("control_check: socket check"); 51 return (-1); 52 } 53 54 if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == 0) { 55 log_debug("control_check: socket in use"); 56 close(fd); 57 return (-1); 58 } 59 60 close(fd); 61 62 return (0); 63 } 64 65 int 66 control_init(char *path) 67 { 68 struct sockaddr_un sa; 69 int fd; 70 mode_t old_umask; 71 72 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) { 73 log_warn("control_init: socket"); 74 return (-1); 75 } 76 77 memset(&sa, 0, sizeof(sa)); 78 sa.sun_family = AF_UNIX; 79 if (strlcpy(sa.sun_path, path, sizeof(sa.sun_path)) >= 80 sizeof(sa.sun_path)) 81 errx(1, "ctl socket name too long"); 82 83 if (unlink(path) == -1) 84 if (errno != ENOENT) { 85 log_warn("control_init: unlink %s", path); 86 close(fd); 87 return (-1); 88 } 89 90 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); 91 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { 92 log_warn("control_init: bind: %s", path); 93 close(fd); 94 umask(old_umask); 95 return (-1); 96 } 97 umask(old_umask); 98 99 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) { 100 log_warn("control_init: chmod"); 101 close(fd); 102 (void)unlink(path); 103 return (-1); 104 } 105 106 session_socket_nonblockmode(fd); 107 108 return (fd); 109 } 110 111 int 112 control_listen(int fd) 113 { 114 if (fd != -1 && listen(fd, CONTROL_BACKLOG) == -1) { 115 log_warn("control_listen: listen"); 116 return (-1); 117 } 118 119 return (0); 120 } 121 122 void 123 control_shutdown(int fd) 124 { 125 close(fd); 126 } 127 128 int 129 control_accept(int listenfd) 130 { 131 int connfd; 132 socklen_t len; 133 struct sockaddr_un sa; 134 struct ctl_conn *ctl_conn; 135 136 len = sizeof(sa); 137 if ((connfd = accept(listenfd, 138 (struct sockaddr *)&sa, &len)) == -1) { 139 if (errno != EWOULDBLOCK && errno != EINTR) 140 log_warn("control_accept: accept"); 141 return (0); 142 } 143 144 session_socket_nonblockmode(connfd); 145 146 if ((ctl_conn = calloc(1, sizeof(struct ctl_conn))) == NULL) { 147 log_warn("control_accept"); 148 close(connfd); 149 return (0); 150 } 151 152 imsg_init(&ctl_conn->ibuf, connfd); 153 154 TAILQ_INSERT_TAIL(&ctl_conns, ctl_conn, entry); 155 156 return (1); 157 } 158 159 struct ctl_conn * 160 control_connbyfd(int fd) 161 { 162 struct ctl_conn *c; 163 164 TAILQ_FOREACH(c, &ctl_conns, entry) { 165 if (c->ibuf.fd == fd) 166 break; 167 } 168 169 return (c); 170 } 171 172 int 173 control_close(int fd) 174 { 175 struct ctl_conn *c; 176 177 if ((c = control_connbyfd(fd)) == NULL) { 178 log_warn("control_close: fd %d: not found", fd); 179 return (0); 180 } 181 182 msgbuf_clear(&c->ibuf.w); 183 TAILQ_REMOVE(&ctl_conns, c, entry); 184 185 close(c->ibuf.fd); 186 free(c); 187 188 return (1); 189 } 190 191 int 192 control_dispatch_msg(struct pollfd *pfd, u_int *ctl_cnt) 193 { 194 struct imsg imsg; 195 struct ctl_conn *c; 196 struct ntp_peer *p; 197 struct ntp_sensor *s; 198 struct ctl_show_status c_status; 199 struct ctl_show_peer c_peer; 200 struct ctl_show_sensor c_sensor; 201 int cnt; 202 ssize_t n; 203 204 if ((c = control_connbyfd(pfd->fd)) == NULL) { 205 log_warn("control_dispatch_msg: fd %d: not found", pfd->fd); 206 return (0); 207 } 208 209 if (pfd->revents & POLLOUT) 210 if (msgbuf_write(&c->ibuf.w) <= 0 && errno != EAGAIN) { 211 *ctl_cnt -= control_close(pfd->fd); 212 return (1); 213 } 214 215 if (!(pfd->revents & POLLIN)) 216 return (0); 217 218 if (((n = imsg_read(&c->ibuf)) == -1 && errno != EAGAIN) || n == 0) { 219 *ctl_cnt -= control_close(pfd->fd); 220 return (1); 221 } 222 223 for (;;) { 224 if ((n = imsg_get(&c->ibuf, &imsg)) == -1) { 225 *ctl_cnt -= control_close(pfd->fd); 226 return (1); 227 } 228 if (n == 0) 229 break; 230 231 switch (imsg.hdr.type) { 232 case IMSG_CTL_SHOW_STATUS: 233 build_show_status(&c_status); 234 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1, 235 &c_status, sizeof (c_status)); 236 break; 237 case IMSG_CTL_SHOW_PEERS: 238 cnt = 0; 239 TAILQ_FOREACH(p, &conf->ntp_peers, entry) { 240 build_show_peer(&c_peer, p); 241 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS, 242 0, 0, -1, &c_peer, sizeof(c_peer)); 243 cnt++; 244 } 245 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS_END, 246 0, 0, -1, &cnt, sizeof(cnt)); 247 break; 248 case IMSG_CTL_SHOW_SENSORS: 249 cnt = 0; 250 TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { 251 build_show_sensor(&c_sensor, s); 252 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS, 253 0, 0, -1, &c_sensor, sizeof(c_sensor)); 254 cnt++; 255 } 256 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS_END, 257 0, 0, -1, &cnt, sizeof(cnt)); 258 break; 259 case IMSG_CTL_SHOW_ALL: 260 build_show_status(&c_status); 261 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_STATUS, 0, 0, -1, 262 &c_status, sizeof (c_status)); 263 264 cnt = 0; 265 TAILQ_FOREACH(p, &conf->ntp_peers, entry) { 266 build_show_peer(&c_peer, p); 267 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS, 268 0, 0, -1, &c_peer, sizeof(c_peer)); 269 cnt++; 270 } 271 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_PEERS_END, 272 0, 0, -1, &cnt, sizeof(cnt)); 273 274 cnt = 0; 275 TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { 276 build_show_sensor(&c_sensor, s); 277 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS, 278 0, 0, -1, &c_sensor, sizeof(c_sensor)); 279 cnt++; 280 } 281 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_SENSORS_END, 282 0, 0, -1, &cnt, sizeof(cnt)); 283 284 imsg_compose(&c->ibuf, IMSG_CTL_SHOW_ALL_END, 285 0, 0, -1, NULL, 0); 286 break; 287 default: 288 break; 289 } 290 imsg_free(&imsg); 291 } 292 return (0); 293 } 294 295 void 296 session_socket_nonblockmode(int fd) 297 { 298 int flags; 299 300 if ((flags = fcntl(fd, F_GETFL)) == -1) 301 fatal("fcntl F_GETFL"); 302 303 flags |= O_NONBLOCK; 304 305 if ((flags = fcntl(fd, F_SETFL, flags)) == -1) 306 fatal("fcntl F_SETFL"); 307 } 308 309 void 310 build_show_status(struct ctl_show_status *cs) 311 { 312 struct ntp_peer *p; 313 struct ntp_sensor *s; 314 315 cs->peercnt = cs->valid_peers = 0; 316 cs->sensorcnt = cs->valid_sensors = 0; 317 318 TAILQ_FOREACH(p, &conf->ntp_peers, entry) { 319 cs->peercnt++; 320 if (p->trustlevel >= TRUSTLEVEL_BADPEER) 321 cs->valid_peers++; 322 } 323 TAILQ_FOREACH(s, &conf->ntp_sensors, entry) { 324 cs->sensorcnt++; 325 if (s->update.good) 326 cs->valid_sensors++; 327 } 328 329 cs->synced = conf->status.synced; 330 cs->stratum = conf->status.stratum; 331 cs->clock_offset = getoffset() * 1000.0; 332 cs->constraints = !TAILQ_EMPTY(&conf->constraints); 333 cs->constraint_median = conf->constraint_median; 334 cs->constraint_last = conf->constraint_last; 335 cs->constraint_errors = conf->constraint_errors; 336 } 337 338 void 339 build_show_peer(struct ctl_show_peer *cp, struct ntp_peer *p) 340 { 341 const char *a = "not resolved"; 342 const char *pool = "", *addr_head_name = ""; 343 const char *auth = ""; 344 u_int8_t shift, best, validdelaycnt, jittercnt; 345 time_t now; 346 347 now = getmonotime(); 348 349 if (p->addr) { 350 a = log_sockaddr((struct sockaddr *)&p->addr->ss); 351 if (p->addr->notauth) 352 auth = " (non-dnssec lookup)"; 353 } 354 if (p->addr_head.pool) 355 pool = "from pool "; 356 357 if (0 != strcmp(a, p->addr_head.name) || p->addr_head.pool) 358 addr_head_name = p->addr_head.name; 359 360 snprintf(cp->peer_desc, sizeof(cp->peer_desc), 361 "%s %s%s%s", a, pool, addr_head_name, auth); 362 363 validdelaycnt = best = 0; 364 cp->offset = cp->delay = 0.0; 365 for (shift = 0; shift < OFFSET_ARRAY_SIZE; shift++) { 366 if (p->reply[shift].delay > 0.0) { 367 cp->offset += p->reply[shift].offset; 368 cp->delay += p->reply[shift].delay; 369 370 if (p->reply[shift].delay < p->reply[best].delay) 371 best = shift; 372 373 validdelaycnt++; 374 } 375 } 376 377 if (validdelaycnt > 1) { 378 cp->offset /= validdelaycnt; 379 cp->delay /= validdelaycnt; 380 } 381 382 jittercnt = 0; 383 cp->jitter = 0.0; 384 for (shift = 0; shift < OFFSET_ARRAY_SIZE; shift++) { 385 if (p->reply[shift].delay > 0.0 && shift != best) { 386 cp->jitter += square(p->reply[shift].delay - 387 p->reply[best].delay); 388 jittercnt++; 389 } 390 } 391 if (jittercnt > 1) 392 cp->jitter /= jittercnt; 393 cp->jitter = sqrt(cp->jitter); 394 395 if (p->shift == 0) 396 shift = OFFSET_ARRAY_SIZE - 1; 397 else 398 shift = p->shift - 1; 399 400 if (conf->status.synced == 1 && 401 p->reply[shift].status.send_refid == conf->status.refid) 402 cp->syncedto = 1; 403 else 404 cp->syncedto = 0; 405 406 /* milliseconds to reduce number of leading zeroes */ 407 cp->offset *= 1000.0; 408 cp->delay *= 1000.0; 409 cp->jitter *= 1000.0; 410 411 cp->weight = p->weight; 412 cp->trustlevel = p->trustlevel; 413 cp->stratum = p->reply[shift].status.stratum; 414 cp->next = p->next - now < 0 ? 0 : p->next - now; 415 cp->poll = p->poll; 416 } 417 418 void 419 build_show_sensor(struct ctl_show_sensor *cs, struct ntp_sensor *s) 420 { 421 time_t now; 422 u_int8_t shift; 423 u_int32_t refid; 424 425 now = getmonotime(); 426 427 memcpy(&refid, SENSOR_DEFAULT_REFID, sizeof(refid)); 428 refid = refid == s->refid ? 0 : s->refid; 429 430 snprintf(cs->sensor_desc, sizeof(cs->sensor_desc), 431 "%s %.4s", s->device, (char *)&refid); 432 433 if (s->shift == 0) 434 shift = SENSOR_OFFSETS - 1; 435 else 436 shift = s->shift - 1; 437 438 if (conf->status.synced == 1 && 439 s->offsets[shift].status.send_refid == conf->status.refid) 440 cs->syncedto = 1; 441 else 442 cs->syncedto = 0; 443 444 cs->weight = s->weight; 445 cs->good = s->update.good; 446 cs->stratum = s->offsets[shift].status.stratum; 447 cs->next = s->next - now < 0 ? 0 : s->next - now; 448 cs->poll = SENSOR_QUERY_INTERVAL; 449 cs->offset = s->offsets[shift].offset * 1000.0; 450 cs->correction = (double)s->correction / 1000.0; 451 } 452