It's about time, my first c++ program

Wrote my first c++ program. Though it doesn’t answer the question I had, it does provide some nice information.

Here is the code:

/** Project Name: ntp_project
* Written By: sparkz_alot
* Date: 19 Jun 2014
* Version: 1.0.0
*
* Description: a simple c++11 program to provide information about the Network Time Protocol (ntp) using the
* ntpq and ntpdc query commands.
*
* Written using Code::Blocks v13.12 IDE and tested on openSUSE 13.1.
*/
#include <iostream>


using namespace std;


int main()


{
    cout << "The results of the ntpq -pn (-p: list of peers known to server 
";
    cout << "-n: use numerical rather than canonical name) and a straight system call 
";
    cout << endl;
    system("ntpq -pn");
    cout << endl;
    // or using popen. Note: 'type' is r(read) or w(write), cant't be both
    cout << "Output using popen and ntpq
";
    FILE *pipe_fp;
    char buff[512];
    // Open pipe and check for errors
    if ((pipe_fp = popen("ntpq -pn" , "r")) == NULL)
        {
            perror("popen");
            exit(1);
        }
    while(fgets(buff, sizeof(buff), pipe_fp)!=NULL)
    {
        cout << buff;
    }
    // Close the pipe
    pclose(pipe_fp);
    cout << endl;
    // Now lets get some info about the server we are currently syncing to
    cout << "Getting system info of current ntp server using ntpdc -sysinfo 
";
    cout << endl;
    if ((pipe_fp = popen("ntpdc -c sysinfo" , "r")) == NULL)
        {
            perror("popen");
            exit(1);
        }
    while(fgets(buff, sizeof(buff), pipe_fp)!=NULL)
    {
        cout << buff;
    }
    // Close the pipe
    pclose(pipe_fp);
    cout << endl;
    // Say good night Gracie
    cout << "\a tata" << endl;


    return 0;
}

And here is the output:

The results of the ntpq -pn (-p: list of peers known to server -n: use numerical rather than canonical name) and a straight system call 


     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-4.53.160.75     64.113.32.5      2 u  675 1024  377   57.563   -9.733   3.646
+50.7.64.4       206.108.0.132    2 u  370 1024  377   48.876   -0.282   2.890
*66.228.59.187   200.98.196.212   2 u  678 1024  367   26.619   -2.186   1.712
+66.7.96.1       140.142.16.34    2 u  613 1024  377  116.687   -8.288   2.773


Output using popen and ntpq
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-4.53.160.75     64.113.32.5      2 u  675 1024  377   57.563   -9.733   3.646
+50.7.64.4       206.108.0.132    2 u  370 1024  377   48.876   -0.282   2.890
*66.228.59.187   200.98.196.212   2 u  678 1024  367   26.619   -2.186   1.712
+66.7.96.1       140.142.16.34    2 u  613 1024  377  116.687   -8.288   2.773


Getting system info of current ntp server using ntpdc -sysinfo 


system peer:          fairy.mattnordhoff.net
system peer mode:     client
leap indicator:       00
stratum:              3
precision:            -22
root distance:        0.05386 s
root dispersion:      0.10902 s
reference ID:         [66.228.59.187]
reference time:       d74d8d32.6dcf68bd  Thu, Jun 19 2014 12:24:18.428
system flags:         auth monitor ntp kernel stats 
jitter:               0.005585 s
stability:            0.000 ppm
broadcastdelay:       0.000000 s
authdelay:            0.000000 s


 tata



On Thu 19 Jun 2014 08:16:01 PM CDT, sparkz alot wrote:

Wrote my first c++ program. Though it doesn’t answer the question I had,
it does provide some nice information.

Hi
Does your app still work with respect to CVE-2013-5211 and
http://lists.opensuse.org/opensuse-security-announce/2014-01/msg00005.html

As in adding “restrict default noquery” line entry to your ntp.conf?

Add to your code the line to test;


ntpdc -n -c monlist 127.0.0.1

Then you could pop up a warning to say the system is vulnerable.


Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.1 Kernel 3.11.10-11-desktop
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!

Thanks for the information Malcolm, was not aware of this bug(?). Tried from the command line:

I am Root: ntpdc -n -c monlist 127.0.0.1
remote address          port local address      count m ver rstr avgint  lstint
===============================================================================
166.70.136.35            123 192.168.10.3          43 4 4      0     54       3
216.66.0.142             123 192.168.10.3          43 4 4      0     54       7
208.79.89.249            123 192.168.10.3          42 4 4      0     55      38
50.116.55.65             123 192.168.10.3          42 4 4      0     55      63

As well as another they recommended:

I am Root: ntpq -c rv
associd=0 status=0615 leap_none, sync_ntp, 1 event, clock_sync,
**version="ntpd 4.2.6p5@1.2349-o **Wed Oct 23 08:21:35 UTC 2013 (1)",
processor="x86_64", system="Linux/3.11.10-11-desktop", leap=00,
stratum=3, precision=-22, rootdelay=25.114, rootdisp=50.830,
refid=216.66.0.142,
reftime=d74ea4f7.31f42b01  Fri, Jun 20 2014  8:17:59.195,
clock=d74ea545.c320c508  Fri, Jun 20 2014  8:19:17.762, peer=10467, tc=7,
mintc=3, offset=0.078, frequency=-9.922, sys_jitter=0.560,
clk_jitter=1.146, clk_wander=0.211

As you can see, the version is less than that recommended. Perhaps openSUSE just hasn’t gotten around to it yet? Or did I miss an update? Anyway, made the change you recommended to my /etc/ntp.conf file, restarted the service and reran the command with the following results:

I am Root: ntpdc -n -c monlist 127.0.0.1
127.0.0.1: timed out, nothing received
***Request timed out

I will add the ‘test’ to my program this weekend. Thanks again for the heads up.

Hi
There is no update, the ntp.conf entry is the fix, hence it’s good to check for :wink: