]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
Fix bat seg fault
[bacula/bacula] / bacula / src / dird / ua_input.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *   Bacula Director -- User Agent Input and scanning code
31  *
32  *     Kern Sibbald, October MMI
33  *
34  *   Version $Id$
35  */
36
37 #include "bacula.h"
38 #include "dird.h"
39
40
41 /* Imported variables */
42
43
44 /* Exported functions */
45
46 int get_cmd(UAContext *ua, const char *prompt)
47 {
48    BSOCK *sock = ua->UA_sock;
49    int stat;
50
51    ua->cmd[0] = 0;
52    if (!sock || ua->batch) {          /* No UA or batch mode */
53       return 0;
54    }
55    sock->fsend("%s", prompt);
56    sock->signal(BNET_PROMPT);         /* request more input */
57    for ( ;; ) {
58       stat = sock->recv();
59       if (stat == BNET_SIGNAL) {
60          continue;                    /* ignore signals */
61       }
62       if (is_bnet_stop(sock)) {
63          return 0;                    /* error or terminate */
64       }
65       pm_strcpy(ua->cmd, sock->msg);
66       strip_trailing_junk(ua->cmd);
67       if (strcmp(ua->cmd, ".messages") == 0) {
68          qmessagescmd(ua, ua->cmd);
69       }
70       /* Lone dot => break */
71       if (ua->cmd[0] == '.' && ua->cmd[1] == 0) {
72          return 0;
73       }
74       break;
75    }
76    return 1;
77 }
78
79 /*
80  * Get a positive integer
81  *  Returns:  false if failure
82  *            true  if success => value in ua->pint32_val
83  */
84 bool get_pint(UAContext *ua, const char *prompt)
85 {
86    double dval;
87    ua->pint32_val = 0;
88    ua->int64_val = 0;
89    for (;;) {
90       ua->cmd[0] = 0;
91       if (!get_cmd(ua, prompt)) {
92          return false;
93       }
94       /* Kludge for slots blank line => 0 */
95       if (ua->cmd[0] == 0 && strncmp(prompt, _("Enter slot"), strlen(_("Enter slot"))) == 0) {
96          return true;
97       }
98       if (!is_a_number(ua->cmd)) {
99          ua->warning_msg(_("Expected a positive integer, got: %s\n"), ua->cmd);
100          continue;
101       }
102       errno = 0;
103       dval = strtod(ua->cmd, NULL);
104       if (errno != 0 || dval < 0) {
105          ua->warning_msg(_("Expected a positive integer, got: %s\n"), ua->cmd);
106          continue;
107       }
108       ua->pint32_val = (uint32_t)dval;
109       ua->int64_val = (int64_t)dval;
110       return true;
111    }
112 }
113
114 /*
115  * Test a yes or no response
116  *  Returns:  false if failure
117  *            true  if success => ret == 1 for yes
118  *                                ret == 0 for no
119  */
120 bool is_yesno(char *val, int *ret)
121 {
122    *ret = 0;
123    if ((strcasecmp(val,   _("yes")) == 0) ||
124        (strcasecmp(val, NT_("yes")) == 0))
125    {
126       *ret = 1;
127    } else if ((strcasecmp(val,   _("no")) == 0) ||
128               (strcasecmp(val, NT_("no")) == 0))
129    {
130       *ret = 0;
131    } else {
132       return false;
133    }
134
135    return true;
136 }
137
138 /*
139  * Gets a yes or no response
140  *  Returns:  false if failure
141  *            true  if success => ua->pint32_val == 1 for yes
142  *                                ua->pint32_val == 0 for no
143  */
144 bool get_yesno(UAContext *ua, const char *prompt)
145 {
146    int len;
147    int ret;
148    ua->pint32_val = 0;
149    for (;;) {
150       if (ua->api) ua->UA_sock->signal(BNET_YESNO);
151       if (!get_cmd(ua, prompt)) {
152          return false;
153       }
154       len = strlen(ua->cmd);
155       if (len < 1 || len > 3) {
156          continue;
157       }
158       if (is_yesno(ua->cmd, &ret)) {
159          ua->pint32_val = ret;
160          return true;
161       }
162       ua->warning_msg(_("Invalid response. You must answer yes or no.\n"));
163    }
164 }
165
166 /* 
167  *  Gets an Enabled value => 0, 1, 2, yes, no, archived
168  *  Returns: 0, 1, 2 if OK
169  *           -1 on error
170  */
171 int get_enabled(UAContext *ua, const char *val) 
172 {
173    int Enabled = -1;
174
175    if (strcasecmp(val, "yes") == 0 || strcasecmp(val, "true") == 0) {
176      Enabled = 1;
177    } else if (strcasecmp(val, "no") == 0 || strcasecmp(val, "false") == 0) {
178       Enabled = 0;
179    } else if (strcasecmp(val, "archived") == 0) { 
180       Enabled = 2;
181    } else {
182       Enabled = atoi(val);
183    }
184    if (Enabled < 0 || Enabled > 2) {
185       ua->error_msg(_("Invalid Enabled value, it must be yes, no, archived, 0, 1, or 2\n"));
186       return -1;     
187    }
188    return Enabled;
189 }
190
191 void parse_ua_args(UAContext *ua)
192 {
193    parse_args(ua->cmd, &ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
194 }
195
196 /*
197  * Check if the comment has legal characters
198  * If ua is non-NULL send the message
199  */
200 bool is_comment_legal(UAContext *ua, const char *name)
201 {
202    int len;
203    const char *p;
204    const char *forbid = "'<>&\\\"";
205
206    /* Restrict the characters permitted in the comment */
207    for (p=name; *p; p++) {
208       if (!strchr(forbid, (int)(*p))) {
209          continue;
210       }
211       if (ua) {
212          ua->error_msg(_("Illegal character \"%c\" in a comment.\n"), *p);
213       }
214       return 0;
215    }
216    len = strlen(name);
217    if (len >= MAX_NAME_LENGTH) {
218       if (ua) {
219          ua->error_msg(_("Comment too long.\n"));
220       }
221       return 0;
222    }
223    if (len == 0) {
224       if (ua) {
225          ua->error_msg(_("Comment must be at least one character long.\n"));
226       }
227       return 0;
228    }
229    return 1;
230 }