]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
Update/enhance copyright/license + add .backups for Nicolas
[bacula/bacula] / bacula / src / dird / ua_dotcmds.c
1 /*
2  *
3  *   Bacula Director -- User Agent Commands
4  *     These are "dot" commands, i.e. commands preceded
5  *        by a period. These commands are meant to be used
6  *        by a program, so there is no prompting, and the
7  *        returned results are (supposed to be) predictable.
8  *
9  *     Kern Sibbald, April MMII
10  *
11  *   Version $Id$
12  */
13
14 /*
15    Copyright (C) 2002-2004 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35 #include "dird.h"
36
37 /* Imported variables */
38 extern int r_first;
39 extern int r_last;
40 extern struct s_res resources[];
41 extern char my_name[];
42 extern const char *client_backups;
43
44 /* Imported functions */
45 extern int qmessagescmd(UAContext *ua, const char *cmd);
46 extern int quit_cmd(UAContext *ua, const char *cmd);
47
48 /* Forward referenced functions */
49 static int diecmd(UAContext *ua, const char *cmd);
50 static int jobscmd(UAContext *ua, const char *cmd);
51 static int filesetscmd(UAContext *ua, const char *cmd);
52 static int clientscmd(UAContext *ua, const char *cmd);
53 static int msgscmd(UAContext *ua, const char *cmd);
54 static int poolscmd(UAContext *ua, const char *cmd);
55 static int storagecmd(UAContext *ua, const char *cmd);
56 static int defaultscmd(UAContext *ua, const char *cmd);
57 static int typescmd(UAContext *ua, const char *cmd);
58 static int backupscmd(UAContext *ua, const char *cmd);
59 static int levelscmd(UAContext *ua, const char *cmd);
60
61 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; }; 
62 static struct cmdstruct commands[] = {
63  { N_(".die"),        diecmd,       NULL},
64  { N_(".jobs"),       jobscmd,      NULL},
65  { N_(".filesets"),   filesetscmd,  NULL},
66  { N_(".clients"),    clientscmd,   NULL},
67  { N_(".msgs"),       msgscmd,      NULL},
68  { N_(".pools"),      poolscmd,     NULL},
69  { N_(".types"),      typescmd,     NULL},
70  { N_(".backups"),    backupscmd,   NULL},
71  { N_(".levels"),     levelscmd,    NULL},
72  { N_(".storage"),    storagecmd,   NULL},
73  { N_(".defaults"),   defaultscmd,  NULL},
74  { N_(".messages"),   qmessagescmd, NULL},
75  { N_(".quit"),       quit_cmd,     NULL},
76  { N_(".exit"),       quit_cmd,     NULL} 
77              };
78 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
79
80 /*
81  * Execute a command from the UA
82  */
83 int do_a_dot_command(UAContext *ua, const char *cmd)
84 {
85    int i;
86    int len, stat;  
87    bool found = false;
88
89    stat = 1;
90
91    Dmsg1(400, "Dot command: %s\n", ua->UA_sock->msg);
92    if (ua->argc == 0) {
93       return 1;
94    }
95
96    len = strlen(ua->argk[0]);
97    if (len == 1) {
98       return 1;                       /* no op */
99    }
100    for (i=0; i<(int)comsize; i++) {     /* search for command */
101       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
102          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
103          found = true;
104          break;
105       }
106    }
107    if (!found) {
108       pm_strcat(&ua->UA_sock->msg, _(": is an illegal command\n"));
109       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
110       bnet_send(ua->UA_sock);
111    }
112    return stat;
113 }
114
115 /*
116  * Create segmentation fault 
117  */
118 static int diecmd(UAContext *ua, const char *cmd)
119 {
120    JCR *jcr = NULL;
121    int a;
122    
123    bsendmsg(ua, "The Director will segment fault.\n");
124    a = jcr->JobId; /* ref NULL pointer */
125    jcr->JobId = 1000; /* another ref NULL pointer */
126    return 0;
127 }
128
129 static int jobscmd(UAContext *ua, const char *cmd)
130 {
131    JOB *job = NULL;
132    LockRes();
133    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
134       bsendmsg(ua, "%s\n", job->hdr.name);
135    }
136    UnlockRes();
137    return 1;
138 }
139
140 static int filesetscmd(UAContext *ua, const char *cmd)
141 {
142    FILESET *fs = NULL;
143    LockRes();
144    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
145       bsendmsg(ua, "%s\n", fs->hdr.name);
146    }
147    UnlockRes();
148    return 1;
149 }
150
151 static int clientscmd(UAContext *ua, const char *cmd)
152 {
153    CLIENT *client = NULL;
154    LockRes();
155    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
156       bsendmsg(ua, "%s\n", client->hdr.name);
157    }
158    UnlockRes();
159    return 1;
160 }
161
162 static int msgscmd(UAContext *ua, const char *cmd)
163 {
164    MSGS *msgs = NULL;
165    LockRes();
166    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
167       bsendmsg(ua, "%s\n", msgs->hdr.name);
168    }
169    UnlockRes();
170    return 1;
171 }
172
173 static int poolscmd(UAContext *ua, const char *cmd)
174 {
175    POOL *pool = NULL;
176    LockRes();
177    while ( (pool = (POOL *)GetNextRes(R_POOL, (RES *)pool)) ) {
178       bsendmsg(ua, "%s\n", pool->hdr.name);
179    }
180    UnlockRes();
181    return 1;
182 }
183
184 static int storagecmd(UAContext *ua, const char *cmd)
185 {
186    STORE *store = NULL;
187    LockRes();
188    while ( (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)) ) {
189       bsendmsg(ua, "%s\n", store->hdr.name);
190    }
191    UnlockRes();
192    return 1;
193 }
194
195
196 static int typescmd(UAContext *ua, const char *cmd)
197 {
198    bsendmsg(ua, "Backup\n");
199    bsendmsg(ua, "Restore\n");
200    bsendmsg(ua, "Admin\n");
201    bsendmsg(ua, "Verify\n");
202    return 1;
203 }
204
205 static int client_backups_handler(void *ctx, int num_field, char **row)
206 {
207    UAContext *ua = (UAContext *)ctx;
208    bsendmsg(ua, "| %s | %s | %s | %s | %s | %s | %s |\n",
209       row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
210    return 0;
211 }
212
213 static int backupscmd(UAContext *ua, const char *cmd)
214 {
215    if (!open_db(ua)) {
216       return 1;
217    }
218    if (ua->argc == 2 && strcmp(ua->argk[1], "client") != 0) {
219       return 1;
220    }
221    Mmsg(&ua->cmd, client_backups, ua->argv[1]);
222    if (!db_sql_query(ua->db, ua->cmd, client_backups_handler, (void *)ua)) {
223       bsendmsg(ua, _("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db));
224       return 1;
225    }
226    return 1;
227 }
228
229
230 static int levelscmd(UAContext *ua, const char *cmd)
231 {
232    bsendmsg(ua, "Incremental\n");
233    bsendmsg(ua, "Full\n");
234    bsendmsg(ua, "Differential\n");
235    bsendmsg(ua, "Catalog\n");
236    bsendmsg(ua, "InitCatalog\n");
237    bsendmsg(ua, "VolumeToCatalog\n");
238    return 1;
239 }
240
241
242
243 /*
244  * Return default values for a job
245  */
246 static int defaultscmd(UAContext *ua, const char *cmd)
247 {
248    JOB *job;       
249    if (ua->argc == 2 && strcmp(ua->argk[1], "job") == 0) {
250       job = (JOB *)GetResWithName(R_JOB, ua->argv[1]);
251       if (job) {
252          bsendmsg(ua, "job=%s", job->hdr.name);
253          bsendmsg(ua, "pool=%s", job->pool->hdr.name);
254          bsendmsg(ua, "messages=%s", job->messages->hdr.name);
255          bsendmsg(ua, "client=%s", job->client->hdr.name);
256          bsendmsg(ua, "storage=%s", job->storage->hdr.name);
257          bsendmsg(ua, "where=%s", job->RestoreWhere?job->RestoreWhere:"");
258          bsendmsg(ua, "level=%s", level_to_str(job->level));
259          bsendmsg(ua, "type=%s", job_type_to_str(job->JobType));
260          bsendmsg(ua, "fileset=%s", job->fileset->hdr.name);
261       }
262    }
263    return 1;
264 }