]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/catreq.c
String length protection, VolUseDuration,MaxVolJobs see kes18Nov02
[bacula/bacula] / bacula / src / dird / catreq.c
1 /*
2  *
3  *   Bacula Director -- catreq.c -- handles the message channel
4  *    catalog request from the Storage daemon.
5  *
6  *     Kern Sibbald, March MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *      Handle Catalog services.
12  *
13  *   Version $Id$
14  */
15 /*
16    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35 #include "bacula.h"
36 #include "dird.h"
37
38 /*
39  * Handle catalog request
40  *  For now, we simply return next Volume to be used
41  */
42
43 /* Requests from the Storage daemon */
44 static char Find_media[] = "CatReq Job=%127s FindMedia=%d\n";
45 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
46
47 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s\
48  VolJobs=%d VolFiles=%d VolBlocks=%d VolBytes=%" lld " VolMounts=%d\
49  VolErrors=%d VolWrites=%d VolMaxBytes=%" lld " EndTime=%d VolStatus=%10s\
50  Slot=%d relabel=%d\n";
51
52 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia \
53  FirstIndex=%d LastIndex=%d StartFile=%d EndFile=%d \
54  StartBlock=%d EndBlock=%d\n";
55
56
57 /* Responses  sent to Storage daemon */
58 static char OK_media[] = "1000 OK VolName=%s VolJobs=%d VolFiles=%d\
59  VolBlocks=%d VolBytes=%" lld " VolMounts=%d VolErrors=%d VolWrites=%d\
60  VolMaxBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%s Slot=%d\n";
61
62 static char OK_update[] = "1000 OK UpdateMedia\n";
63
64 /* static char FileAttributes[] = "UpdCat Job=%127s FileAttributes "; */
65
66
67 void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
68 {
69    MEDIA_DBR mr; 
70    JOBMEDIA_DBR jm;
71    char Job[MAX_NAME_LENGTH];
72    int index, ok, relabel, writing, retry = 0;
73    POOLMEM *omsg;
74
75    memset(&mr, 0, sizeof(mr));
76    memset(&jm, 0, sizeof(jm));
77
78    /*
79     * Request to find next appendable Volume for this Job
80     */
81    Dmsg1(120, "catreq %s", bs->msg);
82    if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
83       mr.PoolId = jcr->PoolId;
84       strcpy(mr.MediaType, jcr->store->media_type);
85       Dmsg2(120, "CatReq FindMedia: Id=%d, MediaType=%s\n",
86          mr.PoolId, mr.MediaType);
87       /*
88        * Find the Next Volume for Append
89        */
90 next_volume:
91       strcpy(mr.VolStatus, "Append");  /* want only appendable volumes */
92       ok = db_find_next_volume(jcr->db, index, &mr);  
93       Dmsg1(200, "catreq after find_next_vol ok=%d\n", ok);
94       if (!ok) {
95          /* Well, try finding recycled tapes */
96          ok = find_recycled_volume(jcr, &mr);
97          Dmsg1(100, "find_recycled_volume1 %d\n", ok);
98          if (!ok) {
99             prune_volumes(jcr);  
100             ok = recycle_a_volume(jcr, &mr);
101             Dmsg1(100, "find_recycled_volume2 %d\n", ok);
102             if (!ok) {
103                /* See if we can create a new Volume */
104                ok = newVolume(jcr, &mr);
105             }
106          }
107       }
108       /* Check if use duration has expired */
109       if (ok && jcr->pool->VolUseDuration > 0 && 
110                 strcmp(mr.VolStatus, "Recycle") != 0) {
111          utime_t now = time(NULL);
112          utime_t start = str_to_utime(mr.cFirstWritten);                         
113          if (start > 0 && jcr->pool->VolUseDuration <= (now - start)) {
114             Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
115                (int)jcr->pool->VolUseDuration, (int)now, (int)start, (int)(now-start));
116             Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
117                "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
118             strcpy(mr.VolStatus, "Used");  /* yes, mark as used */
119             if (!db_update_media_record(jcr->db, &mr)) {
120                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
121                     db_strerror(jcr->db));
122             } else if (retry++ < 200) {     /* sanity check */
123                goto next_volume;
124             } else {
125                Jmsg(jcr, M_ERROR, 0, _(
126 "We seem to be looping trying to find the next volume. I give up. Ask operator.\n"));
127             }
128             ok = FALSE;               /* give up */
129          }
130       }
131
132       /*
133        * Send Find Media response to Storage daemon 
134        */
135       if (ok) {
136          jcr->MediaId = mr.MediaId;
137          pm_strcpy(&jcr->VolumeName, mr.VolumeName);
138          bash_spaces(mr.VolumeName);
139          bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
140             mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
141             mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
142             mr.VolStatus, mr.Slot);
143       } else {
144          bnet_fsend(bs, "1999 No Media\n");
145       }
146
147    /* 
148     * Request to find specific Volume information
149     */
150    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
151       Dmsg1(120, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
152       /*
153        * Find the Volume
154        */
155       unbash_spaces(mr.VolumeName);
156       if (db_get_media_record(jcr->db, &mr)) {
157          int VolSuitable = 0;
158          jcr->MediaId = mr.MediaId;
159          Dmsg1(120, "VolumeInfo MediaId=%d\n", jcr->MediaId);
160          strcpy(jcr->VolumeName, mr.VolumeName);
161          if (!writing) {
162             VolSuitable = 1;          /* accept anything for read */
163          } else {
164             /* 
165              * Make sure this volume is suitable for this job, i.e.
166              *  it is either Append or Recycle and Media Type matches 
167              *  and Pool allows any volume.
168              */
169             if (mr.PoolId == jcr->PoolId && 
170                 (strcmp(mr.VolStatus, "Append") == 0 ||
171                  strcmp(mr.VolStatus, "Recycle") == 0) &&
172                  strcmp(mr.MediaType, jcr->store->media_type) == 0 &&
173                  jcr->pool->accept_any_volume) {
174                VolSuitable = 1;
175             }
176          }
177          if (VolSuitable) {
178             /*
179              * Send Find Media response to Storage daemon 
180              */
181             bash_spaces(mr.VolumeName);
182             bnet_fsend(bs, OK_media, mr.VolumeName, mr.VolJobs,
183                mr.VolFiles, mr.VolBlocks, mr.VolBytes, mr.VolMounts, mr.VolErrors,
184                mr.VolWrites, mr.VolMaxBytes, mr.VolCapacityBytes,
185                mr.VolStatus, mr.Slot);
186             Dmsg5(200, "get_media_record PoolId=%d wanted %d, Status=%s, Slot=%d \
187 MediaType=%s\n", mr.PoolId, jcr->PoolId, mr.VolStatus, mr.Slot, mr.MediaType);
188          } else { 
189             /* Not suitable volume */
190             bnet_fsend(bs, "1998 Volume not appropriate.\n");
191          }
192
193       } else {
194          bnet_fsend(bs, "1999 Volume Not Found.\n");
195       }
196
197    
198    /*
199     * Request to update Media record. Comes typically at the end
200     *  of a Storage daemon Job Session
201     */
202    } else if (sscanf(bs->msg, Update_media, &Job, &mr.VolumeName, &mr.VolJobs,
203       &mr.VolFiles, &mr.VolBlocks, &mr.VolBytes, &mr.VolMounts, &mr.VolErrors,
204       &mr.VolWrites, &mr.VolMaxBytes, &mr.LastWritten, &mr.VolStatus, 
205       &mr.Slot, &relabel) == 14) {
206
207       /*     
208        * Update Media Record
209        */
210
211       /* First handle Max Volume Bytes */
212       if ((mr.VolMaxBytes > 0 && mr.VolBytes >= mr.VolMaxBytes)) {
213          Jmsg(jcr, M_INFO, 0, _("Max configure Volume bytes exceeded. "             
214              "Marking Volume \"%s\" as Full.\n"), mr.VolumeName);
215          strcpy(mr.VolStatus, "Full");
216
217       /* Now see if Volume should only be used once */
218       } else if (mr.VolBytes > 0 && jcr->pool->use_volume_once) {
219          Jmsg(jcr, M_INFO, 0, _("Volume used once. "             
220              "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
221          strcpy(mr.VolStatus, "Used");
222
223       /* Now see if Max Jobs written to volume */
224       } else if (jcr->pool->MaxVolJobs > 0 &&
225                  jcr->pool->MaxVolJobs <= mr.VolJobs) {
226          Jmsg(jcr, M_INFO, 0, _("Max configured Volume jobs exceeded. "       
227              "Marking Volume \"%s\" as Used.\n"), mr.VolumeName);
228          strcpy(mr.VolStatus, "Used");
229
230       /* Finally, check Use duration expiration */
231       } else if (jcr->pool->VolUseDuration > 0) {
232          MEDIA_DBR omr;
233          memset(&omr, 0, sizeof(omr));   /* clear media record */
234          strcpy(omr.VolumeName, mr.VolumeName); /* copy Volume name */
235          if (db_get_media_record(jcr->db, &omr)) {
236             utime_t start;
237             utime_t now = time(NULL);
238             start = str_to_utime(omr.cFirstWritten);                         
239             /* See if Vol Use has expired */
240             if (start > 0 && jcr->pool->VolUseDuration <= (now - start)) {
241                Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
242                   "Marking Volume \"%s\"as Used.\n"), mr.VolumeName);
243                strcpy(mr.VolStatus, "Used");  /* yes, mark as used */
244             }
245          } else {
246             Jmsg(jcr, M_ERROR, 0, _("Unable to get Media record for Volume %s: ERR=%s\n"),
247                mr.VolumeName, db_strerror(jcr->db));
248          }
249       }
250
251       Dmsg2(200, "db_update_media_record. Stat=%s Vol=%s\n",
252          mr.VolStatus, mr.VolumeName);
253       if (db_update_media_record(jcr->db, &mr)) {
254          bnet_fsend(bs, OK_update);
255          Dmsg0(190, "send OK\n");
256       } else {
257          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
258             db_strerror(jcr->db));
259          bnet_fsend(bs, "1992 Update Media error\n");
260          Dmsg0(190, "send error\n");
261       }
262
263    /*
264     * Request to create a JobMedia record
265     */
266    } else if (sscanf(bs->msg, Create_job_media, &Job,
267       &jm.FirstIndex, &jm.LastIndex, &jm.StartFile, &jm.EndFile,
268       &jm.StartBlock, &jm.EndBlock) == 7) {
269
270       jm.JobId = jcr->JobId;
271       jm.MediaId = jcr->MediaId;
272       Dmsg6(100, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
273          jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
274       if(!db_create_jobmedia_record(jcr->db, &jm)) {
275          Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
276             db_strerror(jcr->db));
277          bnet_fsend(bs, "1991 Update JobMedia error\n");
278       } else {
279          Dmsg0(100, "JobMedia record created\n");
280          bnet_fsend(bs, OK_update);
281       }
282
283    } else {
284       omsg = get_memory(bs->msglen+1);
285       pm_strcpy(&omsg, bs->msg);
286       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
287       free_memory(omsg);
288    }
289    Dmsg1(120, ">CatReq response: %s", bs->msg);
290    Dmsg1(200, "Leave catreq jcr 0x%x\n", jcr);
291    return;
292 }
293
294 /*
295  * Update File Attributes in the catalog with data
296  *  sent by the Storage daemon.
297  */
298 void catalog_update(JCR *jcr, BSOCK *bs, char *msg)
299 {
300    unser_declare;
301    uint32_t VolSessionId, VolSessionTime;
302    int32_t Stream;
303    uint32_t FileIndex;
304    uint32_t data_len;
305    char *p = bs->msg;
306    int len;
307    char *fname, *attr;
308    ATTR_DBR ar;
309
310    if (!jcr->pool->catalog_files) {
311       return;
312    }
313    db_start_transaction(jcr->db);     /* start transaction if not already open */
314    skip_nonspaces(&p);                /* UpdCat */
315    skip_spaces(&p);
316    skip_nonspaces(&p);                /* Job=nnn */
317    skip_spaces(&p);
318    skip_nonspaces(&p);                /* FileAttributes */
319    p += 1;
320    unser_begin(p, 0);
321    unser_uint32(VolSessionId);
322    unser_uint32(VolSessionTime);
323    unser_int32(FileIndex);
324    unser_int32(Stream);
325    unser_uint32(data_len);
326    p += unser_length(p);
327
328    Dmsg1(99, "UpdCat msg=%s\n", bs->msg);
329    Dmsg5(99, "UpdCat VolSessId=%d VolSessT=%d FI=%d Strm=%d data_len=%d\n",
330       VolSessionId, VolSessionTime, FileIndex, Stream, data_len);
331
332    if (Stream == STREAM_UNIX_ATTRIBUTES || Stream == STREAM_WIN32_ATTRIBUTES) {
333       skip_nonspaces(&p);             /* skip FileIndex */
334       skip_spaces(&p);
335       skip_nonspaces(&p);             /* skip FileType */
336       skip_spaces(&p);
337       fname = p;
338       len = strlen(fname);        /* length before attributes */
339       attr = &fname[len+1];
340
341       Dmsg2(109, "dird<stored: stream=%d %s\n", Stream, fname);
342       Dmsg1(109, "dird<stored: attr=%s\n", attr);
343       ar.attr = attr; 
344       ar.fname = fname;
345       ar.FileIndex = FileIndex;
346       ar.Stream = Stream;
347       ar.link = NULL;
348       ar.JobId = jcr->JobId;
349
350       Dmsg2(111, "dird<filed: stream=%d %s\n", Stream, fname);
351       Dmsg1(120, "dird<filed: attr=%s\n", attr);
352
353       if (!db_create_file_attributes_record(jcr->db, &ar)) {
354          Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
355       }
356       /* Save values for MD5 update */
357       jcr->FileId = ar.FileId;
358       jcr->FileIndex = FileIndex;
359    } else if (Stream == STREAM_MD5_SIGNATURE) {
360       fname = p;
361       if (jcr->FileIndex != FileIndex) {    
362          Jmsg(jcr, M_WARNING, 0, "Got MD5 but not same File as attributes\n");
363       } else {
364          /* Update MD5 signature in catalog */
365          char MD5buf[50];           /* 24 bytes should be enough */
366          bin_to_base64(MD5buf, fname, 16);
367          Dmsg2(190, "MD5len=%d MD5=%s\n", strlen(MD5buf), MD5buf);
368          if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5buf)) {
369             Jmsg(jcr, M_ERROR, 0, _("Catalog error updating MD5. %s"), 
370                db_strerror(jcr->db));
371          }
372       }
373    }
374 }