-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add/enhance audit support on AIX #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ajaykini
wants to merge
2
commits into
openssh:master
Choose a base branch
from
ajaykini:aix_audit_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,278 @@ | ||
| /* | ||
| * Copyright (c) 2026 IBM Ajay Kini. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * 1. Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
| * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
| * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include "includes.h" | ||
|
|
||
| #include <errno.h> | ||
| #include <stdarg.h> | ||
| #include <string.h> | ||
| #include <unistd.h> | ||
| #include <pwd.h> | ||
|
|
||
| #ifdef USE_AIX_AUDIT | ||
| # include <sys/audit.h> | ||
| # include <usersec.h> | ||
|
|
||
|
|
||
| #include "audit.h" | ||
| #include "log.h" | ||
| #include "hostfile.h" | ||
|
|
||
|
|
||
| const char * | ||
| audit_event_lookup(ssh_audit_event_t ev) | ||
| { | ||
| int i; | ||
| static struct event_lookup_struct { | ||
| ssh_audit_event_t event; | ||
| const char *name; | ||
| } event_lookup[] = { | ||
| /* These envent names comply with AIX audit requirements */ | ||
| {SSH_LOGIN_EXCEED_MAXTRIES, "SSH_exceedmtrix"}, | ||
| {SSH_LOGIN_ROOT_DENIED, "SSH_rootdned"}, | ||
| {SSH_AUTH_SUCCESS, "SSH_authsuccess"}, | ||
| {SSH_AUTH_FAIL_NONE, "SSH_failnone"}, | ||
| {SSH_AUTH_FAIL_PASSWD, "SSH_failpasswd"}, | ||
| {SSH_AUTH_FAIL_KBDINT, "SSH_failkbdint"}, | ||
| {SSH_AUTH_FAIL_PUBKEY, "SSH_failpubkey"}, | ||
| {SSH_AUTH_FAIL_HOSTBASED, "SSH_failhstbsd"}, | ||
| {SSH_AUTH_FAIL_GSSAPI, "SSH_failgssapi"}, | ||
| {SSH_INVALID_USER, "SSH_invldusr"}, | ||
| {SSH_NOLOGIN, "SSH_nologin"}, | ||
| {SSH_CONNECTION_CLOSE, "SSH_connclose"}, | ||
| {SSH_CONNECTION_ABANDON, "SSH_connabndn"}, | ||
| {SSH_BAD_PCKT, "SSH_badpckt"}, | ||
| {SSH_CIPHER_NO_MATCH, "SSH_cipmismatch"}, | ||
| {SSH_SESSION_OPEN, "SSH_sessionopn"}, | ||
| {SSH_AUDIT_UNKNOWN, "SSH_auditknwn"} | ||
| }; | ||
|
|
||
| for (i = 0; event_lookup[i].event != SSH_AUDIT_UNKNOWN; i++) | ||
| if (event_lookup[i].event == ev) | ||
| break; | ||
| return (event_lookup[i].name); | ||
| } | ||
|
|
||
| /* | ||
| * Called after a connection has been accepted but before any authentication | ||
| * has been attempted. | ||
| */ | ||
| void | ||
| audit_connection_from(const char *host, int port) | ||
| { | ||
| debug("audit connection from %s port %d euid %d", host, port, | ||
| (int)geteuid()); | ||
| } | ||
|
|
||
| /* | ||
| * Called when various events occur (see audit.h for a list of possible | ||
| * events and what they mean). | ||
| */ | ||
| void | ||
| audit_event(struct ssh *ssh, ssh_audit_event_t event) | ||
| { | ||
| char buf[1024]; | ||
| const char *username; | ||
| const char *event_name; | ||
| const char *remote_ip; | ||
| int ret; | ||
| uid_t auth_uid; /* UID of the user attempting authentication */ | ||
| int res = 0; /* AIX audit result code: 0 = success */ | ||
|
|
||
| /* Debug: Log that audit_event was called */ | ||
| debug("audit_event: called with event=%d", event); | ||
|
|
||
| /* Get username from authctxt */ | ||
| username = audit_username(); | ||
|
|
||
| /* Get the UID of the authenticating user */ | ||
| auth_uid = (uid_t)-1; /* Default to -1 if user doesn't exist */ | ||
|
|
||
| /* On AIX, use getuserattr to get UID from username */ | ||
| if (username != NULL ) { | ||
| int uid_val; | ||
| if (getuserattr(username, S_ID, &uid_val, SEC_INT) == 0) { | ||
| auth_uid = (uid_t)uid_val; | ||
| } | ||
| } | ||
|
|
||
| event_name = audit_event_lookup(event); | ||
|
|
||
| /* Set audit result code based on event type */ | ||
| switch (event) { | ||
| case SSH_AUTH_FAIL_NONE: | ||
| case SSH_AUTH_FAIL_PASSWD: | ||
| case SSH_AUTH_FAIL_KBDINT: | ||
| case SSH_AUTH_FAIL_PUBKEY: | ||
| case SSH_AUTH_FAIL_HOSTBASED: | ||
| case SSH_AUTH_FAIL_GSSAPI: | ||
| case SSH_INVALID_USER: | ||
| case SSH_BAD_PCKT: | ||
| case SSH_CIPHER_NO_MATCH: | ||
| res = 1; /* Failure */ | ||
| break; | ||
| default: | ||
| res = 0; /* Success */ | ||
| break; | ||
| } | ||
|
|
||
| /* Handle NULL ssh pointer gracefully */ | ||
| if (ssh != NULL) { | ||
| remote_ip = ssh_remote_ipaddr(ssh); | ||
| } else { | ||
| remote_ip = "(unknown)"; | ||
| } | ||
|
|
||
| /* Build audit message with proper error checking */ | ||
| if (auth_uid == (uid_t)-1) { | ||
| ret = snprintf(buf, sizeof(buf), | ||
| "audit event for user %s event %d (%s) remote ip (%s)", | ||
| username, event, event_name, remote_ip); | ||
| } else { | ||
| ret = snprintf(buf, sizeof(buf), | ||
| "audit event auth_uid %u user %s event %d (%s) remote ip (%s)", | ||
| (unsigned int)auth_uid, username, event, event_name, remote_ip); | ||
| } | ||
|
|
||
| /* Check for truncation */ | ||
| if (ret < 0 || (size_t)ret >= sizeof(buf)) { | ||
| debug("audit_event: message truncated (needed %d bytes)", ret); | ||
| } | ||
|
|
||
| /* Log to debug output */ | ||
| if (auth_uid == (uid_t)-1) { | ||
| debug("audit event auth_uid -1 user %s event %d (%s) remote ip (%s)", | ||
| username, event, event_name, remote_ip); | ||
| } else { | ||
| debug("audit event auth_uid %u user %s event %d (%s) remote ip (%s)", | ||
| (unsigned int)auth_uid, username, event, event_name, remote_ip); | ||
| } | ||
|
|
||
| /* Write to AIX audit subsystem with required format */ | ||
| if (auditwrite(event_name, res, buf, strlen(buf) + 1, 0) < 0) { | ||
| error("auditwrite failed for event %s: %s", | ||
| event_name, strerror(errno)); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Called when a user session is started. Argument is the tty allocated to | ||
| * the session, or NULL if no tty was allocated. | ||
| * | ||
| * Note that this may be called multiple times if multiple sessions are used | ||
| * within a single connection. | ||
| */ | ||
| void | ||
| audit_session_open(struct logininfo *li) | ||
| { | ||
| const char *t = li->line ? li->line : "(no tty)"; | ||
| const char *username; | ||
| const char *hostname; | ||
| const char *event_name; | ||
| char buf[1024]; | ||
| int ret; | ||
| uid_t auth_uid; | ||
|
|
||
| int res = 0; /* AIX audit result code: 0 = success for session open */ | ||
|
|
||
| /* Use username from logininfo if available, otherwise from authctxt */ | ||
| if (li->username[0] != '\0') { | ||
| username = li->username; | ||
| } else { | ||
| username = audit_username(); | ||
| } | ||
|
|
||
| /* Use hostname from logininfo if available */ | ||
| if (li->hostname[0] != '\0') { | ||
| hostname = li->hostname; | ||
| } else { | ||
| hostname = "(unknown)"; | ||
| } | ||
|
|
||
| /* Get the UID from logininfo */ | ||
| auth_uid = li->uid; | ||
|
|
||
| event_name = audit_event_lookup(SSH_SESSION_OPEN); | ||
|
|
||
| /* Build audit message with logininfo details */ | ||
| if (auth_uid == (uid_t)-1) { | ||
| ret = snprintf(buf, sizeof(buf), | ||
| "audit session open auth_uid -1 user %s tty %s hostname %s pid %ld", | ||
| username, t, hostname, (long)li->pid); | ||
| } else { | ||
| ret = snprintf(buf, sizeof(buf), | ||
| "audit session open auth_uid %u user %s tty %s hostname %s pid %ld", | ||
| (unsigned int)auth_uid, username, t, hostname, (long)li->pid); | ||
| } | ||
|
|
||
| /* Check for truncation */ | ||
| if (ret < 0 || (size_t)ret >= sizeof(buf)) { | ||
| debug("audit_session_open: message truncated (needed %d bytes)", ret); | ||
| } | ||
|
|
||
| /* Log to debug output */ | ||
| if (auth_uid == (uid_t)-1) { | ||
| debug("audit session open auth_uid -1 user %s tty %s hostname %s pid %ld", | ||
| username, t, hostname, (long)li->pid); | ||
| } else { | ||
| debug("audit session open auth_uid %u user %s tty %s hostname %s pid %ld", | ||
| (unsigned int)auth_uid, username, t, hostname, (long)li->pid); | ||
| } | ||
|
|
||
| /* Write to AIX audit subsystem */ | ||
| if (auditwrite(event_name, res, buf, strlen(buf) + 1, 0) < 0) { | ||
| error("auditwrite failed for event %s: %s", | ||
| event_name, strerror(errno)); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Called when a user session is closed. Argument is the tty allocated to | ||
| * the session, or NULL if no tty was allocated. | ||
| * | ||
| * Note that this may be called multiple times if multiple sessions are used | ||
| * within a single connection. | ||
| */ | ||
| void | ||
| audit_session_close(struct logininfo *li) | ||
| { | ||
| const char *t = li->line ? li->line : "(no tty)"; | ||
|
|
||
| debug("audit session close euid %d user %s tty name %s", geteuid(), | ||
| audit_username(), t); | ||
| } | ||
|
|
||
| /* | ||
| * This will be called when a user runs a non-interactive command. Note that | ||
| * it may be called multiple times for a single connection since SSH2 allows | ||
| * multiple sessions within a single connection. | ||
| */ | ||
| void | ||
| audit_run_command(const char *command) | ||
| { | ||
| debug("audit run command euid %d user %s command '%.200s'", geteuid(), | ||
| audit_username(), command); | ||
| } | ||
|
|
||
| #endif /* USE_AIX_AUDIT */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.