Annotation of cci/usr/src/etc/auditon.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * $Header: auditon.c,v 1.1 85/07/08 08:29:15 root Exp $
        !             3:  *
        !             4:  * $Log:       auditon.c,v $
        !             5:  * Revision 1.1  85/07/08  08:29:15  root
        !             6:  * Initial revision
        !             7:  * 
        !             8:  * Revision 1.4  84/10/08  16:33:44  guy
        !             9:  * Changed to print the reason a system call failed.
        !            10:  * 
        !            11:  * Revision 1.3  84/03/28  09:38:25  root
        !            12:  * Added a -q (query) option to allow the user to ask the current audit trail
        !            13:  * log file. Also made it complain about attempts to log to a directory.
        !            14:  * 
        !            15:  * Revision 1.2  84/03/10  17:00:28  brian
        !            16:  * Added code to log the currentldy active log file.
        !            17:  * 
        !            18:  */
        !            19: 
        !            20: static char rcsid[] = "$Header: auditon.c,v 1.1 85/07/08 08:29:15 root Exp $";
        !            21: 
        !            22: /*
        !            23:  *     auditon - calls 'audon' syscall with super-user privileges
        !            24:  */
        !            25: #include <stdio.h>
        !            26: #include <sys/param.h>
        !            27: #include <sys/stat.h>
        !            28: #include <sys/dir.h>
        !            29: #include <errno.h>
        !            30: 
        !            31: #define        ROOT 0
        !            32: #define        ERROR   -1
        !            33: #define PATHLEN 512
        !            34: #define DEFLTAUD "/usr/adm/audit"
        !            35: #define AUDFILELOG "/usr/adm/audfile.log"
        !            36: char *audfile;
        !            37: struct stat buf;
        !            38: int    uid;
        !            39: 
        !            40: struct stat    d, dd;
        !            41: struct direct  dir;
        !            42: 
        !            43: char   dot[]    = ".";
        !            44: char   dotdot[] = "..";
        !            45: char   result[PATHLEN];
        !            46: 
        !            47: char   name[PATHLEN];
        !            48: int    file;
        !            49: int    off = 0;
        !            50: int    optind;
        !            51: 
        !            52: main(argc,argv)
        !            53: int argc;
        !            54: char **argv;
        !            55: {
        !            56: register int fd;
        !            57: register char c;
        !            58: extern int errno;
        !            59: char *getwd();
        !            60:        if( (uid=getuid()) != ROOT)
        !            61:        {
        !            62:                        fprintf(stderr,"%s: must be superuser\n",argv[0]);
        !            63:                        exit(1);
        !            64:        }
        !            65: 
        !            66:        while((c = getopt(argc,argv,"q")) != EOF)
        !            67:        {
        !            68:                switch (c)
        !            69:                {
        !            70:                        case 'q':
        !            71:                                query();
        !            72:                                exit(0);
        !            73:                        default:
        !            74:                                printf("usage: auditon [-q] [file]\n");
        !            75:                                exit(1);
        !            76:                }
        !            77:        }
        !            78:        if(optind < argc )
        !            79:        {
        !            80:                if(*(argv[optind]) != '/')
        !            81:                {
        !            82:                        audfile = getwd(name);
        !            83:                        if(strcmp(audfile,"/") != 0)
        !            84:                                strncat(audfile,"/",PATHLEN);
        !            85:                        strncat(audfile,argv[optind],PATHLEN);
        !            86:                }
        !            87:                else
        !            88:                        audfile = argv[optind];
        !            89:        }
        !            90:        else
        !            91:                audfile = DEFLTAUD;
        !            92: 
        !            93:        if(stat(audfile,&buf) == ERROR) {
        !            94:                 if(errno == ENOENT){
        !            95:                        if(creat(audfile,0600) == ERROR){
        !            96:                                fprintf(stderr,"%s: cannot create %s\n",
        !            97:                                                        argv[0],audfile);
        !            98:                                exit(8);
        !            99:                        }
        !           100:                 }
        !           101:                 else{
        !           102:                fprintf(stderr,"%s: cannot open ",argv[0]);
        !           103:                perror(audfile);
        !           104:                exit(1);
        !           105:                }
        !           106:        }
        !           107:        if((buf.st_mode & S_IFMT) == S_IFDIR)
        !           108:        {
        !           109:                fprintf(stderr,"%s: %s is a directory\n",argv[0],audfile);
        !           110:                exit(1);
        !           111:        }
        !           112: 
        !           113:                if(audon(audfile) == ERROR) {   
        !           114:                        if(errno == EBUSY)
        !           115:                                fprintf(stderr,"%s: already logging to %s\n",argv[0],audfile);
        !           116:                        else
        !           117:                        {
        !           118:                                perror(argv[0]);
        !           119:                                fprintf(stderr,"%s: auditing not enabled\n",argv[0]);
        !           120:                        }
        !           121:                        exit(1);
        !           122:                }
        !           123: 
        !           124:                if((fd = creat(AUDFILELOG,0600)) == ERROR){
        !           125:                        fprintf(stderr,"%s: cannot create %s\n",
        !           126:                                                argv[0],AUDFILELOG);
        !           127:                        audon((char *) NULL);
        !           128:                        fprintf(stderr,"%s: auditing not enabled\n",argv[0]);
        !           129:                        exit(8);
        !           130:                }
        !           131:                if(write(fd,audfile,strlen(audfile)) < strlen(audfile))
        !           132:                {
        !           133:                        fprintf(stderr,"%s: cannot write to %s\n",
        !           134:                                                argv[0],AUDFILELOG);
        !           135:                        audon((char *) NULL);
        !           136:                        fprintf(stderr,"%s: auditing not enabled\n",argv[0]);
        !           137:                        exit(8);
        !           138:                }
        !           139:                fprintf(stderr,"%s: auditing ON.\tFile used = %s\n\n",argv[0],audfile);
        !           140: }
        !           141: 
        !           142: /*
        !           143:  *     Report currently active audit trail logfile
        !           144:  */
        !           145: 
        !           146: query()
        !           147: {
        !           148:     register int fd, cnt;
        !           149:     extern int errno;
        !           150:     char buf[PATHLEN];
        !           151: 
        !           152:        if((fd = open(AUDFILELOG,0)) == ERROR) 
        !           153:        {
        !           154:                 if(errno == ENOENT)
        !           155:                {
        !           156:                        printf("auditon: Audit trail facility currently not enabled\n");
        !           157:                        return;
        !           158:                 }
        !           159:                else
        !           160:                {
        !           161:                        fprintf(stderr,"auditon: cannot open %s\n", AUDFILELOG);
        !           162:                        exit(8);
        !           163:                }
        !           164:        }
        !           165:        if((cnt = read(fd,buf,PATHLEN)) == ERROR)
        !           166:        {
        !           167:                fprintf(stderr,"auditon: cannot read %s\n", AUDFILELOG);
        !           168:                exit(8);
        !           169:        }
        !           170:        buf[cnt] = '\0';        /* terminate string with null */
        !           171:        printf("auditon: The current audit trail log file is %s\n",buf);
        !           172:        return;
        !           173: }
        !           174: 
        !           175: 
        !           176: 
        !           177: cat(path)
        !           178: register char *path;
        !           179: {
        !           180:        register i, j;
        !           181: 
        !           182:        i = -1;
        !           183:        while ((dir.d_name[++i] != 0) && (i < 14));
        !           184:        if ((off+i+2) > (PATHLEN-1))
        !           185:                return;
        !           186:        for(j=off+1; j>=0; --j)
        !           187:                path[j+i+1] = path[j];
        !           188:        off=i+off+1;
        !           189:        path[i] = '/';
        !           190:        for(--i; i>=0; --i)
        !           191:                path[i] = dir.d_name[i];
        !           192: }
        !           193: /*     @(#)getopt.c    1.5     */
        !           194: /*     3.0 SID #       1.2     */
        !           195: /*LINTLIBRARY*/
        !           196: #define NULL   0
        !           197: #define EOF    (-1)
        !           198: #define ERR(s, c)      if(opterr){\
        !           199:        extern int strlen(), write();\
        !           200:        char errbuf[2];\
        !           201:        errbuf[0] = c; errbuf[1] = '\n';\
        !           202:        (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
        !           203:        (void) write(2, s, (unsigned)strlen(s));\
        !           204:        (void) write(2, errbuf, 2);}
        !           205: 
        !           206: extern int strcmp();
        !           207: extern char *strchr();
        !           208: 
        !           209: int    opterr = 1;
        !           210: int    optopt;
        !           211: int    optind = 1;
        !           212: char   *optarg;
        !           213: 
        !           214: int
        !           215: getopt(argc, argv, opts)
        !           216: int    argc;
        !           217: char   **argv, *opts;
        !           218: {
        !           219:        static int sp = 1;
        !           220:        register int c;
        !           221:        register char *cp;
        !           222: 
        !           223:        if(sp == 1)
        !           224:                if(optind >= argc ||
        !           225:                   argv[optind][0] != '-' || argv[optind][1] == '\0')
        !           226:                        return(EOF);
        !           227:                else if(strcmp(argv[optind], "--") == NULL) {
        !           228:                        optind++;
        !           229:                        return(EOF);
        !           230:                }
        !           231:        optopt = c = argv[optind][sp];
        !           232:        if(c == ':' || (cp=strchr(opts, c)) == NULL) {
        !           233:                ERR(": illegal option -- ", c);
        !           234:                if(argv[optind][++sp] == '\0') {
        !           235:                        optind++;
        !           236:                        sp = 1;
        !           237:                }
        !           238:                return('?');
        !           239:        }
        !           240:        if(*++cp == ':') {
        !           241:                if(argv[optind][sp+1] != '\0')
        !           242:                        optarg = &argv[optind++][sp+1];
        !           243:                else if(++optind >= argc) {
        !           244:                        ERR(": option requires an argument -- ", c);
        !           245:                        sp = 1;
        !           246:                        return('?');
        !           247:                } else
        !           248:                        optarg = argv[optind++];
        !           249:                sp = 1;
        !           250:        } else {
        !           251:                if(argv[optind][++sp] == '\0') {
        !           252:                        sp = 1;
        !           253:                        optind++;
        !           254:                }
        !           255:                optarg = NULL;
        !           256:        }
        !           257:        return(c);
        !           258: }
        !           259: /*     @(#)strchr.c    1.2     */
        !           260: /*     3.0 SID #       1.2     */
        !           261: /*LINTLIBRARY*/
        !           262: /*
        !           263:  * Return the ptr in sp at which the character c appears;
        !           264:  * NULL if not found
        !           265:  */
        !           266: 
        !           267: #define        NULL    0
        !           268: 
        !           269: char *
        !           270: strchr(sp, c)
        !           271: register char *sp, c;
        !           272: {
        !           273:        do {
        !           274:                if(*sp == c)
        !           275:                        return(sp);
        !           276:        } while(*sp++);
        !           277:        return(NULL);
        !           278: }

unix.superglobalmegacorp.com

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