|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright 1988-2002 Sun Microsystems, Inc. All rights reserved. |
| 3 |
* Use is subject to license terms. |
| 4 |
* |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#pragma ident "@(#)bsmaudit.c 1.1 01/09/17 SMI" |
| 28 |
|
| 29 |
#include <sys/systeminfo.h> |
| 30 |
#include <sys/param.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <sys/systeminfo.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#include <sys/wait.h> |
| 36 |
#include <netinet/in.h> |
| 37 |
#include <netdb.h> |
| 38 |
#include <signal.h> |
| 39 |
|
| 40 |
#include <pwd.h> |
| 41 |
#include <shadow.h> |
| 42 |
#include <utmpx.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#include <bsm/audit.h> |
| 47 |
#include <bsm/libbsm.h> |
| 48 |
#include <bsm/audit_uevents.h> |
| 49 |
#include <bsm/audit_record.h> |
| 50 |
#include "bsmaudit.h" |
| 51 |
|
| 52 |
#include <locale.h> |
| 53 |
|
| 54 |
#include "includes.h" |
| 55 |
#include "ssh.h" |
| 56 |
#include "log.h" |
| 57 |
|
| 58 |
static void audit_sshd_record(int typ, char *string, au_event_t event_no); |
| 59 |
static void audit_sshd_session_setup(void); |
| 60 |
static int selected(char *nam, uid_t uid, au_event_t event, int sf); |
| 61 |
|
| 62 |
static void get_terminal_id(); |
| 63 |
|
| 64 |
extern int cannot_audit(int); |
| 65 |
extern void aug_init(void); |
| 66 |
extern dev_t aug_get_port(void); |
| 67 |
extern int aug_get_machine(char *, uint32_t *, uint32_t *); |
| 68 |
extern void aug_save_auid(au_id_t); |
| 69 |
extern void aug_save_uid(uid_t); |
| 70 |
extern void aug_save_euid(uid_t); |
| 71 |
extern void aug_save_gid(gid_t); |
| 72 |
extern void aug_save_egid(gid_t); |
| 73 |
extern void aug_save_pid(pid_t); |
| 74 |
extern void aug_save_asid(au_asid_t); |
| 75 |
extern void aug_save_tid(dev_t, unsigned int); |
| 76 |
extern void aug_save_tid_ex(dev_t, uint32_t *, uint32_t); |
| 77 |
extern int aug_save_me(void); |
| 78 |
extern int aug_save_namask(void); |
| 79 |
extern void aug_save_event(au_event_t); |
| 80 |
extern void aug_save_sorf(int); |
| 81 |
extern void aug_save_text(char *); |
| 82 |
extern void aug_save_text1(char *); |
| 83 |
extern void aug_save_text2(char *); |
| 84 |
extern void aug_save_na(int); |
| 85 |
extern void aug_save_user(char *); |
| 86 |
extern void aug_save_path(char *); |
| 87 |
extern int aug_save_policy(void); |
| 88 |
extern void aug_save_afunc(int (*)(int)); |
| 89 |
extern int aug_audit(void); |
| 90 |
extern int aug_na_selected(void); |
| 91 |
extern int aug_selected(void); |
| 92 |
extern int aug_daemon_session(void); |
| 93 |
|
| 94 |
static char sav_ttyn[512]; |
| 95 |
static char sav_name[512]; |
| 96 |
static uid_t sav_uid; |
| 97 |
static gid_t sav_gid; |
| 98 |
static dev_t sav_port; |
| 99 |
static uint32_t sav_machine[4]; |
| 100 |
static uint32_t sav_iptype; |
| 101 |
static char sav_host[MAXHOSTNAMELEN]; |
| 102 |
static char *sav_cmd; |
| 103 |
|
| 104 |
void |
| 105 |
audit_sshd_save_port(int port) |
| 106 |
{ |
| 107 |
if (cannot_audit(0)) { |
| 108 |
return; |
| 109 |
} |
| 110 |
sav_port = port; |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
audit_sshd_save_host(const char *host) |
| 115 |
{ |
| 116 |
if (cannot_audit(0)) { |
| 117 |
return; |
| 118 |
} |
| 119 |
(void) strlcpy(sav_host, host, sizeof (sav_host)); |
| 120 |
(void) aug_get_machine(sav_host, &sav_machine[0], &sav_iptype); |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
audit_sshd_save_command(const char *command) |
| 125 |
{ |
| 126 |
if (cannot_audit(0)) { |
| 127 |
return; |
| 128 |
} |
| 129 |
sav_cmd = strdup(command); |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
audit_sshd_save_ttyn(const char *ttyn) |
| 134 |
{ |
| 135 |
if (cannot_audit(0)) { |
| 136 |
return; |
| 137 |
} |
| 138 |
(void) strlcpy(sav_ttyn, ttyn, sizeof (sav_ttyn)); |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
audit_sshd_save_pw(struct passwd *pwd) |
| 143 |
{ |
| 144 |
if (cannot_audit(0)) { |
| 145 |
return; |
| 146 |
} |
| 147 |
if (pwd == NULL) { |
| 148 |
sav_name[0] = '\0'; |
| 149 |
sav_uid = -1; |
| 150 |
sav_gid = -1; |
| 151 |
} else { |
| 152 |
(void) strlcpy(sav_name, pwd->pw_name, sizeof (sav_name)); |
| 153 |
sav_uid = pwd->pw_uid; |
| 154 |
sav_gid = pwd->pw_gid; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
audit_sshd_nologin(void) |
| 160 |
{ |
| 161 |
if (cannot_audit(0)) { |
| 162 |
return; |
| 163 |
} |
| 164 |
audit_sshd_record(1, gettext("logins disabled by /etc/nologin"), |
| 165 |
AUE_ssh); |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
audit_sshd_maxtrys(void) |
| 170 |
{ |
| 171 |
if (cannot_audit(0)) { |
| 172 |
return; |
| 173 |
} |
| 174 |
audit_sshd_record(1, gettext("maxtrys"), AUE_ssh); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
audit_sshd_not_console(void) |
| 179 |
{ |
| 180 |
if (cannot_audit(0)) { |
| 181 |
return; |
| 182 |
} |
| 183 |
audit_sshd_record(2, gettext("not_console"), AUE_ssh); |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
audit_sshd_bad_pw(void) |
| 188 |
{ |
| 189 |
if (cannot_audit(0)) { |
| 190 |
return; |
| 191 |
} |
| 192 |
if (sav_uid == -1) { |
| 193 |
audit_sshd_record(3, gettext("invalid user name"), AUE_ssh); |
| 194 |
} else { |
| 195 |
audit_sshd_record(4, gettext("invalid password or publickey"), |
| 196 |
AUE_ssh); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
audit_sshd_success(void) |
| 202 |
{ |
| 203 |
if (cannot_audit(0)) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
audit_sshd_session_setup(); |
| 208 |
audit_sshd_record(0, gettext("successful login"), AUE_ssh); |
| 209 |
} |
| 210 |
|
| 211 |
static void |
| 212 |
audit_sshd_record(int typ, char *string, au_event_t event_no) |
| 213 |
{ |
| 214 |
int ad, rc; |
| 215 |
uid_t uid; |
| 216 |
gid_t gid; |
| 217 |
pid_t pid; |
| 218 |
au_tid_addr_t tid; |
| 219 |
|
| 220 |
uid = sav_uid; |
| 221 |
gid = sav_gid; |
| 222 |
pid = getpid(); |
| 223 |
|
| 224 |
get_terminal_id(&tid); |
| 225 |
|
| 226 |
if (typ == 0) { |
| 227 |
rc = 0; |
| 228 |
} else { |
| 229 |
rc = -1; |
| 230 |
} |
| 231 |
|
| 232 |
if (!selected(sav_name, uid, event_no, rc)) |
| 233 |
return; |
| 234 |
|
| 235 |
debug3("BSM audit: sav_host=%s", sav_host); |
| 236 |
ad = au_open(); |
| 237 |
|
| 238 |
(void) au_write(ad, au_to_subject_ex(uid, uid, gid, uid, gid, |
| 239 |
pid, pid, &tid)); |
| 240 |
(void) au_write(ad, au_to_text(string)); |
| 241 |
if (sav_cmd != NULL) { |
| 242 |
(void) au_write(ad, au_to_text(sav_cmd)); |
| 243 |
} |
| 244 |
(void) au_write(ad, au_to_return32(typ, (int32_t)rc)); |
| 245 |
|
| 246 |
rc = au_close(ad, AU_TO_WRITE, event_no); |
| 247 |
if (rc < 0) { |
| 248 |
fatal("audit_sshd_record failed to write record: %s", |
| 249 |
strerror(errno)); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
audit_sshd_session_setup(void) |
| 255 |
{ |
| 256 |
int rc; |
| 257 |
struct auditinfo_addr info; |
| 258 |
au_mask_t mask; |
| 259 |
struct auditinfo_addr now; |
| 260 |
|
| 261 |
info.ai_auid = sav_uid; |
| 262 |
info.ai_asid = getpid(); |
| 263 |
mask.am_success = 0; |
| 264 |
mask.am_failure = 0; |
| 265 |
|
| 266 |
(void) au_user_mask(sav_name, &mask); |
| 267 |
|
| 268 |
info.ai_mask.am_success = mask.am_success; |
| 269 |
info.ai_mask.am_failure = mask.am_failure; |
| 270 |
|
| 271 |
/* see if terminal id already set */ |
| 272 |
if (getaudit_addr(&now, sizeof (now)) < 0) { |
| 273 |
fatal("audit_sshd_session_setup: getaudit_addr failed: %s", |
| 274 |
strerror(errno)); |
| 275 |
} |
| 276 |
|
| 277 |
debug("BSM audit_sshd_setup_session: calling get_terminal_id"); |
| 278 |
get_terminal_id(&(info.ai_termid)); |
| 279 |
|
| 280 |
rc = setaudit_addr(&info, sizeof (info)); |
| 281 |
if (rc < 0) { |
| 282 |
fatal("audit_sshd_session_setup: setaudit_addr failed: %s", |
| 283 |
strerror(errno)); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
static void |
| 289 |
get_terminal_id(au_tid_addr_t *tid) |
| 290 |
{ |
| 291 |
tid->at_port = sav_port; |
| 292 |
tid->at_type = sav_iptype; |
| 293 |
tid->at_addr[0] = sav_machine[0]; |
| 294 |
tid->at_addr[1] = sav_machine[1]; |
| 295 |
tid->at_addr[2] = sav_machine[2]; |
| 296 |
tid->at_addr[3] = sav_machine[3]; |
| 297 |
} |
| 298 |
|
| 299 |
void |
| 300 |
audit_sshd_logout(void) |
| 301 |
{ |
| 302 |
char textbuf[BSM_TEXTBUFSZ]; |
| 303 |
|
| 304 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 305 |
gettext("sshd logout %s"), sav_name); |
| 306 |
|
| 307 |
audit_sshd_record(0, textbuf, AUE_logout); |
| 308 |
} |
| 309 |
|
| 310 |
static int |
| 311 |
selected(char *nam, uid_t uid, au_event_t event, int sf) |
| 312 |
{ |
| 313 |
int rc, sorf; |
| 314 |
char naflags[512]; |
| 315 |
struct au_mask mask; |
| 316 |
|
| 317 |
mask.am_success = mask.am_failure = 0; |
| 318 |
if (uid < 0) { |
| 319 |
rc = getacna(naflags, 256); /* get non-attrib flags */ |
| 320 |
if (rc == 0) |
| 321 |
(void) getauditflagsbin(naflags, &mask); |
| 322 |
} else { |
| 323 |
rc = au_user_mask(nam, &mask); |
| 324 |
} |
| 325 |
|
| 326 |
if (sf == 0) { |
| 327 |
sorf = AU_PRS_SUCCESS; |
| 328 |
} else { |
| 329 |
sorf = AU_PRS_FAILURE; |
| 330 |
} |
| 331 |
rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD); |
| 332 |
|
| 333 |
return (rc); |
| 334 |
} |