File:  [Power 6/32 Unix Tahoe 4.2BSD] / cci / usr / src / usr.bin / audfmt.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Sun Jul 28 12:24:19 2019 UTC (7 years ago) by root
Branches: bsd, MAIN
CVS tags: v12b, v121, HEAD
Power 6/32 Unix version 1.2b

/*
 *
 *$Header: /var/lib/cvsd/repos/cci/cci/usr/src/usr.bin/audfmt.c,v 1.1.1.1 2019/07/28 12:24:19 root Exp $
 *
 *$Log: audfmt.c,v $
 *Revision 1.1.1.1  2019/07/28 12:24:19  root
 *Power 6/32 Unix version 1.2b
 *
 * Revision 1.1  85/07/08  10:24:44  root
 * Initial revision
 * 
 * Revision 1.1  84/04/19  08:00:54  brian
 * Initial revision
 * 
 */

static char rcsid[] = "$Header: /var/lib/cvsd/repos/cci/cci/usr/src/usr.bin/audfmt.c,v 1.1.1.1 2019/07/28 12:24:19 root Exp $";

#define VAX
#include <sys/param.h>
#include <sys/dir.h>
#include <stdio.h>
#include <signal.h>
#include <sys/audit.h>
#include <sys/time.h>
#include <errno.h>
#include <pwd.h>

#define NOUSER "invalid uid"
#define ROOT 0
#ifdef VAX
#define PATHLEN MAXPATHLEN
#else
#include <sys/stat.h>
#define PATHLEN 512
#endif

int	uid;
int	audfno;
char 	result[PATHLEN];
char	name[PATHLEN];
#define AUDFILELOG "/usr/adm/audfile.log"
char	*audfile	= "/usr/adm/audit";
char	*outfile = NULL;
struct aud_rec	audrec;
struct aud_rec *abp;
extern int errno;

char logpath[PATHLEN];
char buf[1024];



main(argc,argv)
int argc;
char **argv;
{
    register int n, m, i, num, logfd;
    int fdout = 1;
    register long check, offseek;
    char string[250];
    register char c;
    char *getwd();
    extern int optind;
    extern char *optarg;
    register char *bufp;

	if( (uid=getuid()) != ROOT)
	{
			fprintf(stderr,"%s: must be superuser\n",argv[0]);
			exit(1);
	}

    if(argc > 1)
    {
	while((c = getopt(argc,argv,"o:")) != EOF)
	{
	   switch(c){
		case 'o':
			outfile = optarg;
                        break;
		default:
			printf("usage: audfmt [-o outfile] [file]\n");
			exit(8);
           }
	}
        if(optind < argc)
	{
		if(*(argv[optind]) != '/')
		{
			audfile = getwd(name);
			if(strcmp(audfile,"/") != 0)
				strncat(audfile,"/",PATHLEN);
			strncat(audfile,argv[optind],PATHLEN);
		}
		else
			audfile = argv[optind];
	}
	if(outfile != NULL)
	{
                if((fdout=open(outfile,2)) < 0){
                    if((errno & ENOENT) == ENOENT){
                        if((fdout=creat(outfile,0666)) < 0){
                            fprintf(stderr,"creat: cannot create %s\n",outfile);
                            exit(9);
                        }
                    }
		    else 
		        perror(open);
	        }
	}
    }
	if((logfd = open(AUDFILELOG,0)) < 0)
	{
		if(errno != ENOENT)
		{
			fprintf(stderr,"%s: can't open %s\n",argv[0],AUDFILELOG);
			exit(8);
		}
	}
	else
	{
		if(read(logfd,logpath,PATHLEN) < 0)
		{
			fprintf(stderr,"%s: can't read %s\n",argv[0],AUDFILELOG);
			exit(8);
		}
		if(strcmp(logpath,audfile) == 0)
		{
			fprintf(stderr,"%s: %s is the active log file\n",argv[0],audfile);
			exit(8);
		}
	}
	close(logfd);

	if((audfno = open(audfile,0)) < 0){
			fprintf(stderr,"Can't open %s\n",audfile);
			exit(8);
		}

	offseek = 0;
	while((n = read(audfno,buf,sizeof(audrec))) != 0){
                if(n < 0){
			fprintf(stderr,"read error");
			perror(read);
                        }
                bufp = buf;
                m = audout(bufp,fdout);

                offseek = offseek + m;
               if(( check=lseek(audfno,offseek,0)) < 0){
                        fprintf(stderr,"lseek: error\n\n");
			perror(lseek);
                }
	}

close(audfno);
}


audout(bp,fd)
int fd;
char *bp;
{
    register short *sp;
    register long *lp;
    register char *cp;
    register int i = 0;
    register int num, j;
    char string[250];
    char *p;
	abp = &audrec;
	sp = (short *)bp;
	abp->a_flag = *sp;
        bp += 2;
	i += 2;
	sp = (short *)bp;
	abp->a_uid = *sp;
        bp += 2;
	i +=  2;
        lp = (long *)bp;
	abp->a_time = *lp;
        i += 4;
        bp += 4;
        cp = abp->a_comm;
	while(*bp != '\0'){
		*cp++ = *bp++;
		i++;
	}                          /*end of command string*/
        *cp++ = '\0';
        i++;
	bp++;
	cp = abp->a_path;
	while(*bp != '\0'){
		*cp++ = *bp++;
		i++;
	}
	*cp++ = '\0';
	bp++;
        i++;
	num = audfrmt(&audrec,string);
	write(fd,string,num);

	return(i);             /*returns number of bytes processed */
}




audfrmt(recp,strp)

