]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_dotcmds.c
1.19 24Apr02
[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 int r_first;
38 extern int r_last;
39 extern struct s_res resources[];
40 extern int console_msg_pending;
41 extern FILE *con_fd;
42 extern char my_name[];
43
44 /* Imported functions */
45 extern int qmessagescmd(UAContext *ua, char *cmd);
46 extern int quitcmd(UAContext *ua, char *cmd);
47
48 /* Forward referenced functions */
49 static int diecmd(UAContext *ua, char *cmd);
50 static int jobscmd(UAContext *ua, char *cmd);
51 static int filesetscmd(UAContext *ua, char *cmd);
52 static int clientscmd(UAContext *ua, char *cmd);
53 static int msgscmd(UAContext *ua, char *cmd);
54
55 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; }; 
56 static struct cmdstruct commands[] = {
57  { N_(".die"),        diecmd,       NULL},
58  { N_(".jobs"),       jobscmd,      NULL},
59  { N_(".filesets"),   filesetscmd,  NULL},
60  { N_(".clients"),    clientscmd,   NULL},
61  { N_(".msgs"),       msgscmd,      NULL},
62  { N_(".messages"),   qmessagescmd, NULL},
63  { N_(".quit"),       quitcmd,      NULL},
64  { N_(".exit"),       quitcmd,      NULL} 
65              };
66 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
67
68 /*
69  * Execute a command from the UA
70  */
71 int do_a_dot_command(UAContext *ua, char *cmd)
72 {
73    unsigned int i;
74    int len, stat;  
75    int found;
76
77    found = 0;
78    stat = 1;
79
80    Dmsg1(200, "Dot command: %s\n", ua->UA_sock->msg);
81    if (ua->argc == 0) {
82       return 1;
83    }
84
85    len = strlen(ua->argk[0]);
86    if (len == 1) {
87       return 1;                       /* no op */
88    }
89    for (i=0; i<comsize; i++) {     /* search for command */
90       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
91          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
92          found = 1;
93          break;
94       }
95    }
96    if (!found) {
97       strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
98       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
99       bnet_send(ua->UA_sock);
100    }
101    return stat;
102 }
103
104 /*
105  * Create segmentation fault 
106  */
107 static int diecmd(UAContext *ua, char *cmd)
108 {
109    JCR *jcr = NULL;
110    int a;
111    
112    a = jcr->JobId; /* ref NULL pointer */
113    return 0;
114 }
115
116 static int jobscmd(UAContext *ua, char *cmd)
117 {
118    JOB *job = NULL;
119    LockRes();
120    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
121       bsendmsg(ua, "%s\n", job->hdr.name);
122    }
123    UnlockRes();
124    return 1;
125 }
126
127 static int filesetscmd(UAContext *ua, char *cmd)
128 {
129    FILESET *fs = NULL;
130    LockRes();
131    while ( (fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs)) ) {
132       bsendmsg(ua, "%s\n", fs->hdr.name);
133    }
134    UnlockRes();
135    return 1;
136 }
137
138 static int clientscmd(UAContext *ua, char *cmd)
139 {
140    CLIENT *client = NULL;
141    LockRes();
142    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
143       bsendmsg(ua, "%s\n", client->hdr.name);
144    }
145    UnlockRes();
146    return 1;
147 }
148
149 static int msgscmd(UAContext *ua, char *cmd)
150 {
151    MSGS *msgs = NULL;
152    LockRes();
153    while ( (msgs = (MSGS *)GetNextRes(R_MSGS, (RES *)msgs)) ) {
154       bsendmsg(ua, "%s\n", msgs->hdr.name);
155    }
156    UnlockRes();
157    return 1;
158 }