1 /*
2 
3    Copyright (C) 2007-2012 Kern Sibbald
4 
5    You may freely use this code to create your own plugin provided
6    it is to write a plugin for Bareos licensed under AGPLv3
7    (as Bareos is), and in that case, you may also remove
8    the above Copyright and this notice as well as modify
9    the code in any way.
10 
11 */
12 
13 #define BUILD_PLUGIN
14 #define BUILDING_DLL /* required for Windows plugin */
15 
16 #include <cinttypes>
17 #include "include/bareos.h"
18 #include "filed/fd_plugins.h"
19 
20 #define PLUGIN_LICENSE "Bareos AGPLv3"
21 #define PLUGIN_AUTHOR "Your name"
22 #define PLUGIN_DATE "January 2010"
23 #define PLUGIN_VERSION "1"
24 #define PLUGIN_DESCRIPTION "Test File Daemon Plugin"
25 
26 namespace filedaemon {
27 
28 /* Forward referenced functions */
29 static bRC newPlugin(bpContext* ctx);
30 static bRC freePlugin(bpContext* ctx);
31 static bRC getPluginValue(bpContext* ctx, pVariable var, void* value);
32 static bRC setPluginValue(bpContext* ctx, pVariable var, void* value);
33 static bRC handlePluginEvent(bpContext* ctx, bEvent* event, void* value);
34 static bRC startBackupFile(bpContext* ctx, struct save_pkt* sp);
35 static bRC endBackupFile(bpContext* ctx);
36 static bRC pluginIO(bpContext* ctx, struct io_pkt* io);
37 static bRC startRestoreFile(bpContext* ctx, const char* cmd);
38 static bRC endRestoreFile(bpContext* ctx);
39 static bRC createFile(bpContext* ctx, struct restore_pkt* rp);
40 static bRC setFileAttributes(bpContext* ctx, struct restore_pkt* rp);
41 static bRC checkFile(bpContext* ctx, char* fname);
42 static bRC getAcl(bpContext* ctx, acl_pkt* ap);
43 static bRC setAcl(bpContext* ctx, acl_pkt* ap);
44 static bRC getXattr(bpContext* ctx, xattr_pkt* xp);
45 static bRC setXattr(bpContext* ctx, xattr_pkt* xp);
46 
47 
48 /* Pointers to Bareos functions */
49 static bFuncs* bfuncs = NULL;
50 static bInfo* binfo = NULL;
51 
52 static genpInfo pluginInfo = {sizeof(pluginInfo), FD_PLUGIN_INTERFACE_VERSION,
53                               FD_PLUGIN_MAGIC,    PLUGIN_LICENSE,
54                               PLUGIN_AUTHOR,      PLUGIN_DATE,
55                               PLUGIN_VERSION,     PLUGIN_DESCRIPTION};
56 
57 static pFuncs pluginFuncs = {
58     sizeof(pluginFuncs), FD_PLUGIN_INTERFACE_VERSION,
59 
60     /* Entry points into plugin */
61     newPlugin,  /* new plugin instance */
62     freePlugin, /* free plugin instance */
63     getPluginValue, setPluginValue, handlePluginEvent, startBackupFile,
64     endBackupFile, startRestoreFile, endRestoreFile, pluginIO, createFile,
65     setFileAttributes, checkFile, getAcl, setAcl, getXattr, setXattr};
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 /*
72  * Plugin called here when it is first loaded
73  */
loadPlugin(bInfo * lbinfo,bFuncs * lbfuncs,genpInfo ** pinfo,pFuncs ** pfuncs)74 bRC loadPlugin(bInfo* lbinfo,
75                bFuncs* lbfuncs,
76                genpInfo** pinfo,
77                pFuncs** pfuncs)
78 {
79   bfuncs = lbfuncs; /* set Bareos funct pointers */
80   binfo = lbinfo;
81   printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
82 
83   *pinfo = &pluginInfo;   /* return pointer to our info */
84   *pfuncs = &pluginFuncs; /* return pointer to our functions */
85 
86   return bRC_OK;
87 }
88 
89 /*
90  * Plugin called here when it is unloaded, normally when
91  *  Bareos is going to exit.
92  */
unloadPlugin()93 bRC unloadPlugin()
94 {
95   printf("plugin: Unloaded\n");
96   return bRC_OK;
97 }
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 /*
104  * Called here to make a new instance of the plugin -- i.e. when
105  *  a new Job is started.  There can be multiple instances of
106  *  each plugin that are running at the same time.  Your
107  *  plugin instance must be thread safe and keep its own
108  *  local data.
109  */
newPlugin(bpContext * ctx)110 static bRC newPlugin(bpContext* ctx)
111 {
112   int JobId = 0;
113   bfuncs->getBareosValue(ctx, bVarJobId, (void*)&JobId);
114   // printf("plugin: newPlugin JobId=%d\n", JobId);
115   bfuncs->registerBareosEvents(
116       ctx, 10, bEventJobStart, bEventJobEnd, bEventStartBackupJob,
117       bEventEndBackupJob, bEventLevel, bEventSince, bEventStartRestoreJob,
118       bEventEndRestoreJob, bEventRestoreCommand, bEventBackupCommand);
119   return bRC_OK;
120 }
121 
122 /*
123  * Release everything concerning a particular instance of a
124  *  plugin. Normally called when the Job terminates.
125  */
freePlugin(bpContext * ctx)126 static bRC freePlugin(bpContext* ctx)
127 {
128   int JobId = 0;
129   bfuncs->getBareosValue(ctx, bVarJobId, (void*)&JobId);
130   // printf("plugin: freePlugin JobId=%d\n", JobId);
131   return bRC_OK;
132 }
133 
134 /*
135  * Called by core code to get a variable from the plugin.
136  *   Not currently used.
137  */
getPluginValue(bpContext * ctx,pVariable var,void * value)138 static bRC getPluginValue(bpContext* ctx, pVariable var, void* value)
139 {
140   // printf("plugin: getPluginValue var=%d\n", var);
141   return bRC_OK;
142 }
143 
144 /*
145  * Called by core code to set a plugin variable.
146  *  Not currently used.
147  */
setPluginValue(bpContext * ctx,pVariable var,void * value)148 static bRC setPluginValue(bpContext* ctx, pVariable var, void* value)
149 {
150   // printf("plugin: setPluginValue var=%d\n", var);
151   return bRC_OK;
152 }
153 
154 /*
155  * Called by Bareos when there are certain events that the
156  *   plugin might want to know.  The value depends on the
157  *   event.
158  */
handlePluginEvent(bpContext * ctx,bEvent * event,void * value)159 static bRC handlePluginEvent(bpContext* ctx, bEvent* event, void* value)
160 {
161   char* name;
162 
163   switch (event->eventType) {
164     case bEventJobStart:
165       printf("plugin: JobStart=%s\n", NPRT((char*)value));
166       break;
167     case bEventJobEnd:
168       printf("plugin: JobEnd\n");
169       break;
170     case bEventStartBackupJob:
171       printf("plugin: BackupStart\n");
172       break;
173     case bEventEndBackupJob:
174       printf("plugin: BackupEnd\n");
175       break;
176     case bEventLevel:
177       printf("plugin: JobLevel=%c %" PRId64 "\n", (int)(int64_t)value,
178              (int64_t)value);
179       break;
180     case bEventSince:
181       printf("plugin: since=%" PRId64 "\n", (int64_t)value);
182       break;
183     case bEventStartRestoreJob:
184       printf("plugin: StartRestoreJob\n");
185       break;
186     case bEventEndRestoreJob:
187       printf("plugin: EndRestoreJob\n");
188       break;
189     case bEventRestoreCommand:
190       /*
191        * Plugin command e.g. plugin = <plugin-name>:<name-space>:command
192        */
193       printf("plugin: backup command=%s\n", NPRT((char*)value));
194       break;
195     case bEventBackupCommand:
196       /*
197        * Plugin command e.g. plugin = <plugin-name>:<name-space>:command
198        */
199       printf("plugin: backup command=%s\n", NPRT((char*)value));
200       break;
201     default:
202       printf("plugin: unknown event=%d\n", event->eventType);
203   }
204   bfuncs->getBareosValue(ctx, bVarFDName, (void*)&name);
205 
206   return bRC_OK;
207 }
208 
209 /*
210  * Called when starting to backup a file.  Here the plugin must
211  * return the "stat" packet for the directory/file and provide
212  * certain information so that Bareos knows what the file is.
213  * The plugin can create "Virtual" files by giving them a
214  * name that is not normally found on the file system.
215  */
startBackupFile(bpContext * ctx,struct save_pkt * sp)216 static bRC startBackupFile(bpContext* ctx, struct save_pkt* sp)
217 {
218   return bRC_OK;
219 }
220 
221 /*
222  * Done backing up a file.
223  */
endBackupFile(bpContext * ctx)224 static bRC endBackupFile(bpContext* ctx) { return bRC_OK; }
225 
226 /*
227  * Do actual I/O. Bareos calls this after startBackupFile
228  * or after startRestoreFile to do the actual file input or output.
229  */
pluginIO(bpContext * ctx,struct io_pkt * io)230 static bRC pluginIO(bpContext* ctx, struct io_pkt* io)
231 {
232   io->status = 0;
233   io->io_errno = 0;
234   switch (io->func) {
235     case IO_OPEN:
236       printf("plugin: IO_OPEN\n");
237       break;
238     case IO_READ:
239       printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count);
240       break;
241     case IO_WRITE:
242       printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count);
243       break;
244     case IO_CLOSE:
245       printf("plugin: IO_CLOSE\n");
246       break;
247   }
248   return bRC_OK;
249 }
250 
startRestoreFile(bpContext * ctx,const char * cmd)251 static bRC startRestoreFile(bpContext* ctx, const char* cmd) { return bRC_OK; }
252 
endRestoreFile(bpContext * ctx)253 static bRC endRestoreFile(bpContext* ctx) { return bRC_OK; }
254 
255 /*
256  * Called here to give the plugin the information needed to
257  * re-create the file on a restore.  It basically gets the
258  * stat packet that was created during the backup phase.
259  * This data is what is needed to create the file, but does
260  * not contain actual file data.
261  */
createFile(bpContext * ctx,struct restore_pkt * rp)262 static bRC createFile(bpContext* ctx, struct restore_pkt* rp) { return bRC_OK; }
263 
264 /*
265  * Called after the file has been restored. This can be used to set directory
266  * permissions, ...
267  */
setFileAttributes(bpContext * ctx,struct restore_pkt * rp)268 static bRC setFileAttributes(bpContext* ctx, struct restore_pkt* rp)
269 {
270   return bRC_OK;
271 }
272 
getAcl(bpContext * ctx,acl_pkt * ap)273 static bRC getAcl(bpContext* ctx, acl_pkt* ap) { return bRC_OK; }
274 
setAcl(bpContext * ctx,acl_pkt * ap)275 static bRC setAcl(bpContext* ctx, acl_pkt* ap) { return bRC_OK; }
276 
getXattr(bpContext * ctx,xattr_pkt * xp)277 static bRC getXattr(bpContext* ctx, xattr_pkt* xp) { return bRC_OK; }
278 
setXattr(bpContext * ctx,xattr_pkt * xp)279 static bRC setXattr(bpContext* ctx, xattr_pkt* xp) { return bRC_OK; }
280 
281 /*
282  * When using Incremental dump, all previous dumps are necessary
283  */
checkFile(bpContext * ctx,char * fname)284 static bRC checkFile(bpContext* ctx, char* fname) { return bRC_OK; }
285 
286 } /* namespace filedaemon */
287