/contrib/ntp/sntp/README

https://bitbucket.org/freebsd/freebsd-head/ · #! · 536 lines · 407 code · 129 blank · 0 comment · 0 complexity · 528465953aae43a0396122bfdeb8d388 MD5 · raw file

  1. SNTP (Simple Network Time Protocol Utility) - Version 1.6
  2. ----------------------------------------------------------
  3. Please read the file Copyright first. Also note that the file RFC2030.TXT is
  4. David Mills's copyright and not the author's - it is just a copy of the RFC
  5. that is available from so many Internet archives.
  6. RFC 1305 (Network Time Protocol - NTP) is an attempt to provide globally
  7. consistent timestamps in an extremely hostile environment; it is fiendishly
  8. complicated and an impressive piece of virtuosity. RFC 2030 (Simple Network
  9. Time Protocol - SNTP) which supersedes RFC 1769 describes a subset of this that
  10. will give excellent accuracy in most environments encountered in practice; it
  11. uses only the obvious algorithms that have been used since time immemorial.
  12. WARNING: the text version of RFC 1305 is incomplete, and omits the tables that
  13. are in the Postscript version. Unfortunately, these contain the only copy of
  14. some critical information.
  15. draft-mills-sntp-v4-00.txt is the next proposed revision of RFC 2030,
  16. and the current goal is to have this code implement that specification.
  17. SNTP Servers - Some Little-Known Facts
  18. --------------------------------------
  19. RFC 2030 states that SNTP clients should be used only at the lowest level,
  20. which is good practice. It then states that SNTP servers should be used only
  21. at stratum 1 (i.e. top level), which is bizarre! A far saner use of them would
  22. be for the very lowest level of server, exporting solely to local clients that
  23. do not themselves act as servers to ANY system (e.g. on a Netware server,
  24. exporting only to the PCs that it manages).
  25. [There is missing language in the previous paragraph. SNTP is designed
  26. to be used in 2 cases: as a client at the lowest levels of the timing
  27. hierarchy, or as a server of last resort at stratum 1 when connected to
  28. a modem or radio clock.]
  29. [This is as far as I have updated this file as part of the upgrade.]
  30. If the NTP network were being run as a directed acyclic graph (i.e. using SNTP
  31. rather than full NTP), with a diameter of D links and a maximum error per link
  32. of E, the maximum synchronisation error would be D*E. Reasonable figures for D
  33. and E are 5 and 0.1 seconds, so this would be adequate for most uses. Note
  34. that the fact that the graph is acyclic is critical, which is one reason why
  35. SNTP client/servers must NEVER be embedded WITHIN an NTP network.
  36. The other reason is that inserting SNTP client/servers at a low stratum (but
  37. not the root) of an NTP network could easily break NTP! See RFC 1305 for why,
  38. but don't expect the answer to stand out at you. It would be easy to extend
  39. SNTP to a full-function client/server application, thus making it into a true
  40. alternative to ntp, but this incompatibility is why it MUST NOT be done.
  41. The above does not mean that the SNTP approach is unsatisfactory, but only that
  42. it is incompatible with full NTP. The author would favour a complete SNTP
  43. network using the SNTP approach, and the statistical error reduction used in
  44. SNTP, but it actually addresses a slightly different problem from that
  45. addressed by NTP. TANSTAAFL.
  46. FINAL WARNING: do NOT use this program to serve NTP requests from outside the
  47. systems that you manage. If you do this, and manage to break the time
  48. synchronisation on other people's systems, you will be regarded very
  49. unfavourably. Actually, this should be possible only if their NTP client is
  50. completely broken, because SNTP does its damnedest to declare its packets as
  51. the lowest form of NTP timestamp.
  52. SNTP and its Assumptions
  53. -------------------------
  54. SNTP is intended to be a straightforward SNTP daemon/utility that is easy to
  55. build on any reasonable Unix platform (and most near-Unix ones), whether or not
  56. it has ever been ported to them before. It is intended to answer the following
  57. requirements, either by challenge and response or the less reliable broadcast
  58. method:
  59. A simple command to run on Unix systems that will check the time
  60. and optionally drift compared with a known, local and reliable NTP
  61. time server. No privilege is required just to read the time and
  62. estimate the drift.
  63. A client for Unix systems that will synchronise the time from a known,
  64. local and reliable NTP time server. This is probably the most common
  65. one, and the need that caused the program to be written.
  66. A server for Unix systems that are synchronised other than by NTP
  67. methods and that need to synchronise other systems by NTP. This is
  68. the classroom of PCs with a central server scenario. It is NOT
  69. intended to work as a peer with true NTP servers, and won't.
  70. A simple method by which two or more Unix systems can keep themselves
  71. synchronised using what is becoming a standard protocol. Yes, I know
  72. that there are half-a-dozen other such methods.
  73. A base for building non-Unix SNTP clients. Some 3/4 of the code
  74. (including all of the complicated algorithms and NTP packet handling)
  75. should work, unchanged, on any system with an ANSI/ISO C compiler.
  76. There are full tracing facilities and a lot of paranoia in the code to check
  77. for bad packets (more than in ntp) which may need relaxing in the light of
  78. experience. Unfortunately, RFC 1305 does not include a precise description of
  79. the data protocol, despite its length, and there are some internal
  80. inconsistencies and differences between it and RFC 2030 and ntp's behaviour.
  81. WARNING: SNTP has not been tested in conjunction with ntp broadcasts or ntp
  82. clients, as the ability to do so was not available to the author. It is very
  83. unlikely that it won't work, but you should check. Much of the paranoid code
  84. is only partially tested, too, because it is dealing with cases that are very
  85. hard to provoke.
  86. It assumes that the local network is tolerably secure and that any accessible
  87. NTP or SNTP servers are trustworthy. It also makes no attempt to check that
  88. it has been installed and is being used correctly (e.g. at an appropriate
  89. priority) or that the changes it makes have the desired effect. When you first
  90. use it, you should both run it in display mode and use the date command as a
  91. cross-check.
  92. Furthermore, it does not attempt to solve all of the problems addressed by the
  93. NTP protocol and you should NOT use it if any of those problems are likely to
  94. cause you serious trouble. If they are, bite the bullet and implement ntp, or
  95. buy a fancy time-server.
  96. Building SNTP
  97. -------------
  98. The contents of the distribution are:
  99. README - this file
  100. Copyright - the copyright notice and conditions of use
  101. Makefile - the makefile, with comments for several systems
  102. header.h - the main header (almost entirely portable)
  103. kludges.h - dirty kludges for difficult systems
  104. internet.h - a very small header for internet.c and socket.c
  105. main.c - most of the source (almost entirely portable)
  106. unix.c - just for isatty, sleep and locking
  107. internet.c - Internet host and service name lookup
  108. socket.c - the Berkeley socket code
  109. sntp.1 - the man page
  110. RFC2030.TXT - the SNTPv4 specification
  111. All you SHOULD need to do is to uncomment the settings in file Makefile for
  112. your system or to add new ones. But real life is not always so simple. As
  113. POSIX does not yet define sub-second timers, Internet addressing facilities,
  114. sockets etc., the code has to rely on the facilities described in the
  115. ill-defined and non-standard 'X/Open' documents and the almost totally
  116. unspecified 'BSD' extensions.
  117. Most hacks should be limited to the compiler options (e.g. setting flags like
  118. _XOPEN_SOURCE), but perverse systems may need additions to kludges.h - please
  119. report them to the author. See Makefile and kludges.h for documentation on
  120. the standard hacks - there only 6, and most are only for obsolete systems.
  121. But, generally, using the generic set of C options usually works with no
  122. further ado.
  123. Sick, Bizarre or non-Unix Systems
  124. ---------------------------------
  125. A very few Unix systems and almost all non-Unix systems may need changes to the
  126. code, such as:
  127. If the system doesn't have Berkeley sockets, you will need to replace
  128. socket.c and possibly modify internet.h and internet.c. All of the
  129. systems for which the author needs this have Berkeley sockets.
  130. NTP is supposedly an Internet protocol, but is not Internet specific.
  131. For other types of network, you will need to replace internet.c and
  132. probably modify internet.h.
  133. If the system doesn't have gettimeofday or settimeofday, you will
  134. need to modify timing.c. If it doesn't have adjtime (e.g. HP-UX
  135. on PA-RISC before 10.0), you can set -DADJTIME_MISSING and the code
  136. will compile but the -a option will always give an error.
  137. If the system has totally broken signal handling, the program will
  138. hang or crash if it can't reach its name server or responses time
  139. out. You may be able to improve matters by hacking internet.c and
  140. socket.c, but don't bet on it.
  141. If the the program won't be able to create files in /etc when
  142. updating the clock, you can use another lock file or even set
  143. -DLOCKFILE=NULL, which will disable the locking code entirely. On
  144. systems that have it, using /var/run would be better than /etc.
  145. If the the program hangs when flushing outstanding packets (which
  146. you can tell by setting -W), it may help to set -DNONBLOCK_BROKEN.
  147. This seems needed only for obsolete systems, like Ultrix.
  148. If the system isn't Unix, even vaguely, you will probably need to
  149. modify all of the above, and unix.c as well.
  150. Note that adjtime is commonly sick, but you don't need to change the
  151. code - just use the -r option whan making large corrections (see below
  152. for more details).
  153. Any changes needed to header.h or main.c are bugs. They may be bugs in the
  154. code or in the compiler or libraries, but they are bugs. Please prod the
  155. people responsible and tell the author, who may be able to bypass them cleanly
  156. even if they aren't bugs in his code. The code also makes the following
  157. assumptions, which would be quite hard to remove:
  158. 8-bit bytes. Strictly, neither ANSI/ISO C nor POSIX require these,
  159. and there were some very early versions of Unix on systems with other
  160. byte sizes. But, without a defined sub-byte facility in C, ....
  161. At least 32-bit ints. Well, actually, this wouldn't be too hard to
  162. remove. But most Unix programs make this assumption, and I have very
  163. little interest in the more rudimentary versions of MS-DOS etc.
  164. An ANSI/ISO C compiler. It didn't seem worth writing dual-language
  165. code in 1996. Tough luck if you haven't got one.
  166. Tolerably efficient floating-point arithmetic, with at least 13 digits
  167. (decimal), preferably 15, in the mantissa of doubles. Ditto. If you
  168. want to port this to a toaster, please accept my insincerest sympathies
  169. and don't bother me.
  170. A trustworthy local network. It does not check for DNS, Ethernet,
  171. packet or other spoofing, and assumes that any accessible NTP or SNTP
  172. servers are properly synchronised.
  173. Warnings about Installation and Use
  174. -----------------------------------
  175. Anyone attempting to fiddle with the clock on their system should already know
  176. how to write system administration scripts, install daemons and so on. There
  177. are a few warnings:
  178. Don't use the broadcast modes unless you really have to, as the
  179. client-server modes are far more reliable. The broadcast modes were
  180. implemented more for virtuosity (a.k.a. SNTP conformance) than use.
  181. In particular, the error estimates are mere guesses, and may be low
  182. or even very low. And even reading broadcasts needs privilege.
  183. The program is not intended to be installed setuid or setgid, and
  184. doing so is asking for trouble. Its ownerships and access modes are
  185. not important. It need not be run by root for merely displaying the
  186. time (even in daemon mode).
  187. The program does not need to run at a high priority (low in Unix
  188. terms!) even when being used to set the clock or as a server, except
  189. when the '-r' option is used. However, doing so may improve its
  190. accuracy.
  191. Unlike NTP, the SNTP protocol contains no protection against
  192. client-server loops. If you set one up, your systems will spin
  193. themselves off into a disconnected vortex of unreality!
  194. It will get very confused if another process changes the local time
  195. while it is running. There is some locking code in unix.c to prevent
  196. this program doing this to itself, but it will protect only against
  197. some errors. However, the remaining failures should be harmless.
  198. Don't run it as a server unless you REALLY know what you are doing.
  199. It should be used as a server only on a system that is properly
  200. synchronised, by fair means or foul. If it isn't, you will simply
  201. perpetrate misinformation. And remember that broadcasts are most
  202. unpopular with overloaded administrators of overloaded networks.
  203. Watch out for multi-server broadcasts and systems with multiple ports
  204. onto the same Ethernet; there is some code to protect against this,
  205. but it is still easy to get confused.
  206. Don't put the lock file onto an automounted partition or delete it by
  207. hand, unless you really want to start two daemons at the same time.
  208. Both will probably fail horribly if you do this.
  209. The daemon save file is checked fairly carefully, but should be in a
  210. reasonably safe directory, unless you want hackers to cause trouble.
  211. /tmp is safe enough on most systems, but not all - /etc is better.
  212. Installing and Using the Program
  213. --------------------------------
  214. Start by copying the executable and man page to where you want them. If you
  215. want only to display the time and as a replacement for the rdate or date
  216. commands, the installation is finished!
  217. You can use it as a simple unprivileged command to check the time, quite
  218. independently of whether it is running as a time-updating daemon or server, or
  219. whether you are running ntp. You can run it in daemon mode without updating
  220. the clock, to check for drift, but it may fail if the clock is changed under
  221. its feet. Unfortunately, you cannot listen to broadcasts without privilege.
  222. If it is used with the -a option to keep the time synchronised, it is best to
  223. run it as one of root's cron jobs - for many systems, running it once a day
  224. should be adequate, but it will depend on the reliability of the local clock.
  225. The author runs it this way with -a and -x - see below.
  226. If it is used with the -r option to set the time (instead of the rdate or date
  227. commands), it should be used interactively and either on a lightly loaded
  228. system or at a high priority. You should then check the result by running it
  229. in display mode.
  230. You are advised NOT to run it with the -r option in a cron job, though this is
  231. not locked out. If you have to (for example under HP-UX before 10.0), be sure
  232. to run it as the highest priority that will not cause other system problems and
  233. set the maximum automatic change to as low a value as you can get away with.
  234. WARNING: adjtime is more than a bit sick on many systems, and will ignore large
  235. corrections, usually without any form of hint that it has done so. It is often
  236. (even usually) necessary to reset the clock to approximately the right time
  237. using the -r option before using the -a and -x options to keep it correct.
  238. It can be started as a time-updating daemon with the -a and -x options (or -r
  239. and -x if you must), and will perform some limited drift correction. In this
  240. case, start it from any suitable system initialisation script and leave it
  241. running. Note that it will stop if it thinks that the time difference or drift
  242. has got out of control, and you will need to reset the time and restart it by
  243. hand.
  244. In daemon mode, it will survive its time server or network disappearing for a
  245. while, but will eventually fail, and will fail immediately if the network call
  246. returns an unexpected error. If this is a problem, you can start it (say,
  247. hourly or nightly) from cron, and it will fail if it is already running
  248. (provided that you haven't disabled or deleted the lock file).
  249. If it is used as a server, it should be started from any suitable system
  250. initialisation script, just like any other daemon. It must be started after
  251. the networking, of course. To run it in both server modes, start one copy with
  252. the -B option and one with the -S option.
  253. Simple Examples of Use
  254. ----------------------
  255. Many people use it solely to check the time of their system, especially as a
  256. cross-check on ntpd. You do not need privilege and it will not cause trouble
  257. to the local network, so you can use it on someone else's system! You can
  258. specify one server or several. For example:
  259. msntp ntp.server.local ntp.server.neighbour
  260. You can use it to check how your system is drifting, but it isn't very good at
  261. this if the system is drifting very badly (in which case use the previous
  262. technique and dc) or if you are running ntp. You do not need privilege and it
  263. will not cause trouble to the local network. For example:
  264. sntp -x 120 -f /tmp/msntp.state ntp.server.local
  265. More generally, it is used to synchronise the clock, in which case you DO need
  266. root privilege. It can be used in many ways, but the author favours running it
  267. in daemon mode, started from a cron job, which will restart after power cuts
  268. with no attention, and send a mail message (if cron is configured to do that)
  269. when it fails badly. For example, the author uses a root crontab entry on one
  270. system of:
  271. 15 0 * * * /bin/nice --10 /usr/local/bin/sntp -a -x 480 ntp.server.local
  272. If you have a home computer, it can be set up to resynchronise each time you
  273. dial up. For example, the author uses a /etc/ppp/ip-up.d/sntp file on his
  274. home Linux system of:
  275. #!/bin/sh
  276. sleep 60
  277. /bin/nice --10 /usr/local/sbin/sntp -r -P 60 ntp.server.local
  278. -a would be better, but adjtime is broken in Linux.
  279. Debugging or Hacking the Program
  280. --------------------------------
  281. Almost everybody who does this is likely to need to modify only the system
  282. interfaces. While they are messy, they are pretty simple and have a simple
  283. specification. This is documented in comments in the source. This is
  284. described above.
  285. The main program SHOULD need no attention, though it may need the odd tweak to
  286. bypass compiler problems - please report these, if you encounter any. If
  287. something looks odd while it is running, start by setting the -v option (lower
  288. case), as for investigating network problems, and checking any diagnostics that
  289. appear. Note that most of it can be checked in display mode without harming
  290. your system.
  291. The client will sometimes give up, complaining about inconsistent timestamps or
  292. similar. This can be caused by the server being rebooted and similar glitches
  293. to the time - unfortunately, there is no reliable way to tell an ignorable
  294. fluctuation from a server up the spout. If this happens annoyingly often,
  295. the -V option may help tie down the problem. In actual use, it is simplest
  296. just to restart the client in a cron job!
  297. If it needs more than this, then you will need to debug the source seriously.
  298. Start by putting an icepack on your head and pouring yourself a large whisky!
  299. While it is commented, it is not well commented, and much of the code interacts
  300. in complex and horrible ways. This isn't so much because it lacks 'structure'
  301. as because one part needs to make assumptions about the numerical properties of
  302. another.
  303. The -W option (upper case) will print out a complete trace of everything it
  304. does, and this should be enough to tie down the problem. It does distort the
  305. timing a bit, but not usually too badly. However, wading through that amount
  306. of gibberish (let alone looking at the source) is not a pleasant task. If you
  307. are pretty sure that you have a bug, you may tell the author, and he may ask
  308. for a copy of the output - but he will reply rudely if you send thousands of
  309. lines of tracing to him by Email!
  310. Note that there are a fair number of circumstances where its error recovery
  311. could be better, but is left as it is to keep the code simple. Most of these
  312. should be pretty rare.
  313. Changes in Version 1.2
  314. ----------------------
  315. The main change was the addition of the daemon mode for drift correction (i.e.
  316. the -x option). The daemon code is complex and has a lot of special-casing for
  317. strange circumstances, not all of which are testable in practice.
  318. A lot of the code was reordered while doing this. The output was slightly
  319. different - considerably different with -V.
  320. The error estimation for broadcasts was modified, and should bear more relation
  321. to reality. It remains a guess, as there is no way to get decent error error
  322. estimates under such circumstances.
  323. The -B option is now in minutes, and has a different permissible range and
  324. default value.
  325. The argument consistency checking for broadcasts was tightened up a bit, and a
  326. few other internal checks added. These should not affect any reasonable
  327. requirement.
  328. A couple of new functions were added to the portability base, but they don't
  329. use any non-standard new facilities. However, the specification of the
  330. functions has changed slightly.
  331. Changes in Version 1.3
  332. ----------------------
  333. The main change was the addition of the restarting facility for daemon mode
  334. (i.e. the -f option), which is pretty straightforward.
  335. There were also a lot of minor changes to the paranoia code in daemon mode, to
  336. try to separate out the case of a demented server from network and other
  337. 'ignorable' problems. These are not entirely successful.
  338. Changes in Version 1.4 and 1.5
  339. ------------------------------
  340. There turned out to be a couple of places where the author misunderstood the
  341. specification of NTP, which affect only its use in server mode. The main
  342. change is to use stratum 15 instead of stratum 0.
  343. And there were some more relaxations of the paranoia code, to allow for more
  344. erratic servers, plus a kludge to improve restarting in daemon mode after a
  345. period of down time has unsynchronised the clock. There is also an
  346. incompatible change to the debugging options to add a new level - the old -V
  347. option is now -W, and -V is an intermediate one for debugging daemon mode - but
  348. they are both hacker's facilities, and not for normal use.
  349. Version 1.5 adds some very minor fixes.
  350. Changes in Version 1.6
  351. ----------------------
  352. The first change is support for multiple server addresses - it uses these in a
  353. round-robin fashion. This may be useful when you have access to several
  354. servers, all of which are a bit iffy. This means that the restart file format
  355. is incompatible with msntp 1.5.
  356. It has also been modified to reset itself automatically after detecting an
  357. inconsistency in its server's timestamps, because the author got sick of the
  358. failures. It writes a comment to syslog (uniquely) in such cases.
  359. The ability to query a daemon save file was added.
  360. Related to the above, the -E argument has been redefined to mean an error bound
  361. on various internal times (which is what it had become, anyway) and a -P option
  362. introduced to be what the -E argument was documented to be.
  363. The lock and save file handling have been changed to allow defaults to be set
  364. at installation time, and to be overridable at run-time. To disable these
  365. at either stage, simply set the file names to the null string.
  366. And there have been the usual changes for portability, as standards have been
  367. modified and/or introduced.
  368. Future Versions
  369. ---------------
  370. There are unlikely to be any, except probably one to fix bugs in version 1.6.
  371. I attempted to put support for intermittent connexions (e.g. dial-up) into the
  372. daemon mode, but doing so needs so much code reorganisation that it isn't worth
  373. it. What needs doing for that is to separate the socket handling from the
  374. timekeeping, so that they can be run asynchronously (either in separate
  375. processes or threads), and to look up a network name and open a socket only
  376. when prodded (and to close it immediately thereafter). So just running it
  377. with the -r option is the current best solution.
  378. I also attempted to put support for the "Unix 2000" interfaces into the code.
  379. Ha, ha. Not merely do very few systems define socklen_t (needed for IPv6
  380. support), but "Unix 2000" neither addresses the leap second problem nor even
  381. provides an adjtime replacement! Some function like the latter is critical,
  382. not so much because of the gradual change, but because of its atomicity;
  383. without it, msntp really needs to be made non-interruptible, and that brings in
  384. a ghastly number of system-dependencies.
  385. Realistically, it needs a complete rewrite before adding any more function.
  386. And, worse, the Unix 'standards' need fixing, too.
  387. Miscellaneous
  388. -------------
  389. Thanks are due to Douglas M. Wells of Connection Technologies for helping the
  390. author with several IP-related conventions, to Sam Nelson of Stirling
  391. University for testing it on some very strange systems, and to David Mills for
  392. clarifying what the NTP specification really is.
  393. Thanks are also due to several other people with locating bugs, finding
  394. appropriate options for the Makefile and passing on extension code and
  395. suggestions. As I am sure to leave someone out, I shall not name anyone else.
  396. Version 1.0 - October 1996.
  397. Version 1.1 - November 1996 - mainly portability improvements.
  398. Version 1.2 - January 1997 - mainly drift handling, but much reorganisation.
  399. Version 1.3 - February 1997 - daemon save file, and some robustness changes.
  400. Version 1.4 - May 1997 - relatively minor fixes, more diagnostic levels etc.
  401. Version 1.5 - December 1997 - some very minor fixes
  402. Version 1.6 - October 2000 - quite a few miscellaneous changes
  403. Nick Maclaren,
  404. University of Cambridge Computer Laboratory,
  405. New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
  406. Email: nmm1@cam.ac.uk
  407. Tel.: +44 1223 334761 Fax: +44 1223 334679