]> granicus.if.org Git - fcron/blob - allow.c
mark as dev release
[fcron] / allow.c
1
2 /*
3  * FCRON - periodic command scheduler 
4  *
5  *  Copyright 2000-2016 Thibault Godouet <fcron@free.fr>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  * 
21  *  The GNU General Public License can also be found in the file
22  *  `LICENSE' that comes with the fcron source distribution.
23  */
24
25
26 #include "fcrontab.h"
27
28 #include "allow.h"
29
30 int
31 in_file(char *str, char *file)
32     /* return -1 if file doesn't exist
33      *        0 if string is not in file,
34      *        1 if it is in file
35      *   and  2 if file contains "all" string */
36 {
37     char buf[LINE_LEN];
38     FILE *f = NULL;
39     char *start = NULL;
40
41     if ((f = fopen(file, "r")) == NULL) {
42         if (errno == ENOENT)
43             /* file does not exist */
44             return -1;
45         else
46             die_e("could not open %s", file);
47     }
48
49     while (fgets(buf, sizeof(buf), f) != NULL) {
50
51         /* skip leading and trailing blanks, comments */
52         start = buf;
53         Skip_blanks(start);
54         if (*start == '#' || *start == '\0')
55             continue;
56         remove_blanks(start);
57
58         if (strcmp(str, start) == 0) {
59             xfclose_check(&f, file);
60             return 1;
61         }
62         if (strcmp(start, "all") == 0) {
63             xfclose_check(&f, file);
64             return 2;
65         }
66     }
67
68     xfclose_check(&f, file);
69     /* if execution gets here, string is not in file */
70     return 0;
71
72
73 }
74
75 int
76 is_allowed(char *user)
77     /* return 1 if user is allowed to use this software
78      * otherwise return 0 */
79 {
80     int allow = 0;
81     int deny = 0;
82
83     /* check if user is in passwd file */
84     if (getpwnam(user) != NULL) {
85
86         /* check if user is in fcron.allow and/or in fcron.deny files */
87         allow = in_file(user, fcronallow);
88         deny = in_file(user, fcrondeny);
89
90         /* in_file() returns:
91          *       -1 if file doesn't exist
92          *        0 if string is not in file,
93          *        1 if it is in file
94          *   and  2 if file contains "all" string */
95
96         if (allow == -1 && deny == -1)
97             /* neither fcron.allow nor fcron.deny exist :
98              * we consider that user is allowed */
99             return 1;
100
101         if (allow == -1 && deny == 0)
102             return 1;
103
104         if (deny == -1 && allow == 1)
105             return 1;
106
107         if (allow == 1 && deny != 1)
108             return 1;
109         if (allow == 2 && deny <= 0)
110             return 1;
111
112     }
113
114     /* if we gets here, user is not allowed */
115
116 #ifdef WITH_AUDIT
117     {
118         int audit_fd = audit_open();
119         audit_log_user_message(audit_fd, AUDIT_USER_START, "fcron deny",
120                                NULL, NULL, NULL, 0);
121         xclose_check(&audit_fd, "audit");
122     }
123 #endif
124
125     return 0;
126
127 }