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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
307c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
317c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
327c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
337c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
347c478bd9Sstevel@tonic-gate #include <bsm/audit_private.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <pwd.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <locale.h>
417c478bd9Sstevel@tonic-gate #include "generic.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_NO_CRONTAB	1
447c478bd9Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_CRONTAB		0
457c478bd9Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_ERR		-1
467c478bd9Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_NO_DIFFS	-2
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int	audit_crontab_get_diffs(char *cf, char *tmp_name,
497c478bd9Sstevel@tonic-gate 		    char **bufptr);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate int
audit_crontab_modify(char * path,char * tmp_path,int sorf)527c478bd9Sstevel@tonic-gate audit_crontab_modify(char *path, char *tmp_path, int sorf)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	int r, create = 0;
557c478bd9Sstevel@tonic-gate 	char *diffs = NULL;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
587c478bd9Sstevel@tonic-gate 		return (0);
597c478bd9Sstevel@tonic-gate 	} else {
607c478bd9Sstevel@tonic-gate 		au_event_t event;
617c478bd9Sstevel@tonic-gate 		char *anc_name;
627c478bd9Sstevel@tonic-gate 		auditinfo_addr_t ai;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		if (getaudit_addr(&ai, sizeof (ai))) {
657c478bd9Sstevel@tonic-gate 			return (-1);
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 		r = audit_crontab_get_diffs(path, tmp_path, &diffs);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		if (r == AUDIT_GET_DIFFS_NO_DIFFS) {
717c478bd9Sstevel@tonic-gate 			return (0);
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 		if (diffs != NULL && r != AUDIT_GET_DIFFS_ERR) {
747c478bd9Sstevel@tonic-gate 			aug_save_text(diffs);
757c478bd9Sstevel@tonic-gate 			free(diffs);
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 		if (r == AUDIT_GET_DIFFS_NO_CRONTAB) {
797c478bd9Sstevel@tonic-gate 			create = 1;
807c478bd9Sstevel@tonic-gate 			if (diffs == NULL)
817c478bd9Sstevel@tonic-gate 				aug_save_text("");
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		/*
857c478bd9Sstevel@tonic-gate 		 * create an ancilary file if audit characteristics exist
867c478bd9Sstevel@tonic-gate 		 * else delete an ancilary if if one exists
877c478bd9Sstevel@tonic-gate 		 */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		anc_name = audit_cron_make_anc_name(path);
907c478bd9Sstevel@tonic-gate 		if (anc_name == NULL)
917c478bd9Sstevel@tonic-gate 			r = -1;
927c478bd9Sstevel@tonic-gate 		else if (audit_crontab_process_not_audited()) {
937c478bd9Sstevel@tonic-gate 			(void) unlink(anc_name);
947c478bd9Sstevel@tonic-gate 			free(anc_name);
957c478bd9Sstevel@tonic-gate 		} else {
967c478bd9Sstevel@tonic-gate 			r = audit_cron_setinfo(anc_name, &ai);
977c478bd9Sstevel@tonic-gate 			free(anc_name);
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 		aug_init();
1007c478bd9Sstevel@tonic-gate 		aug_save_auid(ai.ai_auid);
1017c478bd9Sstevel@tonic-gate 		aug_save_euid(geteuid());
1027c478bd9Sstevel@tonic-gate 		aug_save_egid(getegid());
1037c478bd9Sstevel@tonic-gate 		aug_save_uid(getuid());
1047c478bd9Sstevel@tonic-gate 		aug_save_gid(getgid());
1057c478bd9Sstevel@tonic-gate 		aug_save_pid(getpid());
1067c478bd9Sstevel@tonic-gate 		aug_save_asid(ai.ai_asid);
1077c478bd9Sstevel@tonic-gate 		aug_save_tid_ex(ai.ai_termid.at_port, ai.ai_termid.at_addr,
1087c478bd9Sstevel@tonic-gate 			ai.ai_termid.at_type);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		aug_save_path(path);
1127c478bd9Sstevel@tonic-gate 		event = (create) ? AUE_crontab_create : AUE_crontab_mod;
1137c478bd9Sstevel@tonic-gate 		aug_save_event(event);
1147c478bd9Sstevel@tonic-gate 		aug_save_sorf(sorf);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if (aug_audit() != 0)
1177c478bd9Sstevel@tonic-gate 			return (-1);
1187c478bd9Sstevel@tonic-gate 		return (r);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate int
audit_crontab_delete(char * path,int sorf)1237c478bd9Sstevel@tonic-gate audit_crontab_delete(char *path, int sorf)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int r = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
1287c478bd9Sstevel@tonic-gate 		return (0);
1297c478bd9Sstevel@tonic-gate 	} else {
1307c478bd9Sstevel@tonic-gate 		char *anc_name;
1317c478bd9Sstevel@tonic-gate 		anc_name = audit_cron_make_anc_name(path);
1327c478bd9Sstevel@tonic-gate 		if (anc_name != NULL) {
1337c478bd9Sstevel@tonic-gate 			r = unlink(anc_name);
1347c478bd9Sstevel@tonic-gate 			free(anc_name);
1357c478bd9Sstevel@tonic-gate 		} else
1367c478bd9Sstevel@tonic-gate 			r = -1;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		aug_init();
1397c478bd9Sstevel@tonic-gate 		(void) aug_save_me();
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		aug_save_path(path);
1427c478bd9Sstevel@tonic-gate 		aug_save_event(AUE_crontab_delete);
1437c478bd9Sstevel@tonic-gate 		aug_save_sorf(sorf);
1447c478bd9Sstevel@tonic-gate 		if (aug_audit() != 0)
1457c478bd9Sstevel@tonic-gate 			return (-1);
1467c478bd9Sstevel@tonic-gate 		return (r);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * gets differences between old and new crontab files.
1527c478bd9Sstevel@tonic-gate  * arguments:
1537c478bd9Sstevel@tonic-gate  * cf        - name of crontab file
1547c478bd9Sstevel@tonic-gate  * tmp_name  - name of new crontab file
1557c478bd9Sstevel@tonic-gate  * bufptr    - pointer to an array of characters with
1567c478bd9Sstevel@tonic-gate  *             either an error message or an output of "diff" command.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * results:
1597c478bd9Sstevel@tonic-gate  * AUDIT_GET_DIFFS_ERR       - errors;
1607c478bd9Sstevel@tonic-gate  *			file not exists (do not free *bufptr in this case)
1617c478bd9Sstevel@tonic-gate  * AUDIT_GET_DIFFS_NO_DIFFS  - errors;
1627c478bd9Sstevel@tonic-gate  *			file exists (do not free *bufptr in this case)
1637c478bd9Sstevel@tonic-gate  * AUDIT_GET_DIFFS_CRONTAB      - OK, old crontab file exists.
1647c478bd9Sstevel@tonic-gate  * AUDIT_GET_DIFFS_NO_CRONTAB   - OK. there is no crontab file.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate static int
audit_crontab_get_diffs(char * cf,char * tmp_name,char ** bufptr)1677c478bd9Sstevel@tonic-gate audit_crontab_get_diffs(char *cf, char *tmp_name, char **bufptr)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	struct stat st, st_tmp;
1707c478bd9Sstevel@tonic-gate 	uid_t	euid;
1717c478bd9Sstevel@tonic-gate 	int	len, r = AUDIT_GET_DIFFS_CRONTAB;
1727c478bd9Sstevel@tonic-gate 	char	*buf = NULL, err_buf[128];
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	(void) memset(err_buf, 0, 128);
1757c478bd9Sstevel@tonic-gate 	euid = geteuid();
1767c478bd9Sstevel@tonic-gate 	if (seteuid(0) == -1) {
1777c478bd9Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_ERR;
1787c478bd9Sstevel@tonic-gate 		(void) snprintf(err_buf, sizeof (err_buf),
1797c478bd9Sstevel@tonic-gate 		    "crontab: seteuid: %s\n", strerror(errno));
1807c478bd9Sstevel@tonic-gate 		goto exit_diff;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	if (stat(cf, &st) == -1) {
1837c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
1847c478bd9Sstevel@tonic-gate 			r = AUDIT_GET_DIFFS_NO_CRONTAB;
1857c478bd9Sstevel@tonic-gate 		} else {
1867c478bd9Sstevel@tonic-gate 			r = AUDIT_GET_DIFFS_ERR;
1877c478bd9Sstevel@tonic-gate 			(void) snprintf(err_buf, sizeof (err_buf),
1887c478bd9Sstevel@tonic-gate 				"crontab: %s: stat: %s\n",
1897c478bd9Sstevel@tonic-gate 				cf, strerror(errno));
1907c478bd9Sstevel@tonic-gate 			goto exit_diff;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		len = 0;
1937c478bd9Sstevel@tonic-gate 	} else
1947c478bd9Sstevel@tonic-gate 		len = st.st_size;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (stat(tmp_name, &st_tmp) == -1) {
1977c478bd9Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_ERR;
1987c478bd9Sstevel@tonic-gate 		(void) snprintf(err_buf, sizeof (err_buf),
1997c478bd9Sstevel@tonic-gate 			"crontab: %s: stat: %s\n",
2007c478bd9Sstevel@tonic-gate 			tmp_name, strerror(errno));
2017c478bd9Sstevel@tonic-gate 		goto exit_diff;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (st_tmp.st_size == 0 && len == 0) {
2057c478bd9Sstevel@tonic-gate 	/* there is no difference */
2067c478bd9Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_NO_DIFFS;
2077c478bd9Sstevel@tonic-gate 		*bufptr = NULL;
2087c478bd9Sstevel@tonic-gate 		goto exit_diff;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate exit_diff:
2127c478bd9Sstevel@tonic-gate 	/* return information on create or update crontab */
2137c478bd9Sstevel@tonic-gate 	(void) seteuid(euid);
2147c478bd9Sstevel@tonic-gate 	switch (r) {
2157c478bd9Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_ERR:
2167c478bd9Sstevel@tonic-gate 		if (buf != NULL)
2177c478bd9Sstevel@tonic-gate 			free(buf);
2187c478bd9Sstevel@tonic-gate 		*bufptr = err_buf;
2197c478bd9Sstevel@tonic-gate 		break;
2207c478bd9Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_NO_DIFFS:
2217c478bd9Sstevel@tonic-gate 		if (buf != NULL)
2227c478bd9Sstevel@tonic-gate 			free(buf);
2237c478bd9Sstevel@tonic-gate 		*bufptr = NULL;
2247c478bd9Sstevel@tonic-gate 		break;
2257c478bd9Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_CRONTAB:
2267c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
2277c478bd9Sstevel@tonic-gate 			if (strlen(buf) != 0) {
2287c478bd9Sstevel@tonic-gate 				*bufptr = buf;
2297c478bd9Sstevel@tonic-gate 			} else {
2307c478bd9Sstevel@tonic-gate 				r = AUDIT_GET_DIFFS_NO_DIFFS;
2317c478bd9Sstevel@tonic-gate 				*bufptr = NULL;
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		break;
2357c478bd9Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_NO_CRONTAB:
2367c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
2377c478bd9Sstevel@tonic-gate 			if (strlen(buf) != 0) {
2387c478bd9Sstevel@tonic-gate 				*bufptr = buf;
2397c478bd9Sstevel@tonic-gate 			} else {
2407c478bd9Sstevel@tonic-gate 				*bufptr = NULL;
2417c478bd9Sstevel@tonic-gate 				free(buf);
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		break;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (r);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * audit_crontab_not_allowed determines if we have a case that should be audited
2527c478bd9Sstevel@tonic-gate  * but we can't.  If auditing is enabled but the current process is not
2537c478bd9Sstevel@tonic-gate  * audited, then the ruid of the user doing the editing must be the owner
2547c478bd9Sstevel@tonic-gate  * id of the file to be edited.
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  * When audit_crontab_not_allowed is called, ruid is for the crontab file
2577c478bd9Sstevel@tonic-gate  * to be modified or created.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate #define	PWD_BUFFER_SIZE	512
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate int
audit_crontab_not_allowed(uid_t ruid,char * user)2637c478bd9Sstevel@tonic-gate audit_crontab_not_allowed(uid_t ruid, char *user) {
2647c478bd9Sstevel@tonic-gate 	struct passwd		pwd;
2657c478bd9Sstevel@tonic-gate 	char			buffer[PWD_BUFFER_SIZE];
2667c478bd9Sstevel@tonic-gate 	int			rc = 0;		/* 0 == allow */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (!cannot_audit(0)) {			/* allow access if audit off */
2697c478bd9Sstevel@tonic-gate 		if (getpwnam_r(user, &pwd, buffer, PWD_BUFFER_SIZE) == NULL) {
2707c478bd9Sstevel@tonic-gate 			rc = 1;			/* deny access if invalid */
2717c478bd9Sstevel@tonic-gate 		} else if (ruid == pwd.pw_uid)
272*48bbca81SDaniel Hoffman 			rc = 0;			/* editing their own crontab */
2737c478bd9Sstevel@tonic-gate 		else
2747c478bd9Sstevel@tonic-gate 			rc = audit_crontab_process_not_audited();
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	return (rc);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate int
audit_crontab_process_not_audited()2807c478bd9Sstevel@tonic-gate audit_crontab_process_not_audited() {
2817c478bd9Sstevel@tonic-gate 	struct auditpinfo_addr	info;
2827c478bd9Sstevel@tonic-gate 	int	rc;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	info.ap_pid = getpid();
2857c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPINFO_ADDR, (caddr_t)&info, sizeof (info)) != 0)
2867c478bd9Sstevel@tonic-gate 		rc = 0;			/* audit failure: not enabled */
2877c478bd9Sstevel@tonic-gate 	else
2887c478bd9Sstevel@tonic-gate 		rc = (info.ap_auid == AU_NOAUDITID);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	return (rc);
2917c478bd9Sstevel@tonic-gate }
292