Security Plugin: set http status for URL rule match
[MonkeyD.git] / src / file.c
blob9e19b47f3b3f6e669975c5fb507f8e0c72784bac
1 /*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
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.
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 Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "monkey.h"
29 #include "file.h"
30 #include "user.h"
31 #include "memory.h"
32 #include "utils.h"
34 struct file_info *mk_file_get_info(char *path)
36 struct file_info *f_info;
37 struct stat f, target;
39 /* Stat right resource */
40 if (lstat(path, &f) == -1) {
41 return NULL;
44 f_info = mk_mem_malloc(sizeof(struct file_info));
45 f_info->is_link = MK_FILE_FALSE;
46 f_info->is_directory = MK_FILE_FALSE;
47 f_info->exec_access = MK_FILE_FALSE;
48 f_info->read_access = MK_FILE_FALSE;
50 if (S_ISLNK(f.st_mode)) {
51 f_info->is_link = MK_FILE_TRUE;
52 if (stat(path, &target) == -1) {
53 return NULL;
56 else {
57 target = f;
60 f_info->size = target.st_size;
61 f_info->last_modification = target.st_mtime;
63 if (S_ISDIR(target.st_mode)) {
64 f_info->is_directory = MK_FILE_TRUE;
67 /* Checking read access */
68 if (((target.st_mode & S_IRUSR) && target.st_uid == EUID) ||
69 ((target.st_mode & S_IRGRP) && target.st_gid == EGID) ||
70 (target.st_mode & S_IROTH)) {
71 f_info->read_access = MK_FILE_TRUE;
73 #ifdef TRACE
74 else {
75 MK_TRACE("Target has not read acess");
77 #endif
79 /* Checking execution access */
80 if ((target.st_mode & S_IXUSR && target.st_uid == EUID) ||
81 (target.st_mode & S_IXGRP && target.st_gid == EGID) ||
82 (target.st_mode & S_IXOTH)) {
83 f_info->exec_access = MK_FILE_TRUE;
86 #ifdef TRACE
87 else {
88 MK_TRACE("Target has not execution permission");
90 #endif
92 return f_info;
95 /* Read file content to a memory buffer,
96 * Use this function just for really SMALL files
98 char *mk_file_to_buffer(char *path)
100 FILE *fp;
101 char *buffer;
102 long bytes;
103 struct file_info *finfo;
105 if (!(finfo = mk_file_get_info(path))) {
106 return NULL;
109 if (!(fp = fopen(path, "r"))) {
110 return NULL;
113 buffer = calloc(finfo->size + 1, sizeof(char));
114 if (!buffer) {
115 fclose(fp);
116 return NULL;
119 bytes = fread(buffer, finfo->size, 1, fp);
121 if (bytes < 1) {
122 mk_mem_free(buffer);
123 fclose(fp);
124 return NULL;
127 fclose(fp);
128 return (char *) buffer;