1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2016 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    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    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero 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 /*
24  * Kern Sibbald, MM
25  */
26 /**
27  * @file
28  * This file handles commands from the File daemon.
29  *
30  * We get here because the Director has initiated a Job with
31  * the Storage daemon, then done the same with the File daemon,
32  * then when the Storage daemon receives a proper connection from
33  * the File daemon, control is passed here to handle the
34  * subsequent File daemon commands.
35  */
36 
37 #include "include/bareos.h"
38 #include "stored/stored.h"
39 #include "stored/append.h"
40 #include "stored/authenticate.h"
41 #include "stored/fd_cmds.h"
42 #include "stored/jcr_private.h"
43 #include "stored/read.h"
44 #include "stored/sd_stats.h"
45 #include "lib/bnet.h"
46 #include "lib/bsock.h"
47 #include "lib/edit.h"
48 #include "include/jcr.h"
49 
50 namespace storagedaemon {
51 
52 /* Static variables */
53 static char ferrmsg[] = "3900 Invalid command\n";
54 
55 /* Imported functions */
56 
57 /* Forward referenced FD commands */
58 static bool AppendOpenSession(JobControlRecord* jcr);
59 static bool AppendCloseSession(JobControlRecord* jcr);
60 static bool AppendDataCmd(JobControlRecord* jcr);
61 static bool AppendEndSession(JobControlRecord* jcr);
62 static bool ReadOpenSession(JobControlRecord* jcr);
63 static bool ReadDataCmd(JobControlRecord* jcr);
64 static bool ReadCloseSession(JobControlRecord* jcr);
65 
66 /* Exported function */
67 
68 struct s_cmds {
69   const char* cmd;
70   bool (*func)(JobControlRecord* jcr);
71 };
72 
73 /**
74  * The following are the recognized commands from the File daemon
75  */
76 static struct s_cmds fd_cmds[] = {
77     {"append open", AppendOpenSession}, {"append data", AppendDataCmd},
78     {"append end", AppendEndSession},   {"append close", AppendCloseSession},
79     {"read open", ReadOpenSession},     {"read data", ReadDataCmd},
80     {"read close", ReadCloseSession},   {NULL, NULL} /* list terminator */
81 };
82 
83 /* Commands from the File daemon that require additional scanning */
84 static char read_open[] = "read open session = %127s %ld %ld %ld %ld %ld %ld\n";
85 
86 /* Responses sent to the File daemon */
87 static char NO_open[] = "3901 Error session already open\n";
88 static char NOT_opened[] = "3902 Error session not opened\n";
89 static char OK_end[] = "3000 OK end\n";
90 static char OK_close[] = "3000 OK close Status = %d\n";
91 static char OK_open[] = "3000 OK open ticket = %d\n";
92 static char ERROR_append[] = "3903 Error append data\n";
93 
94 /* Responses sent to the Director */
95 static char Job_start[] = "3010 Job %s start\n";
96 static char Job_end[] =
97     "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s JobErrors=%u\n";
98 
99 /**
100  * After receiving a connection (in dircmd.c) if it is
101  * from the File daemon, this routine is called.
102  */
HandleFiledConnection(BareosSocket * fd,char * job_name)103 void* HandleFiledConnection(BareosSocket* fd, char* job_name)
104 {
105   JobControlRecord* jcr;
106 
107   /**
108    * With the following Bmicrosleep on, running the
109    * SD under the debugger fails.
110    */
111   // Bmicrosleep(0, 50000);             /* wait 50 millisecs */
112   if (!(jcr = get_jcr_by_full_name(job_name))) {
113     Jmsg1(NULL, M_FATAL, 0, _("FD connect failed: Job name not found: %s\n"),
114           job_name);
115     Dmsg1(3, "**** Job \"%s\" not found.\n", job_name);
116     fd->close();
117     return NULL;
118   }
119 
120   Dmsg1(50, "Found Job %s\n", job_name);
121 
122   if (jcr->authenticated) {
123     Jmsg2(jcr, M_FATAL, 0,
124           _("Hey!!!! JobId %u Job %s already authenticated.\n"),
125           (uint32_t)jcr->JobId, jcr->Job);
126     Dmsg2(50, "Hey!!!! JobId %u Job %s already authenticated.\n",
127           (uint32_t)jcr->JobId, jcr->Job);
128     fd->close();
129     FreeJcr(jcr);
130     return NULL;
131   }
132 
133   jcr->file_bsock = fd;
134   jcr->file_bsock->SetJcr(jcr);
135 
136   /*
137    * Authenticate the File daemon
138    */
139   if (!AuthenticateFiledaemon(jcr)) {
140     Dmsg1(50, "Authentication failed Job %s\n", jcr->Job);
141     Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
142     jcr->setJobStatus(JS_ErrorTerminated);
143   } else {
144     utime_t now;
145 
146     jcr->authenticated = true;
147     Dmsg2(50, "OK Authentication jid=%u Job %s\n", (uint32_t)jcr->JobId,
148           jcr->Job);
149 
150     /*
151      * Update the initial Job Statistics.
152      */
153     now = (utime_t)time(NULL);
154     UpdateJobStatistics(jcr, now);
155   }
156 
157   pthread_cond_signal(&jcr->impl->job_start_wait); /* wake waiting job */
158   FreeJcr(jcr);
159 
160   return NULL;
161 }
162 
163 /**
164  * Run a File daemon Job -- File daemon already authorized
165  * Director sends us this command.
166  *
167  * Basic task here is:
168  * - Read a command from the File daemon
169  * - Execute it
170  */
RunJob(JobControlRecord * jcr)171 void RunJob(JobControlRecord* jcr)
172 {
173   BareosSocket* dir = jcr->dir_bsock;
174   char ec1[30];
175 
176   dir->SetJcr(jcr);
177   Dmsg1(120, "Start run Job=%s\n", jcr->Job);
178   dir->fsend(Job_start, jcr->Job);
179   jcr->start_time = time(NULL);
180   jcr->run_time = jcr->start_time;
181   jcr->sendJobStatus(JS_Running);
182 
183   DoFdCommands(jcr);
184 
185   jcr->end_time = time(NULL);
186   DequeueMessages(jcr); /* send any queued messages */
187   jcr->setJobStatus(JS_Terminated);
188 
189   GeneratePluginEvent(jcr, bsdEventJobEnd);
190 
191   dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
192              edit_uint64(jcr->JobBytes, ec1), jcr->JobErrors);
193   dir->signal(BNET_EOD); /* send EOD to Director daemon */
194 
195   FreePlugins(jcr); /* release instantiated plugins */
196 }
197 
198 /**
199  * Now talk to the FD and do what he says
200  */
DoFdCommands(JobControlRecord * jcr)201 void DoFdCommands(JobControlRecord* jcr)
202 {
203   int i, status;
204   bool found, quit;
205   BareosSocket* fd = jcr->file_bsock;
206 
207   fd->SetJcr(jcr);
208   quit = false;
209   while (!quit) {
210     /*
211      * Read command coming from the File daemon
212      */
213     status = fd->recv();
214     if (IsBnetStop(fd)) { /* hardeof or error */
215       break;              /* connection terminated */
216     }
217     if (status <= 0) { continue; /* ignore signals and zero length msgs */ }
218 
219     Dmsg1(110, "<filed: %s", fd->msg);
220     found = false;
221     for (i = 0; fd_cmds[i].cmd; i++) {
222       if (bstrncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd))) {
223         found = true; /* indicate command found */
224         jcr->errmsg[0] = 0;
225         if (!fd_cmds[i].func(jcr)) { /* do command */
226           /*
227            * Note fd->msg command may be destroyed by comm activity
228            */
229           if (!JobCanceled(jcr)) {
230             if (jcr->errmsg[0]) {
231               Jmsg1(jcr, M_FATAL, 0,
232                     _("Command error with FD, hanging up. %s\n"), jcr->errmsg);
233             } else {
234               Jmsg0(jcr, M_FATAL, 0, _("Command error with FD, hanging up.\n"));
235             }
236             jcr->setJobStatus(JS_ErrorTerminated);
237           }
238           quit = true;
239         }
240         break;
241       }
242     }
243 
244     if (!found) { /* command not found */
245       if (!JobCanceled(jcr)) {
246         Jmsg1(jcr, M_FATAL, 0, _("FD command not found: %s\n"), fd->msg);
247         Dmsg1(110, "<filed: Command not found: %s", fd->msg);
248       }
249       fd->fsend(ferrmsg);
250       break;
251     }
252   }
253   fd->signal(BNET_TERMINATE); /* signal to FD job is done */
254 }
255 
256 /**
257  * Append Data command
258  *    Open Data Channel and receive Data for archiving
259  *    Write the Data to the archive device
260  */
AppendDataCmd(JobControlRecord * jcr)261 static bool AppendDataCmd(JobControlRecord* jcr)
262 {
263   BareosSocket* fd = jcr->file_bsock;
264 
265   Dmsg1(120, "Append data: %s", fd->msg);
266   if (jcr->impl->session_opened) {
267     Dmsg1(110, "<filed: %s", fd->msg);
268     jcr->setJobType(JT_BACKUP);
269     if (DoAppendData(jcr, fd, "FD")) {
270       return true;
271     } else {
272       PmStrcpy(jcr->errmsg, _("Append data error.\n"));
273       BnetSuppressErrorMessages(fd, 1); /* ignore errors at this point */
274       fd->fsend(ERROR_append);
275     }
276   } else {
277     PmStrcpy(jcr->errmsg, _("Attempt to append on non-open session.\n"));
278     fd->fsend(NOT_opened);
279   }
280   return false;
281 }
282 
AppendEndSession(JobControlRecord * jcr)283 static bool AppendEndSession(JobControlRecord* jcr)
284 {
285   BareosSocket* fd = jcr->file_bsock;
286 
287   Dmsg1(120, "stored<filed: %s", fd->msg);
288   if (!jcr->impl->session_opened) {
289     PmStrcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
290     fd->fsend(NOT_opened);
291     return false;
292   }
293   return fd->fsend(OK_end);
294 }
295 
296 /**
297  * Append Open session command
298  */
AppendOpenSession(JobControlRecord * jcr)299 static bool AppendOpenSession(JobControlRecord* jcr)
300 {
301   BareosSocket* fd = jcr->file_bsock;
302 
303   Dmsg1(120, "Append open session: %s", fd->msg);
304   if (jcr->impl->session_opened) {
305     PmStrcpy(jcr->errmsg, _("Attempt to open already open session.\n"));
306     fd->fsend(NO_open);
307     return false;
308   }
309 
310   jcr->impl->session_opened = true;
311 
312   /* Send "Ticket" to File Daemon */
313   fd->fsend(OK_open, jcr->VolSessionId);
314   Dmsg1(110, ">filed: %s", fd->msg);
315 
316   return true;
317 }
318 
319 /**
320  * Append Close session command
321  *    Close the append session and send back Statistics
322  *    (need to fix statistics)
323  */
AppendCloseSession(JobControlRecord * jcr)324 static bool AppendCloseSession(JobControlRecord* jcr)
325 {
326   BareosSocket* fd = jcr->file_bsock;
327 
328   Dmsg1(120, "<filed: %s", fd->msg);
329   if (!jcr->impl->session_opened) {
330     PmStrcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
331     fd->fsend(NOT_opened);
332     return false;
333   }
334 
335   /*
336    * Send final statistics to File daemon
337    */
338   fd->fsend(OK_close, jcr->JobStatus);
339   Dmsg1(120, ">filed: %s", fd->msg);
340 
341   fd->signal(BNET_EOD); /* send EOD to File daemon */
342 
343   jcr->impl->session_opened = false;
344   return true;
345 }
346 
347 /**
348  * Read Data command
349  *    Open Data Channel, read the data from
350  *    the archive device and send to File
351  *    daemon.
352  */
ReadDataCmd(JobControlRecord * jcr)353 static bool ReadDataCmd(JobControlRecord* jcr)
354 {
355   BareosSocket* fd = jcr->file_bsock;
356 
357   Dmsg1(120, "Read data: %s", fd->msg);
358   if (jcr->impl->session_opened) {
359     Dmsg1(120, "<filed: %s", fd->msg);
360     return DoReadData(jcr);
361   } else {
362     PmStrcpy(jcr->errmsg, _("Attempt to read on non-open session.\n"));
363     fd->fsend(NOT_opened);
364     return false;
365   }
366 }
367 
368 /**
369  * Read Open session command
370  *    We need to scan for the parameters of the job
371  *    to be restored.
372  */
ReadOpenSession(JobControlRecord * jcr)373 static bool ReadOpenSession(JobControlRecord* jcr)
374 {
375   BareosSocket* fd = jcr->file_bsock;
376 
377   Dmsg1(120, "%s\n", fd->msg);
378   if (jcr->impl->session_opened) {
379     PmStrcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
380     fd->fsend(NO_open);
381     return false;
382   }
383 
384   if (sscanf(fd->msg, read_open, jcr->impl->read_dcr->VolumeName,
385              &jcr->impl->read_session.read_VolSessionId,
386              &jcr->impl->read_session.read_VolSessionTime,
387              &jcr->impl->read_session.read_StartFile,
388              &jcr->impl->read_session.read_EndFile,
389              &jcr->impl->read_session.read_StartBlock,
390              &jcr->impl->read_session.read_EndBlock) == 7) {
391     if (jcr->impl->session_opened) {
392       PmStrcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
393       fd->fsend(NOT_opened);
394       return false;
395     }
396     Dmsg4(100,
397           "ReadOpenSession got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
398           jcr->JobId, jcr->impl->read_dcr->VolumeName,
399           jcr->impl->read_session.read_VolSessionId,
400           jcr->impl->read_session.read_VolSessionTime);
401     Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
402           jcr->impl->read_session.read_StartFile,
403           jcr->impl->read_session.read_EndFile,
404           jcr->impl->read_session.read_StartBlock,
405           jcr->impl->read_session.read_EndBlock);
406   }
407 
408   jcr->impl->session_opened = true;
409   jcr->setJobType(JT_RESTORE);
410 
411   /*
412    * Send "Ticket" to File Daemon
413    */
414   fd->fsend(OK_open, jcr->VolSessionId);
415   Dmsg1(110, ">filed: %s", fd->msg);
416 
417   return true;
418 }
419 
420 /**
421  * Read Close session command
422  *    Close the read session
423  */
ReadCloseSession(JobControlRecord * jcr)424 static bool ReadCloseSession(JobControlRecord* jcr)
425 {
426   BareosSocket* fd = jcr->file_bsock;
427 
428   Dmsg1(120, "Read close session: %s\n", fd->msg);
429   if (!jcr->impl->session_opened) {
430     fd->fsend(NOT_opened);
431     return false;
432   }
433 
434   /*
435    * Send final close msg to File daemon
436    */
437   fd->fsend(OK_close, jcr->JobStatus);
438   Dmsg1(160, ">filed: %s", fd->msg);
439 
440   fd->signal(BNET_EOD); /* send EOD to File daemon */
441 
442   jcr->impl->session_opened = false;
443   return true;
444 }
445 
446 } /* namespace storagedaemon */
447