audit: Support multiple audit interfaces and interface options
[openafs.git] / src / audit / audit-file.c
index 3c3c8d2..490e498 100644 (file)
 
 #include "audit-api.h"
 
-static FILE *auditout;
+struct file_context {
+    FILE *auditout;
+};
 
 static void
-send_msg(void)
+send_msg(void *rock, const char *message, int msglen, int truncated)
 {
-    fprintf(auditout, "\n");
-    fflush(auditout);
-}
+    struct file_context *ctx = rock;
 
-static void
-append_msg(const char *format, ...)
-{
-    va_list vaList;
+    fwrite(message, 1, msglen, ctx->auditout);
+    fputc('\n', ctx->auditout);
 
-    va_start(vaList, format);
-    vfprintf(auditout, format, vaList);
-    va_end(vaList);
+    fflush(ctx->auditout);
 }
 
 static int
-open_file(const char *fileName)
+open_file(void *rock, const char *fileName)
 {
-    int tempfd, flags;
-    char oldName[MAXPATHLEN];
+    struct file_context *ctx = rock;
+    int tempfd, flags, r;
+    char *oldName;
 
 #ifndef AFS_NT40_ENV
     struct stat statbuf;
@@ -50,16 +47,20 @@ open_file(const char *fileName)
     } else
 #endif
     {
-        strcpy(oldName, fileName);
-        strcat(oldName, ".old");
-        renamefile(fileName, oldName);
+       r = asprintf(&oldName, "%s.old", fileName);
+       if (r < 0 || oldName == NULL) {
+           printf("Warning: Unable to create backup filename. Auditing ignored\n");
+           return 1;
+       }
+        rk_rename(fileName, oldName);
         flags = O_WRONLY | O_TRUNC | O_CREAT;
+       free(oldName);
     }
     tempfd = open(fileName, flags, 0666);
     if (tempfd > -1) {
-        auditout = fdopen(tempfd, "a");
-        if (!auditout) {
-            printf("Warning: auditlog %s not writable, ignored.\n", fileName);
+       ctx->auditout = fdopen(tempfd, "a");
+       if (!ctx->auditout) {
+           printf("Warning: fdopen failed for auditlog %s, ignored.\n", fileName);
             return 1;
         }
     } else {
@@ -70,14 +71,41 @@ open_file(const char *fileName)
 }
 
 static void
-print_interface_stats(FILE *out)
+print_interface_stats(void *rock, FILE *out)
 {
     return;
 }
 
+static void *
+create_interface(void)
+{
+    struct file_context *ctx;
+    ctx = calloc(1, sizeof(*ctx));
+    if (!ctx) {
+       printf("error allocating memory\n");
+       return NULL;
+    }
+    return ctx;
+}
+
+static void
+close_interface(void **rock)
+{
+    struct file_context *ctx = *rock;
+    if (!ctx)
+       return;
+
+    if (ctx->auditout)
+       fclose(ctx->auditout);
+    free(ctx);
+    *rock = NULL;
+}
 const struct osi_audit_ops audit_file_ops = {
     &send_msg,
-    &append_msg,
     &open_file,
     &print_interface_stats,
+    &create_interface,
+    &close_interface,
+    NULL,    /* set_option */
+    NULL,    /* open_interface */
 };