1 /*- 2 * Copyright (c) 2004 Apple Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $P4: //depot/projects/trustedbsd/openbsm/bsm/libbsm.h#35 $ 30 */ 31 32 #ifndef _LIBBSM_H_ 33 #define _LIBBSM_H_ 34 35 /* 36 * NB: definitions, etc., marked with "OpenSSH compatibility" were introduced 37 * solely to allow OpenSSH to compile; Darwin/Apple code should not use them. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/cdefs.h> 42 43 #include <inttypes.h> /* Required for audit.h. */ 44 #include <time.h> /* Required for clock_t on Linux. */ 45 46 #include <bsm/audit.h> 47 #include <bsm/audit_record.h> 48 49 #include <stdio.h> 50 51 #ifdef __APPLE__ 52 #include <mach/mach.h> /* audit_token_t */ 53 #endif 54 55 /* 56 * Size parsed token vectors for execve(2) arguments and environmental 57 * variables. Note: changing these sizes affects the ABI of the token 58 * structure, and as the token structure is often placed in the caller stack, 59 * this is undesirable. 60 */ 61 #define AUDIT_MAX_ARGS 128 62 #define AUDIT_MAX_ENV 128 63 64 /* 65 * Arguments to au_preselect(3). 66 */ 67 #define AU_PRS_USECACHE 0 68 #define AU_PRS_REREAD 1 69 70 #define AU_PRS_SUCCESS 1 71 #define AU_PRS_FAILURE 2 72 #define AU_PRS_BOTH (AU_PRS_SUCCESS|AU_PRS_FAILURE) 73 74 #define AUDIT_EVENT_FILE "/etc/security/audit_event" 75 #define AUDIT_CLASS_FILE "/etc/security/audit_class" 76 #define AUDIT_CONTROL_FILE "/etc/security/audit_control" 77 #define AUDIT_USER_FILE "/etc/security/audit_user" 78 79 #define DIR_CONTROL_ENTRY "dir" 80 #define MINFREE_CONTROL_ENTRY "minfree" 81 #define FILESZ_CONTROL_ENTRY "filesz" 82 #define FLAGS_CONTROL_ENTRY "flags" 83 #define NA_CONTROL_ENTRY "naflags" 84 #define POLICY_CONTROL_ENTRY "policy" 85 #define AUDIT_HOST_CONTROL_ENTRY "host" 86 87 #define AU_CLASS_NAME_MAX 8 88 #define AU_CLASS_DESC_MAX 72 89 #define AU_EVENT_NAME_MAX 30 90 #define AU_EVENT_DESC_MAX 50 91 #define AU_USER_NAME_MAX 50 92 #define AU_LINE_MAX 256 93 #define MAX_AUDITSTRING_LEN 256 94 #define BSM_TEXTBUFSZ MAX_AUDITSTRING_LEN /* OpenSSH compatibility */ 95 96 /* 97 * Arguments to au_close(3). 98 */ 99 #define AU_TO_NO_WRITE 0 /* Abandon audit record. */ 100 #define AU_TO_WRITE 1 /* Commit audit record. */ 101 102 __BEGIN_DECLS 103 struct au_event_ent { 104 au_event_t ae_number; 105 char *ae_name; 106 char *ae_desc; 107 au_class_t ae_class; 108 }; 109 typedef struct au_event_ent au_event_ent_t; 110 111 struct au_class_ent { 112 char *ac_name; 113 au_class_t ac_class; 114 char *ac_desc; 115 }; 116 typedef struct au_class_ent au_class_ent_t; 117 118 struct au_user_ent { 119 char *au_name; 120 au_mask_t au_always; 121 au_mask_t au_never; 122 }; 123 typedef struct au_user_ent au_user_ent_t; 124 __END_DECLS 125 126 #define ADD_TO_MASK(m, c, sel) do { \ 127 if (sel & AU_PRS_SUCCESS) \ 128 (m)->am_success |= c; \ 129 if (sel & AU_PRS_FAILURE) \ 130 (m)->am_failure |= c; \ 131 } while (0) 132 133 #define SUB_FROM_MASK(m, c, sel) do { \ 134 if (sel & AU_PRS_SUCCESS) \ 135 (m)->am_success &= ((m)->am_success ^ c); \ 136 if (sel & AU_PRS_FAILURE) \ 137 (m)->am_failure &= ((m)->am_failure ^ c); \ 138 } while (0) 139 140 #define ADDMASK(m, v) do { \ 141 (m)->am_success |= (v)->am_success; \ 142 (m)->am_failure |= (v)->am_failure; \ 143 } while(0) 144 145 #define SUBMASK(m, v) do { \ 146 (m)->am_success &= ((m)->am_success ^ (v)->am_success); \ 147 (m)->am_failure &= ((m)->am_failure ^ (v)->am_failure); \ 148 } while(0) 149 150 __BEGIN_DECLS 151 152 typedef struct au_tid32 { 153 u_int32_t port; 154 u_int32_t addr; 155 } au_tid32_t; 156 157 typedef struct au_tid64 { 158 u_int64_t port; 159 u_int32_t addr; 160 } au_tid64_t; 161 162 typedef struct au_tidaddr32 { 163 u_int32_t port; 164 u_int32_t type; 165 u_int32_t addr[4]; 166 } au_tidaddr32_t; 167 168 typedef struct au_tidaddr64 { 169 u_int64_t port; 170 u_int32_t type; 171 u_int32_t addr[4]; 172 } au_tidaddr64_t; 173 174 /* 175 * argument # 1 byte 176 * argument value 4 bytes/8 bytes (32-bit/64-bit value) 177 * text length 2 bytes 178 * text N bytes + 1 terminating NULL byte 179 */ 180 typedef struct { 181 u_char no; 182 u_int32_t val; 183 u_int16_t len; 184 char *text; 185 } au_arg32_t; 186 187 typedef struct { 188 u_char no; 189 u_int64_t val; 190 u_int16_t len; 191 char *text; 192 } au_arg64_t; 193 194 /* 195 * how to print 1 byte 196 * basic unit 1 byte 197 * unit count 1 byte 198 * data items (depends on basic unit) 199 */ 200 typedef struct { 201 u_char howtopr; 202 u_char bu; 203 u_char uc; 204 u_char *data; 205 } au_arb_t; 206 207 /* 208 * file access mode 4 bytes 209 * owner user ID 4 bytes 210 * owner group ID 4 bytes 211 * file system ID 4 bytes 212 * node ID 8 bytes 213 * device 4 bytes/8 bytes (32-bit/64-bit) 214 */ 215 typedef struct { 216 u_int32_t mode; 217 u_int32_t uid; 218 u_int32_t gid; 219 u_int32_t fsid; 220 u_int64_t nid; 221 u_int32_t dev; 222 } au_attr32_t; 223 224 typedef struct { 225 u_int32_t mode; 226 u_int32_t uid; 227 u_int32_t gid; 228 u_int32_t fsid; 229 u_int64_t nid; 230 u_int64_t dev; 231 } au_attr64_t; 232 233 /* 234 * count 4 bytes 235 * text count null-terminated string(s) 236 */ 237 typedef struct { 238 u_int32_t count; 239 char *text[AUDIT_MAX_ARGS]; 240 } au_execarg_t; 241 242 /* 243 * count 4 bytes 244 * text count null-terminated string(s) 245 */ 246 typedef struct { 247 u_int32_t count; 248 char *text[AUDIT_MAX_ENV]; 249 } au_execenv_t; 250 251 /* 252 * status 4 bytes 253 * return value 4 bytes 254 */ 255 typedef struct { 256 u_int32_t status; 257 u_int32_t ret; 258 } au_exit_t; 259 260 /* 261 * seconds of time 4 bytes 262 * milliseconds of time 4 bytes 263 * file name length 2 bytes 264 * file pathname N bytes + 1 terminating NULL byte 265 */ 266 typedef struct { 267 u_int32_t s; 268 u_int32_t ms; 269 u_int16_t len; 270 char *name; 271 } au_file_t; 272 273 274 /* 275 * number groups 2 bytes 276 * group list N * 4 bytes 277 */ 278 typedef struct { 279 u_int16_t no; 280 u_int32_t list[AUDIT_MAX_GROUPS]; 281 } au_groups_t; 282 283 /* 284 * record byte count 4 bytes 285 * version # 1 byte [2] 286 * event type 2 bytes 287 * event modifier 2 bytes 288 * seconds of time 4 bytes/8 bytes (32-bit/64-bit value) 289 * milliseconds of time 4 bytes/8 bytes (32-bit/64-bit value) 290 */ 291 typedef struct { 292 u_int32_t size; 293 u_char version; 294 u_int16_t e_type; 295 u_int16_t e_mod; 296 u_int32_t s; 297 u_int32_t ms; 298 } au_header32_t; 299 300 /* 301 * record byte count 4 bytes 302 * version # 1 byte [2] 303 * event type 2 bytes 304 * event modifier 2 bytes 305 * address type/length 1 byte (XXX: actually, 4 bytes) 306 * machine address 4 bytes/16 bytes (IPv4/IPv6 address) 307 * seconds of time 4 bytes/8 bytes (32/64-bits) 308 * nanoseconds of time 4 bytes/8 bytes (32/64-bits) 309 */ 310 typedef struct { 311 u_int32_t size; 312 u_char version; 313 u_int16_t e_type; 314 u_int16_t e_mod; 315 u_int32_t ad_type; 316 u_int32_t addr[4]; 317 u_int32_t s; 318 u_int32_t ms; 319 } au_header32_ex_t; 320 321 typedef struct { 322 u_int32_t size; 323 u_char version; 324 u_int16_t e_type; 325 u_int16_t e_mod; 326 u_int64_t s; 327 u_int64_t ms; 328 } au_header64_t; 329 330 typedef struct { 331 u_int32_t size; 332 u_char version; 333 u_int16_t e_type; 334 u_int16_t e_mod; 335 u_int32_t ad_type; 336 u_int32_t addr[4]; 337 u_int64_t s; 338 u_int64_t ms; 339 } au_header64_ex_t; 340 341 /* 342 * internet address 4 bytes 343 */ 344 typedef struct { 345 u_int32_t addr; 346 } au_inaddr_t; 347 348 /* 349 * type 4 bytes 350 * internet address 16 bytes 351 */ 352 typedef struct { 353 u_int32_t type; 354 u_int32_t addr[4]; 355 } au_inaddr_ex_t; 356 357 /* 358 * version and ihl 1 byte 359 * type of service 1 byte 360 * length 2 bytes 361 * id 2 bytes 362 * offset 2 bytes 363 * ttl 1 byte 364 * protocol 1 byte 365 * checksum 2 bytes 366 * source address 4 bytes 367 * destination address 4 bytes 368 */ 369 typedef struct { 370 u_char version; 371 u_char tos; 372 u_int16_t len; 373 u_int16_t id; 374 u_int16_t offset; 375 u_char ttl; 376 u_char prot; 377 u_int16_t chksm; 378 u_int32_t src; 379 u_int32_t dest; 380 } au_ip_t; 381 382 /* 383 * object ID type 1 byte 384 * object ID 4 bytes 385 */ 386 typedef struct { 387 u_char type; 388 u_int32_t id; 389 } au_ipc_t; 390 391 /* 392 * owner user ID 4 bytes 393 * owner group ID 4 bytes 394 * creator user ID 4 bytes 395 * creator group ID 4 bytes 396 * access mode 4 bytes 397 * slot sequence # 4 bytes 398 * key 4 bytes 399 */ 400 typedef struct { 401 u_int32_t uid; 402 u_int32_t gid; 403 u_int32_t puid; 404 u_int32_t pgid; 405 u_int32_t mode; 406 u_int32_t seq; 407 u_int32_t key; 408 } au_ipcperm_t; 409 410 /* 411 * port IP address 2 bytes 412 */ 413 typedef struct { 414 u_int16_t port; 415 } au_iport_t; 416 417 /* 418 * length 2 bytes 419 * data length bytes 420 */ 421 typedef struct { 422 u_int16_t size; 423 char *data; 424 } au_opaque_t; 425 426 /* 427 * path length 2 bytes 428 * path N bytes + 1 terminating NULL byte 429 */ 430 typedef struct { 431 u_int16_t len; 432 char *path; 433 } au_path_t; 434 435 /* 436 * audit ID 4 bytes 437 * effective user ID 4 bytes 438 * effective group ID 4 bytes 439 * real user ID 4 bytes 440 * real group ID 4 bytes 441 * process ID 4 bytes 442 * session ID 4 bytes 443 * terminal ID 444 * port ID 4 bytes/8 bytes (32-bit/64-bit value) 445 * machine address 4 bytes 446 */ 447 typedef struct { 448 u_int32_t auid; 449 u_int32_t euid; 450 u_int32_t egid; 451 u_int32_t ruid; 452 u_int32_t rgid; 453 u_int32_t pid; 454 u_int32_t sid; 455 au_tid32_t tid; 456 } au_proc32_t; 457 458 typedef struct { 459 u_int32_t auid; 460 u_int32_t euid; 461 u_int32_t egid; 462 u_int32_t ruid; 463 u_int32_t rgid; 464 u_int32_t pid; 465 u_int32_t sid; 466 au_tid64_t tid; 467 } au_proc64_t; 468 469 /* 470 * audit ID 4 bytes 471 * effective user ID 4 bytes 472 * effective group ID 4 bytes 473 * real user ID 4 bytes 474 * real group ID 4 bytes 475 * process ID 4 bytes 476 * session ID 4 bytes 477 * terminal ID 478 * port ID 4 bytes/8 bytes (32-bit/64-bit value) 479 * type 4 bytes 480 * machine address 16 bytes 481 */ 482 typedef struct { 483 u_int32_t auid; 484 u_int32_t euid; 485 u_int32_t egid; 486 u_int32_t ruid; 487 u_int32_t rgid; 488 u_int32_t pid; 489 u_int32_t sid; 490 au_tidaddr32_t tid; 491 } au_proc32ex_t; 492 493 typedef struct { 494 u_int32_t auid; 495 u_int32_t euid; 496 u_int32_t egid; 497 u_int32_t ruid; 498 u_int32_t rgid; 499 u_int32_t pid; 500 u_int32_t sid; 501 au_tidaddr64_t tid; 502 } au_proc64ex_t; 503 504 /* 505 * error status 1 byte 506 * return value 4 bytes/8 bytes (32-bit/64-bit value) 507 */ 508 typedef struct { 509 u_char status; 510 u_int32_t ret; 511 } au_ret32_t; 512 513 typedef struct { 514 u_char err; 515 u_int64_t val; 516 } au_ret64_t; 517 518 /* 519 * sequence number 4 bytes 520 */ 521 typedef struct { 522 u_int32_t seqno; 523 } au_seq_t; 524 525 /* 526 * socket type 2 bytes 527 * local port 2 bytes 528 * local Internet address 4 bytes 529 * remote port 2 bytes 530 * remote Internet address 4 bytes 531 */ 532 typedef struct { 533 u_int16_t type; 534 u_int16_t l_port; 535 u_int32_t l_addr; 536 u_int16_t r_port; 537 u_int32_t r_addr; 538 } au_socket_t; 539 540 /* 541 * socket type 2 bytes 542 * local port 2 bytes 543 * address type/length 4 bytes 544 * local Internet address 4 bytes/16 bytes (IPv4/IPv6 address) 545 * remote port 4 bytes 546 * address type/length 4 bytes 547 * remote Internet address 4 bytes/16 bytes (IPv4/IPv6 address) 548 */ 549 typedef struct { 550 u_int16_t type; 551 u_int16_t l_port; 552 u_int32_t l_ad_type; 553 u_int32_t l_addr; 554 u_int32_t r_port; 555 u_int32_t r_ad_type; 556 u_int32_t r_addr; 557 } au_socket_ex32_t; 558 559 /* 560 * socket family 2 bytes 561 * local port 2 bytes 562 * socket address 4 bytes/16 bytes (IPv4/IPv6 address) 563 */ 564 typedef struct { 565 u_int16_t family; 566 u_int16_t port; 567 u_int32_t addr; 568 } au_socketinet32_t; 569 570 /* 571 * socket family 2 bytes 572 * path 104 bytes 573 */ 574 typedef struct { 575 u_int16_t family; 576 char path[104]; 577 } au_socketunix_t; 578 579 /* 580 * audit ID 4 bytes 581 * effective user ID 4 bytes 582 * effective group ID 4 bytes 583 * real user ID 4 bytes 584 * real group ID 4 bytes 585 * process ID 4 bytes 586 * session ID 4 bytes 587 * terminal ID 588 * port ID 4 bytes/8 bytes (32-bit/64-bit value) 589 * machine address 4 bytes 590 */ 591 typedef struct { 592 u_int32_t auid; 593 u_int32_t euid; 594 u_int32_t egid; 595 u_int32_t ruid; 596 u_int32_t rgid; 597 u_int32_t pid; 598 u_int32_t sid; 599 au_tid32_t tid; 600 } au_subject32_t; 601 602 typedef struct { 603 u_int32_t auid; 604 u_int32_t euid; 605 u_int32_t egid; 606 u_int32_t ruid; 607 u_int32_t rgid; 608 u_int32_t pid; 609 u_int32_t sid; 610 au_tid64_t tid; 611 } au_subject64_t; 612 613 /* 614 * audit ID 4 bytes 615 * effective user ID 4 bytes 616 * effective group ID 4 bytes 617 * real user ID 4 bytes 618 * real group ID 4 bytes 619 * process ID 4 bytes 620 * session ID 4 bytes 621 * terminal ID 622 * port ID 4 bytes/8 bytes (32-bit/64-bit value) 623 * type 4 bytes 624 * machine address 16 bytes 625 */ 626 typedef struct { 627 u_int32_t auid; 628 u_int32_t euid; 629 u_int32_t egid; 630 u_int32_t ruid; 631 u_int32_t rgid; 632 u_int32_t pid; 633 u_int32_t sid; 634 au_tidaddr32_t tid; 635 } au_subject32ex_t; 636 637 typedef struct { 638 u_int32_t auid; 639 u_int32_t euid; 640 u_int32_t egid; 641 u_int32_t ruid; 642 u_int32_t rgid; 643 u_int32_t pid; 644 u_int32_t sid; 645 au_tidaddr64_t tid; 646 } au_subject64ex_t; 647 648 /* 649 * text length 2 bytes 650 * text N bytes + 1 terminating NULL byte 651 */ 652 typedef struct { 653 u_int16_t len; 654 char *text; 655 } au_text_t; 656 657 /* 658 * zonename length 2 bytes 659 * zonename text N bytes + 1 NULL terminator 660 */ 661 typedef struct { 662 u_int16_t len; 663 char *zonename; 664 } au_zonename_t; 665 666 typedef struct { 667 u_int32_t ident; 668 u_int16_t filter; 669 u_int16_t flags; 670 u_int32_t fflags; 671 u_int32_t data; 672 } au_kevent_t; 673 674 typedef struct { 675 u_int16_t length; 676 char *data; 677 } au_invalid_t; 678 679 /* 680 * trailer magic number 2 bytes 681 * record byte count 4 bytes 682 */ 683 typedef struct { 684 u_int16_t magic; 685 u_int32_t count; 686 } au_trailer_t; 687 688 struct tokenstr { 689 u_char id; 690 u_char *data; 691 size_t len; 692 union { 693 au_arg32_t arg32; 694 au_arg64_t arg64; 695 au_arb_t arb; 696 au_attr32_t attr32; 697 au_attr64_t attr64; 698 au_execarg_t execarg; 699 au_execenv_t execenv; 700 au_exit_t exit; 701 au_file_t file; 702 au_groups_t grps; 703 au_header32_t hdr32; 704 au_header32_ex_t hdr32_ex; 705 au_header64_t hdr64; 706 au_header64_ex_t hdr64_ex; 707 au_inaddr_t inaddr; 708 au_inaddr_ex_t inaddr_ex; 709 au_ip_t ip; 710 au_ipc_t ipc; 711 au_ipcperm_t ipcperm; 712 au_iport_t iport; 713 au_opaque_t opaque; 714 au_path_t path; 715 au_proc32_t proc32; 716 au_proc32ex_t proc32_ex; 717 au_proc64_t proc64; 718 au_proc64ex_t proc64_ex; 719 au_ret32_t ret32; 720 au_ret64_t ret64; 721 au_seq_t seq; 722 au_socket_t socket; 723 au_socket_ex32_t socket_ex32; 724 au_socketinet32_t sockinet32; 725 au_socketunix_t sockunix; 726 au_subject32_t subj32; 727 au_subject32ex_t subj32_ex; 728 au_subject64_t subj64; 729 au_subject64ex_t subj64_ex; 730 au_text_t text; 731 au_kevent_t kevent; 732 au_invalid_t invalid; 733 au_trailer_t trail; 734 au_zonename_t zonename; 735 } tt; /* The token is one of the above types */ 736 }; 737 738 typedef struct tokenstr tokenstr_t; 739 740 int audit_submit(short au_event, au_id_t auid, 741 char status, int reterr, const char *fmt, ...); 742 743 /* 744 * Functions relating to querying audit class information. 745 */ 746 void setauclass(void); 747 void endauclass(void); 748 struct au_class_ent *getauclassent(void); 749 struct au_class_ent *getauclassent_r(au_class_ent_t *class_int); 750 struct au_class_ent *getauclassnam(const char *name); 751 struct au_class_ent *getauclassnam_r(au_class_ent_t *class_int, 752 const char *name); 753 struct au_class_ent *getauclassnum(au_class_t class_number); 754 struct au_class_ent *getauclassnum_r(au_class_ent_t *class_int, 755 au_class_t class_number); 756 757 /* 758 * Functions relating to querying audit control information. 759 */ 760 void setac(void); 761 void endac(void); 762 int getacdir(char *name, int len); 763 int getacmin(int *min_val); 764 int getacfilesz(size_t *size_val); 765 int getacflg(char *auditstr, int len); 766 int getacna(char *auditstr, int len); 767 int getacpol(char *auditstr, size_t len); 768 int getachost(char *auditstr, size_t len); 769 int getauditflagsbin(char *auditstr, au_mask_t *masks); 770 int getauditflagschar(char *auditstr, au_mask_t *masks, 771 int verbose); 772 int au_preselect(au_event_t event, au_mask_t *mask_p, 773 int sorf, int flag); 774 ssize_t au_poltostr(long policy, size_t maxsize, char *buf); 775 int au_strtopol(const char *polstr, long *policy); 776 777 /* 778 * Functions relating to querying audit event information. 779 */ 780 void setauevent(void); 781 void endauevent(void); 782 struct au_event_ent *getauevent(void); 783 struct au_event_ent *getauevent_r(struct au_event_ent *e); 784 struct au_event_ent *getauevnam(const char *name); 785 struct au_event_ent *getauevnam_r(struct au_event_ent *e, 786 const char *name); 787 struct au_event_ent *getauevnum(au_event_t event_number); 788 struct au_event_ent *getauevnum_r(struct au_event_ent *e, 789 au_event_t event_number); 790 au_event_t *getauevnonam(const char *event_name); 791 au_event_t *getauevnonam_r(au_event_t *ev, 792 const char *event_name); 793 794 /* 795 * Functions relating to querying audit user information. 796 */ 797 void setauuser(void); 798 void endauuser(void); 799 struct au_user_ent *getauuserent(void); 800 struct au_user_ent *getauuserent_r(struct au_user_ent *u); 801 struct au_user_ent *getauusernam(const char *name); 802 struct au_user_ent *getauusernam_r(struct au_user_ent *u, 803 const char *name); 804 int au_user_mask(char *username, au_mask_t *mask_p); 805 int getfauditflags(au_mask_t *usremask, 806 au_mask_t *usrdmask, au_mask_t *lastmask); 807 808 /* 809 * Functions for reading and printing records and tokens from audit trails. 810 */ 811 int au_read_rec(FILE *fp, u_char **buf); 812 int au_fetch_tok(tokenstr_t *tok, u_char *buf, int len); 813 //XXX The following interface has different prototype from BSM 814 void au_print_tok(FILE *outfp, tokenstr_t *tok, 815 char *del, char raw, char sfrm); 816 void au_print_tok_xml(FILE *outfp, tokenstr_t *tok, 817 char *del, char raw, char sfrm); 818 819 /* 820 * Functions relating to XML output. 821 */ 822 void au_print_xml_header(FILE *outfp); 823 void au_print_xml_footer(FILE *outfp); 824 __END_DECLS 825 826 /* 827 * The remaining APIs are associated with Apple's BSM implementation, in 828 * particular as relates to Mach IPC auditing and triggers passed via Mach 829 * IPC. 830 */ 831 #ifdef __APPLE__ 832 #include <sys/appleapiopts.h> 833 834 /************************************************************************** 835 ************************************************************************** 836 ** The following definitions, functions, etc., are NOT officially 837 ** supported: they may be changed or removed in the future. Do not use 838 ** them unless you are prepared to cope with that eventuality. 839 ************************************************************************** 840 **************************************************************************/ 841 842 #ifdef __APPLE_API_PRIVATE 843 #define __BSM_INTERNAL_NOTIFY_KEY "com.apple.audit.change" 844 #endif /* __APPLE_API_PRIVATE */ 845 846 /* 847 * au_get_state() return values 848 * XXX use AUC_* values directly instead (<bsm/audit.h>); AUDIT_OFF and 849 * AUDIT_ON are deprecated and WILL be removed. 850 */ 851 #ifdef __APPLE_API_PRIVATE 852 #define AUDIT_OFF AUC_NOAUDIT 853 #define AUDIT_ON AUC_AUDITING 854 #endif /* __APPLE_API_PRIVATE */ 855 #endif /* !__APPLE__ */ 856 857 /* 858 * Error return codes for audit_set_terminal_id(), audit_write() and its 859 * brethren. We have 255 (not including kAUNoErr) to play with. 860 * 861 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 862 */ 863 enum { 864 kAUNoErr = 0, 865 kAUBadParamErr = -66049, 866 kAUStatErr, 867 kAUSysctlErr, 868 kAUOpenErr, 869 kAUMakeSubjectTokErr, 870 kAUWriteSubjectTokErr, 871 kAUWriteCallerTokErr, 872 kAUMakeReturnTokErr, 873 kAUWriteReturnTokErr, 874 kAUCloseErr, 875 kAUMakeTextTokErr, 876 kAULastErr 877 }; 878 879 #ifdef __APPLE__ 880 /* 881 * Error return codes for au_get_state() and/or its private support 882 * functions. These codes are designed to be compatible with the 883 * NOTIFY_STATUS_* codes defined in <notify.h> but non-overlapping. 884 * Any changes to notify(3) may cause these values to change in future. 885 * 886 * AU_UNIMPL should never happen unless you've changed your system software 887 * without rebooting. Shame on you. 888 */ 889 #ifdef __APPLE_API_PRIVATE 890 #define AU_UNIMPL NOTIFY_STATUS_FAILED + 1 /* audit unimplemented */ 891 #endif /* __APPLE_API_PRIVATE */ 892 #endif /* !__APPLE__ */ 893 894 __BEGIN_DECLS 895 /* 896 * XXX This prototype should be in audit_record.h 897 * 898 * au_free_token() 899 * 900 * @summary - au_free_token() deallocates a token_t created by any of 901 * the au_to_*() BSM API functions. 902 * 903 * The BSM API generally manages deallocation of token_t objects. However, 904 * if au_write() is passed a bad audit descriptor, the token_t * parameter 905 * will be left untouched. In that case, the caller can deallocate the 906 * token_t using au_free_token() if desired. This is, in fact, what 907 * audit_write() does, in keeping with the existing memory management model 908 * of the BSM API. 909 * 910 * @param tok - A token_t * generated by one of the au_to_*() BSM API 911 * calls. For convenience, tok may be NULL, in which case 912 * au_free_token() returns immediately. 913 * 914 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 915 */ 916 void au_free_token(token_t *tok); 917 918 /* 919 * Lightweight check to determine if auditing is enabled. If a client 920 * wants to use this to govern whether an entire series of audit calls 921 * should be made--as in the common case of a caller building a set of 922 * tokens, then writing them--it should cache the audit status in a local 923 * variable. This call always returns the current state of auditing. 924 * 925 * @return - AUC_AUDITING or AUC_NOAUDIT if no error occurred. 926 * Otherwise the function can return any of the errno values defined for 927 * setaudit(2), or AU_UNIMPL if audit does not appear to be supported by 928 * the system. 929 * 930 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 931 */ 932 int au_get_state(void); 933 __END_DECLS 934 935 /* OpenSSH compatibility */ 936 int cannot_audit(int); 937 938 __BEGIN_DECLS 939 /* 940 * audit_set_terminal_id() 941 * 942 * @summary - audit_set_terminal_id() fills in an au_tid_t struct, which is 943 * used in audit session initialization by processes like /usr/bin/login. 944 * 945 * @param tid - A pointer to an au_tid_t struct. 946 * 947 * @return - kAUNoErr on success; kAUBadParamErr if tid is NULL, kAUStatErr 948 * or kAUSysctlErr if one of the underlying system calls fails (a message 949 * is sent to the system log in those cases). 950 * 951 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 952 */ 953 int audit_set_terminal_id(au_tid_t *tid); 954 955 /* 956 * BEGIN au_write() WRAPPERS 957 * 958 * The following calls all wrap the existing BSM API. They use the 959 * provided subject information, if any, to construct the subject token 960 * required for every log message. They use the provided return/error 961 * value(s), if any, to construct the success/failure indication required 962 * for every log message. They only permit one "miscellaneous" token, 963 * which should contain the event-specific logging information mandated by 964 * CAPP. 965 * 966 * All these calls assume the caller has previously determined that 967 * auditing is enabled by calling au_get_state(). 968 */ 969 970 /* 971 * audit_write() 972 * 973 * @summary - audit_write() is the basis for the other audit_write_*() 974 * calls. Performs a basic write of an audit record (subject, additional 975 * info, success/failure). Note that this call only permits logging one 976 * caller-specified token; clients needing to log more flexibly must use 977 * the existing BSM API (au_open(), et al.) directly. 978 * 979 * Note on memory management: audit_write() guarantees that the token_t *s 980 * passed to it will be deallocated whether or not the underlying write to 981 * the audit log succeeded. This addresses an inconsistency in the 982 * underlying BSM API in which token_t *s are usually but not always 983 * deallocated. 984 * 985 * @param event_code - The code for the event being logged. This should 986 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 987 * 988 * @param subject - A token_t * generated by au_to_subject(), 989 * au_to_subject32(), au_to_subject64(), or au_to_me(). If no subject is 990 * required, subject should be NULL. 991 * 992 * @param misctok - A token_t * generated by one of the au_to_*() BSM API 993 * calls. This should correspond to the additional information required by 994 * CAPP for the event being audited. If no additional information is 995 * required, misctok should be NULL. 996 * 997 * @param retval - The return value to be logged for this event. This 998 * should be 0 (zero) for success, otherwise the value is event-specific. 999 * 1000 * @param errcode - Any error code associated with the return value (e.g., 1001 * errno or h_errno). If there was no error, errcode should be 0 (zero). 1002 * 1003 * @return - The status of the call: 0 (zero) on success, else one of the 1004 * kAU*Err values defined above. 1005 * 1006 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1007 */ 1008 int audit_write(short event_code, token_t *subject, token_t *misctok, 1009 char retval, int errcode); 1010 1011 /* 1012 * audit_write_success() 1013 * 1014 * @summary - audit_write_success() records an auditable event that did not 1015 * encounter an error. The interface is designed to require as little 1016 * direct use of the au_to_*() API as possible. It builds a subject token 1017 * from the information passed in and uses that to invoke audit_write(). 1018 * A subject, as defined by CAPP, is a process acting on the user's behalf. 1019 * 1020 * If the subject information is the same as the current process, use 1021 * au_write_success_self(). 1022 * 1023 * @param event_code - The code for the event being logged. This should 1024 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 1025 * 1026 * @param misctok - A token_t * generated by one of the au_to_*() BSM API 1027 * calls. This should correspond to the additional information required by 1028 * CAPP for the event being audited. If no additional information is 1029 * required, misctok should be NULL. 1030 * 1031 * @param auid - The subject's audit ID. 1032 * 1033 * @param euid - The subject's effective user ID. 1034 * 1035 * @param egid - The subject's effective group ID. 1036 * 1037 * @param ruid - The subject's real user ID. 1038 * 1039 * @param rgid - The subject's real group ID. 1040 * 1041 * @param pid - The subject's process ID. 1042 * 1043 * @param sid - The subject's session ID. 1044 * 1045 * @param tid - The subject's terminal ID. 1046 * 1047 * @return - The status of the call: 0 (zero) on success, else one of the 1048 * kAU*Err values defined above. 1049 * 1050 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1051 */ 1052 int audit_write_success(short event_code, token_t *misctok, au_id_t auid, 1053 uid_t euid, gid_t egid, uid_t ruid, gid_t rgid, pid_t pid, 1054 au_asid_t sid, au_tid_t *tid); 1055 1056 /* 1057 * audit_write_success_self() 1058 * 1059 * @summary - Similar to audit_write_success(), but used when the subject 1060 * (process) is owned and operated by the auditable user him/herself. 1061 * 1062 * @param event_code - The code for the event being logged. This should 1063 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 1064 * 1065 * @param misctok - A token_t * generated by one of the au_to_*() BSM API 1066 * calls. This should correspond to the additional information required by 1067 * CAPP for the event being audited. If no additional information is 1068 * required, misctok should be NULL. 1069 * 1070 * @return - The status of the call: 0 (zero) on success, else one of the 1071 * kAU*Err values defined above. 1072 * 1073 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1074 */ 1075 int audit_write_success_self(short event_code, token_t *misctok); 1076 1077 /* 1078 * audit_write_failure() 1079 * 1080 * @summary - audit_write_failure() records an auditable event that 1081 * encountered an error. The interface is designed to require as little 1082 * direct use of the au_to_*() API as possible. It builds a subject token 1083 * from the information passed in and uses that to invoke audit_write(). 1084 * A subject, as defined by CAPP, is a process acting on the user's behalf. 1085 * 1086 * If the subject information is the same as the current process, use 1087 * au_write_failure_self(). 1088 * 1089 * @param event_code - The code for the event being logged. This should 1090 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 1091 * 1092 * @param errmsg - A text message providing additional information about 1093 * the event being audited. 1094 * 1095 * @param errret - A numerical value providing additional information about 1096 * the error. This is intended to store the value of errno or h_errno if 1097 * it's relevant. This can be 0 (zero) if no additional information is 1098 * available. 1099 * 1100 * @param auid - The subject's audit ID. 1101 * 1102 * @param euid - The subject's effective user ID. 1103 * 1104 * @param egid - The subject's effective group ID. 1105 * 1106 * @param ruid - The subject's real user ID. 1107 * 1108 * @param rgid - The subject's real group ID. 1109 * 1110 * @param pid - The subject's process ID. 1111 * 1112 * @param sid - The subject's session ID. 1113 * 1114 * @param tid - The subject's terminal ID. 1115 * 1116 * @return - The status of the call: 0 (zero) on success, else one of the 1117 * kAU*Err values defined above. 1118 * 1119 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1120 */ 1121 int audit_write_failure(short event_code, char *errmsg, int errret, 1122 au_id_t auid, uid_t euid, gid_t egid, uid_t ruid, gid_t rgid, 1123 pid_t pid, au_asid_t sid, au_tid_t *tid); 1124 1125 /* 1126 * audit_write_failure_self() 1127 * 1128 * @summary - Similar to audit_write_failure(), but used when the subject 1129 * (process) is owned and operated by the auditable user him/herself. 1130 * 1131 * @param event_code - The code for the event being logged. This should 1132 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 1133 * 1134 * @param errmsg - A text message providing additional information about 1135 * the event being audited. 1136 * 1137 * @param errret - A numerical value providing additional information about 1138 * the error. This is intended to store the value of errno or h_errno if 1139 * it's relevant. This can be 0 (zero) if no additional information is 1140 * available. 1141 * 1142 * @return - The status of the call: 0 (zero) on success, else one of the 1143 * kAU*Err values defined above. 1144 * 1145 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1146 */ 1147 int audit_write_failure_self(short event_code, char *errmsg, int errret); 1148 1149 /* 1150 * audit_write_failure_na() 1151 * 1152 * @summary - audit_write_failure_na() records errors during login. Such 1153 * errors are implicitly non-attributable (i.e., not ascribable to any user). 1154 * 1155 * @param event_code - The code for the event being logged. This should 1156 * be one of the AUE_ values in /usr/include/bsm/audit_uevents.h. 1157 * 1158 * @param errmsg - A text message providing additional information about 1159 * the event being audited. 1160 * 1161 * @param errret - A numerical value providing additional information about 1162 * the error. This is intended to store the value of errno or h_errno if 1163 * it's relevant. This can be 0 (zero) if no additional information is 1164 * available. 1165 * 1166 * @param euid - The subject's effective user ID. 1167 * 1168 * @param egid - The subject's effective group ID. 1169 * 1170 * @param pid - The subject's process ID. 1171 * 1172 * @param tid - The subject's terminal ID. 1173 * 1174 * @return - The status of the call: 0 (zero) on success, else one of the 1175 * kAU*Err values defined above. 1176 * 1177 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1178 */ 1179 int audit_write_failure_na(short event_code, char *errmsg, int errret, 1180 uid_t euid, gid_t egid, pid_t pid, au_tid_t *tid); 1181 1182 /* END au_write() WRAPPERS */ 1183 1184 #ifdef __APPLE__ 1185 /* 1186 * audit_token_to_au32() 1187 * 1188 * @summary - Extract information from an audit_token_t, used to identify 1189 * Mach tasks and senders of Mach messages as subjects to the audit system. 1190 * audit_tokent_to_au32() is the only method that should be used to parse 1191 * an audit_token_t, since its internal representation may change over 1192 * time. A pointer parameter may be NULL if that information is not 1193 * needed. 1194 * 1195 * @param atoken - the audit token containing the desired information 1196 * 1197 * @param auidp - Pointer to a uid_t; on return will be set to the task or 1198 * sender's audit user ID 1199 * 1200 * @param euidp - Pointer to a uid_t; on return will be set to the task or 1201 * sender's effective user ID 1202 * 1203 * @param egidp - Pointer to a gid_t; on return will be set to the task or 1204 * sender's effective group ID 1205 * 1206 * @param ruidp - Pointer to a uid_t; on return will be set to the task or 1207 * sender's real user ID 1208 * 1209 * @param rgidp - Pointer to a gid_t; on return will be set to the task or 1210 * sender's real group ID 1211 * 1212 * @param pidp - Pointer to a pid_t; on return will be set to the task or 1213 * sender's process ID 1214 * 1215 * @param asidp - Pointer to an au_asid_t; on return will be set to the 1216 * task or sender's audit session ID 1217 * 1218 * @param tidp - Pointer to an au_tid_t; on return will be set to the task 1219 * or sender's terminal ID 1220 * 1221 * XXXRW: In Apple's bsm-8, these are marked __APPLE_API_PRIVATE. 1222 */ 1223 void audit_token_to_au32( 1224 audit_token_t atoken, 1225 uid_t *auidp, 1226 uid_t *euidp, 1227 gid_t *egidp, 1228 uid_t *ruidp, 1229 gid_t *rgidp, 1230 pid_t *pidp, 1231 au_asid_t *asidp, 1232 au_tid_t *tidp); 1233 #endif /* !__APPLE__ */ 1234 1235 __END_DECLS 1236 1237 #endif /* !_LIBBSM_H_ */ 1238