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