]>
Commit | Line | Data |
---|---|---|
663996b3 MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
663996b3 | 20 | #include <errno.h> |
4c89c718 | 21 | #include <linux/netlink.h> |
663996b3 | 22 | #include <stdio.h> |
4c89c718 | 23 | #include <sys/socket.h> |
663996b3 | 24 | |
db2df898 MP |
25 | #include "alloc-util.h" |
26 | #include "audit-util.h" | |
27 | #include "fd-util.h" | |
28 | #include "fileio.h" | |
663996b3 | 29 | #include "macro.h" |
db2df898 | 30 | #include "parse-util.h" |
e3bff60a | 31 | #include "process-util.h" |
db2df898 | 32 | #include "user-util.h" |
663996b3 MS |
33 | |
34 | int audit_session_from_pid(pid_t pid, uint32_t *id) { | |
60f067b4 JS |
35 | _cleanup_free_ char *s = NULL; |
36 | const char *p; | |
663996b3 MS |
37 | uint32_t u; |
38 | int r; | |
39 | ||
40 | assert(id); | |
41 | ||
d9dfd233 MP |
42 | /* We don't convert ENOENT to ESRCH here, since we can't |
43 | * really distuingish between "audit is not available in the | |
44 | * kernel" and "the process does not exist", both which will | |
45 | * result in ENOENT. */ | |
46 | ||
60f067b4 | 47 | p = procfs_file_alloca(pid, "sessionid"); |
663996b3 | 48 | |
60f067b4 | 49 | r = read_one_line_file(p, &s); |
663996b3 MS |
50 | if (r < 0) |
51 | return r; | |
52 | ||
53 | r = safe_atou32(s, &u); | |
663996b3 MS |
54 | if (r < 0) |
55 | return r; | |
56 | ||
e3bff60a | 57 | if (u == AUDIT_SESSION_INVALID || u <= 0) |
d9dfd233 | 58 | return -ENODATA; |
663996b3 MS |
59 | |
60 | *id = u; | |
61 | return 0; | |
62 | } | |
63 | ||
64 | int audit_loginuid_from_pid(pid_t pid, uid_t *uid) { | |
60f067b4 JS |
65 | _cleanup_free_ char *s = NULL; |
66 | const char *p; | |
663996b3 MS |
67 | uid_t u; |
68 | int r; | |
69 | ||
70 | assert(uid); | |
71 | ||
60f067b4 | 72 | p = procfs_file_alloca(pid, "loginuid"); |
663996b3 | 73 | |
60f067b4 | 74 | r = read_one_line_file(p, &s); |
663996b3 MS |
75 | if (r < 0) |
76 | return r; | |
77 | ||
78 | r = parse_uid(s, &u); | |
d9dfd233 MP |
79 | if (r == -ENXIO) /* the UID was -1 */ |
80 | return -ENODATA; | |
663996b3 MS |
81 | if (r < 0) |
82 | return r; | |
83 | ||
663996b3 MS |
84 | *uid = (uid_t) u; |
85 | return 0; | |
86 | } | |
f47781d8 MP |
87 | |
88 | bool use_audit(void) { | |
89 | static int cached_use = -1; | |
90 | ||
91 | if (cached_use < 0) { | |
92 | int fd; | |
93 | ||
94 | fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT); | |
95 | if (fd < 0) | |
96 | cached_use = errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT; | |
97 | else { | |
98 | cached_use = true; | |
99 | safe_close(fd); | |
100 | } | |
101 | } | |
102 | ||
103 | return cached_use; | |
104 | } |