2 * FCRON - periodic command scheduler
4 * Copyright 2000-2014 Thibault Godouet <fcron@free.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * The GNU General Public License can also be found in the file
21 * `LICENSE' that comes with the fcron source distribution.
27 #include "fcronconf.h"
32 extern char debug_opt;
36 /* fcron.conf parameters */
37 char *fcronconf = NULL;
38 char *fcrontabs = NULL;
40 char *fifofile = NULL;
41 char *fcronallow = NULL;
42 char *fcrondeny = NULL;
44 char *sendmail = NULL;
49 /* initialises config with compiled in constants */
51 /* set fcronconf if cmd line option -c has not been used */
52 if (fcronconf == NULL)
53 fcronconf = strdup2(ETC "/" FCRON_CONF);
54 fcrontabs = strdup2(FCRONTABS);
55 pidfile = strdup2(PIDFILE);
56 fifofile = strdup2(FIFOFILE);
57 fcronallow = strdup2(ETC "/" FCRON_ALLOW);
58 fcrondeny = strdup2(ETC "/" FCRON_DENY);
60 /* shell = strdup2(FCRON_SHELL); */
64 sendmail = strdup2(SENDMAIL);
66 editor = strdup2(FCRON_EDITOR);
71 /* free() the memory allocated in init_conf() */
77 Free_safe(fcronallow);
86 /* reads in a config file and updates the necessary global variables */
91 char *ptr1 = NULL, *ptr2 = NULL;
93 char err_on_enoent = 0;
94 struct group *gr = NULL;
97 if (fcronconf != NULL)
98 /* fcronconf has been set by -c option : file must exist */
103 if ((f = fopen(fcronconf, "r")) == NULL) {
104 if (errno == ENOENT) {
106 die_e("Could not read %s", fcronconf);
108 /* file does not exist, it is not an error */
112 error_e("Could not read %s : config file ignored", fcronconf);
118 gr = getgrnam(GROUPNAME);
120 die_e("Unable to find %s in /etc/group", GROUPNAME);
122 fcrongid = gr->gr_gid;
124 /* check if the file is secure : owner:root, group:fcron,
125 * writable only by owner */
126 if (fstat(fileno(f), &st) != 0
127 || st.st_uid != rootuid || st.st_gid != fcrongid
128 || st.st_mode & S_IWGRP || st.st_mode & S_IWOTH) {
129 error("Conf file (%s) must be owned by root:" GROUPNAME
130 " and (no more than) 644 : ignored", fcronconf, GROUPNAME);
131 xfclose_check(&f, fcronconf);
135 while ((ptr1 = fgets(buf, sizeof(buf), f)) != NULL) {
137 Skip_blanks(ptr1); /* at the beginning of the line */
139 /* ignore comments and blank lines */
140 if (*ptr1 == '#' || *ptr1 == '\n' || *ptr1 == '\0')
143 remove_blanks(ptr1); /* at the end of the line */
145 /* get the name of the var */
146 if ((namesize = get_word(&ptr1)) == 0)
147 /* name is zero-length */
148 error("Zero-length var name at line %s : line ignored", buf);
150 ptr2 = ptr1 + namesize;
152 /* skip the blanks and the "=" and go to the value */
153 while (isspace((int)*ptr2))
157 while (isspace((int)*ptr2))
160 /* find which var the line refers to and update it */
161 if (strncmp(ptr1, "fcrontabs", namesize) == 0) {
162 Set(fcrontabs, ptr2);
164 else if (strncmp(ptr1, "pidfile", namesize) == 0) {
167 else if (strncmp(ptr1, "fifofile", namesize) == 0) {
170 else if (strncmp(ptr1, "fcronallow", namesize) == 0) {
171 Set(fcronallow, ptr2);
173 else if (strncmp(ptr1, "fcrondeny", namesize) == 0) {
174 Set(fcrondeny, ptr2);
176 else if (strncmp(ptr1, "shell", namesize) == 0) {
179 else if (strncmp(ptr1, "sendmail", namesize) == 0) {
182 else if (strncmp(ptr1, "editor", namesize) == 0) {
186 error("Unknown var name at line %s : line ignored", buf);
191 debug(" fcronconf=%s", fcronconf);
192 /* debug(" fcronallow=%s", fcronallow); */
193 /* debug(" fcrondeny=%s", fcrondeny); */
194 /* debug(" fcrontabs=%s", fcrontabs); */
195 /* debug(" pidfile=%s", pidfile); */
196 /* debug(" fifofile=%s", fifofile); */
197 /* debug(" editor=%s", editor); */
198 /* debug(" shell=%s", shell); */
199 /* debug(" sendmail=%s", sendmail); */
202 xfclose_check(&f, fcronconf);