1 /* $NetBSD: cmd_args.c,v 1.1.1.1 2009/12/13 16:56:08 kardel Exp $ */ 2 3 /* 4 * cmd_args.c = command-line argument processing 5 */ 6 #ifdef HAVE_CONFIG_H 7 # include <config.h> 8 #endif 9 10 #include "ntpd.h" 11 #include "ntp_stdlib.h" 12 #include "ntp_cmdargs.h" 13 14 #include "ntpd-opts.h" 15 16 /* 17 * Definitions of things either imported from or exported to outside 18 */ 19 extern char const *progname; 20 extern const char *specific_interface; 21 extern short default_ai_family; 22 23 #ifdef HAVE_NETINFO 24 extern int check_netinfo; 25 #endif 26 27 28 /* 29 * getCmdOpts - get command line options 30 */ 31 void 32 getCmdOpts( 33 int argc, 34 char *argv[] 35 ) 36 { 37 extern const char *config_file; 38 int errflg; 39 tOptions *myOptions = &ntpdOptions; 40 41 /* 42 * Initialize, initialize 43 */ 44 errflg = 0; 45 46 if (HAVE_OPT( IPV4 )) 47 default_ai_family = AF_INET; 48 else if (HAVE_OPT( IPV6 )) 49 default_ai_family = AF_INET6; 50 51 if (HAVE_OPT( AUTHREQ )) 52 proto_config(PROTO_AUTHENTICATE, 1, 0., NULL); 53 else if (HAVE_OPT( AUTHNOREQ )) 54 proto_config(PROTO_AUTHENTICATE, 0, 0., NULL); 55 56 if (HAVE_OPT( BCASTSYNC )) 57 proto_config(PROTO_BROADCLIENT, 1, 0., NULL); 58 59 if (HAVE_OPT( CONFIGFILE )) { 60 config_file = OPT_ARG( CONFIGFILE ); 61 #ifdef HAVE_NETINFO 62 check_netinfo = 0; 63 #endif 64 } 65 66 if (HAVE_OPT( DRIFTFILE )) 67 stats_config(STATS_FREQ_FILE, OPT_ARG( DRIFTFILE )); 68 69 if (HAVE_OPT( PANICGATE )) 70 allow_panic = TRUE; 71 72 #ifdef HAVE_DROPROOT 73 if (HAVE_OPT( JAILDIR )) { 74 droproot = 1; 75 chrootdir = OPT_ARG( JAILDIR ); 76 } 77 #endif 78 79 if (HAVE_OPT( KEYFILE )) 80 getauthkeys(OPT_ARG( KEYFILE )); 81 82 if (HAVE_OPT( PIDFILE )) 83 stats_config(STATS_PID_FILE, OPT_ARG( PIDFILE )); 84 85 if (HAVE_OPT( QUIT )) 86 mode_ntpdate = TRUE; 87 88 if (HAVE_OPT( PROPAGATIONDELAY )) 89 do { 90 double tmp; 91 const char *my_ntp_optarg = OPT_ARG( PROPAGATIONDELAY ); 92 93 if (sscanf(my_ntp_optarg, "%lf", &tmp) != 1) { 94 msyslog(LOG_ERR, 95 "command line broadcast delay value %s undecodable", 96 my_ntp_optarg); 97 } else { 98 proto_config(PROTO_BROADDELAY, 0, tmp, NULL); 99 } 100 } while (0); 101 102 if (HAVE_OPT( STATSDIR )) 103 stats_config(STATS_STATSDIR, OPT_ARG( STATSDIR )); 104 105 if (HAVE_OPT( TRUSTEDKEY )) { 106 int ct = STACKCT_OPT( TRUSTEDKEY ); 107 const char** pp = STACKLST_OPT( TRUSTEDKEY ); 108 109 do { 110 u_long tkey; 111 const char* p = *pp++; 112 113 tkey = (int)atol(p); 114 if (tkey == 0 || tkey > NTP_MAXKEY) { 115 msyslog(LOG_ERR, 116 "command line trusted key %s is invalid", 117 p); 118 } else { 119 authtrust(tkey, 1); 120 } 121 } while (--ct > 0); 122 } 123 124 #ifdef HAVE_DROPROOT 125 if (HAVE_OPT( USER )) { 126 droproot = 1; 127 user = estrdup(OPT_ARG( USER )); 128 group = rindex(user, ':'); 129 if (group) 130 *group++ = '\0'; /* get rid of the ':' */ 131 } 132 #endif 133 134 if (HAVE_OPT( VAR )) { 135 int ct = STACKCT_OPT( VAR ); 136 const char** pp = STACKLST_OPT( VAR ); 137 138 do { 139 const char* my_ntp_optarg = *pp++; 140 141 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1, 142 (u_short) (RW)); 143 } while (--ct > 0); 144 } 145 146 if (HAVE_OPT( DVAR )) { 147 int ct = STACKCT_OPT( DVAR ); 148 const char** pp = STACKLST_OPT( DVAR ); 149 150 do { 151 const char* my_ntp_optarg = *pp++; 152 153 set_sys_var(my_ntp_optarg, strlen(my_ntp_optarg)+1, 154 (u_short) (RW | DEF)); 155 } while (--ct > 0); 156 } 157 158 if (HAVE_OPT( SLEW )) { 159 clock_max = 600; 160 kern_enable = 0; 161 } 162 if (HAVE_OPT( UPDATEINTERVAL )) { 163 long val = OPT_VALUE_UPDATEINTERVAL; 164 165 if (val >= 0) 166 interface_interval = val; 167 else { 168 fprintf(stderr, 169 "command line interface update interval %ld must not be negative\n", 170 val); 171 msyslog(LOG_ERR, 172 "command line interface update interval %ld must not be negative", 173 val); 174 errflg++; 175 } 176 } 177 #ifdef SIM 178 179 /* SK: 180 * The simulator no longer takes any command line arguments. Hence, 181 * all the code that was here has been removed. 182 */ 183 184 #endif /* SIM */ 185 186 if (errflg || argc) { 187 if (argc) 188 fprintf(stderr, "argc after processing is <%d>\n", argc); 189 optionUsage(myOptions, 2); 190 } 191 return; 192 } 193