]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
12aa6f7141c7c5088bd5c75edfea2ae1cb18316a
[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
12 /*
13    Copyright (C) 2002 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "dird.h"
34 #include "ua.h"
35
36 /* Imported variables */
37 extern struct s_jl joblevels[];
38 extern int r_first;
39 extern int r_last;
40 extern struct s_res resources[];
41 extern int console_msg_pending;
42 extern FILE *con_fd;
43 extern char my_name[];
44
45 /* Imported functions */
46 extern int qmessagescmd(UAContext *ua, char *cmd);
47 extern int quitcmd(UAContext *ua, char *cmd);
48
49 /* Forward referenced functions */
50 static int diecmd(UAContext *ua, char *cmd);
51 static int jobscmd(UAContext *ua, char *cmd);
52 static int filesetscmd(UAContext *ua, char *cmd);
53 static int clientscmd(UAContext *ua, char *cmd);
54 static int msgscmd(UAContext *ua, char *cmd);
55
56 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; }; 
57 static struct cmdstruct commands[] = {
58  { N_(".die"),        diecmd,       NULL},
59  { N_(".jobs"),       jobscmd,      NULL},
60  { N_(".filesets"),   filesetscmd,  NULL},
61  { N_(".clients"),    clientscmd,   NULL},
62  { N_(".msgs"),       msgscmd,      NULL},
63  { N_(".messages"),   qmessagescmd, NULL},
64  { N_(".quit"),       quitcmd,      NULL},
65  { N_(".exit"),       quitcmd,      NULL} 
66              };
67 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
68
69 /*
70  * Execute a command from the UA
71  */
72 int do_a_dot_command(UAContext *ua, char *cmd)
73 {
74    unsigned int i;
75    int len, stat;  
76    int found;
77
78    found = 0;
79    stat = 1;
80
81    Dmsg1(200, "Dot command: %s\n", ua->UA_sock->msg);
82    if (ua->argc == 0) {
83       return 1;
84    }
85
86    len = strlen(ua->argk[0]);
87    if (len == 1) {
88       return 1;                       /* no op */
89    }
90    for (i=0; i<comsize; i++) {     /* search for command */
91       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
92          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
93          found = 1;
94          break;
95       }
96    }
97    if (!found) {
98       strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
99       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
100       bnet_send(ua->UA_sock);
101    }
102    return stat;
103 }
104
105 /*
106  * Create segmentation fault 
107  */
108 static int diecmd(UAContext *ua, char *cmd)
109 {
110    JCR *jcr = NULL;
111    int a;
112    
113    a = jcr->JobId; /* ref NULL pointer */
114    return 0;
115 }
116
117 static int jobscmd(UAContext *ua, char *cmd)
118 {
119    JOB *job = NULL;
120    LockRes();
121    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
122       bsendmsg(ua, "%s\n", job->hdr.name);
123    }
124    UnlockRes();
125    return 1;
126 }
127
128 static int filesetscmd(UAContext *ua, char *cmd)
129 {
130    FILESET *fs = NULL;
131    LockRes();
132    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
133       bsendmsg(ua, "%s\n", fs->hdr.name);
134    }
135    UnlockRes();
136    return 1;
137 }
138
139 static int clientscmd(UAContext *ua, char *cmd)
140 {
141    CLIENT *client = NULL;
142    LockRes();
143    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
144       bsendmsg(ua, "%s\n", client->hdr.name);
145    }
146    UnlockRes();
147    return 1;
148 }
149
150 static int msgscmd(UAContext *ua, char *cmd)
151 {
152    MSGS *msgs = NULL;
153    LockRes();
154    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
155       bsendmsg(ua, "%s\n", msgs->hdr.name);
156    }
157    UnlockRes();
158    return 1;
159 }