register struct aud_rec *recp;	/* ptr to audit record */
register char *strp; 		/* ptr to buffer to be stuffed */

{
struct passwd *passp;	/* ptr to passwd structure; for getpwuid */
register int num = 0;
register char *cp, *slashp;
char *ctime();
struct passwd *getpwuid();


/*
*	Flags section
*/
if((recp->a_flag & AUDREAD) == AUDREAD){
	*strp++ = 'R';
        num++;
	}
else if((recp->a_flag & AUDWRITE) == AUDWRITE){
	*strp++ = 'W';
        num++;
	}
else if ((recp->a_flag & AUDEXEC) == AUDEXEC){
	*strp++ = 'E';
	num++;
	}
else if ((recp->a_flag & AUDUNLINK) == AUDUNLINK){
	*strp++ = 'L';
	num++;
	}
else if(( recp->a_flag & AUDOFFTON) == AUDOFFTON){
	*strp++ = 'N';
	num++;
	}
else if((recp->a_flag & AUDONTOFF) == AUDONTOFF){
	*strp++ = 'F';
	num++;
	}
else{
	*strp++ = ' ';
        num++;
	}
if((recp->a_flag & AUDSUCCESS) == AUDSUCCESS){
	*strp++ = 'S';
	num++;
	}
else{
	*strp++ = 'U';
	num++;
	}

*strp++ = ' ';
num++;
/*
 * 	User name section
 */
if (( passp = getpwuid(recp->a_uid)) != (struct passwd *) NULL){
	cp = passp->pw_name;
	while(*cp != '\0'){
		*strp++ = *cp++;
		num++;
		}
	}
else{
        cp = NOUSER;
	while(*cp != '\0'){
        	*strp++ = *cp++;
		num++;
		}
	}
*strp++ = ' ';
num++;
/*
*	Date\time section
*/
	cp = ctime(&(recp->a_time));
	while(*cp != '\n'){
		*strp++ = *cp++;
		num++;
	        }
	*strp++ = ' ';
	num++;
/*
*	command name section
*/
cp = recp->a_comm;
while(*cp != '\0'){
	*strp++ = *cp++;
	num++;
	}
*strp++ = ' ';
num++;
/*
*	Path Name section
*/

cp = recp->a_path;
while(*cp != '\0'){
        if (*cp == '/'){
                *strp++ = *cp++;
                slashp = cp;

                if(*slashp == '.' && *(slashp+1) == '/'){

                        cp += 2;
                }

               else if (*slashp =='.' && *(slashp+1) == '.' &&
                       *(slashp+2) == '/'){
                       cp += 2;
                       strp -= 2;
                       num -= 2;
                       while(*strp != '/'){
                               strp--;
                               num--;
                       }

               }

        }
        else
        	*strp++ = *cp++;
        num++;
   }
*strp++ = '\n';
num++;
*strp++ = '\0';
num++;
return(num);
}

#ifdef POWER5

struct	stat	d, dd;
struct	direct	dir;

char	dot[]	 = ".";
char	dotdot[] = "..";

int	file;
int	off = 0;


cat(path)
register char *path;
{
	register i, j;

	i = -1;
	while ((dir.d_name[++i] != 0) && (i < 14));
	if ((off+i+2) > (PATHLEN-1))
		return;
	for(j=off+1; j>=0; --j)
		path[j+i+1] = path[j];
	off=i+off+1;
	path[i] = '/';
	for(--i; i>=0; --i)
		path[i] = dir.d_name[i];
}
#endif
/*	@(#)getopt.c	1.5	*/
/*	3.0 SID #	1.2	*/
/*LINTLIBRARY*/
#define NULL	0
#define EOF	(-1)
#define ERR(s, c)	if(opterr){\
	extern int strlen(), write();\
	char errbuf[2];\
	errbuf[0] = c; errbuf[1] = '\n';\
	(void) write(2, argv[0], (unsigned)strlen(argv[0]));\
	(void) write(2, s, (unsigned)strlen(s));\
	(void) write(2, errbuf, 2);}

extern int strcmp();
extern char *strchr();

int	opterr = 1;
int	optind = 1;
int	optopt;
char	*optarg;

int
getopt(argc, argv, opts)
int	argc;
char	**argv, *opts;
{
	static int sp = 1;
	register int c;
	register char *cp;

	if(sp == 1)
		if(optind >= argc ||
		   argv[optind][0] != '-' || argv[optind][1] == '\0')
			return(EOF);
		else if(strcmp(argv[optind], "--") == NULL) {
			optind++;
			return(EOF);
		}
	optopt = c = argv[optind][sp];
	if(c == ':' || (cp=strchr(opts, c)) == NULL) {
		ERR(": illegal option -- ", c);
		if(argv[optind][++sp] == '\0') {
			optind++;
			sp = 1;
		}
		return('?');
	}
	if(*++cp == ':') {
		if(argv[optind][sp+1] != '\0')
			optarg = &argv[optind++][sp+1];
		else if(++optind >= argc) {
			ERR(": option requires an argument -- ", c);
			sp = 1;
			return('?');
		} else
			optarg = argv[optind++];
		sp = 1;
	} else {
		if(argv[optind][++sp] == '\0') {
			sp = 1;
			optind++;
		}
		optarg = NULL;
	}
	return(c);
}

/*	@(#)strchr.c	1.2	*/
/*	3.0 SID #	1.2	*/
/*LINTLIBRARY*/
/*
 * Return the ptr in sp at which the character c appears;
 * NULL if not found
 */

#define	NULL	0

char *
strchr(sp, c)
register char *sp, c;
{
	do {
		if(*sp == c)
			return(sp);
	} while(*sp++);
	return(NULL);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.