]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
- First cut of new Python implementation.
[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-2005 Kern Sibbald
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 /* Imported functions */
44 extern int status_cmd(UAContext *ua, const char *cmd);
45 extern int list_cmd(UAContext *ua, const char *cmd);
46 extern int llist_cmd(UAContext *ua, const char *cmd);
47 extern int show_cmd(UAContext *ua, const char *cmd);
48 extern int messagescmd(UAContext *ua, const char *cmd);
49 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
50 extern int gui_cmd(UAContext *ua, const char *cmd);
51 extern int sqlquerycmd(UAContext *ua, const char *cmd);
52 extern int querycmd(UAContext *ua, const char *cmd);
53 extern int retentioncmd(UAContext *ua, const char *cmd);
54 extern int prunecmd(UAContext *ua, const char *cmd);
55 extern int purgecmd(UAContext *ua, const char *cmd);
56 extern int restore_cmd(UAContext *ua, const char *cmd);
57 extern int label_cmd(UAContext *ua, const char *cmd);
58 extern int relabel_cmd(UAContext *ua, const char *cmd);
59 extern int update_cmd(UAContext *ua, const char *cmd);
60
61 /* Forward referenced functions */
62 static int add_cmd(UAContext *ua, const char *cmd);
63 static int create_cmd(UAContext *ua, const char *cmd);
64 static int cancel_cmd(UAContext *ua, const char *cmd);
65 static int setdebug_cmd(UAContext *ua, const char *cmd);
66 static int trace_cmd(UAContext *ua, const char *cmd);
67 static int var_cmd(UAContext *ua, const char *cmd);
68 static int estimate_cmd(UAContext *ua, const char *cmd);
69 static int help_cmd(UAContext *ua, const char *cmd);
70 static int delete_cmd(UAContext *ua, const char *cmd);
71 static int use_cmd(UAContext *ua, const char *cmd);
72 static int unmount_cmd(UAContext *ua, const char *cmd);
73 static int version_cmd(UAContext *ua, const char *cmd);
74 static int automount_cmd(UAContext *ua, const char *cmd);
75 static int time_cmd(UAContext *ua, const char *cmd);
76 static int reload_cmd(UAContext *ua, const char *cmd);
77 static int delete_volume(UAContext *ua);
78 static int delete_pool(UAContext *ua);
79 static void delete_job(UAContext *ua);
80 static int mount_cmd(UAContext *ua, const char *cmd);
81 static int release_cmd(UAContext *ua, const char *cmd);
82 static int wait_cmd(UAContext *ua, const char *cmd);
83 static int setip_cmd(UAContext *ua, const char *cmd);
84 static int python_cmd(UAContext *ua, const char *cmd);
85 static void do_job_delete(UAContext *ua, JobId_t JobId);
86 static void delete_job_id_range(UAContext *ua, char *tok);
87
88 int qhelp_cmd(UAContext *ua, const char *cmd);
89 int quit_cmd(UAContext *ua, const char *cmd);
90
91
92 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; };
93 static struct cmdstruct commands[] = {
94  { N_("add"),        add_cmd,         _("add media to a pool")},
95  { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on|off] -- console messages")},
96  { N_("automount"),   automount_cmd,  _("automount [on|off] -- after label")},
97  { N_("cancel"),     cancel_cmd,    _("cancel <job=nnn> -- cancel a job")},
98  { N_("create"),     create_cmd,    _("create DB Pool from resource")},
99  { N_("delete"),     delete_cmd,    _("delete [pool=<pool-name> | media volume=<volume-name>]")},
100  { N_("estimate"),   estimate_cmd,  _("performs FileSet estimate, listing gives full listing")},
101  { N_("exit"),       quit_cmd,      _("exit = quit")},
102  { N_("gui"),        gui_cmd,       _("gui [on|off] -- non-interactive gui mode")},
103  { N_("help"),       help_cmd,      _("print this command")},
104  { N_("list"),       list_cmd,      _("list [pools | jobs | jobtotals | media <pool=pool-name> | files <jobid=nn>]; from catalog")},
105  { N_("label"),      label_cmd,     _("label a tape")},
106  { N_("llist"),      llist_cmd,     _("full or long list like list command")},
107  { N_("messages"),   messagescmd,   _("messages")},
108  { N_("mount"),      mount_cmd,     _("mount <storage-name>")},
109  { N_("prune"),      prunecmd,      _("prune expired records from catalog")},
110  { N_("purge"),      purgecmd,      _("purge records from catalog")},
111  { N_("python"),     python_cmd,    _("python control commands")},
112  { N_("quit"),       quit_cmd,      _("quit")},
113  { N_("query"),      querycmd,      _("query catalog")},
114  { N_("restore"),    restore_cmd,   _("restore files")},
115  { N_("relabel"),    relabel_cmd,   _("relabel a tape")},
116  { N_("release"),    release_cmd,   _("release <storage-name>")},
117  { N_("reload"),     reload_cmd,    _("reload conf file")},
118  { N_("run"),        run_cmd,       _("run <job-name>")},
119  { N_("status"),     status_cmd,    _("status [storage | client]=<name>")},
120  { N_("setdebug"),   setdebug_cmd,  _("sets debug level")},
121  { N_("setip"),      setip_cmd,     _("sets new client address -- if authorized")},
122  { N_("show"),       show_cmd,      _("show (resource records) [jobs | pools | ... | all]")},
123  { N_("sqlquery"),   sqlquerycmd,   _("use SQL to query catalog")},
124  { N_("time"),       time_cmd,      _("print current time")},
125  { N_("trace"),      trace_cmd,     _("turn on/off trace to file")},
126  { N_("unmount"),    unmount_cmd,   _("unmount <storage-name>")},
127  { N_("umount"),     unmount_cmd,   _("umount <storage-name> for old-time Unix guys")},
128  { N_("update"),     update_cmd,    _("update Volume or Pool")},
129  { N_("use"),        use_cmd,       _("use catalog xxx")},
130  { N_("var"),        var_cmd,       _("does variable expansion")},
131  { N_("version"),    version_cmd,   _("print Director version")},
132  { N_("wait"),       wait_cmd,      _("wait until no jobs are running")},
133              };
134 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
135
136 /*
137  * Execute a command from the UA
138  */
139 int do_a_command(UAContext *ua, const char *cmd)
140 {
141    unsigned int i;
142    int len, stat;
143    bool found = false;
144
145    stat = 1;
146
147    Dmsg1(900, "Command: %s\n", ua->UA_sock->msg);
148    if (ua->argc == 0) {
149       return 1;
150    }
151
152    len = strlen(ua->argk[0]);
153    for (i=0; i<comsize; i++) {     /* search for command */
154       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
155          if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
156             break;
157          }
158          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
159          found = true;
160          break;
161       }
162    }
163    if (!found) {
164       bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
165    }
166    return stat;
167 }
168
169 /*
170  * This is a common routine used to stuff the Pool DB record defaults
171  *   into the Media DB record just before creating a media (Volume)
172  *   record.
173  */
174 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
175 {
176    mr->PoolId = pr->PoolId;
177    bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
178    mr->Recycle = pr->Recycle;
179    mr->VolRetention = pr->VolRetention;
180    mr->VolUseDuration = pr->VolUseDuration;
181    mr->MaxVolJobs = pr->MaxVolJobs;
182    mr->MaxVolFiles = pr->MaxVolFiles;
183    mr->MaxVolBytes = pr->MaxVolBytes;
184    mr->LabelType = pr->LabelType;
185 }
186
187
188 /*
189  *  Add Volumes to an existing Pool
190  */
191 static int add_cmd(UAContext *ua, const char *cmd)
192 {
193    POOL_DBR pr;
194    MEDIA_DBR mr;
195    int num, i, max, startnum;
196    int first_id = 0;
197    char name[MAX_NAME_LENGTH];
198    STORE *store;
199    int Slot = 0, InChanger = 0;
200
201    bsendmsg(ua, _(
202 "You probably don't want to be using this command since it\n"
203 "creates database records without labeling the Volumes.\n"
204 "You probably want to use the \"label\" command.\n\n"));
205
206    if (!open_db(ua)) {
207       return 1;
208    }
209
210    memset(&pr, 0, sizeof(pr));
211    memset(&mr, 0, sizeof(mr));
212
213    if (!get_pool_dbr(ua, &pr)) {
214       return 1;
215    }
216
217    Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
218       pr.MaxVols, pr.PoolType);
219
220    while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
221       bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
222       for (;;) {
223          if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
224             return 1;
225          }
226          pr.MaxVols = ua->pint32_val;
227       }
228    }
229
230    /* Get media type */
231    if ((store = get_storage_resource(ua, 0)) != NULL) {
232       bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
233    } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
234       return 1;
235    }
236
237    if (pr.MaxVols == 0) {
238       max = 1000;
239    } else {
240       max = pr.MaxVols - pr.NumVols;
241    }
242    for (;;) {
243       char buf[100];
244       bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
245       if (!get_pint(ua, buf)) {
246          return 1;
247       }
248       num = ua->pint32_val;
249       if (num < 0 || num > max) {
250          bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
251          continue;
252       }
253       break;
254    }
255 getVolName:
256    if (num == 0) {
257       if (!get_cmd(ua, _("Enter Volume name: "))) {
258          return 1;
259       }
260    } else {
261       if (!get_cmd(ua, _("Enter base volume name: "))) {
262          return 1;
263       }
264    }
265    /* Don't allow | in Volume name because it is the volume separator character */
266    if (!is_volume_name_legal(ua, ua->cmd)) {
267       goto getVolName;
268    }
269    if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
270       bsendmsg(ua, _("Volume name too long.\n"));
271       goto getVolName;
272    }
273    if (strlen(ua->cmd) == 0) {
274       bsendmsg(ua, _("Volume name must be at least one character long.\n"));
275       goto getVolName;
276    }
277
278    bstrncpy(name, ua->cmd, sizeof(name));
279    if (num > 0) {
280       bstrncat(name, "%04d", sizeof(name));
281
282       for (;;) {
283          if (!get_pint(ua, _("Enter the starting number: "))) {
284             return 1;
285          }
286          startnum = ua->pint32_val;
287          if (startnum < 1) {
288             bsendmsg(ua, _("Start number must be greater than zero.\n"));
289             continue;
290          }
291          break;
292       }
293    } else {
294       startnum = 1;
295       num = 1;
296    }
297
298    if (store && store->autochanger) {
299       if (!get_pint(ua, _("Enter slot (0 for none): "))) {
300          return 1;
301       }
302       Slot = ua->pint32_val;
303       if (!get_yesno(ua, _("InChanger? yes/no: "))) {
304          return 1;
305       }
306       InChanger = ua->pint32_val;
307    }
308
309    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
310    for (i=startnum; i < num+startnum; i++) {
311       bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
312       mr.Slot = Slot++;
313       mr.InChanger = InChanger;
314       mr.StorageId = store->StorageId;
315       Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
316       if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
317          bsendmsg(ua, "%s", db_strerror(ua->db));
318          return 1;
319       }
320       if (i == startnum) {
321          first_id = mr.PoolId;
322       }
323    }
324    pr.NumVols += num;
325    Dmsg0(200, "Update pool record.\n");
326    if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
327       bsendmsg(ua, "%s", db_strerror(ua->db));
328       return 1;
329    }
330    bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
331
332    return 1;
333 }
334
335 /*
336  * Turn auto mount on/off
337  *
338  *  automount on
339  *  automount off
340  */
341 int automount_cmd(UAContext *ua, const char *cmd)
342 {
343    char *onoff;
344
345    if (ua->argc != 2) {
346       if (!get_cmd(ua, _("Turn on or off? "))) {
347             return 1;
348       }
349       onoff = ua->cmd;
350    } else {
351       onoff = ua->argk[1];
352    }
353
354    ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
355    return 1;
356 }
357
358
359 /*
360  * Cancel a job
361  */
362 static int cancel_cmd(UAContext *ua, const char *cmd)
363 {
364    int i, ret;
365    int njobs = 0;
366    JCR *jcr = NULL;
367    char JobName[MAX_NAME_LENGTH];
368
369    if (!open_db(ua)) {
370       return 1;
371    }
372
373    for (i=1; i<ua->argc; i++) {
374       if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
375          uint32_t JobId;
376          if (!ua->argv[i]) {
377             break;
378          }
379          JobId = str_to_int64(ua->argv[i]);
380          if (!(jcr=get_jcr_by_id(JobId))) {
381             bsendmsg(ua, _("JobId %d is not running.\n"),  JobId);
382             return 1;
383          }
384          break;
385       } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
386          if (!ua->argv[i]) {
387             break;
388          }
389          if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
390             bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
391             return 1;
392          }
393          break;
394       }
395    }
396    /* If we still do not have a jcr,
397     *   throw up a list and ask the user to select one.
398     */
399    if (!jcr) {
400       char buf[1000];
401       /* Count Jobs running */
402       lock_jcr_chain();
403       foreach_jcr(jcr) {
404          if (jcr->JobId == 0) {      /* this is us */
405             free_locked_jcr(jcr);
406             continue;
407          }
408          free_locked_jcr(jcr);
409          njobs++;
410       }
411       unlock_jcr_chain();
412
413       if (njobs == 0) {
414          bsendmsg(ua, _("No Jobs running.\n"));
415          return 1;
416       }
417       start_prompt(ua, _("Select Job:\n"));
418       lock_jcr_chain();
419       foreach_jcr(jcr) {
420          if (jcr->JobId == 0) {      /* this is us */
421             free_locked_jcr(jcr);
422             continue;
423          }
424          bsnprintf(buf, sizeof(buf), "JobId=%d Job=%s", jcr->JobId, jcr->Job);
425          add_prompt(ua, buf);
426          free_locked_jcr(jcr);
427       }
428       unlock_jcr_chain();
429
430       if (do_prompt(ua, _("Job"),  _("Choose Job to cancel"), buf, sizeof(buf)) < 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       sscanf(buf, "JobId=%d Job=%127s", &njobs, JobName);
440       jcr = get_jcr_by_full_name(JobName);
441       if (!jcr) {
442          bsendmsg(ua, _("Job %s not found.\n"), JobName);
443          return 1;
444       }
445    }
446
447    ret = cancel_job(ua, jcr);
448    free_jcr(jcr);
449
450    return ret;
451 }
452
453 /*
454  * This is a common routine to create or update a
455  *   Pool DB base record from a Pool Resource. We handle
456  *   the setting of MaxVols and NumVols slightly differently
457  *   depending on if we are creating the Pool or we are
458  *   simply bringing it into agreement with the resource (updage).
459  */
460 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
461 {
462    bstrncpy(pr->PoolType, pool->pool_type, sizeof(pr->PoolType));
463    if (op == POOL_OP_CREATE) {
464       pr->MaxVols = pool->max_volumes;
465       pr->NumVols = 0;
466    } else {          /* update pool */
467       if (pr->MaxVols != pool->max_volumes) {
468          pr->MaxVols = pool->max_volumes;
469       }
470       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
471          pr->MaxVols = pr->NumVols;
472       }
473    }
474    pr->LabelType = pool->LabelType;
475    pr->UseOnce = pool->use_volume_once;
476    pr->UseCatalog = pool->use_catalog;
477    pr->AcceptAnyVolume = pool->accept_any_volume;
478    pr->Recycle = pool->Recycle;
479    pr->VolRetention = pool->VolRetention;
480    pr->VolUseDuration = pool->VolUseDuration;
481    pr->MaxVolJobs = pool->MaxVolJobs;
482    pr->MaxVolFiles = pool->MaxVolFiles;
483    pr->MaxVolBytes = pool->MaxVolBytes;
484    pr->AutoPrune = pool->AutoPrune;
485    pr->Recycle = pool->Recycle;
486    if (pool->label_format) {
487       bstrncpy(pr->LabelFormat, pool->label_format, sizeof(pr->LabelFormat));
488    } else {
489       bstrncpy(pr->LabelFormat, "*", sizeof(pr->LabelFormat));    /* none */
490    }
491 }
492
493
494 /*
495  * Create a pool record from a given Pool resource
496  *   Also called from backup.c
497  * Returns: -1  on error
498  *           0  record already exists
499  *           1  record created
500  */
501
502 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
503 {
504    POOL_DBR  pr;
505
506    memset(&pr, 0, sizeof(POOL_DBR));
507
508    bstrncpy(pr.Name, pool->hdr.name, sizeof(pr.Name));
509
510    if (db_get_pool_record(jcr, db, &pr)) {
511       /* Pool Exists */
512       if (op == POOL_OP_UPDATE) {  /* update request */
513          set_pooldbr_from_poolres(&pr, pool, op);
514          db_update_pool_record(jcr, db, &pr);
515       }
516       return 0;                       /* exists */
517    }
518
519    set_pooldbr_from_poolres(&pr, pool, op);
520
521    if (!db_create_pool_record(jcr, db, &pr)) {
522       return -1;                      /* error */
523    }
524    return 1;
525 }
526
527
528
529 /*
530  * Create a Pool Record in the database.
531  *  It is always created from the Resource record.
532  */
533 static int create_cmd(UAContext *ua, const char *cmd)
534 {
535    POOL *pool;
536
537    if (!open_db(ua)) {
538       return 1;
539    }
540
541    pool = get_pool_resource(ua);
542    if (!pool) {
543       return 1;
544    }
545
546    switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
547    case 0:
548       bsendmsg(ua, _("Error: Pool %s already exists.\n"
549                "Use update to change it.\n"), pool->hdr.name);
550       break;
551
552    case -1:
553       bsendmsg(ua, "%s", db_strerror(ua->db));
554       break;
555
556    default:
557      break;
558    }
559    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
560    return 1;
561 }
562
563
564 extern DIRRES *director;
565
566 /*
567  * Python control command
568  *  python restart (restarts interpreter)
569  */
570 static int python_cmd(UAContext *ua, const char *cmd)
571 {
572    if (strcasecmp(ua->argk[1], _("restart")) == 0) {
573       term_python_interpreter();
574       init_python_interpreter(director->hdr.name, director->scripts_directory ?
575          director->scripts_directory : ".", "DirStartUp");
576       bsendmsg(ua, _("Python interpreter restarted.\n"));
577    } else {
578       bsendmsg(ua, _("Nothing done.\n"));
579    }
580    return 1;
581 }
582
583
584 /*
585  * Set a new address in a Client resource. We do this only
586  *  if the Console name is the same as the Client name
587  *  and the Console can access the client.
588  */
589 static int setip_cmd(UAContext *ua, const char *cmd)
590 {
591    CLIENT *client;
592    char buf[1024];
593    if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
594       bsendmsg(ua, _("Illegal command from this console.\n"));
595       return 1;
596    }
597    LockRes();
598    client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
599
600    if (!client) {
601       bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
602       goto get_out;
603    }
604    if (client->address) {
605       free(client->address);
606    }
607    /* MA Bug 6 remove ifdef */
608    sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf));
609    client->address = bstrdup(buf);
610    bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
611             client->hdr.name, client->address);
612 get_out:
613    UnlockRes();
614    return 1;
615 }
616
617
618 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
619 {
620    BSOCK *sd;
621    JCR *jcr = ua->jcr;
622
623    set_storage(jcr, store);
624    /* Try connecting for up to 15 seconds */
625    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"),
626       store->hdr.name, store->address, store->SDport);
627    if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
628       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
629       return;
630    }
631    Dmsg0(120, _("Connected to storage daemon\n"));
632    sd = jcr->store_bsock;
633    bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
634    if (bnet_recv(sd) >= 0) {
635       bsendmsg(ua, "%s", sd->msg);
636    }
637    bnet_sig(sd, BNET_TERMINATE);
638    bnet_close(sd);
639    jcr->store_bsock = NULL;
640    return;
641 }
642
643 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
644 {
645    BSOCK *fd;
646
647    /* Connect to File daemon */
648
649    ua->jcr->client = client;
650    /* Try to connect for 15 seconds */
651    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
652       client->hdr.name, client->address, client->FDport);
653    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
654       bsendmsg(ua, _("Failed to connect to Client.\n"));
655       return;
656    }
657    Dmsg0(120, "Connected to file daemon\n");
658    fd = ua->jcr->file_bsock;
659    bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
660    if (bnet_recv(fd) >= 0) {
661       bsendmsg(ua, "%s", fd->msg);
662    }
663    bnet_sig(fd, BNET_TERMINATE);
664    bnet_close(fd);
665    ua->jcr->file_bsock = NULL;
666    return;
667 }
668
669
670 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
671 {
672    STORE *store, **unique_store;
673    CLIENT *client, **unique_client;
674    int i, j, found;
675
676    /* Director */
677    debug_level = level;
678
679    /* Count Storage items */
680    LockRes();
681    store = NULL;
682    i = 0;
683    foreach_res(store, R_STORAGE) {
684       i++;
685    }
686    unique_store = (STORE **) malloc(i * sizeof(STORE));
687    /* Find Unique Storage address/port */
688    store = (STORE *)GetNextRes(R_STORAGE, NULL);
689    i = 0;
690    unique_store[i++] = store;
691    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
692       found = 0;
693       for (j=0; j<i; j++) {
694          if (strcmp(unique_store[j]->address, store->address) == 0 &&
695              unique_store[j]->SDport == store->SDport) {
696             found = 1;
697             break;
698          }
699       }
700       if (!found) {
701          unique_store[i++] = store;
702          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
703       }
704    }
705    UnlockRes();
706
707    /* Call each unique Storage daemon */
708    for (j=0; j<i; j++) {
709       do_storage_setdebug(ua, unique_store[j], level, trace_flag);
710    }
711    free(unique_store);
712
713    /* Count Client items */
714    LockRes();
715    client = NULL;
716    i = 0;
717    foreach_res(client, R_CLIENT) {
718       i++;
719    }
720    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
721    /* Find Unique Client address/port */
722    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
723    i = 0;
724    unique_client[i++] = client;
725    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
726       found = 0;
727       for (j=0; j<i; j++) {
728          if (strcmp(unique_client[j]->address, client->address) == 0 &&
729              unique_client[j]->FDport == client->FDport) {
730             found = 1;
731             break;
732          }
733       }
734       if (!found) {
735          unique_client[i++] = client;
736          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
737       }
738    }
739    UnlockRes();
740
741    /* Call each unique File daemon */
742    for (j=0; j<i; j++) {
743       do_client_setdebug(ua, unique_client[j], level, trace_flag);
744    }
745    free(unique_client);
746 }
747
748 /*
749  * setdebug level=nn all trace=1/0
750  */
751 static int setdebug_cmd(UAContext *ua, const char *cmd)
752 {
753    STORE *store;
754    CLIENT *client;
755    int level;
756    int trace_flag = -1;
757    int i;
758
759    if (!open_db(ua)) {
760       return 1;
761    }
762    Dmsg1(120, "setdebug:%s:\n", cmd);
763
764    level = -1;
765    i = find_arg_with_value(ua, _("level"));
766    if (i >= 0) {
767       level = atoi(ua->argv[i]);
768    }
769    if (level < 0) {
770       if (!get_pint(ua, _("Enter new debug level: "))) {
771          return 1;
772       }
773       level = ua->pint32_val;
774    }
775
776    /* Look for trace flag. -1 => not change */
777    i = find_arg_with_value(ua, _("trace"));
778    if (i >= 0) {
779       trace_flag = atoi(ua->argv[i]);
780       if (trace_flag > 0) {
781          trace_flag = 1;
782       }
783    }
784
785    /* General debug? */
786    for (i=1; i<ua->argc; i++) {
787       if (strcasecmp(ua->argk[i], _("all")) == 0) {
788          do_all_setdebug(ua, level, trace_flag);
789          return 1;
790       }
791       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
792           strcasecmp(ua->argk[i], _("director")) == 0) {
793          debug_level = level;
794          set_trace(trace_flag);
795          return 1;
796       }
797       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
798           strcasecmp(ua->argk[i], _("fd")) == 0) {
799          client = NULL;
800          if (ua->argv[i]) {
801             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
802             if (client) {
803                do_client_setdebug(ua, client, level, trace_flag);
804                return 1;
805             }
806          }
807          client = select_client_resource(ua);
808          if (client) {
809             do_client_setdebug(ua, client, level, trace_flag);
810             return 1;
811          }
812       }
813
814       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
815           strcasecmp(ua->argk[i], _("storage")) == 0 ||
816           strcasecmp(ua->argk[i], _("sd")) == 0) {
817          store = NULL;
818          if (ua->argv[i]) {
819             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
820             if (store) {
821                do_storage_setdebug(ua, store, level, trace_flag);
822                return 1;
823             }
824          }
825          store = get_storage_resource(ua, 0);
826          if (store) {
827             do_storage_setdebug(ua, store, level, trace_flag);
828             return 1;
829          }
830       }
831    }
832    /*
833     * We didn't find an appropriate keyword above, so
834     * prompt the user.
835     */
836    start_prompt(ua, _("Available daemons are: \n"));
837    add_prompt(ua, _("Director"));
838    add_prompt(ua, _("Storage"));
839    add_prompt(ua, _("Client"));
840    add_prompt(ua, _("All"));
841    switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
842    case 0:                         /* Director */
843       debug_level = level;
844       set_trace(trace_flag);
845       break;
846    case 1:
847       store = get_storage_resource(ua, 0);
848       if (store) {
849          do_storage_setdebug(ua, store, level, trace_flag);
850       }
851       break;
852    case 2:
853       client = select_client_resource(ua);
854       if (client) {
855          do_client_setdebug(ua, client, level, trace_flag);
856       }
857       break;
858    case 3:
859       do_all_setdebug(ua, level, trace_flag);
860       break;
861    default:
862       break;
863    }
864    return 1;
865 }
866
867 /*
868  * Turn debug tracing to file on/off
869  */
870 static int trace_cmd(UAContext *ua, const char *cmd)
871 {
872    char *onoff;
873
874    if (ua->argc != 2) {
875       if (!get_cmd(ua, _("Turn on or off? "))) {
876             return 1;
877       }
878       onoff = ua->cmd;
879    } else {
880       onoff = ua->argk[1];
881    }
882
883    set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
884    return 1;
885
886 }
887
888 static int var_cmd(UAContext *ua, const char *cmd)
889 {
890    POOLMEM *val = get_pool_memory(PM_FNAME);
891    char *var;
892
893    if (!open_db(ua)) {
894       return 1;
895    }
896    for (var=ua->cmd; *var != ' '; ) {    /* skip command */
897       var++;
898    }
899    while (*var == ' ') {                 /* skip spaces */
900       var++;
901    }
902    Dmsg1(100, "Var=%s:\n", var);
903    variable_expansion(ua->jcr, var, &val);
904    bsendmsg(ua, "%s\n", val);
905    free_pool_memory(val);
906    return 1;
907 }
908
909 static int estimate_cmd(UAContext *ua, const char *cmd)
910 {
911    JOB *job = NULL;
912    CLIENT *client = NULL;
913    FILESET *fileset = NULL;
914    FILESET_DBR fsr;
915    int listing = 0;
916    char since[MAXSTRING];
917    JCR *jcr = ua->jcr;
918
919    jcr->JobLevel = L_FULL;
920    for (int i=1; i<ua->argc; i++) {
921       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
922           strcasecmp(ua->argk[i], _("fd")) == 0) {
923          if (ua->argv[i]) {
924             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
925             continue;
926          }
927       }
928       if (strcasecmp(ua->argk[i], _("job")) == 0) {
929          if (ua->argv[i]) {
930             job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
931             continue;
932          }
933       }
934       if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
935          if (ua->argv[i]) {
936             fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
937             continue;
938          }
939       }
940       if (strcasecmp(ua->argk[i], _("listing")) == 0) {
941          listing = 1;
942          continue;
943       }
944       if (strcasecmp(ua->argk[i], _("level")) == 0) {
945          if (!get_level_from_name(ua->jcr, ua->argv[i])) {
946             bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
947          }
948          continue;
949       }
950    }
951    if (!job && !(client && fileset)) {
952       if (!(job = select_job_resource(ua))) {
953          return 1;
954       }
955    }
956    if (!job) {
957       job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
958       if (!job) {
959          bsendmsg(ua, _("No job specified.\n"));
960          return 1;
961       }
962    }
963    if (!client) {
964       client = job->client;
965    }
966    if (!fileset) {
967       fileset = job->fileset;
968    }
969    jcr->client = client;
970    jcr->fileset = fileset;
971    close_db(ua);
972    ua->catalog = client->catalog;
973
974    if (!open_db(ua)) {
975       return 1;
976    }
977
978    jcr->job = job;
979    jcr->JobType = JT_BACKUP;
980    init_jcr_job_record(jcr);
981
982    if (!get_or_create_client_record(jcr)) {
983       return 1;
984    }
985    if (!get_or_create_fileset_record(jcr, &fsr)) {
986       return 1;
987    }
988
989    get_level_since_time(ua->jcr, since, sizeof(since));
990
991    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
992       job->client->hdr.name, job->client->address, job->client->FDport);
993    if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
994       bsendmsg(ua, _("Failed to connect to Client.\n"));
995       return 1;
996    }
997
998    if (!send_include_list(jcr)) {
999       bsendmsg(ua, _("Error sending include list.\n"));
1000       goto bail_out;
1001    }
1002
1003    if (!send_exclude_list(jcr)) {
1004       bsendmsg(ua, _("Error sending exclude list.\n"));
1005       goto bail_out;
1006    }
1007
1008    if (!send_level_command(jcr)) {
1009       goto bail_out;
1010    }
1011
1012    bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1013    while (bnet_recv(jcr->file_bsock) >= 0) {
1014       bsendmsg(ua, "%s", jcr->file_bsock->msg);
1015    }
1016
1017 bail_out:
1018    if (jcr->file_bsock) {
1019       bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1020       bnet_close(jcr->file_bsock);
1021       jcr->file_bsock = NULL;
1022    }
1023    return 1;
1024 }
1025
1026
1027 /*
1028  * print time
1029  */
1030 static int time_cmd(UAContext *ua, const char *cmd)
1031 {
1032    char sdt[50];
1033    time_t ttime = time(NULL);
1034    struct tm tm;
1035    localtime_r(&ttime, &tm);
1036    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1037    bsendmsg(ua, "%s\n", sdt);
1038    return 1;
1039 }
1040
1041 /*
1042  * reload the conf file
1043  */
1044 extern "C" void reload_config(int sig);
1045
1046 static int reload_cmd(UAContext *ua, const char *cmd)
1047 {
1048    reload_config(1);
1049    return 1;
1050 }
1051
1052 /*
1053  * Delete Pool records (should purge Media with it).
1054  *
1055  *  delete pool=<pool-name>
1056  *  delete volume pool=<pool-name> volume=<name>
1057  *  delete job jobid=xxx
1058  */
1059 static int delete_cmd(UAContext *ua, const char *cmd)
1060 {
1061    static const char *keywords[] = {
1062       N_("volume"),
1063       N_("pool"),
1064       N_("job"),
1065       N_("jobid"),
1066       NULL};
1067
1068    if (!open_db(ua)) {
1069       return 1;
1070    }
1071
1072    switch (find_arg_keyword(ua, keywords)) {
1073    case 0:
1074       delete_volume(ua);
1075       return 1;
1076    case 1:
1077       delete_pool(ua);
1078       return 1;
1079    case 2:
1080    case 3:
1081       int i;
1082       while ((i=find_arg(ua, _("jobid"))) > 0) {
1083          delete_job(ua);
1084          *ua->argk[i] = 0;         /* zap keyword already visited */
1085       }
1086       return 1;
1087    default:
1088       break;
1089    }
1090
1091    bsendmsg(ua, _(
1092 "In general it is not a good idea to delete either a\n"
1093 "Pool or a Volume since they may contain data.\n\n"));
1094
1095    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1096    case 0:
1097       delete_volume(ua);
1098       break;
1099    case 1:
1100       delete_pool(ua);
1101       break;
1102    case 2:
1103       delete_job(ua);
1104       return 1;
1105    default:
1106       bsendmsg(ua, _("Nothing done.\n"));
1107       break;
1108    }
1109    return 1;
1110 }
1111
1112
1113 /*
1114  * delete_job has been modified to parse JobID lists like the
1115  * following:
1116  * delete job JobID=3,4,6,7-11,14
1117  *
1118  * Thanks to Phil Stracchino for the above addition.
1119  */
1120
1121 static void delete_job(UAContext *ua)
1122 {
1123    JobId_t JobId;
1124    char *s,*sep,*tok;
1125
1126    int i = find_arg_with_value(ua, _("jobid"));
1127    if (i >= 0) {
1128       if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1129         s = bstrdup(ua->argv[i]);
1130         tok = s;
1131         /*
1132          * We could use strtok() here.  But we're not going to, because:
1133          * (a) strtok() is deprecated, having been replaced by strsep();
1134          * (b) strtok() is broken in significant ways.
1135          * we could use strsep() instead, but it's not universally available.
1136          * so we grow our own using strchr().
1137          */
1138         sep = strchr(tok, ',');
1139         while (sep != NULL) {
1140            *sep = '\0';
1141            if (strchr(tok, '-')) {
1142                delete_job_id_range(ua, tok);
1143            } else {
1144               JobId = str_to_int64(tok);
1145               do_job_delete(ua, JobId);
1146            }
1147            tok = ++sep;
1148            sep = strchr(tok, ',');
1149         }
1150         /* pick up the last token */
1151         if (strchr(tok, '-')) {
1152             delete_job_id_range(ua, tok);
1153         } else {
1154             JobId = str_to_int64(tok);
1155             do_job_delete(ua, JobId);
1156         }
1157
1158          free(s);
1159       } else {
1160          JobId = str_to_int64(ua->argv[i]);
1161         do_job_delete(ua, JobId);
1162       }
1163    } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1164       return;
1165    } else {
1166       JobId = ua->int64_val;
1167       do_job_delete(ua, JobId);
1168    }
1169 }
1170
1171 /*
1172  * we call delete_job_id_range to parse range tokens and iterate over ranges
1173  */
1174 static void delete_job_id_range(UAContext *ua, char *tok)
1175 {
1176    char *tok2;
1177    JobId_t j,j1,j2;
1178
1179    tok2 = strchr(tok, '-');
1180    *tok2 = '\0';
1181    tok2++;
1182    j1 = str_to_int64(tok);
1183    j2 = str_to_int64(tok2);
1184    for (j=j1; j<=j2; j++) {
1185       do_job_delete(ua, j);
1186    }
1187 }
1188
1189 /*
1190  * do_job_delete now performs the actual delete operation atomically
1191  * we always return 1 because C++ is pissy about void functions
1192  */
1193
1194 static void do_job_delete(UAContext *ua, JobId_t JobId)
1195 {
1196    POOLMEM *query = get_pool_memory(PM_MESSAGE);
1197    char ed1[50];
1198
1199    Mmsg(query, "DELETE FROM Job WHERE JobId=%s", edit_int64(JobId, ed1));
1200    db_sql_query(ua->db, query, NULL, (void *)NULL);
1201    Mmsg(query, "DELETE FROM File WHERE JobId=%s", edit_int64(JobId, ed1));
1202    db_sql_query(ua->db, query, NULL, (void *)NULL);
1203    Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%s", edit_int64(JobId, ed1));
1204    db_sql_query(ua->db, query, NULL, (void *)NULL);
1205    free_pool_memory(query);
1206    bsendmsg(ua, _("Job %s and associated records deleted from the catalog.\n"), edit_int64(JobId, ed1));
1207 }
1208
1209 /*
1210  * Delete media records from database -- dangerous
1211  */
1212 static int delete_volume(UAContext *ua)
1213 {
1214    MEDIA_DBR mr;
1215
1216    if (!select_media_dbr(ua, &mr)) {
1217       return 1;
1218    }
1219    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1220       "and all Jobs saved on that volume from the Catalog\n"),
1221       mr.VolumeName);
1222
1223    if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1224       return 1;
1225    }
1226    if (ua->pint32_val) {
1227       db_delete_media_record(ua->jcr, ua->db, &mr);
1228    }
1229    return 1;
1230 }
1231
1232 /*
1233  * Delete a pool record from the database -- dangerous
1234  */
1235 static int delete_pool(UAContext *ua)
1236 {
1237    POOL_DBR  pr;
1238
1239    memset(&pr, 0, sizeof(pr));
1240
1241    if (!get_pool_dbr(ua, &pr)) {
1242       return 1;
1243    }
1244    if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1245       return 1;
1246    }
1247    if (ua->pint32_val) {
1248       db_delete_pool_record(ua->jcr, ua->db, &pr);
1249    }
1250    return 1;
1251 }
1252
1253
1254 static void do_mount_cmd(UAContext *ua, const char *command)
1255 {
1256    STORE *store;
1257    BSOCK *sd;
1258    JCR *jcr = ua->jcr;
1259    char dev_name[MAX_NAME_LENGTH];
1260
1261    if (!open_db(ua)) {
1262       return;
1263    }
1264    Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1265
1266    store = get_storage_resource(ua, 1);
1267    if (!store) {
1268       return;
1269    }
1270
1271    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1272       store->media_type, store->dev_name());
1273
1274    set_storage(jcr, store);
1275    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1276       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1277       return;
1278    }
1279    sd = jcr->store_bsock;
1280    bstrncpy(dev_name, store->dev_name(), sizeof(dev_name));
1281    bash_spaces(dev_name);
1282    bnet_fsend(sd, "%s %s", command, dev_name);
1283    while (bnet_recv(sd) >= 0) {
1284       bsendmsg(ua, "%s", sd->msg);
1285    }
1286    bnet_sig(sd, BNET_TERMINATE);
1287    bnet_close(sd);
1288    jcr->store_bsock = NULL;
1289 }
1290
1291 /*
1292  * mount [storage | device] <name>
1293  */
1294 static int mount_cmd(UAContext *ua, const char *cmd)
1295 {
1296    do_mount_cmd(ua, "mount");          /* mount */
1297    return 1;
1298 }
1299
1300
1301 /*
1302  * unmount [storage | device] <name>
1303  */
1304 static int unmount_cmd(UAContext *ua, const char *cmd)
1305 {
1306    do_mount_cmd(ua, "unmount");          /* unmount */
1307    return 1;
1308 }
1309
1310
1311 /*
1312  * release [storage | device] <name>
1313  */
1314 static int release_cmd(UAContext *ua, const char *cmd)
1315 {
1316    do_mount_cmd(ua, "release");          /* release */
1317    return 1;
1318 }
1319
1320
1321 /*
1322  * Switch databases
1323  *   use catalog=<name>
1324  */
1325 static int use_cmd(UAContext *ua, const char *cmd)
1326 {
1327    CAT *oldcatalog, *catalog;
1328
1329
1330    close_db(ua);                      /* close any previously open db */
1331    oldcatalog = ua->catalog;
1332
1333    if (!(catalog = get_catalog_resource(ua))) {
1334       ua->catalog = oldcatalog;
1335    } else {
1336       ua->catalog = catalog;
1337    }
1338    if (open_db(ua)) {
1339       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1340          ua->catalog->hdr.name, ua->catalog->db_name);
1341    }
1342    return 1;
1343 }
1344
1345 int quit_cmd(UAContext *ua, const char *cmd)
1346 {
1347    ua->quit = TRUE;
1348    return 1;
1349 }
1350
1351 /*
1352  * Wait until no job is running
1353  */
1354 int wait_cmd(UAContext *ua, const char *cmd)
1355 {
1356    JCR *jcr;
1357    bmicrosleep(0, 200000);            /* let job actually start */
1358    for (bool running=true; running; ) {
1359       running = false;
1360       lock_jcr_chain();
1361       foreach_jcr(jcr) {
1362          if (jcr->JobId != 0) {
1363             running = true;
1364             free_locked_jcr(jcr);
1365             break;
1366          }
1367          free_locked_jcr(jcr);
1368       }
1369       unlock_jcr_chain();
1370       if (running) {
1371          bmicrosleep(1, 0);
1372       }
1373    }
1374    return 1;
1375 }
1376
1377
1378 static int help_cmd(UAContext *ua, const char *cmd)
1379 {
1380    unsigned int i;
1381
1382    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1383    for (i=0; i<comsize; i++) {
1384       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1385    }
1386    bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1387    return 1;
1388 }
1389
1390 int qhelp_cmd(UAContext *ua, const char *cmd)
1391 {
1392    unsigned int i;
1393
1394    for (i=0; i<comsize; i++) {
1395       bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1396    }
1397    return 1;
1398 }
1399
1400 static int version_cmd(UAContext *ua, const char *cmd)
1401 {
1402    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1403    return 1;
1404 }
1405
1406
1407 /* A bit brain damaged in that if the user has not done
1408  * a "use catalog xxx" command, we simply find the first
1409  * catalog resource and open it.
1410  */
1411 int open_db(UAContext *ua)
1412 {
1413    if (ua->db) {
1414       return 1;
1415    }
1416    if (!ua->catalog) {
1417       LockRes();
1418       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1419       UnlockRes();
1420       if (!ua->catalog) {
1421          bsendmsg(ua, _("Could not find a Catalog resource\n"));
1422          return 0;
1423       } else {
1424          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"),
1425             ua->catalog->hdr.name, ua->catalog->db_name);
1426       }
1427    }
1428
1429    ua->jcr->catalog = ua->catalog;
1430
1431    Dmsg0(150, "Open database\n");
1432    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1433                              ua->catalog->db_password, ua->catalog->db_address,
1434                              ua->catalog->db_port, ua->catalog->db_socket,
1435                              ua->catalog->mult_db_connections);
1436    if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
1437       bsendmsg(ua, _("Could not open database \"%s\".\n"),
1438                  ua->catalog->db_name);
1439       if (ua->db) {
1440          bsendmsg(ua, "%s", db_strerror(ua->db));
1441       }
1442       close_db(ua);
1443       return 0;
1444    }
1445    ua->jcr->db = ua->db;
1446    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1447    return 1;
1448 }
1449
1450 void close_db(UAContext *ua)
1451 {
1452    if (ua->db) {
1453       db_close_database(ua->jcr, ua->db);
1454       ua->db = NULL;
1455       if (ua->jcr) {
1456          ua->jcr->db = NULL;
1457       }
1458    }
1459 }