2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2018 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * A simple pipe plugin for the Bacula File Daemon
22 * Kern Sibbald, October 2007
23 * Copied into Windows plugin environment March, 2010 (KES)
27 #include "fd_plugins.h"
40 static const int dbglvl = 150;
42 #define PLUGIN_LICENSE "Bacula"
43 #define PLUGIN_AUTHOR "Kern Sibbald"
44 #define PLUGIN_DATE "January 2010"
45 #define PLUGIN_VERSION "1"
46 #define PLUGIN_DESCRIPTION "Bacula Pipe Windows File Daemon Plugin"
48 /* Forward referenced functions */
49 static bRC newPlugin(bpContext *ctx);
50 static bRC freePlugin(bpContext *ctx);
51 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
52 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
53 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
54 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
55 static bRC endBackupFile(bpContext *ctx);
56 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
57 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
58 static bRC endRestoreFile(bpContext *ctx);
59 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
60 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
62 static char *apply_rp_codes(struct plugin_ctx * p_ctx);
64 /* Pointers to Bacula functions */
65 static bFuncs *bfuncs = NULL;
66 static bInfo *binfo = NULL;
68 /* Plugin Information block */
69 static pInfo pluginInfo = {
71 FD_PLUGIN_INTERFACE_VERSION,
80 /* Plugin entry points for Bacula */
81 static pFuncs pluginFuncs = {
83 FD_PLUGIN_INTERFACE_VERSION,
85 /* Entry points into plugin */
86 newPlugin, /* new plugin instance */
87 freePlugin, /* free plugin instance */
98 NULL, /* No checkFile */
99 NULL /* No ACL/XATTR */
103 * Plugin private context
107 FILE *fd; /* pipe file descriptor */
108 bool backup; /* set for backup (not needed) */
109 char *cmd; /* plugin command line */
110 char *fname; /* filename to "backup/restore" */
111 char *reader; /* reader program for backup */
112 char *writer; /* writer program for backup */
119 * loadPlugin() and unloadPlugin() are entry points that are
120 * exported, so Bacula can directly call these two entry points
121 * they are common to all Bacula plugins.
124 * External entry point called by Bacula to "load the plugin
126 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
128 bfuncs = lbfuncs; /* set Bacula funct pointers */
130 *pinfo = &pluginInfo; /* return pointer to our info */
131 *pfuncs = &pluginFuncs; /* return pointer to our functions */
137 * External entry point to unload the plugin
141 // printf("bpipe-fd: Unloaded\n");
146 * The following entry points are accessed through the function
147 * pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
148 * has its own set of entry points that the plugin must define.
151 * Create a new instance of the plugin i.e. allocate our private storage
153 static bRC newPlugin(bpContext *ctx)
155 struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
159 memset(p_ctx, 0, sizeof(struct plugin_ctx));
160 ctx->pContext = (void *)p_ctx; /* set our context pointer */
165 * Free a plugin instance, i.e. release our private storage
167 static bRC freePlugin(bpContext *ctx)
169 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
174 free(p_ctx->cmd); /* free any allocated command string */
176 free(p_ctx); /* free our private context */
182 * Return some plugin value (none defined)
184 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
190 * Set a plugin value (none defined)
192 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
198 * Handle an event that was generated in Bacula
200 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
202 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
210 * Most events don't interest us so we ignore them.
211 * the printfs are so that plugin writers can enable them to see
212 * what is really going on.
214 switch (event->eventType) {
216 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: JobStart=%s\n", (char *)value);
219 // printf("bpipe-fd: JobEnd\n");
221 case bEventStartBackupJob:
222 // printf("bpipe-fd: StartBackupJob\n");
224 case bEventEndBackupJob:
225 // printf("bpipe-fd: EndBackupJob\n");
228 // printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
231 // printf("bpipe-fd: since=%d\n", (int)value);
234 case bEventStartRestoreJob:
235 // printf("bpipe-fd: StartRestoreJob\n");
238 case bEventEndRestoreJob:
239 // printf("bpipe-fd: EndRestoreJob\n");
242 /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
243 case bEventRestoreCommand:
244 // printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
245 /* Fall-through wanted */
246 case bEventBackupCommand:
248 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
249 p_ctx->cmd = strdup((char *)value);
250 p = strchr(p_ctx->cmd, ':');
252 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
255 *p++ = 0; /* terminate plugin */
259 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
262 *p++ = 0; /* terminate file */
266 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
269 *p++ = 0; /* terminate reader string */
271 // printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n",
272 // p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
275 /* Ignore all unknown event types */
283 * Start the backup of a specific file
285 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
287 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
291 time_t now = time(NULL);
292 sp->fname = p_ctx->fname;
294 sp->statp.st_mode = 0700 | S_IFREG;
295 sp->statp.st_ctime = now;
296 sp->statp.st_mtime = now;
297 sp->statp.st_atime = now;
298 sp->statp.st_size = 0;
299 sp->statp.st_blksize = 4096;
300 sp->statp.st_blocks = 1;
301 p_ctx->backup = true;
302 // printf("bpipe-fd: startBackupFile\n");
307 * Done with backup of this file
309 static bRC endBackupFile(bpContext *ctx)
312 * We would return bRC_More if we wanted startBackupFile to be
313 * called again to backup another file
320 * Bacula is calling us to do the actual I/O
322 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
324 struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
333 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN\n");
334 if (io->flags & (O_CREAT | O_WRONLY)) {
335 char *writer_codes = apply_rp_codes(p_ctx);
337 p_ctx->fd = popen(writer_codes, "w");
338 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%d writer=%s\n",
339 p_ctx->fd, writer_codes);
341 io->io_errno = errno;
342 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
343 "Open pipe writer=%s failed: ERR=%s\n", writer_codes, strerror(errno));
353 p_ctx->fd = popen(p_ctx->reader, "r");
354 bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_OPEN fd=%p reader=%s\n",
355 p_ctx->fd, p_ctx->reader);
357 io->io_errno = errno;
358 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
359 "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
363 bmicrosleep(1,0); /* let pipe connect */
368 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL read FD\n");
371 io->status = fread(io->buf, 1, io->count, p_ctx->fd);
372 // bfuncs->DebugMessage(ctx, fi, li, dbglvl, "bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
373 if (io->status == 0 && ferror(p_ctx->fd)) {
374 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
375 "Pipe read error: ERR=%s\n", strerror(errno));
376 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
377 "Pipe read error: ERR=%s\n", strerror(errno));
384 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL write FD\n");
387 // printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
388 io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
389 // printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
390 if (io->status == 0 && ferror(p_ctx->fd)) {
391 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0,
392 "Pipe write error\n");
393 bfuncs->DebugMessage(ctx, fi, li, dbglvl,
394 "Pipe read error: ERR=%s\n", strerror(errno));
401 bfuncs->JobMessage(ctx, fi, li, M_FATAL, 0, "Logic error: NULL FD on bpipe close\n");
404 io->status = pclose(p_ctx->fd);
408 io->offset = p_ctx->offset;
415 * Bacula is notifying us that a plugin name string was found, and
416 * passing us the plugin command, so we can prepare for a restore.
418 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
420 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
425 * Bacula is notifying us that the plugin data has terminated, so
426 * the restore for this particular file is done.
428 static bRC endRestoreFile(bpContext *ctx)
430 // printf("bpipe-fd: endRestoreFile\n");
435 * This is called during restore to create the file (if necessary)
436 * We must return in rp->create_status:
439 * CF_SKIP -- skip processing this file
440 * CF_EXTRACT -- extract the file (i.e.call i/o routines)
441 * CF_CREATED -- created, but no content to extract (typically directories)
444 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
446 // printf("bpipe-fd: createFile\n");
447 if (strlen(rp->where) > 512) {
448 printf("Restore target dir too long. Restricting to first 512 bytes.\n");
450 strncpy(((struct plugin_ctx *)ctx->pContext)->where, rp->where, 513);
451 ((struct plugin_ctx *)ctx->pContext)->replace = rp->replace;
452 rp->create_status = CF_EXTRACT;
457 * We will get here if the File is a directory after everything
458 * is written in the directory.
460 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
462 // printf("bpipe-fd: setFileAttributes\n");
466 /*************************************************************************
467 * Apply codes in writer command:
472 * 'always' => 'a', chr(97)
473 * 'ifnewer' => 'w', chr(119)
474 * 'ifolder' => 'o', chr(111)
475 * 'never' => 'n', chr(110)
477 * This function will allocate the required amount of memory with malloc.
478 * Need to be free()d manually.
479 * Inspired by edit_job_codes in lib/util.c
482 static char *apply_rp_codes(struct plugin_ctx * p_ctx)
487 int w_count = 0, r_count = 0;
490 char *imsg = p_ctx->writer;
497 while ((q = strstr(p, "%w"))) {
503 while ((q = strstr(p, "%r"))) {
511 * + number of "where" codes * (len(where)-2)
512 * - number of "replace" codes
514 omsg = (char*)malloc(strlen(imsg) + (w_count * (strlen(p_ctx->where)-2)) - r_count + 1);
516 fprintf(stderr, "Out of memory.");
521 //printf("apply_rp_codes: %s\n", imsg);
522 for (p=imsg; *p; p++) {
532 snprintf(add, 2, "%c", p_ctx->replace);
547 //printf("add_str %s\n", str);
549 //printf("omsg=%s\n", omsg);