]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/xacl.h
19df6539d6e30a75af64856ef88535e07759a24f
[bacula/bacula] / bacula / src / filed / xacl.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18  */
19 /**
20  * Major refactoring of ACL and XATTR code written by:
21  *
22  *  RadosÅ‚aw Korzeniewski, MMXVI
23  *  radoslaw@korzeniewski.net, radekk@inteos.pl
24  *  Inteos Sp. z o.o. http://www.inteos.pl/
25  *
26  */
27
28 #ifndef __BXACL_H_
29 #define __BXACL_H_
30
31 /*
32  * Magic used in the magic field of the xattr struct.
33  * This way we can see if we encounter a valid xattr struct.
34  * Used for backward compatibility only.
35  */
36 #define XATTR_MAGIC 0x5C5884
37
38 /*
39  * Return value status enumeration
40  * You have an error when value is less then zero.
41  * You have a positive status when value is not negative
42  * (greater or equal to zero).
43  */
44 enum bRC_XACL {
45    bRC_XACL_inval = -3,       // input data invalid
46    bRC_XACL_fatal = -2,       // a fatal error
47    bRC_XACL_error = -1,       // standard error
48    bRC_XACL_ok    = 0,        // success
49    bRC_XACL_skip  = 1,        // processing should skip current runtime
50    bRC_XACL_cont  = 2         // processing should skip current element
51                               // and continue with next one
52 };
53
54 /*
55  * We support the following types of ACLs
56  */
57 typedef enum {
58    XACL_TYPE_NONE             = 0,
59    XACL_TYPE_ACCESS           = 1,
60    XACL_TYPE_DEFAULT          = 2,
61    XACL_TYPE_DEFAULT_DIR      = 3,
62    XACL_TYPE_EXTENDED         = 4,
63    XACL_TYPE_NFS4             = 5,
64    XACL_TYPE_PLUGIN           = 6
65 } XACL_type;
66
67 /*
68  * Flags which control what ACL/XATTR engine
69  * to use for backup/restore
70  */
71 #define XACL_FLAG_NONE        0
72 #define XACL_FLAG_NATIVE      0x01
73 #define XACL_FLAG_AFS         0x02
74 #define XACL_FLAG_PLUGIN      0x04
75
76 /*
77  * Ensure we have none
78  */
79 #ifndef ACL_TYPE_NONE
80 #define ACL_TYPE_NONE         0x0
81 #endif
82
83 /*
84  * Extended attribute (xattr) list element.
85  *
86  * Every xattr consist of a Key=>Value pair where
87  * both could be a binary data.
88  */
89 struct XACL_xattr {
90    uint32_t name_len;
91    char *name;
92    uint32_t value_len;
93    char *value;
94 };
95
96 /*
97  * Basic ACL/XATTR class which is a foundation for any other OS specyfic implementation.
98  *
99  * This class cannot be used directly as it is an abstraction class with a lot of virtual
100  * methods laying around. As a basic class it has all public API available for backup and
101  * restore functionality. As a bonus it handles all ACL/XATTR generic functions and OS
102  * independent API, i.e. for AFS ACL/XATTR or Plugins ACL/XATTR (future functionality).
103  */
104 class XACL {
105 private:
106    bool acl_ena;
107    bool xattr_ena;
108    uint32_t flags;
109    uint32_t current_dev;
110    POOLMEM *content;
111    uint32_t content_len;
112    uint32_t acl_nr_errors;
113    uint32_t xattr_nr_errors;
114    const int *acl_streams;
115    const int *default_acl_streams;
116    const int *xattr_streams;
117    const char **xattr_skiplist;
118    const char **xattr_acl_skiplist;
119
120    void init();
121
122    /**
123     * Perform OS specyfic ACL backup.
124     * in:
125     *    jcr - Job Control Record
126     *    ff_pkt - file to backup information rector
127     * out:
128     *    bRC_XACL_ok - backup performed without problems
129     *    any other - some error ocurred
130     */
131    virtual bRC_XACL os_backup_acl (JCR *jcr, FF_PKT *ff_pkt){return bRC_XACL_fatal;};
132
133    /**
134     * Perform OS specyfic ACL restore. Runtime is called only when stream is supported by OS.
135     * in:
136     *    jcr - Job Control Record
137     *    ff_pkt - file to backup information rector
138     * out:
139     *    bRC_XACL_ok - backup performed without problems
140     *    any other - some error ocurred
141     */
142    virtual bRC_XACL os_restore_acl (JCR *jcr, int stream, char *content, uint32_t length){return bRC_XACL_fatal;};
143
144    /**
145     * Perform OS specyfic XATTR backup.
146     *
147     * in:
148     *    jcr - Job Control Record
149     *    ff_pkt - file to backup control package
150     * out:
151     *    bRC_XACL_ok - xattr backup ok or no xattr to backup found
152     *    bRC_XACL_error/fatal - an error or fatal error occurred
153     */
154    virtual bRC_XACL os_backup_xattr (JCR *jcr, FF_PKT *ff_pkt){return bRC_XACL_fatal;};
155
156    /**
157     * Perform OS specyfic XATTR restore. Runtime is called only when stream is supported by OS.
158     *
159     * in:
160     *    jcr - Job Control Record
161     *    stream - backup stream number
162     *    content - a buffer with data to restore
163     *    length - a data restore length
164     * out:
165     *    bRC_XACL_ok - xattr backup ok or no xattr to backup found
166     *    bRC_XACL_error/fatal - an error or fatal error occurred
167     */
168    virtual bRC_XACL os_restore_xattr (JCR *jcr, int stream, char *content, uint32_t length){return bRC_XACL_fatal;};
169
170    /**
171     * Low level OS specyfic runtime to get ACL data from file. The ACL data is set in internal content buffer.
172     *
173     * in:
174     *    jcr - Job Control Record
175     *    xacltype - the acl type to restore
176     * out:
177     *    bRC_XACL_ok -
178     *    bRC_XACL_error/fatal - an error or fatal error occurred
179     */
180    virtual bRC_XACL os_get_acl (JCR *jcr, XACL_type xacltype){return bRC_XACL_fatal;};
181
182    /**
183     * Low level OS specyfic runtime to set ACL data on file.
184     *
185     * in:
186     *    jcr - Job Control Record
187     *    xacltype - the acl type to restore
188     *    content - a buffer with data to restore
189     *    length - a data restore length
190     * out:
191     *    bRC_XACL_ok -
192     *    bRC_XACL_error/fatal - an error or fatal error occurred
193     */
194    virtual bRC_XACL os_set_acl (JCR *jcr, XACL_type xacltype, char *content, uint32_t length){return bRC_XACL_fatal;};
195
196    /**
197     * Returns a list of xattr names in newly allocated pool memory and a length of the allocated buffer.
198     * It allocates a memory with poolmem subroutines every time a function is called, so it must be freed
199     * when not needed. The list of xattr names is returned as an unordered array of NULL terminated
200     * character strings (attribute names are separated by NULL characters), like this:
201     *  user.name1\0system.name1\0user.name2\0
202     * The format of the list is based on standard "llistxattr" function call.
203     * TODO: change the format of the list from an array of NULL terminated strings into an alist of structures.
204     *
205     * in:
206     *    jcr - Job Control Record
207     *    xlen - non NULL pointer to the uint32_t variable for storing a length of the xattr names list
208     *    pxlist - non NULL pointer to the char* variable for allocating a memoty data for xattr names list
209     * out:
210     *    bRC_XACL_ok - we've got a xattr data to backup
211     *    bRC_XACL_skip - no xattr data available, no fatal error, skip rest of the runtime
212     *    bRC_XACL_fatal - when required buffers are unallocated
213     *    bRC_XACL_error - in case of any error
214     */
215    virtual bRC_XACL os_get_xattr_names (JCR *jcr, POOLMEM ** pxlist, uint32_t * xlen){return bRC_XACL_fatal;};
216
217    /**
218     * Returns a value of the requested attribute name and a length of the allocated buffer.
219     * It allocates a memory with poolmem subroutines every time a function is called, so it must be freed
220     * when not needed.
221     *
222     * in:
223     *    jcr - Job Control Record
224     *    name - a name of the extended attribute
225     *    pvalue - the pointer for the buffer with value - it is allocated by function and should be freed when no needed
226     *    plen - the pointer for the length of the allocated buffer
227     *
228     * out:
229     *    pxlist - the atributes list
230     *    bRC_XACL_ok - we've got a xattr data which could be empty when xlen=0
231     *    bRC_XACL_skip - no xattr data available, no fatal error, skip rest of the runtime
232     *    bRC_XACL_error - error getting an attribute
233     *    bRC_XACL_fatal - required buffers are unallocated
234     */
235    virtual bRC_XACL os_get_xattr_value (JCR *jcr, char * name, char ** pvalue, uint32_t * plen){return bRC_XACL_fatal;};
236
237    /**
238     * Low level OS specyfic runtime to set extended attribute on file
239     *
240     * in:
241     *    jcr - Job Control Record
242     *    xattr - the struct with attribute/value to set
243     *
244     * out:
245     *    bRC_XACL_ok - setting the attribute was ok
246     *    bRC_XACL_error - error during extattribute set
247     *    bRC_XACL_fatal - required buffers are unallocated
248     */
249    virtual bRC_XACL os_set_xattr (JCR *jcr, XACL_xattr *xattr){return bRC_XACL_fatal;};
250
251    void inc_acl_errors(){ acl_nr_errors++;};
252    void inc_xattr_errors(){ xattr_nr_errors++;};
253    bRC_XACL check_dev (JCR *jcr);
254    void check_dev (JCR *jcr, uint32_t dev);
255
256 public:
257    XACL ();
258    virtual ~XACL();
259
260    /* enable/disable functionality */
261    void enable_acl();
262    void disable_acl();
263    void enable_xattr();
264    void disable_xattr();
265
266    /*
267     * public methods used outside the class or derivatives
268     */
269    bRC_XACL backup_acl (JCR *jcr, FF_PKT *ff_pkt);
270    bRC_XACL restore_acl (JCR *jcr, int stream, char *content, uint32_t content_length);
271    bRC_XACL backup_xattr (JCR *jcr, FF_PKT *ff_pkt);
272    bRC_XACL restore_xattr (JCR *jcr, int stream, char *content, uint32_t content_length);
273
274    /* utility functions */
275    inline uint32_t get_acl_nr_errors(){ return acl_nr_errors;};
276    inline uint32_t get_xattr_nr_errors(){ return xattr_nr_errors;};
277    void set_acl_streams (const int *pacl, const int *pacl_def);
278    void set_xattr_streams (const int *pxattr);
279    void set_xattr_skiplists (const char **pxattr, const char **pxattr_acl);
280    inline void clear_flag (uint32_t flag){ flags &= ~flag;};
281    inline void set_flag (uint32_t flag){ flags |= flag;};
282    POOLMEM * set_content (char *text);
283    POOLMEM * set_content(char *data, int len);
284    inline POOLMEM * get_content (void){ return content;};
285    inline uint32_t get_content_size (void){ return sizeof_pool_memory(content);};
286    inline uint32_t get_content_len (void){ return content_len;};
287    bool check_xattr_skiplists (JCR *jcr, FF_PKT *ff_pkt, char * name);
288
289    /* sending data to the storage */
290    bRC_XACL send_acl_stream (JCR *jcr, int stream);
291    bRC_XACL send_xattr_stream (JCR *jcr, int stream);
292
293    /* serialize / unserialize stream */
294    bRC_XACL unserialize_xattr_stream(JCR *jcr, char *content, uint32_t length, alist *list);
295    bRC_XACL serialize_xattr_stream(JCR *jcr, uint32_t len, alist *list);
296
297    /* generic functions */
298    bRC_XACL generic_backup_acl (JCR *jcr, FF_PKT *ff_pkt);
299    bRC_XACL generic_restore_acl (JCR *jcr, int stream);
300    bRC_XACL afs_backup_acl (JCR *jcr, FF_PKT *ff_pkt);
301    bRC_XACL afs_restore_acl (JCR *jcr, int stream);
302    bRC_XACL generic_backup_xattr (JCR *jcr, FF_PKT *ff_pkt);
303    bRC_XACL generic_restore_xattr (JCR *jcr, int stream);
304 };
305
306 void *new_xacl();
307
308 #endif /* __BXACL_H_ */