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