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