audit-bsm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * TODO
  3. *
  4. * - deal with overlap between this and sys_auth_allowed_user
  5. * sys_auth_record_login and record_failed_login.
  6. */
  7. /*
  8. * Copyright 1988-2002 Sun Microsystems, Inc. All rights reserved.
  9. * Use is subject to license terms.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. /* #pragma ident "@(#)bsmaudit.c 1.1 01/09/17 SMI" */
  33. #include "includes.h"
  34. #if defined(USE_BSM_AUDIT)
  35. #include <sys/types.h>
  36. #include <errno.h>
  37. #include <netdb.h>
  38. #include <stdarg.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #ifdef BROKEN_BSM_API
  42. #include <libscf.h>
  43. #endif
  44. #include "ssh.h"
  45. #include "log.h"
  46. #include "hostfile.h"
  47. #include "auth.h"
  48. #include "xmalloc.h"
  49. #ifndef AUE_openssh
  50. # define AUE_openssh 32800
  51. #endif
  52. #include <bsm/audit.h>
  53. #include <bsm/libbsm.h>
  54. #include <bsm/audit_uevents.h>
  55. #include <bsm/audit_record.h>
  56. #include <locale.h>
  57. #if defined(HAVE_GETAUDIT_ADDR)
  58. #define AuditInfoStruct auditinfo_addr
  59. #define AuditInfoTermID au_tid_addr_t
  60. #define SetAuditFunc(a,b) setaudit_addr((a),(b))
  61. #define SetAuditFuncText "setaudit_addr"
  62. #define AUToSubjectFunc au_to_subject_ex
  63. #define AUToReturnFunc(a,b) au_to_return32((a), (int32_t)(b))
  64. #else
  65. #define AuditInfoStruct auditinfo
  66. #define AuditInfoTermID au_tid_t
  67. #define SetAuditFunc(a,b) setaudit(a)
  68. #define SetAuditFuncText "setaudit"
  69. #define AUToSubjectFunc au_to_subject
  70. #define AUToReturnFunc(a,b) au_to_return((a), (u_int)(b))
  71. #endif
  72. #ifndef cannot_audit
  73. extern int cannot_audit(int);
  74. #endif
  75. extern void aug_init(void);
  76. extern void aug_save_auid(au_id_t);
  77. extern void aug_save_uid(uid_t);
  78. extern void aug_save_euid(uid_t);
  79. extern void aug_save_gid(gid_t);
  80. extern void aug_save_egid(gid_t);
  81. extern void aug_save_pid(pid_t);
  82. extern void aug_save_asid(au_asid_t);
  83. extern void aug_save_tid(dev_t, unsigned int);
  84. extern void aug_save_tid_ex(dev_t, u_int32_t *, u_int32_t);
  85. extern int aug_save_me(void);
  86. extern int aug_save_namask(void);
  87. extern void aug_save_event(au_event_t);
  88. extern void aug_save_sorf(int);
  89. extern void aug_save_text(char *);
  90. extern void aug_save_text1(char *);
  91. extern void aug_save_text2(char *);
  92. extern void aug_save_na(int);
  93. extern void aug_save_user(char *);
  94. extern void aug_save_path(char *);
  95. extern int aug_save_policy(void);
  96. extern void aug_save_afunc(int (*)(int));
  97. extern int aug_audit(void);
  98. extern int aug_na_selected(void);
  99. extern int aug_selected(void);
  100. extern int aug_daemon_session(void);
  101. #ifndef HAVE_GETTEXT
  102. # define gettext(a) (a)
  103. #endif
  104. extern Authctxt *the_authctxt;
  105. static AuditInfoTermID ssh_bsm_tid;
  106. #ifdef BROKEN_BSM_API
  107. /* For some reason this constant is no longer defined
  108. in Solaris 11. */
  109. #define BSM_TEXTBUFSZ 256
  110. #endif
  111. /* Below is the low-level BSM interface code */
  112. /*
  113. * aug_get_machine is only required on IPv6 capable machines, we use a
  114. * different mechanism in audit_connection_from() for IPv4-only machines.
  115. * getaudit_addr() is only present on IPv6 capable machines.
  116. */
  117. #if defined(HAVE_AUG_GET_MACHINE) || !defined(HAVE_GETAUDIT_ADDR)
  118. extern int aug_get_machine(char *, u_int32_t *, u_int32_t *);
  119. #else
  120. static int
  121. aug_get_machine(char *host, u_int32_t *addr, u_int32_t *type)
  122. {
  123. struct addrinfo *ai;
  124. struct sockaddr_in *in4;
  125. struct sockaddr_in6 *in6;
  126. int ret = 0, r;
  127. if ((r = getaddrinfo(host, NULL, NULL, &ai)) != 0) {
  128. error("BSM audit: getaddrinfo failed for %.100s: %.100s", host,
  129. r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
  130. return -1;
  131. }
  132. switch (ai->ai_family) {
  133. case AF_INET:
  134. in4 = (struct sockaddr_in *)ai->ai_addr;
  135. *type = AU_IPv4;
  136. memcpy(addr, &in4->sin_addr, sizeof(struct in_addr));
  137. break;
  138. #ifdef AU_IPv6
  139. case AF_INET6:
  140. in6 = (struct sockaddr_in6 *)ai->ai_addr;
  141. *type = AU_IPv6;
  142. memcpy(addr, &in6->sin6_addr, sizeof(struct in6_addr));
  143. break;
  144. #endif
  145. default:
  146. error("BSM audit: unknown address family for %.100s: %d",
  147. host, ai->ai_family);
  148. ret = -1;
  149. }
  150. freeaddrinfo(ai);
  151. return ret;
  152. }
  153. #endif
  154. #ifdef BROKEN_BSM_API
  155. /*
  156. In Solaris 11 the audit daemon has been moved to SMF. In the process
  157. they simply dropped getacna() from the API, since it read from a now
  158. non-existent config file. This function re-implements getacna() to
  159. read from the SMF repository instead.
  160. */
  161. int
  162. getacna(char *auditstring, int len)
  163. {
  164. scf_handle_t *handle = NULL;
  165. scf_property_t *property = NULL;
  166. scf_value_t *value = NULL;
  167. int ret = 0;
  168. handle = scf_handle_create(SCF_VERSION);
  169. if (handle == NULL)
  170. return -2; /* The man page for getacna on Solaris 10 states
  171. we should return -2 in case of error and set
  172. errno to indicate the error. We don't bother
  173. with errno here, though, since the only use
  174. of this function below doesn't check for errors
  175. anyway.
  176. */
  177. ret = scf_handle_bind(handle);
  178. if (ret == -1)
  179. return -2;
  180. property = scf_property_create(handle);
  181. if (property == NULL)
  182. return -2;
  183. ret = scf_handle_decode_fmri(handle,
  184. "svc:/system/auditd:default/:properties/preselection/naflags",
  185. NULL, NULL, NULL, NULL, property, 0);
  186. if (ret == -1)
  187. return -2;
  188. value = scf_value_create(handle);
  189. if (value == NULL)
  190. return -2;
  191. ret = scf_property_get_value(property, value);
  192. if (ret == -1)
  193. return -2;
  194. ret = scf_value_get_astring(value, auditstring, len);
  195. if (ret == -1)
  196. return -2;
  197. scf_value_destroy(value);
  198. scf_property_destroy(property);
  199. scf_handle_destroy(handle);
  200. return 0;
  201. }
  202. #endif
  203. /*
  204. * Check if the specified event is selected (enabled) for auditing.
  205. * Returns 1 if the event is selected, 0 if not and -1 on failure.
  206. */
  207. static int
  208. selected(char *username, uid_t uid, au_event_t event, int sf)
  209. {
  210. int rc, sorf;
  211. char naflags[512];
  212. struct au_mask mask;
  213. mask.am_success = mask.am_failure = 0;
  214. if (uid < 0) {
  215. /* get flags for non-attributable (to a real user) events */
  216. rc = getacna(naflags, sizeof(naflags));
  217. if (rc == 0)
  218. (void) getauditflagsbin(naflags, &mask);
  219. } else
  220. rc = au_user_mask(username, &mask);
  221. sorf = (sf == 0) ? AU_PRS_SUCCESS : AU_PRS_FAILURE;
  222. return(au_preselect(event, &mask, sorf, AU_PRS_REREAD));
  223. }
  224. static void
  225. bsm_audit_record(int typ, char *string, au_event_t event_no)
  226. {
  227. int ad, rc, sel;
  228. uid_t uid = -1;
  229. gid_t gid = -1;
  230. pid_t pid = getpid();
  231. AuditInfoTermID tid = ssh_bsm_tid;
  232. if (the_authctxt != NULL && the_authctxt->valid) {
  233. uid = the_authctxt->pw->pw_uid;
  234. gid = the_authctxt->pw->pw_gid;
  235. }
  236. rc = (typ == 0) ? 0 : -1;
  237. sel = selected(the_authctxt->user, uid, event_no, rc);
  238. debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string);
  239. if (!sel)
  240. return; /* audit event does not match mask, do not write */
  241. debug3("BSM audit: writing audit new record");
  242. ad = au_open();
  243. (void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid,
  244. pid, pid, &tid));
  245. (void) au_write(ad, au_to_text(string));
  246. (void) au_write(ad, AUToReturnFunc(typ, rc));
  247. #ifdef BROKEN_BSM_API
  248. /* The last argument is the event modifier flags. For
  249. some seemingly undocumented reason it was added in
  250. Solaris 11. */
  251. rc = au_close(ad, AU_TO_WRITE, event_no, 0);
  252. #else
  253. rc = au_close(ad, AU_TO_WRITE, event_no);
  254. #endif
  255. if (rc < 0)
  256. error("BSM audit: %s failed to write \"%s\" record: %s",
  257. __func__, string, strerror(errno));
  258. }
  259. static void
  260. bsm_audit_session_setup(void)
  261. {
  262. int rc;
  263. struct AuditInfoStruct info;
  264. au_mask_t mask;
  265. if (the_authctxt == NULL) {
  266. error("BSM audit: session setup internal error (NULL ctxt)");
  267. return;
  268. }
  269. if (the_authctxt->valid)
  270. info.ai_auid = the_authctxt->pw->pw_uid;
  271. else
  272. info.ai_auid = -1;
  273. info.ai_asid = getpid();
  274. mask.am_success = 0;
  275. mask.am_failure = 0;
  276. (void) au_user_mask(the_authctxt->user, &mask);
  277. info.ai_mask.am_success = mask.am_success;
  278. info.ai_mask.am_failure = mask.am_failure;
  279. info.ai_termid = ssh_bsm_tid;
  280. rc = SetAuditFunc(&info, sizeof(info));
  281. if (rc < 0)
  282. error("BSM audit: %s: %s failed: %s", __func__,
  283. SetAuditFuncText, strerror(errno));
  284. }
  285. static void
  286. bsm_audit_bad_login(const char *what)
  287. {
  288. char textbuf[BSM_TEXTBUFSZ];
  289. if (the_authctxt->valid) {
  290. (void) snprintf(textbuf, sizeof (textbuf),
  291. gettext("invalid %s for user %s"),
  292. what, the_authctxt->user);
  293. bsm_audit_record(4, textbuf, AUE_openssh);
  294. } else {
  295. (void) snprintf(textbuf, sizeof (textbuf),
  296. gettext("invalid user name \"%s\""),
  297. the_authctxt->user);
  298. bsm_audit_record(3, textbuf, AUE_openssh);
  299. }
  300. }
  301. /* Below is the sshd audit API code */
  302. void
  303. audit_connection_from(const char *host, int port)
  304. {
  305. AuditInfoTermID *tid = &ssh_bsm_tid;
  306. char buf[1024];
  307. if (cannot_audit(0))
  308. return;
  309. debug3("BSM audit: connection from %.100s port %d", host, port);
  310. /* populate our terminal id structure */
  311. #if defined(HAVE_GETAUDIT_ADDR)
  312. tid->at_port = (dev_t)port;
  313. aug_get_machine((char *)host, &(tid->at_addr[0]), &(tid->at_type));
  314. snprintf(buf, sizeof(buf), "%08x %08x %08x %08x", tid->at_addr[0],
  315. tid->at_addr[1], tid->at_addr[2], tid->at_addr[3]);
  316. debug3("BSM audit: iptype %d machine ID %s", (int)tid->at_type, buf);
  317. #else
  318. /* this is used on IPv4-only machines */
  319. tid->port = (dev_t)port;
  320. tid->machine = inet_addr(host);
  321. snprintf(buf, sizeof(buf), "%08x", tid->machine);
  322. debug3("BSM audit: machine ID %s", buf);
  323. #endif
  324. }
  325. int
  326. audit_run_command(struct ssh *ssh, const char *command)
  327. {
  328. /* not implemented */
  329. return 0;
  330. }
  331. void
  332. audit_end_command(struct ssh *ssh, int handle, const char *command)
  333. {
  334. /* not implemented */
  335. }
  336. void
  337. audit_count_session_open(void)
  338. {
  339. /* not necessary */
  340. }
  341. void
  342. audit_session_open(struct logininfo *li)
  343. {
  344. /* not implemented */
  345. }
  346. void
  347. audit_session_close(struct logininfo *li)
  348. {
  349. /* not implemented */
  350. }
  351. int
  352. audit_keyusage(struct ssh *ssh, int host_user, char *fp, int rv)
  353. {
  354. /* not implemented */
  355. }
  356. void
  357. audit_event(struct ssh *ssh, ssh_audit_event_t event)
  358. {
  359. char textbuf[BSM_TEXTBUFSZ];
  360. static int logged_in = 0;
  361. const char *user = the_authctxt ? the_authctxt->user : "(unknown user)";
  362. if (cannot_audit(0))
  363. return;
  364. switch(event) {
  365. case SSH_AUTH_SUCCESS:
  366. logged_in = 1;
  367. bsm_audit_session_setup();
  368. snprintf(textbuf, sizeof(textbuf),
  369. gettext("successful login %s"), user);
  370. bsm_audit_record(0, textbuf, AUE_openssh);
  371. break;
  372. case SSH_CONNECTION_CLOSE:
  373. /*
  374. * We can also get a close event if the user attempted auth
  375. * but never succeeded.
  376. */
  377. if (logged_in) {
  378. snprintf(textbuf, sizeof(textbuf),
  379. gettext("sshd logout %s"), the_authctxt->user);
  380. bsm_audit_record(0, textbuf, AUE_logout);
  381. } else {
  382. debug("%s: connection closed without authentication",
  383. __func__);
  384. }
  385. break;
  386. case SSH_NOLOGIN:
  387. bsm_audit_record(1,
  388. gettext("logins disabled by /etc/nologin"), AUE_openssh);
  389. break;
  390. case SSH_LOGIN_EXCEED_MAXTRIES:
  391. snprintf(textbuf, sizeof(textbuf),
  392. gettext("too many tries for user %s"), the_authctxt->user);
  393. bsm_audit_record(1, textbuf, AUE_openssh);
  394. break;
  395. case SSH_LOGIN_ROOT_DENIED:
  396. bsm_audit_record(2, gettext("not_console"), AUE_openssh);
  397. break;
  398. case SSH_AUTH_FAIL_PASSWD:
  399. bsm_audit_bad_login("password");
  400. break;
  401. case SSH_AUTH_FAIL_KBDINT:
  402. bsm_audit_bad_login("interactive password entry");
  403. break;
  404. default:
  405. debug("%s: unhandled event %d", __func__, event);
  406. }
  407. }
  408. void
  409. audit_unsupported_body(struct ssh *ssh, int what)
  410. {
  411. /* not implemented */
  412. }
  413. void
  414. audit_kex_body(struct ssh *ssh, int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
  415. {
  416. /* not implemented */
  417. }
  418. void
  419. audit_session_key_free_body(struct ssh * ssh, int ctos, pid_t pid, uid_t uid)
  420. {
  421. /* not implemented */
  422. }
  423. void
  424. audit_destroy_sensitive_data(struct ssh *ssh, const char *fp, pid_t pid, uid_t uid)
  425. {
  426. /* not implemented */
  427. }
  428. #endif /* BSM */