10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51676Sjpk * Common Development and Distribution License (the "License").
61676Sjpk * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12918SJan.Friedel@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
264321Scasper #include <sys/param.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <sys/fcntl.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/sockio.h>
360Sstevel@tonic-gate #include <netinet/in.h>
371676Sjpk #include <tsol/label.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include <bsm/audit.h>
400Sstevel@tonic-gate #include <bsm/audit_record.h>
410Sstevel@tonic-gate #include <bsm/audit_uevents.h>
420Sstevel@tonic-gate #include <bsm/libbsm.h>
430Sstevel@tonic-gate #include <bsm/audit_private.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate #include <locale.h>
460Sstevel@tonic-gate #include <pwd.h>
470Sstevel@tonic-gate #include <generic.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define BAD_PASSWD (1)
500Sstevel@tonic-gate #define UNKNOWN_USER (2)
510Sstevel@tonic-gate #define EXCLUDED_USER (3)
520Sstevel@tonic-gate #define NO_ANONYMOUS (4)
530Sstevel@tonic-gate #define MISC_FAILURE (5)
540Sstevel@tonic-gate
55*12918SJan.Friedel@Sun.COM static char luser[LOGNAME_MAX + 1];
560Sstevel@tonic-gate
570Sstevel@tonic-gate static void generate_record(char *, int, char *);
580Sstevel@tonic-gate static int selected(uid_t, char *, au_event_t, int);
590Sstevel@tonic-gate
600Sstevel@tonic-gate void
audit_ftpd_bad_pw(char * uname)610Sstevel@tonic-gate audit_ftpd_bad_pw(char *uname)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate if (cannot_audit(0)) {
640Sstevel@tonic-gate return;
650Sstevel@tonic-gate }
66*12918SJan.Friedel@Sun.COM (void) strncpy(luser, uname, LOGNAME_MAX);
67*12918SJan.Friedel@Sun.COM generate_record(luser, BAD_PASSWD, dgettext(bsm_dom, "bad password"));
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate
710Sstevel@tonic-gate void
audit_ftpd_unknown(char * uname)720Sstevel@tonic-gate audit_ftpd_unknown(char *uname)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate if (cannot_audit(0)) {
750Sstevel@tonic-gate return;
760Sstevel@tonic-gate }
77*12918SJan.Friedel@Sun.COM (void) strncpy(luser, uname, LOGNAME_MAX);
78*12918SJan.Friedel@Sun.COM generate_record(luser, UNKNOWN_USER, dgettext(bsm_dom, "unknown user"));
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate
820Sstevel@tonic-gate void
audit_ftpd_excluded(char * uname)830Sstevel@tonic-gate audit_ftpd_excluded(char *uname)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate if (cannot_audit(0)) {
860Sstevel@tonic-gate return;
870Sstevel@tonic-gate }
88*12918SJan.Friedel@Sun.COM (void) strncpy(luser, uname, LOGNAME_MAX);
890Sstevel@tonic-gate generate_record(luser, EXCLUDED_USER, dgettext(bsm_dom,
90*12918SJan.Friedel@Sun.COM "excluded user"));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate
940Sstevel@tonic-gate void
audit_ftpd_no_anon(void)950Sstevel@tonic-gate audit_ftpd_no_anon(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate if (cannot_audit(0)) {
980Sstevel@tonic-gate return;
990Sstevel@tonic-gate }
100*12918SJan.Friedel@Sun.COM generate_record("", NO_ANONYMOUS, dgettext(bsm_dom, "no anonymous"));
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate void
audit_ftpd_failure(char * uname)1040Sstevel@tonic-gate audit_ftpd_failure(char *uname)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate if (cannot_audit(0)) {
1070Sstevel@tonic-gate return;
1080Sstevel@tonic-gate }
109*12918SJan.Friedel@Sun.COM generate_record(uname, MISC_FAILURE, dgettext(bsm_dom, "misc failure"));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate void
audit_ftpd_success(char * uname)1130Sstevel@tonic-gate audit_ftpd_success(char *uname)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate if (cannot_audit(0)) {
1160Sstevel@tonic-gate return;
1170Sstevel@tonic-gate }
118*12918SJan.Friedel@Sun.COM (void) strncpy(luser, uname, LOGNAME_MAX);
1190Sstevel@tonic-gate generate_record(luser, 0, "");
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate static void
generate_record(char * locuser,int err,char * msg)1250Sstevel@tonic-gate generate_record(
1260Sstevel@tonic-gate char *locuser, /* username of local user */
1270Sstevel@tonic-gate int err, /* error status */
1280Sstevel@tonic-gate /* (=0 success, >0 error code) */
1290Sstevel@tonic-gate char *msg) /* error message */
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate int rd; /* audit record descriptor */
1320Sstevel@tonic-gate char buf[256]; /* temporary buffer */
1330Sstevel@tonic-gate uid_t uid;
1340Sstevel@tonic-gate gid_t gid;
1350Sstevel@tonic-gate uid_t ruid; /* real uid */
1360Sstevel@tonic-gate gid_t rgid; /* real gid */
1370Sstevel@tonic-gate pid_t pid;
1380Sstevel@tonic-gate struct passwd *pwd;
1390Sstevel@tonic-gate uid_t ceuid; /* current effective uid */
1400Sstevel@tonic-gate struct auditinfo_addr info;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate if (cannot_audit(0)) {
1430Sstevel@tonic-gate return;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate pwd = getpwnam(locuser);
1470Sstevel@tonic-gate if (pwd == NULL) {
1484321Scasper uid = (uid_t)-1;
1494321Scasper gid = (gid_t)-1;
1500Sstevel@tonic-gate } else {
1510Sstevel@tonic-gate uid = pwd->pw_uid;
1520Sstevel@tonic-gate gid = pwd->pw_gid;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate ceuid = geteuid(); /* save current euid */
1560Sstevel@tonic-gate (void) seteuid(0); /* change to root so you can audit */
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /* determine if we're preselected */
1590Sstevel@tonic-gate if (!selected(uid, locuser, AUE_ftpd, err)) {
1600Sstevel@tonic-gate (void) seteuid(ceuid);
1610Sstevel@tonic-gate return;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate ruid = getuid(); /* get real uid */
1650Sstevel@tonic-gate rgid = getgid(); /* get real gid */
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate pid = getpid();
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /* see if terminal id already set */
1700Sstevel@tonic-gate if (getaudit_addr(&info, sizeof (info)) < 0) {
1710Sstevel@tonic-gate perror("getaudit");
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate rd = au_open();
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* add subject token */
1770Sstevel@tonic-gate (void) au_write(rd, au_to_subject_ex(uid, uid, gid,
178*12918SJan.Friedel@Sun.COM ruid, rgid, pid, pid, &info.ai_termid));
1790Sstevel@tonic-gate
1801676Sjpk if (is_system_labeled())
1811676Sjpk (void) au_write(rd, au_to_mylabel());
1821676Sjpk
1830Sstevel@tonic-gate /* add return token */
1840Sstevel@tonic-gate errno = 0;
1850Sstevel@tonic-gate if (err) {
1860Sstevel@tonic-gate /* add reason for failure */
1870Sstevel@tonic-gate if (err == UNKNOWN_USER)
1880Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf),
1890Sstevel@tonic-gate "%s %s", msg, locuser);
1900Sstevel@tonic-gate else
1910Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s", msg);
1920Sstevel@tonic-gate (void) au_write(rd, au_to_text(buf));
1930Sstevel@tonic-gate #ifdef _LP64
1940Sstevel@tonic-gate (void) au_write(rd, au_to_return64(-1, (int64_t)err));
1950Sstevel@tonic-gate #else
1960Sstevel@tonic-gate (void) au_write(rd, au_to_return32(-1, (int32_t)err));
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate } else {
1990Sstevel@tonic-gate #ifdef _LP64
2000Sstevel@tonic-gate (void) au_write(rd, au_to_return64(0, (int64_t)0));
2010Sstevel@tonic-gate #else
2020Sstevel@tonic-gate (void) au_write(rd, au_to_return32(0, (int32_t)0));
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /* write audit record */
2070Sstevel@tonic-gate if (au_close(rd, 1, AUE_ftpd) < 0) {
2080Sstevel@tonic-gate (void) au_close(rd, 0, 0);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate (void) seteuid(ceuid);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate static int
selected(uid_t uid,char * locuser,au_event_t event,int err)2150Sstevel@tonic-gate selected(
2160Sstevel@tonic-gate uid_t uid,
2170Sstevel@tonic-gate char *locuser,
2180Sstevel@tonic-gate au_event_t event,
2190Sstevel@tonic-gate int err)
2200Sstevel@tonic-gate {
221*12918SJan.Friedel@Sun.COM int sorf;
222*12918SJan.Friedel@Sun.COM struct au_mask mask;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate mask.am_success = mask.am_failure = 0;
2254321Scasper if (uid > MAXEPHUID) {
226*12918SJan.Friedel@Sun.COM /* get non-attrib flags */
227*12918SJan.Friedel@Sun.COM (void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
2280Sstevel@tonic-gate } else {
229*12918SJan.Friedel@Sun.COM (void) au_user_mask(locuser, &mask);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
232*12918SJan.Friedel@Sun.COM if (err == 0) {
2330Sstevel@tonic-gate sorf = AU_PRS_SUCCESS;
234*12918SJan.Friedel@Sun.COM } else if (err >= 1) {
2350Sstevel@tonic-gate sorf = AU_PRS_FAILURE;
236*12918SJan.Friedel@Sun.COM } else {
2370Sstevel@tonic-gate sorf = AU_PRS_BOTH;
238*12918SJan.Friedel@Sun.COM }
239*12918SJan.Friedel@Sun.COM
240*12918SJan.Friedel@Sun.COM return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate void
audit_ftpd_logout(void)2450Sstevel@tonic-gate audit_ftpd_logout(void)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate int rd; /* audit record descriptor */
2480Sstevel@tonic-gate uid_t euid;
2490Sstevel@tonic-gate gid_t egid;
2500Sstevel@tonic-gate uid_t uid;
2510Sstevel@tonic-gate gid_t gid;
2520Sstevel@tonic-gate pid_t pid;
2530Sstevel@tonic-gate struct auditinfo_addr info;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (cannot_audit(0)) {
2560Sstevel@tonic-gate return;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate (void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /* see if terminal id already set */
2620Sstevel@tonic-gate if (getaudit_addr(&info, sizeof (info)) < 0) {
2630Sstevel@tonic-gate perror("getaudit");
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /* determine if we're preselected */
2670Sstevel@tonic-gate if (au_preselect(AUE_ftpd_logout, &info.ai_mask, AU_PRS_SUCCESS,
268*12918SJan.Friedel@Sun.COM AU_PRS_USECACHE) == 0) {
2690Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT,
2700Sstevel@tonic-gate NULL);
2710Sstevel@tonic-gate return;
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate euid = geteuid();
2750Sstevel@tonic-gate egid = getegid();
2760Sstevel@tonic-gate uid = getuid();
2770Sstevel@tonic-gate gid = getgid();
2780Sstevel@tonic-gate pid = getpid();
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate rd = au_open();
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate /* add subject token */
2830Sstevel@tonic-gate (void) au_write(rd, au_to_subject_ex(info.ai_auid, euid,
284*12918SJan.Friedel@Sun.COM egid, uid, gid, pid, pid, &info.ai_termid));
2850Sstevel@tonic-gate
2861676Sjpk if (is_system_labeled())
2871676Sjpk (void) au_write(rd, au_to_mylabel());
2881676Sjpk
2890Sstevel@tonic-gate /* add return token */
2900Sstevel@tonic-gate errno = 0;
2910Sstevel@tonic-gate #ifdef _LP64
2920Sstevel@tonic-gate (void) au_write(rd, au_to_return64(0, (int64_t)0));
2930Sstevel@tonic-gate #else
2940Sstevel@tonic-gate (void) au_write(rd, au_to_return32(0, (int32_t)0));
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /* write audit record */
2980Sstevel@tonic-gate if (au_close(rd, 1, AUE_ftpd_logout) < 0) {
2990Sstevel@tonic-gate (void) au_close(rd, 0, 0);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate (void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
3020Sstevel@tonic-gate }
303