|
|
1.1 ! root 1: /* ! 2: * ! 3: *$Header: audfmt.c,v 1.1 85/07/08 10:24:44 root Exp $ ! 4: * ! 5: *$Log: audfmt.c,v $ ! 6: * Revision 1.1 85/07/08 10:24:44 root ! 7: * Initial revision ! 8: * ! 9: * Revision 1.1 84/04/19 08:00:54 brian ! 10: * Initial revision ! 11: * ! 12: */ ! 13: ! 14: static char rcsid[] = "$Header: audfmt.c,v 1.1 85/07/08 10:24:44 root Exp $"; ! 15: ! 16: #define VAX ! 17: #include <sys/param.h> ! 18: #include <sys/dir.h> ! 19: #include <stdio.h> ! 20: #include <signal.h> ! 21: #include <sys/audit.h> ! 22: #include <sys/time.h> ! 23: #include <errno.h> ! 24: #include <pwd.h> ! 25: ! 26: #define NOUSER "invalid uid" ! 27: #define ROOT 0 ! 28: #ifdef VAX ! 29: #define PATHLEN MAXPATHLEN ! 30: #else ! 31: #include <sys/stat.h> ! 32: #define PATHLEN 512 ! 33: #endif ! 34: ! 35: int uid; ! 36: int audfno; ! 37: char result[PATHLEN]; ! 38: char name[PATHLEN]; ! 39: #define AUDFILELOG "/usr/adm/audfile.log" ! 40: char *audfile = "/usr/adm/audit"; ! 41: char *outfile = NULL; ! 42: struct aud_rec audrec; ! 43: struct aud_rec *abp; ! 44: extern int errno; ! 45: ! 46: char logpath[PATHLEN]; ! 47: char buf[1024]; ! 48: ! 49: ! 50: ! 51: main(argc,argv) ! 52: int argc; ! 53: char **argv; ! 54: { ! 55: register int n, m, i, num, logfd; ! 56: int fdout = 1; ! 57: register long check, offseek; ! 58: char string[250]; ! 59: register char c; ! 60: char *getwd(); ! 61: extern int optind; ! 62: extern char *optarg; ! 63: register char *bufp; ! 64: ! 65: if( (uid=getuid()) != ROOT) ! 66: { ! 67: fprintf(stderr,"%s: must be superuser\n",argv[0]); ! 68: exit(1); ! 69: } ! 70: ! 71: if(argc > 1) ! 72: { ! 73: while((c = getopt(argc,argv,"o:")) != EOF) ! 74: { ! 75: switch(c){ ! 76: case 'o': ! 77: outfile = optarg; ! 78: break; ! 79: default: ! 80: printf("usage: audfmt [-o outfile] [file]\n"); ! 81: exit(8); ! 82: } ! 83: } ! 84: if(optind < argc) ! 85: { ! 86: if(*(argv[optind]) != '/') ! 87: { ! 88: audfile = getwd(name); ! 89: if(strcmp(audfile,"/") != 0) ! 90: strncat(audfile,"/",PATHLEN); ! 91: strncat(audfile,argv[optind],PATHLEN); ! 92: } ! 93: else ! 94: audfile = argv[optind]; ! 95: } ! 96: if(outfile != NULL) ! 97: { ! 98: if((fdout=open(outfile,2)) < 0){ ! 99: if((errno & ENOENT) == ENOENT){ ! 100: if((fdout=creat(outfile,0666)) < 0){ ! 101: fprintf(stderr,"creat: cannot create %s\n",outfile); ! 102: exit(9); ! 103: } ! 104: } ! 105: else ! 106: perror(open); ! 107: } ! 108: } ! 109: } ! 110: if((logfd = open(AUDFILELOG,0)) < 0) ! 111: { ! 112: if(errno != ENOENT) ! 113: { ! 114: fprintf(stderr,"%s: can't open %s\n",argv[0],AUDFILELOG); ! 115: exit(8); ! 116: } ! 117: } ! 118: else ! 119: { ! 120: if(read(logfd,logpath,PATHLEN) < 0) ! 121: { ! 122: fprintf(stderr,"%s: can't read %s\n",argv[0],AUDFILELOG); ! 123: exit(8); ! 124: } ! 125: if(strcmp(logpath,audfile) == 0) ! 126: { ! 127: fprintf(stderr,"%s: %s is the active log file\n",argv[0],audfile); ! 128: exit(8); ! 129: } ! 130: } ! 131: close(logfd); ! 132: ! 133: if((audfno = open(audfile,0)) < 0){ ! 134: fprintf(stderr,"Can't open %s\n",audfile); ! 135: exit(8); ! 136: } ! 137: ! 138: offseek = 0; ! 139: while((n = read(audfno,buf,sizeof(audrec))) != 0){ ! 140: if(n < 0){ ! 141: fprintf(stderr,"read error"); ! 142: perror(read); ! 143: } ! 144: bufp = buf; ! 145: m = audout(bufp,fdout); ! 146: ! 147: offseek = offseek + m; ! 148: if(( check=lseek(audfno,offseek,0)) < 0){ ! 149: fprintf(stderr,"lseek: error\n\n"); ! 150: perror(lseek); ! 151: } ! 152: } ! 153: ! 154: close(audfno); ! 155: } ! 156: ! 157: ! 158: audout(bp,fd) ! 159: int fd; ! 160: char *bp; ! 161: { ! 162: register short *sp; ! 163: register long *lp; ! 164: register char *cp; ! 165: register int i = 0; ! 166: register int num, j; ! 167: char string[250]; ! 168: char *p; ! 169: abp = &audrec; ! 170: sp = (short *)bp; ! 171: abp->a_flag = *sp; ! 172: bp += 2; ! 173: i += 2; ! 174: sp = (short *)bp; ! 175: abp->a_uid = *sp; ! 176: bp += 2; ! 177: i += 2; ! 178: lp = (long *)bp; ! 179: abp->a_time = *lp; ! 180: i += 4; ! 181: bp += 4; ! 182: cp = abp->a_comm; ! 183: while(*bp != '\0'){ ! 184: *cp++ = *bp++; ! 185: i++; ! 186: } /*end of command string*/ ! 187: *cp++ = '\0'; ! 188: i++; ! 189: bp++; ! 190: cp = abp->a_path; ! 191: while(*bp != '\0'){ ! 192: *cp++ = *bp++; ! 193: i++; ! 194: } ! 195: *cp++ = '\0'; ! 196: bp++; ! 197: i++; ! 198: num = audfrmt(&audrec,string); ! 199: write(fd,string,num); ! 200: ! 201: return(i); /*returns number of bytes processed */ ! 202: } ! 203: ! 204: ! 205: ! 206: ! 207: audfrmt(recp,strp) ! 208: ! 209: register struct aud_rec *recp; /* ptr to audit record */ ! 210: register char *strp; /* ptr to buffer to be stuffed */ ! 211: ! 212: { ! 213: struct passwd *passp; /* ptr to passwd structure; for getpwuid */ ! 214: register int num = 0; ! 215: register char *cp, *slashp; ! 216: char *ctime(); ! 217: struct passwd *getpwuid(); ! 218: ! 219: ! 220: /* ! 221: * Flags section ! 222: */ ! 223: if((recp->a_flag & AUDREAD) == AUDREAD){ ! 224: *strp++ = 'R'; ! 225: num++; ! 226: } ! 227: else if((recp->a_flag & AUDWRITE) == AUDWRITE){ ! 228: *strp++ = 'W'; ! 229: num++; ! 230: } ! 231: else if ((recp->a_flag & AUDEXEC) == AUDEXEC){ ! 232: *strp++ = 'E'; ! 233: num++; ! 234: } ! 235: else if ((recp->a_flag & AUDUNLINK) == AUDUNLINK){ ! 236: *strp++ = 'L'; ! 237: num++; ! 238: } ! 239: else if(( recp->a_flag & AUDOFFTON) == AUDOFFTON){ ! 240: *strp++ = 'N'; ! 241: num++; ! 242: } ! 243: else if((recp->a_flag & AUDONTOFF) == AUDONTOFF){ ! 244: *strp++ = 'F'; ! 245: num++; ! 246: } ! 247: else{ ! 248: *strp++ = ' '; ! 249: num++; ! 250: } ! 251: if((recp->a_flag & AUDSUCCESS) == AUDSUCCESS){ ! 252: *strp++ = 'S'; ! 253: num++; ! 254: } ! 255: else{ ! 256: *strp++ = 'U'; ! 257: num++; ! 258: } ! 259: ! 260: *strp++ = ' '; ! 261: num++; ! 262: /* ! 263: * User name section ! 264: */ ! 265: if (( passp = getpwuid(recp->a_uid)) != (struct passwd *) NULL){ ! 266: cp = passp->pw_name; ! 267: while(*cp != '\0'){ ! 268: *strp++ = *cp++; ! 269: num++; ! 270: } ! 271: } ! 272: else{ ! 273: cp = NOUSER; ! 274: while(*cp != '\0'){ ! 275: *strp++ = *cp++; ! 276: num++; ! 277: } ! 278: } ! 279: *strp++ = ' '; ! 280: num++; ! 281: /* ! 282: * Date\time section ! 283: */ ! 284: cp = ctime(&(recp->a_time)); ! 285: while(*cp != '\n'){ ! 286: *strp++ = *cp++; ! 287: num++; ! 288: } ! 289: *strp++ = ' '; ! 290: num++; ! 291: /* ! 292: * command name section ! 293: */ ! 294: cp = recp->a_comm; ! 295: while(*cp != '\0'){ ! 296: *strp++ = *cp++; ! 297: num++; ! 298: } ! 299: *strp++ = ' '; ! 300: num++; ! 301: /* ! 302: * Path Name section ! 303: */ ! 304: ! 305: cp = recp->a_path; ! 306: while(*cp != '\0'){ ! 307: if (*cp == '/'){ ! 308: *strp++ = *cp++; ! 309: slashp = cp; ! 310: ! 311: if(*slashp == '.' && *(slashp+1) == '/'){ ! 312: ! 313: cp += 2; ! 314: } ! 315: ! 316: else if (*slashp =='.' && *(slashp+1) == '.' && ! 317: *(slashp+2) == '/'){ ! 318: cp += 2; ! 319: strp -= 2; ! 320: num -= 2; ! 321: while(*strp != '/'){ ! 322: strp--; ! 323: num--; ! 324: } ! 325: ! 326: } ! 327: ! 328: } ! 329: else ! 330: *strp++ = *cp++; ! 331: num++; ! 332: } ! 333: *strp++ = '\n'; ! 334: num++; ! 335: *strp++ = '\0'; ! 336: num++; ! 337: return(num); ! 338: } ! 339: ! 340: #ifdef POWER5 ! 341: ! 342: struct stat d, dd; ! 343: struct direct dir; ! 344: ! 345: char dot[] = "."; ! 346: char dotdot[] = ".."; ! 347: ! 348: int file; ! 349: int off = 0; ! 350: ! 351: ! 352: cat(path) ! 353: register char *path; ! 354: { ! 355: register i, j; ! 356: ! 357: i = -1; ! 358: while ((dir.d_name[++i] != 0) && (i < 14)); ! 359: if ((off+i+2) > (PATHLEN-1)) ! 360: return; ! 361: for(j=off+1; j>=0; --j) ! 362: path[j+i+1] = path[j]; ! 363: off=i+off+1; ! 364: path[i] = '/'; ! 365: for(--i; i>=0; --i) ! 366: path[i] = dir.d_name[i]; ! 367: } ! 368: #endif ! 369: /* @(#)getopt.c 1.5 */ ! 370: /* 3.0 SID # 1.2 */ ! 371: /*LINTLIBRARY*/ ! 372: #define NULL 0 ! 373: #define EOF (-1) ! 374: #define ERR(s, c) if(opterr){\ ! 375: extern int strlen(), write();\ ! 376: char errbuf[2];\ ! 377: errbuf[0] = c; errbuf[1] = '\n';\ ! 378: (void) write(2, argv[0], (unsigned)strlen(argv[0]));\ ! 379: (void) write(2, s, (unsigned)strlen(s));\ ! 380: (void) write(2, errbuf, 2);} ! 381: ! 382: extern int strcmp(); ! 383: extern char *strchr(); ! 384: ! 385: int opterr = 1; ! 386: int optind = 1; ! 387: int optopt; ! 388: char *optarg; ! 389: ! 390: int ! 391: getopt(argc, argv, opts) ! 392: int argc; ! 393: char **argv, *opts; ! 394: { ! 395: static int sp = 1; ! 396: register int c; ! 397: register char *cp; ! 398: ! 399: if(sp == 1) ! 400: if(optind >= argc || ! 401: argv[optind][0] != '-' || argv[optind][1] == '\0') ! 402: return(EOF); ! 403: else if(strcmp(argv[optind], "--") == NULL) { ! 404: optind++; ! 405: return(EOF); ! 406: } ! 407: optopt = c = argv[optind][sp]; ! 408: if(c == ':' || (cp=strchr(opts, c)) == NULL) { ! 409: ERR(": illegal option -- ", c); ! 410: if(argv[optind][++sp] == '\0') { ! 411: optind++; ! 412: sp = 1; ! 413: } ! 414: return('?'); ! 415: } ! 416: if(*++cp == ':') { ! 417: if(argv[optind][sp+1] != '\0') ! 418: optarg = &argv[optind++][sp+1]; ! 419: else if(++optind >= argc) { ! 420: ERR(": option requires an argument -- ", c); ! 421: sp = 1; ! 422: return('?'); ! 423: } else ! 424: optarg = argv[optind++]; ! 425: sp = 1; ! 426: } else { ! 427: if(argv[optind][++sp] == '\0') { ! 428: sp = 1; ! 429: optind++; ! 430: } ! 431: optarg = NULL; ! 432: } ! 433: return(c); ! 434: } ! 435: ! 436: /* @(#)strchr.c 1.2 */ ! 437: /* 3.0 SID # 1.2 */ ! 438: /*LINTLIBRARY*/ ! 439: /* ! 440: * Return the ptr in sp at which the character c appears; ! 441: * NULL if not found ! 442: */ ! 443: ! 444: #define NULL 0 ! 445: ! 446: char * ! 447: strchr(sp, c) ! 448: register char *sp, c; ! 449: { ! 450: do { ! 451: if(*sp == c) ! 452: return(sp); ! 453: } while(*sp++); ! 454: return(NULL); ! 455: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.