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