On Saturday 22 March 2014 23:46, Isaac Dunham wrote:
> On Sat, Mar 22, 2014 at 08:40:48PM +0100, Harald Becker wrote:
> > Hi Isaac !
> > 
> > Your program will fail on lines starting with the word server
> > (eg. serverxyz), that is it does not check for clear word
> > boundary and gives wrong results in that case.
> 
> ...which are not legitimate entries in ntp.conf.
> 
> My aim is to parse a correct ntp.conf, and not cause security problems
> on incorrect ones.

bbox has config parsing routines to avoid coding this again and again.

How about this?

function                                             old     new   delta
add_peers                                              -      98     +98
packed_usage                                       29470   29511     +41
ntp_init                                             407     428     +21
pw_encrypt                                            14      27     +13
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/0 up/down: 173/0)             Total: 173 bytes

diff -ad -urpN busybox.5/networking/Config.src busybox.6/networking/Config.src
--- busybox.5/networking/Config.src     2014-02-09 09:48:42.000000000 +0100
+++ busybox.6/networking/Config.src     2014-03-23 14:35:04.000000000 +0100
@@ -664,6 +664,14 @@ config FEATURE_NTPD_SERVER
          Make ntpd usable as a NTP server. If you disable this option
          ntpd will be usable only as a NTP client.
 
+config FEATURE_NTPD_CONF
+       bool "Make ntpd understand /etc/ntp.conf"
+       default y
+       depends on NTPD
+       help
+         Make ntpd look in /etc/ntp.conf for peers. Only "server address"
+         is supported.
+
 config PSCAN
        bool "pscan"
        default y
diff -ad -urpN busybox.5/networking/ntpd.c busybox.6/networking/ntpd.c
--- busybox.5/networking/ntpd.c 2014-02-09 15:33:14.000000000 +0100
+++ busybox.6/networking/ntpd.c 2014-03-23 15:01:55.000000000 +0100
@@ -42,6 +42,13 @@
 //usage:       )
 //usage:     "\n       -S PROG Run PROG after stepping time, stratum change, 
and every 11 mins"
 //usage:     "\n       -p PEER Obtain time from PEER (may be repeated)"
+//usage:       IF_FEATURE_NTPD_CONF(
+//usage:     "\n               If -p is not given, read /etc/ntp.conf"
+//usage:       )
+
+// -l and -p options are not compatible with "standard" ntpd:
+// it has them as "-l logfile" and "-p pidfile".
+// -S and -w are not compat either, "standard" ntpd has no such opts.
 
 #include "libbb.h"
 #include <math.h>
@@ -730,7 +737,7 @@ reset_peer_stats(peer_t *p, double offse
 }
 
 static void
-add_peers(char *s)
+add_peers(const char *s)
 {
        peer_t *p;
 
@@ -2087,14 +2094,34 @@ static NOINLINE void ntp_init(char **arg
                        "d" /* compat */
                        "46aAbgL", /* compat, ignored */
                        &peers, &G.script_name, &G.verbose);
-       if (!(opts & (OPT_p|OPT_l)))
-               bb_show_usage();
+
 //     if (opts & OPT_x) /* disable stepping, only slew is allowed */
 //             G.time_was_stepped = 1;
        if (peers) {
                while (peers)
                        add_peers(llist_pop(&peers));
-       } else {
+       }
+#if ENABLE_FEATURE_NTPD_CONF
+       else {
+               parser_t *parser;
+               char *token[3];
+
+               parser = config_open("/etc/ntp.conf");
+               while (config_read(parser, token, 3, 1, "# \t", PARSE_NORMAL)) {
+                       if (strcmp(token[0], "server") == 0 && token[1]) {
+                               add_peers(token[1]);
+                               continue;
+                       }
+                       bb_error_msg("skipping %s:%u: unimplemented command 
'%s'",
+                               "/etc/ntp.conf", parser->lineno, token[0]
+                       );
+               }
+               config_close(parser);
+       }
+#endif
+       if (G.peer_cnt == 0) {
+               if (!(opts & OPT_l))
+                       bb_show_usage();
                /* -l but no peers: "stratum 1 server" mode */
                G.stratum = 1;
        }
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to