Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/iolog/iolog_open.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2009-2021 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 <stdio.h>
22
#include <stdlib.h>
23
#ifdef HAVE_STDBOOL_H
24
# include <stdbool.h>
25
#else
26
# include <compat/stdbool.h>
27
#endif
28
#include <errno.h>
29
#include <fcntl.h>
30
#include <time.h>
31
#include <unistd.h>
32
33
#include <sudo_compat.h>
34
#include <sudo_debug.h>
35
#include <sudo_iolog.h>
36
#include <sudo_util.h>
37
38
static unsigned char const gzip_magic[2] = {0x1f, 0x8b};
39
40
/*
41
* Open the specified I/O log file and store in iol.
42
* Stores the open file handle which has the close-on-exec flag set.
43
* Also locks the file if iofd is IOFD_TIMING and mode is writable.
44
* The "r+" and "w+" modes are not supported for compressed logs
45
* so the "+" will be stripped before calling gzdopen().
46
*/
47
bool
48
iolog_open(struct iolog_file *iol, int dfd, int iofd, const char *mode)
49
{
50
int flags;
51
const char *file;
52
bool lockit = false;
53
unsigned char magic[2];
54
const uid_t iolog_uid = iolog_get_uid();
55
const gid_t iolog_gid = iolog_get_gid();
56
debug_decl(iolog_open, SUDO_DEBUG_UTIL);
57
58
if (mode[0] == 'r') {
59
flags = mode[1] == '+' ? O_RDWR : O_RDONLY;
60
} else if (mode[0] == 'w') {
61
flags = O_CREAT|O_TRUNC;
62
flags |= mode[1] == '+' ? O_RDWR : O_WRONLY;
63
} else {
64
sudo_debug_printf(SUDO_DEBUG_ERROR,
65
"%s: invalid I/O mode %s", __func__, mode);
66
debug_return_bool(false);
67
}
68
if ((file = iolog_fd_to_name(iofd)) == NULL) {
69
sudo_debug_printf(SUDO_DEBUG_ERROR,
70
"%s: invalid iofd %d", __func__, iofd);
71
debug_return_bool(false);
72
}
73
74
/* Lock the timing file if opening for writing. */
75
if (iofd == IOFD_TIMING && (mode[0] == 'w' || mode[1] == '+')) {
76
lockit = true;
77
}
78
79
iol->writable = false;
80
iol->compressed = false;
81
iol->locked = false;
82
if (iol->enabled) {
83
int fd = iolog_openat(dfd, file, flags);
84
if (lockit && fd != -1) {
85
if (sudo_lock_file(fd, SUDO_TLOCK)) {
86
iol->locked = true;
87
} else {
88
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
89
"%s: unable to lock %s", __func__, file);
90
close(fd);
91
fd = -1;
92
}
93
}
94
if (fd != -1) {
95
if (*mode == 'w') {
96
if (fchown(fd, iolog_uid, iolog_gid) != 0) {
97
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
98
"%s: unable to fchown %d:%d %s", __func__,
99
(int)iolog_uid, (int)iolog_gid, file);
100
}
101
iol->compressed = iolog_get_compress();
102
} else {
103
/* check for gzip magic number */
104
if (pread(fd, magic, sizeof(magic), 0) == ssizeof(magic)) {
105
if (magic[0] == gzip_magic[0] && magic[1] == gzip_magic[1])
106
iol->compressed = true;
107
}
108
}
109
/*
110
* Compressed logs don't support random access, gzdopen() will
111
* fail for mode "r+". Caller must check the compressed flag.
112
*/
113
if (iol->compressed)
114
mode = *mode == 'r' ? "r" : "w";
115
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1) {
116
#ifdef HAVE_ZLIB_H
117
if (iol->compressed)
118
iol->fd.g = gzdopen(fd, mode);
119
else
120
#endif
121
iol->fd.f = fdopen(fd, mode);
122
}
123
if (iol->fd.v != NULL) {
124
switch ((flags & O_ACCMODE)) {
125
case O_WRONLY:
126
case O_RDWR:
127
iol->writable = true;
128
break;
129
}
130
} else {
131
int save_errno = errno;
132
close(fd);
133
errno = save_errno;
134
fd = -1;
135
}
136
}
137
if (fd == -1) {
138
iol->enabled = false;
139
debug_return_bool(false);
140
}
141
} else {
142
if (*mode == 'w') {
143
/* Remove old log file in case we recycled sequence numbers. */
144
(void)unlinkat(dfd, file, 0);
145
}
146
}
147
debug_return_bool(true);
148
}
149
150