Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/ntpd/cmd_args.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * cmd_args.c = command-line argument processing
3
 */
4
#ifdef HAVE_CONFIG_H
5
# include <config.h>
6
#endif
7
8
#include "ntpd.h"
9
#include "ntp_stdlib.h"
10
#include "ntp_config.h"
11
#include "ntp_cmdargs.h"
12
13
#include "ntpd-opts.h"
14
15
/*
16
 * Definitions of things either imported from or exported to outside
17
 */
18
extern char const *progname;
19
20
#ifdef HAVE_NETINFO
21
extern int  check_netinfo;
22
#endif
23
24
25
/*
26
 * getCmdOpts - apply most command line options
27
 *
28
 * A few options are examined earlier in ntpd.c ntpdmain() and
29
 * ports/winnt/ntpd/ntservice.c main().
30
 */
31
void
32
getCmdOpts(
33
  int argc,
34
  char ** argv
35
  )
36
0
{
37
0
  extern const char *config_file;
38
0
  int errflg;
39
40
  /*
41
   * Initialize, initialize
42
   */
43
0
  errflg = 0;
44
45
0
  if (ipv4_works && ipv6_works) {
46
0
    if (HAVE_OPT( IPV4 ))
47
0
      ipv6_works = 0;
48
0
    else if (HAVE_OPT( IPV6 ))
49
0
      ipv4_works = 0;
50
0
  } else if (!ipv4_works && !ipv6_works) {
51
0
    msyslog(LOG_ERR, "Neither IPv4 nor IPv6 networking detected, fatal.");
52
0
    exit(1);
53
0
  } else if (HAVE_OPT( IPV4 ) && !ipv4_works)
54
0
    msyslog(LOG_WARNING, "-4/--ipv4 ignored, IPv4 networking not found.");
55
0
  else if (HAVE_OPT( IPV6 ) && !ipv6_works)
56
0
    msyslog(LOG_WARNING, "-6/--ipv6 ignored, IPv6 networking not found.");
57
58
0
  if (HAVE_OPT( AUTHREQ ))
59
0
    proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
60
0
  else if (HAVE_OPT( AUTHNOREQ ))
61
0
    proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
62
63
0
  if (HAVE_OPT( BCASTSYNC ))
64
0
    proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
65
66
0
  if (HAVE_OPT( CONFIGFILE )) {
67
0
    config_file = OPT_ARG( CONFIGFILE );
68
#ifdef HAVE_NETINFO
69
    check_netinfo = 0;
70
#endif
71
0
  }
72
73
0
  if (HAVE_OPT( DRIFTFILE ))
74
0
    stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
75
76
0
  if (HAVE_OPT( PANICGATE ))
77
0
    allow_panic = TRUE;
78
79
0
  if (HAVE_OPT( FORCE_STEP_ONCE ))
80
0
    force_step_once = TRUE;
81
82
#ifdef HAVE_DROPROOT
83
  if (HAVE_OPT( JAILDIR )) {
84
    droproot = 1;
85
    chrootdir = OPT_ARG( JAILDIR );
86
  }
87
#endif
88
89
0
  if (HAVE_OPT( KEYFILE ))
90
0
    getauthkeys(OPT_ARG( KEYFILE ));
91
92
0
  if (HAVE_OPT( PIDFILE ))
93
0
    stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
94
95
0
  if (HAVE_OPT( QUIT ))
96
0
    mode_ntpdate = TRUE;
97
98
0
  if (HAVE_OPT( PROPAGATIONDELAY ))
99
0
    do {
100
0
      double tmp;
101
0
      const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
102
103
0
      if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
104
0
        msyslog(LOG_ERR,
105
0
          "command line broadcast delay value %s undecodable",
106
0
          my_ntp_optarg);
107
0
      } else {
108
0
        proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
109
0
      }
110
0
    } while (0);
111
112
0
  if (HAVE_OPT( STATSDIR ))
113
0
    stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
114
115
0
  if (HAVE_OPT( TRUSTEDKEY )) {
116
0
    int   ct = STACKCT_OPT(  TRUSTEDKEY );
117
0
    const char**  pp = STACKLST_OPT( TRUSTEDKEY );
118
119
0
    do  {
120
0
      u_long tkey;
121
0
      const char* p = *pp++;
122
123
0
      tkey = (int)atol(p);
124
0
      if (tkey == 0 || tkey > NTP_MAXKEY) {
125
0
        msyslog(LOG_ERR,
126
0
            "command line trusted key %s is invalid",
127
0
            p);
128
0
      } else {
129
0
        authtrust(tkey, 1);
130
0
      }
131
0
    } while (--ct > 0);
132
0
  }
133
134
#ifdef HAVE_DROPROOT
135
  if (HAVE_OPT( USER )) {
136
    droproot = 1;
137
    user = estrdup(OPT_ARG( USER ));
138
    group = strrchr(user, ':');
139
    if (group != NULL) {
140
      size_t  len;
141
142
      *group++ = '\0'; /* get rid of the ':' */
143
      len = group - user;
144
      group = estrdup(group);
145
      user = erealloc(user, len);
146
    }
147
  }
148
#endif
149
150
0
  if (HAVE_OPT( VAR )) {
151
0
    int   ct;
152
0
    const char ** pp;
153
0
    const char *  v_assign;
154
155
0
    ct = STACKCT_OPT(  VAR );
156
0
    pp = STACKLST_OPT( VAR );
157
158
0
    do  {
159
0
      v_assign = *pp++;
160
0
      set_sys_var(v_assign, strlen(v_assign) + 1, RW);
161
0
    } while (--ct > 0);
162
0
  }
163
164
0
  if (HAVE_OPT( DVAR )) {
165
0
    int   ct = STACKCT_OPT(  DVAR );
166
0
    const char**  pp = STACKLST_OPT( DVAR );
167
168
0
    do  {
169
0
      const char* my_ntp_optarg = *pp++;
170
171
0
      set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
172
0
          (u_short) (RW | DEF));
173
0
    } while (--ct > 0);
174
0
  }
175
176
0
  if (HAVE_OPT( SLEW ))
177
0
    loop_config(LOOP_MAX, 600);
178
179
0
  if (HAVE_OPT( UPDATEINTERVAL )) {
180
0
    long val = OPT_VALUE_UPDATEINTERVAL;
181
182
0
    if (val >= 0)
183
0
      interface_interval = val;
184
0
    else {
185
0
      fprintf(stderr,
186
0
        "command line interface update interval %ld must not be negative\n",
187
0
        val);
188
0
      msyslog(LOG_ERR,
189
0
        "command line interface update interval %ld must not be negative",
190
0
        val);
191
0
      errflg++;
192
0
    }
193
0
  }
194
195
196
  /* save list of servers from cmd line for config_peers() use */
197
0
  if (argc > 0) {
198
0
    cmdline_server_count = argc;
199
0
    cmdline_servers = argv;
200
0
  }
201
202
  /* display usage & exit with any option processing errors */
203
0
  if (errflg)
204
0
    optionUsage(&ntpdOptions, 2); /* does not return */
205
0
}