source code open - smack
[framework/security/smack.git] / utils / smackd.c
1 /*
2  * This file is part of libsmack
3  *
4  * Copyright (C) 2011 Intel Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * version 2.1 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  *
20  * Authors:
21  * Brian McGillion <brian.mcgillion@intel.com>
22  */
23
24 #include "common.h"
25 #include <signal.h>
26 #include <syslog.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <sys/inotify.h>
35 #include <sys/select.h>
36 #include <sys/stat.h>
37
38 #define PID_FILE "/var/run/smackd.pid"
39 #define BUF_SIZE (4 * (sizeof(struct inotify_event) + NAME_MAX + 1))
40
41 #define ACCESS_FD 0
42 #define CIPSO_FD 1
43
44 int notify_handles[2];
45 static volatile sig_atomic_t terminate = 0;
46 static volatile sig_atomic_t restart = 0;
47
48 enum mask_action {
49         CREATE,
50         MODIFY,
51         DELETE
52 };
53
54 static void clear_all_rules()
55 {
56         if (clear() == -1)
57                 syslog(LOG_ERR, "Failed to clear all rules");
58 }
59
60 static void load_all_rules()
61 {
62         if (apply_rules(ACCESSES_D_PATH, 0))
63                 syslog(LOG_DEBUG, "Failed to load all rules");
64 }
65
66 static void signal_handler(int sig)
67 {
68         switch (sig) {
69         case SIGTERM:
70                 terminate = 1;
71                 break;
72         case SIGHUP:
73                 restart = 1;
74                 break;
75         default:
76                 syslog(LOG_DEBUG, "Unrequested signal : %d", sig);
77                 break;
78         }
79 }
80
81 static int lockPidFile()
82 {
83         int fd;
84         struct flock lock;
85         char buf[BUF_SIZE];
86
87         fd = open(PID_FILE, O_RDWR | O_CREAT | O_CLOEXEC,
88                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
89         if (fd < 0) {
90                 syslog(LOG_ERR, "Failed to open (%s) : %m", PID_FILE);
91                 return -1;
92         }
93
94         lock.l_len = 0;
95         lock.l_start = 0;
96         lock.l_type = F_WRLCK;
97         lock.l_whence = SEEK_SET;
98
99         if (fcntl(fd, F_SETLK, &lock) < 0) {
100                 if (errno == EACCES || errno == EAGAIN) {
101                         syslog(LOG_ERR, "Daemon is already running (%s) : %m", PID_FILE);
102                 }
103                 else
104                         syslog(LOG_ERR, "Could not lock PID_FILE (%s) : %m", PID_FILE);
105
106                 close(fd);
107                 return -1;
108         }
109
110         if (ftruncate(fd, 0) < 0) {
111                 syslog(LOG_ERR, "Could not truncate PID_FILE (%s) : %m", PID_FILE);
112                 close(fd);
113                 return -1;
114         }
115
116         snprintf(buf, BUF_SIZE, "%ld\n", (long)getpid());
117         if (write(fd, buf, strlen(buf)) != strlen(buf)) {
118                 syslog(LOG_ERR, "Could not write to PID_FILE (%s) : %m", PID_FILE);
119                 close(fd);
120                 return -1;
121         }
122
123         return fd;
124 }
125
126 static int daemonize()
127 {
128         int maxfd, fd;
129
130         switch (fork()) {
131         case -1:
132                 syslog(LOG_ERR, "Failed to fork : %m");
133                 return -1;
134         case 0:
135                 break;
136         default:
137                 exit(EXIT_SUCCESS);
138         }
139
140         if (setsid() < 0)
141                 return -1;
142
143         //do not regain a terminal
144         switch (fork()) {
145         case -1:
146                 syslog(LOG_ERR, "Failed to fork (2) : %m");
147                 return -1;
148         case 0:
149                 break;
150         default:
151                 exit(EXIT_SUCCESS);
152         }
153
154         umask(0);
155
156         if (chdir("/") < 0)
157                 syslog(LOG_ERR, "Failed to chdir '/' : %m");
158
159         maxfd = sysconf(_SC_OPEN_MAX);
160         maxfd = maxfd != -1 ? maxfd : 4096;
161
162         for (fd = 0; fd < maxfd; fd++)
163                 close(fd);
164
165         if (!freopen("/dev/null", "r", stdin))
166                 syslog(LOG_DEBUG, "Failed to reopen stdin : %m");
167         if(!freopen("/dev/null", "w", stdout))
168                 syslog(LOG_DEBUG, "Failed to reopen stout : %m");
169         if(!freopen("/dev/null", "w", stderr))
170                 syslog(LOG_DEBUG, "Failed to reopen sterr : %m");
171
172         return lockPidFile();
173 }
174
175 static int configure_inotify()
176 {
177         int inotifyFd, fd;
178
179         inotifyFd = inotify_init();
180         if (inotifyFd < 0) {
181                 syslog(LOG_ERR, "Failed to init inotify : %m");
182                 return -1;
183         }
184
185         fd = inotify_add_watch(inotifyFd, ACCESSES_D_PATH,
186                                IN_DELETE | IN_CLOSE_WRITE | IN_MOVE);
187         if (fd < 0) {
188                 syslog(LOG_ERR, "Failed to inotify_add_watch (%s) : %m",
189                        ACCESSES_D_PATH);
190                 return -1;
191         }
192
193         notify_handles[ACCESS_FD] = fd;
194
195         fd = inotify_add_watch(inotifyFd, CIPSO_D_PATH,
196                                IN_DELETE | IN_CLOSE_WRITE | IN_MOVE);
197         if (fd < 0) {
198                 syslog(LOG_ERR, "Failed to inotify_add_watch (%s) : %m",
199                        CIPSO_D_PATH);
200                 return -1;
201         }
202
203         notify_handles[CIPSO_FD] = fd;
204
205         return inotifyFd;
206 }
207
208 static void modify_access_rules(char *file, enum mask_action action)
209 {
210         char path[PATH_MAX];
211         int ret;
212
213         sprintf(path,"%s/%s", ACCESSES_D_PATH, file);
214
215         if (action == CREATE)
216                 ret = apply_rules(path, 0);
217         else if (action == MODIFY) {
218                 ret = apply_rules(path, 1);
219                 ret = apply_rules(path, 0);
220         }
221
222         if (ret)
223                 syslog(LOG_ERR, "Failed load access rules (%s), action (%d) :%m",
224                        path, action);
225 }
226
227 static void modify_cipso_rules(char *file)
228 {
229         char path[PATH_MAX];
230         sprintf(path,"%s/%s", CIPSO_D_PATH, file);
231
232         if (apply_cipso(path))
233                 syslog(LOG_ERR, "Failed to load cipso rules (%s) : %m", path);
234 }
235
236 static int handle_inotify_event(int inotifyFd)
237 {
238         struct inotify_event *event;
239         char buf[BUF_SIZE];
240         ssize_t num_read;
241         char *head;
242         enum mask_action action;
243         int del = 0;
244         int size = sizeof(struct inotify_event);
245
246         num_read = read(inotifyFd, buf, BUF_SIZE);
247         if (num_read <= 0) {
248                 syslog(LOG_ERR, "Error reading inotify event : %m");
249                 return -1;
250         }
251
252         for (head = buf; head < buf + num_read; head += size + event->len) {
253                 event = (struct inotify_event *) head;
254
255                 if (event->mask & IN_MOVED_TO)
256                         action = CREATE;
257                 else if (event->mask & IN_CLOSE_WRITE)
258                         action = MODIFY;
259                 else if (event->mask & IN_DELETE || event->mask & IN_MOVED_FROM) {
260                         del = 1;
261                         continue;
262                 }
263
264                 if (event->wd == notify_handles[ACCESS_FD])
265                         modify_access_rules(event->name, action);
266                 else if (event->wd == notify_handles[CIPSO_FD])
267                         modify_cipso_rules(event->name);
268         }
269
270         if (del) {
271                 //at least one file was removed so we should reparse the rules
272                 clear_all_rules();
273                 load_all_rules();
274         }
275
276         return 0;
277 }
278
279 static int monitor(int inotifyFd)
280 {
281         fd_set readSet;
282         FD_ZERO(&readSet);
283         FD_SET(inotifyFd, &readSet);
284
285         return select(inotifyFd + 1, &readSet, NULL, NULL, NULL);
286 }
287
288 int main(int argc, char **argv)
289 {
290         struct sigaction sa;
291         int inotify_fd;
292         int ret;
293         int pid_fd;
294
295         sigemptyset(&sa.sa_mask);
296         sa.sa_handler = signal_handler;
297         sa.sa_flags = SA_RESTART;
298
299         if (sigaction(SIGHUP, &sa, NULL) < 0) {
300                 syslog(LOG_ERR, "failed to listen for signal SIGHUP : %m");
301                 exit(EXIT_FAILURE);
302         }
303
304         if (sigaction(SIGTERM, &sa, NULL) < 0) {
305                 syslog(LOG_ERR, "failed to listen for signal SIGTERM : %m");
306                 exit(EXIT_FAILURE);
307         }
308
309         pid_fd = daemonize();
310         if (pid_fd < 0)
311                 exit(EXIT_FAILURE);
312
313         clear_all_rules();
314         load_all_rules();
315
316         inotify_fd = configure_inotify();
317
318         while (inotify_fd >= 0 && !terminate && !restart) {
319                 ret = monitor(inotify_fd);
320                 if (ret < 0 && errno == EINTR) {
321                         continue;
322                 }
323                 else if (ret < 0) {
324                         syslog(LOG_ERR, "Failed to monitor properly : %m");
325                         break;
326                 }
327
328                 ret = handle_inotify_event(inotify_fd);
329                 if (ret < 0)
330                         break;
331         }
332
333         close(pid_fd);
334         remove(PID_FILE);
335
336         if (restart && execv(argv[0], argv))
337                 syslog(LOG_ERR, "Failed to restart : %m");
338
339         clear_all_rules();
340
341         syslog(LOG_DEBUG, "Finished %s", argv[0]);
342         exit(terminate == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
343 }