chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / audit-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2010 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <linux/netlink.h>
8 #include <stdio.h>
9 #include <sys/socket.h>
10
11 #include "alloc-util.h"
12 #include "audit-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "macro.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "user-util.h"
19
20 int audit_session_from_pid(pid_t pid, uint32_t *id) {
21         _cleanup_free_ char *s = NULL;
22         const char *p;
23         uint32_t u;
24         int r;
25
26         assert(id);
27
28         /* We don't convert ENOENT to ESRCH here, since we can't
29          * really distuingish between "audit is not available in the
30          * kernel" and "the process does not exist", both which will
31          * result in ENOENT. */
32
33         p = procfs_file_alloca(pid, "sessionid");
34
35         r = read_one_line_file(p, &s);
36         if (r < 0)
37                 return r;
38
39         r = safe_atou32(s, &u);
40         if (r < 0)
41                 return r;
42
43         if (!audit_session_is_valid(u))
44                 return -ENODATA;
45
46         *id = u;
47         return 0;
48 }
49
50 int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
51         _cleanup_free_ char *s = NULL;
52         const char *p;
53         uid_t u;
54         int r;
55
56         assert(uid);
57
58         p = procfs_file_alloca(pid, "loginuid");
59
60         r = read_one_line_file(p, &s);
61         if (r < 0)
62                 return r;
63
64         r = parse_uid(s, &u);
65         if (r == -ENXIO) /* the UID was -1 */
66                 return -ENODATA;
67         if (r < 0)
68                 return r;
69
70         *uid = u;
71         return 0;
72 }
73
74 #if 0 /// UNNEEDED by elogind
75 bool use_audit(void) {
76         static int cached_use = -1;
77
78         if (cached_use < 0) {
79                 int fd;
80
81                 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
82                 if (fd < 0) {
83                         cached_use = !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT, EPERM);
84                         if (!cached_use)
85                                 log_debug_errno(errno, "Won't talk to audit: %m");
86                 } else {
87                         cached_use = true;
88                         safe_close(fd);
89                 }
90         }
91
92         return cached_use;
93 }
94 #endif // 0