1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
|
/**
* Copyright (C) 2022 Matt Burt, all xrdp contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file libipm/libipm.c
* @brief Inter-Process Messaging building and parsing definitions
*/
#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include "libipm.h"
#include "libipm_private.h"
#include "libipm_facilities.h"
#include "trans.h"
#include "log.h"
#include "os_calls.h"
#include "string_calls.h"
/**************************************************************************//**
* Log an output appending error
*
* @param trans libipm transport
* @param format printf-like format descriptor
*/
printflike(2, 3) static void
log_append_error(struct trans *trans, const char *format, ...)
{
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
const char *msgno_str = NULL;
char msgno_str_buff[32];
char buff[256];
unsigned int len;
/* Find a string for the message number */
if (priv->msgno_to_str != NULL)
{
msgno_str = priv->msgno_to_str(priv->out_msgno);
}
if (msgno_str == NULL)
{
g_snprintf(msgno_str_buff, sizeof(msgno_str_buff),
"[code #%d]", priv->out_msgno);
msgno_str = msgno_str_buff;
}
len = g_snprintf(buff, sizeof(buff),
"Error creating ipm message for %s, parameter %d :",
msgno_str, priv->out_param_count);
if (len < sizeof(buff))
{
va_list ap;
va_start(ap, format);
vsnprintf(&buff[len], sizeof(buff) - len, format, ap);
va_end(ap);
}
LOG(LOG_LEVEL_ERROR, "%s", buff);
}
/* Common format string for overflow reporting */
static const char *
not_enough_output_msg = "Not enough space in output buffer for '%c'";
/* Common format string for bad value reporting */
static const char *
bad_value_msg = "Type '%c' has unsupported value '%d'";
/**************************************************************************//**
* Add a bool to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack (promoted to int)
* @return != 0 for error
*
* The boolean value must be a 0 or a 1.
*/
static enum libipm_status
append_bool_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
if (!s_check_rem_out(s, 1 + 1))
{
log_append_error(trans, not_enough_output_msg, c);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
int tmp = va_arg(*argptr, int);
if (tmp < 0 || tmp > 1)
{
log_append_error(trans, bad_value_msg, c, tmp);
rv = E_LI_BAD_VALUE;
}
else
{
out_uint8(s, c);
out_uint8(s, tmp);
}
}
return rv;
}
/**************************************************************************//**
* Add an octet to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack (promoted to int)
* @return != 0 for error
*/
static enum libipm_status
append_int8_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
if (!s_check_rem_out(s, 1 + 1))
{
log_append_error(trans, not_enough_output_msg, c);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
int tmp = va_arg(*argptr, int);
if (tmp < 0 || tmp > 255)
{
log_append_error(trans, bad_value_msg, c, tmp);
rv = E_LI_BAD_VALUE;
}
else
{
out_uint8(s, c);
out_uint8(s, tmp);
}
}
return rv;
}
/**************************************************************************//**
* Add an 16-bit integer to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack (promoted to int)
* @return != 0 for error
*/
static enum libipm_status
append_int16_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
if (!s_check_rem_out(s, 1 + 2))
{
log_append_error(trans, not_enough_output_msg, c);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
int tmp = va_arg(*argptr, int);
if ((c == 'n' && (tmp < -0x8000 || tmp > 0x7fff)) ||
(c == 'q' && (tmp < 0 || tmp > 0xffff)))
{
log_append_error(trans, bad_value_msg, c, tmp);
rv = E_LI_BAD_VALUE;
}
else
{
out_uint8(s, c);
out_uint16_le(s, tmp);
}
}
return rv;
}
/**************************************************************************//**
* Add a 32-bit integer to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack
* @return != 0 for error
*/
static enum libipm_status
append_int32_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
if (!s_check_rem_out(s, 1 + 4))
{
log_append_error(trans, not_enough_output_msg, c);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
/* If int is bigger than 4 bytes, the argument will be
* promoted to 'int' rather than 'int32_t'/'uint32_t',
* and we will need to check the specified value is in range.
*/
#if SIZEOF_INT > 4
int tmp = va_arg(*argptr, int);
if ((c == 'i' && (tmp < -0x80000000 || tmp > 0x7fffffff)) ||
(c == 'u' && (tmp < 0 || tmp > 0xffffffff)))
{
log_append_error(trans, bad_value_msg, c, tmp);
rv = E_LI_BAD_VALUE;
}
else
{
out_uint8(s, c);
out_uint32_le(s, tmp);
}
#else
uint32_t tmp = va_arg(*argptr, uint32_t);
out_uint8(s, c);
out_uint32_le(s, tmp);
#endif
}
return rv;
}
/**************************************************************************//**
* Add a 64-bit integer to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack
* @return != 0 for error
*/
static enum libipm_status
append_int64_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
if (!s_check_rem_out(s, 1 + 8))
{
log_append_error(trans, not_enough_output_msg, c);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
uint64_t tmp = va_arg(*argptr, uint64_t);
out_uint8(s, c);
out_uint64_le(s, tmp);
}
return rv;
}
/**************************************************************************//**
* Add a terminated string to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack (promoted to int)
* @return != 0 for error
*
* NULL pointers are not allowed for the string.
*/
static enum libipm_status
append_char_ptr_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
const char *str = va_arg(*argptr, const char *);
if (str == NULL)
{
log_append_error(trans, "String cannot be NULL");
rv = E_LI_PROGRAM_ERROR;
}
else
{
unsigned int len = g_strlen(str);
if ((len > (LIBIPM_MAX_MSG_SIZE - HEADER_SIZE - 1)) ||
(!s_check_rem_out(s, 1 + 1 + len)))
{
log_append_error(trans,
"Not enough space in output buffer for "
"'%c'[len=%u]", c, len);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
out_uint8(s, c);
out_uint8p(s, str, len + 1);
}
}
return rv;
}
/**************************************************************************//**
* Add a file descriptor to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr Pointer to value in argument stack (promoted to int)
* @return != 0 for error
*/
static enum libipm_status
append_fd_type(char c, va_list *argptr, struct trans *trans)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
int fd = va_arg(*argptr, int);
if (fd < 0)
{
log_append_error(trans, "File descriptor cannot be < 0");
rv = E_LI_PROGRAM_ERROR;
}
else if (!s_check_rem_out(s, 1))
{
log_append_error(trans,
"Not enough space in output buffer for '%c'", c);
rv = E_LI_BUFFER_OVERFLOW;
}
else if (priv->out_fd_count >= MAX_FD_PER_MSG)
{
log_append_error(trans,
"Too many file descriptors for '%c'", c);
rv = E_LI_TOO_MANY_FDS;
}
else
{
out_uint8(s, c);
priv->out_fds[priv->out_fd_count++] = fd;
}
return rv;
}
/**************************************************************************//**
* Append a fixed size block to the output stream
*
* @param c Type letter which triggered the call
* @param trans libipm transport
* @param argptr argptr to pointer to block descriptor
* @return != 0 for error
*/
static enum libipm_status
append_fsb_type(char c, struct trans *trans, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct stream *s = trans->out_s;
const struct libipm_fsb *fsb = va_arg(*argptr, const struct libipm_fsb *);
if (fsb == NULL || fsb->data == NULL)
{
log_append_error(trans, "Malformed descriptor for '%c'", c);
rv = E_LI_PROGRAM_ERROR;
}
else
{
unsigned int len = fsb->datalen;
if ((len > (LIBIPM_MAX_MSG_SIZE - HEADER_SIZE - 2)) ||
(!s_check_rem_out(s, 1 + 2 + len)))
{
log_append_error(trans, "Not enough space in output buffer"
" for '%c'[len=%u]", c, len);
rv = E_LI_BUFFER_OVERFLOW;
}
else
{
out_uint8(s, c);
out_uint16_le(s, len);
out_uint8a(s, fsb->data, len);
}
}
return rv;
}
/**************************************************************************//**
* Local function to append data to the output stream
*
* @param trans libipm transport
* @param format type-system compatible string
* @param argptr Variables containing data to append
* @pre - trans->priv has been checked to be non-NULL
* @return != 0 for error
*/
static enum libipm_status
libipm_msg_out_appendv(struct trans *trans, const char *format, va_list *argptr)
{
enum libipm_status rv = E_LI_SUCCESS;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
const char *cp;
if (format != NULL)
{
for (cp = format; rv == 0 && *cp != '\0' ; ++cp)
{
char c = *cp;
++priv->out_param_count; /* Count the parameter */
if (g_strchr(libipm_valid_type_chars, c) == NULL)
{
log_append_error(trans,
"Type code '%c' is not supported", c);
rv = E_LI_UNSUPPORTED_TYPE;
break;
}
switch (c)
{
case 'y':
rv = append_int8_type(c, trans, argptr);
break;
case 'b':
rv = append_bool_type(c, trans, argptr);
break;
case 'n':
case 'q':
rv = append_int16_type(c, trans, argptr);
break;
case 'i':
case 'u':
rv = append_int32_type(c, trans, argptr);
break;
case 'x':
case 't':
rv = append_int64_type(c, trans, argptr);
break;
case 's':
rv = append_char_ptr_type(c, trans, argptr);
break;
case 'h':
rv = append_fd_type(c, argptr, trans);
break;
case 'B':
rv = append_fsb_type(c, trans, argptr);
break;
default:
log_append_error(trans,
"Reserved type code '%c' "
"is unimplemented", c);
rv = E_LI_UNIMPLEMENTED_TYPE;
break;
}
}
}
return rv;
}
/**************************************************************************//**
* Prepare the transport to build an output message
* @param trans libipm trans
* @param msgno Number of message
* @return != 0 for error
*/
static void
init_output_buffer(struct trans *trans, unsigned short msgno)
{
struct stream *s = trans->out_s;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
init_stream(s, LIBIPM_MAX_MSG_SIZE);
/* Leave space for header */
s_push_layer(s, iso_hdr, HEADER_SIZE);
priv->out_msgno = msgno;
priv->out_param_count = 0;
priv->out_fd_count = 0;
}
/*****************************************************************************/
enum libipm_status
libipm_msg_out_init(struct trans *trans, unsigned short msgno,
const char *format, ...)
{
enum libipm_status rv;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
if (priv == NULL)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "uninitialised transport");
rv = E_LI_PROGRAM_ERROR;
}
else
{
va_list argptr;
init_output_buffer(trans, msgno);
va_start(argptr, format);
rv = libipm_msg_out_appendv(trans, format, &argptr);
va_end(argptr);
}
return rv;
}
/*****************************************************************************/
enum libipm_status
libipm_msg_out_append(struct trans *trans, const char *format, ...)
{
enum libipm_status rv;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
if (priv == NULL)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "uninitialised transport");
rv = E_LI_PROGRAM_ERROR;
}
else
{
va_list argptr;
va_start(argptr, format);
rv = libipm_msg_out_appendv(trans, format, &argptr);
va_end(argptr);
}
return rv;
}
/*****************************************************************************/
void
libipm_msg_out_mark_end(struct trans *trans)
{
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
if (priv == NULL)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "uninitialised transport");
}
else
{
struct stream *s = trans->out_s;
s_mark_end(s);
s_pop_layer(s, iso_hdr);
/* Write the message header */
out_uint16_le(s, LIBIPM_VERSION);
out_uint16_le(s, s->end - s->data);
out_uint16_le(s, priv->facility);
out_uint16_le(s, priv->out_msgno);
out_uint32_le(s, 0); /* Reserved */
/* Move the output pointer back to the end so another
* append works, and so libipm_msg_out_erase() knows
* exactly what to do */
s->p = s->end;
}
}
/*****************************************************************************/
enum libipm_status
libipm_msg_out_simple_send(struct trans *trans, unsigned short msgno,
const char *format, ...)
{
enum libipm_status rv;
struct libipm_priv *priv = (struct libipm_priv *)trans->extra_data;
if (priv == NULL)
{
LOG_DEVEL(LOG_LEVEL_ERROR, "uninitialised transport");
rv = E_LI_PROGRAM_ERROR;
}
else
{
va_list argptr;
va_start(argptr, format);
init_output_buffer(trans, msgno);
rv = libipm_msg_out_appendv(trans, format, &argptr);
if (rv == E_LI_SUCCESS)
{
libipm_msg_out_mark_end(trans);
if (trans_force_write(trans) != 0)
{
rv = E_LI_TRANSPORT_ERROR;
}
}
va_end(argptr);
}
return rv;
}
/*****************************************************************************/
void
libipm_msg_out_erase(struct trans *trans)
{
struct stream *s = trans->out_s;
if (s->size > 0 && s->data != 0)
{
g_memset(s->data, '\0', s->p - s->data);
}
}
|