]> andersk Git - openssh.git/blob - audit-bsm.c
- (dtucker) [audit-bsm.c] Add additional headers now required.
[openssh.git] / audit-bsm.c
1 /* $Id$ */
2
3 /*
4  * TODO
5  *
6  * - deal with overlap between this and sys_auth_allowed_user
7  *   sys_auth_record_login and record_failed_login.
8  */
9
10 /*
11  * Copyright 1988-2002 Sun Microsystems, Inc.  All rights reserved.
12  * Use is subject to license terms.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 /* #pragma ident        "@(#)bsmaudit.c 1.1     01/09/17 SMI" */
36
37 #include "includes.h"
38 #if defined(USE_BSM_AUDIT)
39
40 #include "ssh.h"
41 #include "log.h"
42 #include "key.h"
43 #include "hostfile.h"
44 #include "auth.h"
45 #include "xmalloc.h"
46
47 #ifndef AUE_openssh
48 # define AUE_openssh     32800
49 #endif
50 #include <bsm/audit.h>
51 #include <bsm/libbsm.h>
52 #include <bsm/audit_uevents.h>
53 #include <bsm/audit_record.h>
54 #include <locale.h>
55
56 #if defined(HAVE_GETAUDIT_ADDR)
57 #define AuditInfoStruct         auditinfo_addr
58 #define AuditInfoTermID         au_tid_addr_t
59 #define GetAuditFunc(a,b)       getaudit_addr((a),(b))
60 #define GetAuditFuncText        "getaudit_addr"
61 #define SetAuditFunc(a,b)       setaudit_addr((a),(b))
62 #define SetAuditFuncText        "setaudit_addr"
63 #define AUToSubjectFunc         au_to_subject_ex
64 #define AUToReturnFunc(a,b)     au_to_return32((a), (int32_t)(b))
65 #else
66 #define AuditInfoStruct         auditinfo
67 #define AuditInfoTermID         au_tid_t
68 #define GetAuditFunc(a,b)       getaudit(a)
69 #define GetAuditFuncText        "getaudit"
70 #define SetAuditFunc(a,b)       setaudit(a)
71 #define SetAuditFuncText        "setaudit"
72 #define AUToSubjectFunc         au_to_subject
73 #define AUToReturnFunc(a,b)     au_to_return((a), (u_int)(b))
74 #endif
75
76 extern int      cannot_audit(int);
77 extern void     aug_init(void);
78 extern dev_t    aug_get_port(void);
79 extern int      aug_get_machine(char *, u_int32_t *, u_int32_t *);
80 extern void     aug_save_auid(au_id_t);
81 extern void     aug_save_uid(uid_t);
82 extern void     aug_save_euid(uid_t);
83 extern void     aug_save_gid(gid_t);
84 extern void     aug_save_egid(gid_t);
85 extern void     aug_save_pid(pid_t);
86 extern void     aug_save_asid(au_asid_t);
87 extern void     aug_save_tid(dev_t, unsigned int);
88 extern void     aug_save_tid_ex(dev_t, u_int32_t *, u_int32_t);
89 extern int      aug_save_me(void);
90 extern int      aug_save_namask(void);
91 extern void     aug_save_event(au_event_t);
92 extern void     aug_save_sorf(int);
93 extern void     aug_save_text(char *);
94 extern void     aug_save_text1(char *);
95 extern void     aug_save_text2(char *);
96 extern void     aug_save_na(int);
97 extern void     aug_save_user(char *);
98 extern void     aug_save_path(char *);
99 extern int      aug_save_policy(void);
100 extern void     aug_save_afunc(int (*)(int));
101 extern int      aug_audit(void);
102 extern int      aug_na_selected(void);
103 extern int      aug_selected(void);
104 extern int      aug_daemon_session(void);
105
106 #ifndef HAVE_GETTEXT
107 # define gettext(a)     (a)
108 #endif
109
110 extern Authctxt *the_authctxt;
111 static AuditInfoTermID ssh_bsm_tid;
112
113 /* Below is the low-level BSM interface code */
114
115 /*
116  * Check if the specified event is selected (enabled) for auditing.
117  * Returns 1 if the event is selected, 0 if not and -1 on failure.
118  */
119 static int
120 selected(char *username, uid_t uid, au_event_t event, int sf)
121 {
122         int rc, sorf;
123         char naflags[512];
124         struct au_mask mask;
125
126         mask.am_success = mask.am_failure = 0;
127         if (uid < 0) {
128                 /* get flags for non-attributable (to a real user) events */
129                 rc = getacna(naflags, sizeof(naflags));
130                 if (rc == 0)
131                         (void) getauditflagsbin(naflags, &mask);
132         } else
133                 rc = au_user_mask(username, &mask);
134
135         sorf = (sf == 0) ? AU_PRS_SUCCESS : AU_PRS_FAILURE;
136         return(au_preselect(event, &mask, sorf, AU_PRS_REREAD));
137 }
138
139 static void
140 bsm_audit_record(int typ, char *string, au_event_t event_no)
141 {
142         int             ad, rc, sel;
143         uid_t           uid = -1;
144         gid_t           gid = -1;
145         pid_t           pid = getpid();
146         AuditInfoTermID tid = ssh_bsm_tid;
147
148         if (the_authctxt != NULL && the_authctxt->valid) {
149                 uid = the_authctxt->pw->pw_uid;
150                 gid = the_authctxt->pw->pw_gid;
151         }
152
153         rc = (typ == 0) ? 0 : -1;
154         sel = selected(the_authctxt->user, uid, event_no, rc);
155         debug3("BSM audit: typ %d rc %d \"%s\"", typ, rc, string);
156         if (!sel)
157                 return; /* audit event does not match mask, do not write */
158
159         debug3("BSM audit: writing audit new record");
160         ad = au_open();
161
162         (void) au_write(ad, AUToSubjectFunc(uid, uid, gid, uid, gid,
163             pid, pid, &tid));
164         (void) au_write(ad, au_to_text(string));
165         (void) au_write(ad, AUToReturnFunc(typ, rc));
166
167         rc = au_close(ad, AU_TO_WRITE, event_no);
168         if (rc < 0)
169                 error("BSM audit: %s failed to write \"%s\" record: %s",
170                     __func__, string, strerror(errno));
171 }
172
173 static void
174 bsm_audit_session_setup(void)
175 {
176         int rc;
177         struct AuditInfoStruct info;
178         au_mask_t mask;
179
180         if (the_authctxt == NULL) {
181                 error("BSM audit: session setup internal error (NULL ctxt)");
182                 return;
183         }
184
185         if (the_authctxt->valid)
186                 info.ai_auid = the_authctxt->pw->pw_uid;
187         else
188                 info.ai_auid = -1;
189         info.ai_asid = getpid();
190         mask.am_success = 0;
191         mask.am_failure = 0;
192
193         (void) au_user_mask(the_authctxt->user, &mask);
194
195         info.ai_mask.am_success  = mask.am_success;
196         info.ai_mask.am_failure  = mask.am_failure;
197
198         info.ai_termid = ssh_bsm_tid;
199
200         rc = SetAuditFunc(&info, sizeof(info));
201         if (rc < 0)
202                 error("BSM audit: %s: %s failed: %s", __func__,
203                     SetAuditFuncText, strerror(errno));
204 }
205
206 static void
207 bsm_audit_bad_login(const char *what)
208 {
209         char textbuf[BSM_TEXTBUFSZ];
210
211         if (the_authctxt->valid) {
212                 (void) snprintf(textbuf, sizeof (textbuf),
213                         gettext("invalid %s for user %s"),
214                             what, the_authctxt->user);
215                 bsm_audit_record(4, textbuf, AUE_openssh);
216         } else {
217                 (void) snprintf(textbuf, sizeof (textbuf),
218                         gettext("invalid user name \"%s\""),
219                             the_authctxt->user);
220                 bsm_audit_record(3, textbuf, AUE_openssh);
221         }
222 }
223
224 /* Below is the sshd audit API code */
225
226 void
227 audit_connection_from(const char *host, int port)
228 {
229         AuditInfoTermID *tid = &ssh_bsm_tid;
230         char buf[1024];
231
232         if (cannot_audit(0))
233                 return;
234         debug3("BSM audit: connection from %.100s port %d", host, port);
235
236         /* populate our terminal id structure */
237 #if defined(HAVE_GETAUDIT_ADDR)
238         tid->at_port = (dev_t)port;
239         aug_get_machine((char *)host, &(tid->at_addr[0]), &(tid->at_type));
240         snprintf(buf, sizeof(buf), "%08x %08x %08x %08x", tid->at_addr[0],
241             tid->at_addr[1], tid->at_addr[2], tid->at_addr[3]);
242         debug3("BSM audit: iptype %d machine ID %s", (int)tid->at_type, buf);
243 #else
244         /* this is used on IPv4-only machines */
245         tid->port = (dev_t)port;
246         tid->machine = inet_addr(host);
247         snprintf(buf, sizeof(buf), "%08x", tid->machine);
248         debug3("BSM audit: machine ID %s", buf);
249 #endif
250 }
251
252 void
253 audit_run_command(const char *command)
254 {
255         /* not implemented */
256 }
257
258 void
259 audit_session_open(const char *ttyn)
260 {
261         /* not implemented */
262 }
263
264 void
265 audit_session_close(const char *ttyn)
266 {
267         /* not implemented */
268 }
269
270 void
271 audit_event(ssh_audit_event_t event)
272 {
273         char    textbuf[BSM_TEXTBUFSZ];
274         static int logged_in = 0;
275         const char *user = the_authctxt ? the_authctxt->user : "(unknown user)";
276
277         if (cannot_audit(0))
278                 return;
279
280         switch(event) {
281         case SSH_AUTH_SUCCESS:
282                 logged_in = 1;
283                 bsm_audit_session_setup();
284                 snprintf(textbuf, sizeof(textbuf),
285                     gettext("successful login %s"), user);
286                 bsm_audit_record(0, textbuf, AUE_openssh);
287                 break;
288
289         case SSH_CONNECTION_CLOSE:
290                 /*
291                  * We can also get a close event if the user attempted auth
292                  * but never succeeded.
293                  */
294                 if (logged_in) {
295                         snprintf(textbuf, sizeof(textbuf),
296                             gettext("sshd logout %s"), the_authctxt->user);
297                         bsm_audit_record(0, textbuf, AUE_logout);
298                 } else {
299                         debug("%s: connection closed without authentication",
300                             __func__);
301                 }
302                 break;
303
304         case SSH_NOLOGIN:
305                 bsm_audit_record(1,
306                     gettext("logins disabled by /etc/nologin"), AUE_openssh);
307                 break;
308
309         case SSH_LOGIN_EXCEED_MAXTRIES:
310                 snprintf(textbuf, sizeof(textbuf),
311                     gettext("too many tries for user %s"), the_authctxt->user);
312                 bsm_audit_record(1, textbuf, AUE_openssh);
313                 break;
314
315         case SSH_LOGIN_ROOT_DENIED:
316                 bsm_audit_record(2, gettext("not_console"), AUE_openssh);
317                 break;
318
319         case SSH_AUTH_FAIL_PASSWD:
320                 bsm_audit_bad_login("password");
321                 break;
322
323         case SSH_AUTH_FAIL_KBDINT:
324                 bsm_audit_bad_login("interactive password entry");
325                 break;
326
327         default:
328                 debug("%s: unhandled event %d", __func__, event);
329         }
330 }
331 #endif /* BSM */
This page took 0.063603 seconds and 5 git commands to generate.