10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*11861SMarek.Pospisil@Sun.COM * Common Development and Distribution License (the "License").
6*11861SMarek.Pospisil@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11861SMarek.Pospisil@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * @(#)audit_path.c 2.7 92/02/16 SMI; SunOS CMW
280Sstevel@tonic-gate * @(#)audit_path.c 4.2.1.2 91/05/08 SMI; BSM Module
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * This code does the audit path processes. Part of this is still in
310Sstevel@tonic-gate * audit.c and will be moved here when time permits.
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * Note that audit debuging is enabled here. We will turn it off at
340Sstevel@tonic-gate * beta shipment.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/user.h>
410Sstevel@tonic-gate #include <sys/vnode.h>
420Sstevel@tonic-gate #include <sys/vfs.h>
430Sstevel@tonic-gate #include <sys/kmem.h> /* for KM_SLEEP */
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/uio.h>
460Sstevel@tonic-gate #include <sys/file.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/pathname.h>
490Sstevel@tonic-gate #include <sys/acct.h>
500Sstevel@tonic-gate #include <c2/audit.h>
510Sstevel@tonic-gate #include <c2/audit_kernel.h>
520Sstevel@tonic-gate #include <c2/audit_record.h>
530Sstevel@tonic-gate #include <sys/sysmacros.h>
540Sstevel@tonic-gate #include <sys/atomic.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate
570Sstevel@tonic-gate int
au_token_size(m)580Sstevel@tonic-gate au_token_size(m)
590Sstevel@tonic-gate token_t *m;
600Sstevel@tonic-gate {
610Sstevel@tonic-gate int i;
620Sstevel@tonic-gate
630Sstevel@tonic-gate if (m == (token_t *)0)
640Sstevel@tonic-gate return (0);
650Sstevel@tonic-gate
660Sstevel@tonic-gate for (i = 0; m != (token_t *)0; m = m->next_buf)
670Sstevel@tonic-gate i += m->len;
680Sstevel@tonic-gate return (i);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate token_t *
au_set(cp,size)720Sstevel@tonic-gate au_set(cp, size)
730Sstevel@tonic-gate caddr_t cp;
740Sstevel@tonic-gate uint_t size;
750Sstevel@tonic-gate {
760Sstevel@tonic-gate au_buff_t *head;
770Sstevel@tonic-gate au_buff_t *tail;
780Sstevel@tonic-gate au_buff_t *m;
790Sstevel@tonic-gate uint_t l;
800Sstevel@tonic-gate
810Sstevel@tonic-gate head = NULL;
820Sstevel@tonic-gate tail = NULL; /* only to satisfy lint */
830Sstevel@tonic-gate
840Sstevel@tonic-gate while (size) {
850Sstevel@tonic-gate m = au_get_buff();
860Sstevel@tonic-gate l = MIN(size, AU_BUFSIZE);
870Sstevel@tonic-gate bcopy(cp, memtod(m, char *), l);
880Sstevel@tonic-gate m->len = l;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (head)
910Sstevel@tonic-gate tail->next_buf = m; /* tail set if head set */
920Sstevel@tonic-gate else
930Sstevel@tonic-gate head = m;
940Sstevel@tonic-gate tail = m;
950Sstevel@tonic-gate size -= l;
960Sstevel@tonic-gate cp += l;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate return (head);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate token_t *
au_append_token(chain,m)1030Sstevel@tonic-gate au_append_token(chain, m)
1040Sstevel@tonic-gate token_t *chain;
1050Sstevel@tonic-gate token_t *m;
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate token_t *mbp;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (chain == (token_t *)0)
1100Sstevel@tonic-gate return (m);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate if (m == (token_t *)0)
1130Sstevel@tonic-gate return (chain);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate for (mbp = chain; mbp->next_buf != (token_t *)0; mbp = mbp->next_buf)
1160Sstevel@tonic-gate ;
1170Sstevel@tonic-gate mbp->next_buf = m;
1180Sstevel@tonic-gate return (chain);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate void
audit_fixpath(struct audit_path * app,int len)1230Sstevel@tonic-gate audit_fixpath(struct audit_path *app, int len)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate int id; /* index of where we are in destination string */
1260Sstevel@tonic-gate int is; /* index of where we are in source string */
1270Sstevel@tonic-gate int cnt; /* # of levels in audit_path */
1280Sstevel@tonic-gate int slashseen; /* have we seen a slash */
1290Sstevel@tonic-gate char *s; /* start of top-level string */
1300Sstevel@tonic-gate char c;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate cnt = app->audp_cnt;
1330Sstevel@tonic-gate s = app->audp_sect[cnt - 1];
1340Sstevel@tonic-gate is = (app->audp_sect[cnt] - s) - len;
1350Sstevel@tonic-gate if (is <= 2)
1360Sstevel@tonic-gate is = 0; /* catch leading // or ./ */
1370Sstevel@tonic-gate slashseen = (is > 0);
1380Sstevel@tonic-gate for (id = is; ; is++) {
1390Sstevel@tonic-gate if ((c = s[is]) == '\0') {
1400Sstevel@tonic-gate /* that's all folks, we've reached the end of input */
1410Sstevel@tonic-gate if (id > 1 && s[id-1] == '/') {
1420Sstevel@tonic-gate /* remove terminating / */
1430Sstevel@tonic-gate --id;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate s[id++] = '\0';
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if (slashseen) {
1490Sstevel@tonic-gate /* previous character was a / */
1500Sstevel@tonic-gate if (c == '/') {
1510Sstevel@tonic-gate /* another slash, ignore it */
1520Sstevel@tonic-gate continue;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate } else if (c == '/') {
1550Sstevel@tonic-gate /* we see a /, just copy it and try again */
1560Sstevel@tonic-gate slashseen = 1;
1570Sstevel@tonic-gate s[id++] = c;
1580Sstevel@tonic-gate continue;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate if (c == '.') {
1610Sstevel@tonic-gate if ((c = s[is+1]) == '\0') {
1620Sstevel@tonic-gate /* XXX/. seen */
1630Sstevel@tonic-gate if (id > 1)
1640Sstevel@tonic-gate id--;
1650Sstevel@tonic-gate continue;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate if (c == '/') {
1680Sstevel@tonic-gate /* XXX/./ seen */
1690Sstevel@tonic-gate is += 1;
1700Sstevel@tonic-gate continue;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate if (c == '.' && (s[is+2] == '\0' || s[is+2] == '/')) {
1730Sstevel@tonic-gate /* XXX/.. or XXX/../ seen */
1740Sstevel@tonic-gate is++;
1750Sstevel@tonic-gate if (id == 0 && cnt > 1) {
1760Sstevel@tonic-gate char *s_attr;
1770Sstevel@tonic-gate /* .. refers to attributed object */
1780Sstevel@tonic-gate app->audp_cnt = --cnt;
1790Sstevel@tonic-gate s_attr = s;
1800Sstevel@tonic-gate s = app->audp_sect[cnt - 1];
1810Sstevel@tonic-gate id = s_attr - s;
1820Sstevel@tonic-gate is += id;
1830Sstevel@tonic-gate id--;
1840Sstevel@tonic-gate slashseen = 0;
1850Sstevel@tonic-gate continue;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate /* backup over previous component */
1880Sstevel@tonic-gate if (id > 0)
1890Sstevel@tonic-gate id--;
1900Sstevel@tonic-gate while (id > 0 && s[id - 1] != '/')
1910Sstevel@tonic-gate id--;
1920Sstevel@tonic-gate continue;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate /* copy component name and terminating /, if any */
1960Sstevel@tonic-gate for (;;) {
1970Sstevel@tonic-gate c = s[is++];
1980Sstevel@tonic-gate if (c == '\0' || c == '/')
1990Sstevel@tonic-gate break;
2000Sstevel@tonic-gate s[id++] = c;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate /* back up to before terminating '\0' or / */
2030Sstevel@tonic-gate slashseen = 0;
2040Sstevel@tonic-gate is -= 2;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate /* fill empty attribute directory reference */
2070Sstevel@tonic-gate if (id == 1 && cnt > 1) {
2080Sstevel@tonic-gate s[0] = '.';
2090Sstevel@tonic-gate s[1] = '\0';
2100Sstevel@tonic-gate id = 2;
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate /* correct end pointer */
2130Sstevel@tonic-gate app->audp_sect[cnt] = s + id;
2140Sstevel@tonic-gate }
215