| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include "generic/common.h" |
| 32 | #include "public/sexp-manip.h" |
| 33 | #include "_seap-packetq.h" |
| 34 | #include "_seap-packet.h" |
| 35 | #include "_seap-types.h" |
| 36 | #include "seap-descriptor.h" |
| 37 | #include "sch_queue.h" |
| 38 | #include "debug_priv.h" |
| 39 | |
| 40 | SEAP_packet_t *SEAP_packet_new (void) |
| 41 | { |
| 42 | SEAP_packet_t *p = malloc(sizeof(SEAP_packet_t)); |
| 43 | memset (p, 0, sizeof (SEAP_packet_t)); |
| 44 | p->type = SEAP_PACKET_INV; |
| 45 | |
| 46 | return (p); |
| 47 | } |
| 48 | |
| 49 | void SEAP_packet_free (SEAP_packet_t *packet) |
| 50 | { |
| 51 | free(packet); |
| 52 | } |
| 53 | |
| 54 | void *SEAP_packet_settype (SEAP_packet_t *packet, uint8_t type) |
| 55 | { |
| 56 | _A(packet != NULL); |
| 57 | _A(type == SEAP_PACKET_MSG || |
| 58 | type == SEAP_PACKET_CMD || |
| 59 | type == SEAP_PACKET_ERR); |
| 60 | |
| 61 | packet->type = type; |
| 62 | |
| 63 | switch (type) { |
| 64 | case SEAP_PACKET_MSG: |
| 65 | return ((void *)&(packet->data.msg)); |
| 66 | case SEAP_PACKET_ERR: |
| 67 | return ((void *)&(packet->data.msg)); |
| 68 | case SEAP_PACKET_CMD: |
| 69 | return ((void *)&(packet->data.msg)); |
| 70 | default: |
| 71 | abort (); |
| 72 | } |
| 73 | |
| 74 | /* not reached */ |
| 75 | return (NULL); |
| 76 | } |
| 77 | |
| 78 | uint8_t SEAP_packet_gettype (SEAP_packet_t *packet) |
| 79 | { |
| 80 | _A(packet != NULL); |
| 81 | return (packet->type); |
| 82 | } |
| 83 | |
| 84 | SEAP_msg_t *SEAP_packet_msg (SEAP_packet_t *packet) |
| 85 | { |
| 86 | return (&(packet->data.msg)); |
| 87 | } |
| 88 | |
| 89 | SEAP_cmd_t *SEAP_packet_cmd (SEAP_packet_t *packet) |
| 90 | { |
| 91 | return (&(packet->data.cmd)); |
| 92 | } |
| 93 | |
| 94 | SEAP_err_t *SEAP_packet_err (SEAP_packet_t *packet) |
| 95 | { |
| 96 | return (&(packet->data.err)); |
| 97 | } |
| 98 | |
| 99 | static int SEAP_packet_sexp2msg (SEXP_t *sexp_msg, SEAP_msg_t *seap_msg) |
| 100 | { |
| 101 | size_t msg_icnt, msg_n, attr_i; |
| 102 | SEXP_t *attr_name, *attr_val; |
| 103 | |
| 104 | _A(sexp_msg != NULL); |
| 105 | _A(seap_msg != NULL); |
| 106 | |
| 107 | msg_icnt = SEXP_list_length (sexp_msg); |
| 108 | |
| 109 | seap_msg->attrs_cnt = msg_icnt - 4; |
| 110 | seap_msg->attrs = malloc(sizeof (SEAP_attr_t) * seap_msg->attrs_cnt); |
| 111 | |
| 112 | for (msg_n = 2, attr_i = 0; msg_n < msg_icnt; ++msg_n) { |
| 113 | |
| 114 | attr_name = SEXP_list_nth (sexp_msg, msg_n); |
| 115 | if (attr_name == NULL) { |
| 116 | dD("Unexpected error: No S-exp (attr_name) at position %lu in the message (%p).", |
| 117 | msg_n, sexp_msg); |
| 118 | |
| 119 | free(seap_msg->attrs); |
| 120 | |
| 121 | return (SEAP_EUNEXP); |
| 122 | } |
| 123 | |
| 124 | if (SEXP_string_nth (attr_name, 1) == ':') { |
| 125 | if (SEXP_strcmp (attr_name, ":id") == 0) { |
| 126 | |
| 127 | attr_val = SEXP_list_nth (sexp_msg, msg_n + 1); |
| 128 | if (attr_val == NULL) { |
| 129 | dD("Unexpected error: \"%s\": No attribute value at position %lu in the message (%p).", |
| 130 | "id", msg_n + 1, sexp_msg); |
| 131 | |
| 132 | free(seap_msg->attrs); |
| 133 | SEXP_free(attr_name); |
| 134 | |
| 135 | return (SEAP_EUNEXP); |
| 136 | } |
| 137 | |
| 138 | #if SEAP_MSGID_BITS == 64 |
| 139 | seap_msg->id = SEXP_number_getu_64 (attr_val); |
| 140 | if (seap_msg->id == UINT64_MAX) |
| 141 | #else |
| 142 | seap_msg->id = SEXP_number_getu_32 (attr_val); |
| 143 | if (seap_msg->id == UINT32_MAX) |
| 144 | #endif |
| 145 | { |
| 146 | switch (errno) { |
| 147 | case EDOM: |
| 148 | dD("id: invalid value or type: s_exp=%p, type=%s", |
| 149 | (void *)attr_val, SEXP_strtype (attr_val)); |
| 150 | |
| 151 | free(seap_msg->attrs); |
| 152 | SEXP_free(attr_val); |
| 153 | SEXP_free(attr_name); |
| 154 | |
| 155 | return (SEAP_EINVAL); |
| 156 | case EFAULT: |
| 157 | dD("id: not found"); |
| 158 | |
| 159 | free(seap_msg->attrs); |
| 160 | SEXP_free(attr_val); |
| 161 | SEXP_free(attr_name); |
| 162 | |
| 163 | return (SEAP_EINVAL); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | SEXP_free (attr_val); |
| 168 | } else { |
| 169 | seap_msg->attrs[attr_i].name = SEXP_string_subcstr (attr_name, 1, 0); |
| 170 | seap_msg->attrs[attr_i].value = SEXP_list_nth (sexp_msg, msg_n + 1); |
| 171 | |
| 172 | if (seap_msg->attrs[attr_i].value == NULL) { |
| 173 | dD("Unexpected error: \"%s\": No attribute value at position %lu in the message (%p).", |
| 174 | seap_msg->attrs[attr_i].name, msg_n + 1, sexp_msg); |
| 175 | |
| 176 | free(seap_msg->attrs[attr_i].name); |
| 177 | |
| 178 | for (; attr_i > 0; --attr_i) { |
| 179 | free(seap_msg->attrs[attr_i - 1].name); |
| 180 | |
| 181 | if (seap_msg->attrs[attr_i - 1].value != NULL) |
| 182 | SEXP_free (seap_msg->attrs[attr_i - 1].value); |
| 183 | } |
| 184 | |
| 185 | free(seap_msg->attrs); |
| 186 | SEXP_free(attr_name); |
| 187 | |
| 188 | return (SEAP_EINVAL); |
| 189 | } |
| 190 | |
| 191 | ++attr_i; |
| 192 | } |
| 193 | |
| 194 | ++msg_n; |
| 195 | } else { |
| 196 | seap_msg->attrs[attr_i].name = SEXP_string_cstr (attr_name); |
| 197 | seap_msg->attrs[attr_i].value = NULL; |
| 198 | |
| 199 | ++attr_i; |
| 200 | } |
| 201 | |
| 202 | SEXP_free (attr_name); |
| 203 | } |
| 204 | |
| 205 | _A(attr_i <= (SEXP_list_length (sexp_msg) - 4)); |
| 206 | _A(attr_i >= (SEXP_list_length (sexp_msg) - 4)/2); |
| 207 | |
| 208 | seap_msg->attrs_cnt = attr_i; |
| 209 | if (seap_msg->attrs_cnt == 0) { |
| 210 | free(seap_msg->attrs); |
| 211 | seap_msg->attrs = NULL; |
| 212 | } else { |
| 213 | void *new_attrs = realloc(seap_msg->attrs, sizeof(SEAP_attr_t) * seap_msg->attrs_cnt); |
| 214 | seap_msg->attrs = new_attrs; |
| 215 | } |
| 216 | |
| 217 | seap_msg->sexp = SEXP_list_last (sexp_msg); |
| 218 | |
| 219 | return (0); |
| 220 | } |
| 221 | |
| 222 | static SEXP_t *SEAP_packet_msg2sexp (SEAP_msg_t *msg) |
| 223 | { |
| 224 | SEXP_t *sexp; |
| 225 | uint16_t i; |
| 226 | |
| 227 | SEXP_t *r0, *r1, *r2; |
| 228 | |
| 229 | _A(msg != NULL); |
| 230 | |
| 231 | sexp = SEXP_list_new (r0 = SEXP_string_new (SEAP_SYM_MSG, strlen (SEAP_SYM_MSG)), |
| 232 | r1 = SEXP_string_new (":id", 3), |
| 233 | #if SEAP_MSGID_BITS == 64 |
| 234 | r2 = SEXP_number_newu_64 (msg->id), |
| 235 | #else |
| 236 | r2 = SEXP_number_newu_32 (msg->id), |
| 237 | #endif |
| 238 | NULL); |
| 239 | |
| 240 | SEXP_free(r0); |
| 241 | SEXP_free(r1); |
| 242 | SEXP_free(r2); |
| 243 | |
| 244 | /* Add message attributes */ |
| 245 | for (i = 0; i < msg->attrs_cnt; ++i) { |
| 246 | if (msg->attrs[i].value != NULL) { |
| 247 | SEXP_list_add (sexp, r0 = SEXP_string_newf (":%s", msg->attrs[i].name)); |
| 248 | SEXP_list_add (sexp, msg->attrs[i].value); |
| 249 | } else { |
| 250 | SEXP_list_add (sexp, r0 = SEXP_string_new (msg->attrs[i].name, |
| 251 | strlen (msg->attrs[i].name))); |
| 252 | } |
| 253 | |
| 254 | SEXP_free (r0); |
| 255 | } |
| 256 | |
| 257 | /* Add data */ |
| 258 | if (msg->sexp != NULL) |
| 259 | SEXP_list_add (sexp, msg->sexp); |
| 260 | else { |
| 261 | SEXP_list_add (sexp, r0 = SEXP_list_new (NULL)); /* FIXME */ |
| 262 | SEXP_free(r0); |
| 263 | } |
| 264 | |
| 265 | dD("MSG -> SEXP"); |
| 266 | dO(OSCAP_DEBUGOBJ_SEXP, sexp); |
| 267 | dD("packet size: %zu", SEXP_sizeof(sexp)); |
| 268 | |
| 269 | return (sexp); |
| 270 | } |
| 271 | |
| 272 | static int SEAP_packet_sexp2cmd (SEXP_t *sexp_cmd, SEAP_cmd_t *seap_cmd) |
| 273 | { |
| 274 | SEXP_t *item, *val; |
| 275 | size_t i; |
| 276 | int mattrs; |
| 277 | |
| 278 | memset (seap_cmd, 0, sizeof (SEAP_cmd_t)); |
| 279 | seap_cmd->class = SEAP_CMDCLASS_INT; |
| 280 | seap_cmd->args = NULL; |
| 281 | |
| 282 | mattrs = 0; |
| 283 | i = 2; |
| 284 | |
| 285 | while ((item = SEXP_list_nth (sexp_cmd, i)) != NULL) { |
| 286 | if (SEXP_strncmp (item, ":", 1) == 0) { |
| 287 | char *attr_name; |
| 288 | |
| 289 | attr_name = SEXP_string_cstr (item); |
| 290 | |
| 291 | switch (*(attr_name + 1)) { |
| 292 | case 'i': |
| 293 | if (strcmp (attr_name + 1, "id") == 0) { |
| 294 | val = SEXP_list_nth (sexp_cmd, i + 1); |
| 295 | |
| 296 | if (val == NULL) |
| 297 | break; |
| 298 | |
| 299 | if (SEXP_numberp (val)) { |
| 300 | seap_cmd->id = SEXP_number_getu_16 (val); |
| 301 | ++mattrs; |
| 302 | } |
| 303 | |
| 304 | SEXP_free (val); |
| 305 | } |
| 306 | break; |
| 307 | case 'r': |
| 308 | if (strcmp (attr_name + 1, "reply_id") == 0) { |
| 309 | val = SEXP_list_nth (sexp_cmd, i + 1); |
| 310 | |
| 311 | if (val == NULL) |
| 312 | break; |
| 313 | |
| 314 | if (SEXP_numberp (val)) { |
| 315 | seap_cmd->rid = SEXP_number_getu_16 (val); |
| 316 | seap_cmd->flags |= SEAP_CMDFLAG_REPLY; |
| 317 | } |
| 318 | |
| 319 | SEXP_free (val); |
| 320 | } |
| 321 | break; |
| 322 | case 'c': |
| 323 | if (strcmp (attr_name + 1, "class") == 0) { |
| 324 | val = SEXP_list_nth (sexp_cmd, i + 1); |
| 325 | |
| 326 | if (val == NULL) |
| 327 | break; |
| 328 | |
| 329 | if (SEXP_stringp (val)) { |
| 330 | if (SEXP_strcmp (val, "usr") == 0) { |
| 331 | seap_cmd->class = SEAP_CMDCLASS_USR; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | SEXP_free (val); |
| 336 | } |
| 337 | break; |
| 338 | case 't': |
| 339 | if (strcmp (attr_name + 1, "type") == 0) { |
| 340 | val = SEXP_list_nth (sexp_cmd, i + 1); |
| 341 | |
| 342 | if (val == NULL) |
| 343 | break; |
| 344 | |
| 345 | if (SEXP_stringp (val)) { |
| 346 | if (SEXP_strcmp (val, "sync") == 0) |
| 347 | seap_cmd->flags |= SEAP_CMDFLAG_SYNC; |
| 348 | } |
| 349 | |
| 350 | SEXP_free (val); |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | free (attr_name); |
| 356 | ++i; ++i; |
| 357 | } else { |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | SEXP_free (item); |
| 362 | } |
| 363 | |
| 364 | if (item == NULL || mattrs < 1) { |
| 365 | if (item != NULL) |
| 366 | SEXP_free(item); |
| 367 | |
| 368 | errno = EINVAL; |
| 369 | return (-1); |
| 370 | } |
| 371 | |
| 372 | if (SEXP_numberp (item)) { |
| 373 | seap_cmd->code = SEXP_number_getu_16 (item); |
| 374 | SEXP_free (item); |
| 375 | } else { |
| 376 | SEXP_free (item); |
| 377 | errno = EINVAL; |
| 378 | return (-1); |
| 379 | } |
| 380 | |
| 381 | seap_cmd->args = SEXP_list_nth (sexp_cmd, ++i); |
| 382 | |
| 383 | return (0); |
| 384 | } |
| 385 | |
| 386 | static SEXP_t *SEAP_packet_cmd2sexp (SEAP_cmd_t *cmd) |
| 387 | { |
| 388 | SEXP_t *sexp; |
| 389 | SEXP_t *r0, *r1, *r2; |
| 390 | |
| 391 | _A(cmd != NULL); |
| 392 | |
| 393 | sexp = SEXP_list_new (r0 = SEXP_string_new (SEAP_SYM_CMD, strlen (SEAP_SYM_CMD)), |
| 394 | r1 = SEXP_string_new (":id", 3), |
| 395 | r2 = SEXP_number_newu_16 (cmd->id), |
| 396 | NULL); |
| 397 | |
| 398 | SEXP_free(r0); |
| 399 | SEXP_free(r1); |
| 400 | SEXP_free(r2); |
| 401 | |
| 402 | if (cmd->flags & SEAP_CMDFLAG_REPLY) { |
| 403 | SEXP_list_add (sexp, |
| 404 | r0 = SEXP_string_new (":reply_id", 9)); |
| 405 | SEXP_list_add (sexp, |
| 406 | r1 = SEXP_number_newu_16 (cmd->rid)); |
| 407 | |
| 408 | SEXP_free(r0); |
| 409 | SEXP_free(r1); |
| 410 | } |
| 411 | |
| 412 | SEXP_list_add (sexp, |
| 413 | r0 = SEXP_string_new (":class", 6)); |
| 414 | SEXP_free (r0); |
| 415 | |
| 416 | switch (cmd->class) { |
| 417 | case SEAP_CMDCLASS_USR: |
| 418 | SEXP_list_add (sexp, |
| 419 | r0 = SEXP_string_new ("usr", 3)); |
| 420 | break; |
| 421 | case SEAP_CMDCLASS_INT: |
| 422 | SEXP_list_add (sexp, |
| 423 | r0 = SEXP_string_new ("int", 3)); |
| 424 | break; |
| 425 | default: |
| 426 | abort (); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | SEXP_free (r0); |
| 431 | |
| 432 | SEXP_list_add (sexp, |
| 433 | r0 = SEXP_string_new (":type", 5)); |
| 434 | |
| 435 | SEXP_list_add (sexp, |
| 436 | r1 = (cmd->flags & SEAP_CMDFLAG_SYNC ? |
| 437 | SEXP_string_new ("sync", 4) : SEXP_string_new ("async", 5))); |
| 438 | |
| 439 | SEXP_list_add (sexp, |
| 440 | r2 = SEXP_number_newu_16 (cmd->code)); |
| 441 | |
| 442 | SEXP_free(r0); |
| 443 | SEXP_free(r1); |
| 444 | SEXP_free(r2); |
| 445 | |
| 446 | if (cmd->args != NULL) |
| 447 | SEXP_list_add (sexp, cmd->args); |
| 448 | |
| 449 | dD("CMD -> SEXP"); |
| 450 | dO(OSCAP_DEBUGOBJ_SEXP, sexp); |
| 451 | dD("packet size: %zu", SEXP_sizeof(sexp)); |
| 452 | |
| 453 | SEXP_VALIDATE(sexp); |
| 454 | return (sexp); |
| 455 | } |
| 456 | |
| 457 | static int SEAP_packet_sexp2err (SEXP_t *sexp_err, SEAP_err_t *seap_err) |
| 458 | { |
| 459 | SEXP_t *member; |
| 460 | uint32_t n; |
| 461 | |
| 462 | seap_err->id = 0; |
| 463 | seap_err->code = SEAP_EUNKNOWN; |
| 464 | seap_err->type = SEAP_ETYPE_INT; |
| 465 | seap_err->data = NULL; |
| 466 | |
| 467 | n = 2; |
| 468 | |
| 469 | while ((member = SEXP_list_nth (sexp_err, n)) != NULL) { |
| 470 | switch (SEXP_typeof (member)) { |
| 471 | case SEXP_TYPE_STRING: |
| 472 | if (SEXP_string_nth (member, 1) != ':') { |
| 473 | dD("Invalid string/attribute in packet"); |
| 474 | SEXP_free (member); |
| 475 | errno = EINVAL; |
| 476 | return (-1); |
| 477 | } |
| 478 | |
| 479 | if (SEXP_strcmp (member, ":orig_id") == 0) { |
| 480 | SEXP_t *val; |
| 481 | |
| 482 | val = SEXP_list_nth (sexp_err, ++n); |
| 483 | |
| 484 | if (!SEXP_numberp (val)) { |
| 485 | dD("Invalid type of :orig_id value"); |
| 486 | SEXP_free (val); |
| 487 | SEXP_free (member); |
| 488 | errno = EINVAL; |
| 489 | return (-1); |
| 490 | } |
| 491 | |
| 492 | #if SEAP_MSGID_BITS == 64 |
| 493 | seap_err->id = SEXP_number_getu_64 (val); |
| 494 | #else |
| 495 | seap_err->id = SEXP_number_getu_32 (val); |
| 496 | #endif |
| 497 | SEXP_free (val); |
| 498 | } else if (SEXP_strcmp (member, ":type") == 0) { |
| 499 | SEXP_t *val; |
| 500 | |
| 501 | val = SEXP_list_nth (sexp_err, ++n); |
| 502 | seap_err->type = SEXP_number_getu_32(val); |
| 503 | |
| 504 | if (!SEXP_numberp(val)) { |
| 505 | dD("Invalid type of the :type attribute"); |
| 506 | SEXP_free (val); |
| 507 | SEXP_free (member); |
| 508 | errno = EINVAL; |
| 509 | return (-1); |
| 510 | } |
| 511 | |
| 512 | if (!(seap_err->type == SEAP_ETYPE_INT || |
| 513 | seap_err->type == SEAP_ETYPE_USER)) { |
| 514 | dD("Invalid value of the :type attribute"); |
| 515 | SEXP_free(val); |
| 516 | SEXP_free(member); |
| 517 | errno = EINVAL; |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | SEXP_free (val); |
| 522 | } else { |
| 523 | dD("Unknown packet attribute"); |
| 524 | SEXP_free (member); |
| 525 | errno = EINVAL; |
| 526 | return (-1); |
| 527 | } |
| 528 | |
| 529 | break; |
| 530 | case SEXP_TYPE_NUMBER: |
| 531 | seap_err->code = SEXP_number_getu_32 (member); |
| 532 | SEXP_free (member); |
| 533 | |
| 534 | goto loop_exit; |
| 535 | default: |
| 536 | dD("Unexpected type of packet member: list"); |
| 537 | SEXP_free (member); |
| 538 | errno = EINVAL; |
| 539 | return (-1); |
| 540 | } |
| 541 | |
| 542 | SEXP_free(member); |
| 543 | ++n; |
| 544 | } |
| 545 | loop_exit: |
| 546 | seap_err->data = SEXP_list_nth (sexp_err, n + 1); |
| 547 | |
| 548 | return (0); |
| 549 | } |
| 550 | |
| 551 | static SEXP_t *SEAP_packet_err2sexp (SEAP_err_t *err) |
| 552 | { |
| 553 | SEXP_t *sexp; |
| 554 | SEXP_t *r0, *r1, *r2, *r3, *r4, *r5; |
| 555 | |
| 556 | sexp = SEXP_list_new (r0 = SEXP_string_new (SEAP_SYM_ERR, strlen (SEAP_SYM_ERR)), |
| 557 | r1 = SEXP_string_new (":orig_id", 8), |
| 558 | #if SEAP_MSGID_BITS == 64 |
| 559 | r2 = SEXP_number_newu_64 (err->id), |
| 560 | #else |
| 561 | r2 = SEXP_number_newu_32 (err->id), |
| 562 | #endif |
| 563 | r3 = SEXP_string_new (":type", 5), |
| 564 | r4 = SEXP_number_newu (err->type), |
| 565 | r5 = SEXP_number_newu (err->code), |
| 566 | NULL); |
| 567 | |
| 568 | SEXP_free(r0); |
| 569 | SEXP_free(r1); |
| 570 | SEXP_free(r2); |
| 571 | SEXP_free(r3); |
| 572 | SEXP_free(r4); |
| 573 | SEXP_free(r5); |
| 574 | |
| 575 | if (err->data != NULL) |
| 576 | SEXP_list_add (sexp, err->data); |
| 577 | |
| 578 | dD("ERR -> SEXP"); |
| 579 | dO(OSCAP_DEBUGOBJ_SEXP, sexp); |
| 580 | dD("packet size: %zu", SEXP_sizeof(sexp)); |
| 581 | |
| 582 | return (sexp); |
| 583 | } |
| 584 | |
| 585 | static SEXP_t *SEAP_packet2sexp (SEAP_packet_t *packet) |
| 586 | { |
| 587 | SEXP_t *sexp = NULL; |
| 588 | |
| 589 | switch (packet->type) { |
| 590 | case SEAP_PACKET_MSG: |
| 591 | sexp = SEAP_packet_msg2sexp (SEAP_packet_msg (packet)); |
| 592 | break; |
| 593 | case SEAP_PACKET_CMD: |
| 594 | sexp = SEAP_packet_cmd2sexp (SEAP_packet_cmd (packet)); |
| 595 | break; |
| 596 | case SEAP_PACKET_ERR: |
| 597 | sexp = SEAP_packet_err2sexp (SEAP_packet_err (packet)); |
| 598 | break; |
| 599 | default: |
| 600 | errno = EINVAL; |
| 601 | } |
| 602 | |
| 603 | return (sexp); |
| 604 | } |
| 605 | |
| 606 | int SEAP_packet_recv (SEAP_CTX_t *ctx, int sd, SEAP_packet_t **packet) |
| 607 | { |
| 608 | SEAP_desc_t *dsc; |
| 609 | SEXP_t *sexp_buffer; |
| 610 | SEXP_t *sexp_packet; |
| 611 | |
| 612 | SEXP_t *psym_sexp; |
| 613 | char psym_cstr_b[16+1]; |
| 614 | char *psym_cstr; |
| 615 | |
| 616 | SEAP_packet_t *_packet; |
| 617 | |
| 618 | dsc = SEAP_desc_get (ctx->sd_table, sd); |
| 619 | |
| 620 | if (dsc == NULL) { |
| 621 | errno = EFAULT; |
| 622 | return (-1); |
| 623 | } |
| 624 | |
| 625 | if (SEAP_packetq_get(&dsc->pck_queue, packet) != -1) |
| 626 | return (0); |
| 627 | |
| 628 | /* |
| 629 | * Event loop |
| 630 | * The read mutex is not locked during the wait for an event. |
| 631 | * This allows other threads to hijack the input stream and |
| 632 | * to perform high priority tasks (e.g. synchronous commands). |
| 633 | * If there is a read event and nobody (except this thread) has |
| 634 | * locked the read mutex then the execution of this loop is |
| 635 | * stopped, the read mutex gets locked and the receiving and |
| 636 | * parsing of data can begin. |
| 637 | */ |
| 638 | |
| 639 | for (;;) { |
| 640 | switch (DESC_TRYRLOCK (dsc)) { |
| 641 | case 1: |
| 642 | goto eloop_exit; |
| 643 | case 0: |
| 644 | switch (DESC_RLOCK (dsc)) { |
| 645 | case 1: |
| 646 | DESC_RUNLOCK(dsc); |
| 647 | continue; |
| 648 | case -1: |
| 649 | goto fail_rmutex; |
| 650 | default: |
| 651 | abort (); |
| 652 | } |
| 653 | case -1: |
| 654 | fail_rmutex: |
| 655 | |
| 656 | protect_errno { |
| 657 | dE("An error ocured while locking the read mutex: dsc=%p, errno=%u, %s.", |
| 658 | dsc, errno, strerror (errno)); |
| 659 | } |
| 660 | |
| 661 | return (-1); |
| 662 | default: |
| 663 | abort (); |
| 664 | } |
| 665 | } |
| 666 | eloop_exit: |
| 667 | |
| 668 | sexp_buffer = sch_queue_recvsexp(dsc); |
| 669 | SEXP_VALIDATE(sexp_buffer); |
| 670 | |
| 671 | (*packet) = NULL; |
| 672 | |
| 673 | while((sexp_packet = SEXP_list_pop (sexp_buffer)) != NULL) { |
| 674 | if (!SEXP_listp(sexp_packet)) { |
| 675 | dD("Invalid SEAP packet received: %s.", "not a list"); |
| 676 | |
| 677 | SEXP_free (sexp_packet); |
| 678 | SEXP_free (sexp_buffer); |
| 679 | |
| 680 | errno = EINVAL; |
| 681 | return (-1); |
| 682 | } else if (SEXP_list_length (sexp_packet) < 2) { |
| 683 | dD("Invalid SEAP packet received: %s.", "list length < 2"); |
| 684 | |
| 685 | SEXP_free (sexp_packet); |
| 686 | SEXP_free (sexp_buffer); |
| 687 | |
| 688 | errno = EINVAL; |
| 689 | return (-1); |
| 690 | } |
| 691 | |
| 692 | psym_sexp = SEXP_list_first (sexp_packet); |
| 693 | |
| 694 | if (!SEXP_stringp(psym_sexp)) { |
| 695 | dD("Invalid SEAP packet received: %s.", "first list item is not a string"); |
| 696 | |
| 697 | SEXP_free (psym_sexp); |
| 698 | SEXP_free (sexp_packet); |
| 699 | SEXP_free (sexp_buffer); |
| 700 | |
| 701 | errno = EINVAL; |
| 702 | return (-1); |
| 703 | } else if (SEXP_string_length (psym_sexp) != (strlen (SEAP_SYM_PREFIX) + 3)) { |
| 704 | dD("Invalid SEAP packet received: %s.", "invalid packet type symbol length"); |
| 705 | |
| 706 | SEXP_free (psym_sexp); |
| 707 | SEXP_free (sexp_packet); |
| 708 | SEXP_free (sexp_buffer); |
| 709 | |
| 710 | errno = EINVAL; |
| 711 | return (-1); |
| 712 | } else if (SEXP_strncmp (psym_sexp, SEAP_SYM_PREFIX, strlen (SEAP_SYM_PREFIX)) != 0) { |
| 713 | dD("Invalid SEAP packet received: %s.", "invalid prefix"); |
| 714 | |
| 715 | SEXP_free (psym_sexp); |
| 716 | SEXP_free (sexp_packet); |
| 717 | SEXP_free (sexp_buffer); |
| 718 | |
| 719 | errno = EINVAL; |
| 720 | return (-1); |
| 721 | } |
| 722 | |
| 723 | SEXP_string_cstr_r (psym_sexp, psym_cstr_b, sizeof psym_cstr_b); |
| 724 | psym_cstr = psym_cstr_b + strlen (SEAP_SYM_PREFIX); |
| 725 | SEXP_free (psym_sexp); |
| 726 | |
| 727 | switch (psym_cstr[0]) { |
| 728 | case 'm': |
| 729 | if (psym_cstr[1] == 's' && |
| 730 | psym_cstr[2] == 'g') |
| 731 | { |
| 732 | _packet = SEAP_packet_new (); |
| 733 | _packet->type = SEAP_PACKET_MSG; |
| 734 | |
| 735 | if (SEAP_packet_sexp2msg (sexp_packet, &(_packet->data.msg)) != 0) { |
| 736 | /* error */ |
| 737 | dD("Invalid SEAP packet received: %s.", "can't translate to msg struct"); |
| 738 | |
| 739 | SEXP_free (sexp_packet); |
| 740 | SEAP_packet_free(_packet); |
| 741 | SEXP_free (sexp_buffer); |
| 742 | |
| 743 | errno = EINVAL; |
| 744 | return (-1); |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | goto invalid; |
| 749 | case 'c': |
| 750 | if (psym_cstr[1] == 'm' && |
| 751 | psym_cstr[2] == 'd') |
| 752 | { |
| 753 | _packet = SEAP_packet_new (); |
| 754 | _packet->type = SEAP_PACKET_CMD; |
| 755 | |
| 756 | if (SEAP_packet_sexp2cmd (sexp_packet, &(_packet->data.cmd)) != 0) { |
| 757 | /* error */ |
| 758 | dD("Invalid SEAP packet received: %s.", "can't translate to cmd struct"); |
| 759 | SEXP_free (sexp_packet); |
| 760 | SEAP_packet_free(_packet); |
| 761 | SEXP_free (sexp_buffer); |
| 762 | |
| 763 | errno = EINVAL; |
| 764 | return (-1); |
| 765 | } |
| 766 | break; |
| 767 | } |
| 768 | goto invalid; |
| 769 | case 'e': |
| 770 | if (psym_cstr[1] == 'r' && |
| 771 | psym_cstr[2] == 'r') |
| 772 | { |
| 773 | _packet = SEAP_packet_new (); |
| 774 | _packet->type = SEAP_PACKET_ERR; |
| 775 | |
| 776 | if (SEAP_packet_sexp2err (sexp_packet, &(_packet->data.err)) != 0) { |
| 777 | /* error */ |
| 778 | dD("Invalid SEAP packet received: %s.", "can't translate to err struct"); |
| 779 | SEXP_free (sexp_packet); |
| 780 | SEAP_packet_free(_packet); |
| 781 | SEXP_free (sexp_buffer); |
| 782 | |
| 783 | errno = EINVAL; |
| 784 | return (-1); |
| 785 | } |
| 786 | break; |
| 787 | } |
| 788 | /* FALLTHROUGH */ |
| 789 | default: |
| 790 | invalid: |
| 791 | dD("Invalid SEAP packet received: %s.", "invalid packet type symbol"); |
| 792 | SEXP_free (sexp_packet); |
| 793 | SEXP_free (sexp_buffer); |
| 794 | errno = EINVAL; |
| 795 | return (-1); |
| 796 | } |
| 797 | |
| 798 | |
| 799 | dD("Received packet"); |
| 800 | dO(OSCAP_DEBUGOBJ_SEXP, sexp_packet); |
| 801 | dD("packet size: %zu", SEXP_sizeof(sexp_packet)); |
| 802 | |
| 803 | SEXP_free(sexp_packet); |
| 804 | |
| 805 | if (*packet == NULL) |
| 806 | (*packet) = _packet; |
| 807 | else |
| 808 | SEAP_packetq_put(&dsc->pck_queue, _packet); |
| 809 | |
| 810 | _packet = NULL; |
| 811 | } |
| 812 | |
| 813 | SEXP_free(sexp_buffer); |
| 814 | |
| 815 | return (0); |
| 816 | } |
| 817 | |
| 818 | int SEAP_packet_recv_bytype (SEAP_CTX_t *ctx, int sd, SEAP_packet_t **packet, uint8_t type) |
| 819 | { |
| 820 | int ret; |
| 821 | SEAP_packet_t *pck; |
| 822 | SEAP_desc_t *dsc; |
| 823 | |
| 824 | _A(ctx != NULL); |
| 825 | _A(packet != NULL); |
| 826 | _A(type == SEAP_PACKET_CMD || |
| 827 | type == SEAP_PACKET_MSG || |
| 828 | type == SEAP_PACKET_ERR); |
| 829 | |
| 830 | dsc = SEAP_desc_get (ctx->sd_table, sd); |
| 831 | |
| 832 | if (dsc == NULL) |
| 833 | return (-1); |
| 834 | |
| 835 | for (;;) { |
| 836 | pck = NULL; |
| 837 | ret = SEAP_packet_recv (ctx, sd, &pck); |
| 838 | |
| 839 | if (ret != 0) |
| 840 | return (ret); |
| 841 | else if (pck->type == type) |
| 842 | break; |
| 843 | else if (SEAP_packetq_put (&dsc->pck_queue, pck) == -1) /* XXX: infinite loop */ |
| 844 | return (-1); |
| 845 | } |
| 846 | |
| 847 | *packet = pck; |
| 848 | |
| 849 | return (ret); |
| 850 | } |
| 851 | |
| 852 | int SEAP_packet_send (SEAP_CTX_t *ctx, int sd, SEAP_packet_t *packet) |
| 853 | { |
| 854 | SEXP_t *packet_sexp; |
| 855 | SEAP_desc_t *dsc; |
| 856 | int ret; |
| 857 | |
| 858 | ret = -1; |
| 859 | dsc = SEAP_desc_get (ctx->sd_table, sd); |
| 860 | |
| 861 | if (dsc == NULL) |
| 862 | return (-1); |
| 863 | |
| 864 | packet_sexp = SEAP_packet2sexp (packet); |
| 865 | |
| 866 | if (packet_sexp == NULL) { |
| 867 | dD("Can't convert S-exp to packet"); |
| 868 | return (-1); |
| 869 | } |
| 870 | |
| 871 | if (DESC_WLOCK(dsc) == 1) { |
| 872 | ret = 0; |
| 873 | |
| 874 | if (sch_queue_sendsexp(dsc, packet_sexp, 0) < 0) { |
| 875 | ret = -1; |
| 876 | |
| 877 | protect_errno { |
| 878 | dD("FAIL: errno=%u, %s.", errno, strerror (errno)); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (DESC_WUNLOCK(dsc) != 1) { |
| 883 | dE("DESC_WUNLOCK failed to unlock a mutex: %s", strerror(errno)); |
| 884 | ret = -1; |
| 885 | } |
| 886 | } else { |
| 887 | dE("DESC_WLOCK failed to lock a mutex: %s", strerror(errno)); |
| 888 | ret = -1; |
| 889 | } |
| 890 | |
| 891 | |
| 892 | protect_errno { |
| 893 | SEXP_free (packet_sexp); |
| 894 | } |
| 895 | |
| 896 | return (ret); |
| 897 | } |
| 898 | |
| 899 | int SEAP_packet_enqueue (SEAP_CTX_t *ctx, int sd, SEAP_packet_t *packet) |
| 900 | { |
| 901 | return (-1); |
| 902 | } |