NetBSD/dist/ntp/ntpd/cmd_args.c

235 lines
4.8 KiB
C
Raw Normal View History

2007-01-06 22:45:21 +03:00
/* $NetBSD: cmd_args.c,v 1.4 2007/01/06 19:45:22 kardel Exp $ */
2003-12-04 19:05:14 +03:00
/*
* cmd_args.c = command-line argument processing
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "ntpd.h"
#include "ntp_stdlib.h"
#include "ntp_cmdargs.h"
#ifdef SIM
2007-01-06 22:45:21 +03:00
# include "ntpsim.h"
# include "ntpdsim-opts.h"
# define OPTSTRUCT ntpdsimOptions
#else
# include "ntpd-opts.h"
# define OPTSTRUCT ntpdOptions
2003-12-04 19:05:14 +03:00
#endif /* SIM */
/*
* Definitions of things either imported from or exported to outside
*/
extern char const *progname;
2007-01-06 22:45:21 +03:00
extern const char *specific_interface;
2006-06-11 23:34:07 +04:00
extern int default_ai_family;
2003-12-04 19:05:14 +03:00
#ifdef HAVE_NETINFO
extern int check_netinfo;
#endif
/*
2007-01-06 22:45:21 +03:00
* getCmdOpts - get command line options
2003-12-04 19:05:14 +03:00
*/
void
2007-01-06 22:45:21 +03:00
getCmdOpts(
2003-12-04 19:05:14 +03:00
int argc,
char *argv[]
)
{
2007-01-06 22:45:21 +03:00
extern const char *config_file;
2003-12-04 19:05:14 +03:00
int errflg;
2007-01-06 22:45:21 +03:00
tOptions *myOptions = &OPTSTRUCT;
2003-12-04 19:05:14 +03:00
/*
2007-01-06 22:45:21 +03:00
* Initialize, initialize
2003-12-04 19:05:14 +03:00
*/
errflg = 0;
2007-01-06 22:45:21 +03:00
switch (WHICH_IDX_IPV4) {
case INDEX_OPT_IPV4:
default_ai_family = AF_INET;
break;
case INDEX_OPT_IPV6:
default_ai_family = AF_INET6;
break;
default:
/* ai_fam_templ = ai_fam_default; */
break;
}
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( AUTHREQ ))
proto_config(PROTO_AUTHENTICATE, 1, 0., NULL);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( AUTHNOREQ ))
proto_config(PROTO_AUTHENTICATE, 0, 0., NULL);
if (HAVE_OPT( BCASTSYNC ))
proto_config(PROTO_BROADCLIENT, 1, 0., NULL);
if (HAVE_OPT( CONFIGFILE )) {
config_file = OPT_ARG( CONFIGFILE );
#ifdef HAVE_NETINFO
check_netinfo = 0;
#endif
2003-12-04 19:05:14 +03:00
}
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( DRIFTFILE ))
stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE ));
if (HAVE_OPT( PANICGATE ))
allow_panic = TRUE;
if (HAVE_OPT( JAILDIR )) {
#ifdef HAVE_DROPROOT
droproot = 1;
chrootdir = (char *)OPT_ARG( JAILDIR );
2003-12-04 19:05:14 +03:00
#else
2007-01-06 22:45:21 +03:00
errflg++;
2003-12-04 19:05:14 +03:00
#endif
}
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( KEYFILE ))
getauthkeys(OPT_ARG( KEYFILE ));
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( PIDFILE ))
stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE ));
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( QUIT ))
mode_ntpdate = TRUE;
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( PROPAGATIONDELAY ))
do {
double tmp;
const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY );
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) {
msyslog(LOG_ERR,
"command line broadcast delay value %s undecodable",
my_ntp_optarg);
} else {
proto_config(PROTO_BROADDELAY, 0, tmp, NULL);
}
} while (0);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( STATSDIR ))
stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR ));
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( TRUSTEDKEY )) {
int ct = STACKCT_OPT( TRUSTEDKEY );
const char** pp = STACKLST_OPT( TRUSTEDKEY );
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
do {
u_long tkey;
const char* p = *pp++;
tkey = (int)atol(p);
if (tkey <= 0 || tkey > NTP_MAXKEY) {
msyslog(LOG_ERR,
"command line trusted key %s is invalid",
p);
} else {
authtrust(tkey, 1);
}
} while (--ct > 0);
}
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( USER )) {
2006-06-11 23:34:07 +04:00
#ifdef HAVE_DROPROOT
2007-01-06 22:45:21 +03:00
char *ntp_optarg = (char *)OPT_ARG( USER );
droproot = 1;
user = malloc(strlen(ntp_optarg) + 1);
if (user == NULL) {
2003-12-04 19:05:14 +03:00
errflg++;
2007-01-06 22:45:21 +03:00
} else {
(void)strncpy(user, ntp_optarg, strlen(ntp_optarg) + 1);
group = rindex(user, ':');
if (group)
*group++ = '\0'; /* get rid of the ':' */
}
#else
errflg++;
2003-12-04 19:05:14 +03:00
#endif
2007-01-06 22:45:21 +03:00
}
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( VAR )) {
int ct = STACKCT_OPT( VAR );
const char** pp = STACKLST_OPT( VAR );
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
do {
const char* my_ntp_optarg = *pp++;
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
(u_short) (RW));
} while (--ct > 0);
}
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( DVAR )) {
int ct = STACKCT_OPT( DVAR );
const char** pp = STACKLST_OPT( DVAR );
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
do {
const char* my_ntp_optarg = *pp++;
set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1,
(u_short) (RW | DEF));
} while (--ct > 0);
}
if (HAVE_OPT( SLEW ))
clock_max = 600;
if (HAVE_OPT( UPDATEINTERVAL )) {
long val = OPT_VALUE_UPDATEINTERVAL;
if ((val >= 60) || (val == 0))
interface_interval = val;
else {
msyslog(LOG_ERR,
"command line interface update interval %ld must be 0 or longer than 60 seconds",
val);
2003-12-04 19:05:14 +03:00
errflg++;
2007-01-06 22:45:21 +03:00
}
}
2003-12-04 19:05:14 +03:00
#ifdef SIM
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( SIMBROADCASTDELAY ))
sscanf(OPT_ARG( SIMBROADCASTDELAY ), "%lf", &ntp_node.bdly);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( PHASENOISE ))
sscanf(OPT_ARG( PHASENOISE ), "%lf", &ntp_node.snse);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( SIMSLEW ))
sscanf(OPT_ARG( SIMSLEW ), "%lf", &ntp_node.slew);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( SERVERTIME ))
sscanf(OPT_ARG( SERVERTIME ), "%lf", &ntp_node.clk_time);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( ENDSIMTIME ))
sscanf(OPT_ARG( ENDSIMTIME ), "%lf", &ntp_node.sim_time);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( FREQERR ))
sscanf(OPT_ARG( FREQERR ), "%lf", &ntp_node.ferr);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( WALKNOISE ))
sscanf(OPT_ARG( WALKNOISE ), "%lf", &ntp_node.fnse);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( NDELAY ))
sscanf(OPT_ARG( NDELAY ), "%lf", &ntp_node.ndly);
2003-12-04 19:05:14 +03:00
2007-01-06 22:45:21 +03:00
if (HAVE_OPT( PDELAY ))
sscanf(OPT_ARG( PDELAY ), "%lf", &ntp_node.pdly);
2003-12-04 19:05:14 +03:00
#endif /* SIM */
2007-01-06 22:45:21 +03:00
if (errflg || argc) {
printf("argc is <%d>\n", argc);
optionUsage(myOptions, 2);
2003-12-04 19:05:14 +03:00
}
return;
}