|
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 "includes.h" |
| 30 |
#if defined(HAVE_BSM_AUDIT_H) && defined(HAVE_LIBBSM) |
| 31 |
#include <sys/systeminfo.h> |
| 32 |
#include <sys/param.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/socket.h> |
| 35 |
#include <sys/systeminfo.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
#include <sys/wait.h> |
| 38 |
#include <netinet/in.h> |
| 39 |
#include <netdb.h> |
| 40 |
#include <signal.h> |
| 41 |
|
| 42 |
#include <pwd.h> |
| 43 |
#include <shadow.h> |
| 44 |
#include <utmpx.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#include <bsm/audit.h> |
| 49 |
#include <bsm/libbsm.h> |
| 50 |
#include <bsm/audit_uevents.h> |
| 51 |
#include <bsm/audit_record.h> |
| 52 |
#include "openbsd-compat/bsd-solaris.h" |
| 53 |
|
| 54 |
#include <locale.h> |
| 55 |
|
| 56 |
#include "ssh.h" |
| 57 |
#include "log.h" |
| 58 |
#include "xmalloc.h" |
| 59 |
|
| 60 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 61 |
#define AuditInfoStruct auditinfo_addr |
| 62 |
#define AuditInfoTermID au_tid_addr_t |
| 63 |
#define GetAuditFunc(a,b) getaudit_addr((a),(b)) |
| 64 |
#define GetAuditFuncText "getaudit_addr" |
| 65 |
#define SetAuditFunc(a,b) setaudit_addr((a),(b)) |
| 66 |
#define SetAuditFuncText "setaudit_addr" |
| 67 |
#define AUToSubjectFunc au_to_subject_ex |
| 68 |
#define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b)) |
| 69 |
#else |
| 70 |
#define AuditInfoStruct auditinfo |
| 71 |
#define AuditInfoTermID au_tid_t |
| 72 |
#define GetAuditFunc(a,b) getaudit(a) |
| 73 |
#define GetAuditFuncText "getaudit" |
| 74 |
#define SetAuditFunc(a,b) setaudit(a) |
| 75 |
#define SetAuditFuncText "setaudit" |
| 76 |
#define AUToSubjectFunc au_to_subject |
| 77 |
#define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b)) |
| 78 |
#endif |
| 79 |
|
| 80 |
static void solaris_audit_record(int typ, char *string, au_event_t event_no); |
| 81 |
static void solaris_audit_session_setup(void); |
| 82 |
static int selected(char *nam, uid_t uid, au_event_t event, int sf); |
| 83 |
|
| 84 |
static void get_terminal_id(AuditInfoTermID *tid); |
| 85 |
|
| 86 |
extern int cannot_audit(int); |
| 87 |
extern void aug_init(void); |
| 88 |
extern dev_t aug_get_port(void); |
| 89 |
extern int aug_get_machine(char *, uint32_t *, uint32_t *); |
| 90 |
extern void aug_save_auid(au_id_t); |
| 91 |
extern void aug_save_uid(uid_t); |
| 92 |
extern void aug_save_euid(uid_t); |
| 93 |
extern void aug_save_gid(gid_t); |
| 94 |
extern void aug_save_egid(gid_t); |
| 95 |
extern void aug_save_pid(pid_t); |
| 96 |
extern void aug_save_asid(au_asid_t); |
| 97 |
extern void aug_save_tid(dev_t, unsigned int); |
| 98 |
extern void aug_save_tid_ex(dev_t, uint32_t *, uint32_t); |
| 99 |
extern int aug_save_me(void); |
| 100 |
extern int aug_save_namask(void); |
| 101 |
extern void aug_save_event(au_event_t); |
| 102 |
extern void aug_save_sorf(int); |
| 103 |
extern void aug_save_text(char *); |
| 104 |
extern void aug_save_text1(char *); |
| 105 |
extern void aug_save_text2(char *); |
| 106 |
extern void aug_save_na(int); |
| 107 |
extern void aug_save_user(char *); |
| 108 |
extern void aug_save_path(char *); |
| 109 |
extern int aug_save_policy(void); |
| 110 |
extern void aug_save_afunc(int (*)(int)); |
| 111 |
extern int aug_audit(void); |
| 112 |
extern int aug_na_selected(void); |
| 113 |
extern int aug_selected(void); |
| 114 |
extern int aug_daemon_session(void); |
| 115 |
|
| 116 |
static char sav_ttyn[512]; |
| 117 |
static char sav_name[512]; |
| 118 |
static uid_t sav_uid; |
| 119 |
static gid_t sav_gid; |
| 120 |
static dev_t sav_port; |
| 121 |
static uint32_t sav_machine[4]; |
| 122 |
static uint32_t sav_iptype; |
| 123 |
static char sav_host[MAXHOSTNAMELEN]; |
| 124 |
static char *sav_cmd = NULL; |
| 125 |
|
| 126 |
void |
| 127 |
solaris_audit_save_port(int port) |
| 128 |
{ |
| 129 |
if (cannot_audit(0)) { |
| 130 |
return; |
| 131 |
} |
| 132 |
sav_port = port; |
| 133 |
debug3("BSM audit: sav_port=%ld", (long)sav_port); |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
solaris_audit_save_host(const char *host) |
| 138 |
{ |
| 139 |
int i; |
| 140 |
#if !defined(HAVE_GETAUDIT_ADDR) |
| 141 |
in_addr_t ia; |
| 142 |
#endif |
| 143 |
|
| 144 |
if (cannot_audit(0)) { |
| 145 |
return; |
| 146 |
} |
| 147 |
(void) strlcpy(sav_host, host, sizeof (sav_host)); |
| 148 |
debug3("BSM audit: sav_host=%s", sav_host); |
| 149 |
memset(sav_machine, 0, sizeof(sav_machine)); |
| 150 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 151 |
(void) aug_get_machine(sav_host, &sav_machine[0], &sav_iptype); |
| 152 |
debug3("BSM audit: sav_iptype=%ld", (long)sav_iptype); |
| 153 |
#else |
| 154 |
ia = inet_addr(host); |
| 155 |
memcpy(&sav_machine[0], &ia, sizeof(sav_machine[0])); |
| 156 |
sav_iptype = 0; /* not used, but just in case */ |
| 157 |
#endif |
| 158 |
for (i = 0; i < sizeof(sav_machine) / sizeof(sav_machine[0]); i++) { |
| 159 |
debug3("BSM audit: sav_machine[%d]=%08lx", |
| 160 |
i, (long)sav_machine[i]); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
solaris_audit_save_command(const char *command) |
| 166 |
{ |
| 167 |
if (cannot_audit(0)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
if (sav_cmd != NULL) { |
| 171 |
free(sav_cmd); |
| 172 |
sav_cmd = NULL; |
| 173 |
} |
| 174 |
sav_cmd = xstrdup(command); |
| 175 |
debug3("BSM audit: sav_cmd=%s", sav_cmd); |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
solaris_audit_save_ttyn(const char *ttyn) |
| 180 |
{ |
| 181 |
if (cannot_audit(0)) { |
| 182 |
return; |
| 183 |
} |
| 184 |
(void) strlcpy(sav_ttyn, ttyn, sizeof (sav_ttyn)); |
| 185 |
debug3("BSM audit: sav_ttyn=%s", sav_ttyn); |
| 186 |
} |
| 187 |
|
| 188 |
void |
| 189 |
solaris_audit_save_name(const char *name) |
| 190 |
{ |
| 191 |
if (cannot_audit(0)) { |
| 192 |
return; |
| 193 |
} |
| 194 |
(void) strlcpy(sav_name, name, sizeof (sav_name)); |
| 195 |
debug3("BSM audit: sav_name=%s", sav_name); |
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
solaris_audit_save_pw(struct passwd *pwd) |
| 200 |
{ |
| 201 |
if (cannot_audit(0)) { |
| 202 |
return; |
| 203 |
} |
| 204 |
if (pwd == NULL) { |
| 205 |
sav_uid = -1; |
| 206 |
sav_gid = -1; |
| 207 |
} else { |
| 208 |
(void) strlcpy(sav_name, pwd->pw_name, sizeof (sav_name)); |
| 209 |
sav_uid = pwd->pw_uid; |
| 210 |
sav_gid = pwd->pw_gid; |
| 211 |
} |
| 212 |
debug3("BSM audit: sav_name=%s", sav_name); |
| 213 |
debug3("BSM audit: sav_uid=%ld", (long)sav_uid); |
| 214 |
debug3("BSM audit: sav_gid=%ld", (long)sav_gid); |
| 215 |
} |
| 216 |
|
| 217 |
void |
| 218 |
solaris_audit_nologin(void) |
| 219 |
{ |
| 220 |
if (cannot_audit(0)) { |
| 221 |
return; |
| 222 |
} |
| 223 |
solaris_audit_record(1, gettext("logins disabled by /etc/nologin"), |
| 224 |
AUE_openssh); |
| 225 |
} |
| 226 |
|
| 227 |
void |
| 228 |
solaris_audit_maxtrys(void) |
| 229 |
{ |
| 230 |
char textbuf[BSM_TEXTBUFSZ]; |
| 231 |
|
| 232 |
if (cannot_audit(0)) { |
| 233 |
return; |
| 234 |
} |
| 235 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 236 |
gettext("too many tries for user %s"), sav_name); |
| 237 |
solaris_audit_record(1, textbuf, AUE_openssh); |
| 238 |
} |
| 239 |
|
| 240 |
void |
| 241 |
solaris_audit_not_console(void) |
| 242 |
{ |
| 243 |
if (cannot_audit(0)) { |
| 244 |
return; |
| 245 |
} |
| 246 |
solaris_audit_record(2, gettext("not_console"), AUE_openssh); |
| 247 |
} |
| 248 |
|
| 249 |
void |
| 250 |
solaris_audit_bad_pw(const char *what) |
| 251 |
{ |
| 252 |
char textbuf[BSM_TEXTBUFSZ]; |
| 253 |
|
| 254 |
if (cannot_audit(0)) { |
| 255 |
return; |
| 256 |
} |
| 257 |
if (sav_uid == -1) { |
| 258 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 259 |
gettext("invalid user name \"%s\""), sav_name); |
| 260 |
solaris_audit_record(3, textbuf, AUE_openssh); |
| 261 |
} else { |
| 262 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 263 |
gettext("invalid %s for user %s"), what, sav_name); |
| 264 |
solaris_audit_record(4, textbuf, AUE_openssh); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void |
| 269 |
solaris_audit_success(void) |
| 270 |
{ |
| 271 |
char textbuf[BSM_TEXTBUFSZ]; |
| 272 |
|
| 273 |
if (cannot_audit(0)) { |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
solaris_audit_session_setup(); |
| 278 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 279 |
gettext("successful login %s"), sav_name); |
| 280 |
solaris_audit_record(0, textbuf, AUE_openssh); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
solaris_audit_record(int typ, char *string, au_event_t event_no) |
| 285 |
{ |
| 286 |
int ad, rc, sel; |
| 287 |
uid_t uid; |
| 288 |
gid_t gid; |
| 289 |
pid_t pid; |
| 290 |
AuditInfoTermID tid; |
| 291 |
|
| 292 |
uid = sav_uid; |
| 293 |
gid = sav_gid; |
| 294 |
pid = getpid(); |
| 295 |
|
| 296 |
get_terminal_id(&tid); |
| 297 |
|
| 298 |
if (typ == 0) { |
| 299 |
rc = 0; |
| 300 |
} else { |
| 301 |
/* |
| 302 |
* The typ value is passed to the au_return function as |
| 303 |
* the error number. We used to use small integer values |
| 304 |
* (e.g. 4) to distinguish between the various errors, |
| 305 |
* but praudit treats the field as an errno value and |
| 306 |
* passes it through strerror(), so they would show |
| 307 |
* up as (e.g.) "interrupted system call" (4 is EINTR) |
| 308 |
* which was confusing: |
| 309 |
* |
| 310 |
* return,failure: Interrupted system call,-1 |
| 311 |
* |
| 312 |
* I tried setting rc to the negative of the typ and typ |
| 313 |
* to zero, but that shows up as a success rather than a |
| 314 |
* failure: |
| 315 |
* |
| 316 |
* return,success,-4 |
| 317 |
* |
| 318 |
* Sigh. |
| 319 |
* |
| 320 |
* Experimentally, using numbers outside the range of |
| 321 |
* valid errno values show up as integers, e.g.: |
| 322 |
* |
| 323 |
* return,failure: 244,-1 |
| 324 |
* |
| 325 |
* which seems much more reasonable. |
| 326 |
* |
| 327 |
* According to the audit.log documentation, the field |
| 328 |
* is only a char type (actually, probably unsigned char) |
| 329 |
* so we have to keep it under 255. |
| 330 |
*/ |
| 331 |
typ += 240; |
| 332 |
if (typ > 255) { |
| 333 |
typ = EINVAL; /* caller goofed */ |
| 334 |
} |
| 335 |
rc = -1; |
| 336 |
} |
| 337 |
|
| 338 |
sel = selected(sav_name, uid, event_no, rc); |
| 339 |
debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string); |
| 340 |
if (!sel) |
| 341 |
return; |
| 342 |
|
| 343 |
ad = au_open(); |
| 344 |
|
| 345 |
(void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid, |
| 346 |
pid, pid, &tid)); |
| 347 |
(void) au_write(ad, au_to_text(string)); |
| 348 |
if (sav_cmd != NULL) { |
| 349 |
(void) au_write(ad, au_to_text(sav_cmd)); |
| 350 |
} |
| 351 |
(void) au_write(ad, AUToReturnFunc(typ, rc)); |
| 352 |
|
| 353 |
rc = au_close(ad, AU_TO_WRITE, event_no); |
| 354 |
if (rc < 0) { |
| 355 |
error("BSM audit: solaris_audit_record failed to write \"%s\" record: %s", |
| 356 |
string, strerror(errno)); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
static void |
| 361 |
solaris_audit_session_setup(void) |
| 362 |
{ |
| 363 |
int rc; |
| 364 |
struct AuditInfoStruct info; |
| 365 |
au_mask_t mask; |
| 366 |
struct AuditInfoStruct now; |
| 367 |
|
| 368 |
info.ai_auid = sav_uid; |
| 369 |
info.ai_asid = getpid(); |
| 370 |
mask.am_success = 0; |
| 371 |
mask.am_failure = 0; |
| 372 |
|
| 373 |
(void) au_user_mask(sav_name, &mask); |
| 374 |
|
| 375 |
info.ai_mask.am_success = mask.am_success; |
| 376 |
info.ai_mask.am_failure = mask.am_failure; |
| 377 |
|
| 378 |
/* see if terminal id already set */ |
| 379 |
if (GetAuditFunc(&now, sizeof (now)) < 0) { |
| 380 |
error("BSM audit: solaris_audit_session_setup: %s failed: %s", |
| 381 |
GetAuditFuncText, strerror(errno)); |
| 382 |
} |
| 383 |
|
| 384 |
debug("BSM solaris_audit_setup_session: calling get_terminal_id"); |
| 385 |
get_terminal_id(&(info.ai_termid)); |
| 386 |
|
| 387 |
rc = SetAuditFunc(&info, sizeof (info)); |
| 388 |
if (rc < 0) { |
| 389 |
error("BSM audit: solaris_audit_session_setup: %s failed: %s", |
| 390 |
SetAuditFuncText, strerror(errno)); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
static void |
| 396 |
get_terminal_id(AuditInfoTermID *tid) |
| 397 |
{ |
| 398 |
#if defined(HAVE_GETAUDIT_ADDR) |
| 399 |
tid->at_port = sav_port; |
| 400 |
tid->at_type = sav_iptype; |
| 401 |
tid->at_addr[0] = sav_machine[0]; |
| 402 |
tid->at_addr[1] = sav_machine[1]; |
| 403 |
tid->at_addr[2] = sav_machine[2]; |
| 404 |
tid->at_addr[3] = sav_machine[3]; |
| 405 |
#else |
| 406 |
tid->port = sav_port; |
| 407 |
tid->machine = sav_machine[0]; |
| 408 |
#endif |
| 409 |
} |
| 410 |
|
| 411 |
void |
| 412 |
solaris_audit_logout(void) |
| 413 |
{ |
| 414 |
char textbuf[BSM_TEXTBUFSZ]; |
| 415 |
|
| 416 |
(void) snprintf(textbuf, sizeof (textbuf), |
| 417 |
gettext("sshd logout %s"), sav_name); |
| 418 |
|
| 419 |
solaris_audit_record(0, textbuf, AUE_logout); |
| 420 |
} |
| 421 |
|
| 422 |
static int |
| 423 |
selected(char *nam, uid_t uid, au_event_t event, int sf) |
| 424 |
{ |
| 425 |
int rc, sorf; |
| 426 |
char naflags[512]; |
| 427 |
struct au_mask mask; |
| 428 |
|
| 429 |
mask.am_success = mask.am_failure = 0; |
| 430 |
if (uid < 0) { |
| 431 |
rc = getacna(naflags, 256); /* get non-attrib flags */ |
| 432 |
if (rc == 0) |
| 433 |
(void) getauditflagsbin(naflags, &mask); |
| 434 |
} else { |
| 435 |
rc = au_user_mask(nam, &mask); |
| 436 |
} |
| 437 |
|
| 438 |
if (sf == 0) { |
| 439 |
sorf = AU_PRS_SUCCESS; |
| 440 |
} else { |
| 441 |
sorf = AU_PRS_FAILURE; |
| 442 |
} |
| 443 |
rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD); |
| 444 |
|
| 445 |
return (rc); |
| 446 |
} |
| 447 |
#endif /* BSM */ |