1224ba2bdSOllivier Robert /*
2224ba2bdSOllivier Robert * cmd_args.c = command-line argument processing
3224ba2bdSOllivier Robert */
4224ba2bdSOllivier Robert #ifdef HAVE_CONFIG_H
5224ba2bdSOllivier Robert # include <config.h>
6224ba2bdSOllivier Robert #endif
7224ba2bdSOllivier Robert
8224ba2bdSOllivier Robert #include "ntpd.h"
9224ba2bdSOllivier Robert #include "ntp_stdlib.h"
102b15cb3dSCy Schubert #include "ntp_config.h"
11224ba2bdSOllivier Robert #include "ntp_cmdargs.h"
12224ba2bdSOllivier Robert
13ea906c41SOllivier Robert #include "ntpd-opts.h"
149c2daa00SOllivier Robert
15224ba2bdSOllivier Robert /*
16224ba2bdSOllivier Robert * Definitions of things either imported from or exported to outside
17224ba2bdSOllivier Robert */
18224ba2bdSOllivier Robert extern char const *progname;
19224ba2bdSOllivier Robert
20224ba2bdSOllivier Robert #ifdef HAVE_NETINFO
21224ba2bdSOllivier Robert extern int check_netinfo;
22224ba2bdSOllivier Robert #endif
23224ba2bdSOllivier Robert
24224ba2bdSOllivier Robert
25224ba2bdSOllivier Robert /*
262b15cb3dSCy Schubert * getCmdOpts - apply most command line options
272b15cb3dSCy Schubert *
282b15cb3dSCy Schubert * A few options are examined earlier in ntpd.c ntpdmain() and
292b15cb3dSCy Schubert * ports/winnt/ntpd/ntservice.c main().
30224ba2bdSOllivier Robert */
31224ba2bdSOllivier Robert void
getCmdOpts(int argc,char ** argv)32224ba2bdSOllivier Robert getCmdOpts(
33224ba2bdSOllivier Robert int argc,
342b15cb3dSCy Schubert char ** argv
35224ba2bdSOllivier Robert )
36224ba2bdSOllivier Robert {
37ea906c41SOllivier Robert extern const char *config_file;
38224ba2bdSOllivier Robert int errflg;
39224ba2bdSOllivier Robert
40224ba2bdSOllivier Robert /*
41224ba2bdSOllivier Robert * Initialize, initialize
42224ba2bdSOllivier Robert */
43224ba2bdSOllivier Robert errflg = 0;
44224ba2bdSOllivier Robert
452b15cb3dSCy Schubert if (ipv4_works && ipv6_works) {
462b15cb3dSCy Schubert if (HAVE_OPT( IPV4 ))
472b15cb3dSCy Schubert ipv6_works = 0;
482b15cb3dSCy Schubert else if (HAVE_OPT( IPV6 ))
492b15cb3dSCy Schubert ipv4_works = 0;
502b15cb3dSCy Schubert } else if (!ipv4_works && !ipv6_works) {
512b15cb3dSCy Schubert msyslog(LOG_ERR, "Neither IPv4 nor IPv6 networking detected, fatal.");
522b15cb3dSCy Schubert exit(1);
532b15cb3dSCy Schubert } else if (HAVE_OPT( IPV4 ) && !ipv4_works)
542b15cb3dSCy Schubert msyslog(LOG_WARNING, "-4/--ipv4 ignored, IPv4 networking not found.");
552b15cb3dSCy Schubert else if (HAVE_OPT( IPV6 ) && !ipv6_works)
562b15cb3dSCy Schubert msyslog(LOG_WARNING, "-6/--ipv6 ignored, IPv6 networking not found.");
57224ba2bdSOllivier Robert
58ea906c41SOllivier Robert if (HAVE_OPT( AUTHREQ ))
599c2daa00SOllivier Robert proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
602b15cb3dSCy Schubert else if (HAVE_OPT( AUTHNOREQ ))
619c2daa00SOllivier Robert proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
62224ba2bdSOllivier Robert
63ea906c41SOllivier Robert if (HAVE_OPT( BCASTSYNC ))
649c2daa00SOllivier Robert proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
65224ba2bdSOllivier Robert
66ea906c41SOllivier Robert if (HAVE_OPT( CONFIGFILE )) {
67ea906c41SOllivier Robert config_file = OPT_ARG( CONFIGFILE );
68224ba2bdSOllivier Robert #ifdef HAVE_NETINFO
69224ba2bdSOllivier Robert check_netinfo = 0;
70224ba2bdSOllivier Robert #endif
71ea906c41SOllivier Robert }
72224ba2bdSOllivier Robert
73ea906c41SOllivier Robert if (HAVE_OPT( DRIFTFILE ))
74a466cc55SCy Schubert stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ), 1);
75224ba2bdSOllivier Robert
76ea906c41SOllivier Robert if (HAVE_OPT( PANICGATE ))
77224ba2bdSOllivier Robert allow_panic = TRUE;
78224ba2bdSOllivier Robert
79a25439b6SCy Schubert if (HAVE_OPT( FORCE_STEP_ONCE ))
80a25439b6SCy Schubert force_step_once = TRUE;
81a25439b6SCy Schubert
82ea906c41SOllivier Robert #ifdef HAVE_DROPROOT
832b15cb3dSCy Schubert if (HAVE_OPT( JAILDIR )) {
84ea906c41SOllivier Robert droproot = 1;
85ea906c41SOllivier Robert chrootdir = OPT_ARG( JAILDIR );
86ea906c41SOllivier Robert }
872b15cb3dSCy Schubert #endif
88224ba2bdSOllivier Robert
89ea906c41SOllivier Robert if (HAVE_OPT( KEYFILE ))
90ea906c41SOllivier Robert getauthkeys(OPT_ARG( KEYFILE ));
91224ba2bdSOllivier Robert
92ea906c41SOllivier Robert if (HAVE_OPT( PIDFILE ))
93a466cc55SCy Schubert stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ), 1);
94224ba2bdSOllivier Robert
95ea906c41SOllivier Robert if (HAVE_OPT( QUIT ))
96224ba2bdSOllivier Robert mode_ntpdate = TRUE;
97224ba2bdSOllivier Robert
98ea906c41SOllivier Robert if (HAVE_OPT( PROPAGATIONDELAY ))
99224ba2bdSOllivier Robert do {
100224ba2bdSOllivier Robert double tmp;
101ea906c41SOllivier Robert const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
102224ba2bdSOllivier Robert
103ea906c41SOllivier Robert if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
104224ba2bdSOllivier Robert msyslog(LOG_ERR,
105224ba2bdSOllivier Robert "command line broadcast delay value %s undecodable",
106ea906c41SOllivier Robert my_ntp_optarg);
107224ba2bdSOllivier Robert } else {
1089c2daa00SOllivier Robert proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
109224ba2bdSOllivier Robert }
110224ba2bdSOllivier Robert } while (0);
111224ba2bdSOllivier Robert
112ea906c41SOllivier Robert if (HAVE_OPT( STATSDIR ))
113a466cc55SCy Schubert stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ), 1);
114ea906c41SOllivier Robert
115ea906c41SOllivier Robert if (HAVE_OPT( TRUSTEDKEY )) {
116ea906c41SOllivier Robert int ct = STACKCT_OPT( TRUSTEDKEY );
117ea906c41SOllivier Robert const char** pp = STACKLST_OPT( TRUSTEDKEY );
118ea906c41SOllivier Robert
119ea906c41SOllivier Robert do {
120ea906c41SOllivier Robert u_long tkey;
121ea906c41SOllivier Robert const char* p = *pp++;
122ea906c41SOllivier Robert
123ea906c41SOllivier Robert tkey = (int)atol(p);
124ea906c41SOllivier Robert if (tkey == 0 || tkey > NTP_MAXKEY) {
125ea906c41SOllivier Robert msyslog(LOG_ERR,
126ea906c41SOllivier Robert "command line trusted key %s is invalid",
127ea906c41SOllivier Robert p);
128ea906c41SOllivier Robert } else {
129ea906c41SOllivier Robert authtrust(tkey, 1);
130ea906c41SOllivier Robert }
131ea906c41SOllivier Robert } while (--ct > 0);
132ea906c41SOllivier Robert }
133ea906c41SOllivier Robert
134ea906c41SOllivier Robert #ifdef HAVE_DROPROOT
1352b15cb3dSCy Schubert if (HAVE_OPT( USER )) {
136ea906c41SOllivier Robert droproot = 1;
1372b15cb3dSCy Schubert user = estrdup(OPT_ARG( USER ));
1382b15cb3dSCy Schubert group = strrchr(user, ':');
1392b15cb3dSCy Schubert if (group != NULL) {
1402b15cb3dSCy Schubert size_t len;
1412b15cb3dSCy Schubert
1429c2daa00SOllivier Robert *group++ = '\0'; /* get rid of the ':' */
1432b15cb3dSCy Schubert len = group - user;
1442b15cb3dSCy Schubert group = estrdup(group);
1452b15cb3dSCy Schubert user = erealloc(user, len);
146224ba2bdSOllivier Robert }
1472b15cb3dSCy Schubert }
1482b15cb3dSCy Schubert #endif
149224ba2bdSOllivier Robert
150ea906c41SOllivier Robert if (HAVE_OPT( VAR )) {
1512b15cb3dSCy Schubert int ct;
1522b15cb3dSCy Schubert const char ** pp;
1532b15cb3dSCy Schubert const char * v_assign;
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert ct = STACKCT_OPT( VAR );
1562b15cb3dSCy Schubert pp = STACKLST_OPT( VAR );
157224ba2bdSOllivier Robert
158ea906c41SOllivier Robert do {
1592b15cb3dSCy Schubert v_assign = *pp++;
1602b15cb3dSCy Schubert set_sys_var(v_assign, strlen(v_assign) + 1, RW);
161ea906c41SOllivier Robert } while (--ct > 0);
162ea906c41SOllivier Robert }
163ea906c41SOllivier Robert
164ea906c41SOllivier Robert if (HAVE_OPT( DVAR )) {
165ea906c41SOllivier Robert int ct = STACKCT_OPT( DVAR );
166ea906c41SOllivier Robert const char** pp = STACKLST_OPT( DVAR );
167ea906c41SOllivier Robert
168ea906c41SOllivier Robert do {
169ea906c41SOllivier Robert const char* my_ntp_optarg = *pp++;
170ea906c41SOllivier Robert
171ea906c41SOllivier Robert set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
172ea906c41SOllivier Robert (u_short) (RW | DEF));
173ea906c41SOllivier Robert } while (--ct > 0);
174ea906c41SOllivier Robert }
175ea906c41SOllivier Robert
176ea906c41SOllivier Robert if (HAVE_OPT( SLEW ))
1772b15cb3dSCy Schubert loop_config(LOOP_MAX, 600);
178ea906c41SOllivier Robert
179ea906c41SOllivier Robert if (HAVE_OPT( UPDATEINTERVAL )) {
180ea906c41SOllivier Robert long val = OPT_VALUE_UPDATEINTERVAL;
181ea906c41SOllivier Robert
182ea906c41SOllivier Robert if (val >= 0)
183ea906c41SOllivier Robert interface_interval = val;
184ea906c41SOllivier Robert else {
185eb6d21b4SOllivier Robert fprintf(stderr,
186eb6d21b4SOllivier Robert "command line interface update interval %ld must not be negative\n",
187eb6d21b4SOllivier Robert val);
188ea906c41SOllivier Robert msyslog(LOG_ERR,
189eb6d21b4SOllivier Robert "command line interface update interval %ld must not be negative",
190ea906c41SOllivier Robert val);
191ea906c41SOllivier Robert errflg++;
192ea906c41SOllivier Robert }
193ea906c41SOllivier Robert }
194224ba2bdSOllivier Robert
1959c2daa00SOllivier Robert
1962b15cb3dSCy Schubert /* save list of servers from cmd line for config_peers() use */
1972b15cb3dSCy Schubert if (argc > 0) {
1982b15cb3dSCy Schubert cmdline_server_count = argc;
1992b15cb3dSCy Schubert cmdline_servers = argv;
200224ba2bdSOllivier Robert }
2012b15cb3dSCy Schubert
2022b15cb3dSCy Schubert /* display usage & exit with any option processing errors */
2032b15cb3dSCy Schubert if (errflg)
2042b15cb3dSCy Schubert optionUsage(&ntpdOptions, 2); /* does not return */
205224ba2bdSOllivier Robert }
206