| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
|
| 3 |
|
|
*
|
| 4 |
|
|
* This program is free software; you can redistribute it and/or
|
| 5 |
|
|
* modify it under the terms of the GNU General Public License
|
| 6 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 7 |
|
|
* of the License, or (at your option) any later version.
|
| 8 |
|
|
*
|
| 9 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
* GNU General Public License for more details.
|
| 13 |
|
|
*
|
| 14 |
|
|
* You should have received a copy of the GNU General Public License
|
| 15 |
|
|
* along with this program; if not, write to the Free Software
|
| 16 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
|
|
*/
|
| 18 |
|
|
#include "common/setup_before.h"
|
| 19 |
|
|
#include "setup.h"
|
| 20 |
|
|
|
| 21 |
|
|
#ifdef HAVE_STDDEF_H
|
| 22 |
|
|
# include <stddef.h>
|
| 23 |
|
|
#else
|
| 24 |
|
|
# ifndef NULL
|
| 25 |
|
|
# define NULL ((void *)0)
|
| 26 |
|
|
# endif
|
| 27 |
|
|
#endif
|
| 28 |
|
|
#include <stdio.h>
|
| 29 |
|
|
#ifdef STDC_HEADERS
|
| 30 |
|
|
# include <stdlib.h>
|
| 31 |
|
|
#else
|
| 32 |
|
|
# ifdef HAVE_MALLOC_H
|
| 33 |
|
|
# include <malloc.h>
|
| 34 |
|
|
# endif
|
| 35 |
|
|
#endif
|
| 36 |
|
|
#ifdef TIME_WITH_SYS_TIME
|
| 37 |
|
|
# include <time.h>
|
| 38 |
|
|
# include <sys/time.h>
|
| 39 |
|
|
#else
|
| 40 |
|
|
# ifdef HAVE_SYS_TIME_H
|
| 41 |
|
|
# include <sys/time.h>
|
| 42 |
|
|
# else
|
| 43 |
|
|
# include <time.h>
|
| 44 |
|
|
# endif
|
| 45 |
|
|
#endif
|
| 46 |
|
|
#ifdef HAVE_STRING_H
|
| 47 |
|
|
# include <string.h>
|
| 48 |
|
|
#else
|
| 49 |
|
|
# ifdef HAVE_STRINGS_H
|
| 50 |
|
|
# include <strings.h>
|
| 51 |
|
|
# endif
|
| 52 |
|
|
# ifdef HAVE_MEMORY_H
|
| 53 |
|
|
# include <memory.h>
|
| 54 |
|
|
# endif
|
| 55 |
|
|
#endif
|
| 56 |
|
|
#include "compat/strcasecmp.h"
|
| 57 |
|
|
#include "compat/strtoul.h"
|
| 58 |
|
|
#include "compat/memset.h"
|
| 59 |
|
|
#include "compat/strdup.h"
|
| 60 |
|
|
#include <errno.h>
|
| 61 |
|
|
#include "compat/strerror.h"
|
| 62 |
|
|
|
| 63 |
|
|
#include "xstring.h"
|
| 64 |
|
|
#include "conf.h"
|
| 65 |
|
|
#include "common/util.h"
|
| 66 |
|
|
#include "common/eventlog.h"
|
| 67 |
|
|
#include "common/xalloc.h"
|
| 68 |
|
|
#include "common/setup_after.h"
|
| 69 |
|
|
|
| 70 |
|
|
static int conf_set_default(t_conf_table * conf_table, void * param_data, int datalen);
|
| 71 |
|
|
static int conf_set_value(t_conf_table * conf, void * param_data, char * value);
|
| 72 |
|
|
static int conf_int_set(void * data, int value);
|
| 73 |
|
|
static int conf_bool_set(void * data, int value);
|
| 74 |
|
|
static int conf_str_set(void * data, char const * value);
|
| 75 |
|
|
static int conf_hexstr_set(void * data, char const * value);
|
| 76 |
|
|
static int conf_type_get_size(e_conf_type type);
|
| 77 |
|
|
static int timestr_to_time(char const * timestr);
|
| 78 |
|
|
|
| 79 |
|
|
static int conf_int_set(void * data, int value)
|
| 80 |
|
|
{
|
| 81 |
|
|
int * p;
|
| 82 |
|
|
|
| 83 |
|
|
p=(int *)data;
|
| 84 |
|
|
*p=value;
|
| 85 |
|
|
return 0;
|
| 86 |
|
|
}
|
| 87 |
|
|
|
| 88 |
|
|
static int conf_bool_set(void * data, int value)
|
| 89 |
|
|
{
|
| 90 |
|
|
char * p;
|
| 91 |
|
|
|
| 92 |
|
|
p=(char *)data;
|
| 93 |
|
|
*p=tf(value);
|
| 94 |
|
|
return 0;
|
| 95 |
|
|
}
|
| 96 |
|
|
|
| 97 |
|
|
static int conf_str_set(void * data, char const * value)
|
| 98 |
|
|
{
|
| 99 |
|
|
char * * p;
|
| 100 |
|
|
|
| 101 |
|
|
p=(char * *)data;
|
| 102 |
|
|
if (*p) xfree(*p);
|
| 103 |
|
|
if (value) *p=xstrdup(value);
|
| 104 |
|
|
else *p=NULL;
|
| 105 |
|
|
|
| 106 |
|
|
return 0;
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
static int conf_hexstr_set(void * data, char const * value)
|
| 110 |
|
|
{
|
| 111 |
|
|
char * * p;
|
| 112 |
|
|
|
| 113 |
|
|
p=(char * *)data;
|
| 114 |
|
|
if (*p) xfree(*p);
|
| 115 |
|
|
if (value) *p=hexstrdup(value);
|
| 116 |
|
|
else *p=NULL;
|
| 117 |
|
|
return 0;
|
| 118 |
|
|
}
|
| 119 |
|
|
|
| 120 |
|
|
|
| 121 |
|
|
static int conf_set_default(t_conf_table * conf_table, void * param_data, int datalen)
|
| 122 |
|
|
{
|
| 123 |
|
|
unsigned int i;
|
| 124 |
|
|
char * p;
|
| 125 |
|
|
|
| 126 |
|
|
ASSERT(conf_table,-1);
|
| 127 |
|
|
ASSERT(param_data,-1);
|
| 128 |
|
|
memset(param_data,0,datalen);
|
| 129 |
|
|
for (i=0; conf_table[i].name; i++) {
|
| 130 |
|
|
if (conf_table[i].offset + conf_type_get_size(conf_table[i].type) > datalen) {
|
| 131 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"conf table item %d bad offset %d exceed %d",i,conf_table[i].offset,datalen);
|
| 132 |
|
|
return -1;
|
| 133 |
|
|
}
|
| 134 |
|
|
p=(char *)param_data+conf_table[i].offset;
|
| 135 |
|
|
switch (conf_table[i].type) {
|
| 136 |
|
|
CASE(conf_type_bool, conf_bool_set(p, conf_table[i].def_intval));
|
| 137 |
|
|
CASE(conf_type_int, conf_int_set(p, conf_table[i].def_intval));
|
| 138 |
|
|
CASE(conf_type_str, conf_str_set(p, conf_table[i].def_strval));
|
| 139 |
|
|
CASE(conf_type_hexstr, conf_hexstr_set(p, conf_table[i].def_strval));
|
| 140 |
|
|
CASE(conf_type_timestr, conf_int_set(p, conf_table[i].def_intval));
|
| 141 |
|
|
default:
|
| 142 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"conf table item %d bad type %d",i,conf_table[i].type);
|
| 143 |
|
|
return -1;
|
| 144 |
|
|
}
|
| 145 |
|
|
}
|
| 146 |
|
|
return 0;
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
static int conf_set_value(t_conf_table * conf, void * param_data, char * value)
|
| 150 |
|
|
{
|
| 151 |
|
|
char * p;
|
| 152 |
|
|
|
| 153 |
|
|
ASSERT(conf,-1);
|
| 154 |
|
|
ASSERT(param_data,-1);
|
| 155 |
|
|
ASSERT(value,-1);
|
| 156 |
|
|
p=(char *)param_data+conf->offset;
|
| 157 |
|
|
switch (conf->type) {
|
| 158 |
|
|
CASE(conf_type_bool, conf_bool_set(p, strtoul(value,NULL,0)));
|
| 159 |
|
|
CASE(conf_type_int, conf_int_set(p, strtoul(value,NULL,0)));
|
| 160 |
|
|
CASE(conf_type_str, conf_str_set(p, value));
|
| 161 |
|
|
CASE(conf_type_hexstr, conf_hexstr_set(p, value));
|
| 162 |
|
|
CASE(conf_type_timestr, conf_int_set(p, timestr_to_time(value)));
|
| 163 |
|
|
default:
|
| 164 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad conf type %d",conf->type);
|
| 165 |
|
|
return -1;
|
| 166 |
|
|
}
|
| 167 |
|
|
return 0;
|
| 168 |
|
|
}
|
| 169 |
|
|
|
| 170 |
|
|
static int conf_type_get_size(e_conf_type type)
|
| 171 |
|
|
{
|
| 172 |
|
|
switch (type) {
|
| 173 |
|
|
case conf_type_bool:
|
| 174 |
|
|
return sizeof(char);
|
| 175 |
|
|
case conf_type_int:
|
| 176 |
|
|
return sizeof(int);
|
| 177 |
|
|
case conf_type_str:
|
| 178 |
|
|
case conf_type_hexstr:
|
| 179 |
|
|
return sizeof(char *);
|
| 180 |
|
|
case conf_type_timestr:
|
| 181 |
|
|
return sizeof(int);
|
| 182 |
|
|
default:
|
| 183 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad conf type %d",type);
|
| 184 |
|
|
return 0;
|
| 185 |
|
|
}
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
extern int conf_parse_param(int argc, char ** argv, t_conf_table * conf_table, void * data, int datalen)
|
| 189 |
|
|
{
|
| 190 |
|
|
int i;
|
| 191 |
|
|
unsigned int j;
|
| 192 |
|
|
char * p;
|
| 193 |
|
|
int match;
|
| 194 |
|
|
|
| 195 |
|
|
ASSERT(argc,-1);
|
| 196 |
|
|
ASSERT(argv,-1);
|
| 197 |
|
|
ASSERT(data, -1);
|
| 198 |
|
|
if (conf_set_default(conf_table, data, datalen)<0) {
|
| 199 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error setting default values");
|
| 200 |
|
|
return -1;
|
| 201 |
|
|
}
|
| 202 |
|
|
for (i=1; i< argc; i++) {
|
| 203 |
|
|
match=0;
|
| 204 |
|
|
for (j=0; conf_table[j].name; j++) {
|
| 205 |
|
|
if (!strcmp(conf_table[j].name,argv[i])) {
|
| 206 |
|
|
match=1;
|
| 207 |
|
|
p=(char *)data + conf_table[j].offset;
|
| 208 |
|
|
switch (conf_table[j].type) {
|
| 209 |
|
|
case conf_type_bool:
|
| 210 |
|
|
conf_bool_set(p,1);
|
| 211 |
|
|
break;
|
| 212 |
|
|
case conf_type_int:
|
| 213 |
|
|
if (i+1>=argc) {
|
| 214 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad int conf %s without value",argv[i]);
|
| 215 |
|
|
break;
|
| 216 |
|
|
}
|
| 217 |
|
|
i++;
|
| 218 |
|
|
conf_int_set(p,strtoul(argv[i],NULL,0));
|
| 219 |
|
|
break;
|
| 220 |
|
|
case conf_type_str:
|
| 221 |
|
|
if (i+1>=argc) {
|
| 222 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad str conf %s without value",argv[i]);
|
| 223 |
|
|
break;
|
| 224 |
|
|
}
|
| 225 |
|
|
i++;
|
| 226 |
|
|
conf_str_set(p,argv[i]);
|
| 227 |
|
|
break;
|
| 228 |
|
|
case conf_type_hexstr:
|
| 229 |
|
|
if (i+1>=argc) {
|
| 230 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad hexstr conf %s without value",argv[i]);
|
| 231 |
|
|
break;
|
| 232 |
|
|
}
|
| 233 |
|
|
i++;
|
| 234 |
|
|
conf_hexstr_set(p,argv[i]);
|
| 235 |
|
|
break;
|
| 236 |
|
|
case conf_type_timestr:
|
| 237 |
|
|
if (i+1>=argc) {
|
| 238 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,argv[i]);
|
| 239 |
|
|
break;
|
| 240 |
|
|
}
|
| 241 |
|
|
i++;
|
| 242 |
|
|
conf_int_set(p,timestr_to_time(argv[i]));
|
| 243 |
|
|
default:
|
| 244 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad conf type %d",conf_table[i].type);
|
| 245 |
|
|
break;
|
| 246 |
|
|
}
|
| 247 |
|
|
break;
|
| 248 |
|
|
}
|
| 249 |
|
|
}
|
| 250 |
|
|
if (!match) {
|
| 251 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"bad option \"%s\"",argv[i]);
|
| 252 |
|
|
return -1;
|
| 253 |
|
|
}
|
| 254 |
|
|
}
|
| 255 |
|
|
return 0;
|
| 256 |
|
|
}
|
| 257 |
|
|
|
| 258 |
|
|
extern int conf_load_file(char const * filename, t_conf_table * conf_table, void * param_data, int datalen)
|
| 259 |
|
|
{
|
| 260 |
|
|
FILE * fp;
|
| 261 |
|
|
unsigned int i, line, count, match;
|
| 262 |
|
|
char * buff;
|
| 263 |
|
|
char * * item;
|
| 264 |
|
|
|
| 265 |
|
|
ASSERT(conf_table,-1);
|
| 266 |
|
|
ASSERT(param_data,-1);
|
| 267 |
|
|
if (conf_set_default(conf_table, param_data, datalen)<0) {
|
| 268 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error setting default values");
|
| 269 |
|
|
return -1;
|
| 270 |
|
|
}
|
| 271 |
|
|
if (!filename) return 0;
|
| 272 |
|
|
if (!(fp=fopen(filename,"r"))) {
|
| 273 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"could not open configuration file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));
|
| 274 |
|
|
return -1;
|
| 275 |
|
|
}
|
| 276 |
|
|
for (line=1; (buff=file_get_line(fp)); line++) {
|
| 277 |
|
|
if (buff[0]=='#') {
|
| 278 |
|
|
continue;
|
| 279 |
|
|
}
|
| 280 |
|
|
if (!(item=strtoargv(buff,&count))) {
|
| 281 |
|
|
continue;
|
| 282 |
|
|
}
|
| 283 |
|
|
if (!count) {
|
| 284 |
|
|
xfree(item);
|
| 285 |
|
|
continue;
|
| 286 |
|
|
}
|
| 287 |
|
|
if (count!=3) {
|
| 288 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"bad item count %d in file %s line %d",count,filename,line);
|
| 289 |
|
|
xfree(item);
|
| 290 |
|
|
continue;
|
| 291 |
|
|
}
|
| 292 |
|
|
if (strcmp(item[1],"=")) {
|
| 293 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"missing '=' in file %s line %d",filename,line);
|
| 294 |
|
|
xfree(item);
|
| 295 |
|
|
continue;
|
| 296 |
|
|
}
|
| 297 |
|
|
match=0;
|
| 298 |
|
|
for (i=0; conf_table[i].name; i++) {
|
| 299 |
|
|
if (!strcasecmp(conf_table[i].name,item[0])) {
|
| 300 |
|
|
conf_set_value(conf_table+i, param_data, item[2]);
|
| 301 |
|
|
match=1;
|
| 302 |
|
|
}
|
| 303 |
|
|
}
|
| 304 |
|
|
if (!match) {
|
| 305 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"got unknown field \"%s\" in line %d,(ignored)",item[0],line);
|
| 306 |
|
|
}
|
| 307 |
|
|
xfree(item);
|
| 308 |
|
|
}
|
| 309 |
|
|
file_get_line(NULL); // clear file_get_line_buffer
|
| 310 |
|
|
fclose(fp);
|
| 311 |
|
|
return 0;
|
| 312 |
|
|
}
|
| 313 |
|
|
|
| 314 |
|
|
extern int conf_cleanup(t_conf_table * conf_table, void * param_data, int datalen)
|
| 315 |
|
|
{
|
| 316 |
|
|
unsigned int i;
|
| 317 |
|
|
char * p;
|
| 318 |
|
|
|
| 319 |
|
|
ASSERT(conf_table,-1);
|
| 320 |
|
|
ASSERT(param_data,-1);
|
| 321 |
|
|
for (i=0; conf_table[i].name; i++) {
|
| 322 |
|
|
if (conf_table[i].offset + conf_type_get_size(conf_table[i].type) > datalen) {
|
| 323 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"conf table item %d bad offset %d exceed %d",i,conf_table[i].offset,datalen);
|
| 324 |
|
|
return -1;
|
| 325 |
|
|
}
|
| 326 |
|
|
|
| 327 |
|
|
p=(char *)param_data+conf_table[i].offset;
|
| 328 |
|
|
switch (conf_table[i].type) {
|
| 329 |
|
|
CASE(conf_type_bool, conf_bool_set(p, 0));
|
| 330 |
|
|
CASE(conf_type_int, conf_int_set(p, 0));
|
| 331 |
|
|
CASE(conf_type_str, conf_str_set(p, NULL));
|
| 332 |
|
|
CASE(conf_type_hexstr, conf_hexstr_set(p, NULL));
|
| 333 |
|
|
CASE(conf_type_timestr, conf_int_set(p,0));
|
| 334 |
|
|
default:
|
| 335 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"conf table item %d bad type %d",i,conf_table[i].type);
|
| 336 |
|
|
return -1;
|
| 337 |
|
|
}
|
| 338 |
|
|
}
|
| 339 |
|
|
memset(param_data,0,datalen);
|
| 340 |
|
|
return 0;
|
| 341 |
|
|
}
|
| 342 |
|
|
|
| 343 |
|
|
|
| 344 |
|
|
/* convert a time string to time_t
|
| 345 |
|
|
time string format is:
|
| 346 |
|
|
yyyy/mm/dd or yyyy-mm-dd or yyyy.mm.dd
|
| 347 |
|
|
hh:mm:ss
|
| 348 |
|
|
*/
|
| 349 |
|
|
static int timestr_to_time(char const * timestr)
|
| 350 |
|
|
{
|
| 351 |
|
|
char const * p;
|
| 352 |
|
|
char ch;
|
| 353 |
|
|
struct tm when;
|
| 354 |
|
|
int day_s, time_s, last;
|
| 355 |
|
|
|
| 356 |
|
|
if (!timestr) return -1;
|
| 357 |
|
|
if (!timestr[0]) return 0;
|
| 358 |
|
|
p = timestr;
|
| 359 |
|
|
day_s = time_s = 0;
|
| 360 |
|
|
last = 0;
|
| 361 |
|
|
memset(&when, 0, sizeof(when));
|
| 362 |
|
|
when.tm_mday = 1;
|
| 363 |
|
|
when.tm_isdst = -1;
|
| 364 |
|
|
while (1) {
|
| 365 |
|
|
ch = *timestr;
|
| 366 |
|
|
timestr++;
|
| 367 |
|
|
switch (ch) {
|
| 368 |
|
|
case '/':
|
| 369 |
|
|
case '-':
|
| 370 |
|
|
case '.':
|
| 371 |
|
|
if (day_s == 0) {
|
| 372 |
|
|
when.tm_year = atoi(p) - 1900;
|
| 373 |
|
|
} else if (day_s == 1) {
|
| 374 |
|
|
when.tm_mon = atoi(p) - 1;
|
| 375 |
|
|
} else if (day_s == 2) {
|
| 376 |
|
|
when.tm_mday = atoi(p);
|
| 377 |
|
|
}
|
| 378 |
|
|
time_s = 0;
|
| 379 |
|
|
day_s++;
|
| 380 |
|
|
p = timestr;
|
| 381 |
|
|
last = 1;
|
| 382 |
|
|
break;
|
| 383 |
|
|
case ':':
|
| 384 |
|
|
if (time_s == 0) {
|
| 385 |
|
|
when.tm_hour = atoi(p);
|
| 386 |
|
|
} else if (time_s == 1) {
|
| 387 |
|
|
when.tm_min = atoi(p);
|
| 388 |
|
|
} else if (time_s == 2) {
|
| 389 |
|
|
when.tm_sec = atoi(p);
|
| 390 |
|
|
}
|
| 391 |
|
|
day_s = 0;
|
| 392 |
|
|
time_s++;
|
| 393 |
|
|
p = timestr;
|
| 394 |
|
|
last = 2;
|
| 395 |
|
|
break;
|
| 396 |
|
|
case ' ':
|
| 397 |
|
|
case '\t':
|
| 398 |
|
|
case '\x0':
|
| 399 |
|
|
if (last == 1) {
|
| 400 |
|
|
if (day_s == 0) {
|
| 401 |
|
|
when.tm_year = atoi(p) - 1900;
|
| 402 |
|
|
} else if (day_s == 1) {
|
| 403 |
|
|
when.tm_mon = atoi(p) - 1;
|
| 404 |
|
|
} else if (day_s == 2) {
|
| 405 |
|
|
when.tm_mday = atoi(p);
|
| 406 |
|
|
}
|
| 407 |
|
|
} else if (last == 2) {
|
| 408 |
|
|
if (time_s == 0) {
|
| 409 |
|
|
when.tm_hour = atoi(p);
|
| 410 |
|
|
} else if (time_s == 1) {
|
| 411 |
|
|
when.tm_min = atoi(p);
|
| 412 |
|
|
} else if (time_s == 2) {
|
| 413 |
|
|
when.tm_sec = atoi(p);
|
| 414 |
|
|
}
|
| 415 |
|
|
}
|
| 416 |
|
|
time_s = day_s = 0;
|
| 417 |
|
|
p = timestr;
|
| 418 |
|
|
last = 0;
|
| 419 |
|
|
break;
|
| 420 |
|
|
default:
|
| 421 |
|
|
break;
|
| 422 |
|
|
}
|
| 423 |
|
|
if (!ch) break;
|
| 424 |
|
|
}
|
| 425 |
|
|
return mktime(&when);
|
| 426 |
|
|
}
|
| 427 |
|
|
|