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