]> granicus.if.org Git - fcron/blob - fcronconf.c
Fixed occasional 1s slippage. Disable fcrondyn if we don't have gettimeofday() (or...
[fcron] / fcronconf.c
1 /*
2  * FCRON - periodic command scheduler
3  *
4  *  Copyright 2000-2014 Thibault Godouet <fcron@free.fr>
5  *
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.
10  *
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.
15  *
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
19  *
20  *  The GNU General Public License can also be found in the file
21  *  `LICENSE' that comes with the fcron source distribution.
22  */
23
24
25 #include "global.h"
26 #include "mem.h"
27 #include "fcronconf.h"
28 #include "subs.h"
29
30 void init_conf(void);
31
32 extern char debug_opt;
33 extern uid_t rootuid;
34 extern gid_t rootgid;
35
36 /* fcron.conf parameters */
37 char *fcronconf = NULL;
38 char *fcrontabs = NULL;
39 char *pidfile = NULL;
40 char *fifofile = NULL;
41 char *fcronallow = NULL;
42 char *fcrondeny = NULL;
43 char *shell = NULL;
44 char *sendmail = NULL;
45 char *editor = NULL;
46
47 void
48 init_conf(void)
49 /* initialises config with compiled in constants */
50 {
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);
59     /* // */
60     /* shell = strdup2(FCRON_SHELL); */
61     shell = strdup2("");
62
63 #ifdef SENDMAIL
64     sendmail = strdup2(SENDMAIL);
65 #endif
66     editor = strdup2(FCRON_EDITOR);
67 }
68
69 void
70 free_conf(void)
71 /* free() the memory allocated in init_conf() */
72 {
73     Free_safe(fcronconf);
74     Free_safe(fcrontabs);
75     Free_safe(pidfile);
76     Free_safe(fifofile);
77     Free_safe(fcronallow);
78     Free_safe(fcrondeny);
79     Free_safe(shell);
80     Free_safe(sendmail);
81     Free_safe(editor);
82 }
83
84 void
85 read_conf(void)
86 /* reads in a config file and updates the necessary global variables */
87 {
88     FILE *f = NULL;
89     struct stat st;
90     char buf[LINE_LEN];
91     char *ptr1 = NULL, *ptr2 = NULL;
92     short namesize = 0;
93     char err_on_enoent = 0;
94     struct group *gr = NULL;
95     gid_t fcrongid = -1;
96
97     if (fcronconf != NULL)
98         /* fcronconf has been set by -c option : file must exist */
99         err_on_enoent = 1;
100
101     init_conf();
102
103     if ((f = fopen(fcronconf, "r")) == NULL) {
104         if (errno == ENOENT) {
105             if (err_on_enoent)
106                 die_e("Could not read %s", fcronconf);
107             else
108                 /* file does not exist, it is not an error  */
109                 return;
110         }
111         else {
112             error_e("Could not read %s : config file ignored", fcronconf);
113             return;
114         }
115     }
116
117     /* get fcrongid */
118     gr = getgrnam(GROUPNAME);
119     if (gr == NULL) {
120         die_e("Unable to find %s in /etc/group", GROUPNAME);
121     }
122     fcrongid = gr->gr_gid;
123
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);
132         return;
133     }
134
135     while ((ptr1 = fgets(buf, sizeof(buf), f)) != NULL) {
136
137         Skip_blanks(ptr1);      /* at the beginning of the line */
138
139         /* ignore comments and blank lines */
140         if (*ptr1 == '#' || *ptr1 == '\n' || *ptr1 == '\0')
141             continue;
142
143         remove_blanks(ptr1);    /* at the end of the line */
144
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);
149
150         ptr2 = ptr1 + namesize;
151
152         /* skip the blanks and the "=" and go to the value */
153         while (isspace((int)*ptr2))
154             ptr2++;
155         if (*ptr2 == '=')
156             ptr2++;
157         while (isspace((int)*ptr2))
158             ptr2++;
159
160         /* find which var the line refers to and update it */
161         if (strncmp(ptr1, "fcrontabs", namesize) == 0) {
162             Set(fcrontabs, ptr2);
163         }
164         else if (strncmp(ptr1, "pidfile", namesize) == 0) {
165             Set(pidfile, ptr2);
166         }
167         else if (strncmp(ptr1, "fifofile", namesize) == 0) {
168             Set(fifofile, ptr2);
169         }
170         else if (strncmp(ptr1, "fcronallow", namesize) == 0) {
171             Set(fcronallow, ptr2);
172         }
173         else if (strncmp(ptr1, "fcrondeny", namesize) == 0) {
174             Set(fcrondeny, ptr2);
175         }
176         else if (strncmp(ptr1, "shell", namesize) == 0) {
177             Set(shell, ptr2);
178         }
179         else if (strncmp(ptr1, "sendmail", namesize) == 0) {
180             Set(sendmail, ptr2);
181         }
182         else if (strncmp(ptr1, "editor", namesize) == 0) {
183             Set(editor, ptr2);
184         }
185         else
186             error("Unknown var name at line %s : line ignored", buf);
187
188     }
189
190     if (debug_opt) {
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); */
200     }
201
202     xfclose_check(&f, fcronconf);
203
204 }