2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* This code parses and executes the commands sent by guests. It
20 * is therefore particularly security sensitive. The protocol is
21 * documented in hostinfo-protocol(5).
36 #include <apr_general.h>
37 #include <apr_pools.h>
39 #include <apr_strings.h>
41 #include "hostinfod.h"
43 #define PROTOCOL_VERSION "1.0"
46 apr_hash_t *commands = NULL;
48 static const char *string_of_arg_type (enum arg_type);
49 static int not_printable (const char *str);
50 static int contains_crlf (const char *str);
51 static char *parse_c_string (struct guest_description *hval, const char *str, size_t *posn);
52 static int parse_long (struct guest_description *hval, const char *str, size_t *posn, long *ret);
55 execute_command (const struct timespec *now,
56 struct guest_description *hval, const char *command)
61 apr_array_header_t *args;
65 debug ("%s: %s", hval->name, command);
67 /* Create a new pool for allocation during the lifetime of the
68 * request/response. **NB** the hval->reply field is allocated
69 * from this pool, which is why it gets nulled below.
72 apr_pool_destroy (hval->rpool);
75 hval->reply_size = hval->reply_posn = 0;
76 apr_pool_create (&hval->rpool, hval->pool);
78 /* Split up the command. Commands have a very narrowly-defined
79 * format, and we reject any malformed commands with a 400 error.
81 * NB. A lot of the code below assumes 7 bit, printable ASCII,
82 * and this is only safe because of the call to 'not_printable'
85 len = strlen (command);
86 if (len < 1 || len > 4094 || not_printable (command)) {
87 warning ("%s: command too short, too long or contained unprintable chars (len = %d)",
89 send_error (hval, 400);
93 /* Command is alphanumeric string, non-zero length. */
95 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
96 "abcdefghijklmnopqrstuvwxyz"
99 warning ("%s: no command part in request", hval->name);
100 send_error (hval, 400);
104 cmd = apr_pstrmemdup (hval->rpool, command, i);
105 for (j = 0; j < i; ++j)
106 cmd[j] = tolower (cmd[j]);
108 args = apr_array_make (hval->rpool, 0, sizeof (struct arg));
110 while (command[i]) { /* command[i] is the space before the next arg */
111 if (command[i] != ' ') {
112 warning ("%s: there must be a single space between command and each argument",
114 send_error (hval, 400);
121 switch (command[i]) {
123 warning ("%s: trailing space after command", hval->name);
124 send_error (hval, 400);
127 case '"': /* string literal */
128 arg.type = arg_type_string;
129 arg.u.str = parse_c_string (hval, command, &i);
131 send_error (hval, 400);
136 case '-': /* integer literal */
148 arg.type = arg_type_int;
149 if (parse_long (hval, command, &i, &arg.u.i) == -1) {
150 send_error (hval, 400);
155 case 't': /* boolean */
159 arg.type = arg_type_bool;
160 if (strncasecmp (&command[i], "true", 4) == 0) {
163 } else if (strncasecmp (&command[i], "false", 5) == 0) {
172 warning ("%s: unknown or malformed argument starting at position %d ('%c')",
173 hval->name, i, command[i]);
174 send_error (hval, 400);
178 APR_ARRAY_PUSH (args, struct arg) = arg;
182 /* Debug output what the guest sent / what we decoded. */
183 debug ("%s: command '%s' with %d args", hval->name, cmd, args->nelts);
184 for (i = 0; i < args->nelts; ++i) {
185 struct arg arg = APR_ARRAY_IDX (args, i, struct arg);
190 debug ("%s: arg %d : %s = %ld",
191 hval->name, i, string_of_arg_type (arg.type), arg.u.i);
193 case arg_type_string:
194 debug ("%s: arg %d : %s = %s",
195 hval->name, i, string_of_arg_type (arg.type), arg.u.str);
201 /* Know about this command? */
202 fn = apr_hash_get (commands, cmd, APR_HASH_KEY_STRING);
204 send_error (hval, 404);
208 /* Before dispatching the command, check the command is enabled
209 * and guest is not calling it too frequently.
211 error ("XXXXXXX enabled check not implemented XXXXXXX");
212 error ("XXXXXXX frequency check not implemented XXXXXXX");
214 /* Dispatch the command. */
215 fn (hval, cmd, args);
218 /* All commands must consist only of printable 7 bit ASCII.
219 * NB. Don't use isprint(3).
222 not_printable (const char *str)
226 while ((c = *(unsigned char *)str)) {
227 if (c < 32 || c > 126)
235 contains_crlf (const char *str)
239 while ((c = *(unsigned char *)str)) {
240 if (c == '\r' || c == '\n')
251 case '0': case '1': case '2': case '3': case '4':
252 case '5': case '6': case '7': case '8': case '9':
254 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
256 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
264 parse_c_string (struct guest_description *hval, const char *str, size_t *posn)
266 apr_array_header_t *r;
269 r = apr_array_make (hval->rpool, 0, 1);
270 #define APPEND(c) APR_ARRAY_PUSH (r,char) = (c);
272 assert (str[*posn] == '"');
275 while (str[*posn] != '"') {
276 if (str[*posn] == '\\') { /* Start of \escape sequence. */
277 switch (str[*posn+1]) {
278 case '0': case '1': case '2': case '3':
279 if ((str[*posn+2] >= '0' && str[*posn+2] <= '7') &&
280 (str[*posn+3] >= '0' && str[*posn+3] <= '7')) {
281 c = (str[*posn+1] - '0') * 0100;
282 c += (str[*posn+2] - '0') * 010;
283 c += str[*posn+3] - '0';
288 warning ("%s: \\0 cannot appear in string literal", hval->name);
293 warning ("%s: invalid octal sequence in string literal", hval->name);
297 if (isxdigit (str[*posn+2]) && isxdigit (str[*posn+3])) {
298 c = hexval (str[*posn+2]) * 0x10;
299 c += hexval (str[*posn+3]);
304 warning ("%s: \\0 cannot appear in string literal", hval->name);
309 warning ("%s: invalid hex sequence in string literal", hval->name);
350 warning ("%s: unterminated escape sequence in string literal",
354 } else if (str[*posn] == '\0') { /* Unterminated string literal. */
355 warning ("%s: unterminated string literal in request", hval->name);
357 } else { /* Ordinary character. */
363 /* Finish off the string and return it. */
365 (*posn)++; /* Skips over the final quote. */
371 parse_long (struct guest_description *hval,
372 const char *str, size_t *posn, long *ret)
374 error ("XXXXXXX parse_long not implemented XXXXXXX");
378 /* For single line replies. */
380 send_reply (struct guest_description *hval, int code, const char *fs, ...)
385 /* All success codes must be 2xx. */
386 assert (code >= 200 && code < 300);
389 msg = apr_pvsprintf (hval->rpool, fs, args);
392 /* The result string must not contain any CR or LF characters. If
393 * not it's an internal error in the command, or else the caller has
394 * (somehow) managed to pass a bad string through.
396 if (contains_crlf (msg)) {
397 error ("%s: send_reply: refusing the send a reply message containing CR/LF characters. This is a serious internal error in the current command.",
399 send_error (hval, 500);
405 apr_psprintf (hval->rpool, PROTOCOL_VERSION " %03d %s" CRLF,
407 hval->reply_size = strlen (hval->reply);
408 hval->reply_posn = 0;
409 hval->state = guest_state_reply;
413 send_error (struct guest_description *hval, int code)
417 /* All errors must be 4xx or 5xx. */
418 assert (code >= 400 && code < 600);
420 /* NB: If you add a code, update COMMON STATUS CODES section
421 * in hostinfo-protocol.pod.
424 case 400: msg = "Bad request"; break;
425 case 401: msg = "Command disabled"; break;
426 case 404: msg = "Command not found"; break;
427 case 406: msg = "Too frequent"; break;
428 case 500: msg = "Internal server error"; break;
429 default: msg = "Unknown error"; break;
432 /* Construct the reply. */
434 apr_psprintf (hval->rpool, PROTOCOL_VERSION " %03d %s" CRLF,
436 hval->reply_size = strlen (hval->reply);
437 hval->reply_posn = 0;
438 hval->state = guest_state_reply;
440 /* Penalty is always increased on errors, to ensure the guest
441 * cannot flood us with invalid commands.
447 string_of_arg_type (enum arg_type t)
450 case arg_type_string: return "string";
451 case arg_type_int: return "int";
452 case arg_type_bool: return "bool";
458 get_args (apr_array_header_t *args, const char *argfs, ...)
463 va_start (vargs, argfs);
468 if (i >= args->nelts) {
473 arg = APR_ARRAY_IDX (args, i, struct arg);
477 if (arg.type == arg_type_string)
478 * va_arg (vargs, const char **) = arg.u.str;
486 if (arg.type == arg_type_int)
487 * va_arg (vargs, int *) = arg.u.i;
495 if (arg.type == arg_type_bool)
496 * va_arg (vargs, int *) = arg.u.i;
504 error ("get_args: invalid character '%c'", *argfs);