|
Added
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 "includes.h" |
| 30 |
#if defined(HAVE_BSM_AUDIT_H) && defined(HAVE_LIBBSM) |
| 31 |
|
| 32 |
#include <bsm/audit.h> |
| 33 |
#include <bsm/libbsm.h> |
| 34 |
#include <bsm/audit_uevents.h> |
| 35 |
#include <bsm/audit_record.h> |
| 36 |
#include "port-solaris.h" |
| 37 |
|
| 38 |
#include <locale.h> |
| 39 |
|
| 40 |
#include "ssh.h" |
| 41 |
#include "log.h" |
| 42 |
|
| 43 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 44 |
#define AuditInfoStruct auditinfo_addr |
| 45 |
#define AuditInfoTermID au_tid_addr_t |
| 46 |
#define GetAuditFunc(a,b) getaudit_addr((a),(b)) |
| 47 |
#define GetAuditFuncText "getaudit_addr" |
| 48 |
#define SetAuditFunc(a,b) setaudit_addr((a),(b)) |
| 49 |
#define SetAuditFuncText "setaudit_addr" |
| 50 |
#define AUToSubjectFunc au_to_subject_ex |
| 51 |
#define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b)) |
| 52 |
#else |
| 53 |
#define AuditInfoStruct auditinfo |
| 54 |
#define AuditInfoTermID au_tid_t |
| 55 |
#define GetAuditFunc(a,b) getaudit(a) |
| 56 |
#define GetAuditFuncText "getaudit" |
| 57 |
#define SetAuditFunc(a,b) setaudit(a) |
| 58 |
#define SetAuditFuncText "setaudit" |
| 59 |
#define AUToSubjectFunc au_to_subject |
| 60 |
#define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b)) |
| 61 |
#endif |
| 62 |
|
| 63 |
static void solaris_audit_record(int typ, char *string, au_event_t event_no); |
| 64 |
static void solaris_audit_session_setup(void); |
| 65 |
static int selected(char *nam, uid_t uid, au_event_t event, int sf); |
| 66 |
|
| 67 |
static void get_terminal_id(AuditInfoTermID *tid); |
| 68 |
|
| 69 |
extern int cannot_audit(int); |
| 70 |
extern void aug_init(void); |
| 71 |
extern dev_t aug_get_port(void); |
| 72 |
extern int aug_get_machine(char *, uint32_t *, uint32_t *); |
| 73 |
extern void aug_save_auid(au_id_t); |
| 74 |
extern void aug_save_uid(uid_t); |
| 75 |
extern void aug_save_euid(uid_t); |
| 76 |
extern void aug_save_gid(gid_t); |
| 77 |
extern void aug_save_egid(gid_t); |
| 78 |
extern void aug_save_pid(pid_t); |
| 79 |
extern void aug_save_asid(au_asid_t); |
| 80 |
extern void aug_save_tid(dev_t, unsigned int); |
| 81 |
extern void aug_save_tid_ex(dev_t, uint32_t *, uint32_t); |
| 82 |
extern int aug_save_me(void); |
| 83 |
extern int aug_save_namask(void); |
| 84 |
extern void aug_save_event(au_event_t); |
| 85 |
extern void aug_save_sorf(int); |
| 86 |
extern void aug_save_text(char *); |
| 87 |
extern void aug_save_text1(char *); |
| 88 |
extern void aug_save_text2(char *); |
| 89 |
extern void aug_save_na(int); |
| 90 |
extern void aug_save_user(char *); |
| 91 |
extern void aug_save_path(char *); |
| 92 |
extern int aug_save_policy(void); |
| 93 |
extern void aug_save_afunc(int (*)(int)); |
| 94 |
extern int aug_audit(void); |
| 95 |
extern int aug_na_selected(void); |
| 96 |
extern int aug_selected(void); |
| 97 |
extern int aug_daemon_session(void); |
| 98 |
|
| 99 |
static char sav_ttyn[512]; |
| 100 |
static char sav_name[512]; |
| 101 |
static uid_t sav_uid; |
| 102 |
static gid_t sav_gid; |
| 103 |
static dev_t sav_port; |
| 104 |
static uint32_t sav_machine[4]; |
| 105 |
static uint32_t sav_iptype; |
| 106 |
static char sav_host[MAXHOSTNAMELEN]; |
| 107 |
static char *sav_cmd = NULL; |
| 108 |
|
| 109 |
void |
| 110 |
solaris_audit_save_port(int port) |
| 111 |
{ |
| 112 |
if (cannot_audit(0)) |
| 113 |
return; |
| 114 |
|
| 115 |
sav_port = port; |
| 116 |
debug3("BSM audit: sav_port=%ld", (long)sav_port); |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
solaris_audit_save_host(const char *host) |
| 121 |
{ |
| 122 |
int i; |
| 123 |
#if !defined(HAVE_GETAUDIT_ADDR) |
| 124 |
in_addr_t ia; |
| 125 |
#endif |
| 126 |
|
| 127 |
if (cannot_audit(0)) |
| 128 |
return; |
| 129 |
|
| 130 |
(void) strlcpy(sav_host, host, sizeof (sav_host)); |
| 131 |
debug3("BSM audit: sav_host=%s", sav_host); |
| 132 |
memset(sav_machine, 0, sizeof(sav_machine)); |
| 133 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 134 |
(void) aug_get_machine(sav_host, &sav_machine[0], &sav_iptype); |
| 135 |
debug3("BSM audit: sav_iptype=%ld", (long)sav_iptype); |
| 136 |
#else |
| 137 |
ia = inet_addr(host); |
| 138 |
memcpy(&sav_machine[0], &ia, sizeof(sav_machine[0])); |
| 139 |
sav_iptype = 0; /* not used, but just in case */ |
| 140 |
#endif |
| 141 |
for (i = 0; i < sizeof(sav_machine) / sizeof(sav_machine[0]); i++) { |
| 142 |
debug3("BSM audit: sav_machine[%d]=%08lx", |
| 143 |
i, (long)sav_machine[i]); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void |
| 148 |
solaris_audit_save_command(const char *command) |
| 149 |
{ |
| 150 |
if (cannot_audit(0)) |
| 151 |
return; |
| 152 |
|
| 153 |
if (sav_cmd != NULL) { |
| 154 |
free(sav_cmd); |
| 155 |
sav_cmd = NULL; |
| 156 |
} |
| 157 |
sav_cmd = strdup(command); |
| 158 |
debug3("BSM audit: sav_cmd=%s", sav_cmd); |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
solaris_audit_save_ttyn(const char *ttyn) |
| 163 |
{ |
| 164 |
if (cannot_audit(0)) |
| 165 |
return; |
| 166 |
|
| 167 |
(void) strlcpy(sav_ttyn, ttyn, sizeof (sav_ttyn)); |
| 168 |
debug3("BSM audit: sav_ttyn=%s", sav_ttyn); |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
solaris_audit_save_name(const char *name) |
| 173 |
{ |
| 174 |
if (cannot_audit(0)) { |
| 175 |
return; |
| 176 |
} |
| 177 |
(void) strlcpy(sav_name, name, sizeof (sav_name)); |
| 178 |
debug3("BSM audit: sav_name=%s", sav_name); |
| 179 |
} |
| 180 |
|
| 181 |
void |
| 182 |
solaris_audit_save_pw(struct passwd *pwd) |
| 183 |
{ |
| 184 |
if (cannot_audit(0)) |
| 185 |
return; |
| 186 |
|
| 187 |
if (pwd == NULL) { |
| 188 |
sav_uid = -1; |
| 189 |
sav_gid = -1; |
| 190 |
} else { |
| 191 |
(void) strlcpy(sav_name, pwd->pw_name, sizeof (sav_name)); |
| 192 |
sav_uid = pwd->pw_uid; |
| 193 |
sav_gid = pwd->pw_gid; |
| 194 |
} |
| 195 |
debug3("BSM audit: sav_name=%s", sav_name); |
| 196 |
debug3("BSM audit: sav_uid=%ld", (long)sav_uid); |
| 197 |
debug3("BSM audit: sav_gid=%ld", (long)sav_gid); |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
solaris_audit_bad_pw(const char *what) |
| 202 |
{ |
| 203 |
char textbuf[BSM_TEXTBUFSZ]; |
| 204 |
|
| 205 |
if (cannot_audit(0)) |
| 206 |
return; |
| 207 |
|
| 208 |
if (sav_uid == -1) { |
| 209 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 210 |
gettext("invalid user name \"%s\""), sav_name); |
| 211 |
solaris_audit_record(3, textbuf, AUE_openssh); |
| 212 |
} else { |
| 213 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 214 |
gettext("invalid %s for user %s"), what, sav_name); |
| 215 |
solaris_audit_record(4, textbuf, AUE_openssh); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
solaris_audit_record(int typ, char *string, au_event_t event_no) |
| 221 |
{ |
| 222 |
int ad, rc, sel; |
| 223 |
uid_t uid; |
| 224 |
gid_t gid; |
| 225 |
pid_t pid; |
| 226 |
AuditInfoTermID tid; |
| 227 |
|
| 228 |
uid = sav_uid; |
| 229 |
gid = sav_gid; |
| 230 |
pid = getpid(); |
| 231 |
|
| 232 |
get_terminal_id(&tid); |
| 233 |
|
| 234 |
if (typ == 0) { |
| 235 |
rc = 0; |
| 236 |
} else { |
| 237 |
rc = -1; |
| 238 |
} |
| 239 |
|
| 240 |
sel = selected(sav_name, uid, event_no, rc); |
| 241 |
debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string); |
| 242 |
if (!sel) |
| 243 |
return; |
| 244 |
|
| 245 |
ad = au_open(); |
| 246 |
|
| 247 |
(void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid, |
| 248 |
pid, pid, &tid)); |
| 249 |
(void) au_write(ad, au_to_text(string)); |
| 250 |
if (sav_cmd != NULL) { |
| 251 |
(void) au_write(ad, au_to_text(sav_cmd)); |
| 252 |
} |
| 253 |
(void) au_write(ad, AUToReturnFunc(typ, rc)); |
| 254 |
|
| 255 |
rc = au_close(ad, AU_TO_WRITE, event_no); |
| 256 |
if (rc < 0) { |
| 257 |
error("BSM audit: solaris_audit_record failed to write \"%s\" record: %s", |
| 258 |
string, strerror(errno)); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
solaris_audit_session_setup(void) |
| 264 |
{ |
| 265 |
int rc; |
| 266 |
struct AuditInfoStruct info; |
| 267 |
au_mask_t mask; |
| 268 |
struct AuditInfoStruct now; |
| 269 |
|
| 270 |
info.ai_auid = sav_uid; |
| 271 |
info.ai_asid = getpid(); |
| 272 |
mask.am_success = 0; |
| 273 |
mask.am_failure = 0; |
| 274 |
|
| 275 |
(void) au_user_mask(sav_name, &mask); |
| 276 |
|
| 277 |
info.ai_mask.am_success = mask.am_success; |
| 278 |
info.ai_mask.am_failure = mask.am_failure; |
| 279 |
|
| 280 |
/* see if terminal id already set */ |
| 281 |
if (GetAuditFunc(&now, sizeof (now)) < 0) { |
| 282 |
error("BSM audit: solaris_audit_session_setup: %s failed: %s", |
| 283 |
GetAuditFuncText, strerror(errno)); |
| 284 |
} |
| 285 |
|
| 286 |
debug("BSM solaris_audit_setup_session: calling get_terminal_id"); |
| 287 |
get_terminal_id(&(info.ai_termid)); |
| 288 |
|
| 289 |
rc = SetAuditFunc(&info, sizeof (info)); |
| 290 |
if (rc < 0) { |
| 291 |
error("BSM audit: solaris_audit_session_setup: %s failed: %s", |
| 292 |
SetAuditFuncText, strerror(errno)); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
static void |
| 298 |
get_terminal_id(AuditInfoTermID *tid) |
| 299 |
{ |
| 300 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 301 |
tid->at_port = sav_port; |
| 302 |
tid->at_type = sav_iptype; |
| 303 |
tid->at_addr[0] = sav_machine[0]; |
| 304 |
tid->at_addr[1] = sav_machine[1]; |
| 305 |
tid->at_addr[2] = sav_machine[2]; |
| 306 |
tid->at_addr[3] = sav_machine[3]; |
| 307 |
#else |
| 308 |
tid->port = sav_port; |
| 309 |
tid->machine = sav_machine[0]; |
| 310 |
#endif |
| 311 |
} |
| 312 |
|
| 313 |
void |
| 314 |
solaris_audit_event(enum audit_event_type event) |
| 315 |
{ |
| 316 |
char textbuf[BSM_TEXTBUFSZ]; |
| 317 |
|
| 318 |
if (cannot_audit(0)) |
| 319 |
return; |
| 320 |
|
| 321 |
switch(event) { |
| 322 |
case LOGOUT: |
| 323 |
snprintf(textbuf, sizeof (textbuf), |
| 324 |
gettext("sshd logout %s"), sav_name); |
| 325 |
solaris_audit_record(0, textbuf, AUE_logout); |
| 326 |
break; |
| 327 |
|
| 328 |
case NOLOGIN: |
| 329 |
solaris_audit_record(1, |
| 330 |
gettext("logins disabled by /etc/nologin"), AUE_openssh); |
| 331 |
break; |
| 332 |
|
| 333 |
case MAXTRIES: |
| 334 |
snprintf(textbuf, sizeof (textbuf), |
| 335 |
gettext("too many tries for user %s"), sav_name); |
| 336 |
solaris_audit_record(1, textbuf, AUE_openssh); |
| 337 |
break; |
| 338 |
|
| 339 |
case NOT_CONSOLE: |
| 340 |
solaris_audit_record(2, gettext("not_console"), AUE_openssh); |
| 341 |
break; |
| 342 |
|
| 343 |
case SUCCESS: |
| 344 |
solaris_audit_session_setup(); |
| 345 |
snprintf(textbuf, sizeof (textbuf), |
| 346 |
gettext("successful login %s"), sav_name); |
| 347 |
solaris_audit_record(0, textbuf, AUE_openssh); |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
static int |
| 353 |
selected(char *nam, uid_t uid, au_event_t event, int sf) |
| 354 |
{ |
| 355 |
int rc, sorf; |
| 356 |
char naflags[512]; |
| 357 |
struct au_mask mask; |
| 358 |
|
| 359 |
mask.am_success = mask.am_failure = 0; |
| 360 |
if (uid < 0) { |
| 361 |
rc = getacna(naflags, 256); /* get non-attrib flags */ |
| 362 |
if (rc == 0) |
| 363 |
(void) getauditflagsbin(naflags, &mask); |
| 364 |
} else { |
| 365 |
rc = au_user_mask(nam, &mask); |
| 366 |
} |
| 367 |
|
| 368 |
if (sf == 0) { |
| 369 |
sorf = AU_PRS_SUCCESS; |
| 370 |
} else { |
| 371 |
sorf = AU_PRS_FAILURE; |
| 372 |
} |
| 373 |
rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD); |
| 374 |
|
| 375 |
return (rc); |
| 376 |
} |
| 377 |
|
| 378 |
# ifdef CUSTOM_FAILED_LOGIN |
| 379 |
void |
| 380 |
record_failed_login(const char *user, const char *ttyname) |
| 381 |
{ |
| 382 |
} |
| 383 |
# endif |
| 384 |
|
| 385 |
#endif /* BSM */ |