xref: /onnv-gate/usr/src/lib/libbsm/common/audit_crontab.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <sys/types.h>
29*0Sstevel@tonic-gate #include <sys/stat.h>
30*0Sstevel@tonic-gate #include <sys/systeminfo.h>
31*0Sstevel@tonic-gate #include <bsm/audit.h>
32*0Sstevel@tonic-gate #include <bsm/libbsm.h>
33*0Sstevel@tonic-gate #include <bsm/audit_uevents.h>
34*0Sstevel@tonic-gate #include <bsm/audit_private.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <pwd.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <locale.h>
41*0Sstevel@tonic-gate #include "generic.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_NO_CRONTAB	1
44*0Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_CRONTAB		0
45*0Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_ERR		-1
46*0Sstevel@tonic-gate #define	AUDIT_GET_DIFFS_NO_DIFFS	-2
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static int	audit_crontab_get_diffs(char *cf, char *tmp_name,
49*0Sstevel@tonic-gate 		    char **bufptr);
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate int
audit_crontab_modify(char * path,char * tmp_path,int sorf)52*0Sstevel@tonic-gate audit_crontab_modify(char *path, char *tmp_path, int sorf)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	int r, create = 0;
55*0Sstevel@tonic-gate 	char *diffs = NULL;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (cannot_audit(0)) {
58*0Sstevel@tonic-gate 		return (0);
59*0Sstevel@tonic-gate 	} else {
60*0Sstevel@tonic-gate 		au_event_t event;
61*0Sstevel@tonic-gate 		char *anc_name;
62*0Sstevel@tonic-gate 		auditinfo_addr_t ai;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 		if (getaudit_addr(&ai, sizeof (ai))) {
65*0Sstevel@tonic-gate 			return (-1);
66*0Sstevel@tonic-gate 		}
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 		r = audit_crontab_get_diffs(path, tmp_path, &diffs);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 		if (r == AUDIT_GET_DIFFS_NO_DIFFS) {
71*0Sstevel@tonic-gate 			return (0);
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 		if (diffs != NULL && r != AUDIT_GET_DIFFS_ERR) {
74*0Sstevel@tonic-gate 			aug_save_text(diffs);
75*0Sstevel@tonic-gate 			free(diffs);
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 		if (r == AUDIT_GET_DIFFS_NO_CRONTAB) {
79*0Sstevel@tonic-gate 			create = 1;
80*0Sstevel@tonic-gate 			if (diffs == NULL)
81*0Sstevel@tonic-gate 				aug_save_text("");
82*0Sstevel@tonic-gate 		}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 		/*
85*0Sstevel@tonic-gate 		 * create an ancilary file if audit characteristics exist
86*0Sstevel@tonic-gate 		 * else delete an ancilary if if one exists
87*0Sstevel@tonic-gate 		 */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 		anc_name = audit_cron_make_anc_name(path);
90*0Sstevel@tonic-gate 		if (anc_name == NULL)
91*0Sstevel@tonic-gate 			r = -1;
92*0Sstevel@tonic-gate 		else if (audit_crontab_process_not_audited()) {
93*0Sstevel@tonic-gate 			(void) unlink(anc_name);
94*0Sstevel@tonic-gate 			free(anc_name);
95*0Sstevel@tonic-gate 		} else {
96*0Sstevel@tonic-gate 			r = audit_cron_setinfo(anc_name, &ai);
97*0Sstevel@tonic-gate 			free(anc_name);
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 		aug_init();
100*0Sstevel@tonic-gate 		aug_save_auid(ai.ai_auid);
101*0Sstevel@tonic-gate 		aug_save_euid(geteuid());
102*0Sstevel@tonic-gate 		aug_save_egid(getegid());
103*0Sstevel@tonic-gate 		aug_save_uid(getuid());
104*0Sstevel@tonic-gate 		aug_save_gid(getgid());
105*0Sstevel@tonic-gate 		aug_save_pid(getpid());
106*0Sstevel@tonic-gate 		aug_save_asid(ai.ai_asid);
107*0Sstevel@tonic-gate 		aug_save_tid_ex(ai.ai_termid.at_port, ai.ai_termid.at_addr,
108*0Sstevel@tonic-gate 			ai.ai_termid.at_type);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		aug_save_path(path);
112*0Sstevel@tonic-gate 		event = (create) ? AUE_crontab_create : AUE_crontab_mod;
113*0Sstevel@tonic-gate 		aug_save_event(event);
114*0Sstevel@tonic-gate 		aug_save_sorf(sorf);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		if (aug_audit() != 0)
117*0Sstevel@tonic-gate 			return (-1);
118*0Sstevel@tonic-gate 		return (r);
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate int
audit_crontab_delete(char * path,int sorf)123*0Sstevel@tonic-gate audit_crontab_delete(char *path, int sorf)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	int r = 0;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (cannot_audit(0)) {
128*0Sstevel@tonic-gate 		return (0);
129*0Sstevel@tonic-gate 	} else {
130*0Sstevel@tonic-gate 		char *anc_name;
131*0Sstevel@tonic-gate 		anc_name = audit_cron_make_anc_name(path);
132*0Sstevel@tonic-gate 		if (anc_name != NULL) {
133*0Sstevel@tonic-gate 			r = unlink(anc_name);
134*0Sstevel@tonic-gate 			free(anc_name);
135*0Sstevel@tonic-gate 		} else
136*0Sstevel@tonic-gate 			r = -1;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 		aug_init();
139*0Sstevel@tonic-gate 		(void) aug_save_me();
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		aug_save_path(path);
142*0Sstevel@tonic-gate 		aug_save_event(AUE_crontab_delete);
143*0Sstevel@tonic-gate 		aug_save_sorf(sorf);
144*0Sstevel@tonic-gate 		if (aug_audit() != 0)
145*0Sstevel@tonic-gate 			return (-1);
146*0Sstevel@tonic-gate 		return (r);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate  * gets differences between old and new crontab files.
152*0Sstevel@tonic-gate  * arguments:
153*0Sstevel@tonic-gate  * cf        - name of crontab file
154*0Sstevel@tonic-gate  * tmp_name  - name of new crontab file
155*0Sstevel@tonic-gate  * bufptr    - pointer to an array of characters with
156*0Sstevel@tonic-gate  *             either an error message or an output of "diff" command.
157*0Sstevel@tonic-gate  *
158*0Sstevel@tonic-gate  * results:
159*0Sstevel@tonic-gate  * AUDIT_GET_DIFFS_ERR       - errors;
160*0Sstevel@tonic-gate  *			file not exists (do not free *bufptr in this case)
161*0Sstevel@tonic-gate  * AUDIT_GET_DIFFS_NO_DIFFS  - errors;
162*0Sstevel@tonic-gate  *			file exists (do not free *bufptr in this case)
163*0Sstevel@tonic-gate  * AUDIT_GET_DIFFS_CRONTAB      - OK, old crontab file exists.
164*0Sstevel@tonic-gate  * AUDIT_GET_DIFFS_NO_CRONTAB   - OK. there is no crontab file.
165*0Sstevel@tonic-gate  */
166*0Sstevel@tonic-gate static int
audit_crontab_get_diffs(char * cf,char * tmp_name,char ** bufptr)167*0Sstevel@tonic-gate audit_crontab_get_diffs(char *cf, char *tmp_name, char **bufptr)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	struct stat st, st_tmp;
170*0Sstevel@tonic-gate 	uid_t	euid;
171*0Sstevel@tonic-gate 	int	len, r = AUDIT_GET_DIFFS_CRONTAB;
172*0Sstevel@tonic-gate 	char	*buf = NULL, err_buf[128];
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	(void) memset(err_buf, 0, 128);
175*0Sstevel@tonic-gate 	euid = geteuid();
176*0Sstevel@tonic-gate 	if (seteuid(0) == -1) {
177*0Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_ERR;
178*0Sstevel@tonic-gate 		(void) snprintf(err_buf, sizeof (err_buf),
179*0Sstevel@tonic-gate 		    "crontab: seteuid: %s\n", strerror(errno));
180*0Sstevel@tonic-gate 		goto exit_diff;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	if (stat(cf, &st) == -1) {
183*0Sstevel@tonic-gate 		if (errno == ENOENT) {
184*0Sstevel@tonic-gate 			r = AUDIT_GET_DIFFS_NO_CRONTAB;
185*0Sstevel@tonic-gate 		} else {
186*0Sstevel@tonic-gate 			r = AUDIT_GET_DIFFS_ERR;
187*0Sstevel@tonic-gate 			(void) snprintf(err_buf, sizeof (err_buf),
188*0Sstevel@tonic-gate 				"crontab: %s: stat: %s\n",
189*0Sstevel@tonic-gate 				cf, strerror(errno));
190*0Sstevel@tonic-gate 			goto exit_diff;
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 		len = 0;
193*0Sstevel@tonic-gate 	} else
194*0Sstevel@tonic-gate 		len = st.st_size;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (stat(tmp_name, &st_tmp) == -1) {
197*0Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_ERR;
198*0Sstevel@tonic-gate 		(void) snprintf(err_buf, sizeof (err_buf),
199*0Sstevel@tonic-gate 			"crontab: %s: stat: %s\n",
200*0Sstevel@tonic-gate 			tmp_name, strerror(errno));
201*0Sstevel@tonic-gate 		goto exit_diff;
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (st_tmp.st_size == 0 && len == 0) {
205*0Sstevel@tonic-gate 	/* there is no difference */
206*0Sstevel@tonic-gate 		r = AUDIT_GET_DIFFS_NO_DIFFS;
207*0Sstevel@tonic-gate 		*bufptr = NULL;
208*0Sstevel@tonic-gate 		goto exit_diff;
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate exit_diff:
212*0Sstevel@tonic-gate 	/* return information on create or update crontab */
213*0Sstevel@tonic-gate 	(void) seteuid(euid);
214*0Sstevel@tonic-gate 	switch (r) {
215*0Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_ERR:
216*0Sstevel@tonic-gate 		if (buf != NULL)
217*0Sstevel@tonic-gate 			free(buf);
218*0Sstevel@tonic-gate 		*bufptr = err_buf;
219*0Sstevel@tonic-gate 		break;
220*0Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_NO_DIFFS:
221*0Sstevel@tonic-gate 		if (buf != NULL)
222*0Sstevel@tonic-gate 			free(buf);
223*0Sstevel@tonic-gate 		*bufptr = NULL;
224*0Sstevel@tonic-gate 		break;
225*0Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_CRONTAB:
226*0Sstevel@tonic-gate 		if (buf != NULL) {
227*0Sstevel@tonic-gate 			if (strlen(buf) != 0) {
228*0Sstevel@tonic-gate 				*bufptr = buf;
229*0Sstevel@tonic-gate 			} else {
230*0Sstevel@tonic-gate 				r = AUDIT_GET_DIFFS_NO_DIFFS;
231*0Sstevel@tonic-gate 				*bufptr = NULL;
232*0Sstevel@tonic-gate 			}
233*0Sstevel@tonic-gate 		}
234*0Sstevel@tonic-gate 		break;
235*0Sstevel@tonic-gate 	case AUDIT_GET_DIFFS_NO_CRONTAB:
236*0Sstevel@tonic-gate 		if (buf != NULL) {
237*0Sstevel@tonic-gate 			if (strlen(buf) != 0) {
238*0Sstevel@tonic-gate 				*bufptr = buf;
239*0Sstevel@tonic-gate 			} else {
240*0Sstevel@tonic-gate 				*bufptr = NULL;
241*0Sstevel@tonic-gate 				free(buf);
242*0Sstevel@tonic-gate 			}
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 		break;
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	return (r);
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate /*
251*0Sstevel@tonic-gate  * audit_crontab_not_allowed determines if we have a case that should be audited
252*0Sstevel@tonic-gate  * but we can't.  If auditing is enabled but the current process is not
253*0Sstevel@tonic-gate  * audited, then the ruid of the user doing the editing must be the owner
254*0Sstevel@tonic-gate  * id of the file to be edited.
255*0Sstevel@tonic-gate  *
256*0Sstevel@tonic-gate  * When audit_crontab_not_allowed is called, ruid is for the crontab file
257*0Sstevel@tonic-gate  * to be modified or created.
258*0Sstevel@tonic-gate  */
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate #define	PWD_BUFFER_SIZE	512
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate int
audit_crontab_not_allowed(uid_t ruid,char * user)263*0Sstevel@tonic-gate audit_crontab_not_allowed(uid_t ruid, char *user) {
264*0Sstevel@tonic-gate 	struct passwd		pwd;
265*0Sstevel@tonic-gate 	char			buffer[PWD_BUFFER_SIZE];
266*0Sstevel@tonic-gate 	int			rc = 0;		/* 0 == allow */
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	if (!cannot_audit(0)) {			/* allow access if audit off */
269*0Sstevel@tonic-gate 		if (getpwnam_r(user, &pwd, buffer, PWD_BUFFER_SIZE) == NULL) {
270*0Sstevel@tonic-gate 			rc = 1;			/* deny access if invalid */
271*0Sstevel@tonic-gate 		} else if (ruid == pwd.pw_uid)
272*0Sstevel@tonic-gate 			rc = 0;			/* editing his own crontab */
273*0Sstevel@tonic-gate 		else
274*0Sstevel@tonic-gate 			rc = audit_crontab_process_not_audited();
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 	return (rc);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate int
audit_crontab_process_not_audited()280*0Sstevel@tonic-gate audit_crontab_process_not_audited() {
281*0Sstevel@tonic-gate 	struct auditpinfo_addr	info;
282*0Sstevel@tonic-gate 	int	rc;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	info.ap_pid = getpid();
285*0Sstevel@tonic-gate 	if (auditon(A_GETPINFO_ADDR, (caddr_t)&info, sizeof (info)) != 0)
286*0Sstevel@tonic-gate 		rc = 0;			/* audit failure: not enabled */
287*0Sstevel@tonic-gate 	else
288*0Sstevel@tonic-gate 		rc = (info.ap_auid == AU_NOAUDITID);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	return (rc);
291*0Sstevel@tonic-gate }
292