]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
Massive SD calling sequence reorganization
[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 extern int qhelp_cmd(UAContext *ua, const char *cmd);
48 extern int qstatus_cmd(UAContext *ua, const char *cmd);
49
50 /* Forward referenced functions */
51 static int diecmd(UAContext *ua, const char *cmd);
52 static int jobscmd(UAContext *ua, const char *cmd);
53 static int filesetscmd(UAContext *ua, const char *cmd);
54 static int clientscmd(UAContext *ua, const char *cmd);
55 static int msgscmd(UAContext *ua, const char *cmd);
56 static int poolscmd(UAContext *ua, const char *cmd);
57 static int storagecmd(UAContext *ua, const char *cmd);
58 static int defaultscmd(UAContext *ua, const char *cmd);
59 static int typescmd(UAContext *ua, const char *cmd);
60 static int backupscmd(UAContext *ua, const char *cmd);
61 static int levelscmd(UAContext *ua, const char *cmd);
62
63 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; }; 
64 static struct cmdstruct commands[] = {
65  { N_(".die"),        diecmd,       NULL},
66  { N_(".jobs"),       jobscmd,      NULL},
67  { N_(".filesets"),   filesetscmd,  NULL},
68  { N_(".clients"),    clientscmd,   NULL},
69  { N_(".msgs"),       msgscmd,      NULL},
70  { N_(".pools"),      poolscmd,     NULL},
71  { N_(".types"),      typescmd,     NULL},
72  { N_(".backups"),    backupscmd,   NULL},
73  { N_(".levels"),     levelscmd,    NULL},
74  { N_(".status"),     qstatus_cmd,  NULL},
75  { N_(".storage"),    storagecmd,   NULL},
76  { N_(".defaults"),   defaultscmd,  NULL},
77  { N_(".messages"),   qmessagescmd, NULL},
78  { N_(".help"),       qhelp_cmd,    NULL},
79  { N_(".quit"),       quit_cmd,     NULL},
80  { N_(".exit"),       quit_cmd,     NULL} 
81              };
82 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
83
84 /*
85  * Execute a command from the UA
86  */
87 int do_a_dot_command(UAContext *ua, const char *cmd)
88 {
89    int i;
90    int len, stat;  
91    bool found = false;
92
93    stat = 1;
94
95    Dmsg1(400, "Dot command: %s\n", ua->UA_sock->msg);
96    if (ua->argc == 0) {
97       return 1;
98    }
99
100    len = strlen(ua->argk[0]);
101    if (len == 1) {
102       return 1;                       /* no op */
103    }
104    for (i=0; i<(int)comsize; i++) {     /* search for command */
105       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
106          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
107          found = true;
108          break;
109       }
110    }
111    if (!found) {
112       pm_strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
113       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
114       bnet_send(ua->UA_sock);
115    }
116    return stat;
117 }
118
119 /*
120  * Create segmentation fault 
121  */
122 static int diecmd(UAContext *ua, const char *cmd)
123 {
124    JCR *jcr = NULL;
125    int a;
126    
127    bsendmsg(ua, "The Director will segment fault.\n");
128    a = jcr->JobId; /* ref NULL pointer */
129    jcr->JobId = 1000; /* another ref NULL pointer */
130    return 0;
131 }
132
133 static int jobscmd(UAContext *ua, const char *cmd)
134 {
135    JOB *job = NULL;
136    LockRes();
137    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
138       bsendmsg(ua, "%s\n", job->hdr.name);
139    }
140    UnlockRes();
141    return 1;
142 }
143
144 static int filesetscmd(UAContext *ua, const char *cmd)
145 {
146    FILESET *fs = NULL;
147    LockRes();
148    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
149       bsendmsg(ua, "%s\n", fs->hdr.name);
150    }
151    UnlockRes();
152    return 1;
153 }
154
155 static int clientscmd(UAContext *ua, const char *cmd)
156 {
157    CLIENT *client = NULL;
158    LockRes();
159    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
160       bsendmsg(ua, "%s\n", client->hdr.name);
161    }
162    UnlockRes();
163    return 1;
164 }
165
166 static int msgscmd(UAContext *ua, const char *cmd)
167 {
168    MSGS *msgs = NULL;
169    LockRes();
170    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
171       bsendmsg(ua, "%s\n", msgs->hdr.name);
172    }
173    UnlockRes();
174    return 1;
175 }
176
177 static int poolscmd(UAContext *ua, const char *cmd)
178 {
179    POOL *pool = NULL;
180    LockRes();
181    while ( (pool = (POOL *)GetNextRes(R_POOL, (RES *)pool)) ) {
182       bsendmsg(ua, "%s\n", pool->hdr.name);
183    }
184    UnlockRes();
185    return 1;
186 }
187
188 static int storagecmd(UAContext *ua, const char *cmd)
189 {
190    STORE *store = NULL;
191    LockRes();
192    while ( (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)) ) {
193       bsendmsg(ua, "%s\n", store->hdr.name);
194    }
195    UnlockRes();
196    return 1;
197 }
198
199
200 static int typescmd(UAContext *ua, const char *cmd)
201 {
202    bsendmsg(ua, "Backup\n");
203    bsendmsg(ua, "Restore\n");
204    bsendmsg(ua, "Admin\n");
205    bsendmsg(ua, "Verify\n");
206    return 1;
207 }
208
209 static int client_backups_handler(void *ctx, int num_field, char **row)
210 {
211    UAContext *ua = (UAContext *)ctx;
212    bsendmsg(ua, "| %s | %s | %s | %s | %s | %s | %s |\n",
213       row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
214    return 0;
215 }
216
217 static int backupscmd(UAContext *ua, const char *cmd)
218 {
219    if (!open_db(ua)) {
220       return 1;
221    }
222    if (ua->argc == 2 && strcmp(ua->argk[1], "client") != 0) {
223       return 1;
224    }
225    Mmsg(ua->cmd, client_backups, ua->argv[1]);
226    if (!db_sql_query(ua->db, ua->cmd, client_backups_handler, (void *)ua)) {
227       bsendmsg(ua, _("Query failed: %s. ERR=%s\n"), ua->cmd, db_strerror(ua->db));
228       return 1;
229    }
230    return 1;
231 }
232
233
234 static int levelscmd(UAContext *ua, const char *cmd)
235 {
236    bsendmsg(ua, "Incremental\n");
237    bsendmsg(ua, "Full\n");
238    bsendmsg(ua, "Differential\n");
239    bsendmsg(ua, "Catalog\n");
240    bsendmsg(ua, "InitCatalog\n");
241    bsendmsg(ua, "VolumeToCatalog\n");
242    return 1;
243 }
244
245
246
247 /*
248  * Return default values for a job
249  */
250 static int defaultscmd(UAContext *ua, const char *cmd)
251 {
252    JOB *job;       
253    if (ua->argc == 2 && strcmp(ua->argk[1], "job") == 0) {
254       job = (JOB *)GetResWithName(R_JOB, ua->argv[1]);
255       if (job) {
256          STORE *store;
257          bsendmsg(ua, "job=%s", job->hdr.name);
258          bsendmsg(ua, "pool=%s", job->pool->hdr.name);
259          bsendmsg(ua, "messages=%s", job->messages->hdr.name);
260          bsendmsg(ua, "client=%s", job->client->hdr.name);
261          store = (STORE *)job->storage[0]->first();
262          bsendmsg(ua, "storage=%s", store->hdr.name);
263          bsendmsg(ua, "where=%s", job->RestoreWhere?job->RestoreWhere:"");
264          bsendmsg(ua, "level=%s", level_to_str(job->JobLevel));
265          bsendmsg(ua, "type=%s", job_type_to_str(job->JobType));
266          bsendmsg(ua, "fileset=%s", job->fileset->hdr.name);
267       }
268    }
269    return 1;
270 }