audit-consolidate-open-20081217
[openafs.git] / src / audit / audit.c
index e310bad..2cd5d5b 100644 (file)
@@ -16,6 +16,9 @@ RCSID
 #include <fcntl.h>
 #include <stdarg.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #ifdef AFS_AIX32_ENV
 #include <sys/audit.h>
 #else
@@ -97,11 +100,6 @@ audmakebuf(char *audEvent, va_list vaList)
            *(afs_int32 *) bufferPtr = vaLong;
            bufferPtr += sizeof(vaLong);
            break;
-       case AUD_LST:           /* Ptr to another list */
-           va_copy(vaLst, vaList);
-           audmakebuf(audEvent, vaLst);
-           va_end(vaLst);
-           break;
        case AUD_FID:           /* AFSFid - contains 3 entries */
            vaFid = (struct AFSFid *)va_arg(vaList, struct AFSFid *);
            if (vaFid) {
@@ -148,7 +146,8 @@ audmakebuf(char *audEvent, va_list vaList)
 }
 
 static void
-printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
+printbuf(FILE *out, int rec, char *audEvent, char *afsName, afs_int32 hostId, 
+        afs_int32 errCode, va_list vaList)
 {
     int vaEntry;
     int vaInt;
@@ -175,8 +174,12 @@ printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
            fprintf(out, "[%d] ", num);
     }
     
-    if (strcmp(audEvent, "VALST") != 0)
-       fprintf(out,  "EVENT %s CODE %d ", audEvent, errCode);
+    fprintf(out,  "EVENT %s CODE %d ", audEvent, errCode);
+
+    if (afsName) {
+       hostAddr.s_addr = hostId;
+       fprintf(out,  "NAME %s HOST %s ", afsName, inet_ntoa(hostAddr));
+    }
 
     vaEntry = va_arg(vaList, int);
     while (vaEntry != AUD_END) {
@@ -223,11 +226,6 @@ printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
            vaLong = va_arg(vaList, afs_int32);
            fprintf(out, "LONG %d ", vaLong);
            break;
-       case AUD_LST:           /* Ptr to another list */
-           va_copy(vaLst, vaList);
-           printbuf(out, 1, "VALST", 0, vaLst);
-           va_end(vaLst);
-           break;
        case AUD_FID:           /* AFSFid - contains 3 entries */
            vaFid = va_arg(vaList, struct AFSFid *);
            if (vaFid)
@@ -238,24 +236,21 @@ printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
            break;
        case AUD_FIDS:          /* array of Fids */
            vaFids = va_arg(vaList, struct AFSCBFids *);
-           vaFid = NULL;
 
            if (vaFids) {
                 int i;
-                if (vaFid)
+                
+                vaFid = vaFids->AFSCBFids_val;
+                
+                if (vaFid) {
                     fprintf(out, "FIDS %u FID %u:%u:%u ", vaFids->AFSCBFids_len, vaFid->Volume,
                              vaFid->Vnode, vaFid->Unique);
-                else
+                    for ( i = 1; i < vaFids->AFSCBFids_len; i++, vaFid++ ) 
+                        fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
+                                vaFid->Vnode, vaFid->Unique);
+                } else
                     fprintf(out, "FIDS 0 FID 0:0:0 ");
 
-                for ( i = 1; i < vaFids->AFSCBFids_len; i++ ) {
-                    vaFid = vaFids->AFSCBFids_val;
-                    if (vaFid)
-                        fprintf(out, "FID %u:%u:%u ", vaFid->Volume,
-                                 vaFid->Vnode, vaFid->Unique);
-                    else
-                        fprintf(out, "FID 0:0:0 ");
-                }
             }
            break;
        default:
@@ -265,8 +260,7 @@ printbuf(FILE *out, int rec, char *audEvent, afs_int32 errCode, va_list vaList)
        vaEntry = va_arg(vaList, int);
     }                          /* end while */
 
-    if (strcmp(audEvent, "VALST") != 0)
-       fprintf(out, "\n");
+    fprintf(out, "\n");
 }
 
 #ifdef AFS_PTHREAD_ENV
@@ -296,9 +290,11 @@ osi_audit_init(void)
 /* The routine that acually does the audit call.
  * ************************************************************************** */
 int
