xref: /titanic_51/usr/src/lib/libbsm/common/audit_ftpd.c (revision f89940742f5d14dde79b69b98a414dd7b7f585c7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f8994074SJan Friedel  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
26f48205beScasper #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
3745916cd2Sjpk #include <tsol/label.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
407c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
417c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
427c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
437c478bd9Sstevel@tonic-gate #include <bsm/audit_private.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <pwd.h>
477c478bd9Sstevel@tonic-gate #include <generic.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	BAD_PASSWD	(1)
507c478bd9Sstevel@tonic-gate #define	UNKNOWN_USER	(2)
517c478bd9Sstevel@tonic-gate #define	EXCLUDED_USER	(3)
527c478bd9Sstevel@tonic-gate #define	NO_ANONYMOUS	(4)
537c478bd9Sstevel@tonic-gate #define	MISC_FAILURE	(5)
547c478bd9Sstevel@tonic-gate 
55*f8994074SJan Friedel static char		luser[LOGNAME_MAX + 1];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static void generate_record(char *, int, char *);
587c478bd9Sstevel@tonic-gate static int selected(uid_t, char *, au_event_t, int);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
617c478bd9Sstevel@tonic-gate audit_ftpd_bad_pw(char *uname)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
647c478bd9Sstevel@tonic-gate 		return;
657c478bd9Sstevel@tonic-gate 	}
66*f8994074SJan Friedel 	(void) strncpy(luser, uname, LOGNAME_MAX);
67*f8994074SJan Friedel 	generate_record(luser, BAD_PASSWD, dgettext(bsm_dom, "bad password"));
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
727c478bd9Sstevel@tonic-gate audit_ftpd_unknown(char	*uname)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
757c478bd9Sstevel@tonic-gate 		return;
767c478bd9Sstevel@tonic-gate 	}
77*f8994074SJan Friedel 	(void) strncpy(luser, uname, LOGNAME_MAX);
78*f8994074SJan Friedel 	generate_record(luser, UNKNOWN_USER, dgettext(bsm_dom, "unknown user"));
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
837c478bd9Sstevel@tonic-gate audit_ftpd_excluded(char *uname)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
867c478bd9Sstevel@tonic-gate 		return;
877c478bd9Sstevel@tonic-gate 	}
88*f8994074SJan Friedel 	(void) strncpy(luser, uname, LOGNAME_MAX);
897c478bd9Sstevel@tonic-gate 	generate_record(luser, EXCLUDED_USER, dgettext(bsm_dom,
907c478bd9Sstevel@tonic-gate 	    "excluded user"));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate void
957c478bd9Sstevel@tonic-gate audit_ftpd_no_anon(void)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
987c478bd9Sstevel@tonic-gate 		return;
997c478bd9Sstevel@tonic-gate 	}
100*f8994074SJan Friedel 	generate_record("", NO_ANONYMOUS, dgettext(bsm_dom, "no anonymous"));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate void
1047c478bd9Sstevel@tonic-gate audit_ftpd_failure(char *uname)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
1077c478bd9Sstevel@tonic-gate 		return;
1087c478bd9Sstevel@tonic-gate 	}
109*f8994074SJan Friedel 	generate_record(uname, MISC_FAILURE, dgettext(bsm_dom, "misc failure"));
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate void
1137c478bd9Sstevel@tonic-gate audit_ftpd_success(char	*uname)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
1167c478bd9Sstevel@tonic-gate 		return;
1177c478bd9Sstevel@tonic-gate 	}
118*f8994074SJan Friedel 	(void) strncpy(luser, uname, LOGNAME_MAX);
1197c478bd9Sstevel@tonic-gate 	generate_record(luser, 0, "");
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static void
1257c478bd9Sstevel@tonic-gate generate_record(
1267c478bd9Sstevel@tonic-gate 		char	*locuser,	/* username of local user */
1277c478bd9Sstevel@tonic-gate 		int	err,		/* error status */
1287c478bd9Sstevel@tonic-gate 					/* (=0 success, >0 error code) */
1297c478bd9Sstevel@tonic-gate 		char	*msg)		/* error message */
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
1327c478bd9Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
1337c478bd9Sstevel@tonic-gate 	uid_t	uid;
1347c478bd9Sstevel@tonic-gate 	gid_t	gid;
1357c478bd9Sstevel@tonic-gate 	uid_t	ruid;		/* real uid */
1367c478bd9Sstevel@tonic-gate 	gid_t	rgid;		/* real gid */
1377c478bd9Sstevel@tonic-gate 	pid_t	pid;
1387c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
1397c478bd9Sstevel@tonic-gate 	uid_t	ceuid;		/* current effective uid */
1407c478bd9Sstevel@tonic-gate 	struct auditinfo_addr info;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
1437c478bd9Sstevel@tonic-gate 		return;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	pwd = getpwnam(locuser);
1477c478bd9Sstevel@tonic-gate 	if (pwd == NULL) {
148f48205beScasper 		uid = (uid_t)-1;
149f48205beScasper 		gid = (gid_t)-1;
1507c478bd9Sstevel@tonic-gate 	} else {
1517c478bd9Sstevel@tonic-gate 		uid = pwd->pw_uid;
1527c478bd9Sstevel@tonic-gate 		gid = pwd->pw_gid;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	ceuid = geteuid();	/* save current euid */
1567c478bd9Sstevel@tonic-gate 	(void) seteuid(0);	/* change to root so you can audit */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* determine if we're preselected */
1597c478bd9Sstevel@tonic-gate 	if (!selected(uid, locuser, AUE_ftpd, err)) {
1607c478bd9Sstevel@tonic-gate 		(void) seteuid(ceuid);
1617c478bd9Sstevel@tonic-gate 		return;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	ruid = getuid();	/* get real uid */
1657c478bd9Sstevel@tonic-gate 	rgid = getgid();	/* get real gid */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	pid = getpid();
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* see if terminal id already set */
1707c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
1717c478bd9Sstevel@tonic-gate 		perror("getaudit");
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	rd = au_open();
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* add subject token */
1777c478bd9Sstevel@tonic-gate 	(void) au_write(rd, au_to_subject_ex(uid, uid, gid,
1787c478bd9Sstevel@tonic-gate 	    ruid, rgid, pid, pid, &info.ai_termid));
1797c478bd9Sstevel@tonic-gate 
18045916cd2Sjpk 	if (is_system_labeled())
18145916cd2Sjpk 		(void) au_write(rd, au_to_mylabel());
18245916cd2Sjpk 
1837c478bd9Sstevel@tonic-gate 	/* add return token */
1847c478bd9Sstevel@tonic-gate 	errno = 0;
1857c478bd9Sstevel@tonic-gate 	if (err) {
1867c478bd9Sstevel@tonic-gate 		/* add reason for failure */
1877c478bd9Sstevel@tonic-gate 		if (err == UNKNOWN_USER)
1887c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf),
1897c478bd9Sstevel@tonic-gate 			    "%s %s", msg, locuser);
1907c478bd9Sstevel@tonic-gate 		else
1917c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", msg);
1927c478bd9Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(buf));
1937c478bd9Sstevel@tonic-gate #ifdef _LP64
1947c478bd9Sstevel@tonic-gate 		(void) au_write(rd, au_to_return64(-1, (int64_t)err));
1957c478bd9Sstevel@tonic-gate #else
1967c478bd9Sstevel@tonic-gate 		(void) au_write(rd, au_to_return32(-1, (int32_t)err));
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 	} else {
1997c478bd9Sstevel@tonic-gate #ifdef _LP64
2007c478bd9Sstevel@tonic-gate 		(void) au_write(rd, au_to_return64(0, (int64_t)0));
2017c478bd9Sstevel@tonic-gate #else
2027c478bd9Sstevel@tonic-gate 		(void) au_write(rd, au_to_return32(0, (int32_t)0));
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* write audit record */
2077c478bd9Sstevel@tonic-gate 	if (au_close(rd, 1, AUE_ftpd) < 0) {
2087c478bd9Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	(void) seteuid(ceuid);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static int
2157c478bd9Sstevel@tonic-gate selected(
2167c478bd9Sstevel@tonic-gate 	uid_t		uid,
2177c478bd9Sstevel@tonic-gate 	char		*locuser,
2187c478bd9Sstevel@tonic-gate 	au_event_t	event,
2197c478bd9Sstevel@tonic-gate 	int	err)
2207c478bd9Sstevel@tonic-gate {
221*f8994074SJan Friedel 	int		sorf;
2227c478bd9Sstevel@tonic-gate 	struct au_mask	mask;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mask.am_success = mask.am_failure = 0;
225f48205beScasper 	if (uid > MAXEPHUID) {
226*f8994074SJan Friedel 		/* get non-attrib flags */
227*f8994074SJan Friedel 		(void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
2287c478bd9Sstevel@tonic-gate 	} else {
229*f8994074SJan Friedel 		(void) au_user_mask(locuser, &mask);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
232*f8994074SJan Friedel 	if (err == 0) {
2337c478bd9Sstevel@tonic-gate 		sorf = AU_PRS_SUCCESS;
234*f8994074SJan Friedel 	} else if (err >= 1) {
2357c478bd9Sstevel@tonic-gate 		sorf = AU_PRS_FAILURE;
236*f8994074SJan Friedel 	} else {
2377c478bd9Sstevel@tonic-gate 		sorf = AU_PRS_BOTH;
238*f8994074SJan Friedel 	}
239*f8994074SJan Friedel 
240*f8994074SJan Friedel 	return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate void
2457c478bd9Sstevel@tonic-gate audit_ftpd_logout(void)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
2487c478bd9Sstevel@tonic-gate 	uid_t	euid;
2497c478bd9Sstevel@tonic-gate 	gid_t	egid;
2507c478bd9Sstevel@tonic-gate 	uid_t	uid;
2517c478bd9Sstevel@tonic-gate 	gid_t	gid;
2527c478bd9Sstevel@tonic-gate 	pid_t	pid;
2537c478bd9Sstevel@tonic-gate 	struct auditinfo_addr info;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
2567c478bd9Sstevel@tonic-gate 		return;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* see if terminal id already set */
2627c478bd9Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
2637c478bd9Sstevel@tonic-gate 		perror("getaudit");
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* determine if we're preselected */
2677c478bd9Sstevel@tonic-gate 	if (au_preselect(AUE_ftpd_logout, &info.ai_mask, AU_PRS_SUCCESS,
2687c478bd9Sstevel@tonic-gate 	    AU_PRS_USECACHE) == 0) {
2697c478bd9Sstevel@tonic-gate 		(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT,
2707c478bd9Sstevel@tonic-gate 		    NULL);
2717c478bd9Sstevel@tonic-gate 		return;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	euid = geteuid();
2757c478bd9Sstevel@tonic-gate 	egid = getegid();
2767c478bd9Sstevel@tonic-gate 	uid = getuid();
2777c478bd9Sstevel@tonic-gate 	gid = getgid();
2787c478bd9Sstevel@tonic-gate 	pid = getpid();
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	rd = au_open();
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* add subject token */
2837c478bd9Sstevel@tonic-gate 	(void) au_write(rd, au_to_subject_ex(info.ai_auid, euid,
2847c478bd9Sstevel@tonic-gate 	    egid, uid, gid, pid, pid, &info.ai_termid));
2857c478bd9Sstevel@tonic-gate 
28645916cd2Sjpk 	if (is_system_labeled())
28745916cd2Sjpk 		(void) au_write(rd, au_to_mylabel());
28845916cd2Sjpk 
2897c478bd9Sstevel@tonic-gate 	/* add return token */
2907c478bd9Sstevel@tonic-gate 	errno = 0;
2917c478bd9Sstevel@tonic-gate #ifdef _LP64
2927c478bd9Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(0, (int64_t)0));
2937c478bd9Sstevel@tonic-gate #else
2947c478bd9Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(0, (int32_t)0));
2957c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* write audit record */
2987c478bd9Sstevel@tonic-gate 	if (au_close(rd, 1, AUE_ftpd_logout) < 0) {
2997c478bd9Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	(void) priv_set(PRIV_OFF, PRIV_EFFECTIVE, PRIV_PROC_AUDIT, NULL);
3027c478bd9Sstevel@tonic-gate }
303