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