1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_FILE__
26 #define __WZD_FILE__
27 
28 /** \file wzd_file.h
29  * \brief Files and directories functions
30  *
31  * Permissions are stored in a file present in each directory on the server.
32  * This allows portable function, and features like symbolic links on
33  * systems which does not have links (like windows).
34  *
35  * \addtogroup libwzd_core
36  * @{
37  */
38 
39 /* WARNING !!! filename MUST be ABSOLUTE path !!! */
40 
41 typedef enum {
42   FILE_NOTSET,
43   FILE_REG,
44   FILE_DIR,
45   FILE_LNK,
46   FILE_VFS,
47 } wzd_file_kind_t;
48 
49 typedef struct _wzd_acl_rule_t {
50   char user[256];
51   char perms[3]; /* rwx */
52   struct _wzd_acl_rule_t * next_acl; /* linked list */
53 } wzd_acl_line_t;
54 
55 /** \brief File: name, owner, permissions, etc. */
56 struct wzd_file_t {
57   /** \todo replace with (char*) */
58   char	filename[256];
59   /** \todo replace with uid */
60   char	owner[256];
61   /** \todo replace with uid */
62   char	group[256];
63   unsigned long permissions;	/**< @brief classic linux format */
64   wzd_acl_line_t *acl;
65   wzd_file_kind_t kind;
66   void * data;
67   struct wzd_file_t	*next_file;
68 };
69 
70 
71 int file_open(const char *filename, int mode, unsigned long wanted_right, wzd_context_t * context);
72 
73 void file_close(int fd, wzd_context_t * context);
74 
75 /* wrappers just to keep things in same memory zones */
76 ssize_t file_read(fd_t fd,void *data,size_t length);
77 ssize_t file_write(fd_t fd,const void *data,size_t length);
78 
79 int file_chown(const char *filename, const char *username, const char *groupname, wzd_context_t * context);
80 
81 int file_rename(const char *old_filename, const char *new_filename, wzd_context_t * context);
82 int file_remove(const char *filename, wzd_context_t * context);
83 
84 int file_mkdir(const char *dirname, unsigned int mode, wzd_context_t * context);
85 int file_rmdir(const char *dirname, wzd_context_t * context);
86 
87 fs_off_t file_seek(fd_t fd, fs_off_t offset, int whence);
88 
89 wzd_user_t * file_getowner(const char *filename, wzd_context_t * context);
90 
91 /** \brief Get all permissions on file for specific context */
92 unsigned long file_getperms(struct wzd_file_t * file, wzd_context_t * context);
93 
94 /* symlink operations */
95 int symlink_create(const char *existing, const char *link);
96 int symlink_remove(const char *link);
97 
98 /* returns 1 if file is currently locked, else 0 */
99 int file_lock(fd_t fd, short lock_mode);
100 int file_unlock(fd_t fd);
101 int file_islocked(fd_t fd, short lock_mode);
102 int file_force_unlock(const char *file);
103 
104 /* low-level func */
105 int _checkPerm(const char *filename, unsigned long wanted_right, wzd_user_t *user);
106 
107 /** \brief Check if user is allowed to perform an action, given a file and the directory containing it */
108 int _checkFileForPerm(const char *dir, const char * wanted_file, unsigned long wanted_right, wzd_user_t * user);
109 
110 int _setPerm(const char *filename, const char *granted_user, const char *owner, const char *group, const char * rights, unsigned long perms, wzd_context_t * context);
111 
112 /** \brief Read permission file and decode it */
113 int readPermFile(const char *permfile, struct wzd_file_t **pTabFiles);
114 
115 void file_insert_sorted(struct wzd_file_t *entry, struct wzd_file_t **tab);
116 
117 /** \brief Copy file structure and members */
118 struct wzd_file_t * file_deep_copy(struct wzd_file_t *file_cur);
119 
120 /** \brief Free file list recursively */
121 void free_file_recursive(struct wzd_file_t * file);
122 
123 /** \brief Get file status */
124 struct wzd_file_t * file_stat(const char *filename, wzd_context_t * context);
125 
126 /** @} */
127 
128 #endif /* __WZD_FILE__ */
129 
130