Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/sudoers/boottime.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-2015, 2018 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <sys/types.h> /* for size_t, ssize_t */
22
#include <sys/time.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <errno.h>
28
#include <limits.h>
29
#include <time.h>
30
#ifndef __linux__
31
# if defined(HAVE_SYS_SYSCTL_H)
32
# include <sys/sysctl.h>
33
# elif defined(HAVE_GETUTXID)
34
# include <utmpx.h>
35
# elif defined(HAVE_GETUTID)
36
# include <utmp.h>
37
# endif
38
#endif /* !__linux__ */
39
40
#include <sudoers.h>
41
42
/*
43
* Fill in a struct timespec with the time the system booted.
44
* Returns 1 on success and 0 on failure.
45
*/
46
47
#if defined(__linux__)
48
bool
49
get_boottime(struct timespec *ts)
50
{
51
char *line = NULL;
52
size_t linesize = 0;
53
bool found = false;
54
long long llval;
55
ssize_t len;
56
FILE *fp;
57
debug_decl(get_boottime, SUDOERS_DEBUG_UTIL);
58
59
/* read btime from /proc/stat */
60
fp = fopen("/proc/stat", "r");
61
if (fp != NULL) {
62
while ((len = getdelim(&line, &linesize, '\n', fp)) != -1) {
63
if (strncmp(line, "btime ", 6) == 0) {
64
if (line[len - 1] == '\n')
65
line[len - 1] = '\0';
66
llval = sudo_strtonum(line + 6, 1, LLONG_MAX, NULL);
67
if (llval > 0) {
68
ts->tv_sec = (time_t)llval;
69
ts->tv_nsec = 0;
70
found = true;
71
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
72
"found btime in /proc/stat: %lld", llval);
73
break;
74
} else {
75
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
76
"invalid btime in /proc/stat: %s", line);
77
}
78
}
79
}
80
fclose(fp);
81
free(line);
82
}
83
84
debug_return_bool(found);
85
}
86
87
#elif defined(HAVE_SYSCTL) && defined(KERN_BOOTTIME)
88
89
bool
90
get_boottime(struct timespec *ts)
91
{
92
size_t size;
93
int mib[2];
94
struct timeval tv;
95
debug_decl(get_boottime, SUDOERS_DEBUG_UTIL);
96
97
mib[0] = CTL_KERN;
98
mib[1] = KERN_BOOTTIME;
99
size = sizeof(tv);
100
if (sysctl(mib, 2, &tv, &size, NULL, 0) != -1) {
101
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
102
"KERN_BOOTTIME: %lld, %ld", (long long)tv.tv_sec, (long)tv.tv_usec);
103
TIMEVAL_TO_TIMESPEC(&tv, ts);
104
debug_return_bool(true);
105
}
106
107
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
108
"KERN_BOOTTIME: %s", strerror(errno));
109
debug_return_bool(false);
110
}
111
112
#elif defined(HAVE_GETUTXID)
113
114
bool
115
get_boottime(struct timespec *ts)
116
{
117
struct utmpx *ut, key;
118
debug_decl(get_boottime, SUDOERS_DEBUG_UTIL);
119
120
memset(&key, 0, sizeof(key));
121
key.ut_type = BOOT_TIME;
122
setutxent();
123
if ((ut = getutxid(&key)) != NULL) {
124
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
125
"BOOT_TIME: %lld, %ld", (long long)ut->ut_tv.tv_sec,
126
(long)ut->ut_tv.tv_usec);
127
TIMEVAL_TO_TIMESPEC(&ut->ut_tv, ts);
128
}
129
endutxent();
130
debug_return_bool(ut != NULL);
131
}
132
133
#elif defined(HAVE_GETUTID)
134
135
bool
136
get_boottime(struct timespec *ts)
137
{
138
struct utmp *ut, key;
139
debug_decl(get_boottime, SUDOERS_DEBUG_UTIL);
140
141
memset(&key, 0, sizeof(key));
142
key.ut_type = BOOT_TIME;
143
setutent();
144
if ((ut = getutid(&key)) != NULL) {
145
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
146
"BOOT_TIME: %lld", (long long)ut->ut_time);
147
ts->tv_sec = ut->ut_time;
148
ts->tv_nsec = 0;
149
}
150
endutent();
151
debug_return_bool(ut != NULL);
152
}
153
154
#else
155
156
bool
157
get_boottime(struct timespec *ts)
158
{
159
debug_decl(get_boottime, SUDOERS_DEBUG_UTIL);
160
debug_return_bool(false);
161
}
162
#endif
163
164