| 630 | } |
| 631 | |
| 632 | static int |
| 633 | biba_parse_element(struct mac_biba_element *element, char *string) |
| 634 | { |
| 635 | char *compartment, *end, *grade; |
| 636 | int value; |
| 637 | |
| 638 | if (strcmp(string, "high") == 0 || strcmp(string, "hi") == 0) { |
| 639 | element->mbe_type = MAC_BIBA_TYPE_HIGH; |
| 640 | element->mbe_grade = MAC_BIBA_TYPE_UNDEF; |
| 641 | } else if (strcmp(string, "low") == 0 || strcmp(string, "lo") == 0) { |
| 642 | element->mbe_type = MAC_BIBA_TYPE_LOW; |
| 643 | element->mbe_grade = MAC_BIBA_TYPE_UNDEF; |
| 644 | } else if (strcmp(string, "equal") == 0 || |
| 645 | strcmp(string, "eq") == 0) { |
| 646 | element->mbe_type = MAC_BIBA_TYPE_EQUAL; |
| 647 | element->mbe_grade = MAC_BIBA_TYPE_UNDEF; |
| 648 | } else { |
| 649 | element->mbe_type = MAC_BIBA_TYPE_GRADE; |
| 650 | |
| 651 | /* |
| 652 | * Numeric grade piece of the element. |
| 653 | */ |
| 654 | grade = strsep(&string, ":"); |
| 655 | value = strtol(grade, &end, 10); |
| 656 | if (end == grade || *end != '\0') |
| 657 | return (EINVAL); |
| 658 | if (value < 0 || value > 65535) |
| 659 | return (EINVAL); |
| 660 | element->mbe_grade = value; |
| 661 | |
| 662 | /* |
| 663 | * Optional compartment piece of the element. If none are |
| 664 | * included, we assume that the label has no compartments. |
| 665 | */ |
| 666 | if (string == NULL) |
| 667 | return (0); |
| 668 | if (*string == '\0') |
| 669 | return (0); |
| 670 | |
| 671 | while ((compartment = strsep(&string, "+")) != NULL) { |
| 672 | value = strtol(compartment, &end, 10); |
| 673 | if (compartment == end || *end != '\0') |
| 674 | return (EINVAL); |
| 675 | if (value < 1 || value > MAC_BIBA_MAX_COMPARTMENTS) |
| 676 | return (EINVAL); |
| 677 | MAC_BIBA_BIT_SET(value, element->mbe_compartments); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | return (0); |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Note: destructively consumes the string, make a local copy before calling |
no test coverage detected