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