-osi_audit(char *audEvent,      /* Event name (15 chars or less) */
-         afs_int32 errCode,    /* The error code */
-         ...)
+osi_audit_internal(char *audEvent,     /* Event name (15 chars or less) */
+                  afs_int32 errCode,   /* The error code */
+                  char *afsName,
+                  afs_int32 hostId,
+                  va_list vaList)
 {
 #ifdef AFS_AIX32_ENV
     afs_int32 code;
@@ -306,7 +302,7 @@ osi_audit(char *audEvent,   /* Event name (15 chars or less) */
     static char BUFFER[32768];
 #endif
     int result;
-    va_list vaList;
+    va_list vaCopy;
 
 #ifdef AFS_PTHREAD_ENV
     /* i'm pretty sure all the server apps now call osi_audit_init(),
@@ -320,6 +316,8 @@ osi_audit(char *audEvent,   /* Event name (15 chars or less) */
     if (!osi_audit_all && !auditout)
        return 0;
 
+    va_copy(vaCopy, vaList);
+
     switch (errCode) {
     case 0:
        result = AUDIT_OK;
@@ -354,30 +352,20 @@ osi_audit(char *audEvent, /* Event name (15 chars or less) */
     *(int *)bufferPtr = errCode;
     bufferPtr += sizeof(errCode);
 
-    va_start(vaList, errCode);
     audmakebuf(audEvent, vaList);
 #endif
 
     if (osi_echo_trail) {
-       va_start(vaList, errCode);
-       printbuf(stdout, 0, audEvent, errCode, vaList);
+       printbuf(stdout, 0, audEvent, afsName, hostId, errCode, vaList);
     }
+    va_end(vaCopy);
 
 #ifdef AFS_AIX32_ENV
     bufferLen = (int)((afs_int32) bufferPtr - (afs_int32) & BUFFER[0]);
     code = auditlog(audEvent, result, BUFFER, bufferLen);
-#ifdef notdef
-    if (code) {
-       err = errno;
-       code = auditlog("AFS_Aud_Fail", result, &err, sizeof(err));
-       if (code)
-           printf("Error while writing audit entry: %d.\n", errno);
-    }
-#endif /* notdef */
 #else
     if (auditout) {
-       va_start(vaList, errCode);
-       printbuf(auditout, 0, audEvent, errCode, vaList);
+       printbuf(auditout, 0, audEvent, afsName, hostId, errCode, vaList);
        fflush(auditout);
     }
 #endif
@@ -387,6 +375,24 @@ osi_audit(char *audEvent,  /* Event name (15 chars or less) */
 
     return 0;
 }
+int
+osi_audit(char *audEvent,      /* Event name (15 chars or less) */
+         afs_int32 errCode,    /* The error code */
+         ...)
+{
+    va_list vaList;
+
+    if ((osi_audit_all < 0) || (osi_echo_trail < 0))
+       osi_audit_check();
+    if (!osi_audit_all && !auditout)
+       return 0;
+
+    va_start(vaList, errCode);
+    osi_audit_internal(audEvent, errCode, NULL, 0, vaList);
+    va_end(vaList);
+
+    return 0;
+}
 
 /* ************************************************************************** */
 /* Given a RPC call structure, this routine extracts the name and host id from the 
@@ -451,7 +457,7 @@ osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
                                    break;
                            }
 
-                           if (i=0)
+                           if (i == 0)
                                strncpy(local_realms[0], "UNKNOWN.LOCAL.REALM", AFS_REALM_SZ);
                            num_lrealms = i;
                         }
@@ -504,9 +510,8 @@ osi_auditU(struct rx_call *call, char *audEvent, int errCode, ...)
        osi_audit("AFS_Aud_NoCall", (-1), AUD_STR, audEvent, AUD_END);
     }
     va_start(vaList, errCode);
-    osi_audit(audEvent, errCode, AUD_NAME, afsName, AUD_HOST, hostId, 
-              AUD_LST, vaList, AUD_END);
-
+    osi_audit_internal(audEvent, errCode, afsName, hostId, vaList);
+    va_end(vaList);
     return 0;
 }
 
@@ -553,8 +558,35 @@ osi_audit_check()
 }
 
 int
-osi_audit_file(FILE *out)
+osi_audit_file(char *fileName)
 {
-    auditout = out;
+    int tempfd, flags;
+    char oldName[MAXPATHLEN];
+    
+#ifndef AFS_NT40_ENV
+    struct stat statbuf;
+    
+    if ((lstat(fileName, &statbuf) == 0)
+        && (S_ISFIFO(statbuf.st_mode))) {
+        flags = O_WRONLY | O_NONBLOCK;
+    } else 
+#endif
+    {
+        strcpy(oldName, fileName);
+        strcat(oldName, ".old");
+        renamefile(fileName, oldName);
+        flags = O_WRONLY | O_TRUNC | O_CREAT;
+    }
+    tempfd = open(fileName, flags, 0666);
+    if (tempfd > -1) {
+        auditout = fdopen(tempfd, "a");
+        if (!auditout) {
+            printf("Warning: auditlog %s not writable, ignored.\n", fileName);
+            return 1;
+        }
+    } else { 
+        printf("Warning: auditlog %s not writable, ignored.\n", fileName);
+        return 1;
+    }
     return 0;
 }