]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/bpipe-fd.c
c083b1361f78ce3f7a2396339d657bf8e6abbca8
[bacula/bacula] / bacula / src / plugins / fd / bpipe-fd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU 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    General Public License for more details.
17
18    You should have received a copy of the GNU 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    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * A simple pipe plugin for the Bacula File Daemon
30  *
31  *  Kern Sibbald, October 2007
32  *
33  */
34 #include "fd_plugins.h"
35
36 #undef malloc
37 #undef free
38 #undef strdup
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #define PLUGIN_LICENSE      "GPLv2"
45 #define PLUGIN_AUTHOR       "Kern Sibbald"
46 #define PLUGIN_DATE         "January 2008"
47 #define PLUGIN_VERSION      "1"
48 #define PLUGIN_DESCRIPTION  "Pipe File Daemon Plugin"
49
50 /* Forward referenced functions */
51 static bRC newPlugin(bpContext *ctx);
52 static bRC freePlugin(bpContext *ctx);
53 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
54 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
55 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
56 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
57 static bRC endBackupFile(bpContext *ctx);
58 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
59 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
60 static bRC endRestoreFile(bpContext *ctx);
61 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
62 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
63
64
65 /* Pointers to Bacula functions */
66 static bFuncs *bfuncs = NULL;
67 static bInfo  *binfo = NULL;
68
69 /* Plugin Information block */
70 static pInfo pluginInfo = {
71    sizeof(pluginInfo),
72    FD_PLUGIN_INTERFACE_VERSION,
73    FD_PLUGIN_MAGIC,
74    PLUGIN_LICENSE,
75    PLUGIN_AUTHOR,
76    PLUGIN_DATE,
77    PLUGIN_VERSION,
78    PLUGIN_DESCRIPTION,
79 };
80
81 /* Plugin entry points for Bacula */
82 static pFuncs pluginFuncs = {
83    sizeof(pluginFuncs),
84    FD_PLUGIN_INTERFACE_VERSION,
85
86    /* Entry points into plugin */
87    newPlugin,                         /* new plugin instance */
88    freePlugin,                        /* free plugin instance */
89    getPluginValue,
90    setPluginValue,
91    handlePluginEvent,
92    startBackupFile,
93    endBackupFile,
94    startRestoreFile,
95    endRestoreFile,
96    pluginIO,
97    createFile,
98    setFileAttributes
99 };
100
101 /*
102  * Plugin private context
103  */
104 struct plugin_ctx {
105    boffset_t offset;
106    FILE *fd;                          /* pipe file descriptor */
107    bool backup;                       /* set for backup (not needed) */
108    char *cmd;                         /* plugin command line */
109    char *fname;                       /* filename to "backup/restore" */
110    char *reader;                      /* reader program for backup */
111    char *writer;                      /* writer program for backup */
112
113    char where[512];
114    int replace;
115 };
116
117 /*
118  * loadPlugin() and unloadPlugin() are entry points that are
119  *  exported, so Bacula can directly call these two entry points
120  *  they are common to all Bacula plugins.
121  */
122 /*
123  * External entry point called by Bacula to "load the plugin
124  */
125 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
126 {
127    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
128    binfo  = lbinfo;
129    *pinfo  = &pluginInfo;             /* return pointer to our info */
130    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
131
132    return bRC_OK;
133 }
134
135 /*
136  * External entry point to unload the plugin 
137  */
138 bRC unloadPlugin() 
139 {
140 // printf("bpipe-fd: Unloaded\n");
141    return bRC_OK;
142 }
143
144 /*
145  * The following entry points are accessed through the function 
146  *   pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
147  *   has its own set of entry points that the plugin must define.
148  */
149 /*
150  * Create a new instance of the plugin i.e. allocate our private storage
151  */
152 static bRC newPlugin(bpContext *ctx)
153 {
154    struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
155    memset(p_ctx, 0, sizeof(struct plugin_ctx));
156    ctx->pContext = (void *)p_ctx;        /* set our context pointer */
157    return bRC_OK;
158 }
159
160 /*
161  * Free a plugin instance, i.e. release our private storage
162  */
163 static bRC freePlugin(bpContext *ctx)
164 {
165    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
166    if (p_ctx->cmd) {
167       free(p_ctx->cmd);                  /* free any allocated command string */
168    }
169    free(p_ctx);                          /* free our private context */
170    return bRC_OK;
171 }
172
173 /*
174  * Return some plugin value (none defined)
175  */
176 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
177 {
178    return bRC_OK;
179 }
180
181 /*
182  * Set a plugin value (none defined)
183  */
184 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
185 {
186    return bRC_OK;
187 }
188
189 /*
190  * Handle an event that was generated in Bacula
191  */
192 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
193 {
194    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
195 // char *name;
196
197    switch (event->eventType) {
198    case bEventJobStart:
199 //    printf("bpipe-fd: JobStart=%s\n", (char *)value);
200       break;
201    case bEventJobEnd:
202 //    printf("bpipe-fd: JobEnd\n");
203       break;
204    case bEventStartBackupJob:
205 //    printf("bpipe-fd: BackupStart\n");
206       break;
207    case bEventEndBackupJob:
208 //    printf("bpipe-fd: BackupEnd\n");
209       break;
210    case bEventLevel:
211 //    printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
212       break;
213    case bEventSince:
214 //    printf("bpipe-fd: since=%d\n", (int)value);
215       break;
216
217    case bEventStartRestoreJob:
218       break;
219
220    case bEventEndRestoreJob:
221       break;
222
223    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
224    case bEventRestoreCommand:
225       printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
226    case bEventBackupCommand:
227       char *p;
228       printf("bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
229       p_ctx->cmd = strdup((char *)value);
230       p = strchr(p_ctx->cmd, ':');
231       if (!p) {
232          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
233          return bRC_Error;
234       }
235       *p++ = 0;           /* terminate plugin */
236       p_ctx->fname = p;
237       p = strchr(p, ':');
238       if (!p) {
239          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
240          return bRC_Error;
241       }
242       *p++ = 0;           /* terminate file */
243       p_ctx->reader = p;
244       p = strchr(p, ':');
245       if (!p) {
246          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
247          return bRC_Error;
248       }
249       *p++ = 0;           /* terminate reader string */
250       p_ctx->writer = p;
251       printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n", 
252          p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
253       break;
254
255    default:
256       printf("bpipe-fd: unknown event=%d\n", event->eventType);
257    }
258    return bRC_OK;
259 }
260
261 /* 
262  * Start the backup of a specific file
263  */
264 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
265 {
266    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
267    time_t now = time(NULL);
268    sp->fname = p_ctx->fname;
269    sp->statp.st_mode = 0700 | S_IFREG;
270    sp->statp.st_ctime = now;
271    sp->statp.st_mtime = now;
272    sp->statp.st_atime = now;
273    sp->statp.st_size = -1;
274    sp->statp.st_blksize = 4096;
275    sp->statp.st_blocks = 1;
276    p_ctx->backup = true;
277 // printf("bpipe-fd: startBackupFile\n");
278    return bRC_OK;
279 }
280
281 /*
282  * Done with backup of this file
283  */
284 static bRC endBackupFile(bpContext *ctx)
285 {
286    /*
287     * We would return bRC_More if we wanted startBackupFile to be
288     * called again to backup another file
289     */
290    return bRC_OK;
291 }
292
293 /*
294  * Apply codes in writer command:
295  * %w -> "where"
296  * %r -> "replace"
297  *
298  * Replace:
299  * 'always' => 'a', chr(97)
300  * 'ifnewer' => 'w', chr(119)
301  * 'ifolder' => 'o', chr(111)
302  * 'never' => 'n', chr(110)
303  *
304  * This function will allocate the required amount of memory with malloc.
305  * Need to be free()d manually.
306  * Inspired by edit_job_codes in lib/util.c
307  */
308
309 char *apply_rp_codes(struct plugin_ctx * p_ctx)
310 {
311    char *p, *q;
312    const char *str;
313    char add[10];
314    int w_count = 0, r_count = 0;
315    char *omsg;
316
317    char *imsg = p_ctx->writer;
318
319    if (!imsg) {
320       return NULL;
321    }
322
323    if ((p = imsg)) {
324       while ((q = strstr(p, "%w"))) {
325          w_count++;
326          p=q+1;
327       }
328
329       p = imsg;
330       while ((q = strstr(p, "%r"))) {
331          r_count++;
332          p=q+1;
333       }
334    }
335
336    /* Required mem: 
337     * len(imsg) 
338     * + number of "where" codes * (len(where)-2) 
339     * - number of "replace" codes
340     */
341    omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
342    if (!omsg) {
343       fprintf(stderr, "Out of memory.");
344       exit(1);
345    }
346
347    *omsg = 0;
348    //printf("apply_rp_codes: %s\n", imsg);
349    for (p=imsg; *p; p++) {
350       if (*p == '%') {
351          switch (*++p) {
352          case '%':
353             str = "%";
354             break;
355          case 'w':
356              str = p_ctx->where;
357              break;
358          case 'r':
359             snprintf(add, 2, "%c", p_ctx->replace);
360             str = add;
361             break;
362          default:
363             add[0] = '%';
364             add[1] = *p;
365             add[2] = 0;
366             str = add;
367             break;
368          }
369       } else {
370          add[0] = *p;
371          add[1] = 0;
372          str = add;
373       }
374       //printf("add_str %s\n", str);
375       strcat(omsg, str);
376       //printf("omsg=%s\n", omsg);
377    }
378    return omsg;
379 }
380
381 /*
382  * Bacula is calling us to do the actual I/O
383  */
384 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
385 {
386    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
387     
388    io->status = 0;
389    io->io_errno = 0;
390    switch(io->func) {
391    case IO_OPEN:
392 //    printf("bpipe-fd: IO_OPEN\n");
393       if (io->flags & (O_CREAT | O_WRONLY)) {
394          char *writer_codes = apply_rp_codes(p_ctx);
395
396          p_ctx->fd = popen(writer_codes, "w");
397          printf("bpipe-fd: IO_OPEN writer=%s\n", writer_codes);
398          if (!p_ctx->fd) {
399             io->io_errno = errno;
400             bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
401                "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
402             if (writer_codes) {
403                free(writer_codes);
404             }
405             return bRC_Error;
406          }
407          if (writer_codes) {
408             free(writer_codes);
409          }
410       } else {
411          p_ctx->fd = popen(p_ctx->reader, "r");
412 //       printf("bpipe-fd: IO_OPEN reader=%s\n", p_ctx->reader);
413          if (!p_ctx->fd) {
414             io->io_errno = errno;
415             bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
416                "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
417             return bRC_Error;
418          }
419       }
420       sleep(1);                 /* let pipe connect */
421       break;
422
423    case IO_READ:
424       if (!p_ctx->fd) {
425          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL read FD\n");
426          return bRC_Error;
427       }
428       io->status = fread(io->buf, 1, io->count, p_ctx->fd);
429 //    printf("bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
430       if (io->status == 0 && ferror(p_ctx->fd)) {
431          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
432             "Pipe read error: ERR=%s\n", strerror(errno));
433 //       printf("Error reading pipe\n");
434          return bRC_Error;
435       }
436       break;
437
438    case IO_WRITE:
439       if (!p_ctx->fd) {
440          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL write FD\n");
441          return bRC_Error;
442       }
443 //    printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
444       io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
445 //    printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
446       if (io->status == 0 && ferror(p_ctx->fd)) {
447          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
448             "Pipe write error\n");
449 //       printf("Error writing pipe\n");
450          return bRC_Error;
451       }
452       break;
453
454    case IO_CLOSE:
455       if (!p_ctx->fd) {
456          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL FD\n");
457          return bRC_Error;
458       }
459       io->status = pclose(p_ctx->fd);
460       break;
461
462    case IO_SEEK:
463       io->offset = p_ctx->offset;
464       break;
465    }
466    return bRC_OK;
467 }
468
469 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
470 {
471 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
472    return bRC_OK;
473 }
474
475 static bRC endRestoreFile(bpContext *ctx)
476 {
477 // printf("bpipe-fd: endRestoreFile\n");
478    return bRC_OK;
479 }
480
481 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
482 {
483 // printf("bpipe-fd: createFile\n");
484    if (strlen(rp->where) > 512) {
485       printf("Restore target dir too long. Restricting to first 512 bytes.\n");
486    }
487    strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
488    ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
489    return bRC_OK;
490 }
491
492 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
493 {
494 // printf("bpipe-fd: setFileAttributes\n");
495    return bRC_OK;
496 }
497
498
499 #ifdef __cplusplus
500 }
501 #endif