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*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
29*4321Scasper #include <sys/param.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <sys/fcntl.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <syslog.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/socket.h>
380Sstevel@tonic-gate #include <sys/sockio.h>
390Sstevel@tonic-gate #include <netinet/in.h>
401676Sjpk #include <tsol/label.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <bsm/audit.h>
430Sstevel@tonic-gate #include <bsm/audit_record.h>
440Sstevel@tonic-gate #include <bsm/audit_uevents.h>
450Sstevel@tonic-gate #include <bsm/libbsm.h>
460Sstevel@tonic-gate #include <bsm/audit_private.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <locale.h>
490Sstevel@tonic-gate #include <pwd.h>
500Sstevel@tonic-gate #include <generic.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	BAD_PASSWD	(1)
530Sstevel@tonic-gate #define	UNKNOWN_USER	(2)
540Sstevel@tonic-gate #define	EXCLUDED_USER	(3)
550Sstevel@tonic-gate #define	NO_ANONYMOUS	(4)
560Sstevel@tonic-gate #define	MISC_FAILURE	(5)
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static char		luser[16];
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void generate_record(char *, int, char *);
610Sstevel@tonic-gate static int selected(uid_t, char *, au_event_t, int);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate void
640Sstevel@tonic-gate audit_ftpd_bad_pw(char *uname)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	if (cannot_audit(0)) {
670Sstevel@tonic-gate 		return;
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	(void) strncpy(luser, uname, 8);
700Sstevel@tonic-gate 	luser[8] = '\0';
710Sstevel@tonic-gate 	generate_record(luser, BAD_PASSWD, dgettext(bsm_dom,
720Sstevel@tonic-gate 		"bad password"));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
760Sstevel@tonic-gate void
770Sstevel@tonic-gate audit_ftpd_unknown(char	*uname)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	if (cannot_audit(0)) {
800Sstevel@tonic-gate 		return;
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	(void) strncpy(luser, uname, 8);
830Sstevel@tonic-gate 	luser[8] = '\0';
840Sstevel@tonic-gate 	generate_record(luser, UNKNOWN_USER, dgettext(bsm_dom,
850Sstevel@tonic-gate 		"unknown user"));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate void
900Sstevel@tonic-gate audit_ftpd_excluded(char *uname)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	if (cannot_audit(0)) {
930Sstevel@tonic-gate 		return;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	(void) strncpy(luser, uname, 8);
960Sstevel@tonic-gate 	luser[8] = '\0';
970Sstevel@tonic-gate 	generate_record(luser, EXCLUDED_USER, dgettext(bsm_dom,
980Sstevel@tonic-gate 		"excluded user"));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate void
1030Sstevel@tonic-gate audit_ftpd_no_anon(void)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	if (cannot_audit(0)) {
1060Sstevel@tonic-gate 		return;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	generate_record("", NO_ANONYMOUS, dgettext(bsm_dom,
1090Sstevel@tonic-gate 		"no anonymous"));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate void
1130Sstevel@tonic-gate audit_ftpd_failure(char *uname)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	if (cannot_audit(0)) {
1160Sstevel@tonic-gate 		return;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	generate_record(uname, MISC_FAILURE, dgettext(bsm_dom,
1190Sstevel@tonic-gate 		"misc failure"));
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate void
1230Sstevel@tonic-gate audit_ftpd_success(char	*uname)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	if (cannot_audit(0)) {
1260Sstevel@tonic-gate 		return;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	(void) strncpy(luser, uname, 8);
1290Sstevel@tonic-gate 	luser[8] = '\0';
1300Sstevel@tonic-gate 	generate_record(luser, 0, "");
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static void
1360Sstevel@tonic-gate generate_record(
1370Sstevel@tonic-gate 		char	*locuser,	/* username of local user */
1380Sstevel@tonic-gate 		int	err,		/* error status */
1390Sstevel@tonic-gate 					/* (=0 success, >0 error code) */
1400Sstevel@tonic-gate 		char	*msg)		/* error message */
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
1430Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
1440Sstevel@tonic-gate 	uid_t	uid;
1450Sstevel@tonic-gate 	gid_t	gid;
1460Sstevel@tonic-gate 	uid_t	ruid;		/* real uid */
1470Sstevel@tonic-gate 	gid_t	rgid;		/* real gid */
1480Sstevel@tonic-gate 	pid_t	pid;
1490Sstevel@tonic-gate 	struct passwd *pwd;
1500Sstevel@tonic-gate 	uid_t	ceuid;		/* current effective uid */
1510Sstevel@tonic-gate 	struct auditinfo_addr info;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (cannot_audit(0)) {
1540Sstevel@tonic-gate 		return;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	pwd = getpwnam(locuser);
1580Sstevel@tonic-gate 	if (pwd == NULL) {
159*4321Scasper 		uid = (uid_t)-1;
160*4321Scasper 		gid = (gid_t)-1;
1610Sstevel@tonic-gate 	} else {
1620Sstevel@tonic-gate 		uid = pwd->pw_uid;
1630Sstevel@tonic-gate 		gid = pwd->pw_gid;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	ceuid = geteuid();	/* save current euid */
1670Sstevel@tonic-gate 	(void) seteuid(0);	/* change to root so you can audit */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/* determine if we're preselected */
1700Sstevel@tonic-gate 	if (!selected(uid, locuser, AUE_ftpd, err)) {
1710Sstevel@tonic-gate 		(void) seteuid(ceuid);
1720Sstevel@tonic-gate 		return;
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	ruid = getuid();	/* get real uid */
1760Sstevel@tonic-gate 	rgid = getgid();	/* get real gid */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	pid = getpid();
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* see if terminal id already set */
1810Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
1820Sstevel@tonic-gate 		perror("getaudit");
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	rd = au_open();
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/* add subject token */
1880Sstevel@tonic-gate 	(void) au_write(rd, au_to_subject_ex(uid, uid, gid,
1890Sstevel@tonic-gate 		ruid, rgid, pid, pid, &info.ai_termid));
1900Sstevel@tonic-gate 
1911676Sjpk 	if (is_system_labeled())
1921676Sjpk 		(void) au_write(rd, au_to_mylabel());
1931676Sjpk 
1940Sstevel@tonic-gate 	/* add return token */
1950Sstevel@tonic-gate 	errno = 0;
1960Sstevel@tonic-gate 	if (err) {
1970Sstevel@tonic-gate 		/* add reason for failure */
1980Sstevel@tonic-gate 		if (err == UNKNOWN_USER)
1990Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
2000Sstevel@tonic-gate 			    "%s %s", msg, locuser);
2010Sstevel@tonic-gate 		else
2020Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", msg);
2030Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(buf));
2040Sstevel@tonic-gate #ifdef _LP64
2050Sstevel@tonic-gate 		(void) au_write(rd, au_to_return64(-1, (int64_t)err));
2060Sstevel@tonic-gate #else
2070Sstevel@tonic-gate 		(void) au_write(rd, au_to_return32(-1, (int32_t)err));
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate 	} else {
2100Sstevel@tonic-gate #ifdef _LP64
2110Sstevel@tonic-gate 		(void) au_write(rd, au_to_return64(0, (int64_t)0));
2120Sstevel@tonic-gate #else
2130Sstevel@tonic-gate 		(void) au_write(rd, au_to_return32(0, (int32_t)0));
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/* write audit record */
2180Sstevel@tonic-gate 	if (au_close(rd, 1, AUE_ftpd) < 0) {
2190Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	(void) seteuid(ceuid);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static int
2260Sstevel@tonic-gate selected(
2270Sstevel@tonic-gate 	uid_t		uid,
2280Sstevel@tonic-gate 	char		*locuser,
2290Sstevel@tonic-gate 	au_event_t	event,
2300Sstevel@tonic-gate 	int	err)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	int	rc, sorf;
2330Sstevel@tonic-gate 	char	naflags[512];
2340Sstevel@tonic-gate 	struct au_mask mask;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	mask.am_success = mask.am_failure = 0;
237*4321Scasper 	if (uid > MAXEPHUID) {
2380Sstevel@tonic-gate 		rc = getacna(naflags, 256); /* get non-attrib flags */
2390Sstevel@tonic-gate 		if (rc == 0)
2400Sstevel@tonic-gate 			(void) getauditflagsbin(naflags, &mask);
2410Sstevel@tonic-gate 	} else {
2420Sstevel@tonic-gate 		rc = au_user_mask(locuser, &mask);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (err == 0)
2460Sstevel@tonic-gate 		sorf = AU_PRS_SUCCESS;
2470Sstevel@tonic-gate 	else if (err >= 1)
2480Sstevel@tonic-gate 		sorf = AU_PRS_FAILURE;
2490Sstevel@tonic-gate 	else
2500Sstevel@tonic-gate 		sorf = AU_PRS_BOTH;
2510Sstevel@tonic-gate 	rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
2520Sstevel@tonic-gate 	return (rc);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate void
2570Sstevel@tonic-gate audit_ftpd_logout(void)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
2600Sstevel@tonic-gate 	uid_t	euid;
2610Sstevel@tonic-gate 	gid_t	egid;
2620Sstevel@tonic-gate 	uid_t	uid;
2630Sstevel@tonic-gate 	gid_t	gid;
2640Sstevel@tonic-gate 	pid_t	pid;
2650Sstevel@tonic-gate 	struct auditinfo_addr info;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (cannot_audit(0)) {
2680Sstevel@tonic-gate 		return;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	(void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/* see if terminal id already set */
2740Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
2750Sstevel@tonic-gate 		perror("getaudit");
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* determine if we're preselected */
2790Sstevel@tonic-gate 	if (au_preselect(AUE_ftpd_logout, &info.ai_mask, AU_PRS_SUCCESS,
2800Sstevel@tonic-gate 		AU_PRS_USECACHE) == 0) {
2810Sstevel@tonic-gate 		(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT,
2820Sstevel@tonic-gate 		    NULL);
2830Sstevel@tonic-gate 		return;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	euid = geteuid();
2870Sstevel@tonic-gate 	egid = getegid();
2880Sstevel@tonic-gate 	uid = getuid();
2890Sstevel@tonic-gate 	gid = getgid();
2900Sstevel@tonic-gate 	pid = getpid();
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	rd = au_open();
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* add subject token */
2950Sstevel@tonic-gate 	(void) au_write(rd, au_to_subject_ex(info.ai_auid, euid,
2960Sstevel@tonic-gate 		egid, uid, gid, pid, pid, &info.ai_termid));
2970Sstevel@tonic-gate 
2981676Sjpk 	if (is_system_labeled())
2991676Sjpk 		(void) au_write(rd, au_to_mylabel());
3001676Sjpk 
3010Sstevel@tonic-gate 	/* add return token */
3020Sstevel@tonic-gate 	errno = 0;
3030Sstevel@tonic-gate #ifdef _LP64
3040Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(0, (int64_t)0));
3050Sstevel@tonic-gate #else
3060Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(0, (int32_t)0));
3070Sstevel@tonic-gate #endif
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	/* write audit record */
3100Sstevel@tonic-gate 	if (au_close(rd, 1, AUE_ftpd_logout) < 0) {
3110Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
3140Sstevel@tonic-gate }
315