]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
57f99e5ac208dde10a4429f9e031b0c543c8d3ed
[bacula/bacula] / bacula / src / dird / ua_cmds.c
1 /*
2  *
3  *   Bacula Director -- User Agent Commands
4  *
5  *     Kern Sibbald, September MM
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* Imported subroutines */
34
35 /* Imported variables */
36 extern int r_first;
37 extern int r_last;
38 extern struct s_res resources[];
39 extern char my_name[];
40 extern jobq_t job_queue;              /* job queue */
41
42
43 extern char *list_pool;
44
45 /* Imported functions */
46 extern int status_cmd(UAContext *ua, const char *cmd);
47 extern int list_cmd(UAContext *ua, const char *cmd);
48 extern int llist_cmd(UAContext *ua, const char *cmd);
49 extern int show_cmd(UAContext *ua, const char *cmd);
50 extern int messagescmd(UAContext *ua, const char *cmd);
51 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
52 extern int gui_cmd(UAContext *ua, const char *cmd);
53 extern int sqlquerycmd(UAContext *ua, const char *cmd);
54 extern int querycmd(UAContext *ua, const char *cmd);
55 extern int run_cmd(UAContext *ua, const char *cmd);
56 extern int retentioncmd(UAContext *ua, const char *cmd);
57 extern int prunecmd(UAContext *ua, const char *cmd);
58 extern int purgecmd(UAContext *ua, const char *cmd);
59 extern int restore_cmd(UAContext *ua, const char *cmd);
60 extern int label_cmd(UAContext *ua, const char *cmd);
61 extern int relabel_cmd(UAContext *ua, const char *cmd);
62 extern int update_slots(UAContext *ua);  /* ua_label.c */
63
64 /* Forward referenced functions */
65 static int add_cmd(UAContext *ua, const char *cmd);  
66 static int create_cmd(UAContext *ua, const char *cmd); 
67 static int cancel_cmd(UAContext *ua, const char *cmd); 
68 static int setdebug_cmd(UAContext *ua, const char *cmd);
69 static int trace_cmd(UAContext *ua, const char *cmd);
70 static int var_cmd(UAContext *ua, const char *cmd);
71 static int estimate_cmd(UAContext *ua, const char *cmd);
72 static int help_cmd(UAContext *ua, const char *cmd);
73 static int delete_cmd(UAContext *ua, const char *cmd);
74 static int use_cmd(UAContext *ua, const char *cmd);
75 static int unmount_cmd(UAContext *ua, const char *cmd);
76 static int version_cmd(UAContext *ua, const char *cmd);
77 static int automount_cmd(UAContext *ua, const char *cmd);
78 static int time_cmd(UAContext *ua, const char *cmd);
79 static int reload_cmd(UAContext *ua, const char *cmd);
80 static int update_volume(UAContext *ua);
81 static int update_pool(UAContext *ua);
82 static int delete_volume(UAContext *ua);
83 static int delete_pool(UAContext *ua);
84 static void delete_job(UAContext *ua);
85 static int mount_cmd(UAContext *ua, const char *cmd);
86 static int release_cmd(UAContext *ua, const char *cmd);
87 static int update_cmd(UAContext *ua, const char *cmd);
88 static int wait_cmd(UAContext *ua, const char *cmd);
89 static int setip_cmd(UAContext *ua, const char *cmd);
90 static int python_cmd(UAContext *ua, const char *cmd);
91 static void do_job_delete(UAContext *ua, JobId_t JobId);
92 static void delete_job_id_range(UAContext *ua, char *tok);
93
94 int qhelp_cmd(UAContext *ua, const char *cmd);
95 int quit_cmd(UAContext *ua, const char *cmd);
96
97
98 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; }; 
99 static struct cmdstruct commands[] = {
100  { N_("add"),        add_cmd,         _("add media to a pool")},
101  { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on/off] -- console messages")},
102  { N_("automount"),   automount_cmd,  _("automount [on/off] -- after label")},
103  { N_("cancel"),     cancel_cmd,    _("cancel job=nnn -- cancel a job")},
104  { N_("create"),     create_cmd,    _("create DB Pool from resource")},  
105  { N_("delete"),     delete_cmd,    _("delete [pool=<pool-name> | media volume=<volume-name>]")},    
106  { N_("estimate"),   estimate_cmd,  _("performs FileSet estimate, listing gives full listing")},
107  { N_("exit"),       quit_cmd,      _("exit = quit")},
108  { N_("gui"),        gui_cmd,       _("gui [on/off] -- non-interactive gui mode")},
109  { N_("help"),       help_cmd,      _("print this command")},
110  { N_("list"),       list_cmd,      _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
111  { N_("label"),      label_cmd,     _("label a tape")},
112  { N_("llist"),      llist_cmd,     _("full or long list like list command")},
113  { N_("messages"),   messagescmd,   _("messages")},
114  { N_("mount"),      mount_cmd,     _("mount <storage-name>")},
115  { N_("prune"),      prunecmd,      _("prune expired records from catalog")},
116  { N_("purge"),      purgecmd,      _("purge records from catalog")},
117  { N_("python"),     python_cmd,    _("python control commands")},
118  { N_("quit"),       quit_cmd,      _("quit")},
119  { N_("query"),      querycmd,      _("query catalog")},
120  { N_("restore"),    restore_cmd,   _("restore files")},
121  { N_("relabel"),    relabel_cmd,   _("relabel a tape")},
122  { N_("release"),    release_cmd,   _("release <storage-name>")},
123  { N_("reload"),     reload_cmd,    _("reload conf file")},
124  { N_("run"),        run_cmd,       _("run <job-name>")},
125  { N_("status"),     status_cmd,    _("status [storage | client]=<name>")},
126  { N_("setdebug"),   setdebug_cmd,  _("sets debug level")},
127  { N_("setip"),      setip_cmd,     _("sets new client address -- if authorized")},
128  { N_("show"),       show_cmd,      _("show (resource records) [jobs | pools | ... | all]")},
129  { N_("sqlquery"),   sqlquerycmd,   _("use SQL to query catalog")}, 
130  { N_("time"),       time_cmd,      _("print current time")},
131  { N_("trace"),      trace_cmd,     _("turn on/off trace to file")},
132  { N_("unmount"),    unmount_cmd,   _("unmount <storage-name>")},
133  { N_("umount"),     unmount_cmd,   _("umount <storage-name> for old-time Unix guys")},
134  { N_("update"),     update_cmd,    _("update Volume or Pool")},
135  { N_("use"),        use_cmd,       _("use catalog xxx")},
136  { N_("var"),        var_cmd,       _("does variable expansion")},
137  { N_("version"),    version_cmd,   _("print Director version")},
138  { N_("wait"),       wait_cmd,      _("wait until no jobs are running")},
139              };
140 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
141
142 /*
143  * Execute a command from the UA
144  */
145 int do_a_command(UAContext *ua, const char *cmd)
146 {
147    unsigned int i;
148    int len, stat;
149    bool found = false;
150
151    stat = 1;
152
153    Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
154    if (ua->argc == 0) {
155       return 1;
156    }
157
158    len = strlen(ua->argk[0]);
159    for (i=0; i<comsize; i++) {     /* search for command */
160       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
161          if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
162             break;
163          }
164          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
165          found = true;
166          break;
167       }
168    }
169    if (!found) {
170       bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
171    }
172    return stat;
173 }
174
175 /*
176  * This is a common routine used to stuff the Pool DB record defaults
177  *   into the Media DB record just before creating a media (Volume) 
178  *   record.
179  */
180 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
181 {
182    mr->PoolId = pr->PoolId;
183    bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
184    mr->Recycle = pr->Recycle;
185    mr->VolRetention = pr->VolRetention;
186    mr->VolUseDuration = pr->VolUseDuration;
187    mr->MaxVolJobs = pr->MaxVolJobs;
188    mr->MaxVolFiles = pr->MaxVolFiles;
189    mr->MaxVolBytes = pr->MaxVolBytes;
190 }
191
192
193 /*
194  *  Add Volumes to an existing Pool
195  */
196 static int add_cmd(UAContext *ua, const char *cmd) 
197 {
198    POOL_DBR pr;
199    MEDIA_DBR mr;
200    int num, i, max, startnum;
201    int first_id = 0;
202    char name[MAX_NAME_LENGTH];
203    STORE *store;
204    int Slot = 0, InChanger = 0;
205
206    bsendmsg(ua, _(
207 "You probably don't want to be using this command since it\n"
208 "creates database records without labeling the Volumes.\n"
209 "You probably want to use the \"label\" command.\n\n"));
210
211    if (!open_db(ua)) {
212       return 1;
213    }
214
215    memset(&pr, 0, sizeof(pr));
216    memset(&mr, 0, sizeof(mr));
217
218    if (!get_pool_dbr(ua, &pr)) {
219       return 1;
220    }
221
222    Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
223       pr.MaxVols, pr.PoolType);
224
225    while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
226       bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
227       for (;;) {
228          if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
229             return 1;
230          }
231          pr.MaxVols = ua->pint32_val;
232       }
233    }
234
235    /* Get media type */
236    if ((store = get_storage_resource(ua, 0)) != NULL) {
237       bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
238    } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
239       return 1;
240    }
241       
242    if (pr.MaxVols == 0) {
243       max = 1000;
244    } else {
245       max = pr.MaxVols - pr.NumVols;
246    }
247    for (;;) {
248       char buf[100]; 
249       bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
250       if (!get_pint(ua, buf)) {
251          return 1;
252       }
253       num = ua->pint32_val;
254       if (num < 0 || num > max) {
255          bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
256          continue;
257       }
258       break;
259    }
260 getVolName:
261    if (num == 0) {
262       if (!get_cmd(ua, _("Enter Volume name: "))) {
263          return 1;
264       }
265    } else {
266       if (!get_cmd(ua, _("Enter base volume name: "))) {
267          return 1;
268       }
269    }
270    /* Don't allow | in Volume name because it is the volume separator character */
271    if (!is_volume_name_legal(ua, ua->cmd)) {
272       goto getVolName;
273    }
274    if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
275       bsendmsg(ua, _("Volume name too long.\n"));
276       goto getVolName;
277    }
278    if (strlen(ua->cmd) == 0) {
279       bsendmsg(ua, _("Volume name must be at least one character long.\n"));
280       goto getVolName;
281    }
282
283    bstrncpy(name, ua->cmd, sizeof(name));
284    if (num > 0) {
285       strcat(name, "%04d");
286
287       for (;;) {
288          if (!get_pint(ua, _("Enter the starting number: "))) {
289             return 1;
290          }
291          startnum = ua->pint32_val;
292          if (startnum < 1) {
293             bsendmsg(ua, _("Start number must be greater than zero.\n"));
294             continue;
295          }
296          break;
297       }
298    } else {
299       startnum = 1;
300       num = 1;
301    }
302
303    if (store && store->autochanger) {
304       if (!get_pint(ua, _("Enter slot (0 for none): "))) {
305          return 1;
306       }
307       Slot = ua->pint32_val;
308       if (!get_yesno(ua, _("InChanger? yes/no: "))) {
309          return 1;
310       }
311       InChanger = ua->pint32_val;
312    }
313            
314    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
315    for (i=startnum; i < num+startnum; i++) { 
316       bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
317       mr.Slot = Slot++;
318       mr.InChanger = InChanger;
319       Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
320       if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
321          bsendmsg(ua, "%s", db_strerror(ua->db));
322          return 1;
323       }
324       if (i == startnum) {
325          first_id = mr.PoolId;
326       }
327    }
328    pr.NumVols += num;
329    Dmsg0(200, "Update pool record.\n"); 
330    if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
331       bsendmsg(ua, "%s", db_strerror(ua->db));
332       return 1;
333    }
334    bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
335
336    return 1;
337 }
338
339 /*
340  * Turn auto mount on/off  
341  * 
342  *  automount on 
343  *  automount off
344  */
345 int automount_cmd(UAContext *ua, const char *cmd)
346 {
347    char *onoff;
348
349    if (ua->argc != 2) {
350       if (!get_cmd(ua, _("Turn on or off? "))) {
351             return 1;
352       }
353       onoff = ua->cmd;
354    } else {
355       onoff = ua->argk[1];
356    }
357
358    ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
359    return 1; 
360 }
361
362
363 /*
364  * Cancel a job
365  */
366 static int cancel_cmd(UAContext *ua, const char *cmd)
367 {
368    int i, ret;
369    int njobs = 0;
370    JCR *jcr = NULL;
371    char JobName[MAX_NAME_LENGTH];
372
373    if (!open_db(ua)) {
374       return 1;
375    }
376
377    for (i=1; i<ua->argc; i++) {
378       if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
379          uint32_t JobId;
380          if (!ua->argv[i]) {
381             break;
382          }
383          JobId = str_to_int64(ua->argv[i]);
384          if (!(jcr=get_jcr_by_id(JobId))) {
385             bsendmsg(ua, _("JobId %d is not running.\n"),  JobId);
386             return 1;
387          }
388          break;
389       } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
390          if (!ua->argv[i]) {
391             break;
392          }
393          if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
394             bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
395             return 1;
396          }
397          break;
398       }
399    }
400    /* If we still do not have a jcr,
401     *   throw up a list and ask the user to select one.
402     */
403    if (!jcr) {
404       /* Count Jobs running */
405       lock_jcr_chain();
406       foreach_jcr(jcr) {
407          if (jcr->JobId == 0) {      /* this is us */
408             free_locked_jcr(jcr);
409             continue;
410          }
411          free_locked_jcr(jcr);
412          njobs++;
413       }
414       unlock_jcr_chain();
415
416       if (njobs == 0) {
417          bsendmsg(ua, _("No Jobs running.\n"));
418          return 1;
419       }
420       start_prompt(ua, _("Select Job:\n"));
421       lock_jcr_chain();
422       foreach_jcr(jcr) {
423          if (jcr->JobId == 0) {      /* this is us */
424             free_locked_jcr(jcr);
425             continue;
426          }
427          add_prompt(ua, jcr->Job);
428          free_locked_jcr(jcr);
429       }
430       unlock_jcr_chain();
431
432       if (do_prompt(ua, _("Job"),  _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
433          return 1;
434       }
435       if (njobs == 1) {
436          if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
437             return 1;
438          }
439       }
440       /* NOTE! This increments the ref_count */
441       jcr = get_jcr_by_full_name(JobName);
442       if (!jcr) {
443          bsendmsg(ua, _("Job %s not found.\n"), JobName);
444          return 1;
445       }
446    }
447
448    ret = cancel_job(ua, jcr);
449    free_jcr(jcr);
450
451    return ret;
452 }
453
454 /*
455  * This is a common routine to create or update a
456  *   Pool DB base record from a Pool Resource. We handle
457  *   the setting of MaxVols and NumVols slightly differently
458  *   depending on if we are creating the Pool or we are
459  *   simply bringing it into agreement with the resource (updage).
460  */
461 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
462 {
463    strcpy(pr->PoolType, pool->pool_type);
464    if (op == POOL_OP_CREATE) {
465       pr->MaxVols = pool->max_volumes;
466       pr->NumVols = 0;
467    } else {          /* update pool */
468       if (pr->MaxVols != pool->max_volumes) {
469          pr->MaxVols = pool->max_volumes;
470       }
471       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
472          pr->MaxVols = pr->NumVols;
473       }
474    }
475    pr->UseOnce = pool->use_volume_once;
476    pr->UseCatalog = pool->use_catalog;
477    pr->AcceptAnyVolume = pool->accept_any_volume;
478    pr->Recycle = pool->Recycle;
479    pr->VolRetention = pool->VolRetention;
480    pr->VolUseDuration = pool->VolUseDuration;
481    pr->MaxVolJobs = pool->MaxVolJobs;
482    pr->MaxVolFiles = pool->MaxVolFiles;
483    pr->MaxVolBytes = pool->MaxVolBytes;
484    pr->AutoPrune = pool->AutoPrune;
485    pr->Recycle = pool->Recycle;
486    if (pool->label_format) {
487       strcpy(pr->LabelFormat, pool->label_format);
488    } else {
489       strcpy(pr->LabelFormat, "*");    /* none */
490    }
491 }
492
493
494 /*
495  * Create a pool record from a given Pool resource
496  *   Also called from backup.c
497  * Returns: -1  on error
498  *           0  record already exists
499  *           1  record created
500  */
501
502 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
503 {
504    POOL_DBR  pr;
505
506    memset(&pr, 0, sizeof(POOL_DBR));
507
508    strcpy(pr.Name, pool->hdr.name);
509
510    if (db_get_pool_record(jcr, db, &pr)) {
511       /* Pool Exists */
512       if (op == POOL_OP_UPDATE) {  /* update request */
513          set_pooldbr_from_poolres(&pr, pool, op);
514          db_update_pool_record(jcr, db, &pr);
515       }
516       return 0;                       /* exists */
517    }
518
519    set_pooldbr_from_poolres(&pr, pool, op);
520
521    if (!db_create_pool_record(jcr, db, &pr)) {
522       return -1;                      /* error */
523    }
524    return 1;
525 }
526
527
528
529 /*
530  * Create a Pool Record in the database.
531  *  It is always created from the Resource record.
532  */
533 static int create_cmd(UAContext *ua, const char *cmd) 
534 {
535    POOL *pool;
536
537    if (!open_db(ua)) {
538       return 1;
539    }
540
541    pool = get_pool_resource(ua);
542    if (!pool) {
543       return 1;
544    }
545
546    switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
547    case 0:
548       bsendmsg(ua, _("Error: Pool %s already exists.\n"
549                "Use update to change it.\n"), pool->hdr.name);
550       break;
551
552    case -1:
553       bsendmsg(ua, "%s", db_strerror(ua->db));
554       break;
555
556    default:
557      break;
558    }
559    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
560    return 1;
561 }
562
563
564 extern DIRRES *director;
565
566 /*
567  * Python control command
568  *  python restart (restarts interpreter)
569  */
570 static int python_cmd(UAContext *ua, const char *cmd)
571 {
572    if (strcasecmp(ua->argk[1], _("restart")) == 0) {
573       term_python_interpreter();
574       init_python_interpreter(director->hdr.name, director->scripts_directory ?
575          director->scripts_directory : ".");
576       bsendmsg(ua, _("Python interpreter restarted.\n"));
577    } else {
578       bsendmsg(ua, _("Nothing done.\n"));
579    }
580    return 1;
581 }
582
583
584 /*
585  * Set a new address in a Client resource. We do this only
586  *  if the Console name is the same as the Client name 
587  *  and the Console can access the client.
588  */
589 static int setip_cmd(UAContext *ua, const char *cmd) 
590 {
591    CLIENT *client;
592    char buf[1024]; 
593    if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
594       bsendmsg(ua, _("Illegal command from this console.\n"));
595       return 1;
596    }
597    LockRes();
598    client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
599
600    if (!client) {
601       bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
602       goto get_out;
603    }
604    if (client->address) {
605       free(client->address);
606    }
607    /* MA Bug 6 remove ifdef */
608    sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf)); 
609    client->address = bstrdup(buf);
610    bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
611             client->hdr.name, client->address);
612 get_out:
613    UnlockRes();
614    return 1;
615 }
616
617
618 /*
619  * Update a Pool Record in the database.
620  *  It is always updated from the Resource record.
621  *
622  *    update pool=<pool-name>
623  *         updates pool from Pool resource
624  *    update media pool=<pool-name> volume=<volume-name>
625  *         changes pool info for volume
626  *    update slots [scan=...]
627  *         updates autochanger slots
628  */
629 static int update_cmd(UAContext *ua, const char *cmd) 
630 {
631    static const char *kw[] = {
632       N_("media"),  /* 0 */
633       N_("volume"), /* 1 */
634       N_("pool"),   /* 2 */
635       N_("slots"),  /* 3 */
636       NULL};
637
638    if (!open_db(ua)) {
639       return 1;
640    }
641
642    switch (find_arg_keyword(ua, kw)) {
643    case 0:
644    case 1:
645       update_volume(ua);
646       return 1;
647    case 2:
648       update_pool(ua);
649       return 1;
650    case 3:
651       update_slots(ua);
652       return 1;
653    default:
654       break;
655    }
656     
657    start_prompt(ua, _("Update choice:\n"));
658    add_prompt(ua, _("Volume parameters"));
659    add_prompt(ua, _("Pool from resource"));
660    add_prompt(ua, _("Slots from autochanger"));
661    switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
662    case 0:
663       update_volume(ua);
664       break;
665    case 1:
666       update_pool(ua);
667       break;
668    case 2:
669       update_slots(ua);
670       break;
671    default:
672       break;
673    }
674    return 1;
675 }
676
677 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
678 {
679    POOLMEM *query = get_pool_memory(PM_MESSAGE);
680    const char *kw[] = {
681       "Append",
682       "Archive",
683       "Disabled",
684       "Full",
685       "Used", 
686       "Cleaning", 
687       "Recycle",
688       "Read-Only",
689       NULL};
690    bool found = false;
691    int i;
692
693    for (i=0; kw[i]; i++) {
694       if (strcasecmp(val, kw[i]) == 0) {
695          found = true;
696          break;
697       }
698    }
699    if (!found) {
700       bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
701    } else {
702       bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
703       Mmsg(query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
704          mr->VolStatus, mr->MediaId);
705       if (!db_sql_query(ua->db, query, NULL, NULL)) {  
706          bsendmsg(ua, "%s", db_strerror(ua->db));
707       } else {
708          bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
709       }
710    }
711    free_pool_memory(query);
712 }
713
714 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
715 {
716    char ed1[150];
717    POOLMEM *query;
718    if (!duration_to_utime(val, &mr->VolRetention)) {
719       bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
720       return;
721    }
722    query = get_pool_memory(PM_MESSAGE);
723    Mmsg(query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
724       edit_uint64(mr->VolRetention, ed1), mr->MediaId);
725    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
726       bsendmsg(ua, "%s", db_strerror(ua->db));
727    } else {
728       bsendmsg(ua, _("New retention period is: %s\n"),
729          edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
730    }
731    free_pool_memory(query);
732 }
733
734 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
735 {
736    char ed1[150];
737    POOLMEM *query;
738
739    if (!duration_to_utime(val, &mr->VolUseDuration)) {
740       bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
741       return;
742    }
743    query = get_pool_memory(PM_MESSAGE);
744    Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
745       edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
746    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
747       bsendmsg(ua, "%s", db_strerror(ua->db));
748    } else {
749       bsendmsg(ua, _("New use duration is: %s\n"),
750          edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
751    }
752    free_pool_memory(query);
753 }
754
755 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
756 {
757    POOLMEM *query = get_pool_memory(PM_MESSAGE);
758    Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
759       val, mr->MediaId);
760    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
761       bsendmsg(ua, "%s", db_strerror(ua->db));
762    } else {
763       bsendmsg(ua, _("New max jobs is: %s\n"), val);
764    }
765    free_pool_memory(query);
766 }
767
768 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
769 {
770    POOLMEM *query = get_pool_memory(PM_MESSAGE);
771    Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
772       val, mr->MediaId);
773    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
774       bsendmsg(ua, "%s", db_strerror(ua->db));
775    } else {
776       bsendmsg(ua, _("New max files is: %s\n"), val);
777    }
778    free_pool_memory(query);
779 }
780
781 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
782 {
783    uint64_t maxbytes;
784    char ed1[50];
785    POOLMEM *query;
786
787    if (!size_to_uint64(val, strlen(val), &maxbytes)) {
788       bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
789       return;
790    } 
791    query = get_pool_memory(PM_MESSAGE);
792    Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
793       edit_uint64(maxbytes, ed1), mr->MediaId);
794    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
795       bsendmsg(ua, "%s", db_strerror(ua->db));
796    } else {
797       bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
798    }
799    free_pool_memory(query);
800 }
801
802 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
803 {
804    int recycle;
805    POOLMEM *query;
806    if (strcasecmp(val, _("yes")) == 0) {
807       recycle = 1;
808    } else if (strcasecmp(val, _("no")) == 0) {
809       recycle = 0;
810    } else {
811       bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
812       return;
813    }
814    query = get_pool_memory(PM_MESSAGE);
815    Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
816       recycle, mr->MediaId);
817    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
818       bsendmsg(ua, "%s", db_strerror(ua->db));
819    } else {       
820       bsendmsg(ua, _("New Recycle flag is: %s\n"),
821          mr->Recycle==1?_("yes"):_("no"));
822    }
823    free_pool_memory(query);
824 }
825
826 /* Modify the Pool in which this Volume is located */
827 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
828 {
829    POOL_DBR pr;
830    POOLMEM *query;
831
832    memset(&pr, 0, sizeof(pr));
833    bstrncpy(pr.Name, val, sizeof(pr.Name));
834    if (!get_pool_dbr(ua, &pr)) {
835       return;
836    }
837    mr->PoolId = pr.PoolId;            /* set new PoolId */
838    /*
839     */
840    query = get_pool_memory(PM_MESSAGE);
841    db_lock(ua->db);
842    Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
843       mr->PoolId, mr->MediaId);
844    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
845       bsendmsg(ua, "%s", db_strerror(ua->db));
846    } else {       
847       bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
848       opr->NumVols--;
849       if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
850          bsendmsg(ua, "%s", db_strerror(ua->db));
851       }
852       pr.NumVols++;
853       if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
854          bsendmsg(ua, "%s", db_strerror(ua->db));
855       }
856       db_make_inchanger_unique(ua->jcr, ua->db, mr);
857    }
858    db_unlock(ua->db);
859    free_pool_memory(query);
860 }
861
862 /*
863  * Refresh the Volume information from the Pool record
864  */
865 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
866 {
867    POOL_DBR pr;
868
869    memset(&pr, 0, sizeof(pr));
870    pr.PoolId = mr->PoolId;
871    if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
872        !acl_access_ok(ua, Pool_ACL, pr.Name)) {
873       return;
874    }
875    set_pool_dbr_defaults_in_media_dbr(mr, &pr);
876    if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
877       bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
878    } else {
879       bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
880          pr.Name);
881    }
882 }
883
884 /*
885  * Refresh the Volume information from the Pool record
886  *   for all Volumes
887  */
888 static void update_all_vols_from_pool(UAContext *ua)
889 {
890    POOL_DBR pr;
891    MEDIA_DBR mr;
892
893    memset(&pr, 0, sizeof(pr));
894    memset(&mr, 0, sizeof(mr));
895    if (!get_pool_dbr(ua, &pr)) {
896       return;
897    }
898    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
899    mr.PoolId = pr.PoolId;
900    if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
901       bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
902    } else {
903       bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
904    }
905 }
906
907
908 /*
909  * Update a media record -- allows you to change the
910  *  Volume status. E.g. if you want Bacula to stop
911  *  writing on the volume, set it to anything other
912  *  than Append.
913  */              
914 static int update_volume(UAContext *ua)
915 {
916    MEDIA_DBR mr;
917    POOL_DBR pr;
918    POOLMEM *query;
919    char ed1[130];
920    bool done = false;
921    const char *kw[] = {
922       N_("VolStatus"),                /* 0 */
923       N_("VolRetention"),             /* 1 */
924       N_("VolUse"),                   /* 2 */
925       N_("MaxVolJobs"),               /* 3 */
926       N_("MaxVolFiles"),              /* 4 */
927       N_("MaxVolBytes"),              /* 5 */
928       N_("Recycle"),                  /* 6 */
929       N_("Pool"),                     /* 7 */
930       N_("FromPool"),                 /* 8 */
931       N_("AllFromPool"),              /* 9 */
932       NULL };
933
934    for (int i=0; kw[i]; i++) {
935       int j;
936       POOL_DBR pr;
937       if ((j=find_arg_with_value(ua, kw[i])) > 0) {
938          if (i != 9 && !select_media_dbr(ua, &mr)) {
939             return 0;
940          }
941          switch (i) {
942          case 0:
943             update_volstatus(ua, ua->argv[j], &mr);
944             break;
945          case 1:
946             update_volretention(ua, ua->argv[j], &mr);
947             break;
948          case 2:
949             update_voluseduration(ua, ua->argv[j], &mr);
950             break;
951          case 3:
952             update_volmaxjobs(ua, ua->argv[j], &mr);
953             break;
954          case 4:
955             update_volmaxfiles(ua, ua->argv[j], &mr);
956             break;
957          case 5:
958             update_volmaxbytes(ua, ua->argv[j], &mr);
959             break;
960          case 6:
961             update_volrecycle(ua, ua->argv[j], &mr);
962             break;
963          case 7:
964             memset(&pr, 0, sizeof(POOL_DBR));
965             pr.PoolId = mr.PoolId;
966             if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
967                bsendmsg(ua, "%s", db_strerror(ua->db));
968                break;
969             }
970             update_vol_pool(ua, ua->argv[j], &mr, &pr);
971             break;
972          case 8:
973             update_volfrompool(ua, &mr);
974             return 1;
975          case 9:
976             update_all_vols_from_pool(ua);
977             return 1;
978          }
979          done = true;
980       }
981    }
982
983    for ( ; !done; ) {
984       if (!select_media_dbr(ua, &mr)) {
985          return 0;
986       }
987       bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
988       start_prompt(ua, _("Parameters to modify:\n"));
989       add_prompt(ua, _("Volume Status"));
990       add_prompt(ua, _("Volume Retention Period"));
991       add_prompt(ua, _("Volume Use Duration"));
992       add_prompt(ua, _("Maximum Volume Jobs"));
993       add_prompt(ua, _("Maximum Volume Files"));
994       add_prompt(ua, _("Maximum Volume Bytes"));
995       add_prompt(ua, _("Recycle Flag"));
996       add_prompt(ua, _("Slot"));
997       add_prompt(ua, _("InChanger Flag"));
998       add_prompt(ua, _("Volume Files"));
999       add_prompt(ua, _("Pool"));
1000       add_prompt(ua, _("Volume from Pool"));
1001       add_prompt(ua, _("All Volumes from Pool"));
1002       add_prompt(ua, _("Done"));
1003       switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
1004       case 0:                         /* Volume Status */
1005          /* Modify Volume Status */
1006          bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
1007          start_prompt(ua, _("Possible Values are:\n"));
1008          add_prompt(ua, "Append");      /* Better not translate these as */
1009          add_prompt(ua, "Archive");     /* They are known in the database code */
1010          add_prompt(ua, "Disabled");
1011          add_prompt(ua, "Full");
1012          add_prompt(ua, "Used");
1013          add_prompt(ua, "Cleaning");
1014          if (strcmp(mr.VolStatus, "Purged") == 0) {
1015             add_prompt(ua, "Recycle");
1016          }
1017          add_prompt(ua, "Read-Only");
1018          if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
1019             return 1;
1020          }
1021          update_volstatus(ua, ua->cmd, &mr);
1022          break;
1023       case 1:                         /* Retention */
1024          bsendmsg(ua, _("Current retention period is: %s\n"),
1025             edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1026          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1027             return 0;
1028          }
1029          update_volretention(ua, ua->cmd, &mr);
1030          break;
1031
1032       case 2:                         /* Use Duration */
1033          bsendmsg(ua, _("Current use duration is: %s\n"),
1034             edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1035          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1036             return 0;
1037          }
1038          update_voluseduration(ua, ua->cmd, &mr);
1039          break;
1040
1041       case 3:                         /* Max Jobs */
1042          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1043          if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1044             return 0;
1045          }
1046          update_volmaxjobs(ua, ua->cmd, &mr);
1047          break;
1048
1049       case 4:                         /* Max Files */
1050          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1051          if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1052             return 0;
1053          }
1054          update_volmaxfiles(ua, ua->cmd, &mr);
1055          break;
1056
1057       case 5:                         /* Max Bytes */
1058          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1059          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1060             return 0;
1061          }
1062          update_volmaxbytes(ua, ua->cmd, &mr);
1063          break;
1064
1065
1066       case 6:                         /* Recycle */
1067          bsendmsg(ua, _("Current recycle flag is: %s\n"),
1068             mr.Recycle==1?_("yes"):_("no"));
1069          if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1070             return 0;
1071          }
1072          update_volrecycle(ua, ua->cmd, &mr);
1073          break;
1074
1075       case 7:                         /* Slot */
1076          int Slot;
1077
1078          memset(&pr, 0, sizeof(POOL_DBR));
1079          pr.PoolId = mr.PoolId;
1080          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1081             bsendmsg(ua, "%s", db_strerror(ua->db));
1082             return 0;
1083          }
1084          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1085          if (!get_pint(ua, _("Enter new Slot: "))) {
1086             return 0;
1087          }
1088          Slot = ua->pint32_val;
1089          if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1090             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1091                pr.MaxVols);
1092             break;
1093          }
1094          mr.Slot = Slot;
1095          /*
1096           * Make sure to use db_update... rather than doing this directly,
1097           *   so that any Slot is handled correctly. 
1098           */
1099          if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1100             bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1101          } else {
1102             bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1103          }
1104          break;
1105
1106       case 8:                         /* InChanger */
1107          bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1108          if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1109             return 0;
1110          }
1111          mr.InChanger = ua->pint32_val;
1112          /*
1113           * Make sure to use db_update... rather than doing this directly,
1114           *   so that any Slot is handled correctly. 
1115           */
1116          if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1117             bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1118          } else {
1119             bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1120          }
1121          break;
1122
1123
1124       case 9:                         /* Volume Files */
1125          int32_t VolFiles;
1126          bsendmsg(ua, _("Warning changing Volume Files can result\n"
1127                         "in loss of data on your Volume\n\n"));
1128          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1129          if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1130             return 0;
1131          }
1132          VolFiles = ua->pint32_val;
1133          if (VolFiles != (int)(mr.VolFiles + 1)) {
1134             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1135             if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1136                break;
1137             }
1138          }
1139          query = get_pool_memory(PM_MESSAGE);
1140          Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1141             VolFiles, mr.MediaId);
1142          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
1143             bsendmsg(ua, "%s", db_strerror(ua->db));
1144          } else {
1145             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1146          }
1147          free_pool_memory(query);
1148          break;
1149
1150       case 10:                        /* Volume's Pool */
1151          memset(&pr, 0, sizeof(POOL_DBR));
1152          pr.PoolId = mr.PoolId;
1153          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1154             bsendmsg(ua, "%s", db_strerror(ua->db));
1155             return 0;
1156          }
1157          bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1158          if (!get_cmd(ua, _("Enter new Pool name: "))) {
1159             return 0;
1160          }
1161          update_vol_pool(ua, ua->cmd, &mr, &pr);
1162          return 1;
1163
1164       case 11:
1165          update_volfrompool(ua, &mr);
1166          return 1;
1167       case 12:
1168          update_all_vols_from_pool(ua);
1169          return 1;
1170       default:                        /* Done or error */
1171          bsendmsg(ua, "Selection done.\n");
1172          return 1;
1173       }
1174    }
1175    return 1;
1176 }
1177
1178 /* 
1179  * Update pool record -- pull info from current POOL resource
1180  */
1181 static int update_pool(UAContext *ua)
1182 {
1183    POOL_DBR  pr;
1184    int id;
1185    POOL *pool;
1186    POOLMEM *query;       
1187    
1188    pool = get_pool_resource(ua);
1189    if (!pool) {
1190       return 0;
1191    }
1192
1193    memset(&pr, 0, sizeof(pr));
1194    strcpy(pr.Name, pool->hdr.name);
1195    if (!get_pool_dbr(ua, &pr)) {
1196       return 0;
1197    }
1198
1199    set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1200
1201    id = db_update_pool_record(ua->jcr, ua->db, &pr);
1202    if (id <= 0) {
1203       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1204          id, db_strerror(ua->db));
1205    }
1206    query = get_pool_memory(PM_MESSAGE);
1207    Mmsg(query, list_pool, pr.PoolId);
1208    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1209    free_pool_memory(query);
1210    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1211    return 1;
1212 }
1213
1214
1215 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1216 {
1217    BSOCK *sd;
1218    JCR *jcr = ua->jcr;
1219
1220    jcr->store = store;
1221    /* Try connecting for up to 15 seconds */
1222    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
1223       store->hdr.name, store->address, store->SDport);
1224    if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1225       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1226       return;
1227    }
1228    Dmsg0(120, _("Connected to storage daemon\n"));
1229    sd = jcr->store_bsock;
1230    bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1231    if (bnet_recv(sd) >= 0) {
1232       bsendmsg(ua, "%s", sd->msg);
1233    }
1234    bnet_sig(sd, BNET_TERMINATE);
1235    bnet_close(sd);
1236    jcr->store_bsock = NULL;
1237    return;  
1238 }
1239    
1240 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1241 {
1242    BSOCK *fd;
1243
1244    /* Connect to File daemon */
1245
1246    ua->jcr->client = client;
1247    /* Try to connect for 15 seconds */
1248    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
1249       client->hdr.name, client->address, client->FDport);
1250    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1251       bsendmsg(ua, _("Failed to connect to Client.\n"));
1252       return;
1253    }
1254    Dmsg0(120, "Connected to file daemon\n");
1255    fd = ua->jcr->file_bsock;
1256    bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1257    if (bnet_recv(fd) >= 0) {
1258       bsendmsg(ua, "%s", fd->msg);
1259    }
1260    bnet_sig(fd, BNET_TERMINATE);
1261    bnet_close(fd);
1262    ua->jcr->file_bsock = NULL;
1263    return;  
1264 }
1265
1266
1267 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1268 {
1269    STORE *store, **unique_store;
1270    CLIENT *client, **unique_client;
1271    int i, j, found;
1272
1273    /* Director */
1274    debug_level = level;
1275
1276    /* Count Storage items */
1277    LockRes();
1278    store = NULL;
1279    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1280       { }
1281    unique_store = (STORE **) malloc(i * sizeof(STORE));
1282    /* Find Unique Storage address/port */         
1283    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1284    i = 0;
1285    unique_store[i++] = store;
1286    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1287       found = 0;
1288       for (j=0; j<i; j++) {
1289          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1290              unique_store[j]->SDport == store->SDport) {
1291             found = 1;
1292             break;
1293          }
1294       }
1295       if (!found) {
1296          unique_store[i++] = store;
1297          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1298       }
1299    }
1300    UnlockRes();
1301
1302    /* Call each unique Storage daemon */
1303    for (j=0; j<i; j++) {
1304       do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1305    }
1306    free(unique_store);
1307
1308    /* Count Client items */
1309    LockRes();
1310    client = NULL;
1311    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1312       { }
1313    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1314    /* Find Unique Client address/port */         
1315    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1316    i = 0;
1317    unique_client[i++] = client;
1318    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1319       found = 0;
1320       for (j=0; j<i; j++) {
1321          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1322              unique_client[j]->FDport == client->FDport) {
1323             found = 1;
1324             break;
1325          }
1326       }
1327       if (!found) {
1328          unique_client[i++] = client;
1329          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1330       }
1331    }
1332    UnlockRes();
1333
1334    /* Call each unique File daemon */
1335    for (j=0; j<i; j++) {
1336       do_client_setdebug(ua, unique_client[j], level, trace_flag);
1337    }
1338    free(unique_client);
1339 }
1340
1341 /*
1342  * setdebug level=nn all trace=1/0
1343  */
1344 static int setdebug_cmd(UAContext *ua, const char *cmd)
1345 {
1346    STORE *store;
1347    CLIENT *client;
1348    int level;
1349    int trace_flag = -1;
1350    int i;
1351
1352    if (!open_db(ua)) {
1353       return 1;
1354    }
1355    Dmsg1(120, "setdebug:%s:\n", cmd);
1356
1357    level = -1;
1358    i = find_arg_with_value(ua, _("level"));
1359    if (i >= 0) {
1360       level = atoi(ua->argv[i]);
1361    }
1362    if (level < 0) {
1363       if (!get_pint(ua, _("Enter new debug level: "))) {
1364          return 1;
1365       }
1366       level = ua->pint32_val;
1367    }
1368
1369    /* Look for trace flag. -1 => not change */
1370    i = find_arg_with_value(ua, _("trace"));
1371    if (i >= 0) {
1372       trace_flag = atoi(ua->argv[i]);
1373       if (trace_flag > 0) {
1374          trace_flag = 1;
1375       }
1376    }
1377
1378    /* General debug? */
1379    for (i=1; i<ua->argc; i++) {
1380       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1381          do_all_setdebug(ua, level, trace_flag);
1382          return 1;
1383       }
1384       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1385           strcasecmp(ua->argk[i], _("director")) == 0) {
1386          debug_level = level;
1387          set_trace(trace_flag);
1388          return 1;
1389       }
1390       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1391           strcasecmp(ua->argk[i], _("fd")) == 0) {
1392          client = NULL;
1393          if (ua->argv[i]) {
1394             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1395             if (client) {
1396                do_client_setdebug(ua, client, level, trace_flag);
1397                return 1;
1398             }
1399          }
1400          client = select_client_resource(ua);   
1401          if (client) {
1402             do_client_setdebug(ua, client, level, trace_flag);
1403             return 1;
1404          }
1405       }
1406
1407       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1408           strcasecmp(ua->argk[i], _("storage")) == 0 ||
1409           strcasecmp(ua->argk[i], _("sd")) == 0) {
1410          store = NULL;
1411          if (ua->argv[i]) {
1412             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1413             if (store) {
1414                do_storage_setdebug(ua, store, level, trace_flag);
1415                return 1;
1416             }
1417          }
1418          store = get_storage_resource(ua, 0);
1419          if (store) {
1420             do_storage_setdebug(ua, store, level, trace_flag);
1421             return 1;
1422          }
1423       }
1424    } 
1425    /*
1426     * We didn't find an appropriate keyword above, so
1427     * prompt the user.
1428     */
1429    start_prompt(ua, _("Available daemons are: \n"));
1430    add_prompt(ua, _("Director"));
1431    add_prompt(ua, _("Storage"));
1432    add_prompt(ua, _("Client"));
1433    add_prompt(ua, _("All"));
1434    switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1435    case 0:                         /* Director */
1436       debug_level = level;
1437       set_trace(trace_flag);
1438       break;
1439    case 1:
1440       store = get_storage_resource(ua, 0);
1441       if (store) {
1442          do_storage_setdebug(ua, store, level, trace_flag);
1443       }
1444       break;
1445    case 2:
1446       client = select_client_resource(ua);
1447       if (client) {
1448          do_client_setdebug(ua, client, level, trace_flag);
1449       }
1450       break;
1451    case 3:
1452       do_all_setdebug(ua, level, trace_flag);
1453       break;
1454    default:
1455       break;
1456    }
1457    return 1;
1458 }
1459
1460 /*
1461  * Turn debug tracing to file on/off
1462  */
1463 static int trace_cmd(UAContext *ua, const char *cmd)
1464 {
1465    char *onoff;
1466
1467    if (ua->argc != 2) {
1468       if (!get_cmd(ua, _("Turn on or off? "))) {
1469             return 1;
1470       }
1471       onoff = ua->cmd;
1472    } else {
1473       onoff = ua->argk[1];
1474    }
1475
1476    set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1477    return 1; 
1478
1479 }
1480
1481 static int var_cmd(UAContext *ua, const char *cmd)
1482 {
1483    POOLMEM *val = get_pool_memory(PM_FNAME);
1484    char *var;
1485
1486    if (!open_db(ua)) {
1487       return 1;
1488    }
1489    for (var=ua->cmd; *var != ' '; ) {    /* skip command */
1490       var++;
1491    }
1492    while (*var == ' ') {                 /* skip spaces */
1493       var++;
1494    }
1495    Dmsg1(100, "Var=%s:\n", var);
1496    variable_expansion(ua->jcr, var, &val);  
1497    bsendmsg(ua, "%s\n", val);
1498    free_pool_memory(val);
1499    return 1;
1500 }
1501
1502 static int estimate_cmd(UAContext *ua, const char *cmd)
1503 {
1504    JOB *job = NULL;
1505    CLIENT *client = NULL;
1506    FILESET *fileset = NULL;
1507    FILESET_DBR fsr;
1508    int listing = 0;
1509    char since[MAXSTRING];
1510    JCR *jcr = ua->jcr;
1511
1512    jcr->JobLevel = L_FULL;
1513    for (int i=1; i<ua->argc; i++) {
1514       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1515           strcasecmp(ua->argk[i], _("fd")) == 0) {
1516          if (ua->argv[i]) {
1517             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1518             continue;
1519          }
1520       }
1521       if (strcasecmp(ua->argk[i], _("job")) == 0) {
1522          if (ua->argv[i]) {
1523             job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1524             continue;
1525          }
1526       }
1527       if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1528          if (ua->argv[i]) {
1529             fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1530             continue;
1531          }
1532       }
1533       if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1534          listing = 1;
1535          continue;
1536       }
1537       if (strcasecmp(ua->argk[i], _("level")) == 0) {
1538          if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1539             bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1540          }
1541          continue;
1542       }
1543    } 
1544    if (!job && !(client && fileset)) {
1545       if (!(job = select_job_resource(ua))) {
1546          return 1;
1547       }
1548    }
1549    if (!job) {
1550       job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1551       if (!job) {
1552          bsendmsg(ua, _("No job specified.\n"));
1553          return 1;
1554       }
1555    }
1556    if (!client) {
1557       client = job->client;
1558    }
1559    if (!fileset) {
1560       fileset = job->fileset;
1561    }
1562    jcr->client = client;
1563    jcr->fileset = fileset;
1564    close_db(ua);
1565    ua->catalog = client->catalog;
1566
1567    if (!open_db(ua)) {
1568       return 1;
1569    }
1570
1571    jcr->job = job;
1572    jcr->JobType = JT_BACKUP;
1573    init_jcr_job_record(jcr);
1574
1575    if (!get_or_create_client_record(jcr)) {
1576       return 1;
1577    }
1578    if (!get_or_create_fileset_record(jcr, &fsr)) {
1579       return 1;
1580    }
1581    
1582    get_level_since_time(ua->jcr, since, sizeof(since));
1583
1584    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1585       job->client->hdr.name, job->client->address, job->client->FDport);
1586    if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1587       bsendmsg(ua, _("Failed to connect to Client.\n"));
1588       return 1;
1589    }
1590
1591    if (!send_include_list(jcr)) {
1592       bsendmsg(ua, _("Error sending include list.\n"));
1593       goto bail_out;
1594    }
1595
1596    if (!send_exclude_list(jcr)) {
1597       bsendmsg(ua, _("Error sending exclude list.\n"));
1598       goto bail_out;
1599    }
1600
1601    if (!send_level_command(jcr)) {
1602       goto bail_out;
1603    }
1604
1605    bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1606    while (bnet_recv(jcr->file_bsock) >= 0) {
1607       bsendmsg(ua, "%s", jcr->file_bsock->msg);
1608    }
1609
1610 bail_out:
1611    if (jcr->file_bsock) {
1612       bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1613       bnet_close(jcr->file_bsock);
1614       jcr->file_bsock = NULL;
1615    }
1616    return 1;
1617 }
1618
1619
1620 /*
1621  * print time
1622  */
1623 static int time_cmd(UAContext *ua, const char *cmd)
1624 {
1625    char sdt[50];
1626    time_t ttime = time(NULL);
1627    struct tm tm;
1628    localtime_r(&ttime, &tm);
1629    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1630    bsendmsg(ua, "%s\n", sdt);
1631    return 1;
1632 }
1633
1634 /*
1635  * reload the conf file
1636  */
1637 extern "C" void reload_config(int sig);
1638
1639 static int reload_cmd(UAContext *ua, const char *cmd)
1640 {
1641    reload_config(1);   
1642    return 1;
1643 }
1644
1645 /*
1646  * Delete Pool records (should purge Media with it).
1647  *
1648  *  delete pool=<pool-name>
1649  *  delete volume pool=<pool-name> volume=<name>
1650  *  delete job jobid=xxx
1651  */
1652 static int delete_cmd(UAContext *ua, const char *cmd)
1653 {
1654    static const char *keywords[] = {
1655       N_("volume"),
1656       N_("pool"),
1657       N_("job"),
1658       N_("jobid"),
1659       NULL};
1660
1661    if (!open_db(ua)) {
1662       return 1;
1663    }
1664
1665    switch (find_arg_keyword(ua, keywords)) {      
1666    case 0:
1667       delete_volume(ua);     
1668       return 1;
1669    case 1:
1670       delete_pool(ua);
1671       return 1;
1672    case 2:
1673    case 3:
1674       int i;
1675       while ((i=find_arg(ua, _("jobid"))) > 0) {
1676          delete_job(ua);
1677          *ua->argk[i] = 0;         /* zap keyword already visited */
1678       }
1679       return 1;
1680    default:
1681       break;
1682    }
1683
1684    bsendmsg(ua, _(
1685 "In general it is not a good idea to delete either a\n"
1686 "Pool or a Volume since they may contain data.\n\n"));
1687
1688    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1689    case 0:
1690       delete_volume(ua);
1691       break;
1692    case 1:
1693       delete_pool(ua);
1694       break;
1695    case 2:
1696       delete_job(ua);
1697       return 1;
1698    default:
1699       bsendmsg(ua, _("Nothing done.\n"));
1700       break;
1701    }
1702    return 1;
1703 }
1704
1705
1706 /*
1707  * delete_job has been modified to parse JobID lists like the
1708  * following:
1709  * delete job JobID=3,4,6,7-11,14
1710  * 
1711  * Thanks to Phil Stracchino for the above addition.
1712  */
1713
1714 static void delete_job(UAContext *ua)
1715 {
1716    JobId_t JobId;
1717    char *s,*sep,*tok;
1718
1719    int i = find_arg_with_value(ua, _("jobid"));
1720    if (i >= 0) {
1721       if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1722         s = bstrdup(ua->argv[i]);
1723         tok = s;
1724         /*
1725          * We could use strtok() here.  But we're not going to, because:
1726          * (a) strtok() is deprecated, having been replaced by strsep();
1727          * (b) strtok() is broken in significant ways.
1728          * we could use strsep() instead, but it's not universally available.
1729          * so we grow our own using strchr().
1730          */
1731         sep = strchr(tok, ',');
1732         while (sep != NULL) {
1733            *sep = '\0';
1734            if (strchr(tok, '-')) {
1735                delete_job_id_range(ua, tok);
1736            } else {
1737               JobId = str_to_int64(tok);
1738               do_job_delete(ua, JobId);
1739            }
1740            tok = ++sep;
1741            sep = strchr(tok, ',');
1742         }
1743         /* pick up the last token */
1744         if (strchr(tok, '-')) {
1745             delete_job_id_range(ua, tok);
1746         } else {
1747             JobId = str_to_int64(tok);
1748             do_job_delete(ua, JobId);
1749         }
1750         
1751          free(s);
1752       } else {
1753          JobId = str_to_int64(ua->argv[i]);
1754         do_job_delete(ua, JobId);
1755       }
1756    } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1757       return;
1758    } else {
1759       JobId = ua->pint32_val;
1760       do_job_delete(ua, JobId);
1761    }
1762 }
1763
1764 /*
1765  * we call delete_job_id_range to parse range tokens and iterate over ranges
1766  */
1767 static void delete_job_id_range(UAContext *ua, char *tok)
1768 {
1769    char *tok2;
1770    JobId_t j,j1,j2;
1771
1772    tok2 = strchr(tok, '-');
1773    *tok2 = '\0';
1774    tok2++;
1775    j1 = str_to_int64(tok);
1776    j2 = str_to_int64(tok2);
1777    for (j=j1; j<=j2; j++) {
1778       do_job_delete(ua, j);
1779    }
1780 }
1781
1782 /*
1783  * do_job_delete now performs the actual delete operation atomically
1784  * we always return 1 because C++ is pissy about void functions
1785  */
1786
1787 static void do_job_delete(UAContext *ua, JobId_t JobId)
1788 {
1789    POOLMEM *query = get_pool_memory(PM_MESSAGE);
1790
1791    Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1792    db_sql_query(ua->db, query, NULL, (void *)NULL);
1793    Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1794    db_sql_query(ua->db, query, NULL, (void *)NULL);
1795    Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1796    db_sql_query(ua->db, query, NULL, (void *)NULL);
1797    free_pool_memory(query);
1798    bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1799 }
1800
1801 /*
1802  * Delete media records from database -- dangerous 
1803  */
1804 static int delete_volume(UAContext *ua)
1805 {
1806    MEDIA_DBR mr;
1807
1808    if (!select_media_dbr(ua, &mr)) {
1809       return 1;
1810    }
1811    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1812       "and all Jobs saved on that volume from the Catalog\n"),
1813       mr.VolumeName);
1814
1815    if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1816       return 1;
1817    }
1818    if (ua->pint32_val) {
1819       db_delete_media_record(ua->jcr, ua->db, &mr);
1820    }
1821    return 1;
1822 }
1823
1824 /*
1825  * Delete a pool record from the database -- dangerous   
1826  */
1827 static int delete_pool(UAContext *ua)
1828 {
1829    POOL_DBR  pr;
1830    
1831    memset(&pr, 0, sizeof(pr));
1832
1833    if (!get_pool_dbr(ua, &pr)) {
1834       return 1;
1835    }
1836    if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1837       return 1;
1838    }
1839    if (ua->pint32_val) {
1840       db_delete_pool_record(ua->jcr, ua->db, &pr);
1841    }
1842    return 1;
1843 }
1844
1845
1846 static void do_mount_cmd(UAContext *ua, const char *command)
1847 {
1848    STORE *store;
1849    BSOCK *sd;
1850    JCR *jcr = ua->jcr;
1851    char dev_name[MAX_NAME_LENGTH];
1852
1853    if (!open_db(ua)) {
1854       return;
1855    }
1856    Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1857
1858    store = get_storage_resource(ua, 1);
1859    if (!store) {
1860       return;
1861    }
1862
1863    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1864       store->media_type, store->dev_name);
1865
1866    jcr->store = store;
1867    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1868       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1869       return;
1870    }
1871    sd = jcr->store_bsock;
1872    bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
1873    bash_spaces(dev_name);
1874    bnet_fsend(sd, "%s %s", command, dev_name);
1875    while (bnet_recv(sd) >= 0) {
1876       bsendmsg(ua, "%s", sd->msg);
1877    }
1878    bnet_sig(sd, BNET_TERMINATE);
1879    bnet_close(sd);
1880    jcr->store_bsock = NULL;
1881 }
1882
1883 /*
1884  * mount [storage | device] <name>
1885  */
1886 static int mount_cmd(UAContext *ua, const char *cmd)
1887 {
1888    do_mount_cmd(ua, "mount");          /* mount */
1889    return 1;
1890 }
1891
1892
1893 /*
1894  * unmount [storage | device] <name>
1895  */
1896 static int unmount_cmd(UAContext *ua, const char *cmd)
1897 {
1898    do_mount_cmd(ua, "unmount");          /* unmount */
1899    return 1;
1900 }
1901
1902
1903 /*
1904  * release [storage | device] <name>
1905  */
1906 static int release_cmd(UAContext *ua, const char *cmd)
1907 {
1908    do_mount_cmd(ua, "release");          /* release */
1909    return 1;
1910 }
1911
1912
1913 /*
1914  * Switch databases
1915  *   use catalog=<name>
1916  */
1917 static int use_cmd(UAContext *ua, const char *cmd)
1918 {
1919    CAT *oldcatalog, *catalog;
1920
1921
1922    close_db(ua);                      /* close any previously open db */
1923    oldcatalog = ua->catalog;
1924
1925    if (!(catalog = get_catalog_resource(ua))) {
1926       ua->catalog = oldcatalog;
1927    } else {
1928       ua->catalog = catalog;
1929    }
1930    if (open_db(ua)) {
1931       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1932          ua->catalog->hdr.name, ua->catalog->db_name);
1933    }
1934    return 1;
1935 }
1936
1937 int quit_cmd(UAContext *ua, const char *cmd) 
1938 {
1939    ua->quit = TRUE;
1940    return 1;
1941 }
1942
1943 /*
1944  * Wait until no job is running 
1945  */
1946 int wait_cmd(UAContext *ua, const char *cmd) 
1947 {
1948    JCR *jcr;
1949    bmicrosleep(0, 200000);            /* let job actually start */
1950    for (bool running=true; running; ) {
1951       running = false;
1952       lock_jcr_chain();
1953       foreach_jcr(jcr) {
1954          if (jcr->JobId != 0) {
1955             running = true;
1956             free_locked_jcr(jcr);
1957             break;
1958          }
1959          free_locked_jcr(jcr);
1960       }
1961       unlock_jcr_chain();
1962       if (running) {
1963          bmicrosleep(1, 0);
1964       }
1965    }
1966    return 1;
1967 }
1968
1969
1970 static int help_cmd(UAContext *ua, const char *cmd)
1971 {
1972    unsigned int i;
1973
1974    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1975    for (i=0; i<comsize; i++) {
1976       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1977    }
1978    bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1979    return 1;
1980 }
1981
1982 int qhelp_cmd(UAContext *ua, const char *cmd)
1983 {
1984    unsigned int i;
1985
1986    for (i=0; i<comsize; i++) {
1987       bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1988    }
1989    return 1;
1990 }
1991
1992 static int version_cmd(UAContext *ua, const char *cmd)
1993 {
1994    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1995    return 1;
1996 }
1997
1998
1999 /* A bit brain damaged in that if the user has not done
2000  * a "use catalog xxx" command, we simply find the first
2001  * catalog resource and open it.
2002  */
2003 int open_db(UAContext *ua)
2004 {
2005    if (ua->db) {
2006       return 1;
2007    }
2008    if (!ua->catalog) {
2009       LockRes();
2010       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
2011       UnlockRes();
2012       if (!ua->catalog) {    
2013          bsendmsg(ua, _("Could not find a Catalog resource\n"));
2014          return 0;
2015       } else {
2016          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
2017             ua->catalog->hdr.name, ua->catalog->db_name);
2018       }
2019    }
2020
2021    ua->jcr->catalog = ua->catalog;
2022
2023    Dmsg0(150, "Open database\n");
2024    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2025                              ua->catalog->db_password, ua->catalog->db_address,
2026                              ua->catalog->db_port, ua->catalog->db_socket,
2027                              ua->catalog->mult_db_connections);
2028    if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2029       bsendmsg(ua, _("Could not open database \"%s\".\n"),
2030                  ua->catalog->db_name);
2031       if (ua->db) {
2032          bsendmsg(ua, "%s", db_strerror(ua->db));
2033       }
2034       close_db(ua);
2035       return 0;
2036    }
2037    ua->jcr->db = ua->db;
2038    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2039    return 1;
2040 }
2041
2042 void close_db(UAContext *ua)
2043 {
2044    if (ua->db) {
2045       db_close_database(ua->jcr, ua->db);
2046       ua->db = NULL;
2047       if (ua->jcr) {
2048          ua->jcr->db = NULL;
2049       }
2050    }
2051 }