xref: /titanic_52/usr/src/cmd/praudit/prio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
32*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
33*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
34*7c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include "praudit.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * pr_adr_char - pull out characters
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate int
43*7c478bd9Sstevel@tonic-gate pr_adr_char(pr_context_t *context, char *cp, int count)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	int	err;
46*7c478bd9Sstevel@tonic-gate 	adr_t	*adr = context->audit_adr;
47*7c478bd9Sstevel@tonic-gate 	adrf_t	*adrf = context->audit_adrf;
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
50*7c478bd9Sstevel@tonic-gate 		err = adrf_char(adrf, cp, count);
51*7c478bd9Sstevel@tonic-gate 		if (err) {
52*7c478bd9Sstevel@tonic-gate 			errno = EIO;
53*7c478bd9Sstevel@tonic-gate 			return (-1);
54*7c478bd9Sstevel@tonic-gate 		} else
55*7c478bd9Sstevel@tonic-gate 			return (0);
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	/* adrm routines don't return error, so check before calling */
59*7c478bd9Sstevel@tonic-gate 	if (!pr_input_remaining(context, (sizeof (char) * count))) {
60*7c478bd9Sstevel@tonic-gate 		errno = EIO;
61*7c478bd9Sstevel@tonic-gate 		return (-1);
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	adrm_char(adr, cp, count);
65*7c478bd9Sstevel@tonic-gate 	return (0);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * pr_adr_short - pull out shorts
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate int
72*7c478bd9Sstevel@tonic-gate pr_adr_short(pr_context_t *context, short *sp, int count)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	int	err;
75*7c478bd9Sstevel@tonic-gate 	adr_t	*adr = context->audit_adr;
76*7c478bd9Sstevel@tonic-gate 	adrf_t	*adrf = context->audit_adrf;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
79*7c478bd9Sstevel@tonic-gate 		err = adrf_short(adrf, sp, count);
80*7c478bd9Sstevel@tonic-gate 		if (err) {
81*7c478bd9Sstevel@tonic-gate 			errno = EIO;
82*7c478bd9Sstevel@tonic-gate 			return (-1);
83*7c478bd9Sstevel@tonic-gate 		} else
84*7c478bd9Sstevel@tonic-gate 			return (0);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	/* adrm routines don't return error, so check before calling */
88*7c478bd9Sstevel@tonic-gate 	if (!pr_input_remaining(context, (sizeof (short) * count))) {
89*7c478bd9Sstevel@tonic-gate 		errno = EIO;
90*7c478bd9Sstevel@tonic-gate 		return (-1);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	adrm_short(adr, sp, count);
94*7c478bd9Sstevel@tonic-gate 	return (0);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * pr_adr_int32 - pull out int32
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate int
101*7c478bd9Sstevel@tonic-gate pr_adr_int32(pr_context_t *context, int32_t *lp, int count)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	int	err;
104*7c478bd9Sstevel@tonic-gate 	adr_t	*adr = context->audit_adr;
105*7c478bd9Sstevel@tonic-gate 	adrf_t	*adrf = context->audit_adrf;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
108*7c478bd9Sstevel@tonic-gate 		err = adrf_int32(adrf, lp, count);
109*7c478bd9Sstevel@tonic-gate 		if (err) {
110*7c478bd9Sstevel@tonic-gate 			errno = EIO;
111*7c478bd9Sstevel@tonic-gate 			return (-1);
112*7c478bd9Sstevel@tonic-gate 		} else
113*7c478bd9Sstevel@tonic-gate 			return (0);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/* adrm routines don't return error, so check before calling */
117*7c478bd9Sstevel@tonic-gate 	if (!pr_input_remaining(context, (sizeof (int32_t) * count))) {
118*7c478bd9Sstevel@tonic-gate 		errno = EIO;
119*7c478bd9Sstevel@tonic-gate 		return (-1);
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	adrm_int32(adr, lp, count);
123*7c478bd9Sstevel@tonic-gate 	return (0);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate int
127*7c478bd9Sstevel@tonic-gate pr_adr_int64(pr_context_t *context, int64_t *lp, int count)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	int	err;
130*7c478bd9Sstevel@tonic-gate 	adr_t	*adr = context->audit_adr;
131*7c478bd9Sstevel@tonic-gate 	adrf_t	*adrf = context->audit_adrf;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
134*7c478bd9Sstevel@tonic-gate 		err = adrf_int64(adrf, lp, count);
135*7c478bd9Sstevel@tonic-gate 		if (err) {
136*7c478bd9Sstevel@tonic-gate 			errno = EIO;
137*7c478bd9Sstevel@tonic-gate 			return (-1);
138*7c478bd9Sstevel@tonic-gate 		} else
139*7c478bd9Sstevel@tonic-gate 			return (0);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	/* adrm routines don't return error, so check before calling */
143*7c478bd9Sstevel@tonic-gate 	if (!pr_input_remaining(context, (sizeof (int64_t) * count))) {
144*7c478bd9Sstevel@tonic-gate 		errno = EIO;
145*7c478bd9Sstevel@tonic-gate 		return (-1);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	adrm_int64(adr, lp, count);
149*7c478bd9Sstevel@tonic-gate 	return (0);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate int
153*7c478bd9Sstevel@tonic-gate pr_adr_u_int32(pr_context_t *context, uint32_t *cp, int count)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	return (pr_adr_int32(context, (int32_t *)cp, count));
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate pr_adr_u_char(pr_context_t *context, uchar_t *cp, int count)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	return (pr_adr_char(context, (char *)cp, count));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate int
165*7c478bd9Sstevel@tonic-gate pr_adr_u_int64(pr_context_t *context, uint64_t *lp, int count)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	return (pr_adr_int64(context, (int64_t *)lp, count));
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate int
171*7c478bd9Sstevel@tonic-gate pr_adr_u_short(pr_context_t *context, ushort_t *sp, int count)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	return (pr_adr_short(context, (short *)sp, count));
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate int
177*7c478bd9Sstevel@tonic-gate pr_putchar(pr_context_t *context, char c)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
180*7c478bd9Sstevel@tonic-gate 		(void) putchar(c);
181*7c478bd9Sstevel@tonic-gate 		return (0);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 	/* Buffer-based output processing otherwise... */
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/* Need at least room for char + null-byte */
186*7c478bd9Sstevel@tonic-gate 	if (context->outbuf_remain_len < 2) {
187*7c478bd9Sstevel@tonic-gate 		/* no space left */
188*7c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
189*7c478bd9Sstevel@tonic-gate 		return (-1);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	*(context->outbuf_p) = c;
193*7c478bd9Sstevel@tonic-gate 	context->outbuf_p += 1;
194*7c478bd9Sstevel@tonic-gate 	context->outbuf_remain_len -= 1;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	return (0);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate int
200*7c478bd9Sstevel@tonic-gate pr_printf(pr_context_t *context, const char *fmt, ...)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	int addlen;
203*7c478bd9Sstevel@tonic-gate 	va_list ap;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (context->data_mode == FILEMODE) {
208*7c478bd9Sstevel@tonic-gate 		(void) vprintf(fmt, ap);
209*7c478bd9Sstevel@tonic-gate 		va_end(ap);
210*7c478bd9Sstevel@tonic-gate 		return (0);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 	/* Buffer-based output processing otherwise... */
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	if (context->outbuf_remain_len < 2) {
215*7c478bd9Sstevel@tonic-gate 		/* no space at all left */
216*7c478bd9Sstevel@tonic-gate 		va_end(ap);
217*7c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
218*7c478bd9Sstevel@tonic-gate 		return (-1);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	/* Attempt to tack on this string */
222*7c478bd9Sstevel@tonic-gate 	addlen = vsnprintf(context->outbuf_p, context->outbuf_remain_len - 1,
223*7c478bd9Sstevel@tonic-gate 	    fmt, ap);
224*7c478bd9Sstevel@tonic-gate 	va_end(ap);
225*7c478bd9Sstevel@tonic-gate 	if (addlen < 0) {
226*7c478bd9Sstevel@tonic-gate 		/* output error */
227*7c478bd9Sstevel@tonic-gate 		errno = EPERM;
228*7c478bd9Sstevel@tonic-gate 		return (-1);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	if (addlen >= context->outbuf_remain_len - 1) {
231*7c478bd9Sstevel@tonic-gate 		/* not enough space; bail out */
232*7c478bd9Sstevel@tonic-gate 		errno = ENOSPC;
233*7c478bd9Sstevel@tonic-gate 		return (-1);
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	/*
237*7c478bd9Sstevel@tonic-gate 	 * vsnprintf was successful; update pointers and counters
238*7c478bd9Sstevel@tonic-gate 	 * as needed. If no bytes were written, treat it as a no-op
239*7c478bd9Sstevel@tonic-gate 	 * and don't need to update anything.
240*7c478bd9Sstevel@tonic-gate 	 */
241*7c478bd9Sstevel@tonic-gate 	if (addlen >= 1) {
242*7c478bd9Sstevel@tonic-gate 		context->outbuf_remain_len -= addlen;
243*7c478bd9Sstevel@tonic-gate 		context->outbuf_p += addlen;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	return (0);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /*
251*7c478bd9Sstevel@tonic-gate  * pr_input_remaining - Check whether size bytes (or more) are remaining in
252*7c478bd9Sstevel@tonic-gate  * the inbuf.
253*7c478bd9Sstevel@tonic-gate  * returns	1 - there are enough bytes remaining
254*7c478bd9Sstevel@tonic-gate  *		0 - not enough bytes left
255*7c478bd9Sstevel@tonic-gate  */
256*7c478bd9Sstevel@tonic-gate int
257*7c478bd9Sstevel@tonic-gate pr_input_remaining(pr_context_t *context, size_t size)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 	adr_t	*adr = context->audit_adr;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	/* no-op if not doing buf mode */
262*7c478bd9Sstevel@tonic-gate 	if (context->data_mode != BUFMODE)
263*7c478bd9Sstevel@tonic-gate 		return (1);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if ((adr_count(adr) + size) > context->inbuf_totalsize)
266*7c478bd9Sstevel@tonic-gate 		return (0);
267*7c478bd9Sstevel@tonic-gate 	else
268*7c478bd9Sstevel@tonic-gate 		return (1);
269*7c478bd9Sstevel@tonic-gate }
270