From: Thibault Godouet Date: Sat, 16 Jan 2010 10:33:33 +0000 (+0000) Subject: Moved fcron.conf code from sub.{c,h} to fcronconf.{c,h} X-Git-Tag: ver3_1_0~38^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bf535d506b9c56aaf83296dbb7ff70cfd5ce0107;p=fcron Moved fcron.conf code from sub.{c,h} to fcronconf.{c,h} --- diff --git a/Makefile.in b/Makefile.in index fea8fc6..7f062ef 100644 --- a/Makefile.in +++ b/Makefile.in @@ -73,11 +73,11 @@ CFLAGS += $(OPTIM) $(OPTION) $(DEFS) $(CPPFLAGS) ifeq ($(FCRONDYN), 1) LIBOBJS := socket.o $(LIBOBJS) endif -OBJSD := fcron.o subs.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o $(LIBOBJS) -OBJSTAB := fcrontab.o subs.o save.o temp_file.o log.o fileconf.o allow.o read_string.o u_list.o env_list.o -OBJSDYN := fcrondyn.o subs.o log.o allow.o read_string.o +OBJSD := fcron.o subs.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o fcronconf.o $(LIBOBJS) +OBJSTAB := fcrontab.o subs.o save.o temp_file.o log.o fileconf.o allow.o read_string.o u_list.o env_list.o fcronconf.o +OBJSDYN := fcrondyn.o subs.o log.o allow.o read_string.o fcronconf.o OBJCONV := convert-fcrontab.o subs.o save.o log.o u_list.o env_list.o -OBJSIG := fcronsighup.o subs.o log.o allow.o +OBJSIG := fcronsighup.o subs.o log.o allow.o fcronconf.o HEADERSALL := config.h $(SRCDIR)/global.h $(SRCDIR)/log.h $(SRCDIR)/subs.h $(SRCDIR)/save.h $(SRCDIR)/option.h $(SRCDIR)/dyncom.h # this is a regular expression : diff --git a/doc/en/todo.sgml b/doc/en/todo.sgml index 443ba0e..216a93e 100644 --- a/doc/en/todo.sgml +++ b/doc/en/todo.sgml @@ -24,14 +24,11 @@ A copy of the license is included in gfdl.sgml. High priority - - move fcron.conf stuff from subs.{c} to a new fcronconf.{c|h} - change copyright to ...,2008,2009,2010 - check fcron for memory leaks using library + could be worth checking fcron for memory leaks using specialized library (just in case...) Test (and use ?) docbook2x-man -- xlstproc ? cf http://antoine.ginies.free.fr/docbook/ch09.html diff --git a/fcron.c b/fcron.c index df8148e..f3d195e 100644 --- a/fcron.c +++ b/fcron.c @@ -29,6 +29,7 @@ #include "conf.h" #include "job.h" #include "temp_file.h" +#include "fcronconf.h" #ifdef FCRONDYN #include "socket.h" #endif diff --git a/fcron.h b/fcron.h index 04a8c66..b3e491e 100644 --- a/fcron.h +++ b/fcron.h @@ -29,6 +29,7 @@ #include "global.h" #include "exe_list.h" #include "lavg_list.h" +#include "fcronconf.h" #ifdef HAVE_CRYPT_H #include diff --git a/fcronconf.c b/fcronconf.c new file mode 100644 index 0000000..f043f64 --- /dev/null +++ b/fcronconf.c @@ -0,0 +1,187 @@ +/* + * FCRON - periodic command scheduler + * + * Copyright 2000-2009 Thibault Godouet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * The GNU General Public License can also be found in the file + * `LICENSE' that comes with the fcron source distribution. + */ + + /* $Id: subs.c,v 1.29 2008-05-11 11:08:23 thib Exp $ */ + +#include "global.h" +#include "fcronconf.h" +#include "subs.h" + +void init_conf(void); + +extern char debug_opt; +extern uid_t rootuid; +extern gid_t rootgid; + +/* fcron.conf parameters */ +char *fcronconf = NULL; +char *fcrontabs = NULL; +char *pidfile = NULL; +char *fifofile = NULL; +char *fcronallow = NULL; +char *fcrondeny = NULL; +char *shell = NULL; +char *sendmail = NULL; +char *editor = NULL; + +void +init_conf(void) +/* initialises config with compiled in constants */ +{ + /* set fcronconf if cmd line option -c has not been used */ + if (fcronconf == NULL) + fcronconf = strdup2(ETC "/" FCRON_CONF); + fcrontabs = strdup2(FCRONTABS); + pidfile = strdup2(PIDFILE); + fifofile = strdup2(FIFOFILE); + fcronallow = strdup2(ETC "/" FCRON_ALLOW); + fcrondeny = strdup2(ETC "/" FCRON_DENY); + /* // */ + /* shell = strdup2(FCRON_SHELL); */ + shell = strdup2(""); + +#ifdef SENDMAIL + sendmail = strdup2(SENDMAIL); +#endif + editor = strdup2(FCRON_EDITOR); +} + +void +free_conf(void) +/* free() the memory allocated in init_conf() */ +{ + free_safe(fcronconf); + free_safe(fcrontabs); + free_safe(pidfile); + free_safe(fifofile); + free_safe(fcronallow); + free_safe(fcrondeny); + free_safe(shell); + free_safe(sendmail); + free_safe(editor); +} + +void +read_conf(void) +/* reads in a config file and updates the necessary global variables */ +{ + FILE *f = NULL; + struct stat st; + char buf[LINE_LEN]; + char *ptr1 = NULL, *ptr2 = NULL; + short namesize = 0; + char err_on_enoent = 0; + struct group *gr = NULL; + gid_t fcrongid = -1; + + if (fcronconf != NULL) + /* fcronconf has been set by -c option : file must exist */ + err_on_enoent = 1; + + init_conf(); + + if ( (f = fopen(fcronconf, "r")) == NULL ) { + if ( errno == ENOENT ) { + if ( err_on_enoent ) + die_e("Could not read %s", fcronconf); + else + /* file does not exist, it is not an error */ + return; + } + else { + error_e("Could not read %s : config file ignored", fcronconf); + return; + } + } + + /* get fcrongid */ + gr = getgrnam(GROUPNAME); + if ( gr == NULL ) { + die_e("Unable to find %s in /etc/group", GROUPNAME); + } + fcrongid = gr->gr_gid; + + /* check if the file is secure : owner:root, group:fcron, + * writable only by owner */ + if ( fstat(fileno(f), &st) != 0 + || st.st_uid != rootuid || st.st_gid != fcrongid + || st.st_mode & S_IWGRP || st.st_mode & S_IWOTH ) { + error("Conf file (%s) must be owned by root:" GROUPNAME + " and (no more than) 644 : ignored", fcronconf, GROUPNAME); + fclose(f); + return; + } + + while ( (ptr1 = fgets(buf, sizeof(buf), f)) != NULL ) { + + Skip_blanks(ptr1); /* at the beginning of the line */ + + /* ignore comments and blank lines */ + if ( *ptr1 == '#' || *ptr1 == '\n' || *ptr1 == '\0') + continue; + + remove_blanks(ptr1); /* at the end of the line */ + + /* get the name of the var */ + if ( ( namesize = get_word(&ptr1) ) == 0 ) + /* name is zero-length */ + error("Zero-length var name at line %s : line ignored", buf); + + ptr2 = ptr1 + namesize; + + /* skip the blanks and the "=" and go to the value */ + while ( isspace( (int) *ptr2 ) ) ptr2++; + if ( *ptr2 == '=' ) ptr2++; + while ( isspace( (int) *ptr2 ) ) ptr2++; + + /* find which var the line refers to and update it */ + if ( strncmp(ptr1, "fcrontabs", namesize) == 0 ) { Set(fcrontabs, ptr2); } + else if ( strncmp(ptr1, "pidfile", namesize) == 0 ) { Set(pidfile, ptr2); } + else if ( strncmp(ptr1, "fifofile", namesize) == 0 ) { Set(fifofile , ptr2); } + else if ( strncmp(ptr1, "fcronallow", namesize) == 0 ) { Set(fcronallow , ptr2); } + else if ( strncmp(ptr1, "fcrondeny", namesize) == 0 ) { Set(fcrondeny , ptr2); } + else if ( strncmp(ptr1, "shell", namesize) == 0 ) { Set(shell , ptr2); } + else if ( strncmp(ptr1, "sendmail", namesize) == 0 ) { Set(sendmail , ptr2); } + else if ( strncmp(ptr1, "editor", namesize) == 0 ) { Set(editor , ptr2); } + else + error("Unknown var name at line %s : line ignored", buf); + + } + + if (debug_opt) { + debug(" fcronconf=%s", fcronconf); +/* debug(" fcronallow=%s", fcronallow); */ +/* debug(" fcrondeny=%s", fcrondeny); */ +/* debug(" fcrontabs=%s", fcrontabs); */ +/* debug(" pidfile=%s", pidfile); */ +/* debug(" fifofile=%s", fifofile); */ +/* debug(" editor=%s", editor); */ +/* debug(" shell=%s", shell); */ +/* debug(" sendmail=%s", sendmail); */ + } + + fclose(f); + +} + + diff --git a/fcronconf.h b/fcronconf.h new file mode 100644 index 0000000..ede75af --- /dev/null +++ b/fcronconf.h @@ -0,0 +1,48 @@ +/* + * FCRON - periodic command scheduler + * + * Copyright 2000-2009 Thibault Godouet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * The GNU General Public License can also be found in the file + * `LICENSE' that comes with the fcron source distribution. + */ + + /* $Id: subs.h,v 1.11 2007-04-14 18:04:23 thib Exp $ */ + +#ifndef __FCRONCONF_H__ +#define __FCRONCONF_H__ + + +/* global variables */ + +/* fcron.conf parameters */ +extern char *fcronconf; +extern char *fcronallow; +extern char *fcrondeny; +extern char *fcrontabs; +extern char *pidfile; +extern char *fifofile; +extern char *editor; +extern char *shell; +extern char *sendmail; +/* end of global variables */ + +/* functions prototypes */ +extern void read_conf(void); +extern void free_conf(void); + +#endif /* __FCRONCONF_H__ */ diff --git a/fcrondyn.h b/fcrondyn.h index 51c4bf7..be2bd1d 100644 --- a/fcrondyn.h +++ b/fcrondyn.h @@ -28,6 +28,7 @@ #include "global.h" #include "dyncom.h" +#include "fcronconf.h" /* global variables */ extern char dosyslog; diff --git a/fcronsighup.c b/fcronsighup.c index 016627e..f198999 100644 --- a/fcronsighup.c +++ b/fcronsighup.c @@ -26,6 +26,7 @@ #include "fcronsighup.h" #include "global.h" #include "allow.h" +#include "fcronconf.h" char rcs_info[] = "$Id: fcronsighup.c,v 1.14 2008-05-11 10:46:54 thib Exp $"; diff --git a/fcrontab.h b/fcrontab.h index c5b0bc9..9e752f1 100644 --- a/fcrontab.h +++ b/fcrontab.h @@ -27,6 +27,7 @@ #define __FCRONTAB_H__ #include "global.h" +#include "fcronconf.h" /* global variables */ extern pid_t daemon_pid; diff --git a/job.c b/job.c index 9e78413..488d53c 100644 --- a/job.c +++ b/job.c @@ -26,6 +26,7 @@ #include "fcron.h" #include "job.h" +#include "temp_file.h" void sig_dfl(void); void end_job(cl_t *line, int status, FILE *mailf, short mailpos, char **sendmailenv); diff --git a/socket.c b/socket.c index 3df0742..efb8a6f 100644 --- a/socket.c +++ b/socket.c @@ -30,6 +30,7 @@ #include "socket.h" #include "getloadavg.h" #include "database.h" +#include "fcronconf.h" void remove_connection(struct fcrondyn_cl **client, struct fcrondyn_cl *prev_client); diff --git a/subs.c b/subs.c index f7a0eb8..fdb19cf 100644 --- a/subs.c +++ b/subs.c @@ -26,24 +26,6 @@ #include "global.h" #include "subs.h" -void init_conf(void); - -extern char debug_opt; -extern uid_t rootuid; -extern gid_t rootgid; - -/* fcron.conf parameters */ -char *fcronconf = NULL; -char *fcrontabs = NULL; -char *pidfile = NULL; -char *fifofile = NULL; -char *fcronallow = NULL; -char *fcrondeny = NULL; -char *shell = NULL; -char *sendmail = NULL; -char *editor = NULL; - - uid_t get_user_uid_safe(char *username) /* get the uid of user username, and die on error */ @@ -194,148 +176,6 @@ get_word(char **str) return (ptr - *str); } -void -init_conf(void) -/* initialises config with compiled in constants */ -{ - /* set fcronconf if cmd line option -c has not been used */ - if (fcronconf == NULL) - fcronconf = strdup2(ETC "/" FCRON_CONF); - fcrontabs = strdup2(FCRONTABS); - pidfile = strdup2(PIDFILE); - fifofile = strdup2(FIFOFILE); - fcronallow = strdup2(ETC "/" FCRON_ALLOW); - fcrondeny = strdup2(ETC "/" FCRON_DENY); - /* // */ - /* shell = strdup2(FCRON_SHELL); */ - shell = strdup2(""); - -#ifdef SENDMAIL - sendmail = strdup2(SENDMAIL); -#endif - editor = strdup2(FCRON_EDITOR); -} - -void -free_conf(void) -/* free() the memory allocated in init_conf() */ -{ - free_safe(fcronconf); - free_safe(fcrontabs); - free_safe(pidfile); - free_safe(fifofile); - free_safe(fcronallow); - free_safe(fcrondeny); - free_safe(shell); - free_safe(sendmail); - free_safe(editor); -} - - - -void -read_conf(void) -/* reads in a config file and updates the necessary global variables */ -{ - FILE *f = NULL; - struct stat st; - char buf[LINE_LEN]; - char *ptr1 = NULL, *ptr2 = NULL; - short namesize = 0; - char err_on_enoent = 0; - struct group *gr = NULL; - gid_t fcrongid = -1; - - if (fcronconf != NULL) - /* fcronconf has been set by -c option : file must exist */ - err_on_enoent = 1; - - init_conf(); - - if ( (f = fopen(fcronconf, "r")) == NULL ) { - if ( errno == ENOENT ) { - if ( err_on_enoent ) - die_e("Could not read %s", fcronconf); - else - /* file does not exist, it is not an error */ - return; - } - else { - error_e("Could not read %s : config file ignored", fcronconf); - return; - } - } - - /* get fcrongid */ - gr = getgrnam(GROUPNAME); - if ( gr == NULL ) { - die_e("Unable to find %s in /etc/group", GROUPNAME); - } - fcrongid = gr->gr_gid; - - /* check if the file is secure : owner:root, group:fcron, - * writable only by owner */ - if ( fstat(fileno(f), &st) != 0 - || st.st_uid != rootuid || st.st_gid != fcrongid - || st.st_mode & S_IWGRP || st.st_mode & S_IWOTH ) { - error("Conf file (%s) must be owned by root:" GROUPNAME - " and (no more than) 644 : ignored", fcronconf, GROUPNAME); - fclose(f); - return; - } - - while ( (ptr1 = fgets(buf, sizeof(buf), f)) != NULL ) { - - Skip_blanks(ptr1); /* at the beginning of the line */ - - /* ignore comments and blank lines */ - if ( *ptr1 == '#' || *ptr1 == '\n' || *ptr1 == '\0') - continue; - - remove_blanks(ptr1); /* at the end of the line */ - - /* get the name of the var */ - if ( ( namesize = get_word(&ptr1) ) == 0 ) - /* name is zero-length */ - error("Zero-length var name at line %s : line ignored", buf); - - ptr2 = ptr1 + namesize; - - /* skip the blanks and the "=" and go to the value */ - while ( isspace( (int) *ptr2 ) ) ptr2++; - if ( *ptr2 == '=' ) ptr2++; - while ( isspace( (int) *ptr2 ) ) ptr2++; - - /* find which var the line refers to and update it */ - if ( strncmp(ptr1, "fcrontabs", namesize) == 0 ) { Set(fcrontabs, ptr2); } - else if ( strncmp(ptr1, "pidfile", namesize) == 0 ) { Set(pidfile, ptr2); } - else if ( strncmp(ptr1, "fifofile", namesize) == 0 ) { Set(fifofile , ptr2); } - else if ( strncmp(ptr1, "fcronallow", namesize) == 0 ) { Set(fcronallow , ptr2); } - else if ( strncmp(ptr1, "fcrondeny", namesize) == 0 ) { Set(fcrondeny , ptr2); } - else if ( strncmp(ptr1, "shell", namesize) == 0 ) { Set(shell , ptr2); } - else if ( strncmp(ptr1, "sendmail", namesize) == 0 ) { Set(sendmail , ptr2); } - else if ( strncmp(ptr1, "editor", namesize) == 0 ) { Set(editor , ptr2); } - else - error("Unknown var name at line %s : line ignored", buf); - - } - - if (debug_opt) { - debug(" fcronconf=%s", fcronconf); -/* debug(" fcronallow=%s", fcronallow); */ -/* debug(" fcrondeny=%s", fcrondeny); */ -/* debug(" fcrontabs=%s", fcrontabs); */ -/* debug(" pidfile=%s", pidfile); */ -/* debug(" fifofile=%s", fifofile); */ -/* debug(" editor=%s", editor); */ -/* debug(" shell=%s", shell); */ -/* debug(" sendmail=%s", sendmail); */ - } - - fclose(f); - -} - void my_unsetenv(const char *name) /* call unsetenv() if available, otherwise call putenv("var="). diff --git a/subs.h b/subs.h index 6a7c420..b41405a 100644 --- a/subs.h +++ b/subs.h @@ -27,20 +27,6 @@ #define __SUBS_H__ -/* global variables */ - -/* fcron.conf parameters */ -extern char *fcronconf; -extern char *fcronallow; -extern char *fcrondeny; -extern char *fcrontabs; -extern char *pidfile; -extern char *fifofile; -extern char *editor; -extern char *shell; -extern char *sendmail; -/* end of global variables */ - /* functions prototypes */ extern uid_t get_user_uid_safe(char *username); extern gid_t get_group_gid_safe(char *groupname); @@ -51,9 +37,6 @@ extern void *alloc_safe(size_t len, const char * desc); extern void *realloc_safe(void *ptr, size_t len, const char * desc); extern void free_safe(void *ptr); extern int get_word(char **str); -extern int temp_file(char **name); -extern void read_conf(void); -extern void free_conf(void); extern void my_unsetenv(const char *name); extern void my_setenv_overwrite(const char *name, const char *value);