1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2020 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation, which is
11    listed in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Application Programming Interface (API) definition for Bareos Plugins
25  *
26  * Kern Sibbald, October 2007
27  */
28 
29 #ifndef BAREOS_FILED_FD_PLUGINS_H_
30 #define BAREOS_FILED_FD_PLUGINS_H_
31 
32 #ifndef BAREOS_INCLUDE_BAREOS_H_
33 #  ifdef __cplusplus
34 /* Workaround for SGI IRIX 6.5 */
35 #    define _LANGUAGE_C_PLUS_PLUS 1
36 #  endif
37 #  define _REENTRANT 1
38 #  define _THREAD_SAFE 1
39 #  define _POSIX_PTHREAD_SEMANTICS 1
40 #  define _FILE_OFFSET_BITS 64
41 #  define _LARGEFILE_SOURCE 1
42 #  define _LARGE_FILES 1
43 #endif /* ! BAREOS_INCLUDE_BAREOS_H_ */
44 
45 #include <sys/types.h>
46 
47 #include "include/config.h"
48 
49 #include "lib/version.h"
50 #include "include/bc_types.h"
51 #include "include/fileopts.h"
52 #include "lib/plugins.h"
53 #include <sys/stat.h>
54 
55 #ifdef HAVE_WIN32
56 #  pragma GCC diagnostic push
57 #  pragma GCC diagnostic ignored "-Wunknown-pragmas"
58 #  include "vss.h"
59 #  pragma GCC diagnostic pop
60 #endif
61 
62 #ifdef FILE_DAEMON
63 struct BareosWinFilePacket;
64 struct FindFilesPacket;
65 #endif /* FILE_DAEMON */
66 
67 class alist;
68 
69 namespace filedaemon {
70 
71 /*
72  * This packet is used for the restore objects
73  *  It is passed to the plugin when restoring
74  *  the object.
75  */
76 struct restore_object_pkt {
77   int32_t pkt_size;           /* Size of this packet */
78   char* object_name;          /* Object name */
79   char* object;               /* Restore object data to save */
80   char* plugin_name;          /* Plugin name */
81   int32_t object_type;        /* FT_xx for this file */
82   int32_t object_len;         /* Restore object length */
83   int32_t object_full_len;    /* Restore object uncompressed length */
84   int32_t object_index;       /* Restore object index */
85   int32_t object_compression; /* Set to compression type */
86   int32_t stream;             /* Attribute stream id */
87   uint32_t JobId;             /* JobId object came from */
88   int32_t pkt_end;            /* End packet sentinel */
89 };
90 
91 /*
92  * This packet is used for file save info transfer.
93  */
94 struct save_pkt {
95   int32_t pkt_size;        /* Size of this packet */
96   char* fname;             /* Full path and filename */
97   char* link;              /* Link name if any */
98   struct stat statp;       /* System stat() packet for file */
99   int32_t type;            /* FT_xx for this file */
100   char flags[FOPTS_BYTES]; /* Bareos internal flags */
101   bool no_read;            /* During the save, the file should not be saved */
102   bool portable;           /* Set if data format is portable */
103   bool accurate_found; /* Found in accurate list (valid after CheckChanges()) */
104   char* cmd;           /* Command */
105   time_t save_time;    /* Start of incremental time */
106   uint32_t delta_seq;  /* Delta sequence number */
107   char* object_name;   /* Object name to create */
108   char* object;        /* Restore object data to save */
109   int32_t object_len;  /* Restore object length */
110   int32_t index;       /* Restore object index */
111   int32_t pkt_end;     /* End packet sentinel */
112 };
113 
114 /*
115  * This packet is used for file restore info transfer.
116  */
117 struct restore_pkt {
118   int32_t pkt_size;       /* Size of this packet */
119   int32_t stream;         /* Attribute stream id */
120   int32_t data_stream;    /* Id of data stream to follow */
121   int32_t type;           /* File type FT */
122   int32_t file_index;     /* File index */
123   int32_t LinkFI;         /* File index to data if hard link */
124   uid_t uid;              /* Userid */
125   struct stat statp;      /* Decoded stat packet */
126   const char* attrEx;     /* Extended attributes if any */
127   const char* ofname;     /* Output filename */
128   const char* olname;     /* Output link name */
129   const char* where;      /* Where */
130   const char* RegexWhere; /* Regex where */
131   int replace;            /* Replace flag */
132   int create_status;      /* Status from createFile() */
133   uint32_t delta_seq;     /* Delta sequence number */
134   int32_t pkt_end;        /* End packet sentinel */
135 };
136 
137 enum
138 {
139   IO_OPEN = 1,
140   IO_READ = 2,
141   IO_WRITE = 3,
142   IO_CLOSE = 4,
143   IO_SEEK = 5
144 };
145 
146 struct io_pkt {
147   int32_t pkt_size;  /* Size of this packet */
148   int32_t func;      /* Function code */
149   int32_t count;     /* Read/write count */
150   int32_t flags;     /* Open flags */
151   mode_t mode;       /* Permissions for created files */
152   char* buf;         /* Read/write buffer */
153   const char* fname; /* Open filename */
154   int32_t status;    /* Return status */
155   int32_t io_errno;  /* Errno code */
156   int32_t lerror;    /* Win32 error code */
157   int32_t whence;    /* Lseek argument */
158   boffset_t offset;  /* Lseek argument */
159   bool win32;        /* Win32 GetLastError returned */
160   int32_t pkt_end;   /* End packet sentinel */
161 };
162 
163 struct acl_pkt {
164   int32_t pkt_size;        /* Size of this packet */
165   const char* fname;       /* Full path and filename */
166   uint32_t content_length; /* ACL content length */
167   char* content;           /* ACL content */
168   int32_t pkt_end;         /* End packet sentinel */
169 };
170 
171 struct xattr_pkt {
172   int32_t pkt_size;      /* Size of this packet */
173   const char* fname;     /* Full path and filename */
174   uint32_t name_length;  /* XATTR name length */
175   char* name;            /* XATTR name */
176   uint32_t value_length; /* XATTR value length */
177   char* value;           /* XATTR value */
178   int32_t pkt_end;       /* End packet sentinel */
179 };
180 
181 /****************************************************************************
182  *                                                                          *
183  *                Bareos definitions                                        *
184  *                                                                          *
185  ****************************************************************************/
186 
187 /*
188  * Bareos Variable Ids
189  */
190 typedef enum
191 {
192   bVarJobId = 1,
193   bVarFDName = 2,
194   bVarLevel = 3,
195   bVarType = 4,
196   bVarClient = 5,
197   bVarJobName = 6,
198   bVarJobStatus = 7,
199   bVarSinceTime = 8,
200   bVarAccurate = 9,
201   bVarFileSeen = 10,
202   bVarVssClient = 11,
203   bVarWorkingDir = 12,
204   bVarWhere = 13,
205   bVarRegexWhere = 14,
206   bVarExePath = 15,
207   bVarVersion = 16,
208   bVarDistName = 17,
209   bVarPrevJobName = 18,
210   bVarPrefixLinks = 19
211 } bVariable;
212 
213 /*
214  * Events that are passed to plugin
215  */
216 typedef enum
217 {
218   bEventJobStart = 1,
219   bEventJobEnd = 2,
220   bEventStartBackupJob = 3,
221   bEventEndBackupJob = 4,
222   bEventStartRestoreJob = 5,
223   bEventEndRestoreJob = 6,
224   bEventStartVerifyJob = 7,
225   bEventEndVerifyJob = 8,
226   bEventBackupCommand = 9,
227   bEventRestoreCommand = 10,
228   bEventEstimateCommand = 11,
229   bEventLevel = 12,
230   bEventSince = 13,
231   bEventCancelCommand = 14,
232   bEventRestoreObject = 15,
233   bEventEndFileSet = 16,
234   bEventPluginCommand = 17,
235   bEventOptionPlugin = 18,
236   bEventHandleBackupFile = 19,
237   bEventNewPluginOptions = 20,
238   bEventVssInitializeForBackup = 21,
239   bEventVssInitializeForRestore = 22,
240   bEventVssSetBackupState = 23,
241   bEventVssPrepareForBackup = 24,
242   bEventVssBackupAddComponents = 25,
243   bEventVssPrepareSnapshot = 26,
244   bEventVssCreateSnapshots = 27,
245   bEventVssRestoreLoadComponentMetadata = 28,
246   bEventVssRestoreSetComponentsSelected = 29,
247   bEventVssCloseRestore = 30,
248   bEventVssBackupComplete = 31
249 } bEventType;
250 
251 #define FD_NR_EVENTS bEventVssBackupComplete /* keep this updated ! */
252 
253 typedef struct s_bEvent {
254   uint32_t eventType;
255 } bEvent;
256 
257 typedef struct s_bareosInfo {
258   uint32_t size;
259   uint32_t version;
260 } PluginApiDefinition;
261 
262 /*
263  * Bareos Core Routines -- not used within a plugin
264  */
265 #ifdef FILE_DAEMON
266 void LoadFdPlugins(const char* plugin_dir, alist* plugin_names);
267 void UnloadFdPlugins(void);
268 int ListFdPlugins(PoolMem& msg);
269 void NewPlugins(JobControlRecord* jcr);
270 void FreePlugins(JobControlRecord* jcr);
271 bRC GeneratePluginEvent(JobControlRecord* jcr,
272                         bEventType event,
273                         void* value = NULL,
274                         bool reverse = false);
275 bool SendPluginName(JobControlRecord* jcr, BareosSocket* sd, bool start);
276 bool PluginNameStream(JobControlRecord* jcr, char* name);
277 int PluginCreateFile(JobControlRecord* jcr,
278                      Attributes* attr,
279                      BareosWinFilePacket* bfd,
280                      int replace);
281 bool PluginSetAttributes(JobControlRecord* jcr,
282                          Attributes* attr,
283                          BareosWinFilePacket* ofd);
284 bacl_exit_code PluginBuildAclStreams(JobControlRecord* jcr,
285                                      AclData* acl_data,
286                                      FindFilesPacket* ff_pkt);
287 bacl_exit_code plugin_parse_acl_streams(JobControlRecord* jcr,
288                                         AclData* acl_data,
289                                         int stream,
290                                         char* content,
291                                         uint32_t content_length);
292 BxattrExitCode PluginBuildXattrStreams(JobControlRecord* jcr,
293                                        struct XattrData* xattr_data,
294                                        FindFilesPacket* ff_pkt);
295 BxattrExitCode PluginParseXattrStreams(JobControlRecord* jcr,
296                                        struct XattrData* xattr_data,
297                                        int stream,
298                                        char* content,
299                                        uint32_t content_length);
300 int PluginSave(JobControlRecord* jcr, FindFilesPacket* ff_pkt, bool top_level);
301 int PluginEstimate(JobControlRecord* jcr,
302                    FindFilesPacket* ff_pkt,
303                    bool top_level);
304 bool PluginCheckFile(JobControlRecord* jcr, char* fname);
305 void PluginUpdateFfPkt(FindFilesPacket* ff_pkt, struct save_pkt* sp);
306 bRC PluginOptionHandleFile(JobControlRecord* jcr,
307                            FindFilesPacket* ff_pkt,
308                            struct save_pkt* sp);
309 #endif
310 
311 #ifdef __cplusplus
312 extern "C" {
313 #endif
314 
315 /*
316  * Bareos interface version and function pointers --
317  *  i.e. callbacks from the plugin to Bareos
318  */
319 typedef struct s_bareosFuncs {
320   uint32_t size;
321   uint32_t version;
322   bRC (*registerBareosEvents)(PluginContext* ctx, int nr_events, ...);
323   bRC (*unregisterBareosEvents)(PluginContext* ctx, int nr_events, ...);
324   bRC (*getInstanceCount)(PluginContext* ctx, int* ret);
325   bRC (*getBareosValue)(PluginContext* ctx, bVariable var, void* value);
326   bRC (*setBareosValue)(PluginContext* ctx, bVariable var, void* value);
327   bRC (*JobMessage)(PluginContext* ctx,
328                     const char* file,
329                     int line,
330                     int type,
331                     utime_t mtime,
332                     const char* fmt,
333                     ...);
334   bRC (*DebugMessage)(PluginContext* ctx,
335                       const char* file,
336                       int line,
337                       int level,
338                       const char* fmt,
339                       ...);
340   void* (*bareosMalloc)(PluginContext* ctx,
341                         const char* file,
342                         int line,
343                         size_t size);
344   void (*bareosFree)(PluginContext* ctx, const char* file, int line, void* mem);
345   bRC (*AddExclude)(PluginContext* ctx, const char* file);
346   bRC (*AddInclude)(PluginContext* ctx, const char* file);
347   bRC (*AddOptions)(PluginContext* ctx, const char* opts);
348   bRC (*AddRegex)(PluginContext* ctx, const char* item, int type);
349   bRC (*AddWild)(PluginContext* ctx, const char* item, int type);
350   bRC (*NewOptions)(PluginContext* ctx);
351   bRC (*NewInclude)(PluginContext* ctx);
352   bRC (*NewPreInclude)(PluginContext* ctx);
353   bRC (*checkChanges)(PluginContext* ctx, struct save_pkt* sp);
354   bRC (*AcceptFile)(PluginContext* ctx,
355                     struct save_pkt* sp); /* Need fname and statp */
356   bRC (*SetSeenBitmap)(PluginContext* ctx, bool all, char* fname);
357   bRC (*ClearSeenBitmap)(PluginContext* ctx, bool all, char* fname);
358 } CoreFunctions;
359 
360 /****************************************************************************
361  *                                                                          *
362  *                Plugin definitions                                        *
363  *                                                                          *
364  ****************************************************************************/
365 
366 typedef enum
367 {
368   pVarName = 1,
369   pVarDescription = 2
370 } pVariable;
371 
372 #define FD_PLUGIN_MAGIC "*FDPluginData*"
373 #define FD_PLUGIN_INTERFACE_VERSION 10
374 
375 /*
376  * This is a set of function pointers that Bareos can call within the plugin.
377  */
378 typedef struct s_pluginFuncs {
379   uint32_t size;
380   uint32_t version;
381   bRC (*newPlugin)(PluginContext* ctx);
382   bRC (*freePlugin)(PluginContext* ctx);
383   bRC (*getPluginValue)(PluginContext* ctx, pVariable var, void* value);
384   bRC (*setPluginValue)(PluginContext* ctx, pVariable var, void* value);
385   bRC (*handlePluginEvent)(PluginContext* ctx, bEvent* event, void* value);
386   bRC (*startBackupFile)(PluginContext* ctx, struct save_pkt* sp);
387   bRC (*endBackupFile)(PluginContext* ctx);
388   bRC (*startRestoreFile)(PluginContext* ctx, const char* cmd);
389   bRC (*endRestoreFile)(PluginContext* ctx);
390   bRC (*pluginIO)(PluginContext* ctx, struct io_pkt* io);
391   bRC (*createFile)(PluginContext* ctx, struct restore_pkt* rp);
392   bRC (*setFileAttributes)(PluginContext* ctx, struct restore_pkt* rp);
393   bRC (*checkFile)(PluginContext* ctx, char* fname);
394   bRC (*getAcl)(PluginContext* ctx, struct acl_pkt* ap);
395   bRC (*setAcl)(PluginContext* ctx, struct acl_pkt* ap);
396   bRC (*getXattr)(PluginContext* ctx, struct xattr_pkt* xp);
397   bRC (*setXattr)(PluginContext* ctx, struct xattr_pkt* xp);
398 } PluginFunctions;
399 
400 #define PlugFunc(plugin) ((PluginFunctions*)(plugin->plugin_functions))
401 #define plug_info(plugin) ((PluginInformation*)(plugin->plugin_information))
402 
403 #ifdef __cplusplus
404 }
405 #endif
406 
407 } /* namespace filedaemon */
408 #endif /* BAREOS_FILED_FD_PLUGINS_H_ */
409