Line data Source code
1 : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 :
3 : /***
4 : This file is part of systemd.
5 :
6 : Copyright 2010 Lennart Poettering
7 :
8 : systemd is free software; you can redistribute it and/or modify it
9 : under the terms of the GNU Lesser General Public License as published by
10 : the Free Software Foundation; either version 2.1 of the License, or
11 : (at your option) any later version.
12 :
13 : systemd is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : Lesser General Public License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 : ***/
21 :
22 : #include <errno.h>
23 : #include <stdio.h>
24 :
25 : #include "macro.h"
26 : #include "audit.h"
27 : #include "util.h"
28 : #include "process-util.h"
29 : #include "fileio.h"
30 :
31 2 : int audit_session_from_pid(pid_t pid, uint32_t *id) {
32 4 : _cleanup_free_ char *s = NULL;
33 : const char *p;
34 : uint32_t u;
35 : int r;
36 :
37 2 : assert(id);
38 :
39 2 : p = procfs_file_alloca(pid, "sessionid");
40 :
41 2 : r = read_one_line_file(p, &s);
42 2 : if (r < 0)
43 2 : return r;
44 :
45 0 : r = safe_atou32(s, &u);
46 0 : if (r < 0)
47 0 : return r;
48 :
49 0 : if (u == AUDIT_SESSION_INVALID || u <= 0)
50 0 : return -ENXIO;
51 :
52 0 : *id = u;
53 0 : return 0;
54 : }
55 :
56 2 : int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
57 4 : _cleanup_free_ char *s = NULL;
58 : const char *p;
59 : uid_t u;
60 : int r;
61 :
62 2 : assert(uid);
63 :
64 2 : p = procfs_file_alloca(pid, "loginuid");
65 :
66 2 : r = read_one_line_file(p, &s);
67 2 : if (r < 0)
68 2 : return r;
69 :
70 0 : r = parse_uid(s, &u);
71 0 : if (r < 0)
72 0 : return r;
73 :
74 0 : *uid = (uid_t) u;
75 0 : return 0;
76 : }
77 :
78 2 : bool use_audit(void) {
79 : static int cached_use = -1;
80 :
81 2 : if (cached_use < 0) {
82 : int fd;
83 :
84 1 : fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
85 1 : if (fd < 0)
86 1 : cached_use = errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT;
87 : else {
88 0 : cached_use = true;
89 0 : safe_close(fd);
90 : }
91 : }
92 :
93 2 : return cached_use;
94 : }
|