Imported Upstream version 2.4.3
[platform/upstream/audit.git] / bindings / golang / audit.go
1 package audit
2
3 /*
4   The audit package is a go bindings to libaudit that only allows for
5   logging audit events.
6
7   Author Steve Grubb <sgrubb@redhat.com>
8
9 */
10
11 // #cgo pkg-config: audit
12 // #include "libaudit.h"
13 // #include <unistd.h>
14 // #include <stdlib.h>
15 // #include <string.h>
16 // #include <stdio.h>
17 import "C"
18
19 import (
20         "unsafe"
21 )
22
23 const (
24         AUDIT_VIRT_CONTROL    = 2500
25         AUDIT_VIRT_RESOURCE   = 2501
26         AUDIT_VIRT_MACHINE_ID = 2502
27 )
28
29 // type=VIRT_CONTROL msg=audit(08/05/2014 17:01:05.891:6471) : pid=1265 uid=root auid=unset ses=unset subj=system_u:system_r:virtd_t:s0-s0:c0.c1023 msg='virt=kvm op=start reason=booted vm=vm1 uuid=462dcd6d-fb68-4a26-a96f-56eb024515b9 vm-pid=22527 exe=/usr/sbin/libvirtd hostname=? addr=? terminal=? res=success'
30
31 func AuditValueNeedsEncoding(str string) bool {
32         cstr := C.CString(str)
33         defer C.free(unsafe.Pointer(cstr))
34         len := C.strlen(cstr)
35
36         res, _ := C.audit_value_needs_encoding(cstr, C.uint(len))
37         if res != 0 {
38                 return true
39         }
40         return false
41 }
42
43 func AuditEncodeNVString(name string, value string) string {
44         cname := C.CString(name)
45         cval := C.CString(value)
46
47         cres := C.audit_encode_nv_string(cname, cval, 0)
48
49         C.free(unsafe.Pointer(cname))
50         C.free(unsafe.Pointer(cval))
51         defer C.free(unsafe.Pointer(cres))
52
53         return C.GoString(cres)
54 }
55
56 func AuditLogUserEvent(event_type int, message string, result bool) error {
57         var r int
58         fd := C.audit_open()
59         if result {
60                 r = 1
61         } else {
62                 r = 0
63         }
64         if fd > 0 {
65                 cmsg := C.CString(message)
66                 _, err := C.audit_log_user_message(fd, C.int(event_type), cmsg, nil, nil, nil, C.int(r))
67                 C.free(unsafe.Pointer(cmsg))
68                 C.close(fd)
69                 return err
70         }
71         return nil
72 }