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