22 #include "fileutils.h"
48 #define MAIN_ERR_PREFIX "CONF: "
51 #define CONF_PERM (api_posix_mode_t) (API_POSIX_S_IRUSR | API_POSIX_S_IWUSR)
72 static int conf_initialized = 0;
73 static const char confname[] =
"configfile";
74 static const char tmpname[] =
"configfile.new";
111 static void conf_init_configuration(
struct conf* cfg)
113 const char server_default[] =
"localhost";
114 const char service_default[] =
"nntp";
115 const char test_ere_default[] =
"\\.test$|^test$";
116 const char test_kwords_default[] =
"ignore,no-reply";
117 const char crl_upd_ts_default[] =
"1970-01-01T00:00:00Z";
118 const char sigfile_default[] =
".signature";
120 const char intro_default[] =
"%s wrote:";
126 conf_add_entry_int(cfg,
CONF_POS_X,
"pos_x:", 10);
127 conf_add_entry_int(cfg,
CONF_POS_Y,
"pos_y:", 40);
128 conf_add_entry_int(cfg,
CONF_SIZE_X,
"size_x:", 750);
129 conf_add_entry_int(cfg,
CONF_SIZE_Y,
"size_y:", 350);
130 conf_add_entry_int(cfg,
CONF_TILE_X,
"tile_x:", 200);
131 conf_add_entry_int(cfg,
CONF_TILE_Y,
"tile_y:", 120);
134 string = (
char*) api_posix_malloc(strlen(server_default) + (size_t) 1);
135 if(NULL ==
string) { error = 1; }
138 strcpy(
string, server_default);
139 conf_add_entry_string(cfg,
CONF_SERVER,
"server:",
string);
143 string = (
char*) api_posix_malloc(strlen(service_default) + (size_t) 1);
144 if(NULL ==
string) { error = 1; }
147 strcpy(
string, service_default);
148 conf_add_entry_string(cfg,
CONF_SERVICE,
"service:",
string);
152 conf_add_entry_int(cfg,
CONF_ENC,
"enc:", 0);
155 conf_add_entry_int(cfg,
CONF_AUTH,
"auth:", 0);
157 empty = (
char*) api_posix_malloc((
size_t) 1);
158 if(NULL == empty) { error = 1; }
162 conf_add_entry_string(cfg,
CONF_USER,
"user:", empty);
164 empty = (
char*) api_posix_malloc((
size_t) 1);
165 if(NULL == empty) { error = 1; }
169 conf_add_entry_string(cfg,
CONF_PASS,
"pass:", empty);
173 conf_add_entry_int(cfg,
CONF_CAC,
"cac:", 250);
176 empty = (
char*) api_posix_malloc((
size_t) 1);
177 if(NULL == empty) { error = 1; }
181 conf_add_entry_string(cfg,
CONF_FROM,
"from:", empty);
185 empty = (
char*) api_posix_malloc((
size_t) 1);
186 if(NULL == empty) { error = 1; }
190 conf_add_entry_string(cfg,
CONF_REPLYTO,
"replyto:", empty);
194 empty = (
char*) api_posix_malloc((
size_t) 1);
195 if(NULL == empty) { error = 1; }
199 conf_add_entry_string(cfg,
CONF_FQDN,
"fqdn:", empty);
203 empty = (
char*) api_posix_malloc((
size_t) 1);
204 if(NULL == empty) { error = 1; }
208 conf_add_entry_string(cfg,
CONF_NEWSRC,
"newsrc:", empty);
212 empty = (
char*) api_posix_malloc((
size_t) 1);
213 if(NULL == empty) { error = 1; }
217 conf_add_entry_string(cfg,
CONF_SCORERC,
"scorerc:", empty);
221 string = (
char*) api_posix_malloc(strlen(intro_default) + (size_t) 1);
222 if(NULL ==
string) { error = 1; }
225 strcpy(
string, intro_default);
226 conf_add_entry_string(cfg,
CONF_INTRO,
"intro:",
string);
230 empty = (
char*) api_posix_malloc((
size_t) 1);
231 if(NULL == empty) { error = 1; }
239 conf_add_entry_int(cfg,
CONF_TVIEW,
"tview:", 1);
242 conf_add_entry_int(cfg,
CONF_ONLYUR,
"onlyur:", 0);
245 empty = (
char*) api_posix_malloc((
size_t) 1);
246 if(NULL == empty) { error = 1; }
254 empty = (
char*) api_posix_malloc((
size_t) 1);
255 if(NULL == empty) { error = 1; }
259 conf_add_entry_string(cfg,
CONF_EDITOR,
"editor:", empty);
263 empty = (
char*) api_posix_malloc((
size_t) 1);
264 if(NULL == empty) { error = 1; }
268 conf_add_entry_string(cfg,
CONF_PPROC,
"post_proc:", empty);
272 empty = (
char*) api_posix_malloc((
size_t) 1);
273 if(NULL == empty) { error = 1; }
277 conf_add_entry_string(cfg,
CONF_INEWS,
"inews:", empty);
287 string = (
char*) api_posix_malloc(strlen(test_ere_default) + (size_t) 1);
288 if(NULL ==
string) { error = 1; }
291 strcpy(
string, test_ere_default);
296 string = (
char*) api_posix_malloc(strlen(test_kwords_default) + (size_t) 1);
297 if(NULL ==
string) { error = 1; }
300 strcpy(
string, test_kwords_default);
312 string = (
char*) api_posix_malloc(strlen(crl_upd_ts_default) + (size_t) 1);
313 if(NULL ==
string) { error = 1; }
316 strcpy(
string, crl_upd_ts_default);
333 conf_add_entry_int(cfg,
CONF_NO_OVER,
"nntp_no_over:", 0);
342 string = (
char*) api_posix_malloc(strlen(sigfile_default) + (size_t) 1);
343 if(NULL ==
string) { error = 1; }
346 strcpy(
string, sigfile_default);
347 conf_add_entry_string(cfg,
CONF_SIGFILE,
"signature_file:",
string);
351 conf_add_entry_int(cfg,
CONF_TS_LTIME,
"timestamp_ltime:", 1);
369 empty = (
char*) api_posix_malloc((
size_t) 1);
370 if(NULL == empty) { error = 1; }
404 if(error) {
PRINT_ERROR(
"Initializing configuration failed"); }
405 else { conf_initialized = 1; }
415 static int conf_get_confpathname(
const char** confpathname,
416 const char* conffile)
422 if(NULL != *confpathname)
424 rv =
fu_create_path(*confpathname, (api_posix_mode_t) API_POSIX_S_IRWXU);
440 api_posix_free((
void*) *confpathname);
441 *confpathname = NULL;
451 static int conf_extract_entry(
char* line,
struct conf* target)
454 size_t labelsize = strlen(target->
label);
461 if(strlen(line) > labelsize)
464 if(!strncmp(target->
label, line, labelsize))
471 string = &line[labelsize];
472 if(1 == sscanf(
string,
"%d", &
val)) target->
val.
i =
val;
480 if(
' ' != line[i]) {
break; }
497 sbuf = (
char*) api_posix_malloc(strlen(
string) + (size_t) 1);
500 PRINT_ERROR(
"Cannot allocate memory for config file entry");
501 target->
val.
s = NULL;
506 strcpy(sbuf,
string);
508 api_posix_free((
void*) target->
val.
s);
509 target->
val.
s = sbuf;
522 static int conf_update_entry(
char** line,
size_t len,
struct conf* target)
525 size_t labelsize = strlen(target->
label);
530 if(strlen(*line) >= labelsize)
533 if(!strncmp(target->
label, *line, labelsize))
544 reqd = api_posix_snprintf(*line, len,
"%s %d\n",
550 reqd = api_posix_snprintf(*line, len,
"%s %s\n",
553 if(0 > reqd) {
break; }
554 else if(len <= (
size_t) reqd)
557 if(API_POSIX_SIZE_MAX <= (
size_t) reqd) {
break; }
558 string = (
char*) api_posix_realloc(*line, (
size_t) (reqd + 1));
559 if(NULL ==
string) {
break; }
560 else { *line = string; }
563 while(len <= (
size_t) reqd);
574 static int conf_import_configuration(
struct conf* cfg, FILE* fs)
579 api_posix_ssize_t readlen;
585 readlen = api_posix_getline(&line, &len, fs);
588 if(API_POSIX_ENOMEM == api_posix_errno)
590 PRINT_ERROR(
"Cannot assign memory for config file parser");
608 if(0 >= readlen) {
break; }
612 if(!strncmp(
"#", line, 1)) {
continue; }
615 if(!conf_extract_entry(line, &cfg[i])) {
break; }
622 api_posix_free((
void*) line);
631 static int conf_export_configuration(
struct conf* cfg,
632 FILE* fs, FILE* fs_tmp)
637 api_posix_ssize_t readlen;
646 readlen = api_posix_getline(&line, &len, fs);
649 if(API_POSIX_ENOMEM == api_posix_errno)
651 PRINT_ERROR(
"Cannot assign memory for config file parser");
669 if(0 >= readlen) {
break; }
675 if(!conf_update_entry(&line, len, &cfg[i])) {
break; }
679 rv = fprintf(fs_tmp,
"%s", line);
680 if(0 > rv) {
break; }
692 reqd = strlen(cfg[i].
label);
693 if(API_POSIX_SIZE_MAX <= reqd) {
break; }
694 if(reqd + (
size_t) 1 > len)
697 p = (
char*) api_posix_realloc(line, reqd + (
size_t) 1);
698 if (NULL != p) line = p;
700 strncpy(line, cfg[i].
label, len);
701 rv = conf_update_entry(&line, len, &cfg[i]);
705 rv = fprintf(fs_tmp,
"%s", line);
717 api_posix_free((
void*) line);
743 api_posix_free((
void*) cfg[i].
val.
s);
750 conf_initialized = 0;
773 const char* confpathname = NULL;
777 conf_init_configuration(cfg);
781 res = conf_get_confpathname(&confpathname, confname);
792 API_POSIX_O_RDWR | API_POSIX_O_CREAT,
CONF_PERM);
797 if(!res) { res = conf_import_configuration(cfg, fs); }
804 api_posix_free((
void*) confpathname);
831 const char* confpathname = NULL;
832 const char* tmppathname = NULL;
835 res = conf_get_confpathname(&confpathname, confname);
836 if(!res) { res = conf_get_confpathname(&tmppathname, tmpname); }
842 API_POSIX_O_RDWR | API_POSIX_O_CREAT,
CONF_PERM);
851 API_POSIX_O_WRONLY | API_POSIX_O_CREAT
869 res = conf_export_configuration(cfg, fs, fs_tmp);
873 if(!res) { res =
fu_sync(fd_tmp, fs_tmp); }
876 if(!res) { res = api_posix_rename(tmppathname, confpathname); }
881 PRINT_ERROR(
"Failed to store configuration in config file");
893 printf(
"%s: %sStore to: %s\n", CFG_NAME,
MAIN_ERR_PREFIX, confpathname);
897 api_posix_free((
void*) tmppathname);
898 api_posix_free((
void*) confpathname);
924 if(NULL != cfg && NULL != cfg->
val.
s && NULL != s)
929 p = api_posix_malloc(++len);
934 api_posix_free((
void*) cfg->
val.
s);