|
Lines 35-47
Link Here
|
| 35 |
|
35 |
|
| 36 |
#include "log.h" |
36 |
#include "log.h" |
| 37 |
#include "audit.h" |
37 |
#include "audit.h" |
|
|
38 |
#include "key.h" |
| 39 |
#include "hostfile.h" |
| 40 |
#include "auth.h" |
| 41 |
#include "misc.h" /* servconf.h needs misc.h for struct ForwardOptions */ |
| 42 |
#include "servconf.h" |
| 38 |
#include "canohost.h" |
43 |
#include "canohost.h" |
|
|
44 |
#include "packet.h" |
| 45 |
#include "cipher.h" |
| 39 |
|
46 |
|
|
|
47 |
#define AUDIT_LOG_SIZE 256 |
| 48 |
|
| 49 |
extern ServerOptions options; |
| 50 |
extern Authctxt *the_authctxt; |
| 51 |
extern u_int utmp_len; |
| 40 |
const char* audit_username(void); |
52 |
const char* audit_username(void); |
| 41 |
|
53 |
|
| 42 |
int |
54 |
static void |
| 43 |
linux_audit_record_event(int uid, const char *username, |
55 |
linux_audit_user_logxxx(int uid, const char *username, |
| 44 |
const char *hostname, const char *ip, const char *ttyn, int success) |
56 |
const char *hostname, const char *ip, const char *ttyn, int success, int event) |
| 45 |
{ |
57 |
{ |
| 46 |
int audit_fd, rc, saved_errno; |
58 |
int audit_fd, rc, saved_errno; |
| 47 |
|
59 |
|
|
Lines 49-59
linux_audit_record_event(int uid, const char *username,
Link Here
|
| 49 |
if (audit_fd < 0) { |
61 |
if (audit_fd < 0) { |
| 50 |
if (errno == EINVAL || errno == EPROTONOSUPPORT || |
62 |
if (errno == EINVAL || errno == EPROTONOSUPPORT || |
| 51 |
errno == EAFNOSUPPORT) |
63 |
errno == EAFNOSUPPORT) |
| 52 |
return 1; /* No audit support in kernel */ |
64 |
return; /* No audit support in kernel */ |
| 53 |
else |
65 |
else |
| 54 |
return 0; /* Must prevent login */ |
66 |
goto fatal_report; /* Must prevent login */ |
| 55 |
} |
67 |
} |
| 56 |
rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN, |
68 |
rc = audit_log_acct_message(audit_fd, event, |
| 57 |
NULL, "login", username ? username : "(unknown)", |
69 |
NULL, "login", username ? username : "(unknown)", |
| 58 |
username == NULL ? uid : -1, hostname, ip, ttyn, success); |
70 |
username == NULL ? uid : -1, hostname, ip, ttyn, success); |
| 59 |
saved_errno = errno; |
71 |
saved_errno = errno; |
|
Lines 65-99
linux_audit_record_event(int uid, const char *username,
Link Here
|
| 65 |
if ((rc == -EPERM) && (geteuid() != 0)) |
77 |
if ((rc == -EPERM) && (geteuid() != 0)) |
| 66 |
rc = 0; |
78 |
rc = 0; |
| 67 |
errno = saved_errno; |
79 |
errno = saved_errno; |
| 68 |
return (rc >= 0); |
80 |
if (rc < 0) { |
|
|
81 |
fatal_report: |
| 82 |
fatal("linux_audit_write_entry failed: %s", strerror(errno)); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
linux_audit_user_auth(int uid, const char *username, |
| 88 |
const char *hostname, const char *ip, const char *ttyn, int success, int event) |
| 89 |
{ |
| 90 |
int audit_fd, rc, saved_errno; |
| 91 |
static const char *event_name[] = { |
| 92 |
"maxtries exceeded", |
| 93 |
"root denied", |
| 94 |
"success", |
| 95 |
"none", |
| 96 |
"password", |
| 97 |
"challenge-response", |
| 98 |
"pubkey", |
| 99 |
"hostbased", |
| 100 |
"gssapi", |
| 101 |
"invalid user", |
| 102 |
"nologin", |
| 103 |
"connection closed", |
| 104 |
"connection abandoned", |
| 105 |
"unknown" |
| 106 |
}; |
| 107 |
|
| 108 |
audit_fd = audit_open(); |
| 109 |
if (audit_fd < 0) { |
| 110 |
if (errno == EINVAL || errno == EPROTONOSUPPORT || |
| 111 |
errno == EAFNOSUPPORT) |
| 112 |
return; /* No audit support in kernel */ |
| 113 |
else |
| 114 |
goto fatal_report; /* Must prevent login */ |
| 115 |
} |
| 116 |
|
| 117 |
if ((event < 0) || (event > SSH_AUDIT_UNKNOWN)) |
| 118 |
event = SSH_AUDIT_UNKNOWN; |
| 119 |
|
| 120 |
rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, |
| 121 |
NULL, event_name[event], username ? username : "(unknown)", |
| 122 |
username == NULL ? uid : -1, hostname, ip, ttyn, success); |
| 123 |
saved_errno = errno; |
| 124 |
close(audit_fd); |
| 125 |
/* |
| 126 |
* Do not report error if the error is EPERM and sshd is run as non |
| 127 |
* root user. |
| 128 |
*/ |
| 129 |
if ((rc == -EPERM) && (geteuid() != 0)) |
| 130 |
rc = 0; |
| 131 |
errno = saved_errno; |
| 132 |
if (rc < 0) { |
| 133 |
fatal_report: |
| 134 |
fatal("linux_audit_write_entry failed: %s", strerror(errno)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
int |
| 139 |
audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv) |
| 140 |
{ |
| 141 |
char buf[AUDIT_LOG_SIZE]; |
| 142 |
int audit_fd, rc, saved_errno; |
| 143 |
|
| 144 |
audit_fd = audit_open(); |
| 145 |
if (audit_fd < 0) { |
| 146 |
if (errno == EINVAL || errno == EPROTONOSUPPORT || |
| 147 |
errno == EAFNOSUPPORT) |
| 148 |
return 1; /* No audit support in kernel */ |
| 149 |
else |
| 150 |
return 0; /* Must prevent login */ |
| 151 |
} |
| 152 |
snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", get_remote_port()); |
| 153 |
rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL, |
| 154 |
buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv); |
| 155 |
if ((rc < 0) && ((rc != -1) || (getuid() == 0))) |
| 156 |
goto out; |
| 157 |
/* is the fingerprint_prefix() still needed? |
| 158 |
snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s%s rport=%d", |
| 159 |
type, bits, sshkey_fingerprint_prefix(), fp, get_remote_port()); |
| 160 |
*/ |
| 161 |
snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s rport=%d", |
| 162 |
type, bits, fp, get_remote_port()); |
| 163 |
rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL, |
| 164 |
buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv); |
| 165 |
out: |
| 166 |
saved_errno = errno; |
| 167 |
audit_close(audit_fd); |
| 168 |
errno = saved_errno; |
| 169 |
/* do not report error if the error is EPERM and sshd is run as non root user */ |
| 170 |
return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0)); |
| 69 |
} |
171 |
} |
| 70 |
|
172 |
|
|
|
173 |
static int user_login_count = 0; |
| 174 |
|
| 71 |
/* Below is the sshd audit API code */ |
175 |
/* Below is the sshd audit API code */ |
| 72 |
|
176 |
|
| 73 |
void |
177 |
void |
| 74 |
audit_connection_from(const char *host, int port) |
178 |
audit_connection_from(const char *host, int port) |
| 75 |
{ |
179 |
{ |
| 76 |
} |
|
|
| 77 |
/* not implemented */ |
180 |
/* not implemented */ |
|
|
181 |
} |
| 78 |
|
182 |
|
| 79 |
void |
183 |
int |
| 80 |
audit_run_command(const char *command) |
184 |
audit_run_command(const char *command) |
| 81 |
{ |
185 |
{ |
| 82 |
/* not implemented */ |
186 |
if (!user_login_count++) |
|
|
187 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 188 |
NULL, "ssh", 1, AUDIT_USER_LOGIN); |
| 189 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 190 |
NULL, "ssh", 1, AUDIT_USER_START); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
audit_end_command(int handle, const char *command) |
| 196 |
{ |
| 197 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 198 |
NULL, "ssh", 1, AUDIT_USER_END); |
| 199 |
if (user_login_count && !--user_login_count) |
| 200 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 201 |
NULL, "ssh", 1, AUDIT_USER_LOGOUT); |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
audit_count_session_open(void) |
| 206 |
{ |
| 207 |
user_login_count++; |
| 83 |
} |
208 |
} |
| 84 |
|
209 |
|
| 85 |
void |
210 |
void |
| 86 |
audit_session_open(struct logininfo *li) |
211 |
audit_session_open(struct logininfo *li) |
| 87 |
{ |
212 |
{ |
| 88 |
if (linux_audit_record_event(li->uid, NULL, li->hostname, |
213 |
if (!user_login_count++) |
| 89 |
NULL, li->line, 1) == 0) |
214 |
linux_audit_user_logxxx(li->uid, NULL, li->hostname, |
| 90 |
fatal("linux_audit_write_entry failed: %s", strerror(errno)); |
215 |
NULL, li->line, 1, AUDIT_USER_LOGIN); |
|
|
216 |
linux_audit_user_logxxx(li->uid, NULL, li->hostname, |
| 217 |
NULL, li->line, 1, AUDIT_USER_START); |
| 91 |
} |
218 |
} |
| 92 |
|
219 |
|
| 93 |
void |
220 |
void |
| 94 |
audit_session_close(struct logininfo *li) |
221 |
audit_session_close(struct logininfo *li) |
| 95 |
{ |
222 |
{ |
| 96 |
/* not implemented */ |
223 |
linux_audit_user_logxxx(li->uid, NULL, li->hostname, |
|
|
224 |
NULL, li->line, 1, AUDIT_USER_END); |
| 225 |
if (user_login_count && !--user_login_count) |
| 226 |
linux_audit_user_logxxx(li->uid, NULL, li->hostname, |
| 227 |
NULL, li->line, 1, AUDIT_USER_LOGOUT); |
| 97 |
} |
228 |
} |
| 98 |
|
229 |
|
| 99 |
void |
230 |
void |
|
Lines 101-121
audit_event(ssh_audit_event_t event)
Link Here
|
| 101 |
{ |
232 |
{ |
| 102 |
switch(event) { |
233 |
switch(event) { |
| 103 |
case SSH_AUTH_SUCCESS: |
234 |
case SSH_AUTH_SUCCESS: |
| 104 |
case SSH_CONNECTION_CLOSE: |
235 |
linux_audit_user_auth(-1, audit_username(), NULL, |
|
|
236 |
get_remote_ipaddr(), "ssh", 1, event); |
| 237 |
break; |
| 238 |
|
| 105 |
case SSH_NOLOGIN: |
239 |
case SSH_NOLOGIN: |
| 106 |
case SSH_LOGIN_EXCEED_MAXTRIES: |
|
|
| 107 |
case SSH_LOGIN_ROOT_DENIED: |
240 |
case SSH_LOGIN_ROOT_DENIED: |
|
|
241 |
linux_audit_user_auth(-1, audit_username(), NULL, |
| 242 |
get_remote_ipaddr(), "ssh", 0, event); |
| 243 |
linux_audit_user_logxxx(-1, audit_username(), NULL, |
| 244 |
get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN); |
| 108 |
break; |
245 |
break; |
| 109 |
|
246 |
|
|
|
247 |
case SSH_LOGIN_EXCEED_MAXTRIES: |
| 110 |
case SSH_AUTH_FAIL_NONE: |
248 |
case SSH_AUTH_FAIL_NONE: |
| 111 |
case SSH_AUTH_FAIL_PASSWD: |
249 |
case SSH_AUTH_FAIL_PASSWD: |
| 112 |
case SSH_AUTH_FAIL_KBDINT: |
250 |
case SSH_AUTH_FAIL_KBDINT: |
| 113 |
case SSH_AUTH_FAIL_PUBKEY: |
251 |
case SSH_AUTH_FAIL_PUBKEY: |
| 114 |
case SSH_AUTH_FAIL_HOSTBASED: |
252 |
case SSH_AUTH_FAIL_HOSTBASED: |
| 115 |
case SSH_AUTH_FAIL_GSSAPI: |
253 |
case SSH_AUTH_FAIL_GSSAPI: |
|
|
254 |
linux_audit_user_auth(-1, audit_username(), NULL, |
| 255 |
get_remote_ipaddr(), "ssh", 0, event); |
| 256 |
break; |
| 257 |
|
| 258 |
case SSH_CONNECTION_CLOSE: |
| 259 |
if (user_login_count) { |
| 260 |
while (user_login_count--) |
| 261 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 262 |
NULL, "ssh", 1, AUDIT_USER_END); |
| 263 |
linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns), |
| 264 |
NULL, "ssh", 1, AUDIT_USER_LOGOUT); |
| 265 |
} |
| 266 |
break; |
| 267 |
|
| 268 |
case SSH_CONNECTION_ABANDON: |
| 116 |
case SSH_INVALID_USER: |
269 |
case SSH_INVALID_USER: |
| 117 |
linux_audit_record_event(-1, audit_username(), NULL, |
270 |
linux_audit_user_logxxx(-1, audit_username(), NULL, |
| 118 |
get_remote_ipaddr(), "sshd", 0); |
271 |
get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN); |
| 119 |
break; |
272 |
break; |
| 120 |
|
273 |
|
| 121 |
default: |
274 |
default: |
|
Lines 123-126
audit_event(ssh_audit_event_t event)
Link Here
|
| 123 |
} |
276 |
} |
| 124 |
} |
277 |
} |
| 125 |
|
278 |
|
|
|
279 |
void |
| 280 |
audit_unsupported_body(int what) |
| 281 |
{ |
| 282 |
#ifdef AUDIT_CRYPTO_SESSION |
| 283 |
char buf[AUDIT_LOG_SIZE]; |
| 284 |
const static char *name[] = { "cipher", "mac", "comp" }; |
| 285 |
char *s; |
| 286 |
int audit_fd; |
| 287 |
|
| 288 |
snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ", |
| 289 |
name[what], get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())), |
| 290 |
get_local_port()); |
| 291 |
free(s); |
| 292 |
audit_fd = audit_open(); |
| 293 |
if (audit_fd < 0) |
| 294 |
/* no problem, the next instruction will be fatal() */ |
| 295 |
return; |
| 296 |
audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION, |
| 297 |
buf, NULL, get_remote_ipaddr(), NULL, 0); |
| 298 |
audit_close(audit_fd); |
| 299 |
#endif |
| 300 |
} |
| 301 |
|
| 302 |
const static char *direction[] = { "from-server", "from-client", "both" }; |
| 303 |
|
| 304 |
void |
| 305 |
audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, |
| 306 |
uid_t uid) |
| 307 |
{ |
| 308 |
#ifdef AUDIT_CRYPTO_SESSION |
| 309 |
char buf[AUDIT_LOG_SIZE]; |
| 310 |
int audit_fd, audit_ok; |
| 311 |
const struct sshcipher *cipher = cipher_by_name(enc); |
| 312 |
char *s; |
| 313 |
|
| 314 |
snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ", |
| 315 |
direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs, |
| 316 |
(intmax_t)pid, (intmax_t)uid, |
| 317 |
get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())), get_local_port()); |
| 318 |
free(s); |
| 319 |
audit_fd = audit_open(); |
| 320 |
if (audit_fd < 0) { |
| 321 |
if (errno == EINVAL || errno == EPROTONOSUPPORT || |
| 322 |
errno == EAFNOSUPPORT) |
| 323 |
return; /* No audit support in kernel */ |
| 324 |
else |
| 325 |
fatal("cannot open audit"); /* Must prevent login */ |
| 326 |
} |
| 327 |
audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION, |
| 328 |
buf, NULL, get_remote_ipaddr(), NULL, 1); |
| 329 |
audit_close(audit_fd); |
| 330 |
/* do not abort if the error is EPERM and sshd is run as non root user */ |
| 331 |
if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) |
| 332 |
fatal("cannot write into audit"); /* Must prevent login */ |
| 333 |
#endif |
| 334 |
} |
| 335 |
|
| 336 |
void |
| 337 |
audit_session_key_free_body(int ctos, pid_t pid, uid_t uid) |
| 338 |
{ |
| 339 |
char buf[AUDIT_LOG_SIZE]; |
| 340 |
int audit_fd, audit_ok; |
| 341 |
char *s; |
| 342 |
|
| 343 |
snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ", |
| 344 |
direction[ctos], (intmax_t)pid, (intmax_t)uid, |
| 345 |
get_remote_port(), |
| 346 |
(s = get_local_ipaddr(packet_get_connection_in())), |
| 347 |
get_local_port()); |
| 348 |
free(s); |
| 349 |
audit_fd = audit_open(); |
| 350 |
if (audit_fd < 0) { |
| 351 |
if (errno != EINVAL && errno != EPROTONOSUPPORT && |
| 352 |
errno != EAFNOSUPPORT) |
| 353 |
error("cannot open audit"); |
| 354 |
return; |
| 355 |
} |
| 356 |
audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, |
| 357 |
buf, NULL, get_remote_ipaddr(), NULL, 1); |
| 358 |
audit_close(audit_fd); |
| 359 |
/* do not abort if the error is EPERM and sshd is run as non root user */ |
| 360 |
if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) |
| 361 |
error("cannot write into audit"); |
| 362 |
} |
| 363 |
|
| 364 |
void |
| 365 |
audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid) |
| 366 |
{ |
| 367 |
char buf[AUDIT_LOG_SIZE]; |
| 368 |
int audit_fd, audit_ok; |
| 369 |
|
| 370 |
snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ", |
| 371 |
fp, (intmax_t)pid, (intmax_t)uid); |
| 372 |
audit_fd = audit_open(); |
| 373 |
if (audit_fd < 0) { |
| 374 |
if (errno != EINVAL && errno != EPROTONOSUPPORT && |
| 375 |
errno != EAFNOSUPPORT) |
| 376 |
error("cannot open audit"); |
| 377 |
return; |
| 378 |
} |
| 379 |
audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, |
| 380 |
buf, NULL, |
| 381 |
listening_for_clients() ? NULL : get_remote_ipaddr(), |
| 382 |
NULL, 1); |
| 383 |
audit_close(audit_fd); |
| 384 |
/* do not abort if the error is EPERM and sshd is run as non root user */ |
| 385 |
if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) |
| 386 |
error("cannot write into audit"); |
| 387 |
} |
| 388 |
|
| 389 |
void |
| 390 |
audit_generate_ephemeral_server_key(const char *fp) |
| 391 |
{ |
| 392 |
char buf[AUDIT_LOG_SIZE]; |
| 393 |
int audit_fd, audit_ok; |
| 394 |
|
| 395 |
snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp); |
| 396 |
audit_fd = audit_open(); |
| 397 |
if (audit_fd < 0) { |
| 398 |
if (errno != EINVAL && errno != EPROTONOSUPPORT && |
| 399 |
errno != EAFNOSUPPORT) |
| 400 |
error("cannot open audit"); |
| 401 |
return; |
| 402 |
} |
| 403 |
audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER, |
| 404 |
buf, NULL, 0, NULL, 1); |
| 405 |
audit_close(audit_fd); |
| 406 |
/* do not abort if the error is EPERM and sshd is run as non root user */ |
| 407 |
if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) |
| 408 |
error("cannot write into audit"); |
| 409 |
} |
| 126 |
#endif /* USE_LINUX_AUDIT */ |
410 |
#endif /* USE_LINUX_AUDIT */ |