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